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