diff --git a/app/CurrentSession.php b/app/CurrentSession.php new file mode 100644 index 0000000..696d9f7 --- /dev/null +++ b/app/CurrentSession.php @@ -0,0 +1,83 @@ + + */ +class CurrentSession +{ + /** + * The user object of the currently active user. + * @var User + */ + public static $user = null; + + /** + * The currently active session object. + * @var Session + */ + public static $session = null; + + /** + * Prepare the current session backend. + * @param int $user + * @param string $session + * @param string $ip + */ + public static function start($user, $session, $ip) + { + // Check if a PHP session was already started and if not start one + if (session_status() !== PHP_SESSION_ACTIVE) { + session_start(); + } + + // Create a session object + self::$session = new Session($session); + + // Create a user object + $user = User::construct($user); + + // Check if the session exists and check if the user is activated + if (self::$session->validate($user->id, $ip) + && !$user->permission(Site::DEACTIVATED)) { + // Assign the user object + self::$user = $user; + } else { + self::$user = User::construct(0); + } + } + + /** + * Stop the current session + */ + public static function stop() + { + self::$session->delete(); + session_regenerate_id(true); + session_destroy(); + } + + /** + * Create a new sakura session. + * @param int $user + * @param string $ip + * @param string $country + * @param string $agent + * @param bool $remember + * @param int $length + * @return Session + */ + public static function create($user, $ip, $country, $agent = null, $remember = false, $length = 604800) + { + return Session::create($user, $ip, $country, $agent, $remember, $length); + } +} diff --git a/app/Template.php b/app/Template.php index b3f0ba4..2dfabb5 100644 --- a/app/Template.php +++ b/app/Template.php @@ -20,16 +20,14 @@ use Twig_SimpleFunction; class Template { /** - * The variables passed on to the templating engine. - * @var array + * The file extension used by template files. */ - private static $vars = []; + const FILE_EXT = '.twig'; /** - * The templating engine. - * @var Twig_Environment + * The path relative to ROOT. */ - private static $engine; + const VIEWS_DIR = 'resources/views/'; /** * The template name. @@ -38,9 +36,16 @@ class Template public static $name; /** - * The file extension used by template files. + * The templating engine. + * @var Twig_Environment */ - const FILE_EXT = '.twig'; + private static $engine; + + /** + * The variables passed on to the templating engine. + * @var array + */ + private static $vars = []; /** * List of utility functions to add to templating. @@ -79,7 +84,7 @@ class Template */ public static function init() { - $views_dir = ROOT . 'resources/views/'; + $views_dir = ROOT . self::VIEWS_DIR; // Initialise Twig Filesystem Loader $loader = new Twig_Loader_Filesystem(); @@ -141,10 +146,19 @@ class Template /** * Render a template file. * @param string $file - * @return bool|string + * @return string */ public static function render($file) { return self::$engine->render($file . self::FILE_EXT, self::$vars); } + + /** + * Checks if a template directory exists. + * @return bool + */ + public static function exists($name) + { + return ctype_alnum($name) && file_exists(ROOT . self::VIEWS_DIR . $name . "/"); + } } diff --git a/app/User.php b/app/User.php index 92c0600..a0847ea 100644 --- a/app/User.php +++ b/app/User.php @@ -204,6 +204,12 @@ class User */ public $lastfm = ''; + /** + * The user's selected design. + * @var string + */ + private $design = ''; + /** * The user's birthday. * @var string @@ -325,6 +331,7 @@ class User $this->steam = $userRow->user_steam; $this->osu = $userRow->user_osu; $this->lastfm = $userRow->user_lastfm; + $this->design = $userRow->user_design; // Temporary backwards compatible IP storage system try { @@ -1060,4 +1067,13 @@ class User return $sessions; } + + /** + * Gets the user's selected design. + * @return string + */ + public function design() + { + return Template::exists($this->design) ? $this->design : config('general.design'); + } } diff --git a/database/2013_01_27_221444_base_tables.php b/database/2013_01_27_221444_base_tables.php index 85b7710..1a7a526 100644 --- a/database/2013_01_27_221444_base_tables.php +++ b/database/2013_01_27_221444_base_tables.php @@ -7,7 +7,6 @@ class BaseTables extends Migration { /** * Run the migrations. - * * @return void */ public function up() @@ -550,7 +549,6 @@ class BaseTables extends Migration /** * Reverse the migrations. - * * @return void */ public function down() diff --git a/database/2016_08_02_204141_move_options_and_profile_into_users.php b/database/2016_08_02_204141_move_options_and_profile_into_users.php index d74a1de..63775d8 100644 --- a/database/2016_08_02_204141_move_options_and_profile_into_users.php +++ b/database/2016_08_02_204141_move_options_and_profile_into_users.php @@ -7,7 +7,6 @@ class MoveOptionsAndProfileIntoUsers extends Migration { /** * Run the migrations. - * * @return void */ public function up() @@ -63,7 +62,6 @@ class MoveOptionsAndProfileIntoUsers extends Migration /** * Reverse the migrations. - * * @return void */ public function down() diff --git a/database/2016_08_04_175711_files_belong_on_the_filesystem.php b/database/2016_08_04_175711_files_belong_on_the_filesystem.php index f552a3d..f991c0f 100644 --- a/database/2016_08_04_175711_files_belong_on_the_filesystem.php +++ b/database/2016_08_04_175711_files_belong_on_the_filesystem.php @@ -7,7 +7,6 @@ class FilesBelongOnTheFilesystem extends Migration { /** * Run the migrations. - * * @return void */ public function up() @@ -21,7 +20,6 @@ class FilesBelongOnTheFilesystem extends Migration /** * Reverse the migrations. - * * @return void */ public function down() diff --git a/database/2016_08_07_150327_record_login_country.php b/database/2016_08_07_150327_record_login_country.php new file mode 100644 index 0000000..7468c7f --- /dev/null +++ b/database/2016_08_07_150327_record_login_country.php @@ -0,0 +1,34 @@ +table('sessions', function (Blueprint $table) { + $table->char('session_country', 2) + ->default('XX'); + }); + } + + /** + * Reverse the migrations. + * @return void + */ + public function down() + { + $schema = DB::getSchemaBuilder(); + + $schema->table('sessions', function (Blueprint $table) { + $table->dropColumn('session_country'); + }); + } +} diff --git a/database/2016_08_07_161213_allow_design_change_per_user.php b/database/2016_08_07_161213_allow_design_change_per_user.php new file mode 100644 index 0000000..144001c --- /dev/null +++ b/database/2016_08_07_161213_allow_design_change_per_user.php @@ -0,0 +1,35 @@ +table('users', function (Blueprint $table) { + $table->string('user_design') + ->nullable() + ->default(null); + }); + } + + /** + * Reverse the migrations. + * @return void + */ + public function down() + { + $schema = DB::getSchemaBuilder(); + + $schema->table('users', function (Blueprint $table) { + $table->dropColumn('user_design'); + }); + } +} diff --git a/sakura.php b/sakura.php index 8324835..1e2ed67 100644 --- a/sakura.php +++ b/sakura.php @@ -67,7 +67,7 @@ if (!defined('IN_CLI')) { ); // Start templating engine and set base variables - Template::set(config('general.design')); + Template::set(CurrentSession::$user->design()); Template::vars([ 'get' => $_GET, 'user' => CurrentSession::$user,