These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Elgg cron library. |
||
4 | * |
||
5 | * @package Elgg.Core |
||
6 | * @subpackage Cron |
||
7 | */ |
||
8 | |||
9 | /** |
||
10 | * Cron initialization |
||
11 | * |
||
12 | * @return void |
||
13 | * @access private |
||
14 | */ |
||
15 | function _elgg_cron_init() { |
||
16 | elgg_register_page_handler('cron', '_elgg_cron_page_handler'); |
||
17 | |||
18 | elgg_register_plugin_hook_handler('cron', 'all', '_elgg_cron_monitor', 1000); |
||
19 | |||
20 | elgg_set_config('elgg_cron_periods', [ |
||
21 | 'minute', |
||
22 | 'fiveminute', |
||
23 | 'fifteenmin', |
||
24 | 'halfhour', |
||
25 | 'hourly', |
||
26 | 'daily', |
||
27 | 'weekly', |
||
28 | 'monthly', |
||
29 | 'yearly', |
||
30 | ]); |
||
31 | |||
32 | elgg_register_menu_item('page', [ |
||
33 | 'name' => 'cron', |
||
34 | 'text' => elgg_echo('admin:cron'), |
||
35 | 'href' => 'admin/cron', |
||
36 | 'section' => 'information', |
||
37 | 'context' => 'admin', |
||
38 | ]); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Cron run |
||
43 | * |
||
44 | * This function was designed to be called every one minute from a cron job to |
||
45 | * executes each Elgg cron period at the desired interval. Will also exeute |
||
46 | * any Elgg cron period that have not fired by the expected deadline. |
||
47 | * |
||
48 | * Can be called manually by: http://YOUR.SITE/cron/run/ |
||
49 | * |
||
50 | * This will only execute cron periods at specified intervals to force execution |
||
51 | * of a specific period you will need to use http://YOUR.SITE/cron/<period>/ |
||
52 | * |
||
53 | * @access private |
||
54 | */ |
||
55 | function _elgg_cron_run() { |
||
56 | $now = time(); |
||
57 | $params = []; |
||
58 | $params['time'] = $now; |
||
59 | |||
60 | $all_std_out = ""; |
||
61 | |||
62 | $periods = [ |
||
63 | 'minute' => 60, |
||
64 | 'fiveminute' => 300, |
||
65 | 'fifteenmin' => 900, |
||
66 | 'halfhour' => 1800, |
||
67 | 'hourly' => 3600, |
||
68 | 'daily' => 86400, |
||
69 | 'weekly' => 604800, |
||
70 | 'monthly' => 2628000, |
||
71 | 'yearly' => 31536000, |
||
72 | ]; |
||
73 | |||
74 | foreach ($periods as $period => $interval) { |
||
75 | $key = "cron_latest:$period:ts"; |
||
76 | $ts = elgg_get_site_entity()->getPrivateSetting($key); |
||
77 | $deadline = $ts + $interval; |
||
78 | |||
79 | if ($now > $deadline) { |
||
80 | $msg_key = "cron_latest:$period:msg"; |
||
81 | $msg = elgg_echo('admin:cron:started', [$period, date('r', time())]); |
||
82 | elgg_get_site_entity()->setPrivateSetting($msg_key, $msg); |
||
83 | |||
84 | ob_start(); |
||
85 | |||
86 | $old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, ''); |
||
87 | $std_out = ob_get_clean(); |
||
88 | |||
89 | $period_std_out = $std_out . $old_stdout; |
||
90 | $all_std_out .= $period_std_out; |
||
91 | |||
92 | elgg_get_site_entity()->setPrivateSetting($msg_key, $period_std_out); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | echo $all_std_out; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Cron handler |
||
101 | * |
||
102 | * @param array $page Pages |
||
103 | * |
||
104 | * @return bool |
||
105 | * @throws CronException |
||
106 | * @access private |
||
107 | */ |
||
108 | function _elgg_cron_page_handler($page) { |
||
109 | if (!isset($page[0])) { |
||
110 | forward(); |
||
111 | } |
||
112 | |||
113 | if (PHP_SAPI !== 'cli' && _elgg_config()->security_protect_cron) { |
||
114 | elgg_signed_request_gatekeeper(); |
||
115 | } |
||
116 | |||
117 | $period = strtolower($page[0]); |
||
118 | |||
119 | $allowed_periods = _elgg_config()->elgg_cron_periods; |
||
120 | |||
121 | if (($period != 'run') && !in_array($period, $allowed_periods)) { |
||
122 | throw new \CronException("$period is not a recognized cron period."); |
||
123 | } |
||
124 | |||
125 | if ($period == 'run') { |
||
126 | _elgg_cron_run(); |
||
127 | } else { |
||
128 | // Get a list of parameters |
||
129 | $params = []; |
||
130 | $params['time'] = time(); |
||
131 | |||
132 | // Data to return to |
||
133 | $old_stdout = ""; |
||
134 | ob_start(); |
||
135 | |||
136 | $msg_key = "cron_latest:$period:msg"; |
||
137 | $msg = elgg_echo('admin:cron:started', [$period, date('r', time())]); |
||
138 | elgg_get_site_entity()->setPrivateSetting($msg_key, $msg); |
||
139 | |||
140 | $old_stdout = elgg_trigger_plugin_hook('cron', $period, $params, $old_stdout); |
||
141 | $std_out = ob_get_clean(); |
||
142 | |||
143 | $msg = $std_out . $old_stdout; |
||
144 | echo $msg; |
||
145 | |||
146 | elgg_get_site_entity()->setPrivateSetting($msg_key, $msg); |
||
147 | } |
||
148 | return true; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Record cron running |
||
153 | * |
||
154 | * @param string $hook Hook name |
||
155 | * @param string $period Cron period |
||
156 | * @param string $output Output content |
||
157 | * @param array $params Hook parameters |
||
158 | * @return void |
||
159 | * @access private |
||
160 | */ |
||
161 | function _elgg_cron_monitor($hook, $period, $output, $params) { |
||
162 | $time = $params['time']; |
||
163 | $periods = _elgg_config()->elgg_cron_periods; |
||
164 | |||
165 | if (in_array($period, $periods)) { |
||
166 | $key = "cron_latest:$period:ts"; |
||
167 | elgg_get_site_entity()->setPrivateSetting($key, $time); |
||
168 | echo elgg_echo('admin:cron:complete', [$period, date('r', $time)]); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) { |
||
0 ignored issues
–
show
|
|||
173 | $events->registerHandler('init', 'system', '_elgg_cron_init'); |
||
174 | }; |
||
175 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.