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