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

Commit

Permalink
Start command line Refactoring using symfony/console package.
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed May 12, 2016
1 parent 18d1c71 commit e6775f3
Show file tree
Hide file tree
Showing 8 changed files with 505 additions and 3 deletions.
39 changes: 39 additions & 0 deletions core/src/cli.php
@@ -0,0 +1,39 @@
<?php
/*
* Copyright 2007-2016 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* 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/>.
*
* Description : Command line access of the framework.
*/
if (php_sapi_name() !== "cli") {
die("This is the command line version of the framework, you are not allowed to access this page");
}
include_once ("base.conf.php");
use Pydio\Core\Services\ConfService;
use Symfony\Component\Console\Application;
use Pydio\Core\Http\Cli\Command;

ConfService::init();
ConfService::start();


$input = new \Pydio\Core\Http\Cli\FreeArgvOptions();
$application = new Application();
$application->add(new Command());
$application->setDefaultCommand("pydio");
$application->run($input);
3 changes: 2 additions & 1 deletion core/src/core/composer.json
Expand Up @@ -13,7 +13,8 @@
"guzzlehttp/guzzle-services": "~0.5", "guzzlehttp/guzzle-services": "~0.5",
"gimler/guzzle-description-loader" : "*", "gimler/guzzle-description-loader" : "*",
"commerceguys/guzzle-oauth2-plugin": "~2.0", "commerceguys/guzzle-oauth2-plugin": "~2.0",
"nikic/fast-route":"~1.0" "nikic/fast-route":"~1.0",
"symfony/console":"^3.0"
} }


} }
103 changes: 103 additions & 0 deletions core/src/core/src/pydio/Core/Http/Cli/AuthCliMiddleware.php
@@ -0,0 +1,103 @@
<?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\Cli;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Pydio\Auth\Core\AJXP_Safe;
use Pydio\Core\Exception\AuthRequiredException;
use Pydio\Core\Http\Server;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Services\ConfService;

defined('AJXP_EXEC') or die('Access not allowed');


class AuthCliMiddleware
{
/**
* @param ServerRequestInterface $requestInterface
* @param \Psr\Http\Message\ResponseInterface $responseInterface
* @param callable|null $next
* @return ResponseInterface
* @throws AuthRequiredException
*/
public static function handleRequest(ServerRequestInterface &$requestInterface, ResponseInterface &$responseInterface, callable $next = null){


$options = $requestInterface->getAttribute("cli-options");
$optUser = $options["u"];
$optPass = $options["p"];
$optRepoId = $options["r"];

$impersonateUser = false;

if (isSet($options["p"])) {
$optPass = $options["p"];
} else {
// Consider "u" is a crypted version of u:p
$optToken = $options["t"];
$cKey = ConfService::getCoreConf("AJXP_CLI_SECRET_KEY", "conf");
if(empty($cKey)) $cKey = "\1CDAFx¨op#";
$optUser = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($optToken.$cKey), base64_decode($optUser), MCRYPT_MODE_ECB), "\0");
$env = getenv("AJXP_SAFE_CREDENTIALS");
if(!empty($env)){
$array = AJXP_Safe::getCredentialsFromEncodedString($env);
if(isSet($array["user"]) && $array["user"] == $optUser){
unset($optToken);
$optPass = $array["password"];
}
}
}


if (AuthService::usersEnabled() && !empty($optUser)) {
$seed = AuthService::generateSeed();
if ($seed != -1) {
$optPass = md5(md5($optPass).$seed);
}
$loggingResult = AuthService::logUser($optUser, $optPass, isSet($optToken), false, $seed);
// Check that current user can access current repository, try to switch otherwise.
$loggedUser = AuthService::getLoggedUser();
if ($loggedUser != null && $impersonateUser !== false && $loggedUser->isAdmin()) {
AuthService::disconnect();
AuthService::logUser($impersonateUser, "empty", true, false, "");
$loggedUser = AuthService::getLoggedUser();
}
if ($loggedUser != null) {
ConfService::switchRootDir($optRepoId, true);
}
if (isset($loggingResult) && $loggingResult != 1) {
throw new AuthRequiredException();
}
} else {
throw new AuthRequiredException();
}

ConfService::reloadServicesAndActivePlugins();

$requestInterface = $requestInterface->withAttribute("action", $options["a"]);

return Server::callNextMiddleWare($requestInterface, $responseInterface, $next);

}

}
86 changes: 86 additions & 0 deletions core/src/core/src/pydio/Core/Http/Cli/CliMiddleware.php
@@ -0,0 +1,86 @@
<?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\Cli;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Pydio\Core\Exception\AuthRequiredException;
use Pydio\Core\Exception\WorkspaceNotFoundException;
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
{
/**
* @param ServerRequestInterface $requestInterface
* @param \Psr\Http\Message\ResponseInterface $responseInterface
* @return \Psr\Http\Message\ResponseInterface
* @param callable|null $next
* @throws WorkspaceNotFoundException
*/
public static function handleRequest(ServerRequestInterface &$requestInterface, ResponseInterface &$responseInterface, callable $next = null){

/**
* @var OutputInterface
*/
$output = $requestInterface->getAttribute("cli-output");
$options = $requestInterface->getAttribute("cli-options");
$statusFile = (!empty($options["s"]) ? $options["s"] : false);

try {

$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);
}
}

$responseInterface->getBody()->write("toto");
$responseInterface->getBody()->rewind();
$output->writeln("" . $responseInterface->getBody());

} catch (AuthRequiredException $e){

$output->writeln("ERROR:Authentication Failed.");
if($statusFile !== false){
file_put_contents($statusFile, "ERROR:Authentication Failed.");
}

}catch (\Exception $e){

$output->writeln("ERROR:".$e->getMessage());
if($statusFile !== false){
file_put_contents($statusFile, "ERROR:Authentication Failed.");
}

}


}
}
99 changes: 99 additions & 0 deletions core/src/core/src/pydio/Core/Http/Cli/Command.php
@@ -0,0 +1,99 @@
<?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\Cli;

defined('AJXP_EXEC') or die('Access not allowed');
use Pydio\Core\Http\Server;
use Symfony;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Command extends Symfony\Component\Console\Command\Command
{
protected function configure()
{
$this->setDefinition(new FreeDefOptions());
$this
->setName('pydio')
->setDescription('Pydio Command Line')
->addOption(
'cli_username',
'u',
InputOption::VALUE_REQUIRED,
'User id or user token'
)->addOption(
'cli_password',
'p',
InputOption::VALUE_OPTIONAL,
'User Password'
)->addOption(
'cli_token',
't',
InputOption::VALUE_OPTIONAL,
'Encrypted Token used to replace password'
)->addOption(
'cli_repository_id',
'r',
InputOption::VALUE_REQUIRED,
'Repository ID or alias'
)->addOption(
'cli_action_name',
'a',
InputOption::VALUE_REQUIRED,
'Action name to apply'
)->addOption(
'cli_status_file',
's',
InputOption::VALUE_OPTIONAL,
'Path to a file to write status information about the running task'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$definitionsKeys = array_keys($this->getDefinition()->getOptions());
$actionParameters = [];
$pydioCliOptions = [];
foreach ($input->getOptions() as $key => $option){
if(in_array($key, $definitionsKeys)){
if(strpos($key, "cli_") === 0) {
$shortcut = $this->getDefinition()->getOption($key)->getShortcut();
$pydioCliOptions[$shortcut] = FreeArgvOptions::removeEqualsSign($option);
}
}else{
$actionParameters[$key] = $option;
}
}

$server = new Server(Server::MODE_CLI);
$request = $server->getRequest();
$request = $request
->withParsedBody($actionParameters)
->withAttribute("cli-options", $pydioCliOptions)
->withAttribute("cli-output", $output);
$server->updateRequest($request);
$server->listen();

}
}

0 comments on commit e6775f3

Please sign in to comment.