Improved error handling by checking the dependencies (e.g. vars and parameters)

This commit is contained in:
Tobias Reich 2014-04-19 21:07:36 +02:00
parent 7ac9ee69f2
commit 1e18094f8f
6 changed files with 85 additions and 40 deletions

View File

@ -28,7 +28,8 @@ class Album extends Module {
public function add($title = 'Untitled', $public = 0, $visible = 1) { public function add($title = 'Untitled', $public = 0, $visible = 1) {
if (!isset($this->database)) return false; # Check dependencies
$this->dependencies(isset($this->database));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -50,7 +51,8 @@ class Album extends Module {
public function get() { public function get() {
if (!isset($this->database, $this->settings, $this->albumIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->settings, $this->albumIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -129,7 +131,8 @@ class Album extends Module {
public function getAll($public) { public function getAll($public) {
if (!isset($this->database, $this->settings, $public)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->settings, $public));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -183,7 +186,8 @@ class Album extends Module {
private function getSmartInfo() { private function getSmartInfo() {
if (!isset($this->database, $this->settings)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->settings));
# Unsorted # Unsorted
$unsorted = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE album = 0 " . $this->settings['sorting']); $unsorted = $this->database->query("SELECT thumbUrl FROM lychee_photos WHERE album = 0 " . $this->settings['sorting']);
@ -224,7 +228,8 @@ class Album extends Module {
public function getArchive() { public function getArchive() {
if (!isset($this->database, $this->albumIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -326,7 +331,8 @@ class Album extends Module {
public function setTitle($title = 'Untitled') { public function setTitle($title = 'Untitled') {
if (!isset($this->database, $this->albumIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -347,7 +353,8 @@ class Album extends Module {
public function setDescription($description = '') { public function setDescription($description = '') {
if (!isset($this->database, $this->albumIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -369,7 +376,8 @@ class Album extends Module {
public function getPublic() { public function getPublic() {
if (!isset($this->database, $this->albumIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -390,7 +398,8 @@ class Album extends Module {
public function setPublic($password) { public function setPublic($password) {
if (!isset($this->database, $this->albumIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -427,7 +436,8 @@ class Album extends Module {
public function setPassword($password) { public function setPassword($password) {
if (!isset($this->database, $this->albumIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -445,7 +455,8 @@ class Album extends Module {
public function checkPassword($password) { public function checkPassword($password) {
if (!isset($this->database, $this->albumIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -465,7 +476,8 @@ class Album extends Module {
public function delete($albumIDs) { public function delete($albumIDs) {
if (!isset($this->database, $this->albumIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());

View File

@ -12,7 +12,8 @@ class Database extends Module {
static function connect($host = 'localhost', $user, $password, $name = 'lychee') { static function connect($host = 'localhost', $user, $password, $name = 'lychee') {
if (!isset($host, $user, $password, $name)) return false; # Check dependencies
Module::dependencies(isset($host, $user, $password, $name));
$database = new mysqli($host, $user, $password); $database = new mysqli($host, $user, $password);
@ -35,9 +36,10 @@ class Database extends Module {
} }
static function update($database, $dbName, $version) { static function update($database, $dbName, $version = 0) {
if (!isset($database)) return false; # Check dependencies
Module::dependencies(isset($database, $dbName));
# List of updates # List of updates
$updates = array( $updates = array(
@ -63,7 +65,8 @@ class Database extends Module {
static function createConfig($host = 'localhost', $user, $password, $name = 'lychee') { static function createConfig($host = 'localhost', $user, $password, $name = 'lychee') {
if (!isset($host, $user, $password, $name)) return false; # Check dependencies
Module::dependencies(isset($host, $user, $password, $name));
$database = new mysqli($host, $user, $password); $database = new mysqli($host, $user, $password);
@ -99,7 +102,8 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
static function createDatabase($database, $name = 'lychee') { static function createDatabase($database, $name = 'lychee') {
if (!isset($database, $name)) return false; # Check dependencies
Module::dependencies(isset($database, $name));
# Create database # Create database
$result = $database->query("CREATE DATABASE IF NOT EXISTS $name;"); $result = $database->query("CREATE DATABASE IF NOT EXISTS $name;");
@ -112,7 +116,8 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
static function createTables($database) { static function createTables($database) {
if (!isset($database)) return false; # Check dependencies
Module::dependencies(isset($database));
# Create settings # Create settings
if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) { if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {

View File

@ -26,4 +26,10 @@ class Module {
} }
protected function dependencies($available = false) {
if ($available===false) exit('Error: Can not execute function. Missing parameters and variables.');
}
} }

View File

@ -28,7 +28,8 @@ class Photo extends Module {
public function add($files, $albumID, $description = '', $tags = '') { public function add($files, $albumID, $description = '', $tags = '') {
if (!isset($this->database)) return false; # Check dependencies
$this->dependencies(isset($this->database));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -138,7 +139,8 @@ class Photo extends Module {
private function createThumb($url, $filename, $width = 200, $height = 200) { private function createThumb($url, $filename, $width = 200, $height = 200) {
if (!isset($this->settings, $url, $filename)) return false; # Check dependencies
$this->dependencies(isset($this->settings, $url, $filename));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -223,7 +225,8 @@ class Photo extends Module {
private function adjustFile($path, $info) { private function adjustFile($path, $info) {
if (!isset($path, $info)) return false; # Check dependencies
$this->dependencies(isset($path, $info));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -336,7 +339,8 @@ class Photo extends Module {
public function get($albumID) { public function get($albumID) {
if (!isset($this->database, $this->photoIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -376,7 +380,8 @@ class Photo extends Module {
private function getInfo($url) { private function getInfo($url) {
if (!isset($this->database, $url)) return false; # Check dependencies
$this->dependencies(isset($this->database, $url));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -469,7 +474,8 @@ class Photo extends Module {
public function getArchive() { public function getArchive() {
if (!isset($this->database, $this->photoIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -501,7 +507,8 @@ class Photo extends Module {
function setTitle($title) { function setTitle($title) {
if (!isset($this->database, $this->photoIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -522,7 +529,8 @@ class Photo extends Module {
function setDescription($description) { function setDescription($description) {
if (!isset($this->database, $this->photoIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -544,7 +552,8 @@ class Photo extends Module {
public function setStar() { public function setStar() {
if (!isset($this->database, $this->photoIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -577,7 +586,8 @@ class Photo extends Module {
function getPublic($password) { function getPublic($password) {
if (!isset($this->database, $this->photoIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -604,7 +614,8 @@ class Photo extends Module {
public function setPublic() { public function setPublic() {
if (!isset($this->database, $this->photoIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -629,7 +640,8 @@ class Photo extends Module {
function setAlbum($albumID) { function setAlbum($albumID) {
if (!isset($this->database, $this->photoIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -647,7 +659,8 @@ class Photo extends Module {
public function setTags($tags) { public function setTags($tags) {
if (!isset($this->database, $this->photoIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -670,7 +683,8 @@ class Photo extends Module {
public function delete() { public function delete() {
if (!isset($this->database, $this->photoIDs)) return false; # Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());

View File

@ -24,7 +24,8 @@ class Session extends Module {
public function init($database, $dbName, $public, $version) { public function init($database, $dbName, $public, $version) {
if (!isset($this->settings, $public, $version)) return false; # Check dependencies
$this->dependencies(isset($this->settings, $public, $version));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
@ -69,7 +70,8 @@ class Session extends Module {
public function login($username, $password) { public function login($username, $password) {
if (!isset($this->settings, $username, $password)) return false; # Check dependencies
$this->dependencies(isset($this->settings, $username, $password));
# Call plugins # Call plugins
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());

View File

@ -23,7 +23,8 @@ class Settings extends Module {
public function get() { public function get() {
if (!isset($this->database)) return false; # Check dependencies
$this->dependencies(isset($this->database));
# Execute query # Execute query
$settings = $this->database->query('SELECT * FROM lychee_settings;'); $settings = $this->database->query('SELECT * FROM lychee_settings;');
@ -40,7 +41,8 @@ class Settings extends Module {
public function setLogin($oldPassword = '', $username, $password) { public function setLogin($oldPassword = '', $username, $password) {
if (!isset($this->database)) return false; # Check dependencies
$this->dependencies(isset($this->database));
# Load settings # Load settings
$settings = $this->get(); $settings = $this->get();
@ -63,7 +65,8 @@ class Settings extends Module {
private function setUsername($username) { private function setUsername($username) {
if (!isset($this->database)) return false; # Check dependencies
$this->dependencies(isset($this->database));
# Parse # Parse
$username = htmlentities($username); $username = htmlentities($username);
@ -79,7 +82,8 @@ class Settings extends Module {
private function setPassword($password) { private function setPassword($password) {
if (!isset($this->database)) return false; # Check dependencies
$this->dependencies(isset($this->database));
if (strlen($password)<1||strlen($password)>50) return false; if (strlen($password)<1||strlen($password)>50) return false;
@ -93,7 +97,8 @@ class Settings extends Module {
public function setDropboxKey($key) { public function setDropboxKey($key) {
if (!isset($this->database, $key)) return false; # Check dependencies
$this->dependencies(isset($this->database, $key));
if (strlen($key)<1||strlen($key)>50) return false; if (strlen($key)<1||strlen($key)>50) return false;
@ -107,7 +112,8 @@ class Settings extends Module {
public function setSorting($type, $order) { public function setSorting($type, $order) {
if (!isset($this->database, $type, $order)) return false; # Check dependencies
$this->dependencies(isset($this->database, $type, $order));
$sorting = 'ORDER BY '; $sorting = 'ORDER BY ';