From ab9b1ee0118ae3722d1d7041e1315690086375bd Mon Sep 17 00:00:00 2001 From: Tobias Reich Date: Wed, 2 Apr 2014 22:14:20 +0200 Subject: [PATCH] Improved plugins --- php/access/admin.php | 7 +- php/access/guest.php | 3 +- php/api.php | 4 +- php/modules/Albums.php | 117 ++++++++++++++++++++++++++ php/modules/Plugins.php | 23 +++-- php/modules/{album.php => _album.php} | 0 6 files changed, 140 insertions(+), 14 deletions(-) create mode 100644 php/modules/Albums.php rename php/modules/{album.php => _album.php} (100%) diff --git a/php/access/admin.php b/php/access/admin.php index f6c11dd..2d96dac 100644 --- a/php/access/admin.php +++ b/php/access/admin.php @@ -13,15 +13,16 @@ switch ($_POST['function']) { // Album Functions - case 'getAlbums': echo json_encode(getAlbums(false)); + case 'getAlbums': $album = new Albums($database, $plugins, $settings, null); + echo json_encode($album->getAll(false)); break; case 'getAlbum': if (isset($_POST['albumID'])) echo json_encode(getAlbum($_POST['albumID'])); break; - case 'addAlbum': if (isset($_POST['title'])) - echo addAlbum($_POST['title']); + case 'addAlbum': $album = new Albums($database, $plugins, $settings, null); + echo $album->add($_POST['title']); break; case 'setAlbumTitle': if (isset($_POST['albumIDs'], $_POST['title'])) diff --git a/php/access/guest.php b/php/access/guest.php index 1810c8f..130ac27 100644 --- a/php/access/guest.php +++ b/php/access/guest.php @@ -13,7 +13,8 @@ switch ($_POST['function']) { // Album Functions - case 'getAlbums': echo json_encode(getAlbums(true)); + case 'getAlbums': $album = new Albums($database, $plugins, $settings, null); + echo json_encode($album->getAll(true)); break; case 'getAlbum': if (isset($_POST['albumID'], $_POST['password'])) { diff --git a/php/api.php b/php/api.php index f409791..f1aa6bf 100755 --- a/php/api.php +++ b/php/api.php @@ -22,7 +22,7 @@ if (!empty($_POST['function'])||!empty($_GET['function'])) { require('autoload.php'); // Load modules - require('modules/album.php'); + require('modules/_album.php'); require('modules/db.php'); require('modules/misc.php'); require('modules/photo.php'); @@ -50,7 +50,7 @@ if (!empty($_POST['function'])||!empty($_GET['function'])) { // Init plugins $plugins = explode(';', $settings['plugins']); - $plugins = new Plugins($plugins); + $plugins = new Plugins($plugins, $database); // Escape foreach(array_keys($_POST) as $key) $_POST[$key] = mysqli_real_escape_string($database, urldecode($_POST[$key])); diff --git a/php/modules/Albums.php b/php/modules/Albums.php new file mode 100644 index 0000000..0e611a7 --- /dev/null +++ b/php/modules/Albums.php @@ -0,0 +1,117 @@ +database = $database; + $this->plugins = $plugins; + $this->settings = $settings; + $this->albumIDs = $albumIDs; + + return true; + + } + + private function plugins($action, $args) { + + if (!isset($this->plugins, $action, $args)) return false; + + # Call plugins + $this->plugins->activate("Albums:$action", $args); + + return true; + + } + + public function add($title = 'Untitled', $public = 0, $visible = 1) { + + if (!isset($this->database)) return false; + + # Call plugins + $this->plugins('add:before', func_get_args()); + + # Parse + if (strlen($title)>50) $title = substr($title, 0, 50); + + # Database + $sysdate = date('d.m.Y'); + $result = $this->database->query("INSERT INTO lychee_albums (title, sysdate, public, visible) VALUES ('$title', '$sysdate', '$public', '$visible');"); + + # Call plugins + $this->plugins('add:after', func_get_args()); + + if (!$result) return false; + return $this->database->insert_id; + + } + + public function getAll($public) { + + if (!isset($public)) return false; + + # Call plugins + $this->plugins('getAll:before', func_get_args()); + + # Get SmartAlbums + if ($public===false) $return = getSmartInfo(); + + # Albums query + $query = 'SELECT id, title, public, sysdate, password FROM lychee_albums WHERE public = 1 AND visible <> 0'; + if ($public===false) $query = 'SELECT id, title, public, sysdate, password FROM lychee_albums'; + + # Execute query + $albums = $this->database->query($query) OR exit('Error: ' . $this->database->error); + + # For each album + while ($album = $albums->fetch_assoc()) { + + # Parse info + $album['sysdate'] = date('F Y', strtotime($album['sysdate'])); + $album['password'] = ($album['password'] != ''); + + # Thumbs + if (($public===true&&$album['password']===false)||($public===false)) { + + # Execute query + $thumbs = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE album = '" . $album['id'] . "' ORDER BY star DESC, " . substr($this->settings['sorting'], 9) . " LIMIT 0, 3"); + + # For each thumb + $k = 0; + while ($thumb = $thumbs->fetch_object()) { + $album["thumb$k"] = $thumb->thumbUrl; + $k++; + } + + } + + # Add to return + $return['content'][$album['id']] = $album; + + } + + # Num of albums + $return['num'] = $albums->num_rows; + + # Call plugins + $this->plugins('getAll:after', func_get_args()); + + return $return; + + } + +} \ No newline at end of file diff --git a/php/modules/Plugins.php b/php/modules/Plugins.php index b59e812..42c0d62 100644 --- a/php/modules/Plugins.php +++ b/php/modules/Plugins.php @@ -1,10 +1,10 @@ files = $files; + # Init vars + $plugins = $this; + $this->files = $files; + # Load plugins foreach ($this->files as $file) { if ($file==='') continue; include('../plugins/' . $file); @@ -34,6 +36,7 @@ class Plugins implements \SplSubject { if (!isset($observer)) return false; + # Add observer $this->observers[] = $observer; return true; @@ -44,6 +47,7 @@ class Plugins implements \SplSubject { if (!isset($observer)) return false; + # Remove observer $key = array_search($observer, $this->observers, true); if ($key) unset($this->observers[$key]); @@ -53,6 +57,7 @@ class Plugins implements \SplSubject { public function notify() { + # Notify each observer foreach ($this->observers as $value) $value->update($this); return true; @@ -63,9 +68,11 @@ class Plugins implements \SplSubject { if (!isset($action, $args)) return false; + # Save vars $this->action = $action; $this->args = $args; + # Notify observers $this->notify(); return true; diff --git a/php/modules/album.php b/php/modules/_album.php similarity index 100% rename from php/modules/album.php rename to php/modules/_album.php