allow users to individually change designs (backend) and some files from the previous commit that didn't get added immediately

This commit is contained in:
flash 2016-08-07 16:23:02 +02:00
parent b666de2151
commit 5a8a80641e
9 changed files with 193 additions and 17 deletions

83
app/CurrentSession.php Normal file
View file

@ -0,0 +1,83 @@
<?php
/**
* Holds information about the currently active session
* @package Sakura
*/
namespace Sakura;
use Sakura\Perms\Site;
/**
* Information about the current active user and session.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
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);
}
}

View file

@ -20,16 +20,14 @@ use Twig_SimpleFunction;
class Template class Template
{ {
/** /**
* The variables passed on to the templating engine. * The file extension used by template files.
* @var array
*/ */
private static $vars = []; const FILE_EXT = '.twig';
/** /**
* The templating engine. * The path relative to ROOT.
* @var Twig_Environment
*/ */
private static $engine; const VIEWS_DIR = 'resources/views/';
/** /**
* The template name. * The template name.
@ -38,9 +36,16 @@ class Template
public static $name; 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. * List of utility functions to add to templating.
@ -79,7 +84,7 @@ class Template
*/ */
public static function init() public static function init()
{ {
$views_dir = ROOT . 'resources/views/'; $views_dir = ROOT . self::VIEWS_DIR;
// Initialise Twig Filesystem Loader // Initialise Twig Filesystem Loader
$loader = new Twig_Loader_Filesystem(); $loader = new Twig_Loader_Filesystem();
@ -141,10 +146,19 @@ class Template
/** /**
* Render a template file. * Render a template file.
* @param string $file * @param string $file
* @return bool|string * @return string
*/ */
public static function render($file) public static function render($file)
{ {
return self::$engine->render($file . self::FILE_EXT, self::$vars); 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 . "/");
}
} }

View file

@ -204,6 +204,12 @@ class User
*/ */
public $lastfm = ''; public $lastfm = '';
/**
* The user's selected design.
* @var string
*/
private $design = '';
/** /**
* The user's birthday. * The user's birthday.
* @var string * @var string
@ -325,6 +331,7 @@ class User
$this->steam = $userRow->user_steam; $this->steam = $userRow->user_steam;
$this->osu = $userRow->user_osu; $this->osu = $userRow->user_osu;
$this->lastfm = $userRow->user_lastfm; $this->lastfm = $userRow->user_lastfm;
$this->design = $userRow->user_design;
// Temporary backwards compatible IP storage system // Temporary backwards compatible IP storage system
try { try {
@ -1060,4 +1067,13 @@ class User
return $sessions; return $sessions;
} }
/**
* Gets the user's selected design.
* @return string
*/
public function design()
{
return Template::exists($this->design) ? $this->design : config('general.design');
}
} }

View file

@ -7,7 +7,6 @@ class BaseTables extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
*
* @return void * @return void
*/ */
public function up() public function up()
@ -550,7 +549,6 @@ class BaseTables extends Migration
/** /**
* Reverse the migrations. * Reverse the migrations.
*
* @return void * @return void
*/ */
public function down() public function down()

View file

@ -7,7 +7,6 @@ class MoveOptionsAndProfileIntoUsers extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
*
* @return void * @return void
*/ */
public function up() public function up()
@ -63,7 +62,6 @@ class MoveOptionsAndProfileIntoUsers extends Migration
/** /**
* Reverse the migrations. * Reverse the migrations.
*
* @return void * @return void
*/ */
public function down() public function down()

View file

@ -7,7 +7,6 @@ class FilesBelongOnTheFilesystem extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
*
* @return void * @return void
*/ */
public function up() public function up()
@ -21,7 +20,6 @@ class FilesBelongOnTheFilesystem extends Migration
/** /**
* Reverse the migrations. * Reverse the migrations.
*
* @return void * @return void
*/ */
public function down() public function down()

View file

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Sakura\DB;
class RecordLoginCountry extends Migration
{
/**
* Run the migrations.
* @return void
*/
public function up()
{
$schema = DB::getSchemaBuilder();
$schema->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');
});
}
}

View file

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Sakura\DB;
class AllowDesignChangePerUser extends Migration
{
/**
* Run the migrations.
* @return void
*/
public function up()
{
$schema = DB::getSchemaBuilder();
$schema->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');
});
}
}

View file

@ -67,7 +67,7 @@ if (!defined('IN_CLI')) {
); );
// Start templating engine and set base variables // Start templating engine and set base variables
Template::set(config('general.design')); Template::set(CurrentSession::$user->design());
Template::vars([ Template::vars([
'get' => $_GET, 'get' => $_GET,
'user' => CurrentSession::$user, 'user' => CurrentSession::$user,