Advertisement
stuppid_bot

Untitled

Oct 22nd, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | None | 0 0
  1. /**
  2.   * Удаляет сегменты с точками из пути.
  3.   *
  4.   * Алгоритм описан по ссылке:
  5.   * https://tools.ietf.org/html/rfc3986#section-5.2.4
  6.   *
  7.   */
  8. function removeDotSegments($path) {
  9.   $input = $path;
  10.   $out = array();
  11.   while (strlen($input)) {
  12.     // A.  If the input buffer begins with a prefix of "../" or "./",
  13.     //     then remove that prefix from the input buffer; otherwise,
  14.     $input = preg_replace('#^[.][.]?/#', '/', $input);
  15.     // B.  if the input buffer begins with a prefix of "/./" or "/.",
  16.     //     where "." is a complete path segment, then replace that
  17.     //     prefix with "/" in the input buffer; otherwise,
  18.     if (substr($input, 0, 3) === '/./') {
  19.       $out[] = '/';
  20.       $input = substr($input, 2);
  21.       continue;
  22.     }
  23.     if ($input == '/.') {
  24.       $out[] = '/';
  25.       break;
  26.     }
  27.     // C.  if the input buffer begins with a prefix of "/../" or "/..",
  28.     //     where ".." is a complete path segment, then replace that
  29.     //     prefix with "/" in the input buffer and remove the last
  30.     //     segment and its preceding "/" (if any) from the output
  31.     //     buffer; otherwise,
  32.     if (substr($input, 0, 4) === '/../') {
  33.       array_pop($out);
  34.       $input = substr($input, 3);
  35.       continue;
  36.     }
  37.     if ($input === '/..') {
  38.       $out[] = '/';
  39.       break;
  40.     }
  41.     // D.  if the input buffer consists only of "." or "..", then remove
  42.     //     that from the input buffer; otherwise,
  43.     if ($input === '.' || $input === '..') {
  44.       break;
  45.     }
  46.     // E.  move the first path segment in the input buffer to the end of
  47.     //     the output buffer, including the initial "/" character (if
  48.     //     any) and any subsequent characters up to, but not including,
  49.     //     the next "/" character or the end of the input buffer.
  50.     $pos = strpos($input, '/', 1);
  51.     if ($pos === false) {
  52.       $out[] = $input;
  53.       break;
  54.     }
  55.     $out[] = substr($input, 0, $pos);
  56.     $input = substr($input, $pos);
  57.   }
  58.   return implode($out);
  59. }
  60.  
  61.  
  62. // STEP   OUTPUT BUFFER         INPUT BUFFER
  63. //
  64. //       1 :                         /a/b/c/./../../g
  65. //       2E:   /a                    /b/c/./../../g
  66. //       2E:   /a/b                  /c/./../../g
  67. //       2E:   /a/b/c                /./../../g
  68. //       2B:   /a/b/c                /../../g
  69. //       2C:   /a/b                  /../g
  70. //       2C:   /a                    /g
  71. //       2E:   /a/g
  72. var_dump(removeDotSegments('/a/b/c/./../../g'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement