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

Commit

Permalink
Clean Controller, move backgroundActions methods to CliRunner class.
Browse files Browse the repository at this point in the history
All hooks MUST provide at least one ContextInterface or ContextProviderInterface argument
  • Loading branch information
cdujeu committed Jun 10, 2016
1 parent 39a87aa commit 4a6973f
Show file tree
Hide file tree
Showing 28 changed files with 465 additions and 352 deletions.
154 changes: 154 additions & 0 deletions core/src/core/src/pydio/Core/Controller/CliRunner.php
@@ -0,0 +1,154 @@
<?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\Controller;

use Pydio\Auth\Core\AJXP_Safe;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\Services\ConfService;
use Pydio\Core\Services\UsersService;
use Pydio\Core\Utils\UnixProcess;
use Pydio\Log\Core\AJXP_Logger;
use Pydio\Tasks\Task;
use Pydio\Tasks\TaskService;

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


/**
* Class CliRunner
* @package Pydio\Core\Controller
*/
class CliRunner
{

/**
* Apply a Task in background
*
* @param Task $task
*/
public static function applyTaskInBackground(Task $task)
{

$parameters = $task->getParameters();
$task->setStatus(Task::STATUS_RUNNING);
TaskService::getInstance()->updateTask($task);
self::applyActionInBackground($task->getContext(), $task->getAction(), $parameters, "", $task->getId());

}

/**
* Launch a command-line version of the framework by passing the actionName & parameters as arguments.
* @static
* @param ContextInterface $ctx
* @param String $actionName
* @param array $parameters
* @param string $statusFile
* @param string $taskId
* @return null|UnixProcess
*/
public static function applyActionInBackground(ContextInterface $ctx, $actionName, $parameters, $statusFile = "", $taskId = null)
{
$repositoryId = $ctx->getRepositoryId();
$user = $ctx->hasUser() ? $ctx->getUser()->getId() : "shared";

$token = md5(time());
$logDir = AJXP_CACHE_DIR . "/cmd_outputs";
if (!is_dir($logDir)) mkdir($logDir, 0755);
$logFile = $logDir . "/" . $token . ".out";

if (UsersService::usersEnabled()) {
$cKey = ConfService::getCoreConf("AJXP_CLI_SECRET_KEY", "conf");
if (empty($cKey)) {
$cKey = "\1CDAFx¨op#";
}
$user = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($token . $cKey), $user, MCRYPT_MODE_ECB));
}
$robustInstallPath = str_replace("/", DIRECTORY_SEPARATOR, AJXP_INSTALL_PATH);
$cmd = ConfService::getCoreConf("CLI_PHP") . " " . $robustInstallPath . DIRECTORY_SEPARATOR . "cmd.php -u=$user -t=$token -a=$actionName -r=$repositoryId";
/* Inserted next 3 lines to quote the command if in windows - rmeske*/
if (PHP_OS == "WIN32" || PHP_OS == "WINNT" || PHP_OS == "Windows") {
$cmd = ConfService::getCoreConf("CLI_PHP") . " " . chr(34) . $robustInstallPath . DIRECTORY_SEPARATOR . "cmd.php" . chr(34) . " -u=$user -t=$token -a=$actionName -r=$repositoryId";
}
if (!empty($statusFile)) {
$cmd .= " -s=" . $statusFile;
}
if (!empty($taskId)) {
$cmd .= " -k=" . $taskId;
}
foreach ($parameters as $key => $value) {
if ($key == "action" || $key == "get_action") continue;
if (is_array($value)) {
$index = 0;
foreach ($value as $v) {
$cmd .= " --file_" . $index . "=" . escapeshellarg($v);
$index++;
}
} else {
$cmd .= " --$key=" . escapeshellarg($value);
}
}

$repoObject = $ctx->getRepository();
$clearEnv = false;
if ($repoObject->getContextOption($ctx, "USE_SESSION_CREDENTIALS")) {
$encodedCreds = AJXP_Safe::getEncodedCredentialString();
if (!empty($encodedCreds)) {
putenv("AJXP_SAFE_CREDENTIALS=" . $encodedCreds);
$clearEnv = "AJXP_SAFE_CREDENTIALS";
}
}

$res = self::runCommandInBackground($cmd, $logFile);
if (!empty($clearEnv)) {
putenv($clearEnv);
}
return $res;
}

/**
* @param $cmd
* @param $logFile
* @return UnixProcess|null
*/
public static function runCommandInBackground($cmd, $logFile)
{
if (PHP_OS == "WIN32" || PHP_OS == "WINNT" || PHP_OS == "Windows") {
if (AJXP_SERVER_DEBUG) $cmd .= " > " . $logFile;
if (class_exists("COM") && ConfService::getCoreConf("CLI_USE_COM")) {
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("cmd /C $cmd", 0, false);
} else {
$basePath = str_replace("/", DIRECTORY_SEPARATOR, AJXP_INSTALL_PATH);
$tmpBat = implode(DIRECTORY_SEPARATOR, array($basePath, "data", "tmp", md5(time()) . ".bat"));
$cmd = "@chcp 1252 > nul \r\n" . $cmd;
$cmd .= "\n DEL " . chr(34) . $tmpBat . chr(34);
AJXP_Logger::debug("Writing file $cmd to $tmpBat");
file_put_contents($tmpBat, $cmd);
pclose(popen('start /b "CLI" "' . $tmpBat . '"', 'r'));
}
return null;
} else {
$process = new UnixProcess($cmd, (AJXP_SERVER_DEBUG ? $logFile : null));
AJXP_Logger::debug("Starting process and sending output dev null");
return $process;
}
}
}

0 comments on commit 4a6973f

Please sign in to comment.