PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Filter/RealPath.php

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