Improved plugins
This commit is contained in:
parent
186efaf86f
commit
ab9b1ee011
@ -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']))
|
||||
|
@ -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'])) {
|
||||
|
@ -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]));
|
||||
|
117
php/modules/Albums.php
Normal file
117
php/modules/Albums.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
###
|
||||
# @name Album Module
|
||||
# @author Tobias Reich
|
||||
# @copyright 2014 by Tobias Reich
|
||||
###
|
||||
|
||||
if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
||||
|
||||
class Albums {
|
||||
|
||||
private $database = null;
|
||||
private $plugins = null;
|
||||
private $settings = array();
|
||||
private $albumIDs = array();
|
||||
|
||||
public function __construct($database = null, $plugins = null, $settings = null, $albumIDs = array()) {
|
||||
|
||||
# Init vars
|
||||
$this->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;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @name Plugins Module
|
||||
* @author Tobias Reich
|
||||
* @copyright 2014 by Tobias Reich
|
||||
*/
|
||||
###
|
||||
# @name Plugins Module
|
||||
# @author Tobias Reich
|
||||
# @copyright 2014 by Tobias Reich
|
||||
###
|
||||
|
||||
class Plugins implements \SplSubject {
|
||||
|
||||
@ -14,13 +14,15 @@ class Plugins implements \SplSubject {
|
||||
public $action = null;
|
||||
public $args = null;
|
||||
|
||||
public function __construct($files) {
|
||||
public function __construct($files, $database) {
|
||||
|
||||
if (!isset($files)) return false;
|
||||
|
||||
$plugins = $this;
|
||||
$this->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;
|
||||
|
Loading…
Reference in New Issue
Block a user