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).
12 Addon names are used in file paths and functions names, and as such:
13 - Can't contain spaces or punctuation.
14 - Can't start with a number.
18 You can provide human-readable information about your addon in the first multi-line comment of your addon file.
24 * Name: {Human-readable name}
25 * Description: {Short description}
27 * Author: {Author1 Name}
28 * Author: {Author2 Name} <{Author profile link}>
29 * Maintainer: {Maintainer1 Name}
30 * Maintainer: {Maintainer2 Name} <{Maintainer profile link}>
31 * Status: {Unsupported|Arbitrary status}
35 You can also provide a longer documentation in a `README` or `README.md` file.
36 The latter will be converted from Markdown to HTML in the addon detail page.
40 If your addon uses hooks, they have to be registered in a `<addon>_install()` function.
41 This function also allows to perform arbitrary actions your addon needs to function properly.
43 Uninstalling an addon automatically unregisters any hook it registered, but if you need to provide specific uninstallation steps, you can add them in a `<addon>_uninstall()` function.
45 The install and uninstall functions will be called (i.e. re-installed) if the addon changes after installation.
46 Therefore your uninstall should not destroy data and install should consider that data may already exist.
47 Future extensions may provide for "setup" amd "remove".
51 Register your addon hooks during installation.
53 \Friendica\Core\Hook::register($hookname, $file, $function);
55 `$hookname` is a string and corresponds to a known Friendica PHP hook.
57 `$file` is a pathname relative to the top-level Friendica directory.
58 This *should* be 'addon/*addon_name*/*addon_name*.php' in most cases and can be shortened to `__FILE__`.
60 `$function` is a string and is the name of the function which will be executed when the hook is called.
63 Your hook callback functions will be called with at least one and possibly two arguments
65 function <addon>_<hookname>(App $a, &$b) {
69 If you wish to make changes to the calling data, you must declare them as reference variables (with `&`) during function declaration.
72 $a is the Friendica `App` class.
73 It contains a wealth of information about the current state of Friendica:
75 * which module has been called,
76 * configuration information,
77 * the page contents at the point the hook was invoked,
78 * profile and user information, etc.
80 It is recommeded you call this `$a` to match its usage elsewhere.
83 $b can be called anything you like.
84 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.
85 Remember to declare it with `&` if you wish to alter it.
89 Your addon can provide user-specific settings via the `addon_settings` PHP hook, but it can also provide node-wide settings in the administration page of your addon.
91 Simply declare a `<addon>_addon_admin(App $a)` function to display the form and a `<addon>_addon_admin_post(App $a)` function to process the data from the form.
95 If your addon requires adding a stylesheet on all pages of Friendica, add the following hook:
98 function <addon>_install()
100 \Friendica\Core\Hook::register('head', __FILE__, '<addon>_head');
105 function <addon>_head(App $a)
107 \Friendica\DI::page()->registerStylesheet(__DIR__ . '/relative/path/to/addon/stylesheet.css');
111 `__DIR__` is the folder path of your addon.
117 If your addon requires adding a script on all pages of Friendica, add the following hook:
121 function <addon>_install()
123 \Friendica\Core\Hook::register('footer', __FILE__, '<addon>_footer');
127 function <addon>_footer(App $a)
129 \Friendica\DI::page()->registerFooterScript(__DIR__ . '/relative/path/to/addon/script.js');
133 `__DIR__` is the folder path of your addon.
137 The main Friendica script provides hooks via events dispatched on the `document` property.
138 In your Javascript file included as described above, add your event listener like this:
141 document.addEventListener(name, callback);
144 - *name* is the name of the hook and corresponds to a known Friendica JavaScript hook.
145 - *callback* is a JavaScript anonymous function to execute.
147 More info about Javascript event listeners: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
149 #### Current JavaScript hooks
151 ##### postprocess_liveupdate
152 Called at the end of the live update process (XmlHttpRequest) and on a post preview.
153 No additional data is provided.
157 Addons may also act as "modules" and intercept all page requests for a given URL path.
158 In order for a addon to act as a module it needs to declare an empty function `<addon>_module()`.
160 If this function exists, you will now receive all page requests for `https://my.web.site/<addon>` - with any number of URL components as additional arguments.
161 These are parsed into an array $a->argv, with a corresponding $a->argc indicating the number of URL components.
162 So `https://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).
167 $a->argv = array(0 => 'addon', 1 => 'arg1', 2 => 'arg2');
170 To display a module page, you need to declare the function `<addon>_content(App $a)`, which defines and returns the page body content.
171 They may also contain `<addon>_post(App $a)` which is called before the `<addon>_content` function and typically handles the results of POST forms.
172 You may also have `<addon>_init(App $a)` which is called before `<addon>_content` and should include common logic to your module.
176 If your addon needs some template, you can use the Friendica template system.
177 Friendica uses [smarty3](http://www.smarty.net/) as a template engine.
179 Put your tpl files in the *templates/* subfolder of your addon.
181 In your code, like in the function addon_name_content(), load the template file and execute it passing needed values:
184 use Friendica\Core\Renderer;
186 # load template file. first argument is the template name,
187 # second is the addon path relative to friendica top folder
188 $tpl = Renderer::getMarkupTemplate('mytemplate.tpl', __DIR__);
190 # apply template. first argument is the loaded template,
191 # second an array of 'name' => 'values' to pass to template
192 $output = Renderer::replaceMacros($tpl, array(
193 'title' => 'My beautiful addon',
197 See also the wiki page [Quick Template Guide](https://github.com/friendica/friendica/wiki/Quick-Template-Guide).
202 Called when a user attempts to login.
203 `$b` is an array containing:
205 - **username**: the supplied username
206 - **password**: the supplied password
207 - **authenticated**: set this to non-zero to authenticate the user.
208 - **user_record**: successful authentication must also return a valid user record from the database
211 Called after a user has successfully logged in.
212 `$b` contains the `$a->user` array.
215 Called when formatting a post for display.
218 - **item**: The item (array) details pulled from the database
219 - **output**: the (string) HTML representation of this item prior to adding it to the page
222 Called when a status post or comment is entered on the local system.
223 `$b` is the item array of the information to be stored in the database.
224 Please note: body contents are bbcode - not HTML.
227 Called when a local status post or comment has been stored on the local system.
228 `$b` is the item array of the information which has just been stored in the database.
229 Please note: body contents are bbcode - not HTML
232 Called when receiving a post from another source. This may also be used to post local activity or system generated messages.
233 `$b` is the item array of information to be stored in the database and the item body is bbcode.
236 Called when generating the HTML for the user Settings page.
237 `$b` is the HTML string of the settings page before the final `</form>` tag.
240 Called when the Settings pages are submitted.
241 `$b` is the $_POST array.
244 Called when generating the HTML for the addon settings page.
245 `$b` is the (string) HTML of the addon settings page before the final `</form>` tag.
247 ### addon_settings_post
248 Called when the Addon Settings pages are submitted.
249 `$b` is the $_POST array.
252 Called when posting a profile page.
253 `$b` is the $_POST array.
256 Called prior to output of profile edit page.
257 `$b` is an array containing:
259 - **profile**: profile (array) record from the database
260 - **entry**: the (string) HTML of the generated entry
263 Called when the HTML is generated for the Advanced profile, corresponding to the Profile tab within a person's profile page.
264 `$b` is the HTML string representation of the generated profile.
265 The profile array details are in `$a->profile`.
268 Called from the Directory page when formatting an item for display.
271 - **contact**: contact record array for the person from the database
272 - **entry**: the HTML string of the generated entry
274 ### profile_sidebar_enter
275 Called prior to generating the sidebar "short" profile for a page.
276 `$b` is the person's profile array
279 Called when generating the sidebar "short" profile for a page.
282 - **profile**: profile record array for the person from the database
283 - **entry**: the HTML string of the generated entry
285 ### contact_block_end
286 Called when formatting the block of contacts/friends on a profile sidebar has completed.
289 - **contacts**: array of contacts
290 - **output**: the generated HTML string of the contact block
293 Called after conversion of bbcode to HTML.
294 `$b` is an HTML string converted text.
297 Called after tag conversion of HTML to bbcode (e.g. remote message posting)
298 `$b` is a string converted text
301 Called when building the `<head>` sections.
302 Stylesheets should be registered using this hook.
303 `$b` is an HTML string of the `<head>` tag.
306 Called after building the page navigation section.
307 `$b` is a string HTML of nav region.
310 Called prior to output of personal XRD file.
313 - **user**: the user record array for the person
314 - **xml**: the complete XML string to be output
317 Called prior to output home page content, shown to unlogged users.
318 `$b` is the HTML sring of section region.
321 Called when editing contact details on an individual from the Contacts page.
324 - **contact**: contact record (array) of target contact
325 - **output**: the (string) generated HTML of the contact edit page
327 ### contact_edit_post
328 Called when posting the contact edit page.
329 `$b` is the `$_POST` array
332 Called just after DB has been opened and before session start.
336 Called after HTML content functions have completed.
337 `$b` is (string) HTML of content div.
340 Called after HTML content functions have completed.
341 Deferred Javascript files should be registered using this hook.
342 `$b` is (string) HTML of footer div/element.
345 Called when looking up the avatar. `$b` is an array:
347 - **size**: the size of the avatar that will be looked up
348 - **email**: email to look up the avatar for
349 - **url**: the (string) generated URL of the avatar
351 ### emailer_send_prepare
352 Called from `Emailer::send()` before building the mime message.
353 `$b` is an array of params to `Emailer::send()`.
355 - **fromName**: name of the sender
356 - **fromEmail**: email fo the sender
357 - **replyTo**: replyTo address to direct responses
358 - **toEmail**: destination email address
359 - **messageSubject**: subject of the message
360 - **htmlVersion**: html version of the message
361 - **textVersion**: text only version of the message
362 - **additionalMailHeader**: additions to the smtp mail header
363 - **sent**: default false, if set to true in the hook, the default mailer will be skipped.
366 Called before calling PHP's `mail()`.
367 `$b` is an array of params to `mail()`.
373 - **sent**: default false, if set to true in the hook, the default mailer will be skipped.
376 Called during `App` initialization to allow addons to load their own configuration file(s) with `App::loadConfigFile()`.
379 Called after the navigational menu is build in `include/nav.php`.
380 `$b` is an array containing `$nav` from `include/nav.php`.
383 Called before vars are passed to the template engine to render the page.
384 The registered function can add,change or remove variables passed to template.
385 `$b` is an array with:
387 - **template**: filename of template
388 - **vars**: array of vars passed to the template
391 Called after the other queries have passed.
392 The registered function can add, change or remove the `acl_lookup()` variables.
394 - **results**: array of the acl_lookup() vars
396 ### prepare_body_init
397 Called at the start of prepare_body
400 - **item** (input/output): item array
402 ### prepare_body_content_filter
403 Called before the HTML conversion in prepare_body. If the item matches a content filter rule set by an addon, it should
404 just add the reason to the filter_reasons element of the hook data.
407 - **item**: item array (input)
408 - **filter_reasons** (input/output): reasons array
411 Called after the HTML conversion in `prepare_body()`.
414 - **item** (input): item array
415 - **html** (input/output): converted item body
416 - **is_preview** (input): post preview flag
417 - **filter_reasons** (input): reasons array
419 ### prepare_body_final
420 Called at the end of `prepare_body()`.
423 - **item**: item array (input)
424 - **html**: converted item body (input/output)
426 ### put_item_in_cache
427 Called after `prepare_text()` in `put_item_in_cache()`.
430 - **item** (input): item array
431 - **rendered-html** (input/output): final item body HTML
432 - **rendered-hash** (input/output): original item body hash
434 ### magic_auth_success
435 Called when a magic-auth was successful.
438 visitor => array with the contact record of the visitor
439 url => the query string
442 Called when displaying the post permission screen.
443 Hook data is a list of form fields that need to be displayed along the ACL.
444 Form field array structure is:
446 - **type**: `checkbox` or `select`.
447 - **field**: Standard field data structure to be used by `field_checkbox.tpl` and `field_select.tpl`.
449 For `checkbox`, **field** is:
450 - [0] (String): Form field name; Mandatory.
451 - [1]: (String): Form field label; Optional, default is none.
452 - [2]: (Boolean): Whether the checkbox should be checked by default; Optional, default is false.
453 - [3]: (String): Additional help text; Optional, default is none.
454 - [4]: (String): Additional HTML attributes; Optional, default is none.
456 For `select`, **field** is:
457 - [0] (String): Form field name; Mandatory.
458 - [1] (String): Form field label; Optional, default is none.
459 - [2] (Boolean): Default value to be selected by default; Optional, default is none.
460 - [3] (String): Additional help text; Optional, default is none.
461 - [4] (Array): Associative array of options. Item key is option value, item value is option label; Mandatory.
464 Called just before dispatching the router.
465 Hook data is a `\FastRoute\RouterCollector` object that should be used to add addon routes pointing to classes.
467 **Notice**: The class whose name is provided in the route handler must be reachable via auto-loader.
471 Called before trying to detect the target network of a URL.
472 If any registered hook function sets the `result` key of the hook data array, it will be returned immediately.
473 Hook functions should also return immediately if the hook data contains an existing result.
477 - **uri** (input): the profile URI.
478 - **network** (input): the target network (can be empty for auto-detection).
479 - **uid** (input): the user to return the contact data for (can be empty for public contacts).
480 - **result** (output): Set by the hook function to indicate a successful detection.
482 ## Complete list of hook callbacks
484 Here is a complete list of all hook callbacks with file locations (as of 24-Sep-2018). Please see the source for details of any hooks not documented above.
488 Hook::callAll('init_1');
489 Hook::callAll('app_menu', $arr);
490 Hook::callAll('page_content_top', DI::page()['content']);
491 Hook::callAll($a->module.'_mod_init', $placeholder);
492 Hook::callAll($a->module.'_mod_init', $placeholder);
493 Hook::callAll($a->module.'_mod_post', $_POST);
494 Hook::callAll($a->module.'_mod_afterpost', $placeholder);
495 Hook::callAll($a->module.'_mod_content', $arr);
496 Hook::callAll($a->module.'_mod_aftercontent', $arr);
497 Hook::callAll('page_end', DI::page()['content']);
501 Hook::callAll('logged_in', $a->user);
502 Hook::callAll('authenticate', $addon_auth);
503 Hook::callAll('logged_in', $a->user);
505 ### include/enotify.php
507 Hook::callAll('enotify', $h);
508 Hook::callAll('enotify_store', $datarray);
509 Hook::callAll('enotify_mail', $datarray);
510 Hook::callAll('check_item_notification', $notification_data);
512 ### include/conversation.php
514 Hook::callAll('conversation_start', $cb);
515 Hook::callAll('render_location', $locate);
516 Hook::callAll('display_item', $arr);
517 Hook::callAll('display_item', $arr);
518 Hook::callAll('item_photo_menu', $args);
519 Hook::callAll('jot_tool', $jotplugins);
521 ### mod/directory.php
523 Hook::callAll('directory_item', $arr);
527 Hook::callAll('personal_xrd', $arr);
531 Hook::callAll('network_ping', $arr);
533 ### mod/parse_url.php
535 Hook::callAll("parse_link", $arr);
537 ### src/Module/Delegation.php
539 Hook::callAll('home_init', $ret);
543 Hook::callAll('acl_lookup_end', $results);
547 Hook::callAll('network_content_init', $arr);
548 Hook::callAll('network_tabs', $arr);
550 ### mod/friendica.php
552 Hook::callAll('about_hook', $o);
554 ### mod/subthread.php
556 Hook::callAll('post_local_end', $arr);
560 Hook::callAll('profile_post', $_POST);
561 Hook::callAll('profile_edit', $arr);
565 Hook::callAll('addon_settings_post', $_POST);
566 Hook::callAll('connector_settings_post', $_POST);
567 Hook::callAll('display_settings_post', $_POST);
568 Hook::callAll('settings_post', $_POST);
569 Hook::callAll('addon_settings', $settings_addons);
570 Hook::callAll('connector_settings', $settings_connectors);
571 Hook::callAll('display_settings', $o);
572 Hook::callAll('settings_form', $o);
576 Hook::callAll('photo_post_init', $_POST);
577 Hook::callAll('photo_post_file', $ret);
578 Hook::callAll('photo_post_end', $foo);
579 Hook::callAll('photo_post_end', $foo);
580 Hook::callAll('photo_post_end', $foo);
581 Hook::callAll('photo_post_end', $foo);
582 Hook::callAll('photo_post_end', intval($item_id));
583 Hook::callAll('photo_upload_form', $ret);
587 Hook::callAll('profile_advanced', $o);
591 Hook::callAll('home_init', $ret);
592 Hook::callAll("home_content", $content);
596 Hook::callAll('post_local_end', $arr);
600 Hook::callAll('contact_edit_post', $_POST);
601 Hook::callAll('contact_edit', $arr);
605 Hook::callAll('post_local_end', $arr);
609 Hook::callAll('uexport_options', $options);
613 Hook::callAll('register_post', $arr);
614 Hook::callAll('register_form', $arr);
618 Hook::callAll('post_local_start', $_REQUEST);
619 Hook::callAll('post_local', $datarray);
620 Hook::callAll('post_local_end', $datarray);
624 Hook::callAll('jot_tool', $jotplugins);
626 ### src/Network/FKOAuth1.php
628 Hook::callAll('logged_in', $a->user);
630 ### src/Render/FriendicaSmartyEngine.php
632 Hook::callAll("template_vars", $arr);
636 Hook::callAll('load_config');
637 Hook::callAll('head');
638 Hook::callAll('footer');
639 Hook::callAll('route_collection');
641 ### src/Model/Item.php
643 Hook::callAll('post_local', $item);
644 Hook::callAll('post_remote', $item);
645 Hook::callAll('post_local_end', $posted_item);
646 Hook::callAll('post_remote_end', $posted_item);
647 Hook::callAll('tagged', $arr);
648 Hook::callAll('post_local_end', $new_item);
649 Hook::callAll('put_item_in_cache', $hook_data);
650 Hook::callAll('prepare_body_init', $item);
651 Hook::callAll('prepare_body_content_filter', $hook_data);
652 Hook::callAll('prepare_body', $hook_data);
653 Hook::callAll('prepare_body_final', $hook_data);
655 ### src/Model/Contact.php
657 Hook::callAll('contact_photo_menu', $args);
658 Hook::callAll('follow', $arr);
660 ### src/Model/Profile.php
662 Hook::callAll('profile_sidebar_enter', $profile);
663 Hook::callAll('profile_sidebar', $arr);
664 Hook::callAll('profile_tabs', $arr);
665 Hook::callAll('zrl_init', $arr);
666 Hook::callAll('magic_auth_success', $arr);
668 ### src/Model/Event.php
670 Hook::callAll('event_updated', $event['id']);
671 Hook::callAll("event_created", $event['id']);
673 ### src/Model/User.php
675 Hook::callAll('register_account', $uid);
676 Hook::callAll('remove_user', $user);
678 ### src/Module/PermissionTooltip.php
680 Hook::callAll('lockview_content', $item);
682 ### src/Content/ContactBlock.php
684 Hook::callAll('contact_block_end', $arr);
686 ### src/Content/Text/BBCode.php
688 Hook::callAll('bbcode', $text);
689 Hook::callAll('bb2diaspora', $text);
691 ### src/Content/Text/HTML.php
693 Hook::callAll('html2bbcode', $message);
695 ### src/Content/Smilies.php
697 Hook::callAll('smilie', $params);
699 ### src/Content/Feature.php
701 Hook::callAll('isEnabled', $arr);
702 Hook::callAll('get', $arr);
704 ### src/Content/ContactSelector.php
706 Hook::callAll('network_to_name', $nets);
708 ### src/Content/OEmbed.php
710 Hook::callAll('oembed_fetch_url', $embedurl, $j);
712 ### src/Content/Nav.php
714 Hook::callAll('page_header', DI::page()['nav']);
715 Hook::callAll('nav_info', $nav);
717 ### src/Core/Authentication.php
719 Hook::callAll('logged_in', $a->user);
721 ### src/Core/StorageManager
723 Hook::callAll('storage_instance', $data);
725 ### src/Worker/Directory.php
727 Hook::callAll('globaldir_update', $arr);
729 ### src/Worker/Notifier.php
731 Hook::callAll('notifier_end', $target_item);
733 ### src/Module/Login.php
735 Hook::callAll('login_hook', $o);
737 ### src/Module/Logout.php
739 Hook::callAll("logging_out");
741 ### src/Object/Post.php
743 Hook::callAll('render_location', $locate);
744 Hook::callAll('display_item', $arr);
748 Hook::callAll('contact_select_options', $x);
749 Hook::callAll($a->module.'_pre_'.$selname, $arr);
750 Hook::callAll($a->module.'_post_'.$selname, $o);
751 Hook::callAll($a->module.'_pre_'.$selname, $arr);
752 Hook::callAll($a->module.'_post_'.$selname, $o);
753 Hook::callAll('jot_networks', $jotnets);
755 ### src/Core/Authentication.php
757 Hook::callAll('logged_in', $a->user);
758 Hook::callAll('authenticate', $addon_auth);
760 ### src/Core/Hook.php
762 self::callSingle(self::getApp(), 'hook_fork', $fork_hook, $hookdata);
764 ### src/Core/L10n/L10n.php
766 Hook::callAll('poke_verbs', $arr);
768 ### src/Core/Worker.php
770 Hook::callAll("proc_run", $arr);
772 ### src/Util/Emailer.php
774 Hook::callAll('emailer_send_prepare', $params);
775 Hook::callAll("emailer_send", $hookdata);
779 Hook::callAll('generate_map', $arr);
780 Hook::callAll('generate_named_map', $arr);
781 Hook::callAll('Map::getCoordinates', $arr);
783 ### src/Util/Network.php
785 Hook::callAll('avatar_lookup', $avatar);
787 ### src/Util/ParseUrl.php
789 Hook::callAll("getsiteinfo", $siteinfo);
791 ### src/Protocol/DFRN.php
793 Hook::callAll('atom_feed_end', $atom);
794 Hook::callAll('atom_feed_end', $atom);
796 ### src/Protocol/Email.php
798 Hook::callAll('email_getmessage', $message);
799 Hook::callAll('email_getmessage_end', $ret);
803 document.dispatchEvent(new Event('postprocess_liveupdate'));