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) {
if (!isset($this->database)) return false;
# Check dependencies
$this->dependencies(isset($this->database));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -50,7 +51,8 @@ class Album extends Module {
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
$this->plugins(__METHOD__, 0, func_get_args());
@ -129,7 +131,8 @@ class Album extends Module {
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
$this->plugins(__METHOD__, 0, func_get_args());
@ -183,7 +186,8 @@ class Album extends Module {
private function getSmartInfo() {
if (!isset($this->database, $this->settings)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->settings));
# Unsorted
$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() {
if (!isset($this->database, $this->albumIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -326,7 +331,8 @@ class Album extends Module {
public function setTitle($title = 'Untitled') {
if (!isset($this->database, $this->albumIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -347,7 +353,8 @@ class Album extends Module {
public function setDescription($description = '') {
if (!isset($this->database, $this->albumIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -369,7 +376,8 @@ class Album extends Module {
public function getPublic() {
if (!isset($this->database, $this->albumIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -390,7 +398,8 @@ class Album extends Module {
public function setPublic($password) {
if (!isset($this->database, $this->albumIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -427,7 +436,8 @@ class Album extends Module {
public function setPassword($password) {
if (!isset($this->database, $this->albumIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -445,7 +455,8 @@ class Album extends Module {
public function checkPassword($password) {
if (!isset($this->database, $this->albumIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -465,7 +476,8 @@ class Album extends Module {
public function delete($albumIDs) {
if (!isset($this->database, $this->albumIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->albumIDs));
# Call plugins
$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') {
if (!isset($host, $user, $password, $name)) return false;
# Check dependencies
Module::dependencies(isset($host, $user, $password, $name));
$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
$updates = array(
@ -63,7 +65,8 @@ class Database extends Module {
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);
@ -99,7 +102,8 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
static function createDatabase($database, $name = 'lychee') {
if (!isset($database, $name)) return false;
# Check dependencies
Module::dependencies(isset($database, $name));
# Create database
$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) {
if (!isset($database)) return false;
# Check dependencies
Module::dependencies(isset($database));
# Create settings
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 = '') {
if (!isset($this->database)) return false;
# Check dependencies
$this->dependencies(isset($this->database));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -138,7 +139,8 @@ class Photo extends Module {
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
$this->plugins(__METHOD__, 0, func_get_args());
@ -223,7 +225,8 @@ class Photo extends Module {
private function adjustFile($path, $info) {
if (!isset($path, $info)) return false;
# Check dependencies
$this->dependencies(isset($path, $info));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -336,7 +339,8 @@ class Photo extends Module {
public function get($albumID) {
if (!isset($this->database, $this->photoIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -376,7 +380,8 @@ class Photo extends Module {
private function getInfo($url) {
if (!isset($this->database, $url)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $url));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -469,7 +474,8 @@ class Photo extends Module {
public function getArchive() {
if (!isset($this->database, $this->photoIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -501,7 +507,8 @@ class Photo extends Module {
function setTitle($title) {
if (!isset($this->database, $this->photoIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -522,7 +529,8 @@ class Photo extends Module {
function setDescription($description) {
if (!isset($this->database, $this->photoIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -544,7 +552,8 @@ class Photo extends Module {
public function setStar() {
if (!isset($this->database, $this->photoIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -577,7 +586,8 @@ class Photo extends Module {
function getPublic($password) {
if (!isset($this->database, $this->photoIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -604,7 +614,8 @@ class Photo extends Module {
public function setPublic() {
if (!isset($this->database, $this->photoIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -629,7 +640,8 @@ class Photo extends Module {
function setAlbum($albumID) {
if (!isset($this->database, $this->photoIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -647,7 +659,8 @@ class Photo extends Module {
public function setTags($tags) {
if (!isset($this->database, $this->photoIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
@ -670,7 +683,8 @@ class Photo extends Module {
public function delete() {
if (!isset($this->database, $this->photoIDs)) return false;
# Check dependencies
$this->dependencies(isset($this->database, $this->photoIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());

View File

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

View File

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