From 9e8a8513b20cfd770429e35135516c6399444930 Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Tue, 15 Sep 2015 17:36:21 +0000 Subject: Indent and format project to PSR-2 style PSR-2 [1] is a highly regarded style guide in the PHP community according to Stack Overflow [2]. The indentation was out of place in the source code. I've seen a combination of 1 space tabs, 3 space tabs, 4 space tabs and 5 space tabs. Some closing braces on the same line and a lot of the code (especially moe code) had reached the point of losing readability. Converted with php-cs-fixer and some hand adjustment. [1]: http://www.php-fig.org/psr/psr-2/ [2]: https://stackoverflow.com/questions/3638083/list-of-highly-regarded -php-style-guides --- static/classes/Response.class.php | 204 ++++++++++--------- static/classes/UploadException.class.php | 92 ++++----- static/classes/UploadedFile.class.php | 44 +++-- static/grill.php | 23 +-- static/includes/database.inc.php | 2 +- static/includes/settings.inc.php | 12 +- static/upload.php | 327 ++++++++++++++++--------------- 7 files changed, 366 insertions(+), 338 deletions(-) (limited to 'static') diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index 41f946c..25121ce 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -1,98 +1,106 @@ -type = $response_type; - break; - default: - header('Content-Type: application/json; charset=UTF-8'); - $this->type = 'json'; - break; - } - } - - public function error ($code, $desc) { - $response = null; - - switch ($this->type) { - case 'csv': - $response = $this->csv_error($desc); - break; - case 'gyazo': - $response = $this->gyazo_error($code, $desc); - break; - default: - $response = $this->json_error($code, $desc); - break; - } - - http_response_code($code); - echo $response; - } - - public function send ($files) { - $response = null; - - switch ($this->type) { - case 'csv': - $response = $this->csv_success($files); - break; - case 'gyazo': - $response = $this->gyazo_success($files); - break; - default: - $response = $this->json_success($files); - break; - } - - http_response_code(200); - echo $response; - } - - private static function csv_error ($description) { - return "error\n" . $description . "\n"; - } - - private static function csv_success ($files) { - $result = "name,url,hash,size\n"; - foreach ($files as $file) { - $result .= $file['name'] . "," . - $file['url'] . "," . - $file['hash'] . "," . - $file['size'] . "\n"; - } - - return $result; - } - - private static function gyazo_error ($code, $description) { - return "ERROR: (" . $code . ") " . $description; - } - - private static function gyazo_success ($files) { - return POMF_URL . $files[0]['url']; - } - - private static function json_error ($code, $description) { - return json_encode(array( - 'success' => false, - 'errorcode' => $code, - 'description' => $description - )); - } - - private static function json_success ($files) { - return json_encode(array( - 'success' => true, - 'files' => $files - )); - } -} - -?> \ No newline at end of file +type = $response_type; + break; + default: + header('Content-Type: application/json; charset=UTF-8'); + $this->type = 'json'; + break; + } + } + + public function error($code, $desc) + { + $response = null; + + switch ($this->type) { + case 'csv': + $response = $this->csv_error($desc); + break; + case 'gyazo': + $response = $this->gyazo_error($code, $desc); + break; + default: + $response = $this->json_error($code, $desc); + break; + } + + http_response_code($code); + echo $response; + } + + public function send($files) + { + $response = null; + + switch ($this->type) { + case 'csv': + $response = $this->csv_success($files); + break; + case 'gyazo': + $response = $this->gyazo_success($files); + break; + default: + $response = $this->json_success($files); + break; + } + + http_response_code(200); + echo $response; + } + + private static function csv_error($description) + { + return "error\n".$description."\n"; + } + + private static function csv_success($files) + { + $result = "name,url,hash,size\n"; + foreach ($files as $file) { + $result .= $file['name'].','. + $file['url'].','. + $file['hash'].','. + $file['size']."\n"; + } + + return $result; + } + + private static function gyazo_error($code, $description) + { + return 'ERROR: ('.$code.') '.$description; + } + + private static function gyazo_success($files) + { + return POMF_URL.$files[0]['url']; + } + + private static function json_error($code, $description) + { + return json_encode(array( + 'success' => false, + 'errorcode' => $code, + 'description' => $description, + )); + } + + private static function json_success($files) + { + return json_encode(array( + 'success' => true, + 'files' => $files, + )); + } +} diff --git a/static/classes/UploadException.class.php b/static/classes/UploadException.class.php index b1b4839..66be853 100755 --- a/static/classes/UploadException.class.php +++ b/static/classes/UploadException.class.php @@ -1,45 +1,47 @@ -codeToMessage($code); - parent::__construct($message, 500); - } - - private function codeToMessage($code) { - switch ($code) { - case UPLOAD_ERR_INI_SIZE: - $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini"; - break; - case UPLOAD_ERR_FORM_SIZE: - $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was " . - "specified in the HTML form"; - break; - case UPLOAD_ERR_PARTIAL: - $message = "The uploaded file was only partially uploaded"; - break; - case UPLOAD_ERR_NO_FILE: - $message = "No file was uploaded"; - break; - case UPLOAD_ERR_NO_TMP_DIR: - $message = "Missing a temporary folder"; - break; - case UPLOAD_ERR_CANT_WRITE: - $message = "Failed to write file to disk"; - break; - case UPLOAD_ERR_EXTENSION: - $message = "File upload stopped by extension"; - break; - - default: - $message = "Unknown upload error"; - break; - } - return $message; - } -} - -?> \ No newline at end of file +codeToMessage($code); + parent::__construct($message, 500); + } + + private function codeToMessage($code) + { + switch ($code) { + case UPLOAD_ERR_INI_SIZE: + $message = 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; + break; + case UPLOAD_ERR_FORM_SIZE: + $message = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was '. + 'specified in the HTML form'; + break; + case UPLOAD_ERR_PARTIAL: + $message = 'The uploaded file was only partially uploaded'; + break; + case UPLOAD_ERR_NO_FILE: + $message = 'No file was uploaded'; + break; + case UPLOAD_ERR_NO_TMP_DIR: + $message = 'Missing a temporary folder'; + break; + case UPLOAD_ERR_CANT_WRITE: + $message = 'Failed to write file to disk'; + break; + case UPLOAD_ERR_EXTENSION: + $message = 'File upload stopped by extension'; + break; + + default: + $message = 'Unknown upload error'; + break; + } + + return $message; + } +} diff --git a/static/classes/UploadedFile.class.php b/static/classes/UploadedFile.class.php index 1cf8914..5e8ae1c 100644 --- a/static/classes/UploadedFile.class.php +++ b/static/classes/UploadedFile.class.php @@ -1,27 +1,29 @@ sha1) - $this->sha1 = sha1_file($this->tempfile); + /** + * Generates the SHA1 or returns the cached SHA1 hash for the file. + * + * @return string + */ + public function get_sha1() + { + if (!$this->sha1) { + $this->sha1 = sha1_file($this->tempfile); + } - return $this->sha1; - } + return $this->sha1; + } } -?> diff --git a/static/grill.php b/static/grill.php index 34bdce1..5984b12 100644 --- a/static/grill.php +++ b/static/grill.php @@ -1,16 +1,17 @@ diff --git a/static/includes/database.inc.php b/static/includes/database.inc.php index 2ed0274..5049487 100644 --- a/static/includes/database.inc.php +++ b/static/includes/database.inc.php @@ -1,6 +1,6 @@ diff --git a/static/includes/settings.inc.php b/static/includes/settings.inc.php index b8a213c..37bc4a0 100644 --- a/static/includes/settings.inc.php +++ b/static/includes/settings.inc.php @@ -17,11 +17,9 @@ define('POMF_FILES_LENGTH', 6); define('POMF_URL', 'https://i.pantsu.cat/'); $doubledots = array_map('strrev', array( - 'tar.gz', - 'tar.bz', - 'tar.bz2', - 'tar.xz', - 'user.js' + 'tar.gz', + 'tar.bz', + 'tar.bz2', + 'tar.xz', + 'user.js', )); - -?> diff --git a/static/upload.php b/static/upload.php index 6e54ae1..0504e54 100644 --- a/static/upload.php +++ b/static/upload.php @@ -1,197 +1,214 @@ name, PATHINFO_EXTENSION); - - // Check if extension is a double-dot extension and, if true, override $ext - $revname = strrev($file->name); - foreach ($doubledots as $ddot) { - if (stripos($revname, $ddot) === 0) { - $ext = strrev($ddot); - } - } - - - do { - // Iterate until we reach the maximum number of retries - if ($tries-- == 0) throw new Exception('Gave up trying to find an unused name', 500); - - $chars = range('a', 'z'); - $name = ''; - for ($i = 0; $i < $length; $i++) { - $name .= $chars[array_rand($chars)]; - } - - // Add the extension to the file name - if (isset($ext) && $ext !== '') - $name .= '.' . strip_tags($ext); - - // Check if a file with the same name does already exist in the database - $q = $db->prepare('SELECT COUNT(name) FROM pomf WHERE name = (:name)'); - $q->bindValue(':name', $name, PDO::PARAM_STR); - $q->execute(); - $result = $q->fetchColumn(); - // If it does, generate a new name - } while($result > 0); - - return $name; +function generate_name($file) +{ + global $db; + global $doubledots; + + // We start at N retries, and --N until we give up + $tries = POMF_FILES_RETRIES; + $length = POMF_FILES_LENGTH; + $ext = pathinfo($file->name, PATHINFO_EXTENSION); + + // Check if extension is a double-dot extension and, if true, override $ext + $revname = strrev($file->name); + foreach ($doubledots as $ddot) { + if (stripos($revname, $ddot) === 0) { + $ext = strrev($ddot); + } + } + + do { + // Iterate until we reach the maximum number of retries + if ($tries-- == 0) { + throw new Exception('Gave up trying to find an unused name', 500); + } + + $chars = range('a', 'z'); + $name = ''; + for ($i = 0; $i < $length; ++$i) { + $name .= $chars[array_rand($chars)]; + } + + // Add the extension to the file name + if (isset($ext) && $ext !== '') { + $name .= '.'.strip_tags($ext); + } + + // Check if a file with the same name does already exist in the database + $q = $db->prepare('SELECT COUNT(name) FROM pomf WHERE name = (:name)'); + $q->bindValue(':name', $name, PDO::PARAM_STR); + $q->execute(); + $result = $q->fetchColumn(); + // If it does, generate a new name + } while ($result > 0); + + return $name; } /** - * Handles the uploading and db entry for a file + * Handles the uploading and db entry for a file. + * + * @param UploadedFile $file * - * @param UploadedFile $file * @return array */ -function upload_file ($file) { - global $db; - - // Handle file errors - if ($file->error) throw new UploadException($file->error); - - // Check if a file with the same hash and size (a file which is the same) does already exist in - // the database; if it does, delete the file just uploaded and return the proper link and data. - $q = $db->prepare('SELECT filename, COUNT(*) AS count FROM files WHERE hash = (:hash) ' . - 'AND size = (:size)'); - $q->bindValue(':hash', $file->get_sha1(), PDO::PARAM_STR); - $q->bindValue(':size', $file->size, PDO::PARAM_INT); - $q->execute(); - $result = $q->fetch(); - if ($result['count'] > 0) { - unlink($file->tempfile); - return array( - 'hash' => $file->get_sha1(), - 'name' => $file->name, - 'url' => POMF_URL . $result['filename'], - 'size' => $file->size - ); - } - - // Generate a name for the file - $newname = generate_name($file); - - // Attempt to move it to the static directory - if (move_uploaded_file($file->tempfile, POMF_FILES_ROOT . $newname)) { - // Need to change permissions for the new file to make it world readable - if (chmod(POMF_FILES_ROOT . $newname, 0644)) { - // Add it to the database - if (empty($_SESSION['id'])) { - // Query if user is NOT logged in - $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ' . - 'expire, delid) VALUES (:hash, :orig, :name, :size, :date, ' . - ':exp, :del)'); - } else { - // Query if user is logged in (insert user id together with other data) - $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ' . - 'expire, delid, user) VALUES (:hash, :orig, :name, :size, ' . - ':date, :expires, :delid, :user)'); - $q->bindValue(':user', $_SESSION['id'], PDO::PARAM_INT); - } - - // Common parameters binding - $q->bindValue(':hash', $file->get_sha1(), PDO::PARAM_STR); - $q->bindValue(':orig', strip_tags($file->name), PDO::PARAM_STR); - $q->bindValue(':name', $newname, PDO::PARAM_STR); - $q->bindValue(':size', $file->size, PDO::PARAM_INT); - $q->bindValue(':date', date('Y-m-d'), PDO::PARAM_STR); - $q->bindValue(':exp', null, PDO::PARAM_STR); - $q->bindValue(':del', sha1($file->tempfile), PDO::PARAM_STR); - $q->execute(); - - return array( - 'hash' => $file->get_sha1(), - 'name' => $file->name, - 'url' => POMF_URL . $newname, - 'size' => $file->size - ); - } else { - throw new Exception('Failed to change file permissions', 500); - } - } else { - throw new Exception('Failed to move file to destination', 500); - } +function upload_file($file) +{ + global $db; + + // Handle file errors + if ($file->error) { + throw new UploadException($file->error); + } + + // Check if a file with the same hash and size (a file which is the same) does already exist in + // the database; if it does, delete the file just uploaded and return the proper link and data. + $q = $db->prepare('SELECT filename, COUNT(*) AS count FROM files WHERE hash = (:hash) '. + 'AND size = (:size)'); + $q->bindValue(':hash', $file->get_sha1(), PDO::PARAM_STR); + $q->bindValue(':size', $file->size, PDO::PARAM_INT); + $q->execute(); + $result = $q->fetch(); + if ($result['count'] > 0) { + unlink($file->tempfile); + + return array( + 'hash' => $file->get_sha1(), + 'name' => $file->name, + 'url' => POMF_URL.$result['filename'], + 'size' => $file->size, + ); + } + + // Generate a name for the file + $newname = generate_name($file); + + // Attempt to move it to the static directory + if (move_uploaded_file($file->tempfile, POMF_FILES_ROOT.$newname)) { + // Need to change permissions for the new file to make it world readable + if (chmod(POMF_FILES_ROOT.$newname, 0644)) { + // Add it to the database + if (empty($_SESSION['id'])) { + // Query if user is NOT logged in + $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, '. + 'expire, delid) VALUES (:hash, :orig, :name, :size, :date, '. + ':exp, :del)'); + } else { + // Query if user is logged in (insert user id together with other data) + $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, '. + 'expire, delid, user) VALUES (:hash, :orig, :name, :size, '. + ':date, :expires, :delid, :user)'); + $q->bindValue(':user', $_SESSION['id'], PDO::PARAM_INT); + } + + // Common parameters binding + $q->bindValue(':hash', $file->get_sha1(), PDO::PARAM_STR); + $q->bindValue(':orig', strip_tags($file->name), PDO::PARAM_STR); + $q->bindValue(':name', $newname, PDO::PARAM_STR); + $q->bindValue(':size', $file->size, PDO::PARAM_INT); + $q->bindValue(':date', date('Y-m-d'), PDO::PARAM_STR); + $q->bindValue(':exp', null, PDO::PARAM_STR); + $q->bindValue(':del', sha1($file->tempfile), PDO::PARAM_STR); + $q->execute(); + + return array( + 'hash' => $file->get_sha1(), + 'name' => $file->name, + 'url' => POMF_URL.$newname, + 'size' => $file->size, + ); + } else { + throw new Exception('Failed to change file permissions', 500); + } + } else { + throw new Exception('Failed to move file to destination', 500); + } } /** - * Reorder files array by file + * Reorder files array by file. * * @param $_FILES + * * @return array */ -function diverse_array ($files) { - $result = array(); - foreach ($files as $key1 => $value1) - foreach ($value1 as $key2 => $value2) - $result[$key2][$key1] = $value2; +function diverse_array($files) +{ + $result = array(); - return $result; + foreach ($files as $key1 => $value1) { + foreach ($value1 as $key2 => $value2) { + $result[$key2][$key1] = $value2; + } + } + + return $result; } /** - * Reorganize the $_FILES array into something saner + * Reorganize the $_FILES array into something saner. * * @param $_FILES + * * @return array */ -function refiles ($files) { - $result = array(); - $files = diverse_array($files); - - foreach ($files as $file) { - $f = new UploadedFile(); - $f->name = $file['name']; - $f->mime = $file['type']; - $f->size = $file['size']; - $f->tempfile = $file['tmp_name']; - $f->error = $file['error']; - // 'expire' doesn't exist neither in $_FILES nor in UploadedFile; - // commented out for future implementation - //$f->expire = $file['expire']; - $result[] = $f; - } - - return $result; +function refiles($files) +{ + $result = array(); + $files = diverse_array($files); + + foreach ($files as $file) { + $f = new UploadedFile(); + $f->name = $file['name']; + $f->mime = $file['type']; + $f->size = $file['size']; + $f->tempfile = $file['tmp_name']; + $f->error = $file['error']; + // 'expire' doesn't exist neither in $_FILES nor in UploadedFile; + // commented out for future implementation + //$f->expire = $file['expire']; + $result[] = $f; + } + + return $result; } - - $type = isset($_GET['output']) ? $_GET['output'] : 'json'; $response = new Response($type); + if (isset($_FILES['files'])) { - $uploads = refiles($_FILES['files']); - try { - foreach ($uploads as $upload) - $res[] = upload_file($upload); - $response->send($res); - } catch (Exception $e) { - $response->error($e->getCode(), $e->getMessage()); - } + $uploads = refiles($_FILES['files']); + + try { + foreach ($uploads as $upload) { + $res[] = upload_file($upload); + } + $response->send($res); + } catch (Exception $e) { + $response->error($e->getCode(), $e->getMessage()); + } } else { - $response->error(400, 'No input file(s)'); + $response->error(400, 'No input file(s)'); } - -?> -- cgit v1.2.3