File indexing completed on 2024-12-22 05:36:41

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Filter
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Filter_Interface
0024  */
0025 // require_once 'Zend/Filter/Interface.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Filter
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Filter_RealPath implements Zend_Filter_Interface
0034 {
0035     /**
0036      * @var boolean $_pathExists
0037      */
0038     protected $_exists = true;
0039 
0040     /**
0041      * Class constructor
0042      *
0043      * @param boolean|Zend_Config $options Options to set
0044      */
0045     public function __construct($options = true)
0046     {
0047         $this->setExists($options);
0048     }
0049 
0050     /**
0051      * Returns true if the filtered path must exist
0052      *
0053      * @return boolean
0054      */
0055     public function getExists()
0056     {
0057         return $this->_exists;
0058     }
0059 
0060     /**
0061      * Sets if the path has to exist
0062      * TRUE when the path must exist
0063      * FALSE when not existing paths can be given
0064      *
0065      * @param boolean|Zend_Config $exists Path must exist
0066      * @return Zend_Filter_RealPath
0067      */
0068     public function setExists($exists)
0069     {
0070         if ($exists instanceof Zend_Config) {
0071             $exists = $exists->toArray();
0072         }
0073 
0074         if (is_array($exists)) {
0075             if (isset($exists['exists'])) {
0076                 $exists = (boolean) $exists['exists'];
0077             }
0078         }
0079 
0080         $this->_exists = (boolean) $exists;
0081         return $this;
0082     }
0083 
0084     /**
0085      * Defined by Zend_Filter_Interface
0086      *
0087      * Returns realpath($value)
0088      *
0089      * @param  string $value
0090      * @return string
0091      */
0092     public function filter($value)
0093     {
0094         $path = (string) $value;
0095         if ($this->_exists) {
0096             return realpath($path);
0097         }
0098 
0099         $realpath = @realpath($path);
0100         if ($realpath) {
0101             return $realpath;
0102         }
0103 
0104         $drive = '';
0105         if (substr(PHP_OS, 0, 3) == 'WIN') {
0106             $path = preg_replace('/[\\\\\/]/', DIRECTORY_SEPARATOR, $path);
0107             if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) {
0108                 list($fullMatch, $drive, $path) = $matches;
0109             } else {
0110                 $cwd   = getcwd();
0111                 $drive = substr($cwd, 0, 2);
0112                 if (substr($path, 0, 1) != DIRECTORY_SEPARATOR) {
0113                     $path = substr($cwd, 3) . DIRECTORY_SEPARATOR . $path;
0114                 }
0115             }
0116         } elseif (substr($path, 0, 1) != DIRECTORY_SEPARATOR) {
0117             $path = getcwd() . DIRECTORY_SEPARATOR . $path;
0118         }
0119 
0120         $stack = array();
0121         $parts = explode(DIRECTORY_SEPARATOR, $path);
0122         foreach ($parts as $dir) {
0123             if (strlen($dir) && $dir !== '.') {
0124                 if ($dir == '..') {
0125                     array_pop($stack);
0126                 } else {
0127                     array_push($stack, $dir);
0128                 }
0129             }
0130         }
0131 
0132         return $drive . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $stack);
0133     }
0134 }