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.
72 If your addon requires adding a stylesheet on all pages of Friendica, add the following hook:
75 function <addon>_install()
77 Addon::registerHook('head', __FILE__, '<addon>_head');
82 function <addon>_head(App $a)
84 $a->registerStylesheet(__DIR__ . '/relative/path/to/addon/stylesheet.css');
92 If your addon requires adding a script on all pages of Friendica, add the following hook:
96 function <addon>_install()
98 Addon::registerHook('footer', __FILE__, '<addon>_footer');
102 function <addon>_footer(App $a)
104 $a->registerFooterScript(__DIR__ . '/relative/path/to/addon/script.js');
110 The main Friendica script provides hooks via events dispatched on the `document` property.
111 In your Javascript file included as described above, add your event listener like this:
114 document.addEventListener(name, callback);
117 - *name* is the name of the hook and corresponds to a known Friendica JavaScript hook.
118 - *callback* is a JavaScript function to execute.
120 #### Current JavaScript hooks
122 ##### postprocess_liveupdate
123 Called at the end of the live update process (XmlHttpRequest) and on a post preview.
124 No additional data is provided.
128 Addons may also act as "modules" and intercept all page requests for a given URL path.
129 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.
131 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.
132 These are parsed into an array $a->argv, with a corresponding $a->argc indicating the number of URL components.
133 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).
138 $a->argv = array(0 => 'addon', 1 => 'arg1', 2 => 'arg2');
141 Your module functions will often contain the function addon_name_content(App $a), which defines and returns the page body content.
142 They may also contain addon_name_post(App $a) which is called before the _content function and typically handles the results of POST forms.
143 You may also have addon_name_init(App $a) which is called very early on and often does module initialisation.
147 If your addon needs some template, you can use the Friendica template system.
148 Friendica uses [smarty3](http://www.smarty.net/) as a template engine.
150 Put your tpl files in the *templates/* subfolder of your addon.
152 In your code, like in the function addon_name_content(), load the template file and execute it passing needed values:
155 # load template file. first argument is the template name,
156 # second is the addon path relative to friendica top folder
157 $tpl = get_markup_template('mytemplate.tpl', 'addon/addon_name/');
159 # apply template. first argument is the loaded template,
160 # second an array of 'name' => 'values' to pass to template
161 $output = replace_macros($tpl, array(
162 'title' => 'My beautiful addon',
166 See also the wiki page [Quick Template Guide](https://github.com/friendica/friendica/wiki/Quick-Template-Guide).
171 Called when a user attempts to login.
172 `$b` is an array containing:
174 - **username**: the supplied username
175 - **password**: the supplied password
176 - **authenticated**: set this to non-zero to authenticate the user.
177 - **user_record**: successful authentication must also return a valid user record from the database
180 Called after a user has successfully logged in.
181 `$b` contains the `$a->user` array.
184 Called when formatting a post for display.
187 - **item**: The item (array) details pulled from the database
188 - **output**: the (string) HTML representation of this item prior to adding it to the page
191 Called when a status post or comment is entered on the local system.
192 `$b` is the item array of the information to be stored in the database.
193 Please note: body contents are bbcode - not HTML.
196 Called when a local status post or comment has been stored on the local system.
197 `$b` is the item array of the information which has just been stored in the database.
198 Please note: body contents are bbcode - not HTML
201 Called when receiving a post from another source. This may also be used to post local activity or system generated messages.
202 `$b` is the item array of information to be stored in the database and the item body is bbcode.
205 Called when generating the HTML for the user Settings page.
206 `$b` is the HTML string of the settings page before the final `</form>` tag.
209 Called when the Settings pages are submitted.
210 `$b` is the $_POST array.
213 Called when generating the HTML for the addon settings page.
214 `$b` is the (string) HTML of the addon settings page before the final `</form>` tag.
216 ### addon_settings_post
217 Called when the Addon Settings pages are submitted.
218 `$b` is the $_POST array.
221 Called when posting a profile page.
222 `$b` is the $_POST array.
225 Called prior to output of profile edit page.
226 `$b` is an array containing:
228 - **profile**: profile (array) record from the database
229 - **entry**: the (string) HTML of the generated entry
232 Called when the HTML is generated for the Advanced profile, corresponding to the Profile tab within a person's profile page.
233 `$b` is the HTML string representation of the generated profile.
234 The profile array details are in `$a->profile`.
237 Called from the Directory page when formatting an item for display.
240 - **contact**: contact record array for the person from the database
241 - **entry**: the HTML string of the generated entry
243 ### profile_sidebar_enter
244 Called prior to generating the sidebar "short" profile for a page.
245 `$b` is the person's profile array
248 Called when generating the sidebar "short" profile for a page.
251 - **profile**: profile record array for the person from the database
252 - **entry**: the HTML string of the generated entry
254 ### contact_block_end
255 Called when formatting the block of contacts/friends on a profile sidebar has completed.
258 - **contacts**: array of contacts
259 - **output**: the generated HTML string of the contact block
262 Called after conversion of bbcode to HTML.
263 `$b` is an HTML string converted text.
266 Called after tag conversion of HTML to bbcode (e.g. remote message posting)
267 `$b` is a string converted text
270 Called when building the `<head>` sections.
271 Stylesheets should be registered using this hook.
272 `$b` is an HTML string of the `<head>` tag.
275 Called after building the page navigation section.
276 `$b` is a string HTML of nav region.
279 Called prior to output of personal XRD file.
282 - **user**: the user record array for the person
283 - **xml**: the complete XML string to be output
286 Called prior to output home page content, shown to unlogged users.
287 `$b` is the HTML sring of section region.
290 Called when editing contact details on an individual from the Contacts page.
293 - **contact**: contact record (array) of target contact
294 - **output**: the (string) generated HTML of the contact edit page
296 ### contact_edit_post
297 Called when posting the contact edit page.
298 `$b` is the `$_POST` array
301 Called just after DB has been opened and before session start.
305 Called after HTML content functions have completed.
306 `$b` is (string) HTML of content div.
309 Called after HTML content functions have completed.
310 Deferred Javascript files should be registered using this hook.
311 `$b` is (string) HTML of footer div/element.
314 Called when looking up the avatar. `$b` is an array:
316 - **size**: the size of the avatar that will be looked up
317 - **email**: email to look up the avatar for
318 - **url**: the (string) generated URL of the avatar
320 ### emailer_send_prepare
321 Called from `Emailer::send()` before building the mime message.
322 `$b` is an array of params to `Emailer::send()`.
324 - **fromName**: name of the sender
325 - **fromEmail**: email fo the sender
326 - **replyTo**: replyTo address to direct responses
327 - **toEmail**: destination email address
328 - **messageSubject**: subject of the message
329 - **htmlVersion**: html version of the message
330 - **textVersion**: text only version of the message
331 - **additionalMailHeader**: additions to the smtp mail header
334 Called before calling PHP's `mail()`.
335 `$b` is an array of params to `mail()`.
343 Called during `App` initialization to allow addons to load their own configuration file(s) with `App::loadConfigFile()`.
346 Called after the navigational menu is build in `include/nav.php`.
347 `$b` is an array containing `$nav` from `include/nav.php`.
350 Called before vars are passed to the template engine to render the page.
351 The registered function can add,change or remove variables passed to template.
352 `$b` is an array with:
354 - **template**: filename of template
355 - **vars**: array of vars passed to the template
358 Called after the other queries have passed.
359 The registered function can add, change or remove the `acl_lookup()` variables.
361 - **results**: array of the acl_lookup() vars
363 ### prepare_body_init
364 Called at the start of prepare_body
367 - **item** (input/output): item array
369 ### prepare_body_content_filter
370 Called before the HTML conversion in prepare_body. If the item matches a content filter rule set by an addon, it should
371 just add the reason to the filter_reasons element of the hook data.
374 - **item**: item array (input)
375 - **filter_reasons** (input/output): reasons array
378 Called after the HTML conversion in `prepare_body()`.
381 - **item** (input): item array
382 - **html** (input/output): converted item body
383 - **is_preview** (input): post preview flag
384 - **filter_reasons** (input): reasons array
386 ### prepare_body_final
387 Called at the end of `prepare_body()`.
390 - **item**: item array (input)
391 - **html**: converted item body (input/output)
393 ### put_item_in_cache
394 Called after `prepare_text()` in `put_item_in_cache()`.
397 - **item** (input): item array
398 - **rendered-html** (input/output): final item body HTML
399 - **rendered-hash** (input/output): original item body hash
401 ### magic_auth_success
402 Called when a magic-auth was successful.
405 visitor => array with the contact record of the visitor
406 url => the query string
408 ## Complete list of hook callbacks
410 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.
414 Addon::callHooks('init_1');
415 Addon::callHooks('app_menu', $arr);
416 Addon::callHooks('page_content_top', $a->page['content']);
417 Addon::callHooks($a->module.'_mod_init', $placeholder);
418 Addon::callHooks($a->module.'_mod_init', $placeholder);
419 Addon::callHooks($a->module.'_mod_post', $_POST);
420 Addon::callHooks($a->module.'_mod_afterpost', $placeholder);
421 Addon::callHooks($a->module.'_mod_content', $arr);
422 Addon::callHooks($a->module.'_mod_aftercontent', $arr);
423 Addon::callHooks('page_end', $a->page['content']);
427 Addon::callHooks('logged_in', $a->user);
428 Addon::callHooks('authenticate', $addon_auth);
429 Addon::callHooks('logged_in', $a->user);
431 ### include/enotify.php
433 Addon::callHooks('enotify', $h);
434 Addon::callHooks('enotify_store', $datarray);
435 Addon::callHooks('enotify_mail', $datarray);
436 Addon::callHooks('check_item_notification', $notification_data);
438 ### include/conversation.php
440 Addon::callHooks('conversation_start', $cb);
441 Addon::callHooks('render_location', $locate);
442 Addon::callHooks('display_item', $arr);
443 Addon::callHooks('display_item', $arr);
444 Addon::callHooks('item_photo_menu', $args);
445 Addon::callHooks('jot_tool', $jotplugins);
447 ### include/security.php
449 Addon::callHooks('logged_in', $a->user);
453 Addon::callHooks('contact_block_end', $arr);
454 Addon::callHooks('poke_verbs', $arr);
455 Addon::callHooks('put_item_in_cache', $hook_data);
456 Addon::callHooks('prepare_body_init', $item);
457 Addon::callHooks('prepare_body_content_filter', $hook_data);
458 Addon::callHooks('prepare_body', $hook_data);
459 Addon::callHooks('prepare_body_final', $hook_data);
461 ### include/items.php
463 Addon::callHooks('page_info_data', $data);
465 ### mod/directory.php
467 Addon::callHooks('directory_item', $arr);
471 Addon::callHooks('personal_xrd', $arr);
475 Addon::callHooks('network_ping', $arr);
477 ### mod/parse_url.php
479 Addon::callHooks("parse_link", $arr);
483 Addon::callHooks('home_init', $ret);
487 Addon::callHooks('acl_lookup_end', $results);
491 Addon::callHooks('network_content_init', $arr);
492 Addon::callHooks('network_tabs', $arr);
494 ### mod/friendica.php
496 Addon::callHooks('about_hook', $o);
498 ### mod/subthread.php
500 Addon::callHooks('post_local_end', $arr);
504 Addon::callHooks('profile_post', $_POST);
505 Addon::callHooks('profile_edit', $arr);
509 Addon::callHooks('addon_settings_post', $_POST);
510 Addon::callHooks('connector_settings_post', $_POST);
511 Addon::callHooks('display_settings_post', $_POST);
512 Addon::callHooks('settings_post', $_POST);
513 Addon::callHooks('addon_settings', $settings_addons);
514 Addon::callHooks('connector_settings', $settings_connectors);
515 Addon::callHooks('display_settings', $o);
516 Addon::callHooks('settings_form', $o);
520 Addon::callHooks('photo_post_init', $_POST);
521 Addon::callHooks('photo_post_file', $ret);
522 Addon::callHooks('photo_post_end', $foo);
523 Addon::callHooks('photo_post_end', $foo);
524 Addon::callHooks('photo_post_end', $foo);
525 Addon::callHooks('photo_post_end', $foo);
526 Addon::callHooks('photo_post_end', intval($item_id));
527 Addon::callHooks('photo_upload_form', $ret);
531 Addon::callHooks('profile_advanced', $o);
535 Addon::callHooks('home_init', $ret);
536 Addon::callHooks("home_content", $content);
540 Addon::callHooks('post_local_end', $arr);
544 Addon::callHooks('contact_edit_post', $_POST);
545 Addon::callHooks('contact_edit', $arr);
549 Addon::callHooks('post_local_end', $arr);
553 Addon::callHooks('lockview_content', $item);
557 Addon::callHooks('uexport_options', $options);
561 Addon::callHooks('register_post', $arr);
562 Addon::callHooks('register_form', $arr);
566 Addon::callHooks('post_local_start', $_REQUEST);
567 Addon::callHooks('post_local', $datarray);
568 Addon::callHooks('post_local_end', $datarray);
572 Addon::callHooks('jot_tool', $jotplugins);
574 ### src/Network/FKOAuth1.php
576 Addon::callHooks('logged_in', $a->user);
578 ### src/Render/FriendicaSmartyEngine.php
580 Addon::callHooks("template_vars", $arr);
584 Addon::callHooks('load_config');
585 Addon::callHooks('head');
586 Addon::callHooks('footer');
588 ### src/Model/Item.php
590 Addon::callHooks('post_local', $item);
591 Addon::callHooks('post_remote', $item);
592 Addon::callHooks('post_local_end', $posted_item);
593 Addon::callHooks('post_remote_end', $posted_item);
594 Addon::callHooks('tagged', $arr);
595 Addon::callHooks('post_local_end', $new_item);
597 ### src/Model/Contact.php
599 Addon::callHooks('contact_photo_menu', $args);
600 Addon::callHooks('follow', $arr);
602 ### src/Model/Profile.php
604 Addon::callHooks('profile_sidebar_enter', $profile);
605 Addon::callHooks('profile_sidebar', $arr);
606 Addon::callHooks('profile_tabs', $arr);
607 Addon::callHooks('zrl_init', $arr);
608 Addon::callHooks('magic_auth_success', $arr);
610 ### src/Model/Event.php
612 Addon::callHooks('event_updated', $event['id']);
613 Addon::callHooks("event_created", $event['id']);
615 ### src/Model/User.php
617 Addon::callHooks('register_account', $uid);
618 Addon::callHooks('remove_user', $user);
620 ### src/Content/Text/BBCode.php
622 Addon::callHooks('bbcode', $text);
623 Addon::callHooks('bb2diaspora', $text);
625 ### src/Content/Text/HTML.php
627 Addon::callHooks('html2bbcode', $message);
629 ### src/Content/Smilies.php
631 Addon::callHooks('smilie', $params);
633 ### src/Content/Feature.php
635 Addon::callHooks('isEnabled', $arr);
636 Addon::callHooks('get', $arr);
638 ### src/Content/ContactSelector.php
640 Addon::callHooks('network_to_name', $nets);
641 Addon::callHooks('gender_selector', $select);
642 Addon::callHooks('sexpref_selector', $select);
643 Addon::callHooks('marital_selector', $select);
645 ### src/Content/OEmbed.php
647 Addon::callHooks('oembed_fetch_url', $embedurl, $j);
649 ### src/Content/Nav.php
651 Addon::callHooks('page_header', $a->page['nav']);
652 Addon::callHooks('nav_info', $nav);
654 ### src/Worker/Directory.php
656 Addon::callHooks('globaldir_update', $arr);
658 ### src/Worker/Notifier.php
660 Addon::callHooks('notifier_end', $target_item);
662 ### src/Worker/Queue.php
664 Addon::callHooks('queue_predeliver', $r);
665 Addon::callHooks('queue_deliver', $params);
667 ### src/Module/Login.php
669 Addon::callHooks('authenticate', $addon_auth);
670 Addon::callHooks('login_hook', $o);
672 ### src/Module/Logout.php
674 Addon::callHooks("logging_out");
676 ### src/Object/Post.php
678 Addon::callHooks('render_location', $locate);
679 Addon::callHooks('display_item', $arr);
683 Addon::callHooks('contact_select_options', $x);
684 Addon::callHooks($a->module.'_pre_'.$selname, $arr);
685 Addon::callHooks($a->module.'_post_'.$selname, $o);
686 Addon::callHooks($a->module.'_pre_'.$selname, $arr);
687 Addon::callHooks($a->module.'_post_'.$selname, $o);
688 Addon::callHooks('jot_networks', $jotnets);
690 ### src/Core/Worker.php
692 Addon::callHooks("proc_run", $arr);
694 ### src/Util/Emailer.php
696 Addon::callHooks('emailer_send_prepare', $params);
697 Addon::callHooks("emailer_send", $hookdata);
701 Addon::callHooks('generate_map', $arr);
702 Addon::callHooks('generate_named_map', $arr);
703 Addon::callHooks('Map::getCoordinates', $arr);
705 ### src/Util/Network.php
707 Addon::callHooks('avatar_lookup', $avatar);
709 ### src/Util/ParseUrl.php
711 Addon::callHooks("getsiteinfo", $siteinfo);
713 ### src/Protocol/DFRN.php
715 Addon::callHooks('atom_feed_end', $atom);
716 Addon::callHooks('atom_feed_end', $atom);
720 document.dispatchEvent(new Event('postprocess_liveupdate'));