removed constants and added filesystem manager

This commit is contained in:
Pachira 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.
*/
const VERSION = SAKURA_VERSION;
const VERSION = 9001;
/**
* Enable command autoloading.

View file

@ -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));

View file

@ -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]);
}

View file

@ -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');

View file

@ -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),

View file

@ -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);

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_CONNECTTIMEOUT => 2,
CURLOPT_TIMEOUT => 4,
CURLOPT_USERAGENT => 'Sakura/' . SAKURA_VERSION,
CURLOPT_USERAGENT => 'Sakura/1.0 (+https://sakura.flash.moe)',
]);
switch (strtolower($method)) {

View file

@ -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 . "/"));
}
}

View file

@ -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');

View file

@ -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;

View file

@ -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