From bbe3ee1f9edcc79d38dc96c5703c9feec39cbcdd Mon Sep 17 00:00:00 2001 From: cdujeu Date: Wed, 1 Jun 2016 10:11:32 +0200 Subject: [PATCH] Use Request/Response objects instead of direct printing. Create dedicated Message object when necessary --- .../Response/SerializableResponseStream.php | 4 +- .../XMLDocSerializableResponseChunk.php | 3 +- .../class.DuoSecurityFrontend.php | 3 - .../authfront.otp/class.OtpAuthFrontend.php | 8 -- .../core.access/src/Model/NodesList.php | 5 + core/src/plugins/editor.eml/EmlXmlMessage.php | 75 ++++++++++++++ core/src/plugins/editor.eml/manifest.xml | 12 +-- .../class.AjxpElasticSearch.php | 36 ++++--- .../src/plugins/meta.git/class.GitManager.php | 76 ++++++++------ .../class.SimpleLockManager.php | 18 ++-- .../meta.watch/class.MetaWatchRegister.php | 99 +++++++++---------- .../uploader.http/class.HttpDownloader.php | 14 --- 12 files changed, 219 insertions(+), 134 deletions(-) create mode 100644 core/src/plugins/editor.eml/EmlXmlMessage.php diff --git a/core/src/core/src/pydio/Core/Http/Response/SerializableResponseStream.php b/core/src/core/src/pydio/Core/Http/Response/SerializableResponseStream.php index 0643e2ce6e..f8191506a4 100644 --- a/core/src/core/src/pydio/Core/Http/Response/SerializableResponseStream.php +++ b/core/src/core/src/pydio/Core/Http/Response/SerializableResponseStream.php @@ -115,6 +115,7 @@ protected function serializeData($data, $serializer){ }else if($serializer == self::SERIALIZER_TYPE_XML){ $wrap = true; $buffer = ""; + $charset = null; foreach ($data as $serializableItem){ if(!$serializableItem instanceof XMLSerializableResponseChunk){ continue; @@ -122,12 +123,13 @@ protected function serializeData($data, $serializer){ $buffer .= $serializableItem->toXML(); if($serializableItem instanceof XMLDocSerializableResponseChunk){ $wrap = false; + $charset = $serializableItem->getCharset(); } } if($wrap){ return XMLWriter::wrapDocument($buffer); }else{ - return "".$buffer; + return "".$buffer; } } return ""; diff --git a/core/src/core/src/pydio/Core/Http/Response/XMLDocSerializableResponseChunk.php b/core/src/core/src/pydio/Core/Http/Response/XMLDocSerializableResponseChunk.php index c23da3159a..d1d00b2d9d 100644 --- a/core/src/core/src/pydio/Core/Http/Response/XMLDocSerializableResponseChunk.php +++ b/core/src/core/src/pydio/Core/Http/Response/XMLDocSerializableResponseChunk.php @@ -20,11 +20,10 @@ */ namespace Pydio\Core\Http\Response; -use Pydio\Core\Http\Response\XMLSerializableResponseChunk; defined('AJXP_EXEC') or die('Access not allowed'); interface XMLDocSerializableResponseChunk extends XMLSerializableResponseChunk { - + public function getCharset(); } \ No newline at end of file diff --git a/core/src/plugins/authfront.duosecurity/class.DuoSecurityFrontend.php b/core/src/plugins/authfront.duosecurity/class.DuoSecurityFrontend.php index 0ba72505a3..82f3924f65 100644 --- a/core/src/plugins/authfront.duosecurity/class.DuoSecurityFrontend.php +++ b/core/src/plugins/authfront.duosecurity/class.DuoSecurityFrontend.php @@ -98,9 +98,6 @@ function tryToLogUser(\Psr\Http\Message\ServerRequestInterface &$request, \Psr\H } $response = $response->withHeader("Content-type", "application/xml"); $response->getBody()->write(XMLWriter::loggingResult($loggingResult, $rememberLogin, $rememberPass, $secureToken, false)); - //XMLWriter::header(); - //XMLWriter::loggingResult($loggingResult, $rememberLogin, $rememberPass, $secureToken); - //XMLWriter::close(); if($loggingResult > 0 && $loggedUser != null){ diff --git a/core/src/plugins/authfront.otp/class.OtpAuthFrontend.php b/core/src/plugins/authfront.otp/class.OtpAuthFrontend.php index 8f17122cd2..da8bf71052 100644 --- a/core/src/plugins/authfront.otp/class.OtpAuthFrontend.php +++ b/core/src/plugins/authfront.otp/class.OtpAuthFrontend.php @@ -127,14 +127,6 @@ function tryToLogUser(\Psr\Http\Message\ServerRequestInterface &$request, \Psr\H protected function breakAndSendError($exceptionMsg){ throw new \Pydio\Core\Exception\AuthRequiredException($exceptionMsg); - /* - XMLWriter::header(); - XMLWriter::loggingResult(-1, null, null, null); - XMLWriter::sendMessage("ERROR", $exceptionMsg); - XMLWriter::close(); - //throw new AJXP_Exception($exceptionMsg); - exit(); - */ } diff --git a/core/src/plugins/core.access/src/Model/NodesList.php b/core/src/plugins/core.access/src/Model/NodesList.php index 2133bd8396..112a404463 100644 --- a/core/src/plugins/core.access/src/Model/NodesList.php +++ b/core/src/plugins/core.access/src/Model/NodesList.php @@ -166,4 +166,9 @@ public function jsonSerializableKey() { return $this->parentNode->getPath(); } + + public function getCharset() + { + return "UTF-8"; + } } \ No newline at end of file diff --git a/core/src/plugins/editor.eml/EmlXmlMessage.php b/core/src/plugins/editor.eml/EmlXmlMessage.php new file mode 100644 index 0000000000..b35c1e3233 --- /dev/null +++ b/core/src/plugins/editor.eml/EmlXmlMessage.php @@ -0,0 +1,75 @@ + + * This file is part of Pydio. + * + * Pydio is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Pydio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Pydio. If not, see . + * + * The latest code can be found at . + */ + +defined('AJXP_EXEC') or die('Access not allowed'); + + +class EmlXmlMessage implements \Pydio\Core\Http\Response\XMLDocSerializableResponseChunk +{ + + protected $charset; + protected $html; + protected $text; + + /** + * EmlXmlMessage constructor. + * @param string $charset + * @param bool $html + * @param bool $text + */ + public function __construct($charset = "UTF-8", $html = false, $text = false) + { + $this->charset = $charset; + $this->html = $html; + $this->text = $text; + } + + /** + * @return string + */ + public function toXML() + { + $buffer = ""; + if (isSet($this->charset)) { + header('Content-Type: text/xml; charset='.$this->charset); + } + $buffer .= ''; + if ($this->html!==false) { + $buffer .= 'html->body; + $buffer .= "]]>"; + } + if ($this->text!==false) { + $buffer .= 'text->body; + $buffer .= "]]>"; + } + $buffer .= ""; + return $buffer; + + } + + public function getCharset() + { + return $this->charset; + } + +} \ No newline at end of file diff --git a/core/src/plugins/editor.eml/manifest.xml b/core/src/plugins/editor.eml/manifest.xml index 23c16f31fc..f29507e1dc 100644 --- a/core/src/plugins/editor.eml/manifest.xml +++ b/core/src/plugins/editor.eml/manifest.xml @@ -37,32 +37,32 @@ - + - + - + - + - + - + diff --git a/core/src/plugins/index.elasticsearch/class.AjxpElasticSearch.php b/core/src/plugins/index.elasticsearch/class.AjxpElasticSearch.php index be701dff64..f29a8557f4 100644 --- a/core/src/plugins/index.elasticsearch/class.AjxpElasticSearch.php +++ b/core/src/plugins/index.elasticsearch/class.AjxpElasticSearch.php @@ -20,6 +20,7 @@ */ use Pydio\Access\Core\Model\AJXP_Node; +use Pydio\Core\Http\Message\UserMessage; use Pydio\Core\Services\AuthService; use Pydio\Core\Services\ConfService; use Pydio\Core\Utils\VarsFilter; @@ -132,24 +133,38 @@ public function indexationEnds($parentNode){ } } - public function applyAction($actionName, $httpVars, $fileVars) + public function applyAction(\Psr\Http\Message\ServerRequestInterface $requestInterface, \Psr\Http\Message\ResponseInterface &$responseInterface) { + $actionName = $requestInterface->getAttribute("action"); + $httpVars = $requestInterface->getParsedBody(); + $messages = ConfService::getMessages(); $repoId = $this->accessDriver->repository->getId(); + $x = new \Pydio\Core\Http\Response\SerializableResponseStream(); + $nodesList = new \Pydio\Access\Core\Model\NodesList(); + $responseInterface = $responseInterface->withBody($x); + $x->addChunk($nodesList); + if ($actionName == "search") { // TMP if (strpos($httpVars["query"], "keyword:") === 0) { $parts = explode(":", $httpVars["query"]); - $this->applyAction("search_by_keyword", array("field" => $parts[1]), array()); - return; + $requestInterface = $requestInterface->withAttribute("action", "search_by_keyword")->withParsedBody(["field" => $parts[1]]); + $this->applyAction($requestInterface, $responseInterface); + return null; } try { $this->loadIndex($repoId, false); } catch (Exception $ex) { - $this->applyAction("index", array(), array()); - throw new Exception($messages["index.lucene.7"]); + if (ConfService::backgroundActionsSupported() && !ConfService::currentContextIsCommandLine()) { + $task = \Pydio\Tasks\TaskService::actionAsTask("index", []); + $responseInterface = \Pydio\Tasks\TaskService::getInstance()->enqueueTask($task, $requestInterface, $responseInterface); + $x->addChunk(new UserMessage($messages["index.lucene.7"])); + }else{ + $x->addChunk(new UserMessage($messages["index.lucene.12"])); + } } $textQuery = $httpVars["query"]; @@ -244,7 +259,6 @@ public function applyAction($actionName, $httpVars, $fileVars) $this->logDebug(__FUNCTION__,"Search finished. "); $hits = $result->getResults(); - XMLWriter::header(); foreach ($hits as $hit) { $source = $hit->getSource(); @@ -262,11 +276,9 @@ public function applyAction($actionName, $httpVars, $fileVars) } $tmpNode->search_score = sprintf("%0.2f", $hit->getScore()); - XMLWriter::renderAjxpNode($tmpNode); + $nodesList->addBranch($tmpNode); } - XMLWriter::close(); - } else if ($actionName == "search_by_keyword") { $scope = "user"; @@ -330,8 +342,8 @@ public function applyAction($actionName, $httpVars, $fileVars) $this->logDebug(__FUNCTION__,"Search finished. "); $hits = $result->getResults(); - XMLWriter::header(); foreach ($hits as $hit) { + if ($hit->serialized_metadata!=null) { $meta = unserialize(base64_decode($hit->serialized_metadata)); $tmpNode = new AJXP_Node(TextEncoder::fromUTF8($hit->node_url), $meta); @@ -344,9 +356,9 @@ public function applyAction($actionName, $httpVars, $fileVars) continue; } $tmpNode->search_score = sprintf("%0.2f", $hit->score); - XMLWriter::renderAjxpNode($tmpNode); + $nodesList->addBranch($tmpNode); + } - XMLWriter::close(); } } diff --git a/core/src/plugins/meta.git/class.GitManager.php b/core/src/plugins/meta.git/class.GitManager.php index 37020fd2a3..7eaf41c784 100644 --- a/core/src/plugins/meta.git/class.GitManager.php +++ b/core/src/plugins/meta.git/class.GitManager.php @@ -67,15 +67,24 @@ public function initMeta($accessDriver) } } - public function applyActions($actionName, $httpVars, $fileVars) + /** + * @param \Psr\Http\Message\ServerRequestInterface $requestInterface + * @param \Psr\Http\Message\ResponseInterface $responseInterface + */ + public function applyActions(\Psr\Http\Message\ServerRequestInterface $requestInterface, \Psr\Http\Message\ResponseInterface &$responseInterface) { + $actionName = $requestInterface->getAttribute("action"); + $httpVars = $requestInterface->getParsedBody(); + $x = new \Pydio\Core\Http\Response\SerializableResponseStream(); + $responseInterface = $responseInterface->withBody($x); + $git = new VersionControl_Git($this->repoBase); switch ($actionName) { case "git_history": + $nodesList = new \Pydio\Access\Core\Model\NodesList(); $file = Utils::decodeSecureMagic($httpVars["file"]); $file = ltrim($file, "/"); $res = $this->gitHistory($git, $file); - XMLWriter::header(); $ic = Utils::mimetype($file, "image", false); $index = count($res); $mess = ConfService::getMessages(); @@ -84,10 +93,13 @@ public function applyActions($actionName, $httpVars, $fileVars) $commit["icon"] = $ic; $commit["index"] = $index; $commit["EVENT"] = $mess["meta.git.".$commit["EVENT"]]; + $commit["text"] = basename($commit["FILE"]); $index --; - XMLWriter::renderNode("/".$commit["ID"], basename($commit["FILE"]), true, $commit); + $n = new AJXP_Node("/".$commit["ID"], $commit); + $n->setLeaf(true); + $nodesList->addBranch($n); } - XMLWriter::close(); + $x->addChunk($nodesList); break; break; @@ -109,9 +121,9 @@ public function applyActions($actionName, $httpVars, $fileVars) $this->executeCommandInStreams($git, $commandLine, $outputStream); fclose($outputStream); $this->commitChanges(); - XMLWriter::header(); - XMLWriter::reloadDataNode(); - XMLWriter::close(); + $diff = new \Pydio\Access\Core\Model\NodesDiff(); + $diff->update(new AJXP_Node("pydio://".$this->accessDriver->repository->getId().$file)); + $x->addChunk($diff); break; @@ -131,30 +143,36 @@ public function applyActions($actionName, $httpVars, $fileVars) $command->addArgument($commitId.":".$file); $commandLine = $command->createCommandString(); - if ($attach == "inline") { - $fileExt = substr(strrchr(basename($file), '.'), 1); - if (empty($fileExt)) { - $fileMime = "application/octet-stream"; - } else { - $regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($fileExt\s)/i"; - $lines = file( AJXP_INSTALL_PATH."/".AJXP_PLUGINS_FOLDER."/editor.browser/resources/other/mime.types"); - foreach ($lines as $line) { - if(substr($line, 0, 1) == '#') - continue; // skip comments - $line = rtrim($line) . " "; - if(!preg_match($regex, $line, $matches)) - continue; // no match to the extension - $fileMime = $matches[1]; + $reader = function() use ($git, $commandLine, $attach, $file, $size){ + if ($attach == "inline") { + $fileExt = substr(strrchr(basename($file), '.'), 1); + if (empty($fileExt)) { + $fileMime = "application/octet-stream"; + } else { + $regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($fileExt\s)/i"; + $lines = file( AJXP_INSTALL_PATH."/".AJXP_PLUGINS_FOLDER."/editor.browser/resources/other/mime.types"); + foreach ($lines as $line) { + if(substr($line, 0, 1) == '#') + continue; // skip comments + $line = rtrim($line) . " "; + if(!preg_match($regex, $line, $matches)) + continue; // no match to the extension + $fileMime = $matches[1]; + } } + if(empty($fileMime)) $fileMime = "application/octet-stream"; + HTMLWriter::generateInlineHeaders(basename($file), $size, $fileMime); + } else { + HTMLWriter::generateAttachmentsHeader(basename($file), $size, false, false); } - if(empty($fileMime)) $fileMime = "application/octet-stream"; - HTMLWriter::generateInlineHeaders(basename($file), $size, $fileMime); - } else { - HTMLWriter::generateAttachmentsHeader(basename($file), $size, false, false); - } - $outputStream = fopen("php://output", "a"); - $this->executeCommandInStreams($git, $commandLine, $outputStream); - fclose($outputStream); + $outputStream = fopen("php://output", "a"); + $this->executeCommandInStreams($git, $commandLine, $outputStream); + fclose($outputStream); + }; + + $async = new \Pydio\Core\Http\Response\AsyncResponseStream($reader); + $responseInterface = $responseInterface->withBody($async); + break; break; diff --git a/core/src/plugins/meta.simple_lock/class.SimpleLockManager.php b/core/src/plugins/meta.simple_lock/class.SimpleLockManager.php index 423b651ffe..95a214bd56 100644 --- a/core/src/plugins/meta.simple_lock/class.SimpleLockManager.php +++ b/core/src/plugins/meta.simple_lock/class.SimpleLockManager.php @@ -55,12 +55,14 @@ public function initMeta($accessDriver) } /** - * @param string $action - * @param array $httpVars - * @param array $fileVars + * @param \Psr\Http\Message\ServerRequestInterface $requestInterface + * @param \Psr\Http\Message\ResponseInterface $responseInterface + * @throws Exception */ - public function applyChangeLock($actionName, $httpVars, $fileVars) + public function applyChangeLock(\Psr\Http\Message\ServerRequestInterface $requestInterface, \Psr\Http\Message\ResponseInterface &$responseInterface) { + $httpVars = $requestInterface->getParsedBody(); + if ($this->accessDriver instanceof \Pydio\Access\Driver\StreamProvider\FS\demoAccessDriver) { throw new Exception("Write actions are disabled in demo mode!"); } @@ -83,9 +85,11 @@ public function applyChangeLock($actionName, $httpVars, $fileVars) AJXP_METADATA_SCOPE_GLOBAL ); } - XMLWriter::header(); - XMLWriter::reloadDataNode(); - XMLWriter::close(); + $x = new \Pydio\Core\Http\Response\SerializableResponseStream(); + $diff = new \Pydio\Access\Core\Model\NodesDiff(); + $diff->update($selection->getUniqueNode()); + $x->addChunk($diff); + $responseInterface = $responseInterface->withBody($x); } /** diff --git a/core/src/plugins/meta.watch/class.MetaWatchRegister.php b/core/src/plugins/meta.watch/class.MetaWatchRegister.php index 154432fd25..97462c4045 100755 --- a/core/src/plugins/meta.watch/class.MetaWatchRegister.php +++ b/core/src/plugins/meta.watch/class.MetaWatchRegister.php @@ -23,7 +23,6 @@ use Pydio\Access\Core\Model\UserSelection; use Pydio\Core\Services\AuthService; use Pydio\Core\Services\ConfService; -use Pydio\Core\Controller\XMLWriter; use Pydio\Core\PluginFramework\PluginsService; use Pydio\Meta\Core\AJXP_AbstractMetaSource; use Pydio\Metastore\Core\MetaStoreProvider; @@ -365,63 +364,59 @@ private function loadWatchesFromMeta($watchType, $currentUserId, $node, $watchMe return $IDS; } - public function switchActions($actionName, $httpVars, $fileVars) + public function switchActions(\Psr\Http\Message\ServerRequestInterface $requestInterface, \Psr\Http\Message\ResponseInterface &$responseInterface) { - switch ($actionName) { + $actionName = $requestInterface->getAttribute("action"); + $httpVars = $requestInterface->getParsedBody(); + if($actionName !== "toggle_watch") return; - case "toggle_watch": + $us = new UserSelection($this->accessDriver->repository, $httpVars); + $node = $us->getUniqueNode(); + $node->loadNodeInfo(); + $cmd = $httpVars["watch_action"]; - $us = new UserSelection($this->accessDriver->repository, $httpVars); - $node = $us->getUniqueNode(); - $node->loadNodeInfo(); - $cmd = $httpVars["watch_action"]; - - $meta = $this->metaStore->retrieveMetadata( - $node, - self::$META_WATCH_NAMESPACE, - false, - AJXP_METADATA_SCOPE_REPOSITORY - ); - $userId = AuthService::getLoggedUser()!= null ? AuthService::getLoggedUser()->getId() : "shared"; - - if ($cmd == "watch_stop" && isSet($meta) && isSet($meta[$userId])) { - unset($meta[$userId]); - $this->metaStore->removeMetadata( - $node, - self::$META_WATCH_NAMESPACE, - false, - AJXP_METADATA_SCOPE_REPOSITORY - ); - } else { - switch ($cmd) { - case "watch_change": $type = self::$META_WATCH_CHANGE;break; - case "watch_read": $type = self::$META_WATCH_READ; break; - case "watch_both": $type = self::$META_WATCH_BOTH; break; - default: break; - } - $meta[$userId] = $type; - $this->metaStore->setMetadata( - $node, - self::$META_WATCH_NAMESPACE, - $meta, - false, - AJXP_METADATA_SCOPE_REPOSITORY - ); - } - - XMLWriter::header(); - $node->metadata = array(); - $node->loadNodeInfo(true, false, "all"); - $this->enrichNode($node); - XMLWriter::writeNodesDiff(array("UPDATE" => array( $node->getPath() => $node )), true); - XMLWriter::close(); + $meta = $this->metaStore->retrieveMetadata( + $node, + self::$META_WATCH_NAMESPACE, + false, + AJXP_METADATA_SCOPE_REPOSITORY + ); + $userId = AuthService::getLoggedUser()!= null ? AuthService::getLoggedUser()->getId() : "shared"; - break; + if ($cmd == "watch_stop" && isSet($meta) && isSet($meta[$userId])) { + unset($meta[$userId]); + $this->metaStore->removeMetadata( + $node, + self::$META_WATCH_NAMESPACE, + false, + AJXP_METADATA_SCOPE_REPOSITORY + ); + } else { + switch ($cmd) { + case "watch_change": $type = self::$META_WATCH_CHANGE;break; + case "watch_read": $type = self::$META_WATCH_READ; break; + case "watch_both": $type = self::$META_WATCH_BOTH; break; + default: $type = self::$META_WATCH_BOTH; break; + } + $meta[$userId] = $type; + $this->metaStore->setMetadata( + $node, + self::$META_WATCH_NAMESPACE, + $meta, + false, + AJXP_METADATA_SCOPE_REPOSITORY + ); + } - default: - break; + $node->metadata = array(); + $node->loadNodeInfo(true, false, "all"); + $this->enrichNode($node); + $nodesDiff = new \Pydio\Access\Core\Model\NodesDiff(); + $nodesDiff->update($node); + $x = new \Pydio\Core\Http\Response\SerializableResponseStream(); + $x->addChunk($nodesDiff); + $responseInterface = $responseInterface->withBody($x); - } } diff --git a/core/src/plugins/uploader.http/class.HttpDownloader.php b/core/src/plugins/uploader.http/class.HttpDownloader.php index 5778576ed7..243e935851 100644 --- a/core/src/plugins/uploader.http/class.HttpDownloader.php +++ b/core/src/plugins/uploader.http/class.HttpDownloader.php @@ -78,20 +78,6 @@ public function switchAction(\Psr\Http\Message\ServerRequestInterface $request, TaskService::getInstance()->enqueueTask($task, $request, $response); break; } - /* - if (!ConfService::currentContextIsCommandLine() && ConfService::backgroundActionsSupported()) { - - $unixProcess = Controller::applyActionInBackground($repository->getId(), "external_download", $httpVars); - if ($unixProcess !== null) { - @file_put_contents($currentDirUrl.".".$basename.".pid", $unixProcess->getPid()); - } - XMLWriter::header(); - XMLWriter::triggerBgAction("reload_node", array(), "Triggering DL ", true, 2); - XMLWriter::close(); - session_write_close(); - break; - } - */ require_once(AJXP_BIN_FOLDER."/lib/http_class/http_class.php"); session_write_close();