From 5b77a4eaab418836820b2a0bad7c88f0b88136a0 Mon Sep 17 00:00:00 2001 From: cenci0 Date: Tue, 3 Feb 2015 21:39:47 +0100 Subject: Minor code refactoring and updates DATABASE UPDATE: `orginalname` renamed to `originalname` (run update.sql to update `files` table). Run `update.sql` to update your database, no data will be lost (if you don't update it, pomf won't work properly). `schema.sql` - `id`s are now unsigned to double the maximum number of supported indexes - `size` and `user` columns are unsigned too (we can't have a negative file size or a negative user id) `static/classes/Response.class.php` New class to manage responses and errors. Using a class for responses makes the code cleaner and easier to add new response type handlers in the future. - The response type is declared while a new variable is initialized and the `error` and `send` methods will print a proper output according to it - The header for the response is also set accordingly to the response type (`text/plain` for `csv`/`gyazo` output, `application/json` for `json` output) `static/classes/UploadException.class.php` `Exception` class extension to manage errors that may have occurred while uploading files to the server. This way every error is reported in detail. `static/includes/settings.inc.php` - `POMF_URL`: add an option to set the domain of the url to prepend to the newly generated filename (before it was hardcoded and it was `http://a.pomf.se/`) [this option is currently used only when the output is set to `gyazo`] `upload.php` - Add functions calls at the beginning to enable the output compression if possible (gzipped output) - Include new classes - `generate_name ()` --- The new name generation algorithm is basically the same of the old one, but it's easier to add/remove characters if needed (eg. lower-case letters isn't enough and you need to add capital-case letters or digits) --- Check directly the database instead of the filesystem to see if a generated filename is already in use - `upload_file ()` --- File errors generate a `UploadException` instead of a normal `Exception` --- Move common code outside the `if` to avoid useless code repetition (only the query is now inside the condition) --- Change file permissions of a file after moving it to the new directory (if the user didn't set a mask previouly these files wouldn't be world readable, we make sure they are) - `diverse_array ()` --- Function to reorder initial `$_FILES` array (easier to handle for `refiles`) - `refiles ()` --- Use a `foreach` cicle instead of a `for` one, easier to understand - Create a new response object and set its response type (`csv`, `gyazo` or default to `json`) - Edit response handling according to the new response class --- schema.sql | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) (limited to 'schema.sql') diff --git a/schema.sql b/schema.sql index 8580f9a..a640e6d 100644 --- a/schema.sql +++ b/schema.sql @@ -7,7 +7,7 @@ -- CREATE TABLE `accounts` ( - `id` int(11) NOT NULL AUTO_INCREMENT, + `id` int(10) unsigned NOT NULL auto_increment, `email` varchar(255) NOT NULL, `pass` varchar(255) NOT NULL, `level` int(11) NOT NULL, @@ -19,29 +19,32 @@ CREATE TABLE `accounts` ( -- CREATE TABLE `files` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `hash` char(40) DEFAULT NULL, - `orginalname` varchar(255) DEFAULT NULL, - `filename` varchar(30) DEFAULT NULL, - `size` int(11) DEFAULT NULL, - `date` date DEFAULT NULL, - `expire` date DEFAULT NULL, - `delid` char(40) DEFAULT NULL, - `user` int(11) DEFAULT '0', - `dir` int(2) DEFAULT '0', + `id` int(10) unsigned NOT NULL auto_increment, + `hash` char(40) default NULL, + `originalname` varchar(255) default NULL, + `filename` varchar(30) default NULL, + `size` int(10) unsigned default NULL, + `date` date default NULL, + `expire` date default NULL, + `delid` char(40) default NULL, + `user` int(10) unsigned default '0', + `dir` int(2) default '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB; +-- `filename` length (30) may be a bit excessive, since the average length would be smaller +-- [6 for the name + the file extension length], but since there is no limit for the file +-- extension length (at the moment, at least) let's keep it high in order to avoid problems -- -- Table structure for table `invites` -- CREATE TABLE `invites` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `email` varchar(50) DEFAULT '0', - `code` varchar(50) DEFAULT '0', - `used` int(11) DEFAULT '0', - `level` int(11) DEFAULT NULL, + `id` int(10) unsigned NOT NULL auto_increment, + `email` varchar(50) default '0', + `code` varchar(50) default '0', + `used` int(11) default '0', + `level` int(11) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; @@ -50,13 +53,13 @@ CREATE TABLE `invites` ( -- CREATE TABLE `reports` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `hash` char(40) DEFAULT '0', - `date` date DEFAULT NULL, - `file` varchar(255) NOT NULL DEFAULT '0', - `fileid` int(11) DEFAULT '0', - `reporter` varchar(255) NOT NULL DEFAULT '0', - `status` int(11) DEFAULT '0', + `id` int(10) unsigned NOT NULL auto_increment, + `hash` char(40) default '0', + `date` date default NULL, + `file` varchar(255) NOT NULL default '0', + `fileid` int(11) default '0', + `reporter` varchar(255) NOT NULL default '0', + `status` int(11) default '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB; -- cgit v1.2.3