Skip to content

Commit

Permalink
Refactor|Homepage: Moved nativePath() and absolutePath() to utilities…
Browse files Browse the repository at this point in the history
….inc.php

These do not belong in FrontController.
  • Loading branch information
danij-deng committed Jul 1, 2013
1 parent 34212e4 commit c1f7b65
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 49 deletions.
12 changes: 6 additions & 6 deletions web/classes/contentcache.class.php
Expand Up @@ -45,7 +45,7 @@ public function store($file, $content)
!isset($content) || $content == '')
return false;

$file = FrontController::absolutePath($this->_docRoot.'/'.$file);
$file = absolutePath($this->_docRoot.'/'.$file);

/*try
{*/
Expand Down Expand Up @@ -88,7 +88,7 @@ public function store($file, $content)
*/
public function has($relPath)
{
return (bool) file_exists(FrontController::nativePath($this->_docRoot."/$relPath"));
return (bool) file_exists(nativePath($this->_docRoot."/$relPath"));
}

/**
Expand All @@ -101,7 +101,7 @@ public function has($relPath)
*/
public function retrieve($relPath)
{
$path = FrontController::nativePath($this->_docRoot."/$relPath");
$path = nativePath($this->_docRoot."/$relPath");

if(!$path || !file_exists($path))
throw new Exception(sprintf('file %s not present in content cache.', $relPath));
Expand All @@ -126,7 +126,7 @@ public function info($relPath, &$info)
{
if(!$info instanceof ContentInfo) return FALSE;

$path = FrontController::nativePath($this->_docRoot."/$relPath");
$path = nativePath($this->_docRoot."/$relPath");
if(!$path || !file_exists($path)) return FALSE;

$info->modifiedTime = filemtime($path);
Expand All @@ -141,7 +141,7 @@ public function info($relPath, &$info)
*/
public function touch($relPath)
{
$path = FrontController::nativePath($this->_docRoot."/$relPath");
$path = nativePath($this->_docRoot."/$relPath");
if(!$path || !file_exists($path)) return FALSE;

touch($path);
Expand All @@ -157,7 +157,7 @@ public function touch($relPath)
*/
public function import($relPath)
{
$path = FrontController::nativePath($this->_docRoot."/$relPath");
$path = nativePath($this->_docRoot."/$relPath");

if(!$path || !file_exists($path))
throw new Exception(sprintf('file %s not present in content cache.', $relPath));
Expand Down
38 changes: 0 additions & 38 deletions web/classes/frontcontroller.class.php
Expand Up @@ -651,44 +651,6 @@ public static function &contentCache()
return self::fc()->getContentCache();
}

public static function absolutePath($path)
{
return $_SERVER['DOCUMENT_ROOT'] . self::nativePath('/'.$path);
}

public static function nativePath($path)
{
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);

$result = array();
$pathA = explode(DIRECTORY_SEPARATOR, $path);
if(!$pathA[0])
$result[] = '';
foreach($pathA AS $key => $dir)
{
if($dir == '..')
{
if(end($result) == '..')
{
$result[] = '..';
}
else if(!array_pop($result))
{
$result[] = '..';
}
}
else if($dir && $dir != '.')
{
$result[] = $dir;
}
}

if(!end($pathA))
$result[] = '';

return implode(DIRECTORY_SEPARATOR, $result);
}

public static function ErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
{
$errortype = array (
Expand Down
38 changes: 38 additions & 0 deletions web/includes/utilities.inc.php
Expand Up @@ -27,6 +27,44 @@

includeGuard('Utils');

function nativePath($path)
{
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);

$result = array();
$pathA = explode(DIRECTORY_SEPARATOR, $path);
if(!$pathA[0])
$result[] = '';
foreach($pathA AS $key => $dir)
{
if($dir == '..')
{
if(end($result) == '..')
{
$result[] = '..';
}
else if(!array_pop($result))
{
$result[] = '..';
}
}
else if($dir && $dir != '.')
{
$result[] = $dir;
}
}

if(!end($pathA))
$result[] = '';

return implode(DIRECTORY_SEPARATOR, $result);
}

function absolutePath($path)
{
return $_SERVER['DOCUMENT_ROOT'] . nativePath('/'.$path);
}

function checkImagePath($path, $formats)
{
if(!isset($path) || !isset($formats) || !is_array($formats))
Expand Down
2 changes: 1 addition & 1 deletion web/plugins/addonrepository/addonrepository.php
Expand Up @@ -201,7 +201,7 @@ public function execute($args=NULL)
$fc = &FrontController::fc();

// Build the add-ons collection.
$addonListXml = file_get_contents(FrontController::nativePath("plugins/addonrepository/addons.xml"));
$addonListXml = file_get_contents(nativePath("plugins/addonrepository/addons.xml"));

$this->addons = array();
AddonsParser::parse($addonListXml, $this->addons);
Expand Down
6 changes: 2 additions & 4 deletions web/plugins/disabled.screens/screens.php
Expand Up @@ -48,8 +48,6 @@ public function title()
*/
public function InterpretRequest($request)
{
global $FrontController;

$url = urldecode($request->url()->path());

// @kludge skip over the first '/' in the home URL.
Expand All @@ -64,7 +62,7 @@ public function InterpretRequest($request)

if(count($tokens) && !strcasecmp(self::$baseRequestName, $tokens[0]))
{
$FrontController->enqueueAction($this, NULL);
FrontController::fc()->enqueueAction($this, NULL);
return true; // Eat the request.
}

Expand Down Expand Up @@ -170,6 +168,6 @@ public function execute($args=NULL)

?></div><?php

$FrontController->endPage();
$fc->endPage();
}
}

0 comments on commit c1f7b65

Please sign in to comment.