示例#1
0
 /**
  * @param string $path
  * @param string $targetDir
  * @param string $root path without trailing slash
  * @return boolean
  */
 static function extractByRoot($path, $targetDir, $root)
 {
     //get items paths from within root
     $namesList = srokap_zip::getArchiveNameIndex($path);
     if ($namesList === false) {
         throw new IOException("Error while reading archive contents: {$path}");
     }
     foreach ($namesList as $key => $item) {
         //keep only items within root path
         if (strpos($item, $root) !== 0) {
             unset($namesList[$key]);
         }
     }
     if (empty($namesList)) {
         throw new IOException("Invalid archive root path privided: {$root}");
     }
     $targetDir .= '/';
     if (!srokap_files::createDir($targetDir)) {
         throw new IOException("Unable to create target dir: {$targetDir}");
     }
     $result = false;
     $zip = new ZipArchive();
     if ($zip->open($path)) {
         foreach ($namesList as $key => $item) {
             $sufix = substr($item, strlen($root) + 1);
             if ($sufix === false) {
                 continue;
             }
             $path = $targetDir . $sufix;
             $dirPath = dirname($path);
             if (!empty($dirPath) && !srokap_files::createDir($dirPath)) {
                 throw new IOException("Unable to create dir: " . dirname($path));
             }
             if ($item[strlen($item) - 1] == '/') {
                 //not a file
                 continue;
             }
             $content = stream_get_contents($zip->getStream($item));
             if ($content === false) {
                 throw new IOException("Error reading from archive path: {$item}");
             }
             if (file_put_contents($path, $content) === false) {
                 throw new IOException("Error extracting file from {$item} to {$path}");
             }
         }
         $result = true;
     }
     $zip->close();
     return $result;
 }
 /**
  * Download most recent version.
  * @throws IOException
  */
 public function fetch($version = null)
 {
     $url = $this->getDownloadURL();
     if ($url === false) {
         throw new IOException(elgg_echo('action:plugin:download:error:no_download_url', array($this->getURL())));
     }
     if ($version === null) {
         $version = $this->getLatestVersion();
     }
     $packagePath = $this->getPackagePath($version);
     srokap_files::createDir($packagePath);
     $meta = srokap_http::getUrlToFile($url, $packagePath . 'package');
     if ($meta === false) {
         throw new IOException("Error while fetching plugin package from: {$url}");
     }
     if (file_put_contents($packagePath . 'version', $this->getLatestVersion()) === false) {
         throw new IOException("Error while saving version file at: {$packagePath}");
     }
     if (file_put_contents($packagePath . 'entity', serialize($this)) === false) {
         throw new IOException("Error while saving entity file at: {$packagePath}");
     }
     if (file_put_contents($packagePath . 'metadata', serialize($meta)) === false) {
         throw new IOException("Error while saving metadata file at: {$packagePath}");
     }
     return $packagePath;
 }
 /**
  * Create dir if not exists and check if all preconditions are correct.
  */
 static function validatePath($path)
 {
     return srokap_files::createDir($path, self::$dataPathMod);
 }