Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Use Request/Response objects instead of direct printing. Create dedic…
Browse files Browse the repository at this point in the history
…ated Message object when necessary
  • Loading branch information
cdujeu committed Jun 1, 2016
1 parent 01c7533 commit bbe3ee1
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 134 deletions.
Expand Up @@ -115,19 +115,21 @@ 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;
}
$buffer .= $serializableItem->toXML();
if($serializableItem instanceof XMLDocSerializableResponseChunk){
$wrap = false;
$charset = $serializableItem->getCharset();
}
}
if($wrap){
return XMLWriter::wrapDocument($buffer);
}else{
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".$buffer;
return "<?xml version=\"1.0\" encoding=\"".$charset."\"?>".$buffer;
}
}
return "";
Expand Down
Expand Up @@ -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();
}
Expand Up @@ -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){

Expand Down
8 changes: 0 additions & 8 deletions core/src/plugins/authfront.otp/class.OtpAuthFrontend.php
Expand Up @@ -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();
*/

}

Expand Down
5 changes: 5 additions & 0 deletions core/src/plugins/core.access/src/Model/NodesList.php
Expand Up @@ -166,4 +166,9 @@ public function jsonSerializableKey()
{
return $this->parentNode->getPath();
}

public function getCharset()
{
return "UTF-8";
}
}
75 changes: 75 additions & 0 deletions core/src/plugins/editor.eml/EmlXmlMessage.php
@@ -0,0 +1,75 @@
<?php
/*
* Copyright 2007-2016 Abstrium <contact (at) pydio.com>
* 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 <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com/>.
*/

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 .= '<email_body>';
if ($this->html!==false) {
$buffer .= '<mimepart type="html"><![CDATA[';
$buffer .= $this->html->body;
$buffer .= "]]></mimepart>";
}
if ($this->text!==false) {
$buffer .= '<mimepart type="plain"><![CDATA[';
$buffer .= $this->text->body;
$buffer .= "]]></mimepart>";
}
$buffer .= "</email_body>";
return $buffer;

}

public function getCharset()
{
return $this->charset;
}

}
12 changes: 6 additions & 6 deletions core/src/plugins/editor.eml/manifest.xml
Expand Up @@ -37,32 +37,32 @@
<actions>
<action name="eml_get_xml_structure">
<processing>
<serverCallback methodName="switchAction"></serverCallback>
<serverCallback methodName="switchAction"/>
</processing>
</action>
<action name="eml_get_bodies">
<processing>
<serverCallback methodName="switchAction"></serverCallback>
<serverCallback methodName="switchAction"/>
</processing>
</action>
<action name="eml_dl_attachment">
<processing>
<serverCallback methodName="switchAction"></serverCallback>
<serverCallback methodName="switchAction"/>
</processing>
</action>
<action name="eml_cp_attachment">
<processing>
<serverCallback methodName="switchAction"></serverCallback>
<serverCallback methodName="switchAction"/>
</processing>
</action>
<action name="ls" dirDefault="true">
<post_processing>
<serverCallback methodName="lsPostProcess" capture="true"></serverCallback>
<serverCallback methodName="lsPostProcess" capture="true"/>
</post_processing>
</action>
</actions>
<hooks>
<serverCallback hookName="node.info" methodName="extractMimeHeaders"></serverCallback>
<serverCallback hookName="node.info" methodName="extractMimeHeaders"/>
</hooks>
</registry_contributions>
<dependencies>
Expand Down
36 changes: 24 additions & 12 deletions core/src/plugins/index.elasticsearch/class.AjxpElasticSearch.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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"];
Expand Down Expand Up @@ -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();

Expand All @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}

}
Expand Down
76 changes: 47 additions & 29 deletions core/src/plugins/meta.git/class.GitManager.php
Expand Up @@ -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();
Expand All @@ -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;

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

0 comments on commit bbe3ee1

Please sign in to comment.