public function getTaskStatus($taskId)
 {
     $statusFile = AJXP_CACHE_DIR . "/cmd_outputs/task_" . $taskId . ".status";
     if (file_exists($statusFile)) {
         $c = explode(":", file_get_contents($statusFile));
         if ($c[0] == "RUNNING" && isset($c[1]) && is_numeric($c[1])) {
             $process = new UnixProcess();
             $process->setPid(intval($c[1]));
             $s = $process->status();
             if ($s === false) {
                 // Process was probably killed!
                 $this->setTaskStatus($taskId, "KILLED", true);
                 return array("KILLED");
             }
         }
         return $c;
     }
     return false;
 }
 public function switchAction($action, $httpVars, $fileVars)
 {
     //AJXP_Logger::logAction("DL file", $httpVars);
     $repository = ConfService::getRepository();
     if (!$repository->detectStreamWrapper(false)) {
         return false;
     }
     $plugin = AJXP_PluginsService::findPlugin("access", $repository->getAccessType());
     $streamData = $plugin->detectStreamWrapper(true);
     $dir = AJXP_Utils::decodeSecureMagic($httpVars["dir"]);
     $destStreamURL = $streamData["protocol"] . "://" . $repository->getId() . $dir . "/";
     if (isset($httpVars["file"])) {
         $parts = parse_url($httpVars["file"]);
         $getPath = $parts["path"];
         $basename = basename($getPath);
     }
     if (isset($httpVars["dlfile"])) {
         $dlFile = $streamData["protocol"] . "://" . $repository->getId() . AJXP_Utils::decodeSecureMagic($httpVars["dlfile"]);
         $realFile = file_get_contents($dlFile);
         if (empty($realFile)) {
             throw new Exception("cannot find file {$dlFile} for download");
         }
         $parts = parse_url($realFile);
         $getPath = $parts["path"];
         $basename = basename($getPath);
     }
     switch ($action) {
         case "external_download":
             if (!ConfService::currentContextIsCommandLine() && ConfService::backgroundActionsSupported()) {
                 $unixProcess = AJXP_Controller::applyActionInBackground($repository->getId(), "external_download", $httpVars);
                 if ($unixProcess !== null) {
                     @file_put_contents($destStreamURL . "." . $basename . ".pid", $unixProcess->getPid());
                 }
                 AJXP_XMLWriter::header();
                 AJXP_XMLWriter::triggerBgAction("reload_node", array(), "Triggering DL ", true, 2);
                 AJXP_XMLWriter::close();
                 session_write_close();
                 exit;
             }
             require_once AJXP_BIN_FOLDER . "/class.HttpClient.php";
             $mess = ConfService::getMessages();
             session_write_close();
             $client = new HttpClient($parts["host"]);
             $collectHeaders = array("ajxp-last-redirection" => "", "content-disposition" => "", "content-length" => "");
             $client->setHeadersOnly(true, $collectHeaders);
             $client->setMaxRedirects(8);
             $client->setDebug(false);
             $client->get($getPath);
             $pidHiddenFileName = $destStreamURL . "." . $basename . ".pid";
             if (is_file($pidHiddenFileName)) {
                 $pid = file_get_contents($pidHiddenFileName);
                 @unlink($pidHiddenFileName);
             }
             AJXP_Logger::debug("COLLECTED HEADERS", $client->collectHeaders);
             $collectHeaders = $client->collectHeaders;
             $totalSize = -1;
             if (!empty($collectHeaders["content-disposition"]) && strstr($collectHeaders["content-disposition"], "filename") !== false) {
                 $ar = explode("filename=", $collectHeaders["content-disposition"]);
                 $basename = trim(array_pop($ar));
                 $basename = str_replace("\"", "", $basename);
                 // Remove quotes
             }
             if (!empty($collectHeaders["content-length"])) {
                 $totalSize = intval($collectHeaders["content-length"]);
                 AJXP_Logger::debug("Should download {$totalSize} bytes!");
             }
             if ($totalSize != -1) {
                 $node = new AJXP_Node($destStreamURL . $basename);
                 AJXP_Controller::applyHook("node.before_create", array($node, $totalSize));
             }
             $qData = false;
             if (!empty($collectHeaders["ajxp-last-redirection"])) {
                 $newParsed = parse_url($collectHeaders["ajxp-last-redirection"]);
                 $client->host = $newParsed["host"];
                 $getPath = $newParsed["path"];
                 if (isset($newParsed["query"])) {
                     $qData = parse_url($newParsed["query"]);
                 }
             }
             $tmpFilename = $destStreamURL . $basename . ".dlpart";
             $hiddenFilename = $destStreamURL . "__" . $basename . ".ser";
             $filename = $destStreamURL . $basename;
             $dlData = array("sourceUrl" => $getPath, "totalSize" => $totalSize);
             if (isset($pid)) {
                 $dlData["pid"] = $pid;
             }
             //file_put_contents($hiddenFilename, serialize($dlData));
             $fpHid = fopen($hiddenFilename, "w");
             fputs($fpHid, serialize($dlData));
             fclose($fpHid);
             $client->redirect_count = 0;
             $client->setHeadersOnly(false);
             $destStream = fopen($tmpFilename, "w");
             if ($destStream !== false) {
                 $client->writeContentToStream($destStream);
                 $client->get($getPath, $qData);
                 fclose($destStream);
             }
             rename($tmpFilename, $filename);
             unlink($hiddenFilename);
             if (isset($dlFile) && isset($httpVars["delete_dlfile"]) && is_file($dlFile)) {
                 AJXP_Controller::applyHook("node.before_change", array(new AJXP_Node($dlFile)));
                 unlink($dlFile);
                 AJXP_Controller::applyHook("node.change", array(new AJXP_Node($dlFile), null, false));
             }
             AJXP_Controller::applyHook("node.change", array(null, new AJXP_Node($filename), false));
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::triggerBgAction("reload_node", array(), $mess["httpdownloader.8"]);
             AJXP_XMLWriter::close();
             exit;
             break;
         case "update_dl_data":
             $file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
             header("text/plain");
             if (is_file($destStreamURL . $file)) {
                 echo filesize($destStreamURL . $file);
             } else {
                 echo "stop";
             }
             exit;
             break;
         case "stop_dl":
             $newName = "__" . str_replace(".dlpart", ".ser", $basename);
             $hiddenFilename = $destStreamURL . $newName;
             $data = @unserialize(@file_get_contents($hiddenFilename));
             header("text/plain");
             AJXP_Logger::debug("Getting {$hiddenFilename}", $data);
             if (isset($data["pid"])) {
                 $process = new UnixProcess();
                 $process->setPid($data["pid"]);
                 $process->stop();
                 unlink($hiddenFilename);
                 unlink($destStreamURL . $basename);
                 echo 'stop';
             } else {
                 echo 'failed';
             }
             exit;
             break;
         default:
             break;
     }
     return true;
 }
Example #3
0
 public function getWebSocketStatus()
 {
     $wDir = $this->getPluginWorkDir(true);
     $pidFile = $wDir . DIRECTORY_SEPARATOR . "ws-pid";
     if (!file_exists($pidFile)) {
         return "OFF";
     } else {
         $pId = file_get_contents($pidFile);
         $unixProcess = new UnixProcess();
         $unixProcess->setPid($pId);
         $status = $unixProcess->status();
         if ($status) {
             return "ON";
         } else {
             return "OFF";
         }
     }
 }
 public function switchAction($action, $httpVars, $fileVars)
 {
     //$this->logInfo("DL file", $httpVars);
     $repository = ConfService::getRepository();
     if (!$repository->detectStreamWrapper(false)) {
         return false;
     }
     $plugin = AJXP_PluginsService::findPlugin("access", $repository->getAccessType());
     $streamData = $plugin->detectStreamWrapper(true);
     $dir = AJXP_Utils::decodeSecureMagic($httpVars["dir"]);
     $destStreamURL = $streamData["protocol"] . "://" . $repository->getId() . $dir . "/";
     $dlURL = null;
     if (isset($httpVars["file"])) {
         $parts = parse_url($httpVars["file"]);
         $getPath = $parts["path"];
         $basename = basename($getPath);
         $dlURL = $httpVars["file"];
     }
     if (isset($httpVars["dlfile"])) {
         $dlFile = $streamData["protocol"] . "://" . $repository->getId() . AJXP_Utils::decodeSecureMagic($httpVars["dlfile"]);
         $realFile = file_get_contents($dlFile);
         if (empty($realFile)) {
             throw new Exception("cannot find file {$dlFile} for download");
         }
         $parts = parse_url($realFile);
         $getPath = $parts["path"];
         $basename = basename($getPath);
         $dlURL = $realFile;
     }
     switch ($action) {
         case "external_download":
             if (!ConfService::currentContextIsCommandLine() && ConfService::backgroundActionsSupported()) {
                 $unixProcess = AJXP_Controller::applyActionInBackground($repository->getId(), "external_download", $httpVars);
                 if ($unixProcess !== null) {
                     @file_put_contents($destStreamURL . "." . $basename . ".pid", $unixProcess->getPid());
                 }
                 AJXP_XMLWriter::header();
                 AJXP_XMLWriter::triggerBgAction("reload_node", array(), "Triggering DL ", true, 2);
                 AJXP_XMLWriter::close();
                 session_write_close();
                 exit;
             }
             require_once AJXP_BIN_FOLDER . "/http_class/http_class.php";
             session_write_close();
             $httpClient = new http_class();
             $arguments = array();
             $httpClient->GetRequestArguments($httpVars["file"], $arguments);
             $err = $httpClient->Open($arguments);
             $collectHeaders = array("ajxp-last-redirection" => "", "content-disposition" => "", "content-length" => "");
             if (empty($err)) {
                 $err = $httpClient->SendRequest($arguments);
                 $httpClient->follow_redirect = true;
                 $pidHiddenFileName = $destStreamURL . "." . $basename . ".pid";
                 if (is_file($pidHiddenFileName)) {
                     $pid = file_get_contents($pidHiddenFileName);
                     @unlink($pidHiddenFileName);
                 }
                 if (empty($err)) {
                     $httpClient->ReadReplyHeaders($collectHeaders);
                     $totalSize = -1;
                     if (!empty($collectHeaders["content-disposition"]) && strstr($collectHeaders["content-disposition"], "filename") !== false) {
                         $ar = explode("filename=", $collectHeaders["content-disposition"]);
                         $basename = trim(array_pop($ar));
                         $basename = str_replace("\"", "", $basename);
                         // Remove quotes
                     }
                     if (!empty($collectHeaders["content-length"])) {
                         $totalSize = intval($collectHeaders["content-length"]);
                         $this->logDebug("Should download {$totalSize} bytes!");
                     }
                     if ($totalSize != -1) {
                         $node = new AJXP_Node($destStreamURL . $basename);
                         AJXP_Controller::applyHook("node.before_create", array($node, $totalSize));
                     }
                     $tmpFilename = $destStreamURL . $basename . ".dlpart";
                     $hiddenFilename = $destStreamURL . "__" . $basename . ".ser";
                     $filename = $destStreamURL . $basename;
                     $dlData = array("sourceUrl" => $getPath, "totalSize" => $totalSize);
                     if (isset($pid)) {
                         $dlData["pid"] = $pid;
                     }
                     //file_put_contents($hiddenFilename, serialize($dlData));
                     $fpHid = fopen($hiddenFilename, "w");
                     fputs($fpHid, serialize($dlData));
                     fclose($fpHid);
                     // NOW READ RESPONSE
                     $destStream = fopen($tmpFilename, "w");
                     while (true) {
                         $body = "";
                         $error = $httpClient->ReadReplyBody($body, 1000);
                         if ($error != "" || strlen($body) == 0) {
                             break;
                         }
                         fwrite($destStream, $body, strlen($body));
                     }
                     fclose($destStream);
                     rename($tmpFilename, $filename);
                     unlink($hiddenFilename);
                 }
                 $httpClient->Close();
                 if (isset($dlFile) && isset($httpVars["delete_dlfile"]) && is_file($dlFile)) {
                     AJXP_Controller::applyHook("node.before_path_change", array(new AJXP_Node($dlFile)));
                     unlink($dlFile);
                     AJXP_Controller::applyHook("node.change", array(new AJXP_Node($dlFile), null, false));
                 }
                 $mess = ConfService::getMessages();
                 AJXP_Controller::applyHook("node.change", array(null, new AJXP_Node($filename), false));
                 AJXP_XMLWriter::header();
                 AJXP_XMLWriter::triggerBgAction("reload_node", array(), $mess["httpdownloader.8"]);
                 AJXP_XMLWriter::close();
             }
             break;
         case "update_dl_data":
             $file = AJXP_Utils::decodeSecureMagic($httpVars["file"]);
             header("text/plain");
             if (is_file($destStreamURL . $file)) {
                 $node = new AJXP_Node($destStreamURL . $file);
                 if (method_exists($node->getDriver(), "filesystemFileSize")) {
                     $filesize = $node->getDriver()->filesystemFileSize($node->getUrl());
                 } else {
                     $filesize = filesize($node->getUrl());
                 }
                 echo $filesize;
             } else {
                 echo "stop";
             }
             break;
         case "stop_dl":
             $newName = "__" . str_replace(".dlpart", ".ser", $basename);
             $hiddenFilename = $destStreamURL . $newName;
             $data = @unserialize(@file_get_contents($hiddenFilename));
             header("text/plain");
             $this->logDebug("Getting {$hiddenFilename}", $data);
             if (isset($data["pid"])) {
                 $process = new UnixProcess();
                 $process->setPid($data["pid"]);
                 $process->stop();
                 unlink($hiddenFilename);
                 unlink($destStreamURL . $basename);
                 echo 'stop';
             } else {
                 echo 'failed';
             }
             break;
         default:
             break;
     }
     return false;
 }