This repository has been archived by the owner on Mar 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.php
109 lines (102 loc) · 4.34 KB
/
index.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
<?php
/**
* ___ ____ _
* / _ \ / ___| | __ _ ___ ___ _ __ ___ ___ _ __ ___
*| | | | | | |/ _` / __/ __| '__/ _ \ / _ \| '_ ` _ \
*| |_| | |___| | (_| \__ \__ \ | | (_) | (_) | | | | | |
* \__\_\\____|_|\__,_|___/___/_| \___/ \___/|_| |_| |_|
* 青草课堂 - index.php
* Copyright (c) 2015 - 2019.,QCTech ,All rights reserved.
* Created by: QCTech
* Created Time: 2019-01-23 - 12:13
*/
//打开缓冲区
ob_start();
header('Content-Type:text/html; charset=UTF-8');
//关掉Notice
error_reporting(E_ERROR);
// 引入配置文件
if(!file_exists("./config.php")) die("站点尚未初始化,请将config.tpl.php重命名为config.php并进行配置!");
require_once ("config.php");
//http method检查
if (!in_array($_SERVER['REQUEST_METHOD'], array('GET', 'POST'))) {
exit('Unsupported HTTP method');
}
//引入功能函数库文件
require_once ("includes/function.php");
//开启session
session_start();
// 域名检测
if($_SERVER['HTTP_HOST'] != $Config["website"]["domain"]) header("Location: "."http://".$Config["website"]["domain"]);
//数据库连接检测
$conn = new mysqli($Config["database"]["address"], $Config["database"]["username"], $Config["database"]["password"]);
if ($conn->connect_error) die("<h1>发生致命错误</h1><br />连接失败: " . $conn->connect_error);
//Router
$NotFound = true;
$HTTPParameters = array();
if (in_array($_SERVER['REQUEST_METHOD'], array('PUT', 'DELETE', 'OPTIONS'))) {
parse_str(file_get_contents('php://input'), $HTTPParameters);
}
$Routes = array(
'GET' => array(),
'POST' => array()
);
//Support HTTP Method: GET / POST
/*
* $Routes的第一维存放请求类型,第二维存放路径
* 在括号内添加正则,<>内为页面变量名称,变量值为指向目标控制器
*/
$Routes['GET']['/'] = 'home';
$Routes['GET']['/sign_up'] = 'show';
$Routes['GET']['/course(/(?<csid>[0-9]+))?'] = 'showCourse';
$Routes['GET']['/user/(?<method>.*)'] = 'user';
$Routes['POST']['/user/(?<method>.*)'] = 'user';
$Routes['GET']['/api/(?<mod>.*)'] = 'api';
$Routes['POST']['/api/(?<mod>.*)'] = 'api';
$Routes['POST']['/callback/(?<mod>.*)'] = 'callback';
$Routes['GET']['/callback/(?<mod>.*)'] = 'callback';
$Routes['GET']['/teacher/(?<mod>.*)'] = 'teacher';
$Routes['POST']['/teacher/(?<mod>.*)'] = 'teacher';
$Routes['GET']['/doc/(?<name>.*)'] = 'document';
$Routes['GET']['/note/(?<ntid>.*)'] = 'note';
//这里是Routes End
if(strpos($_SERVER['REQUEST_URI'],"?")){
$URI = substr($_SERVER['REQUEST_URI'],0, strripos($_SERVER['REQUEST_URI'], '?'));
}else{
$URI = $_SERVER['REQUEST_URI'];
}
$UrlPath = 'home';
$ParametersVariableName = '_' . $_SERVER['REQUEST_METHOD'];
foreach ($Routes[$_SERVER['REQUEST_METHOD']] as $URL => $Controller) {
if (preg_match("#^" . $URL . "$#i", $URI, $Parameters)) {
$NotFound = false;
$Parameters = array_merge($Parameters, $HTTPParameters);
foreach ($Parameters as $Key => $Value) {
if (!is_int($Key)) {
${$ParametersVariableName}[$Key] = urldecode($Value);
$_REQUEST[$Key] = urldecode($Value);
}
}
$UrlPath = $Controller;
break;
}
}
if ($NotFound === true && $_SERVER['REQUEST_METHOD']== 'GET') {
http_response_code(404);
require_once("controller/http_error.php");
die();
}elseif ($NotFound === true){
http_response_code(404);
$return = [
'status' => 'failed',
'code' => -99,
'msg' => '未知模块-'
];
die(json_encode($return));
}
if(!file_exists("controller/".$UrlPath.".php")){
require_once("controller/http_error.php");
die("路由出现问题");
}
//引入页面模板
require_once("./views/common.php");