-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.php
114 lines (103 loc) · 2.67 KB
/
App.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Created by PhpStorm.
* User: Aslangery
* Date: 13.02.2018
* Time: 21:42
*/
if(!defined('APP')) die();
use Models\Session;
use Models\User;
use Controllers\UserController;
class App
{
protected $view='';
public $username='guest';
public $request;
public $session;
public function __construct()
{
$this->request=new \Request();
$this->view=$this->getView('main');
}
/**
* @param string $name
* @param string $vars
* @return bool|string
*/
public static function getView($name='', $vars='')
{
$file='views/'.$name.'.php';
if (file_exists($file)){
ob_start();
include($file);
$view=ob_get_contents();
ob_end_clean();
return $view;
}
return false;
}
/**
* @param string $view
* @param string $position
*/
public function render($view='', $position='content')
{
$pattern='/\{\{'.$position.'\}\}/';
$this->view=preg_replace($pattern, $view, $this->view);
}
/**
* @return bool|string
*/
public function response()
{
$pattern='/\{\{.*\}\}/';
$this->view=preg_replace($pattern, '', $this->view);
return $this->view;
}
/**
* @return bool
*/
public function authorise()
{
$session=new Session();
$this->session=$session->get(session_id());
if ($this->session->session_id!=='')
{
return true;
}
return false;
}
public function run()
{
$vars='';
if ($this->authorise()) {
$u=new User();
$user=$u->get('id',$this->session->user_id);
$this->username=$user->username;
if ($this->request->get['task'] !== null) {
$task = $this->request->get['task'];
$args = explode('.', $task);
$cname = '\\Controllers\\' . ucfirst($args[0]) . 'Controller';
$controller = new $cname;
$method = $args[1];
$vars = $controller->$method($this);
$vars['username']=$this->username;
}
if ($this->request->get['view'] !== null) {
$view = $this->request->get['view'];
$this->render($this->getView($view, $vars));
$this->render($this->getView('logout',$vars),'auth');
}
}
else
{
if($this->request->get['task']=='user.login')
{
$controller=new UserController();
$controller->login($this);
}
$this->render($this->getView('login'),'auth');
}
}
}