CC-1695 Remove Campcaster Studio and make install easier
Fixed references to files for new directory structure for the backend stuff.
This commit is contained in:
parent
fe724b7814
commit
2ece374513
319 changed files with 75 additions and 357 deletions
628
3rd_party/php/pear/File/CSV.php
vendored
Normal file
628
3rd_party/php/pear/File/CSV.php
vendored
Normal file
|
@ -0,0 +1,628 @@
|
|||
<?php
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* File::CSV
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category File
|
||||
* @package File
|
||||
* @author Tomas V.V.Cox <cox@idecnet.com>
|
||||
* @author Helgi Þormar <dufuz@php.net>
|
||||
* @copyright 2004-2005 The Authors
|
||||
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
||||
* @version CVS: $Id: CSV.php,v 1.41 2007/05/20 12:25:14 dufuz Exp $
|
||||
* @link http://pear.php.net/package/File
|
||||
*/
|
||||
|
||||
require_once 'PEAR.php';
|
||||
require_once 'File.php';
|
||||
|
||||
/**
|
||||
* File class for handling CSV files (Comma Separated Values), a common format
|
||||
* for exchanging data.
|
||||
*
|
||||
* TODO:
|
||||
* - Usage example and Doc
|
||||
* - Use getPointer() in discoverFormat
|
||||
* - Add a line counter for being able to output better error reports
|
||||
* - Store the last error in GLOBALS and add File_CSV::getLastError()
|
||||
*
|
||||
* Wish:
|
||||
* - Other methods like readAll(), writeAll(), numFields(), numRows()
|
||||
* - Try to detect if a CSV has header or not in discoverFormat() (not possible with CSV)
|
||||
*
|
||||
* Known Bugs:
|
||||
* (they has been analyzed but for the moment the impact in the speed for
|
||||
* properly handle this uncommon cases is too high and won't be supported)
|
||||
* - A field which is composed only by a single quoted separator (ie -> ;";";)
|
||||
* is not handled properly
|
||||
* - When there is exactly one field minus than the expected number and there
|
||||
* is a field with a separator inside, the parser will throw the "wrong count" error
|
||||
*
|
||||
* Info about CSV and links to other sources
|
||||
* http://www.shaftek.org/publications/drafts/mime-csv/draft-shafranovich-mime-csv-00.html#appendix
|
||||
*
|
||||
* @author Tomas V.V.Cox <cox@idecnet.com>
|
||||
* @author Helgi Þormar <dufuz@php.net>
|
||||
* @package File
|
||||
*/
|
||||
class File_CSV
|
||||
{
|
||||
/**
|
||||
* This raiseError method works in a different way. It will always return
|
||||
* false (an error occurred) but it will call PEAR::raiseError() before
|
||||
* it. If no default PEAR global handler is set, will trigger an error.
|
||||
*
|
||||
* @param string $error The error message
|
||||
* @return bool always false
|
||||
*/
|
||||
function raiseError($error)
|
||||
{
|
||||
// If a default PEAR Error handler is not set trigger the error
|
||||
// XXX Add a PEAR::isSetHandler() method?
|
||||
if ($GLOBALS['_PEAR_default_error_mode'] == PEAR_ERROR_RETURN) {
|
||||
PEAR::raiseError($error, null, PEAR_ERROR_TRIGGER, E_USER_WARNING);
|
||||
} else {
|
||||
PEAR::raiseError($error);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the configuration given by the user
|
||||
*
|
||||
* @access private
|
||||
* @param string &$error The error will be written here if any
|
||||
* @param array &$conf The configuration assoc array
|
||||
* @return string error Returns a error message
|
||||
*/
|
||||
function _conf(&$error, &$conf)
|
||||
{
|
||||
// check conf
|
||||
if (!is_array($conf)) {
|
||||
return $error = 'Invalid configuration';
|
||||
}
|
||||
|
||||
if (!isset($conf['fields']) || !(int)$conf['fields']) {
|
||||
return $error = 'The number of fields must be numeric (the "fields" key)';
|
||||
}
|
||||
|
||||
if (isset($conf['sep'])) {
|
||||
if (strlen($conf['sep']) != 1) {
|
||||
return $error = 'Separator can only be one char';
|
||||
}
|
||||
} elseif ($conf['fields'] > 1) {
|
||||
return $error = 'Missing separator (the "sep" key)';
|
||||
}
|
||||
|
||||
if (isset($conf['quote'])) {
|
||||
if (strlen($conf['quote']) != 1) {
|
||||
return $error = 'The quote char must be one char (the "quote" key)';
|
||||
}
|
||||
} else {
|
||||
$conf['quote'] = null;
|
||||
}
|
||||
|
||||
if (!isset($conf['crlf'])) {
|
||||
$conf['crlf'] = "\n";
|
||||
}
|
||||
|
||||
if (!isset($conf['eol2unix'])) {
|
||||
$conf['eol2unix'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return or create the file descriptor associated with a file
|
||||
*
|
||||
* @param string $file The name of the file
|
||||
* @param array &$conf The configuration
|
||||
* @param string $mode The open node (ex: FILE_MODE_READ or FILE_MODE_WRITE)
|
||||
* @param boolean $reset if passed as true and resource for the file exists
|
||||
* than the file pointer will be moved to the beginning
|
||||
*
|
||||
* @return mixed A file resource or false
|
||||
*/
|
||||
function getPointer($file, &$conf, $mode = FILE_MODE_READ, $reset = false)
|
||||
{
|
||||
static $resources = array();
|
||||
static $config;
|
||||
if (isset($resources[$file][$mode])) {
|
||||
$conf = $config;
|
||||
if ($reset) {
|
||||
fseek($resources[$file][$mode], 0);
|
||||
}
|
||||
return $resources[$file][$mode];
|
||||
}
|
||||
File_CSV::_conf($error, $conf);
|
||||
if ($error) {
|
||||
return File_CSV::raiseError($error);
|
||||
}
|
||||
$config = $conf;
|
||||
PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
|
||||
$fp = File::_getFilePointer($file, $mode);
|
||||
PEAR::popErrorHandling();
|
||||
if (PEAR::isError($fp)) {
|
||||
return File_CSV::raiseError($fp);
|
||||
}
|
||||
$resources[$file][$mode] = $fp;
|
||||
return $fp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unquote data
|
||||
*
|
||||
* @param string $field The data to unquote
|
||||
* @param string $quote The quote char
|
||||
* @return string the unquoted data
|
||||
*/
|
||||
function unquote($field, $quote)
|
||||
{
|
||||
// Trim first the string.
|
||||
$field = trim($field);
|
||||
$quote = trim($quote);
|
||||
|
||||
// Incase null fields (form: ;;)
|
||||
if (!strlen($field)) {
|
||||
return $field;
|
||||
}
|
||||
|
||||
// excel compat
|
||||
if ($field[0] == '=' && $field[1] == '"') {
|
||||
$field = str_replace('="', '"', $field);
|
||||
}
|
||||
|
||||
$field_len = strlen($field);
|
||||
if ($quote && $field[0] == $quote && $field[$field_len - 1] == $quote) {
|
||||
// Get rid of escaping quotes
|
||||
$new = $prev = $c = '';
|
||||
for ($i = 0; $i < $field_len; ++$i) {
|
||||
$prev = $c;
|
||||
$c = $field[$i];
|
||||
// Deal with escaping quotes
|
||||
if ($c == $quote && $prev == $quote) {
|
||||
$c = '';
|
||||
}
|
||||
|
||||
$new .= $c;
|
||||
}
|
||||
$field = substr($new, 1, -1);
|
||||
}
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a row of data as an array from a CSV file. It's able to
|
||||
* read memo fields with multiline data.
|
||||
*
|
||||
* @param string $file The filename where to write the data
|
||||
* @param array &$conf The configuration of the dest CSV
|
||||
*
|
||||
* @return mixed Array with the data read or false on error/no more data
|
||||
*/
|
||||
function readQuoted($file, &$conf)
|
||||
{
|
||||
if (!$fp = File_CSV::getPointer($file, $conf, FILE_MODE_READ)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$buff = $old = $prev = $c = '';
|
||||
$ret = array();
|
||||
$i = 1;
|
||||
$in_quote = false;
|
||||
$quote = $conf['quote'];
|
||||
$f = $conf['fields'];
|
||||
$sep = $conf['sep'];
|
||||
while (false !== $ch = fgetc($fp)) {
|
||||
$old = $prev;
|
||||
$prev = $c;
|
||||
$c = $ch;
|
||||
|
||||
// Common case
|
||||
if ($c != $quote && $c != $sep && $c != "\n" && $c != "\r") {
|
||||
$buff .= $c;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start quote.
|
||||
if (
|
||||
$in_quote === false &&
|
||||
$quote && $c == $quote &&
|
||||
(
|
||||
$prev == $sep || $prev == "\n" || $prev === null ||
|
||||
$prev == "\r" || $prev == '' || $prev == ' '
|
||||
|| $prev == '=' //excel compat
|
||||
)
|
||||
) {
|
||||
$in_quote = true;
|
||||
// excel compat, removing the = part but only if we are in a quote
|
||||
if ($prev == '=') {
|
||||
$buff{strlen($buff) - 1} = '';
|
||||
}
|
||||
}
|
||||
|
||||
if ($in_quote) {
|
||||
|
||||
// When does the quote end, make sure it's not double quoted
|
||||
if ($c == $sep && $prev == $quote && $old != $quote) {
|
||||
$in_quote = false;
|
||||
} elseif ($c == $sep && $buff == $quote.$quote) {
|
||||
// In case we are dealing with double quote but empty value
|
||||
$in_quote = false;
|
||||
} elseif ($c == "\n" || $c == "\r") {
|
||||
$sub = ($prev == "\r") ? 2 : 1;
|
||||
$buff_len = strlen($buff);
|
||||
if (
|
||||
$buff_len >= $sub &&
|
||||
$buff[$buff_len - $sub] == $quote
|
||||
) {
|
||||
$in_quote = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$in_quote && ($c == $sep || $c == "\n" || $c == "\r") && $prev != '') {
|
||||
// More fields than expected
|
||||
if ($c == $sep && (count($ret) + 1) == $f) {
|
||||
// Seek the pointer into linebreak character.
|
||||
while (true) {
|
||||
$c = fgetc($fp);
|
||||
if ($c == "\n" || $c == "\r" || $c == '') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Insert last field value.
|
||||
$ret[] = File_CSV::unquote($buff, $quote);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
// Less fields than expected
|
||||
if (($c == "\n" || $c == "\r") && $i != $f) {
|
||||
// Insert last field value.
|
||||
$ret[] = File_CSV::unquote($buff, $quote);
|
||||
if (count($ret) == 1 && empty($ret[0])) {
|
||||
return array();
|
||||
}
|
||||
|
||||
// Pair the array elements to fields count. - inserting empty values
|
||||
$ret_count = count($ret);
|
||||
$sum = ($f - 1) - ($ret_count - 1);
|
||||
$data = array_merge($ret, array_fill($ret_count, $sum, ''));
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ($prev == "\r") {
|
||||
$buff = substr($buff, 0, -1);
|
||||
}
|
||||
|
||||
// Convert EOL character to Unix EOL (LF).
|
||||
if ($conf['eol2unix']) {
|
||||
$buff = preg_replace('/(\r\n|\r)$/', "\n", $buff);
|
||||
}
|
||||
|
||||
$ret[] = File_CSV::unquote($buff, $quote);
|
||||
if (count($ret) == $f) {
|
||||
return $ret;
|
||||
}
|
||||
$buff = '';
|
||||
++$i;
|
||||
continue;
|
||||
}
|
||||
$buff .= $c;
|
||||
}
|
||||
|
||||
/* If it's the end of the file and we still have something in buffer
|
||||
* then we process it since files can have no CL/FR at the end
|
||||
*/
|
||||
$feof = feof($fp);
|
||||
if ($feof && !in_array($buff, array("\r", "\n", "\r\n")) && strlen($buff) > 0) {
|
||||
$ret[] = File_CSV::unquote($buff, $quote);
|
||||
if (count($ret) == $f) {
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
|
||||
return !$feof ? $ret : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a "row" from a CSV file and return it as an array
|
||||
*
|
||||
* @param string $file The CSV file
|
||||
* @param array &$conf The configuration of the dest CSV
|
||||
*
|
||||
* @return mixed Array or false
|
||||
*/
|
||||
function read($file, &$conf)
|
||||
{
|
||||
static $headers = array();
|
||||
if (!$fp = File_CSV::getPointer($file, $conf, FILE_MODE_READ)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The size is limited to 4K
|
||||
if (!$line = fgets($fp, 4096)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fields = $conf['fields'] == 1 ? array($line) : explode($conf['sep'], $line);
|
||||
|
||||
$nl = array("\n", "\r", "\r\n");
|
||||
if (in_array($fields[count($fields) - 1], $nl)) {
|
||||
array_pop($fields);
|
||||
}
|
||||
|
||||
$field_count = count($fields);
|
||||
$last =& $fields[$field_count - 1];
|
||||
$len = strlen($last);
|
||||
if (
|
||||
$field_count != $conf['fields'] ||
|
||||
$conf['quote'] &&
|
||||
(
|
||||
$len !== 0 && $last[$len - 1] == "\n"
|
||||
&&
|
||||
(
|
||||
($last[0] == $conf['quote']
|
||||
&& $last[strlen(rtrim($last)) - 1] != $conf['quote'])
|
||||
||
|
||||
// excel support
|
||||
($last[0] == '=' && $last[1] == $conf['quote'])
|
||||
||
|
||||
// if the row has spaces before the quote
|
||||
preg_match('|^\s+'.preg_quote($conf['quote']) .'|Ums', $last, $match)
|
||||
)
|
||||
)
|
||||
// XXX perhaps there is a separator inside a quoted field
|
||||
//preg_match("|{$conf['quote']}.*{$conf['sep']}.*{$conf['quote']}|U", $line)
|
||||
) {
|
||||
fseek($fp, -1 * strlen($line), SEEK_CUR);
|
||||
return File_CSV::readQuoted($file, $conf);
|
||||
} else {
|
||||
foreach ($fields as $k => $v) {
|
||||
$fields[$k] = File_CSV::unquote($v, $conf['quote']);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($conf['header']) && empty($headers)) {
|
||||
// read the first row and assign to $headers
|
||||
$headers = $fields;
|
||||
return $headers;
|
||||
}
|
||||
|
||||
if ($field_count != $conf['fields']) {
|
||||
File_CSV::raiseError("Read wrong fields number count: '". $field_count .
|
||||
"' expected ".$conf['fields']);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!empty($headers)) {
|
||||
$tmp = array();
|
||||
foreach ($fields as $k => $v) {
|
||||
$tmp[$headers[$k]] = $v;
|
||||
}
|
||||
$fields = $tmp;
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal use only, will be removed in the future
|
||||
*
|
||||
* @param string $str The string to debug
|
||||
* @access private
|
||||
*/
|
||||
function _dbgBuff($str)
|
||||
{
|
||||
if (strpos($str, "\r") !== false) {
|
||||
$str = str_replace("\r", "_r_", $str);
|
||||
}
|
||||
if (strpos($str, "\n") !== false) {
|
||||
$str = str_replace("\n", "_n_", $str);
|
||||
}
|
||||
if (strpos($str, "\t") !== false) {
|
||||
$str = str_replace("\t", "_t_", $str);
|
||||
}
|
||||
if ($str === null) {
|
||||
$str = '_NULL_';
|
||||
}
|
||||
if ($str === '') {
|
||||
$str = 'Empty string';
|
||||
}
|
||||
echo "buff: ($str)\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a struc (array) in a file as CSV
|
||||
*
|
||||
* @param string $file The filename where to write the data
|
||||
* @param array $fields Ordered array with the data
|
||||
* @param array &$conf The configuration of the dest CSV
|
||||
*
|
||||
* @return bool True on success false otherwise
|
||||
*/
|
||||
function write($file, $fields, &$conf)
|
||||
{
|
||||
if (!$fp = File_CSV::getPointer($file, $conf, FILE_MODE_WRITE)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$field_count = count($fields);
|
||||
if ($field_count != $conf['fields']) {
|
||||
File_CSV::raiseError("Wrong fields number count: '". $field_count .
|
||||
"' expected ".$conf['fields']);
|
||||
return true;
|
||||
}
|
||||
|
||||
$write = '';
|
||||
for ($i = 0; $i < $field_count; ++$i) {
|
||||
// only quote if the field contains a sep
|
||||
if (!is_numeric($fields[$i]) && $conf['quote']
|
||||
&& isset($conf['sep']) && strpos($fields[$i], $conf['sep'])
|
||||
) {
|
||||
$write .= $conf['quote'] . $fields[$i] . $conf['quote'];
|
||||
} else {
|
||||
$write .= $fields[$i];
|
||||
}
|
||||
|
||||
$write .= ($i < ($field_count - 1)) ? $conf['sep']: $conf['crlf'];
|
||||
}
|
||||
|
||||
if (!fwrite($fp, $write, strlen($write))) {
|
||||
return File_CSV::raiseError('Can not write to file');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover the format of a CSV file (the number of fields, the separator
|
||||
* and if it quote string fields)
|
||||
*
|
||||
* @param string the CSV file name
|
||||
* @param array extra separators that should be checked for.
|
||||
* @return mixed Assoc array or false
|
||||
*/
|
||||
function discoverFormat($file, $extraSeps = array())
|
||||
{
|
||||
if (!$fp = @fopen($file, 'rb')) {
|
||||
return File_CSV::raiseError("Could not open file: $file");
|
||||
}
|
||||
|
||||
// Set auto detect line ending for Mac EOL support
|
||||
$oldini = ini_get('auto_detect_line_endings');
|
||||
if ($oldini != '1') {
|
||||
ini_set('auto_detect_line_endings', '1');
|
||||
}
|
||||
|
||||
// Take the first 30 lines and store the number of ocurrences
|
||||
// for each separator in each line
|
||||
$lines = '';
|
||||
for ($i = 0; $i < 30 && $line = fgets($fp, 4096); $i++) {
|
||||
$lines .= $line;
|
||||
}
|
||||
fclose($fp);
|
||||
|
||||
if ($oldini != '1') {
|
||||
ini_set('auto_detect_line_endings', $oldini);
|
||||
}
|
||||
|
||||
$seps = array("\t", ';', ':', ',');
|
||||
$seps = array_merge($seps, $extraSeps);
|
||||
$matches = array();
|
||||
$quotes = '"\'';
|
||||
|
||||
$lines = str_replace('""', '', $lines);
|
||||
while ($lines != ($newLines = preg_replace('|((["\'])[^"]*(\2))|', '\2_\2', $lines))){
|
||||
$lines = $newLines;
|
||||
}
|
||||
|
||||
$eol = strpos($lines, "\r") ? "\r" : "\n";
|
||||
$lines = explode($eol, $lines);
|
||||
foreach ($lines as $line) {
|
||||
$orgLine = $line;
|
||||
foreach ($seps as $sep) {
|
||||
$line = preg_replace("|^[^$quotes$sep]*$sep*([$quotes][^$quotes]*[$quotes])|sm", '_', $orgLine);
|
||||
// Find all seps that are within qoutes
|
||||
///FIXME ... counts legitimit lines as bad ones
|
||||
|
||||
// In case there's a whitespace infront the field
|
||||
$regex = '|\s*?';
|
||||
// Match the first quote (optional), also optionally match = since it's excel stuff
|
||||
$regex.= "(?:\=?[$quotes])";
|
||||
$regex.= '(.*';
|
||||
// Don't match a sep if we are inside a quote
|
||||
// also don't accept the sep if it has a quote on the either side
|
||||
///FIXME has to be possible if we are inside a quote! (tests fail because of this)
|
||||
$regex.= "(?:[^$quotes])$sep(?:[^$quotes])";
|
||||
$regex.= '.*)';
|
||||
// Close quote (if it's present) and the sep (optional, could be end of line)
|
||||
$regex.= "(?:[$quotes](?:$sep?))|Ums";
|
||||
preg_match_all($regex, $line, $match);
|
||||
// Finding all seps, within quotes or not
|
||||
$sep_count = substr_count($line, $sep);
|
||||
// Real count
|
||||
$matches[$sep][] = $sep_count - count($match[0]);
|
||||
}
|
||||
}
|
||||
|
||||
$final = array();
|
||||
// Group the results by amount of equal ocurrences
|
||||
foreach ($matches as $sep => $res) {
|
||||
$times = array();
|
||||
$times[0] = 0;
|
||||
foreach ($res as $k => $num) {
|
||||
if ($num > 0) {
|
||||
$times[$num] = (isset($times[$num])) ? $times[$num] + 1 : 1;
|
||||
}
|
||||
}
|
||||
arsort($times);
|
||||
|
||||
// Use max fields count.
|
||||
$fields[$sep] = max(array_flip($times));
|
||||
$amount[$sep] = $times[key($times)];
|
||||
}
|
||||
|
||||
arsort($amount);
|
||||
$sep = key($amount);
|
||||
|
||||
$conf['fields'] = $fields[$sep] + 1;
|
||||
$conf['sep'] = $sep;
|
||||
|
||||
// Test if there are fields with quotes around in the first 30 lines
|
||||
$quote = null;
|
||||
|
||||
$string = implode('', $lines);
|
||||
foreach (array('"', '\'') as $q) {
|
||||
if (preg_match_all("|$sep(?:\s*?)(\=?[$q]).*([$q])$sep|Us", $string, $match)) {
|
||||
if ($match[1][0] == $match[2][0]) {
|
||||
$quote = $match[1][0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
preg_match_all("|^(\=?[$q]).*([$q])$sep{0,1}|Ums", $string, $match)
|
||||
|| preg_match_all("|(\=?[$q]).*([$q])$sep\s$|Ums", $string, $match)
|
||||
) {
|
||||
if ($match[1][0] == $match[2][0]) {
|
||||
$quote = $match[1][0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$conf['quote'] = $quote;
|
||||
return $conf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Front to call getPointer and moving the resource to the
|
||||
* beginning of the file
|
||||
* Reset it if you like.
|
||||
*
|
||||
* @param string $file The name of the file
|
||||
* @param array &$conf The configuration
|
||||
* @param string $mode The open node (ex: FILE_MODE_READ or FILE_MODE_WRITE)
|
||||
*
|
||||
* @return boolean true on success false on failure
|
||||
*/
|
||||
function resetPointer($file, &$conf, $mode)
|
||||
{
|
||||
if (!File_CSV::getPointer($file, $conf, $mode, true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
485
3rd_party/php/pear/File/Find.php
vendored
Normal file
485
3rd_party/php/pear/File/Find.php
vendored
Normal file
|
@ -0,0 +1,485 @@
|
|||
<?php
|
||||
//
|
||||
// +----------------------------------------------------------------------+
|
||||
// | PHP Version 4 |
|
||||
// +----------------------------------------------------------------------+
|
||||
// | Copyright (c) 1997-2005 The PHP Group |
|
||||
// +----------------------------------------------------------------------+
|
||||
// | This source file is subject to version 2.02 of the PHP license, |
|
||||
// | that is bundled with this package in the file LICENSE, and is |
|
||||
// | available at through the world-wide-web at |
|
||||
// | http://www.php.net/license/2_02.txt. |
|
||||
// | If you did not receive a copy of the PHP license and are unable to |
|
||||
// | obtain it through the world-wide-web, please send a note to |
|
||||
// | license@php.net so we can mail you a copy immediately. |
|
||||
// +----------------------------------------------------------------------+
|
||||
// | Author: Sterling Hughes <sterling@php.net> |
|
||||
// +----------------------------------------------------------------------+
|
||||
//
|
||||
// $Id: Find.php,v 1.27 2006/06/30 14:06:16 techtonik Exp $
|
||||
//
|
||||
|
||||
require_once 'PEAR.php';
|
||||
|
||||
define('FILE_FIND_VERSION', '@package_version@');
|
||||
|
||||
// to debug uncomment this string
|
||||
// define('FILE_FIND_DEBUG', '');
|
||||
|
||||
/**
|
||||
* Commonly needed functions searching directory trees
|
||||
*
|
||||
* @access public
|
||||
* @version $Id: Find.php,v 1.27 2006/06/30 14:06:16 techtonik Exp $
|
||||
* @package File
|
||||
* @author Sterling Hughes <sterling@php.net>
|
||||
*/
|
||||
class File_Find
|
||||
{
|
||||
/**
|
||||
* internal dir-list
|
||||
* @var array
|
||||
*/
|
||||
var $_dirs = array();
|
||||
|
||||
/**
|
||||
* directory separator
|
||||
* @var string
|
||||
*/
|
||||
var $dirsep = "/";
|
||||
|
||||
/**
|
||||
* found files
|
||||
* @var array
|
||||
*/
|
||||
var $files = array();
|
||||
|
||||
/**
|
||||
* found dirs
|
||||
* @var array
|
||||
*/
|
||||
var $directories = array();
|
||||
|
||||
/**
|
||||
* Search specified directory to find matches for specified pattern
|
||||
*
|
||||
* @param string $pattern a string containing the pattern to search
|
||||
* the directory for.
|
||||
*
|
||||
* @param string $dirpath a string containing the directory path
|
||||
* to search.
|
||||
*
|
||||
* @param string $pattern_type a string containing the type of
|
||||
* pattern matching functions to use (can either be 'php',
|
||||
* 'perl' or 'shell').
|
||||
*
|
||||
* @return array containing all of the files and directories
|
||||
* matching the pattern or null if no matches
|
||||
*
|
||||
* @author Sterling Hughes <sterling@php.net>
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
function &glob($pattern, $dirpath, $pattern_type = 'php')
|
||||
{
|
||||
$dh = @opendir($dirpath);
|
||||
|
||||
if (!$dh) {
|
||||
$pe = PEAR::raiseError("Cannot open directory $dirpath");
|
||||
return $pe;
|
||||
}
|
||||
|
||||
$match_function = File_Find::_determineRegex($pattern, $pattern_type);
|
||||
$matches = array();
|
||||
|
||||
// empty string cannot be specified for 'php' and 'perl' pattern
|
||||
if ($pattern || ($pattern_type != 'php' && $pattern_type != 'perl')) {
|
||||
while (false !== ($entry = @readdir($dh))) {
|
||||
if ($match_function($pattern, $entry) &&
|
||||
$entry != '.' && $entry != '..') {
|
||||
$matches[] = $entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@closedir($dh);
|
||||
|
||||
if (0 == count($matches)) {
|
||||
$matches = null;
|
||||
}
|
||||
|
||||
return $matches ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the directory tree given by the directory_path parameter.
|
||||
*
|
||||
* @param string $directory contains the directory path that you
|
||||
* want to map.
|
||||
*
|
||||
* @return array a two element array, the first element containing a list
|
||||
* of all the directories, the second element containing a list of all the
|
||||
* files.
|
||||
*
|
||||
* @author Sterling Hughes <sterling@php.net>
|
||||
* @access public
|
||||
*/
|
||||
function &maptree($directory)
|
||||
{
|
||||
|
||||
/* if called statically */
|
||||
if (!isset($this) || !is_a($this, "File_Find")) {
|
||||
$obj = &new File_Find();
|
||||
return $obj->maptree($directory);
|
||||
}
|
||||
|
||||
/* clear the results just in case */
|
||||
$this->files = array();
|
||||
$this->directories = array();
|
||||
|
||||
/* strip out trailing slashes */
|
||||
$directory = preg_replace('![\\\\/]+$!', '', $directory);
|
||||
|
||||
$this->_dirs = array($directory);
|
||||
|
||||
while (count($this->_dirs)) {
|
||||
$dir = array_pop($this->_dirs);
|
||||
File_Find::_build($dir, $this->dirsep);
|
||||
array_push($this->directories, $dir);
|
||||
}
|
||||
|
||||
$retval = array($this->directories, $this->files);
|
||||
return $retval;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the directory tree given by the directory parameter.
|
||||
*
|
||||
* @param string $directory contains the directory path that you
|
||||
* want to map.
|
||||
* @param integer $maxrecursion maximun number of folders to recursive
|
||||
* map
|
||||
*
|
||||
* @return array a multidimensional array containing all subdirectories
|
||||
* and their files. For example:
|
||||
*
|
||||
* Array
|
||||
* (
|
||||
* [0] => file_1.php
|
||||
* [1] => file_2.php
|
||||
* [subdirname] => Array
|
||||
* (
|
||||
* [0] => file_1.php
|
||||
* )
|
||||
* )
|
||||
*
|
||||
* @author Mika Tuupola <tuupola@appelsiini.net>
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
function &mapTreeMultiple($directory, $maxrecursion = 0, $count = 0)
|
||||
{
|
||||
$retval = array();
|
||||
|
||||
$count++;
|
||||
|
||||
/* strip trailing slashes */
|
||||
$directory = preg_replace('![\\\\/]+$!', '', $directory);
|
||||
|
||||
if (is_readable($directory)) {
|
||||
$dh = opendir($directory);
|
||||
while (false !== ($entry = @readdir($dh))) {
|
||||
if ($entry != '.' && $entry != '..') {
|
||||
array_push($retval, $entry);
|
||||
}
|
||||
}
|
||||
closedir($dh);
|
||||
}
|
||||
|
||||
while (list($key, $val) = each($retval)) {
|
||||
$path = $directory . "/" . $val;
|
||||
|
||||
if (!is_array($val) && is_dir($path)) {
|
||||
unset($retval[$key]);
|
||||
if ($maxrecursion == 0 || $count < $maxrecursion) {
|
||||
$retval[$val] = &File_Find::mapTreeMultiple($path,
|
||||
$maxrecursion, $count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the specified directory tree with the specified pattern. Return
|
||||
* an array containing all matching files (no directories included).
|
||||
*
|
||||
* @param string $pattern the pattern to match every file with.
|
||||
*
|
||||
* @param string $directory the directory tree to search in.
|
||||
*
|
||||
* @param string $type the type of regular expression support to use, either
|
||||
* 'php', 'perl' or 'shell'.
|
||||
*
|
||||
* @param bool $fullpath whether the regex should be matched against the
|
||||
* full path or only against the filename
|
||||
*
|
||||
* @param string $match can be either 'files', 'dirs' or 'both' to specify
|
||||
* the kind of list to return
|
||||
*
|
||||
* @return array a list of files matching the pattern parameter in the the
|
||||
* directory path specified by the directory parameter
|
||||
*
|
||||
* @author Sterling Hughes <sterling@php.net>
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
function &search($pattern, $directory, $type = 'php', $fullpath = true, $match = 'files')
|
||||
{
|
||||
|
||||
$matches = array();
|
||||
list ($directories,$files) = File_Find::maptree($directory);
|
||||
switch($match) {
|
||||
case 'directories':
|
||||
$data = $directories;
|
||||
break;
|
||||
case 'both':
|
||||
$data = array_merge($directories, $files);
|
||||
break;
|
||||
case 'files':
|
||||
default:
|
||||
$data = $files;
|
||||
}
|
||||
unset($files, $directories);
|
||||
|
||||
$match_function = File_Find::_determineRegex($pattern, $type);
|
||||
|
||||
reset($data);
|
||||
// check if empty string given (ok for 'shell' method, but bad for others)
|
||||
if ($pattern || ($type != 'php' && $type != 'perl')) {
|
||||
while (list(,$entry) = each($data)) {
|
||||
if ($match_function($pattern,
|
||||
$fullpath ? $entry : basename($entry))) {
|
||||
$matches[] = $entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether or not a variable is a PEAR error
|
||||
*
|
||||
* @param object PEAR_Error $var the variable to test.
|
||||
*
|
||||
* @return boolean returns true if the variable is a PEAR error, otherwise
|
||||
* it returns false.
|
||||
* @access public
|
||||
*/
|
||||
function isError(&$var)
|
||||
{
|
||||
return PEAR::isError($var);
|
||||
}
|
||||
|
||||
/**
|
||||
* internal function to build singular directory trees, used by
|
||||
* File_Find::maptree()
|
||||
*
|
||||
* @param string $directory name of the directory to read
|
||||
* @param string $separator directory separator
|
||||
* @return void
|
||||
*/
|
||||
function _build($directory, $separator = "/")
|
||||
{
|
||||
|
||||
$dh = @opendir($directory);
|
||||
|
||||
if (!$dh) {
|
||||
$pe = PEAR::raiseError("Cannot open directory");
|
||||
return $pe;
|
||||
}
|
||||
|
||||
while (false !== ($entry = @readdir($dh))) {
|
||||
if ($entry != '.' && $entry != '..') {
|
||||
|
||||
$entry = $directory.$separator.$entry;
|
||||
|
||||
if (is_dir($entry)) {
|
||||
array_push($this->_dirs, $entry);
|
||||
} else {
|
||||
array_push($this->files, $entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@closedir($dh);
|
||||
}
|
||||
|
||||
/**
|
||||
* internal function to determine the type of regular expression to
|
||||
* use, implemented by File_Find::glob() and File_Find::search()
|
||||
*
|
||||
* @param string $type given RegExp type
|
||||
* @return string kind of function ( "eregi", "ereg" or "preg_match") ;
|
||||
*
|
||||
*/
|
||||
function _determineRegex($pattern, $type)
|
||||
{
|
||||
if (!strcasecmp($type, 'shell')) {
|
||||
$match_function = 'File_Find_match_shell';
|
||||
} else if (!strcasecmp($type, 'perl')) {
|
||||
$match_function = 'preg_match';
|
||||
} else if (!strcasecmp(substr($pattern, -2), '/i')) {
|
||||
$match_function = 'eregi';
|
||||
} else {
|
||||
$match_function = 'ereg';
|
||||
}
|
||||
return $match_function;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Package method to match via 'shell' pattern. Provided in global
|
||||
* scope, because it should be called like 'preg_match' and 'eregi'
|
||||
* and can be easily copied into other packages
|
||||
*
|
||||
* @author techtonik <techtonik@php.net>
|
||||
* @return mixed bool on success and PEAR_Error on failure
|
||||
*/
|
||||
function File_Find_match_shell($pattern, $filename)
|
||||
{
|
||||
// {{{ convert pattern to positive and negative regexps
|
||||
$positive = $pattern;
|
||||
$negation = substr_count($pattern, "|");
|
||||
|
||||
if ($negation > 1) {
|
||||
PEAR::raiseError("Mask string contains errors!");
|
||||
return FALSE;
|
||||
} elseif ($negation) {
|
||||
list($positive, $negative) = explode("|", $pattern);
|
||||
if (strlen($negative) == 0) {
|
||||
PEAR::raiseError("File-mask string contains errors!");
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
$positive = _File_Find_match_shell_get_pattern($positive);
|
||||
if ($negation) {
|
||||
$negative = _File_Find_match_shell_get_pattern($negative);
|
||||
}
|
||||
// }}} convert end
|
||||
|
||||
|
||||
if (defined("FILE_FIND_DEBUG")) {
|
||||
print("Method: $type\nPattern: $pattern\n Converted pattern:");
|
||||
print_r($positive);
|
||||
if (isset($negative)) print_r($negative);
|
||||
}
|
||||
|
||||
if (!preg_match($positive, $filename)) {
|
||||
return FALSE;
|
||||
} else {
|
||||
if (isset($negative)
|
||||
&& preg_match($negative, $filename)) {
|
||||
return FALSE;
|
||||
} else {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* function used by File_Find_match_shell to convert 'shell' mask
|
||||
* into pcre regexp. Some of the rules (see testcases for more):
|
||||
* escaping all special chars and replacing
|
||||
* . with \.
|
||||
* * with .*
|
||||
* ? with .{1}
|
||||
* also adding ^ and $ as the pattern matches whole filename
|
||||
*
|
||||
* @author techtonik <techtonik@php.net>
|
||||
* @return string pcre regexp for preg_match
|
||||
*/
|
||||
function _File_Find_match_shell_get_pattern($mask) {
|
||||
// get array of several masks (if any) delimited by comma
|
||||
// do not touch commas in char class
|
||||
$premasks = preg_split("|(\[[^\]]+\])|", $mask, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
|
||||
if (defined("FILE_FIND_DEBUG")) {
|
||||
print("\nPremask: ");
|
||||
print_r($premasks);
|
||||
}
|
||||
$pi = 0;
|
||||
foreach($premasks as $pm) {
|
||||
if (!isset($masks[$pi])) $masks[$pi] = "";
|
||||
if ($pm{0} == '[' && $pm{strlen($pm)-1} == ']') {
|
||||
// strip commas from character class
|
||||
$masks[$pi] .= str_replace(",", "", $pm);
|
||||
} else {
|
||||
$tarr = explode(",", $pm);
|
||||
if (sizeof($tarr) == 1) {
|
||||
$masks[$pi] .= $pm;
|
||||
} else {
|
||||
foreach ($tarr as $te) {
|
||||
$masks[$pi++] .= $te;
|
||||
$masks[$pi] = "";
|
||||
}
|
||||
unset($masks[$pi--]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if empty string given return *.* pattern
|
||||
if (strlen($mask) == 0) return "!^.*$!";
|
||||
|
||||
// convert to preg regexp
|
||||
$regexmask = implode("|", $masks);
|
||||
if (defined("FILE_FIND_DEBUG")) {
|
||||
print("regexMask step one(implode): $regexmask");
|
||||
}
|
||||
$regexmask = addcslashes($regexmask, '^$}!{)(\/.+');
|
||||
if (defined("FILE_FIND_DEBUG")) {
|
||||
print("\nregexMask step two(addcslashes): $regexmask");
|
||||
}
|
||||
$regexmask = preg_replace("!(\*|\?)!", ".$1", $regexmask);
|
||||
if (defined("FILE_FIND_DEBUG")) {
|
||||
print("\nregexMask step three(* ? -> .* .?): $regexmask");
|
||||
}
|
||||
// a special case '*.' at the end means that there is no extension
|
||||
$regexmask = preg_replace("!\.\*\\\.(\||$)!", "[^\.]*$1", $regexmask);
|
||||
// it is impossible to have dot at the end of filename
|
||||
$regexmask = preg_replace("!\\\.(\||$)!", "$1", $regexmask);
|
||||
// and .* at the end also means that there could be nothing at all
|
||||
// (i.e. no dot at the end also)
|
||||
$regexmask = preg_replace("!\\\.\.\*(\||$)!", "(\\\\..*)?$1", $regexmask);
|
||||
if (defined("FILE_FIND_DEBUG")) {
|
||||
print("\nregexMask step two and half(*.$ \\..*$ .$ -> [^.]*$ .?.* $): $regexmask");
|
||||
}
|
||||
// if no extension supplied - add .* to match partially from filename start
|
||||
if (strpos($regexmask, "\\.") === FALSE) $regexmask .= ".*";
|
||||
|
||||
// file mask match whole name - adding restrictions
|
||||
$regexmask = preg_replace("!(\|)!", '^'."$1".'$', $regexmask);
|
||||
$regexmask = '^'.$regexmask.'$';
|
||||
if (defined("FILE_FIND_DEBUG")) {
|
||||
print("\nregexMask step three(^ and $ to match whole name): $regexmask");
|
||||
}
|
||||
// wrap regex into ! since all ! are already escaped
|
||||
$regexmask = "!$regexmask!i";
|
||||
if (defined("FILE_FIND_DEBUG")) {
|
||||
print("\nWrapped regex: $regexmask\n");
|
||||
}
|
||||
return $regexmask;
|
||||
}
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* tab-width: 4
|
||||
* c-basic-offset: 4
|
||||
* End:
|
||||
*/
|
||||
|
||||
?>
|
482
3rd_party/php/pear/File/Util.php
vendored
Normal file
482
3rd_party/php/pear/File/Util.php
vendored
Normal file
|
@ -0,0 +1,482 @@
|
|||
<?php
|
||||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
||||
|
||||
/**
|
||||
* File::Util
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category File
|
||||
* @package File
|
||||
* @author Michael Wallner <mike@php.net>
|
||||
* @copyright 2004-2005 Michael Wallner
|
||||
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
||||
* @version CVS: $Id: Util.php,v 1.25 2007/02/20 14:19:08 mike Exp $
|
||||
* @link http://pear.php.net/package/File
|
||||
*/
|
||||
|
||||
/**#@+
|
||||
* Sorting Constants
|
||||
*/
|
||||
define('FILE_SORT_NONE', 0);
|
||||
define('FILE_SORT_REVERSE', 1);
|
||||
define('FILE_SORT_NAME', 2);
|
||||
define('FILE_SORT_SIZE', 4);
|
||||
define('FILE_SORT_DATE', 8);
|
||||
define('FILE_SORT_RANDOM', 16);
|
||||
/**#@-*/
|
||||
|
||||
/**#@+
|
||||
* Listing Constants
|
||||
*/
|
||||
define('FILE_LIST_FILES', 1);
|
||||
define('FILE_LIST_DIRS', 2);
|
||||
define('FILE_LIST_DOTS', 4);
|
||||
define('FILE_LIST_ALL', FILE_LIST_FILES | FILE_LIST_DIRS | FILE_LIST_DOTS);
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
define('FILE_WIN32', defined('OS_WINDOWS') ? OS_WINDOWS : !strncasecmp(PHP_OS, 'win', 3));
|
||||
|
||||
/**
|
||||
* File_Util
|
||||
*
|
||||
* File and directory utility functions.
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
class File_Util
|
||||
{
|
||||
/**
|
||||
* Returns a string path built from the array $pathParts. Where a join
|
||||
* occurs multiple separators are removed. Joins using the optional
|
||||
* separator, defaulting to the PHP DIRECTORY_SEPARATOR constant.
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @param array $parts Array containing the parts to be joined
|
||||
* @param string $separator The directory seperator
|
||||
*/
|
||||
function buildPath($parts, $separator = DIRECTORY_SEPARATOR)
|
||||
{
|
||||
$qs = '/^'. preg_quote($separator, '/') .'+$/';
|
||||
for ($i = 0, $c = count($parts); $i < $c; $i++) {
|
||||
if (!strlen($parts[$i]) || preg_match($qs, $parts[$i])) {
|
||||
unset($parts[$i]);
|
||||
} elseif (0 == $i) {
|
||||
$parts[$i] = rtrim($parts[$i], $separator);
|
||||
} elseif ($c - 1 == $i) {
|
||||
$parts[$i] = ltrim($parts[$i], $separator);
|
||||
} else {
|
||||
$parts[$i] = trim($parts[$i], $separator);
|
||||
}
|
||||
}
|
||||
return implode($separator, $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a path without leading / or C:\. If this is not
|
||||
* present the path is returned as is.
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @param string $path The path to be processed
|
||||
* @return string The processed path or the path as is
|
||||
*/
|
||||
function skipRoot($path)
|
||||
{
|
||||
if (File_Util::isAbsolute($path)) {
|
||||
if (FILE_WIN32) {
|
||||
return substr($path, $path{3} == '\\' ? 4 : 3);
|
||||
}
|
||||
return ltrim($path, '/');
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the temp directory according to either the TMP, TMPDIR, or
|
||||
* TEMP env variables. If these are not set it will also check for the
|
||||
* existence of /tmp, %WINDIR%\temp
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return string The system tmp directory
|
||||
*/
|
||||
function tmpDir()
|
||||
{
|
||||
if (FILE_WIN32) {
|
||||
if (isset($_ENV['TEMP'])) {
|
||||
return $_ENV['TEMP'];
|
||||
}
|
||||
if (isset($_ENV['TMP'])) {
|
||||
return $_ENV['TMP'];
|
||||
}
|
||||
if (isset($_ENV['windir'])) {
|
||||
return $_ENV['windir'] . '\\temp';
|
||||
}
|
||||
if (isset($_ENV['SystemRoot'])) {
|
||||
return $_ENV['SystemRoot'] . '\\temp';
|
||||
}
|
||||
if (isset($_SERVER['TEMP'])) {
|
||||
return $_SERVER['TEMP'];
|
||||
}
|
||||
if (isset($_SERVER['TMP'])) {
|
||||
return $_SERVER['TMP'];
|
||||
}
|
||||
if (isset($_SERVER['windir'])) {
|
||||
return $_SERVER['windir'] . '\\temp';
|
||||
}
|
||||
if (isset($_SERVER['SystemRoot'])) {
|
||||
return $_SERVER['SystemRoot'] . '\\temp';
|
||||
}
|
||||
return '\temp';
|
||||
}
|
||||
if (isset($_ENV['TMPDIR'])) {
|
||||
return $_ENV['TMPDIR'];
|
||||
}
|
||||
if (isset($_SERVER['TMPDIR'])) {
|
||||
return $_SERVER['TMPDIR'];
|
||||
}
|
||||
return '/tmp';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a temporary filename using tempnam() and File::tmpDir().
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @param string $dirname Optional directory name for the tmp file
|
||||
* @return string Filename and path of the tmp file
|
||||
*/
|
||||
function tmpFile($dirname = null)
|
||||
{
|
||||
if (!isset($dirname)) {
|
||||
$dirname = File_Util::tmpDir();
|
||||
}
|
||||
return tempnam($dirname, 'temp.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns boolean based on whether given path is absolute or not.
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @param string $path Given path
|
||||
* @return boolean True if the path is absolute, false if it is not
|
||||
*/
|
||||
function isAbsolute($path)
|
||||
{
|
||||
if (preg_match('/(?:\/|\\\)\.\.(?=\/|$)/', $path)) {
|
||||
return false;
|
||||
}
|
||||
if (FILE_WIN32) {
|
||||
return preg_match('/^[a-zA-Z]:(\\\|\/)/', $path);
|
||||
}
|
||||
return ($path{0} == '/') || ($path{0} == '~');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a file's existence, taking the current include path
|
||||
* into consideration
|
||||
*
|
||||
* This method can be called statically
|
||||
* (e.g., File_Util::isIncludable('config.php'))
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $sep the directory separator (optional)
|
||||
* @return string the includable path
|
||||
* @access public
|
||||
* @static
|
||||
*/
|
||||
function isIncludable($file, $sep = DIRECTORY_SEPARATOR)
|
||||
{
|
||||
foreach ((array) explode(PATH_SEPARATOR, ini_get('include_path')) as $path) {
|
||||
if (file_exists($path .= $sep . $file)) {
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
if (file_exists($file)) {
|
||||
return $file;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get path relative to another path
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return string
|
||||
* @param string $path
|
||||
* @param string $root
|
||||
* @param string $separator
|
||||
*/
|
||||
function relativePath($path, $root, $separator = DIRECTORY_SEPARATOR)
|
||||
{
|
||||
$path = File_Util::realpath($path, $separator);
|
||||
$root = File_Util::realpath($root, $separator);
|
||||
$dirs = explode($separator, $path);
|
||||
$comp = explode($separator, $root);
|
||||
|
||||
if (FILE_WIN32) {
|
||||
if (strcasecmp($dirs[0], $comp[0])) {
|
||||
return $path;
|
||||
}
|
||||
unset($dirs[0], $comp[0]);
|
||||
}
|
||||
|
||||
foreach ($comp as $i => $part) {
|
||||
if (isset($dirs[$i]) && $part == $dirs[$i]) {
|
||||
unset($dirs[$i], $comp[$i]);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return str_repeat('..' . $separator, count($comp)) . implode($separator, $dirs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get real path (works with non-existant paths)
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return string
|
||||
* @param string $path
|
||||
* @param string $separator
|
||||
*/
|
||||
function realPath($path, $separator = DIRECTORY_SEPARATOR)
|
||||
{
|
||||
if (!strlen($path)) {
|
||||
return $separator;
|
||||
}
|
||||
|
||||
$drive = '';
|
||||
if (FILE_WIN32) {
|
||||
$path = preg_replace('/[\\\\\/]/', $separator, $path);
|
||||
if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) {
|
||||
$drive = $matches[1];
|
||||
$path = $matches[2];
|
||||
} else {
|
||||
$cwd = getcwd();
|
||||
$drive = substr($cwd, 0, 2);
|
||||
if ($path{0} !== $separator{0}) {
|
||||
$path = substr($cwd, 3) . $separator . $path;
|
||||
}
|
||||
}
|
||||
} elseif ($path{0} !== $separator) {
|
||||
$path = getcwd() . $separator . $path;
|
||||
}
|
||||
|
||||
$dirStack = array();
|
||||
foreach (explode($separator, $path) as $dir) {
|
||||
if (strlen($dir) && $dir !== '.') {
|
||||
if ($dir == '..') {
|
||||
array_pop($dirStack);
|
||||
} else {
|
||||
$dirStack[] = $dir;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $drive . $separator . implode($separator, $dirStack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether path is in root path
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return bool
|
||||
* @param string $path
|
||||
* @param string $root
|
||||
*/
|
||||
function pathInRoot($path, $root)
|
||||
{
|
||||
static $realPaths = array();
|
||||
|
||||
if (!isset($realPaths[$root])) {
|
||||
$realPaths[$root] = File_Util::realPath($root);
|
||||
}
|
||||
|
||||
return false !== strstr(File_Util::realPath($path), $realPaths[$root]);
|
||||
}
|
||||
|
||||
/**
|
||||
* List Directory
|
||||
*
|
||||
* The final argument, $cb, is a callback that either evaluates to true or
|
||||
* false and performs a filter operation, or it can also modify the
|
||||
* directory/file names returned. To achieve the latter effect use as
|
||||
* follows:
|
||||
*
|
||||
* <code>
|
||||
* <?php
|
||||
* function uc(&$filename) {
|
||||
* $filename = strtoupper($filename);
|
||||
* return true;
|
||||
* }
|
||||
* $entries = File_Util::listDir('.', FILE_LIST_ALL, FILE_SORT_NONE, 'uc');
|
||||
* foreach ($entries as $e) {
|
||||
* echo $e->name, "\n";
|
||||
* }
|
||||
* ?>
|
||||
* </code>
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return array
|
||||
* @param string $path
|
||||
* @param int $list
|
||||
* @param int $sort
|
||||
* @param mixed $cb
|
||||
*/
|
||||
function listDir($path, $list = FILE_LIST_ALL, $sort = FILE_SORT_NONE, $cb = null)
|
||||
{
|
||||
if (!strlen($path) || !is_dir($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$entries = array();
|
||||
for ($dir = dir($path); false !== $entry = $dir->read(); ) {
|
||||
if ($list & FILE_LIST_DOTS || $entry{0} !== '.') {
|
||||
$isRef = ($entry === '.' || $entry === '..');
|
||||
$isDir = $isRef || is_dir($path .'/'. $entry);
|
||||
if ( ((!$isDir && $list & FILE_LIST_FILES) ||
|
||||
($isDir && $list & FILE_LIST_DIRS)) &&
|
||||
(!is_callable($cb) ||
|
||||
call_user_func_array($cb, array(&$entry)))) {
|
||||
$entries[] = (object) array(
|
||||
'name' => $entry,
|
||||
'size' => $isDir ? null : filesize($path .'/'. $entry),
|
||||
'date' => filemtime($path .'/'. $entry),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
$dir->close();
|
||||
|
||||
if ($sort) {
|
||||
$entries = File_Util::sortFiles($entries, $sort);
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort Files
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return array
|
||||
* @param array $files
|
||||
* @param int $sort
|
||||
*/
|
||||
function sortFiles($files, $sort)
|
||||
{
|
||||
if (!$files) {
|
||||
return array();
|
||||
}
|
||||
|
||||
if (!$sort) {
|
||||
return $files;
|
||||
}
|
||||
|
||||
if ($sort === 1) {
|
||||
return array_reverse($files);
|
||||
}
|
||||
|
||||
if ($sort & FILE_SORT_RANDOM) {
|
||||
shuffle($files);
|
||||
return $files;
|
||||
}
|
||||
|
||||
$names = array();
|
||||
$sizes = array();
|
||||
$dates = array();
|
||||
|
||||
if ($sort & FILE_SORT_NAME) {
|
||||
$r = &$names;
|
||||
} elseif ($sort & FILE_SORT_DATE) {
|
||||
$r = &$dates;
|
||||
} elseif ($sort & FILE_SORT_SIZE) {
|
||||
$r = &$sizes;
|
||||
} else {
|
||||
asort($files, SORT_REGULAR);
|
||||
return $files;
|
||||
}
|
||||
|
||||
$sortFlags = array(
|
||||
FILE_SORT_NAME => SORT_STRING,
|
||||
FILE_SORT_DATE => SORT_NUMERIC,
|
||||
FILE_SORT_SIZE => SORT_NUMERIC,
|
||||
);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$names[] = $file->name;
|
||||
$sizes[] = $file->size;
|
||||
$dates[] = $file->date;
|
||||
}
|
||||
|
||||
if ($sort & FILE_SORT_REVERSE) {
|
||||
arsort($r, $sortFlags[$sort & ~1]);
|
||||
} else {
|
||||
asort($r, $sortFlags[$sort]);
|
||||
}
|
||||
|
||||
$result = array();
|
||||
foreach ($r as $i => $f) {
|
||||
$result[] = $files[$i];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch File Extension
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return string|array
|
||||
* @param string|array $filename
|
||||
* @param string $to new file extension
|
||||
* @param string $from change only files with this extension
|
||||
* @param bool $reverse change only files not having $from extension
|
||||
*/
|
||||
function switchExt($filename, $to, $from = null, $reverse = false)
|
||||
{
|
||||
if (is_array($filename)) {
|
||||
foreach ($filename as $key => $file) {
|
||||
$filename[$key] = File_Util::switchExt($file, $to, $from);
|
||||
}
|
||||
return $filename;
|
||||
}
|
||||
|
||||
if ($len = strlen($from)) {
|
||||
$ext = substr($filename, -$len - 1);
|
||||
$cfn = FILE_WIN32 ? 'strcasecmp' : 'strcmp';
|
||||
if (!$reverse == $cfn($ext, '.'. $from)) {
|
||||
return $filename;
|
||||
}
|
||||
return substr($filename, 0, -$len - 1) .'.'. $to;
|
||||
}
|
||||
|
||||
if ($pos = strpos($filename, '.')) {
|
||||
return substr($filename, 0, $pos) .'.'. $to;
|
||||
}
|
||||
|
||||
return $filename .'.'. $to;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue