/usr/share/php/File.php is in php-file 1.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | <?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* File
*
* 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 Richard Heyes <richard@php.net>
* @author Tal Peer <tal@php.net>
* @author Michael Wallner <mike@php.net>
* @copyright 2002-2005 The Authors
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: File.php,v 1.38 2007/03/24 16:38:56 dufuz Exp $
* @link http://pear.php.net/package/File
*/
/**
* Requires PEAR
*/
require_once 'PEAR.php';
/**
* The default number of bytes for reading
*/
if (!defined('FILE_DEFAULT_READSIZE')) {
define('FILE_DEFAULT_READSIZE', 1024, true);
}
/**
* The maximum number of bytes for reading lines
*/
if (!defined('FILE_MAX_LINE_READSIZE')) {
define('FILE_MAX_LINE_READSIZE', 40960, true);
}
/**
* Whether file locks should block
*/
if (!defined('FILE_LOCKS_BLOCK')) {
define('FILE_LOCKS_BLOCK', true, true);
}
/**
* Mode to use for reading from files
*/
define('FILE_MODE_READ', 'rb', true);
/**
* Mode to use for truncating files, then writing
*/
define('FILE_MODE_WRITE', 'wb', true);
/**
* Mode to use for appending to files
*/
define('FILE_MODE_APPEND', 'ab', true);
/**
* Use this when a shared (read) lock is required
*/
define('FILE_LOCK_SHARED', LOCK_SH | (FILE_LOCKS_BLOCK ? 0 : LOCK_NB), true);
/**
* Use this when an exclusive (write) lock is required
*/
define('FILE_LOCK_EXCLUSIVE', LOCK_EX | (FILE_LOCKS_BLOCK ? 0 : LOCK_NB), true);
/**
* Class for handling files
*
* A class with common functions for writing,
* reading and handling files and directories
*
* @author Richard Heyes <richard@php.net>
* @author Tal Peer <tal@php.net>
* @author Michael Wallner <mike@php.net>
* @access public
* @package File
*
* @static
*/
class File extends PEAR
{
/**
* Destructor
*
* Unlocks any locked file pointers and closes all filepointers
*
* @access private
*/
function _File()
{
File::closeAll();
}
/**
* Handles file pointers. If a file pointer needs to be opened,
* it will be. If it already exists (based on filename and mode)
* then the existing one will be returned.
*
* @access private
* @param string $filename Filename to be used
* @param string $mode Mode to open the file in
* @param mixed $lock Type of lock to use
* @return mixed PEAR_Error on error or file pointer resource on success
*/
function _getFilePointer($filename, $mode, $lock = false)
{
$filePointers = &PEAR::getStaticProperty('File', 'filePointers');
// Win32 is case-insensitive
if (OS_WINDOWS) {
$filename = strtolower($filename);
}
// check if file pointer already exists
if (!isset($filePointers[$filename][$mode]) ||
!is_resource($filePointers[$filename][$mode])) {
// check if we can open the file in the desired mode
switch ($mode)
{
case FILE_MODE_READ:
if (!preg_match('/^.+(?<!file):\/\//i', $filename) &&
!file_exists($filename)) {
return PEAR::raiseError("File does not exist: $filename");
}
break;
case FILE_MODE_APPEND:
case FILE_MODE_WRITE:
if (file_exists($filename)) {
if (!is_writable($filename)) {
return PEAR::raiseError("File is not writable: $filename");
}
} elseif (!is_writable($dir = dirname($filename))) {
return PEAR::raiseError("Cannot create file in directory: $dir");
}
break;
default:
return PEAR::raiseError("Invalid access mode: $mode");
}
// open file
$filePointers[$filename][$mode] = @fopen($filename, $mode);
if (!is_resource($filePointers[$filename][$mode])) {
return PEAR::raiseError('Failed to open file: ' . $filename);
}
}
// lock file
if ($lock) {
$lock = $mode == FILE_MODE_READ ? FILE_LOCK_SHARED : FILE_LOCK_EXCLUSIVE;
$locks = &PEAR::getStaticProperty('File', 'locks');
if (@flock($filePointers[$filename][$mode], $lock)) {
$locks[] = &$filePointers[$filename][$mode];
} elseif (FILE_LOCKS_BLOCK) {
return PEAR::raiseError("File already locked: $filename");
} else {
return PEAR::raiseError("Could not lock file: $filename");
}
}
return $filePointers[$filename][$mode];
}
/**
* Reads an entire file and returns it.
* Uses file_get_contents if available.
*
* @access public
* @param string $filename Name of file to read from
* @param mixed $lock Type of lock to use
* @return mixed PEAR_Error if an error has occured or a string with the contents of the the file
*/
function readAll($filename, $lock = false)
{
if (false === $file = @file_get_contents($filename)) {
return PEAR::raiseError("Cannot read file: $filename");
}
return $file;
}
/**
* Returns a specified number of bytes of a file.
* Defaults to FILE_DEFAULT_READSIZE. If $size is 0, all file will be read.
*
* @access public
* @param string $filename Name of file to read from
* @param integer $size Bytes to read
* @param mixed $lock Type of lock to use
* @return mixed PEAR_Error on error or a string which contains the data read
* Will also return false upon EOF
*/
function read($filename, $size = FILE_DEFAULT_READSIZE, $lock = false)
{
static $filePointers;
if ($size == 0) {
return File::readAll($filename, $lock);
}
if (!isset($filePointers[$filename]) ||
!is_resource($filePointers[$filename])) {
$fp = File::_getFilePointer($filename, FILE_MODE_READ, $lock);
if (PEAR::isError($fp)) {
return $fp;
}
$filePointers[$filename] = $fp;
} else {
$fp = $filePointers[$filename];
}
return !feof($fp) ? fread($fp, $size) : false;
}
/**
* Writes the given data to the given filename.
* Defaults to no lock, append mode.
*
* @access public
* @param string $filename Name of file to write to
* @param string $data Data to write to file
* @param string $mode Mode to open file in
* @param mixed $lock Type of lock to use
* @return mixed PEAR_Error on error or number of bytes written to file.
*/
function write($filename, $data, $mode = FILE_MODE_APPEND, $lock = false)
{
$fp = File::_getFilePointer($filename, $mode, $lock);
if (PEAR::isError($fp)) {
return $fp;
}
if (false === $bytes = @fwrite($fp, $data, strlen($data))) {
return PEAR::raiseError("Cannot write data: '$data' to file: '$filename'");
}
return $bytes;
}
/**
* Reads and returns a single character from given filename
*
* @access public
* @param string $filename Name of file to read from
* @param mixed $lock Type of lock to use
* @return mixed PEAR_Error on error or one character of the specified file
*/
function readChar($filename, $lock = false)
{
return File::read($filename, 1, $lock);
}
/**
* Writes a single character to a file
*
* @access public
* @param string $filename Name of file to write to
* @param string $char Character to write
* @param string $mode Mode to use when writing
* @param mixed $lock Type of lock to use
* @return mixed PEAR_Error on error, or 1 on success
*/
function writeChar($filename, $char, $mode = FILE_MODE_APPEND, $lock = false)
{
$fp = File::_getFilePointer($filename, $mode, $lock);
if (PEAR::isError($fp)) {
return $fp;
}
if (false === @fwrite($fp, $char, 1)) {
return PEAR::raiseError("Cannot write data: '$data' to file: '$filename'");
}
return 1;
}
/**
* Returns a line of the file (without trailing CRLF).
* Maximum read line length is FILE_MAX_LINE_READSIZE.
*
* @access public
* @param string $filename Name of file to read from
* @param boolean $lock Whether file should be locked
* @return mixed PEAR_Error on error or a string containing the line read from file
*/
function readLine($filename, $lock = false)
{
static $filePointers; // Used to prevent unnecessary calls to _getFilePointer()
if (!isset($filePointers[$filename]) ||
!is_resource($filePointers[$filename])) {
$fp = File::_getFilePointer($filename, FILE_MODE_READ, $lock);
if (PEAR::isError($fp)) {
return $fp;
}
$filePointers[$filename] = $fp;
} else {
$fp = $filePointers[$filename];
}
if (feof($fp)) {
return false;
}
return rtrim(fgets($fp, FILE_MAX_LINE_READSIZE), "\r\n");
}
/**
* Writes a single line, appending a LF (by default)
*
* @access public
* @param string $filename Name of file to write to
* @param string $line Line of data to be written to file
* @param string $mode Write mode, can be either FILE_MODE_WRITE or FILE_MODE_APPEND
* @param string $crlf The CRLF your system is using. UNIX = \n Windows = \r\n Mac = \r
* @param mixed $lock Whether to lock the file
* @return mixed PEAR_Error on error or number of bytes written to file (including appended crlf)
*/
function writeLine($filename, $line, $mode = FILE_MODE_APPEND, $crlf = "\n", $lock = false)
{
$fp = File::_getFilePointer($filename, $mode, $lock);
if (PEAR::isError($fp)) {
return $fp;
}
if (false === $bytes = fwrite($fp, $line . $crlf)) {
return PEAR::raiseError("Cannot write data: '$data' to file: '$file'");
}
return $bytes;
}
/**
* This rewinds a filepointer to the start of a file
*
* @access public
* @param string $filename The filename
* @param string $mode Mode the file was opened in
* @return mixed PEAR Error on error, true on success
*/
function rewind($filename, $mode)
{
$fp = File::_getFilePointer($filename, $mode);
if (PEAR::isError($fp)) {
return $fp;
}
if (!@rewind($fp)) {
return PEAR::raiseError("Cannot rewind file: $filename");
}
return true;
}
/**
* Closes all open file pointers
*
* @access public
* @return void
*/
function closeAll()
{
$locks = &PEAR::getStaticProperty('File', 'locks');
$filePointers = &PEAR::getStaticProperty('File', 'filePointers');
// unlock files
for ($i = 0, $c = count($locks); $i < $c; $i++) {
is_resource($locks[$i]) and @flock($locks[$i], LOCK_UN);
}
// close files
if (!empty($filePointers)) {
foreach ($filePointers as $fname => $modes) {
foreach (array_keys($modes) as $mode) {
if (is_resource($filePointers[$fname][$mode])) {
@fclose($filePointers[$fname][$mode]);
}
unset($filePointers[$fname][$mode]);
}
}
}
}
/**
* This closes an open file pointer
*
* @access public
* @param string $filename The filename that was opened
* @param string $mode Mode the file was opened in
* @return mixed PEAR Error on error, true otherwise
*/
function close($filename, $mode)
{
$filePointers = &PEAR::getStaticProperty('File', 'filePointers');
if (OS_WINDOWS) {
$filename = strToLower($filename);
}
if (!isset($filePointers[$filename][$mode])) {
return true;
}
$fp = $filePointers[$filename][$mode];
unset($filePointers[$filename][$mode]);
if (is_resource($fp)) {
// unlock file
@flock($fp, LOCK_UN);
// close file
if (!@fclose($fp)) {
return PEAR::raiseError("Cannot close file: $filename");
}
}
return true;
}
/**
* This unlocks a locked file pointer.
*
* @access public
* @param string $filename The filename that was opened
* @param string $mode Mode the file was opened in
* @return mixed PEAR Error on error, true otherwise
*/
function unlock($filename, $mode)
{
$fp = File::_getFilePointer($filename, $mode);
if (PEAR::isError($fp)) {
return $fp;
}
if (!@flock($fp, LOCK_UN)) {
return PEAR::raiseError("Cacnnot unlock file: $filename");
}
return true;
}
/**
* @deprecated
*/
function stripTrailingSeparators($path, $separator = DIRECTORY_SEPARATOR)
{
if ($path === $separator) {
return $path;
}
return rtrim($path, $separator);
}
/**
* @deprecated
*/
function stripLeadingSeparators($path, $separator = DIRECTORY_SEPARATOR)
{
if ($path === $separator) {
return $path;
}
return ltrim($path, $separator);
}
/**
* @deprecated Use File_Util::buildPath() instead.
*/
function buildPath($parts, $separator = DIRECTORY_SEPARATOR)
{
require_once 'File/Util.php';
return File_Util::buildPath($parts, $separator);
}
/**
* @deprecated Use File_Util::skipRoot() instead.
*/
function skipRoot($path)
{
require_once 'File/Util.php';
return File_Util::skipRoot($path);
}
/**
* @deprecated Use File_Util::tmpDir() instead.
*/
function getTempDir()
{
require_once 'File/Util.php';
return File_Util::tmpDir();
}
/**
* @deprecated Use File_Util::tmpFile() instead.
*/
function getTempFile($dirname = null)
{
require_once 'File/Util.php';
return File_Util::tmpFile($dirname);
}
/**
* @deprecated Use File_Util::isAbsolute() instead.
*/
function isAbsolute($path)
{
require_once 'File/Util.php';
return File_Util::isAbsolute($path);
}
/**
* @deprecated Use File_Util::relativePath() instead.
*/
function relativePath($path, $root, $separator = DIRECTORY_SEPARATOR)
{
require_once 'File/Util.php';
return File_Util::relativePath($path, $root, $separator);
}
/**
* @deprecated Use File_Util::realpath() instead.
*/
function realpath($path, $separator = DIRECTORY_SEPARATOR)
{
require_once 'File/Util.php';
return File_Util::realpath($path, $separator);
}
}
PEAR::registerShutdownFunc(array('File', '_File'));
?>
|