-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.php
More file actions
104 lines (85 loc) · 2.86 KB
/
Copy pathrouter.php
File metadata and controls
104 lines (85 loc) · 2.86 KB
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
<?php
/**
* Created by PhpStorm.
* User: ashulpekov
* Date: 04.05.2016
* Time: 11:17
*/
$route = new Router();
$route->init();
class Router
{
static $config;
static $response;
public $request;
static $db;
function init()
{
session_start();
require_once('config.php');
if ($_REQUEST == '' || empty($_REQUEST) || !isset($_REQUEST['page']))
{
$_REQUEST['page'] = 'hash';
$_REQUEST['action'] = 'saveFile';
}
$this->request = $_REQUEST;
$this->dbConnect();
$page = $_REQUEST['page'];
$action = $_REQUEST['action'];
require_once('server/'.$page.'/'.$page.'_ctrl.php');
$controller_name = ucfirst($page).'_ctrl';
$controller = new $controller_name();
$controller->$action();
self::returnResponse();
}
static function showError($place = __CLASS__, $problem = ' undefined error', $system_message = ' none ')
{
self::$response['serverError']['place'] = $place;
self::$response['serverError']['problem'] = $problem;
self::$response['serverError']['message'] = $system_message;
echo json_encode(self::$response);
die;
}
static function returnResponse()
{
echo json_encode(self::$response);
die;
}
private function dbConnect()
{
$connSTR = 'mysql:host=' . self::$config['DB']['hostname'] . ';' .
'dbname=' . self::$config['DB']['database'] . ';' .
'charset=' . self::$config['DB']['charset'];
try {
self::$db = new PDO($connSTR, self::$config['DB']['user'], self::$config['DB']['password'],
array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false));
} catch (PDOException $e) {
self::showError(__METHOD__, $connSTR, $e->getMessage());
}
}
static function query_select($queryStr = '')
{
if ($queryStr == '' || strpos($queryStr, 'INSERT') || strpos($queryStr, 'UPDATE'))
self::showError(__METHOD__, $queryStr, ' Wrong method using');
try {
self::$db->quote($queryStr);
$result = self::$db->prepare($queryStr);
$result->execute();
$dbArray = $result->fetchAll(PDO::FETCH_ASSOC);
$result->closeCursor();
return $dbArray;
} catch (PDOException $e) {
self::showError(__METHOD__, $queryStr, $e->getMessage());
}
}
static function query_execute($queryStr = '')
{
try {
self::$db->quote($queryStr);
$result = self::$db->prepare($queryStr);
return $result->execute();
} catch (PDOException $e) {
self::showError(__METHOD__, $queryStr, $e->getMessage());
}
}
}