options.php 18 KB

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