-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
81 lines (70 loc) · 2.47 KB
/
common.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
<?php define('TANG_FENG', TRUE);
define('DOC_ROOT', __DIR__);
define('DOC_DIR', substr(dirname($_SERVER['SCRIPT_NAME']), 0));
define('BASE_URL', DOC_DIR == DIRECTORY_SEPARATOR? '' : DOC_DIR);
define('LIB_DIR', dirname(__FILE__).'/lib/');
require('lib/Slim/Slim.php');
require('lib/ip/ip.php');
require('helper/helper.php');
require('helper/phone.php');
require('src/admin.php');
date_default_timezone_set('Asia/Shanghai');
\Slim\Slim::registerAutoloader();
\Config::init();
if(DEBUG) {
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$logFile = Config::get('log', 'dir'). '/'. Config::get('log', 'file');
$setting = array(
'mode' => 'development',
'log.enable' => true,
'debug' => true,
'log.writer' => new \Slim\LogWriter(fopen($logFile, 'a')),
);
} else {
ini_set('display_errors', 'Off');
error_reporting(0);
$logFile = Config::get('log', 'dir'). '/'. Config::get('log', 'file');
$setting = array(
'mode' => 'production',
'log.enable' => true,
'debug' => false,
'log.writer' => new \Slim\LogWriter(fopen($logFile, 'a')),
'log.level' => \Slim\Log::DEBUG,
);
}
$app = new \Slim\Slim($setting);
Admin::init($app);
Phone::init();
$sessionName = Config::get('settings', 'sessionName');
if($sessionName) {
ini_set('session.name', $sessionName);
ini_set('session.cookie_httponly', 1);
session_start();
}
$app->hook('slim.before.router', function() use($app){
$path = $app->request->getPathInfo();
$routes = Config::getOptions('routes');
$dir = basename($_SERVER['SCRIPT_NAME'], '.php');
if(array_key_exists($path, $routes)) {
list($method, $control, $action) = explode('::', $routes[$path]);
$controlFile = DOC_ROOT. "/control/{$dir}/". $control. '.php';
if(file_exists($controlFile)) {
require($controlFile);
$controller = new $control($app);
if(method_exists($controller, $action)) {
if($method) {
$app->map($path, array($controller, $action))->via($method);
} else {
$app->map($path, array($controller, $action))->via('GET', 'POST');
}
}
}
}
if(!isset($_SESSION[Config::SESSION_USER]) || !$_SESSION[Config::SESSION_USER]) {
if(!in_array($path, Config::getOptions('notauth'))) {
$app->redirect(BASE_URL . "/{$dir}/login?from=" . urlencode($_SERVER['REQUEST_URI']));
}
}
});
return $app;