-
Notifications
You must be signed in to change notification settings - Fork 32
/
script.php
262 lines (233 loc) · 9.63 KB
/
script.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2016 Phorum Development Team //
// http://www.phorum.org //
// //
// This program is free software. You can redistribute it and/or modify //
// it under the terms of either the current Phorum License (viewable at //
// phorum.org) or the Phorum License that was distributed with this file //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY, without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
// //
// You should have received a copy of the Phorum License //
// You should have received a copy of the Phorum License //
// along with this program. //
// //
////////////////////////////////////////////////////////////////////////////////
define('phorum_page','script');
define('PHORUM_SCRIPT', 1);
chdir(dirname(__FILE__));
require_once './common.php';
// if we are running in the webserver, bail out
if (isset($_SERVER["REMOTE_ADDR"])) {
echo $PHORUM["DATA"]["LANG"]["CannotBeRunFromBrowser"];
return;
}
// ----------------------------------------------------------------------
// Parse the command line arguments.
// ----------------------------------------------------------------------
$modules = array();
$callhook = NULL;
$callargs = array();
$args = $_SERVER["argv"];
array_shift($args);
while (count($args))
{
$arg = array_shift($args);
if (preg_match('/^--module=(.+)$/', $arg, $m)) {
if ($callhook === NULL) $callhook = 'external';
$modules[$m[1]] = $m[1];
continue;
}
if ($arg == '-m') {
if (count($arg)) {
if ($callhook === NULL) $callhook = 'external';
$mod = array_shift($args);
$modules[$mod] = $mod;
continue;
} else trigger_error(
"Missing argument for the -m option.\n"
);
}
if ($arg == '--scheduled' || $arg == '-s') {
$callhook = 'scheduled';
continue;
}
$callargs[] = $arg;
}
// At least one of --module or --scheduled is required.
// Additionally, exactly one module name is required for "external" mode.
if ($callhook === NULL || ($callhook == 'external' and count($modules) != 1)) {
echo $PHORUM["DATA"]["LANG"]["ScriptUsage"];
exit(1);
}
// ----------------------------------------------------------------------
// Filter hooks to only keep the requested external or scheduled hook(s).
// ----------------------------------------------------------------------
if (count($modules))
{
$process = $modules;
$filtered = NULL;
foreach ($PHORUM['hooks'][$callhook]['mods'] as $id => $mod) {
if (!empty($process[$mod])) {
$filtered = array(
'mods' => array( $mod ),
'funcs' => array( $PHORUM['hooks'][$callhook]['funcs'][$id] )
);
unset($process[$mod]);
break;
}
}
$PHORUM['hooks'][$callhook] = $filtered;
// If there are modules left in the list, it means that we could not
// find a registered external/scheduled hook for them.
if (count($process)) {
$mod = array_shift($process);
if (empty($PHORUM['mods'][$mod])) trigger_error(
"Requested module \"$mod\" does not exist or is not enabled.",
E_USER_ERROR
);
trigger_error(
"Requested module \"$mod\" does not implement hook \"$callhook\".",
E_USER_ERROR
);
}
}
// ----------------------------------------------------------------------
// Run the "external" hook for a module.
// ----------------------------------------------------------------------
/*
* [hook]
* external
*
* [description]
* The external hook functions are never called from any of the standard
* Phorum pages. These functions are called by invoking
* <filename>script.php</filename> on the command line with the
* <literal>--module</literal> parameter. This can be used to pipe output
* from some arbitrary command to a specific module, which can do something
* with that input. If your module does not need any command line input and
* is meant to be run on a regular basis, you should consider using the
* <hook>scheduled</hook> hook.<sbr/>
* <sbr/>
* Mind that for using an <hook>external</hook> hook, the module in which it
* is handled must be enabled in your admin interface. So if an
* <hook>external</hook> hook is not running, the containing module might be
* disabled.<sbr/>
* <sbr/>
* To run this hook from the command line, you have to be in the Phorum
* installation directory. So running the <hook>external</hook> hook of
* a module named <literal>external_foo</literal> would be done like this on
* a UNIX system prompt:
* <hookcode>
* # cd /your/phorum/dir
* # php ./script.php --module=external_foo
* </hookcode>
* For easy use, you can of course put these commands in a script file.
*
* [category]
* Miscellaneous
*
* [when]
* In the <filename>script.php</filename> when called from the command
* prompt or a script file.
*
* [input]
* Any array of arguments. (Optional)
*
* [output]
* Same as input.
*/
if ($callhook == 'external')
{
$module = array_shift($modules);
// The first argument in $callargs is set to the name of the
// called module. This module name is not really needed, but it
// in there for backward compatibility (in older code, all "external"
// hooks were called and the external hook implementation had to check
// the module name to see if it had to be run or not).
array_unshift($callargs, $module);
$callargs = array_values($callargs); // reindex (0, 1, 2, ...) array keys.
// Call the external hook.
phorum_api_hook("external", $callargs);
}
// ----------------------------------------------------------------------
// Run the "scheduled" hook for all modules.
// ----------------------------------------------------------------------
/*
* [hook]
* scheduled
*
* [description]
* <hook>scheduled</hook> hook functions are similar to
* <hook>external</hook> ones, except these functions do not require any
* input from the command line. The modules containing this hook are invoked
* by running <filename>script.php</filename> with the
* <literal>--scheduled</literal> argument (no module name is taken; this
* argument will run all scheduled hooks for all available modules).<sbr/>
* <sbr/>
* Like the name of the hook already suggests, this hook can be used for
* creating tasks which have to be executed on a regular basis. To archieve
* this, you can let <filename>script.php</filename> run from a scheduling
* service (like a cron job on a UNIX system).<sbr/>
* <sbr/>
* In general, <hook>scheduled</hook> hooks are used for automating tasks
* you want to execute without having to perform any manual action.
* Practical uses for a scheduled hook could be:
* <ul>
* <li>housekeeping (cleanup of stale/old data)</li>
* <li>daily content generation (like sending daily digests containing all
* posted messages for that day)</li>
* <li>forum statistics generation</li>
* </ul>
* Keep in mind that for using this hook, the module in which it is handled
* must be enabled in your admin interface. So if this hook is not running,
* the containing module might be disabled.<sbr/>
* <sbr/>
* To run this hook from the command line or from a scheduling service, you
* have to be in the Phorum installation directory. So running this hook for
* your Phorum installation would be done like this on a UNIX system prompt:
* <hookcode>
* # cd /your/phorum/dir
* # php ./script.php --scheduled
* </hookcode>
* When creating a scheduling service entry for running this automatically,
* remember to change the directory as well. You might also have to use the
* full path to your PHP binary (<filename>/usr/bin/php</filename> or
* whatever it is on your system), because the scheduling service might not
* know the path to it. An entry for the cron system on UNIX could look like
* this:
* <hookcode>
* 0 0 * * * cd /your/phorum/dir && /usr/bin/php ./script.php --scheduled
* </hookcode>
* Please refer to your system's documentation to see how to use your
* system's scheduling service.
*
* [category]
* Miscellaneous
*
* [when]
* In the <filename>script.php</filename> when called from the command
* prompt or a script file with the <literal>--scheduled</literal> argument.
*
* [input]
* None
*
* [output]
* None
*/
elseif ($callhook == 'scheduled')
{
phorum_api_hook('scheduled');
}
// ----------------------------------------------------------------------
// The command is not recognized. Show the usage message.
// ----------------------------------------------------------------------
else {
echo $PHORUM["DATA"]["LANG"]["ScriptUsage"];
exit(1);
}
?>