cvs: pear /PHP_Archive/Archive Creator.php
| From: | Greg Beaver | Date: | Mon, 11 Apr 2005 05:11:50 +0000 |
| Subject: | cvs: pear /PHP_Archive/Archive Creator.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-31091@lists.php.net to get a copy of this message | ||
cellog Mon Apr 11 01:11:50 2005 EDT
Modified files:
/pear/PHP_Archive/Archive Creator.php
Log:
make sure __HALT_PHP_PARSER__ is on its own line
so that the 5.1 lexer finds it
implement addDir(), grabbing the ignore/include
code from PEAR_PFM (that was convenient)
http://cvs.php.net/diff.php/pear/PHP_Archive/Archive/Creator.php?r1=1.4&r2=1.5&ty=u Index: pear/PHP_Archive/Archive/Creator.php diff -u pear/PHP_Archive/Archive/Creator.php:1.4 pear/PHP_Archive/Archive/Creator.php:1.5 --- pear/PHP_Archive/Archive/Creator.php:1.4 Sat Apr 9 00:45:13 2005 +++ pear/PHP_Archive/Archive/Creator.php Mon Apr 11 01:11:49 2005 @@ -31,7 +31,7 @@ * @copyright Copyright © David Shafik and Synaptic Media 2004. All rights reserved. * @author Davey Shafik <davey@synapticmedia.net> * @link http://www.synapticmedia.net Synaptic Media - * @version $Id: Creator.php,v 1.4 2005/04/09 04:45:13 cellog Exp $ + * @version $Id: Creator.php,v 1.5 2005/04/11 05:11:49 cellog Exp $ * @package PHP_Archive * @category PHP * @example c:\web\php-cvs\pear\PHP_Archive\docs\examples\PHP_Archive_Creator_Example_1.php Example PHP_Archive_Creator usage @@ -143,6 +143,7 @@ } ?> <?php __HALT_PHP_PARSER__; ?> + PHP; $tar->addString('<?php #PHP_ARCHIVE_HEADER-0.5.0.php', $unpack_code); @@ -152,7 +153,7 @@ } /** - * PHP4 COmpatible Constructor + * PHP4 Compatible Constructor * * @see PHP_Archive_Creator::__construct */ @@ -185,19 +186,212 @@ clearstatcache(); // a newly created archive could be erased if this is not performed return $this->tar->addString($save_path, $file_contents); } + + /** + * Tell whether to ignore a file or a directory + * allows * and ? wildcards + * + * @param string $file just the file name of the file or directory, + * in the case of directories this is the last dir + * @param string $path the full path + * @param 1|0 $return value to return if regexp matches. Set this to + * false to include only matches, true to exclude + * all matches + * @return bool true if $path should be ignored, false if it should not + * @access private + */ + function _checkIgnore($file, $path, $return = 1) + { + if (file_exists($path)) { + $path = realpath($path); + } + if (is_array($this->ignore[$return])) { + foreach($this->ignore[$return] as $match) { + // match is an array if the ignore parameter was a /path/to/pattern + if (is_array($match)) { + // check to see if the path matches with a path delimiter appended + preg_match('/^' . strtoupper($match[0]).'$/', strtoupper($path) . '/',$find); + if (!count($find)) { + // check to see if it matches without an appended path delimiter + preg_match('/^' . strtoupper($match[0]).'$/', strtoupper($path), $find); + } + if (count($find)) { + // check to see if the file matches the file portion of the regex string + preg_match('/^' . strtoupper($match[1]).'$/', strtoupper($file), $find); + if (count($find)) { + return $return; + } + } + // check to see if the full path matches the regex + preg_match('/^' . strtoupper($match[0]).'$/', + strtoupper($path . DIRECTORY_SEPARATOR . $file), $find); + if (count($find)) { + return $return; + } + } else { + // ignore parameter was just a pattern with no path delimiters + // check it against the path + preg_match('/^' . strtoupper($match).'$/', strtoupper($path), $find); + if (count($find)) { + return $return; + } + // check it against the file only + preg_match('/^' . strtoupper($match).'$/', strtoupper($file), $find); + if (count($find)) { + return $return; + } + } + } + } + return !$return; + } /** - * Add a directory to the archive + * Construct the {@link $ignore} array + * @param array strings of files/paths/wildcards to ignore + * @param 0|1 0 = files to include, 1 = files to ignore + * @access private + */ + function _setupIgnore($ignore, $index) + { + $ig = array(); + if (is_array($ignore)) { + for($i=0; $i<count($ignore);$i++) { + $ignore[$i] = strtr($ignore[$i], "\\", "/"); + $ignore[$i] = str_replace('//','/',$ignore[$i]); + + if (!empty($ignore[$i])) { + if (!is_numeric(strpos($ignore[$i], '/'))) { + $ig[] = $this->_getRegExpableSearchString($ignore[$i]); + } else { + if (basename($ignore[$i]) . '/' == $ignore[$i]) { + $ig[] = $this->_getRegExpableSearchString($ignore[$i]); + } else { + $ig[] = array($this->_getRegExpableSearchString($ignore[$i]), + $this->_getRegExpableSearchString(basename($ignore[$i]))); + } + } + } + } + if (count($ig)) { + $this->ignore[$index] = $ig; + } else { + $this->ignore[$index] = false; + } + } else $this->ignore[$index] = false; + } + + /** + * Converts $s into a string that can be used with preg_match + * @param string $s string with wildcards ? and * + * @return string converts * to .*, ? to ., etc. + * @access private + */ + function _getRegExpableSearchString($s) + { + $y = '\/'; + if (DIRECTORY_SEPARATOR == '\\') { + $y = '\\\\'; + } + $s = str_replace('/', DIRECTORY_SEPARATOR, $s); + $x = strtr($s, array('?' => '.','*' => '.*','.' => '\\.','\\' => '\\\\','/' => '\\/', + '[' => '\\[',']' => '\\]','-' => '\\-')); + if (strpos($s, DIRECTORY_SEPARATOR) !== false && + strrpos($s, DIRECTORY_SEPARATOR) === strlen($s) - 1) { + $x = "(?:.*$y$x?.*|$x.*)"; + } + return $x; + } + + /** + * Test whether an entry should be processed. + * + * Normally, it ignores all files and directories that begin with "." addhiddenfiles option + * instead only ignores "." and ".." entries + * @access private + * @param string directory name of entry + * @param string name + */ + function _testFile($directory, $entry) + { + return is_file($directory . '/' . $entry) || + (is_dir($directory . '/' . $entry) && !in_array($entry, array('.', '..'))); + } + + /** + * Retrieve a listing of every file in $directory and + * all subdirectories. * - * Not implemented yet! + * The return format is an array of full paths to files + * @access protected + * @return array list of files in a directory + * @param string $directory full path to the directory you want the list of + */ + function dirList($directory, $toplevel = null) + { + if ($toplevel === null) { + $toplevel = $directory; + } + $ret = false; + $dirname = str_replace($toplevel . DIRECTORY_SEPARATOR, '', $directory); + $dirname = str_replace($toplevel, '', $dirname); + if ($dirname) { + $dirname .= DIRECTORY_SEPARATOR; + } + if (@is_dir($directory)) { + $ret = array(); + $d = @dir($directory); + while($d && false !== ($entry = $d->read())) { + if ($this->_testFile($directory, $entry)) { + if (is_file($directory . '/' . $entry)) { + // if include option was set, then only pass included files + if ($this->ignore[0]) { + if ($this->_checkIgnore($entry, $directory . '/' . $entry, 0)) { + continue; + } + } + // if ignore option was set, then only pass included files + if ($this->ignore[1]) { + if ($this->_checkIgnore($entry, $directory . '/' . $entry, 1)) { + continue; + } + } + $ret[$directory . DIRECTORY_SEPARATOR . $entry] = $dirname . $entry; + } + if (is_dir($directory . '/' . $entry)) { + $tmp = $this->dirList($directory . DIRECTORY_SEPARATOR . $entry, $toplevel); + if (is_array($tmp)) { + foreach($tmp as $i => $ent) { + $ret[$i] = $ent; + } + } + } + } + } + if ($d) { + $d->close(); + } + } else { + return false; + } + return $ret; + } + + /** + * Add a directory to the archive * * @param string $dir The directory path to add + * @param array files to ignore + * @param array files to include (all others ignored) * @return boolean */ - function addDir($dir) + function addDir($dir, $ignore = array(), $include = array()) { - // Not implemented yet! + $this->_setupIgnore($ignore, 1); + $this->_setupIgnore($include, 0); + $list = $this->dirList($dir); + return $this->addArray($list); } /** @@ -209,10 +403,10 @@ function addArray($files) { - $this->modified = true; - if (!$this->compress) { - $this->compress = $compress; + if (!is_array($files) || empty($files)) { + return false; } + $this->modified = true; foreach ($files as $file_path => $save_path) { $returns[] = $this->addFile($file_path, $save_path, $compress); }
http://cvs.php.net/diff.php/pear/PHP_Archive/Archive/Creator.php?r1=1.4&r2=1.5&ty=u Index: pear/PHP_Archive/Archive/Creator.php diff -u pear/PHP_Archive/Archive/Creator.php:1.4 pear/PHP_Archive/Archive/Creator.php:1.5 --- pear/PHP_Archive/Archive/Creator.php:1.4 Sat Apr 9 00:45:13 2005 +++ pear/PHP_Archive/Archive/Creator.php Mon Apr 11 01:11:49 2005 @@ -31,7 +31,7 @@ * @copyright Copyright © David Shafik and Synaptic Media 2004. All rights reserved. * @author Davey Shafik <davey@synapticmedia.net> * @link http://www.synapticmedia.net Synaptic Media - * @version $Id: Creator.php,v 1.4 2005/04/09 04:45:13 cellog Exp $ + * @version $Id: Creator.php,v 1.5 2005/04/11 05:11:49 cellog Exp $ * @package PHP_Archive * @category PHP * @example c:\web\php-cvs\pear\PHP_Archive\docs\examples\PHP_Archive_Creator_Example_1.php Example PHP_Archive_Creator usage @@ -143,6 +143,7 @@ } ?> <?php __HALT_PHP_PARSER__; ?> + PHP; $tar->addString('<?php #PHP_ARCHIVE_HEADER-0.5.0.php', $unpack_code); @@ -152,7 +153,7 @@ } /** - * PHP4 COmpatible Constructor + * PHP4 Compatible Constructor * * @see PHP_Archive_Creator::__construct */ @@ -185,19 +186,212 @@ clearstatcache(); // a newly created archive could be erased if this is not performed return $this->tar->addString($save_path, $file_contents); } + + /** + * Tell whether to ignore a file or a directory + * allows * and ? wildcards + * + * @param string $file just the file name of the file or directory, + * in the case of directories this is the last dir + * @param string $path the full path + * @param 1|0 $return value to return if regexp matches. Set this to + * false to include only matches, true to exclude + * all matches + * @return bool true if $path should be ignored, false if it should not + * @access private + */ + function _checkIgnore($file, $path, $return = 1) + { + if (file_exists($path)) { + $path = realpath($path); + } + if (is_array($this->ignore[$return])) { + foreach($this->ignore[$return] as $match) { + // match is an array if the ignore parameter was a /path/to/pattern + if (is_array($match)) { + // check to see if the path matches with a path delimiter appended + preg_match('/^' . strtoupper($match[0]).'$/', strtoupper($path) . '/',$find); + if (!count($find)) { + // check to see if it matches without an appended path delimiter + preg_match('/^' . strtoupper($match[0]).'$/', strtoupper($path), $find); + } + if (count($find)) { + // check to see if the file matches the file portion of the regex string + preg_match('/^' . strtoupper($match[1]).'$/', strtoupper($file), $find); + if (count($find)) { + return $return; + } + } + // check to see if the full path matches the regex + preg_match('/^' . strtoupper($match[0]).'$/', + strtoupper($path . DIRECTORY_SEPARATOR . $file), $find); + if (count($find)) { + return $return; + } + } else { + // ignore parameter was just a pattern with no path delimiters + // check it against the path + preg_match('/^' . strtoupper($match).'$/', strtoupper($path), $find); + if (count($find)) { + return $return; + } + // check it against the file only + preg_match('/^' . strtoupper($match).'$/', strtoupper($file), $find); + if (count($find)) { + return $return; + } + } + } + } + return !$return; + } /** - * Add a directory to the archive + * Construct the {@link $ignore} array + * @param array strings of files/paths/wildcards to ignore + * @param 0|1 0 = files to include, 1 = files to ignore + * @access private + */ + function _setupIgnore($ignore, $index) + { + $ig = array(); + if (is_array($ignore)) { + for($i=0; $i<count($ignore);$i++) { + $ignore[$i] = strtr($ignore[$i], "\\", "/"); + $ignore[$i] = str_replace('//','/',$ignore[$i]); + + if (!empty($ignore[$i])) { + if (!is_numeric(strpos($ignore[$i], '/'))) { + $ig[] = $this->_getRegExpableSearchString($ignore[$i]); + } else { + if (basename($ignore[$i]) . '/' == $ignore[$i]) { + $ig[] = $this->_getRegExpableSearchString($ignore[$i]); + } else { + $ig[] = array($this->_getRegExpableSearchString($ignore[$i]), + $this->_getRegExpableSearchString(basename($ignore[$i]))); + } + } + } + } + if (count($ig)) { + $this->ignore[$index] = $ig; + } else { + $this->ignore[$index] = false; + } + } else $this->ignore[$index] = false; + } + + /** + * Converts $s into a string that can be used with preg_match + * @param string $s string with wildcards ? and * + * @return string converts * to .*, ? to ., etc. + * @access private + */ + function _getRegExpableSearchString($s) + { + $y = '\/'; + if (DIRECTORY_SEPARATOR == '\\') { + $y = '\\\\'; + } + $s = str_replace('/', DIRECTORY_SEPARATOR, $s); + $x = strtr($s, array('?' => '.','*' => '.*','.' => '\\.','\\' => '\\\\','/' => '\\/', + '[' => '\\[',']' => '\\]','-' => '\\-')); + if (strpos($s, DIRECTORY_SEPARATOR) !== false && + strrpos($s, DIRECTORY_SEPARATOR) === strlen($s) - 1) { + $x = "(?:.*$y$x?.*|$x.*)"; + } + return $x; + } + + /** + * Test whether an entry should be processed. + * + * Normally, it ignores all files and directories that begin with "." addhiddenfiles option + * instead only ignores "." and ".." entries + * @access private + * @param string directory name of entry + * @param string name + */ + function _testFile($directory, $entry) + { + return is_file($directory . '/' . $entry) || + (is_dir($directory . '/' . $entry) && !in_array($entry, array('.', '..'))); + } + + /** + * Retrieve a listing of every file in $directory and + * all subdirectories. * - * Not implemented yet! + * The return format is an array of full paths to files + * @access protected + * @return array list of files in a directory + * @param string $directory full path to the directory you want the list of + */ + function dirList($directory, $toplevel = null) + { + if ($toplevel === null) { + $toplevel = $directory; + } + $ret = false; + $dirname = str_replace($toplevel . DIRECTORY_SEPARATOR, '', $directory); + $dirname = str_replace($toplevel, '', $dirname); + if ($dirname) { + $dirname .= DIRECTORY_SEPARATOR; + } + if (@is_dir($directory)) { + $ret = array(); + $d = @dir($directory); + while($d && false !== ($entry = $d->read())) { + if ($this->_testFile($directory, $entry)) { + if (is_file($directory . '/' . $entry)) { + // if include option was set, then only pass included files + if ($this->ignore[0]) { + if ($this->_checkIgnore($entry, $directory . '/' . $entry, 0)) { + continue; + } + } + // if ignore option was set, then only pass included files + if ($this->ignore[1]) { + if ($this->_checkIgnore($entry, $directory . '/' . $entry, 1)) { + continue; + } + } + $ret[$directory . DIRECTORY_SEPARATOR . $entry] = $dirname . $entry; + } + if (is_dir($directory . '/' . $entry)) { + $tmp = $this->dirList($directory . DIRECTORY_SEPARATOR . $entry, $toplevel); + if (is_array($tmp)) { + foreach($tmp as $i => $ent) { + $ret[$i] = $ent; + } + } + } + } + } + if ($d) { + $d->close(); + } + } else { + return false; + } + return $ret; + } + + /** + * Add a directory to the archive * * @param string $dir The directory path to add + * @param array files to ignore + * @param array files to include (all others ignored) * @return boolean */ - function addDir($dir) + function addDir($dir, $ignore = array(), $include = array()) { - // Not implemented yet! + $this->_setupIgnore($ignore, 1); + $this->_setupIgnore($include, 0); + $list = $this->dirList($dir); + return $this->addArray($list); } /** @@ -209,10 +403,10 @@ function addArray($files) { - $this->modified = true; - if (!$this->compress) { - $this->compress = $compress; + if (!is_array($files) || empty($files)) { + return false; } + $this->modified = true; foreach ($files as $file_path => $save_path) { $returns[] = $this->addFile($file_path, $save_path, $compress); }