Browse Source

Added Database::execute to run a query

Database::execute automatically logs errors
Tobias Reich 8 years ago
parent
commit
7b86a737af

+ 89 - 88
php/Modules/Album.php

@@ -32,15 +32,12 @@ final class Album {
 		// Database
 		$sysstamp = time();
 		$query    = Database::prepare(Database::get(), "INSERT INTO ? (title, sysstamp, public, visible) VALUES ('?', '?', '?', '?')", array(LYCHEE_TABLE_ALBUMS, $title, $sysstamp, $public, $visible));
-		$result   = Database::get()->query($query);
+		$result   = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return Database::get()->insert_id;
 
 	}
@@ -111,7 +108,7 @@ final class Album {
 
 			default:
 				$query  = Database::prepare(Database::get(), "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
-				$albums = Database::get()->query($query);
+				$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 				$return = $albums->fetch_assoc();
 				$return = Album::prepareData($return);
 				$query  = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = '?' " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
@@ -120,8 +117,11 @@ final class Album {
 		}
 
 		// Get photos
-		$photos          = Database::get()->query($query);
+		$photos          = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 		$previousPhotoID = '';
+
+		if ($photos===false) exit('Error: Could not get photos of album from database!');
+
 		while ($photo = $photos->fetch_assoc()) {
 
 			// Turn data from the database into a front-end friendly format
@@ -190,12 +190,9 @@ final class Album {
 		else                 $query = Database::prepare(Database::get(), 'SELECT id, title, public, sysstamp, password FROM ? WHERE public = 1 AND visible <> 0 ' . Settings::get()['sortingAlbums'], array(LYCHEE_TABLE_ALBUMS));
 
 		// Execute query
-		$albums = Database::get()->query($query);
+		$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-		if ($albums===false) {
-			Log::error(__METHOD__, __LINE__, 'Could not get albums from database (' . Database::get()->error . ')');
-			exit('Error: Could not get albums from database!');
-		}
+		if ($albums===false) exit('Error: Could not get albums from database!');
 
 		// For each album
 		while ($album = $albums->fetch_assoc()) {
@@ -209,12 +206,9 @@ final class Album {
 
 					// Execute query
 					$query  = Database::prepare(Database::get(), "SELECT thumbUrl FROM ? WHERE album = '?' ORDER BY star DESC, " . substr(Settings::get()['sortingPhotos'], 9) . " LIMIT 3", array(LYCHEE_TABLE_PHOTOS, $album['id']));
-					$thumbs = Database::get()->query($query);
+					$thumbs = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-					if ($thumbs===false) {
-						Log::error(__METHOD__, __LINE__, 'Could not get thumbs of album from database (' . Database::get()->error . ')');
-						exit('Error: Could not get thumbs of album from database!');
-					}
+					if ($thumbs===false) exit('Error: Could not get thumbs of album from database!');
 
 					// For each thumb
 					$k = 0;
@@ -255,9 +249,11 @@ final class Album {
 		 */
 
 		$query    = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE album = 0 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
-		$unsorted = Database::get()->query($query);
+		$unsorted = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 		$i        = 0;
 
+		if ($unsorted===false) exit('Error: Could not get unsorted photos from database!');
+
 		$return['unsorted'] = array(
 			'thumbs' => array(),
 			'num'    => $unsorted->num_rows
@@ -275,9 +271,11 @@ final class Album {
 		 */
 
 		$query   = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE star = 1 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
-		$starred = Database::get()->query($query);
+		$starred = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 		$i       = 0;
 
+		if ($starred===false) exit('Error: Could not get starred photos from database!');
+
 		$return['starred'] = array(
 			'thumbs' => array(),
 			'num'    => $starred->num_rows
@@ -295,9 +293,11 @@ final class Album {
 		 */
 
 		$query  = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE public = 1 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
-		$public = Database::get()->query($query);
+		$public = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 		$i      = 0;
 
+		if ($public===false) exit('Error: Could not get public photos from database!');
+
 		$return['public'] = array(
 			'thumbs' => array(),
 			'num'    => $public->num_rows
@@ -315,9 +315,11 @@ final class Album {
 		 */
 
 		$query  = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
-		$recent = Database::get()->query($query);
+		$recent = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 		$i      = 0;
 
+		if ($recent===false) exit('Error: Could not get recent photos from database!');
+
 		$return['recent'] = array(
 			'thumbs' => array(),
 			'num'    => $recent->num_rows
@@ -372,20 +374,17 @@ final class Album {
 		if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) {
 
 			$query = Database::prepare(Database::get(), "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
-			$album = Database::get()->query($query);
+			$album = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-			if ($album===false) {
-				Log::error(__METHOD__, __LINE__, Database::get()->error);
-				return false;
-			}
+			if ($album===false) exit('Error: Could not get album from database!');
 
-			// Fetch object
+			// Get album object
 			$album = $album->fetch_object();
 
 			// Photo not found
 			if ($album===null) {
-				Log::error(__METHOD__, __LINE__, 'Album not found. Cannot start download.');
-				return false;
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
+				exit('Error: Could not find specified album!');
 			}
 
 			// Set title
@@ -401,17 +400,19 @@ final class Album {
 		// Create zip
 		$zip = new ZipArchive();
 		if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
-			Log::error(__METHOD__, __LINE__, 'Could not create ZipArchive');
+			Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive');
 			return false;
 		}
 
 		// Execute query
-		$photos = Database::get()->query($photos);
+		$photos = Database::execute(Database::get(), $photos, __METHOD__, __LINE__);
+
+		if ($album===null) exit('Error: Could not get photos from database!');
 
 		// Check if album empty
 		if ($photos->num_rows==0) {
-			Log::error(__METHOD__, __LINE__, 'Could not create ZipArchive without images');
-			return false;
+			Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive without images');
+			exit('Error: Could not create ZipArchive without images!');
 		}
 
 		// Parse each path
@@ -484,15 +485,12 @@ final class Album {
 
 		// Execute query
 		$query  = Database::prepare(Database::get(), "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $title, $this->albumIDs));
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return true;
 
 	}
@@ -507,15 +505,12 @@ final class Album {
 
 		// Execute query
 		$query  = Database::prepare(Database::get(), "UPDATE ? SET description = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $description, $this->albumIDs));
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return true;
 
 	}
@@ -532,8 +527,12 @@ final class Album {
 
 		// Execute query
 		$query  = Database::prepare(Database::get(), "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
-		$albums = Database::get()->query($query);
-		$album  = $albums->fetch_object();
+		$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+		if ($albums===false) return false;
+
+		// Get album object
+		$album = $albums->fetch_object();
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
@@ -555,8 +554,12 @@ final class Album {
 
 		// Execute query
 		$query  = Database::prepare(Database::get(), "SELECT downloadable FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
-		$albums = Database::get()->query($query);
-		$album  = $albums->fetch_object();
+		$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+		if ($albums===false) return false;
+
+		// Get album object
+		$album = $albums->fetch_object();
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
@@ -581,20 +584,18 @@ final class Album {
 
 		// Set public
 		$query  = Database::prepare(Database::get(), "UPDATE ? SET public = '?', visible = '?', downloadable = '?', password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $public, $visible, $downloadable, $this->albumIDs));
-		$result = Database::get()->query($query);
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+		if ($result===false) return false;
 
 		// Reset permissions for photos
 		if ($public===1) {
+
 			$query  = Database::prepare(Database::get(), "UPDATE ? SET public = 0 WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
-			$result = Database::get()->query($query);
-			if ($result===false) {
-				Log::error(__METHOD__, __LINE__, Database::get()->error);
-				return false;
-			}
+			$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+			if ($result===false) return false;
+
 		}
 
 		// Call plugins
@@ -602,7 +603,6 @@ final class Album {
 
 		// Set password
 		if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
-
 		return true;
 
 	}
@@ -633,15 +633,12 @@ final class Album {
 		}
 
 		// Execute query
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return true;
 
 	}
@@ -656,14 +653,19 @@ final class Album {
 
 		// Execute query
 		$query  = Database::prepare(Database::get(), "SELECT password FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
-		$albums = Database::get()->query($query);
-		$album  = $albums->fetch_object();
+		$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+		if ($albums===false) return false;
+
+		// Get album object
+		$album = $albums->fetch_object();
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
+		// Check if password is correct
 		if ($album->password=='') return true;
-		else if ($album->password===crypt($password, $album->password)) return true;
+		if ($album->password===crypt($password, $album->password)) return true;
 		return false;
 
 	}
@@ -684,27 +686,21 @@ final class Album {
 		$albumID = $albumID[0];
 
 		$query  = Database::prepare(Database::get(), "UPDATE ? SET album = ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->albumIDs));
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 
 		// $albumIDs contains all IDs without the first albumID
 		// Convert to string
 		$filteredIDs = implode(',', $albumIDs);
 
 		$query  = Database::prepare(Database::get(), "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $filteredIDs));
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return true;
 
 	}
@@ -718,32 +714,37 @@ final class Album {
 		Plugins::get()->activate(__METHOD__, 0, func_get_args());
 
 		// Init vars
-		$error = false;
+		$photoIDs = array();
 
 		// Execute query
 		$query  = Database::prepare(Database::get(), "SELECT id FROM ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
-		$photos = Database::get()->query($query);
+		$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-		// For each album delete photo
-		while ($row = $photos->fetch_object()) {
+		if ($photos===false) return false;
 
-			$photo = new Photo($row->id);
-			if (!$photo->delete($row->id)) $error = true;
+		// Only delete photos when albums contain photos
+		if ($photos->num_rows>0) {
+
+			// Add each id to photoIDs
+			while ($row = $photos->fetch_object()) $photoIDs[] = $row->id;
+
+			// Convert photoIDs to a string
+			$photoIDs = implode(',', $photoIDs);
+
+			// Delete all photos
+			$photo = new Photo($photoIDs);
+			if ($photo->delete()!==true) return false;
 
 		}
 
 		// Delete albums
 		$query  = Database::prepare(Database::get(), "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($error) return false;
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return true;
 
 	}

+ 73 - 51
php/Modules/Database.php

@@ -87,8 +87,10 @@ final class Database {
 		else @$connection->set_charset('utf8');
 
 		// Set unicode
-		$connection->query('SET NAMES utf8;');
+		$query  = 'SET NAMES utf8';
+		$result = self::execute($connection, $query, null, null);
 
+		if ($result===false) return false;
 		return true;
 
 	}
@@ -103,8 +105,9 @@ final class Database {
 
 		// Create database
 		$query  = self::prepare($connection, 'CREATE DATABASE IF NOT EXISTS ?', array($name));
-		$result = $connection->query($query);
+		$result = self::execute($connection, $query, null, null);
 
+		if ($result===false) return false;
 		if ($connection->select_db($name)===false) return false;
 		return true;
 
@@ -116,12 +119,14 @@ final class Database {
 		Validator::required(isset($connection), __METHOD__);
 
 		// Check if tables exist
-		$query = self::prepare($connection, 'SELECT * FROM ?, ?, ?, ? LIMIT 0', array(LYCHEE_TABLE_PHOTOS, LYCHEE_TABLE_ALBUMS, LYCHEE_TABLE_SETTINGS, LYCHEE_TABLE_LOG));
-		if ($connection->query($query)) return true;
+		$query  = self::prepare($connection, 'SELECT * FROM ?, ?, ?, ? LIMIT 0', array(LYCHEE_TABLE_PHOTOS, LYCHEE_TABLE_ALBUMS, LYCHEE_TABLE_SETTINGS, LYCHEE_TABLE_LOG));
+		$result = self::execute($connection, $query, null, null);
+		if ($result!==false) return true;
 
-		// Create log
+		// Check if log table exists
 		$exist  = self::prepare($connection, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_LOG));
-		$result = $connection->query($exist);
+		$result = self::execute($connection, $exist, null, null);
+
 		if ($result===false) {
 
 			// Read file
@@ -132,14 +137,16 @@ final class Database {
 
 			// Create table
 			$query  = self::prepare($connection, $query, array(LYCHEE_TABLE_LOG));
-			$result = $connection->query($exist);
+			$result = self::execute($connection, $query, null, null);
+
 			if ($result===false) return false;
 
 		}
 
-		// Create settings
+		// Check if settings table exists
 		$exist  = self::prepare($connection, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_SETTINGS));
-		$result = $connection->query($exist);
+		$result = self::execute($connection, $exist, __METHOD__, __LINE__);
+
 		if ($result===false) {
 
 			// Read file
@@ -147,49 +154,44 @@ final class Database {
 			$query = @file_get_contents($file);
 
 			if ($query===false) {
-				Log::error(__METHOD__, __LINE__, 'Could not load query for lychee_settings');
+				Log::error($connection, __METHOD__, __LINE__, 'Could not load query for lychee_settings');
 				return false;
 			}
 
 			// Create table
 			$query  = self::prepare($connection, $query, array(LYCHEE_TABLE_SETTINGS));
-			$result = $connection->query($exist);
-			if ($result===false) {
-				Log::error(__METHOD__, __LINE__, $connection->error);
-				return false;
-			}
+			$result = self::execute($connection, $query, __METHOD__, __LINE__);
+
+			if ($result===false) return false;
 
 			// Read file
 			$file  = __DIR__ . '/../database/settings_content.sql';
 			$query = @file_get_contents($file);
 
 			if ($query===false) {
-				Log::error(__METHOD__, __LINE__, 'Could not load content-query for lychee_settings');
+				Log::error($connection, __METHOD__, __LINE__, 'Could not load content-query for lychee_settings');
 				return false;
 			}
 
 			// Add content
 			$query  = self::prepare($connection, $query, array(LYCHEE_TABLE_SETTINGS));
-			$result = $connection->query($exist);
-			if ($result===false) {
-				Log::error(__METHOD__, __LINE__, $connection->error);
-				return false;
-			}
+			$result = self::execute($connection, $query, __METHOD__, __LINE__);
+
+			if ($result===false) return false;
 
 			// Generate identifier
 			$identifier = md5(microtime(true));
 			$query      = self::prepare($connection, "UPDATE `?` SET `value` = '?' WHERE `key` = 'identifier' LIMIT 1", array(LYCHEE_TABLE_SETTINGS, $identifier));
-			$result     = $connection->query($exist);
-			if ($result===false) {
-				Log::error(__METHOD__, __LINE__, $connection->error);
-				return false;
-			}
+			$result     = self::execute($connection, $query, __METHOD__, __LINE__);
+
+			if ($result===false) return false;
 
 		}
 
-		// Create albums
+		// Check if albums table exists
 		$exist  = self::prepare($connection, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_ALBUMS));
-		$result = $connection->query($exist);
+		$result = self::execute($connection, $exist, __METHOD__, __LINE__);
+
 		if ($result===false) {
 
 			// Read file
@@ -197,23 +199,22 @@ final class Database {
 			$query = @file_get_contents($file);
 
 			if ($query===false) {
-				Log::error(__METHOD__, __LINE__, 'Could not load query for lychee_albums');
+				Log::error($connection, __METHOD__, __LINE__, 'Could not load query for lychee_albums');
 				return false;
 			}
 
 			// Create table
 			$query  = self::prepare($connection, $query, array(LYCHEE_TABLE_ALBUMS));
-			$result = $connection->query($exist);
-			if ($result===false) {
-				Log::error(__METHOD__, __LINE__, $connection->error);
-				return false;
-			}
+			$result = self::execute($connection, $query, __METHOD__, __LINE__);
+
+			if ($result===false) return false;
 
 		}
 
-		// Create photos
+		// Check if photos table exists
 		$exist  = self::prepare($connection, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_PHOTOS));
-		$result = $connection->query($exist);
+		$result = self::execute($connection, $exist, __METHOD__, __LINE__);
+
 		if ($result===false) {
 
 			// Read file
@@ -221,17 +222,15 @@ final class Database {
 			$query = @file_get_contents($file);
 
 			if ($query===false) {
-				Log::error(__METHOD__, __LINE__, 'Could not load query for lychee_photos');
+				Log::error($connection, __METHOD__, __LINE__, 'Could not load query for lychee_photos');
 				return false;
 			}
 
 			// Create table
 			$query  = self::prepare($connection, $query, array(LYCHEE_TABLE_PHOTOS));
-			$result = $connection->query($exist);
-			if ($result===false) {
-				Log::error(__METHOD__, __LINE__, $connection->error);
-				return false;
-			}
+			$result = self::execute($connection, $query, __METHOD__, __LINE__);
+
+			if ($result===false) return false;
 
 		}
 
@@ -245,9 +244,13 @@ final class Database {
 		Validator::required(isset($connection, $dbName), __METHOD__);
 
 		// Get current version
-		$query   = self::prepare($connection, "SELECT * FROM ? WHERE `key` = 'version'", array(LYCHEE_TABLE_SETTINGS));
-		$results = $connection->query($query);
-		$current = $results->fetch_object()->value;
+		$query  = self::prepare($connection, "SELECT * FROM ? WHERE `key` = 'version'", array(LYCHEE_TABLE_SETTINGS));
+		$result = self::execute($connection, $query, __METHOD__, __LINE__);
+
+		if ($result===false) return false;
+
+		// Extract current version
+		$current = $result->fetch_object()->value;
 
 		// For each update
 		foreach (self::$versions as $version) {
@@ -270,11 +273,9 @@ final class Database {
 		Validator::required(isset($connection), __METHOD__);
 
 		$query  = self::prepare($connection, "UPDATE ? SET value = '?' WHERE `key` = 'version'", array(LYCHEE_TABLE_SETTINGS, $version));
-		$result = $connection->query($query);
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, 'Could not update database (' . $connection->error . ')');
-			return false;
-		}
+		$result = self::execute($connection, $query, __METHOD__, __LINE__);
+
+		if ($result===false) return false;
 
 	}
 
@@ -294,7 +295,7 @@ final class Database {
 			'data'        => count($data)
 		);
 
-		if (($num['data']-$num['placeholder'])<0) Log::notice(__METHOD__, __LINE__, 'Could not completely prepare query. Query has more placeholders than values.');
+		if (($num['data']-$num['placeholder'])<0) Log::notice($connection, __METHOD__, __LINE__, 'Could not completely prepare query. Query has more placeholders than values.');
 
 		foreach ($data as $value) {
 
@@ -344,6 +345,27 @@ final class Database {
 
 	}
 
+	public static function execute($connection, $query, $function, $line) {
+
+		// Check dependencies
+		Validator::required(isset($connection, $query), __METHOD__);
+
+		// Only activate logging when $function and $line is set
+		$logging = ($function===null||$line===null ? false : true);
+
+		// Execute query
+		$result = $connection->query($query);
+
+		// Check if execution failed
+		if ($result===false) {
+			if ($logging===true) Log::error($connection, $function, $line, $connection->error);
+			return false;
+		}
+
+		return $result;
+
+	}
+
 }
 
 ?>

+ 10 - 10
php/Modules/Import.php

@@ -46,7 +46,7 @@ final class Import {
 			$extension = getExtension($url);
 			if (!in_array(strtolower($extension), Photo::$validExtensions, true)) {
 				$error = true;
-				Log::error(__METHOD__, __LINE__, 'Photo format not supported (' . $url . ')');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Photo format not supported (' . $url . ')');
 				continue;
 			}
 
@@ -54,7 +54,7 @@ final class Import {
 			$type = @exif_imagetype($url);
 			if (!in_array($type, Photo::$validTypes, true)) {
 				$error = true;
-				Log::error(__METHOD__, __LINE__, 'Photo type not supported (' . $url . ')');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Photo type not supported (' . $url . ')');
 				continue;
 			}
 
@@ -64,14 +64,14 @@ final class Import {
 
 			if (@copy($url, $tmp_name)===false) {
 				$error = true;
-				Log::error(__METHOD__, __LINE__, 'Could not copy file (' . $tmp_name . ') to temp-folder (' . $tmp_name . ')');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Could not copy file (' . $tmp_name . ') to temp-folder (' . $tmp_name . ')');
 				continue;
 			}
 
 			// Import photo
 			if (!$this->photo($tmp_name, $albumID)) {
 				$error = true;
-				Log::error(__METHOD__, __LINE__, 'Could not import file: ' . $tmp_name);
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Could not import file (' . $tmp_name . ')');
 				continue;
 			}
 
@@ -92,7 +92,7 @@ final class Import {
 		if (substr($path, -1)==='/') $path = substr($path, 0, -1);
 
 		if (is_dir($path)===false) {
-			Log::error(__METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
+			Log::error(Database::get(), __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
 			return 'Error: Given path is not a directory!';
 		}
 
@@ -100,7 +100,7 @@ final class Import {
 		if ($path===LYCHEE_UPLOADS_BIG||($path . '/')===LYCHEE_UPLOADS_BIG||
 			$path===LYCHEE_UPLOADS_MEDIUM||($path . '/')===LYCHEE_UPLOADS_MEDIUM||
 			$path===LYCHEE_UPLOADS_THUMB||($path . '/')===LYCHEE_UPLOADS_THUMB) {
-				Log::error(__METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')');
 				return 'Error: Given path is a reserved path of Lychee!';
 		}
 
@@ -122,7 +122,7 @@ final class Import {
 			// the file may still be unreadable by the user
 			if (!is_readable($file)) {
 				$error = true;
-				Log::error(__METHOD__, __LINE__, 'Could not read file or directory: ' . $file);
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Could not read file or directory (' . $file . ')');
 				continue;
 			}
 
@@ -134,7 +134,7 @@ final class Import {
 
 				if (!$this->photo($file, $albumID)) {
 					$error = true;
-					Log::error(__METHOD__, __LINE__, 'Could not import file: ' . $file);
+					Log::error(Database::get(), __METHOD__, __LINE__, 'Could not import file (' . $file . ')');
 					continue;
 				}
 
@@ -148,7 +148,7 @@ final class Import {
 
 				if ($newAlbumID===false) {
 					$error = true;
-					Log::error(__METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
+					Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
 					continue;
 				}
 
@@ -156,7 +156,7 @@ final class Import {
 
 				if ($import!==true&&$import!=='Notice: Import only contains albums!') {
 					$error = true;
-					Log::error(__METHOD__, __LINE__, 'Could not import folder. Function returned warning.');
+					Log::error(Database::get(), __METHOD__, __LINE__, 'Could not import folder. Function returned warning.');
 					continue;
 				}
 

+ 10 - 10
php/Modules/Log.php

@@ -4,35 +4,35 @@ namespace Lychee\Modules;
 
 final class Log {
 
-	public static function notice($function, $line, $text = '') {
+	public static function notice($connection, $function, $line, $text = '') {
 
-		return Log::text('notice', $function, $line, $text);
+		return Log::text($connection, 'notice', $function, $line, $text);
 
 	}
 
-	public static function warning($function, $line, $text = '') {
+	public static function warning($connection, $function, $line, $text = '') {
 
-		return Log::text('warning', $function, $line, $text);
+		return Log::text($connection, 'warning', $function, $line, $text);
 
 	}
 
-	public static function error($function, $line, $text = '') {
+	public static function error($connection, $function, $line, $text = '') {
 
-		return Log::text('error', $function, $line, $text);
+		return Log::text($connection, 'error', $function, $line, $text);
 
 	}
 
-	private static function text($type, $function, $line, $text = '') {
+	private static function text($connection, $type, $function, $line, $text = '') {
 
 		// Check dependencies
-		Validator::required(isset($type, $function, $line, $text), __METHOD__);
+		Validator::required(isset($connection, $type, $function, $line, $text), __METHOD__);
 
 		// Get time
 		$sysstamp = time();
 
 		// Save in database
-		$query  = Database::prepare(Database::get(), "INSERT INTO ? (time, type, function, line, text) VALUES ('?', '?', '?', '?', '?')", array(LYCHEE_TABLE_LOG, $sysstamp, $type, $function, $line, $text));
-		$result = Database::get()->query($query);
+		$query  = Database::prepare($connection, "INSERT INTO ? (time, type, function, line, text) VALUES ('?', '?', '?', '?', '?')", array(LYCHEE_TABLE_LOG, $sysstamp, $type, $function, $line, $text));
+		$result = Database::execute($connection, $query, null, null);
 
 		if ($result===false) return false;
 		return true;

+ 88 - 99
php/Modules/Photo.php

@@ -39,7 +39,7 @@ final class Photo {
 		if (hasPermissions(LYCHEE_UPLOADS)===false||
 			hasPermissions(LYCHEE_UPLOADS_BIG)===false||
 			hasPermissions(LYCHEE_UPLOADS_THUMB)===false) {
-				Log::error(__METHOD__, __LINE__, 'An upload-folder is missing or not readable and writable');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'An upload-folder is missing or not readable and writable');
 				exit('Error: An upload-folder is missing or not readable and writable!');
 		}
 
@@ -80,35 +80,35 @@ final class Photo {
 
 			// Check if file exceeds the upload_max_filesize directive
 			if ($file['error']===UPLOAD_ERR_INI_SIZE) {
-				Log::error(__METHOD__, __LINE__, 'The uploaded file exceeds the upload_max_filesize directive in php.ini');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'The uploaded file exceeds the upload_max_filesize directive in php.ini');
 				if ($returnOnError===true) return false;
 				exit('Error: The uploaded file exceeds the upload_max_filesize directive in php.ini!');
 			}
 
 			// Check if file was only partially uploaded
 			if ($file['error']===UPLOAD_ERR_PARTIAL) {
-				Log::error(__METHOD__, __LINE__, 'The uploaded file was only partially uploaded');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'The uploaded file was only partially uploaded');
 				if ($returnOnError===true) return false;
 				exit('Error: The uploaded file was only partially uploaded!');
 			}
 
 			// Check if writing file to disk failed
 			if ($file['error']===UPLOAD_ERR_CANT_WRITE) {
-				Log::error(__METHOD__, __LINE__, 'Failed to write photo to disk');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Failed to write photo to disk');
 				if ($returnOnError===true) return false;
 				exit('Error: Failed to write photo to disk!');
 			}
 
 			// Check if a extension stopped the file upload
 			if ($file['error']===UPLOAD_ERR_EXTENSION) {
-				Log::error(__METHOD__, __LINE__, 'A PHP extension stopped the file upload');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'A PHP extension stopped the file upload');
 				if ($returnOnError===true) return false;
 				exit('Error: A PHP extension stopped the file upload!');
 			}
 
 			// Check if the upload was successful
 			if ($file['error']!==UPLOAD_ERR_OK) {
-				Log::error(__METHOD__, __LINE__, 'Upload contains an error (' . $file['error'] . ')');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Upload contains an error (' . $file['error'] . ')');
 				if ($returnOnError===true) return false;
 				exit('Error: Upload failed!');
 			}
@@ -116,7 +116,7 @@ final class Photo {
 			// Verify extension
 			$extension = getExtension($file['name']);
 			if (!in_array(strtolower($extension), self::$validExtensions, true)) {
-				Log::error(__METHOD__, __LINE__, 'Photo format not supported');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Photo format not supported');
 				if ($returnOnError===true) return false;
 				exit('Error: Photo format not supported!');
 			}
@@ -124,7 +124,7 @@ final class Photo {
 			// Verify image
 			$type = @exif_imagetype($file['tmp_name']);
 			if (!in_array($type, self::$validTypes, true)) {
-				Log::error(__METHOD__, __LINE__, 'Photo type not supported');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Photo type not supported');
 				if ($returnOnError===true) return false;
 				exit('Error: Photo type not supported!');
 			}
@@ -141,7 +141,7 @@ final class Photo {
 			// Calculate checksum
 			$checksum = sha1_file($tmp_name);
 			if ($checksum===false) {
-				Log::error(__METHOD__, __LINE__, 'Could not calculate checksum for photo');
+				Log::error(Database::get(), __METHOD__, __LINE__, 'Could not calculate checksum for photo');
 				if ($returnOnError===true) return false;
 				exit('Error: Could not calculate checksum for photo!');
 			}
@@ -171,13 +171,13 @@ final class Photo {
 				// Import if not uploaded via web
 				if (!is_uploaded_file($tmp_name)) {
 					if (!@copy($tmp_name, $path)) {
-						Log::error(__METHOD__, __LINE__, 'Could not copy photo to uploads');
+						Log::error(Database::get(), __METHOD__, __LINE__, 'Could not copy photo to uploads');
 						if ($returnOnError===true) return false;
 						exit('Error: Could not copy photo to uploads!');
 					} else @unlink($tmp_name);
 				} else {
 					if (!@move_uploaded_file($tmp_name, $path)) {
-						Log::error(__METHOD__, __LINE__, 'Could not move photo to uploads');
+						Log::error(Database::get(), __METHOD__, __LINE__, 'Could not move photo to uploads');
 						if ($returnOnError===true) return false;
 						exit('Error: Could not move photo to uploads!');
 					}
@@ -188,7 +188,7 @@ final class Photo {
 				// Photo already exists
 				// Check if the user wants to skip duplicates
 				if (Settings::get()['skipDuplicates']==='1') {
-					Log::notice(__METHOD__, __LINE__, 'Skipped upload of existing photo because skipDuplicates is activated');
+					Log::notice(Database::get(), __METHOD__, __LINE__, 'Skipped upload of existing photo because skipDuplicates is activated');
 					if ($returnOnError===true) return false;
 					exit('Warning: This photo has been skipped because it\'s already in your library.');
 				}
@@ -210,7 +210,7 @@ final class Photo {
 				if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&$info['orientation']!=='') {
 					$adjustFile = $this->adjustFile($path, $info);
 					if ($adjustFile!==false) $info = $adjustFile;
-					else Log::notice(__METHOD__, __LINE__, 'Skipped adjustment of photo (' . $info['title'] . ')');
+					else Log::notice(Database::get(), __METHOD__, __LINE__, 'Skipped adjustment of photo (' . $info['title'] . ')');
 				}
 
 				// Set original date
@@ -218,7 +218,7 @@ final class Photo {
 
 				// Create Thumb
 				if (!$this->createThumb($path, $photo_name, $info['type'], $info['width'], $info['height'])) {
-					Log::error(__METHOD__, __LINE__, 'Could not create thumbnail for photo');
+					Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create thumbnail for photo');
 					if ($returnOnError===true) return false;
 					exit('Error: Could not create thumbnail for photo!');
 				}
@@ -235,10 +235,9 @@ final class Photo {
 			// Save to DB
 			$values = array(LYCHEE_TABLE_PHOTOS, $id, $info['title'], $photo_name, $description, $tags, $info['type'], $info['width'], $info['height'], $info['size'], $info['iso'], $info['aperture'], $info['make'], $info['model'], $info['shutter'], $info['focal'], $info['takestamp'], $path_thumb, $albumID, $public, $star, $checksum, $medium);
 			$query  = Database::prepare(Database::get(), "INSERT INTO ? (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum, medium) VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')", $values);
-			$result = Database::get()->query($query);
+			$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 			if ($result===false) {
-				Log::error(__METHOD__, __LINE__, Database::get()->error);
 				if ($returnOnError===true) return false;
 				exit('Error: Could not save photo in database!');
 			}
@@ -258,12 +257,9 @@ final class Photo {
 		if (isset($photoID)) $query = Database::prepare(Database::get(), "SELECT id, url, thumbUrl, medium FROM ? WHERE checksum = '?' AND id <> '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $checksum, $photoID));
 		else $query = Database::prepare(Database::get(), "SELECT id, url, thumbUrl, medium FROM ? WHERE checksum = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $checksum));
 
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, 'Could not check for existing photos with the same checksum (' . Database::get()->error . ')');
-			return false;
-		}
+		if ($result===false) return false;
 
 		if ($result->num_rows===1) {
 
@@ -343,7 +339,7 @@ final class Photo {
 				case 'image/jpeg': $sourceImg = imagecreatefromjpeg($url); break;
 				case 'image/png':  $sourceImg = imagecreatefrompng($url); break;
 				case 'image/gif':  $sourceImg = imagecreatefromgif($url); break;
-				default:           Log::error(__METHOD__, __LINE__, 'Type of photo is not supported');
+				default:           Log::error(Database::get(), __METHOD__, __LINE__, 'Type of photo is not supported');
 				                   return false;
 				                   break;
 			}
@@ -398,7 +394,7 @@ final class Photo {
 		if (hasPermissions(LYCHEE_UPLOADS_MEDIUM)===false) {
 
 			// Permissions are missing
-			Log::notice(__METHOD__, __LINE__, 'Skipped creation of medium-photo, because uploads/medium/ is missing or not readable and writable.');
+			Log::notice(Database::get(), __METHOD__, __LINE__, 'Skipped creation of medium-photo, because uploads/medium/ is missing or not readable and writable.');
 			$error = true;
 
 		}
@@ -423,7 +419,7 @@ final class Photo {
 			// Save image
 			try { $medium->writeImage($newUrl); }
 			catch (ImagickException $err) {
-				Log::notice(__METHOD__, __LINE__, 'Could not save medium-photo: ' . $err->getMessage());
+				Log::notice(Database::get(), __METHOD__, __LINE__, 'Could not save medium-photo: ' . $err->getMessage());
 				$error = true;
 			}
 
@@ -636,8 +632,12 @@ final class Photo {
 
 		// Get photo
 		$query  = Database::prepare(Database::get(), "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
-		$photos = Database::get()->query($query);
-		$photo  = $photos->fetch_assoc();
+		$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+		if ($photos===false) exit('Error: Could not get photo from database!');
+
+		// Get photo object
+		$photo = $photos->fetch_assoc();
 
 		// Parse photo
 		$photo['sysdate'] = date('d M. Y', substr($photo['id'], 0, -4));
@@ -659,8 +659,12 @@ final class Photo {
 
 				// Get album
 				$query  = Database::prepare(Database::get(), "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $photo['album']));
-				$albums = Database::get()->query($query);
-				$album  = $albums->fetch_assoc();
+				$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+				if ($albums===false) exit('Error: Could not get album of photo from database!');
+
+				// Get album object
+				$album = $albums->fetch_assoc();
 
 				// Parse album
 				$photo['public'] = ($album['public']==='1' ? '2' : $photo['public']);
@@ -799,26 +803,23 @@ final class Photo {
 
 		// Get photo
 		$query  = Database::prepare(Database::get(), "SELECT title, url FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
-		$photos = Database::get()->query($query);
+		$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-		if ($photos===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($photos===false) exit('Error: Could not get photo from database!');
 
 		// Get photo object
 		$photo = $photos->fetch_object();
 
 		// Photo not found
 		if ($photo===null) {
-			Log::error(__METHOD__, __LINE__, 'Album not found. Cannot start download.');
-			return false;
+			Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified photo');
+			exit('Error: Could not find specified photo!');
 		}
 
 		// Get extension
 		$extension = getExtension($photo->url);
 		if ($extension===false) {
-			Log::error(__METHOD__, __LINE__, 'Invalid photo extension');
+			Log::error(Database::get(), __METHOD__, __LINE__, 'Invalid photo extension');
 			return false;
 		}
 
@@ -866,15 +867,12 @@ final class Photo {
 
 		// Set title
 		$query  = Database::prepare(Database::get(), "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $title, $this->photoIDs));
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return true;
 
 	}
@@ -896,15 +894,12 @@ final class Photo {
 
 		// Set description
 		$query  = Database::prepare(Database::get(), "UPDATE ? SET description = '?' WHERE id IN ('?')", array(LYCHEE_TABLE_PHOTOS, $description, $this->photoIDs));
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return true;
 
 	}
@@ -927,7 +922,9 @@ final class Photo {
 
 		// Get photos
 		$query  = Database::prepare(Database::get(), "SELECT id, star FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
-		$photos = Database::get()->query($query);
+		$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+		if ($photos===false) return false;
 
 		// For each photo
 		while ($photo = $photos->fetch_object()) {
@@ -937,7 +934,7 @@ final class Photo {
 
 			// Set star
 			$query  = Database::prepare(Database::get(), "UPDATE ? SET star = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $star, $photo->id));
-			$result = Database::get()->query($query);
+			$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 			if ($result===false) $error = true;
 
@@ -946,10 +943,7 @@ final class Photo {
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($error===true) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($error===true) return false;
 		return true;
 
 	}
@@ -970,8 +964,12 @@ final class Photo {
 
 		// Get photo
 		$query  = Database::prepare(Database::get(), "SELECT public, album FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
-		$photos = Database::get()->query($query);
-		$photo  = $photos->fetch_object();
+		$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+		if ($photos===false) return 0;
+
+		// Get photo object
+		$photo = $photos->fetch_object();
 
 		// Check if public
 		if ($photo->public==='1') {
@@ -1017,23 +1015,24 @@ final class Photo {
 
 		// Get public
 		$query  = Database::prepare(Database::get(), "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
-		$photos = Database::get()->query($query);
-		$photo  = $photos->fetch_object();
+		$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+		if ($photos===false) return false;
+
+		// Get photo object
+		$photo = $photos->fetch_object();
 
 		// Invert public
 		$public = ($photo->public==0 ? 1 : 0);
 
 		// Set public
 		$query  = Database::prepare(Database::get(), "UPDATE ? SET public = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $public, $this->photoIDs));
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return true;
 
 	}
@@ -1053,15 +1052,12 @@ final class Photo {
 
 		// Set album
 		$query  = Database::prepare(Database::get(), "UPDATE ? SET album = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->photoIDs));
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return true;
 
 	}
@@ -1087,15 +1083,12 @@ final class Photo {
 
 		// Set tags
 		$query  = Database::prepare(Database::get(), "UPDATE ? SET tags = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $tags, $this->photoIDs));
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
-		if ($result===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($result===false) return false;
 		return true;
 
 	}
@@ -1113,14 +1106,14 @@ final class Photo {
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 0, func_get_args());
 
+		// Init vars
+		$error = false;
+
 		// Get photos
 		$query  = Database::prepare(Database::get(), "SELECT id, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
-		$photos = Database::get()->query($query);
+		$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-		if ($photos===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($photos===false) return false;
 
 		// For each photo
 		while ($photo = $photos->fetch_object()) {
@@ -1132,15 +1125,13 @@ final class Photo {
 			// Duplicate entry
 			$values = array(LYCHEE_TABLE_PHOTOS, $id, LYCHEE_TABLE_PHOTOS, $photo->id);
 			$query  = Database::prepare(Database::get(), "INSERT INTO ? (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum) SELECT '?' AS id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum FROM ? WHERE id = '?'", $values);
-			$result = Database::get()->query($query);
+			$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-			if ($result===false) {
-				Log::error(__METHOD__, __LINE__, Database::get()->error);
-				return false;
-			}
+			if ($result===false) $error = true;
 
 		}
 
+		if ($error===true) return false;
 		return true;
 
 	}
@@ -1158,14 +1149,14 @@ final class Photo {
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 0, func_get_args());
 
+		// Init vars
+		$error = false;
+
 		// Get photos
 		$query  = Database::prepare(Database::get(), "SELECT id, url, thumbUrl, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
-		$photos = Database::get()->query($query);
+		$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-		if ($photos===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if ($photos===false) return false;
 
 		// For each photo
 		while ($photo = $photos->fetch_object()) {
@@ -1180,44 +1171,42 @@ final class Photo {
 
 				// Delete big
 				if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) {
-					Log::error(__METHOD__, __LINE__, 'Could not delete photo in uploads/big/');
-					return false;
+					Log::error(Database::get(), __METHOD__, __LINE__, 'Could not delete photo in uploads/big/');
+					$error = true;
 				}
 
 				// Delete medium
 				if (file_exists(LYCHEE_UPLOADS_MEDIUM . $photo->url)&&!unlink(LYCHEE_UPLOADS_MEDIUM . $photo->url)) {
-					Log::error(__METHOD__, __LINE__, 'Could not delete photo in uploads/medium/');
-					return false;
+					Log::error(Database::get(), __METHOD__, __LINE__, 'Could not delete photo in uploads/medium/');
+					$error = true;
 				}
 
 				// Delete thumb
 				if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) {
-					Log::error(__METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/');
-					return false;
+					Log::error(Database::get(), __METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/');
+					$error = true;
 				}
 
 				// Delete thumb@2x
 				if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) {
-					Log::error(__METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
-					return false;
+					Log::error(Database::get(), __METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
+					$error = true;
 				}
 
 			}
 
 			// Delete db entry
 			$query  = Database::prepare(Database::get(), "DELETE FROM ? WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $photo->id));
-			$result = Database::get()->query($query);
+			$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-			if ($result===false) {
-				Log::error(__METHOD__, __LINE__, Database::get()->error);
-				return false;
-			}
+			if ($result===false) $error = true;
 
 		}
 
 		// Call plugins
 		Plugins::get()->activate(__METHOD__, 1, func_get_args());
 
+		if ($error===true) return false;
 		return true;
 
 	}

+ 8 - 23
php/Modules/Settings.php

@@ -12,7 +12,7 @@ final class Settings {
 
 		// Execute query
 		$query    = Database::prepare(Database::get(), "SELECT * FROM ?", array(LYCHEE_TABLE_SETTINGS));
-		$settings = Database::get()->query($query);
+		$settings = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		// Add each to return
 		while ($setting = $settings->fetch_object()) $return[$setting->key] = $setting->value;
@@ -43,7 +43,7 @@ final class Settings {
 
 		}
 
-		$result = Database::get()->query($query);
+		$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
 		if ($result===false) return false;
 		return true;
@@ -79,10 +79,7 @@ final class Settings {
 		// Execute query
 		// Do not prepare $username because it is hashed and save
 		// Preparing (escaping) the username would destroy the hash
-		if (self::set('username', $username, true)===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if (self::set('username', $username, true)===false) return false;
 		return true;
 
 	}
@@ -97,10 +94,7 @@ final class Settings {
 
 		// Do not prepare $password because it is hashed and save
 		// Preparing (escaping) the password would destroy the hash
-		if (self::set('password', $password, true)===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if (self::set('password', $password, true)===false) return false;
 		return true;
 
 	}
@@ -108,14 +102,11 @@ final class Settings {
 	public static function setDropboxKey($dropboxKey) {
 
 		if (strlen($dropboxKey)<1||strlen($dropboxKey)>50) {
-			Log::notice(__METHOD__, __LINE__, 'Dropbox key is either too short or too long');
+			Log::notice(Database::get(), __METHOD__, __LINE__, 'Dropbox key is either too short or too long');
 			return false;
 		}
 
-		if (self::set('dropboxKey', $dropboxKey)===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if (self::set('dropboxKey', $dropboxKey)===false) return false;
 		return true;
 
 	}
@@ -152,10 +143,7 @@ final class Settings {
 		// Do not prepare $sorting because it is a true statement
 		// Preparing (escaping) the sorting would destroy it
 		// $sorting is save and can't contain user-input
-		if (self::set('sortingPhotos', $sorting, true)===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if (self::set('sortingPhotos', $sorting, true)===false) return false;
 		return true;
 
 	}
@@ -189,10 +177,7 @@ final class Settings {
 		// Do not prepare $sorting because it is a true statement
 		// Preparing (escaping) the sorting would destroy it
 		// $sorting is save and can't contain user-input
-		if (self::set('sortingAlbums', $sorting, true)===false) {
-			Log::error(__METHOD__, __LINE__, Database::get()->error);
-			return false;
-		}
+		if (self::set('sortingAlbums', $sorting, true)===false) return false;
 		return true;
 
 	}

+ 17 - 13
php/database/update_020700.php

@@ -10,34 +10,38 @@ if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 
 // Add medium to photos
 $query  = Database::prepare($connection, "SELECT `medium` FROM `?` LIMIT 1", array(LYCHEE_TABLE_PHOTOS));
-$result = $connection->query($query);
+$result = Database::execute($connection, $query, 'update_020700', __LINE__);
+
 if ($result===false) {
+
 	$query  = Database::prepare($connection, "ALTER TABLE `?` ADD `medium` TINYINT(1) NOT NULL DEFAULT 0", array(LYCHEE_TABLE_PHOTOS));
-	$result = $connection->query($query);
-	if ($result===false) {
-		Log::error('update_020700', __LINE__, 'Could not update database (' . $connection->error . ')');
-		return false;
-	}
+	$result = Database::execute($connection, $query, 'update_020700', __LINE__);
+
+	if ($result===false) return false;
+
 }
 
 // Create medium folder
 if (is_dir(LYCHEE_UPLOADS_MEDIUM)===false) {
+
 	// Only create the folder when it is missing
 	if (@mkdir(LYCHEE_UPLOADS_MEDIUM)===false) {
-		Log::error('update_020700', __LINE__, 'Could not create medium-folder');
+		Log::error($connection, 'update_020700', __LINE__, 'Could not create medium-folder');
 	}
+
 }
 
 // Add medium to settings
 $query  = Database::prepare($connection, "SELECT `key` FROM `?` WHERE `key` = 'medium' LIMIT 1", array(LYCHEE_TABLE_SETTINGS));
-$result = $connection->query($query);
+$result = Database::execute($connection, $query, 'update_020700', __LINE__);
+
 if ($result->num_rows===0) {
+
 	$query  = Database::prepare($connection, "INSERT INTO `?` (`key`, `value`) VALUES ('medium', '1')", array(LYCHEE_TABLE_SETTINGS));
-	$result = $connection->query($query);
-	if ($result===false) {
-		Log::error('update_020700', __LINE__, 'Could not update database (' . $connection->error . ')');
-		return false;
-	}
+	$result = Database::execute($connection, $query, 'update_020700', __LINE__);
+
+	if ($result===false) return false;
+
 }
 
 // Set version

+ 10 - 15
php/database/update_030000.php

@@ -11,26 +11,21 @@ if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 // Remove login
 // Login now saved as crypt without md5. Legacy code has been removed.
 $query  = Database::prepare($connection, "UPDATE `?` SET `value` = '' WHERE `key` = 'username' LIMIT 1", array(LYCHEE_TABLE_SETTINGS));
-$result = $connection->query($query);
-if ($result===false) {
-	Log::error('update_030000', __LINE__, 'Could not reset username (' . $connection->error . ')');
-	return false;
-}
+$result = Database::execute($connection, $query, 'update_030000', __LINE__);
+
+if ($result===false) return false;
+
 $query  = Database::prepare($connection, "UPDATE `?` SET `value` = '' WHERE `key` = 'password' LIMIT 1", array(LYCHEE_TABLE_SETTINGS));
-$result = $connection->query($query);
-if ($result===false) {
-	Log::error('update_030000', __LINE__, 'Could not reset password (' . $connection->error . ')');
-	return false;
-}
+$result = Database::execute($connection, $query, 'update_030000', __LINE__);
+
+if ($result===false) return false;
 
 // Make public albums private and reset password
 // Password now saved as crypt without md5. Legacy code has been removed.
 $query  = Database::prepare($connection, "UPDATE `?` SET `public` = 0, `password` = NULL", array(LYCHEE_TABLE_ALBUMS));
-$result = $connection->query($query);
-if ($result===false) {
-	Log::error('update_030000', __LINE__, 'Could not reset public albums (' . $connection->error . ')');
-	return false;
-}
+$result = Database::execute($connection, $query, 'update_030000', __LINE__);
+
+if ($result===false) return false;
 
 // Set version
 if (Database::setVersion($connection, '030000')===false) return false;

+ 27 - 27
php/database/update_030001.php

@@ -10,51 +10,51 @@ if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 
 // Change length of photo title
 $query  = Database::prepare($connection, "ALTER TABLE `?` CHANGE `title` `title` VARCHAR( 100 ) NOT NULL DEFAULT ''", array(LYCHEE_TABLE_PHOTOS));
-$result = $connection->query($query);
-if ($result===false) {
-	Log::error('update_030001', __LINE__, 'Could not update database (' . $connection->error . ')');
-	return false;
-}
+$result = Database::execute($connection, $query, 'update_030001', __LINE__);
+
+if ($result===false) return false;
 
 // Change length of album title
 $query  = Database::prepare($connection, "ALTER TABLE `?` CHANGE `title` `title` VARCHAR( 100 ) NOT NULL DEFAULT ''", array(LYCHEE_TABLE_ALBUMS));
-$result = $connection->query($query);
-if ($result===false) {
-	Log::error('update_030001', __LINE__, 'Could not update database (' . $connection->error . ')');
-	return false;
-}
+$result = Database::execute($connection, $query, 'update_030001', __LINE__);
+
+if ($result===false) return false;
 
 // Add album sorting to settings
 $query  = Database::prepare($connection, "SELECT `key` FROM `?` WHERE `key` = 'sortingAlbums' LIMIT 1", array(LYCHEE_TABLE_SETTINGS));
-$result = $connection->query($query);
+$result = Database::execute($connection, $query, 'update_030001', __LINE__);
+
+if ($result===false) return false;
+
 if ($result->num_rows===0) {
+
 	$query  = Database::prepare($connection, "INSERT INTO `?` (`key`, `value`) VALUES ('sortingAlbums', 'ORDER BY id DESC')", array(LYCHEE_TABLE_SETTINGS));
-	$result = $connection->query($query);
-	if ($result===false) {
-		Log::error('update_030001', __LINE__, 'Could not update database (' . $connection->error . ')');
-		return false;
-	}
+	$result = Database::execute($connection, $query, 'update_030001', __LINE__);
+
+	if ($result===false) return false;
+
 }
 
 // Rename sorting to sortingPhotos
 $query  = Database::prepare($connection, "UPDATE ? SET `key` = 'sortingPhotos' WHERE `key` = 'sorting' LIMIT 1", array(LYCHEE_TABLE_SETTINGS));
-$result = $connection->query($query);
-if ($result===false) {
-	Log::error('update_030001', __LINE__, 'Could not update database (' . $connection->error . ')');
-	return false;
-}
+$result = Database::execute($connection, $query, 'update_030001', __LINE__);
+
+if ($result===false) return false;
 
 // Add identifier to settings
 $query  = Database::prepare($connection, "SELECT `key` FROM `?` WHERE `key` = 'identifier' LIMIT 1", array(LYCHEE_TABLE_SETTINGS));
-$result = $connection->query($query);
+$result = Database::execute($connection, $query, 'update_030001', __LINE__);
+
+if ($result===false) return false;
+
 if ($result->num_rows===0) {
+
 	$identifier = md5(microtime(true));
 	$query      = Database::prepare($connection, "INSERT INTO `?` (`key`, `value`) VALUES ('identifier', '?')", array(LYCHEE_TABLE_SETTINGS, $identifier));
-	$result     = $connection->query($query);
-	if ($result===false) {
-		Log::error('update_030001', __LINE__, 'Could not update database (' . $connection->error . ')');
-		return false;
-	}
+	$result     = Database::execute($connection, $query, 'update_030001', __LINE__);
+
+	if ($result===false) return false;
+
 }
 
 // Set version

+ 9 - 6
php/database/update_030003.php

@@ -10,14 +10,17 @@ if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 
 // Add skipDuplicates to settings
 $query  = Database::prepare($connection, "SELECT `key` FROM `?` WHERE `key` = 'skipDuplicates' LIMIT 1", array(LYCHEE_TABLE_SETTINGS));
-$result = $connection->query($query);
+$result = Database::execute($connection, $query, 'update_030003', __LINE__);
+
+if ($result===false) return false;
+
 if ($result->num_rows===0) {
+
 	$query  = Database::prepare($connection, "INSERT INTO `?` (`key`, `value`) VALUES ('skipDuplicates', '0')", array(LYCHEE_TABLE_SETTINGS));
-	$result = $connection->query($query);
-	if ($result===false) {
-		Log::error('update_030003', __LINE__, 'Could not update database (' . $connection->error . ')');
-		return false;
-	}
+	$result = Database::execute($connection, $query, 'update_030003', __LINE__);
+
+	if ($result===false) return false;
+
 }
 
 // Set version

+ 3 - 6
php/helpers/getGraphHeader.php

@@ -9,17 +9,14 @@ function getGraphHeader($photoID) {
 	if ($photo->getPublic('')===false) return false;
 
 	$query  = Database::prepare(Database::get(), "SELECT title, description, url, medium FROM ? WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $photoID));
-	$result = Database::get()->query($query);
+	$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
 
-	if ($result===false) {
-		Log::error(__METHOD__, __LINE__, Database::get()->error);
-		return false;
-	}
+	if ($result===false) return false;
 
 	$row = $result->fetch_object();
 
 	if ($row===null) {
-		Log::error(__METHOD__, __LINE__, 'Could not find photo in database');
+		Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find photo in database');
 		return false;
 	}
 

+ 9 - 3
php/helpers/search.php

@@ -18,7 +18,9 @@ function search($term) {
 	 */
 
 	$query  = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE title LIKE '%?%' OR description LIKE '%?%' OR tags LIKE '%?%'", array(LYCHEE_TABLE_PHOTOS, $term, $term, $term));
-	$result = Database::get()->query($query);
+	$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+	if ($result===false) return false;
 
 	while($photo = $result->fetch_assoc()) {
 
@@ -32,7 +34,9 @@ function search($term) {
 	 */
 
 	$query  = Database::prepare(Database::get(), "SELECT id, title, public, sysstamp, password FROM ? WHERE title LIKE '%?%' OR description LIKE '%?%'", array(LYCHEE_TABLE_ALBUMS, $term, $term));
-	$result = Database::get()->query($query);
+	$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+	if ($result===false) return false;
 
 	while($album = $result->fetch_assoc()) {
 
@@ -41,7 +45,9 @@ function search($term) {
 
 		// Thumbs
 		$query  = Database::prepare(Database::get(), "SELECT thumbUrl FROM ? WHERE album = '?' " . Settings::get()['sortingPhotos'] . " LIMIT 0, 3", array(LYCHEE_TABLE_PHOTOS, $album['id']));
-		$thumbs = Database::get()->query($query);
+		$thumbs = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+		if ($thumbs===false) return false;
 
 		// For each thumb
 		$k = 0;