options.php 16 KB

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