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