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);
30 /* Define constants for the options refresh levels. */
31 define('SMOPT_REFRESH_NONE', 0);
32 define('SMOPT_REFRESH_FOLDERLIST', 1);
33 define('SMOPT_REFRESH_ALL', 2);
35 /* Define constants for the options size. */
36 define('SMOPT_SIZE_TINY', 0);
37 define('SMOPT_SIZE_SMALL', 1);
38 define('SMOPT_SIZE_MEDIUM', 2);
39 define('SMOPT_SIZE_LARGE', 3);
40 define('SMOPT_SIZE_HUGE', 4);
41 define('SMOPT_SIZE_NORMAL', 5);
43 define('SMOPT_SAVE_DEFAULT', 'save_option');
44 define('SMOPT_SAVE_NOOP', 'save_option_noop');
47 * SquirrelOption: An option for SquirrelMail.
49 * @package squirrelmail
52 class SquirrelOption
{
54 * The name of this setting
59 * The text that prefaces setting on the preferences page
64 * The type of INPUT element
66 * See SMOPT_TYPE_* defines
71 * Indicates if a link should be shown to refresh part
72 * or all of the window
74 * See SMOPT_REFRESH_* defines
79 * Specifies the size of certain input items
81 * See SMOPT_SIZE_* defines
86 * Text that follows a text input or
87 * select list input on the preferences page
89 * useful for indicating units, meanings of special values, etc.
94 * text displayed to the user
96 * Used with SMOPT_TYPE_COMMENT options
101 * additional javascript or other widget attributes added to the
102 * user input; must be an array where keys are attribute names
103 * ("onclick", etc) and values are the attribute values.
108 * script (usually Javascript) that will be placed after (outside of)
115 * The name of the Save Function for this option.
120 /* The various 'values' for this options. */
122 * default/preselected value for this option
132 * associative array, where each key is an actual input value
133 * and the corresponding value is what is displayed to the user
134 * for that list item in the drop-down list
137 var $possible_values;
139 * disables html sanitizing.
141 * WARNING - don't use it, if user input is possible in option
142 * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
145 var $htmlencoded=false;
147 * Controls folder list limits in SMOPT_TYPE_FLDRLIST widget.
148 * See $flag argument in sqimap_mailbox_option_list() function.
152 var $folder_filter='noselect';
155 * Constructor function
156 * @param string $name
157 * @param string $caption
158 * @param integer $type
159 * @param integer $refresh_level
160 * @param mixed $initial_value
161 * @param array $possible_values
162 * @param bool $htmlencoded
164 function SquirrelOption
165 ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
166 /* Set the basic stuff. */
168 $this->caption
= $caption;
170 $this->refresh_level
= $refresh_level;
171 $this->possible_values
= $possible_values;
172 $this->htmlencoded
= $htmlencoded;
173 $this->size
= SMOPT_SIZE_MEDIUM
;
174 $this->trailing_text
= '';
176 $this->aExtraAttribs
= array();
177 $this->post_script
= '';
179 //Check for a current value.
180 if (isset($GLOBALS[$name])) {
181 $this->value
= $GLOBALS[$name];
182 } else if (!empty($initial_value)) {
183 $this->value
= $initial_value;
188 /* Check for a new value. */
189 if ( !sqgetGlobalVar("new_$name", $this->new_value
, SQ_POST
) ) {
190 $this->new_value
= '';
193 /* Set the default save function. */
194 if (($type != SMOPT_TYPE_HIDDEN
) && ($type != SMOPT_TYPE_COMMENT
)) {
195 $this->save_function
= SMOPT_SAVE_DEFAULT
;
197 $this->save_function
= SMOPT_SAVE_NOOP
;
202 * Set the value for this option.
203 * @param mixed $value
205 function setValue($value) {
206 $this->value
= $value;
210 * Set the new value for this option.
211 * @param mixed $new_value
213 function setNewValue($new_value) {
214 $this->new_value
= $new_value;
218 * Set the size for this option.
219 * @param integer $size
221 function setSize($size) {
226 * Set the trailing_text for this option.
227 * @param string $trailing_text
229 function setTrailingText($trailing_text) {
230 $this->trailing_text
= $trailing_text;
234 * Set the comment for this option.
235 * @param string $comment
237 function setComment($comment) {
238 $this->comment
= $comment;
242 * Set the extra attributes for this option.
243 * @param array $aExtraAttribs
245 function setExtraAttributes($aExtraAttribs) {
246 $this->aExtraAttribs
= $aExtraAttribs;
250 * Set the "post script" for this option.
251 * @param string $post_script
253 function setPostScript($post_script) {
254 $this->post_script
= $post_script;
258 * Set the save function for this option.
259 * @param string $save_function
261 function setSaveFunction($save_function) {
262 $this->save_function
= $save_function;
266 * Set the trailing_text for this option.
267 * @param string $folder_filter
270 function setFolderFilter($folder_filter) {
271 $this->folder_filter
= $folder_filter;
275 * Creates fields on option pages according to option type
277 * Function that calls other createWidget* functions.
278 * @return string html formated option field
280 function createHTMLWidget() {
283 // Use new value if available
284 if (!empty($this->new_value
)) {
285 $tempValue = $this->value
;
286 $this->value
= $this->new_value
;
289 /* Get the widget for this option type. */
290 switch ($this->type
) {
291 case SMOPT_TYPE_STRING
:
292 $result = $this->createWidget_String();
294 case SMOPT_TYPE_STRLIST
:
295 $result = $this->createWidget_StrList();
297 case SMOPT_TYPE_TEXTAREA
:
298 $result = $this->createWidget_TextArea();
300 case SMOPT_TYPE_INTEGER
:
301 $result = $this->createWidget_Integer();
303 case SMOPT_TYPE_FLOAT
:
304 $result = $this->createWidget_Float();
306 case SMOPT_TYPE_BOOLEAN
:
307 $result = $this->createWidget_Boolean();
309 case SMOPT_TYPE_HIDDEN
:
310 $result = $this->createWidget_Hidden();
312 case SMOPT_TYPE_COMMENT
:
313 $result = $this->createWidget_Comment();
315 case SMOPT_TYPE_FLDRLIST
:
316 $result = $this->createWidget_FolderList();
320 sprintf(_("Option Type '%s' Not Found"), $this->type
)
324 /* Add the "post script" for this option. */
325 $result .= $this->post_script
;
327 // put correct value back if need be
328 if (!empty($this->new_value
)) {
329 $this->value
= $tempValue;
332 /* Now, return the created widget. */
337 * Create string field
338 * @return string html formated option field
340 function createWidget_String() {
341 switch ($this->size
) {
342 case SMOPT_SIZE_TINY
:
345 case SMOPT_SIZE_SMALL
:
348 case SMOPT_SIZE_LARGE
:
351 case SMOPT_SIZE_HUGE
:
354 case SMOPT_SIZE_NORMAL
:
359 return addInput('new_' . $this->name
, $this->value
, $width, 0, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
363 * Create selection box
365 * When $this->htmlencoded is TRUE, the keys and values in
366 * $this->possible_values are assumed to be display-safe.
369 * @return string html formated selection box
371 function createWidget_StrList() {
372 //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
373 return addSelect('new_' . $this->name
, $this->possible_values
, $this->value
, TRUE, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
378 * Create folder selection box
379 * @return string html formated selection box
381 function createWidget_FolderList() {
383 // possible values might include a nested array of
384 // possible values (list of folders)
386 $option_list = array();
387 foreach ($this->possible_values
as $value => $text) {
389 // list of folders (boxes array)
391 if (is_array($text)) {
392 $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, array(strtolower($this->value
)), 0, $text, $this->folder_filter
));
394 // just one option here
397 $option_list = array_merge($option_list, array($value => $text));
401 if (empty($option_list))
402 $option_list = array('ignore' => _("unavailable"));
405 return addSelect('new_' . $this->name
, $option_list, $this->value
, TRUE, $this->aExtraAttribs
) . htmlspecialchars($this->trailing_text
);
411 * @return string html formated textarea field
413 function createWidget_TextArea() {
414 switch ($this->size
) {
415 case SMOPT_SIZE_TINY
: $rows = 3; $cols = 10; break;
416 case SMOPT_SIZE_SMALL
: $rows = 4; $cols = 30; break;
417 case SMOPT_SIZE_LARGE
: $rows = 10; $cols = 60; break;
418 case SMOPT_SIZE_HUGE
: $rows = 20; $cols = 80; break;
419 case SMOPT_SIZE_NORMAL
:
420 default: $rows = 5; $cols = 50;
422 return addTextArea('new_' . $this->name
, $this->value
, $cols, $rows, $this->aExtraAttribs
);
426 * Creates field for integer
428 * Difference from createWidget_String is visible only when javascript is enabled
429 * @return string html formated option field
431 function createWidget_Integer() {
433 // add onChange javascript handler to a regular string widget
434 // which will strip out all non-numeric chars
435 if (checkForJavascript())
436 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
437 . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
438 . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
439 . 'this.value=newVal;';
441 return $this->createWidget_String();
445 * Creates field for floating number
446 * Difference from createWidget_String is visible only when javascript is enabled
447 * @return string html formated option field
449 function createWidget_Float() {
451 // add onChange javascript handler to a regular string widget
452 // which will strip out all non-numeric (period also OK) chars
453 if (checkForJavascript())
454 $this->aExtraAttribs
['onchange'] = 'origVal=this.value; newVal=\'\'; '
455 . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
456 . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
457 . 'newVal += origVal.charAt(i); } this.value=newVal;';
459 return $this->createWidget_String();
463 * Creates radio field (yes/no)
464 * @return string html formated radio field
466 function createWidget_Boolean() {
469 $nbsp = $oTemplate->fetch('non_breaking_space.tpl');
471 /* Build the yes choice. */
472 $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');
474 /* Build the no choice. */
475 $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');
477 /* Build and return the combined "boolean widget". */
478 $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
483 * Creates hidden field
484 * @return string html formated hidden input field
486 function createWidget_Hidden() {
487 return addHidden('new_' . $this->name
, $this->value
, $this->aExtraAttribs
);
492 * @return string comment
494 function createWidget_Comment() {
495 $result = $this->comment
;
503 $function = $this->save_function
;
511 return ($this->value
!= $this->new_value
);
513 } /* End of SquirrelOption class*/
517 * @param object $option object that holds option name and new_value
519 function save_option($option) {
520 if ( !sqgetGlobalVar('username', $username, SQ_SESSION
) ) {
521 /* Can't save the pref if we don't have the username */
525 setPref($data_dir, $username, $option->name
, $option->new_value
);
529 * save function that does not save
530 * @param object $option
532 function save_option_noop($option) {
533 /* Do nothing here... */
537 * Create hidden 'optpage' input field with value set by argument
538 * @param string $optpage identification of option page
539 * @return string html formated hidden input field
541 function create_optpage_element($optpage) {
542 return addHidden('optpage', $optpage);
546 * Create hidden 'optmode' input field with value set by argument
547 * @param string $optmode
548 * @return string html formated hidden input field
550 function create_optmode_element($optmode) {
551 return addHidden('optmode', $optmode);
555 * @param array $optgrps
556 * @param array $optvals
559 function create_option_groups($optgrps, $optvals) {
560 /* Build a simple array with which to start. */
563 /* Create option group for each option group name. */
564 foreach ($optgrps as $grpkey => $grpname) {
565 $result[$grpkey] = array();
566 $result[$grpkey]['name'] = $grpname;
567 $result[$grpkey]['options'] = array();
570 /* Create a new SquirrelOption for each set of option values. */
571 foreach ($optvals as $grpkey => $grpopts) {
572 foreach ($grpopts as $optset) {
573 /* Create a new option with all values given. */
574 $next_option = new SquirrelOption(
578 (isset($optset['refresh']) ?
$optset['refresh'] : SMOPT_REFRESH_NONE
),
579 (isset($optset['initial_value']) ?
$optset['initial_value'] : ''),
580 (isset($optset['posvals']) ?
$optset['posvals'] : ''),
581 (isset($optset['htmlencoded']) ?
$optset['htmlencoded'] : false)
584 /* If provided, set the size for this option. */
585 if (isset($optset['size'])) {
586 $next_option->setSize($optset['size']);
589 /* If provided, set the trailing_text for this option. */
590 if (isset($optset['trailing_text'])) {
591 $next_option->setTrailingText($optset['trailing_text']);
594 /* If provided, set the comment for this option. */
595 if (isset($optset['comment'])) {
596 $next_option->setComment($optset['comment']);
599 /* If provided, set the save function for this option. */
600 if (isset($optset['save'])) {
601 $next_option->setSaveFunction($optset['save']);
604 /* If provided, set the extra attributes for this option. */
605 if (isset($optset['extra_attributes'])) {
606 $next_option->setExtraAttributes($optset['extra_attributes']);
609 /* If provided, set the "post script" for this option. */
610 if (isset($optset['post_script'])) {
611 $next_option->setPostScript($optset['post_script']);
614 /* If provided, set the folder_filter for this option. */
615 if (isset($optset['folder_filter'])) {
616 $next_option->setFolderFilter($optset['folder_filter']);
619 /* Add this option to the option array. */
620 $result[$grpkey]['options'][] = $next_option;
624 /* Return our resulting array. */