From d925cb40fe41da8469b979a16a9cefe3828defaf Mon Sep 17 00:00:00 2001 From: Ciki Date: Thu, 10 Jul 2014 08:46:35 +0200 Subject: [PATCH] fix formatting --- MultipleFileUpload/Model/BaseQueue.php | 38 ++-- MultipleFileUpload/Model/BaseQueues.php | 25 ++- MultipleFileUpload/Model/Dibi/Queue.php | 102 ++++++----- MultipleFileUpload/Model/Dibi/Queues.php | 50 +++-- MultipleFileUpload/Model/IQueue.php | 14 +- MultipleFileUpload/Model/IQueues.php | 6 +- MultipleFileUpload/Model/Log/Queue.php | 62 ++++--- MultipleFileUpload/Model/Log/Queues.php | 33 ++-- MultipleFileUpload/Model/SQLite/Queue.php | 80 ++++---- MultipleFileUpload/Model/SQLite/Queues.php | 92 ++++++---- MultipleFileUpload/Model/SQLite3/Queue.php | 7 +- MultipleFileUpload/MultipleFileUpload.php | 173 ++++++++++++------ MultipleFileUpload/UI/AbstractInterface.php | 22 ++- .../UI/HTML4SingleUpload/Controller.php | 48 +++-- MultipleFileUpload/UI/IUserInterface.php | 7 +- MultipleFileUpload/UI/Plupload/Controller.php | 69 ++++--- MultipleFileUpload/UI/Registrator.php | 24 ++- .../UI/Swfupload/Controller.php | 39 ++-- MultipleFileUpload/UI/Template.php | 17 +- .../UI/Uploadify/Controller.php | 34 +++- 20 files changed, 589 insertions(+), 353 deletions(-) diff --git a/MultipleFileUpload/Model/BaseQueue.php b/MultipleFileUpload/Model/BaseQueue.php index 8b10475..14cc639 100644 --- a/MultipleFileUpload/Model/BaseQueue.php +++ b/MultipleFileUpload/Model/BaseQueue.php @@ -9,83 +9,95 @@ * the file license.txt that was distributed with this source code. */ - namespace MultipleFileUpload\Model; use Nette\InvalidStateException, Nette\Object; - /** * @property IMFUQueuesModel $queuesModel * @property string $queueID */ -abstract class BaseQueue extends Object implements IQueue { - +abstract class BaseQueue extends Object implements IQueue +{ /** * Queues model * @var IQueues */ private $queuesModel; + /** * Gets queues model * @return IQueues */ - function getQueuesModel() { + function getQueuesModel() + { if (!$this->queuesModel) throw new InvalidStateException("Queues model is not set!"); return $this->queuesModel; } + /** * Sets queues model * @param IQueues $model */ - function setQueuesModel(IQueues $model) { + function setQueuesModel(IQueues $model) + { $this->queuesModel = $model; return $this; } + /** * Queue ID (token) * @var string */ private $queueID; + /** * Gets queue ID * @return string */ - function getQueueID() { + function getQueueID() + { return $this->queueID; } + /** * Sets queue ID * @param string $queueID */ - function setQueueID($queueID) { + function setQueueID($queueID) + { $this->queueID = $queueID; return $this; } + /** * Returns unique file name * @return string */ - protected function getUniqueFilePath() { - return $this->getUploadedFilesTemporaryPath() . DIRECTORY_SEPARATOR + protected function getUniqueFilePath() + { + return $this->getUploadedFilesTemporaryPath() . DIRECTORY_SEPARATOR . "upload-" . $this->getQueueID() . "-" . uniqid() . ".tmp"; } + /** * Initialization */ - function initialize() { - if (!$this->queueID or !$this->queuesModel) { + function initialize() + { + if (!$this->queueID or ! $this->queuesModel) { throw new InvalidStateException("queueID and queuesModel must be setup before call initialize()!"); } } -} + +} \ No newline at end of file diff --git a/MultipleFileUpload/Model/BaseQueues.php b/MultipleFileUpload/Model/BaseQueues.php index d1ef2d9..940f96a 100644 --- a/MultipleFileUpload/Model/BaseQueues.php +++ b/MultipleFileUpload/Model/BaseQueues.php @@ -9,37 +9,39 @@ * the file license.txt that was distributed with this source code. */ - namespace MultipleFileUpload\Model; use Nette\Object; - /** * @property int $lifeTime Life time * @property int $cleanInterval Clean interval */ -abstract class BaseQueues extends Object implements IQueues { - +abstract class BaseQueues extends Object implements IQueues +{ /** * Life time * @var int */ private $lifeTime; + /** * Gets life time of file * @return int */ - function getLifeTime() { + function getLifeTime() + { return $this->lifeTime; } + /** * Sets life time of file * @param int $time */ - function setLifeTime($time) { + function setLifeTime($time) + { $this->lifeTime = $time; return $this; } @@ -51,21 +53,26 @@ function setLifeTime($time) { */ private $cleanInterval; + /** * Gets cleaning interval * @return int */ - function getCleanInterval() { + function getCleanInterval() + { return $this->cleanInterval; } + /** * Sets cleaning interval * @param int $interval */ - function setCleanInterval($interval) { + function setCleanInterval($interval) + { $this->cleanInterval = $interval; return $this; } -} + +} \ No newline at end of file diff --git a/MultipleFileUpload/Model/Dibi/Queue.php b/MultipleFileUpload/Model/Dibi/Queue.php index 7bd2ea7..1f7373d 100644 --- a/MultipleFileUpload/Model/Dibi/Queue.php +++ b/MultipleFileUpload/Model/Dibi/Queue.php @@ -9,12 +9,13 @@ * the file license.txt that was distributed with this source code. */ - namespace MultipleFileUpload\Model\Dibi; -use MultipleFileUpload\Model\BaseQueue; -use dibi; -use Nette\Http\FileUpload; +use dibi, + MultipleFileUpload\Model\BaseQueue, + Nette\Environment, + Nette\Http\FileUpload, + Nette\InvalidStateException; /** * Multiple File Uploader driver for Dibi @@ -22,107 +23,122 @@ * @author Martin Sadový (SodaE), Jan Kuchař (honzakuchar) * @license New BSD License */ -class Queue extends BaseQueue { +class Queue extends BaseQueue +{ /** * Executes query * @return DibiResult */ - function query() { - $params = func_get_args(); // arguments + function query() + { + $params = func_get_args(); return call_user_func_array( - array($this->getQueuesModel(), 'query'), // what + array($this->getQueuesModel(), 'query'), $params ); } + /** * Adds file to queue * @param FileUpload $file */ - function addFile(FileUpload $file) { + function addFile(FileUpload $file) + { $file->move($this->getUniqueFilePath()); $data = array( 'queueID%s' => $this->getQueueID(), 'created%i' => time(), - 'data%s' => base64_encode(serialize($file)), // workaround: http://forum.dibiphp.com/cs/1003-pgsql-a-znak-x00-oriznuti-zbytku-vstupu - 'name%s' => $file->getName() + 'data%s' => base64_encode(serialize($file)), // workaround: http://forum.dibiphp.com/cs/1003-pgsql-a-znak-x00-oriznuti-zbytku-vstupu + 'name%s' => $file->getName() ); $this->query('INSERT INTO [files]', $data); } - function addFileManually($name, $chunk,$chunks) { + + function addFileManually($name, $chunk, $chunks) + { $data = array( 'queueID%s' => $this->getQueueID(), 'created%i' => time(), - 'name%s' => $name, - 'chunk%i' => $chunk, - 'chunks%i' => $chunks + 'name%s' => $name, + 'chunk%i' => $chunk, + 'chunks%i' => $chunks ); $this->query('INSERT INTO [files]', $data); } - function updateFile($name, $chunk, FileUpload $file = null) { + + function updateFile($name, $chunk, FileUpload $file = null) + { dibi::begin(); $where = array( - "queueID%s" => $this->getQueueID(), - "name%s" => $name + "queueID%s" => $this->getQueueID(), + "name%s" => $name ); - $this->query('UPDATE files SET ',array("chunk"=>$chunk),'WHERE %and',$where); - if($file) { - $this->query('UPDATE files SET ',array("data"=>base64_encode(serialize($file))),'WHERE %and',$where); // workaround: http://forum.dibiphp.com/cs/1003-pgsql-a-znak-x00-oriznuti-zbytku-vstupu + $this->query('UPDATE files SET ', array("chunk" => $chunk), 'WHERE %and', $where); + if ($file) { + $this->query('UPDATE files SET ', array("data" => base64_encode(serialize($file))), 'WHERE %and', $where); // workaround: http://forum.dibiphp.com/cs/1003-pgsql-a-znak-x00-oriznuti-zbytku-vstupu } dibi::commit(); } + /** * Gets upload directory path * @return string */ - function getUploadedFilesTemporaryPath() { - if(!Queues::$uploadsTempDir) { - Queues::$uploadsTempDir =\Nette\Environment::expand("%tempDir%".DIRECTORY_SEPARATOR."uploads-MFU"); + function getUploadedFilesTemporaryPath() + { + if (!Queues::$uploadsTempDir) { + Queues::$uploadsTempDir = Environment::expand("%tempDir%" . DIRECTORY_SEPARATOR . "uploads-MFU"); } - if(!file_exists(Queues::$uploadsTempDir)) { - mkdir(Queues::$uploadsTempDir,0777,true); + if (!file_exists(Queues::$uploadsTempDir)) { + mkdir(Queues::$uploadsTempDir, 0777, true); } - if(!is_writable(Queues::$uploadsTempDir)) { - Queues::$uploadsTempDir =\Nette\Environment::expand("%tempDir%"); + if (!is_writable(Queues::$uploadsTempDir)) { + Queues::$uploadsTempDir = Environment::expand("%tempDir%"); } - if(!is_writable(Queues::$uploadsTempDir)) { + if (!is_writable(Queues::$uploadsTempDir)) { throw new InvalidStateException("Directory for temp files is not writable!"); } return Queues::$uploadsTempDir; } + /** * Gets files - * @return array of FileUpload + * @return FileUpload[] */ - function getFiles() { + function getFiles() + { $files = array(); - foreach($this->query('SELECT * FROM [files] WHERE [queueID] = %s', $this->getQueueID())->fetchAll() as $row) { + foreach ($this->query('SELECT * FROM [files] WHERE [queueID] = %s', $this->getQueueID())->fetchAll() as $row) { $f = unserialize(base64_decode($row["data"])); - if(!$f instanceof FileUpload) continue; + if (!$f instanceof FileUpload) + continue; $files[] = $f; } return $files; } - function delete() { + + function delete() + { $dir = realpath($this->getUploadedFilesTemporaryPath()); - foreach($this->getFiles() AS $file) { + foreach ($this->getFiles() AS $file) { $fileDir = dirname($file->getTemporaryFile()); - if(realpath($fileDir) == $dir and file_exists($file->getTemporaryFile())) { + if (realpath($fileDir) == $dir and file_exists($file->getTemporaryFile())) { // Delete only files left in temp directory [not moved by user] @unlink($file->getTemporaryFile()); // intentionally @ } @@ -131,18 +147,20 @@ function delete() { $this->query("DELETE FROM [files] WHERE [queueID] = %s", $this->getQueueID()); } + /** * When was queue last accessed? * @return int timestamp */ - function getLastAccess() { - $lastAccess = (int)$this->query( - "SELECT [created] + function getLastAccess() + { + $lastAccess = (int) $this->query( + "SELECT [created] FROM [files] - WHERE [queueID] = %s", $this->getQueueID(), - "ORDER BY [created] DESC" + WHERE [queueID] = %s", $this->getQueueID(), "ORDER BY [created] DESC" )->fetchSingle(); return $lastAccess; } -} + +} \ No newline at end of file diff --git a/MultipleFileUpload/Model/Dibi/Queues.php b/MultipleFileUpload/Model/Dibi/Queues.php index bc3f1bd..ad27af5 100644 --- a/MultipleFileUpload/Model/Dibi/Queues.php +++ b/MultipleFileUpload/Model/Dibi/Queues.php @@ -9,7 +9,6 @@ * the file license.txt that was distributed with this source code. */ - namespace MultipleFileUpload\Model\Dibi; use dibi, @@ -23,8 +22,8 @@ * @author Martin Sadový (SodaE), Jan Kuchař (honzakuchar) * @license New BSD License */ -class Queues extends BaseQueues { - +class Queues extends BaseQueues +{ /** * Path to directory of uploaded files (temp) * @var string @@ -37,39 +36,45 @@ class Queues extends BaseQueues { */ public static $dibiConnection; + /** * Initializes driver */ - function initialize() { + function initialize() + { } + // /** * Gets dibi connection * @return \DibiConnection */ - function getConnection() { - if(!self::$dibiConnection) { + function getConnection() + { + if (!self::$dibiConnection) { self::$dibiConnection = dibi::getConnection(); } return self::$dibiConnection; } + /** * Executes query * @return \DibiResult * @throws InvalidStateException */ - function query() { - $params = func_get_args(); // arguments + function query() + { + $params = func_get_args(); return call_user_func_array( - array($this->getConnection(), 'query'), - $params + array($this->getConnection(), 'query'), $params ); } + // /** @@ -77,16 +82,19 @@ function query() { * @param string $id * @return Queue */ - function getQueue($id) { + function getQueue($id) + { return $this->createQueueObj($id); } + /** * Factory for MFUQueueDibi * @param string $queueID * @return Queue */ - function createQueueObj($queueID) { + function createQueueObj($queueID) + { $queue = new Queue(); $queue->setQueuesModel($this); $queue->setQueueID($queueID); @@ -94,32 +102,38 @@ function createQueueObj($queueID) { return $queue; } + /** * Executes cleanup */ - function cleanup() { + function cleanup() + { $this->getConnection()->begin(); - foreach($this->getQueues() AS $queue) { - if($queue->getLastAccess() < time() - $this->getLifeTime()) { + foreach ($this->getQueues() AS $queue) { + if ($queue->getLastAccess() < time() - $this->getLifeTime()) { $queue->delete(); } } $this->getConnection()->commit(); } + /** * Gets all queues * @return IQueue[] */ - function getQueues() { + function getQueues() + { $queuesOut = array(); $qs = $this->query("SELECT [queueID] FROM [files] GROUP BY [queueID]")->fetchAll(); - foreach($qs AS $row) { + foreach ($qs AS $row) { $queuesOut[] = $this->createQueueObj($row["queueID"]); } return $queuesOut; } -} + + +} \ No newline at end of file diff --git a/MultipleFileUpload/Model/IQueue.php b/MultipleFileUpload/Model/IQueue.php index 2166792..25843df 100644 --- a/MultipleFileUpload/Model/IQueue.php +++ b/MultipleFileUpload/Model/IQueue.php @@ -9,12 +9,13 @@ * the file license.txt that was distributed with this source code. */ - namespace MultipleFileUpload\Model; -use Nette\Http\FileUpload; +use MultipleFileUpload\Model\IQueues, + Nette\Http\FileUpload; -interface IQueue { +interface IQueue +{ /** * Gets queues model @@ -63,7 +64,7 @@ function addFile(FileUpload $file); * @param type $chunk * @param type $chunks */ - function addFileManually($name, $chunk,$chunks); + function addFileManually($name, $chunk, $chunks); /** * Updates file information (useful when processing chunked upload) @@ -75,7 +76,7 @@ function updateFile($name, $chunk, FileUpload $file = null); /** * Gets all files in queue - * @return array of FileUpload + * @return FileUpload[] */ function getFiles(); @@ -88,5 +89,4 @@ function delete(); * Gets WRITABLE path to write temps of this upload queue */ function getUploadedFilesTemporaryPath(); - -} +} \ No newline at end of file diff --git a/MultipleFileUpload/Model/IQueues.php b/MultipleFileUpload/Model/IQueues.php index 48a4c08..412a516 100644 --- a/MultipleFileUpload/Model/IQueues.php +++ b/MultipleFileUpload/Model/IQueues.php @@ -9,10 +9,10 @@ * the file license.txt that was distributed with this source code. */ - namespace MultipleFileUpload\Model; -interface IQueues { +interface IQueues +{ /** * Gets life time of file @@ -60,4 +60,4 @@ function getQueues(); * Executes cleanup */ function cleanup(); -} +} \ No newline at end of file diff --git a/MultipleFileUpload/Model/Log/Queue.php b/MultipleFileUpload/Model/Log/Queue.php index fc6125a..c0ebabc 100644 --- a/MultipleFileUpload/Model/Log/Queue.php +++ b/MultipleFileUpload/Model/Log/Queue.php @@ -9,67 +9,83 @@ * the file license.txt that was distributed with this source code. */ - namespace MultipleFileUpload\Model\Log; -use MultipleFileUpload\Model\BaseQueue; - -use Nette\Diagnostics\Debugger; +use MultipleFileUpload\Model\BaseQueue, + Nette\Http\FileUpload, + Tracy\Debugger; -class Queue extends BaseQueue { +class Queue extends BaseQueue +{ /** * Initializes driver */ - function initialize() { + function initialize() + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); } + /** * Adds file to queue * @param FileUpload $file */ - function addFile(\Nette\Http\FileUpload $file) { + function addFile(FileUpload $file) + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); } - function updateFile($name, $chunk, \Nette\Http\FileUpload $file = null) { + + function updateFile($name, $chunk, FileUpload $file = null) + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); } - function addFileManually($name, $chunk, $chunks) { + + function addFileManually($name, $chunk, $chunks) + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); } - function getUploadedFilesTemporaryPath() { + + function getUploadedFilesTemporaryPath() + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); return " "; } - function getLastAccess() { + + function getLastAccess() + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); return time(); } + /** * Gets files - * @return array of FileUpload + * @return FileUpload[] */ - function getFiles() { + function getFiles() + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); return array(); } - function delete() { + + function delete() + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); } -} + +} \ No newline at end of file diff --git a/MultipleFileUpload/Model/Log/Queues.php b/MultipleFileUpload/Model/Log/Queues.php index d2bb596..7211b22 100644 --- a/MultipleFileUpload/Model/Log/Queues.php +++ b/MultipleFileUpload/Model/Log/Queues.php @@ -9,29 +9,33 @@ * the file license.txt that was distributed with this source code. */ - namespace MultipleFileUpload\Model\Log; -use MultipleFileUpload\Model\BaseQueues; +use MultipleFileUpload\Model\BaseQueues, + Tracy\Debugger; -class Queues extends BaseQueues { +class Queues extends BaseQueues +{ /** * Initializes driver */ - function initialize() { + function initialize() + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); } + /** * Gets queue * @param string $token * @return Queue */ - function getQueue($token) { + function getQueue($token) + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); $q = new Queue(); $q->setQueueID($token); @@ -39,17 +43,22 @@ function getQueue($token) { return $q; } - function getQueues() { + + function getQueues() + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); } + /** * Executes cleanup */ - function cleanup() { + function cleanup() + { $a = func_get_args(); - Debugger::log(__CLASS__.": ".__METHOD__."; args: ".print_r($a, true)); + Debugger::log(__CLASS__ . ": " . __METHOD__ . "; args: " . print_r($a, true)); } -} + +} \ No newline at end of file diff --git a/MultipleFileUpload/Model/SQLite/Queue.php b/MultipleFileUpload/Model/SQLite/Queue.php index 69579bc..927517e 100644 --- a/MultipleFileUpload/Model/SQLite/Queue.php +++ b/MultipleFileUpload/Model/SQLite/Queue.php @@ -9,94 +9,107 @@ * the file license.txt that was distributed with this source code. */ - namespace MultipleFileUpload\Model\SQLite; -use MultipleFileUpload\Model\BaseQueue; -use Nette\Http\FileUpload; -use Nette\Environment; - -use Nette\InvalidStateException; +use MultipleFileUpload\Model\BaseQueue, + Nette\Environment, + Nette\Http\FileUpload, + Nette\InvalidStateException; -class Queue extends BaseQueue { +class Queue extends BaseQueue +{ /** * Executes query * @param string $sql * @return SQLiteResult */ - function query($sql) { + function query($sql) + { return $this->getQueuesModel()->query($sql); } + /** * Adds file to queue * @param FileUpload $file */ - function addFile(FileUpload $file) { + function addFile(FileUpload $file) + { $file->move($this->getUniqueFilePath()); - $this->query('INSERT INTO files (queueID, created, data, name) VALUES ("'.sqlite_escape_string($this->getQueueID()).'",'.time().',\''.sqlite_escape_string(serialize($file)).'\', \''.sqlite_escape_string($file->getName()).'\')'); + $this->query('INSERT INTO files (queueID, created, data, name) VALUES ("' . sqlite_escape_string($this->getQueueID()) . '",' . time() . ',\'' . sqlite_escape_string(serialize($file)) . '\', \'' . sqlite_escape_string($file->getName()) . '\')'); } + // TODO: rename!!! - function addFileManually($name, $chunk,$chunks) { - $this->query('INSERT INTO files (queueID, created, name, chunk, chunks) VALUES ("'.sqlite_escape_string($this->getQueueID()).'",'.time().',\''.sqlite_escape_string($name).'\', \''.sqlite_escape_string($chunk).'\', \''.sqlite_escape_string($chunks).'\')'); + function addFileManually($name, $chunk, $chunks) + { + $this->query('INSERT INTO files (queueID, created, name, chunk, chunks) VALUES ("' . sqlite_escape_string($this->getQueueID()) . '",' . time() . ',\'' . sqlite_escape_string($name) . '\', \'' . sqlite_escape_string($chunk) . '\', \'' . sqlite_escape_string($chunks) . '\')'); } - function updateFile($name, $chunk, FileUpload $file = null) { + + function updateFile($name, $chunk, FileUpload $file = null) + { $this->query("BEGIN TRANSACTION"); - $where = 'queueID = \''.sqlite_escape_string($this->getQueueID()).'\' AND name = \''.sqlite_escape_string($name).'\''; - $this->query('UPDATE files SET chunk = \''.sqlite_escape_string($chunk).'\' WHERE '.$where); - if($file) { - $this->query('UPDATE files SET data = \''.sqlite_escape_string(serialize($file)).'\' WHERE '.$where); + $where = 'queueID = \'' . sqlite_escape_string($this->getQueueID()) . '\' AND name = \'' . sqlite_escape_string($name) . '\''; + $this->query('UPDATE files SET chunk = \'' . sqlite_escape_string($chunk) . '\' WHERE ' . $where); + if ($file) { + $this->query('UPDATE files SET data = \'' . sqlite_escape_string(serialize($file)) . '\' WHERE ' . $where); } $this->query("END TRANSACTION"); } + /** * Gets upload directory path * @return string */ - function getUploadedFilesTemporaryPath() { - if(!Queues::$uploadsTempDir) { + function getUploadedFilesTemporaryPath() + { + if (!Queues::$uploadsTempDir) { Queues::$uploadsTempDir = Environment::expand("%tempDir%" . DIRECTORY_SEPARATOR . "uploads-MFU"); } - if(!file_exists(Queues::$uploadsTempDir)) { + if (!file_exists(Queues::$uploadsTempDir)) { mkdir(Queues::$uploadsTempDir, 0777, true); } - if(!is_writable(Queues::$uploadsTempDir)) { + if (!is_writable(Queues::$uploadsTempDir)) { Queues::$uploadsTempDir = Environment::expand("%tempDir%"); } - if(!is_writable(Queues::$uploadsTempDir)) { + if (!is_writable(Queues::$uploadsTempDir)) { throw new InvalidStateException("Directory for temp files is not writable!"); } return Queues::$uploadsTempDir; } + /** * Gets files - * @return array of FileUpload + * @return FileUpload[] */ - function getFiles() { + function getFiles() + { $files = array(); - foreach($this->query("SELECT * FROM files WHERE queueID = '".sqlite_escape_string($this->getQueueID())."'")->fetchAll() AS $row) { + foreach ($this->query("SELECT * FROM files WHERE queueID = '" . sqlite_escape_string($this->getQueueID()) . "'")->fetchAll() AS $row) { $f = unserialize($row["data"]); - if(!$f instanceof FileUpload) continue; + if (!$f instanceof FileUpload) + continue; $files[] = $f; } return $files; } - function delete() { + + function delete() + { $dir = realpath($this->getUploadedFilesTemporaryPath()); - foreach($this->getFiles() AS $file) { + foreach ($this->getFiles() AS $file) { $fileDir = dirname($file->getTemporaryFile()); - if(realpath($fileDir) == $dir and file_exists($file->getTemporaryFile())) { + if (realpath($fileDir) == $dir and file_exists($file->getTemporaryFile())) { // Delete file only if user leaved file in temp directory @unlink($file->getTemporaryFile()); // intentionally @ } @@ -105,16 +118,19 @@ function delete() { $this->query("DELETE FROM files WHERE queueID = '" . sqlite_escape_string($this->getQueueID()) . "'"); } + /** * When was queue last accessed? * @return int timestamp */ - function getLastAccess() { - $lastAccess = (int)$this->query("SELECT created + function getLastAccess() + { + $lastAccess = (int) $this->query("SELECT created FROM files - WHERE queueID = '".sqlite_escape_string($this->getQueueID())."' + WHERE queueID = '" . sqlite_escape_string($this->getQueueID()) . "' ORDER BY created DESC")->fetchSingle(); return $lastAccess; } + } \ No newline at end of file diff --git a/MultipleFileUpload/Model/SQLite/Queues.php b/MultipleFileUpload/Model/SQLite/Queues.php index 891e6a3..4514a28 100644 --- a/MultipleFileUpload/Model/SQLite/Queues.php +++ b/MultipleFileUpload/Model/SQLite/Queues.php @@ -9,7 +9,6 @@ * the file license.txt that was distributed with this source code. */ - namespace MultipleFileUpload\Model\SQLite; use MultipleFileUpload\Model\BaseQueues, @@ -17,8 +16,8 @@ Nette\Environment, Nette\InvalidStateException; -class Queues extends BaseQueues { - +class Queues extends BaseQueues +{ /** * @var \SQLiteDatabase */ @@ -36,25 +35,29 @@ class Queues extends BaseQueues { */ public static $uploadsTempDir; + /** * Initializes driver */ - function initialize() { + function initialize() + { } + // - function getConnection() { - if(!$this->connection) { + function getConnection() + { + if (!$this->connection) { $this->connection = $this->openDB(); // load database - if(filesize(self::$databasePath) == 0) { + if (filesize(self::$databasePath) == 0) { //$this->beginTransaction(); - $this->connection->queryExec(file_get_contents(dirname(__FILE__)."/setupDB.sql"),$error); - if($error) { - throw new InvalidStateException("Can't create SQLite database: ".$error); + $this->connection->queryExec(file_get_contents(dirname(__FILE__) . "/setupDB.sql"), $error); + if ($error) { + throw new InvalidStateException("Can't create SQLite database: " . $error); } //$this->endTransaction(); } @@ -62,41 +65,47 @@ function getConnection() { return $this->connection; } + /** * Executes query * @param string $sql * @return SQLiteResult * @throws InvalidStateException */ - function query($sql) { - $r = $this->getConnection()->query($sql,SQLITE_ASSOC, $error); - if($error) { - throw new InvalidStateException("Can't execute queury: '".$sql."'. error: ".$error); + function query($sql) + { + $r = $this->getConnection()->query($sql, SQLITE_ASSOC, $error); + if ($error) { + throw new InvalidStateException("Can't execute queury: '" . $sql . "'. error: " . $error); } return $r; } - /*function beginTransaction() { - $this->query("BEGIN TRANSACTION"); - } - function endTransaction() { - $this->query("END TRANSACTION"); - }*/ + /* function beginTransaction() { + $this->query("BEGIN TRANSACTION"); + } + + function endTransaction() { + $this->query("END TRANSACTION"); + } */ /** * Open SQLite file * @return SQLiteDatabase * @throws InvalidStateException */ - function openDB() { + function openDB() + { - if(!($connection = new \SQLiteDatabase(self::$databasePath, 0777, $error))) { - throw new InvalidStateException("Can't create sqlite database: ".$error); + if (!($connection = new \SQLiteDatabase(self::$databasePath, 0777, $error))) { + throw new InvalidStateException("Can't create sqlite database: " . $error); } return $connection; } + + // /** @@ -104,16 +113,19 @@ function openDB() { * @param string $id * @return Queue */ - function getQueue($id) { + function getQueue($id) + { return $this->createQueueObj($id); } + /** * Factory for MFUQueueSQLite * @param string $queueID * @return Queue */ - function createQueueObj($queueID) { + function createQueueObj($queueID) + { $queue = new Queue(); $queue->setQueuesModel($this); $queue->setQueueID($queueID); @@ -121,13 +133,15 @@ function createQueueObj($queueID) { return $queue; } + /** * Executes cleanup */ - function cleanup() { + function cleanup() + { $this->query("BEGIN TRANSACTION"); - foreach($this->getQueues() AS $queue) { - if($queue->getLastAccess() < time() - $this->getLifeTime()) { + foreach ($this->getQueues() AS $queue) { + if ($queue->getLastAccess() < time() - $this->getLifeTime()) { $queue->delete(); } } @@ -137,35 +151,39 @@ function cleanup() { $this->query("VACUUM"); } + /** * Gets all queues * @return IQueue[] */ - function getQueues() { + function getQueues() + { $queuesOut = array(); $qs = $this->query("SELECT queueID FROM files GROUP BY queueID")->fetchAll(); - foreach($qs AS $row) { + foreach ($qs AS $row) { $queuesOut[] = $this->createQueueObj($row["queueID"]); } return $queuesOut; } - static function init() { + + static function init() + { // TODO: remove this magic - $config = Environment::getConfig("MultipleFileUploader",array( - "databasePath" => dirname(__FILE__)."/database.sdb", - "uploadsTempDir" => "" + $config = Environment::getConfig("MultipleFileUploader", array( + "databasePath" => dirname(__FILE__) . "/database.sdb", + "uploadsTempDir" => "" )); - foreach($config AS $key => $val) { + foreach ($config AS $key => $val) { self::$$key = $val; } } -} -Queues::init(); \ No newline at end of file +} +Queues::init(); diff --git a/MultipleFileUpload/Model/SQLite3/Queue.php b/MultipleFileUpload/Model/SQLite3/Queue.php index 205df77..87b614e 100644 --- a/MultipleFileUpload/Model/SQLite3/Queue.php +++ b/MultipleFileUpload/Model/SQLite3/Queue.php @@ -9,8 +9,7 @@ * For the full copyright and license information, please view * the file license.txt that was distributed with this source code. */ - - + namespace MultipleFileUpload\Model\SQLite3; use MultipleFileUpload\Model\BaseQueue, @@ -132,7 +131,7 @@ function getUploadedFilesTemporaryPath() /** * Get files - * @return array of \Nette\Http\FileUpload + * @return FileUpload[] */ function getFiles() { @@ -184,4 +183,4 @@ function getLastAccess() } -} +} \ No newline at end of file diff --git a/MultipleFileUpload/MultipleFileUpload.php b/MultipleFileUpload/MultipleFileUpload.php index 04a9ef2..799bf9d 100644 --- a/MultipleFileUpload/MultipleFileUpload.php +++ b/MultipleFileUpload/MultipleFileUpload.php @@ -12,17 +12,22 @@ namespace MultipleFileUpload; -use Nette\Environment; -use Nette\Utils\Html; - -use Nette\Forms; - -use Nette\InvalidStateException; -use Nette\NotSupportedException; - - -class MultipleFileUpload extends Forms\Controls\UploadControl { - +use MultipleFileUpload\Model\IQueue, + MultipleFileUpload\Model\IQueues, + MultipleFileUpload\UI\Registrator, + Nette\Environment, + Nette\Forms, + Nette\Forms\Container, + Nette\Forms\Controls\UploadControl, + Nette\Http\FileUpload, + Nette\InvalidStateException, + Nette\NotSupportedException, + Nette\Utils\Callback, + Nette\Utils\Html, + Nette\Utils\Strings; + +class MultipleFileUpload extends UploadControl +{ /** * Is files handle uploads called? * @var bool @@ -32,7 +37,7 @@ class MultipleFileUpload extends Forms\Controls\UploadControl { /** * Model - * @var Model\IQueues + * @var IQueues * @see self::init() */ protected static $queuesModel; @@ -41,29 +46,30 @@ class MultipleFileUpload extends Forms\Controls\UploadControl { * Validate file callback * @var Callback * @return bool - * @param \Nette\Http\FileUpload File to be checked + * @param FileUpload File to be checked */ public static $validateFileCallback; /** * Interface registrator instance - * @var UI\Registrator + * @var Registrator */ public static $interfaceRegistrator; /** * Root of mfu directory in public folder (used for serving js, css, ...) - * @var type string + * @var string */ public static $baseWWWRoot = null; + /** * Initialize MFU */ - public static function init() { - + public static function init() + { // Init UI registrator - $uiReg = self::$interfaceRegistrator = new UI\Registrator(); + $uiReg = self::$interfaceRegistrator = new Registrator(); $uiReg->register("MultipleFileUpload\\UI\\HTML4SingleUpload"); $uiReg->register("MultipleFileUpload\\UI\\Plupload"); @@ -74,29 +80,35 @@ public static function init() { self::$baseWWWRoot = Environment::getHttpRequest()->url->baseUrl . "MultipleFileUpload/"; } + /** * Register MFU into Nette */ - public static function register() { + public static function register() + { self::init(); $application = Environment::getApplication(); - $application->onStartup[] = callback(__CLASS__, "handleUploads"); + $application->onStartup[] = callback(__CLASS__, "handleUploads"); $application->onShutdown[] = callback(__CLASS__, "cleanCache"); } + /* ########## HANDLING UPLOADS ########### */ /** * Sets life time of files in queue (shortcut for self::getQueuesModel()->setLifeTime) * @param int $lifeTime Time in seconds */ - static function setLifeTime($lifeTime) { + static function setLifeTime($lifeTime) + { self::getQueuesModel() ->setLifeTime((int) $lifeTime); } - protected static function _doSetLifetime() { + + protected static function _doSetLifetime() + { // Auto config of lifeTime $maxInputTime = (int) ini_get("max_input_time"); // default if no max input time defined (-1) @@ -109,10 +121,12 @@ protected static function _doSetLifetime() { self::setLifeTime($lifeTime); } + /** * Handles uploading files */ - public static function handleUploads() { + public static function handleUploads() + { if (self::$handleUploadsCalled === true) { return; } else { @@ -143,59 +157,70 @@ public static function handleUploads() { } } + /** * Checks file if is ok and can be processed - * @param \Nette\Http\FileUpload $file + * @param FileUpload $file * @return bool */ - public static function validateFile(\Nette\Http\FileUpload $file) { + public static function validateFile(FileUpload $file) + { return $file->isOk(); } + /** * Cleans cache */ - public static function cleanCache() { + public static function cleanCache() + { if (!Environment::isProduction() or rand(1, 100) < 5) { self::getQueuesModel()->cleanup(); } } + /** - * @return type - * @throws \Nette\InvalidStateException + * @return IQueues + * @throws InvalidStateException */ - public static function getQueuesModel() { + public static function getQueuesModel() + { if (!self::$queuesModel) { // if nothing is set, setup sqlite model, which should work on all systems with SQLite self::setQueuesModel(new Model\SQLite3\Queues()); } - if (!self::$queuesModel instanceof Model\IQueues) { - throw new \Nette\InvalidStateException("Queues model is not instance of Model\IQueues!"); + if (!self::$queuesModel instanceof IQueues) { + throw new InvalidStateException("Queues model is not instance of Model\IQueues!"); } return self::$queuesModel; } + /** * Sets new queues model - * @param \MultipleFileUpload\Model\IQueues $model + * @param IQueues $model */ - public static function setQueuesModel(Model\IQueues $model) { + public static function setQueuesModel(IQueues $model) { self::$queuesModel = $model; self::_doSetLifetime(); } + /** - * @return UI\Registrator + * @return Registrator */ - public static function getUIRegistrator() { - if (!self::$interfaceRegistrator instanceof UI\Registrator) { + public static function getUIRegistrator() + { + if (!self::$interfaceRegistrator instanceof Registrator) { throw new InvalidStateException("Interface registrator is not instance of MFUUIRegistrator!"); } return self::$interfaceRegistrator; } - public static function getHead() { + + public static function getHead() + { // TODO: Add MFUFallbackController? $out = ""; @@ -205,10 +230,10 @@ public static function getHead() { return $out; } - /* ***************************************************************************** + + /* * **************************************************************************** * ************************* Form Control ************************************** * ***************************************************************************** */ - /** * Unique identifier * @var string @@ -233,11 +258,13 @@ public static function getHead() { */ public $simUploadThreads; + /** * Constructor * @param string $label Label */ - public function __construct($label = NULL, $maxSelectedFiles = 25) { + public function __construct($label = NULL, $maxSelectedFiles = 25) + { parent::__construct($label); if (!self::$handleUploadsCalled) { @@ -255,7 +282,8 @@ public function __construct($label = NULL, $maxSelectedFiles = 25) { * Generates control * @return Html */ - public function getControl() { + public function getControl() + { $this->setOption('rendered', TRUE); // Create control @@ -274,14 +302,14 @@ public function getControl() { foreach ($interfaces AS $interface) { $html = $interface->render($this); // remove wrapping