-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.php
executable file
·170 lines (142 loc) · 4.29 KB
/
Base.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
defined('ACCESS') or die(header('HTTP/1.1 403 Forbidden'));
// Base controller reperesent necessary objects and libraries
class BaseController
{
static $page;
var $smarty;
var $session;
var $model;
var $config;
public function __construct()
{
$this->smarty = new Smarty;
$this->session = new Sessions();
$this->model = NULL;
$this->config = NULL;
// Load module model
if (file_exists('Modules/' . self::$page . '/Model.php')) {
require 'Modules/' . self::$page . '/Model.php';
$this->model = new Model();
}
// Some modules may have specific config, so we must load it
if (file_exists('Modules/' . self::$page . '/Config.php')) {
require 'Modules/' . self::$page . '/Config.php';
$this->config = new Config();
}
}
// Pass the view to Smarty
// Default view is "View"
// Some modules may have several view's, We can pass different view on different conditions
public function render($view = 'View')
{
$this->smarty->display('Modules/' . self::$page . '/' . $view . '.tpl');
}
// Check current user has permission
// Return boolean
public function hasPermission($permission)
{
switch ($permission) {
case 'admin':
break;
case 'user':
if ($this->session->get('user_id')) {
return true;
} else {
return false;
}
break;
case 'guest':
if ($this->session->get('user_id')) {
return false;
} else {
return true;
}
break;
}
}
public static function loadLibrary($library)
{
switch ($library) {
case 'Jalali':
require 'Libraries/Jalali/jdf.php';
break;
}
}
}
// Base model reperesent db connection and query functions
class BaseModel
{
var $db;
public function __construct()
{
}
private function prepare_database()
{
if (isset($this->db)) return;
$this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_PORT);
/* check connection */
if ($this->db->connect_errno) {
printf("Connect failed: %s\n", $this->db->connect_error);
exit();
}
$this->db->set_charset("utf8");
}
public function query($sql, $params = array(), $execution_result = false)
{
$this->prepare_database();
$stmt = $this->db->prepare($sql);
if (count($params) > 0) {
$tmp = array();
foreach ($params as $key => $value) $tmp[$key] = &$params[$key];
call_user_func_array(array($stmt, 'bind_param'), $tmp);
}
$exe_res = $stmt->execute();
if ($execution_result) {
$stmt->close();
return $exe_res;
}
$res = $this->get_result($stmt);
$a_data = $res;
// if( count($res) > 0 ) {
// while ($row = $res->fetch_array(MYSQLI_ASSOC)) {
// $a_data[] = $row;
// }
// }
$stmt->close();
return $a_data;
}
function get_result($Statement)
{
$RESULT = array();
$Statement->store_result();
for ($i = 0; $i < $Statement->num_rows; $i++) {
$Metadata = $Statement->result_metadata();
$PARAMS = array();
while ($Field = $Metadata->fetch_field()) {
$PARAMS[] = &$RESULT[$i][$Field->name];
}
call_user_func_array(array($Statement, 'bind_result'), $PARAMS);
$Statement->fetch();
}
return $RESULT;
}
public function visit_log($action)
{
$this->query(
'INSERT INTO visit_log (session_id, ip, user_agent, action) VALUES (?, ?, ?, ?)'
, array('ssss', session_id(), get_user_ip(), $_SERVER['HTTP_USER_AGENT'], $action), true
);
}
}
function getAddress()
{
return 'http://' . $_SERVER['HTTP_HOST'] . DEFAULT_PATH;
}
function get_user_ip()
{
if (!empty($_SERVER['REMOTE_ADDR']))
return $_SERVER['REMOTE_ADDR'];
else
return '0.0.0.0';
}