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