1 Friendica Addon development
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).
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".
19 Addons should contain a comment block with the four following parameters:
22 * Name: My Great Addon
23 * Description: This is what my addon does. It's really cool.
25 * Author: John Q. Public <john@myfriendicasite.com>
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.
33 Register your addon hooks during installation.
35 Addon::registerHook($hookname, $file, $function);
37 $hookname is a string and corresponds to a known Friendica PHP hook.
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.
42 $function is a string and is the name of the function which will be executed when the hook is called.
45 Your hook callback functions will be called with at least one and possibly two arguments
47 function myhook_function(App $a, &$b) {
52 If you wish to make changes to the calling data, you must declare them as reference variables (with `&`) during function declaration.
55 $a is the Friendica `App` class.
56 It contains a wealth of information about the current state of Friendica:
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.
63 It is recommeded you call this `$a` to match its usage elsewhere.
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.
70 ## JavaScript addon hooks
74 Make sure your JavaScript addon file (addon/*addon_name*/*addon_name*.js) is listed in the document response.
76 In your addon install function, add:
79 Addon::registerHook('template_vars', __FILE__, '<addon_name>_template_vars');
82 In your addon uninstall function, add:
85 Addon::unregisterHook('template_vars', __FILE__, '<addon_name>_template_vars');
88 Then, add your addon name to the *addon_hooks* template variable array:
91 function <addon_name>_template_vars($a, &$arr)
93 if (!array_key_exists('addon_hooks', $arr['vars']))
95 $arr['vars']['addon_hooks'] = array();
97 $arr['vars']['addon_hooks'][] = "<addon_name>";
103 Register your addon hooks in file `addon/*addon_name*/*addon_name*.js`.
106 document.addEventListener(name, callback);
109 *name* is the name of the hook and corresponds to a known Friendica JavaScript hook.
110 *callback* is a JavaScript function to execute.
114 Addons may also act as "modules" and intercept all page requests for a given URL path.
115 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.
117 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.
118 These are parsed into an array $a->argv, with a corresponding $a->argc indicating the number of URL components.
119 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).
124 $a->argv = array(0 => 'addon', 1 => 'arg1', 2 => 'arg2');
127 Your module functions will often contain the function addon_name_content(App $a), which defines and returns the page body content.
128 They may also contain addon_name_post(App $a) which is called before the _content function and typically handles the results of POST forms.
129 You may also have addon_name_init(App $a) which is called very early on and often does module initialisation.
133 If your addon needs some template, you can use the Friendica template system.
134 Friendica uses [smarty3](http://www.smarty.net/) as a template engine.
136 Put your tpl files in the *templates/* subfolder of your addon.
138 In your code, like in the function addon_name_content(), load the template file and execute it passing needed values:
141 # load template file. first argument is the template name,
142 # second is the addon path relative to friendica top folder
143 $tpl = get_markup_template('mytemplate.tpl', 'addon/addon_name/');
145 # apply template. first argument is the loaded template,
146 # second an array of 'name' => 'values' to pass to template
147 $output = replace_macros($tpl, array(
148 'title' => 'My beautiful addon',
152 See also the wiki page [Quick Template Guide](https://github.com/friendica/friendica/wiki/Quick-Template-Guide).
157 Called when a user attempts to login.
158 `$b` is an array containing:
160 - **username**: the supplied username
161 - **password**: the supplied password
162 - **authenticated**: set this to non-zero to authenticate the user.
163 - **user_record**: successful authentication must also return a valid user record from the database
166 Called after a user has successfully logged in.
167 `$b` contains the `$a->user` array.
170 Called when formatting a post for display.
173 - **item**: The item (array) details pulled from the database
174 - **output**: the (string) HTML representation of this item prior to adding it to the page
177 Called when a status post or comment is entered on the local system.
178 `$b` is the item array of the information to be stored in the database.
179 Please note: body contents are bbcode - not HTML.
182 Called when a local status post or comment has been stored on the local system.
183 `$b` is the item array of the information which has just been stored in the database.
184 Please note: body contents are bbcode - not HTML
187 Called when receiving a post from another source. This may also be used to post local activity or system generated messages.
188 `$b` is the item array of information to be stored in the database and the item body is bbcode.
191 Called when generating the HTML for the user Settings page.
192 `$b` is the HTML string of the settings page before the final `</form>` tag.
195 Called when the Settings pages are submitted.
196 `$b` is the $_POST array.
199 Called when generating the HTML for the addon settings page.
200 `$b` is the (string) HTML of the addon settings page before the final `</form>` tag.
202 ### addon_settings_post
203 Called when the Addon Settings pages are submitted.
204 `$b` is the $_POST array.
207 Called when posting a profile page.
208 `$b` is the $_POST array.
211 Called prior to output of profile edit page.
212 `$b` is an array containing:
214 - **profile**: profile (array) record from the database
215 - **entry**: the (string) HTML of the generated entry
218 Called when the HTML is generated for the Advanced profile, corresponding to the Profile tab within a person's profile page.
219 `$b` is the HTML string representation of the generated profile.
220 The profile array details are in `$a->profile`.
223 Called from the Directory page when formatting an item for display.
226 - **contact**: contact record array for the person from the database
227 - **entry**: the HTML string of the generated entry
229 ### profile_sidebar_enter
230 Called prior to generating the sidebar "short" profile for a page.
231 `$b` is the person's profile array
234 Called when generating the sidebar "short" profile for a page.
237 - **profile**: profile record array for the person from the database
238 - **entry**: the HTML string of the generated entry
240 ### contact_block_end
241 Called when formatting the block of contacts/friends on a profile sidebar has completed.
244 - **contacts**: array of contacts
245 - **output**: the generated HTML string of the contact block
248 Called after conversion of bbcode to HTML.
249 `$b` is an HTML string converted text.
252 Called after tag conversion of HTML to bbcode (e.g. remote message posting)
253 `$b` is a string converted text
256 Called after building the page navigation section.
257 `$b` is a string HTML of nav region.
260 Called prior to output of personal XRD file.
263 - **user**: the user record array for the person
264 - **xml**: the complete XML string to be output
267 Called prior to output home page content, shown to unlogged users.
268 `$b` is the HTML sring of section region.
271 Called when editing contact details on an individual from the Contacts page.
274 - **contact**: contact record (array) of target contact
275 - **output**: the (string) generated HTML of the contact edit page
277 ### contact_edit_post
278 Called when posting the contact edit page.
279 `$b` is the `$_POST` array
282 Called just after DB has been opened and before session start.
286 Called after HTML content functions have completed.
287 `$b` is (string) HTML of content div.
290 Called when looking up the avatar. `$b` is an array:
292 - **size**: the size of the avatar that will be looked up
293 - **email**: email to look up the avatar for
294 - **url**: the (string) generated URL of the avatar
296 ### emailer_send_prepare
297 Called from `Emailer::send()` before building the mime message.
298 `$b` is an array of params to `Emailer::send()`.
300 - **fromName**: name of the sender
301 - **fromEmail**: email fo the sender
302 - **replyTo**: replyTo address to direct responses
303 - **toEmail**: destination email address
304 - **messageSubject**: subject of the message
305 - **htmlVersion**: html version of the message
306 - **textVersion**: text only version of the message
307 - **additionalMailHeader**: additions to the smtp mail header
310 Called before calling PHP's `mail()`.
311 `$b` is an array of params to `mail()`.
319 Called during `App` initialization to allow addons to load their own configuration file(s) with `App::loadConfigFile()`.
322 Called after the navigational menu is build in `include/nav.php`.
323 `$b` is an array containing `$nav` from `include/nav.php`.
326 Called before vars are passed to the template engine to render the page.
327 The registered function can add,change or remove variables passed to template.
328 `$b` is an array with:
330 - **template**: filename of template
331 - **vars**: array of vars passed to the template
334 Called after the other queries have passed.
335 The registered function can add, change or remove the `acl_lookup()` variables.
337 - **results**: array of the acl_lookup() vars
339 ### prepare_body_init
340 Called at the start of prepare_body
343 - **item** (input/output): item array
345 ### prepare_body_content_filter
346 Called before the HTML conversion in prepare_body. If the item matches a content filter rule set by an addon, it should
347 just add the reason to the filter_reasons element of the hook data.
350 - **item**: item array (input)
351 - **filter_reasons** (input/output): reasons array
354 Called after the HTML conversion in `prepare_body()`.
357 - **item** (input): item array
358 - **html** (input/output): converted item body
359 - **is_preview** (input): post preview flag
360 - **filter_reasons** (input): reasons array
362 ### prepare_body_final
363 Called at the end of `prepare_body()`.
366 - **item**: item array (input)
367 - **html**: converted item body (input/output)
369 ### put_item_in_cache
370 Called after `prepare_text()` in `put_item_in_cache()`.
373 - **item** (input): item array
374 - **rendered-html** (input/output): final item body HTML
375 - **rendered-hash** (input/output): original item body hash
377 ### magic_auth_success
378 Called when a magic-auth was successful.
381 visitor => array with the contact record of the visitor
382 url => the query string
384 ## Current JavaScript hooks
386 ### postprocess_liveupdate
387 Called at the end of the live update process (XmlHttpRequest)
389 ## Complete list of hook callbacks
391 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.
395 Addon::callHooks('init_1');
396 Addon::callHooks('app_menu', $arr);
397 Addon::callHooks('page_content_top', $a->page['content']);
398 Addon::callHooks($a->module.'_mod_init', $placeholder);
399 Addon::callHooks($a->module.'_mod_init', $placeholder);
400 Addon::callHooks($a->module.'_mod_post', $_POST);
401 Addon::callHooks($a->module.'_mod_afterpost', $placeholder);
402 Addon::callHooks($a->module.'_mod_content', $arr);
403 Addon::callHooks($a->module.'_mod_aftercontent', $arr);
404 Addon::callHooks('page_end', $a->page['content']);
408 Addon::callHooks('logged_in', $a->user);
409 Addon::callHooks('authenticate', $addon_auth);
410 Addon::callHooks('logged_in', $a->user);
412 ### include/enotify.php
414 Addon::callHooks('enotify', $h);
415 Addon::callHooks('enotify_store', $datarray);
416 Addon::callHooks('enotify_mail', $datarray);
417 Addon::callHooks('check_item_notification', $notification_data);
419 ### include/conversation.php
421 Addon::callHooks('conversation_start', $cb);
422 Addon::callHooks('render_location', $locate);
423 Addon::callHooks('display_item', $arr);
424 Addon::callHooks('display_item', $arr);
425 Addon::callHooks('item_photo_menu', $args);
426 Addon::callHooks('jot_tool', $jotplugins);
428 ### include/security.php
430 Addon::callHooks('logged_in', $a->user);
434 Addon::callHooks('contact_block_end', $arr);
435 Addon::callHooks('poke_verbs', $arr);
436 Addon::callHooks('put_item_in_cache', $hook_data);
437 Addon::callHooks('prepare_body_init', $item);
438 Addon::callHooks('prepare_body_content_filter', $hook_data);
439 Addon::callHooks('prepare_body', $hook_data);
440 Addon::callHooks('prepare_body_final', $hook_data);
442 ### include/items.php
444 Addon::callHooks('page_info_data', $data);
446 ### mod/directory.php
448 Addon::callHooks('directory_item', $arr);
452 Addon::callHooks('personal_xrd', $arr);
456 Addon::callHooks('network_ping', $arr);
458 ### mod/parse_url.php
460 Addon::callHooks("parse_link", $arr);
464 Addon::callHooks('home_init', $ret);
468 Addon::callHooks('acl_lookup_end', $results);
472 Addon::callHooks('network_content_init', $arr);
473 Addon::callHooks('network_tabs', $arr);
475 ### mod/friendica.php
477 Addon::callHooks('about_hook', $o);
479 ### mod/subthread.php
481 Addon::callHooks('post_local_end', $arr);
485 Addon::callHooks('profile_post', $_POST);
486 Addon::callHooks('profile_edit', $arr);
490 Addon::callHooks('addon_settings_post', $_POST);
491 Addon::callHooks('connector_settings_post', $_POST);
492 Addon::callHooks('display_settings_post', $_POST);
493 Addon::callHooks('settings_post', $_POST);
494 Addon::callHooks('addon_settings', $settings_addons);
495 Addon::callHooks('connector_settings', $settings_connectors);
496 Addon::callHooks('display_settings', $o);
497 Addon::callHooks('settings_form', $o);
501 Addon::callHooks('photo_post_init', $_POST);
502 Addon::callHooks('photo_post_file', $ret);
503 Addon::callHooks('photo_post_end', $foo);
504 Addon::callHooks('photo_post_end', $foo);
505 Addon::callHooks('photo_post_end', $foo);
506 Addon::callHooks('photo_post_end', $foo);
507 Addon::callHooks('photo_post_end', intval($item_id));
508 Addon::callHooks('photo_upload_form', $ret);
512 Addon::callHooks('profile_advanced', $o);
516 Addon::callHooks('home_init', $ret);
517 Addon::callHooks("home_content", $content);
521 Addon::callHooks('post_local_end', $arr);
525 Addon::callHooks('contact_edit_post', $_POST);
526 Addon::callHooks('contact_edit', $arr);
530 Addon::callHooks('post_local_end', $arr);
534 Addon::callHooks('lockview_content', $item);
538 Addon::callHooks('uexport_options', $options);
542 Addon::callHooks('register_post', $arr);
543 Addon::callHooks('register_form', $arr);
547 Addon::callHooks('post_local_start', $_REQUEST);
548 Addon::callHooks('post_local', $datarray);
549 Addon::callHooks('post_local_end', $datarray);
553 Addon::callHooks('jot_tool', $jotplugins);
555 ### src/Network/FKOAuth1.php
557 Addon::callHooks('logged_in', $a->user);
559 ### src/Render/FriendicaSmartyEngine.php
561 Addon::callHooks("template_vars", $arr);
565 Addon::callHooks('load_config');
567 ### src/Model/Item.php
569 Addon::callHooks('post_local', $item);
570 Addon::callHooks('post_remote', $item);
571 Addon::callHooks('post_local_end', $posted_item);
572 Addon::callHooks('post_remote_end', $posted_item);
573 Addon::callHooks('tagged', $arr);
574 Addon::callHooks('post_local_end', $new_item);
576 ### src/Model/Contact.php
578 Addon::callHooks('contact_photo_menu', $args);
579 Addon::callHooks('follow', $arr);
581 ### src/Model/Profile.php
583 Addon::callHooks('profile_sidebar_enter', $profile);
584 Addon::callHooks('profile_sidebar', $arr);
585 Addon::callHooks('profile_tabs', $arr);
586 Addon::callHooks('zrl_init', $arr);
587 Addon::callHooks('magic_auth_success', $arr);
589 ### src/Model/Event.php
591 Addon::callHooks('event_updated', $event['id']);
592 Addon::callHooks("event_created", $event['id']);
594 ### src/Model/User.php
596 Addon::callHooks('register_account', $uid);
597 Addon::callHooks('remove_user', $user);
599 ### src/Content/Text/BBCode.php
601 Addon::callHooks('bbcode', $text);
602 Addon::callHooks('bb2diaspora', $text);
604 ### src/Content/Text/HTML.php
606 Addon::callHooks('html2bbcode', $message);
608 ### src/Content/Smilies.php
610 Addon::callHooks('smilie', $params);
612 ### src/Content/Feature.php
614 Addon::callHooks('isEnabled', $arr);
615 Addon::callHooks('get', $arr);
617 ### src/Content/ContactSelector.php
619 Addon::callHooks('network_to_name', $nets);
620 Addon::callHooks('gender_selector', $select);
621 Addon::callHooks('sexpref_selector', $select);
622 Addon::callHooks('marital_selector', $select);
624 ### src/Content/OEmbed.php
626 Addon::callHooks('oembed_fetch_url', $embedurl, $j);
628 ### src/Content/Nav.php
630 Addon::callHooks('page_header', $a->page['nav']);
631 Addon::callHooks('nav_info', $nav);
633 ### src/Worker/Directory.php
635 Addon::callHooks('globaldir_update', $arr);
637 ### src/Worker/Notifier.php
639 Addon::callHooks('notifier_end', $target_item);
641 ### src/Worker/Queue.php
643 Addon::callHooks('queue_predeliver', $r);
644 Addon::callHooks('queue_deliver', $params);
646 ### src/Module/Login.php
648 Addon::callHooks('authenticate', $addon_auth);
649 Addon::callHooks('login_hook', $o);
651 ### src/Module/Logout.php
653 Addon::callHooks("logging_out");
655 ### src/Object/Post.php
657 Addon::callHooks('render_location', $locate);
658 Addon::callHooks('display_item', $arr);
662 Addon::callHooks('contact_select_options', $x);
663 Addon::callHooks($a->module.'_pre_'.$selname, $arr);
664 Addon::callHooks($a->module.'_post_'.$selname, $o);
665 Addon::callHooks($a->module.'_pre_'.$selname, $arr);
666 Addon::callHooks($a->module.'_post_'.$selname, $o);
667 Addon::callHooks('jot_networks', $jotnets);
669 ### src/Core/Worker.php
671 Addon::callHooks("proc_run", $arr);
673 ### src/Util/Emailer.php
675 Addon::callHooks('emailer_send_prepare', $params);
676 Addon::callHooks("emailer_send", $hookdata);
680 Addon::callHooks('generate_map', $arr);
681 Addon::callHooks('generate_named_map', $arr);
682 Addon::callHooks('Map::getCoordinates', $arr);
684 ### src/Util/Network.php
686 Addon::callHooks('avatar_lookup', $avatar);
688 ### src/Util/ParseUrl.php
690 Addon::callHooks("getsiteinfo", $siteinfo);
692 ### src/Protocol/DFRN.php
694 Addon::callHooks('atom_feed_end', $atom);
695 Addon::callHooks('atom_feed_end', $atom);
699 document.dispatchEvent(new Event('postprocess_liveupdate'));