<?php
// Hocus Pocus, grab the focus
// Magic. Do not touch.
class Controller {
function beforeroute() {
// Inactivity Check
if( $this->f3->get('SESSION.granted') ) {
if( time() - $this->f3->get('SESSION.timestamp') > $this->f3->get('auto_logout') ) {
$this->f3->clear('SESSION');
$this->f3->reroute('@home');
} else {
$this->f3->set('SESSION.timestamp', time());
}
}
// CSRF Prevention
if( NULL === $this->f3->get('POST.token') ){
$this->f3->CSRF = $this->f3->session->csrf();
$this->f3->copy('CSRF','SESSION.csrf');
}
if( $this->f3->VERB === 'POST' ){
if( $this->f3->get('POST.token') == $this->f3->get('SESSION.csrf') ) {
// Thing check out! No CSRF attack was detectd.
$this->f3->set('CSRF', $this->f3->session->csrf()); // Reset CSRF token vor next post request.
$this->f3->copy('CSRF','SESSION.csrf'); // Copy the token to the variable.
} else {
// DANGER: CSRF Attack!
$this->flash->addMessage($this->f3->get('DICT.csrf'), 'danger');
$this->f3->reroute('@home');
}
}
// Access Control
$access = \Access::instance();
$access->policy('allow'); // Allow access to all routes by default.
$somebody = $this->f3->exists('SESSION.user.group') ? $this->f3->get('SESSION.user.group') : 0;
$access->authorize($somebody, function($route,$subject){
$this->flash->addMessage($this->f3->get('DICT.accessDenied'), 'danger');
$this->f3->reroute('@home');
});
// Database insert/edit validation
$this->validator->loadLang();
$this->validator->onError(function($text,$key) {
$this->f3->set('error.'.$key, $text);
$this->flash->addMessage($text,'danger');
});
}
function __construct(){
$this->f3 = \Base::instance();
$this->db = new DB\SQL("mysql:host=" . $this->f3->get("database.host") . ";port=" . $this->f3->get("database.port") . ";dbname=" . $this->f3->get("database.name"),$this->f3->get("database.user"),$this->f3->get("database.pass"), array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)); $this->f3->set('DB',$this->db); // Set DB keys for Cortex
$this->template = \Template::instance();
$this->flash = \Flash::instance();
$this->validator = \Validation::instance();
$this->today = date("Y-m-d H:i:s");
$this->pushbullet = new \Pushbullet\Pushbullet($this->f3->get('pushbullet.apikey'));
}
}