Checks for snooping outside of the file system root.

@param   string  $path  A file system path to check.

@return  string  A cleaned version of the path or exit on error.

@since   1.0
@throws  \Exception
518030
public static function check($path)
	{
		if (strpos($path, '..') !== false)
		{
			throw new \Exception('JPath::check Use of relative paths not permitted', 20);
		}

		$path = self::clean($path);

		if ((JPATH_ROOT != '') && strpos($path, self::clean(JPATH_ROOT)) !== 0)
		{
			throw new \Exception('JPath::check Snooping out of bounds @ ' . $path, 20);
		}

		return $path;
	}
324703
private function getCleanPath($path)
    {
        // remove the first slash for safety reasons [delimitor mapping] based on the web-server Rewrite
        if (mb_strpos($path, $this->delimiter) === 0) {
            $path = mb_substr($path, 1);
        }

        // if the last character is a delimiter remove it as well
        if (mb_strpos($path, $this->delimiter) == mb_strlen($path) - 1) {
            $path = mb_substr($path, 0, -1);
        }

        //remove the get parameter so it's clean
        if (($ppos = mb_strpos($path, '?')) && $ppos > 0) {
            $path = mb_substr($path, 0, $ppos);
        }

        unset($ppos);


        return $path;
    }
86652
public function checkPath($path = null)
    {
        if (is_null($path)) {
            $path = $this->getDir();
        }

        $real_path = realpath($path);
        if (!$real_path) {
            $real_path = realpath(dirname($path));
        }
        $upload_path = realpath($this->getUploadPath());

        if (strcasecmp($real_path, $upload_path) > 0) {
            return true;
        } else {
            throw new \Exception("Directory is not in the upload path", 403);
        }
    }
900081
private function locateUsingRootPathsSearch(string $path): ?string
    {
        foreach ($this->roots as $root) {
            if (null !== $absolute = $this->generateAbsolutePath($root, $path)) {
                return $absolute;
            }
        }

        return null;
    }
328116
public static function cleanPathname($path) {
		if (UString::isNotNull ( $path )) {
			if (\DS === "/")
				$path = \str_replace ( "\\", \DS, $path );
			else
				$path = \str_replace ( "/", \DS, $path );
			$path = \str_replace ( \DS . \DS, \DS, $path );
			if (! UString::endswith ( $path, \DS )) {
				$path = $path . \DS;
			}
		}
		return $path;
	}
197501
protected function validatePath($path)
    {
        if (empty($path)) {
            return false;
        }

        // we are always operating from root directory
        // so forbid relative paths
        if (false !== strpos($path, '..')) {
            return false;
        }

        return true;
    }
1271773
public function getCreateFilePermissions($path = null)
    {
        try {
            if (!isset($path)) {
                $path = DIR_FILES_UPLOADED_STANDARD;
            }

            if (!is_dir($path)) {
                $path = @dirname($path);
            }
            $perms = @fileperms($path);

            if (!$perms) {
                throw new Exception(t('An error occurred while attempting to determine file permissions.'));
            }
            clearstatcache();
            $dir_perms = substr(decoct($perms), 1);

            $file_perms = "0";
            $parts[] = substr($dir_perms, 1, 1);
            $parts[] = substr($dir_perms, 2, 1);
            $parts[] = substr($dir_perms, 3, 1);
            foreach ($parts as $p) {
                if (intval($p) % 2 == 0) {
                    $file_perms .= $p;
                    continue;
                }
                $file_perms .= intval($p) - 1;
            }
        } catch (Exception $e) {
            return false;
        }
        $res = new \stdClass();
        $res->file = intval($file_perms, 8);
        $res->dir = intval($dir_perms, 8);

        return $res;
    }
227018
public function read( $path )
	{
		try {
			return $this->fs->get( $path );
		} catch( \Exception $e ) {
			throw new Exception( $e->getMessage(), 0, $e );
		}
	}
1267048
public static function cleanPath($path, $params = ['checkDir' => false, 'checkFile' => false])
    {

        $params = Arr::mergeWithDefaultParams($params);

        if ($params['checkDir']) {
            if (!is_dir($path)) {
                Throw new Exception('Path is not a dir');
            }
        }

        if ($params['checkFile']) {
            if (!is_file($path)) {
                Throw new Exception('Path is not a file');
            }
        }

        return str_replace(array('//'), array('/'), $path);
    }
1184481
public function path($path)
    {
        if ($path !== '/' . trim($path, '/') . '/') {
            throw new Exception('Invalid path "' . $path . '", use like this /foo/', 2);
        }

        $this->path = $path;

        return $this;
    }
327152
private static function checkSavePath($path) {
            if (is_string($path)) {
                $dir = dirname($path);

                return file_exists($dir) && is_writeable($dir);
            } elseif (is_resource($path)) {
                return true;
            }

            return false;
        }
736910
protected function sanitizePath($path)
    {
        Assert::stringNotEmpty($path, 'The path must be a non-empty string. Got: %s');
        Assert::startsWith($path, '/', 'The path %s is not absolute.');

        return Path::canonicalize($path);
    }
556400
public static function cleanFilePathname($path) {
		if (UString::isNotNull ( $path )) {
			if (\DS === "/")
				$path = \str_replace ( "\\", \DS, $path );
			else
				$path = \str_replace ( "/", \DS, $path );
			$path = \str_replace ( \DS . \DS, \DS, $path );
		}
		return $path;
	}
1237499
private function isCached(string $path)
	{
		$views = glob($this->cacheDirectory . $this->DS . '*'. str_replace($this->DS, '___', $path));

		if (count($views) > 0) {
			$this->canRecache($path, $views[0]);
			return file_get_contents($views[0]) == null ? ' ' : file_get_contents($views[0]);
		}
		else {
			$this->cache($path, uniqid());
		}

		$views = glob($this->cacheDirectory . $this->DS . '*'. str_replace($this->DS, '___', $path));
		if (count($views) > 0) {
			return file_get_contents($views[0]) == null ? ' ' : file_get_contents($views[0]);
		}
	}
271172
public static function cleanPath($path)
    {
        $regex = '/(\/)\/+/';
        $path = str_replace(['\\', '/ /'], '/', $path);
        $path = preg_replace($regex,'$1',$path);

        return $path;
    }
1348218
protected static function validatePath($path)
    {
        if (realpath($path) === false) {
            throw new Exception(
                'Path "' . $path . '" does not exist.'
            );
        }

        if (is_dir($path) === false || is_writable($path) === false) {
            throw new Exception(
                'Make sure path "' . $path . '" exists and that it\'s writable.'
            );
        }

        // Make full usable with trailing slash
        $path = realpath($path) . DIRECTORY_SEPARATOR;

        return $path;
    }
1071699
private function getPath($path)
    {
        return $this->root .
            ltrim(
                preg_replace(
                    sprintf('|%s{2,}|', preg_quote(DIRECTORY_SEPARATOR)),
                    DIRECTORY_SEPARATOR,
                    $path
                ),
                DIRECTORY_SEPARATOR
            );
    }