PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Filter/RealPath.php

https://github.com/praveenuniyal/zf2
PHP | 135 lines | 83 code | 13 blank | 39 comment | 18 complexity | 34dc8f2f8f2251c0e06a4dc712687541 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 self
  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. * If the value provided is non-scalar, the value will remain unfiltered
  63. * and an E_USER_WARNING will be raised indicating it's unfilterable.
  64. *
  65. * @param string $value
  66. * @return string|mixed
  67. */
  68. public function filter($value)
  69. {
  70. if (null === $value) {
  71. return null;
  72. }
  73. if (!is_scalar($value)){
  74. trigger_error(
  75. sprintf(
  76. '%s expects parameter to be scalar, "%s" given; cannot filter',
  77. __METHOD__,
  78. (is_object($value) ? get_class($value) : gettype($value))
  79. ),
  80. E_USER_WARNING
  81. );
  82. return $value;
  83. }
  84. $path = (string) $value;
  85. if ($this->options['exists']) {
  86. return realpath($path);
  87. }
  88. ErrorHandler::start();
  89. $realpath = realpath($path);
  90. ErrorHandler::stop();
  91. if ($realpath) {
  92. return $realpath;
  93. }
  94. $drive = '';
  95. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  96. $path = preg_replace('/[\\\\\/]/', DIRECTORY_SEPARATOR, $path);
  97. if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) {
  98. list(, $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. }