You are here

function PclZipUtilPathReduction in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/moodle/lib/pclzip/pclzip.lib.php \PclZipUtilPathReduction()
4 calls to PclZipUtilPathReduction()
PclZip::privAddFile in includes/moodle/lib/pclzip/pclzip.lib.php
PclZip::privCalculateStoredFilename in includes/moodle/lib/pclzip/pclzip.lib.php
PclZip::privFileDescrExpand in includes/moodle/lib/pclzip/pclzip.lib.php
PclZip::privFileDescrParseAtt in includes/moodle/lib/pclzip/pclzip.lib.php

File

includes/moodle/lib/pclzip/pclzip.lib.php, line 5419

Code

function PclZipUtilPathReduction($p_dir) {

  //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathReduction", "dir='$p_dir'");
  $v_result = "";

  // ----- Look for not empty path
  if ($p_dir != "") {

    // ----- Explode path by directory names
    $v_list = explode("/", $p_dir);

    // ----- Study directories from last to first
    $v_skip = 0;
    for ($i = sizeof($v_list) - 1; $i >= 0; $i--) {

      // ----- Look for current path
      if ($v_list[$i] == ".") {

        // ----- Ignore this directory
        // Should be the first $i=0, but no check is done
      }
      else {
        if ($v_list[$i] == "..") {
          $v_skip++;
        }
        else {
          if ($v_list[$i] == "") {

            // ----- First '/' i.e. root slash
            if ($i == 0) {
              $v_result = "/" . $v_result;
              if ($v_skip > 0) {

                // ----- It is an invalid path, so the path is not modified
                // TBC
                $v_result = $p_dir;

                //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid path is unchanged");
                $v_skip = 0;
              }
            }
            else {
              if ($i == sizeof($v_list) - 1) {
                $v_result = $v_list[$i];
              }
              else {

                // ----- Ignore only the double '//' in path,
                // but not the first and last '/'
              }
            }
          }
          else {

            // ----- Look for item to skip
            if ($v_skip > 0) {
              $v_skip--;
            }
            else {
              $v_result = $v_list[$i] . ($i != sizeof($v_list) - 1 ? "/" . $v_result : "");
            }
          }
        }
      }
    }

    // ----- Look for skip
    if ($v_skip > 0) {
      while ($v_skip > 0) {
        $v_result = '../' . $v_result;
        $v_skip--;
      }
    }
  }

  // ----- Return

  //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
  return $v_result;
}