commit | author | age
|
19351a
|
1 |
<?php |
B |
2 |
|
|
3 |
/** |
|
4 |
程序说明 |
|
5 |
* =========================================================== |
|
6 |
* |
|
7 |
|
|
8 |
* ---------------------------------------------------------- |
|
9 |
|
|
10 |
* ========================================================== |
|
11 |
|
|
12 |
* $Id: cron.php 17217 2011-01-19 06:29:08Z $ |
|
13 |
*/ |
|
14 |
|
|
15 |
define('IN_ECS', true); |
|
16 |
|
|
17 |
require('./init.php'); |
|
18 |
//require('../includes/lib_time.php'); |
|
19 |
|
|
20 |
$timestamp = gmtime(); |
|
21 |
//check_method(); |
|
22 |
$error_log = array(); |
|
23 |
if (isset($set_modules)) |
|
24 |
{ |
|
25 |
$set_modules = false; |
|
26 |
unset($set_modules); |
|
27 |
} |
|
28 |
$crondb = get_cron_info(); // 获得需要执行的计划任务数据 |
|
29 |
|
|
30 |
foreach ($crondb AS $key => $cron_val) |
|
31 |
{ |
|
32 |
if (file_exists(ROOT_PATH . 'includes/modules/cron/' . $cron_val['cron_code'] . '.php')) |
|
33 |
{ |
|
34 |
if (!empty($cron_val['allow_ip'])) // 设置了允许ip |
|
35 |
{ |
|
36 |
$allow_ip = explode(',', $cron_val['allow_ip']); |
|
37 |
$server_ip = real_server_ip(); |
|
38 |
if (!in_array($server_ip, $allow_ip)) |
|
39 |
{ |
|
40 |
continue; |
|
41 |
} |
|
42 |
} |
|
43 |
if (!empty($cron_val['minute'])) // 设置了允许分钟段 |
|
44 |
{ |
|
45 |
$m = explode(',', $cron_val['minute']); |
|
46 |
$m_now = intval(local_date('i',$timestamp)); |
|
47 |
if (!in_array($m_now, $m)) |
|
48 |
{ |
|
49 |
continue; |
|
50 |
} |
|
51 |
} |
|
52 |
if (!empty($cron_val['alow_files'])) // 设置允许调用文件 |
|
53 |
{ |
|
54 |
$f_info = parse_url($_SERVER['HTTP_REFERER']); |
|
55 |
$f_now = basename($f_info['path']); |
|
56 |
$f = explode(' ', $cron_val['alow_files']); |
|
57 |
if (!in_array($f_now, $f)) |
|
58 |
{ |
|
59 |
continue; |
|
60 |
} |
|
61 |
} |
|
62 |
if (!empty($cron_val['cron_config'])) |
|
63 |
{ |
|
64 |
foreach ($cron_val['cron_config'] AS $k => $v) |
|
65 |
{ |
|
66 |
$cron[$v['name']] = $v['value']; |
|
67 |
} |
|
68 |
} |
|
69 |
include_once(ROOT_PATH . 'includes/modules/cron/' . $cron_val['cron_code'] . '.php'); |
|
70 |
} |
|
71 |
else |
|
72 |
{ |
|
73 |
$error_log[] = make_error_arr('includes/modules/cron/' . $cron_val['cron_code'] . '.php not found!',__FILE__); |
|
74 |
} |
|
75 |
|
|
76 |
$close = $cron_val['run_once'] ? 0 : 1; |
|
77 |
$next_time = get_next_time($cron_val['cron']); |
|
78 |
$sql = "UPDATE " . $ecs->table('crons') . |
|
79 |
"SET thistime = '$timestamp', nextime = '$next_time', enable = $close " . |
|
80 |
"WHERE cron_id = '$cron_val[cron_id]' LIMIT 1"; |
|
81 |
|
|
82 |
$db->query($sql); |
|
83 |
} |
|
84 |
write_error_arr($error_log); |
|
85 |
|
|
86 |
function get_next_time($cron) |
|
87 |
{ |
|
88 |
$y = local_date('Y', $GLOBALS['timestamp']); |
|
89 |
$mo = local_date('n', $GLOBALS['timestamp']); |
|
90 |
$d = local_date('j', $GLOBALS['timestamp']); |
|
91 |
$w = local_date('w', $GLOBALS['timestamp']); |
|
92 |
$h = local_date('G', $GLOBALS['timestamp']); |
|
93 |
$sh = $sm = 0; |
|
94 |
$sy = $y; |
|
95 |
if ($cron['day']) |
|
96 |
{ |
|
97 |
$sd = $cron['day']; |
|
98 |
$smo = $mo + 1; |
|
99 |
} |
|
100 |
else |
|
101 |
{ |
|
102 |
$sd = $d; |
|
103 |
$smo = $mo; |
|
104 |
if ($cron['week'] != '') |
|
105 |
{ |
|
106 |
$sd += $cron['week'] - $w + 7; |
|
107 |
} |
|
108 |
} |
|
109 |
if ($cron['hour']) |
|
110 |
{ |
|
111 |
$sh = $cron['hour']; |
|
112 |
if (empty($cron['day']) && $cron['week']=='') |
|
113 |
{ |
|
114 |
$sd++; |
|
115 |
} |
|
116 |
} |
|
117 |
//$next = gmmktime($sh,$sm,0,$smo,$sd,$sy); |
|
118 |
$next = local_strtotime("$sy-$smo-$sd $sh:$sm:0"); |
|
119 |
if ($next < $GLOBALS['timestamp']) |
|
120 |
{ |
|
121 |
if ($cron['m']) |
|
122 |
{ |
|
123 |
return $GLOBALS['timestamp'] + 60 - intval(local_date('s', $GLOBALS['timestamp'])); |
|
124 |
} |
|
125 |
else |
|
126 |
{ |
|
127 |
return $GLOBALS['timestamp']; |
|
128 |
} |
|
129 |
} |
|
130 |
else |
|
131 |
{ |
|
132 |
return $next; |
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
function get_cron_info() |
|
137 |
{ |
|
138 |
$crondb = array(); |
|
139 |
|
|
140 |
$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('crons') . " WHERE enable = 1 AND nextime < $GLOBALS[timestamp]"; |
|
141 |
$query = $GLOBALS['db']->query($sql); |
|
142 |
|
|
143 |
while ($rt = $GLOBALS['db']->fetch_array($query)) |
|
144 |
{ |
|
145 |
$rt['cron'] = array('day'=>$rt['day'],'week'=>$rt['week'],'m'=>$rt['minute'],'hour'=>$rt['hour']); |
|
146 |
$rt['cron_config'] = unserialize($rt['cron_config']); |
|
147 |
$rt['minute'] = trim($rt['minute']); |
|
148 |
$rt['allow_ip'] = trim($rt['allow_ip']); |
|
149 |
$crondb[] = $rt; |
|
150 |
} |
|
151 |
|
|
152 |
return $crondb; |
|
153 |
} |
|
154 |
|
|
155 |
function make_error_arr($msg,$file) |
|
156 |
{ |
|
157 |
$file = str_replace(ROOT_PATH, '' ,$file); |
|
158 |
|
|
159 |
return array('info' => $msg, 'file' => $file, 'time' => $GLOBALS['timestamp']); |
|
160 |
} |
|
161 |
|
|
162 |
function write_error_arr($err_arr) |
|
163 |
{ |
|
164 |
if (!empty($err_arr)) |
|
165 |
{ |
|
166 |
$query = ''; |
|
167 |
foreach ($err_arr AS $key => $val) |
|
168 |
{ |
|
169 |
$query .= $query ? ",('$val[info]', '$val[file]', '$val[time]')" : "('$val[info]', '$val[file]', '$val[time]')"; |
|
170 |
} |
|
171 |
if ($query) |
|
172 |
{ |
|
173 |
$sql = "INSERT INTO " . $GLOBALS['ecs']->table('error_log') . "(info, file, time) VALUES " . $query; |
|
174 |
$GLOBALS['db']->query($sql); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
|
|
179 |
function check_method() |
|
180 |
{ |
|
181 |
if (PHP_VERSION >= '4.2') |
|
182 |
{ |
|
183 |
$if_cron = PHP_SAPI == 'cli' ? true : false; |
|
184 |
} |
|
185 |
else |
|
186 |
{ |
|
187 |
$if_cron = php_sapi_name() == 'cgi' ? true : false; |
|
188 |
} |
|
189 |
if (!empty($GLOBALS['_CFG']['cron_method'])) |
|
190 |
{ |
|
191 |
if (!$if_cron) |
|
192 |
{ |
|
193 |
die('Hacking attempt'); |
|
194 |
} |
|
195 |
} |
|
196 |
else |
|
197 |
{ |
|
198 |
if ($if_cron) |
|
199 |
{ |
|
200 |
die('Hacking attempt'); |
|
201 |
} |
|
202 |
elseif (!isset($_GET['t']) || $GLOBALS['timestamp'] - $_GET['t'] > 60 || empty($_SERVER['HTTP_REFERER'])) |
|
203 |
{ |
|
204 |
exit; |
|
205 |
} |
|
206 |
} |
|
207 |
} |
|
208 |
|
|
209 |
?> |