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 Register your addon hooks during installation.
30 Addon::registerHook($hookname, $file, $function);
32 $hookname is a string and corresponds to a known Friendica hook.
34 $file is a pathname relative to the top-level Friendica directory.
35 This *should* be 'addon/addon_name/addon_name.php' in most cases.
37 $function is a string and is the name of the function which will be executed when the hook is called.
39 Please also add a README or README.md file to the addon directory.
40 It will be displayed in the admin panel and should include some further information in addition to the header information.
44 Your hook callback functions will be called with at least one and possibly two arguments
46 function myhook_function(App $a, &$b) {
51 If you wish to make changes to the calling data, you must declare them as reference variables (with '&') during function declaration.
54 $a is the Friendica 'App' class.
55 It contains a wealth of information about the current state of Friendica:
57 * which module has been called,
58 * configuration information,
59 * the page contents at the point the hook was invoked,
60 * profile and user information, etc.
62 It is recommeded you call this '$a' to match its usage elsewhere.
65 $b can be called anything you like.
66 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.
67 Remember to declare it with '&' if you wish to alter it.
72 Addons may also act as "modules" and intercept all page requests for a given URL path.
73 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.
75 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.
76 These are parsed into an array $a->argv, with a corresponding $a->argc indicating the number of URL components.
77 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).
81 $a->argv = array(0 => 'addon', 1 => 'arg1', 2 => 'arg2');
83 Your module functions will often contain the function addon_name_content(App $a), which defines and returns the page body content.
84 They may also contain addon_name_post(App $a) which is called before the _content function and typically handles the results of POST forms.
85 You may also have addon_name_init(App $a) which is called very early on and often does module initialisation.
90 If your addon needs some template, you can use the Friendica template system.
91 Friendica uses [smarty3](http://www.smarty.net/) as a template engine.
93 Put your tpl files in the *templates/* subfolder of your addon.
95 In your code, like in the function addon_name_content(), load the template file and execute it passing needed values:
97 # load template file. first argument is the template name,
98 # second is the addon path relative to friendica top folder
99 $tpl = get_markup_template('mytemplate.tpl', 'addon/addon_name/');
101 # apply template. first argument is the loaded template,
102 # second an array of 'name'=>'values' to pass to template
103 $output = replace_macros($tpl,array(
104 'title' => 'My beautiful addon',
107 See also the wiki page [Quick Template Guide](https://github.com/friendica/friendica/wiki/Quick-Template-Guide).
113 'authenticate' is called when a user attempts to login.
114 $b is an array containing:
116 'username' => the supplied username
117 'password' => the supplied password
118 'authenticated' => set this to non-zero to authenticate the user.
119 'user_record' => successful authentication must also return a valid user record from the database
122 'logged_in' is called after a user has successfully logged in.
123 $b contains the $a->user array.
126 'display_item' is called when formatting a post for display.
129 'item' => The item (array) details pulled from the database
130 'output' => the (string) HTML representation of this item prior to adding it to the page
133 * called when a status post or comment is entered on the local system
134 * $b is the item array of the information to be stored in the database
135 * Please note: body contents are bbcode - not HTML
138 * called when a local status post or comment has been stored on the local system
139 * $b is the item array of the information which has just been stored in the database
140 * Please note: body contents are bbcode - not HTML
143 * called when receiving a post from another source. This may also be used to post local activity or system generated messages.
144 * $b is the item array of information to be stored in the database and the item body is bbcode.
147 * called when generating the HTML for the user Settings page
148 * $b is the (string) HTML of the settings page before the final '</form>' tag.
151 * called when the Settings pages are submitted
152 * $b is the $_POST array
155 * called when generating the HTML for the addon settings page
156 * $b is the (string) HTML of the addon settings page before the final '</form>' tag.
158 ### 'addon_settings_post'
159 * called when the Addon Settings pages are submitted
160 * $b is the $_POST array
163 * called when posting a profile page
164 * $b is the $_POST array
167 'profile_edit' is called prior to output of profile edit page.
168 $b is an array containing:
170 'profile' => profile (array) record from the database
171 'entry' => the (string) HTML of the generated entry
173 ### 'profile_advanced'
174 * called when the HTML is generated for the 'Advanced profile', corresponding to the 'Profile' tab within a person's profile page
175 * $b is the (string) HTML representation of the generated profile
176 * The profile array details are in $a->profile.
179 'directory_item' is called from the Directory page when formatting an item for display.
182 'contact' => contact (array) record for the person from the database
183 'entry' => the (string) HTML of the generated entry
185 ### 'profile_sidebar_enter'
186 * called prior to generating the sidebar "short" profile for a page
187 * $b is the person's profile array
189 ### 'profile_sidebar'
190 'profile_sidebar is called when generating the sidebar "short" profile for a page.
193 'profile' => profile (array) record for the person from the database
194 'entry' => the (string) HTML of the generated entry
196 ### 'contact_block_end'
197 is called when formatting the block of contacts/friends on a profile sidebar has completed.
200 'contacts' => array of contacts
201 'output' => the (string) generated HTML of the contact block
204 * called during conversion of bbcode to html
205 * $b is a string converted text
208 * called during conversion of html to bbcode (e.g. remote message posting)
209 * $b is a string converted text
212 * called after building the page navigation section
213 * $b is a string HTML of nav region
216 'personal_xrd' is called prior to output of personal XRD file.
219 'user' => the user record for the person
220 'xml' => the complete XML to be output
223 * called prior to output home page content, shown to unlogged users
224 * $b is (string) HTML of section region
227 is called when editing contact details on an individual from the Contacts page.
230 'contact' => contact record (array) of target contact
231 'output' => the (string) generated HTML of the contact edit page
233 ### 'contact_edit_post'
234 * called when posting the contact edit page.
235 * $b is the $_POST array
238 * called just after DB has been opened and before session start
239 * $b is not used or passed
242 * called after HTML content functions have completed
243 * $b is (string) HTML of content div
246 'avatar_lookup' is called when looking up the avatar.
249 'size' => the size of the avatar that will be looked up
250 'email' => email to look up the avatar for
251 'url' => the (string) generated URL of the avatar
253 ### 'emailer_send_prepare'
254 'emailer_send_prepare' called from Emailer::send() before building the mime message.
255 $b is an array, params to Emailer::send()
257 'fromName' => name of the sender
258 'fromEmail' => email fo the sender
259 'replyTo' => replyTo address to direct responses
260 'toEmail' => destination email address
261 'messageSubject' => subject of the message
262 'htmlVersion' => html version of the message
263 'textVersion' => text only version of the message
264 'additionalMailHeader' => additions to the smtp mail header
267 is called before calling PHP's mail().
268 $b is an array, params to mail()
276 is called after the navigational menu is build in include/nav.php.
277 $b is an array containing $nav from nav.php.
280 is called before vars are passed to the template engine to render the page.
281 The registered function can add,change or remove variables passed to template.
284 'template' => filename of template
285 'vars' => array of vars passed to template
287 ### ''acl_lookup_end'
288 is called after the other queries have passed.
289 The registered function can add, change or remove the acl_lookup() variables.
291 'results' => array of the acl_lookup() vars
294 Complete list of hook callbacks
297 Here is a complete list of all hook callbacks with file locations (as of 14-Feb-2012). Please see the source for details of any hooks not documented above.
299 boot.php: Addon::callHooks('login_hook',$o);
301 boot.php: Addon::callHooks('profile_sidebar_enter', $profile);
303 boot.php: Addon::callHooks('profile_sidebar', $arr);
305 boot.php: Addon::callHooks("proc_run", $arr);
307 include/contact_selectors.php: Addon::callHooks('network_to_name', $nets);
309 include/api.php: Addon::callHooks('logged_in', $a->user);
311 include/api.php: Addon::callHooks('logged_in', $a->user);
313 include/queue.php: Addon::callHooks('queue_predeliver', $a, $r);
315 include/queue.php: Addon::callHooks('queue_deliver', $a, $params);
317 include/text.php: Addon::callHooks('contact_block_end', $arr);
319 include/text.php: Addon::callHooks('smilie', $s);
321 include/text.php: Addon::callHooks('prepare_body_init', $item);
323 include/text.php: Addon::callHooks('prepare_body', $prep_arr);
325 include/text.php: Addon::callHooks('prepare_body_final', $prep_arr);
327 include/nav.php: Addon::callHooks('page_header', $a->page['nav']);
329 include/auth.php: Addon::callHooks('authenticate', $addon_auth);
331 include/bbcode.php: Addon::callHooks('bbcode',$Text);
333 include/oauth.php: Addon::callHooks('logged_in', $a->user);
335 include/acl_selectors.php: Addon::callHooks($a->module . '_pre_' . $selname, $arr);
337 include/acl_selectors.php: Addon::callHooks($a->module . '_post_' . $selname, $o);
339 include/acl_selectors.php: Addon::callHooks('contact_select_options', $x);
341 include/acl_selectors.php: Addon::callHooks($a->module . '_pre_' . $selname, $arr);
343 include/acl_selectors.php: Addon::callHooks($a->module . '_post_' . $selname, $o);
345 include/acl_selectors.php: Addon::callHooks($a->module . '_pre_' . $selname, $arr);
347 include/acl_selectors.php: Addon::callHooks($a->module . '_post_' . $selname, $o);
349 include/acl_selectors.php Addon::callHooks('acl_lookup_end', $results);
351 include/notifier.php: Addon::callHooks('notifier_normal',$target_item);
353 include/notifier.php: Addon::callHooks('notifier_end',$target_item);
355 include/items.php: Addon::callHooks('atom_feed', $atom);
357 include/items.php: Addon::callHooks('atom_feed_end', $atom);
359 include/items.php: Addon::callHooks('atom_feed_end', $atom);
361 include/items.php: Addon::callHooks('parse_atom', $arr);
363 include/items.php: Addon::callHooks('post_remote',$arr);
365 include/items.php: Addon::callHooks('atom_author', $o);
367 include/items.php: Addon::callHooks('atom_entry', $o);
369 include/bb2diaspora.php: Addon::callHooks('bb2diaspora',$Text);
371 include/cronhooks.php: Addon::callHooks('cron', $d);
373 include/security.php: Addon::callHooks('logged_in', $a->user);
375 include/html2bbcode.php: Addon::callHooks('html2bbcode', $text);
377 include/Contact.php: Addon::callHooks('remove_user',$r[0]);
379 include/Contact.php: Addon::callHooks('contact_photo_menu', $args);
381 include/conversation.php: Addon::callHooks('conversation_start',$cb);
383 include/conversation.php: Addon::callHooks('render_location',$locate);
385 include/conversation.php: Addon::callHooks('display_item', $arr);
387 include/conversation.php: Addon::callHooks('render_location',$locate);
389 include/conversation.php: Addon::callHooks('display_item', $arr);
391 include/conversation.php: Addon::callHooks('item_photo_menu', $args);
393 include/conversation.php: Addon::callHooks('jot_tool', $jotplugins);
395 include/conversation.php: Addon::callHooks('jot_networks', $jotnets);
397 index.php: Addon::callHooks('init_1');
399 index.php:Addon::callHooks('app_menu', $arr);
401 index.php:Addon::callHooks('page_end', $a->page['content']);
403 mod/photos.php: Addon::callHooks('photo_post_init', $_POST);
405 mod/photos.php: Addon::callHooks('photo_post_file',$ret);
407 mod/photos.php: Addon::callHooks('photo_post_end',$foo);
409 mod/photos.php: Addon::callHooks('photo_post_end',$foo);
411 mod/photos.php: Addon::callHooks('photo_post_end',$foo);
413 mod/photos.php: Addon::callHooks('photo_post_end',intval($item_id));
415 mod/photos.php: Addon::callHooks('photo_upload_form',$ret);
417 mod/friendica.php: Addon::callHooks('about_hook', $o);
419 mod/editpost.php: Addon::callHooks('jot_tool', $jotplugins);
421 mod/editpost.php: Addon::callHooks('jot_networks', $jotnets);
423 mod/parse_url.php: Addon::callHooks('parse_link', $arr);
425 mod/home.php: Addon::callHooks('home_init',$ret);
427 mod/home.php: Addon::callHooks("home_content",$o);
429 mod/contacts.php: Addon::callHooks('contact_edit_post', $_POST);
431 mod/contacts.php: Addon::callHooks('contact_edit', $arr);
433 mod/settings.php: Addon::callHooks('addon_settings_post', $_POST);
435 mod/settings.php: Addon::callHooks('connector_settings_post', $_POST);
437 mod/settings.php: Addon::callHooks('settings_post', $_POST);
439 mod/settings.php: Addon::callHooks('addon_settings', $settings_addons);
441 mod/settings.php: Addon::callHooks('connector_settings', $settings_connectors);
443 mod/settings.php: Addon::callHooks('settings_form',$o);
445 mod/register.php: Addon::callHooks('register_account', $newuid);
447 mod/like.php: Addon::callHooks('post_local_end', $arr);
449 mod/xrd.php: Addon::callHooks('personal_xrd', $arr);
451 mod/item.php: Addon::callHooks('post_local_start', $_REQUEST);
453 mod/item.php: Addon::callHooks('post_local',$datarray);
455 mod/item.php: Addon::callHooks('post_local_end', $datarray);
457 mod/profile.php: Addon::callHooks('profile_advanced',$o);
459 mod/profiles.php: Addon::callHooks('profile_post', $_POST);
461 mod/profiles.php: Addon::callHooks('profile_edit', $arr);
463 mod/tagger.php: Addon::callHooks('post_local_end', $arr);
465 mod/cb.php: Addon::callHooks('cb_init');
467 mod/cb.php: Addon::callHooks('cb_post', $_POST);
469 mod/cb.php: Addon::callHooks('cb_afterpost');
471 mod/cb.php: Addon::callHooks('cb_content', $o);
473 mod/directory.php: Addon::callHooks('directory_item', $arr);
475 src/Model/Item.php Addon::callHooks('tagged', $arr);