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