Permalink
Browse files

Use Request/Response objects instead of direct printing. Create dedic…

…ated Message object when necessary
  • Loading branch information...
1 parent 9e2537a commit 3d1834065d947643bea8bf772ab27e4fc2d42d22 @cdujeu cdujeu committed Jun 1, 2016
@@ -0,0 +1,123 @@
+<?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/>.
+ */
+namespace Pydio\Core\Http\Message;
+
+use Pydio\Core\Controller\XMLWriter;
+use Pydio\Core\Http\Response\JSONSerializableResponseChunk;
+use Pydio\Core\Http\Response\XMLDocSerializableResponseChunk;
+use Pydio\Core\Utils\Utils;
+
+defined('AJXP_EXEC') or die('Access not allowed');
+
+
+class RegistryMessage implements XMLDocSerializableResponseChunk, JSONSerializableResponseChunk
+{
+ /**
+ * @var \DOMDocument
+ */
+ protected $registry;
+
+ /**
+ * @var string|null
+ */
+ protected $xPath;
+
+ /**
+ * @var \DOMXPath|null
+ */
+ protected $xPathObject;
+
+ /**
+ * @var String
+ */
+ protected $renderedXML;
+
+ public function __construct($registry, $xPath = null, $xPathObject = null)
+ {
+ $this->registry = $registry;
+ $this->xPath = $xPath;
+ $this->xPathObject = $xPathObject;
+ }
+
+
+ public function getCharset()
+ {
+ return "UTF-8";
+ }
+
+ /**
+ * @return string
+ */
+ public function toXML()
+ {
+ if(!empty($this->renderedXML)){
+ return $this->renderedXML;
+ }
+ if (!empty($this->xPath)) {
+
+ $xml = "<ajxp_registry_part xPath=\"".$this->xPath."\">";
+ if(empty($this->xPathObject)){
+ $this->xPathObject = new \DOMXPath($this->registry);
+ }
+ $nodes = $this->xPathObject->query($this->xPath);
+ if ($nodes->length) {
+ $xml .= XMLWriter::replaceAjxpXmlKeywords($this->registry->saveXML($nodes->item(0)));
+ }
+ $xml .= "</ajxp_registry_part>";
+
+ } else {
+
+ Utils::safeIniSet("zlib.output_compression", "4096");
+ $xml = XMLWriter::replaceAjxpXmlKeywords($this->registry->saveXML());
+
+ }
+ $this->renderedXML = $xml;
+ return $xml;
+ }
+
+ /**
+ * @return mixed
+ */
+ public function jsonSerializableData()
+ {
+ if(!empty($this->xPath)){
+ if(empty($this->xPathObject)){
+ $this->xPathObject = new \DOMXPath($this->registry);
+ }
+ $nodes = $this->xPathObject->query($this->xPath);
+ $data = [];
+ if($nodes->length){
+ $data = XMLWriter::xmlToArray($nodes->item(0));
+ }
+ return $data;
+ }else{
+ return XMLWriter::xmlToArray($this->registry);
+ }
+ }
+
+ /**
+ * @return string
+ */
+ public function jsonSerializableKey()
+ {
+ return null;
+ }
+}
@@ -0,0 +1,50 @@
+<?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/>.
+ */
+namespace Pydio\Core\Http\Message;
+
+defined('AJXP_EXEC') or die('Access not allowed');
+
+use Pydio\Core\Http\Response\XMLDocSerializableResponseChunk;
+
+class XMLDocMessage extends \DOMDocument implements XMLDocSerializableResponseChunk
+{
+
+ public function __construct($xmlString = null)
+ {
+ parent::__construct("1.0", "UTF-8");
+ if(!empty($xmlString)){
+ $this->loadXML($xmlString);
+ }
+ }
+
+ public function getCharset()
+ {
+ return 'UTF-8';
+ }
+
+ /**
+ * @return string
+ */
+ public function toXML()
+ {
+ return $this->saveXML();
+ }
+}
@@ -94,7 +94,12 @@ public function emitResponse(ServerRequestInterface $request, ResponseInterface
* @var SerializableResponseStream $body;
*/
$body = &$response->getBody();
- if($request->hasHeader("Accept") && $request->getHeader("Accept")[0] == "application/json"){
+ $params = $request->getParsedBody();
+ $forceJson = false;
+ if(isSet($params["format"]) && $params["format"] == "json"){
+ $forceJson = true;
+ }
+ if(($request->hasHeader("Accept") && $request->getHeader("Accept")[0] == "application/json") || $forceJson){
$body->setSerializer(SerializableResponseStream::SERIALIZER_TYPE_JSON);
$response = $response->withHeader("Content-type", "application/json; charset=UTF-8");
}else{
@@ -44,6 +44,13 @@ class SerializableResponseStream implements StreamInterface
private $streamStatus = 'open';
+ public function __construct($chunks = [])
+ {
+ if(count($chunks)){
+ $this->data = $chunks;
+ }
+ }
+
/**
* @param string $serializer SERIALIZER_TYPE_XML|SERIALIZER_TYPE_JSON
*/
@@ -129,7 +136,10 @@ protected function serializeData($data, $serializer){
if($wrap){
return XMLWriter::wrapDocument($buffer);
}else{
- return "<?xml version=\"1.0\" encoding=\"".$charset."\"?>".$buffer;
+ if(substr($buffer, 0, 5) !== "<?xml"){
+ $buffer = "<?xml version=\"1.0\" encoding=\"".$charset."\"?>".$buffer;
+ }
+ return $buffer;
}
}
return "";
@@ -71,7 +71,11 @@ public function userExists($login)
return true ;
}
- public function logoutCallback($actionName, $httpVars, $fileVars)
+ /**
+ * @param \Psr\Http\Message\ServerRequestInterface $requestInterface
+ * @param \Psr\Http\Message\ResponseInterface $responseInterface
+ */
+ public function logoutCallback(\Psr\Http\Message\ServerRequestInterface $requestInterface, \Psr\Http\Message\ResponseInterface &$responseInterface)
{
$safeCredentials = AJXP_Safe::loadCredentials();
$crtUser = $safeCredentials["user"];
@@ -90,15 +94,17 @@ public function logoutCallback($actionName, $httpVars, $fileVars)
AuthService::disconnect();
session_destroy();
session_write_close();
- XMLWriter::header();
- XMLWriter::loggingResult(2);
- XMLWriter::close();
+
+ $x = new \Pydio\Core\Http\Response\SerializableResponseStream();
+ $x->addChunk(new \Pydio\Core\Http\Message\LoggingResult(2));
+ $responseInterface = $responseInterface->withBody($x);
}
- public function setFtpDataCallback($actionName, $httpVars, $fileVars)
+ public function setFtpDataCallback(\Psr\Http\Message\ServerRequestInterface $requestInterface, \Psr\Http\Message\ResponseInterface &$responseInterface)
{
$options = array("CHARSET", "FTP_DIRECT", "FTP_HOST", "FTP_PORT", "FTP_SECURE", "PATH");
$ftpOptions = array();
+ $httpVars = $requestInterface->getParsedBody();
foreach ($options as $option) {
if (isSet($httpVars[$option])) {
$ftpOptions[$option] = $httpVars[$option];
@@ -2,7 +2,6 @@
use Pydio\Auth\Core\AbstractAuthDriver;
use Pydio\Auth\Core\AJXP_Safe;
use Pydio\Core\Services\AuthService;
-use Pydio\Core\Controller\XMLWriter;
use Pydio\Log\Core\AJXP_Logger;
defined('AJXP_EXEC') or die('Access not allowed');
@@ -70,16 +69,19 @@ public function userExists($login)
{
return true;
}
- public function logoutCallback($actionName, $httpVars, $fileVars)
+
+
+ public function logoutCallback(\Psr\Http\Message\ServerRequestInterface $requestInterface, \Psr\Http\Message\ResponseInterface &$responseInterface)
{
AJXP_Safe::clearCredentials();
- $adminUser = $this->options["AJXP_ADMIN_LOGIN"];
AuthService::disconnect();
session_write_close();
- XMLWriter::header();
- XMLWriter::loggingResult(2);
- XMLWriter::close();
+
+ $x = new \Pydio\Core\Http\Response\SerializableResponseStream();
+ $x->addChunk(new \Pydio\Core\Http\Message\LoggingResult(2));
+ $responseInterface = $responseInterface->withBody($x);
}
+
public function prepareRequest($res, $login, $pass, $seed)
{
if (!radius_add_server($res, $this->radiusServer, $this->radiusPort, $this->radiusSecret, 3, 3)) {
@@ -22,7 +22,6 @@
use Pydio\Auth\Core\AJXP_Safe;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Services\ConfService;
-use Pydio\Core\Controller\XMLWriter;
defined('AJXP_EXEC') or die( 'Access not allowed');
@@ -46,21 +45,19 @@ public function userExists($login)
return true;
}
- public function logoutCallback($actionName, $httpVars, $fileVars)
+ public function logoutCallback(\Psr\Http\Message\ServerRequestInterface $requestInterface, \Psr\Http\Message\ResponseInterface &$responseInterface)
{
AJXP_Safe::clearCredentials();
- $adminUser = $this->options["ADMIN_USER"];
- $subUsers = array();
foreach($_SESSION as $key => $val){
if((substr($key,-4) === "disk") && (substr($key,0,4) == "smb_")){
unset($_SESSION[$key]);
}
}
AuthService::disconnect();
session_write_close();
- XMLWriter::header();
- XMLWriter::loggingResult(2);
- XMLWriter::close();
+ $x = new \Pydio\Core\Http\Response\SerializableResponseStream();
+ $x->addChunk(new \Pydio\Core\Http\Message\LoggingResult(2));
+ $responseInterface = $responseInterface->withBody($x);
}
public function checkPassword($login, $pass, $seed)
@@ -0,0 +1,46 @@
+<?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 LogoutUrlMessage implements \Pydio\Core\Http\Response\XMLDocSerializableResponseChunk
+{
+ protected $url;
+
+ public function __construct($url)
+ {
+ $this->url = $url;
+ }
+
+ public function getCharset()
+ {
+ return "UTF-8";
+ }
+
+ /**
+ * @return string
+ */
+ public function toXML()
+ {
+ return "<url>".$this->url."</url>";
+ }
+}
Oops, something went wrong.

0 comments on commit 3d18340

Please sign in to comment.