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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -514,7 +514,7 @@ class PostController
PrivilegesHelper::confirmWithException(Privilege::ListPosts, PostSafety::toString($post->safety)); PrivilegesHelper::confirmWithException(Privilege::ListPosts, PostSafety::toString($post->safety));
$post->makeThumb($width, $height); $post->makeThumb($width, $height);
if (!file_exists($path)) 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);
PrivilegesHelper::confirmWithException(Privilege::RetrievePost, PostSafety::toString($post->safety)); 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)) if (!file_exists($path))
throw new SimpleException('Post file does not exist'); throw new SimpleException('Post file does not exist');
if (!is_readable($path)) if (!is_readable($path))

View file

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

View file

@ -224,4 +224,13 @@ class TextHelper
$path = rtrim($path, DS); $path = rtrim($path, DS);
return $path; 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); list ($width, $height) = self::validateThumbSize($width, $height);
return TextHelper::replaceTokens($text, [ return TextHelper::absolutePath(TextHelper::replaceTokens($text, [
'fullpath' => self::$config->main->thumbsPath . DS . $name, 'fullpath' => self::$config->main->thumbsPath . DS . $name,
'width' => $width, 'width' => $width,
'height' => $height]); 'height' => $height]));
} }
public static function getThumbCustomPath($name, $width = null, $height = null) public static function getThumbCustomPath($name, $width = null, $height = null)
@ -148,7 +148,7 @@ class Model_Post extends AbstractModel
public static function getFullPath($name) 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) public function isTaggedWith($tagName)

View file

@ -41,7 +41,7 @@ $context->startTime = $startTime;
$context->rootDir = $rootDir; $context->rootDir = $rootDir;
//load database //load database
R::setup('sqlite:' . $config->main->dbPath); R::setup('sqlite:' . TextHelper::absolutePath($config->main->dbPath));
R::freeze(true); R::freeze(true);
R::dependencies(['tag' => ['post'], 'favoritee' => ['post', 'user'], 'comment' => ['post', 'user']]); 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); $dbVersion = Model_Property::get(Model_Property::DbVersion);
printf('DB version = %d' . PHP_EOL, $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); natcasesort($upgrades);
foreach ($upgrades as $upgradePath) foreach ($upgrades as $upgradePath)