]> git.mxchange.org Git - friendica.git/blob - doc/Addons.md
Remove addon_hooks template variable
[friendica.git] / doc / Addons.md
1 Friendica Addon development
2 ==============
3
4 * [Home](help)
5
6 Please see the sample addon 'randplace' for a working example of using some of these features.
7 Addons work by intercepting event hooks - which must be registered.
8 Modules work by intercepting specific page requests (by URL path).
9
10 Addon names cannot contain spaces or other punctuation and are used as filenames and function names.
11 You may supply a "friendly" name within the comment block.
12 Each addon must contain both an install and an uninstall function based on the addon name.
13 For instance "addon1name_install()".
14 These two functions take no arguments and are usually responsible for registering (and unregistering) event hooks that your addon will require.
15 The install and uninstall functions will also be called (i.e. re-installed) if the addon changes after installation.
16 Therefore your uninstall should not destroy data and install should consider that data may already exist.
17 Future extensions may provide for "setup" amd "remove".
18
19 Addons should contain a comment block with the four following parameters:
20
21     /*
22      * Name: My Great Addon
23      * Description: This is what my addon does. It's really cool.
24      * Version: 1.0
25      * Author: John Q. Public <john@myfriendicasite.com>
26      */
27
28 Please also add a README or README.md file to the addon directory.
29 It will be displayed in the admin panel and should include some further information in addition to the header information.
30
31 ## PHP addon hooks
32
33 Register your addon hooks during installation.
34
35     Addon::registerHook($hookname, $file, $function);
36
37 $hookname is a string and corresponds to a known Friendica PHP hook.
38
39 $file is a pathname relative to the top-level Friendica directory.
40 This *should* be 'addon/*addon_name*/*addon_name*.php' in most cases.
41
42 $function is a string and is the name of the function which will be executed when the hook is called.
43
44 ### Arguments
45 Your hook callback functions will be called with at least one and possibly two arguments
46
47     function myhook_function(App $a, &$b) {
48
49     }
50
51
52 If you wish to make changes to the calling data, you must declare them as reference variables (with `&`) during function declaration.
53
54 #### $a
55 $a is the Friendica `App` class.
56 It contains a wealth of information about the current state of Friendica:
57
58 * which module has been called,
59 * configuration information,
60 * the page contents at the point the hook was invoked,
61 * profile and user information, etc.
62
63 It is recommeded you call this `$a` to match its usage elsewhere.
64
65 #### $b
66 $b can be called anything you like.
67 This is information specific to the hook currently being processed, and generally contains information that is being immediately processed or acted on that you can use, display, or alter.
68 Remember to declare it with `&` if you wish to alter it.
69
70 ## Global stylesheets
71
72 If your addon requires adding a stylesheet on all pages of Friendica, add the following hook:
73
74 ```php
75 function <addon>_install()
76 {
77         Addon::registerHook('head', __FILE__, '<addon>_head');
78         ...
79 }
80
81
82 function <addon>_head(App $a)
83 {
84         $a->registerStylesheet(__DIR__ . '/relative/path/to/addon/stylesheet.css');
85 }
86 ```
87
88 ## JavaScript
89
90 ### Global scripts
91
92 If your addon requires adding a script on all pages of Friendica, add the following hook:
93
94
95 ```php
96 function <addon>_install()
97 {
98         Addon::registerHook('footer', __FILE__, '<addon>_footer');
99         ...
100 }
101
102 function <addon>_footer(App $a)
103 {
104         $a->registerFooterScript(__DIR__ . '/relative/path/to/addon/script.js');
105 }
106 ```
107
108 ### JavaScript hooks
109
110 The main Friendica script provides hooks via events dispatched on the `document` property.
111 In your Javascript file included as described above, add your event listener like this:
112
113 ```js
114 document.addEventListener(name, callback);
115 ```
116
117 - *name* is the name of the hook and corresponds to a known Friendica JavaScript hook.
118 - *callback* is a JavaScript function to execute.
119
120 #### Current JavaScript hooks
121
122 ##### postprocess_liveupdate
123 Called at the end of the live update process (XmlHttpRequest) and on a post preview.
124 No additional data is provided.
125
126 ## Modules
127
128 Addons may also act as "modules" and intercept all page requests for a given URL path.
129 In order for a addon to act as a module it needs to define a function "addon_name_module()" which takes no arguments and needs not do anything.
130
131 If this function exists, you will now receive all page requests for "http://my.web.site/addon_name" - with any number of URL components as additional arguments.
132 These are parsed into an array $a->argv, with a corresponding $a->argc indicating the number of URL components.
133 So http://my.web.site/addon/arg1/arg2 would look for a module named "addon" and pass its module functions the $a App structure (which is available to many components).
134 This will include:
135
136 ```php
137 $a->argc = 3
138 $a->argv = array(0 => 'addon', 1 => 'arg1', 2 => 'arg2');
139 ```
140
141 Your module functions will often contain the function addon_name_content(App $a), which defines and returns the page body content.
142 They may also contain addon_name_post(App $a) which is called before the _content function and typically handles the results of POST forms.
143 You may also have addon_name_init(App $a) which is called very early on and often does module initialisation.
144
145 ## Templates
146
147 If your addon needs some template, you can use the Friendica template system.
148 Friendica uses [smarty3](http://www.smarty.net/) as a template engine.
149
150 Put your tpl files in the *templates/* subfolder of your addon.
151
152 In your code, like in the function addon_name_content(), load the template file and execute it passing needed values:
153
154 ```php
155 # load template file. first argument is the template name,
156 # second is the addon path relative to friendica top folder
157 $tpl = get_markup_template('mytemplate.tpl', 'addon/addon_name/');
158
159 # apply template. first argument is the loaded template,
160 # second an array of 'name' => 'values' to pass to template
161 $output = replace_macros($tpl, array(
162         'title' => 'My beautiful addon',
163 ));
164 ```
165
166 See also the wiki page [Quick Template Guide](https://github.com/friendica/friendica/wiki/Quick-Template-Guide).
167
168 ## Current PHP hooks
169
170 ### authenticate
171 Called when a user attempts to login.
172 `$b` is an array containing:
173
174 - **username**: the supplied username
175 - **password**: the supplied password
176 - **authenticated**: set this to non-zero to authenticate the user.
177 - **user_record**: successful authentication must also return a valid user record from the database
178
179 ### logged_in
180 Called after a user has successfully logged in.
181 `$b` contains the `$a->user` array.
182
183 ### display_item
184 Called when formatting a post for display.
185 $b is an array:
186
187 - **item**: The item (array) details pulled from the database
188 - **output**: the (string) HTML representation of this item prior to adding it to the page
189
190 ### post_local
191 Called when a status post or comment is entered on the local system.
192 `$b` is the item array of the information to be stored in the database.
193 Please note: body contents are bbcode - not HTML.
194
195 ### post_local_end
196 Called when a local status post or comment has been stored on the local system.
197 `$b` is the item array of the information which has just been stored in the database.
198 Please note: body contents are bbcode - not HTML
199
200 ### post_remote
201 Called when receiving a post from another source. This may also be used to post local activity or system generated messages.
202 `$b` is the item array of information to be stored in the database and the item body is bbcode.
203
204 ### settings_form
205 Called when generating the HTML for the user Settings page.
206 `$b` is the HTML string of the settings page before the final `</form>` tag.
207
208 ### settings_post
209 Called when the Settings pages are submitted.
210 `$b` is the $_POST array.
211
212 ### addon_settings
213 Called when generating the HTML for the addon settings page.
214 `$b` is the (string) HTML of the addon settings page before the final `</form>` tag.
215
216 ### addon_settings_post
217 Called when the Addon Settings pages are submitted.
218 `$b` is the $_POST array.
219
220 ### profile_post
221 Called when posting a profile page.
222 `$b` is the $_POST array.
223
224 ### profile_edit
225 Called prior to output of profile edit page.
226 `$b` is an array containing:
227
228 - **profile**: profile (array) record from the database
229 - **entry**: the (string) HTML of the generated entry
230
231 ### profile_advanced
232 Called when the HTML is generated for the Advanced profile, corresponding to the Profile tab within a person's profile page.
233 `$b` is the HTML string representation of the generated profile.
234 The profile array details are in `$a->profile`.
235
236 ### directory_item
237 Called from the Directory page when formatting an item for display.
238 `$b` is an array:
239
240 - **contact**: contact record array for the person from the database
241 - **entry**: the HTML string of the generated entry
242
243 ### profile_sidebar_enter
244 Called prior to generating the sidebar "short" profile for a page.
245 `$b` is the person's profile array
246
247 ### profile_sidebar
248 Called when generating the sidebar "short" profile for a page.
249 `$b` is an array:
250
251 - **profile**: profile record array for the person from the database
252 - **entry**: the HTML string of the generated entry
253
254 ### contact_block_end
255 Called when formatting the block of contacts/friends on a profile sidebar has completed.
256 `$b` is an array:
257
258 - **contacts**: array of contacts
259 - **output**: the generated HTML string of the contact block
260
261 ### bbcode
262 Called after conversion of bbcode to HTML.
263 `$b` is an HTML string converted text.
264
265 ### html2bbcode
266 Called after tag conversion of HTML to bbcode (e.g. remote message posting)
267 `$b` is a string converted text
268
269 ### head
270 Called when building the `<head>` sections.
271 Stylesheets should be registered using this hook.
272 `$b` is an HTML string of the `<head>` tag.
273
274 ### page_header
275 Called after building the page navigation section.
276 `$b` is a string HTML of nav region.
277
278 ### personal_xrd
279 Called prior to output of personal XRD file.
280 `$b` is an array:
281
282 - **user**: the user record array for the person
283 - **xml**: the complete XML string to be output
284
285 ### home_content
286 Called prior to output home page content, shown to unlogged users.
287 `$b` is the HTML sring of section region.
288
289 ### contact_edit
290 Called when editing contact details on an individual from the Contacts page.
291 $b is an array:
292
293 - **contact**: contact record (array) of target contact
294 - **output**: the (string) generated HTML of the contact edit page
295
296 ### contact_edit_post
297 Called when posting the contact edit page.
298 `$b` is the `$_POST` array
299
300 ### init_1
301 Called just after DB has been opened and before session start.
302 No hook data.
303
304 ### page_end
305 Called after HTML content functions have completed.
306 `$b` is (string) HTML of content div.
307
308 ### footer
309 Called after HTML content functions have completed.
310 Deferred Javascript files should be registered using this hook.
311 `$b` is (string) HTML of footer div/element.
312
313 ### avatar_lookup
314 Called when looking up the avatar. `$b` is an array:
315
316 - **size**: the size of the avatar that will be looked up
317 - **email**: email to look up the avatar for
318 - **url**: the (string) generated URL of the avatar
319
320 ### emailer_send_prepare
321 Called from `Emailer::send()` before building the mime message.
322 `$b` is an array of params to `Emailer::send()`.
323
324 - **fromName**: name of the sender
325 - **fromEmail**: email fo the sender
326 - **replyTo**: replyTo address to direct responses
327 - **toEmail**: destination email address
328 - **messageSubject**: subject of the message
329 - **htmlVersion**: html version of the message
330 - **textVersion**: text only version of the message
331 - **additionalMailHeader**: additions to the smtp mail header
332
333 ### emailer_send
334 Called before calling PHP's `mail()`.
335 `$b` is an array of params to `mail()`.
336
337 - **to**
338 - **subject**
339 - **body**
340 - **headers**
341
342 ### load_config
343 Called during `App` initialization to allow addons to load their own configuration file(s) with `App::loadConfigFile()`.
344
345 ### nav_info
346 Called after the navigational menu is build in `include/nav.php`.
347 `$b` is an array containing `$nav` from `include/nav.php`.
348
349 ### template_vars
350 Called before vars are passed to the template engine to render the page.
351 The registered function can add,change or remove variables passed to template.
352 `$b` is an array with:
353
354 - **template**: filename of template
355 - **vars**: array of vars passed to the template
356
357 ### acl_lookup_end
358 Called after the other queries have passed.
359 The registered function can add, change or remove the `acl_lookup()` variables.
360
361 - **results**: array of the acl_lookup() vars
362
363 ### prepare_body_init
364 Called at the start of prepare_body
365 Hook data:
366
367 - **item** (input/output): item array
368
369 ### prepare_body_content_filter
370 Called before the HTML conversion in prepare_body. If the item matches a content filter rule set by an addon, it should
371 just add the reason to the filter_reasons element of the hook data.
372 Hook data:
373
374 - **item**: item array (input)
375 - **filter_reasons** (input/output): reasons array
376
377 ### prepare_body
378 Called after the HTML conversion in `prepare_body()`.
379 Hook data:
380
381 - **item** (input): item array
382 - **html** (input/output): converted item body
383 - **is_preview** (input): post preview flag
384 - **filter_reasons** (input): reasons array
385
386 ### prepare_body_final
387 Called at the end of `prepare_body()`.
388 Hook data:
389
390 - **item**: item array (input)
391 - **html**: converted item body (input/output)
392
393 ### put_item_in_cache
394 Called after `prepare_text()` in `put_item_in_cache()`.
395 Hook data:
396
397 - **item** (input): item array
398 - **rendered-html** (input/output): final item body HTML
399 - **rendered-hash** (input/output): original item body hash
400
401 ### magic_auth_success
402 Called when a magic-auth was successful.
403 Hook data:
404
405     visitor => array with the contact record of the visitor
406     url => the query string
407
408 ## Complete list of hook callbacks
409
410 Here is a complete list of all hook callbacks with file locations (as of 24-Sep-2018). Please see the source for details of any hooks not documented above.
411
412 ### index.php
413
414     Addon::callHooks('init_1');
415     Addon::callHooks('app_menu', $arr);
416     Addon::callHooks('page_content_top', $a->page['content']);
417     Addon::callHooks($a->module.'_mod_init', $placeholder);
418     Addon::callHooks($a->module.'_mod_init', $placeholder);
419     Addon::callHooks($a->module.'_mod_post', $_POST);
420     Addon::callHooks($a->module.'_mod_afterpost', $placeholder);
421     Addon::callHooks($a->module.'_mod_content', $arr);
422     Addon::callHooks($a->module.'_mod_aftercontent', $arr);
423     Addon::callHooks('page_end', $a->page['content']);
424
425 ### include/api.php
426
427     Addon::callHooks('logged_in', $a->user);
428     Addon::callHooks('authenticate', $addon_auth);
429     Addon::callHooks('logged_in', $a->user);
430
431 ### include/enotify.php
432
433     Addon::callHooks('enotify', $h);
434     Addon::callHooks('enotify_store', $datarray);
435     Addon::callHooks('enotify_mail', $datarray);
436     Addon::callHooks('check_item_notification', $notification_data);
437
438 ### include/conversation.php
439
440     Addon::callHooks('conversation_start', $cb);
441     Addon::callHooks('render_location', $locate);
442     Addon::callHooks('display_item', $arr);
443     Addon::callHooks('display_item', $arr);
444     Addon::callHooks('item_photo_menu', $args);
445     Addon::callHooks('jot_tool', $jotplugins);
446
447 ### include/security.php
448
449     Addon::callHooks('logged_in', $a->user);
450
451 ### include/text.php
452
453     Addon::callHooks('contact_block_end', $arr);
454     Addon::callHooks('poke_verbs', $arr);
455     Addon::callHooks('put_item_in_cache', $hook_data);
456     Addon::callHooks('prepare_body_init', $item);
457     Addon::callHooks('prepare_body_content_filter', $hook_data);
458     Addon::callHooks('prepare_body', $hook_data);
459     Addon::callHooks('prepare_body_final', $hook_data);
460
461 ### include/items.php
462
463     Addon::callHooks('page_info_data', $data);
464
465 ### mod/directory.php
466
467     Addon::callHooks('directory_item', $arr);
468
469 ### mod/xrd.php
470
471     Addon::callHooks('personal_xrd', $arr);
472
473 ### mod/ping.php
474
475     Addon::callHooks('network_ping', $arr);
476
477 ### mod/parse_url.php
478
479     Addon::callHooks("parse_link", $arr);
480
481 ### mod/manage.php
482
483     Addon::callHooks('home_init', $ret);
484
485 ### mod/acl.php
486
487     Addon::callHooks('acl_lookup_end', $results);
488
489 ### mod/network.php
490
491     Addon::callHooks('network_content_init', $arr);
492     Addon::callHooks('network_tabs', $arr);
493
494 ### mod/friendica.php
495
496     Addon::callHooks('about_hook', $o);
497
498 ### mod/subthread.php
499
500     Addon::callHooks('post_local_end', $arr);
501
502 ### mod/profiles.php
503
504     Addon::callHooks('profile_post', $_POST);
505     Addon::callHooks('profile_edit', $arr);
506
507 ### mod/settings.php
508
509     Addon::callHooks('addon_settings_post', $_POST);
510     Addon::callHooks('connector_settings_post', $_POST);
511     Addon::callHooks('display_settings_post', $_POST);
512     Addon::callHooks('settings_post', $_POST);
513     Addon::callHooks('addon_settings', $settings_addons);
514     Addon::callHooks('connector_settings', $settings_connectors);
515     Addon::callHooks('display_settings', $o);
516     Addon::callHooks('settings_form', $o);
517
518 ### mod/photos.php
519
520     Addon::callHooks('photo_post_init', $_POST);
521     Addon::callHooks('photo_post_file', $ret);
522     Addon::callHooks('photo_post_end', $foo);
523     Addon::callHooks('photo_post_end', $foo);
524     Addon::callHooks('photo_post_end', $foo);
525     Addon::callHooks('photo_post_end', $foo);
526     Addon::callHooks('photo_post_end', intval($item_id));
527     Addon::callHooks('photo_upload_form', $ret);
528
529 ### mod/profile.php
530
531     Addon::callHooks('profile_advanced', $o);
532
533 ### mod/home.php
534
535     Addon::callHooks('home_init', $ret);
536     Addon::callHooks("home_content", $content);
537
538 ### mod/poke.php
539
540     Addon::callHooks('post_local_end', $arr);
541
542 ### mod/contacts.php
543
544     Addon::callHooks('contact_edit_post', $_POST);
545     Addon::callHooks('contact_edit', $arr);
546
547 ### mod/tagger.php
548
549     Addon::callHooks('post_local_end', $arr);
550
551 ### mod/lockview.php
552
553     Addon::callHooks('lockview_content', $item);
554
555 ### mod/uexport.php
556
557     Addon::callHooks('uexport_options', $options);
558
559 ### mod/register.php
560
561     Addon::callHooks('register_post', $arr);
562     Addon::callHooks('register_form', $arr);
563
564 ### mod/item.php
565
566     Addon::callHooks('post_local_start', $_REQUEST);
567     Addon::callHooks('post_local', $datarray);
568     Addon::callHooks('post_local_end', $datarray);
569
570 ### mod/editpost.php
571
572     Addon::callHooks('jot_tool', $jotplugins);
573
574 ### src/Network/FKOAuth1.php
575
576     Addon::callHooks('logged_in', $a->user);
577
578 ### src/Render/FriendicaSmartyEngine.php
579
580     Addon::callHooks("template_vars", $arr);
581
582 ### src/App.php
583
584     Addon::callHooks('load_config');
585         Addon::callHooks('head');
586     Addon::callHooks('footer');
587
588 ### src/Model/Item.php
589
590     Addon::callHooks('post_local', $item);
591     Addon::callHooks('post_remote', $item);
592     Addon::callHooks('post_local_end', $posted_item);
593     Addon::callHooks('post_remote_end', $posted_item);
594     Addon::callHooks('tagged', $arr);
595     Addon::callHooks('post_local_end', $new_item);
596
597 ### src/Model/Contact.php
598
599     Addon::callHooks('contact_photo_menu', $args);
600     Addon::callHooks('follow', $arr);
601
602 ### src/Model/Profile.php
603
604     Addon::callHooks('profile_sidebar_enter', $profile);
605     Addon::callHooks('profile_sidebar', $arr);
606     Addon::callHooks('profile_tabs', $arr);
607     Addon::callHooks('zrl_init', $arr);
608     Addon::callHooks('magic_auth_success', $arr);
609
610 ### src/Model/Event.php
611
612     Addon::callHooks('event_updated', $event['id']);
613     Addon::callHooks("event_created", $event['id']);
614
615 ### src/Model/User.php
616
617     Addon::callHooks('register_account', $uid);
618     Addon::callHooks('remove_user', $user);
619
620 ### src/Content/Text/BBCode.php
621
622     Addon::callHooks('bbcode', $text);
623     Addon::callHooks('bb2diaspora', $text);
624
625 ### src/Content/Text/HTML.php
626
627     Addon::callHooks('html2bbcode', $message);
628
629 ### src/Content/Smilies.php
630
631     Addon::callHooks('smilie', $params);
632
633 ### src/Content/Feature.php
634
635     Addon::callHooks('isEnabled', $arr);
636     Addon::callHooks('get', $arr);
637
638 ### src/Content/ContactSelector.php
639
640     Addon::callHooks('network_to_name', $nets);
641     Addon::callHooks('gender_selector', $select);
642     Addon::callHooks('sexpref_selector', $select);
643     Addon::callHooks('marital_selector', $select);
644
645 ### src/Content/OEmbed.php
646
647     Addon::callHooks('oembed_fetch_url', $embedurl, $j);
648
649 ### src/Content/Nav.php
650
651     Addon::callHooks('page_header', $a->page['nav']);
652     Addon::callHooks('nav_info', $nav);
653
654 ### src/Worker/Directory.php
655
656     Addon::callHooks('globaldir_update', $arr);
657
658 ### src/Worker/Notifier.php
659
660     Addon::callHooks('notifier_end', $target_item);
661
662 ### src/Worker/Queue.php
663
664     Addon::callHooks('queue_predeliver', $r);
665     Addon::callHooks('queue_deliver', $params);
666
667 ### src/Module/Login.php
668
669     Addon::callHooks('authenticate', $addon_auth);
670     Addon::callHooks('login_hook', $o);
671
672 ### src/Module/Logout.php
673
674     Addon::callHooks("logging_out");
675
676 ### src/Object/Post.php
677
678     Addon::callHooks('render_location', $locate);
679     Addon::callHooks('display_item', $arr);
680
681 ### src/Core/ACL.php
682
683     Addon::callHooks('contact_select_options', $x);
684     Addon::callHooks($a->module.'_pre_'.$selname, $arr);
685     Addon::callHooks($a->module.'_post_'.$selname, $o);
686     Addon::callHooks($a->module.'_pre_'.$selname, $arr);
687     Addon::callHooks($a->module.'_post_'.$selname, $o);
688     Addon::callHooks('jot_networks', $jotnets);
689
690 ### src/Core/Worker.php
691
692     Addon::callHooks("proc_run", $arr);
693
694 ### src/Util/Emailer.php
695
696     Addon::callHooks('emailer_send_prepare', $params);
697     Addon::callHooks("emailer_send", $hookdata);
698
699 ### src/Util/Map.php
700
701     Addon::callHooks('generate_map', $arr);
702     Addon::callHooks('generate_named_map', $arr);
703     Addon::callHooks('Map::getCoordinates', $arr);
704
705 ### src/Util/Network.php
706
707     Addon::callHooks('avatar_lookup', $avatar);
708
709 ### src/Util/ParseUrl.php
710
711     Addon::callHooks("getsiteinfo", $siteinfo);
712
713 ### src/Protocol/DFRN.php
714
715     Addon::callHooks('atom_feed_end', $atom);
716     Addon::callHooks('atom_feed_end', $atom);
717
718 ### view/js/main.js
719
720     document.dispatchEvent(new Event('postprocess_liveupdate'));