From 01ab585796ae9af611981ad17ee41fd8c7752915 Mon Sep 17 00:00:00 2001 From: Ciki Date: Thu, 10 Jul 2014 09:09:17 +0200 Subject: [PATCH] Get rid of $_POST & Environment in interfaces [Close #24, Ref #23] --- MultipleFileUpload/UI/AbstractInterface.php | 12 +++++++++++- .../UI/HTML4SingleUpload/Controller.php | 6 +++--- MultipleFileUpload/UI/Plupload/Controller.php | 9 ++++----- MultipleFileUpload/UI/Swfupload/Controller.php | 13 +++++-------- MultipleFileUpload/UI/Uploadify/Controller.php | 13 +++++-------- 5 files changed, 28 insertions(+), 25 deletions(-) diff --git a/MultipleFileUpload/UI/AbstractInterface.php b/MultipleFileUpload/UI/AbstractInterface.php index 9ba84b9..dca6bdf 100644 --- a/MultipleFileUpload/UI/AbstractInterface.php +++ b/MultipleFileUpload/UI/AbstractInterface.php @@ -14,6 +14,7 @@ use MultipleFileUpload\MultipleFileUpload, Nette\Environment, Nette\Http\FileUpload, + Nette\Http\Request, Nette\Object, Nette\Utils\Callback; @@ -22,6 +23,15 @@ */ abstract class AbstractInterface extends Object implements IUserInterface { + /** @var Request */ + protected $httpRequest; + + + public function __construct() + { + $this->httpRequest = Environment::getHttpRequest(); + } + /** * Gets interface base url @@ -69,7 +79,7 @@ protected function createTemplate($file = null) { $template = new Template($file); - $template->baseUrl = Environment::getHttpRequest()->url->baseUrl; + $template->baseUrl = $this->httpRequest->url->baseUrl; $template->basePath = rtrim($template->baseUrl, '/'); $template->interface = $this; diff --git a/MultipleFileUpload/UI/HTML4SingleUpload/Controller.php b/MultipleFileUpload/UI/HTML4SingleUpload/Controller.php index 4ccd337..63bc57d 100644 --- a/MultipleFileUpload/UI/HTML4SingleUpload/Controller.php +++ b/MultipleFileUpload/UI/HTML4SingleUpload/Controller.php @@ -28,7 +28,7 @@ class Controller extends \MultipleFileUpload\UI\AbstractInterface */ public function isThisYourUpload() { - return !(Environment::getHttpRequest()->getHeader('user-agent') === 'Shockwave Flash'); + return !($this->httpRequest->getHeader('user-agent') === 'Shockwave Flash'); } @@ -52,7 +52,7 @@ private function processFiles(array $files, array $names = []) // ) // ) // expanded POST array with $names indexes - $postArr = Arrays::getRef($_POST, $names); + $postArr = Arrays::getRef($this->httpRequest->getPost(), $names); $isFormMFU = ( is_array($controlValue) and isset($controlValue["files"]) and @@ -81,7 +81,7 @@ private function processFiles(array $files, array $names = []) public function handleUploads() { // Iterate over all received files - $this->processFiles(Environment::getHttpRequest()->getFiles()); + $this->processFiles($this->httpRequest->getFiles()); return true; // Skip all next } diff --git a/MultipleFileUpload/UI/Plupload/Controller.php b/MultipleFileUpload/UI/Plupload/Controller.php index 160d0ba..700ac74 100644 --- a/MultipleFileUpload/UI/Plupload/Controller.php +++ b/MultipleFileUpload/UI/Plupload/Controller.php @@ -39,7 +39,7 @@ function getBaseUrl() */ public function isThisYourUpload() { - $req = Environment::getHttpRequest(); + $req = $this->httpRequest; return ( $req->getQuery("token") !== null AND @@ -55,8 +55,7 @@ public function isThisYourUpload() public function handleUploads() { /* @var $token string */ - $token = Environment::getHttpRequest() - ->getQuery("token"); + $token = $this->httpRequest->getQuery("token"); if (empty($token)) { return; @@ -194,7 +193,7 @@ public function handleUploads() die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}'); /* @var $file FileUpload */ - /* foreach(Environment::getHttpRequest()->getFiles() AS $file) { + /* foreach($this->httpRequest->getFiles() AS $file) { self::processFile($token, $file); } @@ -242,7 +241,7 @@ public function renderInitJavaScript(MultipleFileUpload $upload) $tpl->maxFiles = $upload->maxFiles; // TODO: make creation of link nicer! - $baseUrl = Environment::getContext()->getService('httpRequest')->url->baseUrl; + $baseUrl = $this->httpRequest->url->baseUrl; $tpl->uploadLink = $baseUrl . "?token=" . $tpl->token . "&uploader=plupload"; $tpl->id = $this->getHtmlIdFlashCompatible($upload); return $tpl->__toString(TRUE); diff --git a/MultipleFileUpload/UI/Swfupload/Controller.php b/MultipleFileUpload/UI/Swfupload/Controller.php index 5893b39..336fa88 100644 --- a/MultipleFileUpload/UI/Swfupload/Controller.php +++ b/MultipleFileUpload/UI/Swfupload/Controller.php @@ -41,9 +41,8 @@ function getBaseUrl() public function isThisYourUpload() { return ( - Environment::getHttpRequest()->getHeader('user-agent') === 'Shockwave Flash' - AND isSet($_POST["sender"]) - AND $_POST["sender"] == "MFU-Swfupload" + $this->httpRequest->getHeader('user-agent') === 'Shockwave Flash' + AND $this->httpRequest->getPost('sender') === "MFU-Swfupload" ); } @@ -54,15 +53,13 @@ public function isThisYourUpload() */ public function handleUploads() { - if (!isset($_POST["token"])) { + $token = $this->httpRequest->getPost('token'); + if (!$token) { return; } - /* @var $token string */ - $token = $_POST["token"]; - /* @var $file FileUpload */ - foreach (Environment::getHttpRequest()->getFiles() AS $file) { + foreach ($this->httpRequest->getFiles() AS $file) { self::processFile($token, $file); } diff --git a/MultipleFileUpload/UI/Uploadify/Controller.php b/MultipleFileUpload/UI/Uploadify/Controller.php index 44d8171..8700014 100644 --- a/MultipleFileUpload/UI/Uploadify/Controller.php +++ b/MultipleFileUpload/UI/Uploadify/Controller.php @@ -38,9 +38,8 @@ function getBaseUrl() public function isThisYourUpload() { return ( - Environment::getHttpRequest()->getHeader('user-agent') === 'Shockwave Flash' - AND isSet($_POST["sender"]) - AND $_POST["sender"] == "MFU-Uploadify" + $this->httpRequest->getHeader('user-agent') === 'Shockwave Flash' + AND $this->httpRequest->getPost('sender') === "MFU-Uploadify" ); } @@ -51,15 +50,13 @@ public function isThisYourUpload() */ public function handleUploads() { - if (!isset($_POST["token"])) { + $token = $this->httpRequest->getPost('token'); + if (!$token) { return; } - /* @var $token string */ - $token = $_POST["token"]; - /* @var $file FileUpload */ - foreach (Environment::getHttpRequest()->getFiles() AS $file) { + foreach ($this->httpRequest->getFiles() AS $file) { self::processFile($token, $file); }