]> git.mxchange.org Git - friendica.git/blob - doc/Plugins.md
Merge pull request #3110 from tobiasd/20170127-translationdocs
[friendica.git] / doc / Plugins.md
1 Friendica Addon/Plugin development
2 ==============
3
4 * [Home](help)
5
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).
9
10 Plugin 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/plugin name.
13 For instance "plugin1name_install()".
14 These two functions take no arguments and are usually responsible for registering (and unregistering) event hooks that your plugin will require.
15 The install and uninstall functions will also be called (i.e. re-installed) if the plugin 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".
18
19 Plugins should contain a comment block with the four following parameters:
20
21     /*
22      * Name: My Great Plugin
23      * Description: This is what my plugin does. It's really cool.
24      * Version: 1.0
25      * Author: John Q. Public <john@myfriendicasite.com>
26      */
27
28 Register your plugin hooks during installation.
29
30     register_hook($hookname, $file, $function);
31
32 $hookname is a string and corresponds to a known Friendica hook.
33
34 $file is a pathname relative to the top-level Friendica directory.
35 This *should* be 'addon/plugin_name/plugin_name.php' in most cases.
36
37 $function is a string and is the name of the function which will be executed when the hook is called.
38
39 Arguments
40 ---
41 Your hook callback functions will be called with at least one and possibly two arguments
42
43     function myhook_function(App $a, &$b) {
44
45     }
46
47
48 If you wish to make changes to the calling data, you must declare them as reference variables (with '&') during function declaration.
49
50 #### $a
51 $a is the Friendica 'App' class.
52 It contains a wealth of information about the current state of Friendica:
53
54 * which module has been called,
55 * configuration information,
56 * the page contents at the point the hook was invoked,
57 * profile and user information, etc.
58
59 It is recommeded you call this '$a' to match its usage elsewhere.
60
61 #### $b
62 $b can be called anything you like.
63 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.
64 Remember to declare it with '&' if you wish to alter it.
65
66 Modules
67 ---
68
69 Plugins/addons may also act as "modules" and intercept all page requests for a given URL path.
70 In order for a plugin to act as a module it needs to define a function "plugin_name_module()" which takes no arguments and needs not do anything.
71
72 If this function exists, you will now receive all page requests for "http://my.web.site/plugin_name" - with any number of URL components as additional arguments.
73 These are parsed into an array $a->argv, with a corresponding $a->argc indicating the number of URL components.
74 So http://my.web.site/plugin/arg1/arg2 would look for a module named "plugin" and pass its module functions the $a App structure (which is available to many components).
75 This will include:
76
77     $a->argc = 3
78     $a->argv = array(0 => 'plugin', 1 => 'arg1', 2 => 'arg2');
79
80 Your module functions will often contain the function plugin_name_content(App $a), which defines and returns the page body content.
81 They may also contain plugin_name_post(App $a) which is called before the _content function and typically handles the results of POST forms.
82 You may also have plugin_name_init(App $a) which is called very early on and often does module initialisation.
83
84 Templates
85 ---
86
87 If your plugin needs some template, you can use the Friendica template system.
88 Friendica uses [smarty3](http://www.smarty.net/) as a template engine.
89
90 Put your tpl files in the *templates/* subfolder of your plugin.
91
92 In your code, like in the function plugin_name_content(), load the template file and execute it passing needed values:
93
94     # load template file. first argument is the template name,
95     # second is the plugin path relative to friendica top folder
96     $tpl = get_markup_template('mytemplate.tpl', 'addon/plugin_name/');
97
98     # apply template. first argument is the loaded template,
99     # second an array of 'name'=>'values' to pass to template
100     $output = replace_macros($tpl,array(
101         'title' => 'My beautiful plugin',
102     ));
103
104 See also the wiki page [Quick Template Guide](https://github.com/friendica/friendica/wiki/Quick-Template-Guide).
105
106 Current hooks
107 -------------
108
109 ### 'authenticate'
110 'authenticate' is called when a user attempts to login.
111 $b is an array containing:
112
113     'username' => the supplied username
114     'password' => the supplied password
115     'authenticated' => set this to non-zero to authenticate the user.
116     'user_record' => successful authentication must also return a valid user record from the database
117
118 ### 'logged_in'
119 'logged_in' is called after a user has successfully logged in.
120 $b contains the $a->user array.
121
122 ### 'display_item'
123 'display_item' is called when formatting a post for display.
124 $b is an array:
125
126         'item' => The item (array) details pulled from the database
127         'output' => the (string) HTML representation of this item prior to adding it to the page
128
129 ### 'post_local'
130 * called when a status post or comment is entered on the local system
131 * $b is the item array of the information to be stored in the database
132 * Please note: body contents are bbcode - not HTML
133
134 ### 'post_local_end'
135 * called when a local status post or comment has been stored on the local system
136 * $b is the item array of the information which has just been stored in the database
137 * Please note: body contents are bbcode - not HTML
138
139 ### 'post_remote'
140 * called when receiving a post from another source. This may also be used to post local activity or system generated messages.
141 * $b is the item array of information to be stored in the database and the item body is bbcode.
142
143 ### 'settings_form'
144 * called when generating the HTML for the user Settings page
145 * $b is the (string) HTML of the settings page before the final '</form>' tag.
146
147 ### 'settings_post'
148 * called when the Settings pages are submitted
149 * $b is the $_POST array
150
151 ### 'plugin_settings'
152 * called when generating the HTML for the addon settings page
153 * $b is the (string) HTML of the addon settings page before the final '</form>' tag.
154
155 ### 'plugin_settings_post'
156 * called when the Addon Settings pages are submitted
157 * $b is the $_POST array
158
159 ### 'profile_post'
160 * called when posting a profile page
161 * $b is the $_POST array
162
163 ### 'profile_edit'
164 'profile_edit' is called prior to output of profile edit page.
165 $b is an array containing:
166
167         'profile' => profile (array) record from the database
168         'entry' => the (string) HTML of the generated entry
169
170 ### 'profile_advanced'
171 * called when the HTML is generated for the 'Advanced profile', corresponding to the 'Profile' tab within a person's profile page
172 * $b is the (string) HTML representation of the generated profile
173 * The profile array details are in $a->profile.
174
175 ### 'directory_item'
176 'directory_item' is called from the Directory page when formatting an item for display.
177 $b is an array:
178
179         'contact' => contact (array) record for the person from the database
180     'entry' => the (string) HTML of the generated entry
181
182 ### 'profile_sidebar_enter'
183 * called prior to generating the sidebar "short" profile for a page
184 * $b is the person's profile array
185
186 ### 'profile_sidebar'
187 'profile_sidebar is called when generating the sidebar "short" profile for a page.
188 $b is an array:
189
190         'profile' => profile (array) record for the person from the database
191         'entry' => the (string) HTML of the generated entry
192
193 ### 'contact_block_end'
194 is called when formatting the block of contacts/friends on a profile sidebar has completed.
195 $b is an array:
196
197         'contacts' => array of contacts
198         'output' => the (string) generated HTML of the contact block
199
200 ### 'bbcode'
201 * called during conversion of bbcode to html
202 * $b is a string converted text
203
204 ### 'html2bbcode'
205 * called during conversion of html to bbcode (e.g. remote message posting)
206 * $b is a string converted text
207
208 ### 'page_header'
209 * called after building the page navigation section
210 * $b is a string HTML of nav region
211
212 ### 'personal_xrd'
213 'personal_xrd' is called prior to output of personal XRD file.
214 $b is an array:
215
216         'user' => the user record for the person
217         'xml' => the complete XML to be output
218
219 ### 'home_content'
220 * called prior to output home page content, shown to unlogged users
221 * $b is (string) HTML of section region
222
223 ### 'contact_edit'
224 is called when editing contact details on an individual from the Contacts page.
225 $b is an array:
226
227         'contact' => contact record (array) of target contact
228         'output' => the (string) generated HTML of the contact edit page
229
230 ### 'contact_edit_post'
231 * called when posting the contact edit page.
232 * $b is the $_POST array
233
234 ### 'init_1'
235 * called just after DB has been opened and before session start
236 * $b is not used or passed
237
238 ### 'page_end'
239 * called after HTML content functions have completed
240 * $b is (string) HTML of content div
241
242 ### 'avatar_lookup'
243 'avatar_lookup' is called when looking up the avatar.
244 $b is an array:
245
246         'size' => the size of the avatar that will be looked up
247         'email' => email to look up the avatar for
248         'url' => the (string) generated URL of the avatar
249
250 ### 'emailer_send_prepare'
251 'emailer_send_prepare' called from Emailer::send() before building the mime message.
252 $b is an array, params to Emailer::send()
253
254     'fromName' => name of the sender
255     'fromEmail' => email fo the sender
256     'replyTo' => replyTo address to direct responses
257     'toEmail' => destination email address
258     'messageSubject' => subject of the message
259     'htmlVersion' => html version of the message
260     'textVersion' => text only version of the message
261     'additionalMailHeader' => additions to the smtp mail header
262
263 ### 'emailer_send'
264 is called before calling PHP's mail().
265 $b is an array, params to mail()
266
267     'to'
268     'subject'
269     'body'
270     'headers'
271
272 ### 'nav_info'
273 is called after the navigational menu is build in include/nav.php.
274 $b is an array containing $nav from nav.php.
275
276 ### 'template_vars'
277 is called before vars are passed to the template engine to render the page.
278 The registered function can add,change or remove variables passed to template.
279 $b is an array with:
280
281     'template' => filename of template
282     'vars' => array of vars passed to template
283
284 ### ''acl_lookup_end'
285 is called after the other queries have passed.
286 The registered function can add, change or remove the acl_lookup() variables.
287
288     'results' => array of the acl_lookup() vars
289
290
291 Complete list of hook callbacks
292 ---
293
294 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.
295
296 boot.php:       call_hooks('login_hook',$o);
297
298 boot.php:       call_hooks('profile_sidebar_enter', $profile);
299
300 boot.php:       call_hooks('profile_sidebar', $arr);
301
302 boot.php:       call_hooks("proc_run", $arr);
303
304 include/contact_selectors.php:  call_hooks('network_to_name', $nets);
305
306 include/api.php:                                call_hooks('logged_in', $a->user);
307
308 include/api.php:                call_hooks('logged_in', $a->user);
309
310 include/queue.php:              call_hooks('queue_predeliver', $a, $r);
311
312 include/queue.php:                              call_hooks('queue_deliver', $a, $params);
313
314 include/text.php:       call_hooks('contact_block_end', $arr);
315
316 include/text.php:       call_hooks('smilie', $s);
317
318 include/text.php:       call_hooks('prepare_body_init', $item);
319
320 include/text.php:       call_hooks('prepare_body', $prep_arr);
321
322 include/text.php:       call_hooks('prepare_body_final', $prep_arr);
323
324 include/nav.php:        call_hooks('page_header', $a->page['nav']);
325
326 include/auth.php:               call_hooks('authenticate', $addon_auth);
327
328 include/bbcode.php:     call_hooks('bbcode',$Text);
329
330 include/oauth.php:              call_hooks('logged_in', $a->user);
331
332 include/acl_selectors.php:      call_hooks($a->module . '_pre_' . $selname, $arr);
333
334 include/acl_selectors.php:      call_hooks($a->module . '_post_' . $selname, $o);
335
336 include/acl_selectors.php:      call_hooks('contact_select_options', $x);
337
338 include/acl_selectors.php:      call_hooks($a->module . '_pre_' . $selname, $arr);
339
340 include/acl_selectors.php:      call_hooks($a->module . '_post_' . $selname, $o);
341
342 include/acl_selectors.php:      call_hooks($a->module . '_pre_' . $selname, $arr);
343
344 include/acl_selectors.php:      call_hooks($a->module . '_post_' . $selname, $o);
345
346 include/acl_selectors.php       call_hooks('acl_lookup_end', $results);
347
348 include/notifier.php:           call_hooks('notifier_normal',$target_item);
349
350 include/notifier.php:   call_hooks('notifier_end',$target_item);
351
352 include/items.php:      call_hooks('atom_feed', $atom);
353
354 include/items.php:              call_hooks('atom_feed_end', $atom);
355
356 include/items.php:      call_hooks('atom_feed_end', $atom);
357
358 include/items.php:      call_hooks('parse_atom', $arr);
359
360 include/items.php:      call_hooks('post_remote',$arr);
361
362 include/items.php:      call_hooks('atom_author', $o);
363
364 include/items.php:      call_hooks('atom_entry', $o);
365
366 include/bb2diaspora.php:        call_hooks('bb2diaspora',$Text);
367
368 include/cronhooks.php:  call_hooks('cron', $d);
369
370 include/security.php:           call_hooks('logged_in', $a->user);
371
372 include/html2bbcode.php:        call_hooks('html2bbcode', $text);
373
374 include/Contact.php:    call_hooks('remove_user',$r[0]);
375
376 include/Contact.php:    call_hooks('contact_photo_menu', $args);
377
378 include/conversation.php:       call_hooks('conversation_start',$cb);
379
380 include/conversation.php:                               call_hooks('render_location',$locate);
381
382 include/conversation.php:                               call_hooks('display_item', $arr);
383
384 include/conversation.php:                               call_hooks('render_location',$locate);
385
386 include/conversation.php:                               call_hooks('display_item', $arr);
387
388 include/conversation.php:       call_hooks('item_photo_menu', $args);
389
390 include/conversation.php:       call_hooks('jot_tool', $jotplugins);
391
392 include/conversation.php:       call_hooks('jot_networks', $jotnets);
393
394 include/plugin.php:if(! function_exists('call_hooks')) {
395
396 include/plugin.php:function call_hooks($name, &$data = null) {
397
398 index.php:      call_hooks('init_1');
399
400 index.php:call_hooks('app_menu', $arr);
401
402 index.php:call_hooks('page_end', $a->page['content']);
403
404 mod/photos.php: call_hooks('photo_post_init', $_POST);
405
406 mod/photos.php: call_hooks('photo_post_file',$ret);
407
408 mod/photos.php:         call_hooks('photo_post_end',$foo);
409
410 mod/photos.php:         call_hooks('photo_post_end',$foo);
411
412 mod/photos.php:         call_hooks('photo_post_end',$foo);
413
414 mod/photos.php: call_hooks('photo_post_end',intval($item_id));
415
416 mod/photos.php:         call_hooks('photo_upload_form',$ret);
417
418 mod/friendica.php:      call_hooks('about_hook', $o);
419
420 mod/editpost.php:       call_hooks('jot_tool', $jotplugins);
421
422 mod/editpost.php:       call_hooks('jot_networks', $jotnets);
423
424 mod/parse_url.php:      call_hooks('parse_link', $arr);
425
426 mod/home.php:   call_hooks('home_init',$ret);
427
428 mod/home.php:   call_hooks("home_content",$o);
429
430 mod/contacts.php:       call_hooks('contact_edit_post', $_POST);
431
432 mod/contacts.php:               call_hooks('contact_edit', $arr);
433
434 mod/settings.php:               call_hooks('plugin_settings_post', $_POST);
435
436 mod/settings.php:               call_hooks('connector_settings_post', $_POST);
437
438 mod/settings.php:       call_hooks('settings_post', $_POST);
439
440 mod/settings.php:               call_hooks('plugin_settings', $settings_addons);
441
442 mod/settings.php:               call_hooks('connector_settings', $settings_connectors);
443
444 mod/settings.php:       call_hooks('settings_form',$o);
445
446 mod/register.php:       call_hooks('register_account', $newuid);
447
448 mod/like.php:   call_hooks('post_local_end', $arr);
449
450 mod/xrd.php:    call_hooks('personal_xrd', $arr);
451
452 mod/item.php:   call_hooks('post_local_start', $_REQUEST);
453
454 mod/item.php:   call_hooks('post_local',$datarray);
455
456 mod/item.php:   call_hooks('post_local_end', $datarray);
457
458 mod/profile.php:                        call_hooks('profile_advanced',$o);
459
460 mod/profiles.php:       call_hooks('profile_post', $_POST);
461
462 mod/profiles.php:               call_hooks('profile_edit', $arr);
463
464 mod/tagger.php: call_hooks('post_local_end', $arr);
465
466 mod/cb.php:     call_hooks('cb_init');
467
468 mod/cb.php:     call_hooks('cb_post', $_POST);
469
470 mod/cb.php:     call_hooks('cb_afterpost');
471
472 mod/cb.php:     call_hooks('cb_content', $o);
473
474 mod/directory.php:                      call_hooks('directory_item', $arr);