/** * Extract a ZIP compressed file to a given path * * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive into * * @return boolean True if successful * @throws Exception */ public function extract($archive, $destination) { $this->data = null; $this->metadata = null; $this->data = file_get_contents($archive); if (!$this->data) { throw new Exception('Unable to read archive'); } $this->getTarInfo($this->data); for ($i = 0, $n = count($this->metadata); $i < $n; $i++) { $type = strtolower($this->metadata[$i]['type']); if ($type == 'file' || $type == 'unix file') { $buffer = $this->metadata[$i]['data']; $path = xapp_Path2::clean($destination . '/' . $this->metadata[$i]['name']); // Make sure the destination folder exists if (!xapp_Folder2::create(dirname($path))) { throw new Exception('Unable to create destination'); } if (xapp_File2::write($path, $buffer) === false) { throw new Exception('Unable to write entry'); } } } return true; }
/** * Create a folder -- and all necessary parent folders. * * @param string $path A path to create from the base path. * @param integer $mode Directory permissions to set for folders created. 0755 by default. * * @return boolean True if successful. * * @since 1.0 * @throws FilesystemException */ public static function create($path = '', $mode = 0755) { static $nested = 0; // Check to make sure the path valid and clean $path = xapp_Path2::clean($path); // Check if parent dir exists $parent = dirname($path); if (!is_dir(xapp_Path2::clean($parent))) { // Prevent infinite loops! $nested++; if ($nested > 20 || $parent == $path) { throw new FilesystemException(__METHOD__ . ': Infinite loop detected'); } // Create the parent directory if (self::create($parent, $mode) !== true) { // Folder::create throws an error $nested--; return false; } // OK, parent directory has been created $nested--; } // Check if dir already exists if (is_dir(xapp_Path2::clean($path))) { return true; } // We need to get and explode the open_basedir paths $obd = ini_get('open_basedir'); // If open_basedir is set we need to get the open_basedir that the path is in if ($obd != null) { if (defined('PHP_WINDOWS_VERSION_MAJOR')) { $obdSeparator = ";"; } else { $obdSeparator = ":"; } // Create the array of open_basedir paths $obdArray = explode($obdSeparator, $obd); $inBaseDir = false; // Iterate through open_basedir paths looking for a match foreach ($obdArray as $test) { $test = xapp_Path2::clean($test); if (strpos($path, $test) === 0) { $inBaseDir = true; break; } } if ($inBaseDir == false) { // Throw a FilesystemException because the path to be created is not in open_basedir throw new FilesystemException(__METHOD__ . ': Path not in open_basedir paths'); } } // First set umask $origmask = @umask(0); // Create the path if (!($ret = @mkdir($path, $mode))) { @umask($origmask); throw new FilesystemException(__METHOD__ . ': Could not create directory. Path: ' . $path); } // Reset umask @umask($origmask); return $ret; }
/** * Write contents to a file * * @param string $file The full file path * @param string &$buffer The buffer to write * @param boolean $use_streams Use streams * * @return boolean True on success * * @since 1.0 * @throws FilesystemException */ public static function write($file, &$buffer, $use_streams = false) { @set_time_limit(ini_get('max_execution_time')); // If the destination directory doesn't exist we need to create it if (!file_exists(dirname($file))) { xapp_Folder2::create(dirname($file)); } if ($use_streams) { $stream = Stream::getStream(); // Beef up the chunk size to a meg $stream->set('chunksize', 1024 * 1024); if (!$stream->writeFile($file, $buffer)) { throw new FilesystemException(sprintf('%1$s(%2$s): %3$s', __METHOD__, $file, $stream->getError())); } return true; } else { $file = xapp_Path2::clean($file); $ret = is_int(file_put_contents($file, $buffer)) ? true : false; return $ret; } }