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 \Friendica\Core\Hook::register($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.
72 If your addon requires adding a stylesheet on all pages of Friendica, add the following hook:
75 function <addon>_install()
77 \Friendica\Core\Hook::register('head', __FILE__, '<addon>_head');
82 function <addon>_head(App $a)
84 $a->registerStylesheet(__DIR__ . '/relative/path/to/addon/stylesheet.css');
88 `__DIR__` is the folder path of your addon.
94 If your addon requires adding a script on all pages of Friendica, add the following hook:
98 function <addon>_install()
100 \Friendica\Core\Hook::register('footer', __FILE__, '<addon>_footer');
104 function <addon>_footer(App $a)
106 $a->registerFooterScript(__DIR__ . '/relative/path/to/addon/script.js');
110 `__DIR__` is the folder path of your addon.
114 The main Friendica script provides hooks via events dispatched on the `document` property.
115 In your Javascript file included as described above, add your event listener like this:
118 document.addEventListener(name, callback);
121 - *name* is the name of the hook and corresponds to a known Friendica JavaScript hook.
122 - *callback* is a JavaScript anonymous function to execute.
124 More info about Javascript event listeners: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
126 #### Current JavaScript hooks
128 ##### postprocess_liveupdate
129 Called at the end of the live update process (XmlHttpRequest) and on a post preview.
130 No additional data is provided.
134 Addons may also act as "modules" and intercept all page requests for a given URL path.
135 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.
137 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.
138 These are parsed into an array $a->argv, with a corresponding $a->argc indicating the number of URL components.
139 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).
144 $a->argv = array(0 => 'addon', 1 => 'arg1', 2 => 'arg2');
147 Your module functions will often contain the function addon_name_content(App $a), which defines and returns the page body content.
148 They may also contain addon_name_post(App $a) which is called before the _content function and typically handles the results of POST forms.
149 You may also have addon_name_init(App $a) which is called very early on and often does module initialisation.
153 If your addon needs some template, you can use the Friendica template system.
154 Friendica uses [smarty3](http://www.smarty.net/) as a template engine.
156 Put your tpl files in the *templates/* subfolder of your addon.
158 In your code, like in the function addon_name_content(), load the template file and execute it passing needed values:
161 # load template file. first argument is the template name,
162 # second is the addon path relative to friendica top folder
163 $tpl = Renderer::getMarkupTemplate('mytemplate.tpl', 'addon/addon_name/');
165 # apply template. first argument is the loaded template,
166 # second an array of 'name' => 'values' to pass to template
167 $output = Renderer::replaceMacros($tpl, array(
168 'title' => 'My beautiful addon',
172 See also the wiki page [Quick Template Guide](https://github.com/friendica/friendica/wiki/Quick-Template-Guide).
177 Called when a user attempts to login.
178 `$b` is an array containing:
180 - **username**: the supplied username
181 - **password**: the supplied password
182 - **authenticated**: set this to non-zero to authenticate the user.
183 - **user_record**: successful authentication must also return a valid user record from the database
186 Called after a user has successfully logged in.
187 `$b` contains the `$a->user` array.
190 Called when formatting a post for display.
193 - **item**: The item (array) details pulled from the database
194 - **output**: the (string) HTML representation of this item prior to adding it to the page
197 Called when a status post or comment is entered on the local system.
198 `$b` is the item array of the information to be stored in the database.
199 Please note: body contents are bbcode - not HTML.
202 Called when a local status post or comment has been stored on the local system.
203 `$b` is the item array of the information which has just been stored in the database.
204 Please note: body contents are bbcode - not HTML
207 Called when receiving a post from another source. This may also be used to post local activity or system generated messages.
208 `$b` is the item array of information to be stored in the database and the item body is bbcode.
211 Called when generating the HTML for the user Settings page.
212 `$b` is the HTML string of the settings page before the final `</form>` tag.
215 Called when the Settings pages are submitted.
216 `$b` is the $_POST array.
219 Called when generating the HTML for the addon settings page.
220 `$b` is the (string) HTML of the addon settings page before the final `</form>` tag.
222 ### addon_settings_post
223 Called when the Addon Settings pages are submitted.
224 `$b` is the $_POST array.
227 Called when posting a profile page.
228 `$b` is the $_POST array.
231 Called prior to output of profile edit page.
232 `$b` is an array containing:
234 - **profile**: profile (array) record from the database
235 - **entry**: the (string) HTML of the generated entry
238 Called when the HTML is generated for the Advanced profile, corresponding to the Profile tab within a person's profile page.
239 `$b` is the HTML string representation of the generated profile.
240 The profile array details are in `$a->profile`.
243 Called from the Directory page when formatting an item for display.
246 - **contact**: contact record array for the person from the database
247 - **entry**: the HTML string of the generated entry
249 ### profile_sidebar_enter
250 Called prior to generating the sidebar "short" profile for a page.
251 `$b` is the person's profile array
254 Called when generating the sidebar "short" profile for a page.
257 - **profile**: profile record array for the person from the database
258 - **entry**: the HTML string of the generated entry
260 ### contact_block_end
261 Called when formatting the block of contacts/friends on a profile sidebar has completed.
264 - **contacts**: array of contacts
265 - **output**: the generated HTML string of the contact block
268 Called after conversion of bbcode to HTML.
269 `$b` is an HTML string converted text.
272 Called after tag conversion of HTML to bbcode (e.g. remote message posting)
273 `$b` is a string converted text
276 Called when building the `<head>` sections.
277 Stylesheets should be registered using this hook.
278 `$b` is an HTML string of the `<head>` tag.
281 Called after building the page navigation section.
282 `$b` is a string HTML of nav region.
285 Called prior to output of personal XRD file.
288 - **user**: the user record array for the person
289 - **xml**: the complete XML string to be output
292 Called prior to output home page content, shown to unlogged users.
293 `$b` is the HTML sring of section region.
296 Called when editing contact details on an individual from the Contacts page.
299 - **contact**: contact record (array) of target contact
300 - **output**: the (string) generated HTML of the contact edit page
302 ### contact_edit_post
303 Called when posting the contact edit page.
304 `$b` is the `$_POST` array
307 Called just after DB has been opened and before session start.
311 Called after HTML content functions have completed.
312 `$b` is (string) HTML of content div.
315 Called after HTML content functions have completed.
316 Deferred Javascript files should be registered using this hook.
317 `$b` is (string) HTML of footer div/element.
320 Called when looking up the avatar. `$b` is an array:
322 - **size**: the size of the avatar that will be looked up
323 - **email**: email to look up the avatar for
324 - **url**: the (string) generated URL of the avatar
326 ### emailer_send_prepare
327 Called from `Emailer::send()` before building the mime message.
328 `$b` is an array of params to `Emailer::send()`.
330 - **fromName**: name of the sender
331 - **fromEmail**: email fo the sender
332 - **replyTo**: replyTo address to direct responses
333 - **toEmail**: destination email address
334 - **messageSubject**: subject of the message
335 - **htmlVersion**: html version of the message
336 - **textVersion**: text only version of the message
337 - **additionalMailHeader**: additions to the smtp mail header
340 Called before calling PHP's `mail()`.
341 `$b` is an array of params to `mail()`.
349 Called during `App` initialization to allow addons to load their own configuration file(s) with `App::loadConfigFile()`.
352 Called after the navigational menu is build in `include/nav.php`.
353 `$b` is an array containing `$nav` from `include/nav.php`.
356 Called before vars are passed to the template engine to render the page.
357 The registered function can add,change or remove variables passed to template.
358 `$b` is an array with:
360 - **template**: filename of template
361 - **vars**: array of vars passed to the template
364 Called after the other queries have passed.
365 The registered function can add, change or remove the `acl_lookup()` variables.
367 - **results**: array of the acl_lookup() vars
369 ### prepare_body_init
370 Called at the start of prepare_body
373 - **item** (input/output): item array
375 ### prepare_body_content_filter
376 Called before the HTML conversion in prepare_body. If the item matches a content filter rule set by an addon, it should
377 just add the reason to the filter_reasons element of the hook data.
380 - **item**: item array (input)
381 - **filter_reasons** (input/output): reasons array
384 Called after the HTML conversion in `prepare_body()`.
387 - **item** (input): item array
388 - **html** (input/output): converted item body
389 - **is_preview** (input): post preview flag
390 - **filter_reasons** (input): reasons array
392 ### prepare_body_final
393 Called at the end of `prepare_body()`.
396 - **item**: item array (input)
397 - **html**: converted item body (input/output)
399 ### put_item_in_cache
400 Called after `prepare_text()` in `put_item_in_cache()`.
403 - **item** (input): item array
404 - **rendered-html** (input/output): final item body HTML
405 - **rendered-hash** (input/output): original item body hash
407 ### magic_auth_success
408 Called when a magic-auth was successful.
411 visitor => array with the contact record of the visitor
412 url => the query string
415 Called when displaying the post permission screen.
416 Hook data is a list of form fields that need to be displayed along the ACL.
417 Form field array structure is:
419 - **type**: `checkbox` or `select`.
420 - **field**: Standard field data structure to be used by `field_checkbox.tpl` and `field_select.tpl`.
422 For `checkbox`, **field** is:
423 - [0] (String): Form field name; Mandatory.
424 - [1]: (String): Form field label; Optional, default is none.
425 - [2]: (Boolean): Whether the checkbox should be checked by default; Optional, default is false.
426 - [3]: (String): Additional help text; Optional, default is none.
427 - [4]: (String): Additional HTML attributes; Optional, default is none.
429 For `select`, **field** is:
430 - [0] (String): Form field name; Mandatory.
431 - [1] (String): Form field label; Optional, default is none.
432 - [2] (Boolean): Default value to be selected by default; Optional, default is none.
433 - [3] (String): Additional help text; Optional, default is none.
434 - [4] (Array): Associative array of options. Item key is option value, item value is option label; Mandatory.
437 Called just before dispatching the router.
438 Hook data is a `\FastRoute\RouterCollector` object that should be used to add addon routes pointing to classes.
440 **Notice**: The class whose name is provided in the route handler must be reachable via auto-loader.
442 ## Complete list of hook callbacks
444 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.
448 Hook::callAll('init_1');
449 Hook::callAll('app_menu', $arr);
450 Hook::callAll('page_content_top', $a->page['content']);
451 Hook::callAll($a->module.'_mod_init', $placeholder);
452 Hook::callAll($a->module.'_mod_init', $placeholder);
453 Hook::callAll($a->module.'_mod_post', $_POST);
454 Hook::callAll($a->module.'_mod_afterpost', $placeholder);
455 Hook::callAll($a->module.'_mod_content', $arr);
456 Hook::callAll($a->module.'_mod_aftercontent', $arr);
457 Hook::callAll('page_end', $a->page['content']);
461 Hook::callAll('logged_in', $a->user);
462 Hook::callAll('authenticate', $addon_auth);
463 Hook::callAll('logged_in', $a->user);
465 ### include/enotify.php
467 Hook::callAll('enotify', $h);
468 Hook::callAll('enotify_store', $datarray);
469 Hook::callAll('enotify_mail', $datarray);
470 Hook::callAll('check_item_notification', $notification_data);
472 ### include/conversation.php
474 Hook::callAll('conversation_start', $cb);
475 Hook::callAll('render_location', $locate);
476 Hook::callAll('display_item', $arr);
477 Hook::callAll('display_item', $arr);
478 Hook::callAll('item_photo_menu', $args);
479 Hook::callAll('jot_tool', $jotplugins);
483 Hook::callAll('contact_block_end', $arr);
484 Hook::callAll('poke_verbs', $arr);
485 Hook::callAll('put_item_in_cache', $hook_data);
486 Hook::callAll('prepare_body_init', $item);
487 Hook::callAll('prepare_body_content_filter', $hook_data);
488 Hook::callAll('prepare_body', $hook_data);
489 Hook::callAll('prepare_body_final', $hook_data);
491 ### include/items.php
493 Hook::callAll('page_info_data', $data);
495 ### mod/directory.php
497 Hook::callAll('directory_item', $arr);
501 Hook::callAll('personal_xrd', $arr);
505 Hook::callAll('network_ping', $arr);
507 ### mod/parse_url.php
509 Hook::callAll("parse_link", $arr);
513 Hook::callAll('home_init', $ret);
517 Hook::callAll('acl_lookup_end', $results);
521 Hook::callAll('network_content_init', $arr);
522 Hook::callAll('network_tabs', $arr);
524 ### mod/friendica.php
526 Hook::callAll('about_hook', $o);
528 ### mod/subthread.php
530 Hook::callAll('post_local_end', $arr);
534 Hook::callAll('profile_post', $_POST);
535 Hook::callAll('profile_edit', $arr);
539 Hook::callAll('addon_settings_post', $_POST);
540 Hook::callAll('connector_settings_post', $_POST);
541 Hook::callAll('display_settings_post', $_POST);
542 Hook::callAll('settings_post', $_POST);
543 Hook::callAll('addon_settings', $settings_addons);
544 Hook::callAll('connector_settings', $settings_connectors);
545 Hook::callAll('display_settings', $o);
546 Hook::callAll('settings_form', $o);
550 Hook::callAll('photo_post_init', $_POST);
551 Hook::callAll('photo_post_file', $ret);
552 Hook::callAll('photo_post_end', $foo);
553 Hook::callAll('photo_post_end', $foo);
554 Hook::callAll('photo_post_end', $foo);
555 Hook::callAll('photo_post_end', $foo);
556 Hook::callAll('photo_post_end', intval($item_id));
557 Hook::callAll('photo_upload_form', $ret);
561 Hook::callAll('profile_advanced', $o);
565 Hook::callAll('home_init', $ret);
566 Hook::callAll("home_content", $content);
570 Hook::callAll('post_local_end', $arr);
574 Hook::callAll('contact_edit_post', $_POST);
575 Hook::callAll('contact_edit', $arr);
579 Hook::callAll('post_local_end', $arr);
583 Hook::callAll('lockview_content', $item);
587 Hook::callAll('uexport_options', $options);
591 Hook::callAll('register_post', $arr);
592 Hook::callAll('register_form', $arr);
596 Hook::callAll('post_local_start', $_REQUEST);
597 Hook::callAll('post_local', $datarray);
598 Hook::callAll('post_local_end', $datarray);
602 Hook::callAll('jot_tool', $jotplugins);
604 ### src/Network/FKOAuth1.php
606 Hook::callAll('logged_in', $a->user);
608 ### src/Render/FriendicaSmartyEngine.php
610 Hook::callAll("template_vars", $arr);
614 Hook::callAll('load_config');
615 Hook::callAll('head');
616 Hook::callAll('footer');
617 Hook::callAll('route_collection');
619 ### src/Model/Item.php
621 Hook::callAll('post_local', $item);
622 Hook::callAll('post_remote', $item);
623 Hook::callAll('post_local_end', $posted_item);
624 Hook::callAll('post_remote_end', $posted_item);
625 Hook::callAll('tagged', $arr);
626 Hook::callAll('post_local_end', $new_item);
628 ### src/Model/Contact.php
630 Hook::callAll('contact_photo_menu', $args);
631 Hook::callAll('follow', $arr);
633 ### src/Model/Profile.php
635 Hook::callAll('profile_sidebar_enter', $profile);
636 Hook::callAll('profile_sidebar', $arr);
637 Hook::callAll('profile_tabs', $arr);
638 Hook::callAll('zrl_init', $arr);
639 Hook::callAll('magic_auth_success', $arr);
641 ### src/Model/Event.php
643 Hook::callAll('event_updated', $event['id']);
644 Hook::callAll("event_created", $event['id']);
646 ### src/Model/User.php
648 Hook::callAll('register_account', $uid);
649 Hook::callAll('remove_user', $user);
651 ### src/Content/Text/BBCode.php
653 Hook::callAll('bbcode', $text);
654 Hook::callAll('bb2diaspora', $text);
656 ### src/Content/Text/HTML.php
658 Hook::callAll('html2bbcode', $message);
660 ### src/Content/Smilies.php
662 Hook::callAll('smilie', $params);
664 ### src/Content/Feature.php
666 Hook::callAll('isEnabled', $arr);
667 Hook::callAll('get', $arr);
669 ### src/Content/ContactSelector.php
671 Hook::callAll('network_to_name', $nets);
672 Hook::callAll('gender_selector', $select);
673 Hook::callAll('sexpref_selector', $select);
674 Hook::callAll('marital_selector', $select);
676 ### src/Content/OEmbed.php
678 Hook::callAll('oembed_fetch_url', $embedurl, $j);
680 ### src/Content/Nav.php
682 Hook::callAll('page_header', $a->page['nav']);
683 Hook::callAll('nav_info', $nav);
685 ### src/Worker/Directory.php
687 Hook::callAll('globaldir_update', $arr);
689 ### src/Worker/Notifier.php
691 Hook::callAll('notifier_end', $target_item);
693 ### src/Module/Login.php
695 Hook::callAll('authenticate', $addon_auth);
696 Hook::callAll('login_hook', $o);
698 ### src/Module/Logout.php
700 Hook::callAll("logging_out");
702 ### src/Object/Post.php
704 Hook::callAll('render_location', $locate);
705 Hook::callAll('display_item', $arr);
709 Hook::callAll('contact_select_options', $x);
710 Hook::callAll($a->module.'_pre_'.$selname, $arr);
711 Hook::callAll($a->module.'_post_'.$selname, $o);
712 Hook::callAll($a->module.'_pre_'.$selname, $arr);
713 Hook::callAll($a->module.'_post_'.$selname, $o);
714 Hook::callAll('jot_networks', $jotnets);
716 ### src/Core/Authentication.php
718 Hook::callAll('logged_in', $a->user);
720 ### src/Core/Hook.php
722 self::callSingle(self::getApp(), 'hook_fork', $fork_hook, $hookdata);
724 ### src/Core/Worker.php
726 Hook::callAll("proc_run", $arr);
728 ### src/Util/Emailer.php
730 Hook::callAll('emailer_send_prepare', $params);
731 Hook::callAll("emailer_send", $hookdata);
735 Hook::callAll('generate_map', $arr);
736 Hook::callAll('generate_named_map', $arr);
737 Hook::callAll('Map::getCoordinates', $arr);
739 ### src/Util/Network.php
741 Hook::callAll('avatar_lookup', $avatar);
743 ### src/Util/ParseUrl.php
745 Hook::callAll("getsiteinfo", $siteinfo);
747 ### src/Protocol/DFRN.php
749 Hook::callAll('atom_feed_end', $atom);
750 Hook::callAll('atom_feed_end', $atom);
754 document.dispatchEvent(new Event('postprocess_liveupdate'));