PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Filter/RealPath.php

https://bitbucket.org/aboozar/zf2
PHP | 116 lines | 65 code | 10 blank | 41 comment | 16 complexity | 9c70feb796c623bc3c7a1092e469a825 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Filter
  9. */
  10. namespace Zend\Filter;
  11. /**
  12. * @category Zend
  13. * @package Zend_Filter
  14. */
  15. class RealPath extends AbstractFilter
  16. {
  17. /**
  18. * @var boolean $_pathExists
  19. */
  20. protected $options = array(
  21. 'exists' => true
  22. );
  23. /**
  24. * Class constructor
  25. *
  26. * @param boolean|\Traversable $options Options to set
  27. */
  28. public function __construct($existsOrOptions = true)
  29. {
  30. if ($existsOrOptions !== null) {
  31. if (!static::isOptions($existsOrOptions)){
  32. $this->setExists($existsOrOptions);
  33. } else {
  34. $this->setOptions($existsOrOptions);
  35. }
  36. }
  37. }
  38. /**
  39. * Sets if the path has to exist
  40. * TRUE when the path must exist
  41. * FALSE when not existing paths can be given
  42. *
  43. * @param boolean $flag Path must exist
  44. * @return RealPath
  45. */
  46. public function setExists($flag = true)
  47. {
  48. $this->options['exists'] = (boolean) $flag;
  49. return $this;
  50. }
  51. /**
  52. * Returns true if the filtered path must exist
  53. *
  54. * @return boolean
  55. */
  56. public function getExists()
  57. {
  58. return $this->options['exists'];
  59. }
  60. /**
  61. * Defined by Zend\Filter\FilterInterface
  62. *
  63. * Returns realpath($value)
  64. *
  65. * @param string $value
  66. * @return string
  67. */
  68. public function filter($value)
  69. {
  70. $path = (string) $value;
  71. if ($this->options['exists']) {
  72. return realpath($path);
  73. }
  74. $realpath = @realpath($path);
  75. if ($realpath) {
  76. return $realpath;
  77. }
  78. $drive = '';
  79. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  80. $path = preg_replace('/[\\\\\/]/', DIRECTORY_SEPARATOR, $path);
  81. if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) {
  82. list(, $drive, $path) = $matches;
  83. } else {
  84. $cwd = getcwd();
  85. $drive = substr($cwd, 0, 2);
  86. if (substr($path, 0, 1) != DIRECTORY_SEPARATOR) {
  87. $path = substr($cwd, 3) . DIRECTORY_SEPARATOR . $path;
  88. }
  89. }
  90. } elseif (substr($path, 0, 1) != DIRECTORY_SEPARATOR) {
  91. $path = getcwd() . DIRECTORY_SEPARATOR . $path;
  92. }
  93. $stack = array();
  94. $parts = explode(DIRECTORY_SEPARATOR, $path);
  95. foreach ($parts as $dir) {
  96. if (strlen($dir) && $dir !== '.') {
  97. if ($dir == '..') {
  98. array_pop($stack);
  99. } else {
  100. array_push($stack, $dir);
  101. }
  102. }
  103. }
  104. return $drive . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $stack);
  105. }
  106. }