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