options.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 $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" id="new_' . $this->name . '_yes" '
  283. . 'name="new_' . $this->name . '" value="' . SMPREF_YES . '"'
  284. . $yes_chk . ' ' . $this->script . '>&nbsp;'
  285. . '<label for="new_'.$this->name.'_yes">' . _("Yes") . '</label>';
  286. /* Build the no choice. */
  287. $no_option = '<input type="radio" id="new_' . $this->name . '_no" '
  288. . 'name="new_' . $this->name . '" value="' . SMPREF_NO . '"'
  289. . $no_chk . ' ' . $this->script . '>&nbsp;'
  290. . '<label for="new_'.$this->name.'_no">' . _("No") . '</label>';
  291. /* Build and return the combined "boolean widget". */
  292. $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
  293. return ($result);
  294. }
  295. function createWidget_Hidden() {
  296. $result = '<input type="hidden" name="new_' . $this->name
  297. . '" value="' . $this->value . '" ' . $this->script . '>';
  298. return ($result);
  299. }
  300. function createWidget_Comment() {
  301. $result = $this->comment;
  302. return ($result);
  303. }
  304. function save() {
  305. $function = $this->save_function;
  306. $function($this);
  307. }
  308. function changed() {
  309. return ($this->value != $this->new_value);
  310. }
  311. }
  312. function save_option($option) {
  313. if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  314. /* Can't save the pref if we don't have the username */
  315. return;
  316. }
  317. global $data_dir;
  318. setPref($data_dir, $username, $option->name, $option->new_value);
  319. }
  320. function save_option_noop($option) {
  321. /* Do nothing here... */
  322. }
  323. function create_optpage_element($optpage) {
  324. return create_hidden_element('optpage', $optpage);
  325. }
  326. function create_optmode_element($optmode) {
  327. return create_hidden_element('optmode', $optmode);
  328. }
  329. function create_hidden_element($name, $value) {
  330. $result = '<input type="hidden" '
  331. . 'name="' . $name . '" '
  332. . 'value="' . $value . '">';
  333. return ($result);
  334. }
  335. function create_option_groups($optgrps, $optvals) {
  336. /* Build a simple array with which to start. */
  337. $result = array();
  338. /* Create option group for each option group name. */
  339. foreach ($optgrps as $grpkey => $grpname) {
  340. $result[$grpkey] = array();
  341. $result[$grpkey]['name'] = $grpname;
  342. $result[$grpkey]['options'] = array();
  343. }
  344. /* Create a new SquirrelOption for each set of option values. */
  345. foreach ($optvals as $grpkey => $grpopts) {
  346. foreach ($grpopts as $optset) {
  347. if (isset($optset['posvals'])) {
  348. /* Create a new option with all values given. */
  349. $next_option = new SquirrelOption(
  350. $optset['name'],
  351. $optset['caption'],
  352. $optset['type'],
  353. (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  354. (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
  355. $optset['posvals']
  356. );
  357. } else {
  358. /* Create a new option with all but possible values given. */
  359. $next_option = new SquirrelOption(
  360. $optset['name'],
  361. $optset['caption'],
  362. $optset['type'],
  363. (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  364. (isset($optset['initial_value']) ? $optset['initial_value'] : '')
  365. );
  366. }
  367. /* If provided, set the size for this option. */
  368. if (isset($optset['size'])) {
  369. $next_option->setSize($optset['size']);
  370. }
  371. /* If provided, set the comment for this option. */
  372. if (isset($optset['comment'])) {
  373. $next_option->setComment($optset['comment']);
  374. }
  375. /* If provided, set the save function for this option. */
  376. if (isset($optset['save'])) {
  377. $next_option->setSaveFunction($optset['save']);
  378. }
  379. /* If provided, set the script for this option. */
  380. if (isset($optset['script'])) {
  381. $next_option->setScript($optset['script']);
  382. }
  383. /* If provided, set the "post script" for this option. */
  384. if (isset($optset['post_script'])) {
  385. $next_option->setPostScript($optset['post_script']);
  386. }
  387. /* Add this option to the option array. */
  388. $result[$grpkey]['options'][] = $next_option;
  389. }
  390. }
  391. /* Return our resulting array. */
  392. return ($result);
  393. }
  394. function print_option_groups($option_groups) {
  395. /* Print each option group. */
  396. foreach ($option_groups as $next_optgrp) {
  397. /* If it is not blank, print the name for this option group. */
  398. if ($next_optgrp['name'] != '') {
  399. echo html_tag( 'tr', "\n".
  400. html_tag( 'td',
  401. '<b>' . $next_optgrp['name'] . '</b>' ,
  402. 'center' ,'', 'valign="middle" colspan="2" nowrap' )
  403. ) ."\n";
  404. }
  405. /* Print each option in this option group. */
  406. foreach ($next_optgrp['options'] as $option) {
  407. if ($option->type != SMOPT_TYPE_HIDDEN) {
  408. echo html_tag( 'tr', "\n".
  409. html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
  410. html_tag( 'td', $option->createHTMLWidget(), 'left' )
  411. ) ."\n";
  412. } else {
  413. echo $option->createHTMLWidget();
  414. }
  415. }
  416. /* Print an empty row after this option group. */
  417. echo html_tag( 'tr',
  418. html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
  419. ) . "\n";
  420. }
  421. }
  422. function OptionSubmit( $name ) {
  423. echo html_tag( 'tr',
  424. html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">&nbsp;&nbsp;&nbsp;&nbsp;', 'right', '', 'colspan="2"' )
  425. ) . "\n";
  426. }
  427. ?>