options.php 17 KB

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