Initial Commit

This commit is contained in:
Riley Schneider
2025-12-03 16:38:10 +01:00
parent c5e26bf594
commit b732d8d4b5
17680 changed files with 5977495 additions and 2 deletions

View File

@@ -0,0 +1,744 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::CSV
*
* PHP versions 4 and 5
*
* Copyright (c) 2002-2008,
* Tomas V.V.Cox <cox@idecnet.com>,
* Helgi Þormar Þorbjörnsson <helgi@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category File
* @package File
* @author Tomas V.V.Cox <cox@idecnet.com>
* @author Helgi Þormar Þorbjörnsson <helgi@php.net>
* @copyright 2004-2011 The Authors
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: CSV.php 309245 2011-03-15 02:02:41Z dufuz $
* @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://rfc.net/rfc4180.html
*
* @author Tomas V.V.Cox <cox@idecnet.com>
* @author Helgi Þormar Þorbjörnsson <helgi@php.net>
* @package File
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
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)';
} else {
// to avoid undefined index notices
$conf['sep'] = ',';
}
if (isset($conf['quote'])) {
if (strlen($conf['quote']) !== 1) {
return $error = 'The quote char must be one char (the "quote" key)';
}
} else {
$conf['quote'] = '"';
}
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();
if (isset($resources[$file][$mode])) {
if ($reset) {
fseek($resources[$file][$mode], 0);
}
return $resources[$file][$mode];
}
File_CSV::_conf($error, $conf);
if ($error) {
return File_CSV::raiseError($error);
}
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 array|string $data The data to unquote
* @param string $quote The quote char
* @return string the unquoted data
*/
function unquote($data, $quote)
{
$isString = false;
if (!is_array($data)) {
$data = array($data);
$isString = true;
}
// Get rid of escaped quotes
$data = str_replace($quote.$quote, $quote, $data);
$tmp = array();
foreach ($data as $key => $field) {
// Trim first the string.
$field = trim($field);
// Incase null fields (form: ;;)
$field_len = strlen($field);
if (!$field_len) {
if ($isString) {
return $field;
}
$tmp[$key] = $field;
continue;
}
// excel compat
if ($field[0] === '=' && $field[1] === '"') {
$field = str_replace('="', '"', $field);
--$field_len;
}
if ($field[0] === $quote && $field[$field_len - 1] === $quote) {
// Get rid of quotes around the field
$field = substr($field, 1, -1);
}
if ($isString) {
$tmp = $field;
} else {
$tmp[$key] = $field;
}
}
return $tmp;
}
/**
* 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();
$fields = 1;
$in_quote = false;
$quote = $conf['quote'];
$f = (int)$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")) {
$return = File_CSV::_readQuotedFillers($fp, $f, $fields, $ret,
$buff, $quote, $c, $sep);
if ($return !== false) {
return $return;
}
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);
// Below replaces things everywhere not just EOL
//$buff = str_replace(array("\r\n", "\r"), "\n", $buff);
}
$ret[] = File_CSV::unquote($buff, $quote);
if (count($ret) === $f) {
return $ret;
}
$buff = '';
++$fields;
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 && strlen($buff) > 0 && !in_array($buff, array("\r", "\n"))) {
$ret[] = File_CSV::unquote($buff, $quote);
if (count($ret) == $f) {
return $ret;
}
}
if ($feof && count($ret) !== $f) {
$return = File_CSV::_readQuotedFillers($fp, $f, $fields, $ret,
$buff, $quote, $c, $sep);
if ($return !== false) {
return $return;
}
}
return !$feof ? $ret : false;
}
/**
* Adds missing fields (empty ones)
*
* @param resource $fp the file resource
* @param string $f
* @param integer $fields the field count
* @param array $ret the processed fields in a array
* @param string $buff the buffer before it gets put through unquote
* @param string $quote Quote in use
* @param string $c the char currently being worked with
* @param string $sep Separator in use
*
* @access private
* @return array | boolean returns false if no data should return out.
*/
function _readQuotedFillers($fp, $f, $fields, $ret, $buff, $quote, &$c, $sep)
{
// 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") && $fields !== $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;
}
return 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)
{
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;
}
if ($conf['fields'] === 1) {
$fields = array($line);
$field_count = 1;
} else {
$fields = explode($conf['sep'], $line);
$field_count = count($fields);
}
$real_field_count = $field_count - 1;
$check_char = $fields[$real_field_count];
if ($check_char === "\n" || $check_char === "\r") {
array_pop($fields);
--$field_count;
}
$last =& $fields[$real_field_count];
if (
$field_count !== $conf['fields'] || $conf['quote']
&& (
$last !== ''
&& (
($last[0] === $conf['quote'] && $last[strlen(rtrim($last)) - 1] !== $conf['quote'])
// excel support
|| ($last[0] === '=' && $last[1] === $conf['quote'])
// if the row has spaces or other extra chars before the quote
//|| preg_match('|^\s+\\' . $conf['quote'] .'|', $last)
)
)
// XXX perhaps there is a separator inside a quoted field
// || preg_match("|{$conf['quote']}.*{$conf['sep']}.*{$conf['quote']}|", $line)
// The regex above is really slow
|| ((count(explode(',', $line))) > $field_count)
) {
fseek($fp, -1 * strlen($line), SEEK_CUR);
$fields = File_CSV::readQuoted($file, $conf);
$fields = File_CSV::_processHeaders($fields, $conf);
return $fields;
}
$fields = File_CSV::unquote($fields, $conf['quote']);
if ($field_count != $conf['fields']) {
File_CSV::raiseError("Read wrong fields number count: '". $field_count .
"' expected ".$conf['fields']);
return true;
}
$fields = File_CSV::_processHeaders($fields, $conf);
return $fields;
}
/**
* Process the field array being passed in and map the array over to
* the header values if the configuration is set on.
*
* @param array $fields The CSV row columns
* @param array $conf File_CSV configuration
*
* @return array Processed array
*/
function _processHeaders($fields, &$conf)
{
static $headers = array();
if (isset($conf['header']) && $conf['header'] == true && empty($headers)) {
// read the first row and assign to $headers
$headers = $fields;
return $headers;
}
if (!empty($headers)) {
$tmp = array();
foreach ($fields as $k => $v) {
if (isset($headers[$k])) {
$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 = '';
$quote = $conf['quote'];
for ($i = 0; $i < $field_count; ++$i) {
// Write a single field
$quote_field = false;
// Only quote this field in the following cases:
if (is_numeric($fields[$i])) {
// Numeric fields should not be quoted
} elseif (isset($conf['sep']) && (strpos($fields[$i], $conf['sep']) !== false)) {
// Separator is present in field
$quote_field = true;
} elseif (strpos($fields[$i], $quote) !== false) {
// Quote character is present in field
$quote_field = true;
} elseif (
strpos($fields[$i], "\n") !== false
|| strpos($fields[$i], "\r") !== false
) {
// Newline is present in field
$quote_field = true;
} elseif (!is_numeric($fields[$i]) && (substr($fields[$i], 0, 1) == " " || substr($fields[$i], -1) == " ")) {
// Space found at beginning or end of field value
$quote_field = true;
}
if ($quote_field) {
// Escape the quote character within the field (e.g. " becomes "")
$quoted_value = str_replace($quote, $quote.$quote, $fields[$i]);
$write .= $quote . $quoted_value . $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 occurrences
// for each separator in each line
$lines = '';
for ($i = 0; $i < 30 && !feof($fp) && $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 = '"\'';
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] + $num : 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;
}
}

View File

@@ -0,0 +1,496 @@
<?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$
//
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$
* @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;
}
sort($matches);
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);
}
sort($this->directories);
sort($this->files);
$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);
}
sort($retval);
while (list($key, $val) = each($retval)) {
if (!is_array($val)) {
$path = $directory . "/" . $val;
if (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;
}
}
}
sort($matches);
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:
*/
?>

View File

@@ -0,0 +1,503 @@
<?php
/* vim: set ts=4 sw=4: */
/*
+-----------------------------------------------------------------------+
| Copyright (c) 2002-2007 Mika Tuupola |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions |
| are met: |
| |
| o Redistributions of source code must retain the above copyright |
| notice, this list of conditions and the following disclaimer. |
| o Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution.|
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
+-----------------------------------------------------------------------+
| Author: Mika Tuupola <tuupola@appelsiini.net> |
+-----------------------------------------------------------------------+
*/
/* $Id: HtAccess.php,v 1.17 2007/08/01 08:04:30 tuupola Exp $ */
require_once 'PEAR.php' ;
/**
* Class for manipulating .htaccess files
*
* A class which provided common methods to manipulate Apache / NCSA
* style .htaccess files.
*
* Example 1 (modifying existing file):
*
* $h = new File_HtAccess('.htaccess');
* $h->load();
* $h->setRequire('valid-user');
* $h->save();
*
* Example 2 (modifying existing file):
*
* $h = new File_HtAccess('.htaccess');
* $h->load();
* $h->addRequire('newuser');
* $h->save();
*
* Example 3 (creating a new file):
*
* $params['authname'] = 'Private';
* $params['authtype'] = 'Basic';
* $params['authuserfile'] = '/path/to/.htpasswd';
* $params['authgroupfile'] = '/path/to/.htgroup';
* $params['require'] = array('group', 'admins');
*
* $h = new File_HtAccess('/path/to/.htaccess', $params);
* $h->save();
*
* @author Mika Tuupola <tuupola@appelsiini.net>
* @access public
* @version 1.0.0
* @package File_HtAccess
* @category File
*/
class File_HtAccess {
var $file;
var $authname;
var $authtype;
var $authuserfile;
var $authgroupfile;
var $authdigestfile;
var $authdigestgroupfile;
var $require = array();
var $additional = array();
/**
* Constructor
*
* @access public
* @param string $file
* @param array $params
* @return object File_HtAccess
*/
function File_HtAccess($file='.htaccess', $params='') {
$this->file = $file;
$this->setProperties($params);
}
/**
* Load the given .htaccess file
*
* @access public
* @return mixed true on success, PEAR_Error on failure
*/
function load() {
$retval = true;
$fd = @fopen($this->getFile(), 'r');
if ($fd) {
while ($buffer = fgets($fd, 4096)) {
$buffer = trim($buffer);
if ($buffer) {
$data = split(' ', $buffer, 2);
if (preg_match('/AuthName/i', $data[0])) {
$this->setAuthName($data[1]);
} elseif (preg_match('/AuthType/i', $data[0])) {
$this->setAuthType($data[1]);
} elseif (preg_match('/AuthUserFile/i', $data[0])) {
$this->setAuthUserFile($data[1]);
} elseif (preg_match('/AuthGroupFile/i', $data[0])) {
$this->setAuthGroupFile($data[1]);
} elseif (preg_match('/AuthDigestFile/i', $data[0])) {
$this->setAuthDigestFile($data[1]);
} elseif (preg_match('/AuthDigestGroupFile/i', $data[0])) {
$this->setAuthDigestGroupFile($data[1]);
} elseif (preg_match('/Require/i', $buffer)) {
$require = split(' ', $data[1]);
$this->addRequire($require);
} elseif (trim($buffer)) {
$this->addAdditional($buffer);
}
}
}
fclose($fd);
} else {
$retval = PEAR::raiseError('Could not open ' . $this->getFile() .
' for reading.');
}
return($retval);
}
/**
* Set class properties
*
* @access public
* @param array $params
*/
function setProperties($params) {
if (is_array($params)) {
foreach ($params as $key => $value) {
$method = 'set' . $key;
$this->$method($value);
}
}
}
/**
* Set the value of authname property
*
* @access public
* @param string $name
*/
function setAuthName($name='Restricted') {
$this->authname = $name;
}
/**
* Set the value of authtype propery
*
* @access public
* @param string $type
*/
function setAuthType($type='Basic') {
$this->authtype = $type;
}
/**
* Set the value of authuserfile propery
*
* @access public
* @param string $file
*/
function setAuthUserFile($file='') {
$this->authuserfile = $file;
}
/**
* Set the value of authgroupfile property
*
* @access public
* @param string $file
*/
function setAuthGroupFile($file='') {
$this->authgroupfile = $file;
}
/**
* Set the value of authdigestfile property
*
* @access public
* @param string $file
*/
function setAuthDigestFile($file='') {
$this->authdigestfile = $file;
}
/**
* Set the value of authdigestgroupfile property
*
* @access public
* @param string $file
*/
function setAuthDigestGroupFile($file='') {
$this->authdigestgroupfile = $file;
}
/**
* Set the value of require property
*
* Parameter can be given as an array or string. If given as a string
* the value will be exploded in to an array by using space as a
* separator.
*
* @access public
* @param mixed $require
*/
function setRequire($require='') {
if (is_array($require)) {
$this->require = $require;
} else {
$this->require = explode(' ', $require);
}
}
/**
* Add a value to require property
*
* @access public
* @param string $require
*/
function addRequire($require) {
if (is_array($require)) {
$merge = array_merge($this->getRequire(), $require);
$merge = array_unique($merge);
$merge = array_merge($merge);
$this->setRequire($merge);
} else {
$this->require[] = $require;
}
}
/**
* Delete a value from require property
*
* @access public
* @param string $require
*/
function delRequire($require) {
$pos = array_search($require, $this->require);
unset($this->require[$pos]);
}
/**
* Set the value of additional property
*
* Additional property is used for all the extra things in .htaccess
* file which don't have specific accessor method for them.
*
* @access public
* @param array $additional
*/
function setAdditional($additional='') {
$this->additional = (array)$additional;
}
/**
* Add a value to additional property
*
* @access public
* @param string $additional
*/
function addAdditional($additional='') {
$this->additional[] = $additional;
}
/**
* Set the value of file property
*
* @access public
* @param file $file
*/
function setFile($file) {
$this->file = $file;
}
/**
* Get the value of authname property
*
* @access public
* @return string
*/
function getAuthName() {
return($this->authname);
}
/**
* Get the value of authtype property
*
* @access public
* @return string
*/
function getAuthType() {
return($this->authtype);
}
/**
* Get the value of authuserfile property
*
* @access public
* @return string
*/
function getAuthUserFile() {
return($this->authuserfile);
}
/**
* Get the value of authgroupfile property
*
* @access public
* @return string
*/
function getAuthGroupFile() {
return($this->authgroupfile);
}
/**
* Get the value of authdigestfile property
*
* @access public
* @return string
*/
function getAuthDigestFile() {
return($this->authdigestfile);
}
/**
* Get the value of authdigestgroupfile property
*
* @access public
* @return string
*/
function getAuthDigestGroupFile() {
return($this->authdigestgroupfile);
}
/**
* Get the value(s) of require property
*
* @access public
* @param string $type whether to return an array or string
* @return mixed string or array, defaults to an array
*/
function getRequire($type='') {
$retval = $this->require;
if ($type == 'string') {
$retval = implode($retval, ' ');
}
return($retval);
}
/**
* Get the value(s) of additional property
*
* @access public
* @param string $type whether to return an array or string
* @return mixed string or array, defaults to an array
*/
function getAdditional($type='') {
$retval = $this->additional;
if ($type == 'string') {
$retval = implode($retval, "\n");
}
return($retval);
}
/**
* Get the value of file property
*
* @access public
* @return string
*/
function getFile() {
return($this->file);
}
/**
* Save the .htaccess file
*
* @access public
* @return mixed true on success, PEAR_Error on failure
* @see PEAR_Error
*/
function save() {
$retval = true;
$str = $this->getContent();
$fd = @fopen($this->getFile(), 'w');
if ($fd) {
fwrite($fd, $str, strlen($str));
} else {
$retval = PEAR::raiseError('Could not open ' . $this->getFile() .
' for writing.');
}
return($retval);
}
/**
* Returns the .htaccess File content
*
* @access public
* @return string
*/
function getContent() {
$retval = '';
if ($this->getAuthName()) {
$retval .= 'AuthName ' . $this->getAuthName() . "\n";
}
if ($this->getAuthType()) {
$retval .= 'AuthType ' . $this->getAuthType() . "\n";
}
if ('basic' == strtolower($this->getAuthType())) {
$retval .= 'AuthUserFile ' . $this->getAuthUserFile() . "\n";
if (trim($this->getAuthGroupFile())) {
$retval .= 'AuthGroupFile ' . $this->getAuthGroupFile() . "\n";
}
} elseif ('digest' == strtolower($this->getAuthType())) {
$retval .= 'AuthDigestFile ' . $this->getAuthDigestFile() . "\n";
if (trim($this->getAuthDigestGroupFile())) {
$retval .= 'AuthDigestGroupFile ' . $this->getAuthDigestGroupFile() . "\n";
}
}
if (trim($this->getRequire('string'))) {
$retval .= 'Require ' . $this->getRequire('string') . "\n";
}
$retval .= $this->getAdditional('string') . "\n";
return($retval);
}
}
?>

View File

@@ -0,0 +1,196 @@
<?php
/**
* php-file-iterator
*
* Copyright (c) 2009-2012, Sebastian Bergmann <sb@sebastian-bergmann.de>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Sebastian Bergmann nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package File
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2009-2012 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @since File available since Release 1.0.0
*/
/**
* FilterIterator implementation that filters files based on prefix(es) and/or
* suffix(es). Hidden files and files from hidden directories are also filtered.
*
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2009-2012 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @version Release: 1.3.3
* @link http://github.com/sebastianbergmann/php-file-iterator/tree
* @since Class available since Release 1.0.0
*/
class File_Iterator extends FilterIterator
{
const PREFIX = 0;
const SUFFIX = 1;
/**
* @var array
*/
protected $suffixes = array();
/**
* @var array
*/
protected $prefixes = array();
/**
* @var array
*/
protected $exclude = array();
/**
* @var string
*/
protected $basepath;
/**
* @param Iterator $iterator
* @param array $suffixes
* @param array $prefixes
* @param array $exclude
* @param string $basepath
*/
public function __construct(Iterator $iterator, array $suffixes = array(), array $prefixes = array(), array $exclude = array(), $basepath = NULL)
{
$exclude = array_filter(array_map('realpath', $exclude));
if ($basepath !== NULL) {
$basepath = realpath($basepath);
}
if ($basepath === FALSE) {
$basepath = NULL;
} else {
foreach ($exclude as &$_exclude) {
$_exclude = str_replace($basepath, '', $_exclude);
}
}
$this->prefixes = $prefixes;
$this->suffixes = $suffixes;
$this->exclude = $exclude;
$this->basepath = $basepath;
parent::__construct($iterator);
}
/**
* @return boolean
*/
public function accept()
{
$current = $this->getInnerIterator()->current();
$filename = $current->getFilename();
$realpath = $current->getRealPath();
if ($this->basepath !== NULL) {
$realpath = str_replace($this->basepath, '', $realpath);
}
// Filter files in hidden directories.
if (preg_match('=/\.[^/]*/=', $realpath)) {
return FALSE;
}
return $this->acceptPath($realpath) &&
$this->acceptPrefix($filename) &&
$this->acceptSuffix($filename);
}
/**
* @param string $path
* @return boolean
* @since Method available since Release 1.1.0
*/
protected function acceptPath($path)
{
foreach ($this->exclude as $exclude) {
if (strpos($path, $exclude) === 0) {
return FALSE;
}
}
return TRUE;
}
/**
* @param string $filename
* @return boolean
* @since Method available since Release 1.1.0
*/
protected function acceptPrefix($filename)
{
return $this->acceptSubString($filename, $this->prefixes, self::PREFIX);
}
/**
* @param string $filename
* @return boolean
* @since Method available since Release 1.1.0
*/
protected function acceptSuffix($filename)
{
return $this->acceptSubString($filename, $this->suffixes, self::SUFFIX);
}
/**
* @param string $filename
* @param array $subString
* @param integer $type
* @return boolean
* @since Method available since Release 1.1.0
*/
protected function acceptSubString($filename, array $subStrings, $type)
{
if (empty($subStrings)) {
return TRUE;
}
$matched = FALSE;
foreach ($subStrings as $string) {
if (($type == self::PREFIX && strpos($filename, $string) === 0) ||
($type == self::SUFFIX &&
substr($filename, -1 * strlen($string)) == $string)) {
$matched = TRUE;
break;
}
}
return $matched;
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* php-file-iterator
*
* Copyright (c) 2009-2012, Sebastian Bergmann <sb@sebastian-bergmann.de>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Sebastian Bergmann nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package File
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2009-2012 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @since File available since Release 1.3.0
*/
spl_autoload_register(
function ($class)
{
static $classes = NULL;
static $path = NULL;
if ($classes === NULL) {
$classes = array(
'file_iterator' => '/Iterator.php',
'file_iterator_facade' => '/Iterator/Facade.php',
'file_iterator_factory' => '/Iterator/Factory.php'
);
$path = dirname(dirname(__FILE__));
}
$cn = strtolower($class);
if (isset($classes[$cn])) {
require $path . $classes[$cn];
}
}
);

View File

@@ -0,0 +1,161 @@
<?php
/**
* php-file-iterator
*
* Copyright (c) 2009-2012, Sebastian Bergmann <sb@sebastian-bergmann.de>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Sebastian Bergmann nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package File
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2009-2012 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @since File available since Release 1.3.0
*/
/**
* Façade implementation that uses File_Iterator_Factory to create a
* File_Iterator that operates on an AppendIterator that contains an
* RecursiveDirectoryIterator for each given path. The list of unique
* files is returned as an array.
*
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2009-2012 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @version Release: 1.3.3
* @link http://github.com/sebastianbergmann/php-file-iterator/tree
* @since Class available since Release 1.3.0
*/
class File_Iterator_Facade
{
/**
* @param array|string $paths
* @param array|string $suffixes
* @param array|string $prefixes
* @param array $exclude
* @param boolean $commonPath
* @return array
*/
public function getFilesAsArray($paths, $suffixes = '', $prefixes = '', array $exclude = array(), $commonPath = FALSE)
{
if (is_string($paths)) {
$paths = array($paths);
}
$factory = new File_Iterator_Factory;
$iterator = $factory->getFileIterator(
$paths, $suffixes, $prefixes, $exclude
);
$files = array();
foreach ($iterator as $file) {
$file = $file->getRealPath();
if ($file) {
$files[] = $file;
}
}
foreach ($paths as $path) {
if (is_file($path)) {
$files[] = realpath($path);
}
}
$files = array_unique($files);
sort($files);
if ($commonPath) {
return array(
'commonPath' => $this->getCommonPath($files),
'files' => $files
);
} else {
return $files;
}
}
/**
* Returns the common path of a set of files.
*
* @param array $files
* @return string
*/
protected function getCommonPath(array $files)
{
$count = count($files);
if ($count == 0) {
return '';
}
if ($count == 1) {
return dirname($files[0]) . DIRECTORY_SEPARATOR;
}
$_files = array();
foreach ($files as $file) {
$_files[] = $_fileParts = explode(DIRECTORY_SEPARATOR, $file);
if (empty($_fileParts[0])) {
$_fileParts[0] = DIRECTORY_SEPARATOR;
}
}
$common = '';
$done = FALSE;
$j = 0;
$count--;
while (!$done) {
for ($i = 0; $i < $count; $i++) {
if ($_files[$i][$j] != $_files[$i+1][$j]) {
$done = TRUE;
break;
}
}
if (!$done) {
$common .= $_files[0][$j];
if ($j > 0) {
$common .= DIRECTORY_SEPARATOR;
}
}
$j++;
}
return DIRECTORY_SEPARATOR . $common;
}
}

View File

@@ -0,0 +1,120 @@
<?php
/**
* php-file-iterator
*
* Copyright (c) 2009-2012, Sebastian Bergmann <sb@sebastian-bergmann.de>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Sebastian Bergmann nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package File
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2009-2012 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @since File available since Release 1.1.0
*/
/**
* Factory Method implementation that creates a File_Iterator that operates on
* an AppendIterator that contains an RecursiveDirectoryIterator for each given
* path.
*
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2009-2012 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @version Release: 1.3.3
* @link http://github.com/sebastianbergmann/php-file-iterator/tree
* @since Class available since Release 1.1.0
*/
class File_Iterator_Factory
{
/**
* @param array|string $paths
* @param array|string $suffixes
* @param array|string $prefixes
* @param array $exclude
* @return AppendIterator
*/
public function getFileIterator($paths, $suffixes = '', $prefixes = '', array $exclude = array())
{
if (is_string($paths)) {
$paths = array($paths);
}
$_paths = array();
foreach ($paths as $path) {
if ($locals = glob($path, GLOB_ONLYDIR)) {
$_paths = array_merge($_paths, $locals);
} else {
$_paths[] = $path;
}
}
$paths = $_paths;
unset($_paths);
if (is_string($prefixes)) {
if ($prefixes != '') {
$prefixes = array($prefixes);
} else {
$prefixes = array();
}
}
if (is_string($suffixes)) {
if ($suffixes != '') {
$suffixes = array($suffixes);
} else {
$suffixes = array();
}
}
$iterator = new AppendIterator;
foreach ($paths as $path) {
if (is_dir($path)) {
$iterator->append(
new File_Iterator(
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path)
),
$suffixes,
$prefixes,
$exclude,
$path
)
);
}
}
return $iterator;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
<?php
/**
* @package File_PDF
*/
for ($i = 0; $i <= 255; $i++) {
$font_widths['courier'][chr($i)] = 600;
}

View File

@@ -0,0 +1,272 @@
<?php
/**
* @package File_PDF
*/
$font_widths['helvetica'] = array(
chr(0) => 278,
chr(1) => 278,
chr(2) => 278,
chr(3) => 278,
chr(4) => 278,
chr(5) => 278,
chr(6) => 278,
chr(7) => 278,
chr(8) => 278,
chr(9) => 278,
chr(10) => 278,
chr(11) => 278,
chr(12) => 278,
chr(13) => 278,
chr(14) => 278,
chr(15) => 278,
chr(16) => 278,
chr(17) => 278,
chr(18) => 278,
chr(19) => 278,
chr(20) => 278,
chr(21) => 278,
chr(22) => 278,
chr(23) => 278,
chr(24) => 278,
chr(25) => 278,
chr(26) => 278,
chr(27) => 278,
chr(28) => 278,
chr(29) => 278,
chr(30) => 278,
chr(31) => 278,
' ' => 278,
'!' => 278,
'"' => 355,
'#' => 556,
'$' => 556,
'%' => 889,
'&' => 667,
'\'' => 191,
'(' => 333,
')' => 333,
'*' => 389,
'+' => 584,
',' => 278,
'-' => 333,
'.' => 278,
'/' => 278,
'0' => 556,
'1' => 556,
'2' => 556,
'3' => 556,
'4' => 556,
'5' => 556,
'6' => 556,
'7' => 556,
'8' => 556,
'9' => 556,
':' => 278,
';' => 278,
'<' => 584,
'=' => 584,
'>' => 584,
'?' => 556,
'@' => 1015,
'A' => 667,
'B' => 667,
'C' => 722,
'D' => 722,
'E' => 667,
'F' => 611,
'G' => 778,
'H' => 722,
'I' => 278,
'J' => 500,
'K' => 667,
'L' => 556,
'M' => 833,
'N' => 722,
'O' => 778,
'P' => 667,
'Q' => 778,
'R' => 722,
'S' => 667,
'T' => 611,
'U' => 722,
'V' => 667,
'W' => 944,
'X' => 667,
'Y' => 667,
'Z' => 611,
'[' => 278,
'\\' => 278,
']' => 278,
'^' => 469,
'_' => 556,
'`' => 333,
'a' => 556,
'b' => 556,
'c' => 500,
'd' => 556,
'e' => 556,
'f' => 278,
'g' => 556,
'h' => 556,
'i' => 222,
'j' => 222,
'k' => 500,
'l' => 222,
'm' => 833,
'n' => 556,
'o' => 556,
'p' => 556,
'q' => 556,
'r' => 333,
's' => 500,
't' => 278,
'u' => 556,
'v' => 500,
'w' => 722,
'x' => 500,
'y' => 500,
'z' => 500,
'{' => 334,
'|' => 260,
'}' => 334,
'~' => 584,
chr(127) => 350,
chr(128) => 556,
chr(129) => 350,
chr(130) => 222,
chr(131) => 556,
chr(132) => 333,
chr(133) => 1000,
chr(134) => 556,
chr(135) => 556,
chr(136) => 333,
chr(137) => 1000,
chr(138) => 667,
chr(139) => 333,
chr(140) => 1000,
chr(141) => 350,
chr(142) => 611,
chr(143) => 350,
chr(144) => 350,
chr(145) => 222,
chr(146) => 222,
chr(147) => 333,
chr(148) => 333,
chr(149) => 350,
chr(150) => 556,
chr(151) => 1000,
chr(152) => 333,
chr(153) => 1000,
chr(154) => 500,
chr(155) => 333,
chr(156) => 944,
chr(157) => 350,
chr(158) => 500,
chr(159) => 667,
chr(160) => 278,
chr(161) => 333,
chr(162) => 556,
chr(163) => 556,
chr(164) => 556,
chr(165) => 556,
chr(166) => 260,
chr(167) => 556,
chr(168) => 333,
chr(169) => 737,
chr(170) => 370,
chr(171) => 556,
chr(172) => 584,
chr(173) => 333,
chr(174) => 737,
chr(175) => 333,
chr(176) => 400,
chr(177) => 584,
chr(178) => 333,
chr(179) => 333,
chr(180) => 333,
chr(181) => 556,
chr(182) => 537,
chr(183) => 278,
chr(184) => 333,
chr(185) => 333,
chr(186) => 365,
chr(187) => 556,
chr(188) => 834,
chr(189) => 834,
chr(190) => 834,
chr(191) => 611,
chr(192) => 667,
chr(193) => 667,
chr(194) => 667,
chr(195) => 667,
chr(196) => 667,
chr(197) => 667,
chr(198) => 1000,
chr(199) => 722,
chr(200) => 667,
chr(201) => 667,
chr(202) => 667,
chr(203) => 667,
chr(204) => 278,
chr(205) => 278,
chr(206) => 278,
chr(207) => 278,
chr(208) => 722,
chr(209) => 722,
chr(210) => 778,
chr(211) => 778,
chr(212) => 778,
chr(213) => 778,
chr(214) => 778,
chr(215) => 584,
chr(216) => 778,
chr(217) => 722,
chr(218) => 722,
chr(219) => 722,
chr(220) => 722,
chr(221) => 667,
chr(222) => 667,
chr(223) => 611,
chr(224) => 556,
chr(225) => 556,
chr(226) => 556,
chr(227) => 556,
chr(228) => 556,
chr(229) => 556,
chr(230) => 889,
chr(231) => 500,
chr(232) => 556,
chr(233) => 556,
chr(234) => 556,
chr(235) => 556,
chr(236) => 278,
chr(237) => 278,
chr(238) => 278,
chr(239) => 278,
chr(240) => 556,
chr(241) => 556,
chr(242) => 556,
chr(243) => 556,
chr(244) => 556,
chr(245) => 556,
chr(246) => 556,
chr(247) => 584,
chr(248) => 611,
chr(249) => 556,
chr(250) => 556,
chr(251) => 556,
chr(252) => 556,
chr(253) => 500,
chr(254) => 556,
chr(255) => 500);

View File

@@ -0,0 +1,272 @@
<?php
/**
* @package File_PDF
*/
$font_widths['helveticaB'] = array(
chr(0) => 278,
chr(1) => 278,
chr(2) => 278,
chr(3) => 278,
chr(4) => 278,
chr(5) => 278,
chr(6) => 278,
chr(7) => 278,
chr(8) => 278,
chr(9) => 278,
chr(10) => 278,
chr(11) => 278,
chr(12) => 278,
chr(13) => 278,
chr(14) => 278,
chr(15) => 278,
chr(16) => 278,
chr(17) => 278,
chr(18) => 278,
chr(19) => 278,
chr(20) => 278,
chr(21) => 278,
chr(22) => 278,
chr(23) => 278,
chr(24) => 278,
chr(25) => 278,
chr(26) => 278,
chr(27) => 278,
chr(28) => 278,
chr(29) => 278,
chr(30) => 278,
chr(31) => 278,
' ' => 278,
'!' => 333,
'"' => 474,
'#' => 556,
'$' => 556,
'%' => 889,
'&' => 722,
'\'' => 238,
'(' => 333,
')' => 333,
'*' => 389,
'+' => 584,
',' => 278,
'-' => 333,
'.' => 278,
'/' => 278,
'0' => 556,
'1' => 556,
'2' => 556,
'3' => 556,
'4' => 556,
'5' => 556,
'6' => 556,
'7' => 556,
'8' => 556,
'9' => 556,
':' => 333,
';' => 333,
'<' => 584,
'=' => 584,
'>' => 584,
'?' => 611,
'@' => 975,
'A' => 722,
'B' => 722,
'C' => 722,
'D' => 722,
'E' => 667,
'F' => 611,
'G' => 778,
'H' => 722,
'I' => 278,
'J' => 556,
'K' => 722,
'L' => 611,
'M' => 833,
'N' => 722,
'O' => 778,
'P' => 667,
'Q' => 778,
'R' => 722,
'S' => 667,
'T' => 611,
'U' => 722,
'V' => 667,
'W' => 944,
'X' => 667,
'Y' => 667,
'Z' => 611,
'[' => 333,
'\\' => 278,
']' => 333,
'^' => 584,
'_' => 556,
'`' => 333,
'a' => 556,
'b' => 611,
'c' => 556,
'd' => 611,
'e' => 556,
'f' => 333,
'g' => 611,
'h' => 611,
'i' => 278,
'j' => 278,
'k' => 556,
'l' => 278,
'm' => 889,
'n' => 611,
'o' => 611,
'p' => 611,
'q' => 611,
'r' => 389,
's' => 556,
't' => 333,
'u' => 611,
'v' => 556,
'w' => 778,
'x' => 556,
'y' => 556,
'z' => 500,
'{' => 389,
'|' => 280,
'}' => 389,
'~' => 584,
chr(127) => 350,
chr(128) => 556,
chr(129) => 350,
chr(130) => 278,
chr(131) => 556,
chr(132) => 500,
chr(133) => 1000,
chr(134) => 556,
chr(135) => 556,
chr(136) => 333,
chr(137) => 1000,
chr(138) => 667,
chr(139) => 333,
chr(140) => 1000,
chr(141) => 350,
chr(142) => 611,
chr(143) => 350,
chr(144) => 350,
chr(145) => 278,
chr(146) => 278,
chr(147) => 500,
chr(148) => 500,
chr(149) => 350,
chr(150) => 556,
chr(151) => 1000,
chr(152) => 333,
chr(153) => 1000,
chr(154) => 556,
chr(155) => 333,
chr(156) => 944,
chr(157) => 350,
chr(158) => 500,
chr(159) => 667,
chr(160) => 278,
chr(161) => 333,
chr(162) => 556,
chr(163) => 556,
chr(164) => 556,
chr(165) => 556,
chr(166) => 280,
chr(167) => 556,
chr(168) => 333,
chr(169) => 737,
chr(170) => 370,
chr(171) => 556,
chr(172) => 584,
chr(173) => 333,
chr(174) => 737,
chr(175) => 333,
chr(176) => 400,
chr(177) => 584,
chr(178) => 333,
chr(179) => 333,
chr(180) => 333,
chr(181) => 611,
chr(182) => 556,
chr(183) => 278,
chr(184) => 333,
chr(185) => 333,
chr(186) => 365,
chr(187) => 556,
chr(188) => 834,
chr(189) => 834,
chr(190) => 834,
chr(191) => 611,
chr(192) => 722,
chr(193) => 722,
chr(194) => 722,
chr(195) => 722,
chr(196) => 722,
chr(197) => 722,
chr(198) => 1000,
chr(199) => 722,
chr(200) => 667,
chr(201) => 667,
chr(202) => 667,
chr(203) => 667,
chr(204) => 278,
chr(205) => 278,
chr(206) => 278,
chr(207) => 278,
chr(208) => 722,
chr(209) => 722,
chr(210) => 778,
chr(211) => 778,
chr(212) => 778,
chr(213) => 778,
chr(214) => 778,
chr(215) => 584,
chr(216) => 778,
chr(217) => 722,
chr(218) => 722,
chr(219) => 722,
chr(220) => 722,
chr(221) => 667,
chr(222) => 667,
chr(223) => 611,
chr(224) => 556,
chr(225) => 556,
chr(226) => 556,
chr(227) => 556,
chr(228) => 556,
chr(229) => 556,
chr(230) => 889,
chr(231) => 556,
chr(232) => 556,
chr(233) => 556,
chr(234) => 556,
chr(235) => 556,
chr(236) => 278,
chr(237) => 278,
chr(238) => 278,
chr(239) => 278,
chr(240) => 611,
chr(241) => 611,
chr(242) => 611,
chr(243) => 611,
chr(244) => 611,
chr(245) => 611,
chr(246) => 611,
chr(247) => 584,
chr(248) => 611,
chr(249) => 611,
chr(250) => 611,
chr(251) => 611,
chr(252) => 611,
chr(253) => 556,
chr(254) => 611,
chr(255) => 556);

View File

@@ -0,0 +1,272 @@
<?php
/**
* @package File_PDF
*/
$font_widths['helveticaBI'] = array(
chr(0) => 278,
chr(1) => 278,
chr(2) => 278,
chr(3) => 278,
chr(4) => 278,
chr(5) => 278,
chr(6) => 278,
chr(7) => 278,
chr(8) => 278,
chr(9) => 278,
chr(10) => 278,
chr(11) => 278,
chr(12) => 278,
chr(13) => 278,
chr(14) => 278,
chr(15) => 278,
chr(16) => 278,
chr(17) => 278,
chr(18) => 278,
chr(19) => 278,
chr(20) => 278,
chr(21) => 278,
chr(22) => 278,
chr(23) => 278,
chr(24) => 278,
chr(25) => 278,
chr(26) => 278,
chr(27) => 278,
chr(28) => 278,
chr(29) => 278,
chr(30) => 278,
chr(31) => 278,
' ' => 278,
'!' => 333,
'"' => 474,
'#' => 556,
'$' => 556,
'%' => 889,
'&' => 722,
'\'' => 238,
'(' => 333,
')' => 333,
'*' => 389,
'+' => 584,
',' => 278,
'-' => 333,
'.' => 278,
'/' => 278,
'0' => 556,
'1' => 556,
'2' => 556,
'3' => 556,
'4' => 556,
'5' => 556,
'6' => 556,
'7' => 556,
'8' => 556,
'9' => 556,
':' => 333,
';' => 333,
'<' => 584,
'=' => 584,
'>' => 584,
'?' => 611,
'@' => 975,
'A' => 722,
'B' => 722,
'C' => 722,
'D' => 722,
'E' => 667,
'F' => 611,
'G' => 778,
'H' => 722,
'I' => 278,
'J' => 556,
'K' => 722,
'L' => 611,
'M' => 833,
'N' => 722,
'O' => 778,
'P' => 667,
'Q' => 778,
'R' => 722,
'S' => 667,
'T' => 611,
'U' => 722,
'V' => 667,
'W' => 944,
'X' => 667,
'Y' => 667,
'Z' => 611,
'[' => 333,
'\\' => 278,
']' => 333,
'^' => 584,
'_' => 556,
'`' => 333,
'a' => 556,
'b' => 611,
'c' => 556,
'd' => 611,
'e' => 556,
'f' => 333,
'g' => 611,
'h' => 611,
'i' => 278,
'j' => 278,
'k' => 556,
'l' => 278,
'm' => 889,
'n' => 611,
'o' => 611,
'p' => 611,
'q' => 611,
'r' => 389,
's' => 556,
't' => 333,
'u' => 611,
'v' => 556,
'w' => 778,
'x' => 556,
'y' => 556,
'z' => 500,
'{' => 389,
'|' => 280,
'}' => 389,
'~' => 584,
chr(127) => 350,
chr(128) => 556,
chr(129) => 350,
chr(130) => 278,
chr(131) => 556,
chr(132) => 500,
chr(133) => 1000,
chr(134) => 556,
chr(135) => 556,
chr(136) => 333,
chr(137) => 1000,
chr(138) => 667,
chr(139) => 333,
chr(140) => 1000,
chr(141) => 350,
chr(142) => 611,
chr(143) => 350,
chr(144) => 350,
chr(145) => 278,
chr(146) => 278,
chr(147) => 500,
chr(148) => 500,
chr(149) => 350,
chr(150) => 556,
chr(151) => 1000,
chr(152) => 333,
chr(153) => 1000,
chr(154) => 556,
chr(155) => 333,
chr(156) => 944,
chr(157) => 350,
chr(158) => 500,
chr(159) => 667,
chr(160) => 278,
chr(161) => 333,
chr(162) => 556,
chr(163) => 556,
chr(164) => 556,
chr(165) => 556,
chr(166) => 280,
chr(167) => 556,
chr(168) => 333,
chr(169) => 737,
chr(170) => 370,
chr(171) => 556,
chr(172) => 584,
chr(173) => 333,
chr(174) => 737,
chr(175) => 333,
chr(176) => 400,
chr(177) => 584,
chr(178) => 333,
chr(179) => 333,
chr(180) => 333,
chr(181) => 611,
chr(182) => 556,
chr(183) => 278,
chr(184) => 333,
chr(185) => 333,
chr(186) => 365,
chr(187) => 556,
chr(188) => 834,
chr(189) => 834,
chr(190) => 834,
chr(191) => 611,
chr(192) => 722,
chr(193) => 722,
chr(194) => 722,
chr(195) => 722,
chr(196) => 722,
chr(197) => 722,
chr(198) => 1000,
chr(199) => 722,
chr(200) => 667,
chr(201) => 667,
chr(202) => 667,
chr(203) => 667,
chr(204) => 278,
chr(205) => 278,
chr(206) => 278,
chr(207) => 278,
chr(208) => 722,
chr(209) => 722,
chr(210) => 778,
chr(211) => 778,
chr(212) => 778,
chr(213) => 778,
chr(214) => 778,
chr(215) => 584,
chr(216) => 778,
chr(217) => 722,
chr(218) => 722,
chr(219) => 722,
chr(220) => 722,
chr(221) => 667,
chr(222) => 667,
chr(223) => 611,
chr(224) => 556,
chr(225) => 556,
chr(226) => 556,
chr(227) => 556,
chr(228) => 556,
chr(229) => 556,
chr(230) => 889,
chr(231) => 556,
chr(232) => 556,
chr(233) => 556,
chr(234) => 556,
chr(235) => 556,
chr(236) => 278,
chr(237) => 278,
chr(238) => 278,
chr(239) => 278,
chr(240) => 611,
chr(241) => 611,
chr(242) => 611,
chr(243) => 611,
chr(244) => 611,
chr(245) => 611,
chr(246) => 611,
chr(247) => 584,
chr(248) => 611,
chr(249) => 611,
chr(250) => 611,
chr(251) => 611,
chr(252) => 611,
chr(253) => 556,
chr(254) => 611,
chr(255) => 556);

View File

@@ -0,0 +1,272 @@
<?php
/**
* @package File_PDF
*/
$font_widths['helveticaI'] = array(
chr(0) => 278,
chr(1) => 278,
chr(2) => 278,
chr(3) => 278,
chr(4) => 278,
chr(5) => 278,
chr(6) => 278,
chr(7) => 278,
chr(8) => 278,
chr(9) => 278,
chr(10) => 278,
chr(11) => 278,
chr(12) => 278,
chr(13) => 278,
chr(14) => 278,
chr(15) => 278,
chr(16) => 278,
chr(17) => 278,
chr(18) => 278,
chr(19) => 278,
chr(20) => 278,
chr(21) => 278,
chr(22) => 278,
chr(23) => 278,
chr(24) => 278,
chr(25) => 278,
chr(26) => 278,
chr(27) => 278,
chr(28) => 278,
chr(29) => 278,
chr(30) => 278,
chr(31) => 278,
' ' => 278,
'!' => 278,
'"' => 355,
'#' => 556,
'$' => 556,
'%' => 889,
'&' => 667,
'\'' => 191,
'(' => 333,
')' => 333,
'*' => 389,
'+' => 584,
',' => 278,
'-' => 333,
'.' => 278,
'/' => 278,
'0' => 556,
'1' => 556,
'2' => 556,
'3' => 556,
'4' => 556,
'5' => 556,
'6' => 556,
'7' => 556,
'8' => 556,
'9' => 556,
':' => 278,
';' => 278,
'<' => 584,
'=' => 584,
'>' => 584,
'?' => 556,
'@' => 1015,
'A' => 667,
'B' => 667,
'C' => 722,
'D' => 722,
'E' => 667,
'F' => 611,
'G' => 778,
'H' => 722,
'I' => 278,
'J' => 500,
'K' => 667,
'L' => 556,
'M' => 833,
'N' => 722,
'O' => 778,
'P' => 667,
'Q' => 778,
'R' => 722,
'S' => 667,
'T' => 611,
'U' => 722,
'V' => 667,
'W' => 944,
'X' => 667,
'Y' => 667,
'Z' => 611,
'[' => 278,
'\\' => 278,
']' => 278,
'^' => 469,
'_' => 556,
'`' => 333,
'a' => 556,
'b' => 556,
'c' => 500,
'd' => 556,
'e' => 556,
'f' => 278,
'g' => 556,
'h' => 556,
'i' => 222,
'j' => 222,
'k' => 500,
'l' => 222,
'm' => 833,
'n' => 556,
'o' => 556,
'p' => 556,
'q' => 556,
'r' => 333,
's' => 500,
't' => 278,
'u' => 556,
'v' => 500,
'w' => 722,
'x' => 500,
'y' => 500,
'z' => 500,
'{' => 334,
'|' => 260,
'}' => 334,
'~' => 584,
chr(127) => 350,
chr(128) => 556,
chr(129) => 350,
chr(130) => 222,
chr(131) => 556,
chr(132) => 333,
chr(133) => 1000,
chr(134) => 556,
chr(135) => 556,
chr(136) => 333,
chr(137) => 1000,
chr(138) => 667,
chr(139) => 333,
chr(140) => 1000,
chr(141) => 350,
chr(142) => 611,
chr(143) => 350,
chr(144) => 350,
chr(145) => 222,
chr(146) => 222,
chr(147) => 333,
chr(148) => 333,
chr(149) => 350,
chr(150) => 556,
chr(151) => 1000,
chr(152) => 333,
chr(153) => 1000,
chr(154) => 500,
chr(155) => 333,
chr(156) => 944,
chr(157) => 350,
chr(158) => 500,
chr(159) => 667,
chr(160) => 278,
chr(161) => 333,
chr(162) => 556,
chr(163) => 556,
chr(164) => 556,
chr(165) => 556,
chr(166) => 260,
chr(167) => 556,
chr(168) => 333,
chr(169) => 737,
chr(170) => 370,
chr(171) => 556,
chr(172) => 584,
chr(173) => 333,
chr(174) => 737,
chr(175) => 333,
chr(176) => 400,
chr(177) => 584,
chr(178) => 333,
chr(179) => 333,
chr(180) => 333,
chr(181) => 556,
chr(182) => 537,
chr(183) => 278,
chr(184) => 333,
chr(185) => 333,
chr(186) => 365,
chr(187) => 556,
chr(188) => 834,
chr(189) => 834,
chr(190) => 834,
chr(191) => 611,
chr(192) => 667,
chr(193) => 667,
chr(194) => 667,
chr(195) => 667,
chr(196) => 667,
chr(197) => 667,
chr(198) => 1000,
chr(199) => 722,
chr(200) => 667,
chr(201) => 667,
chr(202) => 667,
chr(203) => 667,
chr(204) => 278,
chr(205) => 278,
chr(206) => 278,
chr(207) => 278,
chr(208) => 722,
chr(209) => 722,
chr(210) => 778,
chr(211) => 778,
chr(212) => 778,
chr(213) => 778,
chr(214) => 778,
chr(215) => 584,
chr(216) => 778,
chr(217) => 722,
chr(218) => 722,
chr(219) => 722,
chr(220) => 722,
chr(221) => 667,
chr(222) => 667,
chr(223) => 611,
chr(224) => 556,
chr(225) => 556,
chr(226) => 556,
chr(227) => 556,
chr(228) => 556,
chr(229) => 556,
chr(230) => 889,
chr(231) => 500,
chr(232) => 556,
chr(233) => 556,
chr(234) => 556,
chr(235) => 556,
chr(236) => 278,
chr(237) => 278,
chr(238) => 278,
chr(239) => 278,
chr(240) => 556,
chr(241) => 556,
chr(242) => 556,
chr(243) => 556,
chr(244) => 556,
chr(245) => 556,
chr(246) => 556,
chr(247) => 584,
chr(248) => 611,
chr(249) => 556,
chr(250) => 556,
chr(251) => 556,
chr(252) => 556,
chr(253) => 500,
chr(254) => 556,
chr(255) => 500);

View File

@@ -0,0 +1,272 @@
<?php
/**
* @package File_PDF
*/
$font_widths['symbol'] = array(
chr(0) => 250,
chr(1) => 250,
chr(2) => 250,
chr(3) => 250,
chr(4) => 250,
chr(5) => 250,
chr(6) => 250,
chr(7) => 250,
chr(8) => 250,
chr(9) => 250,
chr(10) => 250,
chr(11) => 250,
chr(12) => 250,
chr(13) => 250,
chr(14) => 250,
chr(15) => 250,
chr(16) => 250,
chr(17) => 250,
chr(18) => 250,
chr(19) => 250,
chr(20) => 250,
chr(21) => 250,
chr(22) => 250,
chr(23) => 250,
chr(24) => 250,
chr(25) => 250,
chr(26) => 250,
chr(27) => 250,
chr(28) => 250,
chr(29) => 250,
chr(30) => 250,
chr(31) => 250,
' ' => 250,
'!' => 333,
'"' => 713,
'#' => 500,
'$' => 549,
'%' => 833,
'&' => 778,
'\'' => 439,
'(' => 333,
')' => 333,
'*' => 500,
'+' => 549,
',' => 250,
'-' => 549,
'.' => 250,
'/' => 278,
'0' => 500,
'1' => 500,
'2' => 500,
'3' => 500,
'4' => 500,
'5' => 500,
'6' => 500,
'7' => 500,
'8' => 500,
'9' => 500,
':' => 278,
';' => 278,
'<' => 549,
'=' => 549,
'>' => 549,
'?' => 444,
'@' => 549,
'A' => 722,
'B' => 667,
'C' => 722,
'D' => 612,
'E' => 611,
'F' => 763,
'G' => 603,
'H' => 722,
'I' => 333,
'J' => 631,
'K' => 722,
'L' => 686,
'M' => 889,
'N' => 722,
'O' => 722,
'P' => 768,
'Q' => 741,
'R' => 556,
'S' => 592,
'T' => 611,
'U' => 690,
'V' => 439,
'W' => 768,
'X' => 645,
'Y' => 795,
'Z' => 611,
'[' => 333,
'\\' => 863,
']' => 333,
'^' => 658,
'_' => 500,
'`' => 500,
'a' => 631,
'b' => 549,
'c' => 549,
'd' => 494,
'e' => 439,
'f' => 521,
'g' => 411,
'h' => 603,
'i' => 329,
'j' => 603,
'k' => 549,
'l' => 549,
'm' => 576,
'n' => 521,
'o' => 549,
'p' => 549,
'q' => 521,
'r' => 549,
's' => 603,
't' => 439,
'u' => 576,
'v' => 713,
'w' => 686,
'x' => 493,
'y' => 686,
'z' => 494,
'{' => 480,
'|' => 200,
'}' => 480,
'~' => 549,
chr(127) => 0,
chr(128) => 0,
chr(129) => 0,
chr(130) => 0,
chr(131) => 0,
chr(132) => 0,
chr(133) => 0,
chr(134) => 0,
chr(135) => 0,
chr(136) => 0,
chr(137) => 0,
chr(138) => 0,
chr(139) => 0,
chr(140) => 0,
chr(141) => 0,
chr(142) => 0,
chr(143) => 0,
chr(144) => 0,
chr(145) => 0,
chr(146) => 0,
chr(147) => 0,
chr(148) => 0,
chr(149) => 0,
chr(150) => 0,
chr(151) => 0,
chr(152) => 0,
chr(153) => 0,
chr(154) => 0,
chr(155) => 0,
chr(156) => 0,
chr(157) => 0,
chr(158) => 0,
chr(159) => 0,
chr(160) => 750,
chr(161) => 620,
chr(162) => 247,
chr(163) => 549,
chr(164) => 167,
chr(165) => 713,
chr(166) => 500,
chr(167) => 753,
chr(168) => 753,
chr(169) => 753,
chr(170) => 753,
chr(171) => 1042,
chr(172) => 987,
chr(173) => 603,
chr(174) => 987,
chr(175) => 603,
chr(176) => 400,
chr(177) => 549,
chr(178) => 411,
chr(179) => 549,
chr(180) => 549,
chr(181) => 713,
chr(182) => 494,
chr(183) => 460,
chr(184) => 549,
chr(185) => 549,
chr(186) => 549,
chr(187) => 549,
chr(188) => 1000,
chr(189) => 603,
chr(190) => 1000,
chr(191) => 658,
chr(192) => 823,
chr(193) => 686,
chr(194) => 795,
chr(195) => 987,
chr(196) => 768,
chr(197) => 768,
chr(198) => 823,
chr(199) => 768,
chr(200) => 768,
chr(201) => 713,
chr(202) => 713,
chr(203) => 713,
chr(204) => 713,
chr(205) => 713,
chr(206) => 713,
chr(207) => 713,
chr(208) => 768,
chr(209) => 713,
chr(210) => 790,
chr(211) => 790,
chr(212) => 890,
chr(213) => 823,
chr(214) => 549,
chr(215) => 250,
chr(216) => 713,
chr(217) => 603,
chr(218) => 603,
chr(219) => 1042,
chr(220) => 987,
chr(221) => 603,
chr(222) => 987,
chr(223) => 603,
chr(224) => 494,
chr(225) => 329,
chr(226) => 790,
chr(227) => 790,
chr(228) => 786,
chr(229) => 713,
chr(230) => 384,
chr(231) => 384,
chr(232) => 384,
chr(233) => 384,
chr(234) => 384,
chr(235) => 384,
chr(236) => 494,
chr(237) => 494,
chr(238) => 494,
chr(239) => 494,
chr(240) => 0,
chr(241) => 329,
chr(242) => 274,
chr(243) => 686,
chr(244) => 686,
chr(245) => 686,
chr(246) => 384,
chr(247) => 384,
chr(248) => 384,
chr(249) => 384,
chr(250) => 384,
chr(251) => 384,
chr(252) => 494,
chr(253) => 494,
chr(254) => 494,
chr(255) => 0);

View File

@@ -0,0 +1,272 @@
<?php
/**
* @package File_PDF
*/
$font_widths['times'] = array(
chr(0) => 250,
chr(1) => 250,
chr(2) => 250,
chr(3) => 250,
chr(4) => 250,
chr(5) => 250,
chr(6) => 250,
chr(7) => 250,
chr(8) => 250,
chr(9) => 250,
chr(10) => 250,
chr(11) => 250,
chr(12) => 250,
chr(13) => 250,
chr(14) => 250,
chr(15) => 250,
chr(16) => 250,
chr(17) => 250,
chr(18) => 250,
chr(19) => 250,
chr(20) => 250,
chr(21) => 250,
chr(22) => 250,
chr(23) => 250,
chr(24) => 250,
chr(25) => 250,
chr(26) => 250,
chr(27) => 250,
chr(28) => 250,
chr(29) => 250,
chr(30) => 250,
chr(31) => 250,
' ' => 250,
'!' => 333,
'"' => 408,
'#' => 500,
'$' => 500,
'%' => 833,
'&' => 778,
'\'' => 180,
'(' => 333,
')' => 333,
'*' => 500,
'+' => 564,
',' => 250,
'-' => 333,
'.' => 250,
'/' => 278,
'0' => 500,
'1' => 500,
'2' => 500,
'3' => 500,
'4' => 500,
'5' => 500,
'6' => 500,
'7' => 500,
'8' => 500,
'9' => 500,
':' => 278,
';' => 278,
'<' => 564,
'=' => 564,
'>' => 564,
'?' => 444,
'@' => 921,
'A' => 722,
'B' => 667,
'C' => 667,
'D' => 722,
'E' => 611,
'F' => 556,
'G' => 722,
'H' => 722,
'I' => 333,
'J' => 389,
'K' => 722,
'L' => 611,
'M' => 889,
'N' => 722,
'O' => 722,
'P' => 556,
'Q' => 722,
'R' => 667,
'S' => 556,
'T' => 611,
'U' => 722,
'V' => 722,
'W' => 944,
'X' => 722,
'Y' => 722,
'Z' => 611,
'[' => 333,
'\\' => 278,
']' => 333,
'^' => 469,
'_' => 500,
'`' => 333,
'a' => 444,
'b' => 500,
'c' => 444,
'd' => 500,
'e' => 444,
'f' => 333,
'g' => 500,
'h' => 500,
'i' => 278,
'j' => 278,
'k' => 500,
'l' => 278,
'm' => 778,
'n' => 500,
'o' => 500,
'p' => 500,
'q' => 500,
'r' => 333,
's' => 389,
't' => 278,
'u' => 500,
'v' => 500,
'w' => 722,
'x' => 500,
'y' => 500,
'z' => 444,
'{' => 480,
'|' => 200,
'}' => 480,
'~' => 541,
chr(127) => 350,
chr(128) => 500,
chr(129) => 350,
chr(130) => 333,
chr(131) => 500,
chr(132) => 444,
chr(133) => 1000,
chr(134) => 500,
chr(135) => 500,
chr(136) => 333,
chr(137) => 1000,
chr(138) => 556,
chr(139) => 333,
chr(140) => 889,
chr(141) => 350,
chr(142) => 611,
chr(143) => 350,
chr(144) => 350,
chr(145) => 333,
chr(146) => 333,
chr(147) => 444,
chr(148) => 444,
chr(149) => 350,
chr(150) => 500,
chr(151) => 1000,
chr(152) => 333,
chr(153) => 980,
chr(154) => 389,
chr(155) => 333,
chr(156) => 722,
chr(157) => 350,
chr(158) => 444,
chr(159) => 722,
chr(160) => 250,
chr(161) => 333,
chr(162) => 500,
chr(163) => 500,
chr(164) => 500,
chr(165) => 500,
chr(166) => 200,
chr(167) => 500,
chr(168) => 333,
chr(169) => 760,
chr(170) => 276,
chr(171) => 500,
chr(172) => 564,
chr(173) => 333,
chr(174) => 760,
chr(175) => 333,
chr(176) => 400,
chr(177) => 564,
chr(178) => 300,
chr(179) => 300,
chr(180) => 333,
chr(181) => 500,
chr(182) => 453,
chr(183) => 250,
chr(184) => 333,
chr(185) => 300,
chr(186) => 310,
chr(187) => 500,
chr(188) => 750,
chr(189) => 750,
chr(190) => 750,
chr(191) => 444,
chr(192) => 722,
chr(193) => 722,
chr(194) => 722,
chr(195) => 722,
chr(196) => 722,
chr(197) => 722,
chr(198) => 889,
chr(199) => 667,
chr(200) => 611,
chr(201) => 611,
chr(202) => 611,
chr(203) => 611,
chr(204) => 333,
chr(205) => 333,
chr(206) => 333,
chr(207) => 333,
chr(208) => 722,
chr(209) => 722,
chr(210) => 722,
chr(211) => 722,
chr(212) => 722,
chr(213) => 722,
chr(214) => 722,
chr(215) => 564,
chr(216) => 722,
chr(217) => 722,
chr(218) => 722,
chr(219) => 722,
chr(220) => 722,
chr(221) => 722,
chr(222) => 556,
chr(223) => 500,
chr(224) => 444,
chr(225) => 444,
chr(226) => 444,
chr(227) => 444,
chr(228) => 444,
chr(229) => 444,
chr(230) => 667,
chr(231) => 444,
chr(232) => 444,
chr(233) => 444,
chr(234) => 444,
chr(235) => 444,
chr(236) => 278,
chr(237) => 278,
chr(238) => 278,
chr(239) => 278,
chr(240) => 500,
chr(241) => 500,
chr(242) => 500,
chr(243) => 500,
chr(244) => 500,
chr(245) => 500,
chr(246) => 500,
chr(247) => 564,
chr(248) => 500,
chr(249) => 500,
chr(250) => 500,
chr(251) => 500,
chr(252) => 500,
chr(253) => 500,
chr(254) => 500,
chr(255) => 500);

View File

@@ -0,0 +1,272 @@
<?php
/**
* @package File_PDF
*/
$font_widths['timesB'] = array(
chr(0) => 250,
chr(1) => 250,
chr(2) => 250,
chr(3) => 250,
chr(4) => 250,
chr(5) => 250,
chr(6) => 250,
chr(7) => 250,
chr(8) => 250,
chr(9) => 250,
chr(10) => 250,
chr(11) => 250,
chr(12) => 250,
chr(13) => 250,
chr(14) => 250,
chr(15) => 250,
chr(16) => 250,
chr(17) => 250,
chr(18) => 250,
chr(19) => 250,
chr(20) => 250,
chr(21) => 250,
chr(22) => 250,
chr(23) => 250,
chr(24) => 250,
chr(25) => 250,
chr(26) => 250,
chr(27) => 250,
chr(28) => 250,
chr(29) => 250,
chr(30) => 250,
chr(31) => 250,
' ' => 250,
'!' => 333,
'"' => 555,
'#' => 500,
'$' => 500,
'%' => 1000,
'&' => 833,
'\'' => 278,
'(' => 333,
')' => 333,
'*' => 500,
'+' => 570,
',' => 250,
'-' => 333,
'.' => 250,
'/' => 278,
'0' => 500,
'1' => 500,
'2' => 500,
'3' => 500,
'4' => 500,
'5' => 500,
'6' => 500,
'7' => 500,
'8' => 500,
'9' => 500,
':' => 333,
';' => 333,
'<' => 570,
'=' => 570,
'>' => 570,
'?' => 500,
'@' => 930,
'A' => 722,
'B' => 667,
'C' => 722,
'D' => 722,
'E' => 667,
'F' => 611,
'G' => 778,
'H' => 778,
'I' => 389,
'J' => 500,
'K' => 778,
'L' => 667,
'M' => 944,
'N' => 722,
'O' => 778,
'P' => 611,
'Q' => 778,
'R' => 722,
'S' => 556,
'T' => 667,
'U' => 722,
'V' => 722,
'W' => 1000,
'X' => 722,
'Y' => 722,
'Z' => 667,
'[' => 333,
'\\' => 278,
']' => 333,
'^' => 581,
'_' => 500,
'`' => 333,
'a' => 500,
'b' => 556,
'c' => 444,
'd' => 556,
'e' => 444,
'f' => 333,
'g' => 500,
'h' => 556,
'i' => 278,
'j' => 333,
'k' => 556,
'l' => 278,
'm' => 833,
'n' => 556,
'o' => 500,
'p' => 556,
'q' => 556,
'r' => 444,
's' => 389,
't' => 333,
'u' => 556,
'v' => 500,
'w' => 722,
'x' => 500,
'y' => 500,
'z' => 444,
'{' => 394,
'|' => 220,
'}' => 394,
'~' => 520,
chr(127) => 350,
chr(128) => 500,
chr(129) => 350,
chr(130) => 333,
chr(131) => 500,
chr(132) => 500,
chr(133) => 1000,
chr(134) => 500,
chr(135) => 500,
chr(136) => 333,
chr(137) => 1000,
chr(138) => 556,
chr(139) => 333,
chr(140) => 1000,
chr(141) => 350,
chr(142) => 667,
chr(143) => 350,
chr(144) => 350,
chr(145) => 333,
chr(146) => 333,
chr(147) => 500,
chr(148) => 500,
chr(149) => 350,
chr(150) => 500,
chr(151) => 1000,
chr(152) => 333,
chr(153) => 1000,
chr(154) => 389,
chr(155) => 333,
chr(156) => 722,
chr(157) => 350,
chr(158) => 444,
chr(159) => 722,
chr(160) => 250,
chr(161) => 333,
chr(162) => 500,
chr(163) => 500,
chr(164) => 500,
chr(165) => 500,
chr(166) => 220,
chr(167) => 500,
chr(168) => 333,
chr(169) => 747,
chr(170) => 300,
chr(171) => 500,
chr(172) => 570,
chr(173) => 333,
chr(174) => 747,
chr(175) => 333,
chr(176) => 400,
chr(177) => 570,
chr(178) => 300,
chr(179) => 300,
chr(180) => 333,
chr(181) => 556,
chr(182) => 540,
chr(183) => 250,
chr(184) => 333,
chr(185) => 300,
chr(186) => 330,
chr(187) => 500,
chr(188) => 750,
chr(189) => 750,
chr(190) => 750,
chr(191) => 500,
chr(192) => 722,
chr(193) => 722,
chr(194) => 722,
chr(195) => 722,
chr(196) => 722,
chr(197) => 722,
chr(198) => 1000,
chr(199) => 722,
chr(200) => 667,
chr(201) => 667,
chr(202) => 667,
chr(203) => 667,
chr(204) => 389,
chr(205) => 389,
chr(206) => 389,
chr(207) => 389,
chr(208) => 722,
chr(209) => 722,
chr(210) => 778,
chr(211) => 778,
chr(212) => 778,
chr(213) => 778,
chr(214) => 778,
chr(215) => 570,
chr(216) => 778,
chr(217) => 722,
chr(218) => 722,
chr(219) => 722,
chr(220) => 722,
chr(221) => 722,
chr(222) => 611,
chr(223) => 556,
chr(224) => 500,
chr(225) => 500,
chr(226) => 500,
chr(227) => 500,
chr(228) => 500,
chr(229) => 500,
chr(230) => 722,
chr(231) => 444,
chr(232) => 444,
chr(233) => 444,
chr(234) => 444,
chr(235) => 444,
chr(236) => 278,
chr(237) => 278,
chr(238) => 278,
chr(239) => 278,
chr(240) => 500,
chr(241) => 556,
chr(242) => 500,
chr(243) => 500,
chr(244) => 500,
chr(245) => 500,
chr(246) => 500,
chr(247) => 570,
chr(248) => 500,
chr(249) => 556,
chr(250) => 556,
chr(251) => 556,
chr(252) => 556,
chr(253) => 500,
chr(254) => 556,
chr(255) => 500);

View File

@@ -0,0 +1,272 @@
<?php
/**
* @package File_PDF
*/
$font_widths['timesBI'] = array(
chr(0) => 250,
chr(1) => 250,
chr(2) => 250,
chr(3) => 250,
chr(4) => 250,
chr(5) => 250,
chr(6) => 250,
chr(7) => 250,
chr(8) => 250,
chr(9) => 250,
chr(10) => 250,
chr(11) => 250,
chr(12) => 250,
chr(13) => 250,
chr(14) => 250,
chr(15) => 250,
chr(16) => 250,
chr(17) => 250,
chr(18) => 250,
chr(19) => 250,
chr(20) => 250,
chr(21) => 250,
chr(22) => 250,
chr(23) => 250,
chr(24) => 250,
chr(25) => 250,
chr(26) => 250,
chr(27) => 250,
chr(28) => 250,
chr(29) => 250,
chr(30) => 250,
chr(31) => 250,
' ' => 250,
'!' => 389,
'"' => 555,
'#' => 500,
'$' => 500,
'%' => 833,
'&' => 778,
'\'' => 278,
'(' => 333,
')' => 333,
'*' => 500,
'+' => 570,
',' => 250,
'-' => 333,
'.' => 250,
'/' => 278,
'0' => 500,
'1' => 500,
'2' => 500,
'3' => 500,
'4' => 500,
'5' => 500,
'6' => 500,
'7' => 500,
'8' => 500,
'9' => 500,
':' => 333,
';' => 333,
'<' => 570,
'=' => 570,
'>' => 570,
'?' => 500,
'@' => 832,
'A' => 667,
'B' => 667,
'C' => 667,
'D' => 722,
'E' => 667,
'F' => 667,
'G' => 722,
'H' => 778,
'I' => 389,
'J' => 500,
'K' => 667,
'L' => 611,
'M' => 889,
'N' => 722,
'O' => 722,
'P' => 611,
'Q' => 722,
'R' => 667,
'S' => 556,
'T' => 611,
'U' => 722,
'V' => 667,
'W' => 889,
'X' => 667,
'Y' => 611,
'Z' => 611,
'[' => 333,
'\\' => 278,
']' => 333,
'^' => 570,
'_' => 500,
'`' => 333,
'a' => 500,
'b' => 500,
'c' => 444,
'd' => 500,
'e' => 444,
'f' => 333,
'g' => 500,
'h' => 556,
'i' => 278,
'j' => 278,
'k' => 500,
'l' => 278,
'm' => 778,
'n' => 556,
'o' => 500,
'p' => 500,
'q' => 500,
'r' => 389,
's' => 389,
't' => 278,
'u' => 556,
'v' => 444,
'w' => 667,
'x' => 500,
'y' => 444,
'z' => 389,
'{' => 348,
'|' => 220,
'}' => 348,
'~' => 570,
chr(127) => 350,
chr(128) => 500,
chr(129) => 350,
chr(130) => 333,
chr(131) => 500,
chr(132) => 500,
chr(133) => 1000,
chr(134) => 500,
chr(135) => 500,
chr(136) => 333,
chr(137) => 1000,
chr(138) => 556,
chr(139) => 333,
chr(140) => 944,
chr(141) => 350,
chr(142) => 611,
chr(143) => 350,
chr(144) => 350,
chr(145) => 333,
chr(146) => 333,
chr(147) => 500,
chr(148) => 500,
chr(149) => 350,
chr(150) => 500,
chr(151) => 1000,
chr(152) => 333,
chr(153) => 1000,
chr(154) => 389,
chr(155) => 333,
chr(156) => 722,
chr(157) => 350,
chr(158) => 389,
chr(159) => 611,
chr(160) => 250,
chr(161) => 389,
chr(162) => 500,
chr(163) => 500,
chr(164) => 500,
chr(165) => 500,
chr(166) => 220,
chr(167) => 500,
chr(168) => 333,
chr(169) => 747,
chr(170) => 266,
chr(171) => 500,
chr(172) => 606,
chr(173) => 333,
chr(174) => 747,
chr(175) => 333,
chr(176) => 400,
chr(177) => 570,
chr(178) => 300,
chr(179) => 300,
chr(180) => 333,
chr(181) => 576,
chr(182) => 500,
chr(183) => 250,
chr(184) => 333,
chr(185) => 300,
chr(186) => 300,
chr(187) => 500,
chr(188) => 750,
chr(189) => 750,
chr(190) => 750,
chr(191) => 500,
chr(192) => 667,
chr(193) => 667,
chr(194) => 667,
chr(195) => 667,
chr(196) => 667,
chr(197) => 667,
chr(198) => 944,
chr(199) => 667,
chr(200) => 667,
chr(201) => 667,
chr(202) => 667,
chr(203) => 667,
chr(204) => 389,
chr(205) => 389,
chr(206) => 389,
chr(207) => 389,
chr(208) => 722,
chr(209) => 722,
chr(210) => 722,
chr(211) => 722,
chr(212) => 722,
chr(213) => 722,
chr(214) => 722,
chr(215) => 570,
chr(216) => 722,
chr(217) => 722,
chr(218) => 722,
chr(219) => 722,
chr(220) => 722,
chr(221) => 611,
chr(222) => 611,
chr(223) => 500,
chr(224) => 500,
chr(225) => 500,
chr(226) => 500,
chr(227) => 500,
chr(228) => 500,
chr(229) => 500,
chr(230) => 722,
chr(231) => 444,
chr(232) => 444,
chr(233) => 444,
chr(234) => 444,
chr(235) => 444,
chr(236) => 278,
chr(237) => 278,
chr(238) => 278,
chr(239) => 278,
chr(240) => 500,
chr(241) => 556,
chr(242) => 500,
chr(243) => 500,
chr(244) => 500,
chr(245) => 500,
chr(246) => 500,
chr(247) => 570,
chr(248) => 500,
chr(249) => 556,
chr(250) => 556,
chr(251) => 556,
chr(252) => 556,
chr(253) => 444,
chr(254) => 500,
chr(255) => 444);

View File

@@ -0,0 +1,272 @@
<?php
/**
* @package File_PDF
*/
$font_widths['timesI'] = array(
chr(0) => 250,
chr(1) => 250,
chr(2) => 250,
chr(3) => 250,
chr(4) => 250,
chr(5) => 250,
chr(6) => 250,
chr(7) => 250,
chr(8) => 250,
chr(9) => 250,
chr(10) => 250,
chr(11) => 250,
chr(12) => 250,
chr(13) => 250,
chr(14) => 250,
chr(15) => 250,
chr(16) => 250,
chr(17) => 250,
chr(18) => 250,
chr(19) => 250,
chr(20) => 250,
chr(21) => 250,
chr(22) => 250,
chr(23) => 250,
chr(24) => 250,
chr(25) => 250,
chr(26) => 250,
chr(27) => 250,
chr(28) => 250,
chr(29) => 250,
chr(30) => 250,
chr(31) => 250,
' ' => 250,
'!' => 333,
'"' => 420,
'#' => 500,
'$' => 500,
'%' => 833,
'&' => 778,
'\'' => 214,
'(' => 333,
')' => 333,
'*' => 500,
'+' => 675,
',' => 250,
'-' => 333,
'.' => 250,
'/' => 278,
'0' => 500,
'1' => 500,
'2' => 500,
'3' => 500,
'4' => 500,
'5' => 500,
'6' => 500,
'7' => 500,
'8' => 500,
'9' => 500,
':' => 333,
';' => 333,
'<' => 675,
'=' => 675,
'>' => 675,
'?' => 500,
'@' => 920,
'A' => 611,
'B' => 611,
'C' => 667,
'D' => 722,
'E' => 611,
'F' => 611,
'G' => 722,
'H' => 722,
'I' => 333,
'J' => 444,
'K' => 667,
'L' => 556,
'M' => 833,
'N' => 667,
'O' => 722,
'P' => 611,
'Q' => 722,
'R' => 611,
'S' => 500,
'T' => 556,
'U' => 722,
'V' => 611,
'W' => 833,
'X' => 611,
'Y' => 556,
'Z' => 556,
'[' => 389,
'\\' => 278,
']' => 389,
'^' => 422,
'_' => 500,
'`' => 333,
'a' => 500,
'b' => 500,
'c' => 444,
'd' => 500,
'e' => 444,
'f' => 278,
'g' => 500,
'h' => 500,
'i' => 278,
'j' => 278,
'k' => 444,
'l' => 278,
'm' => 722,
'n' => 500,
'o' => 500,
'p' => 500,
'q' => 500,
'r' => 389,
's' => 389,
't' => 278,
'u' => 500,
'v' => 444,
'w' => 667,
'x' => 444,
'y' => 444,
'z' => 389,
'{' => 400,
'|' => 275,
'}' => 400,
'~' => 541,
chr(127) => 350,
chr(128) => 500,
chr(129) => 350,
chr(130) => 333,
chr(131) => 500,
chr(132) => 556,
chr(133) => 889,
chr(134) => 500,
chr(135) => 500,
chr(136) => 333,
chr(137) => 1000,
chr(138) => 500,
chr(139) => 333,
chr(140) => 944,
chr(141) => 350,
chr(142) => 556,
chr(143) => 350,
chr(144) => 350,
chr(145) => 333,
chr(146) => 333,
chr(147) => 556,
chr(148) => 556,
chr(149) => 350,
chr(150) => 500,
chr(151) => 889,
chr(152) => 333,
chr(153) => 980,
chr(154) => 389,
chr(155) => 333,
chr(156) => 667,
chr(157) => 350,
chr(158) => 389,
chr(159) => 556,
chr(160) => 250,
chr(161) => 389,
chr(162) => 500,
chr(163) => 500,
chr(164) => 500,
chr(165) => 500,
chr(166) => 275,
chr(167) => 500,
chr(168) => 333,
chr(169) => 760,
chr(170) => 276,
chr(171) => 500,
chr(172) => 675,
chr(173) => 333,
chr(174) => 760,
chr(175) => 333,
chr(176) => 400,
chr(177) => 675,
chr(178) => 300,
chr(179) => 300,
chr(180) => 333,
chr(181) => 500,
chr(182) => 523,
chr(183) => 250,
chr(184) => 333,
chr(185) => 300,
chr(186) => 310,
chr(187) => 500,
chr(188) => 750,
chr(189) => 750,
chr(190) => 750,
chr(191) => 500,
chr(192) => 611,
chr(193) => 611,
chr(194) => 611,
chr(195) => 611,
chr(196) => 611,
chr(197) => 611,
chr(198) => 889,
chr(199) => 667,
chr(200) => 611,
chr(201) => 611,
chr(202) => 611,
chr(203) => 611,
chr(204) => 333,
chr(205) => 333,
chr(206) => 333,
chr(207) => 333,
chr(208) => 722,
chr(209) => 667,
chr(210) => 722,
chr(211) => 722,
chr(212) => 722,
chr(213) => 722,
chr(214) => 722,
chr(215) => 675,
chr(216) => 722,
chr(217) => 722,
chr(218) => 722,
chr(219) => 722,
chr(220) => 722,
chr(221) => 556,
chr(222) => 611,
chr(223) => 500,
chr(224) => 500,
chr(225) => 500,
chr(226) => 500,
chr(227) => 500,
chr(228) => 500,
chr(229) => 500,
chr(230) => 667,
chr(231) => 444,
chr(232) => 444,
chr(233) => 444,
chr(234) => 444,
chr(235) => 444,
chr(236) => 278,
chr(237) => 278,
chr(238) => 278,
chr(239) => 278,
chr(240) => 500,
chr(241) => 500,
chr(242) => 500,
chr(243) => 500,
chr(244) => 500,
chr(245) => 500,
chr(246) => 500,
chr(247) => 675,
chr(248) => 500,
chr(249) => 500,
chr(250) => 500,
chr(251) => 500,
chr(252) => 500,
chr(253) => 444,
chr(254) => 500,
chr(255) => 444);

View File

@@ -0,0 +1,272 @@
<?php
/**
* @package File_PDF
*/
$font_widths['zapfdingbats'] = array(
chr(0) => 0,
chr(1) => 0,
chr(2) => 0,
chr(3) => 0,
chr(4) => 0,
chr(5) => 0,
chr(6) => 0,
chr(7) => 0,
chr(8) => 0,
chr(9) => 0,
chr(10) => 0,
chr(11) => 0,
chr(12) => 0,
chr(13) => 0,
chr(14) => 0,
chr(15) => 0,
chr(16) => 0,
chr(17) => 0,
chr(18) => 0,
chr(19) => 0,
chr(20) => 0,
chr(21) => 0,
chr(22) => 0,
chr(23) => 0,
chr(24) => 0,
chr(25) => 0,
chr(26) => 0,
chr(27) => 0,
chr(28) => 0,
chr(29) => 0,
chr(30) => 0,
chr(31) => 0,
' ' => 278,
'!' => 974,
'"' => 961,
'#' => 974,
'$' => 980,
'%' => 719,
'&' => 789,
'\'' => 790,
'(' => 791,
')' => 690,
'*' => 960,
'+' => 939,
',' => 549,
'-' => 855,
'.' => 911,
'/' => 933,
'0' => 911,
'1' => 945,
'2' => 974,
'3' => 755,
'4' => 846,
'5' => 762,
'6' => 761,
'7' => 571,
'8' => 677,
'9' => 763,
':' => 760,
';' => 759,
'<' => 754,
'=' => 494,
'>' => 552,
'?' => 537,
'@' => 577,
'A' => 692,
'B' => 786,
'C' => 788,
'D' => 788,
'E' => 790,
'F' => 793,
'G' => 794,
'H' => 816,
'I' => 823,
'J' => 789,
'K' => 841,
'L' => 823,
'M' => 833,
'N' => 816,
'O' => 831,
'P' => 923,
'Q' => 744,
'R' => 723,
'S' => 749,
'T' => 790,
'U' => 792,
'V' => 695,
'W' => 776,
'X' => 768,
'Y' => 792,
'Z' => 759,
'[' => 707,
'\\' => 708,
']' => 682,
'^' => 701,
'_' => 826,
'`' => 815,
'a' => 789,
'b' => 789,
'c' => 707,
'd' => 687,
'e' => 696,
'f' => 689,
'g' => 786,
'h' => 787,
'i' => 713,
'j' => 791,
'k' => 785,
'l' => 791,
'm' => 873,
'n' => 761,
'o' => 762,
'p' => 762,
'q' => 759,
'r' => 759,
's' => 892,
't' => 892,
'u' => 788,
'v' => 784,
'w' => 438,
'x' => 138,
'y' => 277,
'z' => 415,
'{' => 392,
'|' => 392,
'}' => 668,
'~' => 668,
chr(127) => 0,
chr(128) => 390,
chr(129) => 390,
chr(130) => 317,
chr(131) => 317,
chr(132) => 276,
chr(133) => 276,
chr(134) => 509,
chr(135) => 509,
chr(136) => 410,
chr(137) => 410,
chr(138) => 234,
chr(139) => 234,
chr(140) => 334,
chr(141) => 334,
chr(142) => 0,
chr(143) => 0,
chr(144) => 0,
chr(145) => 0,
chr(146) => 0,
chr(147) => 0,
chr(148) => 0,
chr(149) => 0,
chr(150) => 0,
chr(151) => 0,
chr(152) => 0,
chr(153) => 0,
chr(154) => 0,
chr(155) => 0,
chr(156) => 0,
chr(157) => 0,
chr(158) => 0,
chr(159) => 0,
chr(160) => 0,
chr(161) => 732,
chr(162) => 544,
chr(163) => 544,
chr(164) => 910,
chr(165) => 667,
chr(166) => 760,
chr(167) => 760,
chr(168) => 776,
chr(169) => 595,
chr(170) => 694,
chr(171) => 626,
chr(172) => 788,
chr(173) => 788,
chr(174) => 788,
chr(175) => 788,
chr(176) => 788,
chr(177) => 788,
chr(178) => 788,
chr(179) => 788,
chr(180) => 788,
chr(181) => 788,
chr(182) => 788,
chr(183) => 788,
chr(184) => 788,
chr(185) => 788,
chr(186) => 788,
chr(187) => 788,
chr(188) => 788,
chr(189) => 788,
chr(190) => 788,
chr(191) => 788,
chr(192) => 788,
chr(193) => 788,
chr(194) => 788,
chr(195) => 788,
chr(196) => 788,
chr(197) => 788,
chr(198) => 788,
chr(199) => 788,
chr(200) => 788,
chr(201) => 788,
chr(202) => 788,
chr(203) => 788,
chr(204) => 788,
chr(205) => 788,
chr(206) => 788,
chr(207) => 788,
chr(208) => 788,
chr(209) => 788,
chr(210) => 788,
chr(211) => 788,
chr(212) => 894,
chr(213) => 838,
chr(214) => 1016,
chr(215) => 458,
chr(216) => 748,
chr(217) => 924,
chr(218) => 748,
chr(219) => 918,
chr(220) => 927,
chr(221) => 928,
chr(222) => 928,
chr(223) => 834,
chr(224) => 873,
chr(225) => 828,
chr(226) => 924,
chr(227) => 924,
chr(228) => 917,
chr(229) => 930,
chr(230) => 931,
chr(231) => 463,
chr(232) => 883,
chr(233) => 836,
chr(234) => 836,
chr(235) => 867,
chr(236) => 867,
chr(237) => 696,
chr(238) => 696,
chr(239) => 874,
chr(240) => 0,
chr(241) => 874,
chr(242) => 760,
chr(243) => 946,
chr(244) => 771,
chr(245) => 865,
chr(246) => 771,
chr(247) => 888,
chr(248) => 967,
chr(249) => 888,
chr(250) => 831,
chr(251) => 873,
chr(252) => 927,
chr(253) => 970,
chr(254) => 918,
chr(255) => 0);

View File

@@ -0,0 +1,416 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd
*
* 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 FileFormats
* @package File_Passwd
* @author Michael Wallner <mike@php.net>
* @copyright 2003-2005 Michael Wallner
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Passwd.php,v 1.25 2005/04/14 15:00:30 mike Exp $
* @link http://pear.php.net/package/File_Passwd
*/
/**
* Requires PEAR.
*/
require_once 'PEAR.php';
/**
* Encryption constants.
*/
// SHA encryption.
define('FILE_PASSWD_SHA', 'sha');
// MD5 encryption
define('FILE_PASSWD_MD5', 'md5');
// DES encryption
define('FILE_PASSWD_DES', 'des');
// NT hash encryption.
define('FILE_PASSWD_NT', 'nt');
// LM hash encryption.
define('FILE_PASSWD_LM', 'lm');
// PLAIN (no encryption)
define('FILE_PASSWD_PLAIN', 'plain');
/**
* Error constants.
*/
// Undefined error.
define('FILE_PASSWD_E_UNDEFINED', 0);
// Invalid file format.
define('FILE_PASSWD_E_INVALID_FORMAT', 1);
define('FILE_PASSWD_E_INVALID_FORMAT_STR', 'Passwd file has invalid format.');
// Invalid extra property.
define('FILE_PASSWD_E_INVALID_PROPERTY', 2);
define('FILE_PASSWD_E_INVALID_PROPERTY_STR', 'Invalid property \'%s\'.');
// Invalid characters.
define('FILE_PASSWD_E_INVALID_CHARS', 3);
define('FILE_PASSWD_E_INVALID_CHARS_STR', '%s\'%s\' contains illegal characters.');
// Invalid encryption mode.
define('FILE_PASSWD_E_INVALID_ENC_MODE', 4);
define('FILE_PASSWD_E_INVALID_ENC_MODE_STR', 'Encryption mode \'%s\' not supported.');
// Exists already.
define('FILE_PASSWD_E_EXISTS_ALREADY', 5);
define('FILE_PASSWD_E_EXISTS_ALREADY_STR', '%s\'%s\' already exists.');
// Exists not.
define('FILE_PASSWD_E_EXISTS_NOT', 6);
define('FILE_PASSWD_E_EXISTS_NOT_STR', '%s\'%s\' doesn\'t exist.');
// User not in group.
define('FILE_PASSWD_E_USER_NOT_IN_GROUP', 7);
define('FILE_PASSWD_E_USER_NOT_IN_GROUP_STR', 'User \'%s\' doesn\'t exist in group \'%s\'.');
// User not in realm.
define('FILE_PASSWD_E_USER_NOT_IN_REALM', 8);
define('FILE_PASSWD_E_USER_NOT_IN_REALM_STR', 'User \'%s\' doesn\'t exist in realm \'%s\'.');
// Parameter must be of type array.
define('FILE_PASSWD_E_PARAM_MUST_BE_ARRAY', 9);
define('FILE_PASSWD_E_PARAM_MUST_BE_ARRAY_STR', 'Parameter %s must be of type array.');
// Method not implemented.
define('FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED', 10);
define('FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR', 'Method \'%s()\' not implemented.');
// Directory couldn't be created.
define('FILE_PASSWD_E_DIR_NOT_CREATED', 11);
define('FILE_PASSWD_E_DIR_NOT_CREATED_STR', 'Couldn\'t create directory \'%s\'.');
// File couldn't be opened.
define('FILE_PASSWD_E_FILE_NOT_OPENED', 12);
define('FILE_PASSWD_E_FILE_NOT_OPENED_STR', 'Couldn\'t open file \'%s\'.');
// File coudn't be locked.
define('FILE_PASSWD_E_FILE_NOT_LOCKED', 13);
define('FILE_PASSWD_E_FILE_NOT_LOCKED_STR', 'Couldn\'t lock file \'%s\'.');
// File couldn't be unlocked.
define('FILE_PASSWD_E_FILE_NOT_UNLOCKED', 14);
define('FILE_PASSWD_E_FILE_NOT_UNLOCKED_STR', 'Couldn\'t unlock file.');
// File couldn't be closed.
define('FILE_PASSWD_E_FILE_NOT_CLOSED', 15);
define('FILE_PASSWD_E_FILE_NOT_CLOSED_STR', 'Couldn\'t close file.');
/**
* Allowed 64 chars for salts
*/
$GLOBALS['_FILE_PASSWD_64'] =
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
/**
* The package File_Passwd provides classes and methods
* to handle many different kinds of passwd files.
*
* The File_Passwd class in certain is a factory container for its special
* purpose extension classes, each handling a specific passwd file format.
* It also provides a static method for reasonable fast user authentication.
* Beside that it offers some encryption methods used by the extensions.
*
* @author Michael Wallner <mike@php.net>
* @version $Revision: 1.25 $
*
* Usage Example:
* <code>
* $passwd = &File_Passwd::factory('Unix');
* </code>
*/
class File_Passwd
{
/**
* Get API version
*
* @static
* @access public
* @return string API version
*/
function apiVersion()
{
return '1.0.0';
}
/**
* Generate salt
*
* @access public
* @return mixed
* @param int $length salt length
* @return string the salt
*/
function salt($length = 2)
{
$salt = '';
$length = (int) $length;
$length < 2 && $length = 2;
for($i = 0; $i < $length; $i++) {
$salt .= $GLOBALS['_FILE_PASSWD_64'][rand(0, 63)];
}
return $salt;
}
/**
* No encryption (plaintext)
*
* @access public
* @return string plaintext input
* @param string plaintext passwd
*/
function crypt_plain($plain)
{
return $plain;
}
/**
* DES encryption
*
* @static
* @access public
* @return string crypted text
* @param string $plain plaintext to encrypt
* @param string $salt the salt to use for encryption (2 chars)
*/
function crypt_des($plain, $salt = null)
{
(is_null($salt) || strlen($salt) < 2) && $salt = File_Passwd::salt(2);
return crypt($plain, $salt);
}
/**
* MD5 encryption
*
* @static
* @access public
* @return string crypted text
* @param string $plain plaintext to encrypt
* @param string $salt the salt to use for encryption
* (>2 chars starting with $1$)
*/
function crypt_md5($plain, $salt = null)
{
if (
is_null($salt) ||
strlen($salt) < 3 ||
!preg_match('/^\$1\$/', $salt))
{
$salt = '$1$' . File_Passwd::salt(8);
}
return crypt($plain, $salt);
}
/**
* SHA1 encryption
*
* Returns a PEAR_Error if sha1() is not available (PHP<4.3).
*
* @static
* @throws PEAR_Error
* @access public
* @return mixed crypted string or PEAR_Error
* @param string $plain plaintext to encrypt
*/
function crypt_sha($plain)
{
if (!function_exists('sha1')) {
return PEAR::raiseError(
'SHA1 encryption is not available (PHP < 4.3).',
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
$hash = PEAR_ZE2 ? sha1($plain, true) : pack('H40', sha1($plain));
return '{SHA}' . base64_encode($hash);
}
/**
* APR compatible MD5 encryption
*
* @access public
* @return mixed
* @param string $plain plaintext to crypt
* @param string $salt the salt to use for encryption
*/
function crypt_apr_md5($plain, $salt = null)
{
if (is_null($salt)) {
$salt = File_Passwd::salt(8);
} elseif (preg_match('/^\$apr1\$/', $salt)) {
$salt = preg_replace('/^\$apr1\$([^$]+)\$.*/', '\\1', $salt);
} else {
$salt = substr($salt, 0,8);
}
$length = strlen($plain);
$context = $plain . '$apr1$' . $salt;
if (PEAR_ZE2) {
$binary = md5($plain . $salt . $plain, true);
} else {
$binary = pack('H32', md5($plain . $salt . $plain));
}
for ($i = $length; $i > 0; $i -= 16) {
$context .= substr($binary, 0, min(16 , $i));
}
for ( $i = $length; $i > 0; $i >>= 1) {
$context .= ($i & 1) ? chr(0) : $plain[0];
}
$binary = PEAR_ZE2 ? md5($context, true) : pack('H32', md5($context));
for($i = 0; $i < 1000; $i++) {
$new = ($i & 1) ? $plain : $binary;
if ($i % 3) {
$new .= $salt;
}
if ($i % 7) {
$new .= $plain;
}
$new .= ($i & 1) ? $binary : $plain;
$binary = PEAR_ZE2 ? md5($new, true) : pack('H32', md5($new));
}
$p = array();
for ($i = 0; $i < 5; $i++) {
$k = $i + 6;
$j = $i + 12;
if ($j == 16) {
$j = 5;
}
$p[] = File_Passwd::_64(
(ord($binary[$i]) << 16) |
(ord($binary[$k]) << 8) |
(ord($binary[$j])),
5
);
}
return
'$apr1$' . $salt . '$' . implode($p) .
File_Passwd::_64(ord($binary[11]), 3);
}
/**
* Convert hexadecimal string to binary data
*
* @static
* @access private
* @return mixed
* @param string $hex
*/
function _hexbin($hex)
{
$rs = '';
$ln = strlen($hex);
for($i = 0; $i < $ln; $i += 2) {
$rs .= chr(hexdec($hex{$i} . $hex{$i+1}));
}
return $rs;
}
/**
* Convert to allowed 64 characters for encryption
*
* @static
* @access private
* @return string
* @param string $value
* @param int $count
*/
function _64($value, $count)
{
$result = '';
while(--$count) {
$result .= $GLOBALS['_FILE_PASSWD_64'][$value & 0x3f];
$value >>= 6;
}
return $result;
}
/**
* Factory for new extensions
*
* o Unix for standard Unix passwd files
* o CVS for CVS pserver passwd files
* o SMB for SMB server passwd files
* o Authbasic for AuthUserFiles
* o Authdigest for AuthDigestFiles
* o Custom for custom formatted passwd files
*
* Returns a PEAR_Error if the desired class/file couldn't be loaded.
*
* @static use &File_Passwd::factory() for instantiating your passwd object
*
* @throws PEAR_Error
* @access public
* @return object File_Passwd_$class - desired Passwd object
* @param string $class the desired subclass of File_Passwd
*/
function &factory($class)
{
$class = ucFirst(strToLower($class));
if (!@include_once "File/Passwd/$class.php") {
return PEAR::raiseError("Couldn't load file Passwd/$class.php", 0);
}
$class = 'File_Passwd_'.$class;
if (!class_exists($class)) {
return PEAR::raiseError("Couldn't load class $class.", 0);
}
$instance = &new $class();
return $instance;
}
/**
* Fast authentication of a certain user
*
* Though this approach should be reasonable fast, it is NOT
* with APR compatible MD5 encryption used for htpasswd style
* password files encrypted in MD5. Generating one MD5 password
* takes about 0.3 seconds!
*
* Returns a PEAR_Error if:
* o file doesn't exist
* o file couldn't be opened in read mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked (only if auth fails)
* o file couldn't be closed (only if auth fails)
* o invalid <var>$type</var> was provided
* o invalid <var>$opt</var> was provided
*
* Depending on <var>$type</var>, <var>$opt</var> should be:
* o Smb: encryption method (NT or LM)
* o Unix: encryption method (des or md5)
* o Authbasic: encryption method (des, sha or md5)
* o Authdigest: the realm the user is in
* o Cvs: n/a (empty)
* o Custom: array of 2 elements: encryption function and delimiter
*
* @static call this method statically for a reasonable fast authentication
*
* @throws PEAR_Error
* @access public
* @return return mixed true if authenticated,
* false if not or PEAR_error
*
* @param string $type Unix, Cvs, Smb, Authbasic or Authdigest
* @param string $file path to passwd file
* @param string $user the user to authenticate
* @param string $pass the plaintext password
* @param mixed $opt o Smb: NT or LM
* o Unix: des or md5
* o Authbasic des, sha or md5
* o Authdigest realm the user is in
* o Custom encryption function and
* delimiter character
*/
function staticAuth($type, $file, $user, $pass, $opt = '')
{
$type = ucFirst(strToLower($type));
if (!@include_once "File/Passwd/$type.php") {
return PEAR::raiseError("Couldn't load file Passwd/$type.php", 0);
}
$func = array('File_Passwd_' . $type, 'staticAuth');
return call_user_func($func, $file, $user, $pass, $opt);
}
}
?>

View File

@@ -0,0 +1,396 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Authbasic
*
* 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 FileFormats
* @package File_Passwd
* @author Michael Wallner <mike@php.net>
* @copyright 2003-2005 Michael Wallner
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Authbasic.php,v 1.17 2005/03/30 18:33:33 mike Exp $
* @link http://pear.php.net/package/File_Passwd
*/
/**
* Requires File::Passwd::Common
*/
require_once 'File/Passwd/Common.php';
/**
* Manipulate AuthUserFiles as used for HTTP Basic Authentication.
*
* <kbd><u>
* Usage Example:
* </u></kbd>
* <code>
* $htp = &File_Passwd::factory('AuthBasic');
* $htp->setMode('sha');
* $htp->setFile('/www/mike/auth/.htpasswd');
* $htp->load();
* $htp->addUser('mike', 'secret');
* $htp->save();
* </code>
*
* <kbd><u>
* Output of listUser()
* </u></kbd>
* <pre>
* array
* + user => crypted_passwd
* + user => crypted_passwd
* </pre>
*
* @author Michael Wallner <mike@php.net>
* @package File_Passwd
* @version $Revision: 1.17 $
* @access public
*/
class File_Passwd_Authbasic extends File_Passwd_Common
{
/**
* Path to AuthUserFile
*
* @var string
* @access private
*/
var $_file = '.htpasswd';
/**
* Actual encryption mode
*
* @var string
* @access private
*/
var $_mode = 'sha';
/**
* Supported encryption modes
*
* @var array
* @access private
*/
var $_modes = array('md5' => 'm', 'des' => 'd', 'sha' => 's');
/**
* Constructor
*
* @access public
* @param string $file path to AuthUserFile
*/
function File_Passwd_Authbasic($file = '.htpasswd')
{
File_Passwd_Authbasic::__construct($file);
}
/**
* Constructor (ZE2)
*
* Rewritten because DES encryption is not
* supportet by the Win32 httpd.
*
* @access protected
* @param string $file path to AuthUserFile
*/
function __construct($file = '.htpasswd')
{
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
unset($this->_modes['des']);
}
$this->setFile($file);
}
/**
* Fast authentication of a certain user
*
* Returns a PEAR_Error if:
* o file doesn't exist
* o file couldn't be opened in read mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked (only if auth fails)
* o file couldn't be closed (only if auth fails)
*
* @static call this method statically for a reasonable fast authentication
*
* @throws PEAR_Error
* @access public
* @return mixed true if authenticated, false if not or PEAR_Error
* @param string $file path to passwd file
* @param string $user user to authenticate
* @param string $pass plaintext password
* @param string $mode des, sha or md5
*/
function staticAuth($file, $user, $pass, $mode)
{
$line = File_Passwd_Common::_auth($file, $user);
if (!$line || PEAR::isError($line)) {
return $line;
}
list(,$real) = explode(':', $line);
$crypted = File_Passwd_Authbasic::_genPass($pass, $real, $mode);
if (PEAR::isError($crypted)) {
return $crypted;
}
return ($real === $crypted);
}
/**
* Apply changes and rewrite AuthUserFile
*
* Returns a PEAR_Error if:
* o directory in which the file should reside couldn't be created
* o file couldn't be opened in write mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked
* o file couldn't be closed
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function save()
{
$content = '';
foreach ($this->_users as $user => $pass) {
$content .= $user . ':' . $pass . "\n";
}
return $this->_save($content);
}
/**
* Add an user
*
* The username must start with an alphabetical character and must NOT
* contain any other characters than alphanumerics, the underline and dash.
*
* Returns a PEAR_Error if:
* o user already exists
* o user contains illegal characters
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
* @param string $user
* @param string $pass
*/
function addUser($user, $pass)
{
if ($this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_ALREADY
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
FILE_PASSWD_E_INVALID_CHARS
);
}
$this->_users[$user] = $this->_genPass($pass);
return true;
}
/**
* Change the password of a certain user
*
* Returns a PEAR_Error if user doesn't exist.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or a PEAR_Error
* @param string $user the user whose password should be changed
* @param string $pass the new plaintext password
*/
function changePasswd($user, $pass)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
$this->_users[$user] = $this->_genPass($pass);
return true;
}
/**
* Verify password
*
* Returns a PEAR_Error if:
* o user doesn't exist
* o an invalid encryption mode was supplied
*
* @throws PEAR_Error
* @access public
* @return mixed true if passwords equal, false if they don't, or PEAR_Error
* @param string $user the user whose password should be verified
* @param string $pass the plaintext password to verify
*/
function verifyPasswd($user, $pass)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
$real = $this->_users[$user];
return ($real === $this->_genPass($pass, $real));
}
/**
* Get actual encryption mode
*
* @access public
* @return string
*/
function getMode()
{
return $this->_mode;
}
/**
* Get supported encryption modes
*
* <pre>
* array
* + md5
* + sha
* + des
* </pre>
*
* ATTN: DES encryption not available on Win32!
*
* @access public
* @return array
*/
function listModes()
{
return array_keys($this->_modes);
}
/**
* Set the encryption mode
*
* You can choose one of md5, sha or des.
*
* ATTN: DES encryption not available on Win32!
*
* Returns a PEAR_Error if a specific encryption mode is not supported.
*
* @throws PEAR_Error
* @access public
* @return mixed true on succes or PEAR_Error
* @param string $mode
*/
function setMode($mode)
{
$mode = strToLower($mode);
if (!isset($this->_modes[$mode])) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $this->_mode),
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
$this->_mode = $mode;
return true;
}
/**
* Generate password with htpasswd executable
*
* @access private
* @return string the crypted password
* @param string $pass the plaintext password
* @param string $salt the salt to use
* @param string $mode encyption mode, usually determined from
* <var>$this->_mode</var>
*/
function _genPass($pass, $salt = null, $mode = null)
{
$mode = is_null($mode) ? strToLower($this->_mode) : strToLower($mode);
if ($mode == 'md5') {
return File_Passwd::crypt_apr_md5($pass, $salt);
} elseif ($mode == 'des') {
return File_Passwd::crypt_des($pass, $salt);
} elseif ($mode == 'sha') {
return File_Passwd::crypt_sha($pass, $salt);
}
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
/**
* Parse the AuthUserFile
*
* Returns a PEAR_Error if AuthUserFile has invalid format.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_error
*/
function parse()
{
$this->_users = array();
foreach ($this->_contents as $line) {
$user = explode(':', $line);
if (count($user) != 2) {
return PEAR::raiseError(
FILE_PASSWD_E_INVALID_FORMAT_STR,
FILE_PASSWD_E_INVALID_FORMAT
);
}
$this->_users[$user[0]] = trim($user[1]);
}
$this->_contents = array();
return true;
}
/**
* Generate Password
*
* Returns PEAR_Error FILE_PASSD_E_INVALID_ENC_MODE if the supplied
* encryption mode is not supported.
*
* @static
* @access public
* @return mixed The crypted password on success or PEAR_Error on failure.
* @param string $pass The plaintext password.
* @param string $mode The encryption mode to use (des|md5|sha).
* @param string $salt The salt to use.
*/
function generatePasswd($pass, $mode = FILE_PASSWD_DES, $salt = null)
{
if (!in_array(strToLower($mode), array('des', 'md5', 'sha'))) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
return File_Passwd_Authbasic::_genPass($pass, $salt, $mode);
}
/**
* @ignore
* @deprecated
*/
function generatePassword($pass, $mode = FILE_PASSWD_DES, $salt = null)
{
return File_Passwd_Authbasic::generatePasswd($pass, $mode, $salt);
}
}
?>

View File

@@ -0,0 +1,362 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Authdigest
*
* 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 FileFormats
* @package File_Passwd
* @author Michael Wallner <mike@php.net>
* @copyright 2003-2005 Michael Wallner
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Authdigest.php,v 1.13 2005/09/27 06:26:08 mike Exp $
* @link http://pear.php.net/package/File_Passwd
*/
/**
* Requires File::Passwd::Common
*/
require_once 'File/Passwd/Common.php';
/**
* Manipulate AuthDigestFiles as used for HTTP Digest Authentication.
*
* <kbd><u>
* Usage Example:
* </u></kbd>
* <code>
* $htd = &File_Passwd::factory('Authdigest');
* $htd->setFile('/www/mike/auth/.htdigest');
* $htd->load();
* $htd->addUser('mike', 'myRealm', 'secret');
* $htd->save();
* </code>
*
* <kbd><u>
* Output of listUser()
* </u></kbd>
* <pre>
* array
* + user => array
* + realm => crypted_passwd
* + realm => crypted_passwd
* + user => array
* + realm => crypted_passwd
* </pre>
*
* @author Michael Wallner <mike@php.net>
* @package File_Passwd
* @version $Revision: 1.13 $
* @access public
*/
class File_Passwd_Authdigest extends File_Passwd_Common
{
/**
* Path to AuthDigestFile
*
* @var string
* @access private
*/
var $_file = '.htdigest';
/**
* Constructor
*
* @access public
* @param string $file path to AuthDigestFile
*/
function File_Passwd_Authdigest($file = '.htdigest')
{
parent::__construct($file);
}
/**
* Fast authentication of a certain user
*
* Returns a PEAR_Error if:
* o file doesn't exist
* o file couldn't be opened in read mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked (only if auth fails)
* o file couldn't be closed (only if auth fails)
*
* @static call this method statically for a reasonable fast authentication
*
* @throws PEAR_Error
* @access public
* @return mixed true if authenticated, false if not or PEAR_Error
* @param string $file path to passwd file
* @param string $user user to authenticate
* @param string $pass plaintext password
* @param string $realm the realm the user is in
*/
function staticAuth($file, $user, $pass, $realm)
{
$line = File_Passwd_Common::_auth($file, $user.':'.$realm);
if (!$line || PEAR::isError($line)) {
return $line;
}
@list(,,$real)= explode(':', $line);
return (md5("$user:$realm:$pass") === $real);
}
/**
* Apply changes and rewrite AuthDigestFile
*
* Returns a PEAR_Error if:
* o directory in which the file should reside couldn't be created
* o file couldn't be opened in write mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked
* o file couldn't be closed
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or a PEAR_Error
*/
function save()
{
$content = '';
if (count($this->_users)) {
foreach ($this->_users as $user => $realm) {
foreach ($realm as $r => $pass){
$content .= "$user:$r:$pass\n";
}
}
}
return $this->_save($content);
}
/**
* Add an user
*
* Returns a PEAR_Error if:
* o the user already exists in the supplied realm
* o the user or realm contain illegal characters
*
* $user and $realm must start with an alphabetical charachter and must NOT
* contain any other characters than alphanumerics, the underline and dash.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or a PEAR_Error
* @param string $user the user to add
* @param string $realm the realm the user should be in
* @param string $pass the plaintext password
*/
function addUser($user, $realm, $pass)
{
if ($this->userInRealm($user, $realm)) {
return PEAR::raiseError(
"User '$user' already exists in realm '$realm'.", 0
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
FILE_PASSWD_E_INVALID_CHARS
);
}
if (!preg_match($this->_pcre, $realm)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'Realm ', $realm),
FILE_PASSWD_E_INVALID_CHARS
);
}
$this->_users[$user][$realm] = md5("$user:$realm:$pass");
return true;
}
/**
* List all user of (a | all) realm(s)
*
* Returns:
* o associative array of users of ONE realm if $inRealm was supplied
* <pre>
* realm1
* + user1 => pass
* + user2 => pass
* + user3 => pass
* </pre>
* o associative array of all realms with all users
* <pre>
* array
* + realm1 => array
* + user1 => pass
* + user2 => pass
* + user3 => pass
* + realm2 => array
* + user3 => pass
* + realm3 => array
* + user1 => pass
* + user2 => pass
* </pre>
*
* @access public
* @return array
* @param string $inRealm the realm to list users of;
* if omitted, you'll get all realms
*/
function listUserInRealm($inRealm = '')
{
$result = array();
foreach ($this->_users as $user => $realms){
foreach ($realms as $realm => $pass){
if (!empty($inRealm) && ($inRealm !== $realm)) {
continue;
}
if (!isset($result[$realm])) {
$result[$realm] = array();
}
$result[$realm][$user] = $pass;
}
}
return $result;
}
/**
* Change the password of a certain user
*
* Returns a PEAR_Error if:
* o user doesn't exist in the supplied realm
* o user or realm contains illegal characters
*
* This method in fact adds the user whith the new password
* after deleting the user.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or a PEAR_Error
* @param string $user the user whose password should be changed
* @param string $realm the realm the user is in
* @param string $pass the new plaintext password
*/
function changePasswd($user, $realm, $pass)
{
if (PEAR::isError($error = $this->delUserInRealm($user, $realm))) {
return $error;
} else {
return $this->addUser($user, $realm, $pass);
}
}
/**
* Verifiy password
*
* Returns a PEAR_Error if the user doesn't exist in the supplied realm.
*
* @throws PEAR_Error
* @access public
* @return mixed true if passwords equal, false if they don't, or PEAR_Error
* @param string $user the user whose password should be verified
* @param string $realm the realm the user is in
* @param string $pass the plaintext password to verify
*/
function verifyPasswd($user, $realm, $pass)
{
if (!$this->userInRealm($user, $realm)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_USER_NOT_IN_REALM_STR, $user, $realm),
FILE_PASSWD_E_USER_NOT_IN_REALM
);
}
return ($this->_users[$user][$realm] === md5("$user:$realm:$pass"));
}
/**
* Ckeck if a certain user is in a specific realm
*
* @throws PEAR_Error
* @access public
* @return boolean
* @param string $user the user to check
* @param string $realm the realm the user shuold be in
*/
function userInRealm($user, $realm)
{
return (isset($this->_users[$user][$realm]));
}
/**
* Delete a certain user in a specific realm
*
* Returns a PEAR_Error if <var>$user</var> doesn't exist <var>$inRealm</var>.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
* @param string $user the user to remove
* @param string $inRealm the realm the user should be in
*/
function delUserInRealm($user, $inRealm)
{
if (!$this->userInRealm($user, $inRealm)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_USER_NOT_IN_REALM_STR, $user, $inRealm),
FILE_PASSWD_E_USER_NOT_IN_REALM
);
}
unset($this->_users[$user][$inRealm]);
return true;
}
/**
* Parse the AuthDigestFile
*
* Returns a PEAR_Error if AuthDigestFile has invalid format.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function parse()
{
$this->_users = array();
foreach ($this->_contents as $line) {
$user = explode(':', $line);
if (count($user) != 3) {
return PEAR::raiseError(
FILE_PASSWD_E_INVALID_FORMAT_STR,
FILE_PASSWD_E_INVALID_FORMAT
);
}
list($user, $realm, $pass) = $user;
$this->_users[$user][$realm] = trim($pass);
}
$this->_contents = array();
return true;
}
/**
* Generate Password
*
* @static
* @access public
* @return string The crypted password.
* @param string $user The username.
* @param string $realm The realm the user is in.
* @param string $pass The plaintext password.
*/
function generatePasswd($user, $realm, $pass)
{
return md5("$user:$realm:$pass");
}
/**
* @ignore
* @deprecated
*/
function generatePassword($user, $realm, $pass)
{
return File_Passwd_Authdigest::generatePasswd($user, $realm, $pass);
}
}
?>

View File

@@ -0,0 +1,382 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Common
*
* 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 FileFormats
* @package File_Passwd
* @author Michael Wallner <mike@php.net>
* @copyright 2003-2005 Michael Wallner
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Common.php,v 1.18 2005/03/30 18:33:33 mike Exp $
* @link http://pear.php.net/package/File_Passwd
*/
/**
* Requires System
*/
require_once 'System.php';
/**
* Requires File::Passwd
*/
require_once 'File/Passwd.php';
/**
* Baseclass for File_Passwd_* classes.
*
* <kbd><u>
* Provides basic operations:
* </u></kbd>
* o opening & closing
* o locking & unlocking
* o loading & saving
* o check if user exist
* o delete a certain user
* o list users
*
* @author Michael Wallner <mike@php.net>
* @package File_Passwd
* @version $Revision: 1.18 $
* @access protected
* @internal extend this class for your File_Passwd_* class
*/
class File_Passwd_Common
{
/**
* passwd file
*
* @var string
* @access protected
*/
var $_file = 'passwd';
/**
* file content
*
* @var aray
* @access protected
*/
var $_contents = array();
/**
* users
*
* @var array
* @access protected
*/
var $_users = array();
/**
* PCRE for valid chars
*
* @var string
* @access protected
*/
var $_pcre = '/^[a-z]+[a-z0-9_-]*$/i';
/**
* Constructor (ZE2)
*
* @access protected
* @param string $file path to passwd file
*/
function __construct($file = 'passwd')
{
$this->setFile($file);
}
/**
* Parse the content of the file
*
* You must overwrite this method in your File_Passwd_* class.
*
* @abstract
* @internal
* @access public
* @return object PEAR_Error
*/
function parse()
{
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR, 'parse'),
FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED
);
}
/**
* Apply changes and rewrite passwd file
*
* You must overwrite this method in your File_Passwd_* class.
*
* @abstract
* @internal
* @access public
* @return object PEAR_Error
*/
function save()
{
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED_STR, 'save'),
FILE_PASSWD_E_METHOD_NOT_IMPLEMENTED
);
}
/**
* Opens a file, locks it exclusively and returns the filehandle
*
* Returns a PEAR_Error if:
* o directory in which the file should reside couldn't be created
* o file couldn't be opened in the desired mode
* o file couldn't be locked exclusively
*
* @throws PEAR_Error
* @access protected
* @return mixed resource of type file handle or PEAR_Error
* @param string $mode the mode to open the file with
*/
function &_open($mode, $file = null)
{
isset($file) or $file = $this->_file;
$dir = dirname($file);
$lock = strstr($mode, 'r') ? LOCK_SH : LOCK_EX;
if (!is_dir($dir) && !System::mkDir('-p -m 0755 ' . $dir)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_DIR_NOT_CREATED_STR, $dir),
FILE_PASSWD_E_DIR_NOT_CREATED
);
}
if (!is_resource($fh = @fopen($file, $mode))) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_FILE_NOT_OPENED_STR, $file),
FILE_PASSWD_E_FILE_NOT_OPENED
);
}
if (!@flock($fh, $lock)) {
fclose($fh);
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_FILE_NOT_LOCKED_STR, $file),
FILE_PASSWD_E_FILE_NOT_LOCKED
);
}
return $fh;
}
/**
* Closes a prior opened and locked file handle
*
* Returns a PEAR_Error if:
* o file couldn't be unlocked
* o file couldn't be closed
*
* @throws PEAR_Error
* @access protected
* @return mixed true on success or PEAR_Error
* @param resource $file_handle the file handle to operate on
*/
function _close(&$file_handle)
{
if (!@flock($file_handle, LOCK_UN)) {
return PEAR::raiseError(
FILE_PASSWD_E_FILE_NOT_UNLOCKED_STR,
FILE_PASSWD_E_FILE_NOT_UNLOCKED
);
}
if (!@fclose($file_handle)) {
return PEAR::raiseError(
FILE_PASSWD_E_FILE_NOT_CLOSED_STR,
FILE_PASSWD_E_FILE_NOT_CLOSED
);
}
return true;
}
/**
* Loads the file
*
* Returns a PEAR_Error if:
* o directory in which the file should reside couldn't be created
* o file couldn't be opened in read mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked
* o file couldn't be closed
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function load()
{
$fh = &$this->_open('r');
if (PEAR::isError($fh)) {
return $fh;
}
$this->_contents = array();
while ($line = fgets($fh)) {
if (!preg_match('/^\s*#/', $line) && $line = trim($line)) {
$this->_contents[] = $line;
}
}
$e = $this->_close($fh);
if (PEAR::isError($e)) {
return $e;
}
return $this->parse();
}
/**
* Save the modified content to the passwd file
*
* Returns a PEAR_Error if:
* o directory in which the file should reside couldn't be created
* o file couldn't be opened in write mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked
* o file couldn't be closed
*
* @throws PEAR_Error
* @access protected
* @return mixed true on success or PEAR_Error
*/
function _save($content)
{
$fh = &$this->_open('w');
if (PEAR::isError($fh)) {
return $fh;
}
fputs($fh, $content);
return $this->_close($fh);
}
/**
* Set path to passwd file
*
* @access public
* @return void
*/
function setFile($file)
{
$this->_file = $file;
}
/**
* Get path of passwd file
*
* @access public
* @return string
*/
function getFile()
{
return $this->_file;
}
/**
* Check if a certain user already exists
*
* @access public
* @return bool
* @param string $user the name of the user to check if already exists
*/
function userExists($user)
{
return isset($this->_users[$user]);
}
/**
* Delete a certain user
*
* Returns a PEAR_Error if user doesn't exist.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
* @param string
*/
function delUser($user)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
unset($this->_users[$user]);
return true;
}
/**
* List user
*
* Returns a PEAR_Error if <var>$user</var> doesn't exist.
*
* @throws PEAR_Error
* @access public
* @return mixed array of a/all user(s) or PEAR_Error
* @param string $user the user to list or all users if empty
*/
function listUser($user = '')
{
if (empty($user)) {
return $this->_users;
}
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
return $this->_users[$user];
}
/**
* Base method for File_Passwd::staticAuth()
*
* Returns a PEAR_Error if:
* o file doesn't exist
* o file couldn't be opened in read mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked (only if auth fails)
* o file couldn't be closed (only if auth fails)
*
* @throws PEAR_Error
* @access protected
* @return mixed line of passwd file containing <var>$id</var>,
* false if <var>$id</var> wasn't found or PEAR_Error
* @param string $file path to passwd file
* @param string $id user_id to search for
* @param string $sep field separator
*/
function _auth($file, $id, $sep = ':')
{
$file = realpath($file);
if (!is_file($file)) {
return PEAR::raiseError("File '$file' couldn't be found.", 0);
}
$fh = &File_Passwd_Common::_open('r', $file);
if (PEAR::isError($fh)) {
return $fh;
}
$cmp = $id . $sep;
$len = strlen($cmp);
while ($line = fgets($fh)) {
if (!strncmp($line, $cmp, $len)) {
File_Passwd_Common::_close($fh);
return trim($line);
}
}
$e = File_Passwd_Common::_close($fh);
if (PEAR::isError($e)) {
return $e;
}
return false;
}
}
?>

View File

@@ -0,0 +1,593 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Custom
*
* 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 FileFormats
* @package File_Passwd
* @author Michael Wallner <mike@php.net>
* @copyright 2003-2005 Michael Wallner
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Custom.php,v 1.10 2005/03/30 18:33:33 mike Exp $
* @link http://pear.php.net/package/File_Passwd
*/
/**
* Requires File::Passwd::Common
*/
require_once 'File/Passwd/Common.php';
/**
* Manipulate custom formatted passwd files
*
* Usage Example:
* <code>
* $cust = &File_Passwd::factory('Custom');
* $cust->setDelim('|');
* $cust->load();
* $cust->setEncFunc(array('File_Passwd', 'crypt_apr_md5'));
* $cust->addUser('mike', 'pass');
* $cust->save();
* </code>
*
* @author Michael Wallner <mike@php.net>
* @version $Revision: 1.10 $
* @access public
*/
class File_Passwd_Custom extends File_Passwd_Common
{
/**
* Delimiter
*
* @access private
* @var string
*/
var $_delim = ':';
/**
* Encryption function
*
* @access private
* @var string
*/
var $_enc = array('File_Passwd', 'crypt_md5');
/**
* 'name map'
*
* @access private
* @var array
*/
var $_map = array();
/**
* Whether to use the 'name map' or not
*
* @var boolean
* @access private
*/
var $_usemap = false;
/**
* Constructor
*
* @access protected
* @return object
*/
function File_Passwd_Custom($file = 'passwd')
{
$this->__construct($file);
}
/**
* Fast authentication of a certain user
*
* Returns a PEAR_Error if:
* o file doesn't exist
* o file couldn't be opened in read mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked (only if auth fails)
* o file couldn't be closed (only if auth fails)
* o invalid encryption function <var>$opts[0]</var>,
* or no delimiter character <var>$opts[1]</var> was provided
*
* @throws PEAR_Error FILE_PASSWD_E_UNDEFINED |
* FILE_PASSWD_E_FILE_NOT_OPENED |
* FILE_PASSWD_E_FILE_NOT_LOCKED |
* FILE_PASSWD_E_FILE_NOT_UNLOCKED |
* FILE_PASSWD_E_FILE_NOT_CLOSED |
* FILE_PASSWD_E_INVALID_ENC_MODE
* @static call this method statically for a reasonable fast authentication
* @access public
* @return mixed Returns &true; if authenticated, &false; if not or
* <classname>PEAR_Error</classname> on failure.
* @param string $file path to passwd file
* @param string $user user to authenticate
* @param string $pass plaintext password
* @param array $otps encryption function and delimiter charachter
* (in this order)
*/
function staticAuth($file, $user, $pass, $opts)
{
setType($opts, 'array');
if (count($opts) != 2 || empty($opts[1])) {
return PEAR::raiseError('Insufficient options.', 0);
}
$line = File_Passwd_Common::_auth($file, $user, $opts[1]);
if (!$line || PEAR::isError($line)) {
return $line;
}
list(,$real)= explode($opts[1], $line);
$crypted = File_Passwd_Custom::_genPass($pass, $real, $opts[0]);
if (PEAR::isError($crypted)) {
return $crypted;
}
return ($crypted === $real);
}
/**
* Set delimiter
*
* You can set a custom char to delimit the columns of a data set.
* Defaults to a colon (':'). Be aware that this char mustn't be
* in the values of your data sets.
*
* @access public
* @return void
* @param string $delim custom delimiting character
*/
function setDelim($delim = ':')
{
@setType($delim, 'string');
if (empty($delim)) {
$this->_delim = ':';
} else {
$this->_delim = $delim{0};
}
}
/**
* Get custom delimiter
*
* @access public
* @return string
*/
function getDelim()
{
return $this->_delim;
}
/**
* Set encryption function
*
* You can set a custom encryption function to use.
* The supplied function will be called by php's call_user_function(),
* so you can supply an array with a method of a class/object, too
* (i.e. array('File_Passwd', 'crypt_apr_md5').
*
*
* @throws PEAR_Error FILE_PASSWD_E_INVALID_ENC_MODE
* @access public
* @return mixed Returns &true; on success or
* <classname>PEAR_Error</classname> on failure.
* @param mixed $function callable encryption function
*/
function setEncFunc($function = array('File_Passwd', 'crypt_md5'))
{
if (!is_callable($function)) {
if (is_array($function)) {
$function = implode('::', $function);
}
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $function),
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
$this->_enc = $function;
return true;
}
/**
* Get current custom encryption method
*
* Possible return values (examples):
* o 'md5'
* o 'File_Passwd::crypt_md5'
*
* @access public
* @return string
*/
function getEncFunc()
{
if (is_array($this->_enc)) {
return implode('::', $this->_enc);
}
return $this->_enc;
}
/**
* Whether to use the 'name map' of the extra properties or not
*
* @see File_Passwd_Custom::useMap()
* @see setMap()
* @see getMap()
*
* @access public
* @return boolean always true if you set a value (true/false) OR
* the actual value if called without param
*
* @param boolean $bool whether to use the 'name map' or not
*/
function useMap($bool = null)
{
if (is_null($bool)) {
return $this->_usemap;
}
$this->_usemap = (bool) $bool;
return true;
}
/**
* Set the 'name map' to use with the extra properties of the user
*
* This map is used for naming the associative array of the extra properties.
*
* Returns a PEAR_Error if <var>$map</var> was not of type array.
*
* @see getMap()
* @see useMap()
*
* @throws PEAR_Error FILE_PASSWD_E_PARAM_MUST_BE_ARRAY
* @access public
* @return mixed true on success or PEAR_Error
*/
function setMap($map = array())
{
if (!is_array($map)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_PARAM_MUST_BE_ARRAY_STR, '$map'),
FILE_PASSWD_E_PARAM_MUST_BE_ARRAY
);
}
$this->_map = $map;
return true;
}
/**
* Get the 'name map' which is used for the extra properties of the user
*
* @see setMap()
* @see useMap()
*
* @access public
* @return array
*/
function getMap()
{
return $this->_map;
}
/**
* Apply changes an rewrite passwd file
*
* Returns a PEAR_Error if:
* o directory in which the file should reside couldn't be created
* o file couldn't be opened in write mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked
* o file couldn't be closed
*
* @throws PEAR_Error FILE_PASSWD_E_FILE_NOT_OPENED |
* FILE_PASSWD_E_FILE_NOT_LOCKED |
* FILE_PASSWD_E_FILE_NOT_UNLOCKED |
* FILE_PASSWD_E_FILE_NOT_CLOSED
* @access public
* @return mixed Returns &true; on success or
* <classname>PEAR_Error</classname> on failure.
*/
function save()
{
$content = '';
foreach ($this->_users as $user => $array){
$pass = array_shift($array);
$extra = implode($this->_delim, $array);
$content .= $user . $this->_delim . $pass;
if (!empty($extra)) {
$content .= $this->_delim . $extra;
}
$content .= "\n";
}
return $this->_save($content);
}
/**
* Parse the Custom password file
*
* Returns a PEAR_Error if passwd file has invalid format.
*
* @throws PEAR_Error FILE_PASSWD_E_INVALID_FORMAT
* @access public
* @return mixed Returns &true; on success or
* <classname>PEAR_Error</classname> on failure.
*/
function parse()
{
$this->_users = array();
foreach ($this->_contents as $line){
$parts = explode($this->_delim, $line);
if (count($parts) < 2) {
return PEAR::raiseError(
FILE_PASSWD_E_INVALID_FORMAT_STR,
FILE_PASSWD_E_INVALID_FORMAT
);
}
$user = array_shift($parts);
$pass = array_shift($parts);
$values = array();
if ($this->_usemap) {
$values['pass'] = $pass;
foreach ($parts as $i => $value){
if (isset($this->_map[$i])) {
$values[$this->_map[$i]] = $value;
} else {
$values[$i+1] = $value;
}
}
} else {
$values = array_merge(array($pass), $parts);
}
$this->_users[$user] = $values;
}
$this->_contents = array();
return true;
}
/**
* Add an user
*
* The username must start with an alphabetical character and must NOT
* contain any other characters than alphanumerics, the underline and dash.
*
* If you use the 'name map' you should also use these naming in
* the supplied extra array, because your values would get mixed up
* if they are in the wrong order, which is always true if you
* DON'T use the 'name map'!
*
* So be warned and USE the 'name map'!
*
* Returns a PEAR_Error if:
* o user already exists
* o user contains illegal characters
* o encryption mode is not supported
* o any element of the <var>$extra</var> array contains the delimiter char
*
* @throws PEAR_Error FILE_PASSWD_E_EXISTS_ALREADY |
* FILE_PASSWD_E_INVALID_ENC_MODE |
* FILE_PASSWD_E_INVALID_CHARS
* @access public
* @return mixed Returns &true; on success or
* <classname>PEAR_Error</classname> on failure.
* @param string $user the name of the user to add
* @param string $pass the password of the user to add
* @param array $extra extra properties of user to add
*/
function addUser($user, $pass, $extra = array())
{
if ($this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_ALREADY
);
}
if (!preg_match($this->_pcre, $user) || strstr($user, $this->_delim)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
FILE_PASSWD_E_INVALID_CHARS
);
}
if (!is_array($extra)) {
setType($extra, 'array');
}
foreach ($extra as $e){
if (strstr($e, $this->_delim)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'Property ', $e),
FILE_PASSWD_E_INVALID_CHARS
);
}
}
$pass = $this->_genPass($pass);
if (PEAR::isError($pass)) {
return $pass;
}
/**
* If you don't use the 'name map' the user array will be numeric.
*/
if (!$this->_usemap) {
array_unshift($extra, $pass);
$this->_users[$user] = $extra;
} else {
$map = $this->_map;
array_unshift($map, 'pass');
$extra['pass'] = $pass;
foreach ($map as $key){
$this->_users[$user][$key] = @$extra[$key];
}
}
return true;
}
/**
* Modify properties of a certain user
*
* # DON'T MODIFY THE PASSWORD WITH THIS METHOD!
*
* You should use this method only if the 'name map' is used, too.
*
* Returns a PEAR_Error if:
* o user doesn't exist
* o any property contains the custom delimiter character
*
* @see changePasswd()
*
* @throws PEAR_Error FILE_PASSWD_E_EXISTS_NOT |
* FILE_PASSWD_E_INVALID_CHARS
* @access public
* @return mixed true on success or PEAR_Error
* @param string $user the user to modify
* @param array $properties an associative array of
* properties to modify
*/
function modUser($user, $properties = array())
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
if (!is_array($properties)) {
setType($properties, 'array');
}
foreach ($properties as $key => $value){
if (strstr($value, $this->_delim)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
FILE_PASSWD_E_INVALID_CHARS
);
}
$this->_users[$user][$key] = $value;
}
return true;
}
/**
* Change the password of a certain user
*
* Returns a PEAR_Error if:
* o user doesn't exists
* o encryption mode is not supported
*
* @throws PEAR_Error FILE_PASSWD_E_EXISTS_NOT |
* FILE_PASSWD_E_INVALID_ENC_MODE
* @access public
* @return mixed Returns &true; on success or
* <classname>PEAR_Error</classname> on failure.
* @param string $user the user whose password should be changed
* @param string $pass the new plaintext password
*/
function changePasswd($user, $pass)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
$pass = $this->_genPass($pass);
if (PEAR::isError($pass)) {
return $pass;
}
if ($this->_usemap) {
$this->_users[$user]['pass'] = $pass;
} else {
$this->_users[$user][0] = $pass;
}
return true;
}
/**
* Verify the password of a certain user
*
* Returns a PEAR_Error if:
* o user doesn't exist
* o encryption mode is not supported
*
* @throws PEAR_Error FILE_PASSWD_E_EXISTS_NOT |
* FILE_PASSWD_E_INVALID_ENC_MODE
* @access public
* @return mixed Returns &true; if passwors equal, &false; if they don't
* or <classname>PEAR_Error</classname> on fialure.
* @param string $user the user whose password should be verified
* @param string $pass the password to verify
*/
function verifyPasswd($user, $pass)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
$real =
$this->_usemap ?
$this->_users[$user]['pass'] :
$this->_users[$user][0]
;
return ($real === $this->_genPass($pass, $real));
}
/**
* Generate crypted password from the plaintext password
*
* Returns a PEAR_Error if actual encryption mode is not supported.
*
* @throws PEAR_Error FILE_PASSWD_E_INVALID_ENC_MODE
* @access private
* @return mixed Returns the crypted password or
* <classname>PEAR_Error</classname>
* @param string $pass the plaintext password
* @param string $salt the crypted password from which to gain the salt
* @param string $func the encryption function to use
*/
function _genPass($pass, $salt = null, $func = null)
{
if (is_null($func)) {
$func = $this->_enc;
}
if (!is_callable($func)) {
if (is_array($func)) {
$func = implode('::', $func);
}
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $func),
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
$return = @call_user_func($func, $pass, $salt);
if (is_null($return) || $return === false) {
$return = @call_user_func($func, $pass);
}
return $return;
}
}
?>

View File

@@ -0,0 +1,304 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Cvs
*
* 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 FileFormats
* @package File_Passwd
* @author Michael Wallner <mike@php.net>
* @copyright 2003-2005 Michael Wallner
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Cvs.php,v 1.14 2005/03/30 18:33:33 mike Exp $
* @link http://pear.php.net/package/File_Passwd
*/
/**
* Requires File::Passwd::Common
*/
require_once 'File/Passwd/Common.php';
/**
* Manipulate CVS pserver passwd files.
*
* <kbd><u>
* A line of a CVS pserver passwd file consists of 2 to 3 colums:
* </u></kbd>
* <pre>
* user1:1HCoDDWxK9tbM:sys_user1
* user2:0O0DYYdzjCVxs
* user3:MIW9UUoifhqRo:sys_user2
* </pre>
*
* If the third column is specified, the CVS user named in the first column is
* mapped to the corresponding system user named in the third column.
* That doesn't really affect us - just for your interest :)
*
* <kbd><u>Output of listUser()</u></kbd>
* <pre>
* array
* + user => array
* + passwd => crypted_passwd
* + system => system_user
* + user => array
* + passwd => crypted_passwd
* + system => system_user
* </pre>
*
* @author Michael Wallner <mike@php.net>
* @package File_Passwd
* @version $Revision: 1.14 $
* @access public
*/
class File_Passwd_Cvs extends File_Passwd_Common
{
/**
* Constructor
*
* @access public
*/
function File_Passwd_Cvs($file = 'passwd')
{
parent::__construct($file);
}
/**
* Fast authentication of a certain user
*
* Returns a PEAR_Error if:
* o file doesn't exist
* o file couldn't be opened in read mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked (only if auth fails)
* o file couldn't be closed (only if auth fails)
*
* @static call this method statically for a reasonable fast authentication
* @access public
* @return mixed true if authenticated, false if not or PEAR_Error
* @param string $file path to passwd file
* @param string $user user to authenticate
* @param string $pass plaintext password
*/
function staticAuth($file, $user, $pass)
{
$line = File_Passwd_Common::_auth($file, $user);
if (!$line || PEAR::isError($line)) {
return $line;
}
@list(,$real) = explode(':', $line);
return (File_Passwd_Cvs::generatePassword($pass, $real) === $real);
}
/**
* Apply changes and rewrite CVS passwd file
*
* Returns a PEAR_Error if:
* o directory in which the file should reside couldn't be created
* o file couldn't be opened in write mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked
* o file couldn't be closed
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function save()
{
$content = '';
foreach ($this->_users as $user => $v){
$content .= $user . ':' . $v['passwd'];
if (isset($v['system']) && !empty($v['system'])) {
$content .= ':' . $v['system'];
}
$content .= "\n";
}
return $this->_save($content);
}
/**
* Parse the CVS passwd file
*
* Returns a PEAR_Error if passwd file has invalid format.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function parse()
{
$this->_users = array();
foreach ($this->_contents as $line) {
$user = explode(':', $line);
if (count($user) < 2) {
return PEAR::raiseError(
FILE_PASSWD_E_INVALID_FORMAT_STR,
FILE_PASSWD_E_INVALID_FORMAT
);
}
@list($user, $pass, $system) = $user;
$this->_users[$user]['passwd'] = $pass;
if (!empty($system)) {
$this->_users[$user]['system'] = $system;
}
}
$this->_contents = array();
return true;
}
/**
* Add an user
*
* The username must start with an alphabetical character and must NOT
* contain any other characters than alphanumerics, the underline and dash.
*
* Returns a PEAR_Error if:
* o user already exists
* o user or system_user contains illegal characters
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
* @param string $user the name of the user to add
* @param string $pass the password of the user tot add
* @param string $system_user the systemuser this user maps to
*/
function addUser($user, $pass, $system_user = '')
{
if ($this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_ALREADY
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
FILE_PASSWD_E_INVALID_CHARS
);
}
@setType($system_user, 'string');
if (!empty($system_user) && !preg_match($this->_pcre, $system_user)) {
return PEAR::raiseError(
sprintf(
FILE_PASSWD_E_INVALID_CHARS_STR,
'System user ',
$system_user
),
FILE_PASSWD_E_INVALID_CHARS
);
}
$this->_users[$user]['passwd'] = $this->generatePassword($pass);
$this->_users[$user]['system'] = $system_user;
return true;
}
/**
* Verify the password of a certain user
*
* Returns a PEAR_Error if the user doesn't exist.
*
* @throws PEAR_Error
* @access public
* @return mixed true if passwords equal, false ifthe don't or PEAR_Error
* @param string $user user whose password should be verified
* @param string $pass the plaintext password that should be verified
*/
function verifyPasswd($user, $pass)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
$real = $this->_users[$user]['passwd'];
return ($real === $this->generatePassword($pass, $real));
}
/**
* Change the password of a certain user
*
* Returns a PEAR_Error if user doesn't exist.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function changePasswd($user, $pass)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
$this->_users[$user]['passwd'] = $this->generatePassword($pass);
return true;
}
/**
* Change the corresponding system user of a certain cvs user
*
* Returns a PEAR_Error if:
* o user doesn't exist
* o system user contains illegal characters
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function changeSysUser($user, $system)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
if (!preg_match($this->_pcre, $system)) {
return PEAR::raiseError(
sprintf(
FILE_PASSWD_E_INVALID_CHARS_STR,
'System user ',
$system_user
),
FILE_PASSWD_E_INVALID_CHARS
);
}
$this->_users[$user]['system'] = $system;
return true;
}
/**
* Generate crypted password
*
* @static
* @access public
* @return string the crypted password
* @param string $pass new plaintext password
* @param string $salt new crypted password from which to gain the salt
*/
function generatePasswd($pass, $salt = null)
{
return File_Passwd::crypt_des($pass, $salt);
}
/**
* @ignore
* @deprecated
*/
function generatePassword($pass, $salt = null)
{
return File_Passwd_Cvs::generatePasswd($pass, $salt);
}
}
?>

View File

@@ -0,0 +1,426 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Smb
*
* 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 FileFormats
* @package File_Passwd
* @author Michael Wallner <mike@php.net>
* @author Michael Bretterklieber <michael@bretterklieber.com>
* @copyright 2003-2005 Michael Wallner
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Smb.php,v 1.18 2005/05/06 10:22:48 mike Exp $
* @link http://pear.php.net/package/File_Passwd
*/
/**
* Requires File::Passwd::Common
*/
require_once 'File/Passwd/Common.php';
/**
* Requires Crypt::CHAP
*/
require_once 'Crypt/CHAP.php';
/**
* Manipulate SMB server passwd files.
*
* # Usage Example 1 (modifying existing file):
* <code>
* $f = &File_Passwd::factory('SMB');
* $f->setFile('./smbpasswd');
* $f->load();
* $f->addUser('sepp3', 'MyPw', array('userid' => 12));
* $f->changePasswd('sepp', 'MyPw');
* $f->delUser('karli');
* foreach($f->listUser() as $user => $data) {
* echo $user . ':' . implode(':', $data) ."\n";
* }
* $f->save();
* </code>
*
* # Usage Example 2 (creating a new file):
* <code>
* $f = &File_Passwd::factory('SMB');
* $f->setFile('./smbpasswd');
* $f->addUser('sepp1', 'MyPw', array('userid'=> 12));
* $f->addUser('sepp3', 'MyPw', array('userid' => 1000));
* $f->save();
* </code>
*
* # Usage Example 3 (authentication):
* <code>
* $f = &File_Passwd::factory('SMB');
* $f->setFile('./smbpasswd');
* $f->load();
* if (true === $f->verifyPasswd('sepp', 'MyPw')) {
* echo "User valid";
* } else {
* echo "User invalid or disabled";
* }
* </code>
*
* @author Michael Bretterklieber <michael@bretterklieber.com>
* @author Michael Wallner <mike@php.net>
* @package File_Passwd
* @version $Revision: 1.18 $
* @access public
*/
class File_Passwd_Smb extends File_Passwd_Common
{
/**
* Object which generates the NT-Hash and LAN-Manager-Hash passwds
*
* @access protected
* @var object
*/
var $msc;
/**
* Constructor
*
* @access public
* @param string $file SMB passwd file
*/
function File_Passwd_Smb($file = 'smbpasswd')
{
File_Passwd_Smb::__construct($file);
}
/**
* Constructor (ZE2)
*
* Rewritten because we want to init our crypt engine.
*
* @access public
* @param string $file SMB passwd file
*/
function __construct($file = 'smbpasswd')
{
$this->setFile($file);
$this->msc = &new Crypt_CHAP_MSv1;
}
/**
* Fast authentication of a certain user
*
* Returns a PEAR_Error if:
* o file doesn't exist
* o file couldn't be opened in read mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked (only if auth fails)
* o file couldn't be closed (only if auth fails)
* o invalid encryption method <var>$nt_or_lm</var> was provided
*
* @static call this method statically for a reasonable fast authentication
* @access public
* @return mixed true if authenticated, false if not or PEAR_Error
* @param string $file path to passwd file
* @param string $user user to authenticate
* @param string $pass plaintext password
* @param string $nt_or_lm encryption mode to use (NT or LM hash)
*/
function staticAuth($file, $user, $pass, $nt_or_lm = 'nt')
{
$line = File_Passwd_Common::_auth($file, $user);
if (!$line || PEAR::isError($line)) {
return $line;
}
@list(,,$lm,$nt) = explode(':', $line);
$chap = &new Crypt_CHAP_MSv1;
switch(strToLower($nt_or_lm)){
case FILE_PASSWD_NT:
$real = $nt;
$crypted = $chap->ntPasswordHash($pass);
break;
case FILE_PASSWD_LM:
$real = $lm;
$crypted = $chap->lmPasswordHash($pass);
break;
default:
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $nt_or_lm),
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
return (strToUpper(bin2hex($crypted)) === $real);
}
/**
* Parse smbpasswd file
*
* Returns a PEAR_Error if passwd file has invalid format.
*
* @access public
* @return mixed true on success or PEAR_Error
*/
function parse()
{
foreach ($this->_contents as $line){
$info = explode(':', $line);
if (count($info) < 4) {
return PEAR::raiseError(
FILE_PASSWD_E_INVALID_FORMAT_STR,
FILE_PASSWD_E_INVALID_FORMAT
);
}
$user = array_shift($info);
if (!empty($user)) {
array_walk($info, 'trim');
$this->_users[$user] = @array(
'userid' => $info[0],
'lmhash' => $info[1],
'nthash' => $info[2],
'flags' => $info[3],
'lct' => $info[4],
'comment' => $info[5]
);
}
}
$this->_contents = array();
return true;
}
/**
* Add a user
*
* Returns a PEAR_Error if:
* o user already exists
* o user contains illegal characters
*
* @throws PEAR_Error
* @return mixed true on success or PEAR_Error
* @access public
* @param string $user the user to add
* @param string $pass the new plaintext password
* @param array $params additional properties of user
* + userid
* + comment
* @param boolean $isMachine whether to add an machine account
*/
function addUser($user, $pass, $params, $isMachine = false)
{
if ($this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_ALREADY
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
FILE_PASSWD_E_INVALID_CHARS
);
}
if ($isMachine) {
$flags = '[W ]';
$user .= '$';
} else {
$flags = '[U ]';
}
$this->_users[$user] = array(
'flags' => $flags,
'userid' => (int)@$params['userid'],
'comment' => trim(@$params['comment']),
'lct' => 'LCT-' . strToUpper(dechex(time()))
);
return $this->changePasswd($user, $pass);
}
/**
* Modify a certain user
*
* <b>You should not modify the password with this method
* unless it is already encrypted as nthash and lmhash!</b>
*
* Returns a PEAR_Error if:
* o user doesn't exist
* o an invalid property was supplied
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
* @param string $user the user to modify
* @param array $params an associative array of properties to change
*/
function modUser($user, $params)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
FILE_PASSWD_E_INVALID_CHARS
);
}
foreach ($params as $key => $value){
$key = strToLower($key);
if (!isset($this->_users[$user][$key])) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_PROPERTY_STR, $key),
FILE_PASSWD_E_INVALID_PROPERTY
);
}
$this->_users[$user][$key] = trim($value);
$this->_users[$user]['lct']= 'LCT-' . strToUpper(dechex(time()));
}
return true;
}
/**
* Change the passwd of a certain user
*
* Returns a PEAR_Error if <var>$user</var> doesn't exist.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
* @param string $user the user whose passwd should be changed
* @param string $pass the new plaintext passwd
*/
function changePasswd($user, $pass)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
if (empty($pass)) {
$nthash = $lmhash = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
} else {
$nthash = strToUpper(bin2hex($this->msc->ntPasswordHash($pass)));
$lmhash = strToUpper(bin2hex($this->msc->lmPasswordHash($pass)));
}
$this->_users[$user]['nthash'] = $nthash;
$this->_users[$user]['lmhash'] = $lmhash;
return true;
}
/**
* Verifies a user's password
*
* Prefer NT-Hash instead of weak LAN-Manager-Hash
*
* Returns a PEAR_Error if:
* o user doesn't exist
* o user is disabled
*
* @return mixed true if passwds equal, false if they don't or PEAR_Error
* @access public
* @param string $user username
* @param string $nthash NT-Hash in hex
* @param string $lmhash LAN-Manager-Hash in hex
*/
function verifyEncryptedPasswd($user, $nthash, $lmhash = '')
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
if (strstr($this->_users[$user]['flags'], 'D')) {
return PEAR::raiseError("User '$user' is disabled.", 0);
}
if (!empty($nthash)) {
return $this->_users[$user]['nthash'] === strToUpper($nthash);
}
if (!empty($lmhash)) {
return $this->_users[$user]['lm'] === strToUpper($lmhash);
}
return false;
}
/**
* Verifies an account with the given plaintext password
*
* Returns a PEAR_Error if:
* o user doesn't exist
* o user is disabled
*
* @throws PEAR_Error
* @return mixed true if passwds equal, false if they don't or PEAR_Error
* @access public
* @param string $user username
* @param string $pass the plaintext password
*/
function verifyPasswd($user, $pass)
{
$nthash = bin2hex($this->msc->ntPasswordHash($pass));
$lmhash = bin2hex($this->msc->lmPasswordHash($pass));
return $this->verifyEncryptedPasswd($user, $nthash, $lmhash);
}
/**
* Apply changes and rewrite CVS passwd file
*
* Returns a PEAR_Error if:
* o directory in which the file should reside couldn't be created
* o file couldn't be opened in write mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked
* o file couldn't be closed
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function save()
{
$content = '';
foreach ($this->_users as $user => $userdata) {
$content .= $user . ':' .
$userdata['userid'] . ':' .
$userdata['lmhash'] . ':' .
$userdata['nthash'] . ':' .
$userdata['flags'] . ':' .
$userdata['lct'] . ':' .
$userdata['comment']. "\n";
}
return $this->_save($content);
}
/**
* Generate Password
*
* @static
* @access public
* @return string The crypted password.
* @param string $pass The plaintext password.
* @param string $mode The encryption mode to use (nt|lm).
*/
function generatePasswd($pass, $mode = 'nt')
{
$chap = &new Crypt_CHAP_MSv1;
$hash = strToLower($mode) == 'nt' ?
$chap->ntPasswordHash($pass) :
$chap->lmPasswordHash($pass);
return strToUpper(bin2hex($hash));
}
/**
* @ignore
* @deprecated
*/
function generatePassword($pass, $mode = 'nt')
{
return File_Passwd_Smb::generatePasswd($pass, $mode);
}
}
?>

View File

@@ -0,0 +1,660 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File::Passwd::Unix
*
* 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 FileFormats
* @package File_Passwd
* @author Michael Wallner <mike@php.net>
* @copyright 2003-2005 Michael Wallner
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Unix.php,v 1.17 2005/03/30 18:33:33 mike Exp $
* @link http://pear.php.net/package/File_Passwd
*/
/**
* Requires File::Passwd::Common
*/
require_once 'File/Passwd/Common.php';
/**
* Manipulate standard Unix passwd files.
*
* <kbd><u>Usage Example:</u></kbd>
* <code>
* $passwd = &File_Passwd::factory('Unix');
* $passwd->setFile('/my/passwd/file');
* $passwd->load();
* $passwd->addUser('mike', 'secret');
* $passwd->save();
* </code>
*
*
* <kbd><u>Output of listUser()</u></kbd>
* # using the 'name map':
* <pre>
* array
* + user => array
* + pass => crypted_passwd or 'x' if shadowed
* + uid => user id
* + gid => group id
* + gecos => comments
* + home => home directory
* + shell => standard shell
* </pre>
* # without 'name map':
* <pre>
* array
* + user => array
* + 0 => crypted_passwd
* + 1 => ...
* + 2 => ...
* </pre>
*
* @author Michael Wallner <mike@php.net>
* @package File_Passwd
* @version $Revision: 1.17 $
* @access public
*/
class File_Passwd_Unix extends File_Passwd_Common
{
/**
* A 'name map' wich refer to the extra properties
*
* @var array
* @access private
*/
var $_map = array('uid', 'gid', 'gecos', 'home', 'shell');
/**
* Whether to use the 'name map' or not
*
* @var boolean
* @access private
*/
var $_usemap = true;
/**
* Whether the passwords of this passwd file are shadowed in another file
*
* @var boolean
* @access private
*/
var $_shadowed = false;
/**
* Encryption mode, either md5 or des
*
* @var string
* @access private
*/
var $_mode = 'des';
/**
* Supported encryption modes
*
* @var array
* @access private
*/
var $_modes = array('md5' => 'md5', 'des' => 'des');
/**
* Constructor
*
* @access public
* @param string $file path to passwd file
*/
function File_Passwd_Unix($file = 'passwd')
{
parent::__construct($file);
}
/**
* Fast authentication of a certain user
*
* Returns a PEAR_Error if:
* o file doesn't exist
* o file couldn't be opened in read mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked (only if auth fails)
* o file couldn't be closed (only if auth fails)
* o invalid encryption mode <var>$mode</var> was provided
*
* @static call this method statically for a reasonable fast authentication
* @access public
* @return mixed true if authenticated, false if not or PEAR_Error
* @param string $file path to passwd file
* @param string $user user to authenticate
* @param string $pass plaintext password
* @param string $mode encryption mode to use (des or md5)
*/
function staticAuth($file, $user, $pass, $mode)
{
$line = File_Passwd_Common::_auth($file, $user);
if (!$line || PEAR::isError($line)) {
return $line;
}
list(,$real)= explode(':', $line);
$crypted = File_Passwd_Unix::_genPass($pass, $real, $mode);
if (PEAR::isError($crypted)) {
return $crypted;
}
return ($crypted === $real);
}
/**
* Apply changes an rewrite passwd file
*
* Returns a PEAR_Error if:
* o directory in which the file should reside couldn't be created
* o file couldn't be opened in write mode
* o file couldn't be locked exclusively
* o file couldn't be unlocked
* o file couldn't be closed
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function save()
{
$content = '';
foreach ($this->_users as $user => $array){
$pass = array_shift($array);
$extra = implode(':', $array);
$content .= $user . ':' . $pass;
if (!empty($extra)) {
$content .= ':' . $extra;
}
$content .= "\n";
}
return $this->_save($content);
}
/**
* Parse the Unix password file
*
* Returns a PEAR_Error if passwd file has invalid format.
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function parse()
{
$this->_users = array();
foreach ($this->_contents as $line){
$parts = explode(':', $line);
if (count($parts) < 2) {
return PEAR::raiseError(
FILE_PASSWD_E_INVALID_FORMAT_STR,
FILE_PASSWD_E_INVALID_FORMAT
);
}
$user = array_shift($parts);
$pass = array_shift($parts);
if ($pass == 'x') {
$this->_shadowed = true;
}
$values = array();
if ($this->_usemap) {
$values['pass'] = $pass;
foreach ($parts as $i => $value){
if (isset($this->_map[$i])) {
$values[$this->_map[$i]] = $value;
} else {
$values[$i+1] = $value;
}
}
} else {
$values = array_merge(array($pass), $parts);
}
$this->_users[$user] = $values;
}
$this->_contents = array();
return true;
}
/**
* Set the encryption mode
*
* Supported encryption modes are des and md5.
*
* Returns a PEAR_Error if supplied encryption mode is not supported.
*
* @see setMode()
* @see listModes()
*
* @throws PEAR_Error
* @access public
* @return mixed true on succes or PEAR_Error
* @param string $mode encryption mode to use; either md5 or des
*/
function setMode($mode)
{
$mode = strToLower($mode);
if (!isset($this->_modes[$mode])) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
$this->_mode = $mode;
return true;
}
/**
* Get supported encryption modes
*
* <pre>
* array
* + md5
* + des
* </pre>
*
* @see setMode()
* @see getMode()
*
* @access public
* @return array
*/
function listModes()
{
return $this->_modes;
}
/**
* Get actual encryption mode
*
* @see listModes()
* @see setMode()
*
* @access public
* @return string
*/
function getMode()
{
return $this->_mode;
}
/**
* Whether to use the 'name map' of the extra properties or not
*
* Default Unix passwd files look like:
* <pre>
* user:password:user_id:group_id:gecos:home_dir:shell
* </pre>
*
* The default 'name map' for properties except user and password looks like:
* o uid
* o gid
* o gecos
* o home
* o shell
*
* If you want to change the naming of the standard map use
* File_Passwd_Unix::setMap(array()).
*
* @see setMap()
* @see getMap()
*
* @access public
* @return boolean always true if you set a value (true/false) OR
* the actual value if called without param
*
* @param boolean $bool whether to use the 'name map' or not
*/
function useMap($bool = null)
{
if (is_null($bool)) {
return $this->_usemap;
}
$this->_usemap = (bool) $bool;
return true;
}
/**
* Set the 'name map' to use with the extra properties of the user
*
* This map is used for naming the associative array of the extra properties.
*
* Returns a PEAR_Error if <var>$map</var> was not of type array.
*
* @see getMap()
* @see useMap()
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
*/
function setMap($map = array())
{
if (!is_array($map)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_PARAM_MUST_BE_ARRAY_STR, '$map'),
FILE_PASSWD_E_PARAM_MUST_BE_ARRAY
);
}
$this->_map = $map;
return true;
}
/**
* Get the 'name map' which is used for the extra properties of the user
*
* @see setMap()
* @see useMap()
*
* @access public
* @return array
*/
function getMap()
{
return $this->_map;
}
/**
* If the passwords of this passwd file are shadowed in another file.
*
* @access public
* @return boolean
*/
function isShadowed()
{
return $this->_shadowed;
}
/**
* Add an user
*
* The username must start with an alphabetical character and must NOT
* contain any other characters than alphanumerics, the underline and dash.
*
* If you use the 'name map' you should also use these naming in
* the supplied extra array, because your values would get mixed up
* if they are in the wrong order, which is always true if you
* DON'T use the 'name map'!
*
* So be warned and USE the 'name map'!
*
* If the passwd file is shadowed, the user will be added though, but
* with an 'x' as password, and a PEAR_Error will be returned, too.
*
* Returns a PEAR_Error if:
* o user already exists
* o user contains illegal characters
* o encryption mode is not supported
* o passwords are shadowed in another file
* o any element of the <var>$extra</var> array contains a colon (':')
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
* @param string $user the name of the user to add
* @param string $pass the password of the user to add
* @param array $extra extra properties of user to add
*/
function addUser($user, $pass, $extra = array())
{
if ($this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_ALREADY_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_ALREADY
);
}
if (!preg_match($this->_pcre, $user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
FILE_PASSWD_E_INVALID_CHARS
);
}
if (!is_array($extra)) {
setType($extra, 'array');
}
foreach ($extra as $e){
if (strstr($e, ':')) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'Property ', $e),
FILE_PASSWD_E_INVALID_CHARS
);
}
}
/**
* If passwords of the passwd file are shadowed,
* the password of the user will be set to 'x'.
*/
if ($this->_shadowed) {
$pass = 'x';
} else {
$pass = $this->_genPass($pass);
if (PEAR::isError($pass)) {
return $pass;
}
}
/**
* If you don't use the 'name map' the user array will be numeric.
*/
if (!$this->_usemap) {
array_unshift($extra, $pass);
$this->_users[$user] = $extra;
} else {
$map = $this->_map;
array_unshift($map, 'pass');
$extra['pass'] = $pass;
foreach ($map as $key){
$this->_users[$user][$key] = @$extra[$key];
}
}
/**
* Raise a PEAR_Error if passwords are shadowed.
*/
if ($this->_shadowed) {
return PEAR::raiseError(
'Password has been set to \'x\' because they are '.
'shadowed in another file.', 0
);
}
return true;
}
/**
* Modify properties of a certain user
*
* # DON'T MODIFY THE PASSWORD WITH THIS METHOD!
*
* You should use this method only if the 'name map' is used, too.
*
* Returns a PEAR_Error if:
* o user doesn't exist
* o any property contains a colon (':')
*
* @see changePasswd()
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
* @param string $user the user to modify
* @param array $properties an associative array of
* properties to modify
*/
function modUser($user, $properties = array())
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
if (!is_array($properties)) {
setType($properties, 'array');
}
foreach ($properties as $key => $value){
if (strstr($value, ':')) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_CHARS_STR, 'User ', $user),
FILE_PASSWD_E_INVALID_CHARS
);
}
$this->_users[$user][$key] = $value;
}
return true;
}
/**
* Change the password of a certain user
*
* Returns a PEAR_Error if:
* o user doesn't exists
* o passwords are shadowed in another file
* o encryption mode is not supported
*
* @throws PEAR_Error
* @access public
* @return mixed true on success or PEAR_Error
* @param string $user the user whose password should be changed
* @param string $pass the new plaintext password
*/
function changePasswd($user, $pass)
{
if ($this->_shadowed) {
return PEAR::raiseError(
'Passwords of this passwd file are shadowed.',
0
);
}
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
$pass = $this->_genPass($pass);
if (PEAR::isError($pass)) {
return $pass;
}
if ($this->_usemap) {
$this->_users[$user]['pass'] = $pass;
} else {
$this->_users[$user][0] = $pass;
}
return true;
}
/**
* Verify the password of a certain user
*
* Returns a PEAR_Error if:
* o user doesn't exist
* o encryption mode is not supported
*
* @throws PEAR_Error
* @access public
* @return mixed true if passwors equal, false if they don't or PEAR_Error
* @param string $user the user whose password should be verified
* @param string $pass the password to verify
*/
function verifyPasswd($user, $pass)
{
if (!$this->userExists($user)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_EXISTS_NOT_STR, 'User ', $user),
FILE_PASSWD_E_EXISTS_NOT
);
}
$real =
$this->_usemap ?
$this->_users[$user]['pass'] :
$this->_users[$user][0]
;
return ($real === $this->_genPass($pass, $real));
}
/**
* Generate crypted password from the plaintext password
*
* Returns a PEAR_Error if actual encryption mode is not supported.
*
* @throws PEAR_Error
* @access private
* @return mixed the crypted password or PEAR_Error
* @param string $pass the plaintext password
* @param string $salt the crypted password from which to gain the salt
* @param string $mode the encryption mode to use; don't set, because
* it's usually taken from File_Passwd_Unix::_mode
*/
function _genPass($pass, $salt = null, $mode = null)
{
static $crypters;
if (!isset($crypters)) {
$crypters = get_class_methods('File_Passwd');
}
$mode = !isset($mode) ? strToLower($this->_mode) : strToLower($mode);
$func = 'crypt_' . $mode;
if (!in_array($func, $crypters)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, $mode),
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
return call_user_func(array('File_Passwd', $func), $pass, $salt);
}
/**
* Generate Password
*
* Returns PEAR_Error FILE_PASSD_E_INVALID_ENC_MODE if the supplied
* encryption mode is not supported.
*
* @see File_Passwd
* @static
* @access public
* @return mixed The crypted password on success or PEAR_Error on failure.
* @param string $pass The plaintext password.
* @param string $mode The encryption mode to use.
* @param string $salt The salt to use.
*/
function generatePasswd($pass, $mode = FILE_PASSWD_MD5, $salt = null)
{
if (!isset($mode)) {
return PEAR::raiseError(
sprintf(FILE_PASSWD_E_INVALID_ENC_MODE_STR, '<NULL>'),
FILE_PASSWD_E_INVALID_ENC_MODE
);
}
return File_Passwd_Unix::_genPass($pass, $salt, $mode);
}
/**
* @ignore
* @deprecated
*/
function generatePassword($pass, $mode = FILE_PASSWD_MD5, $salt = null)
{
return File_Passwd_Unix::generatePasswd($pass, $mode, $salt);
}
}
?>

View File

@@ -0,0 +1,528 @@
<?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 309134 2011-03-12 16:45:50Z dufuz $
* @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 (($path{0} == '/') || 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 = '';
$path = preg_replace('/[\\\\\/]/', $separator, $path);
if (FILE_WIN32) {
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;
}
/**
* Returns the filesize using a prefix like "kilo", "mebi" or "giga"
*
* @author Christian Weiske <cweiske@cweiske.de>
*
* @param integer $size The size to convert
* @param integer $decimals The number of decimals to use
* @param boolean $long Use long names (kilobyte) instead of
* short ones (kB)
* @param boolean $oldStyle If the old style should be used
* @param boolean $useBiBytes If the "BiBytes" names should be
* used [applies only to !$bOldStyle]
*
* @return string The filesize in human readable format
*
* @static
*/
function prefixed(
$size, $decimals = 1, $long = false, $oldStyle = true,
$useBiBytes = true
) {
$base = ($oldStyle || $useBiBytes) ? 1024 : 1000;
$names = array(
'', 'kilo', 'mega', 'giga', 'tera',
'peta', 'exa', 'zetta', 'yotta'
);
$max = count($names) - 1;
for ($a = 0; $size >= $base && $a < $max; $a++) {
$size /= $base;
}
$name = ($oldStyle || !$useBiBytes)
? $names[$a]
: $names[$a] . 'bi';
if (!$long) {
$name = $oldStyle || !$useBiBytes
? strtoupper(substr($name, 0, 1))
: strtoupper(substr($name, 0, 1)) . 'i';
$name .= 'B';
} else {
$name .= $size == 1 ? 'byte' : 'bytes';
}
return round($size, $decimals) . ' ' . $name;
}
}