From f4e909895f6daa24bac9bf6695539c50dfe2c169 Mon Sep 17 00:00:00 2001 From: Tobias Reich Date: Sun, 7 Feb 2016 00:16:48 +0100 Subject: [PATCH] Added Response class to handle exit() responses --- php/Modules/Album.php | 22 +++++++++++----------- php/Modules/Config.php | 2 +- php/Modules/Database.php | 10 +++++----- php/Modules/Photo.php | 36 ++++++++++++++++++------------------ php/Modules/Response.php | 25 +++++++++++++++++++++++++ php/Modules/Settings.php | 14 +++++++------- php/Modules/Validator.php | 2 +- php/index.php | 11 ++++++----- 8 files changed, 74 insertions(+), 48 deletions(-) create mode 100644 php/Modules/Response.php diff --git a/php/Modules/Album.php b/php/Modules/Album.php index 9007032..5537450 100644 --- a/php/Modules/Album.php +++ b/php/Modules/Album.php @@ -120,7 +120,7 @@ final class Album { $photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__); $previousPhotoID = ''; - if ($photos===false) exit('Error: Could not get photos of album from database!'); + if ($photos===false) Response::error('Could not get photos of album from database!'); while ($photo = $photos->fetch_assoc()) { @@ -192,7 +192,7 @@ final class Album { // Execute query $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__); - if ($albums===false) exit('Error: Could not get albums from database!'); + if ($albums===false) Response::error('Could not get albums from database!'); // For each album while ($album = $albums->fetch_assoc()) { @@ -208,7 +208,7 @@ final class Album { $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::execute(Database::get(), $query, __METHOD__, __LINE__); - if ($thumbs===false) exit('Error: Could not get thumbs of album from database!'); + if ($thumbs===false) Response::error('Could not get thumbs of album from database!'); // For each thumb $k = 0; @@ -252,7 +252,7 @@ final class Album { $unsorted = Database::execute(Database::get(), $query, __METHOD__, __LINE__); $i = 0; - if ($unsorted===false) exit('Error: Could not get unsorted photos from database!'); + if ($unsorted===false) Response::error('Could not get unsorted photos from database!'); $return['unsorted'] = array( 'thumbs' => array(), @@ -274,7 +274,7 @@ final class Album { $starred = Database::execute(Database::get(), $query, __METHOD__, __LINE__); $i = 0; - if ($starred===false) exit('Error: Could not get starred photos from database!'); + if ($starred===false) Response::error('Could not get starred photos from database!'); $return['starred'] = array( 'thumbs' => array(), @@ -296,7 +296,7 @@ final class Album { $public = Database::execute(Database::get(), $query, __METHOD__, __LINE__); $i = 0; - if ($public===false) exit('Error: Could not get public photos from database!'); + if ($public===false) Response::error('Could not get public photos from database!'); $return['public'] = array( 'thumbs' => array(), @@ -318,7 +318,7 @@ final class Album { $recent = Database::execute(Database::get(), $query, __METHOD__, __LINE__); $i = 0; - if ($recent===false) exit('Error: Could not get recent photos from database!'); + if ($recent===false) Response::error('Could not get recent photos from database!'); $return['recent'] = array( 'thumbs' => array(), @@ -376,7 +376,7 @@ final class Album { $query = Database::prepare(Database::get(), "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs)); $album = Database::execute(Database::get(), $query, __METHOD__, __LINE__); - if ($album===false) exit('Error: Could not get album from database!'); + if ($album===false) Response::error('Could not get album from database!'); // Get album object $album = $album->fetch_object(); @@ -384,7 +384,7 @@ final class Album { // Photo not found if ($album===null) { Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album'); - exit('Error: Could not find specified album!'); + Response::error('Could not find specified album!'); } // Set title @@ -407,12 +407,12 @@ final class Album { // Execute query $photos = Database::execute(Database::get(), $photos, __METHOD__, __LINE__); - if ($album===null) exit('Error: Could not get photos from database!'); + if ($album===null) Response::error('Could not get photos from database!'); // Check if album empty if ($photos->num_rows==0) { Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive without images'); - exit('Error: Could not create ZipArchive without images!'); + Response::error('Could not create ZipArchive without images!'); } // Parse each path diff --git a/php/Modules/Config.php b/php/Modules/Config.php index 8ce5dc5..d7f3914 100644 --- a/php/Modules/Config.php +++ b/php/Modules/Config.php @@ -25,7 +25,7 @@ final class Config { // Save config.php $config = "connect_error); + if ($connection===false) Response::error('' . $connection->connect_error); - if (!self::setCharset($connection)) exit('Error: Could not set database charset!'); + if (!self::setCharset($connection)) Response::error('Could not set database charset!'); // Create database - if (!self::createDatabase($connection, $name)) exit('Error: Could not create database!'); + if (!self::createDatabase($connection, $name)) Response::error('Could not create database!'); // Create tables - if (!self::createTables($connection)) exit('Error: Could not create tables!'); + if (!self::createTables($connection)) Response::error('Could not create tables!'); // Update database - if (!self::update($connection, $name)) exit('Error: Could not update database and tables!'); + if (!self::update($connection, $name)) Response::error('Could not update database and tables!'); $this->connection = $connection; diff --git a/php/Modules/Photo.php b/php/Modules/Photo.php index 6c0ac3c..59d23e9 100755 --- a/php/Modules/Photo.php +++ b/php/Modules/Photo.php @@ -40,7 +40,7 @@ final class Photo { hasPermissions(LYCHEE_UPLOADS_BIG)===false|| hasPermissions(LYCHEE_UPLOADS_THUMB)===false) { 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!'); + Response::error('An upload-folder is missing or not readable and writable!'); } // Call plugins @@ -82,35 +82,35 @@ final class Photo { if ($file['error']===UPLOAD_ERR_INI_SIZE) { 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!'); + Response::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(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!'); + Response::error('The uploaded file was only partially uploaded!'); } // Check if writing file to disk failed if ($file['error']===UPLOAD_ERR_CANT_WRITE) { 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!'); + Response::error('Failed to write photo to disk!'); } // Check if a extension stopped the file upload if ($file['error']===UPLOAD_ERR_EXTENSION) { 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!'); + Response::error('A PHP extension stopped the file upload!'); } // Check if the upload was successful if ($file['error']!==UPLOAD_ERR_OK) { Log::error(Database::get(), __METHOD__, __LINE__, 'Upload contains an error (' . $file['error'] . ')'); if ($returnOnError===true) return false; - exit('Error: Upload failed!'); + Response::error('Upload failed!'); } // Verify extension @@ -118,7 +118,7 @@ final class Photo { if (!in_array(strtolower($extension), self::$validExtensions, true)) { Log::error(Database::get(), __METHOD__, __LINE__, 'Photo format not supported'); if ($returnOnError===true) return false; - exit('Error: Photo format not supported!'); + Response::error('Photo format not supported!'); } // Verify image @@ -126,7 +126,7 @@ final class Photo { if (!in_array($type, self::$validTypes, true)) { Log::error(Database::get(), __METHOD__, __LINE__, 'Photo type not supported'); if ($returnOnError===true) return false; - exit('Error: Photo type not supported!'); + Response::error('Photo type not supported!'); } // Generate id @@ -143,7 +143,7 @@ final class Photo { if ($checksum===false) { 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!'); + Response::error('Could not calculate checksum for photo!'); } // Check if image exists based on checksum @@ -173,13 +173,13 @@ final class Photo { if (!@copy($tmp_name, $path)) { 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!'); + Response::error('Could not copy photo to uploads!'); } else @unlink($tmp_name); } else { if (!@move_uploaded_file($tmp_name, $path)) { 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!'); + Response::error('Could not move photo to uploads!'); } } @@ -190,7 +190,7 @@ final class Photo { if (Settings::get()['skipDuplicates']==='1') { 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.'); + Response::warning('This photo has been skipped because it\'s already in your library.'); } } @@ -220,7 +220,7 @@ final class Photo { if (!$this->createThumb($path, $photo_name, $info['type'], $info['width'], $info['height'])) { 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!'); + Response::error('Could not create thumbnail for photo!'); } // Create Medium @@ -239,7 +239,7 @@ final class Photo { if ($result===false) { if ($returnOnError===true) return false; - exit('Error: Could not save photo in database!'); + Response::error('Could not save photo in database!'); } } @@ -637,7 +637,7 @@ final class Photo { $query = Database::prepare(Database::get(), "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs)); $photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__); - if ($photos===false) exit('Error: Could not get photo from database!'); + if ($photos===false) Response::error('Could not get photo from database!'); // Get photo object $photo = $photos->fetch_assoc(); @@ -664,7 +664,7 @@ final class Photo { $query = Database::prepare(Database::get(), "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $photo['album'])); $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__); - if ($albums===false) exit('Error: Could not get album of photo from database!'); + if ($albums===false) Response::error('Could not get album of photo from database!'); // Get album object $album = $albums->fetch_assoc(); @@ -808,7 +808,7 @@ final class Photo { $query = Database::prepare(Database::get(), "SELECT title, url FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs)); $photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__); - if ($photos===false) exit('Error: Could not get photo from database!'); + if ($photos===false) Response::error('Could not get photo from database!'); // Get photo object $photo = $photos->fetch_object(); @@ -816,7 +816,7 @@ final class Photo { // Photo not found if ($photo===null) { Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified photo'); - exit('Error: Could not find specified photo!'); + Response::error('Could not find specified photo!'); } // Get extension diff --git a/php/Modules/Response.php b/php/Modules/Response.php new file mode 100644 index 0000000..01c4c10 --- /dev/null +++ b/php/Modules/Response.php @@ -0,0 +1,25 @@ +