Absolute paths used where necessary

- No random chdir() calls
- No more exceptions when executing scripts from dirs other than root
- find-posts.php prints absolute paths making piping more useful
This commit is contained in:
Marcin Kurczewski 2013-11-23 20:52:41 +01:00
parent 75775cdc15
commit 0ef5f1b46d
13 changed files with 44 additions and 44 deletions

View file

@ -1,8 +1,8 @@
<?php
require_once 'src/core.php';
$config = \Chibi\Registry::getConfig();
$fontsPath = $config->main->mediaPath . DS . 'fonts' . DS;
$libPath = $config->main->mediaPath . DS . 'lib' . DS;
$fontsPath = TextHelper::absolutePath($config->main->mediaPath . DS . 'fonts');
$libPath = TextHelper::absolutePath($config->main->mediaPath . DS . 'lib');
@ -29,10 +29,10 @@ function download($source, $destination = null)
//jQuery
download('http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', $libPath . 'jquery' . DS . 'jquery.min.js');
download('http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', $libPath . DS . 'jquery' . DS . 'jquery.min.js');
//jQuery UI
download('http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', $libPath . 'jquery-ui' . DS . 'jquery-ui.min.js');
download('http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', $libPath . DS . 'jquery-ui' . DS . 'jquery-ui.min.js');
$manifest = download('http://ajax.googleapis.com/ajax/libs/jqueryui/1/MANIFEST');
$lines = explode("\n", str_replace("\r", '', $manifest));
foreach ($lines as $line)
@ -40,21 +40,21 @@ foreach ($lines as $line)
if (preg_match('/themes\/flick\/(.*?) /', $line, $matches))
{
$srcUrl = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1/' . $matches[0];
$dstUrl = $libPath . 'jquery-ui' . DS . $matches[1];
$dstUrl = $libPath . DS . 'jquery-ui' . DS . $matches[1];
download($srcUrl, $dstUrl);
}
}
//jQuery Tag-it!
download('http://raw.github.com/aehlke/tag-it/master/css/jquery.tagit.css', $libPath . 'tagit' . DS . 'jquery.tagit.css');
download('http://raw.github.com/aehlke/tag-it/master/js/tag-it.min.js', $libPath . 'tagit' . DS . 'jquery.tagit.js');
download('http://raw.github.com/aehlke/tag-it/master/css/jquery.tagit.css', $libPath . DS . 'tagit' . DS . 'jquery.tagit.css');
download('http://raw.github.com/aehlke/tag-it/master/js/tag-it.min.js', $libPath . DS . 'tagit' . DS . 'jquery.tagit.js');
//Mousetrap
download('http://raw.github.com/ccampbell/mousetrap/master/mousetrap.min.js', $libPath . 'mousetrap' . DS . 'mousetrap.min.js');
download('http://raw.github.com/ccampbell/mousetrap/master/mousetrap.min.js', $libPath . DS . 'mousetrap' . DS . 'mousetrap.min.js');
//fonts
download('http://googlefontdirectory.googlecode.com/hg/apache/droidsans/DroidSans.ttf', $fontsPath . 'DroidSans.ttf');
download('http://googlefontdirectory.googlecode.com/hg/apache/droidsans/DroidSans-Bold.ttf', $fontsPath . 'DroidSans-Bold.ttf');
download('http://googlefontdirectory.googlecode.com/hg/apache/droidsans/DroidSans.ttf', $fontsPath . DS . 'DroidSans.ttf');
download('http://googlefontdirectory.googlecode.com/hg/apache/droidsans/DroidSans-Bold.ttf', $fontsPath . DS . 'DroidSans-Bold.ttf');

View file

@ -1,6 +1,5 @@
<?php
chdir('..');
require_once 'src/core.php';
require_once '../src/core.php';
$query = $_SERVER['REQUEST_URI'];
\Chibi\Facade::run($query, new Bootstrap());

View file

@ -12,7 +12,6 @@ array_shift($argv);
if (empty($argv))
usage() and die;
$filesPath = rtrim(\Chibi\Registry::getConfig()->main->filesPath, DS);
$query = array_shift($argv);
$posts = Model_Post::getEntities($query, null, null);
foreach ($posts as $post)
@ -21,7 +20,7 @@ foreach ($posts as $post)
[
$post->id,
$post->name,
$filesPath . DS . $post->name,
Model_Post::getFullPath($post->name),
$post->mimeType,
]). PHP_EOL;
}

View file

@ -1,6 +1,5 @@
<?php
require_once __DIR__ . '/../src/core.php';
$config = \Chibi\Registry::getConfig();
function usage()
{
@ -31,24 +30,21 @@ switch ($action)
mkdir($dir, 0755, true);
if (!is_dir($dir))
die($dir . ' is not a dir' . PHP_EOL);
$func = function($name) use ($dir, $config)
$func = function($name) use ($dir)
{
echo $name . PHP_EOL;
static $filesPath = null;
if ($filesPath == null)
$filesPath = $config->main->filesPath;
rename($filesPath . DS . $name, $dir . DS . $name);
$srcPath = Model_Post::getFullPath($name);
$dstPath = $dir . DS . $name;
rename($srcPath, $dstPath);
};
break;
case '-purge':
$func = function($name) use ($dir, $config)
$func = function($name)
{
echo $name . PHP_EOL;
static $filesPath = null;
if ($filesPath == null)
$filesPath = $config->main->filesPath;
unlink($filesPath . DS . $name);
$srcPath = Model_Post::getFullPath($name);
unlink($srcPath);
};
break;
@ -63,8 +59,8 @@ foreach (R::findAll('post') as $post)
}
$names = array_flip($names);
$filesPath = $config->main->filesPath;
foreach (glob($filesPath . DS . '*') as $name)
$config = \Chibi\Registry::getConfig();
foreach (glob(TextHelper::absolutePath($config->main->filesPath) . DS . '*') as $name)
{
$name = basename($name);
if (!isset($names[$name]))

View file

@ -32,7 +32,7 @@ switch ($action)
$func = function($user)
{
printUser($user);
R::trash($user);
Model_User::remove($user);
};
break;

View file

@ -32,7 +32,7 @@ class IndexController
$tab = $tab ?: array_keys($this->config->help->subTitles)[0];
if (!isset($this->config->help->paths[$tab]))
throw new SimpleException('Invalid tab');
$this->context->path = $this->config->help->paths[$tab];
$this->context->path = TextHelper::absolutePath($this->config->help->paths[$tab]);
$this->context->stylesheets []= 'index-help.css';
$this->context->stylesheets []= 'tabs.css';
$this->context->subTitle = 'help';

View file

@ -9,8 +9,7 @@ class LogController
$this->context->subTitle = 'latest logs';
PrivilegesHelper::confirmWithException(Privilege::ListLogs);
$path = $this->context->rootDir . DS . $this->config->main->logsPath;
$path = TextHelper::cleanPath($path);
$path = TextHelper::absolutePath($this->config->main->logsPath);
$logs = [];
foreach (glob($path . DS . '*.log') as $log)
@ -36,8 +35,7 @@ class LogController
PrivilegesHelper::confirmWithException(Privilege::ViewLog);
$name = str_replace(['/', '\\'], '', $name); //paranoia mode
$path = $this->context->rootDir . DS . $this->config->main->logsPath . DS . $name;
$path = TextHelper::cleanPath($path);
$path = TextHelper::absolutePath($this->config->main->logsPath . DS . $name);
if (!file_exists($path))
throw new SimpleException('Specified log doesn\'t exist');

View file

@ -514,7 +514,7 @@ class PostController
PrivilegesHelper::confirmWithException(Privilege::ListPosts, PostSafety::toString($post->safety));
$post->makeThumb($width, $height);
if (!file_exists($path))
$path = $this->config->main->mediaPath . DS . 'img' . DS . 'thumb.jpg';
$path = TextHelper::absolutePath($this->config->main->mediaPath . DS . 'img' . DS . 'thumb.jpg');
}
}
@ -542,7 +542,7 @@ class PostController
PrivilegesHelper::confirmWithException(Privilege::RetrievePost);
PrivilegesHelper::confirmWithException(Privilege::RetrievePost, PostSafety::toString($post->safety));
$path = $this->config->main->filesPath . DS . $post->name;
$path = TextHelper::absolutePath($this->config->main->filesPath . DS . $post->name);
if (!file_exists($path))
throw new SimpleException('Post file does not exist');
if (!is_readable($path))

View file

@ -1,7 +1,6 @@
<?php
class LogHelper
{
static $path;
static $context;
static $config;
static $autoFlush;
@ -9,9 +8,7 @@ class LogHelper
public static function init()
{
self::$path = \Chibi\Registry::getConfig()->main->logsPath . date('Y-m') . '.log';
self::$autoFlush = true;
self::$buffer = [];
}
@ -39,7 +36,8 @@ class LogHelper
public static function getLogPath()
{
return self::$path;
return TextHelper::absolutePath(
\Chibi\Registry::getConfig()->main->logsPath . DS . date('Y-m') . '.log');
}
public static function log($text, array $tokens = [])

View file

@ -224,4 +224,13 @@ class TextHelper
$path = rtrim($path, DS);
return $path;
}
public static function absolutePath($path)
{
if ($path{0} != DS)
$path = \Chibi\Registry::getContext()->rootDir . DS . $path;
$path = self::cleanPath($path);
return $path;
}
}

View file

@ -130,10 +130,10 @@ class Model_Post extends AbstractModel
{
list ($width, $height) = self::validateThumbSize($width, $height);
return TextHelper::replaceTokens($text, [
return TextHelper::absolutePath(TextHelper::replaceTokens($text, [
'fullpath' => self::$config->main->thumbsPath . DS . $name,
'width' => $width,
'height' => $height]);
'height' => $height]));
}
public static function getThumbCustomPath($name, $width = null, $height = null)
@ -148,7 +148,7 @@ class Model_Post extends AbstractModel
public static function getFullPath($name)
{
return self::$config->main->filesPath . DS . $name;
return TextHelper::absolutePath(self::$config->main->filesPath . DS . $name);
}
public function isTaggedWith($tagName)

View file

@ -41,7 +41,7 @@ $context->startTime = $startTime;
$context->rootDir = $rootDir;
//load database
R::setup('sqlite:' . $config->main->dbPath);
R::setup('sqlite:' . TextHelper::absolutePath($config->main->dbPath));
R::freeze(true);
R::dependencies(['tag' => ['post'], 'favoritee' => ['post', 'user'], 'comment' => ['post', 'user']]);

View file

@ -5,7 +5,8 @@ $config = \Chibi\Registry::getConfig();
$dbVersion = Model_Property::get(Model_Property::DbVersion);
printf('DB version = %d' . PHP_EOL, $dbVersion);
$upgrades = glob('src/Upgrades/*.sql');
$upgradesPath = TextHelper::absolutePath(\Chibi\Registry::getContext()->rootDir . DS . 'src' . DS . 'Upgrades');
$upgrades = glob($upgradesPath . DS . '*.sql');
natcasesort($upgrades);
foreach ($upgrades as $upgradePath)