elgglib.php 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  1. <?php
  2. /**
  3. * Bootstrapping and helper procedural code available for use in Elgg core and plugins.
  4. *
  5. * @package Elgg.Core
  6. * @todo These functions can't be subpackaged because they cover a wide mix of
  7. * purposes and subsystems. Many of them should be moved to more relevant files.
  8. */
  9. /**
  10. * Register a PHP file as a library.
  11. *
  12. * @see elgg_load_library
  13. *
  14. * @param string $name The name of the library
  15. * @param string $location The location of the file
  16. *
  17. * @return void
  18. * @since 1.8.0
  19. */
  20. function elgg_register_library($name, $location) {
  21. $config = _elgg_services()->config;
  22. $libraries = $config->get('libraries');
  23. if ($libraries === null) {
  24. $libraries = array();
  25. }
  26. $libraries[$name] = $location;
  27. $config->set('libraries', $libraries);
  28. }
  29. /**
  30. * Load a PHP library.
  31. *
  32. * @see elgg_register_library
  33. *
  34. * @param string $name The name of the library
  35. *
  36. * @return void
  37. * @throws InvalidParameterException
  38. * @since 1.8.0
  39. */
  40. function elgg_load_library($name) {
  41. static $loaded_libraries = array();
  42. if (in_array($name, $loaded_libraries)) {
  43. return;
  44. }
  45. $libraries = _elgg_services()->config->get('libraries');
  46. if (!isset($libraries[$name])) {
  47. $error = "$name is not a registered library";
  48. throw new \InvalidParameterException($error);
  49. }
  50. if (!include_once($libraries[$name])) {
  51. $error = "Could not load the $name library from {$libraries[$name]}";
  52. throw new \InvalidParameterException($error);
  53. }
  54. $loaded_libraries[] = $name;
  55. }
  56. /**
  57. * Forward to $location.
  58. *
  59. * Sends a 'Location: $location' header and exists. If headers have
  60. * already been sent, throws an exception.
  61. *
  62. * @param string $location URL to forward to browser to. This can be a path
  63. * relative to the network's URL.
  64. * @param string $reason Short explanation for why we're forwarding. Set to
  65. * '404' to forward to error page. Default message is
  66. * 'system'.
  67. *
  68. * @return void
  69. * @throws SecurityException
  70. */
  71. function forward($location = "", $reason = 'system') {
  72. if (!headers_sent($file, $line)) {
  73. if ($location === REFERER) {
  74. $location = _elgg_services()->request->headers->get('Referer');
  75. }
  76. $location = elgg_normalize_url($location);
  77. // return new forward location or false to stop the forward or empty string to exit
  78. $current_page = current_page_url();
  79. $params = array('current_url' => $current_page, 'forward_url' => $location);
  80. $location = elgg_trigger_plugin_hook('forward', $reason, $params, $location);
  81. if ($location) {
  82. header("Location: {$location}");
  83. }
  84. exit;
  85. } else {
  86. throw new \SecurityException("Redirect could not be issued due to headers already being sent. Halting execution for security. "
  87. . "Output started in file $file at line $line. Search http://learn.elgg.org/ for more information.");
  88. }
  89. }
  90. /**
  91. * Register a JavaScript file for inclusion
  92. *
  93. * This function handles adding JavaScript to a web page. If multiple
  94. * calls are made to register the same JavaScript file based on the $id
  95. * variable, only the last file is included. This allows a plugin to add
  96. * JavaScript from a view that may be called more than once. It also handles
  97. * more than one plugin adding the same JavaScript.
  98. *
  99. * jQuery plugins often have filenames such as jquery.rating.js. A best practice
  100. * is to base $name on the filename: "jquery.rating". It is recommended to not
  101. * use version numbers in the name.
  102. *
  103. * The JavaScript files can be local to the server or remote (such as
  104. * Google's CDN).
  105. *
  106. * @param string $name An identifier for the JavaScript library
  107. * @param string $url URL of the JavaScript file
  108. * @param string $location Page location: head or footer. (default: head)
  109. * @param int $priority Priority of the JS file (lower numbers load earlier)
  110. *
  111. * @return bool
  112. * @since 1.8.0
  113. */
  114. function elgg_register_js($name, $url, $location = 'head', $priority = null) {
  115. return elgg_register_external_file('js', $name, $url, $location, $priority);
  116. }
  117. /**
  118. * Defines a JS lib as an AMD module. This is useful for shimming
  119. * traditional JS or for setting the paths of AMD modules.
  120. *
  121. * Calling multiple times for the same name will:
  122. * * set the preferred path to the last call setting a path
  123. * * overwrite the shimmed AMD modules with the last call setting a shimmed module
  124. *
  125. * Use elgg_require_js($name) to load on the current page.
  126. *
  127. * Calling this function is not needed if your JS are in views named like `js/module/name.js`
  128. * Instead, simply call elgg_require_js("module/name").
  129. *
  130. * @param string $name The module name
  131. * @param array $config An array like the following:
  132. * array 'deps' An array of AMD module dependencies
  133. * string 'exports' The name of the exported module
  134. * string 'src' The URL to the JS. Can be relative.
  135. *
  136. * @return void
  137. */
  138. function elgg_define_js($name, $config) {
  139. $src = elgg_extract('src', $config);
  140. if ($src) {
  141. $url = elgg_normalize_url($src);
  142. _elgg_services()->amdConfig->addPath($name, $url);
  143. }
  144. // shimmed module
  145. if (isset($config['deps']) || isset($config['exports'])) {
  146. _elgg_services()->amdConfig->addShim($name, $config);
  147. }
  148. }
  149. /**
  150. * Unregister a JavaScript file
  151. *
  152. * @param string $name The identifier for the JavaScript library
  153. *
  154. * @return bool
  155. * @since 1.8.0
  156. */
  157. function elgg_unregister_js($name) {
  158. return elgg_unregister_external_file('js', $name);
  159. }
  160. /**
  161. * Load a JavaScript resource on this page
  162. *
  163. * This must be called before elgg_view_page(). It can be called before the
  164. * script is registered. If you do not want a script loaded, unregister it.
  165. *
  166. * @param string $name Identifier of the JavaScript resource
  167. *
  168. * @return void
  169. * @since 1.8.0
  170. */
  171. function elgg_load_js($name) {
  172. elgg_load_external_file('js', $name);
  173. }
  174. /**
  175. * Request that Elgg load an AMD module onto the page.
  176. *
  177. * @param string $name The AMD module name.
  178. * @return void
  179. * @since 1.9.0
  180. */
  181. function elgg_require_js($name) {
  182. _elgg_services()->amdConfig->addDependency($name);
  183. }
  184. /**
  185. * Get the JavaScript URLs that are loaded
  186. *
  187. * @param string $location 'head' or 'footer'
  188. *
  189. * @return array
  190. * @since 1.8.0
  191. */
  192. function elgg_get_loaded_js($location = 'head') {
  193. return elgg_get_loaded_external_files('js', $location);
  194. }
  195. /**
  196. * Register a CSS file for inclusion in the HTML head
  197. *
  198. * @param string $name An identifier for the CSS file
  199. * @param string $url URL of the CSS file
  200. * @param int $priority Priority of the CSS file (lower numbers load earlier)
  201. *
  202. * @return bool
  203. * @since 1.8.0
  204. */
  205. function elgg_register_css($name, $url, $priority = null) {
  206. return elgg_register_external_file('css', $name, $url, 'head', $priority);
  207. }
  208. /**
  209. * Unregister a CSS file
  210. *
  211. * @param string $name The identifier for the CSS file
  212. *
  213. * @return bool
  214. * @since 1.8.0
  215. */
  216. function elgg_unregister_css($name) {
  217. return elgg_unregister_external_file('css', $name);
  218. }
  219. /**
  220. * Load a CSS file for this page
  221. *
  222. * This must be called before elgg_view_page(). It can be called before the
  223. * CSS file is registered. If you do not want a CSS file loaded, unregister it.
  224. *
  225. * @param string $name Identifier of the CSS file
  226. *
  227. * @return void
  228. * @since 1.8.0
  229. */
  230. function elgg_load_css($name) {
  231. elgg_load_external_file('css', $name);
  232. }
  233. /**
  234. * Get the loaded CSS URLs
  235. *
  236. * @return array
  237. * @since 1.8.0
  238. */
  239. function elgg_get_loaded_css() {
  240. return elgg_get_loaded_external_files('css', 'head');
  241. }
  242. /**
  243. * Core registration function for external files
  244. *
  245. * @param string $type Type of external resource (js or css)
  246. * @param string $name Identifier used as key
  247. * @param string $url URL
  248. * @param string $location Location in the page to include the file
  249. * @param int $priority Loading priority of the file
  250. *
  251. * @return bool
  252. * @since 1.8.0
  253. */
  254. function elgg_register_external_file($type, $name, $url, $location, $priority = 500) {
  255. return _elgg_services()->externalFiles->register($type, $name, $url, $location, $priority);
  256. }
  257. /**
  258. * Unregister an external file
  259. *
  260. * @param string $type Type of file: js or css
  261. * @param string $name The identifier of the file
  262. *
  263. * @return bool
  264. * @since 1.8.0
  265. */
  266. function elgg_unregister_external_file($type, $name) {
  267. return _elgg_services()->externalFiles->unregister($type, $name);
  268. }
  269. /**
  270. * Load an external resource for use on this page
  271. *
  272. * @param string $type Type of file: js or css
  273. * @param string $name The identifier for the file
  274. *
  275. * @return void
  276. * @since 1.8.0
  277. */
  278. function elgg_load_external_file($type, $name) {
  279. return _elgg_services()->externalFiles->load($type, $name);
  280. }
  281. /**
  282. * Get external resource descriptors
  283. *
  284. * @param string $type Type of file: js or css
  285. * @param string $location Page location
  286. *
  287. * @return array
  288. * @since 1.8.0
  289. */
  290. function elgg_get_loaded_external_files($type, $location) {
  291. return _elgg_services()->externalFiles->getLoadedFiles($type, $location);
  292. }
  293. /**
  294. * Returns a list of files in $directory.
  295. *
  296. * Only returns files. Does not recurse into subdirs.
  297. *
  298. * @param string $directory Directory to look in
  299. * @param array $exceptions Array of filenames to ignore
  300. * @param array $list Array of files to append to
  301. * @param mixed $extensions Array of extensions to allow, null for all. Use a dot: array('.php').
  302. *
  303. * @return array Filenames in $directory, in the form $directory/filename.
  304. */
  305. function elgg_get_file_list($directory, $exceptions = array(), $list = array(),
  306. $extensions = null) {
  307. $directory = sanitise_filepath($directory);
  308. if ($handle = opendir($directory)) {
  309. while (($file = readdir($handle)) !== false) {
  310. if (!is_file($directory . $file) || in_array($file, $exceptions)) {
  311. continue;
  312. }
  313. if (is_array($extensions)) {
  314. if (in_array(strrchr($file, '.'), $extensions)) {
  315. $list[] = $directory . $file;
  316. }
  317. } else {
  318. $list[] = $directory . $file;
  319. }
  320. }
  321. closedir($handle);
  322. }
  323. return $list;
  324. }
  325. /**
  326. * Sanitise file paths ensuring that they begin and end with slashes etc.
  327. *
  328. * @param string $path The path
  329. * @param bool $append_slash Add tailing slash
  330. *
  331. * @return string
  332. */
  333. function sanitise_filepath($path, $append_slash = true) {
  334. // Convert to correct UNIX paths
  335. $path = str_replace('\\', '/', $path);
  336. $path = str_replace('../', '/', $path);
  337. // replace // with / except when preceeded by :
  338. $path = preg_replace("/([^:])\/\//", "$1/", $path);
  339. // Sort trailing slash
  340. $path = trim($path);
  341. // rtrim defaults plus /
  342. $path = rtrim($path, " \n\t\0\x0B/");
  343. if ($append_slash) {
  344. $path = $path . '/';
  345. }
  346. return $path;
  347. }
  348. /**
  349. * Queues a message to be displayed.
  350. *
  351. * Messages will not be displayed immediately, but are stored in
  352. * for later display, usually upon next page load.
  353. *
  354. * The method of displaying these messages differs depending upon plugins and
  355. * viewtypes. The core default viewtype retrieves messages in
  356. * {@link views/default/page/shells/default.php} and displays messages as
  357. * javascript popups.
  358. *
  359. * @note Internal: Messages are stored as strings in the Elgg session as ['msg'][$register] array.
  360. *
  361. * @warning This function is used to both add to and clear the message
  362. * stack. If $messages is null, $register will be returned and cleared.
  363. * If $messages is null and $register is empty, all messages will be
  364. * returned and removed.
  365. *
  366. * @param mixed $message Optionally, a single message or array of messages to add, (default: null)
  367. * @param string $register Types of message: "error", "success" (default: success)
  368. * @param bool $count Count the number of messages (default: false)
  369. *
  370. * @return bool|array Either the array of messages, or a response regarding
  371. * whether the message addition was successful.
  372. */
  373. function system_messages($message = null, $register = "success", $count = false) {
  374. if ($count) {
  375. return _elgg_services()->systemMessages->count($register);
  376. }
  377. if ($message === null) {
  378. return _elgg_services()->systemMessages->dumpRegister($register);
  379. }
  380. return _elgg_services()->systemMessages->addMessageToRegister($message, $register);
  381. }
  382. /**
  383. * Counts the number of messages, either globally or in a particular register
  384. *
  385. * @param string $register Optionally, the register
  386. *
  387. * @return integer The number of messages
  388. */
  389. function count_messages($register = "") {
  390. return _elgg_services()->systemMessages->count($register);
  391. }
  392. /**
  393. * Display a system message on next page load.
  394. *
  395. * @see system_messages()
  396. *
  397. * @param string|array $message Message or messages to add
  398. *
  399. * @return bool
  400. */
  401. function system_message($message) {
  402. return _elgg_services()->systemMessages->addSuccessMessage($message);
  403. }
  404. /**
  405. * Display an error on next page load.
  406. *
  407. * @see system_messages()
  408. *
  409. * @param string|array $error Error or errors to add
  410. *
  411. * @return bool
  412. */
  413. function register_error($error) {
  414. return _elgg_services()->systemMessages->addErrorMessage($error);
  415. }
  416. /**
  417. * Register a callback as an Elgg event handler.
  418. *
  419. * Events are emitted by Elgg when certain actions occur. Plugins
  420. * can respond to these events or halt them completely by registering a handler
  421. * as a callback to an event. Multiple handlers can be registered for
  422. * the same event and will be executed in order of $priority.
  423. *
  424. * For most events, any handler returning false will halt the execution chain and
  425. * cause the event to be "cancelled". For After Events, the return values of the
  426. * handlers will be ignored and all handlers will be called.
  427. *
  428. * This function is called with the event name, event type, and handler callback name.
  429. * Setting the optional $priority allows plugin authors to specify when the
  430. * callback should be run. Priorities for plugins should be 1-1000.
  431. *
  432. * The callback is passed 3 arguments when called: $event, $type, and optional $params.
  433. *
  434. * $event is the name of event being emitted.
  435. * $type is the type of event or object concerned.
  436. * $params is an optional parameter passed that can include a related object. See
  437. * specific event documentation for details on which events pass what parameteres.
  438. *
  439. * @tip If a priority isn't specified it is determined by the order the handler was
  440. * registered relative to the event and type. For plugins, this generally means
  441. * the earlier the plugin is in the load order, the earlier the priorities are for
  442. * any event handlers.
  443. *
  444. * @tip $event and $object_type can use the special keyword 'all'. Handler callbacks registered
  445. * with $event = all will be called for all events of type $object_type. Similarly,
  446. * callbacks registered with $object_type = all will be called for all events of type
  447. * $event, regardless of $object_type. If $event and $object_type both are 'all', the
  448. * handler callback will be called for all events.
  449. *
  450. * @tip Event handler callbacks are considered in the follow order:
  451. * - Specific registration where 'all' isn't used.
  452. * - Registration where 'all' is used for $event only.
  453. * - Registration where 'all' is used for $type only.
  454. * - Registration where 'all' is used for both.
  455. *
  456. * @warning If you use the 'all' keyword, you must have logic in the handler callback to
  457. * test the passed parameters before taking an action.
  458. *
  459. * @tip When referring to events, the preferred syntax is "event, type".
  460. *
  461. * Internal note: Events are stored in $CONFIG->events as:
  462. * <code>
  463. * $CONFIG->events[$event][$type][$priority] = $callback;
  464. * </code>
  465. *
  466. * @param string $event The event type
  467. * @param string $object_type The object type
  468. * @param string $callback The handler callback
  469. * @param int $priority The priority - 0 is default, negative before, positive after
  470. *
  471. * @return bool
  472. * @example documentation/events/basic.php
  473. * @example documentation/events/advanced.php
  474. * @example documentation/events/all.php
  475. */
  476. function elgg_register_event_handler($event, $object_type, $callback, $priority = 500) {
  477. return _elgg_services()->events->registerHandler($event, $object_type, $callback, $priority);
  478. }
  479. /**
  480. * Unregisters a callback for an event.
  481. *
  482. * @param string $event The event type
  483. * @param string $object_type The object type
  484. * @param string $callback The callback. Since 1.11, static method callbacks will match dynamic methods
  485. *
  486. * @return bool true if a handler was found and removed
  487. * @since 1.7
  488. */
  489. function elgg_unregister_event_handler($event, $object_type, $callback) {
  490. return _elgg_services()->events->unregisterHandler($event, $object_type, $callback);
  491. }
  492. /**
  493. * Trigger an Elgg Event and attempt to run all handler callbacks registered to that
  494. * event, type.
  495. *
  496. * This function attempts to run all handlers registered to $event, $object_type or
  497. * the special keyword 'all' for either or both. If a handler returns false, the
  498. * event will be cancelled (no further handlers will be called, and this function
  499. * will return false).
  500. *
  501. * $event is usually a verb: create, update, delete, annotation.
  502. *
  503. * $object_type is usually a noun: object, group, user, annotation, relationship, metadata.
  504. *
  505. * $object is usually an Elgg* object associated with the event.
  506. *
  507. * @warning Elgg events should only be triggered by core. Plugin authors should use
  508. * {@link trigger_elgg_plugin_hook()} instead.
  509. *
  510. * @tip When referring to events, the preferred syntax is "event, type".
  511. *
  512. * @note Internal: Only rarely should events be changed, added, or removed in core.
  513. * When making changes to events, be sure to first create a ticket on Github.
  514. *
  515. * @note Internal: @tip Think of $object_type as the primary namespace element, and
  516. * $event as the secondary namespace.
  517. *
  518. * @param string $event The event type
  519. * @param string $object_type The object type
  520. * @param string $object The object involved in the event
  521. *
  522. * @return bool False if any handler returned false, otherwise true.
  523. * @example documentation/examples/events/trigger.php
  524. */
  525. function elgg_trigger_event($event, $object_type, $object = null) {
  526. return _elgg_services()->events->trigger($event, $object_type, $object);
  527. }
  528. /**
  529. * Trigger a "Before event" indicating a process is about to begin.
  530. *
  531. * Like regular events, a handler returning false will cancel the process and false
  532. * will be returned.
  533. *
  534. * To register for a before event, append ":before" to the event name when registering.
  535. *
  536. * @param string $event The event type. The fired event type will be appended with ":before".
  537. * @param string $object_type The object type
  538. * @param string $object The object involved in the event
  539. *
  540. * @return bool False if any handler returned false, otherwise true
  541. *
  542. * @see elgg_trigger_event
  543. * @see elgg_trigger_after_event
  544. */
  545. function elgg_trigger_before_event($event, $object_type, $object = null) {
  546. return _elgg_services()->events->trigger("$event:before", $object_type, $object);
  547. }
  548. /**
  549. * Trigger an "After event" indicating a process has finished.
  550. *
  551. * Unlike regular events, all the handlers will be called, their return values ignored.
  552. *
  553. * To register for an after event, append ":after" to the event name when registering.
  554. *
  555. * @param string $event The event type. The fired event type will be appended with ":after".
  556. * @param string $object_type The object type
  557. * @param string $object The object involved in the event
  558. *
  559. * @return true
  560. *
  561. * @see elgg_trigger_before_event
  562. */
  563. function elgg_trigger_after_event($event, $object_type, $object = null) {
  564. $options = array(
  565. \Elgg\EventsService::OPTION_STOPPABLE => false,
  566. );
  567. return _elgg_services()->events->trigger("$event:after", $object_type, $object, $options);
  568. }
  569. /**
  570. * Trigger an event normally, but send a notice about deprecated use if any handlers are registered.
  571. *
  572. * @param string $event The event type
  573. * @param string $object_type The object type
  574. * @param string $object The object involved in the event
  575. * @param string $message The deprecation message
  576. * @param string $version Human-readable *release* version: 1.9, 1.10, ...
  577. *
  578. * @return bool
  579. *
  580. * @see elgg_trigger_event
  581. */
  582. function elgg_trigger_deprecated_event($event, $object_type, $object = null, $message, $version) {
  583. $options = array(
  584. \Elgg\EventsService::OPTION_DEPRECATION_MESSAGE => $message,
  585. \Elgg\EventsService::OPTION_DEPRECATION_VERSION => $version,
  586. );
  587. return _elgg_services()->events->trigger($event, $object_type, $object, $options);
  588. }
  589. /**
  590. * Register a callback as a plugin hook handler.
  591. *
  592. * Plugin hooks allow developers to losely couple plugins and features by
  593. * responding to and emitting {@link elgg_trigger_plugin_hook()} customizable hooks.
  594. * Handler callbacks can respond to the hook, change the details of the hook, or
  595. * ignore it.
  596. *
  597. * Multiple handlers can be registered for a plugin hook, and each callback
  598. * is called in order of priority. If the return value of a handler is not
  599. * null, that value is passed to the next callback in the call stack. When all
  600. * callbacks have been run, the final value is passed back to the caller
  601. * via {@link elgg_trigger_plugin_hook()}.
  602. *
  603. * Similar to Elgg Events, plugin hook handler callbacks are registered by passing
  604. * a hook, a type, and a priority.
  605. *
  606. * The callback is passed 4 arguments when called: $hook, $type, $value, and $params.
  607. *
  608. * - str $hook The name of the hook.
  609. * - str $type The type of hook.
  610. * - mixed $value The return value of the last handler or the default
  611. * value if no other handlers have been called.
  612. * - mixed $params An optional array of parameters. Used to provide additional
  613. * information to plugins.
  614. *
  615. * @note Internal: Plugin hooks are stored in $CONFIG->hooks as:
  616. * <code>
  617. * $CONFIG->hooks[$hook][$type][$priority] = $callback;
  618. * </code>
  619. *
  620. * @tip Plugin hooks are similar to Elgg Events in that Elgg emits
  621. * a plugin hook when certain actions occur, but a plugin hook allows you to alter the
  622. * parameters, as well as halt execution.
  623. *
  624. * @tip If a priority isn't specified it is determined by the order the handler was
  625. * registered relative to the event and type. For plugins, this generally means
  626. * the earlier the plugin is in the load order, the earlier the priorities are for
  627. * any event handlers.
  628. *
  629. * @tip Like Elgg Events, $hook and $type can use the special keyword 'all'.
  630. * Handler callbacks registered with $hook = all will be called for all hooks
  631. * of type $type. Similarly, handlers registered with $type = all will be
  632. * called for all hooks of type $event, regardless of $object_type. If $hook
  633. * and $type both are 'all', the handler will be called for all hooks.
  634. *
  635. * @tip Plugin hooks are sometimes used to gather lists from plugins. This is
  636. * usually done by pushing elements into an array passed in $params. Be sure
  637. * to append to and then return $value so you don't overwrite other plugin's
  638. * values.
  639. *
  640. * @warning Unlike Elgg Events, a handler that returns false will NOT halt the
  641. * execution chain.
  642. *
  643. * @param string $hook The name of the hook
  644. * @param string $type The type of the hook
  645. * @param callable $callback The name of a valid function or an array with object and method
  646. * @param int $priority The priority - 500 is default, lower numbers called first
  647. *
  648. * @return bool
  649. *
  650. * @example hooks/register/basic.php Registering for a plugin hook and examining the variables.
  651. * @example hooks/register/advanced.php Registering for a plugin hook and changing the params.
  652. * @since 1.8.0
  653. */
  654. function elgg_register_plugin_hook_handler($hook, $type, $callback, $priority = 500) {
  655. return _elgg_services()->hooks->registerHandler($hook, $type, $callback, $priority);
  656. }
  657. /**
  658. * Unregister a callback as a plugin hook.
  659. *
  660. * @param string $hook The name of the hook
  661. * @param string $entity_type The name of the type of entity (eg "user", "object" etc)
  662. * @param callable $callback The PHP callback to be removed. Since 1.11, static method
  663. * callbacks will match dynamic methods
  664. *
  665. * @return void
  666. * @since 1.8.0
  667. */
  668. function elgg_unregister_plugin_hook_handler($hook, $entity_type, $callback) {
  669. _elgg_services()->hooks->unregisterHandler($hook, $entity_type, $callback);
  670. }
  671. /**
  672. * Trigger a Plugin Hook and run all handler callbacks registered to that hook:type.
  673. *
  674. * This function runs all handlers registered to $hook, $type or
  675. * the special keyword 'all' for either or both.
  676. *
  677. * Use $params to send additional information to the handler callbacks.
  678. *
  679. * $returnvalue is the initial value to pass to the handlers, which can
  680. * change it by returning non-null values. It is useful to use $returnvalue
  681. * to set defaults. If no handlers are registered, $returnvalue is immediately
  682. * returned.
  683. *
  684. * Handlers that return null (or with no explicit return or return value) will
  685. * not change the value of $returnvalue.
  686. *
  687. * $hook is usually a verb: import, get_views, output.
  688. *
  689. * $type is usually a noun: user, ecml, page.
  690. *
  691. * @tip Like Elgg Events, $hook and $type can use the special keyword 'all'.
  692. * Handler callbacks registered with $hook = all will be called for all hooks
  693. * of type $type. Similarly, handlers registered with $type = all will be
  694. * called for all hooks of type $event, regardless of $object_type. If $hook
  695. * and $type both are 'all', the handler will be called for all hooks.
  696. *
  697. * @tip It's not possible for a plugin hook to change a non-null $returnvalue
  698. * to null.
  699. *
  700. * @note Internal: The checks for $hook and/or $type not being equal to 'all' is to
  701. * prevent a plugin hook being registered with an 'all' being called more than
  702. * once if the trigger occurs with an 'all'. An example in core of this is in
  703. * actions.php:
  704. * elgg_trigger_plugin_hook('action_gatekeeper:permissions:check', 'all', ...)
  705. *
  706. * @see elgg_register_plugin_hook_handler()
  707. *
  708. * @param string $hook The name of the hook to trigger ("all" will
  709. * trigger for all $types regardless of $hook value)
  710. * @param string $type The type of the hook to trigger ("all" will
  711. * trigger for all $hooks regardless of $type value)
  712. * @param mixed $params Additional parameters to pass to the handlers
  713. * @param mixed $returnvalue An initial return value
  714. *
  715. * @return mixed|null The return value of the last handler callback called
  716. *
  717. * @example hooks/trigger/basic.php Trigger a hook that determines if execution
  718. * should continue.
  719. * @example hooks/trigger/advanced.php Trigger a hook with a default value and use
  720. * the results to populate a menu.
  721. * @example hooks/basic.php Trigger and respond to a basic plugin hook.
  722. *
  723. * @since 1.8.0
  724. */
  725. function elgg_trigger_plugin_hook($hook, $type, $params = null, $returnvalue = null) {
  726. return _elgg_services()->hooks->trigger($hook, $type, $params, $returnvalue);
  727. }
  728. /**
  729. * Intercepts, logs, and displays uncaught exceptions.
  730. *
  731. * To use a viewtype other than failsafe, create the views:
  732. * <viewtype>/messages/exceptions/admin_exception
  733. * <viewtype>/messages/exceptions/exception
  734. * See the json viewtype for an example.
  735. *
  736. * @warning This function should never be called directly.
  737. *
  738. * @see http://www.php.net/set-exception-handler
  739. *
  740. * @param Exception $exception The exception being handled
  741. *
  742. * @return void
  743. * @access private
  744. */
  745. function _elgg_php_exception_handler($exception) {
  746. $timestamp = time();
  747. error_log("Exception #$timestamp: $exception");
  748. // Wipe any existing output buffer
  749. ob_end_clean();
  750. // make sure the error isn't cached
  751. header("Cache-Control: no-cache, must-revalidate", true);
  752. header('Expires: Fri, 05 Feb 1982 00:00:00 -0500', true);
  753. // we don't want the 'pagesetup', 'system' event to fire
  754. global $CONFIG;
  755. $CONFIG->pagesetupdone = true;
  756. try {
  757. // allow custom scripts to trigger on exception
  758. // $CONFIG->exception_include can be set locally in settings.php
  759. // value should be a system path to a file to include
  760. if (!empty($CONFIG->exception_include) && is_file($CONFIG->exception_include)) {
  761. ob_start();
  762. include $CONFIG->exception_include;
  763. $exception_output = ob_get_clean();
  764. // if content is returned from the custom handler we will output
  765. // that instead of our default failsafe view
  766. if (!empty($exception_output)) {
  767. echo $exception_output;
  768. exit;
  769. }
  770. }
  771. if (elgg_is_xhr()) {
  772. elgg_set_viewtype('json');
  773. $response = new \Symfony\Component\HttpFoundation\JsonResponse(null, 500);
  774. } else {
  775. elgg_set_viewtype('failsafe');
  776. $response = new \Symfony\Component\HttpFoundation\Response('', 500);
  777. }
  778. if (elgg_is_admin_logged_in()) {
  779. $body = elgg_view("messages/exceptions/admin_exception", array(
  780. 'object' => $exception,
  781. 'ts' => $timestamp
  782. ));
  783. } else {
  784. $body = elgg_view("messages/exceptions/exception", array(
  785. 'object' => $exception,
  786. 'ts' => $timestamp
  787. ));
  788. }
  789. $response->setContent(elgg_view_page(elgg_echo('exception:title'), $body));
  790. $response->send();
  791. } catch (Exception $e) {
  792. $timestamp = time();
  793. $message = $e->getMessage();
  794. echo "Fatal error in exception handler. Check log for Exception #$timestamp";
  795. error_log("Exception #$timestamp : fatal error in exception handler : $message");
  796. }
  797. }
  798. /**
  799. * Intercepts catchable PHP errors.
  800. *
  801. * @warning This function should never be called directly.
  802. *
  803. * @internal
  804. * For catchable fatal errors, throws an Exception with the error.
  805. *
  806. * For non-fatal errors, depending upon the debug settings, either
  807. * log the error or ignore it.
  808. *
  809. * @see http://www.php.net/set-error-handler
  810. *
  811. * @param int $errno The level of the error raised
  812. * @param string $errmsg The error message
  813. * @param string $filename The filename the error was raised in
  814. * @param int $linenum The line number the error was raised at
  815. * @param array $vars An array that points to the active symbol table where error occurred
  816. *
  817. * @return true
  818. * @throws Exception
  819. * @access private
  820. * @todo Replace error_log calls with elgg_log calls.
  821. */
  822. function _elgg_php_error_handler($errno, $errmsg, $filename, $linenum, $vars) {
  823. // Elgg 2.0 no longer uses ext/mysql, so these warnings are just a nuisance for 1.x site
  824. // owners and plugin devs.
  825. if (0 === strpos($errmsg, "mysql_connect(): The mysql extension is deprecated")) {
  826. // only suppress core's usage
  827. if (preg_match('~/classes/Elgg/Database\.php$~', strtr($filename, '\\', '/'))) {
  828. return true;
  829. }
  830. }
  831. $error = date("Y-m-d H:i:s (T)") . ": \"$errmsg\" in file $filename (line $linenum)";
  832. switch ($errno) {
  833. case E_USER_ERROR:
  834. error_log("PHP ERROR: $error");
  835. register_error("ERROR: $error");
  836. // Since this is a fatal error, we want to stop any further execution but do so gracefully.
  837. throw new \Exception($error);
  838. break;
  839. case E_WARNING :
  840. case E_USER_WARNING :
  841. case E_RECOVERABLE_ERROR: // (e.g. type hint violation)
  842. // check if the error wasn't suppressed by the error control operator (@)
  843. if (error_reporting()) {
  844. error_log("PHP WARNING: $error");
  845. }
  846. break;
  847. default:
  848. global $CONFIG;
  849. if (isset($CONFIG->debug) && $CONFIG->debug === 'NOTICE') {
  850. error_log("PHP NOTICE: $error");
  851. }
  852. }
  853. return true;
  854. }
  855. /**
  856. * Display or log a message.
  857. *
  858. * If $level is >= to the debug setting in {@link $CONFIG->debug}, the
  859. * message will be sent to {@link elgg_dump()}. Messages with lower
  860. * priority than {@link $CONFIG->debug} are ignored.
  861. *
  862. * Outputs all levels but NOTICE to screen by default.
  863. *
  864. * @note No messages will be displayed unless debugging has been enabled.
  865. *
  866. * @param string $message User message
  867. * @param string $level NOTICE | WARNING | ERROR
  868. *
  869. * @return bool
  870. * @since 1.7.0
  871. */
  872. function elgg_log($message, $level = 'NOTICE') {
  873. static $levels = array(
  874. 'INFO' => 200,
  875. 'NOTICE' => 250,
  876. 'WARNING' => 300,
  877. 'DEBUG' => 300,
  878. 'ERROR' => 400,
  879. );
  880. if ($level == 'DEBUG') {
  881. elgg_deprecated_notice("The 'DEBUG' level for logging has been deprecated.", 1.9);
  882. }
  883. $level = $levels[$level];
  884. return _elgg_services()->logger->log($message, $level);
  885. }
  886. /**
  887. * Logs or displays $value.
  888. *
  889. * If $to_screen is true, $value is displayed to screen. Else,
  890. * it is handled by PHP's {@link error_log()} function.
  891. *
  892. * A {@elgg_plugin_hook debug log} is called. If a handler returns
  893. * false, it will stop the default logging method.
  894. *
  895. * @param mixed $value The value
  896. * @param bool $to_screen Display to screen?
  897. * @return void
  898. * @since 1.7.0
  899. */
  900. function elgg_dump($value, $to_screen = true) {
  901. _elgg_services()->logger->dump($value, $to_screen);
  902. }
  903. /**
  904. * Get the current Elgg version information
  905. *
  906. * @param bool $human_readable Whether to return a human readable version (default: false)
  907. *
  908. * @return string|false Depending on success
  909. * @since 1.9
  910. */
  911. function elgg_get_version($human_readable = false) {
  912. global $CONFIG;
  913. static $version, $release;
  914. if (isset($CONFIG->path)) {
  915. if (!isset($version) || !isset($release)) {
  916. if (!include($CONFIG->path . "version.php")) {
  917. return false;
  918. }
  919. }
  920. return $human_readable ? $release : $version;
  921. }
  922. return false;
  923. }
  924. /**
  925. * Log a notice about deprecated use of a function, view, etc.
  926. *
  927. * @param string $msg Message to log
  928. * @param string $dep_version Human-readable *release* version: 1.7, 1.8, ...
  929. * @param int $backtrace_level How many levels back to display the backtrace.
  930. * Useful if calling from functions that are called
  931. * from other places (like elgg_view()). Set to -1
  932. * for a full backtrace.
  933. *
  934. * @return bool
  935. * @since 1.7.0
  936. */
  937. function elgg_deprecated_notice($msg, $dep_version, $backtrace_level = 1) {
  938. $backtrace_level += 1;
  939. return _elgg_services()->deprecation->sendNotice($msg, $dep_version, $backtrace_level);
  940. }
  941. /**
  942. * Builds a URL from the a parts array like one returned by {@link parse_url()}.
  943. *
  944. * @note If only partial information is passed, a partial URL will be returned.
  945. *
  946. * @param array $parts Associative array of URL components like parse_url() returns
  947. * 'user' and 'pass' parts are ignored because of security reasons
  948. * @param bool $html_encode HTML Encode the url?
  949. *
  950. * @see https://github.com/Elgg/Elgg/pull/8146#issuecomment-91544585
  951. * @return string Full URL
  952. * @since 1.7.0
  953. */
  954. function elgg_http_build_url(array $parts, $html_encode = true) {
  955. // build only what's given to us.
  956. $scheme = isset($parts['scheme']) ? "{$parts['scheme']}://" : '';
  957. $host = isset($parts['host']) ? "{$parts['host']}" : '';
  958. $port = isset($parts['port']) ? ":{$parts['port']}" : '';
  959. $path = isset($parts['path']) ? "{$parts['path']}" : '';
  960. $query = isset($parts['query']) ? "?{$parts['query']}" : '';
  961. $fragment = isset($parts['fragment']) ? "#{$parts['fragment']}" : '';
  962. $string = $scheme . $host . $port . $path . $query . $fragment;
  963. if ($html_encode) {
  964. return elgg_format_url($string);
  965. } else {
  966. return $string;
  967. }
  968. }
  969. /**
  970. * Adds action tokens to URL
  971. *
  972. * As of 1.7.0 action tokens are required on all actions.
  973. * Use this function to append action tokens to a URL's GET parameters.
  974. * This will preserve any existing GET parameters.
  975. *
  976. * @note If you are using {@elgg_view input/form} you don't need to
  977. * add tokens to the action. The form view automatically handles
  978. * tokens.
  979. *
  980. * @param string $url Full action URL
  981. * @param bool $html_encode HTML encode the url? (default: false)
  982. *
  983. * @return string URL with action tokens
  984. * @since 1.7.0
  985. */
  986. function elgg_add_action_tokens_to_url($url, $html_encode = false) {
  987. $url = elgg_normalize_url($url);
  988. $components = parse_url($url);
  989. if (isset($components['query'])) {
  990. $query = elgg_parse_str($components['query']);
  991. } else {
  992. $query = array();
  993. }
  994. if (isset($query['__elgg_ts']) && isset($query['__elgg_token'])) {
  995. return $url;
  996. }
  997. // append action tokens to the existing query
  998. $query['__elgg_ts'] = time();
  999. $query['__elgg_token'] = generate_action_token($query['__elgg_ts']);
  1000. $components['query'] = http_build_query($query);
  1001. // rebuild the full url
  1002. return elgg_http_build_url($components, $html_encode);
  1003. }
  1004. /**
  1005. * Removes an element from a URL's query string.
  1006. *
  1007. * @note You can send a partial URL string.
  1008. *
  1009. * @param string $url Full URL
  1010. * @param string $element The element to remove
  1011. *
  1012. * @return string The new URL with the query element removed.
  1013. * @since 1.7.0
  1014. */
  1015. function elgg_http_remove_url_query_element($url, $element) {
  1016. return elgg_http_add_url_query_elements($url, array($element => null));
  1017. }
  1018. /**
  1019. * Sets elements in a URL's query string.
  1020. *
  1021. * @param string $url The URL
  1022. * @param array $elements Key/value pairs to set in the URL. If the value is null, the
  1023. * element is removed from the URL.
  1024. *
  1025. * @return string The new URL with the query strings added
  1026. * @since 1.7.0
  1027. */
  1028. function elgg_http_add_url_query_elements($url, array $elements) {
  1029. $url_array = parse_url($url);
  1030. if (isset($url_array['query'])) {
  1031. $query = elgg_parse_str($url_array['query']);
  1032. } else {
  1033. $query = array();
  1034. }
  1035. foreach ($elements as $k => $v) {
  1036. if ($v === null) {
  1037. unset($query[$k]);
  1038. } else {
  1039. $query[$k] = $v;
  1040. }
  1041. }
  1042. // why check path? A: if no path, this may be a relative URL like "?foo=1". In this case,
  1043. // the output "" would be interpreted the current URL, so in this case we *must* set
  1044. // a query to make sure elements are removed.
  1045. if ($query || empty($url_array['path'])) {
  1046. $url_array['query'] = http_build_query($query);
  1047. } else {
  1048. unset($url_array['query']);
  1049. }
  1050. $string = elgg_http_build_url($url_array, false);
  1051. return $string;
  1052. }
  1053. /**
  1054. * Test if two URLs are functionally identical.
  1055. *
  1056. * @tip If $ignore_params is used, neither the name nor its value will be considered when comparing.
  1057. *
  1058. * @tip The order of GET params doesn't matter.
  1059. *
  1060. * @param string $url1 First URL
  1061. * @param string $url2 Second URL
  1062. * @param array $ignore_params GET params to ignore in the comparison
  1063. *
  1064. * @return bool
  1065. * @since 1.8.0
  1066. */
  1067. function elgg_http_url_is_identical($url1, $url2, $ignore_params = array('offset', 'limit')) {
  1068. $url1 = elgg_normalize_url($url1);
  1069. $url2 = elgg_normalize_url($url2);
  1070. // @todo - should probably do something with relative URLs
  1071. if ($url1 == $url2) {
  1072. return true;
  1073. }
  1074. $url1_info = parse_url($url1);
  1075. $url2_info = parse_url($url2);
  1076. if (isset($url1_info['path'])) {
  1077. $url1_info['path'] = trim($url1_info['path'], '/');
  1078. }
  1079. if (isset($url2_info['path'])) {
  1080. $url2_info['path'] = trim($url2_info['path'], '/');
  1081. }
  1082. // compare basic bits
  1083. $parts = array('scheme', 'host', 'path');
  1084. foreach ($parts as $part) {
  1085. if ((isset($url1_info[$part]) && isset($url2_info[$part]))
  1086. && $url1_info[$part] != $url2_info[$part]) {
  1087. return false;
  1088. } elseif (isset($url1_info[$part]) && !isset($url2_info[$part])) {
  1089. return false;
  1090. } elseif (!isset($url1_info[$part]) && isset($url2_info[$part])) {
  1091. return false;
  1092. }
  1093. }
  1094. // quick compare of get params
  1095. if (isset($url1_info['query']) && isset($url2_info['query'])
  1096. && $url1_info['query'] == $url2_info['query']) {
  1097. return true;
  1098. }
  1099. // compare get params that might be out of order
  1100. $url1_params = array();
  1101. $url2_params = array();
  1102. if (isset($url1_info['query'])) {
  1103. if ($url1_info['query'] = html_entity_decode($url1_info['query'])) {
  1104. $url1_params = elgg_parse_str($url1_info['query']);
  1105. }
  1106. }
  1107. if (isset($url2_info['query'])) {
  1108. if ($url2_info['query'] = html_entity_decode($url2_info['query'])) {
  1109. $url2_params = elgg_parse_str($url2_info['query']);
  1110. }
  1111. }
  1112. // drop ignored params
  1113. foreach ($ignore_params as $param) {
  1114. if (isset($url1_params[$param])) {
  1115. unset($url1_params[$param]);
  1116. }
  1117. if (isset($url2_params[$param])) {
  1118. unset($url2_params[$param]);
  1119. }
  1120. }
  1121. // array_diff_assoc only returns the items in arr1 that aren't in arrN
  1122. // but not the items that ARE in arrN but NOT in arr1
  1123. // if arr1 is an empty array, this function will return 0 no matter what.
  1124. // since we only care if they're different and not how different,
  1125. // add the results together to get a non-zero (ie, different) result
  1126. $diff_count = count(array_diff_assoc($url1_params, $url2_params));
  1127. $diff_count += count(array_diff_assoc($url2_params, $url1_params));
  1128. if ($diff_count > 0) {
  1129. return false;
  1130. }
  1131. return true;
  1132. }
  1133. /**
  1134. * Checks for $array[$key] and returns its value if it exists, else
  1135. * returns $default.
  1136. *
  1137. * Shorthand for $value = (isset($array['key'])) ? $array['key'] : 'default';
  1138. *
  1139. * @param string $key The key to check.
  1140. * @param array $array The array to check against.
  1141. * @param mixed $default Default value to return if nothing is found.
  1142. * @param bool $strict Return array key if it's set, even if empty. If false,
  1143. * return $default if the array key is unset or empty.
  1144. *
  1145. * @return mixed
  1146. * @since 1.8.0
  1147. */
  1148. function elgg_extract($key, array $array, $default = null, $strict = true) {
  1149. if (!is_array($array)) {
  1150. return $default;
  1151. }
  1152. if ($strict) {
  1153. return (isset($array[$key])) ? $array[$key] : $default;
  1154. } else {
  1155. return (isset($array[$key]) && !empty($array[$key])) ? $array[$key] : $default;
  1156. }
  1157. }
  1158. /**
  1159. * Sorts a 3d array by specific element.
  1160. *
  1161. * @warning Will re-index numeric indexes.
  1162. *
  1163. * @note This operates the same as the built-in sort functions.
  1164. * It sorts the array and returns a bool for success.
  1165. *
  1166. * Do this: elgg_sort_3d_array_by_value($my_array);
  1167. * Not this: $my_array = elgg_sort_3d_array_by_value($my_array);
  1168. *
  1169. * @param array &$array Array to sort
  1170. * @param string $element Element to sort by
  1171. * @param int $sort_order PHP sort order
  1172. * {@link http://us2.php.net/array_multisort}
  1173. * @param int $sort_type PHP sort type
  1174. * {@link http://us2.php.net/sort}
  1175. *
  1176. * @return bool
  1177. */
  1178. function elgg_sort_3d_array_by_value(&$array, $element, $sort_order = SORT_ASC,
  1179. $sort_type = SORT_LOCALE_STRING) {
  1180. $sort = array();
  1181. foreach ($array as $v) {
  1182. if (isset($v[$element])) {
  1183. $sort[] = strtolower($v[$element]);
  1184. } else {
  1185. $sort[] = null;
  1186. }
  1187. };
  1188. return array_multisort($sort, $sort_order, $sort_type, $array);
  1189. }
  1190. /**
  1191. * Return the state of a php.ini setting as a bool
  1192. *
  1193. * @warning Using this on ini settings that are not boolean
  1194. * will be inaccurate!
  1195. *
  1196. * @param string $ini_get_arg The INI setting
  1197. *
  1198. * @return bool Depending on whether it's on or off
  1199. */
  1200. function ini_get_bool($ini_get_arg) {
  1201. $temp = strtolower(ini_get($ini_get_arg));
  1202. if ($temp == '1' || $temp == 'on' || $temp == 'true') {
  1203. return true;
  1204. }
  1205. return false;
  1206. }
  1207. /**
  1208. * Returns a PHP INI setting in bytes.
  1209. *
  1210. * @tip Use this for arithmetic when determining if a file can be uploaded.
  1211. *
  1212. * @param string $setting The php.ini setting
  1213. *
  1214. * @return int
  1215. * @since 1.7.0
  1216. * @link http://www.php.net/manual/en/function.ini-get.php
  1217. */
  1218. function elgg_get_ini_setting_in_bytes($setting) {
  1219. // retrieve INI setting
  1220. $val = ini_get($setting);
  1221. // convert INI setting when shorthand notation is used
  1222. $last = strtolower($val[strlen($val) - 1]);
  1223. switch($last) {
  1224. case 'g':
  1225. $val *= 1024;
  1226. // fallthrough intentional
  1227. case 'm':
  1228. $val *= 1024;
  1229. // fallthrough intentional
  1230. case 'k':
  1231. $val *= 1024;
  1232. }
  1233. // return byte value
  1234. return $val;
  1235. }
  1236. /**
  1237. * Returns true is string is not empty, false, or null.
  1238. *
  1239. * Function to be used in array_filter which returns true if $string is not null.
  1240. *
  1241. * @param string $string The string to test
  1242. *
  1243. * @return bool
  1244. * @todo This is used once in metadata.php. Use a lambda function instead.
  1245. */
  1246. function is_not_null($string) {
  1247. if (($string === '') || ($string === false) || ($string === null)) {
  1248. return false;
  1249. }
  1250. return true;
  1251. }
  1252. /**
  1253. * Normalise the singular keys in an options array to plural keys.
  1254. *
  1255. * Used in elgg_get_entities*() functions to support shortcutting plural
  1256. * names by singular names.
  1257. *
  1258. * @param array $options The options array. $options['keys'] = 'values';
  1259. * @param array $singulars A list of singular words to pluralize by adding 's'.
  1260. *
  1261. * @return array
  1262. * @since 1.7.0
  1263. * @access private
  1264. */
  1265. function _elgg_normalize_plural_options_array($options, $singulars) {
  1266. foreach ($singulars as $singular) {
  1267. $plural = $singular . 's';
  1268. if (array_key_exists($singular, $options)) {
  1269. if ($options[$singular] === ELGG_ENTITIES_ANY_VALUE) {
  1270. $options[$plural] = $options[$singular];
  1271. } else {
  1272. // Test for array refs #2641
  1273. if (!is_array($options[$singular])) {
  1274. $options[$plural] = array($options[$singular]);
  1275. } else {
  1276. $options[$plural] = $options[$singular];
  1277. }
  1278. }
  1279. }
  1280. unset($options[$singular]);
  1281. }
  1282. return $options;
  1283. }
  1284. /**
  1285. * Emits a shutdown:system event upon PHP shutdown, but before database connections are dropped.
  1286. *
  1287. * @tip Register for the shutdown:system event to perform functions at the end of page loads.
  1288. *
  1289. * @warning Using this event to perform long-running functions is not very
  1290. * useful. Servers will hold pages until processing is done before sending
  1291. * them out to the browser.
  1292. *
  1293. * @see http://www.php.net/register-shutdown-function
  1294. *
  1295. * @internal This is registered in engine/start.php
  1296. *
  1297. * @return void
  1298. * @see register_shutdown_hook()
  1299. * @access private
  1300. */
  1301. function _elgg_shutdown_hook() {
  1302. global $START_MICROTIME;
  1303. try {
  1304. _elgg_services()->logger->setDisplay(false);
  1305. elgg_trigger_event('shutdown', 'system');
  1306. $time = (float)(microtime(true) - $START_MICROTIME);
  1307. $uri = _elgg_services()->request->server->get('REQUEST_URI', 'CLI');
  1308. // demoted to NOTICE from DEBUG so javascript is not corrupted
  1309. elgg_log("Page {$uri} generated in $time seconds", 'INFO');
  1310. } catch (Exception $e) {
  1311. $message = 'Error: ' . get_class($e) . ' thrown within the shutdown handler. ';
  1312. $message .= "Message: '{$e->getMessage()}' in file {$e->getFile()} (line {$e->getLine()})";
  1313. error_log($message);
  1314. error_log("Exception trace stack: {$e->getTraceAsString()}");
  1315. }
  1316. // Prevent an APC session bug: https://bugs.php.net/bug.php?id=60657
  1317. session_write_close();
  1318. }
  1319. /**
  1320. * Serve javascript pages.
  1321. *
  1322. * Searches for views under js/ and outputs them with special
  1323. * headers for caching control.
  1324. *
  1325. * @param array $page The page array
  1326. *
  1327. * @return bool
  1328. * @elgg_pagehandler js
  1329. * @access private
  1330. */
  1331. function _elgg_js_page_handler($page) {
  1332. return _elgg_cacheable_view_page_handler($page, 'js');
  1333. }
  1334. /**
  1335. * Serve individual views for Ajax.
  1336. *
  1337. * /ajax/view/<view_name>?<key/value params>
  1338. * /ajax/form/<action_name>?<key/value params>
  1339. *
  1340. * @param string[] $segments URL segments (not including "ajax")
  1341. * @return bool
  1342. *
  1343. * @see elgg_register_ajax_view()
  1344. * @elgg_pagehandler ajax
  1345. * @access private
  1346. */
  1347. function _elgg_ajax_page_handler($segments) {
  1348. elgg_ajax_gatekeeper();
  1349. if (count($segments) < 2) {
  1350. return false;
  1351. }
  1352. if ($segments[0] === 'view' || $segments[0] === 'form') {
  1353. if ($segments[0] === 'view') {
  1354. // ignore 'view/'
  1355. $view = implode('/', array_slice($segments, 1));
  1356. } else {
  1357. // form views start with "forms", not "form"
  1358. $view = 'forms/' . implode('/', array_slice($segments, 1));
  1359. }
  1360. $allowed_views = elgg_get_config('allowed_ajax_views');
  1361. if (!array_key_exists($view, $allowed_views)) {
  1362. header('HTTP/1.1 403 Forbidden');
  1363. exit;
  1364. }
  1365. // pull out GET parameters through filter
  1366. $vars = array();
  1367. foreach (_elgg_services()->request->query->keys() as $name) {
  1368. $vars[$name] = get_input($name);
  1369. }
  1370. if (isset($vars['guid'])) {
  1371. $vars['entity'] = get_entity($vars['guid']);
  1372. }
  1373. if ($segments[0] === 'view') {
  1374. // Try to guess the mime-type
  1375. switch ($segments[1]) {
  1376. case "js":
  1377. header("Content-Type: text/javascript;charset=utf-8");
  1378. break;
  1379. case "css":
  1380. header("Content-Type: text/css;charset=utf-8");
  1381. break;
  1382. }
  1383. echo elgg_view($view, $vars);
  1384. } else {
  1385. $action = implode('/', array_slice($segments, 1));
  1386. echo elgg_view_form($action, array(), $vars);
  1387. }
  1388. return true;
  1389. }
  1390. return false;
  1391. }
  1392. /**
  1393. * Serve CSS
  1394. *
  1395. * Serves CSS from the css views directory with headers for caching control
  1396. *
  1397. * @param array $page The page array
  1398. *
  1399. * @return bool
  1400. * @elgg_pagehandler css
  1401. * @access private
  1402. */
  1403. function _elgg_css_page_handler($page) {
  1404. if (!isset($page[0])) {
  1405. // default css
  1406. $page[0] = 'elgg';
  1407. }
  1408. return _elgg_cacheable_view_page_handler($page, 'css');
  1409. }
  1410. /**
  1411. * Handle requests for /favicon.ico
  1412. *
  1413. * @param string[] $segments The URL segments
  1414. * @return bool
  1415. * @access private
  1416. * @since 1.10
  1417. */
  1418. function _elgg_favicon_page_handler($segments) {
  1419. header("HTTP/1.1 404 Not Found", true, 404);
  1420. header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+1 week")), true);
  1421. header("Pragma: public", true);
  1422. header("Cache-Control: public", true);
  1423. // TODO in next 1.x send our default icon
  1424. //header('Content-Type: image/vnd.microsoft.icon');
  1425. //readfile(elgg_get_root_path() . '_graphics/favicon.ico');
  1426. return true;
  1427. }
  1428. /**
  1429. * Serves a JS or CSS view with headers for caching.
  1430. *
  1431. * /<css||js>/name/of/view.<last_cache>.<css||js>
  1432. *
  1433. * @param array $page The page array
  1434. * @param string $type The type: js or css
  1435. *
  1436. * @return bool
  1437. * @access private
  1438. */
  1439. function _elgg_cacheable_view_page_handler($page, $type) {
  1440. switch ($type) {
  1441. case 'js':
  1442. $content_type = 'text/javascript';
  1443. break;
  1444. case 'css':
  1445. $content_type = 'text/css';
  1446. break;
  1447. default:
  1448. return false;
  1449. break;
  1450. }
  1451. if ($page) {
  1452. // the view file names can have multiple dots
  1453. // eg: views/default/js/calendars/jquery.fullcalendar.min.php
  1454. // translates to the url /js/<ts>/calendars/jquery.fullcalendar.min.js
  1455. // and the view js/calendars/jquery.fullcalendar.min
  1456. // we ignore the last two dots for the ts and the ext.
  1457. // Additionally, the timestamp is optional.
  1458. $page = implode('/', $page);
  1459. $regex = '|(.+?)\.\w+$|';
  1460. if (!preg_match($regex, $page, $matches)) {
  1461. return false;
  1462. }
  1463. $view = "$type/{$matches[1]}";
  1464. if (!elgg_view_exists($view)) {
  1465. return false;
  1466. }
  1467. $return = elgg_view($view);
  1468. header("Content-type: $content_type;charset=utf-8");
  1469. // @todo should js be cached when simple cache turned off
  1470. //header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+10 days")), true);
  1471. //header("Pragma: public");
  1472. //header("Cache-Control: public");
  1473. //header("Content-Length: " . strlen($return));
  1474. echo $return;
  1475. return true;
  1476. }
  1477. return false;
  1478. }
  1479. /**
  1480. * Reverses the ordering in an ORDER BY clause. This is achived by replacing
  1481. * asc with desc, or appending desc to the end of the clause.
  1482. *
  1483. * This is used mostly for elgg_get_entities() and other similar functions.
  1484. *
  1485. * @param string $order_by An order by clause
  1486. * @access private
  1487. * @return string
  1488. * @access private
  1489. */
  1490. function _elgg_sql_reverse_order_by_clause($order_by) {
  1491. $order_by = strtolower($order_by);
  1492. if (strpos($order_by, ' asc') !== false) {
  1493. $return = str_replace(' asc', ' desc', $order_by);
  1494. } elseif (strpos($order_by, ' desc') !== false) {
  1495. $return = str_replace(' desc', ' asc', $order_by);
  1496. } else {
  1497. // no order specified, so default to desc since mysql defaults to asc
  1498. $return = $order_by . ' desc';
  1499. }
  1500. return $return;
  1501. }
  1502. /**
  1503. * Enable objects with an enable() method.
  1504. *
  1505. * Used as a callback for \ElggBatch.
  1506. *
  1507. * @todo why aren't these static methods on \ElggBatch?
  1508. *
  1509. * @param object $object The object to enable
  1510. * @return bool
  1511. * @access private
  1512. */
  1513. function elgg_batch_enable_callback($object) {
  1514. // our db functions return the number of rows affected...
  1515. return $object->enable() ? true : false;
  1516. }
  1517. /**
  1518. * Disable objects with a disable() method.
  1519. *
  1520. * Used as a callback for \ElggBatch.
  1521. *
  1522. * @param object $object The object to disable
  1523. * @return bool
  1524. * @access private
  1525. */
  1526. function elgg_batch_disable_callback($object) {
  1527. // our db functions return the number of rows affected...
  1528. return $object->disable() ? true : false;
  1529. }
  1530. /**
  1531. * Delete objects with a delete() method.
  1532. *
  1533. * Used as a callback for \ElggBatch.
  1534. *
  1535. * @param object $object The object to disable
  1536. * @return bool
  1537. * @access private
  1538. */
  1539. function elgg_batch_delete_callback($object) {
  1540. // our db functions return the number of rows affected...
  1541. return $object->delete() ? true : false;
  1542. }
  1543. /**
  1544. * Checks if there are some constraints on the options array for
  1545. * potentially dangerous operations.
  1546. *
  1547. * @param array $options Options array
  1548. * @param string $type Options type: metadata or annotation
  1549. * @return bool
  1550. * @access private
  1551. */
  1552. function _elgg_is_valid_options_for_batch_operation($options, $type) {
  1553. if (!$options || !is_array($options)) {
  1554. return false;
  1555. }
  1556. // at least one of these is required.
  1557. $required = array(
  1558. // generic restraints
  1559. 'guid', 'guids'
  1560. );
  1561. switch ($type) {
  1562. case 'metadata':
  1563. $metadata_required = array(
  1564. 'metadata_owner_guid', 'metadata_owner_guids',
  1565. 'metadata_name', 'metadata_names',
  1566. 'metadata_value', 'metadata_values'
  1567. );
  1568. $required = array_merge($required, $metadata_required);
  1569. break;
  1570. case 'annotations':
  1571. case 'annotation':
  1572. $annotations_required = array(
  1573. 'annotation_owner_guid', 'annotation_owner_guids',
  1574. 'annotation_name', 'annotation_names',
  1575. 'annotation_value', 'annotation_values'
  1576. );
  1577. $required = array_merge($required, $annotations_required);
  1578. break;
  1579. default:
  1580. return false;
  1581. }
  1582. foreach ($required as $key) {
  1583. // check that it exists and is something.
  1584. if (isset($options[$key]) && $options[$key]) {
  1585. return true;
  1586. }
  1587. }
  1588. return false;
  1589. }
  1590. /**
  1591. * Intercepts the index page when Walled Garden mode is enabled.
  1592. *
  1593. * @return bool
  1594. * @access private
  1595. */
  1596. function _elgg_walled_garden_index() {
  1597. elgg_load_css('elgg.walled_garden');
  1598. elgg_load_js('elgg.walled_garden');
  1599. $content = elgg_view('core/walled_garden/login');
  1600. $params = array(
  1601. 'content' => $content,
  1602. 'class' => 'elgg-walledgarden-double',
  1603. 'id' => 'elgg-walledgarden-login',
  1604. );
  1605. $body = elgg_view_layout('walled_garden', $params);
  1606. echo elgg_view_page('', $body, 'walled_garden');
  1607. return true;
  1608. }
  1609. /**
  1610. * Serve walled garden sections
  1611. *
  1612. * @param array $page Array of URL segments
  1613. * @return string
  1614. * @access private
  1615. */
  1616. function _elgg_walled_garden_ajax_handler($page) {
  1617. $view = $page[0];
  1618. $params = array(
  1619. 'content' => elgg_view("core/walled_garden/$view"),
  1620. 'class' => 'elgg-walledgarden-single hidden',
  1621. 'id' => str_replace('_', '-', "elgg-walledgarden-$view"),
  1622. );
  1623. echo elgg_view_layout('walled_garden', $params);
  1624. return true;
  1625. }
  1626. /**
  1627. * Checks the status of the Walled Garden and forwards to a login page
  1628. * if required.
  1629. *
  1630. * If the site is in Walled Garden mode, all page except those registered as
  1631. * plugin pages by {@elgg_hook public_pages walled_garden} will redirect to
  1632. * a login page.
  1633. *
  1634. * @since 1.8.0
  1635. * @elgg_event_handler init system
  1636. * @return void
  1637. * @access private
  1638. */
  1639. function _elgg_walled_garden_init() {
  1640. global $CONFIG;
  1641. elgg_register_css('elgg.walled_garden', elgg_get_simplecache_url('css', 'walled_garden'));
  1642. elgg_register_js('elgg.walled_garden', elgg_get_simplecache_url('js', 'walled_garden'));
  1643. elgg_register_page_handler('walled_garden', '_elgg_walled_garden_ajax_handler');
  1644. // check for external page view
  1645. if (isset($CONFIG->site) && $CONFIG->site instanceof \ElggSite) {
  1646. $CONFIG->site->checkWalledGarden();
  1647. }
  1648. }
  1649. /**
  1650. * Remove public access for walled gardens
  1651. *
  1652. * @param string $hook
  1653. * @param string $type
  1654. * @param array $accesses
  1655. * @return array
  1656. * @access private
  1657. */
  1658. function _elgg_walled_garden_remove_public_access($hook, $type, $accesses) {
  1659. if (isset($accesses[ACCESS_PUBLIC])) {
  1660. unset($accesses[ACCESS_PUBLIC]);
  1661. }
  1662. return $accesses;
  1663. }
  1664. /**
  1665. * Boots the engine
  1666. *
  1667. * 1. sets error handlers
  1668. * 2. connects to database
  1669. * 3. verifies the installation succeeded
  1670. * 4. loads application configuration
  1671. * 5. loads i18n data
  1672. * 6. loads cached autoloader state
  1673. * 7. loads site configuration
  1674. *
  1675. * @access private
  1676. */
  1677. function _elgg_engine_boot() {
  1678. // Register the error handlers
  1679. set_error_handler('_elgg_php_error_handler');
  1680. set_exception_handler('_elgg_php_exception_handler');
  1681. _elgg_services()->db->setupConnections();
  1682. _elgg_services()->db->assertInstalled();
  1683. _elgg_load_application_config();
  1684. _elgg_load_autoload_cache();
  1685. _elgg_load_site_config();
  1686. _elgg_session_boot();
  1687. _elgg_services()->systemCache->loadAll();
  1688. _elgg_services()->translator->loadTranslations();
  1689. }
  1690. /**
  1691. * Elgg's main init.
  1692. *
  1693. * Handles core actions for comments, the JS pagehandler, and the shutdown function.
  1694. *
  1695. * @elgg_event_handler init system
  1696. * @return void
  1697. * @access private
  1698. */
  1699. function _elgg_init() {
  1700. global $CONFIG;
  1701. elgg_register_action('comment/save');
  1702. elgg_register_action('comment/delete');
  1703. elgg_register_page_handler('js', '_elgg_js_page_handler');
  1704. elgg_register_page_handler('css', '_elgg_css_page_handler');
  1705. elgg_register_page_handler('ajax', '_elgg_ajax_page_handler');
  1706. elgg_register_page_handler('favicon.ico', '_elgg_favicon_page_handler');
  1707. elgg_register_page_handler('manifest.json', function() {
  1708. $site = elgg_get_site_entity();
  1709. $resource = new \Elgg\Http\WebAppManifestResource($site);
  1710. header('Content-Type: application/json;charset=utf-8');
  1711. echo json_encode($resource->get());
  1712. return true;
  1713. });
  1714. elgg_register_plugin_hook_handler('head', 'page', function($hook, $type, array $result) {
  1715. $result['links']['manifest'] = [
  1716. 'rel' => 'manifest',
  1717. 'href' => elgg_normalize_url('/manifest.json'),
  1718. ];
  1719. return $result;
  1720. });
  1721. elgg_register_js('elgg.autocomplete', 'js/lib/ui.autocomplete.js');
  1722. elgg_register_js('jquery.ui.autocomplete.html', 'vendors/jquery/jquery.ui.autocomplete.html.js');
  1723. elgg_define_js('jquery.ui.autocomplete.html', array(
  1724. 'src' => '/vendors/jquery/jquery.ui.autocomplete.html.js',
  1725. 'deps' => array('jquery.ui')
  1726. ));
  1727. elgg_register_external_view('js/elgg/UserPicker.js', true);
  1728. elgg_register_js('elgg.friendspicker', 'js/lib/ui.friends_picker.js');
  1729. elgg_register_js('elgg.avatar_cropper', 'js/lib/ui.avatar_cropper.js');
  1730. elgg_register_js('jquery.imgareaselect', 'vendors/jquery/jquery.imgareaselect/scripts/jquery.imgareaselect.min.js');
  1731. elgg_register_js('elgg.ui.river', 'js/lib/ui.river.js');
  1732. elgg_register_css('jquery.imgareaselect', 'vendors/jquery/jquery.imgareaselect/css/imgareaselect-deprecated.css');
  1733. // Sets a blacklist of words in the current language.
  1734. // This is a comma separated list in word:blacklist.
  1735. // @todo possibly deprecate
  1736. $CONFIG->wordblacklist = array();
  1737. $list = explode(',', elgg_echo('word:blacklist'));
  1738. if ($list) {
  1739. foreach ($list as $l) {
  1740. $CONFIG->wordblacklist[] = trim($l);
  1741. }
  1742. }
  1743. }
  1744. /**
  1745. * Adds unit tests for the general API.
  1746. *
  1747. * @param string $hook unit_test
  1748. * @param string $type system
  1749. * @param array $value array of test files
  1750. * @param array $params empty
  1751. *
  1752. * @elgg_plugin_hook unit_tests system
  1753. * @return array
  1754. * @access private
  1755. */
  1756. function _elgg_api_test($hook, $type, $value, $params) {
  1757. global $CONFIG;
  1758. $value[] = $CONFIG->path . 'engine/tests/ElggTravisInstallTest.php';
  1759. $value[] = $CONFIG->path . 'engine/tests/ElggCoreHelpersTest.php';
  1760. $value[] = $CONFIG->path . 'engine/tests/ElggCoreRegressionBugsTest.php';
  1761. $value[] = $CONFIG->path . 'engine/tests/ElggBatchTest.php';
  1762. return $value;
  1763. }
  1764. /**#@+
  1765. * Controls access levels on \ElggEntity entities, metadata, and annotations.
  1766. *
  1767. * @warning ACCESS_DEFAULT is a place holder for the input/access view. Do not
  1768. * use it when saving an entity.
  1769. *
  1770. * @var int
  1771. */
  1772. define('ACCESS_DEFAULT', -1);
  1773. define('ACCESS_PRIVATE', 0);
  1774. define('ACCESS_LOGGED_IN', 1);
  1775. define('ACCESS_PUBLIC', 2);
  1776. define('ACCESS_FRIENDS', -2);
  1777. /**#@-*/
  1778. /**
  1779. * Constant to request the value of a parameter be ignored in elgg_get_*() functions
  1780. *
  1781. * @see elgg_get_entities()
  1782. * @var null
  1783. * @since 1.7
  1784. */
  1785. define('ELGG_ENTITIES_ANY_VALUE', null);
  1786. /**
  1787. * Constant to request the value of a parameter be nothing in elgg_get_*() functions.
  1788. *
  1789. * @see elgg_get_entities()
  1790. * @var int 0
  1791. * @since 1.7
  1792. */
  1793. define('ELGG_ENTITIES_NO_VALUE', 0);
  1794. /**
  1795. * Used in calls to forward() to specify the browser should be redirected to the
  1796. * referring page.
  1797. *
  1798. * @see forward
  1799. * @var int -1
  1800. */
  1801. define('REFERRER', -1);
  1802. /**
  1803. * Alternate spelling for REFERRER. Included because of some bad documentation
  1804. * in the original HTTP spec.
  1805. *
  1806. * @see forward()
  1807. * @link http://en.wikipedia.org/wiki/HTTP_referrer#Origin_of_the_term_referer
  1808. * @var int -1
  1809. */
  1810. define('REFERER', -1);
  1811. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  1812. $events->registerHandler('init', 'system', '_elgg_init');
  1813. $events->registerHandler('boot', 'system', '_elgg_engine_boot', 1);
  1814. $hooks->registerHandler('unit_test', 'system', '_elgg_api_test');
  1815. $events->registerHandler('init', 'system', '_elgg_walled_garden_init', 1000);
  1816. };