adding some i18n information
[squirrelmail.git] / doc / plugin.txt
CommitLineData
99098885 1$Id$
2
9cd2ae7d 3In addition to this document, please check out the SquirrelMail
4development FAQ for more information. Also, help writing plugins
5is easily obtained by posting to the squirrelmail-plugins mailing
b2978b37 6list. (See details about mailing lists on the website)
99098885 7
8FAQ -> http://www.squirrelmail.org/wiki/wiki.php?DeveloperFAQ
9cd2ae7d 9Plugin Development ->
10 http://www.squirrelmail.org/wiki/wiki.php?DevelopingPlugins
99098885 11
12
1aaef171 13A FEW NOTES ON THE PLUGIN ARCHITECTURE
14======================================
15
9cd2ae7d 16The plugin architecture of SquirrelMail is designed to make it possible
17to add new features without having to patch SquirrelMail itself.
18Functionality like password changing, displaying ads and calendars should
19be possible to add as plugins.
1aaef171 20
21
9cd2ae7d 22The Idea
1aaef171 23--------
24
25The idea is to be able to run random code at given places in the
26SquirrelMail code. This random code should then be able to do whatever
27needed to enhance the functionality of SquirrelMail. The places where
28code can be executed are called "hooks".
29
30There are some limitations in what these hooks can do. It is difficult
31to use them to change the layout and to change functionality that
32already is in SquirrelMail.
33
34Some way for the plugins to interact with the help subsystem and
35translations will be provided.
36
37
9cd2ae7d 38The Implementation
1aaef171 39------------------
40
9cd2ae7d 41The plugin jumping off point in the main SquirrelMail code is in the
42file functions/plugin.php. In places where hooks are made available,
43they are executed by calling the function do_hook('hookname'). The
44do_hook function then traverses the array
45$squirrelmail_plugin_hooks['hookname'] and executes all the functions
46that are named in that array. Those functions are placed there when
47plugins register themselves with SquirrelMail as discussed below. A
48plugin may add its own internal functions to this array under any
49hook name provided by the SquirrelMail developers.
1aaef171 50
51A plugin must reside in a subdirectory in the plugins/ directory. The
9cd2ae7d 52name of the subdirectory is considered to be the name of the plugin.
53(The plugin will not function correctly if this is not the case.)
1aaef171 54
55To start using a plugin, its name must be added to the $plugins array
56in config.php like this:
57
9cd2ae7d 58 $plugins[0] = 'plugin_name';
1aaef171 59
9cd2ae7d 60When a plugin is registered, the file plugins/plugin_name/setup.php is
61included and the function squirrelmail_plugin_init_plugin_name() is
62called with no parameters. That function is where the plugin may
63register itself against any hooks it wishes to take advantage of.
1aaef171 64
65
9cd2ae7d 66WRITING PLUGINS
67===============
68
69All plugins must contain a file called setup.php and must include a
70function called squirrelmail_plugin_init_plugin_name() therein. Since
71including numerous plugins can slow SquirrelMail performance
72considerably, the setup.php file should contain little else. Any
73functions that are registered against plugin hooks should do little
74more than call another function in a different file.
75
76Any other files used by the plugin should also be placed in the
77plugin directory (or subdirectory thereof) and should contain the
78bulk of the plugin logic.
1aaef171 79
9cd2ae7d 80The function squirrelmail_plugin_init_plugin_name() is called to
81initalize a plugin. This function could look something like this (if
82the plugin was named "demo" and resided in the directory plugins/demo/):
1aaef171 83
9cd2ae7d 84function squirrelmail_plugin_init_demo ()
85{
86 global $squirrelmail_plugin_hooks;
1aaef171 87
9cd2ae7d 88 $squirrelmail_plugin_hooks['generic_header']['demo'] = 'plugin_demo_header';
89 $squirrelmail_plugin_hooks['menuline']['demo'] = 'plugin_demo_menuline';
90}
91
92Please note that as of SquirrelMail 1.5.0, this function will no longer
93be called at run time and will instead be called only once at configure-
94time. Thus, the inclusion of any dynamic code (anything except hook
95registration) here is strongly discouraged.
1aaef171 96
9cd2ae7d 97In this example, the "demo" plugin should also have two other functions
98in its setup.php file called plugin_demo_header() and plugin_demo_menuline().
99The first of these might look something like this:
100
101function plugin_demo_header()
102{
103 include_once(SM_PATH . 'plugins/demo/functions.php');
104 plugin_demo_header_do();
1aaef171 105}
106
9cd2ae7d 107The function called plugin_demo_header_do() would be in the file called
108functions.php in the demo plugin directory and would contain the plugin's
109core logic for the "generic_header" hook.
110
111
112Including Other Files
113---------------------
114
115A plugin may need to reference functionality provided in other
116files, and therefore need to include those files. Most of the
117core SquirrelMail functions are already available to your plugin
118unless it has any files that are requested directly by the client
119browser (custom options page, etc.). In this case, you'll need
120to make sure you include the files you need (see below).
121
122Note that as of SquirrelMail 1.4.0, all files are accessed using a
123constant called SM_PATH that always contains the relative path to
124the main SquirrelMail directory. This constant is always available
125for you to use when including other files from the SquirrelMail core,
126your own plugin, or other plugins, should the need arise. If any of
127your plugin files are requested directly from the client browser,
128you will need to define this constant before you do anything else:
129
130 define('SM_PATH', '../../');
131
132Files are included like this:
133
134 include_once(SM_PATH . 'include/validate.php');
135
136When including files, please make sure to use the include_once() function
137and NOT include(), require(), or require_once(), since these all are much
138less efficient than include_once() and can have a cumulative effect on
139SquirrelMail performance.
140
141The files that you may need to include in a plugin will vary greatly
142depending upon what the plugin is designed to do. For files that are
143requested directly by the client browser, we strongly recommend that
144you include the file include/validate.php, since it will set up the
145SquirrelMail environment automatically. It will ensure the the user
146has been authenticated and is currently logged in, load all user
147preferences, include internationalization support, call stripslashes()
148on all incoming data (if magic_quotes_gpc is on), and initialize and
149include all other basic SquirrelMail resources and functions. You may
150see other plugins that directly include other SquirrelMail files, but
151that is no longer necessary and is a hold-over from older SquirrelMail
152versions.
6b638171 153
e398ba12 154List of files, that are included by include/validate.php (If SquirrelMail
155version is not listed, files are included from v.1.3.2.):
156 1. class/mime.class.php
157 1.1. class/mime/Rfc822Header.class.php
158 1.2. class/mime/MessageHeader.class.php
159 1.3. class/mime/AddressStructure.class.php
160 1.4. class/mime/Message.class.php
161 1.5. class/mime/SMimeMessage.class.php
162 1.6. class/mime/Disposition.class.php
163 1.7. class/mime/Language.class.php
164 1.8. class/mime/ContentType.class.php
165 2. functions/global.php
166 3. functions/strings.php
167 4. config/config.php
168 4.1. config/config_local.php (from 1.4.0rc1)
169 5. functions/i18n.php
170 5.1. functions/global.php (from 1.4.0)
171 6. functions/auth.php
172 7. include/load_prefs.php
173 7.1. include/validate.php
174 7.2. functions/prefs.php
175 7.3. functions/plugin.php
176 7.3.1. functions/global.php (from 1.4.0 and 1.5.0)
177 7.3.2. functions/prefs.php (from 1.5.1)
178 7.4. functions/constants.php
179 7.5. do_hook('loading_prefs')
180 8. functions/page_header.php
181 8.1. functions/strings.php
182 8.2. functions/html.php
183 8.3. functions/imap_mailbox.php
184 8.3.1. functions/imap_utf7_local.php
185 8.4. functions/global.php
186 9. functions/prefs.php
187 9.1. functions/global.php
188 9.2. $prefs_backend (from 1.4.3rc1 and 1.5.0)
189 functions/db_prefs.php
190 functions/file_prefs.php
6b638171 191
9cd2ae7d 192Hook Types: Parameters and Return Values
193-----------------------------------------
194
f4fd89e0 195Hooks, when executed, are called with differing parameters and may or may
196not take return values, all depending on the type of hook being called and
197the context in which it is being used. On the source side (where the hook
198call originates), all hooks have at least one parameter, which is the
199name of the hook. After that, things get complicated.
200
201 do_hook
202 -------
203 Most hook calls don't pass any data and don't ask for anything back.
204 These always use the do_hook call. A limited number of do_hook calls do
205 pass some extra parameters, in which case your plugin may modify the
206 given data if you do so by reference. It is not necessary to return
207 anything from your function in such a case; modifying the parameter
208 data by reference is what does the job (although the hook call itself
209 (in the source) must grab the return value for this to work). Note
210 that in this case, the parameter to your hook function will be an array,
211 the first element simply being the hook name, followed by any other
212 parameters that may have been included in the actual hook call in the
213 source. Modify parameters with care!
214
215 do_hook_function
216 ----------------
217 This hook type was intended to be the main hook type used when the
218 source needs to get something back from your plugin. It is somewhat
219 limited in that it will only use the value returned from the LAST
220 plugin registered against the hook. The source for this hook might
221 use the return value for internal purposes, or might expect you to
222 provide text or HTML to be sent to the client browser (you'll have to
223 look at its use in context to understand how you should return values
224 here). The parameters that your hook function gets will be anything
225 you see AFTER the hook name in the actual hook call in the source.
226 These cannot be changed in the same way that the do_hook parameters
227 can be.
228
229 concat_hook_function
230 --------------------
231 This is a newer hook type meant to address the shortcomings of
232 do_hook_function; specifically in that it uses the return values of
233 all plugins registered against the hook. In order to do so, the
234 return value is assumed to be a string, which is just piled on top
235 of whatever it got from the other plugins working on the same hook.
236 Again, you'll have to inspect the source code to see how such data
237 is put to use, but most of the time, it is used to create a string
238 of HTML to be inserted into the output page. The parameters that
239 your hook function will get are the same as for the do_hook_function;
240 they are anything AFTER the hook name in the actual hook call in the
241 source.
242
efea59ed 243 boolean_hook_function
244 ---------------------
f4fd89e0 245 The newest of the SquirrelMail hooks, this type is used to let all
246 plugins registered against the hook to "vote" for some action. What
247 that action is is entirely dependent on how the hook is used in the
248 source (look for yourself). Plugins make their "vote" by returning
249 TRUE or FALSE. This hook may be configured to "tally votes" in one
250 of three ways. This configuration is done with the third parameter
251 in the hook call in the source:
252 > 0 -- Any one or more TRUEs will override any FALSEs
253 < 0 -- Any one or more FALSEs will override any TRUEs
254 = 0 -- Majority wins. Ties are broken in this case with
255 the last parameter in the hook call in the source.
256 Your hook function will get the second paramter in the hook call in
257 the source as its parameter (this might be an array if multiple values
258 need to be passed).
a3a95e4a 259
f4fd89e0 260See below for further discussion of special hook types and the values
a3a95e4a 261
262
9cd2ae7d 263List of Hooks
6b638171 264-------------
ef3c69f0 265
9cd2ae7d 266This is a list of all hooks currently available in SquirrelMail, ordered
267by file. Note that this list is accurate as of June 17, 2003 (should be
268close to what is contained in release 1.4.1, plus or minus a hook or two),
269but may be out of date soon thereafter. You never know. ;-)
6b638171 270
9cd2ae7d 271 Hook Name Found In Called With(#)
272 --------- -------- --------------
df788686 273 abook_init functions/addressbook.php do_hook
274 abook_add_class functions/addressbook.php do_hook
9cd2ae7d 275 loading_constants functions/constants.php do_hook
54067ccd 276 logout_error functions/display_messages.php do_hook
277 error_box functions/display_messages.php concat_hook
9cd2ae7d 278 get_pref_override functions/file_prefs.php hook_func
279 get_pref functions/file_prefs.php hook_func
280 special_mailbox functions/imap_mailbox.php hook_func
281 % rename_or_delete_folder functions/imap_mailbox.php hook_func
9cd2ae7d 282 mailbox_index_before functions/mailbox_display.php do_hook
283 mailbox_form_before functions/mailbox_display.php do_hook
284 mailbox_index_after functions/mailbox_display.php do_hook
285 check_handleAsSent_result functions/mailbox_display.php do_hook
286 subject_link functions/mailbox_display.php concat_hook
c5aaf57f 287 mailbox_display_buttons functions/mailbox_display.php do_hook
7c788b1c 288 mailbox_display_button_action functions/mailbox_display.php hook_func
9cd2ae7d 289 message_body functions/mime.php do_hook
290 ^ attachment $type0/$type1 functions/mime.php do_hook
9ad17edb 291 attachments_bottom functions/mime.php hook_func
c4115032 292 decode_body functions/mime.php hook_func
9cd2ae7d 293 generic_header functions/page_header.php do_hook
294 menuline functions/page_header.php do_hook
c5aaf57f 295 internal_link functions/page_header.php hook_func
9cd2ae7d 296 loading_prefs include/load_prefs.php do_hook
297 addrbook_html_search_below src/addrbook_search_html.php do_hook
298 addressbook_bottom src/addressbook.php do_hook
299 compose_form src/compose.php do_hook
300 compose_bottom src/compose.php do_hook
301 compose_button_row src/compose.php do_hook
302 compose_send src/compose.php do_hook
303 folders_bottom src/folders.php do_hook
304 help_top src/help.php do_hook
305 help_chapter src/help.php do_hook
306 help_bottom src/help.php do_hook
7022cc97 307 left_main_after_each_folder src/left_main.php concat_hook
9cd2ae7d 308 left_main_before src/left_main.php do_hook
309 left_main_after src/left_main.php do_hook
310 login_cookie src/login.php do_hook
311 login_top src/login.php do_hook
312 login_form src/login.php do_hook
313 login_bottom src/login.php do_hook
9cd2ae7d 314 * optpage_set_loadinfo src/options.php do_hook
315 * optpage_loadhook_personal src/options.php do_hook
316 * optpage_loadhook_display src/options.php do_hook
317 * optpage_loadhook_highlight src/options.php do_hook
318 * optpage_loadhook_folder src/options.php do_hook
319 * optpage_loadhook_order src/options.php do_hook
320 * options_personal_save src/options.php do_hook
321 * options_display_save src/options.php do_hook
322 * options_folder_save src/options.php do_hook
323 * options_save src/options.php do_hook
324 * optpage_register_block src/options.php do_hook
325 * options_link_and_description src/options.php do_hook
326 * options_personal_inside src/options.php do_hook
327 * options_display_inside src/options.php do_hook
328 * options_highlight_inside src/options.php do_hook
329 * options_folder_inside src/options.php do_hook
330 * options_order_inside src/options.php do_hook
331 * options_personal_bottom src/options.php do_hook
332 * options_display_bottom src/options.php do_hook
333 * options_highlight_bottom src/options.php do_hook
334 * options_folder_bottom src/options.php do_hook
335 * options_order_bottom src/options.php do_hook
336 * options_highlight_bottom src/options_highlight.php do_hook
337 & options_identities_process src/options_identities.php do_hook
338 & options_identities_top src/options_identities.php do_hook
339 &% options_identities_renumber src/options_identities.php do_hook
340 & options_identities_table src/options_identities.php concat_hook
341 & options_identities_buttons src/options_identities.php concat_hook
342 message_body src/printer_friendly_bottom.php do_hook
343 read_body_header src/read_body.php do_hook
d44e63d5 344 read_body_menu_top src/read_body.php hook_func
9cd2ae7d 345 read_body_menu_bottom src/read_body.php do_hook
346 read_body_header_right src/read_body.php do_hook
9cd2ae7d 347 read_body_top src/read_body.php do_hook
348 read_body_bottom src/read_body.php do_hook
9cd2ae7d 349 login_before src/redirect.php do_hook
350 login_verified src/redirect.php do_hook
351 generic_header src/right_main.php do_hook
352 right_main_after_header src/right_main.php do_hook
353 right_main_bottom src/right_main.php do_hook
354 search_before_form src/search.php do_hook
355 search_after_form src/search.php do_hook
356 search_bottom src/search.php do_hook
357 logout src/signout.php do_hook
358 webmail_top src/webmail.php do_hook
d44e63d5 359 webmail_bottom src/webmail.php concat_hook
9cd2ae7d 360 logout_above_text src/signout.php concat_hook
8ed1cfc6 361 O info_bottom plugins/info/options.php do_hook
9cd2ae7d 362
363% = This hook is used in multiple places in the given file
364# = Called with hook type (see below)
365& = Special identity hooks (see below)
366^ = Special attachments hook (see below)
367* = Special options hooks (see below)
8ed1cfc6 368O = optional hook used by plugin
6b638171 369
6b638171 370
9cd2ae7d 371(#) Called With
372---------------
373Each hook is called using the hook type specified in the list above:
374 do_hook do_hook()
375 hook_func do_hook_function()
376 concat_hook concat_hook_function()
a3a95e4a 377
378
0f101579 379(&) Identity Hooks
380------------------
9cd2ae7d 381This set of hooks is passed special information in the array of arguments:
0f101579 382
383options_identities_process
9cd2ae7d 384
385 This hook is called at the top of the Identities page, which is
386 most useful when the user has changed any identity settings - this
387 is where you'll want to save any custom information you are keeping
388 for each identity or catch any custom submit buttons that you may
389 have added to the identities page. The arguments to this hook are:
390
391 [0] = hook name (always "options_identities_process")
392 [1] = should I run the SaveUpdateFunction() (alterable)
393
394 Obviously, set the second array element to 1/true if you want to
395 trigger SaveUpdateFunction() after the hook is finished - by default,
396 it will not be called.
0f101579 397
398options_identities_renumber
9cd2ae7d 399
400 This hook is called when one of the identities is being renumbered,
401 such as if the user had three identities and deletes the second -
402 this hook would be called with an array that looks like this:
403 ('options_identities_renumber', 2, 1). The arguments to this hook
404 are:
405
406 [0] = hook name (always "options_identities_renumber")
407 [1] = being renumbered from ('default' or 1 through (# idents) - 1)
408 [2] = being renumbered to ('default' or 1 through (# idents) - 1)
0f101579 409
410options_identities_table
9cd2ae7d 411
412 This hook allows you to insert additional rows into the table that
413 holds each identity. The arguments to this hook are:
414
415 [0] = color of table (use it like this in your plugin:
cb3425db 416 <tr bgcolor="<?php echo $info[1]; ?>">
9cd2ae7d 417 [1] = is this an empty section (the one at the end of the list)?
418 [2] = what is the 'post' value? (ident # or empty string if default)
419
420 You need to return any HTML you would like to add to the table.
421 You could add a table row with code similar to this:
422
423 function demo_identities_table(&$args)
424 {
425 return '<tr bgcolor="' . $args[0] . '"><td>&nbsp;</td><td>'
426 . 'YOUR CODE HERE' . '</td></tr>' . "\n";
427 }
0f101579 428
429options_identities_buttons
9cd2ae7d 430
431 This hook allows you to add a button (or other HTML) to the row of
432 buttons under each identity. The arguments to this hook are:
433
434 [0] = is this an empty section (the one at the end of the list)?
435 [1] = what is the 'post' value? (ident # or empty string if default)
436
437 You need to return any HTML you would like to add here. You could add
438 a button with code similar to this:
439
440 function demo_identities_button(&$args)
441 {
442 return '<input type="submit" name="demo_button_' . $args[1]
5f75494f 443 . '" value="Press Me" />';
9cd2ae7d 444 }
0f101579 445
446
a3a95e4a 447(^) Attachment Hooks
448--------------------
449When a message has attachments, this hook is called with the MIME types. For
450instance, a .zip file hook is "attachment application/x-zip". The hook should
451probably show a link to do a specific action, such as "Verify" or "View" for a
9cd2ae7d 452.zip file. Thus, to register your plugin for .zip attachments, you'd do this
453in setup.php (assuming your plugin is called "demo"):
454
455 $squirrelmail_plugin_hooks['attachment application/x-zip']['demo']
456 = 'demo_handle_zip_attachment';
a3a95e4a 457
458This is a breakdown of the data passed in the array to the hook that is called:
459
460 [0] = Hook's name ('attachment text/plain')
9cd2ae7d 461 [1] = Array of links of actions (see below) (alterable)
a3a95e4a 462 [2] = Used for returning to mail message (startMessage)
463 [3] = Used for finding message to display (id)
464 [4] = Mailbox name, urlencode()'d (urlMailbox)
465 [5] = Entity ID inside mail message (ent)
9cd2ae7d 466 [6] = Default URL to go to when filename is clicked on (alterable)
ef30bf50 467 [7] = Filename that is displayed for the attachment
468 [8] = Sent if message was found from a search (where)
469 [9] = Sent if message was found from a search (what)
a3a95e4a 470
471To set up links for actions, you assign them like this:
472
9cd2ae7d 473 $Args[1]['<plugin_name>']['href'] = 'URL to link to';
474 $Args[1]['<plugin_name>']['text'] = 'What to display';
441f2d33 475
ae2f65a9 476It's also possible to specify a hook as "attachment type0/*",
477for example "attachment text/*". This hook will be executed whenever there's
478no more specific rule available for that type.
479
9cd2ae7d 480Putting all this together, the demo_handle_zip_attachment() function should
481look like this (note the argument being passed):
57945c53 482
9cd2ae7d 483 function demo_handle_zip_attachment(&$Args)
484 {
485 include_once(SM_PATH . 'plugins/demo/functions.php');
486 demo_handle_zip_attachment_do($Args);
487 }
57945c53 488
9cd2ae7d 489And the demo_handle_zip_attachment_do() function in the
490plugins/demo/functions.php file would typically (but not necessarily)
491display a custom link:
492
493 function demo_handle_zip_attachment_do(&$Args)
494 {
495 $Args[1]['demo']['href'] = SM_PATH . 'plugins/demo/zip_handler.php?'
496 . 'passed_id=' . $Args[3] . '&mailbox=' . $Args[4]
497 . '&passed_ent_id=' . $Args[5];
498 $Args[1]['demo']['text'] = 'show zip contents';
499 }
500
501The file plugins/demo/zip_handler.php can now do whatever it needs with the
502attachment (note that this will hand information about how to retrieve the
503source message from the IMAP server as GET varibles).
504
505
506(*) Options
507-----------
508Before you start adding user preferences to your plugin, please take a moment
509to think about it: in some cases, more options may not be a good thing.
510Having too many options can be confusing. Thinking from the user's
511perspective, will the proposed options actually be used? Will users
512understand what these options are for?
513
514There are two ways to add options for your plugin. When you only have a few
515options that don't merit an entirely new preferences page, you can incorporate
516them into an existing section of SquirrelMail preferences (Personal
517Information, Display Preferences, Message Highlighting, Folder Preferences or
518Index Order). Or, if you have an extensive number of settings or for some
519reason need a separate page for the user to interact with, you can create your
520own preferences page.
521
522
523Integrating Your Options Into Existing SquirrelMail Preferences Pages
524---------------------------------------------------------------------
525
526There are two ways to accomplish the integration of your plugin's settings
527into another preferences page. The first method is to add the HTML code
528for your options directly to the preferences page of your choice. Although
529currently very popular, this method will soon be deprecated, so avoid it
530if you can. That said, here is how it works. :) Look for any of the hooks
531named as "options_<pref page>_inside", where <pref page> is "display",
532"personal", etc. For this example, we'll use "options_display_inside" and,
533as above, "demo" as our plugin name:
534
535 1. In setup.php in the squirrelmail_plugin_init_demo() function:
536
537 $squirrelmail_plugin_hooks['options_display_inside']['demo']
538 = 'demo_show_options';
539
540 Note that there are also hooks such as "options_display_bottom",
541 however, they place your options at the bottom of the preferences
542 page, which is usually not desirable (mostly because they also
543 come AFTER the HTML FORM tag is already closed). It is possible
544 to use these hooks if you want to create your own FORM with custom
545 submission logic.
546
547 2. Assuming the function demo_show_options() calls another function
548 elsewhere called demo_show_options_do(), that function should have
549 output similar to this (note that you will be inserting code into
550 a table that is already defined with two columns, so please be sure
551 to keep this framework in your plugin):
552
553 ------cut here-------
554 <tr>
555 <td>
556 OPTION_NAME
557 </td>
558 <td>
559 OPTION_INPUT
560 </td>
561 </tr>
562 ------cut here-------
563
564 Of course, you can place any text where OPTION_NAME is and any input
565 tags where OPTION_INPUT is.
566
567 3. You will want to use the "options_<pref page>_save" hook (in this case,
568 "options_display_save") to save the user's settings after they have
569 pressed the "Submit" button. Again, back in setup.php in the
570 squirrelmail_plugin_init_demo() function:
57945c53 571
9cd2ae7d 572 $squirrelmail_plugin_hooks['options_display_save']['demo']
573 = 'demo_save_options';
57945c53 574
9cd2ae7d 575 4. Assuming the function demo_save_options() calls another function
576 elsewhere called demo_save_options_do(), that function should put
577 the user's settings into permanent storage (see the preferences
578 section below for more information). This example assumes that
579 in the preferences page, the INPUT tag's NAME attribute was set
580 to "demo_option":
581
582 global $data_dir, $username;
583 sqgetGlobalVar('demo_option', $demo_option);
584 setPref($data_dir, $username, 'demo_option', $demo_option);
585
586
587The second way to add options to one of the SquirrelMail preferences page is
588to use one of the "optpage_loadhook_<pref page>" hooks. The sent_subfolders
60eeb409 589plugin has an excellent example of this method. Briefly, this way of adding
9cd2ae7d 590options consists of adding some plugin-specific information to a predefined
591data structure which SquirrelMail then uses to build the HTML input forms
592for you. This is the preferred method of building options lists going forward.
593
594 1. We'll use the "optpage_loadhook_display" hook to add a new group of
595 options to the display preferences page. In setup.php in the
596 squirrelmail_plugin_init_demo() function:
597
598 $squirrelmail_plugin_hooks['optpage_loadhook_display']['demo']
599 = 'demo_options';
600
601 2. Assuming the function demo_options() calls another function elsewhere
602 called demo_options_do(), that function needs to add a new key to two
603 arrays, $optpage_data['grps'] and $optpage_data['vals']. The value
604 associated with that key should simply be a section heading for your
605 plugin on the preferences page for the $optpage_data['grps'] array,
606 and yet another array with all of your plugin's options for the
607 $optpage_data['vals'] array. The options are built as arrays (yes,
608 that's four levels of nested arrays) that specify attributes that are
609 used by SquirrelMail to build your HTML input tags automatically.
610 This example includes just one input element, a SELECT (drop-down)
611 list:
612
613 global $optpage_data;
614 $optpage_data['grps']['DEMO_PLUGIN'] = 'Demo Options';
615 $optionValues = array();
616 $optionValues[] = array(
617 'name' => 'plugin_demo_favorite_color',
618 'caption' => 'Please Choose Your Favorite Color',
619 'type' => SMOPT_TYPE_STRLIST,
620 'refresh' => SMOPT_REFRESH_ALL,
621 'posvals' => array(0 => 'red',
622 1 => 'blue',
623 2 => 'green',
624 3 => 'orange'),
625 'save' => 'save_plugin_demo_favorite_color'
626 );
627 $optpage_data['vals']['DEMO_PLUGIN'] = $optionValues;
628
629 The array that you use to specify each plugin option has the following
630 possible attributes:
631
6976aad7 632 name The name of this setting, which is used not only for
633 the INPUT tag name, but also for the name of this
634 setting in the user's preferences
635 caption The text that prefaces this setting on the preferences
636 page
361d6e1b 637 trailing_text Text that follows a text input or select list input on
638 the preferences page (useful for indicating units,
639 meanings of special values, etc.)
6976aad7 640 type The type of INPUT element, which should be one of:
641 SMOPT_TYPE_STRING String/text input
642 SMOPT_TYPE_STRLIST Select list input
643 SMOPT_TYPE_TEXTAREA Text area input
644 SMOPT_TYPE_INTEGER Integer input
645 SMOPT_TYPE_FLOAT Floating point number input
646 SMOPT_TYPE_BOOLEAN Boolean (yes/no radio buttons)
60eeb409 647 input
6976aad7 648 SMOPT_TYPE_HIDDEN Hidden input (not actually
649 shown on preferences page)
650 SMOPT_TYPE_COMMENT Text is shown (specified by the
651 'comment' attribute), but no
652 user input is needed
653 SMOPT_TYPE_FLDRLIST Select list of IMAP folders
654 refresh Indicates if a link should be shown to refresh part or
655 all of the window (optional). Possible values are:
656 SMOPT_REFRESH_NONE No refresh link is shown
657 SMOPT_REFRESH_FOLDERLIST Link is shown to refresh
658 only the folder list
659 SMOPT_REFRESH_ALL Link is shown to refresh
660 the entire window
661 initial_value The value that should initially be placed in this
662 INPUT element
663 posvals For select lists, this should be an associative array,
664 where each key is an actual input value and the
665 corresponding value is what is displayed to the user
666 for that list item in the drop-down list
667 value Specify the default/preselected value for this option
668 input
669 save You may indicate that special functionality needs to be
670 used instead of just saving this setting by giving the
671 name of a function to call when this value would
672 otherwise just be saved in the user's preferences
673 size Specifies the size of certain input items (typically
674 textual inputs). Possible values are:
675 SMOPT_SIZE_TINY
676 SMOPT_SIZE_SMALL
677 SMOPT_SIZE_MEDIUM
678 SMOPT_SIZE_LARGE
679 SMOPT_SIZE_HUGE
680 SMOPT_SIZE_NORMAL
681 comment For SMOPT_TYPE_COMMENT type options, this is the text
682 displayed to the user
683 script This is where you may add any additional javascript
684 or other code to the user input
685 post_script You may specify some script (usually Javascript) that
686 will be placed after (outside of) the INPUT tag.
9cd2ae7d 687
60eeb409 688 Note that you do not have to create a whole new section on the options
689 page if you merely want to add a simple input item or two to an options
690 section that already exists. For example, the Display Options page has
691 these groups:
692
693 0 - General Display Options
694 1 - Mailbox Display Options
695 2 - Message Display and Composition
696
697 To add our previous input drop-down to the Mailbox Display Options,
698 we would not have to create our own group; just add it to group
699 number one:
700
701 global $optpage_data;
702 $optpage_data['vals'][1][] = array(
703 'name' => 'plugin_demo_favorite_color',
704 'caption' => 'Please Choose Your Favorite Color',
705 'type' => SMOPT_TYPE_STRLIST,
706 'refresh' => SMOPT_REFRESH_ALL,
707 'posvals' => array(0 => 'red',
708 1 => 'blue',
709 2 => 'green',
710 3 => 'orange'),
711 'save' => 'save_plugin_demo_favorite_color'
712 );
713
9cd2ae7d 714 3. If you indicated a 'save' attribute for any of your options, you must
715 create that function (you'll only need to do this if you need to do
716 some special processing for one of your settings). The function gets
717 one parameter, which is an object with mostly the same attributes you
718 defined when you made the option above... the 'new_value' (and possibly
719 'value', which is the current value for this setting) is the most useful
720 attribute in this context:
721
722 function save_plugin_demo_favorite_color($option)
723 {
724 // if user chose orange, make note that they are really dumb
725 if ($option->new_value == 3)
726 {
727 // more code here as needed
728 }
729
730 // don't even save this setting if user chose green (old
731 // setting will remain)
732 if ($option->new_value == 2)
733 return;
734
735 // for all other colors, save as normal
736 save_option($option);
737 }
738
739
740Creating Your Own Preferences Page
741----------------------------------
742
743It is also possible to create your own preferences page for a plugin. This
744is particularly useful when your plugin has numerous options or needs to
745offer special interaction with the user (for things such as changing password,
746etc.). Here is an outline of how to do so (again, using the "demo" plugin
747name):
748
749 1. Add a new listing to the main Options page. Older versions of
750 SquirrelMail offered a hook called "options_link_and_description"
751 although its use is deprecated (and it is harder to use in that
752 it requires you to write your own HTML to add the option). Instead,
753 you should always use the "optpage_register_block" hook where you
754 create a simple array that lets SquirrelMail build the HTML
755 to add the plugin options entry automatically. In setup.php in the
756 squirrelmail_plugin_init_demo() function:
757
758 $squirrelmail_plugin_hooks['optpage_register_block']['demo']
759 = 'demo_options_block';
760
761 2. Assuming the function demo_options_block() calls another function
762 elsewhere called demo_options_block_do(), that function only needs
763 to create a simple array and add it to the $optpage_blocks array:
764
765 global $optpage_blocks;
766 $optpage_blocks[] = array(
767 'name' => 'Favorite Color Settings',
768 'url' => SM_PATH . 'plugins/demo/options.php',
769 'desc' => 'Change your favorite color & find new exciting colors',
770 'js' => FALSE
771 );
772
773 The array should have four elements:
774 name The title of the plugin's options as it will be displayed on
775 the Options page
776 url The URI that points to your plugin's custom preferences page
777 desc A description of what the preferences page offers the user,
778 displayed on the Options page below the title
779 js Indicates if this option page requires the client browser
780 to be Javascript-capable. Should be TRUE or FALSE.
781
782 3. There are two different ways to create the actual preferences page
783 itself. One is to simply write all of your own HTML and other
784 interactive functionality, while the other is to define some data
785 structures that allow SquirrelMail to build your user inputs and save
786 your data automatically.
787
788 Building your own page is wide open, and for ideas, you should look at
789 any of the plugins that currently have their own preferences pages. If
790 you do this, make sure to read step number 4 below for information on
791 saving settings. In order to maintain security, consistant look and
792 feel, internationalization support and overall integrity, there are just
793 a few things you should always do in this case: define the SM_PATH
794 constant, include the file include/validate.php (see the section about
795 including other files above) and make a call to place the standard page
796 heading at the top of your preferences page. The top of your PHP file
797 might look something like this:
798
799 define('SM_PATH', '../../');
800 include_once(SM_PATH . 'include/validate.php');
801 global $color;
802 displayPageHeader($color, 'None');
803
804 From here you are on your own, although you are encouraged to do things
805 such as use the $color array to keep your HTML correctly themed, etc.
806
807 If you want SquirrelMail to build your preferences page for you,
808 creating input forms and automatically saving users' settings, then
809 you should change the 'url' attribute in the options block you created
810 in step number 2 above to read as follows:
811
812 'url' => SM_PATH . 'src/options.php?optpage=plugin_demo',
813
814 Now, you will need to use the "optpage_set_loadinfo" hook to tell
815 SquirrelMail about your new preferences page. In setup.php in the
816 squirrelmail_plugin_init_demo() function:
57945c53 817
9cd2ae7d 818 $squirrelmail_plugin_hooks['optpage_set_loadinfo']['demo']
819 = 'demo_optpage_loadinfo';
820
821 Assuming the function demo_optpage_loadinfo() calls another function
822 elsewhere called demo_optpage_loadinfo_do(), that function needs to
823 define values for four variables (make sure you test to see that it
824 is your plugin that is being called by checking the GET variable you
825 added to the url just above):
826
827 global $optpage, $optpage_name, $optpage_file,
828 $optpage_loader, $optpage_loadhook;
829 if ($optpage == 'plugin_demo')
830 {
831 $optpage_name = "Favorite Color Preferences";
832 $optpage_file = SM_PATH . 'plugins/demo/options.php';
833 $optpage_loader = 'load_optpage_data_demo';
834 $optpage_loadhook = 'optpage_loadhook_demo';
835 }
836
837 Now you are ready to build all of your options. In the file you
838 indicated for the variable $optpage_file above, you'll need to create
839 a function named the same as the value you used for $optpage_loader
840 above. In this example, the file plugins/demo/options.php should
841 have at least this function in it:
842
843 function load_optpage_data_demo()
844 {
845 $optpage_data = array();
846 $optpage_data['grps']['DEMO_PLUGIN'] = 'Demo Options';
847 $optionValues = array();
848 $optionValues[] = array(
849 'name' => 'plugin_demo_favorite_color',
850 'caption' => 'Please Choose Your Favorite Color',
851 'type' => SMOPT_TYPE_STRLIST,
852 'refresh' => SMOPT_REFRESH_ALL,
853 'posvals' => array(0 => 'red',
854 1 => 'blue',
855 2 => 'green',
856 3 => 'orange'),
857 'save' => 'save_plugin_demo_favorite_color'
858 );
859 $optpage_data['vals']['DEMO_PLUGIN'] = $optionValues;
860 return $optpage_data;
861 }
862
863 For a detailed description of how you build these options, please read
864 step number 2 for the second method of adding options to an existing
865 preferences page above. Notice that the only difference here is in the
866 very first and last lines of this function where you are actually
867 creating and returning the options array instead of just adding onto it.
868
869 That's all there is to it - SquirrelMail will create a preferences page
870 titled as you indicated for $optpage_name above, and other plugins
871 can even add extra options to this new preferences page. To do so,
872 they should use the hook name you specified for $optpage_loadhook above
873 and use the second method for adding option settings to existing
874 preferences pages described above.
875
876 4. Saving your options settings: if you used the second method in step
877 number 3 above, your settings will be saved automatically (or you can
878 define special functions to save special settings such as the
879 save_plugin_demo_favorite_color() function in the example described
880 above) and there is probably no need to follow this step. If you
881 created your own preferences page from scratch, you'll need to follow
882 this step. First, you need to register your plugin against the
883 "options_save" hook. In setup.php in the squirrelmail_plugin_init_demo()
884 function:
885
886 $squirrelmail_plugin_hooks['options_save']['demo']
887 = 'demo_save_options';
888
889 Assuming the function demo_save_options() calls another function
890 elsewhere called demo_save_options_do(), that function needs to grab
891 all of your POST and/or GET settings values and save them in the user's
892 preferences (for more about preferences, see that section below). Since
893 this is a generic hook called for all custom preferences pages, you
894 should always set "optpage" as a POST or GET variable with a string that
895 uniquely identifies your plugin:
896
6fd95361 897 <input type="hidden" name="optpage" value="plugin_demo" />
9cd2ae7d 898
899 Now in your demo_save_options_do() function, do something like this:
900
901 global $username, $data_dir, $optpage, $favorite_color;
902 if ($optpage == 'plugin_demo')
903 {
904 sqgetGlobalVar('favorite_color', $favorite_color, SQ_FORM);
905 setPref($data_dir, $username, 'favorite_color', $favorite_color);
906 }
907
908 Note that $favorite_color may not need to be globalized, although
909 experience has shown that some versions of PHP don't behave as expected
910 unless you do so. Even when you use SquirrelMail's built-in preferences
911 page generation functionality, you may still use this hook, although
912 there should be no need to do so. If you need to do some complex
913 validation routines, note that it might be better to do so in the file
914 you specified as the "$optpage_file" (in our example, that was the
915 plugins/demo/options.php file), since at this point, you can still
916 redisplay your preferences page. You could put code similar to this
917 in the plugins/demp/options.php file (note that there is no function;
918 this code needs to be executed at include time):
919
920 global $optmode;
921 if ($optmode == 'submit')
922 {
923 // do something here such as validation, etc
924 if (you want to redisplay your preferences page)
925 $optmode = '';
926 }
927
928
929Preferences
930-----------
931
932Saving and retrieving user preferences is very easy in SquirrelMail.
933SquirrelMail supports preference storage in files or in a database
934backend, however, the code you need to write to manipulate preferences
935is the same in both cases.
936
937Setting preferences:
938
939 Setting preferences is done for you if you use the built-in facilities
940 for automatic options construction and presentation (see above). If
941 you need to manually set preferences, however, all you need to do is:
942
943 global $data_dir, $username;
944 setPref($data_dir, $username, 'pref_name', $pref_value);
945
946 Where "pref_name" is the key under which the value will be stored
947 and "pref_value" is a variable that should contain the actual
948 preference value to be stored.
949
950Loading preferences:
951
952 There are two approaches to retrieving plugin (or any other) preferences.
953 You can grab individual preferences one at a time or you can add your
954 plugin's preferences to the routine that loads up user preferences at
955 the beginning of each page request. If you do the latter, making sure
956 to place your preference variables into the global scope, they will be
957 immediately available in all other plugin code. To retrieve a single
958 preference value at any time, do this:
959
960 global $data_dir, $username;
961 $pref_value = getPref($data_dir, $username, 'pref_name', 'default value');
962
963 Where "pref_name" is the preference you are retrieving, "default_value"
964 is what will be returned if the preference is not found for this user,
965 and, of course, "pref_value" is the variable that will get the actual
966 preference value.
967
968 To have all your preferences loaded at once when each page request is
969 made, you'll need to register a function against the "loading_prefs" hook.
970 For our "demo" plugin, in setup.php in the squirrelmail_plugin_init_demo()
971 function:
972
973 $squirrelmail_plugin_hooks['loading_prefs']['demo']
974 = 'demo_load_prefs';
975
976 Assuming the function demo_load_prefs() calls another function
977 elsewhere called demo_load_prefs_do(), that function just needs to
978 pull out any all all preferences you'll be needing elsewhere:
979
980 global $data_dir, $username, $pref_value;
981 $pref_value = getPref($data_dir, $username, 'pref_name', 'default value');
982
983 Remember to globalize each preference, or this code is useless.
984
985
986Internationalization
987--------------------
988
989Although this document may only be available in English, we sure hope that you
990are thinking about making your plugin useful to the thousands of non-English
991speaking SquirrelMail users out there! It is almost rude not to do so, and
992it isn't much trouble, either. This document will only describe how you can
993accomplish the internationalization of a plugin. For more general information
994about PHP and SquirrelMail translation facilities, see:
995
996http://www.squirrelmail.org/wiki/wiki.php?LanguageTranslation
997
998The unofficial way to internationalize a plugin is to put all plugin output
999into the proper format but to rely on the SquirrelMail translation facilities
1000for all the rest. If the plugin were really to get translated, you'd need
1001to make sure that all output strings for your plugin are either added to or
1002already exist in the main SquirrelMail locale files.
1003
1004The better way to make sure your plugin is translated is to create your own
1005locale files and what is called a "gettext domain" (see the link above for
1006more information).
1007
1008There are three basic steps to getting your plugins internationalized: put
1009all output into the proper format, switch gettext domains and create locale
1010files.
1011
1012 1. Putting plugin output into the correct format is quite easy. The hard
1013 part is making sure you catch every last echo statement. You need to
1014 echo text like this:
1015
1016 echo _("Hello");
1017
1018 So, even in the HTML segments of your plugin files, you need to do this:
1019
6fd95361 1020 <input type="submit" value="<?php echo _("Submit"); ?>" />
9cd2ae7d 1021
1022 You can put any text you want inside of the quotes (you MUST use double
1023 quotes!), including HTML tags, etc. What you should think carefully
1024 about is that some languages may use different word ordering, so this
1025 might be problematic:
1026
1027 echo _("I want to eat a ") . $fruitName . _(" before noon");
1028
1029 Because some languages (Japanese, for instance) would need to translate
1030 such a sentence to "Before noon " . $fruitName . " I want to eat", but
1031 with the format above, they are stuck having to translate each piece
1032 separately. You might want to reword your original sentence:
1033
1034 echo _("This is what I want to eat before noon: ") . $fruitName;
1035
1036 2. By default, the SquirrelMail gettext domain is always in use. That
1037 means that any text in the format described above will be translated
1038 using the locale files found in the main SquirrelMail locale directory.
1039 Unless your plugin produces no output or only output that is in fact
1040 translated under the default SquirrelMail domain, you need to create
1041 your own gettext domain. The PHP for doing so is very simple. At
1042 the top of any file that produces any output, place the following code
1043 (again, using "demo" as the plugin name):
1044
1045 bindtextdomain('demo', SM_PATH . 'plugins/demo/locale');
1046 textdomain('demo');
1047
1048 Now all output will be translated using your own custom locale files.
1049 Please be sure to switch back to the SquirrelMail domain at the end
1050 of the file, or many of the other SquirrelMail files may misbehave:
1051
1052 bindtextdomain('squirrelmail', SM_PATH . 'locale');
1053 textdomain('squirrelmail');
1054
1055 Note that if, in the middle of your plugin file, you use any
1056 SquirrelMail functions that send output to the browser, you'll need
1057 to temporarily switch back to the SquirrelMail domain:
1058
1059 bindtextdomain('squirrelmail', SM_PATH . 'locale');
1060 textdomain('squirrelmail');
1061 displayPageHeader($color, 'None');
1062 bindtextdomain('demo', SM_PATH . 'plugins/demo/locale');
1063 textdomain('demo');
1064
1065 Note that technically speaking, you only need to have one bindtextdomain
1066 call per file, you should always use it before every textdomain call,
1067 since PHP installations without gettext compiled into them will not
1068 function properly if you do not.
1069
1070 3. Finally, you just need to create your own locale. You should create
1071 a directory structure like this in the plugin directory:
1072
1073 demo
1074 |
1075 ------locale
1076 |
1077 ------de_DE
1078 | |
1079 | ------LC_MESSAGES
1080 |
1081 ------ja_JP
1082 |
1083 ------LC_MESSAGES
1084
1085 Create a directories such as de_DE for each language (de_DE is German,
1086 ja_JP is Japanese, etc. - check the SquirrelMail locale directory for
1087 a fairly comprehensive listing). Inside of each LC_MESSAGES directory
1088 you should place two files, one with your translations in it, called
1089 <plugin name>.po (in this case, "demo.po"), and one that is a compiled
1090 version of the ".po" file, called <plugin name>.mo (in this case,
1091 "demo.mo"). On most linux systems, there is a tool you can use to pull
1092 out most of the strings that you need to have translated from your PHP
1093 files into a sample .po file:
1094
1095 xgettext --keyword=_ -d <plugin name> -s -C *.php
1096
1097 --keyword option tells xgettext what your strings are enclosed in
1098 -d is the domain of your plugin which should be the plugin's name
1099 -s tells xgettext to sort the results and remove duplicate strings
1100 -C means you are translating a file with C/C++ type syntax (ie. PHP)
1101 *.php is all the files you want translations for
1102
1103 Note, however, that this will not always pick up all strings, so you
1104 should double-check manually. Of course, it's easiest if you just keep
1105 track of all your strings as you are coding your plugin. Your .po file
1106 will now look something like:
1107
1108 # SOME DESCRIPTIVE TITLE.
1109 # Copyright (C) YEAR Free Software Foundation, Inc.
1110 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
1111 #
1112 #, fuzzy
1113 msgid ""
1114 msgstr ""
1115 "Project-Id-Version: PACKAGE VERSION\n"
1116 "POT-Creation-Date: 2003-06-18 11:22-0600\n"
1117 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1118 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1119 "Language-Team: LANGUAGE <LL@li.org>\n"
1120 "MIME-Version: 1.0\n"
1121 "Content-Type: text/plain; charset=CHARSET\n"
1122 "Content-Transfer-Encoding: ENCODING\n"
1123
1124 #: functions.php:45
1125 msgid "Hello"
1126 msgstr ""
1127
1128 #: functions.php:87
1129 msgid "Favorite Color"
1130 msgstr ""
1131
1132 You should change the header to look something more like:
1133
1134 # Copyright (c) 1999-2003 The Squirrelmail Development Team
1135 # Roland Bauerschmidt <rb@debian.org>, 1999.
df788686 1136 # $Id$
9cd2ae7d 1137 msgid ""
1138 msgstr ""
df788686 1139 "Project-Id-Version: plugin-name version\n"
9cd2ae7d 1140 "POT-Creation-Date: 2003-01-21 19:21+0100\n"
1141 "PO-Revision-Date: 2003-01-21 21:01+0100\n"
1142 "Last-Translator: Juergen Edner <juergen.edner@epost.de>\n"
1143 "Language-Team: German <squirrelmail-i18n@lists.squirrelmail.net>\n"
1144 "MIME-Version: 1.0\n"
1145 "Content-Type: text/plain; charset=ISO-8859-1\n"
1146 "Content-Transfer-Encoding: 8bit\n"
1147
1148 The most important thing to change here is the charset on the next to
1149 last line. You'll want to keep a master copy of the .po file and make
1150 a copy for each language you have a translation for. You'll need to
1151 translate each string in the .po file:
1152
1153 msgid "Hello"
1154 msgstr "Guten Tag"
1155
1156 After you're done translating, you can create the .mo file very simply
1157 by running the following command (available on most linux systems):
1158
d2b351d7 1159 msgfmt -o <plugin name>.mo <plugin name>.po
9cd2ae7d 1160
1161 In the case of the "demo" plugin:
1162
d2b351d7 1163 msgfmt -o demo.mo demo.po
9cd2ae7d 1164
1165 Please be sure that the .po and .mo files both are named exactly the
1166 same as the domain you bound in step 2 above and everything else works
1167 automatically. In SquirrelMail, go to Options -> Display Preferences
1168 and change your Language setting to see the translations in action!
1169
1170
a7532db9 1171
1172Documenting the Code (Optional)
1173-------------------------------
1174
1175If you wish, you can use phpdoc (Javadoc-style) comments, when documenting your
1176code.
1177
1178If you follow the standards that are followed between Squirrelmail core &
1179plugin developers, the resulted documentation can be included with the rest of
1180the Squirrelmail code & API documentation. Specifically, in the page-level
1181docblock, declare the package to be 'plugins', and the subpackage to be the
1182name of your plugin. For instance:
1183
1184/**
1185 * demo.php
1186 *
1187 * Copyright (c) 2003 My Name <my-email-address>
1188 * Licensed under the GNU GPL. For full terms see the file COPYING.
1189 *
1190 * @package plugins
1191 * @subpackage demo
1192 */
1193
1194The rest is up to you. Try to follow some common sense and document what is
1195really needed. Documenting the code properly can be a big help not only to
1196yourself, but to those who will take a look at your code, fix the bugs and even
1197improve it, in the true open-source spirit that Squirrelmail was built upon.
1198
1199For more information about phpdocumentor and how to write proper-tagged
1200comments, you are directed at:
1201
1202http://phpdocu.sourceforge.net/
1203
1204
1205
9cd2ae7d 1206PLUGIN STANDARDS AND REQUIREMENTS
1207=================================
1208
1209The SquirrelMail project has some important goals, such as avoiding the
1210use of JavaScript, avoiding non-standard HTML tags, keeping file sizes
1211small and providing the fastest webmail client on the Internet. As such,
1212we'd like it if plugin authors coded with the same goals in mind that the
1213core developers do. Common sense is always a good tool to have in your
1214programming repertoire, but below is an outline of some standards that we
1215ask you as a plugin developer to meet. Depending upon how far you bend
1216these rules, we may not want to post your plugin on the SquirrelMail
1217website... and of course, no one really wants your efforts to go to waste
1218and for the SquirrelMail community to miss out on a potentially useful
1219plugin, so please try to follow these guidelines as closely as possible.
1220
1221
1222Small setup.php
1223---------------
1224
1225In order for SquirrelMail to remain fast and lean, we are now asking
1226that all plugin authors remove all unnecessary functionality from setup.php
d2b351d7 1227and refactor it into another file. There are a few ways to accomplish
9cd2ae7d 1228this, none of which are difficult. At a minimum, you'll want to have the
1229squirrelmail_plugin_init_<plugin name>() function in setup.php, and naturally,
1230you'll need functions that are merely stubs for each hook that you are using.
1231One (but not the only) way to do it is:
1232
1233 function squirrelmail_plugin_init_demo()
1234 {
1235 global $squirrelmail_plugin_hooks;
1236 $squirrelmail_plugin_hooks['generic_header']['demo'] = 'plugin_demo_header';
1237 }
1238 function plugin_demo_header()
1239 {
1240 include_once(SM_PATH . 'plugins/demo/functions.php');
1241 plugin_demo_header_do();
1242 }
1243
1244
1245Internationalization
1246--------------------
1247
1248Q: What is more disappointing to users in France who would make good
1249 use of your plugin than learning that it is written entirely in English?
1250A: Learning that they cannot send you a French translation file for your
1251 plugin.
1252
1253There are thousands of users out there whose native tongue is not English,
1254and when you develop your plugin without going through the three simple steps
1255needed to internationalize it, you are effectively writing them all off.
1256PLEASE consider internationalizing your plugin!
1257
1258
1259Developing with E_ALL
1260---------------------
1261
1262When you are developing your plugin, you should always have error reporting
1263turned all the way up. You can do this by changing two settings in your
1264php.ini and restarting your web server:
1265
799c2046 1266 display_errors = On
9cd2ae7d 1267 error_reporting = E_ALL
1268
1269This way, you'll be sure to see all Notices, Warnings and Errors that your
1270code generates (it's OK, really, it happens to the best of us... except me!).
1271Please make sure to fix them all before you release the plugin.
1272
1273
1b6b1526 1274Compatibility with register_globals=Off
1275---------------------------------------
1276
1277Most sensible systems administrators now run their PHP systems with the
1278setting "register_globals" as OFF. This is a prudent security setting,
1279and as the SquirrelMail core code has long since been upgraded to work
1280in such an environment, we are now requiring that all plugins do the same.
1281Compatibility with this setting amounts to little more than explicitly
1282gathering any and all variables you sent from a <form> tag as GET or POST
1283values instead of just assuming that they will be placed in the global
1284scope automatically. There is nothing more to do than this:
1285
1286 global $favorite_color;
1287 sqgetGlobalVar('favorite_color', $favorite_color, SQ_FORM);
1288
1289
9cd2ae7d 1290Extra Blank Lines
1291-----------------
1292
1293It may seem innocuous, but if you have any blank lines either before the
1294first <?php tag or after the last ?> tag in any of your plugin files, you
1295you will break SquirrelMail in ways that may seem entirely unrelated. For
1296instance, this will often cause a line feed character to be included with
1297email attachments when they are viewed or downloaded, rendering them useless!
1298
1299
1300include_once
1301------------
1302
1303When including files, please make sure to use the include_once() function
1304and NOT include(), require(), or require_once(), since these all are much
1305less efficient than include_once() and can have a cumulative effect on
1306SquirrelMail performance.
1307
1308
1309Version Reporting
1310-----------------
1311
1312In order for systems administrators to keep better track of your plugin and
1313get upgrades more efficiently, you are requested to make version information
1314available to SquirrelMail in a format that it understands. There are two
1315ways to do this. Presently, we are asking that you do both, since we are
1316still in a transition period between the two. This is painless, so please
1317be sure to include it:
1318
1319 1. Create a file called "version" in the plugin directory. That file
1320 should have only two lines: the first line should have the name of
1321 the plugin as named on the SquirrelMail web site (this is often a
1322 prettified version of the plugin directory name), the second line
1323 must have the version and nothing more. So for our "demo" plugin,
1324 whose name on the web site might be something like "Demo Favorite
1325 Colors", the file plugins/demo/version should have these two lines:
1326
1327 Demo Favorite Colors
1328 1.0
1329
1330 2. In setup.php, you should have a function called <plugin name>_version().
1331 That function should return the version of your plugin. For the "demo"
1332 plugin, that should look like this:
1333
1334 function demo_version()
1335 {
1336 return '1.0';
1337 }
1338
1339
1340Configuration Files
1341-------------------
1342
1343It is common to need a configuration file that holds some variables that
1344are set up at install time. For ease of installation and maintenance, you
1345should place all behavioral settings in a config file, isolated from the
1346rest of your plugin code. A typical file name to use is "config.php". If
1347you are using such a file, you should NOT include a file called "config.php"
1348in your plugin distribution, but instead a copy of that file called
1349"config.php.sample". This helps systems administrators avoid overwriting
1350the "config.php" files and losing all of their setup information when they
1351upgrade your plugin.
1352
1353
1354Session Variables
1355-----------------
1356
1357In the past, there have been some rather serious issues with PHP sessions
1358and SquirrelMail, and certain people have worked long and hard to ensure
1359that these problems no longer occur in an extremely wide variety of OS/PHP/
1360web server environments. Thus, if you need to place any values into the
1361user's session, there are some built-in SquirrelMail functions that you are
1362strongly encouraged to make use of. Using them also makes your job easier.
1363
1364 1. To place a variable into the session:
1365
1366 global $favorite_color;
1367 $favoriteColor = 'green';
1368 sqsession_register($favorite_color, 'favorite_color');
1369
1370 Strictly speaking, globalizing the variable shouldn't be necessary,
1371 but certain versions of PHP seem to behave more predictably if you do.
1372
1373 2. To retrieve a variable from the session:
1374
1375 global $favorite_color;
1376 sqgetGlobalVar('favorite_color', $favorite_color, SQ_SESSION);
1377
1378 3. You can also check for the presence of a variable in the session:
1379
1380 if (sqsession_is_registered('favorite_color'))
1381 // do something important
1382
1383 4. To remove a variable from the session:
1384
ea26c996 1385 global $favorite_color;
9cd2ae7d 1386 sqsession_unregister('favorite_color');
1387
ea26c996 1388 Strictly speaking, globalizing the variable shouldn't be necessary,
1389 but certain versions of PHP seem to behave more predictably if you do.
1390
9cd2ae7d 1391
1392Form Variables
1393--------------
1394
1395You are also encouraged to use SquirrelMail's built-in facilities to
1396retrieve variables from POST and GET submissions. This is also much
1397easier on you and makes sure that all PHP installations are accounted
1398for (such as those that don't make the $_POST array automatically
1399global, etc.):
1400
1401 global $favorite_color;
1402 sqgetGlobalVar('favorite_color', $favorite_color, SQ_FORM);
1403
1404
1405Files In Plugin Directory
1406-------------------------
1407
1408There are a few files that you should make sure to include when you build
1409your final plugin distribution:
1410
1411 1. A copy of the file index.php from the main plugins directory. When
1412 working in your plugin directory, just copy it in like this:
1413
1414 $ cp ../index.php .
1415
1416 This will redirect anyone who tries to browse to your plugin directory
1417 to somewhere more appropriate. If you create other directories under
1418 your plugin directory, you may copy the file there as well to be extra
1419 safe. If you are storing sensitive configuration files or other data
1420 in such a directory, you could even include a .htaccess file with the
1421 contents "Deny From All" that will disallow access to that directory
1422 entirely (when the target system is running the Apache web server).
1423 Keep in mind that not all web servers will honor an .htaccess file, so
1424 don't depend on it for security. Make sure not to put such a file in
1425 your main plugin directory!
1426
1427 2. A file that describes your plugin and offers detailed instructions for
1428 configuration or help with troubleshooting, etc. This file is usually
1429 entitled "README". Some useful sections to include might be:
1430
1431 Plugin Name and Author
1432 Current Version
1433 Plugin Features
1434 Detailed Plugin Description
1435 How-to for Plugin Configuration
1436 Change Log
1437 Future Ideas/Enhancements/To Do List
1438
1439 3. A file that explains how to install your plugin. This file is typically
1440 called "INSTALL". If you do not require any special installation
1441 actions, you can probably copy one from another plugin or use this as
1442 a template:
1443
1444 Installing the Demo Plugin
1445 ==========================
1446
1447 1) Start with untaring the file into the plugins directory.
1448 Here is a example for the 1.0 version of the Demo plugin.
1449
1450 $ cd plugins
1451 $ tar -zxvf demo-1.0-1.4.0.tar.gz
1452
1453 2) Change into the demo directory, copy config.php.sample
1454 to config.php and edit config.php, making adjustments as
1455 you deem necessary. For more detailed explanations about
1456 each of these parameters, consult the README file.
1457
1458 $ cd demo
1459 $ cp config.php.sample config.php
1460 $ vi config.php
1461
1462
1463 3) Then go to your config directory and run conf.pl. Choose
1464 option 8 and move the plugin from the "Available Plugins"
1465 category to the "Installed Plugins" category. Save and exit.
1466
1467 $ cd ../../config/
1468 $ ./conf.pl
1469
1470
1471 Upgrading the Demo Plugin
1472 =========================
1473
1474 1) Start with untaring the file into the plugins directory.
1475 Here is a example for the 3.1 version of the demo plugin.
1476
1477 $ cd plugins
1478 $ tar -zxvf demo-3.1-1.4.0.tar.gz
1479
1480
1481 2) Change into the demo directory, check your config.php
1482 file against the new version, to see if there are any new
1483 settings that you must add to your config.php file.
1484
1485 $ diff -Nau config.php config.php.sample
1486
1487 Or simply replace your config.php file with the provided sample
1488 and reconfigure the plugin from scratch (see step 2 under the
1489 installation procedure above).
1490
1491
1492COMPATIBILITY WITH OLDER VERSIONS OF SQUIRRELMAIL
1493=================================================
1494
1495Whenever new versions of SquirrelMail are released, there is always a
1496considerable lag time before it is widely adopted. During that transitional
1497time, especially when the new SquirrelMail version contains any architectural
1498and/or functional changes, plugin developers are put in a unique and very
1499difficult position. That is, there will be people running both the old and
1500new versions of SquirrelMail who want to use your plugin, and you will
1501probably want to accomodate them both.
1502
1503The easiest way to keep both sides happy is to keep two different versions
1504of your pluign up to date, one that runs under the older SquirrelMail, and
1505one that requires the newest SquirrelMail. This is inconvenient, however,
1506especially if you are continuing to develop the plugin. Depending on the
1507changes the SquirrelMail has implemented in the new version, you may be able
1508to include code that can auto-sense SquirrelMail version and make adjustments
1509on the fly. There is a function available to you for determining the
1510SquirrelMail version called check_sm_version() and it can be used as such:
1511
1512 check_sm_version(1, 4, 0)
1513
1514This will return TRUE if the SquirrelMail being used is at least 1.4.0, and
1515FALSE otherwise.
1516
1517As this document is written, we are in a transition period between versions
15181.2.11 and 1.4.0. There is a plugin called "Compatibilty" that is intended
1519for use by plugin authors so they can develop one version of their plugin
1520and seamlessly support both 1.2.x and 1.4.x SquirrelMail installations. For
1521more information about how to use the "Compatibility" plugin, download it and
1522read its README file or see:
1523
1524 http://www.squirrelmail.org/wiki/wiki.php?PluginUpgrading
1525
1526
1527REQUESTING NEW HOOKS
1528====================
1529
1530It's impossible to foresee all of the places where hooks might be useful
1531(it's also impossible to put in hooks everywhere!), so you might need to
1532negotiate the insertion of a new hook to make your plugin work. In order
1533to do so, you should post such a request to the squirrelmail-devel mailing
1534list.
1535
1536
1537HOW TO RELEASE YOUR PLUGIN
1538==========================
1539
1540As long as you've consulted the list of plugin standards and done your
1541best to follow them, there's little standing in the way of great fame as an
1542official SquirrelMail plugin developer.
1543
1544 1. Make a distribution file. There is a convenient Perl script in
1545 the plugins directory that will help you do this:
1546
1547 make_archive.pl -v demo 1.0 1.4.0
1548
1549 -v is optional and indicates that the script should run in verbose mode
1550 demo is the name of your plugin
1551 1.0 is the version of your plugin
1552 1.4.0 is the version of SquirrelMail that is required to run your plugin
1553
1554 You can also create the distribution file manually in most *nix
1555 environments by running this command from the plugins directory (NOT
1556 your plugin directory):
1557
1558 $ tar czvf demo-1.0-1.4.0.tar.gz demo
1559
1560 Where "demo" is the name of your plugin, "1.0" is the version of
1561 your plugin, and "1.4.0" is the version of SquirrelMail required
1562 to use your plugin.
1563
1564 2. Consult the SquirrelMail web site for contact information for the
1565 Plugins Team Leaders, to whom you should make your request. If they
1566 do not respond, you should feel free to ask for help contacting them
1567 on the squirrelmail-plugins mailing list.
1568
1569 http://www.squirrelmail.org/wiki/wiki.php?SquirrelMailLeadership
1570