removed constants and added filesystem manager

This commit is contained in:
flash 2016-10-07 19:37:00 +02:00
parent 32132eeb77
commit 374b4e4209
12 changed files with 63 additions and 23 deletions

View file

@ -21,7 +21,7 @@ class Application extends \CLIFramework\Application
/** /**
* CLI Application version. * CLI Application version.
*/ */
const VERSION = SAKURA_VERSION; const VERSION = 9001;
/** /**
* Enable command autoloading. * Enable command autoloading.

View file

@ -45,7 +45,7 @@ class DatabaseMigrateCommand extends Command
return; return;
} }
$migrator->run(ROOT . self::MIGRATIONS); $migrator->run(path(self::MIGRATIONS));
foreach ($migrator->getNotes() as $note) { foreach ($migrator->getNotes() as $note) {
$this->getLogger()->writeln(strip_tags($note)); $this->getLogger()->writeln(strip_tags($note));

View file

@ -55,7 +55,7 @@ class DatabaseStatusCommand extends Command
'Migration', '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]); $migrations->addRow([in_array($migration, $ran) ? 'Y' : 'N', $migration]);
} }

View file

@ -29,8 +29,8 @@ class ServeCommand extends Command
*/ */
public function execute() public function execute()
{ {
$document_root = addslashes(ROOT . 'public/'); $document_root = addslashes(path('public'));
$router_proxy = addslashes(ROOT . 'server.php'); $router_proxy = addslashes(path('server.php'));
$php_dir = PHP_BINDIR; $php_dir = PHP_BINDIR;
$host = config('dev.host'); $host = config('dev.host');

View file

@ -188,11 +188,11 @@ class FileController extends Controller
} }
} }
$noFile = ROOT . 'public/' . str_replace( $noFile = path('public/' . str_replace(
'%tplname%', '%tplname%',
Template::$name, Template::$name,
config("user.{$method}_none") config("user.{$method}_none")
); ));
$none = [ $none = [
'name' => basename($noFile), 'name' => basename($noFile),
'data' => file_get_contents($noFile), 'data' => file_get_contents($noFile),

View file

@ -81,7 +81,7 @@ class File
]); ]);
// Save the file data // 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 a new File object
return new File($id); return new File($id);
@ -102,7 +102,7 @@ class File
if ($fileRow) { if ($fileRow) {
$this->id = $fileRow->file_id; $this->id = $fileRow->file_id;
$this->user = User::construct($fileRow->user_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->name = $fileRow->file_name;
$this->mime = $fileRow->file_mime; $this->mime = $fileRow->file_mime;
$this->time = $fileRow->file_time; $this->time = $fileRow->file_time;
@ -115,7 +115,7 @@ class File
*/ */
public function delete() 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)) { if (file_exists($filename)) {
unlink($filename); unlink($filename);

37
app/FileSystem.php Normal file
View file

@ -0,0 +1,37 @@
<?php
/**
* Holds file system interaction stuff.
* @package Sakura
*/
namespace Sakura;
/**
* Used for handling file system interactions.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
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);
}
}

View file

@ -219,7 +219,7 @@ class Net
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 2, CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_TIMEOUT => 4, CURLOPT_TIMEOUT => 4,
CURLOPT_USERAGENT => 'Sakura/' . SAKURA_VERSION, CURLOPT_USERAGENT => 'Sakura/1.0 (+https://sakura.flash.moe)',
]); ]);
switch (strtolower($method)) { switch (strtolower($method)) {

View file

@ -25,7 +25,7 @@ class Template
const FILE_EXT = '.twig'; const FILE_EXT = '.twig';
/** /**
* The path relative to ROOT. * The path relative to the root.
*/ */
const VIEWS_DIR = 'resources/views/'; const VIEWS_DIR = 'resources/views/';
@ -84,7 +84,7 @@ class Template
*/ */
public static function init() public static function init()
{ {
$views_dir = ROOT . self::VIEWS_DIR; $views_dir = path(self::VIEWS_DIR);
// Initialise Twig Filesystem Loader // Initialise Twig Filesystem Loader
$loader = new Twig_Loader_Filesystem(); $loader = new Twig_Loader_Filesystem();
@ -102,7 +102,7 @@ class Template
// Environment variable // Environment variable
$env = [ $env = [
'cache' => config("performance.template_cache") 'cache' => config("performance.template_cache")
? realpath(ROOT . config("performance.cache_dir") . 'views') ? path(config("performance.cache_dir") . 'views')
: false, : false,
'auto_reload' => true, 'auto_reload' => true,
'debug' => config("dev.twig_debug"), 'debug' => config("dev.twig_debug"),
@ -159,6 +159,6 @@ class Template
*/ */
public static function exists($name) 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 . "/"));
} }
} }

View file

@ -14,7 +14,7 @@ ob_start(config('performance.compression') ? 'ob_gzhandler' : null);
// Initialise the router and include the routes file // Initialise the router and include the routes file
Routerv1::init(); Routerv1::init();
include_once ROOT . 'routes.php'; include_once path('routes.php');
// Initialise the current session // Initialise the current session
$cookiePrefix = config('cookie.prefix'); $cookiePrefix = config('cookie.prefix');

View file

@ -6,10 +6,6 @@
namespace Sakura; namespace Sakura;
// Define version and root path
define('SAKURA_VERSION', 20160913);
define('ROOT', __DIR__ . '/');
// Turn error reporting on regardless of anything // Turn error reporting on regardless of anything
error_reporting(-1); error_reporting(-1);
@ -26,19 +22,19 @@ if (version_compare(phpversion(), '7.0.0', '<')) {
} }
// Check if the composer autoloader exists // 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?'); die('Autoloader not found, did you run composer install?');
} }
// Include the autoloader // Include the autoloader
require_once ROOT . 'vendor/autoload.php'; require_once 'vendor/autoload.php';
// Register the handlers // Register the handlers
set_exception_handler([ExceptionHandler::class, 'exception']); set_exception_handler([ExceptionHandler::class, 'exception']);
set_error_handler([ExceptionHandler::class, 'error']); set_error_handler([ExceptionHandler::class, 'error']);
// Load the configuration // Load the configuration
Config::init(ROOT . 'config/config.ini'); Config::init(path('config/config.ini'));
// Start the database module // Start the database module
$capsule = new DB; $capsule = new DB;

View file

@ -5,6 +5,7 @@
use Sakura\Config; use Sakura\Config;
use Sakura\Exceptions\ConfigValueNotFoundException; use Sakura\Exceptions\ConfigValueNotFoundException;
use Sakura\FileSystem;
use Sakura\Net; use Sakura\Net;
use Sakura\Routerv1; use Sakura\Routerv1;
use Sakura\Template; use Sakura\Template;
@ -52,6 +53,12 @@ function view($name, $vars = [])
return Template::render($name); return Template::render($name);
} }
// Get a path
function path($path)
{
return FileSystem::getPath($path);
}
function clean_string($string, $lower = false, $noSpecial = false, $replaceSpecial = '') function clean_string($string, $lower = false, $noSpecial = false, $replaceSpecial = '')
{ {
// Run common sanitisation function over string // Run common sanitisation function over string