Skip to content

Commit

Permalink
Get rid of $_POST & Environment in interfaces [Close #24, Ref #23]
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciki committed Jul 10, 2014
1 parent d925cb4 commit bca5d5b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
12 changes: 11 additions & 1 deletion MultipleFileUpload/UI/AbstractInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use MultipleFileUpload\MultipleFileUpload,
Nette\Environment,
Nette\Http\FileUpload,
Nette\Http\Request,
Nette\Object,
Nette\Utils\Callback;

Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 3 additions & 4 deletions MultipleFileUpload/UI/HTML4SingleUpload/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace MultipleFileUpload\UI\HTML4SingleUpload;

use MultipleFileUpload\MultipleFileUpload,
Nette\Environment,
Nette\Utils\Arrays;

/**
Expand All @@ -28,7 +27,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');
}


Expand All @@ -52,7 +51,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
Expand Down Expand Up @@ -81,7 +80,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
}

Expand Down
10 changes: 4 additions & 6 deletions MultipleFileUpload/UI/Plupload/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use MultipleFileUpload\MultipleFileUpload,
MultipleFileUpload\UI\AbstractInterface,
Nette\Environment,
Nette\Http\FileUpload;

/**
Expand All @@ -39,7 +38,7 @@ function getBaseUrl()
*/
public function isThisYourUpload()
{
$req = Environment::getHttpRequest();
$req = $this->httpRequest;
return (
$req->getQuery("token") !== null
AND
Expand All @@ -55,8 +54,7 @@ public function isThisYourUpload()
public function handleUploads()
{
/* @var $token string */
$token = Environment::getHttpRequest()
->getQuery("token");
$token = $this->httpRequest->getQuery("token");

if (empty($token)) {
return;
Expand Down Expand Up @@ -194,7 +192,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);
}
Expand Down Expand Up @@ -242,7 +240,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);
Expand Down
14 changes: 5 additions & 9 deletions MultipleFileUpload/UI/Swfupload/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use MultipleFileUpload\MultipleFileUpload,
MultipleFileUpload\UI\AbstractInterface,
Nette\Environment,
Nette\Http\FileUpload;

/**
Expand All @@ -41,9 +40,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"
);
}

Expand All @@ -54,15 +52,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);
}

Expand Down
16 changes: 6 additions & 10 deletions MultipleFileUpload/UI/Uploadify/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

namespace MultipleFileUpload\UI\Uploadify;

use MultipleFileUpload\MultipleFileUpload,
Nette\Environment;
use MultipleFileUpload\MultipleFileUpload;

/**
* Description of MFUUIUploadify
Expand All @@ -38,9 +37,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"
);
}

Expand All @@ -51,15 +49,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);
}

Expand Down

0 comments on commit bca5d5b

Please sign in to comment.