示例#1
0
 /**
  * Get a real reference to the filesystem. Remote wrappers will copy the file locally.
  * This will last the time of the script and will be removed afterward.
  * @return string
  */
 public function getRealFile()
 {
     if (!isset($this->realFilePointer)) {
         $this->realFilePointer = AJXP_MetaStreamWrapper::getRealFSReference($this->_url, true);
         $isRemote = AJXP_MetaStreamWrapper::wrapperIsRemote($this->_url);
         if ($isRemote) {
             register_shutdown_function(array("AJXP_Utils", "silentUnlink"), $this->realFilePointer);
         }
     }
     return $this->realFilePointer;
 }
 /**
  * Get a "usable" reference to a file : the real file or a tmp copy.
  *
  * @param string $path
  * @param bool $persistent
  * @return string
  */
 public static function getRealFSReference($path, $persistent = false)
 {
     $url = self::translateURL($path);
     if (self::$linkNode !== null) {
         $isRemote = AJXP_MetaStreamWrapper::wrapperIsRemote($url);
         $realFilePointer = AJXP_MetaStreamWrapper::getRealFSReference($url, true);
         if (!$isRemote) {
             $ext = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
             $tmpname = tempnam(AJXP_Utils::getAjxpTmpDir(), "real-file-inbox-pointer") . "." . $ext;
             copy($realFilePointer, $tmpname);
             $realFilePointer = $tmpname;
         }
         ConfService::loadDriverForRepository(self::$linkNode->getRepository());
         return $realFilePointer;
     } else {
         $tmpname = tempnam(AJXP_Utils::getAjxpTmpDir(), "real-file-inbox-pointer");
         $source = fopen($url, "r");
         $dest = fopen($tmpname, "w");
         stream_copy_to_stream($source, $dest);
         return $tmpname;
     }
 }
示例#3
0
 public function countFiles($dirName, $foldersOnly = false, $nonEmptyCheckOnly = false, $dirHANDLE = null)
 {
     if (is_resource($dirHANDLE)) {
         $handle = $dirHANDLE;
     } else {
         $handle = @opendir($dirName);
     }
     if ($handle === false) {
         throw new Exception("Error while trying to open directory " . $dirName);
     }
     if ($foldersOnly && !AJXP_MetaStreamWrapper::wrapperIsRemote($dirName)) {
         if ($dirHANDLE == null || !is_resource($dirHANDLE)) {
             closedir($handle);
         }
         $path = AJXP_MetaStreamWrapper::getRealFSReference($dirName, true);
         $dirs = glob($path . "/*", GLOB_ONLYDIR | GLOB_NOSORT);
         if ($dirs === false) {
             return 0;
         }
         return count($dirs);
     }
     $count = 0;
     $showHiddenFiles = $this->getFilteredOption("SHOW_HIDDEN_FILES", $this->repository);
     while (strlen($file = readdir($handle)) > 0) {
         if ($file != "." && $file != ".." && !(AJXP_Utils::isHidden($file) && !$showHiddenFiles)) {
             if ($foldersOnly && is_file($dirName . "/" . $file)) {
                 continue;
             }
             $count++;
             if ($nonEmptyCheckOnly) {
                 break;
             }
         }
     }
     if ($dirHANDLE == null || !is_resource($dirHANDLE)) {
         closedir($handle);
     }
     return $count;
 }
 public function unifyChunks($action, &$httpVars, &$fileVars)
 {
     $filename = AJXP_Utils::decodeSecureMagic($httpVars["name"]);
     $tmpName = $fileVars["file"]["tmp_name"];
     $chunk = $httpVars["chunk"];
     $chunks = $httpVars["chunks"];
     //error_log("currentChunk:".$chunk."  chunks: ".$chunks);
     $repository = ConfService::getRepository();
     if (!$repository->detectStreamWrapper(true)) {
         return false;
     }
     $userSelection = new UserSelection($repository);
     $dir = AJXP_Utils::securePath($httpVars["dir"]);
     $destStreamURL = $userSelection->currentBaseUrl() . $dir . "/";
     $driver = ConfService::loadDriverForRepository($repository);
     $remote = false;
     if (method_exists($driver, "storeFileToCopy")) {
         $remote = true;
         $destCopy = AJXP_XMLWriter::replaceAjxpXmlKeywords($repository->getOption("TMP_UPLOAD"));
         // Make tmp folder a bit more unique using secure_token
         $tmpFolder = $destCopy . "/" . $httpVars["secure_token"];
         if (!is_dir($tmpFolder)) {
             @mkdir($tmpFolder, 0700, true);
         }
         $target = $tmpFolder . '/' . $filename;
         $fileVars["file"]["destination"] = base64_encode($dir);
     } else {
         if (AJXP_MetaStreamWrapper::wrapperIsRemote($destStreamURL)) {
             $remote = true;
             $tmpFolder = AJXP_Utils::getAjxpTmpDir() . "/" . $httpVars["secure_token"];
             if (!is_dir($tmpFolder)) {
                 @mkdir($tmpFolder, 0700, true);
             }
             $target = $tmpFolder . '/' . $filename;
         } else {
             $target = $destStreamURL . $filename;
         }
     }
     //error_log("Directory: ".$dir);
     // Clean the fileName for security reasons
     //$filename = preg_replace('/[^\w\._]+/', '', $filename);
     $contentType = "";
     // Look for the content type header
     if (isset($_SERVER["HTTP_CONTENT_TYPE"])) {
         $contentType = $_SERVER["HTTP_CONTENT_TYPE"];
     }
     if (isset($_SERVER["CONTENT_TYPE"])) {
         $contentType = $_SERVER["CONTENT_TYPE"];
     }
     // Handle non multipart uploads older WebKit versions didn't support multipart in HTML5
     if (strpos($contentType, "multipart") !== false) {
         if (isset($tmpName) && is_uploaded_file($tmpName)) {
             //error_log("tmpName: ".$tmpName);
             // Open temp file
             $out = fopen($target, $chunk == 0 ? "wb" : "ab");
             if ($out) {
                 // Read binary input stream and append it to temp file
                 $in = fopen($tmpName, "rb");
                 if ($in) {
                     while ($buff = fread($in, 4096)) {
                         fwrite($out, $buff);
                     }
                 } else {
                     die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
                 }
                 fclose($in);
                 fclose($out);
                 @unlink($tmpName);
             } else {
                 die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
             }
         } else {
             die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
         }
     } else {
         // Open temp file
         $out = fopen($target, $chunk == 0 ? "wb" : "ab");
         if ($out) {
             // Read binary input stream and append it to temp file
             $in = fopen("php://input", "rb");
             if ($in) {
                 while ($buff = fread($in, 4096)) {
                     fwrite($out, $buff);
                 }
             } else {
                 die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
             }
             fclose($in);
             fclose($out);
         } else {
             die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
         }
     }
     /* we apply the hook if we are uploading the last chunk */
     if ($chunk == $chunks - 1) {
         if (!$remote) {
             AJXP_Controller::applyHook("node.change", array(null, new AJXP_Node($destStreamURL . $filename), false));
         } else {
             if (method_exists($driver, "storeFileToCopy")) {
                 $fileVars["file"]["tmp_name"] = $target;
                 $fileVars["file"]["name"] = $filename;
                 $driver->storeFileToCopy($fileVars["file"]);
                 AJXP_Controller::findActionAndApply("next_to_remote", array(), array());
             } else {
                 // Remote Driver case: copy temp file to destination
                 $node = new AJXP_Node($destStreamURL . $filename);
                 AJXP_Controller::applyHook("node.before_create", array($node, filesize($target)));
                 AJXP_Controller::applyHook("node.before_change", array(new AJXP_Node($destStreamURL)));
                 $res = copy($target, $destStreamURL . $filename);
                 if ($res) {
                     @unlink($target);
                 }
                 AJXP_Controller::applyHook("node.change", array(null, $node, false));
             }
         }
     }
     // Return JSON-RPC response
     die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
 }
 /**
  * @param String $srcFile url of source file
  * @param String $destFile url of destination file
  */
 protected function filecopy($srcFile, $destFile)
 {
     if (!AJXP_MetaStreamWrapper::nodesUseSameWrappers($srcFile, $destFile) || AJXP_MetaStreamWrapper::wrapperIsRemote($srcFile) || AJXP_MetaStreamWrapper::wrapperIsRemote($destFile)) {
         $src = fopen($srcFile, "r");
         $dest = fopen($destFile, "w");
         if (is_resource($src) && is_resource($dest)) {
             while (!feof($src)) {
                 //stream_copy_to_stream($src, $dest, 4096);
                 $count = stream_copy_to_stream($src, $dest, 4096);
                 if ($count == 0) {
                     break;
                 }
             }
         }
         if (is_resource($dest)) {
             fclose($dest);
         }
         if (is_resource($src)) {
             fclose($src);
         }
     } else {
         copy($srcFile, $destFile);
     }
 }