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