note 90337 deleted from function.realpath by peehaa
| From: | peehaa@php.net | Date: | Sun, 28 Apr 2019 09:48:52 +0000 |
| Subject: | note 90337 deleted from function.realpath by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210712@lists.php.net to get a copy of this message | ||
Note Submitter: orssey
----
<?php
Class RealPath
{
private $ServerPath;
private $ScriptPath;
private $AddSlashEnd;
function __construct()
{
$this->AddSlashEnd = "";
$this->ServerPath = $_SERVER['DOCUMENT_ROOT'];
$this->ScriptPath = substr(dirname($_SERVER['SCRIPT_FILENAME']),
strlen($this->ServerPath));
}
private function Server($adress)
{
$ThisPath = @realpath($adress);
$ThisPathLength = strlen($ThisPath);
return substr($ThisPath, strlen($this->ServerPath), $ThisPathLength);
}
public function Absolute($adress)
{
return $_SERVER['SERVER_NAME'] . $this->Server($adress) . $this->AddSlashEnd;
}
public function RelativeServer($adress)
{
return $this->Server($adress) . $this->AddSlashEnd;
}
public function RelativeScript($adress)
{
$ScriptExplode = explode("/", $this->ScriptPath);
$ServerExplode = explode("/", $this->Server($adress));
$TmpCount = $ScriptCount = count($ScriptExplode);
$ServerCount = count($ServerExplode);
$ReturnPath = "";
for($i = 1; $i < $TmpCount; $i++)
{
if($ScriptExplode[$i] == $ServerExplode[$i])
--$ScriptCount;
else
break;
}
for($i = 1; $i < $ScriptCount; $i++)
$ReturnPath .= "/..";
for($i = $TmpCount - $ScriptCount + 1; $i < $ServerCount; $i++)
$ReturnPath .= "/" . $ServerExplode[$i];
return $ReturnPath . $this->AddSlashEnd;
}
public function AddSlash($value)
{
if(is_bool($value))
{
$this->AddSlashEnd = ($value)? "/" : "";
return true;
}
else
return false;
}
}
example:
$ObjectPath = new RealPath;
$ObjectPath->RelativeScript("../a/b../c/d/f../g../h");