Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

727 lines
22 KiB

7 years ago
7 years ago
7 years ago
4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. Friendica Addon development
  2. ==============
  3. * [Home](help)
  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. Addon names cannot contain spaces or other punctuation and are used as filenames and function names.
  8. You may supply a "friendly" name within the comment block.
  9. Each addon must contain both an install and an uninstall function based on the addon name.
  10. For instance "addon1name_install()".
  11. These two functions take no arguments and are usually responsible for registering (and unregistering) event hooks that your addon will require.
  12. The install and uninstall functions will also be called (i.e. re-installed) if the addon changes after installation.
  13. Therefore your uninstall should not destroy data and install should consider that data may already exist.
  14. Future extensions may provide for "setup" amd "remove".
  15. Addons should contain a comment block with the four following parameters:
  16. /*
  17. * Name: My Great Addon
  18. * Description: This is what my addon does. It's really cool.
  19. * Version: 1.0
  20. * Author: John Q. Public <john@myfriendicasite.com>
  21. */
  22. Please also add a README or README.md file to the addon directory.
  23. It will be displayed in the admin panel and should include some further information in addition to the header information.
  24. ## PHP addon hooks
  25. Register your addon hooks during installation.
  26. Addon::registerHook($hookname, $file, $function);
  27. $hookname is a string and corresponds to a known Friendica PHP hook.
  28. $file is a pathname relative to the top-level Friendica directory.
  29. This *should* be 'addon/*addon_name*/*addon_name*.php' in most cases.
  30. $function is a string and is the name of the function which will be executed when the hook is called.
  31. ### Arguments
  32. Your hook callback functions will be called with at least one and possibly two arguments
  33. function myhook_function(App $a, &$b) {
  34. }
  35. If you wish to make changes to the calling data, you must declare them as reference variables (with `&`) during function declaration.
  36. #### $a
  37. $a is the Friendica `App` class.
  38. It contains a wealth of information about the current state of Friendica:
  39. * which module has been called,
  40. * configuration information,
  41. * the page contents at the point the hook was invoked,
  42. * profile and user information, etc.
  43. It is recommeded you call this `$a` to match its usage elsewhere.
  44. #### $b
  45. $b can be called anything you like.
  46. 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.
  47. Remember to declare it with `&` if you wish to alter it.
  48. ## Global stylesheets
  49. If your addon requires adding a stylesheet on all pages of Friendica, add the following hook:
  50. ```php
  51. function <addon>_install()
  52. {
  53. Addon::registerHook('head', __FILE__, '<addon>_head');
  54. ...
  55. }
  56. function <addon>_head(App $a)
  57. {
  58. $a->registerStylesheet(__DIR__ . '/relative/path/to/addon/stylesheet.css');
  59. }
  60. ```
  61. `__DIR__` is the folder path of your addon.
  62. ## JavaScript
  63. ### Global scripts
  64. If your addon requires adding a script on all pages of Friendica, add the following hook:
  65. ```php
  66. function <addon>_install()
  67. {
  68. Addon::registerHook('footer', __FILE__, '<addon>_footer');
  69. ...
  70. }
  71. function <addon>_footer(App $a)
  72. {
  73. $a->registerFooterScript(__DIR__ . '/relative/path/to/addon/script.js');
  74. }
  75. ```
  76. `__DIR__` is the folder path of your addon.
  77. ### JavaScript hooks
  78. The main Friendica script provides hooks via events dispatched on the `document` property.
  79. In your Javascript file included as described above, add your event listener like this:
  80. ```js
  81. document.addEventListener(name, callback);
  82. ```
  83. - *name* is the name of the hook and corresponds to a known Friendica JavaScript hook.
  84. - *callback* is a JavaScript anonymous function to execute.
  85. More info about Javascript event listeners: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
  86. #### Current JavaScript hooks
  87. ##### postprocess_liveupdate
  88. Called at the end of the live update process (XmlHttpRequest) and on a post preview.
  89. No additional data is provided.
  90. ## Modules
  91. Addons may also act as "modules" and intercept all page requests for a given URL path.
  92. 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.
  93. 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.
  94. These are parsed into an array $a->argv, with a corresponding $a->argc indicating the number of URL components.
  95. 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).
  96. This will include:
  97. ```php
  98. $a->argc = 3
  99. $a->argv = array(0 => 'addon', 1 => 'arg1', 2 => 'arg2');
  100. ```
  101. Your module functions will often contain the function addon_name_content(App $a), which defines and returns the page body content.
  102. They may also contain addon_name_post(App $a) which is called before the _content function and typically handles the results of POST forms.
  103. You may also have addon_name_init(App $a) which is called very early on and often does module initialisation.
  104. ## Templates
  105. If your addon needs some template, you can use the Friendica template system.
  106. Friendica uses [smarty3](http://www.smarty.net/) as a template engine.
  107. Put your tpl files in the *templates/* subfolder of your addon.
  108. In your code, like in the function addon_name_content(), load the template file and execute it passing needed values:
  109. ```php
  110. # load template file. first argument is the template name,
  111. # second is the addon path relative to friendica top folder
  112. $tpl = Renderer::getMarkupTemplate('mytemplate.tpl', 'addon/addon_name/');
  113. # apply template. first argument is the loaded template,
  114. # second an array of 'name' => 'values' to pass to template
  115. $output = Renderer::replaceMacros($tpl, array(
  116. 'title' => 'My beautiful addon',
  117. ));
  118. ```
  119. See also the wiki page [Quick Template Guide](https://github.com/friendica/friendica/wiki/Quick-Template-Guide).
  120. ## Current PHP hooks
  121. ### authenticate
  122. Called when a user attempts to login.
  123. `$b` is an array containing:
  124. - **username**: the supplied username
  125. - **password**: the supplied password
  126. - **authenticated**: set this to non-zero to authenticate the user.
  127. - **user_record**: successful authentication must also return a valid user record from the database
  128. ### logged_in
  129. Called after a user has successfully logged in.
  130. `$b` contains the `$a->user` array.
  131. ### display_item
  132. Called when formatting a post for display.
  133. $b is an array:
  134. - **item**: The item (array) details pulled from the database
  135. - **output**: the (string) HTML representation of this item prior to adding it to the page
  136. ### post_local
  137. Called when a status post or comment is entered on the local system.
  138. `$b` is the item array of the information to be stored in the database.
  139. Please note: body contents are bbcode - not HTML.
  140. ### post_local_end
  141. Called when a local status post or comment has been stored on the local system.
  142. `$b` is the item array of the information which has just been stored in the database.
  143. Please note: body contents are bbcode - not HTML
  144. ### post_remote
  145. Called when receiving a post from another source. This may also be used to post local activity or system generated messages.
  146. `$b` is the item array of information to be stored in the database and the item body is bbcode.
  147. ### settings_form
  148. Called when generating the HTML for the user Settings page.
  149. `$b` is the HTML string of the settings page before the final `</form>` tag.
  150. ### settings_post
  151. Called when the Settings pages are submitted.
  152. `$b` is the $_POST array.
  153. ### addon_settings
  154. Called when generating the HTML for the addon settings page.
  155. `$b` is the (string) HTML of the addon settings page before the final `</form>` tag.
  156. ### addon_settings_post
  157. Called when the Addon Settings pages are submitted.
  158. `$b` is the $_POST array.
  159. ### profile_post
  160. Called when posting a profile page.
  161. `$b` is the $_POST array.
  162. ### profile_edit
  163. Called prior to output of profile edit page.
  164. `$b` is an array containing:
  165. - **profile**: profile (array) record from the database
  166. - **entry**: the (string) HTML of the generated entry
  167. ### profile_advanced
  168. Called when the HTML is generated for the Advanced profile, corresponding to the Profile tab within a person's profile page.
  169. `$b` is the HTML string representation of the generated profile.
  170. The profile array details are in `$a->profile`.
  171. ### directory_item
  172. Called from the Directory page when formatting an item for display.
  173. `$b` is an array:
  174. - **contact**: contact record array for the person from the database
  175. - **entry**: the HTML string of the generated entry
  176. ### profile_sidebar_enter
  177. Called prior to generating the sidebar "short" profile for a page.
  178. `$b` is the person's profile array
  179. ### profile_sidebar
  180. Called when generating the sidebar "short" profile for a page.
  181. `$b` is an array:
  182. - **profile**: profile record array for the person from the database
  183. - **entry**: the HTML string of the generated entry
  184. ### contact_block_end
  185. Called when formatting the block of contacts/friends on a profile sidebar has completed.
  186. `$b` is an array:
  187. - **contacts**: array of contacts
  188. - **output**: the generated HTML string of the contact block
  189. ### bbcode
  190. Called after conversion of bbcode to HTML.
  191. `$b` is an HTML string converted text.
  192. ### html2bbcode
  193. Called after tag conversion of HTML to bbcode (e.g. remote message posting)
  194. `$b` is a string converted text
  195. ### head
  196. Called when building the `<head>` sections.
  197. Stylesheets should be registered using this hook.
  198. `$b` is an HTML string of the `<head>` tag.
  199. ### page_header
  200. Called after building the page navigation section.
  201. `$b` is a string HTML of nav region.
  202. ### personal_xrd
  203. Called prior to output of personal XRD file.
  204. `$b` is an array:
  205. - **user**: the user record array for the person
  206. - **xml**: the complete XML string to be output
  207. ### home_content
  208. Called prior to output home page content, shown to unlogged users.
  209. `$b` is the HTML sring of section region.
  210. ### contact_edit
  211. Called when editing contact details on an individual from the Contacts page.
  212. $b is an array:
  213. - **contact**: contact record (array) of target contact
  214. - **output**: the (string) generated HTML of the contact edit page
  215. ### contact_edit_post
  216. Called when posting the contact edit page.
  217. `$b` is the `$_POST` array
  218. ### init_1
  219. Called just after DB has been opened and before session start.
  220. No hook data.
  221. ### page_end
  222. Called after HTML content functions have completed.
  223. `$b` is (string) HTML of content div.
  224. ### footer
  225. Called after HTML content functions have completed.
  226. Deferred Javascript files should be registered using this hook.
  227. `$b` is (string) HTML of footer div/element.
  228. ### avatar_lookup
  229. Called when looking up the avatar. `$b` is an array:
  230. - **size**: the size of the avatar that will be looked up
  231. - **email**: email to look up the avatar for
  232. - **url**: the (string) generated URL of the avatar
  233. ### emailer_send_prepare
  234. Called from `Emailer::send()` before building the mime message.
  235. `$b` is an array of params to `Emailer::send()`.
  236. - **fromName**: name of the sender
  237. - **fromEmail**: email fo the sender
  238. - **replyTo**: replyTo address to direct responses
  239. - **toEmail**: destination email address
  240. - **messageSubject**: subject of the message
  241. - **htmlVersion**: html version of the message
  242. - **textVersion**: text only version of the message
  243. - **additionalMailHeader**: additions to the smtp mail header
  244. ### emailer_send
  245. Called before calling PHP's `mail()`.
  246. `$b` is an array of params to `mail()`.
  247. - **to**
  248. - **subject**
  249. - **body**
  250. - **headers**
  251. ### load_config
  252. Called during `App` initialization to allow addons to load their own configuration file(s) with `App::loadConfigFile()`.
  253. ### nav_info
  254. Called after the navigational menu is build in `include/nav.php`.
  255. `$b` is an array containing `$nav` from `include/nav.php`.
  256. ### template_vars
  257. Called before vars are passed to the template engine to render the page.
  258. The registered function can add,change or remove variables passed to template.
  259. `$b` is an array with:
  260. - **template**: filename of template
  261. - **vars**: array of vars passed to the template
  262. ### acl_lookup_end
  263. Called after the other queries have passed.
  264. The registered function can add, change or remove the `acl_lookup()` variables.
  265. - **results**: array of the acl_lookup() vars
  266. ### prepare_body_init
  267. Called at the start of prepare_body
  268. Hook data:
  269. - **item** (input/output): item array
  270. ### prepare_body_content_filter
  271. Called before the HTML conversion in prepare_body. If the item matches a content filter rule set by an addon, it should
  272. just add the reason to the filter_reasons element of the hook data.
  273. Hook data:
  274. - **item**: item array (input)
  275. - **filter_reasons** (input/output): reasons array
  276. ### prepare_body
  277. Called after the HTML conversion in `prepare_body()`.
  278. Hook data:
  279. - **item** (input): item array
  280. - **html** (input/output): converted item body
  281. - **is_preview** (input): post preview flag
  282. - **filter_reasons** (input): reasons array
  283. ### prepare_body_final
  284. Called at the end of `prepare_body()`.
  285. Hook data:
  286. - **item**: item array (input)
  287. - **html**: converted item body (input/output)
  288. ### put_item_in_cache
  289. Called after `prepare_text()` in `put_item_in_cache()`.
  290. Hook data:
  291. - **item** (input): item array
  292. - **rendered-html** (input/output): final item body HTML
  293. - **rendered-hash** (input/output): original item body hash
  294. ### magic_auth_success
  295. Called when a magic-auth was successful.
  296. Hook data:
  297. visitor => array with the contact record of the visitor
  298. url => the query string
  299. ## Complete list of hook callbacks
  300. 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.
  301. ### index.php
  302. Addon::callHooks('init_1');
  303. Addon::callHooks('app_menu', $arr);
  304. Addon::callHooks('page_content_top', $a->page['content']);
  305. Addon::callHooks($a->module.'_mod_init', $placeholder);
  306. Addon::callHooks($a->module.'_mod_init', $placeholder);
  307. Addon::callHooks($a->module.'_mod_post', $_POST);
  308. Addon::callHooks($a->module.'_mod_afterpost', $placeholder);
  309. Addon::callHooks($a->module.'_mod_content', $arr);
  310. Addon::callHooks($a->module.'_mod_aftercontent', $arr);
  311. Addon::callHooks('page_end', $a->page['content']);
  312. ### include/api.php
  313. Addon::callHooks('logged_in', $a->user);
  314. Addon::callHooks('authenticate', $addon_auth);
  315. Addon::callHooks('logged_in', $a->user);
  316. ### include/enotify.php
  317. Addon::callHooks('enotify', $h);
  318. Addon::callHooks('enotify_store', $datarray);
  319. Addon::callHooks('enotify_mail', $datarray);
  320. Addon::callHooks('check_item_notification', $notification_data);
  321. ### include/conversation.php
  322. Addon::callHooks('conversation_start', $cb);
  323. Addon::callHooks('render_location', $locate);
  324. Addon::callHooks('display_item', $arr);
  325. Addon::callHooks('display_item', $arr);
  326. Addon::callHooks('item_photo_menu', $args);
  327. Addon::callHooks('jot_tool', $jotplugins);
  328. ### include/text.php
  329. Addon::callHooks('contact_block_end', $arr);
  330. Addon::callHooks('poke_verbs', $arr);
  331. Addon::callHooks('put_item_in_cache', $hook_data);
  332. Addon::callHooks('prepare_body_init', $item);
  333. Addon::callHooks('prepare_body_content_filter', $hook_data);
  334. Addon::callHooks('prepare_body', $hook_data);
  335. Addon::callHooks('prepare_body_final', $hook_data);
  336. ### include/items.php
  337. Addon::callHooks('page_info_data', $data);
  338. ### mod/directory.php
  339. Addon::callHooks('directory_item', $arr);
  340. ### mod/xrd.php
  341. Addon::callHooks('personal_xrd', $arr);
  342. ### mod/ping.php
  343. Addon::callHooks('network_ping', $arr);
  344. ### mod/parse_url.php
  345. Addon::callHooks("parse_link", $arr);
  346. ### mod/manage.php
  347. Addon::callHooks('home_init', $ret);
  348. ### mod/acl.php
  349. Addon::callHooks('acl_lookup_end', $results);
  350. ### mod/network.php
  351. Addon::callHooks('network_content_init', $arr);
  352. Addon::callHooks('network_tabs', $arr);
  353. ### mod/friendica.php
  354. Addon::callHooks('about_hook', $o);
  355. ### mod/subthread.php
  356. Addon::callHooks('post_local_end', $arr);
  357. ### mod/profiles.php
  358. Addon::callHooks('profile_post', $_POST);
  359. Addon::callHooks('profile_edit', $arr);
  360. ### mod/settings.php
  361. Addon::callHooks('addon_settings_post', $_POST);
  362. Addon::callHooks('connector_settings_post', $_POST);
  363. Addon::callHooks('display_settings_post', $_POST);
  364. Addon::callHooks('settings_post', $_POST);
  365. Addon::callHooks('addon_settings', $settings_addons);
  366. Addon::callHooks('connector_settings', $settings_connectors);
  367. Addon::callHooks('display_settings', $o);
  368. Addon::callHooks('settings_form', $o);
  369. ### mod/photos.php
  370. Addon::callHooks('photo_post_init', $_POST);
  371. Addon::callHooks('photo_post_file', $ret);
  372. Addon::callHooks('photo_post_end', $foo);
  373. Addon::callHooks('photo_post_end', $foo);
  374. Addon::callHooks('photo_post_end', $foo);
  375. Addon::callHooks('photo_post_end', $foo);
  376. Addon::callHooks('photo_post_end', intval($item_id));
  377. Addon::callHooks('photo_upload_form', $ret);
  378. ### mod/profile.php
  379. Addon::callHooks('profile_advanced', $o);
  380. ### mod/home.php
  381. Addon::callHooks('home_init', $ret);
  382. Addon::callHooks("home_content", $content);
  383. ### mod/poke.php
  384. Addon::callHooks('post_local_end', $arr);
  385. ### mod/contacts.php
  386. Addon::callHooks('contact_edit_post', $_POST);
  387. Addon::callHooks('contact_edit', $arr);
  388. ### mod/tagger.php
  389. Addon::callHooks('post_local_end', $arr);
  390. ### mod/lockview.php
  391. Addon::callHooks('lockview_content', $item);
  392. ### mod/uexport.php
  393. Addon::callHooks('uexport_options', $options);
  394. ### mod/register.php
  395. Addon::callHooks('register_post', $arr);
  396. Addon::callHooks('register_form', $arr);
  397. ### mod/item.php
  398. Addon::callHooks('post_local_start', $_REQUEST);
  399. Addon::callHooks('post_local', $datarray);
  400. Addon::callHooks('post_local_end', $datarray);
  401. ### mod/editpost.php
  402. Addon::callHooks('jot_tool', $jotplugins);
  403. ### src/Network/FKOAuth1.php
  404. Addon::callHooks('logged_in', $a->user);
  405. ### src/Render/FriendicaSmartyEngine.php
  406. Addon::callHooks("template_vars", $arr);
  407. ### src/App.php
  408. Addon::callHooks('load_config');
  409. Addon::callHooks('head');
  410. Addon::callHooks('footer');
  411. ### src/Model/Item.php
  412. Addon::callHooks('post_local', $item);
  413. Addon::callHooks('post_remote', $item);
  414. Addon::callHooks('post_local_end', $posted_item);
  415. Addon::callHooks('post_remote_end', $posted_item);
  416. Addon::callHooks('tagged', $arr);
  417. Addon::callHooks('post_local_end', $new_item);
  418. ### src/Model/Contact.php
  419. Addon::callHooks('contact_photo_menu', $args);
  420. Addon::callHooks('follow', $arr);
  421. ### src/Model/Profile.php
  422. Addon::callHooks('profile_sidebar_enter', $profile);
  423. Addon::callHooks('profile_sidebar', $arr);
  424. Addon::callHooks('profile_tabs', $arr);
  425. Addon::callHooks('zrl_init', $arr);
  426. Addon::callHooks('magic_auth_success', $arr);
  427. ### src/Model/Event.php
  428. Addon::callHooks('event_updated', $event['id']);
  429. Addon::callHooks("event_created", $event['id']);
  430. ### src/Model/User.php
  431. Addon::callHooks('register_account', $uid);
  432. Addon::callHooks('remove_user', $user);
  433. ### src/Content/Text/BBCode.php
  434. Addon::callHooks('bbcode', $text);
  435. Addon::callHooks('bb2diaspora', $text);
  436. ### src/Content/Text/HTML.php
  437. Addon::callHooks('html2bbcode', $message);
  438. ### src/Content/Smilies.php
  439. Addon::callHooks('smilie', $params);
  440. ### src/Content/Feature.php
  441. Addon::callHooks('isEnabled', $arr);
  442. Addon::callHooks('get', $arr);
  443. ### src/Content/ContactSelector.php
  444. Addon::callHooks('network_to_name', $nets);
  445. Addon::callHooks('gender_selector', $select);
  446. Addon::callHooks('sexpref_selector', $select);
  447. Addon::callHooks('marital_selector', $select);
  448. ### src/Content/OEmbed.php
  449. Addon::callHooks('oembed_fetch_url', $embedurl, $j);
  450. ### src/Content/Nav.php
  451. Addon::callHooks('page_header', $a->page['nav']);
  452. Addon::callHooks('nav_info', $nav);
  453. ### src/Worker/Directory.php
  454. Addon::callHooks('globaldir_update', $arr);
  455. ### src/Worker/Notifier.php
  456. Addon::callHooks('notifier_end', $target_item);
  457. ### src/Worker/Queue.php
  458. Addon::callHooks('queue_predeliver', $r);
  459. Addon::callHooks('queue_deliver', $params);
  460. ### src/Module/Login.php
  461. Addon::callHooks('authenticate', $addon_auth);
  462. Addon::callHooks('login_hook', $o);
  463. ### src/Module/Logout.php
  464. Addon::callHooks("logging_out");
  465. ### src/Object/Post.php
  466. Addon::callHooks('render_location', $locate);
  467. Addon::callHooks('display_item', $arr);
  468. ### src/Core/ACL.php
  469. Addon::callHooks('contact_select_options', $x);
  470. Addon::callHooks($a->module.'_pre_'.$selname, $arr);
  471. Addon::callHooks($a->module.'_post_'.$selname, $o);
  472. Addon::callHooks($a->module.'_pre_'.$selname, $arr);
  473. Addon::callHooks($a->module.'_post_'.$selname, $o);
  474. Addon::callHooks('jot_networks', $jotnets);
  475. ### src/Core/Authentication.php
  476. Addon::callHooks('logged_in', $a->user);
  477. ### src/Core/Worker.php
  478. Addon::callHooks("proc_run", $arr);
  479. ### src/Util/Emailer.php
  480. Addon::callHooks('emailer_send_prepare', $params);
  481. Addon::callHooks("emailer_send", $hookdata);
  482. ### src/Util/Map.php
  483. Addon::callHooks('generate_map', $arr);
  484. Addon::callHooks('generate_named_map', $arr);
  485. Addon::callHooks('Map::getCoordinates', $arr);
  486. ### src/Util/Network.php
  487. Addon::callHooks('avatar_lookup', $avatar);
  488. ### src/Util/ParseUrl.php
  489. Addon::callHooks("getsiteinfo", $siteinfo);
  490. ### src/Protocol/DFRN.php
  491. Addon::callHooks('atom_feed_end', $atom);
  492. Addon::callHooks('atom_feed_end', $atom);
  493. ### view/js/main.js
  494. document.dispatchEvent(new Event('postprocess_liveupdate'));