Browse Source

Added Response class to handle exit() responses

Tobias Reich 8 years ago
parent
commit
f4e909895f

+ 11 - 11
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

+ 1 - 1
php/Modules/Config.php

@@ -25,7 +25,7 @@ final class Config {
 		// Save config.php
 $config = "<?php
 
-if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
+if(!defined('LYCHEE')) Response::error('Direct access is not allowed!');
 
 // Database configuration
 \$dbHost = '$host'; // Host of the database

+ 5 - 5
php/Modules/Database.php

@@ -48,18 +48,18 @@ final class Database {
 		$connection = self::connect($host, $user, $password);
 
 		// Check if the connection was successful
-		if ($connection===false) exit('Error: ' . $connection->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;
 

+ 18 - 18
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

+ 25 - 0
php/Modules/Response.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace Lychee\Modules;
+
+final class Response {
+
+	public static function warning($msg) {
+
+		exit('Warning: ' . $msg);
+
+	}
+
+	public static function error($msg) {
+
+		exit('Error: ' . $msg);
+
+	}
+
+	public static function json($str) {
+
+		exit(json_encode($str));
+
+	}
+
+}

+ 7 - 7
php/Modules/Settings.php

@@ -55,16 +55,16 @@ final class Settings {
 		if ($oldPassword===self::get()['password']||self::get()['password']===crypt($oldPassword, self::get()['password'])) {
 
 			// Save username
-			if (self::setUsername($username)!==true) exit('Error: Updating username failed!');
+			if (self::setUsername($username)!==true) Response::error('Updating username failed!');
 
 			// Save password
-			if (self::setPassword($password)!==true) exit('Error: Updating password failed!');
+			if (self::setPassword($password)!==true) Response::error('Updating password failed!');
 
 			return true;
 
 		}
 
-		exit('Error: Current password entered incorrectly!');
+		Response::error('Current password entered incorrectly!');
 
 	}
 
@@ -125,7 +125,7 @@ final class Settings {
 			case 'type':        $sorting .= 'type'; break;
 			case 'star':        $sorting .= 'star'; break;
 			case 'takestamp':   $sorting .= 'takestamp'; break;
-			default:            exit('Error: Unknown type for sorting!');
+			default:            Response::error('Unknown type for sorting!');
 
 		}
 
@@ -136,7 +136,7 @@ final class Settings {
 
 			case 'ASC':  $sorting .= 'ASC'; break;
 			case 'DESC': $sorting .= 'DESC'; break;
-			default:     exit('Error: Unknown order for sorting!');
+			default:     Response::error('Unknown order for sorting!');
 
 		}
 
@@ -159,7 +159,7 @@ final class Settings {
 			case 'title':       $sorting .= 'title'; break;
 			case 'description': $sorting .= 'description'; break;
 			case 'public':      $sorting .= 'public'; break;
-			default:            exit('Error: Unknown type for sorting!');
+			default:            Response::error('Unknown type for sorting!');
 
 		}
 
@@ -170,7 +170,7 @@ final class Settings {
 
 			case 'ASC':  $sorting .= 'ASC'; break;
 			case 'DESC': $sorting .= 'DESC'; break;
-			default:     exit('Error: Unknown order for sorting!');
+			default:     Response::error('Unknown order for sorting!');
 
 		}
 

+ 1 - 1
php/Modules/Validator.php

@@ -6,7 +6,7 @@ final class Validator {
 
 	public static function required($available = false, $function) {
 
-		if ($available===false) exit("Error: Missing parameters. Can not execute function $function.");
+		if ($available===false) Response::error('Missing parameters. Can not execute function ' . $function);
 
 		return true;
 

+ 6 - 5
php/index.php

@@ -8,6 +8,7 @@
 namespace Lychee;
 
 use Lychee\Modules\Config;
+use Lychee\Modules\Response;
 use Lychee\Modules\Settings;
 use Lychee\Modules\Validator;
 
@@ -38,10 +39,10 @@ if (!empty($fn)) {
 	date_default_timezone_set('UTC');
 
 	// Validate parameters
-	if (isset($_POST['albumIDs'])&&Validator::isAlbumIDs($_POST['albumIDs'])===false) exit('Error: Wrong parameter type for albumIDs!');
-	if (isset($_POST['photoIDs'])&&Validator::isPhotoIDs($_POST['photoIDs'])===false) exit('Error: Wrong parameter type for photoIDs!');
-	if (isset($_POST['albumID'])&&Validator::isAlbumID($_POST['albumID'])==false)     exit('Error: Wrong parameter type for albumID!');
-	if (isset($_POST['photoID'])&&Validator::isPhotoID($_POST['photoID'])==false)     exit('Error: Wrong parameter type for photoID!');
+	if (isset($_POST['albumIDs'])&&Validator::isAlbumIDs($_POST['albumIDs'])===false) Response::error('Wrong parameter type for albumIDs!');
+	if (isset($_POST['photoIDs'])&&Validator::isPhotoIDs($_POST['photoIDs'])===false) Response::error('Wrong parameter type for photoIDs!');
+	if (isset($_POST['albumID'])&&Validator::isAlbumID($_POST['albumID'])==false)     Response::error('Wrong parameter type for albumID!');
+	if (isset($_POST['photoID'])&&Validator::isPhotoID($_POST['photoID'])==false)     Response::error('Wrong parameter type for photoID!');
 
 	// Check if a configuration exists
 	if (Config::exists()===false) {
@@ -82,7 +83,7 @@ if (!empty($fn)) {
 
 } else {
 
-	exit('Error: No API function specified!');
+	Response::error('No API function specified!');
 
 }