-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathinit.php
More file actions
189 lines (171 loc) · 4.74 KB
/
init.php
File metadata and controls
189 lines (171 loc) · 4.74 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
error_reporting(E_ALL & ~E_NOTICE);
date_default_timezone_set('PRC');
define('TIME', time());
!defined('ROOT') && define('ROOT', str_replace("\\", "/", dirname(__FILE__)) . '/');
//__autoload方法
function i_autoload($className) {
if (is_int(strripos($className, '..'))) {
return;
}
$file = ROOT . 'lib/' . $className . '.php';
if (file_exists($file)) {
include $file;
}
}
spl_autoload_register('i_autoload');
!defined('FILE_FLAGS') && define('FILE_FLAGS', LOCK_EX);
/**
* config('name');
* config('name@file');
* config('@file');
*/
if (!function_exists('config')) {
!defined('CONFIG_PATH') && define('CONFIG_PATH', ROOT . 'config/');
function config($key) {
static $configs = array();
list($key, $file) = explode('@', $key, 2);
$file = empty($file) ? 'base' : $file;
$file_name = CONFIG_PATH . $file . '.php';
//读取配置
if (empty($configs[$file]) AND file_exists($file_name)) {
$configs[$file] = @include $file_name;
}
if (func_num_args() === 2) {
$value = func_get_arg(1);
//写入配置
if (!empty($key)) {
$configs[$file] = (array) $configs[$file];
if (is_null($value)) {
unset($configs[$file][$key]);
} else {
$configs[$file][$key] = $value;
}
} else {
if (is_null($value)) {
return unlink($file_name);
} else {
$configs[$file] = $value;
}
}
file_put_contents($file_name, "<?php return " . var_export($configs[$file], true) . ";", FILE_FLAGS);
} else {
//返回结果
if (!empty($key)) {
return $configs[$file][$key];
}
return $configs[$file];
}
}
}
/**
* config('name');
* config('name@file');
* config('@file');
*/
if (!function_exists('cache')) {
!defined('CACHE_PATH') && define('CACHE_PATH', ROOT . 'cache/');
function cache($key, $value = null) {
//$file = CACHE_PATH . md5($key) . '.php';
$redis=new RedisDrive();
if(!$redis){
echo 'Redis 服务未连接';
die();
}
$redis->key=md5($key);
if (is_null($value)) {
if($redis->exists()){
return (array)unserialize($redis->get());
}
else{
$redis->value=serialize([time(), $value]);
$redis->setex();
return array(TIME, $value);
}
} else {
$redis->value=serialize([time(), $value]);
$redis->setex();
//file_put_contents($file, "<?php return " . var_export(array(TIME, $value), true) . ";", FILE_FLAGS);
return array(TIME, $value);
}
}
}
//获取token专用函数
if(!function_exists('getToken')){
function getToken(){
$redis=new RedisDrive();
if(!$redis){
echo 'Redis 服务未连接';
die();
}
$redis->key=md5('token');
if($redis->exists()){
$token=(array)unserialize($redis->get());
if($token['expires_on'] > time()+600){
return $token;
}else{
$refresh_token = config('refresh_token');
$token = onedrive::get_token($refresh_token);
if(!empty($token['refresh_token'])) {
$token['expires_on'] = time() + $token['expires_in'];
$redis->value=serialize($token);
$redis->setex();
return $token;
}
}
}else{
$refresh_token = config('refresh_token');
$token = onedrive::get_token($refresh_token);
$token['expires_on'] = time() + $token['expires_in'];
$redis->value=serialize($token);
$redis->setex();
return (array)$token;
}
}
}
if (!function_exists('db')) {
function db($table) {
return db::table($table);
}
}
if(!function_exists('dd')){
function dd($dump=null){
if(!$dump){
var_dump($dump);
}
die();
}
}
if (!function_exists('view')) {
function view($file, $set = null) {
return view::load($file, $set = null);
}
}
if (!function_exists('_')) {
function _($str) {
return htmlspecialchars($str);
}
}
if (!function_exists('e')) {
function e($str) {
echo $str;
}
}
function get_absolute_path($path) {
$path = str_replace(array('/', '\\', '//'), '/', $path);
$parts = array_filter(explode('/', $path), 'strlen');
$absolutes = array();
foreach ($parts as $part) {
if ('.' == $part) continue;
if ('..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
return str_replace('//','/','/'.implode('/', $absolutes).'/');
}
!defined('CONTROLLER_PATH') && define('CONTROLLER_PATH', ROOT.'controller/');
onedrive::$client_id = config('client_id');
onedrive::$client_secret = config('client_secret');
onedrive::$redirect_uri = config('redirect_uri');