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