/Zend/Filter/RealPath.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 139 lines · 66 code · 13 blank · 60 comment · 17 complexity · 0893f2c8341745d55fe9fc466f8f9f36 MD5 · raw file

  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. * @namespace
  23. */
  24. namespace Zend\Filter;
  25. /**
  26. * @see Zend_Filter_Interface
  27. */
  28. require_once 'Zend/Filter/Interface.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Filter
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class RealPath implements FilterInterface
  36. {
  37. /**
  38. * @var boolean $_pathExists
  39. */
  40. protected $_exists = true;
  41. /**
  42. * Class constructor
  43. *
  44. * @param boolean|Zend_Config $options Options to set
  45. */
  46. public function __construct($options = true)
  47. {
  48. $this->setExists($options);
  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. * Sets if the path has to exist
  61. * TRUE when the path must exist
  62. * FALSE when not existing paths can be given
  63. *
  64. * @param boolean|Zend_Config $exists Path must exist
  65. * @return Zend_Filter_RealPath
  66. */
  67. public function setExists($exists)
  68. {
  69. if ($exists instanceof \Zend\Config\Config) {
  70. $exists = $exists->toArray();
  71. }
  72. if (is_array($exists)) {
  73. if (isset($exists['exists'])) {
  74. $exists = (boolean) $exists['exists'];
  75. }
  76. }
  77. $this->_exists = (boolean) $exists;
  78. return $this;
  79. }
  80. /**
  81. * Defined by Zend_Filter_Interface
  82. *
  83. * Returns realpath($value)
  84. *
  85. * @param string $value
  86. * @return string
  87. */
  88. public function filter($value)
  89. {
  90. $path = (string) $value;
  91. if ($this->_exists) {
  92. return realpath($path);
  93. }
  94. $realpath = @realpath($path);
  95. if ($realpath) {
  96. return $realpath;
  97. }
  98. $drive = '';
  99. if (substr(PHP_OS, 0, 3) == 'WIN') {
  100. $path = preg_replace('/[\\\\\/]/', DIRECTORY_SEPARATOR, $path);
  101. if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) {
  102. list($fullMatch, $drive, $path) = $matches;
  103. } else {
  104. $cwd = getcwd();
  105. $drive = substr($cwd, 0, 2);
  106. if (substr($path, 0, 1) != DIRECTORY_SEPARATOR) {
  107. $path = substr($cwd, 3) . DIRECTORY_SEPARATOR . $path;
  108. }
  109. }
  110. } elseif (substr($path, 0, 1) != DIRECTORY_SEPARATOR) {
  111. $path = getcwd() . DIRECTORY_SEPARATOR . $path;
  112. }
  113. $stack = array();
  114. $parts = explode(DIRECTORY_SEPARATOR, $path);
  115. foreach ($parts as $dir) {
  116. if (strlen($dir) && $dir !== '.') {
  117. if ($dir == '..') {
  118. array_pop($stack);
  119. } else {
  120. array_push($stack, $dir);
  121. }
  122. }
  123. }
  124. return $drive . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $stack);
  125. }
  126. }