Permalink
Browse files

Set server as registerCatchAll, remove from ConfService and XMLWriter…

…, implement SerializableResponseChunk and certain exceptions to pass them directly.
  • Loading branch information...
1 parent 5015584 commit c330762a56244f733da560bd9269e3f6d5e305dd @cdujeu cdujeu committed May 24, 2016
@@ -22,17 +22,13 @@
use Pydio\Access\Core\Model\AJXP_Node;
use Pydio\Access\Core\IAjxpWrapperProvider;
-use Pydio\Access\Core\Model\Repository;
-use Pydio\Core\Exception\AuthRequiredException;
use Pydio\Core\Utils\Utils;
-use Pydio\Core\Exception\PydioPromptException;
use Pydio\Core\Services;
use Pydio\Core\Services\AuthService;
use Pydio\Conf\Core\AbstractAjxpUser;
use Pydio\Core\Services\ConfService;
use Pydio\Core\PluginFramework\PluginsService;
use Pydio\Core\Utils\TextEncoder;
-use Pydio\Log\Core\AJXP_Logger;
defined('AJXP_EXEC') or die( 'Access not allowed');
@@ -260,89 +256,6 @@ public static function renderAjxpNode($ajxpNode, $close = true, $print = true)
}
/**
- * Render a node with arguments passed as array
- * @static
- * @param $array
- * @return void
- */
- public static function renderNodeArray($array)
- {
- self::renderNode($array[0],$array[1],$array[2],$array[3]);
- }
-
- /**
- * Error Catcher for PHP errors. Depending on the SERVER_DEBUG config
- * shows the file/line info or not.
- * @static
- * @param $code
- * @param $message
- * @param $fichier
- * @param $ligne
- * @param $context
- */
- public static function catchError($code, $message, $fichier, $ligne, $context)
- {
- if(error_reporting() == 0) {
- return ;
- }
- AJXP_Logger::error(basename($fichier), "error l.$ligne", array("message" => $message));
- if (ConfService::getConf("SERVER_DEBUG")) {
- $stack = debug_backtrace();
- $stackLen = count($stack);
- for ($i = 1; $i < $stackLen; $i++) {
- $entry = $stack[$i];
-
- $func = $entry['function'] . '(';
- $argsLen = count($entry['args']);
- for ($j = 0; $j < $argsLen; $j++) {
- $s = $entry['args'][$j];
- if(is_string($s)){
- $func .= $s;
- }else if (is_object($s)){
- $func .= get_class($s);
- }
- if ($j < $argsLen - 1) $func .= ', ';
- }
- $func .= ')';
-
- $message .= "\n". str_replace(dirname(__FILE__), '', $entry['file']) . ':' . $entry['line'] . ' - ' . $func . PHP_EOL;
- }
- }
- if(!headers_sent()) XMLWriter::header();
- if(!empty($context) && is_object($context) && $context instanceof PydioPromptException){
- XMLWriter::write("<prompt type=\"".$context->getPromptType()."\"><message>".$message."</message><data><![CDATA[".json_encode($context->getPromptData())."]]></data></prompt>", true);
- }else{
- XMLWriter::sendMessage(null, TextEncoder::toUTF8($message), true);
- }
- XMLWriter::close();
- }
-
- /**
- * Catch exceptions, @see catchError
- * @param \Exception $exception
- */
- public static function catchException($exception)
- {
- if($exception instanceof AuthRequiredException){
- XMLWriter::header();
- $message = $exception->getMessage();
- if(!empty($message)){
- XMLWriter::sendMessage(null, $message);
- }
- XMLWriter::requireAuth();
- XMLWriter::close();
- return;
- }
- try {
- XMLWriter::catchError($exception->getCode(), TextEncoder::fromUTF8($exception->getMessage()), $exception->getFile(), $exception->getLine(), $exception);
- } catch (\Exception $innerEx) {
- error_log(get_class($innerEx)." thrown within the exception handler!");
- error_log("Original exception was: ".$innerEx->getMessage()." in ".$innerEx->getFile()." on line ".$innerEx->getLine());
- error_log("New exception is: ".$innerEx->getMessage()." in ".$innerEx->getFile()." on line ".$innerEx->getLine()." ".$innerEx->getTraceAsString());
- print("Error");
- }
- }
- /**
* Dynamically replace XML keywords with their live values.
* AJXP_SERVER_ACCESS, AJXP_MIMES_*,AJXP_ALL_MESSAGES, etc.
* @static
@@ -20,12 +20,15 @@
*/
namespace Pydio\Core\Exception;
+use Pydio\Core\Http\Message\UserMessage;
+use Pydio\Core\Http\Response\JSONSerializableResponseChunk;
+use Pydio\Core\Http\Response\XMLSerializableResponseChunk;
use Pydio\Core\Services\ConfService;
defined('AJXP_EXEC') or die('Access not allowed');
-class AuthRequiredException extends PydioException
+class AuthRequiredException extends PydioException implements XMLSerializableResponseChunk, JSONSerializableResponseChunk
{
public function __construct($messageId = "", $messageString = "")
{
@@ -35,4 +38,33 @@ public function __construct($messageId = "", $messageString = "")
}
parent::__construct($messageString, $messageId);
}
+
+ /**
+ * @return mixed
+ */
+ public function jsonSerializableData()
+ {
+ return ["message" => $this->getMessage()];
+ }
+
+ /**
+ * @return string
+ */
+ public function jsonSerializableKey()
+ {
+ return "authRequired";
+ }
+
+ /**
+ * @return string
+ */
+ public function toXML()
+ {
+ $xml = "<require_auth/>";
+ if($this->getMessage()){
+ $error = new UserMessage($this->getMessage(), LOG_LEVEL_ERROR);
+ $xml.= $error->toXML();
+ }
+ return $xml;
+ }
}
@@ -64,4 +64,36 @@ public function hasErrorCode(){
public function getErrorCode(){
return $this->errorCode;
}
+
+ public static function buildDebugBackTrace(){
+
+ $message = "";
+
+ if (ConfService::getConf("SERVER_DEBUG")) {
+ $stack = debug_backtrace();
+ $stackLen = count($stack);
+ for ($i = 1; $i < $stackLen; $i++) {
+ $entry = $stack[$i];
+
+ $func = $entry['function'] . '(';
+ $argsLen = count($entry['args']);
+ for ($j = 0; $j < $argsLen; $j++) {
+ $s = $entry['args'][$j];
+ if(is_string($s)){
+ $func .= $s;
+ }else if (is_object($s)){
+ $func .= get_class($s);
+ }
+ if ($j < $argsLen - 1) $func .= ', ';
+ }
+ $func .= ')';
+
+ $message .= "\n". str_replace(dirname(__FILE__), '', $entry['file']) . ':' . $entry['line'] . ' - ' . $func . PHP_EOL;
+ }
+ }
+
+ return $message;
+
+ }
+
}
@@ -20,7 +20,8 @@
*/
namespace Pydio\Core\Exception;
-use Pydio\Core\Exception\PydioException;
+use Pydio\Core\Http\Response\JSONSerializableResponseChunk;
+use Pydio\Core\Http\Response\XMLSerializableResponseChunk;
defined('AJXP_EXEC') or die( 'Access not allowed');
@@ -35,7 +36,8 @@
* @package Pydio
* @subpackage Core
*/
-class PydioPromptException extends PydioException{
+class PydioPromptException extends PydioException implements XMLSerializableResponseChunk, JSONSerializableResponseChunk
+{
private $promptType = "prompt";
/**
@@ -105,4 +107,33 @@ public static function testOrPromptForCredentials($sessionVariable, $switchToRep
}
+ /**
+ * @return mixed
+ */
+ public function jsonSerializableData()
+ {
+ return [
+ "promptType" => $this->promptType,
+ "promptMessage" => $this->getMessage(),
+ "promptData" => $this->promptData
+ ];
+ }
+
+ /**
+ * @return string
+ */
+ public function jsonSerializableKey()
+ {
+ return "userPrompt";
+ }
+
+ /**
+ * @return string
+ */
+ public function toXML()
+ {
+ return "<prompt type=\"".$this->promptType."\"><message>".$this->getMessage()."</message><data><![CDATA[".json_encode($this->promptData)."]]></data></prompt>";
+ }
+
+
}
@@ -24,14 +24,15 @@
use Psr\Http\Message\ServerRequestInterface;
use Pydio\Core\Exception\AuthRequiredException;
use Pydio\Core\Exception\WorkspaceNotFoundException;
+use Pydio\Core\Http\Middleware\ITopLevelMiddleware;
use Pydio\Core\Http\Response\SerializableResponseStream;
use Pydio\Core\Http\Server;
use Symfony\Component\Console\Output\OutputInterface;
defined('AJXP_EXEC') or die('Access not allowed');
-class CliMiddleware
+class CliMiddleware implements ITopLevelMiddleware
{
/**
* @param ServerRequestInterface $requestInterface
@@ -40,7 +41,7 @@ class CliMiddleware
* @param callable|null $next
* @throws WorkspaceNotFoundException
*/
- public static function handleRequest(ServerRequestInterface $requestInterface, ResponseInterface $responseInterface, callable $next = null){
+ public function handleRequest(ServerRequestInterface $requestInterface, ResponseInterface $responseInterface, callable $next = null){
/**
* @var OutputInterface
@@ -53,17 +54,7 @@ public static function handleRequest(ServerRequestInterface $requestInterface, R
$responseInterface = Server::callNextMiddleWare($requestInterface, $responseInterface, $next);
- if($responseInterface !== false && $responseInterface->getBody() && $responseInterface->getBody() instanceof SerializableResponseStream){
- // For the moment, use XML by default
- // Todo: Create A CLI Serializer for pretty printing?
- if($requestInterface->getParsedBody()["format"] == "json"){
- $responseInterface->getBody()->setSerializer(SerializableResponseStream::SERIALIZER_TYPE_JSON);
- }
- }
- $output->writeln("Executing Action" . $requestInterface->getAttribute("action"));
- $output->writeln("----------------");
- $output->writeln("" . $responseInterface->getBody());
- $output->writeln("");
+ $this->emitResponse($requestInterface, $responseInterface);
} catch (AuthRequiredException $e){
@@ -81,6 +72,28 @@ public static function handleRequest(ServerRequestInterface $requestInterface, R
}
+ }
+
+ public function emitResponse(ServerRequestInterface $requestInterface, ResponseInterface $responseInterface){
+
+ $output = $requestInterface->getAttribute("cli-output");
+ if($responseInterface !== false && $responseInterface->getBody() && $responseInterface->getBody() instanceof SerializableResponseStream){
+ // For the moment, use XML by default
+ // Todo: Create A CLI Serializer for pretty printing?
+ if($requestInterface->getParsedBody()["format"] == "json"){
+ $responseInterface->getBody()->setSerializer(SerializableResponseStream::SERIALIZER_TYPE_JSON);
+ }
+ }
+ $body = "".$responseInterface->getBody();
+ $output->writeln("");
+ $output->writeln("-----------------------------------");
+ $output->writeln("Executing Action" . $requestInterface->getAttribute("action"));
+ $output->writeln("-----------------------------------");
+ if(!empty($body)){
+ $output->writeln("" . $responseInterface->getBody());
+ }else{
+ $output->writeln("No output");
+ }
}
}
@@ -82,6 +82,9 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
+ $server = new Server(Server::MODE_CLI);
+ $server->registerCatchAll();
+
$definitionsKeys = array_keys($this->getDefinition()->getOptions());
$actionParameters = [];
$pydioCliOptions = [];
@@ -104,7 +107,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$reqOptions = $pydioCliOptions;
$reqOptions["r"] = $repoId;
- $server = new Server(Server::MODE_CLI);
$request = $server->getRequest();
$request = $request
->withParsedBody($actionParameters)
@@ -0,0 +1,47 @@
+<?php
+/*
+ * Copyright 2007-2015 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 <http://pyd.io/>.
+ */
+namespace Pydio\Core\Http\Middleware;
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Pydio\Core\Exception\WorkspaceNotFoundException;
+
+defined('AJXP_EXEC') or die('Access not allowed');
+
+interface ITopLevelMiddleware
+{
+ /**
+ * @param ServerRequestInterface $requestInterface
+ * @param ResponseInterface $responseInterface
+ * @return mixed
+ */
+ public function emitResponse(ServerRequestInterface $requestInterface, ResponseInterface $responseInterface);
+
+ /**
+ * @param ServerRequestInterface $requestInterface
+ * @param \Psr\Http\Message\ResponseInterface $responseInterface
+ * @return \Psr\Http\Message\ResponseInterface
+ * @param callable|null $next
+ * @throws WorkspaceNotFoundException
+ */
+ public function handleRequest(ServerRequestInterface $requestInterface, ResponseInterface $responseInterface, callable $next = null);
+
+}
Oops, something went wrong.

0 comments on commit c330762

Please sign in to comment.