/
elgglib.php
1441 lines (1300 loc) · 42.8 KB
/
elgglib.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use Elgg\Http\ResponseBuilder;
/**
* Bootstrapping and helper procedural code available for use in Elgg core and plugins.
*
* @todo These functions can't be subpackaged because they cover a wide mix of
* purposes and subsystems. Many of them should be moved to more relevant files.
*/
/**
* Get a reference to the global Application object
*
* @return \Elgg\Di\PublicContainer
* @since 2.0.0
*/
function elgg() {
return _elgg_services()->dic;
}
/**
* Forward to $location.
*
* Sends a 'Location: $location' header and exits. If headers have already been sent, throws an exception.
*
* @param string $location URL to forward to browser to. This can be a path
* relative to the network's URL.
* @param string $reason Short explanation for why we're forwarding. Set to
* '404' to forward to error page. Default message is
* 'system'.
*
* @return void
* @throws SecurityException|InvalidParameterException
*/
function forward($location = "", $reason = 'system') {
if (headers_sent($file, $line)) {
throw new \SecurityException("Redirect could not be issued due to headers already being sent. Halting execution for security. "
. "Output started in file $file at line $line. Search http://learn.elgg.org/ for more information.");
}
_elgg_services()->responseFactory->redirect($location, $reason);
exit;
}
/**
* Set a response HTTP header
*
* @see header()
*
* @param string $header Header
* @param bool $replace Replace existing header
* @return void
* @since 2.3
*/
function elgg_set_http_header($header, $replace = true) {
if (!preg_match('~^HTTP/\\d\\.\\d~', $header)) {
list($name, $value) = explode(':', $header, 2);
_elgg_services()->responseFactory->setHeader($name, ltrim($value), $replace);
}
}
/**
* Defines a JS lib as an AMD module. This is useful for shimming
* traditional JS or for setting the paths of AMD modules.
*
* Calling multiple times for the same name will:
* * set the preferred path to the last call setting a path
* * overwrite the shimmed AMD modules with the last call setting a shimmed module
*
* Use elgg_require_js($name) to load on the current page.
*
* Calling this function is not needed if your JS are in views named like `module/name.js`
* Instead, simply call elgg_require_js("module/name").
*
* @note The configuration is cached in simplecache, so logic should not depend on user-
* specific values like get_current_language().
*
* @param string $name The module name
* @param array $config An array like the following:
* array 'deps' An array of AMD module dependencies
* string 'exports' The name of the exported module
* string 'src' The URL to the JS. Can be relative.
*
* @return void
*/
function elgg_define_js($name, $config) {
$src = elgg_extract('src', $config);
if ($src) {
$url = elgg_normalize_url($src);
_elgg_services()->amdConfig->addPath($name, $url);
}
// shimmed module
if (isset($config['deps']) || isset($config['exports'])) {
_elgg_services()->amdConfig->addShim($name, $config);
}
}
/**
* Request that Elgg load an AMD module onto the page.
*
* @param string $name The AMD module name.
* @return void
* @since 1.9.0
*/
function elgg_require_js($name) {
_elgg_services()->amdConfig->addDependency($name);
}
/**
* Cancel a request to load an AMD module onto the page.
*
* @note The elgg, jquery, and jquery-ui modules cannot be cancelled.
*
* @param string $name The AMD module name.
* @return void
* @since 2.1.0
*/
function elgg_unrequire_js($name) {
_elgg_services()->amdConfig->removeDependency($name);
}
/**
* Get the JavaScript URLs that are loaded
*
* @param string $location 'head' or 'footer'
*
* @return array
* @since 1.8.0
*/
function elgg_get_loaded_js($location = 'head') {
return elgg_get_loaded_external_files('js', $location);
}
/**
* Register a CSS view name to be included in the HTML head
*
* @param string $view The css view name
*
* @return void
*
* @since 3.1
*/
function elgg_require_css(string $view) {
$view_name = "{$view}.css";
if (!elgg_view_exists($view_name)) {
$view_name = $view;
}
elgg_register_external_file('css', $view, elgg_get_simplecache_url($view_name));
elgg_load_external_file('css', $view);
}
/**
* Unregister a CSS view name to be included in the HTML head
*
* @param string $view The css view name
*
* @return void
*
* @since 3.1
*/
function elgg_unrequire_css(string $view) {
elgg_unregister_external_file('css', $view);
}
/**
* Get the loaded CSS URLs
*
* @return array
* @since 1.8.0
*/
function elgg_get_loaded_css() {
return elgg_get_loaded_external_files('css', 'head');
}
/**
* Core registration function for external files
*
* @param string $type Type of external resource (js or css)
* @param string $name Identifier used as key
* @param string $url URL
* @param string $location Location in the page to include the file (default = 'head')
* @param int $priority Loading priority of the file
*
* @return bool
* @since 1.8.0
*/
function elgg_register_external_file($type, $name, $url, $location = 'head', $priority = 500) {
return _elgg_services()->externalFiles->register($type, $name, $url, $location, $priority);
}
/**
* Unregister an external file
*
* @param string $type Type of file: js or css
* @param string $name The identifier of the file
*
* @return bool
* @since 1.8.0
*/
function elgg_unregister_external_file($type, $name) {
return _elgg_services()->externalFiles->unregister($type, $name);
}
/**
* Load an external resource for use on this page
*
* @param string $type Type of file: js or css
* @param string $name The identifier for the file
*
* @return void
* @since 1.8.0
*/
function elgg_load_external_file($type, $name) {
_elgg_services()->externalFiles->load($type, $name);
}
/**
* Get external resource descriptors
*
* @param string $type Type of file: js or css
* @param string $location Page location
*
* @return array
* @since 1.8.0
*/
function elgg_get_loaded_external_files($type, $location) {
return _elgg_services()->externalFiles->getLoadedFiles($type, $location);
}
/**
* Display a system message on next page load.
*
* @param string|array $message Message or messages to add
*
* @return bool
*/
function system_message($message) {
elgg()->system_messages->addSuccessMessage($message);
return true;
}
/**
* Display an error on next page load.
*
* @param string|array $error Error or errors to add
*
* @return bool
*/
function register_error($error) {
elgg()->system_messages->addErrorMessage($error);
return true;
}
/**
* Get a copy of the current system messages.
*
* @return \Elgg\SystemMessages\RegisterSet
* @since 2.1
*/
function elgg_get_system_messages() {
return elgg()->system_messages->loadRegisters();
}
/**
* Set the system messages. This will overwrite the state of all messages and errors!
*
* @param \Elgg\SystemMessages\RegisterSet $set Set of messages
* @return void
* @since 2.1
*/
function elgg_set_system_messages(\Elgg\SystemMessages\RegisterSet $set) {
elgg()->system_messages->saveRegisters($set);
}
/**
* Register a callback as an Elgg event handler.
*
* Events are emitted by Elgg when certain actions occur. Plugins
* can respond to these events or halt them completely by registering a handler
* as a callback to an event. Multiple handlers can be registered for
* the same event and will be executed in order of $priority.
*
* For most events, any handler returning false will halt the execution chain and
* cause the event to be "cancelled". For After Events, the return values of the
* handlers will be ignored and all handlers will be called.
*
* This function is called with the event name, event type, and handler callback name.
* Setting the optional $priority allows plugin authors to specify when the
* callback should be run. Priorities for plugins should be 1-1000.
*
* The callback is passed 3 arguments when called: $event, $type, and optional $params.
*
* $event is the name of event being emitted.
* $type is the type of event or object concerned.
* $params is an optional parameter passed that can include a related object. See
* specific event documentation for details on which events pass what parameteres.
*
* @tip If a priority isn't specified it is determined by the order the handler was
* registered relative to the event and type. For plugins, this generally means
* the earlier the plugin is in the load order, the earlier the priorities are for
* any event handlers.
*
* @tip $event and $object_type can use the special keyword 'all'. Handler callbacks registered
* with $event = all will be called for all events of type $object_type. Similarly,
* callbacks registered with $object_type = all will be called for all events of type
* $event, regardless of $object_type. If $event and $object_type both are 'all', the
* handler callback will be called for all events.
*
* @tip Event handler callbacks are considered in the follow order:
* - Specific registration where 'all' isn't used.
* - Registration where 'all' is used for $event only.
* - Registration where 'all' is used for $type only.
* - Registration where 'all' is used for both.
*
* @warning If you use the 'all' keyword, you must have logic in the handler callback to
* test the passed parameters before taking an action.
*
* @tip When referring to events, the preferred syntax is "event, type".
*
* @param string $event The event type
* @param string $object_type The object type
* @param callable $callback The handler callback
* @param int $priority The priority - 0 is default, negative before, positive after
*
* @return bool
* @example documentation/events/basic.php
* @example documentation/events/advanced.php
* @example documentation/events/all.php
*/
function elgg_register_event_handler($event, $object_type, $callback, $priority = 500) {
return _elgg_services()->events->registerHandler($event, $object_type, $callback, $priority);
}
/**
* Unregisters a callback for an event.
*
* @param string $event The event type
* @param string $object_type The object type
* @param callable $callback The callback. Since 1.11, static method callbacks will match dynamic methods
*
* @return bool true if a handler was found and removed
* @since 1.7
*/
function elgg_unregister_event_handler($event, $object_type, $callback) {
return _elgg_services()->events->unregisterHandler($event, $object_type, $callback);
}
/**
* Clears all callback registrations for a event.
*
* @param string $event The name of the event
* @param string $object_type The objecttype of the event
*
* @return void
* @since 2.3
*/
function elgg_clear_event_handlers($event, $object_type) {
_elgg_services()->events->clearHandlers($event, $object_type);
}
/**
* Trigger an Elgg Event and attempt to run all handler callbacks registered to that
* event, type.
*
* This function attempts to run all handlers registered to $event, $object_type or
* the special keyword 'all' for either or both. If a handler returns false, the
* event will be cancelled (no further handlers will be called, and this function
* will return false).
*
* $event is usually a verb: create, update, delete, annotation.
*
* $object_type is usually a noun: object, group, user, annotation, relationship, metadata.
*
* $object is usually an Elgg* object associated with the event.
*
* @warning Elgg events should only be triggered by core. Plugin authors should use
* {@link trigger_elgg_plugin_hook()} instead.
*
* @tip When referring to events, the preferred syntax is "event, type".
*
* @note Internal: Only rarely should events be changed, added, or removed in core.
* When making changes to events, be sure to first create a ticket on Github.
*
* @note Internal: @tip Think of $object_type as the primary namespace element, and
* $event as the secondary namespace.
*
* @param string $event The event type
* @param string $object_type The object type
* @param mixed $object The object involved in the event
*
* @return bool False if any handler returned false, otherwise true.
* @example documentation/examples/events/trigger.php
*/
function elgg_trigger_event($event, $object_type, $object = null) {
return elgg()->events->trigger($event, $object_type, $object);
}
/**
* Trigger a "Before event" indicating a process is about to begin.
*
* Like regular events, a handler returning false will cancel the process and false
* will be returned.
*
* To register for a before event, append ":before" to the event name when registering.
*
* @param string $event The event type. The fired event type will be appended with ":before".
* @param string $object_type The object type
* @param mixed $object The object involved in the event
*
* @return bool False if any handler returned false, otherwise true
*
* @see elgg_trigger_event()
* @see elgg_trigger_after_event()
*/
function elgg_trigger_before_event($event, $object_type, $object = null) {
return elgg()->events->triggerBefore($event, $object_type, $object);
}
/**
* Trigger an "After event" indicating a process has finished.
*
* Unlike regular events, all the handlers will be called, their return values ignored.
*
* To register for an after event, append ":after" to the event name when registering.
*
* @param string $event The event type. The fired event type will be appended with ":after".
* @param string $object_type The object type
* @param string $object The object involved in the event
*
* @return true
*
* @see elgg_trigger_before_event()
*/
function elgg_trigger_after_event($event, $object_type, $object = null) {
return elgg()->events->triggerAfter($event, $object_type, $object);
}
/**
* Trigger an event normally, but send a notice about deprecated use if any handlers are registered.
*
* @param string $event The event type
* @param string $object_type The object type
* @param string $object The object involved in the event
* @param string $message The deprecation message
* @param string $version Human-readable *release* version: 1.9, 1.10, ...
*
* @return bool
*
* @see elgg_trigger_event()
*/
function elgg_trigger_deprecated_event($event, $object_type, $object = null, $message = null, $version = null) {
return elgg()->events->triggerDeprecated($event, $object_type, $object, $message, $version);
}
/**
* Register a callback as a plugin hook handler.
*
* Plugin hooks allow developers to losely couple plugins and features by
* responding to and emitting {@link elgg_trigger_plugin_hook()} customizable hooks.
* Handler callbacks can respond to the hook, change the details of the hook, or
* ignore it.
*
* Multiple handlers can be registered for a plugin hook, and each callback
* is called in order of priority. If the return value of a handler is not
* null, that value is passed to the next callback in the call stack. When all
* callbacks have been run, the final value is passed back to the caller
* via {@link elgg_trigger_plugin_hook()}.
*
* Similar to Elgg Events, plugin hook handler callbacks are registered by passing
* a hook, a type, and a priority.
*
* The callback is passed 4 arguments when called: $hook, $type, $value, and $params.
*
* - str $hook The name of the hook.
* - str $type The type of hook.
* - mixed $value The return value of the last handler or the default
* value if no other handlers have been called.
* - mixed $params An optional array of parameters. Used to provide additional
* information to plugins.
*
* @tip Plugin hooks are similar to Elgg Events in that Elgg emits
* a plugin hook when certain actions occur, but a plugin hook allows you to alter the
* parameters, as well as halt execution.
*
* @tip If a priority isn't specified it is determined by the order the handler was
* registered relative to the event and type. For plugins, this generally means
* the earlier the plugin is in the load order, the earlier the priorities are for
* any event handlers.
*
* @tip Like Elgg Events, $hook and $type can use the special keyword 'all'.
* Handler callbacks registered with $hook = all will be called for all hooks
* of type $type. Similarly, handlers registered with $type = all will be
* called for all hooks of type $event, regardless of $object_type. If $hook
* and $type both are 'all', the handler will be called for all hooks.
*
* @tip Plugin hooks are sometimes used to gather lists from plugins. This is
* usually done by pushing elements into an array passed in $params. Be sure
* to append to and then return $value so you don't overwrite other plugin's
* values.
*
* @warning Unlike Elgg Events, a handler that returns false will NOT halt the
* execution chain.
*
* @param string $hook The name of the hook
* @param string $type The type of the hook
* @param callable $callback The name of a valid function or an array with object and method
* @param int $priority The priority - 500 is default, lower numbers called first
*
* @return bool
*
* @example hooks/register/basic.php Registering for a plugin hook and examining the variables.
* @example hooks/register/advanced.php Registering for a plugin hook and changing the params.
* @since 1.8.0
*/
function elgg_register_plugin_hook_handler($hook, $type, $callback, $priority = 500) {
return elgg()->hooks->registerHandler($hook, $type, $callback, $priority);
}
/**
* Unregister a callback as a plugin hook.
*
* @param string $hook The name of the hook
* @param string $entity_type The name of the type of entity (eg "user", "object" etc)
* @param callable $callback The PHP callback to be removed. Since 1.11, static method
* callbacks will match dynamic methods
*
* @return void
* @since 1.8.0
*/
function elgg_unregister_plugin_hook_handler($hook, $entity_type, $callback) {
elgg()->hooks->unregisterHandler($hook, $entity_type, $callback);
}
/**
* Clears all callback registrations for a plugin hook.
*
* @param string $hook The name of the hook
* @param string $type The type of the hook
*
* @return void
* @since 2.0
*/
function elgg_clear_plugin_hook_handlers($hook, $type) {
elgg()->hooks->clearHandlers($hook, $type);
}
/**
* Trigger a Plugin Hook and run all handler callbacks registered to that hook:type.
*
* This function runs all handlers registered to $hook, $type or
* the special keyword 'all' for either or both.
*
* Use $params to send additional information to the handler callbacks.
*
* $returnvalue is the initial value to pass to the handlers, which can
* change it by returning non-null values. It is useful to use $returnvalue
* to set defaults. If no handlers are registered, $returnvalue is immediately
* returned.
*
* Handlers that return null (or with no explicit return or return value) will
* not change the value of $returnvalue.
*
* $hook is usually a verb: import, get_views, output.
*
* $type is usually a noun: user, ecml, page.
*
* @tip Like Elgg Events, $hook and $type can use the special keyword 'all'.
* Handler callbacks registered with $hook = all will be called for all hooks
* of type $type. Similarly, handlers registered with $type = all will be
* called for all hooks of type $event, regardless of $object_type. If $hook
* and $type both are 'all', the handler will be called for all hooks.
*
* @tip It's not possible for a plugin hook to change a non-null $returnvalue
* to null.
*
* @note Internal: The checks for $hook and/or $type not being equal to 'all' is to
* prevent a plugin hook being registered with an 'all' being called more than
* once if the trigger occurs with an 'all'. An example in core of this is in
* actions.php:
* elgg_trigger_plugin_hook('action_gatekeeper:permissions:check', 'all', ...)
*
* @see elgg_register_plugin_hook_handler()
*
* @param string $hook The name of the hook to trigger ("all" will
* trigger for all $types regardless of $hook value)
* @param string $type The type of the hook to trigger ("all" will
* trigger for all $hooks regardless of $type value)
* @param mixed $params Additional parameters to pass to the handlers
* @param mixed $returnvalue An initial return value
*
* @return mixed|null The return value of the last handler callback called
*
* @example hooks/trigger/basic.php Trigger a hook that determines if execution
* should continue.
* @example hooks/trigger/advanced.php Trigger a hook with a default value and use
* the results to populate a menu.
* @example hooks/basic.php Trigger and respond to a basic plugin hook.
*
* @since 1.8.0
*/
function elgg_trigger_plugin_hook($hook, $type, $params = null, $returnvalue = null) {
return elgg()->hooks->trigger($hook, $type, $params, $returnvalue);
}
/**
* Trigger an plugin hook normally, but send a notice about deprecated use if any handlers are registered.
*
* @param string $hook The name of the plugin hook
* @param string $type The type of the plugin hook
* @param mixed $params Supplied params for the hook
* @param mixed $returnvalue The value of the hook, this can be altered by registered callbacks
* @param string $message The deprecation message
* @param string $version Human-readable *release* version: 1.9, 1.10, ...
*
* @return mixed
*
* @see elgg_trigger_plugin_hook()
* @since 3.0
*/
function elgg_trigger_deprecated_plugin_hook($hook, $type, $params = null, $returnvalue = null, $message = null, $version = null) {
return elgg()->hooks->triggerDeprecated($hook, $type, $params, $returnvalue, $message, $version);
}
/**
* Log a message.
*
* If $level is >= to the debug setting in {@link $CONFIG->debug}, the
* message will be sent to {@link elgg_dump()}. Messages with lower
* priority than {@link $CONFIG->debug} are ignored.
*
* @note Use the developers plugin to display logs
*
* @param string $message User message
* @param string $level NOTICE | WARNING | ERROR
*
* @return bool
* @since 1.7.0
*/
function elgg_log($message, $level = \Psr\Log\LogLevel::NOTICE) {
return _elgg_services()->logger->log($level, $message);
}
/**
* Logs $value to PHP's {@link error_log()}
*
* A {@elgg_plugin_hook debug log} is called. If a handler returns
* false, it will stop the default logging method.
*
* @note Use the developers plugin to display logs
*
* @param mixed $value The value
* @return void
* @since 1.7.0
*/
function elgg_dump($value) {
_elgg_services()->logger->dump($value);
}
/**
* Get the current Elgg version information
*
* @param bool $human_readable Whether to return a human readable version (default: false)
*
* @return string|false Depending on success
* @since 1.9
*/
function elgg_get_version($human_readable = false) {
static $version, $release;
if (!isset($version) || !isset($release)) {
$path = \Elgg\Application::elggDir()->getPath('version.php');
if (!is_file($path)) {
return false;
}
include $path;
}
return $human_readable ? $release : $version;
}
/**
* Log a notice about deprecated use of a function, view, etc.
*
* @param string $msg Message to log
* @param string $dep_version Human-readable *release* version: 1.7, 1.8, ...
* @param mixed $ignored No longer used argument
*
* @return bool
* @since 1.7.0
*/
function elgg_deprecated_notice($msg, $dep_version, $ignored = null) {
return _elgg_services()->deprecation->sendNotice($msg, $dep_version);
}
/**
* Builds a URL from the a parts array like one returned by {@link parse_url()}.
*
* @note If only partial information is passed, a partial URL will be returned.
*
* @param array $parts Associative array of URL components like parse_url() returns
* 'user' and 'pass' parts are ignored because of security reasons
* @param bool $html_encode HTML Encode the url?
*
* @see https://github.com/Elgg/Elgg/pull/8146#issuecomment-91544585
* @return string Full URL
* @since 1.7.0
*/
function elgg_http_build_url(array $parts, $html_encode = true) {
// build only what's given to us.
$scheme = isset($parts['scheme']) ? "{$parts['scheme']}://" : '';
$host = isset($parts['host']) ? "{$parts['host']}" : '';
$port = isset($parts['port']) ? ":{$parts['port']}" : '';
$path = isset($parts['path']) ? "{$parts['path']}" : '';
$query = isset($parts['query']) ? "?{$parts['query']}" : '';
$fragment = isset($parts['fragment']) ? "#{$parts['fragment']}" : '';
$string = $scheme . $host . $port . $path . $query . $fragment;
if ($html_encode) {
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8', false);
} else {
return $string;
}
}
/**
* Adds action tokens to URL
*
* As of 1.7.0 action tokens are required on all actions.
* Use this function to append action tokens to a URL's GET parameters.
* This will preserve any existing GET parameters.
*
* @note If you are using {@elgg_view input/form} you don't need to
* add tokens to the action. The form view automatically handles
* tokens.
*
* @param string $url Full action URL
* @param bool $html_encode HTML encode the url? (default: false)
*
* @return string URL with action tokens
* @since 1.7.0
*/
function elgg_add_action_tokens_to_url($url, $html_encode = false) {
$url = elgg_normalize_url($url);
$components = parse_url($url);
if (isset($components['query'])) {
$query = elgg_parse_str($components['query']);
} else {
$query = [];
}
if (isset($query['__elgg_ts']) && isset($query['__elgg_token'])) {
return $url;
}
// append action tokens to the existing query
$query['__elgg_ts'] = elgg()->csrf->getCurrentTime()->getTimestamp();
$query['__elgg_token'] = elgg()->csrf->generateActionToken($query['__elgg_ts']);
$components['query'] = http_build_query($query);
// rebuild the full url
return elgg_http_build_url($components, $html_encode);
}
/**
* Removes an element from a URL's query string.
*
* @note You can send a partial URL string.
*
* @param string $url Full URL
* @param string $element The element to remove
*
* @return string The new URL with the query element removed.
* @since 1.7.0
*/
function elgg_http_remove_url_query_element($url, $element) {
return elgg_http_add_url_query_elements($url, [$element => null]);
}
/**
* Sets elements in a URL's query string.
*
* @param string $url The URL
* @param array $elements Key/value pairs to set in the URL. If the value is null, the
* element is removed from the URL.
*
* @return string The new URL with the query strings added
* @since 1.7.0
*/
function elgg_http_add_url_query_elements($url, array $elements) {
$url_array = parse_url($url);
if (isset($url_array['query'])) {
$query = elgg_parse_str($url_array['query']);
} else {
$query = [];
}
foreach ($elements as $k => $v) {
if ($v === null) {
unset($query[$k]);
} else {
$query[$k] = $v;
}
}
// why check path? A: if no path, this may be a relative URL like "?foo=1". In this case,
// the output "" would be interpreted the current URL, so in this case we *must* set
// a query to make sure elements are removed.
if ($query || empty($url_array['path'])) {
$url_array['query'] = http_build_query($query);
} else {
unset($url_array['query']);
}
$string = elgg_http_build_url($url_array, false);
// Restore relative protocol to url if missing and is provided as part of the initial url (see #9874)
if (!isset($url['scheme']) && (substr($url, 0, 2) == '//')) {
$string = "//{$string}";
}
return $string;
}
/**
* Test if two URLs are functionally identical.
*
* @tip If $ignore_params is used, neither the name nor its value will be considered when comparing.
*
* @tip The order of GET params doesn't matter.
*
* @param string $url1 First URL
* @param string $url2 Second URL
* @param array $ignore_params GET params to ignore in the comparison
*
* @return bool
* @since 1.8.0
*/
function elgg_http_url_is_identical($url1, $url2, $ignore_params = ['offset', 'limit']) {
if (!is_string($url1) || !is_string($url2)) {
return false;
}
$url1 = elgg_normalize_url($url1);
$url2 = elgg_normalize_url($url2);
if ($url1 == $url2) {
return true;
}
$url1_info = parse_url($url1);
$url2_info = parse_url($url2);
if (isset($url1_info['path'])) {
$url1_info['path'] = trim($url1_info['path'], '/');
}
if (isset($url2_info['path'])) {
$url2_info['path'] = trim($url2_info['path'], '/');
}
// compare basic bits
$parts = ['scheme', 'host', 'path'];
foreach ($parts as $part) {
if ((isset($url1_info[$part]) && isset($url2_info[$part]))
&& $url1_info[$part] != $url2_info[$part]) {
return false;
} elseif (isset($url1_info[$part]) && !isset($url2_info[$part])) {
return false;
} elseif (!isset($url1_info[$part]) && isset($url2_info[$part])) {
return false;
}
}
// quick compare of get params
if (isset($url1_info['query']) && isset($url2_info['query'])
&& $url1_info['query'] == $url2_info['query']) {
return true;
}
// compare get params that might be out of order
$url1_params = [];
$url2_params = [];
if (isset($url1_info['query'])) {
if ($url1_info['query'] = html_entity_decode($url1_info['query'])) {
$url1_params = elgg_parse_str($url1_info['query']);
}
}
if (isset($url2_info['query'])) {
if ($url2_info['query'] = html_entity_decode($url2_info['query'])) {
$url2_params = elgg_parse_str($url2_info['query']);
}
}
// drop ignored params
foreach ($ignore_params as $param) {
if (isset($url1_params[$param])) {
unset($url1_params[$param]);
}
if (isset($url2_params[$param])) {
unset($url2_params[$param]);
}
}
// array_diff_assoc only returns the items in arr1 that aren't in arrN
// but not the items that ARE in arrN but NOT in arr1
// if arr1 is an empty array, this function will return 0 no matter what.
// since we only care if they're different and not how different,
// add the results together to get a non-zero (ie, different) result
$diff_count = count(array_diff_assoc($url1_params, $url2_params));
$diff_count += count(array_diff_assoc($url2_params, $url1_params));
if ($diff_count > 0) {
return false;
}
return true;
}
/**
* Signs provided URL with a SHA256 HMAC key
*
* @note Signed URLs do not offer CSRF protection and should not be used instead of action tokens.
*
* @param string $url URL to sign
* @param string $expires Expiration time
* A string suitable for strtotime()
* Falsey values indicate non-expiring URL
* @return string
*/
function elgg_http_get_signed_url($url, $expires = false) {
return _elgg_services()->urlSigner->sign($url, $expires);
}
/**
* Validates if the HMAC signature of the URL is valid
*
* @param string $url URL to validate
* @return bool
*/
function elgg_http_validate_signed_url($url) {
return _elgg_services()->urlSigner->isValid($url);
}
/**
* Validates if the HMAC signature of the current request is valid
* Issues 403 response if signature is invalid
*
* @return void
* @throws \Elgg\HttpException
*/
function elgg_signed_request_gatekeeper() {
if (\Elgg\Application::isCli()) {
return;
}
_elgg_services()->urlSigner->assertValid(current_page_url());
}
/**
* Checks for $array[$key] and returns its value if it exists, else
* returns $default.
*
* Shorthand for $value = (isset($array['key'])) ? $array['key'] : 'default';
*
* @param string $key Key to check in the source array
* @param array $array Source array
* @param mixed $default Value to return if key is not found
* @param bool $strict Return array key if it's set, even if empty. If false,
* return $default if the array key is unset or empty.
*
* @return mixed
* @since 1.8.0
*/
function elgg_extract($key, $array, $default = null, $strict = true) {
if (!is_array($array) && !$array instanceof ArrayAccess) {
return $default;
}
if ($strict) {
return (isset($array[$key])) ? $array[$key] : $default;
} else {
return (isset($array[$key]) && !empty($array[$key])) ? $array[$key] : $default;
}
}
/**
* Extract class names from an array, optionally merging into a preexisting set.
*
* @param array $array Source array
* @param string|string[] $existing Existing name(s)
* @param string $extract_key Key to extract new classes from
* @return string[]