options.php 17 KB

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