|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Filter |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: RealPath.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Filter_Interface |
|
24 */ |
|
25 require_once 'Zend/Filter/Interface.php'; |
|
26 |
|
27 /** |
|
28 * @category Zend |
|
29 * @package Zend_Filter |
|
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
31 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
32 */ |
|
33 class Zend_Filter_RealPath implements Zend_Filter_Interface |
|
34 { |
|
35 /** |
|
36 * @var boolean $_pathExists |
|
37 */ |
|
38 protected $_exists = true; |
|
39 |
|
40 /** |
|
41 * Class constructor |
|
42 * |
|
43 * @param boolean|Zend_Config $options Options to set |
|
44 */ |
|
45 public function __construct($options = true) |
|
46 { |
|
47 $this->setExists($options); |
|
48 } |
|
49 |
|
50 /** |
|
51 * Returns true if the filtered path must exist |
|
52 * |
|
53 * @return boolean |
|
54 */ |
|
55 public function getExists() |
|
56 { |
|
57 return $this->_exists; |
|
58 } |
|
59 |
|
60 /** |
|
61 * Sets if the path has to exist |
|
62 * TRUE when the path must exist |
|
63 * FALSE when not existing paths can be given |
|
64 * |
|
65 * @param boolean|Zend_Config $exists Path must exist |
|
66 * @return Zend_Filter_RealPath |
|
67 */ |
|
68 public function setExists($exists) |
|
69 { |
|
70 if ($exists instanceof Zend_Config) { |
|
71 $exists = $exists->toArray(); |
|
72 } |
|
73 |
|
74 if (is_array($exists)) { |
|
75 if (isset($exists['exists'])) { |
|
76 $exists = (boolean) $exists['exists']; |
|
77 } |
|
78 } |
|
79 |
|
80 $this->_exists = (boolean) $exists; |
|
81 return $this; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Defined by Zend_Filter_Interface |
|
86 * |
|
87 * Returns realpath($value) |
|
88 * |
|
89 * @param string $value |
|
90 * @return string |
|
91 */ |
|
92 public function filter($value) |
|
93 { |
|
94 $path = (string) $value; |
|
95 if ($this->_exists) { |
|
96 return realpath($path); |
|
97 } |
|
98 |
|
99 $realpath = @realpath($path); |
|
100 if ($realpath) { |
|
101 return $realpath; |
|
102 } |
|
103 |
|
104 $drive = ''; |
|
105 if (substr(PHP_OS, 0, 3) == 'WIN') { |
|
106 $path = preg_replace('/[\\\\\/]/', DIRECTORY_SEPARATOR, $path); |
|
107 if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) { |
|
108 list($fullMatch, $drive, $path) = $matches; |
|
109 } else { |
|
110 $cwd = getcwd(); |
|
111 $drive = substr($cwd, 0, 2); |
|
112 if (substr($path, 0, 1) != DIRECTORY_SEPARATOR) { |
|
113 $path = substr($cwd, 3) . DIRECTORY_SEPARATOR . $path; |
|
114 } |
|
115 } |
|
116 } elseif (substr($path, 0, 1) != DIRECTORY_SEPARATOR) { |
|
117 $path = getcwd() . DIRECTORY_SEPARATOR . $path; |
|
118 } |
|
119 |
|
120 $stack = array(); |
|
121 $parts = explode(DIRECTORY_SEPARATOR, $path); |
|
122 foreach ($parts as $dir) { |
|
123 if (strlen($dir) && $dir !== '.') { |
|
124 if ($dir == '..') { |
|
125 array_pop($stack); |
|
126 } else { |
|
127 array_push($stack, $dir); |
|
128 } |
|
129 } |
|
130 } |
|
131 |
|
132 return $drive . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $stack); |
|
133 } |
|
134 } |