options.php 16 KB

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