PageRenderTime 33ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/public/forum/script.php

https://gitlab.com/sheldonels/dejavu
PHP | 263 lines | 85 code | 25 blank | 153 comment | 26 complexity | 53f606fda8422b61747d70a68991be79 MD5 | raw file
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // Copyright (C) 2007 Phorum Development Team //
  5. // http://www.phorum.org //
  6. // //
  7. // This program is free software. You can redistribute it and/or modify //
  8. // it under the terms of either the current Phorum License (viewable at //
  9. // phorum.org) or the Phorum License that was distributed with this file //
  10. // //
  11. // This program is distributed in the hope that it will be useful, //
  12. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  14. // //
  15. // You should have received a copy of the Phorum License //
  16. // You should have received a copy of the Phorum License //
  17. // along with this program. //
  18. // //
  19. ////////////////////////////////////////////////////////////////////////////////
  20. define('phorum_page','script');
  21. define('PHORUM_SCRIPT', 1);
  22. chdir(dirname(__FILE__));
  23. include_once("./common.php");
  24. // if we are running in the webserver, bail out
  25. if (isset($_SERVER["REMOTE_ADDR"])) {
  26. echo $PHORUM["DATA"]["LANG"]["CannotBeRunFromBrowser"];
  27. return;
  28. }
  29. // ----------------------------------------------------------------------
  30. // Parse the command line arguments.
  31. // ----------------------------------------------------------------------
  32. $modules = array();
  33. $callhook = NULL;
  34. $callargs = array();
  35. $args = $_SERVER["argv"];
  36. array_shift($args);
  37. while (count($args))
  38. {
  39. $arg = array_shift($args);
  40. if (preg_match('/^--module=(.+)$/', $arg, $m)) {
  41. if ($callhook === NULL) $callhook = 'external';
  42. $modules[$m[1]] = $m[1];
  43. continue;
  44. }
  45. if ($arg == '-m') {
  46. if (count($arg)) {
  47. if ($callhook === NULL) $callhook = 'external';
  48. $mod = array_shift($args);
  49. $modules[$mod] = $mod;
  50. continue;
  51. } else trigger_error(
  52. "Missing argument for the -m option.\n"
  53. );
  54. }
  55. if ($arg == '--scheduled' || $arg == '-s') {
  56. $callhook = 'scheduled';
  57. continue;
  58. }
  59. $callargs[] = $arg;
  60. }
  61. // At least one of --module or --scheduled is required.
  62. // Additionally, exactly one module name is required for "external" mode.
  63. if ($callhook === NULL || ($callhook == 'external' and count($modules) != 1)) {
  64. echo $GLOBALS["PHORUM"]["DATA"]["LANG"]["ScriptUsage"];
  65. exit(1);
  66. }
  67. // ----------------------------------------------------------------------
  68. // Filter hooks to only keep the requested external or scheduled hook(s).
  69. // ----------------------------------------------------------------------
  70. if (count($modules))
  71. {
  72. $process = $modules;
  73. $filtered = NULL;
  74. foreach ($PHORUM['hooks'][$callhook]['mods'] as $id => $mod) {
  75. if (!empty($process[$mod])) {
  76. $filtered = array(
  77. 'mods' => array( $mod ),
  78. 'funcs' => array( $PHORUM['hooks'][$callhook]['funcs'][$id] )
  79. );
  80. unset($process[$mod]);
  81. break;
  82. }
  83. }
  84. $PHORUM['hooks'][$callhook] = $filtered;
  85. // If there are modules left in the list, it means that we could not
  86. // find a registered external/scheduled hook for them.
  87. if (count($process)) {
  88. $mod = array_shift($process);
  89. if (empty($PHORUM['mods'][$mod])) trigger_error(
  90. "Requested module \"$mod\" does not exist or is not enabled.",
  91. E_USER_ERROR
  92. );
  93. trigger_error(
  94. "Requested module \"$mod\" does not implement hook \"$callhook\".",
  95. E_USER_ERROR
  96. );
  97. }
  98. }
  99. // ----------------------------------------------------------------------
  100. // Run the "external" hook for a module.
  101. // ----------------------------------------------------------------------
  102. /*
  103. * [hook]
  104. * external
  105. *
  106. * [description]
  107. * The external hook functions are never called from any of the standard
  108. * Phorum pages. These functions are called by invoking
  109. * <filename>script.php</filename> on the command line with the
  110. * <literal>--module</literal> parameter. This can be used to pipe output
  111. * from some arbitrary command to a specific module, which can do something
  112. * with that input. If your module does not need any command line input and
  113. * is meant to be run on a regular basis, you should consider using the
  114. * <hook>scheduled</hook> hook.<sbr/>
  115. * <sbr/>
  116. * Mind that for using an <hook>external</hook> hook, the module in which it
  117. * is handled must be enabled in your admin interface. So if an
  118. * <hook>external</hook> hook is not running, the containing module might be
  119. * disabled.<sbr/>
  120. * <sbr/>
  121. * To run this hook from the command line, you have to be in the Phorum
  122. * installation directory. So running the <hook>external</hook> hook of
  123. * a module named <literal>external_foo</literal> would be done like this on
  124. * a UNIX system prompt:
  125. * <hookcode>
  126. * # cd /your/phorum/dir
  127. * # php ./script.php --module=external_foo
  128. * </hookcode>
  129. * For easy use, you can of course put these commands in a script file.
  130. *
  131. * [category]
  132. * Miscellaneous
  133. *
  134. * [when]
  135. * In the <filename>script.php</filename> when called from the command
  136. * prompt or a script file.
  137. *
  138. * [input]
  139. * Any array of arguments. (Optional)
  140. *
  141. * [output]
  142. * None
  143. *
  144. */
  145. if ($callhook == 'external')
  146. {
  147. $module = array_shift($modules);
  148. // The first argument in $callargs is set to the name of the
  149. // called module. This module name is not really needed, but it
  150. // in there for backward compatibility (in older code, all "external"
  151. // hooks were called and the external hook implementation had to check
  152. // the module name to see if it had to be run or not).
  153. array_unshift($callargs, $module);
  154. $callargs = array_values($callargs); // reindex (0, 1, 2, ...) array keys.
  155. // Call the external hook.
  156. phorum_hook("external", $callargs);
  157. }
  158. // ----------------------------------------------------------------------
  159. // Run the "scheduled" hook for all modules.
  160. // ----------------------------------------------------------------------
  161. /*
  162. * [hook]
  163. * scheduled
  164. *
  165. * [description]
  166. * <hook>Scheduled</hook> hook functions are similar to
  167. * <hook>external</hook> ones, except these functions do not require any
  168. * input from the command line. The modules containing this hook are invoked
  169. * by running <filename>script.php</filename> with the
  170. * <literal>--scheduled</literal> argument (no module name is taken; this
  171. * argument will run all scheduled hooks for all available modules).<sbr/>
  172. * <sbr/>
  173. * Like the name of the hook already suggests, this hook can be used for
  174. * creating tasks which have to be executed on a regular basis. To archieve
  175. * this, you can let <filename>script.php</filename> run from a scheduling
  176. * service (like a cron job on a UNIX system).<sbr/>
  177. * <sbr/>
  178. * In general, <hook>scheduled</hook> hooks are used for automating tasks
  179. * you want to execute without having to perform any manual action.
  180. * Practical uses for a scheduled hook could be:
  181. * <ul>
  182. * <li>housekeeping (cleanup of stale/old data)</li>
  183. * <li>daily content generation (like sending daily digests containing all
  184. * posted messages for that day)</li>
  185. * <li>forum statistics generation</li>
  186. * </ul>
  187. * <sbr/>
  188. * Keep in mind that for using this hook, the module in which it is handled
  189. * must be enabled in your admin interface. So if this hook is not running,
  190. * the containing module might be disabled.<sbr/>
  191. * <sbr/>
  192. * To run this hook from the command line or from a scheduling service, you
  193. * have to be in the Phorum installation directory. So running this hook for
  194. * your Phorum installation would be done like this on a UNIX system prompt:
  195. * <hookcode>
  196. * # cd /your/phorum/dir
  197. * # php ./script.php --scheduled
  198. * </hookcode>
  199. * <sbr/>
  200. * When creating a scheduling service entry for running this automatically,
  201. * remember to change the directory as well. You might also have to use the
  202. * full path to your PHP binary (<filename>/usr/bin/php</filename> or
  203. * whatever it is on your system), because the scheduling service might not
  204. * know the path to it. An entry for the cron system on UNIX could look like
  205. * this:
  206. * <hookcode>
  207. * 0 0 * * * cd /your/phorum/dir && /usr/bin/php ./script.php --scheduled
  208. * </hookcode>
  209. * <sbr/>
  210. * Please refer to your system's documentation to see how to use your
  211. * system's scheduling service.
  212. *
  213. * [category]
  214. * Miscellaneous
  215. *
  216. * [when]
  217. * In the <filename>script.php</filename> when called from the command
  218. * prompt or a script file with the <literal>--scheduled</literal> argument.
  219. *
  220. * [input]
  221. * None
  222. *
  223. * [output]
  224. * None
  225. *
  226. */
  227. elseif ($callhook == 'scheduled')
  228. {
  229. phorum_hook('scheduled');
  230. }
  231. // ----------------------------------------------------------------------
  232. // The command is not recognized. Show the usage message.
  233. // ----------------------------------------------------------------------
  234. else {
  235. echo $GLOBALS["PHORUM"]["DATA"]["LANG"]["ScriptUsage"];
  236. exit(1);
  237. }
  238. ?>