Permalink
Browse files

Use Guzzle for PixlrEditor

  • Loading branch information...
1 parent a601082 commit ef02e6f5300754399e7ee0bdfd832a60b6b77cef @ghecquet ghecquet committed Jun 8, 2016
Showing with 74 additions and 47 deletions.
  1. +74 −47 core/src/plugins/editor.pixlr/class.PixlrEditor.php
@@ -19,16 +19,21 @@
* The latest code can be found at <http://pyd.io/>.
*/
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
use Pydio\Access\Core\AJXP_MetaStreamWrapper;
use Pydio\Access\Core\Model\AJXP_Node;
use Pydio\Access\Core\Model\UserSelection;
+use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Services\ConfService;
use Pydio\Core\Controller\Controller;
use Pydio\Core\Exception\PydioException;
use Pydio\Core\Utils\Utils;
use Pydio\Core\PluginFramework\Plugin;
use Pydio\Core\Utils\TextEncoder;
+use GuzzleHttp\Client;
+
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
@@ -38,15 +43,35 @@
*/
class PixlrEditor extends Plugin
{
- public function switchAction($action, $httpVars, $filesVars, \Pydio\Core\Model\ContextInterface $contextInterface)
- {
- $selection = UserSelection::fromContext($contextInterface, $httpVars);
- $repository = $contextInterface->getRepository();
+
+ /**
+ * @var Client
+ */
+ private $client;
+
+ // Plugin initialisation
+ public function init(\Pydio\Core\Model\ContextInterface $context) {
+
+ parent::init($context);
+
+ $this->client = new Client([
+ 'base_url' => "https://pixlr.com/editor/"
+ ]);
+ }
+
+ // Main controller function
+ public function switchAction(ServerRequestInterface &$request, ResponseInterface &$response) {
+
+ /** @var ContextInterface $ctx */
+ $ctx = $request->getAttribute("ctx");
+ $action = $request->getAttribute("action");
+ $httpVars = $request->getParsedBody();
+
+ $selection = UserSelection::fromContext($ctx, $httpVars);
$selectedNode = $selection->getUniqueNode();
$selectedNodeUrl = $selectedNode->getUrl();
if ($action == "post_to_server") {
-
if(!is_writeable($selectedNodeUrl)){
header("Location:".Utils::detectServerURL(true)."/plugins/editor.pixlr/fake_error_pixlr.php");
return false;
@@ -65,51 +90,37 @@ public function switchAction($action, $httpVars, $filesVars, \Pydio\Core\Model\C
$this->logInfo('Preview', 'Sending content of '.$selectedNodeUrl.' to Pixlr server.', array("files" => $selectedNodeUrl));
Controller::applyHook("node.read", array($selectedNode));
-
$saveTarget = $target."/fake_save_pixlr.php";
- if ($this->getContextualOption($contextInterface, "CHECK_SECURITY_TOKEN")) {
+ if ($this->getContextualOption($ctx, "CHECK_SECURITY_TOKEN")) {
$saveTarget = $target."/fake_save_pixlr_".md5($httpVars["secure_token"]).".php";
}
- $params = array(
- "referrer" => "Pydio",
- "method" => "get",
- "loc" => ConfService::getLanguage(),
- "target" => $saveTarget,
- "exit" => $target."/fake_close_pixlr.php",
- "title" => urlencode(basename($selectedNodeUrl)),
- "locktarget"=> "false",
- "locktitle" => "true",
- "locktype" => "source"
- );
-
- require_once(AJXP_BIN_FOLDER."/lib/http_class/http_class.php");
- $arguments = array();
- $httpClient = new http_class();
- $httpClient->request_method = "POST";
- $httpClient->GetRequestArguments("https://pixlr.com/editor/", $arguments);
- $arguments["PostValues"] = $params;
- $arguments["PostFiles"] = array(
- "image" => array("FileName" => $tmp, "Content-Type" => "automatic/name"));
-
- $err = $httpClient->Open($arguments);
- if (empty($err)) {
- $err = $httpClient->SendRequest($arguments);
- if (empty($err)) {
- $response = "";
- while (true) {
- $header = array();
- $error = $httpClient->ReadReplyHeaders($header, 1000);
- if ($error != "" || $header != null) break;
- $response .= $header;
- }
- }
- }
- if(isSet($header) && isSet($header["location"])){
- header("Location: {$header['location']}"); //$response");
- }else{
- header("Location:".Utils::detectServerURL(true)."/plugins/editor.pixlr/fake_error_pixlr.php");
- }
+ $type = pathinfo($tmp, PATHINFO_EXTENSION);
+ $data = file_get_contents($tmp);
+ $rawData = 'data:image/' . $type . ';base64,' . base64_encode($data);
+
+ $params = [
+ 'allow_redirects' => false,
+ 'body' => [
+ "referrer" => "Pydio",
+ "method" => "get",
+ "type" => $type,
+ "loc" => ConfService::getLanguage(),
+ "target" => $saveTarget,
+ "exit" => $target."/fake_close_pixlr.php",
+ "title" => urlencode(basename($selectedNodeUrl)),
+ "locktarget" => "false",
+ "locktitle" => "true",
+ "locktype" => "source",
+ "image" => $rawData
+ ]
+ ];
+
+ $postResponse = $this->client->post("/editor/", $params);
+
+ $response = $response
+ ->withStatus(302)
+ ->withHeader("Location", $postResponse->getHeader("Location"));
} else if ($action == "retrieve_pixlr_image") {
@@ -127,7 +138,7 @@ public function switchAction($action, $httpVars, $filesVars, \Pydio\Core\Model\C
$url = $httpVars["new_url"];
$urlParts = parse_url($url);
$query = $urlParts["query"];
- if ($this->getContextualOption($contextInterface, "CHECK_SECURITY_TOKEN")) {
+ if ($this->getContextualOption($ctx, "CHECK_SECURITY_TOKEN")) {
$scriptName = basename($urlParts["path"]);
$token = str_replace(array("fake_save_pixlr_", ".php"), "", $scriptName);
if ($token != md5($httpVars["secure_token"])) {
@@ -162,4 +173,20 @@ public function switchAction($action, $httpVars, $filesVars, \Pydio\Core\Model\C
}
+ /**
+ * @return mixed
+ */
+ public function getClient()
+ {
+ return $this->client;
+ }
+
+ /**
+ * @param mixed $client
+ */
+ public function setClient($client)
+ {
+ $this->client = $client;
+ }
+
}

0 comments on commit ef02e6f

Please sign in to comment.