Permalink
Browse files

added message session system & ajax class, WIP

  • Loading branch information...
aaroniker committed Feb 7, 2018
1 parent f1befd4 commit 69e482f8a3c204e9e5d4a774e0e8c6ef5710a201
@@ -0,0 +1,28 @@
<?php
class ajax {
protected static $return = [];
protected static $data = [];
public static function is() {
$json = json_decode(file_get_contents('php://input'), true);
self::$data = (isset($json) && is_array($json)) ? $json : [];
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
}
public static function get($key) {
return (isset(self::$data[$key])) ? self::$data[$key] : null;
}
public static function addReturn($text) {
self::$return[] = $text;
}
public static function getReturn() {
return implode('<br>', self::$return);
}
}
?>
@@ -13,6 +13,7 @@ public function __construct($config) {
error_reporting(E_ALL);
}
$this->message = new message();
$this->hook = new hook();
$this->route = new route($this);
$this->assets = new assets($this);
@@ -28,6 +29,11 @@ public function boot() {
$this->view->register($this->route->includeController(), 'content');
if(ajax::is()) {
echo ajax::getReturn();
exit();
}
echo $this->hook->apply_filters('application.show', $this->view->get('content'));
}
@@ -0,0 +1,74 @@
<?php
class message {
protected $sessionName = 'messages';
function __construct() {
if(ajax::is()) {
if(ajax::get('method') == 'getMessages') {
ajax::addReturn(json_encode($this->getAll()));
}
if(ajax::get('method') == 'setMessage') {
$array = type::post('message', 'string', '');
$this->add($array['message'], $array['class'], $array['stay']);
}
if(ajax::get('method') == 'deleteMessage') {
$index = type::post('index', 'int', 0);
$this->delete($index);
}
}
}
public function add($message, $class = 'success', $stay = false) {
$_SESSION[$this->sessionName][$this->getIndex()] = [
'message' => $message,
'class' => $class,
'stay' => $stay
];
}
public function getAll() {
$return = [];
$messages = type::session($this->sessionName);
if(is_array($messages) && count($messages)) {
foreach($messages as $index => $val) {
$return[] = [
'message' => $val['message'],
'class' => $val['class'],
'stay' => $val['stay'],
'index' => $index
];
}
}
return $return;
}
public function delete($index) {
if(isset($_SESSION[$this->sessionName][$index])) {
unset($_SESSION[$this->sessionName][$index]);
}
}
protected function getIndex($index = 0) {
$index = (!$index) ? count(type::session($this->sessionName, 'array', [])) + 1 : $index;
if(isset($_SESSION[$this->sessionName][$index])) {
$index++;
return $this->getIndex($index);
}
return $index;
}
}
?>
@@ -135,7 +135,7 @@ public function getAllRoutes() {
public function redirect($name, $array = []) {
$this->getAllRoutes();
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(isset($this->routes[$name]) && $actual_link != $this->getLink($name, $array)) {
if(isset($this->routes[$name]) && $actual_link != $this->getLink($name, $array) && !ajax::is()) {
header('location: '.$this->getLink($name, $array));
exit();
}
@@ -158,9 +158,10 @@ public function getLink($name, $array = []) {
public function error404() {
$url = ($this->admin) ? $this->url.'/'.$this->app->config->get('system')['adminURL'] : $this->url;
header('location: '.$url);
exit();
if(!ajax::is()) {
header('location: '.$url);
exit();
}
}
@@ -26,44 +26,40 @@ public function __construct($app, $module) {
public function login($email, $password, $remember = 0) {
$return['error'] = true;
if($block = $this->attempt->isBlocked()) {
$return['message'] = 'user blocked for '.$block['minutes'].'min and '.$block['seconds'].'sec';
return $return;
$this->app->message->add('user blocked for '.$block['minutes'].'min and '.$block['seconds'].'sec', 'error');
return false;
}
$validateEmail = $this->validateEmail($email);
if($validateEmail['error']) {
$this->attempt->add();
$return['message'] = $validateEmail['message'];
return $return;
$this->app->message->add($validateEmail['message'], 'error');
return false;
}
$userID = $this->user->getID(strtolower($email));
if(!$userID) {
$this->attempt->add();
$return['message'] = 'email not found';
$this->app->message->add('email not found', 'error');
return $return;
}
$user = $this->user->getUser($userID);
if(!$this->passwordRehash($password, $user->password, $userID)) {
$this->attempt->add();
$return['message'] = 'incorrect password';
$this->app->message->add('incorrect password', 'error');
return $return;
}
$session = $this->addSession($user->id, $remember);
$this->app->hook->do_action('auth.login', $this->app, $this);
$return['error'] = false;
return $return;
return true;
}
@@ -21,6 +21,7 @@ public function index() {
public function logout() {
$this->app->message->add('logged out');
$this->app->auth->logout();
$this->app->route->redirect('login');
@@ -31,11 +31,8 @@
'application.boot-6' => function($app) {
if(type::post('action') == 'login') {
$return = $app->auth->login(type::post('email'), type::post('password'), type::post('remember'));
if(!$return['error']) {
if($app->auth->login(type::post('email'), type::post('password'), type::post('remember'))) {
$app->route->redirect('dashboard');
} else {
echo $return['message'];
}
}
@@ -1,3 +1,9 @@
dashboard <a href="<?= $route->getLink('login', ['logout']); ?>"><?= __('Logout'); ?></a>

<test-comp></test-comp>

<pre>
<?php
var_dump(json_encode($app->message->getAll()));
?>
</pre>
@@ -5,12 +5,15 @@ function install(Vue) {

Vue.config.debug = gear.debug;

var VueResource = require('vue-resource');
var axios = require('axios');
var $ = require('jquery');

Vue.use(VueResource);

Vue.http.options.root = gear.url;
Vue.http.options.emulateHTTP = true;
Vue.prototype.$api = axios.create({
baseURL: gear.url + '/' + gear.adminURL + '/',
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
});

function getLang(name) {
if(name in lang) {
@@ -19,6 +22,23 @@ function install(Vue) {
return name;
}

function displayMessages(handle) {
handle.$api.post('index.php', {
'method': 'getMessages'
}).then(function(response) {
var messages = response.data;
Object.keys(messages).forEach(function(index) {
var div = $("<div />").addClass([
'message',
messages[index].class
]).text(messages[index].message);
div.appendTo($('#gear'));
});
}).catch(function(error) {
console.log(error);
});
}

Vue.filter('lang', function(name) {
return getLang(name);
});
@@ -28,7 +48,10 @@ function install(Vue) {
};

new Vue({
el: '#gear'
el: '#gear',
created() {
displayMessages(this);
}
});

}
@@ -10,7 +10,8 @@ module.exports = [
},
resolve: {
alias: {
"vue-resource$": assets + "/vue-resource/dist/vue-resource.common.js",
"axios$": assets + "/axios/dist/axios.min.js",
"jquery$": assets + "/jquery/dist/jquery.min.js"
}
},
module: {
@@ -1,5 +1,7 @@
<?php
session_start();
if(version_compare($version = PHP_VERSION, $required = '5.6', '<=')) {
exit(sprintf('Gear CMS needs at least <strong>PHP %s</strong> (Current: <strong>PHP %s</strong>).', $required, $version));
}

0 comments on commit 69e482f

Please sign in to comment.