Source for file options.php

Documentation is available at options.php

  1. <?php
  2.  
  3. /**
  4.  * options.php
  5.  *
  6.  * Functions needed to display the options pages.
  7.  *
  8.  * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: options.php,v 1.34.2.15 2006/04/14 22:27:08 jervfors Exp $
  11.  * @package squirrelmail
  12.  * @subpackage prefs
  13.  */
  14.  
  15. /**********************************************/
  16. /* Define constants used in the options code. */
  17. /**********************************************/
  18.  
  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.  
  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);
  34.  
  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);
  42.  
  43. define('SMOPT_SAVE_DEFAULT', 'save_option');
  44. define('SMOPT_SAVE_NOOP', 'save_option_noop');
  45.  
  46. /**
  47.  * SquirrelOption: An option for Squirrelmail.
  48.  *
  49.  * This class is a work in progress. When complete, it will handle
  50.  * presentation and saving of Squirrelmail user options in a simple,
  51.  * streamline manner. Stay tuned for more stuff.
  52.  *
  53.  * Also, I'd like to ask that people leave this alone (mostly :) until
  54.  * I get it a little further along. That should only be a day or two or
  55.  * three. I will remove this message when it is ready for primetime usage.
  56.  * @package squirrelmail
  57.  */
  58. class SquirrelOption {
  59.     /* The basic stuff. */
  60.     var $name;
  61.     var $caption;
  62.     var $type;
  63.     var $refresh_level;
  64.     var $size;
  65.     var $comment;
  66.     var $script;
  67.     var $post_script;
  68.  
  69.     /* The name of the Save Function for this option. */
  70.     var $save_function;
  71.  
  72.     /* The various 'values' for this options. */
  73.     var $value;
  74.     var $new_value;
  75.     var $possible_values;
  76.     var $htmlencoded=false;
  77.  
  78.     function SquirrelOption
  79.     ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
  80.         /* Set the basic stuff. */
  81.         $this->name = $name;
  82.         $this->caption = $caption;
  83.         $this->type = $type;
  84.         $this->refresh_level = $refresh_level;
  85.         $this->possible_values = $possible_values;
  86.         $this->htmlencoded = $htmlencoded;
  87.         $this->size = SMOPT_SIZE_MEDIUM;
  88.         $this->comment = '';
  89.         $this->script = '';
  90.         $this->post_script = '';
  91.  
  92.         /* Check for a current value. */
  93.         if (isset($GLOBALS[$name])) {
  94.             $this->value = $GLOBALS[$name];
  95.         } else if (!empty($initial_value)) {
  96.             $this->value = $initial_value;
  97.         } else {
  98.             $this->value = '';
  99.         }
  100.  
  101.         /* Check for a new value. */
  102.     if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
  103.             $this->new_value = '';
  104.         }
  105.  
  106.         /* Set the default save function. */
  107.         if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
  108.             $this->save_function = SMOPT_SAVE_DEFAULT;
  109.         } else {
  110.             $this->save_function = SMOPT_SAVE_NOOP;
  111.         }
  112.     }
  113.  
  114.     /* Set the value for this option. */
  115.     function setValue($value) {
  116.         $this->value = $value;
  117.     }
  118.  
  119.     /* Set the new value for this option. */
  120.     function setNewValue($new_value) {
  121.         $this->new_value = $new_value;
  122.     }
  123.  
  124.     /* Set the size for this option. */
  125.     function setSize($size) {
  126.         $this->size = $size;
  127.     }
  128.  
  129.     /* Set the comment for this option. */
  130.     function setComment($comment) {
  131.         $this->comment = $comment;
  132.     }
  133.  
  134.     /* Set the script for this option. */
  135.     function setScript($script) {
  136.         $this->script = $script;
  137.     }
  138.  
  139.     /* Set the "post script" for this option. */
  140.     function setPostScript($post_script) {
  141.         $this->post_script = $post_script;
  142.     }
  143.  
  144.     /* Set the save function for this option. */
  145.     function setSaveFunction($save_function) {
  146.         $this->save_function = $save_function;
  147.     }
  148.  
  149.     function createHTMLWidget() {
  150.         global $javascript_on, $color;
  151.  
  152.         // Use new value if available
  153.         if (!empty($this->new_value)) {
  154.             $tempValue = $this->value;
  155.             $this->value = $this->new_value;
  156.         }
  157.  
  158.         /* Get the widget for this option type. */
  159.         switch ($this->type) {
  160.             case SMOPT_TYPE_STRING:
  161.                 $result = $this->createWidget_String();
  162.                 break;
  163.             case SMOPT_TYPE_STRLIST:
  164.                 $result = $this->createWidget_StrList();
  165.                 break;
  166.             case SMOPT_TYPE_TEXTAREA:
  167.                 $result = $this->createWidget_TextArea();
  168.                 break;
  169.             case SMOPT_TYPE_INTEGER:
  170.                 $result = $this->createWidget_Integer();
  171.                 break;
  172.             case SMOPT_TYPE_FLOAT:
  173.                 $result = $this->createWidget_Float();
  174.                 break;
  175.             case SMOPT_TYPE_BOOLEAN:
  176.                 $result = $this->createWidget_Boolean();
  177.                 break;
  178.             case SMOPT_TYPE_HIDDEN:
  179.                 $result = $this->createWidget_Hidden();
  180.                 break;
  181.             case SMOPT_TYPE_COMMENT:
  182.                 $result = $this->createWidget_Comment();
  183.                 break;
  184.             case SMOPT_TYPE_FLDRLIST:
  185.                 $result = $this->createWidget_FolderList();
  186.                 break;
  187.             default:
  188.                $result = '<font color="' . $color[2] . '">'
  189.                        . sprintf(_("Option Type '%s' Not Found"), $this->type)
  190.                        . '</font>';
  191.         }
  192.  
  193.         /* Add the "post script" for this option. */
  194.         $result .= $this->post_script;
  195.  
  196.         // put correct value back if need be
  197.         if (!empty($this->new_value)) {
  198.             $this->value = $tempValue;
  199.         }
  200.  
  201.         /* Now, return the created widget. */
  202.         return ($result);
  203.     }
  204.  
  205.     function createWidget_String() {
  206.         switch ($this->size) {
  207.             case SMOPT_SIZE_TINY:
  208.                 $width = 5;
  209.                 break;
  210.             case SMOPT_SIZE_SMALL:
  211.                 $width = 12;
  212.                 break;
  213.             case SMOPT_SIZE_LARGE:
  214.                 $width = 38;
  215.                 break;
  216.             case SMOPT_SIZE_HUGE:
  217.                 $width = 50;
  218.                 break;
  219.             case SMOPT_SIZE_NORMAL:
  220.             default:
  221.                 $width = 25;
  222.         }
  223.  
  224.         $result = "<input type=\"text\" name=\"new_$this->name\" value=\"" .
  225.             htmlspecialchars($this->value) .
  226.             "\" size=\"$width\" $this->script />\n";
  227.         return ($result);
  228.     }
  229.  
  230.     function createWidget_StrList() {
  231.         /* Begin the select tag. */
  232.         $result = "<select name=\"new_$this->name\" $this->script>\n";
  233.  
  234.         /* Add each possible value to the select list. */
  235.         foreach ($this->possible_values as $real_value => $disp_value) {
  236.             /* Start the next new option string. */
  237.             $new_option = '<option value="' .
  238.                 ($this->htmlencoded ? $real_value : htmlspecialchars($real_value)) . '"';
  239.  
  240.             /* If this value is the current value, select it. */
  241.             if ($real_value == $this->value) {
  242.                $new_option .= ' selected="selected"';
  243.             }
  244.  
  245.             /* Add the display value to our option string. */
  246.             $new_option .= '>' . ($this->htmlencoded ? $disp_value : htmlspecialchars($disp_value)) . "</option>\n";
  247.             /* And add the new option string to our select tag. */
  248.             $result .= $new_option;
  249.         }
  250.  
  251.         /* Close the select tag and return our happy result. */
  252.         $result .= "</select>\n";
  253.         return ($result);
  254.     }
  255.  
  256.     function createWidget_FolderList() {
  257.         $selected = array(strtolower($this->value));
  258.  
  259.         /* Begin the select tag. */
  260.         $result = "<select name=\"new_$this->name\" $this->script>\n";
  261.  
  262.         /* Add each possible value to the select list. */
  263.         foreach ($this->possible_values as $real_value => $disp_value) {
  264.             if ( is_array($disp_value) ) {
  265.               /* For folder list, we passed in the array of boxes.. */
  266.               $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
  267.             } else {
  268.               /* Start the next new option string. */
  269.               $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
  270.  
  271.               /* If this value is the current value, select it. */
  272.               if ($real_value == $this->value) {
  273.                  $new_option .= ' selected="selected"';
  274.               }
  275.  
  276.               /* Add the display value to our option string. */
  277.               $new_option .= '>' . htmlspecialchars($disp_value) . "</option>\n";
  278.             }
  279.             /* And add the new option string to our select tag. */
  280.             $result .= $new_option;
  281.         }
  282.         /* Close the select tag and return our happy result. */
  283.         $result .= "</select>\n";
  284.         return ($result);
  285.     }
  286.  
  287.  
  288.     function createWidget_TextArea() {
  289.         switch ($this->size) {
  290.             case SMOPT_SIZE_TINY:  $rows = 3; $cols =  10; break;
  291.             case SMOPT_SIZE_SMALL: $rows = 4; $cols =  30; break;
  292.             case SMOPT_SIZE_LARGE: $rows = 10; $cols =  60; break;
  293.             case SMOPT_SIZE_HUGE:  $rows = 20; $cols =  80; break;
  294.             case SMOPT_SIZE_NORMAL:
  295.             default: $rows = 5; $cols =  50;
  296.         }
  297.         $result = "<textarea name=\"new_$this->name\" rows=\"$rows\" "
  298.                 . "cols=\"$cols\" $this->script>"
  299.                 . htmlspecialchars($this->value) . "</textarea>\n";
  300.         return ($result);
  301.     }
  302.  
  303.     function createWidget_Integer() {
  304.  
  305.         global $javascript_on;
  306.  
  307.         // add onChange javascript handler to a regular string widget
  308.         // which will strip out all non-numeric chars
  309.         if ($javascript_on)
  310.            return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
  311.                     . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
  312.                     . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
  313.                     . 'this.value=newVal;" />', $this->createWidget_String());
  314.         else
  315.            return $this->createWidget_String();
  316.     }
  317.  
  318.     function createWidget_Float() {
  319.  
  320.         global $javascript_on;
  321.  
  322.         // add onChange javascript handler to a regular string widget
  323.         // which will strip out all non-numeric (period also OK) chars
  324.         if ($javascript_on)
  325.            return preg_replace('/\/>/', ' onChange="origVal=this.value; newVal=\'\'; '
  326.                     . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
  327.                     . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
  328.                     . 'newVal += origVal.charAt(i); } this.value=newVal;" />'
  329.                 , $this->createWidget_String());
  330.         else
  331.            return $this->createWidget_String();
  332.     }
  333.  
  334.     function createWidget_Boolean() {
  335.         /* Do the whole current value thing. */
  336.         if ($this->value != SMPREF_NO) {
  337.             $yes_chk = ' checked="checked"';
  338.             $no_chk = '';
  339.         } else {
  340.             $yes_chk = '';
  341.             $no_chk = ' checked="checked"';
  342.         }
  343.  
  344.         /* Build the yes choice. */
  345.         $yes_option = '<input type="radio" name="new_' . $this->name
  346.                     . '" value="' . SMPREF_YES . "\"$yes_chk $this->script />&nbsp;"
  347.                     . _("Yes");
  348.  
  349.         /* Build the no choice. */
  350.         $no_option = '<input type="radio" name="new_' . $this->name
  351.                    . '" value="' . SMPREF_NO . "\"$no_chk $this->script />&nbsp;"
  352.                    . _("No");
  353.  
  354.         /* Build and return the combined "boolean widget". */
  355.         $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
  356.         return ($result);
  357.     }
  358.  
  359.     function createWidget_Hidden() {
  360.         $result = '<input type="hidden" name="new_' . $this->name
  361.                 . '" value="' . htmlspecialchars($this->value)
  362.                 . '" ' . $this->script . ' />';
  363.         return ($result);
  364.     }
  365.  
  366.     function createWidget_Comment() {
  367.         $result = $this->comment;
  368.         return ($result);
  369.     }
  370.  
  371.     function save() {
  372.         $function = $this->save_function;
  373.         $function($this);
  374.     }
  375.  
  376.     function changed() {
  377.         return ($this->value != $this->new_value);
  378.     }
  379. }
  380.  
  381. function save_option($option) {
  382.     if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  383.         /* Can't save the pref if we don't have the username */
  384.         return;
  385.     }
  386.     global $data_dir;
  387.     setPref($data_dir, $username, $option->name, $option->new_value);
  388. }
  389.  
  390. function save_option_noop($option) {
  391.     /* Do nothing here... */
  392. }
  393.  
  394. function create_optpage_element($optpage) {
  395.     return create_hidden_element('optpage', $optpage);
  396. }
  397.  
  398. function create_optmode_element($optmode) {
  399.     return create_hidden_element('optmode', $optmode);
  400. }
  401.  
  402. function create_hidden_element($name, $value) {
  403.     $result = '<input type="hidden" '
  404.             . 'name="' . $name . '" '
  405.             . 'value="' . htmlspecialchars($value) . '" />';
  406.     return ($result);
  407. }
  408.  
  409. function create_option_groups($optgrps, $optvals) {
  410.     /* Build a simple array with which to start. */
  411.     $result = array();
  412.  
  413.     /* Create option group for each option group name. */
  414.     foreach ($optgrps as $grpkey => $grpname) {
  415.         $result[$grpkey] = array();
  416.         $result[$grpkey]['name'] = $grpname;
  417.         $result[$grpkey]['options'] = array();
  418.     }
  419.  
  420.      /* Create a new SquirrelOption for each set of option values. */
  421.     foreach ($optvals as $grpkey => $grpopts) {
  422.         foreach ($grpopts as $optset) {
  423.             /* Create a new option with all values given. */
  424.             $next_option = new SquirrelOption(
  425.                 $optset['name'],
  426.                 $optset['caption'],
  427.                 $optset['type'],
  428.                 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  429.                 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
  430.                 (isset($optset['posvals']) ? $optset['posvals'] : ''),
  431.                 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
  432.                 );
  433.  
  434.             /* If provided, set the size for this option. */
  435.             if (isset($optset['size'])) {
  436.                 $next_option->setSize($optset['size']);
  437.             }
  438.  
  439.             /* If provided, set the comment for this option. */
  440.             if (isset($optset['comment'])) {
  441.                 $next_option->setComment($optset['comment']);
  442.             }
  443.  
  444.             /* If provided, set the save function for this option. */
  445.             if (isset($optset['save'])) {
  446.                 $next_option->setSaveFunction($optset['save']);
  447.             }
  448.  
  449.             /* If provided, set the script for this option. */
  450.             if (isset($optset['script'])) {
  451.                 $next_option->setScript($optset['script']);
  452.             }
  453.  
  454.             /* If provided, set the "post script" for this option. */
  455.             if (isset($optset['post_script'])) {
  456.                 $next_option->setPostScript($optset['post_script']);
  457.             }
  458.  
  459.             /* Add this option to the option array. */
  460.             $result[$grpkey]['options'][] = $next_option;
  461.         }
  462.     }
  463.  
  464.     /* Return our resulting array. */
  465.     return ($result);
  466. }
  467.  
  468. function print_option_groups($option_groups) {
  469.     /* Print each option group. */
  470.     foreach ($option_groups as $next_optgrp) {
  471.         /* If it is not blank, print the name for this option group. */
  472.         if ($next_optgrp['name'] != '') {
  473.             echo html_tag( 'tr', "\n".
  474.                         html_tag( 'td',
  475.                             '<b>' . $next_optgrp['name'] . '</b>' ,
  476.                         'center' ,'', 'valign="middle" colspan="2" nowrap' )
  477.                     ) ."\n";
  478.         }
  479.  
  480.         /* Print each option in this option group. */
  481.         foreach ($next_optgrp['options'] as $option) {
  482.             if ($option->type != SMOPT_TYPE_HIDDEN) {
  483.                 echo html_tag( 'tr', "\n".
  484.                            html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
  485.                            html_tag( 'td', $option->createHTMLWidget(), 'left' )
  486.                        ) ."\n";
  487.             } else {
  488.                 echo $option->createHTMLWidget();
  489.             }
  490.         }
  491.  
  492.         /* Print an empty row after this option group. */
  493.         echo html_tag( 'tr',
  494.                    html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
  495.                 ) . "\n";
  496.     }
  497. }
  498.  
  499. function OptionSubmit( $name ) {
  500.         echo html_tag( 'tr',
  501.                    html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '" />&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
  502.                 ) . "\n";
  503. }
  504.  
  505. // vim: et ts=4
  506. ?>

Documentation generated on Sat, 07 Oct 2006 16:32:45 +0300 by phpDocumentor 1.3.0RC6