From 374b4e4209e7942fdd17e97099092677d003cdd8 Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 7 Oct 2016 19:37:00 +0200 Subject: [PATCH] removed constants and added filesystem manager --- app/Console/Application.php | 2 +- .../Command/DatabaseMigrateCommand.php | 2 +- app/Console/Command/DatabaseStatusCommand.php | 2 +- app/Console/Command/ServeCommand.php | 4 +- app/Controllers/FileController.php | 4 +- app/File.php | 6 +-- app/FileSystem.php | 37 +++++++++++++++++++ app/Net.php | 2 +- app/Template.php | 8 ++-- public/index.php | 2 +- sakura.php | 10 ++--- utility.php | 7 ++++ 12 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 app/FileSystem.php diff --git a/app/Console/Application.php b/app/Console/Application.php index 130ae03..3906d6b 100644 --- a/app/Console/Application.php +++ b/app/Console/Application.php @@ -21,7 +21,7 @@ class Application extends \CLIFramework\Application /** * CLI Application version. */ - const VERSION = SAKURA_VERSION; + const VERSION = 9001; /** * Enable command autoloading. diff --git a/app/Console/Command/DatabaseMigrateCommand.php b/app/Console/Command/DatabaseMigrateCommand.php index ecf2965..bce6250 100644 --- a/app/Console/Command/DatabaseMigrateCommand.php +++ b/app/Console/Command/DatabaseMigrateCommand.php @@ -45,7 +45,7 @@ class DatabaseMigrateCommand extends Command return; } - $migrator->run(ROOT . self::MIGRATIONS); + $migrator->run(path(self::MIGRATIONS)); foreach ($migrator->getNotes() as $note) { $this->getLogger()->writeln(strip_tags($note)); diff --git a/app/Console/Command/DatabaseStatusCommand.php b/app/Console/Command/DatabaseStatusCommand.php index 73d524f..fb25443 100644 --- a/app/Console/Command/DatabaseStatusCommand.php +++ b/app/Console/Command/DatabaseStatusCommand.php @@ -55,7 +55,7 @@ class DatabaseStatusCommand extends Command 'Migration', ]); - foreach ($migrator->getMigrationFiles(ROOT . self::MIGRATIONS) as $migration) { + foreach ($migrator->getMigrationFiles(path(self::MIGRATIONS)) as $migration) { $migrations->addRow([in_array($migration, $ran) ? 'Y' : 'N', $migration]); } diff --git a/app/Console/Command/ServeCommand.php b/app/Console/Command/ServeCommand.php index c989a27..4322e1f 100644 --- a/app/Console/Command/ServeCommand.php +++ b/app/Console/Command/ServeCommand.php @@ -29,8 +29,8 @@ class ServeCommand extends Command */ public function execute() { - $document_root = addslashes(ROOT . 'public/'); - $router_proxy = addslashes(ROOT . 'server.php'); + $document_root = addslashes(path('public')); + $router_proxy = addslashes(path('server.php')); $php_dir = PHP_BINDIR; $host = config('dev.host'); diff --git a/app/Controllers/FileController.php b/app/Controllers/FileController.php index 7ecf4e9..1ff1940 100644 --- a/app/Controllers/FileController.php +++ b/app/Controllers/FileController.php @@ -188,11 +188,11 @@ class FileController extends Controller } } - $noFile = ROOT . 'public/' . str_replace( + $noFile = path('public/' . str_replace( '%tplname%', Template::$name, config("user.{$method}_none") - ); + )); $none = [ 'name' => basename($noFile), 'data' => file_get_contents($noFile), diff --git a/app/File.php b/app/File.php index c098307..a2a3e35 100644 --- a/app/File.php +++ b/app/File.php @@ -81,7 +81,7 @@ class File ]); // Save the file data - file_put_contents(ROOT . config('file.uploads_dir') . $id . ".bin", $data); + file_put_contents(path(config('file.uploads_dir') . $id . ".bin"), $data); // Return a new File object return new File($id); @@ -102,7 +102,7 @@ class File if ($fileRow) { $this->id = $fileRow->file_id; $this->user = User::construct($fileRow->user_id); - $this->data = file_get_contents(ROOT . config('file.uploads_dir') . $fileRow->file_id . ".bin"); + $this->data = file_get_contents(path(config('file.uploads_dir') . $fileRow->file_id . ".bin")); $this->name = $fileRow->file_name; $this->mime = $fileRow->file_mime; $this->time = $fileRow->file_time; @@ -115,7 +115,7 @@ class File */ public function delete() { - $filename = ROOT . config('file.uploads_dir') . $this->id . ".bin"; + $filename = path(config('file.uploads_dir') . $this->id . ".bin"); if (file_exists($filename)) { unlink($filename); diff --git a/app/FileSystem.php b/app/FileSystem.php new file mode 100644 index 0000000..94a88aa --- /dev/null +++ b/app/FileSystem.php @@ -0,0 +1,37 @@ + + */ +class FileSystem +{ + private static $rootPath = null; + + public static function getRootPath() + { + if (self::$rootPath === null) { + // assuming we're running from the 'app' subdirectory + self::$rootPath = realpath(__DIR__ . '/..'); + } + + return self::$rootPath; + } + + public static function getPath($path) + { + return self::getRootPath() . DIRECTORY_SEPARATOR . self::fixSlashes($path); + } + + private static function fixSlashes($path) + { + return str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path); + } +} diff --git a/app/Net.php b/app/Net.php index e857a69..89467fc 100644 --- a/app/Net.php +++ b/app/Net.php @@ -219,7 +219,7 @@ class Net CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 2, CURLOPT_TIMEOUT => 4, - CURLOPT_USERAGENT => 'Sakura/' . SAKURA_VERSION, + CURLOPT_USERAGENT => 'Sakura/1.0 (+https://sakura.flash.moe)', ]); switch (strtolower($method)) { diff --git a/app/Template.php b/app/Template.php index 2dfabb5..ef74dc5 100644 --- a/app/Template.php +++ b/app/Template.php @@ -25,7 +25,7 @@ class Template const FILE_EXT = '.twig'; /** - * The path relative to ROOT. + * The path relative to the root. */ const VIEWS_DIR = 'resources/views/'; @@ -84,7 +84,7 @@ class Template */ public static function init() { - $views_dir = ROOT . self::VIEWS_DIR; + $views_dir = path(self::VIEWS_DIR); // Initialise Twig Filesystem Loader $loader = new Twig_Loader_Filesystem(); @@ -102,7 +102,7 @@ class Template // Environment variable $env = [ 'cache' => config("performance.template_cache") - ? realpath(ROOT . config("performance.cache_dir") . 'views') + ? path(config("performance.cache_dir") . 'views') : false, 'auto_reload' => true, 'debug' => config("dev.twig_debug"), @@ -159,6 +159,6 @@ class Template */ public static function exists($name) { - return ctype_alnum($name) && file_exists(ROOT . self::VIEWS_DIR . $name . "/"); + return ctype_alnum($name) && file_exists(path(self::VIEWS_DIR . $name . "/")); } } diff --git a/public/index.php b/public/index.php index cd355e1..51b2dd9 100644 --- a/public/index.php +++ b/public/index.php @@ -14,7 +14,7 @@ ob_start(config('performance.compression') ? 'ob_gzhandler' : null); // Initialise the router and include the routes file Routerv1::init(); -include_once ROOT . 'routes.php'; +include_once path('routes.php'); // Initialise the current session $cookiePrefix = config('cookie.prefix'); diff --git a/sakura.php b/sakura.php index 404c548..25e9a93 100644 --- a/sakura.php +++ b/sakura.php @@ -6,10 +6,6 @@ namespace Sakura; -// Define version and root path -define('SAKURA_VERSION', 20160913); -define('ROOT', __DIR__ . '/'); - // Turn error reporting on regardless of anything error_reporting(-1); @@ -26,19 +22,19 @@ if (version_compare(phpversion(), '7.0.0', '<')) { } // Check if the composer autoloader exists -if (!file_exists(ROOT . 'vendor/autoload.php')) { +if (!file_exists('vendor/autoload.php')) { die('Autoloader not found, did you run composer install?'); } // Include the autoloader -require_once ROOT . 'vendor/autoload.php'; +require_once 'vendor/autoload.php'; // Register the handlers set_exception_handler([ExceptionHandler::class, 'exception']); set_error_handler([ExceptionHandler::class, 'error']); // Load the configuration -Config::init(ROOT . 'config/config.ini'); +Config::init(path('config/config.ini')); // Start the database module $capsule = new DB; diff --git a/utility.php b/utility.php index c225e4d..d58dc21 100644 --- a/utility.php +++ b/utility.php @@ -5,6 +5,7 @@ use Sakura\Config; use Sakura\Exceptions\ConfigValueNotFoundException; +use Sakura\FileSystem; use Sakura\Net; use Sakura\Routerv1; use Sakura\Template; @@ -52,6 +53,12 @@ function view($name, $vars = []) return Template::render($name); } +// Get a path +function path($path) +{ + return FileSystem::getPath($path); +} + function clean_string($string, $lower = false, $noSpecial = false, $replaceSpecial = '') { // Run common sanitisation function over string