options.php 14 KB

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