options.php 15 KB

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