PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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