PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Filter/RealPath.php

https://bitbucket.org/pcelta/zf2
PHP | 121 lines | 69 code | 11 blank | 41 comment | 16 complexity | 669e5f8731d24e92eb58bd1d9b66c8d0 MD5 | raw file
  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. use Traversable;
  12. use Zend\Stdlib\ErrorHandler;
  13. /**
  14. * @category Zend
  15. * @package Zend_Filter
  16. */
  17. class RealPath extends AbstractFilter
  18. {
  19. /**
  20. * @var array $options
  21. */
  22. protected $options = array(
  23. 'exists' => true
  24. );
  25. /**
  26. * Class constructor
  27. *
  28. * @param boolean|Traversable $existsOrOptions Options to set
  29. */
  30. public function __construct($existsOrOptions = true)
  31. {
  32. if ($existsOrOptions !== null) {
  33. if (!static::isOptions($existsOrOptions)) {
  34. $this->setExists($existsOrOptions);
  35. } else {
  36. $this->setOptions($existsOrOptions);
  37. }
  38. }
  39. }
  40. /**
  41. * Sets if the path has to exist
  42. * TRUE when the path must exist
  43. * FALSE when not existing paths can be given
  44. *
  45. * @param boolean $flag Path must exist
  46. * @return RealPath
  47. */
  48. public function setExists($flag = true)
  49. {
  50. $this->options['exists'] = (boolean) $flag;
  51. return $this;
  52. }
  53. /**
  54. * Returns true if the filtered path must exist
  55. *
  56. * @return boolean
  57. */
  58. public function getExists()
  59. {
  60. return $this->options['exists'];
  61. }
  62. /**
  63. * Defined by Zend\Filter\FilterInterface
  64. *
  65. * Returns realpath($value)
  66. *
  67. * @param string $value
  68. * @return string
  69. */
  70. public function filter($value)
  71. {
  72. $path = (string) $value;
  73. if ($this->options['exists']) {
  74. return realpath($path);
  75. }
  76. ErrorHandler::start();
  77. $realpath = realpath($path);
  78. ErrorHandler::stop();
  79. if ($realpath) {
  80. return $realpath;
  81. }
  82. $drive = '';
  83. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  84. $path = preg_replace('/[\\\\\/]/', DIRECTORY_SEPARATOR, $path);
  85. if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) {
  86. list(, $drive, $path) = $matches;
  87. } else {
  88. $cwd = getcwd();
  89. $drive = substr($cwd, 0, 2);
  90. if (substr($path, 0, 1) != DIRECTORY_SEPARATOR) {
  91. $path = substr($cwd, 3) . DIRECTORY_SEPARATOR . $path;
  92. }
  93. }
  94. } elseif (substr($path, 0, 1) != DIRECTORY_SEPARATOR) {
  95. $path = getcwd() . DIRECTORY_SEPARATOR . $path;
  96. }
  97. $stack = array();
  98. $parts = explode(DIRECTORY_SEPARATOR, $path);
  99. foreach ($parts as $dir) {
  100. if (strlen($dir) && $dir !== '.') {
  101. if ($dir == '..') {
  102. array_pop($stack);
  103. } else {
  104. array_push($stack, $dir);
  105. }
  106. }
  107. }
  108. return $drive . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $stack);
  109. }
  110. }