commit | author | age
|
8bd4d9
|
1 |
<?php |
JK |
2 |
// Skrypt aktualizujący dane pobierane okresowo. |
|
3 |
|
|
4 |
define('MAINSTAR', ' '."\033".'[1;37m*'.' '); |
|
5 |
define('NORMAL', "\033".'[0m'); |
|
6 |
define('STAR', ' '."\033".'[1;34m*'."\033".'[0m '); |
|
7 |
define('OK', "\033[55G\033[32m".'[ OK ]'."\033[0m\n"); |
|
8 |
define('NOT', "\033[55G\033[33m".'[ OK ]'."\033[0m\n"); |
|
9 |
define('FAIL', "\033[55G\033[31m".'[ FAIL ]'."\033[0m\n"); |
|
10 |
|
|
11 |
echo "\033".'[1;37;44m Bot GG - Skrypt aktualizujący ('.date('d.m.Y H:i:s').')'."\033".'[K'."\033".'[0m'."\n\n"; |
|
12 |
|
|
13 |
function crontab_field($field, $number) { |
|
14 |
$field = explode(',', $field); |
|
15 |
foreach($field as $one) { |
|
16 |
$mod = FALSE; |
|
17 |
$range_start = 0; |
|
18 |
$range_stop = 0; |
|
19 |
|
|
20 |
if(($pos=strpos($one, '/'))!==FALSE) { |
|
21 |
$mod = (int)substr($one, $pos+1); |
|
22 |
if($mod==0) { |
|
23 |
continue; |
|
24 |
} |
|
25 |
$one = substr($one, 0, $pos); |
|
26 |
} |
|
27 |
|
|
28 |
if($one != '*') { |
|
29 |
if(($pos=strpos($one, '-'))!==FALSE) { |
|
30 |
$range_start = (int)substr($one, 0, $pos); |
|
31 |
$range_stop = (int)substr($one, $pos+1); |
|
32 |
} |
|
33 |
else |
|
34 |
{ |
|
35 |
$range_start = $one; |
|
36 |
$range_stop = $one; |
|
37 |
} |
|
38 |
|
|
39 |
if($range_start > $number OR $range_stop < $number) { |
|
40 |
continue; |
|
41 |
} |
|
42 |
} |
|
43 |
|
|
44 |
if($mod && ($number-$range_start)%$mod != 0) { |
|
45 |
continue; |
|
46 |
} |
|
47 |
|
|
48 |
return TRUE; |
|
49 |
} |
|
50 |
|
|
51 |
return FALSE; |
|
52 |
} |
|
53 |
|
|
54 |
function crontab_match($line) { |
|
55 |
$parts = preg_split('/[\40\t]+/', $line, 6); |
|
56 |
// Minutes part - skip |
|
57 |
|
|
58 |
// Hour part |
|
59 |
if(!crontab_field($parts[1], date('H'))) { |
|
60 |
return FALSE; |
|
61 |
} |
|
62 |
|
|
63 |
// Day part |
|
64 |
if(!crontab_field($parts[2], date('j'))) { |
|
65 |
return FALSE; |
|
66 |
} |
|
67 |
|
|
68 |
// Month part |
|
69 |
if(!crontab_field($parts[3], date('n'))) { |
|
70 |
return FALSE; |
|
71 |
} |
|
72 |
|
|
73 |
// Weekday part |
|
74 |
if(!crontab_field($parts[4], date('w'))) { |
|
75 |
return FALSE; |
|
76 |
} |
|
77 |
|
|
78 |
return $parts[5]; |
|
79 |
} |
|
80 |
|
|
81 |
function launch($file) { |
|
82 |
return include($file); |
|
83 |
} |
|
84 |
|
|
85 |
function crontab_parse($dir) { |
|
86 |
chdir($dir); |
|
87 |
|
|
88 |
$done = FALSE; |
|
89 |
|
|
90 |
$file = file('crontab'); |
|
91 |
foreach($file as $line) { |
|
92 |
$line = trim($line); |
|
93 |
if(empty($line) || substr($line, 0, 1)=='#') continue; |
|
94 |
|
|
95 |
$ret = crontab_match($line); |
|
96 |
if($ret) { |
|
97 |
if(!$done) |
|
98 |
echo "\n"; |
|
99 |
launch($ret); |
|
100 |
$done = TRUE; |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
if(!$done) { |
|
105 |
echo NOT; |
|
106 |
} |
|
107 |
|
|
108 |
chdir('..'); |
|
109 |
} |
|
110 |
|
|
111 |
chdir(dirname(__FILE__)); |
|
112 |
|
|
113 |
$dirs = glob('./*', GLOB_ONLYDIR); |
|
114 |
foreach($dirs as $dir) { |
|
115 |
if(file_exists($dir.'/crontab')) { |
|
116 |
echo MAINSTAR.'Moduł '.basename($dir).NORMAL; |
|
117 |
crontab_parse($dir); |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
echo "\n"; |
|
122 |
?> |