better-staridc-MNBT
1<?php
2
3/************************************************
4 *
5 * Amysql PHPMVC - AMP 1.5
6 * amh.sh
7 * @param Object $AmysqlProcess 网站进程
8 * @copyright AMH & amh.sh
9 * @license APL (http://amh.sh/apl.htm) V1.0
10 *
11 */
12class Amysql {
13
14 public $AmysqlProcess;
15
16 public function __construct()
17 {
18 global $Config;
19 ini_set("magic_quotes_runtime", false);
20 ($Config['DebugPhp'] && error_reporting(E_ALL & ~E_NOTICE)) || error_reporting(0); // 是否报错
21 ($Config['SessionStart'] && @session_start()); // SESSION
22 (!empty($Config['CharSet']) && header('Content-type: text/html;charset=' . $Config['CharSet']));
23
24 }
25
26 static public function AmysqlNotice($notice)
27 {
28 $AmysqlController = new AmysqlController();
29 $AmysqlController -> notice = $notice;
30 $AmysqlController -> _view('AmysqlNotice');
31 exit();
32 }
33
34 static public function filter(&$array, $function)
35 {
36 if (!is_array($array)) Return $array = $function($array);
37 foreach ($array as $key => $value)
38 (is_array($value) && $array[$key] = Amysql::filter($value, $function)) || (is_string($value) && $array[$key] = $function($value));
39 Return $array;
40 }
41}
42
43
44/************************************************
45 *
46 * 总进程对象
47 * @param Object $ControllerName 控制器对象
48 * @param string $ControllerName 控制器名称
49 * @param string $ActionName 方法名称
50 * @param string $ControllerFile 控制器文件
51 *
52 */
53class AmysqlProcess {
54
55 public $AmysqlController;
56 public $ControllerName;
57 public $ActionName;
58 public $ControllerFile;
59
60 function ProcessStart()
61 {
62 global $Config;
63 if ($Config['HttpPath'])
64 {
65 $GETParam = (strlen($_SERVER['REQUEST_URI']) > strlen($_SERVER['SCRIPT_NAME'])) ? explode('/', trim(str_ireplace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']), '/')) : '';
66 $GETCount = count($GETParam);
67 if($GETCount > 1)
68 for ($i=2; $i<$GETCount; ++$i) $_GET[$GETParam[$i]] = isset($GETParam[++$i]) ? $GETParam[$i] : '';
69 }
70
71 $magic_quotes = function_exists('get_magic_quotes_gpc') ? get_magic_quotes_gpc() : false; // 环境是否有过滤
72 if($Config['Filter']) // 开启过滤
73 {
74 ( !$magic_quotes && (Amysql::filter($_GET, 'addslashes') && Amysql::filter($_POST, 'addslashes') && Amysql::filter($_COOKIE, 'addslashes') && Amysql::filter($_FILES, 'addslashes')) );
75 }
76 else
77 {
78 ( $magic_quotes && (Amysql::filter($_GET, 'stripslashes') && Amysql::filter($_POST, 'stripslashes') && Amysql::filter($_COOKIE, 'stripslashes') && Amysql::filter($_FILES, 'stripslashes')) );
79 }
80
81 $this -> ControllerName = !empty($GETParam[0]) ? $GETParam[0] : ( (isset($_GET[$Config['UrlControllerName']]) && !empty($_GET[$Config['UrlControllerName']])) ? $_GET[$Config['UrlControllerName']] : 'index');
82 $this -> ControllerName = str_replace(_PathTag, DIRECTORY_SEPARATOR, $this -> ControllerName);
83 $this -> ActionName = !empty($GETParam[1]) ? $GETParam[1] : ( (isset($_GET[$Config['UrlActionName']]) && !empty($_GET[$Config['UrlActionName']])) ? $_GET[$Config['UrlActionName']] : 'IndexAction');
84 $this -> ControllerFile = _Controller . $this -> ControllerName . '.php';
85 }
86
87 function ControllerStart()
88 {
89 ((is_file($this -> ControllerFile) && include_once($this -> ControllerFile)) ||
90 (is_file(_Controller . 'index.php') && include_once(_Controller . 'index.php')) ||
91 Amysql::AmysqlNotice($this -> ControllerFile . ' 控制器文件不存在'));
92
93 (class_exists($this -> ControllerName) || (($this -> ControllerName = 'index') && class_exists('index')) ||
94 Amysql::AmysqlNotice($this -> ControllerName . ' 控制器不存在'));
95
96 $methods = get_class_methods($this -> ControllerName); // 获取类中的方法名
97
98 (in_array($this -> ActionName, $methods, true) ||
99 (($this -> ActionName = 'IndexAction') && in_array($this -> ActionName, $methods, true)) ||
100 Amysql::AmysqlNotice($this -> ActionName . ' 方法不存在'));
101
102 $this -> AmysqlController = new $this->ControllerName($_GET); // 实例控制器
103 $this -> AmysqlController -> {$this -> ActionName}(); // 执行方法
104 }
105
106
107}
108
109
110/************************************************
111 *
112 * 控制器逻辑层 (C)
113 * @param Object $DB 数据库对象
114 * @param Array $DBConfig 链接配置
115 * @param Array $AmysqlModel 模型对象集
116 * @param Object $AmysqlTemplates 模板对象
117 * @param Array $EchoData 调试数据
118 *
119 */
120class AmysqlController {
121
122 public $DB;
123 public $DBConfig; // 控制器调用Mysql的链接配置
124 public $AmysqlModel; // 控制器关联的数据模型对象集
125 public $AmysqlClass; // 控制器关联的类对象集
126 public $AmysqlTemplates; // 控制器关联的模板对象
127 public $EchoData = array(); // 调试数据
128
129 function __construct()
130 {
131 global $Config;
132 $this -> DBConfig = $Config;
133
134 $this -> DB = new DB();
135 $this -> AmysqlTemplates = new AmysqlTemplates();
136 }
137
138 /**
139 * 设置数据库配置
140 * @param Array $Config 链接配置
141 */
142 function _DBSet($_Config = NULL)
143 {
144 global $Config;
145 $_Config = ($_Config == NULL) ? $Config : $_Config;
146 foreach ($Config as $key=>$val)
147 (empty($_Config[$key]) && $_Config[$key] = $val);
148 $this -> DBConfig = $_Config;
149 }
150
151 /**
152 * 模板赋值
153 * @param string $name 名称
154 * @param string $value 值
155 */
156 public function __set($name, $value = NULL)
157 {
158 $this -> AmysqlTemplates -> TemplateValue[$name] = $value;
159 }
160
161 /**
162 * 数组模板赋值
163 * @param Array $array 数据
164 */
165 public function _array($array)
166 {
167 if (is_array($array))
168 {
169 foreach ($array as $key=>$val)
170 $this -> AmysqlTemplates -> TemplateValue[$key] = $val;
171 }
172 }
173
174 /**
175 * 加载自定义类文件
176 * @param string $file 文件名
177 * @param string $ClassName 类名(可选,默认为文件名)
178 * @return Object $ClassName() 类对象
179 */
180 public function _class($file, $ClassName = NULL)
181 {
182 $file = str_replace(_PathTag, DIRECTORY_SEPARATOR, $file);
183 $ClassName = ($ClassName == NULL) ? $file : $ClassName;
184 $file = _Class . $file . '.php';
185 if(is_file($file))
186 {
187 include_once($file);
188 if(!class_exists($ClassName))
189 Amysql::AmysqlNotice($ClassName . ' 类对象不存在');
190
191 if(!isset($this -> AmysqlClass[$ClassName])) // 不存在类对象
192 $this -> AmysqlClass[$ClassName] = new $ClassName();
193 Return $this -> AmysqlClass[$ClassName];
194 }
195
196 Amysql::AmysqlNotice($file . ' 类文件不存在');
197 }
198
199 /**
200 * 加载模型文件
201 * @param string $file 文件名
202 * @param string $ClassName 模型名(可选,默认为文件名)
203 * @return Object $ClassName() 模型对象
204 */
205 public function _model($file, $ClassName = NULL)
206 {
207 $file = str_replace(_PathTag, DIRECTORY_SEPARATOR, $file);
208 $ClassName = ($ClassName == NULL) ? $file : $ClassName;
209 $file = _Model . $file . '.php';
210 if(is_file($file))
211 {
212 include_once($file);
213 if(!class_exists($ClassName))
214 Amysql::AmysqlNotice($ClassName . ' 模型对象不存在');
215
216 $this -> DBConfig['ModelTag'] = $ModelTag = $ClassName . '_' . $this -> DBConfig['ConnectTag']; // 模型标识
217 if(!isset($this -> AmysqlModel[$ModelTag])) // 不存在模型
218 {
219 $this -> AmysqlModel[$ModelTag] = new $ClassName();
220 if(!isset($this -> DB -> DBS[$ModelTag])) $this -> DB -> DBConnect($this -> DBConfig); // 没连接就进行数据库连接
221 $this -> AmysqlModel[$ModelTag] -> MysqlConnect = $this -> DB -> DBS[$ModelTag]; // 模型使用的数据库连接
222 }
223 Return $this -> AmysqlModel[$ModelTag];
224 }
225
226 Amysql::AmysqlNotice($file . ' 模型文件不存在');
227 }
228
229 /**
230 * 获得相关文件数据(模板目录)
231 * @param string $file 文件名
232 * @return string 数据
233 */
234 public function _file($file)
235 {
236 $file = str_replace(_PathTag, DIRECTORY_SEPARATOR, $file);
237 $file = _View . $file . '.php';
238 if(is_file($file))
239 Return file_get_contents($file);
240
241 Amysql::AmysqlNotice($file . ' 数据文件不存在');
242 }
243
244 /**
245 * 数据调试
246 * @param $data 数据
247 */
248 public function _echo($data)
249 {
250 $this -> EchoData[] = $data;
251 }
252
253 /**
254 * 模板显示
255 */
256 public function _view($file = null)
257 {
258 // 控制器属性共享于模板 2012-4-20
259 $AmysqlSelfPropertyKey = array_keys(get_class_vars('AmysqlController'));
260 $ControllerPropertyParam = get_object_vars($this);
261 foreach ($ControllerPropertyParam as $key=>$val)
262 {
263 if(!in_array($key, $AmysqlSelfPropertyKey)) // 非Amysql控制器属性
264 $this -> AmysqlTemplates -> TemplateValue[$key] = $val;
265 }
266
267 $this -> AmysqlTemplates -> _view($file);
268
269 // 信息调试输出
270 if (is_array($this -> AmysqlModel))
271 {
272 foreach ($this -> AmysqlModel as $key=>$val) // Sql调试
273 echo $this -> AmysqlModel[$key] -> SqlBug;
274 }
275 foreach ($this -> EchoData as $val)
276 echo "<!--<pre>\n" , print_r($val, true) , "\n</pre>-->";
277 }
278}
279
280/************************************************
281 *
282 * 视图表现层 (V)
283 * @param Array $TemplateValue 模板数据
284 *
285 */
286class AmysqlTemplates
287{
288 public $TemplateValue; // 模板数据
289
290 function __construct()
291 {
292 // 自定义模板常用参数数据
293 $this -> TemplateValue['_date'] = date('Y-m-d H:i:s', time());
294 }
295
296 public function _view($file = null)
297 {
298 global $Config;
299 // 2012-10-8
300 ($Config['XSS'] && Amysql::filter($this -> TemplateValue, 'htmlspecialchars') && Amysql::filter($_GET, 'htmlspecialchars') && Amysql::filter($_POST, 'htmlspecialchars') && Amysql::filter($_COOKIE, 'htmlspecialchars') );
301
302 @extract($this -> TemplateValue);
303 $file = str_replace(_PathTag, DIRECTORY_SEPARATOR, $file);
304 $file = _View . $file . '.php';
305 if(is_file($file))
306 Return include($file);
307
308 Amysql::AmysqlNotice($file . ' 模板文件不存在');
309 }
310}
311
312/************************************************
313 *
314 * 模型数据层 (M)
315 * @param Object $MysqlConnect 当前数据链接
316 * @param String $Affected 影响数
317 * @param String $InsertId 新增ID
318 * @param String $ResultSum 结果集数量
319 * @param String $QueryStatus 查询状态
320 *
321 */
322class AmysqlModel {
323
324 public $MysqlConnect;
325 public $Affected;
326 public $InsertId;
327 public $ResultSum;
328 public $QueryStatus;
329 public $SqlBug = '';
330
331 /**
332 * 基本查询
333 * @param string $sql SQL语句
334 * @return Object 结果集
335 */
336 public function _query($sql)
337 {
338 global $Config;
339 if(!$this -> MysqlConnect) Return false;
340 $this -> QueryStatus = '(0)';
341 $this -> Affected = 0;
342 if($Config['DebugSql']) $this -> SqlBug .= "\n". '<!--DebugSql: ' . $sql . '-->' . "\n";
343 $result = mysqli_query($this -> MysqlConnect, $sql);
344 if (!$result) Return false;
345 $this -> Affected = mysqli_affected_rows($this -> MysqlConnect);
346 $this -> QueryStatus = '(ok)';
347 Return $result;
348 }
349
350 /**
351 * 插入数据
352 * @param string $table 表名
353 * @param Array $data 数据
354 * @return int InsertId 新增ID
355 */
356 public function _insert($table, $data)
357 {
358 $this -> InsertId = 0;
359 if (!is_array($data) || count($data) == 0) Return 0;
360
361 $field_arr = array();
362 foreach ($data as $key=>$val)
363 $field_arr[] = " `$key` = '$val' ";
364
365 $sql = "INSERT INTO `$table` SET " . implode(',', $field_arr);
366 $this -> _query($sql);
367 $this -> InsertId = mysqli_insert_id($this -> MysqlConnect);
368 Return $this -> InsertId;
369 }
370
371 /**
372 * 更新数据
373 * @param string $table 表名
374 * @param Array $data 数据
375 * @param string $where 更新条件
376 * @return int Affected 影响数
377 */
378 public function _update($table, $data, $where = '')
379 {
380 $this -> Affected = 0;
381 if (!is_array($data) || count($data) == 0) Return 0;
382
383 $field_arr = array();
384 foreach ($data as $key=>$val)
385 $field_arr[] = " `$key` = '$val' ";
386
387 $sql = "UPDATE `$table` SET " . implode(',', $field_arr) . $where;
388 $this -> _query($sql);
389 Return $this -> Affected;
390 }
391
392 /**
393 * 取得一排数据
394 * @param string $sql SQL语句
395 * @param string $result_type 类型
396 * @return Array $row 数据
397 */
398 public function _row($sql)
399 {
400 $result = $this -> _query($sql);
401 if (!$result) Return false;
402 $row = mysqli_fetch_array($result);
403 $this -> ResultSum = 1;
404 $this -> _free($result);
405 Return $row;
406 }
407
408 /**
409 * 取得所有数据
410 * @param string $sql SQL语句
411 * @param string $result_type 类型
412 * @return Array $row 数据
413 */
414 public function _all($sql)
415 {
416 $result = $this -> _query($sql);
417 if (!$result) Return false;
418 $data = array();
419 $this -> ResultSum = 0;
420 while ($row = mysqli_fetch_array($result))
421 {
422 $data[] = $row;
423 ++$this -> ResultSum;
424 }
425 $this -> _free($result);
426 Return $data;
427 }
428
429 /**
430 * 取得数据总数
431 * @param string $sql SQL语句
432 * @return int 数量
433 */
434 public function _sum($sql)
435 {
436 $result = $this -> _query($sql);
437 if (!$result) Return false;
438 Return mysqli_num_rows($result);
439 }
440
441 /**
442 * 释放结果集
443 * @param Object $rs 释放
444 */
445 public function _free($rs)
446 {
447 Return mysqli_free_result($rs);
448 }
449
450}
451
452/************************************************
453 *
454 * 数据库对象
455 * @param Object $DBS 链接对象集
456 */
457class DB
458{
459 public $DBS;
460 public function DBConnect($Config)
461 {
462 $this -> DBS[$Config['ModelTag']] = @mysqli_connect($Config['Host'], $Config['User'], $Config['Password']);
463 (!$this -> DBS[$Config['ModelTag']] && Amysql::AmysqlNotice(mysqli_connect_error() . ' MySQL连接出错。'));
464
465 (!empty($Config['DBname']) && mysqli_select_db($this -> DBS[$Config['ModelTag']], $Config['DBname']));
466 $CharSet = str_replace('-', '', $Config['CharSet']);
467 mysqli_query($this -> DBS[$Config['ModelTag']], "SET NAMES '$CharSet'");
468 }
469}