예제 #1
0
 public function applyAction($actionName, $httpVars, $fileVars)
 {
     $messages = ConfService::getMessages();
     if ($actionName == "index") {
         $repository = ConfService::getRepository();
         $repositoryId = $repository->getId();
         $userSelection = new UserSelection($repository, $httpVars);
         if ($userSelection->isEmpty()) {
             $userSelection->addFile("/");
         }
         $nodes = $userSelection->buildNodes($repository->driverInstance);
         if (isset($httpVars["verbose"]) && $httpVars["verbose"] == "true") {
             $this->verboseIndexation = true;
         }
         if (ConfService::backgroundActionsSupported() && !ConfService::currentContextIsCommandLine()) {
             AJXP_Controller::applyActionInBackground($repositoryId, "index", $httpVars);
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::triggerBgAction("check_index_status", array("repository_id" => $repositoryId), sprintf($messages["core.index.8"], $nodes[0]->getPath()), true, 2);
             if (!isset($httpVars["inner_apply"])) {
                 AJXP_XMLWriter::close();
             }
             return null;
         }
         // GIVE BACK THE HAND TO USER
         session_write_close();
         foreach ($nodes as $node) {
             $dir = $node->getPath() == "/" || is_dir($node->getUrl());
             // SIMPLE FILE
             if (!$dir) {
                 try {
                     $this->logDebug("Indexing - node.index " . $node->getUrl());
                     AJXP_Controller::applyHook("node.index", array($node));
                 } catch (Exception $e) {
                     $this->logDebug("Error Indexing Node " . $node->getUrl() . " (" . $e->getMessage() . ")");
                 }
             } else {
                 try {
                     $this->recursiveIndexation($node);
                 } catch (Exception $e) {
                     $this->logDebug("Indexation of " . $node->getUrl() . " interrupted by error: (" . $e->getMessage() . ")");
                 }
             }
         }
     } else {
         if ($actionName == "check_index_status") {
             $repoId = $httpVars["repository_id"];
             list($status, $message) = $this->getIndexStatus(ConfService::getRepositoryById($repoId), AuthService::getLoggedUser());
             if (!empty($status)) {
                 AJXP_XMLWriter::header();
                 AJXP_XMLWriter::triggerBgAction("check_index_status", array("repository_id" => $repoId), $message, true, 3);
                 AJXP_XMLWriter::close();
             } else {
                 AJXP_XMLWriter::header();
                 AJXP_XMLWriter::triggerBgAction("info_message", array(), $messages["core.index.5"], true, 5);
                 AJXP_XMLWriter::close();
             }
         }
     }
     return null;
 }
예제 #2
0
 public function performChecks()
 {
     if (!ConfService::backgroundActionsSupported()) {
         throw new Exception("The command line must be supported. See 'Pydio Core Options'.");
     }
     if (!is_dir(dirname($this->getDbFile()))) {
         throw new Exception("Could not create the db folder!");
     }
 }
 public function switchActions($actionName, $httpVars, $fileVars)
 {
     if ($actionName != "changes" || !isset($httpVars["seq_id"])) {
         return false;
     }
     if (!dibi::isConnected()) {
         dibi::connect($this->sqlDriver);
     }
     $filter = null;
     $masks = array();
     $currentRepo = $this->accessDriver->repository;
     AJXP_Controller::applyHook("role.masks", array($currentRepo->getId(), &$masks, AJXP_Permission::READ));
     $recycle = $currentRepo->getOption("RECYCLE_BIN");
     $recycle = !empty($recycle) ? $recycle : false;
     if ($this->options["OBSERVE_STORAGE_CHANGES"] === true) {
         // Do it every XX minutes
         $minutes = 5;
         if (isset($this->options["OBSERVE_STORAGE_EVERY"])) {
             $minutes = intval($this->options["OBSERVE_STORAGE_EVERY"]);
         }
         $file = $this->getResyncTimestampFile();
         $last = 0;
         if (is_file($file)) {
             $last = intval(file_get_contents($file));
         }
         if (time() - $last > $minutes * 60) {
             $this->resyncAction("resync_storage", array(), array());
         }
     }
     if ($this->options["REQUIRES_INDEXATION"]) {
         if (ConfService::backgroundActionsSupported()) {
             AJXP_Controller::applyActionInBackground(ConfService::getRepository()->getId(), "index", array());
         } else {
             AJXP_Controller::findActionAndApply("index", array(), array());
         }
         // Unset the REQUIRES_INDEXATION FLAG
         $meta = $currentRepo->getOption("META_SOURCES");
         unset($meta["meta.syncable"]["REQUIRES_INDEXATION"]);
         $currentRepo->addOption("META_SOURCES", $meta);
         ConfService::replaceRepository($currentRepo->getId(), $currentRepo);
     }
     HTMLWriter::charsetHeader('application/json', 'UTF-8');
     $stream = isset($httpVars["stream"]);
     $separator = $stream ? "\n" : ",";
     $veryLastSeq = intval(dibi::query("SELECT MAX([seq]) FROM [ajxp_changes]")->fetchSingle());
     $seqId = intval(AJXP_Utils::sanitize($httpVars["seq_id"], AJXP_SANITIZE_ALPHANUM));
     if ($veryLastSeq > 0 && $seqId > $veryLastSeq) {
         // This is not normal! Send a signal reload all changes from start.
         if (!$stream) {
             echo json_encode(array('changes' => array(), 'last_seq' => 1));
         } else {
             echo 'LAST_SEQ:1';
         }
         return null;
     }
     $ands = array();
     $ands[] = array("[ajxp_changes].[repository_identifier] = %s", $this->computeIdentifier($currentRepo));
     $ands[] = array("[seq] > %i", $seqId);
     if (isset($httpVars["filter"])) {
         $filter = AJXP_Utils::decodeSecureMagic($httpVars["filter"]);
         $filterLike = rtrim($filter, "/") . "/";
         $ands[] = array("[source] LIKE %like~ OR [target] LIKE %like~", $filterLike, $filterLike);
     }
     if (count($masks)) {
         $ors = array();
         foreach ($masks as $mask) {
             $filterLike = rtrim($mask, "/") . "/";
             $ors[] = array("[source] LIKE %like~ OR [target] LIKE %like~", $filterLike, $filterLike);
         }
         if (count($ors)) {
             $ands[] = array("%or", $ors);
         }
     }
     $res = dibi::query("SELECT\n            [seq] , [ajxp_changes].[repository_identifier] , [ajxp_changes].[node_id] , [type] , [source] ,  [target] , [ajxp_index].[bytesize], [ajxp_index].[md5], [ajxp_index].[mtime], [ajxp_index].[node_path]\n            FROM [ajxp_changes]\n            LEFT JOIN [ajxp_index]\n                ON [ajxp_changes].[node_id] = [ajxp_index].[node_id]\n            WHERE %and\n            ORDER BY [ajxp_changes].[node_id], [seq] ASC", $ands);
     if (!$stream) {
         echo '{"changes":[';
     }
     $previousNodeId = -1;
     $previousRow = null;
     $order = array("path" => 0, "content" => 1, "create" => 2, "delete" => 3);
     $relocateAttrs = array("bytesize", "md5", "mtime", "node_path", "repository_identifier");
     $valuesSent = false;
     foreach ($res as $row) {
         $row->node = array();
         foreach ($relocateAttrs as $att) {
             $row->node[$att] = $row->{$att};
             unset($row->{$att});
         }
         if (!empty($recycle)) {
             $this->cancelRecycleNodes($row, $recycle);
         }
         if (!isset($httpVars["flatten"]) || $httpVars["flatten"] == "false") {
             if (!$this->filterMasks($row, $masks) && !$this->filterRow($row, $filter)) {
                 if ($valuesSent) {
                     echo $separator;
                 }
                 echo json_encode($row);
                 $valuesSent = true;
             }
         } else {
             if ($row->node_id == $previousNodeId) {
                 $previousRow->target = $row->target;
                 $previousRow->seq = $row->seq;
                 if ($order[$row->type] > $order[$previousRow->type]) {
                     $previousRow->type = $row->type;
                 }
             } else {
                 if (isset($previousRow) && ($previousRow->source != $previousRow->target || $previousRow->type == "content")) {
                     if ($this->filterMasks($previousRow, $masks) || $this->filterRow($previousRow, $filter)) {
                         $previousRow = $row;
                         $previousNodeId = $row->node_id;
                         $lastSeq = $row->seq;
                         continue;
                     }
                     if ($valuesSent) {
                         echo $separator;
                     }
                     echo json_encode($previousRow);
                     $valuesSent = true;
                 }
                 $previousRow = $row;
                 $previousNodeId = $row->node_id;
             }
             $lastSeq = $row->seq;
             flush();
         }
         //CODES HERE HAVE BEEN MOVE OUT OF THE LOOP
     }
     /**********RETURN TO SENDER************/
     // is 'not NULL' included in isSet()?
     if ($previousRow && isset($previousRow) && ($previousRow->source != $previousRow->target || $previousRow->type == "content") && !$this->filterRow($previousRow, $filter)) {
         if ($valuesSent) {
             echo $separator;
         }
         echo json_encode($previousRow);
         if ($previousRow->seq > $lastSeq) {
             $lastSeq = $previousRow->seq;
         }
         $valuesSent = true;
     }
     /*************************************/
     if (isset($lastSeq)) {
         if ($stream) {
             echo "\nLAST_SEQ:" . $lastSeq;
         } else {
             echo '], "last_seq":' . $lastSeq . '}';
         }
     } else {
         $lastSeq = dibi::query("SELECT MAX([seq]) FROM [ajxp_changes]")->fetchSingle();
         if (empty($lastSeq)) {
             $lastSeq = 1;
         }
         if ($stream) {
             echo "\nLAST_SEQ:" . $lastSeq;
         } else {
             echo '], "last_seq":' . $lastSeq . '}';
         }
     }
     return null;
 }
 /**
  * @param String $action
  * @param Array $httpVars
  * @param Array $fileVars
  * @throws Exception
  */
 public function receiveAction($action, $httpVars, $fileVars)
 {
     //VAR CREATION OUTSIDE OF ALL CONDITIONS, THEY ARE "MUST HAVE" VAR !!
     $messages = ConfService::getMessages();
     $repository = ConfService::getRepository();
     $userSelection = new UserSelection($repository, $httpVars);
     $nodes = $userSelection->buildNodes();
     $currentDirPath = AJXP_Utils::safeDirname($userSelection->getUniqueNode()->getPath());
     $currentDirPath = rtrim($currentDirPath, "/") . "/";
     $currentDirUrl = $userSelection->currentBaseUrl() . $currentDirPath;
     if (empty($httpVars["compression_id"])) {
         $compressionId = sha1(rand());
         $httpVars["compression_id"] = $compressionId;
     } else {
         $compressionId = $httpVars["compression_id"];
     }
     $progressCompressionFileName = $this->getPluginCacheDir(false, true) . DIRECTORY_SEPARATOR . "progressCompressionID-" . $compressionId . ".txt";
     if (empty($httpVars["extraction_id"])) {
         $extractId = sha1(rand());
         $httpVars["extraction_id"] = $extractId;
     } else {
         $extractId = $httpVars["extraction_id"];
     }
     $progressExtractFileName = $this->getPluginCacheDir(false, true) . DIRECTORY_SEPARATOR . "progressExtractID-" . $extractId . ".txt";
     if ($action == "compression") {
         $archiveName = AJXP_Utils::sanitize(AJXP_Utils::decodeSecureMagic($httpVars["archive_name"]), AJXP_SANITIZE_FILENAME);
         $archiveFormat = $httpVars["type_archive"];
         $tabTypeArchive = array(".tar", ".tar.gz", ".tar.bz2");
         $acceptedExtension = false;
         foreach ($tabTypeArchive as $extensionArchive) {
             if ($extensionArchive == $archiveFormat) {
                 $acceptedExtension = true;
                 break;
             }
         }
         if ($acceptedExtension == false) {
             file_put_contents($progressCompressionFileName, "Error : " . $messages["compression.16"]);
             throw new AJXP_Exception($messages["compression.16"]);
         }
         $typeArchive = $httpVars["type_archive"];
         //if we can run in background we do it
         if (ConfService::backgroundActionsSupported() && !ConfService::currentContextIsCommandLine()) {
             $archivePath = $currentDirPath . $archiveName;
             file_put_contents($progressCompressionFileName, $messages["compression.5"]);
             AJXP_Controller::applyActionInBackground($repository->getId(), "compression", $httpVars);
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::triggerBgAction("check_compression_status", array("repository_id" => $repository->getId(), "compression_id" => $compressionId, "archive_path" => SystemTextEncoding::toUTF8($archivePath)), $messages["compression.5"], true, 2);
             AJXP_XMLWriter::close();
             return null;
         } else {
             $maxAuthorizedSize = 4294967296;
             $currentDirUrlLength = strlen($currentDirUrl);
             $tabFolders = array();
             $tabAllRecursiveFiles = array();
             $tabFilesNames = array();
             foreach ($nodes as $node) {
                 $nodeUrl = $node->getUrl();
                 if (is_file($nodeUrl) && filesize($nodeUrl) < $maxAuthorizedSize) {
                     array_push($tabAllRecursiveFiles, $nodeUrl);
                     array_push($tabFilesNames, substr($nodeUrl, $currentDirUrlLength));
                 }
                 if (is_dir($nodeUrl)) {
                     array_push($tabFolders, $nodeUrl);
                 }
             }
             //DO A FOREACH OR IT'S GONNA HAVE SOME SAMES FILES NAMES
             foreach ($tabFolders as $value) {
                 $dossiers = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($value));
                 foreach ($dossiers as $file) {
                     if ($file->isDir()) {
                         continue;
                     }
                     array_push($tabAllRecursiveFiles, $file->getPathname());
                     array_push($tabFilesNames, substr($file->getPathname(), $currentDirUrlLength));
                 }
             }
             //WE STOP IF IT'S JUST AN EMPTY FOLDER OR NO FILES
             if (empty($tabFilesNames)) {
                 file_put_contents($progressCompressionFileName, "Error : " . $messages["compression.17"]);
                 throw new AJXP_Exception($messages["compression.17"]);
             }
             try {
                 $tmpArchiveName = tempnam(AJXP_Utils::getAjxpTmpDir(), "tar-compression") . ".tar";
                 $archive = new PharData($tmpArchiveName);
             } catch (Exception $e) {
                 file_put_contents($progressCompressionFileName, "Error : " . $e->getMessage());
                 throw $e;
             }
             $counterCompression = 0;
             //THE TWO ARRAY ARE MERGED FOR THE FOREACH LOOP
             $tabAllFiles = array_combine($tabAllRecursiveFiles, $tabFilesNames);
             foreach ($tabAllFiles as $fullPath => $fileName) {
                 try {
                     $archive->addFile(AJXP_MetaStreamWrapper::getRealFSReference($fullPath), $fileName);
                     $counterCompression++;
                     file_put_contents($progressCompressionFileName, sprintf($messages["compression.6"], round($counterCompression / count($tabAllFiles) * 100, 0, PHP_ROUND_HALF_DOWN) . " %"));
                 } catch (Exception $e) {
                     unlink($tmpArchiveName);
                     file_put_contents($progressCompressionFileName, "Error : " . $e->getMessage());
                     throw $e;
                 }
             }
             $finalArchive = $tmpArchiveName;
             if ($typeArchive != ".tar") {
                 $archiveTypeCompress = substr(strrchr($typeArchive, "."), 1);
                 file_put_contents($progressCompressionFileName, sprintf($messages["compression.7"], strtoupper($archiveTypeCompress)));
                 if ($archiveTypeCompress == "gz") {
                     $archive->compress(Phar::GZ);
                 } elseif ($archiveTypeCompress == "bz2") {
                     $archive->compress(Phar::BZ2);
                 }
                 $finalArchive = $tmpArchiveName . "." . $archiveTypeCompress;
             }
             $destArchive = AJXP_MetaStreamWrapper::getRealFSReference($currentDirUrl . $archiveName);
             rename($finalArchive, $destArchive);
             AJXP_Controller::applyHook("node.before_create", array($destArchive, filesize($destArchive)));
             if (file_exists($tmpArchiveName)) {
                 unlink($tmpArchiveName);
                 unlink(substr($tmpArchiveName, 0, -4));
             }
             $newNode = new AJXP_Node($currentDirUrl . $archiveName);
             AJXP_Controller::applyHook("node.change", array(null, $newNode, false));
             file_put_contents($progressCompressionFileName, "SUCCESS");
         }
     } elseif ($action == "check_compression_status") {
         $archivePath = AJXP_Utils::decodeSecureMagic($httpVars["archive_path"]);
         $progressCompression = file_get_contents($progressCompressionFileName);
         $substrProgressCompression = substr($progressCompression, 0, 5);
         if ($progressCompression != "SUCCESS" && $substrProgressCompression != "Error") {
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::triggerBgAction("check_compression_status", array("repository_id" => $repository->getId(), "compression_id" => $compressionId, "archive_path" => SystemTextEncoding::toUTF8($archivePath)), $progressCompression, true, 5);
             AJXP_XMLWriter::close();
         } elseif ($progressCompression == "SUCCESS") {
             $newNode = new AJXP_Node($userSelection->currentBaseUrl() . $archivePath);
             $nodesDiffs = array("ADD" => array($newNode), "REMOVE" => array(), "UPDATE" => array());
             AJXP_Controller::applyHook("node.change", array(null, $newNode, false));
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::sendMessage($messages["compression.8"], null);
             AJXP_XMLWriter::writeNodesDiff($nodesDiffs, true);
             AJXP_XMLWriter::close();
             if (file_exists($progressCompressionFileName)) {
                 unlink($progressCompressionFileName);
             }
         } elseif ($substrProgressCompression == "Error") {
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::sendMessage(null, $progressCompression);
             AJXP_XMLWriter::close();
             if (file_exists($progressCompressionFileName)) {
                 unlink($progressCompressionFileName);
             }
         }
     } elseif ($action == "extraction") {
         $fileArchive = AJXP_Utils::sanitize(AJXP_Utils::decodeSecureMagic($httpVars["file"]), AJXP_SANITIZE_DIRNAME);
         $fileArchive = substr(strrchr($fileArchive, DIRECTORY_SEPARATOR), 1);
         $authorizedExtension = array("tar" => 4, "gz" => 7, "bz2" => 8);
         $acceptedArchive = false;
         $extensionLength = 0;
         $counterExtract = 0;
         $currentAllPydioPath = $currentDirUrl . $fileArchive;
         $pharCurrentAllPydioPath = "phar://" . AJXP_MetaStreamWrapper::getRealFSReference($currentAllPydioPath);
         $pathInfoCurrentAllPydioPath = pathinfo($currentAllPydioPath, PATHINFO_EXTENSION);
         //WE TAKE ONLY TAR, TAR.GZ AND TAR.BZ2 ARCHIVES
         foreach ($authorizedExtension as $extension => $strlenExtension) {
             if ($pathInfoCurrentAllPydioPath == $extension) {
                 $acceptedArchive = true;
                 $extensionLength = $strlenExtension;
                 break;
             }
         }
         if ($acceptedArchive == false) {
             file_put_contents($progressExtractFileName, "Error : " . $messages["compression.15"]);
             throw new AJXP_Exception($messages["compression.15"]);
         }
         $onlyFileName = substr($fileArchive, 0, -$extensionLength);
         $lastPosOnlyFileName = strrpos($onlyFileName, "-");
         $tmpOnlyFileName = substr($onlyFileName, 0, $lastPosOnlyFileName);
         $counterDuplicate = substr($onlyFileName, $lastPosOnlyFileName + 1);
         if (!is_int($lastPosOnlyFileName) || !is_int($counterDuplicate)) {
             $tmpOnlyFileName = $onlyFileName;
             $counterDuplicate = 1;
         }
         while (file_exists($currentDirUrl . $onlyFileName)) {
             $onlyFileName = $tmpOnlyFileName . "-" . $counterDuplicate;
             $counterDuplicate++;
         }
         if (ConfService::backgroundActionsSupported() && !ConfService::currentContextIsCommandLine()) {
             file_put_contents($progressExtractFileName, $messages["compression.12"]);
             AJXP_Controller::applyActionInBackground($repository->getId(), "extraction", $httpVars);
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::triggerBgAction("check_extraction_status", array("repository_id" => $repository->getId(), "extraction_id" => $extractId, "currentDirUrl" => $currentDirUrl, "onlyFileName" => $onlyFileName), $messages["compression.12"], true, 2);
             AJXP_XMLWriter::close();
             return null;
         }
         mkdir($currentDirUrl . $onlyFileName, 0777, true);
         chmod(AJXP_MetaStreamWrapper::getRealFSReference($currentDirUrl . $onlyFileName), 0777);
         try {
             $archive = new PharData(AJXP_MetaStreamWrapper::getRealFSReference($currentAllPydioPath));
             $fichiersArchive = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pharCurrentAllPydioPath));
             foreach ($fichiersArchive as $file) {
                 $fileGetPathName = $file->getPathname();
                 if ($file->isDir()) {
                     continue;
                 }
                 $fileNameInArchive = substr(strstr($fileGetPathName, $fileArchive), strlen($fileArchive) + 1);
                 try {
                     $archive->extractTo(AJXP_MetaStreamWrapper::getRealFSReference($currentDirUrl . $onlyFileName), $fileNameInArchive, false);
                 } catch (Exception $e) {
                     file_put_contents($progressExtractFileName, "Error : " . $e->getMessage());
                     throw new AJXP_Exception($e);
                 }
                 $counterExtract++;
                 file_put_contents($progressExtractFileName, sprintf($messages["compression.13"], round($counterExtract / $archive->count() * 100, 0, PHP_ROUND_HALF_DOWN) . " %"));
             }
         } catch (Exception $e) {
             file_put_contents($progressExtractFileName, "Error : " . $e->getMessage());
             throw new AJXP_Exception($e);
         }
         file_put_contents($progressExtractFileName, "SUCCESS");
         $newNode = new AJXP_Node($currentDirUrl . $onlyFileName);
         AJXP_Controller::findActionAndApply("index", array("file" => $newNode->getPath()), array());
     } elseif ($action == "check_extraction_status") {
         $currentDirUrl = $httpVars["currentDirUrl"];
         $onlyFileName = $httpVars["onlyFileName"];
         $progressExtract = file_get_contents($progressExtractFileName);
         $substrProgressExtract = substr($progressExtract, 0, 5);
         if ($progressExtract != "SUCCESS" && $progressExtract != "INDEX" && $substrProgressExtract != "Error") {
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::triggerBgAction("check_extraction_status", array("repository_id" => $repository->getId(), "extraction_id" => $extractId, "currentDirUrl" => $currentDirUrl, "onlyFileName" => $onlyFileName), $progressExtract, true, 4);
             AJXP_XMLWriter::close();
         } elseif ($progressExtract == "SUCCESS") {
             $newNode = new AJXP_Node($currentDirUrl . $onlyFileName);
             $nodesDiffs = array("ADD" => array($newNode), "REMOVE" => array(), "UPDATE" => array());
             AJXP_Controller::applyHook("node.change", array(null, $newNode, false));
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::sendMessage(sprintf($messages["compression.14"], $onlyFileName), null);
             AJXP_XMLWriter::triggerBgAction("check_index_status", array("repository_id" => $newNode->getRepositoryId()), "starting indexation", true, 5);
             AJXP_XMLWriter::writeNodesDiff($nodesDiffs, true);
             AJXP_XMLWriter::close();
             if (file_exists($progressExtractFileName)) {
                 unlink($progressExtractFileName);
             }
         } elseif ($substrProgressExtract == "Error") {
             AJXP_XMLWriter::header();
             AJXP_XMLWriter::sendMessage(null, $progressExtract);
             AJXP_XMLWriter::close();
             if (file_exists($progressExtractFileName)) {
                 unlink($progressExtractFileName);
             }
         }
     }
 }
 public function switchAction($action, $httpVars, $fileVars)
 {
     $selection = new UserSelection();
     $dir = $httpVars["dir"] or "";
     $dir = AJXP_Utils::decodeSecureMagic($dir);
     if ($dir == "/") {
         $dir = "";
     }
     $selection->initFromHttpVars($httpVars);
     if (!$selection->isEmpty()) {
         //$this->filterUserSelectionToHidden($selection->getFiles());
     }
     $urlBase = "pydio://" . ConfService::getRepository()->getId();
     $mess = ConfService::getMessages();
     switch ($action) {
         case "monitor_compression":
             $percentFile = fsAccessWrapper::getRealFSReference($urlBase . $dir . "/.zip_operation_" . $httpVars["ope_id"]);
             $percent = 0;
             if (is_file($percentFile)) {
                 $percent = intval(file_get_contents($percentFile));
             }
             if ($percent < 100) {
                 AJXP_XMLWriter::header();
                 AJXP_XMLWriter::triggerBgAction("monitor_compression", $httpVars, $mess["powerfs.1"] . " ({$percent}%)", true, 1);
                 AJXP_XMLWriter::close();
             } else {
                 @unlink($percentFile);
                 AJXP_XMLWriter::header();
                 if ($httpVars["on_end"] == "reload") {
                     AJXP_XMLWriter::triggerBgAction("reload_node", array(), "powerfs.2", true, 2);
                 } else {
                     $archiveName = AJXP_Utils::sanitize($httpVars["archive_name"], AJXP_SANITIZE_FILENAME);
                     $archiveName = str_replace("'", "\\'", $archiveName);
                     $jsCode = "\n                            PydioApi.getClient().downloadSelection(null, \$('download_form'), 'postcompress_download', {ope_id:'" . $httpVars["ope_id"] . "',archive_name:'" . $archiveName . "'});\n                        ";
                     AJXP_XMLWriter::triggerBgJsAction($jsCode, $mess["powerfs.3"], true);
                     AJXP_XMLWriter::triggerBgAction("reload_node", array(), "powerfs.2", true, 2);
                 }
                 AJXP_XMLWriter::close();
             }
             break;
         case "postcompress_download":
             $archive = AJXP_Utils::getAjxpTmpDir() . DIRECTORY_SEPARATOR . $httpVars["ope_id"] . "_" . AJXP_Utils::sanitize(AJXP_Utils::decodeSecureMagic($httpVars["archive_name"]), AJXP_SANITIZE_FILENAME);
             $fsDriver = AJXP_PluginsService::getInstance()->getUniqueActivePluginForType("access");
             if (is_file($archive)) {
                 if (!$fsDriver->getFilteredOption("USE_XSENDFILE", ConfService::getRepository()) && !$fsDriver->getFilteredOption("USE_XACCELREDIRECT", ConfService::getRepository())) {
                     register_shutdown_function("unlink", $archive);
                 }
                 $fsDriver->readFile($archive, "force-download", $httpVars["archive_name"], false, null, true);
             } else {
                 echo "<script>alert('Cannot find archive! Is ZIP correctly installed?');</script>";
             }
             break;
         case "compress":
         case "precompress":
             $archiveName = AJXP_Utils::sanitize(AJXP_Utils::decodeSecureMagic($httpVars["archive_name"]), AJXP_SANITIZE_FILENAME);
             if (!ConfService::currentContextIsCommandLine() && ConfService::backgroundActionsSupported()) {
                 $opeId = substr(md5(time()), 0, 10);
                 $httpVars["ope_id"] = $opeId;
                 AJXP_Controller::applyActionInBackground(ConfService::getRepository()->getId(), $action, $httpVars);
                 AJXP_XMLWriter::header();
                 $bgParameters = array("dir" => SystemTextEncoding::toUTF8($dir), "archive_name" => SystemTextEncoding::toUTF8($archiveName), "on_end" => isset($httpVars["on_end"]) ? $httpVars["on_end"] : "reload", "ope_id" => $opeId);
                 AJXP_XMLWriter::triggerBgAction("monitor_compression", $bgParameters, $mess["powerfs.1"] . " (0%)", true);
                 AJXP_XMLWriter::close();
                 session_write_close();
                 exit;
             }
             $rootDir = fsAccessWrapper::getRealFSReference($urlBase) . $dir;
             $percentFile = $rootDir . "/.zip_operation_" . $httpVars["ope_id"];
             $compressLocally = $action == "compress" ? true : false;
             // List all files
             $todo = array();
             $args = array();
             $replaceSearch = array($rootDir, "\\");
             $replaceReplace = array("", "/");
             foreach ($selection->getFiles() as $selectionFile) {
                 $baseFile = $selectionFile;
                 $args[] = escapeshellarg(substr($selectionFile, strlen($dir) + ($dir == "/" ? 0 : 1)));
                 $selectionFile = fsAccessWrapper::getRealFSReference($urlBase . $selectionFile);
                 $todo[] = ltrim(str_replace($replaceSearch, $replaceReplace, $selectionFile), "/");
                 if (is_dir($selectionFile)) {
                     $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($selectionFile), RecursiveIteratorIterator::SELF_FIRST);
                     foreach ($objects as $name => $object) {
                         $todo[] = str_replace($replaceSearch, $replaceReplace, $name);
                     }
                 }
                 if (trim($baseFile, "/") == "") {
                     // ROOT IS SELECTED, FIX IT
                     $args = array(escapeshellarg(basename($rootDir)));
                     $rootDir = dirname($rootDir);
                     break;
                 }
             }
             $cmdSeparator = PHP_OS == "WIN32" || PHP_OS == "WINNT" || PHP_OS == "Windows" ? "&" : ";";
             if (!$compressLocally) {
                 $archiveName = AJXP_Utils::getAjxpTmpDir() . DIRECTORY_SEPARATOR . $httpVars["ope_id"] . "_" . $archiveName;
             }
             chdir($rootDir);
             $cmd = $this->getFilteredOption("ZIP_PATH") . " -r " . escapeshellarg($archiveName) . " " . implode(" ", $args);
             $fsDriver = AJXP_PluginsService::getInstance()->getUniqueActivePluginForType("access");
             $c = $fsDriver->getConfigs();
             if ((!isset($c["SHOW_HIDDEN_FILES"]) || $c["SHOW_HIDDEN_FILES"] == false) && stripos(PHP_OS, "win") === false) {
                 $cmd .= " -x .\\*";
             }
             $cmd .= " " . $cmdSeparator . " echo ZIP_FINISHED";
             $proc = popen($cmd, "r");
             $toks = array();
             $handled = array();
             $finishedEchoed = false;
             while (!feof($proc)) {
                 set_time_limit(20);
                 $results = fgets($proc, 256);
                 if (strlen($results) == 0) {
                 } else {
                     $tok = strtok($results, "\n");
                     while ($tok !== false) {
                         $toks[] = $tok;
                         if ($tok == "ZIP_FINISHED") {
                             $finishedEchoed = true;
                         } else {
                             $test = preg_match('/(\\w+): (.*) \\(([^\\(]+)\\) \\(([^\\(]+)\\)/', $tok, $matches);
                             if ($test !== false) {
                                 $handled[] = $matches[2];
                             }
                         }
                         $tok = strtok("\n");
                     }
                     if ($finishedEchoed) {
                         $percent = 100;
                     } else {
                         $percent = min(round(count($handled) / count($todo) * 100), 100);
                     }
                     file_put_contents($percentFile, $percent);
                 }
                 // avoid a busy wait
                 if ($percent < 100) {
                     usleep(1);
                 }
             }
             pclose($proc);
             file_put_contents($percentFile, 100);
             break;
         default:
             break;
     }
 }
 function switchAction($action, $httpVars, $fileVars)
 {
     if (!isset($this->actions[$action])) {
         return;
     }
     $selection = new UserSelection();
     $dir = $httpVars["dir"] or "";
     $dir = AJXP_Utils::securePath($dir);
     if ($dir == "/") {
         $dir = "";
     }
     $selection->initFromHttpVars($httpVars);
     if (!$selection->isEmpty()) {
         //$this->filterUserSelectionToHidden($selection->getFiles());
     }
     $urlBase = "ajxp.fs://" . ConfService::getRepository()->getId();
     $mess = ConfService::getMessages();
     switch ($action) {
         case "monitor_compression":
             $percentFile = fsAccessWrapper::getRealFSReference($urlBase . $dir . "/.zip_operation_" . $httpVars["ope_id"]);
             $percent = 0;
             if (is_file($percentFile)) {
                 $percent = intval(file_get_contents($percentFile));
             }
             if ($percent < 100) {
                 AJXP_XMLWriter::header();
                 AJXP_XMLWriter::triggerBgAction("monitor_compression", $httpVars, $mess["powerfs.1"] . " ({$percent}%)", true, 1);
                 AJXP_XMLWriter::close();
             } else {
                 @unlink($percentFile);
                 AJXP_XMLWriter::header();
                 if ($httpVars["on_end"] == "reload") {
                     AJXP_XMLWriter::triggerBgAction("reload_node", array(), "powerfs.2", true, 2);
                 } else {
                     $archiveName = $httpVars["archive_name"];
                     $jsCode = "\n                            \$('download_form').action = window.ajxpServerAccessPath;\n                            \$('download_form').secure_token.value = window.Connexion.SECURE_TOKEN;\n                            \$('download_form').select('input').each(function(input){\n                                if(input.name!='get_action' && input.name!='secure_token') input.remove();\n                            });\n                            \$('download_form').insert(new Element('input', {type:'hidden', name:'ope_id', value:'" . $httpVars["ope_id"] . "'}));\n                            \$('download_form').insert(new Element('input', {type:'hidden', name:'archive_name', value:'" . $archiveName . "'}));\n                            \$('download_form').insert(new Element('input', {type:'hidden', name:'get_action', value:'postcompress_download'}));\n                            \$('download_form').submit();\n                        ";
                     AJXP_XMLWriter::triggerBgJsAction($jsCode, "powerfs.3", true);
                     AJXP_XMLWriter::triggerBgAction("reload_node", array(), "powerfs.2", true, 2);
                 }
                 AJXP_XMLWriter::close();
             }
             break;
         case "postcompress_download":
             $archive = AJXP_Utils::getAjxpTmpDir() . "/" . $httpVars["ope_id"] . "_" . $httpVars["archive_name"];
             //$fsDriver = new fsAccessDriver("fake", "");
             $fsDriver = AJXP_PluginsService::getInstance()->getUniqueActivePluginForType("access");
             $fsDriver->readFile($archive, "force-download", $httpVars["archive_name"], false, null, true);
             break;
         case "compress":
         case "precompress":
             if (!ConfService::currentContextIsCommandLine() && ConfService::backgroundActionsSupported()) {
                 $opeId = substr(md5(time()), 0, 10);
                 $httpVars["ope_id"] = $opeId;
                 AJXP_Controller::applyActionInBackground(ConfService::getRepository()->getId(), $action, $httpVars);
                 AJXP_XMLWriter::header();
                 $bgParameters = array("dir" => $dir, "archive_name" => $httpVars["archive_name"], "on_end" => isset($httpVars["on_end"]) ? $httpVars["on_end"] : "reload", "ope_id" => $opeId);
                 AJXP_XMLWriter::triggerBgAction("monitor_compression", $bgParameters, $mess["powerfs.1"] . " (0%)", true);
                 AJXP_XMLWriter::close();
                 session_write_close();
                 exit;
             }
             $rootDir = fsAccessWrapper::getRealFSReference($urlBase) . $dir;
             $percentFile = $rootDir . "/.zip_operation_" . $httpVars["ope_id"];
             $compressLocally = $action == "compress" ? true : false;
             // List all files
             $todo = array();
             $args = array();
             $replaceSearch = array($rootDir, "\\");
             $replaceReplace = array("", "/");
             foreach ($selection->getFiles() as $selectionFile) {
                 $args[] = '"' . substr($selectionFile, strlen($dir) + ($dir == "/" ? 0 : 1)) . '"';
                 $selectionFile = fsAccessWrapper::getRealFSReference($urlBase . $selectionFile);
                 $todo[] = ltrim(str_replace($replaceSearch, $replaceReplace, $selectionFile), "/");
                 if (is_dir($selectionFile)) {
                     $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($selectionFile), RecursiveIteratorIterator::SELF_FIRST);
                     foreach ($objects as $name => $object) {
                         $todo[] = str_replace($replaceSearch, $replaceReplace, $name);
                     }
                 }
             }
             $cmdSeparator = PHP_OS == "WIN32" || PHP_OS == "WINNT" || PHP_OS == "Windows" ? "&" : ";";
             $archiveName = $httpVars["archive_name"];
             if (!$compressLocally) {
                 $archiveName = AJXP_Utils::getAjxpTmpDir() . "/" . $httpVars["ope_id"] . "_" . $archiveName;
             }
             chdir($rootDir);
             $cmd = "zip -r \"" . $archiveName . "\" " . implode(" ", $args);
             $fsDriver = AJXP_PluginsService::getInstance()->getUniqueActivePluginForType("access");
             $c = $fsDriver->getConfigs();
             if (!isset($c["SHOW_HIDDEN_FILES"]) || $c["SHOW_HIDDEN_FILES"] == false) {
                 $cmd .= " -x .\\*";
             }
             $cmd .= " " . $cmdSeparator . " echo ZIP_FINISHED";
             $proc = popen($cmd, "r");
             $toks = array();
             $handled = array();
             $finishedEchoed = false;
             while (!feof($proc)) {
                 set_time_limit(20);
                 $results = fgets($proc, 256);
                 if (strlen($results) == 0) {
                 } else {
                     $tok = strtok($results, "\n");
                     while ($tok !== false) {
                         $toks[] = $tok;
                         if ($tok == "ZIP_FINISHED") {
                             $finishedEchoed = true;
                         } else {
                             $test = preg_match('/(\\w+): (.*) \\(([^\\(]+)\\) \\(([^\\(]+)\\)/', $tok, $matches);
                             if ($test !== false) {
                                 $handled[] = $matches[2];
                             }
                         }
                         $tok = strtok("\n");
                     }
                     if ($finishedEchoed) {
                         $percent = 100;
                     } else {
                         $percent = min(round(count($handled) / count($todo) * 100), 100);
                     }
                     file_put_contents($percentFile, $percent);
                 }
                 // avoid a busy wait
                 if ($percent < 100) {
                     usleep(1);
                 }
             }
             pclose($proc);
             file_put_contents($percentFile, 100);
             break;
         default:
             break;
     }
 }
 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;
 }
예제 #8
0
 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;
 }
 public function applyAction($actionName, $httpVars, $fileVars)
 {
     $messages = ConfService::getMessages();
     $repoId = $this->accessDriver->repository->getId();
     if ($actionName == "search") {
         // TMP
         if (strpos($httpVars["query"], "keyword:") === 0) {
             $parts = explode(":", $httpVars["query"]);
             $this->applyAction("search_by_keyword", array("field" => $parts[1]), array());
             return null;
         }
         require_once "Zend/Search/Lucene.php";
         try {
             $index = $this->loadIndex($repoId, false);
         } catch (Exception $ex) {
             AJXP_XMLWriter::header();
             if ($this->seemsCurrentlyIndexing($repoId, 3)) {
                 AJXP_XMLWriter::sendMessage($messages["index.lucene.11"], null);
             } else {
                 if (ConfService::backgroundActionsSupported() && !ConfService::currentContextIsCommandLine()) {
                     AJXP_Controller::applyActionInBackground($repoId, "index", array());
                     sleep(2);
                     AJXP_XMLWriter::triggerBgAction("check_index_status", array("repository_id" => $repoId), sprintf($messages["index.lucene.8"], "/"), true, 5);
                     AJXP_XMLWriter::sendMessage($messages["index.lucene.7"], null);
                 } else {
                     AJXP_XMLWriter::sendMessage($messages["index.lucene.12"], null);
                 }
             }
             AJXP_XMLWriter::close();
             return null;
         }
         $textQuery = $httpVars["query"];
         if ($this->getFilteredOption("AUTO_WILDCARD") === true && strlen($textQuery) > 0 && ctype_alnum($textQuery)) {
             if ($textQuery[0] == '"' && $textQuery[strlen($textQuery) - 1] == '"') {
                 $textQuery = substr($textQuery, 1, -1);
             } else {
                 if ($textQuery[strlen($textQuery) - 1] != "*") {
                     $textQuery .= "*";
                 }
             }
         }
         if (strpos($textQuery, ":") !== false) {
             $textQuery = str_replace("ajxp_meta_ajxp_document_content:", "body:", $textQuery);
             $textQuery = $this->filterSearchRangesKeywords($textQuery);
             $query = "ajxp_scope:shared AND ({$textQuery})";
         } else {
             if ((isset($this->metaFields) || $this->indexContent) && isset($httpVars["fields"])) {
                 $sParts = array();
                 foreach (explode(",", $httpVars["fields"]) as $searchField) {
                     if ($searchField == "filename") {
                         $sParts[] = "basename:" . $textQuery;
                     } else {
                         if ($searchField == "ajxp_document_content") {
                             $sParts[] = $textQuery;
                         } else {
                             if (in_array($searchField, $this->metaFields)) {
                                 $sParts[] = "ajxp_meta_" . $searchField . ":" . $textQuery;
                             } else {
                                 if ($searchField == "ajxp_document_content") {
                                     $sParts[] = "title:" . $textQuery;
                                     $sParts[] = "body:" . $textQuery;
                                     $sParts[] = "keywords:" . $textQuery;
                                 }
                             }
                         }
                     }
                 }
                 $query = implode(" OR ", $sParts);
                 $query = "ajxp_scope:shared AND ({$query})";
                 $this->logDebug("Query : {$query}");
             } else {
                 $index->setDefaultSearchField("basename");
                 $query = $this->filterSearchRangesKeywords($textQuery);
             }
         }
         $this->setDefaultAnalyzer();
         if ($query == "*") {
             $index->setDefaultSearchField("ajxp_node");
             $query = "yes";
             $hits = $index->find($query, "node_url", SORT_STRING);
         } else {
             $hits = $index->find($query);
         }
         $commitIndex = false;
         if (isset($httpVars['return_selection'])) {
             $returnNodes = array();
         } else {
             AJXP_XMLWriter::header();
         }
         $cursor = 0;
         if (isset($httpVars['limit'])) {
             $limit = intval($httpVars['limit']);
         }
         foreach ($hits as $hit) {
             // Backward compatibility
             $hit->node_url = preg_replace("#ajxp\\.[a-z_]+://#", "pydio://", $hit->node_url);
             if ($hit->serialized_metadata != null) {
                 $meta = unserialize(base64_decode($hit->serialized_metadata));
                 if (isset($meta["ajxp_modiftime"])) {
                     $meta["ajxp_relativetime"] = $meta["ajxp_description"] = $messages[4] . " " . AJXP_Utils::relativeDate($meta["ajxp_modiftime"], $messages);
                 }
                 $tmpNode = new AJXP_Node(SystemTextEncoding::fromUTF8($hit->node_url), $meta);
             } else {
                 $tmpNode = new AJXP_Node(SystemTextEncoding::fromUTF8($hit->node_url), array());
                 $tmpNode->loadNodeInfo();
             }
             if ($tmpNode->getRepositoryId() != $repoId) {
                 $this->logDebug(__CLASS__, "Strange case, search retrieves a node from wrong repository!");
                 $index->delete($hit->id);
                 $commitIndex = true;
                 continue;
             }
             if (!file_exists($tmpNode->getUrl())) {
                 $index->delete($hit->id);
                 $commitIndex = true;
                 continue;
             }
             if (!is_readable($tmpNode->getUrl())) {
                 continue;
             }
             $basename = basename($tmpNode->getPath());
             $isLeaf = $tmpNode->isLeaf();
             if (!$this->accessDriver->filterNodeName($tmpNode->getPath(), $basename, $isLeaf, array("d" => true, "f" => true))) {
                 continue;
             }
             $tmpNode->search_score = sprintf("%0.2f", $hit->score);
             if (isset($returnNodes)) {
                 $returnNodes[] = $tmpNode;
             } else {
                 AJXP_XMLWriter::renderAjxpNode($tmpNode);
             }
             $cursor++;
             if (isset($limit) && $cursor > $limit) {
                 break;
             }
         }
         if (!isset($returnNodes)) {
             AJXP_XMLWriter::close();
         }
         if ($commitIndex) {
             $index->commit();
         }
     } else {
         if ($actionName == "search_by_keyword") {
             require_once "Zend/Search/Lucene.php";
             $scope = "user";
             try {
                 $index = $this->loadIndex($repoId, false);
             } catch (Exception $ex) {
                 AJXP_XMLWriter::header();
                 if (ConfService::backgroundActionsSupported() && !ConfService::currentContextIsCommandLine()) {
                     AJXP_Controller::applyActionInBackground($repoId, "index", array());
                     AJXP_XMLWriter::triggerBgAction("check_index_status", array("repository_id" => $repoId), sprintf($messages["index.lucene.8"], "/"), true, 2);
                 }
                 AJXP_XMLWriter::sendMessage($messages["index.lucene.7"], null);
                 AJXP_XMLWriter::close();
                 return null;
             }
             $sParts = array();
             $searchField = $httpVars["field"];
             if ($searchField == "ajxp_node") {
                 $sParts[] = "{$searchField}:yes";
             } else {
                 $sParts[] = "{$searchField}:true";
             }
             if ($scope == "user" && AuthService::usersEnabled()) {
                 if (AuthService::getLoggedUser() == null) {
                     throw new Exception("Cannot find current user");
                 }
                 $sParts[] = "ajxp_scope:user";
                 $sParts[] = "ajxp_user:"******"ajxp_scope:shared";
             }
             $query = implode(" AND ", $sParts);
             $this->logDebug("Query : {$query}");
             $hits = $index->find($query);
             $commitIndex = false;
             if (isset($httpVars['return_selection'])) {
                 $returnNodes = array();
             } else {
                 AJXP_XMLWriter::header();
             }
             foreach ($hits as $hit) {
                 // Backward compat with old protocols
                 $hit->node_url = preg_replace("#ajxp\\.[a-z_]+://#", "pydio://", $hit->node_url);
                 if ($hit->serialized_metadata != null) {
                     $meta = unserialize(base64_decode($hit->serialized_metadata));
                     $tmpNode = new AJXP_Node(SystemTextEncoding::fromUTF8($hit->node_url), $meta);
                 } else {
                     $tmpNode = new AJXP_Node(SystemTextEncoding::fromUTF8($hit->node_url), array());
                     $tmpNode->loadNodeInfo();
                 }
                 if (!file_exists($tmpNode->getUrl())) {
                     $index->delete($hit->id);
                     $commitIndex = true;
                     continue;
                 }
                 if (!is_readable($tmpNode->getUrl())) {
                     continue;
                 }
                 $basename = basename($tmpNode->getPath());
                 $isLeaf = $tmpNode->isLeaf();
                 if (!$this->accessDriver->filterNodeName($tmpNode->getPath(), $basename, $isLeaf, array("d" => true, "f" => true))) {
                     continue;
                 }
                 $tmpNode->search_score = sprintf("%0.2f", $hit->score);
                 if (isset($returnNodes)) {
                     $returnNodes[] = $tmpNode;
                 } else {
                     AJXP_XMLWriter::renderAjxpNode($tmpNode);
                 }
             }
             if (!isset($returnNodes)) {
                 AJXP_XMLWriter::close();
             }
             if ($commitIndex) {
                 $index->commit();
             }
         }
     }
     if (isset($returnNodes)) {
         return $returnNodes;
     } else {
         return null;
     }
 }
 public function applyAction($actionName, $httpVars, $fileVars)
 {
     $messages = ConfService::getMessages();
     if ($actionName == "search") {
         require_once "Zend/Search/Lucene.php";
         if ($this->isIndexLocked(ConfService::getRepository()->getId())) {
             throw new Exception($messages["index.lucene.6"]);
         }
         try {
             $index = $this->loadIndex(ConfService::getRepository()->getId(), false);
         } catch (Exception $ex) {
             $this->applyAction("index", array(), array());
             throw new Exception($messages["index.lucene.7"]);
         }
         if ((isset($this->metaFields) || $this->indexContent) && isset($httpVars["fields"])) {
             $sParts = array();
             foreach (explode(",", $httpVars["fields"]) as $searchField) {
                 if ($searchField == "filename") {
                     $sParts[] = "basename:" . $httpVars["query"];
                 } else {
                     if (in_array($searchField, $this->metaFields)) {
                         $sParts[] = "ajxp_meta_" . $searchField . ":" . $httpVars["query"];
                     } else {
                         if ($searchField == "ajxp_document_content") {
                             $sParts[] = "title:" . $httpVars["query"];
                             $sParts[] = "body:" . $httpVars["query"];
                             $sParts[] = "keywords:" . $httpVars["query"];
                         }
                     }
                 }
             }
             $query = implode(" OR ", $sParts);
             AJXP_Logger::debug("Query : {$query}");
         } else {
             $index->setDefaultSearchField("basename");
             $query = $httpVars["query"];
         }
         $hits = $index->find($query);
         $commitIndex = false;
         AJXP_XMLWriter::header();
         foreach ($hits as $hit) {
             $meta = array();
             //$isDir = false; // TO BE STORED IN INDEX
             //$meta["icon"] = AJXP_Utils::mimetype(SystemTextEncoding::fromUTF8($hit->node_url), "image", $isDir);
             if ($hit->serialized_metadata != null) {
                 $meta = unserialize(base64_decode($hit->serialized_metadata));
                 $tmpNode = new AJXP_Node(SystemTextEncoding::fromUTF8($hit->node_url), $meta);
             } else {
                 $tmpNode = new AJXP_Node(SystemTextEncoding::fromUTF8($hit->node_url), array());
                 $tmpNode->loadNodeInfo();
             }
             if (!file_exists($tmpNode->getUrl())) {
                 $index->delete($hit->id);
                 $commitIndex = true;
                 continue;
             }
             $tmpNode->search_score = sprintf("%0.2f", $hit->score);
             AJXP_XMLWriter::renderAjxpNode($tmpNode);
         }
         AJXP_XMLWriter::close();
         if ($commitIndex) {
             $index->commit();
         }
     } else {
         if ($actionName == "index") {
             $dir = AJXP_Utils::decodeSecureMagic($httpVars["dir"]);
             if (empty($dir)) {
                 $dir = "/";
             }
             $repo = ConfService::getRepository();
             if ($this->isIndexLocked($repo->getId())) {
                 throw new Exception($messages["index.lucene.6"]);
             }
             $accessType = $repo->getAccessType();
             $accessPlug = AJXP_PluginsService::getInstance()->getPluginByTypeName("access", $accessType);
             $stData = $accessPlug->detectStreamWrapper(true);
             $repoId = $repo->getId();
             $url = $stData["protocol"] . "://" . $repoId . $dir;
             if (isset($httpVars["verbose"]) && $httpVars["verbose"] == "true") {
                 $this->verboseIndexation = true;
             }
             if (ConfService::backgroundActionsSupported() && !ConfService::currentContextIsCommandLine()) {
                 AJXP_Controller::applyActionInBackground($repoId, "index", $httpVars);
                 AJXP_XMLWriter::header();
                 AJXP_XMLWriter::triggerBgAction("check_lock", array(), sprintf($messages["index.lucene.8"], $dir), true, 2);
                 AJXP_XMLWriter::close();
                 return;
             }
             $this->lockIndex($repoId);
             // GIVE BACK THE HAND TO USER
             session_write_close();
             $this->currentIndex = $this->loadIndex($repoId);
             $this->recursiveIndexation($url);
             if (ConfService::currentContextIsCommandLine() && $this->verboseIndexation) {
                 print "Optimizing\n";
             }
             $this->currentIndex->optimize();
             if (ConfService::currentContextIsCommandLine() && $this->verboseIndexation) {
                 print "Commiting Index\n";
             }
             $this->currentIndex->commit();
             $this->currentIndex = null;
             $this->releaseLock($repoId);
         } else {
             if ($actionName == "check_lock") {
                 $repoId = $httpVars["repository_id"];
                 if ($this->isIndexLocked($repoId)) {
                     AJXP_XMLWriter::header();
                     AJXP_XMLWriter::triggerBgAction("check_lock", array("repository_id" => $repoId), sprintf($messages["index.lucene.8"], ""), true, 3);
                     AJXP_XMLWriter::close();
                 } else {
                     AJXP_XMLWriter::header();
                     AJXP_XMLWriter::triggerBgAction("info_message", array(), $messages["index.lucene.5"], true, 5);
                     AJXP_XMLWriter::close();
                 }
             }
         }
     }
 }
예제 #11
0
 public function applyAction($actionName, $httpVars, $fileVars)
 {
     $messages = ConfService::getMessages();
     $repoId = ConfService::getRepository()->getId();
     if ($actionName == "search") {
         // TMP
         if (strpos($httpVars["query"], "keyword:") === 0) {
             $parts = explode(":", $httpVars["query"]);
             $this->applyAction("search_by_keyword", array("field" => $parts[1]), array());
             return;
         }
         if ($this->isIndexLocked($repoId)) {
             throw new Exception($messages["index.lucene.6"]);
         }
         try {
             $this->loadIndex($repoId, false);
         } catch (Exception $ex) {
             $this->applyAction("index", array(), array());
             throw new Exception($messages["index.lucene.7"]);
         }
         /*
         
         if ((isSet($this->metaFields) || $this->indexContent) && isSet($httpVars["fields"])) {
              $sParts = array();
              foreach (explode(",",$httpVars["fields"]) as $searchField) {
                  if ($searchField == "filename") {
                      $sParts[] = "basename:".$httpVars["query"];
                  } else if (in_array($searchField, $this->metaFields)) {
                      $sParts[] = "ajxp_meta_".$searchField.":".$httpVars["query"];
                  } else if ($searchField == "ajxp_document_content") {
                      $sParts[] = "title:".$httpVars["query"];
                      $sParts[] = "body:".$httpVars["query"];
                      $sParts[] = "keywords:".$httpVars["query"];
                  }
              }
              $query = implode(" OR ", $sParts);
              $query = "ajxp_scope:shared AND ($query)";
              $this->logDebug("Query : $query");
         } else {
         */
         $this->currentIndex->open();
         $query = $httpVars["query"];
         $fieldQuery = new Elastica\Query\Field();
         //}
         //$this->setDefaultAnalyzer();
         if ($query == "*") {
             $fieldQuery->setField("ajxp_node");
             $fieldQuery->setQueryString("yes");
         } else {
             $fieldQuery->setField("basename");
             $fieldQuery->setQueryString($query);
         }
         /*
            We create this object search because it'll allow us to fetch the number of results we want at once.
            We just have to set some parameters, the query type and the size of the result set.
         */
         $search = new Elastica\Search($this->client);
         $search->addIndex($this->currentIndex)->addType($this->currentType);
         $maxResults = $this->getFilteredOption("MAX_RESULTS");
         $searchOptions = array(\Elastica\Search::OPTION_SEARCH_TYPE => \Elastica\Search::OPTION_SEARCH_TYPE_QUERY_THEN_FETCH, \Elastica\Search::OPTION_SIZE => $maxResults);
         $result = $search->search($fieldQuery, $searchOptions);
         $total_hits = $result->getTotalHits();
         $hits = $result->getResults();
         AJXP_XMLWriter::header();
         for ($i = 0, $count = count($hits); $i < $count; $i++) {
             $hit = $hits[$i];
             $source = $hit->getSource();
             if ($source["serialized_metadata"] != null) {
                 $meta = unserialize(base64_decode($source["serialized_metadata"]));
                 $tmpNode = new AJXP_Node(SystemTextEncoding::fromUTF8($source["node_url"]), $meta);
             } else {
                 $tmpNode = new AJXP_Node(SystemTextEncoding::fromUTF8($source["node_url"]), array());
                 $tmpNode->loadNodeInfo();
             }
             if (!file_exists($tmpNode->getUrl())) {
                 $this->currentType->deleteById($hit->getId());
                 continue;
             }
             $tmpNode->search_score = sprintf("%0.2f", $hit->getScore());
             AJXP_XMLWriter::renderAjxpNode($tmpNode);
         }
         AJXP_XMLWriter::close();
         $this->currentIndex->close();
     } else {
         if ($actionName == "search_by_keyword") {
             /*    require_once("Zend/Search/Lucene.php");
                         $scope = "user";
             
                         if ($this->isIndexLocked(ConfService::getRepository()->getId())) {
                             throw new Exception($messages["index.lucene.6"]);
                         }
                         try {
                             $this->currentInd =  $this->loadIndex(ConfService::getRepository()->getId(), false);
                         } catch (Exception $ex) {
                             $this->applyAction("index", array(), array());
                             throw new Exception($messages["index.lucene.7"]);
                         }
                         $sParts = array();
                         $searchField = $httpVars["field"];
                         if ($searchField == "ajxp_node") {
                             $sParts[] = "$searchField:yes";
                         } else {
                             $sParts[] = "$searchField:true";
                         }
                         if ($scope == "user") {
                             if (AuthService::usersEnabled() && AuthService::getLoggedUser() == null) {
                                 throw new Exception("Cannot find current user");
                             }
                             $sParts[] = "ajxp_scope:user";
                             $sParts[] = "ajxp_user:"******"ajxp_scope:shared";
                         }
                         $query = implode(" AND ", $sParts);
                         $this->logDebug("Query : $query");
                         $hits = $this->currentIndex->find($query);
             
                         $commitIndex = false;
             
                         AJXP_XMLWriter::header();
                         foreach ($hits as $hit) {
                             if ($hit->serialized_metadata!=null) {
                                 $meta = unserialize(base64_decode($hit->serialized_metadata));
                                 $tmpNode = new AJXP_Node(SystemTextEncoding::fromUTF8($hit->node_url), $meta);
                             } else {
                                 $tmpNode = new AJXP_Node(SystemTextEncoding::fromUTF8($hit->node_url), array());
                                 $tmpNode->loadNodeInfo();
                             }
                             if (!file_exists($tmpNode->getUrl())) {
                                 $this->currentIndex->delete($hit->id);
                                 $commitIndex = true;
                                 continue;
                             }
                             $tmpNode->search_score = sprintf("%0.2f", $hit->score);
                             AJXP_XMLWriter::renderAjxpNode($tmpNode);
                         }
                         AJXP_XMLWriter::close();
                         if ($commitIndex) {
                             $this->currentIndex->commit();
                         }*/
         } else {
             if ($actionName == "index") {
                 $dir = AJXP_Utils::decodeSecureMagic($httpVars["dir"]);
                 if (empty($dir)) {
                     $dir = "/";
                 }
                 $repo = ConfService::getRepository();
                 if ($this->isIndexLocked($repo->getId())) {
                     throw new Exception($messages["index.lucene.6"]);
                 }
                 $accessType = $repo->getAccessType();
                 $accessPlug = AJXP_PluginsService::getInstance()->getPluginByTypeName("access", $accessType);
                 $stData = $accessPlug->detectStreamWrapper(true);
                 $repoId = $repo->getId();
                 $url = $stData["protocol"] . "://" . $repoId . $dir;
                 if (isset($httpVars["verbose"]) && $httpVars["verbose"] == "true") {
                     $this->verboseIndexation = true;
                 }
                 if (ConfService::backgroundActionsSupported() && !ConfService::currentContextIsCommandLine()) {
                     AJXP_Controller::applyActionInBackground($repoId, "index", $httpVars);
                     AJXP_XMLWriter::header();
                     AJXP_XMLWriter::triggerBgAction("check_lock", array("repository_id" => $repoId), sprintf($messages["index.lucene.8"], $dir), true, 2);
                     AJXP_XMLWriter::close();
                     return;
                 }
                 $this->lockIndex($repoId);
                 // GIVE BACK THE HAND TO USER
                 session_write_close();
                 $this->loadIndex($repoId);
                 $this->currentIndex->open();
                 $this->recursiveIndexation($url);
                 if (ConfService::currentContextIsCommandLine() && $this->verboseIndexation) {
                     print "Optimizing\n";
                     $this->currentIndex->optimize();
                 }
                 $this->currentIndex->close();
                 $this->currentIndex = null;
                 $this->releaseLock($repoId);
             } else {
                 if ($actionName == "check_lock") {
                     $repoId = $httpVars["repository_id"];
                     if ($this->isIndexLocked($repoId)) {
                         AJXP_XMLWriter::header();
                         AJXP_XMLWriter::triggerBgAction("check_lock", array("repository_id" => $repoId), $messages["index.lucene.10"], true, 3);
                         AJXP_XMLWriter::close();
                     } else {
                         AJXP_XMLWriter::header();
                         AJXP_XMLWriter::triggerBgAction("info_message", array(), $messages["index.lucene.5"], true, 5);
                         AJXP_XMLWriter::close();
                     }
                 }
             }
         }
     }
 }