6 * Functions needed to display the options pages.
8 * @copyright © 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
15 /**********************************************/
16 /* Define constants used in the options code. */
17 /**********************************************/
19 /* Define constants for the various option types. */
20 define('SMOPT_TYPE_STRING', 0);
21 define('SMOPT_TYPE_STRLIST', 1);
22 define('SMOPT_TYPE_TEXTAREA', 2);
23 define('SMOPT_TYPE_INTEGER', 3);
24 define('SMOPT_TYPE_FLOAT', 4);
25 define('SMOPT_TYPE_BOOLEAN', 5);
26 define('SMOPT_TYPE_HIDDEN', 6);
27 define('SMOPT_TYPE_COMMENT', 7);
28 define('SMOPT_TYPE_FLDRLIST', 8);
29 define('SMOPT_TYPE_FLDRLIST_MULTI', 9);
31 /* Define constants for the options refresh levels. */
32 define('SMOPT_REFRESH_NONE', 0);
33 define('SMOPT_REFRESH_FOLDERLIST', 1);
34 define('SMOPT_REFRESH_ALL', 2);
36 /* Define constants for the options size. */
37 define('SMOPT_SIZE_TINY', 0);
38 define('SMOPT_SIZE_SMALL', 1);
39 define('SMOPT_SIZE_MEDIUM', 2);
40 define('SMOPT_SIZE_LARGE', 3);
41 define('SMOPT_SIZE_HUGE', 4);
42 define('SMOPT_SIZE_NORMAL', 5);
44 define('SMOPT_SAVE_DEFAULT', 'save_option');
45 define('SMOPT_SAVE_NOOP', 'save_option_noop');
48 * SquirrelOption: An option for SquirrelMail.
50 * @package squirrelmail
53 class SquirrelOption
{
55 * The name of this setting
60 * The text that prefaces setting on the preferences page
65 * The type of INPUT element
67 * See SMOPT_TYPE_* defines
72 * Indicates if a link should be shown to refresh part
73 * or all of the window
75 * See SMOPT_REFRESH_* defines
80 * Specifies the size of certain input items
82 * See SMOPT_SIZE_* defines
87 * Text that follows a text input or
88 * select list input on the preferences page
90 * useful for indicating units, meanings of special values, etc.
95 * text displayed to the user
97 * Used with SMOPT_TYPE_COMMENT options
102 * additional javascript or other widget attributes added to the
103 * user input; must be an array where keys are attribute names
104 * ("onclick", etc) and values are the attribute values.
109 * script (usually Javascript) that will be placed after (outside of)
116 * The name of the Save Function for this option.
121 /* The various 'values' for this options. */
123 * default/preselected value for this option
133 * associative array, where each key is an actual input value
134 * and the corresponding value is what is displayed to the user
135 * for that list item in the drop-down list
138 var $possible_values;
140 * disables html sanitizing.
142 * WARNING - don't use it, if user input is possible in option
143 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
146 var $htmlencoded=false;
148 * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
149 * SMOPT_TYPE_FLDRLIST_MULTI widgets.
150 * See $flag argument in sqimap_mailbox_option_list() function.
154 var $folder_filter='noselect';
157 * Constructor function
158 * @param string $name
159 * @param string $caption
160 * @param integer $type
161 * @param integer $refresh_level
162 * @param mixed $initial_value
163 * @param array $possible_values
164 * @param bool $htmlencoded
166 function SquirrelOption
167 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
168 /* Set the basic stuff. */
170 $this->caption
= $caption;
172 $this->refresh_level
= $refresh_level;
173 $this->possible_values
= $possible_values;
174 $this->htmlencoded
= $htmlencoded;
175 $this->size
= SMOPT_SIZE_MEDIUM
;
176 $this->trailing_text
= '';
178 $this->aExtraAttribs
= array();
179 $this->post_script
= '';
181 //Check for a current value.
182 if (isset($GLOBALS[$name])) {
183 $this->value
= $GLOBALS[$name];
184 } else if (!empty($initial_value)) {
185 $this->value
= $initial_value;
190 /* Check for a new value. */
191 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
192 $this->new_value
= '';
195 /* Set the default save function. */
196 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
197 $this->save_function
= SMOPT_SAVE_DEFAULT
;
199 $this->save_function
= SMOPT_SAVE_NOOP
;
204 * Set the value for this option.
205 * @param mixed $value
207 function setValue($value) {
208 $this->value
= $value;
212 * Set the new value for this option.
213 * @param mixed $new_value
215 function setNewValue($new_value) {
216 $this->new_value
= $new_value;
220 * Set the size for this option.
221 * @param integer $size
223 function setSize($size) {
228 * Set the trailing_text for this option.
229 * @param string $trailing_text
231 function setTrailingText($trailing_text) {
232 $this->trailing_text
= $trailing_text;
236 * Set the comment for this option.
237 * @param string $comment
239 function setComment($comment) {
240 $this->comment
= $comment;
244 * Set the extra attributes for this option.
245 * @param array $aExtraAttribs
247 function setExtraAttributes($aExtraAttribs) {
248 $this->aExtraAttribs
= $aExtraAttribs;
252 * Set the "post script" for this option.
253 * @param string $post_script
255 function setPostScript($post_script) {
256 $this->post_script
= $post_script;
260 * Set the save function for this option.
261 * @param string $save_function
263 function setSaveFunction($save_function) {
264 $this->save_function
= $save_function;
268 * Set the trailing_text for this option.
269 * @param string $folder_filter
272 function setFolderFilter($folder_filter) {
273 $this->folder_filter
= $folder_filter;
277 * Creates fields on option pages according to option type
279 * This is the function that calls all other createWidget* functions.
281 * @return string The formated option field
284 function createWidget() {
287 // Use new value if available
288 if (!empty($this->new_value
)) {
289 $tempValue = $this->value
;
290 $this->value
= $this->new_value
;
293 /* Get the widget for this option type. */
294 switch ($this->type
) {
295 case SMOPT_TYPE_STRING
:
296 $result = $this->createWidget_String();
298 case SMOPT_TYPE_STRLIST
:
299 $result = $this->createWidget_StrList();
301 case SMOPT_TYPE_TEXTAREA
:
302 $result = $this->createWidget_TextArea();
304 case SMOPT_TYPE_INTEGER
:
305 $result = $this->createWidget_Integer();
307 case SMOPT_TYPE_FLOAT
:
308 $result = $this->createWidget_Float();
310 case SMOPT_TYPE_BOOLEAN
:
311 $result = $this->createWidget_Boolean();
313 case SMOPT_TYPE_HIDDEN
:
314 $result = $this->createWidget_Hidden();
316 case SMOPT_TYPE_COMMENT
:
317 $result = $this->createWidget_Comment();
319 case SMOPT_TYPE_FLDRLIST
:
320 $result = $this->createWidget_FolderList();
322 case SMOPT_TYPE_FLDRLIST_MULTI
:
323 $result = $this->createWidget_FolderList(TRUE);
327 sprintf(_("Option Type '%s' Not Found"), $this->type
)
331 /* Add the "post script" for this option. */
332 $result .= $this->post_script
;
334 // put correct value back if need be
335 if (!empty($this->new_value
)) {
336 $this->value
= $tempValue;
339 /* Now, return the created widget. */
344 * Create string field
345 * @return string html formated option field
347 function createWidget_String() {
348 switch ($this->size
) {
349 case SMOPT_SIZE_TINY
:
352 case SMOPT_SIZE_SMALL
:
355 case SMOPT_SIZE_LARGE
:
358 case SMOPT_SIZE_HUGE
:
361 case SMOPT_SIZE_NORMAL
:
366 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
370 * Create selection box
372 * When $this->htmlencoded is TRUE, the keys and values in
373 * $this->possible_values are assumed to be display-safe.
376 * @return string html formated selection box
378 function createWidget_StrList() {
379 //FIXME: Currently, $this->htmlencoded is ignored here -- was removed when changing to template-based output; a fix is available as part of proposed centralized sanitizing patch
380 return addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
385 * Create folder selection box
387 * @param boolean $multiple_select When TRUE, the select widget
388 * will allow multiple selections
389 * (OPTIONAL; default is FALSE
390 * (single select list))
392 * @return string html formated selection box
395 function createWidget_FolderList($multiple_select=FALSE) {
397 // possible values might include a nested array of
398 // possible values (list of folders)
400 $option_list = array();
401 foreach ($this->possible_values
as $value => $text) {
403 // list of folders (boxes array)
405 if (is_array($text)) {
406 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter
));
408 // just one option here
411 $option_list = array_merge($option_list, array($value => $text));
415 if (empty($option_list))
416 $option_list = array('ignore' => _("unavailable"));
419 // OK to use sq_htmlspecialchars() below because addSelect() already does
420 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
, $multiple_select) . sq_htmlspecialchars($this->trailing_text
);
426 * @return string html formated textarea field
428 function createWidget_TextArea() {
429 switch ($this->size
) {
430 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
431 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
432 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
433 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
434 case SMOPT_SIZE_NORMAL
:
435 default: $rows = 5; $cols = 50;
437 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
441 * Creates field for integer
443 * Difference from createWidget_String is visible only when javascript is enabled
444 * @return string html formated option field
446 function createWidget_Integer() {
448 // add onChange javascript handler to a regular string widget
449 // which will strip out all non-numeric chars
450 if (checkForJavascript())
451 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
452 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
453 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
454 . 'this.value=newVal;';
456 return $this->createWidget_String();
460 * Creates field for floating number
461 * Difference from createWidget_String is visible only when javascript is enabled
462 * @return string html formated option field
464 function createWidget_Float() {
466 // add onChange javascript handler to a regular string widget
467 // which will strip out all non-numeric (period also OK) chars
468 if (checkForJavascript())
469 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
470 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
471 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
472 . 'newVal += origVal.charAt(i); } this.value=newVal;';
474 return $this->createWidget_String();
478 * Creates radio field (yes/no)
479 * @return string html formated radio field
481 function createWidget_Boolean() {
483 global $oTemplate, $nbsp;
485 /* Build the yes choice. */
486 $yes_option = addRadioBox('new_' . $this->name
, ($this->value
!= SMPREF_NO
), SMPREF_YES
, array_merge(array('id' => 'new_' . $this->name
. '_yes'), $this->aExtraAttribs
)) . $nbsp . create_label(_("Yes"), 'new_' . $this->name
. '_yes');
488 /* Build the no choice. */
489 $no_option = addRadioBox('new_' . $this->name
, ($this->value
== SMPREF_NO
), SMPREF_NO
, array_merge(array('id' => 'new_' . $this->name
. '_no'), $this->aExtraAttribs
)) . $nbsp . create_label(_("No"), 'new_' . $this->name
. '_no');
491 /* Build and return the combined "boolean widget". */
492 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
497 * Creates hidden field
498 * @return string html formated hidden input field
500 function createWidget_Hidden() {
501 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
506 * @return string comment
508 function createWidget_Comment() {
509 $result = $this->comment
;
517 $function = $this->save_function
;
525 return ($this->value
!= $this->new_value
);
527 } /* End of SquirrelOption class*/
530 * Saves the option value (this is the default save function
531 * unless overridden by the user)
533 * @param object $option object that holds option name and new_value
535 function save_option($option) {
537 // Can't save the pref if we don't have the username
539 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
545 // Certain option types need to be serialized because
546 // they are not scalar
548 if ($option->type
== SMOPT_TYPE_FLDRLIST_MULTI
)
549 setPref($data_dir, $username, $option->name
, serialize($option->new_value
));
551 setPref($data_dir, $username, $option->name
, $option->new_value
);
556 * save function that does not save
557 * @param object $option
559 function save_option_noop($option) {
560 /* Do nothing here... */
564 * Create hidden 'optpage' input field with value set by argument
565 * @param string $optpage identification of option page
566 * @return string html formated hidden input field
568 function create_optpage_element($optpage) {
569 return addHidden('optpage', $optpage);
573 * Create hidden 'optmode' input field with value set by argument
574 * @param string $optmode
575 * @return string html formated hidden input field
577 function create_optmode_element($optmode) {
578 return addHidden('optmode', $optmode);
582 * @param array $optgrps
583 * @param array $optvals
586 function create_option_groups($optgrps, $optvals) {
587 /* Build a simple array with which to start. */
590 /* Create option group for each option group name. */
591 foreach ($optgrps as $grpkey => $grpname) {
592 $result[$grpkey] = array();
593 $result[$grpkey]['name'] = $grpname;
594 $result[$grpkey]['options'] = array();
597 /* Create a new SquirrelOption for each set of option values. */
598 foreach ($optvals as $grpkey => $grpopts) {
599 foreach ($grpopts as $optset) {
600 /* Create a new option with all values given. */
601 $next_option = new SquirrelOption(
605 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
606 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
607 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
608 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
611 /* If provided, set the size for this option. */
612 if (isset($optset['size'])) {
613 $next_option->setSize($optset['size']);
616 /* If provided, set the trailing_text for this option. */
617 if (isset($optset['trailing_text'])) {
618 $next_option->setTrailingText($optset['trailing_text']);
621 /* If provided, set the comment for this option. */
622 if (isset($optset['comment'])) {
623 $next_option->setComment($optset['comment']);
626 /* If provided, set the save function for this option. */
627 if (isset($optset['save'])) {
628 $next_option->setSaveFunction($optset['save']);
631 /* If provided, set the extra attributes for this option. */
632 if (isset($optset['extra_attributes'])) {
633 $next_option->setExtraAttributes($optset['extra_attributes']);
636 /* If provided, set the "post script" for this option. */
637 if (isset($optset['post_script'])) {
638 $next_option->setPostScript($optset['post_script']);
641 /* If provided, set the folder_filter for this option. */
642 if (isset($optset['folder_filter'])) {
643 $next_option->setFolderFilter($optset['folder_filter']);
646 /* Add this option to the option array. */
647 $result[$grpkey]['options'][] = $next_option;
651 /* Return our resulting array. */