Permalink
Browse files

Clean Controller, move backgroundActions methods to CliRunner class.

All hooks MUST provide at least one ContextInterface or ContextProviderInterface argument
  • Loading branch information...
1 parent 39a87aa commit 4a6973f2680d0908e153ee0bba61f6478798369d @cdujeu cdujeu committed Jun 10, 2016
Showing with 465 additions and 352 deletions.
  1. +154 −0 core/src/core/src/pydio/Core/Controller/CliRunner.php
  2. +47 −167 core/src/core/src/pydio/Core/Controller/Controller.php
  3. +32 −0 core/src/core/src/pydio/Core/Model/ContextProviderInterface.php
  4. +3 −2 core/src/core/src/pydio/Core/Services/AuthService.php
  5. +10 −7 core/src/core/src/pydio/Core/Services/UsersService.php
  6. +1 −1 core/src/plugins/access.ajxp_conf/class.ajxp_confAccessDriver.php
  7. +3 −3 core/src/plugins/access.fs/class.fsAccessDriver.php
  8. +7 −1 core/src/plugins/access.imap/class.imapAccessDriver.php
  9. +3 −2 core/src/plugins/action.scheduler/class.AjxpScheduler.php
  10. +3 −0 core/src/plugins/action.share/res/minisite.css
  11. +3 −0 core/src/plugins/action.share/res/minisite.less
  12. +4 −3 core/src/plugins/action.share/src/ShareCenter.php
  13. +2 −2 core/src/plugins/action.share/src/Store/ShareRightsManager.php
  14. +7 −4 core/src/plugins/action.share/src/Store/ShareStore.php
  15. +1 −2 core/src/plugins/action.share/src/View/MinisiteRenderer.php
  16. +14 −2 core/src/plugins/core.access/src/Model/AJXP_Node.php
  17. +1 −1 core/src/plugins/core.conf/class.AbstractConfDriver.php
  18. +4 −3 core/src/plugins/core.mq/class.MqManager.php
  19. +11 −1 core/src/plugins/core.notifications/class.AJXP_Notification.php
  20. +2 −1 core/src/plugins/core.tasks/src/TaskController.php
  21. +2 −1 core/src/plugins/core.tasks/src/TaskService.php
  22. +49 −32 core/src/plugins/editor.audio/class.AudioPreviewer.php
  23. +1 −1 core/src/plugins/feed.sql/class.AJXP_SqlFeedStore.php
  24. +0 −5 core/src/plugins/gui.ajax/ajxpclient_actions.xml
  25. +93 −107 core/src/plugins/gui.ajax/class.AJXP_ClientDriver.php
  26. +2 −1 core/src/plugins/index.lucene/class.AjxpLuceneIndexer.php
  27. +2 −1 core/src/plugins/meta.monitor_fs/class.FSMonitoringManager.php
  28. +4 −2 core/src/plugins/meta.syncable/class.ChangesTracker.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;
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 4a6973f

Please sign in to comment.