options.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 constants for the options refresh levels. */
  25. define('SMOPT_REFRESH_NONE', 0);
  26. define('SMOPT_REFRESH_FOLDERLIST', 1);
  27. define('SMOPT_REFRESH_ALL', 2);
  28. /* Define constants for the options size. */
  29. define('SMOPT_SIZE_TINY', 0);
  30. define('SMOPT_SIZE_SMALL', 1);
  31. define('SMOPT_SIZE_MEDIUM', 2);
  32. define('SMOPT_SIZE_LARGE', 3);
  33. define('SMOPT_SIZE_HUGE', 4);
  34. define('SMOPT_SIZE_NORMAL', 5);
  35. define('SMOPT_SAVE_DEFAULT', 'save_option');
  36. define('SMOPT_SAVE_NOOP', 'save_option_noop');
  37. /**
  38. * SquirrelOption: An option for Squirrelmail.
  39. *
  40. * This class is a work in progress. When complete, it will handle
  41. * presentation and saving of Squirrelmail user options in a simple,
  42. * streamline manner. Stay tuned for more stuff.
  43. *
  44. * Also, I'd like to ask that people leave this alone (mostly :) until
  45. * I get it a little further along. That should only be a day or two or
  46. * three. I will remove this message when it is ready for primetime usage.
  47. */
  48. class SquirrelOption {
  49. /* The basic stuff. */
  50. var $name;
  51. var $caption;
  52. var $type;
  53. var $refresh_level;
  54. var $size;
  55. var $comment;
  56. var $script;
  57. /* The name of the Save Function for this option. */
  58. var $save_function;
  59. /* The various 'values' for this options. */
  60. var $value;
  61. var $new_value;
  62. var $possible_values;
  63. function SquirrelOption
  64. ($name, $caption, $type, $refresh_level, $possible_values = '') {
  65. /* Set the basic stuff. */
  66. $this->name = $name;
  67. $this->caption = $caption;
  68. $this->type = $type;
  69. $this->refresh_level = $refresh_level;
  70. $this->possible_values = $possible_values;
  71. $this->size = SMOPT_SIZE_MEDIUM;
  72. $this->comment = '';
  73. $this->script = '';
  74. /* Check for a current value. */
  75. if (isset($GLOBALS[$name])) {
  76. $this->value = $GLOBALS[$name];
  77. } else {
  78. $this->value = '';
  79. }
  80. /* Check for a new value. */
  81. if (isset($GLOBALS["new_$name"])) {
  82. $this->new_value = $GLOBALS["new_$name"];
  83. } else {
  84. $this->new_value = '';
  85. }
  86. /* Set the default save function. */
  87. if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) {
  88. $this->save_function = SMOPT_SAVE_DEFAULT;
  89. } else {
  90. $this->save_function = SMOPT_SAVE_NOOP;
  91. }
  92. }
  93. /* Set the value for this option. */
  94. function setValue($value) {
  95. $this->value = $value;
  96. }
  97. /* Set the new value for this option. */
  98. function setNewValue($new_value) {
  99. $this->new_value = $new_value;
  100. }
  101. /* Set the size for this option. */
  102. function setSize($size) {
  103. $this->size = $size;
  104. }
  105. /* Set the comment for this option. */
  106. function setComment($comment) {
  107. $this->comment = $comment;
  108. }
  109. /* Set the script for this option. */
  110. function setScript($script) {
  111. $this->script = $script;
  112. }
  113. /* Set the save function for this option. */
  114. function setSaveFunction($save_function) {
  115. $this->save_function = $save_function;
  116. }
  117. function createHTMLWidget() {
  118. global $javascript_on;
  119. /* Get the widget for this option type. */
  120. switch ($this->type) {
  121. case SMOPT_TYPE_STRING:
  122. $result = $this->createWidget_String();
  123. break;
  124. case SMOPT_TYPE_STRLIST:
  125. $result = $this->createWidget_StrList();
  126. break;
  127. case SMOPT_TYPE_TEXTAREA:
  128. $result = $this->createWidget_TextArea();
  129. break;
  130. case SMOPT_TYPE_INTEGER:
  131. $result = $this->createWidget_Integer();
  132. break;
  133. case SMOPT_TYPE_FLOAT:
  134. $result = $this->createWidget_Float();
  135. break;
  136. case SMOPT_TYPE_BOOLEAN:
  137. $result = $this->createWidget_Boolean();
  138. break;
  139. case SMOPT_TYPE_HIDDEN:
  140. $result = $this->createWidget_Hidden();
  141. break;
  142. case SMOPT_TYPE_COMMENT:
  143. $result = $this->createWidget_Comment();
  144. break;
  145. default:
  146. $result = '<FONT COLOR="' . $color[2] . '">'
  147. . sprintf(_("Option Type '%s' Not Found"), $this->type)
  148. . '</FONT>';
  149. }
  150. /* Add the script for this option. */
  151. $result .= $this->script;
  152. /* Now, return the created widget. */
  153. return ($result);
  154. }
  155. function createWidget_String() {
  156. switch ($this->size) {
  157. case SMOPT_SIZE_TINY:
  158. $width = 5;
  159. break;
  160. case SMOPT_SIZE_SMALL:
  161. $width = 12;
  162. break;
  163. case SMOPT_SIZE_LARGE:
  164. $width = 38;
  165. break;
  166. case SMOPT_SIZE_HUGE:
  167. $width = 50;
  168. break;
  169. case SMOPT_SIZE_NORMAL:
  170. default:
  171. $width = 25;
  172. }
  173. $result = "<INPUT NAME=\"new_$this->name\" VALUE=\"$this->value\" SIZE=\"$width\">";
  174. return ($result);
  175. }
  176. function createWidget_StrList() {
  177. /* Begin the select tag. */
  178. $result = "<SELECT NAME=\"new_$this->name\">";
  179. /* Add each possible value to the select list. */
  180. foreach ($this->possible_values as $real_value => $disp_value) {
  181. /* Start the next new option string. */
  182. $new_option = "<OPTION VALUE=\"$real_value\"";
  183. /* If this value is the current value, select it. */
  184. if ($real_value == $this->value) {
  185. $new_option .= ' SELECTED';
  186. }
  187. /* Add the display value to our option string. */
  188. $new_option .= ">$disp_value</OPTION>";
  189. /* And add the new option string to our select tag. */
  190. $result .= $new_option;
  191. }
  192. /* Close the select tag and return our happy result. */
  193. $result .= '</SELECT>';
  194. return ($result);
  195. }
  196. function createWidget_TextArea() {
  197. switch ($this->size) {
  198. case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
  199. case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
  200. case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
  201. case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
  202. case SMOPT_SIZE_NORMAL:
  203. default: $rows = 5; $cols = 50;
  204. }
  205. $result = "<TEXTAREA NAME=\"new_$this->name\" ROWS=\"$rows\" "
  206. . "COLS=\"$cols\">$this->value</TEXTAREA>";
  207. return ($result);
  208. }
  209. function createWidget_Integer() {
  210. return ($this->createWidget_String());
  211. }
  212. function createWidget_Float() {
  213. return ($this->createWidget_String());
  214. }
  215. function createWidget_Boolean() {
  216. /* Do the whole current value thing. */
  217. if ($this->value != SMPREF_NO) {
  218. $yes_chk = ' CHECKED';
  219. $no_chk = '';
  220. } else {
  221. $yes_chk = '';
  222. $no_chk = ' CHECKED';
  223. }
  224. /* Build the yes choice. */
  225. $yes_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
  226. . '" VALUE="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
  227. . _("Yes");
  228. /* Build the no choice. */
  229. $no_option = '<INPUT TYPE="RADIO" NAME="new_' . $this->name
  230. . '" VALUE="' . SMPREF_NO . "\"$no_chk>&nbsp;"
  231. . _("No");
  232. /* Build and return the combined "boolean widget". */
  233. $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
  234. return ($result);
  235. }
  236. function createWidget_Hidden() {
  237. $result = '<INPUT TYPE="HIDDEN" NAME="new_' . $this->name
  238. . '" VALUE="' . $this->value . '">';
  239. return ($result);
  240. }
  241. function createWidget_Comment() {
  242. $result = $this->comment;
  243. return ($result);
  244. }
  245. function save() {
  246. $function = $this->save_function;
  247. $function($this);
  248. }
  249. function changed() {
  250. return ($this->value !== $this->new_value);
  251. }
  252. }
  253. function save_option($option) {
  254. global $data_dir, $username;
  255. setPref($data_dir, $username, $option->name, $option->new_value);
  256. /* I do not know if this next line does any good. */
  257. $GLOBALS[$option->name] = $option->new_value;
  258. }
  259. function save_option_noop($option) {
  260. /* Do nothing here... */
  261. }
  262. function create_optpage_element($optpage) {
  263. return create_hidden_element('optpage', $optpage);
  264. }
  265. function create_optmode_element($optmode) {
  266. return create_hidden_element('optmode', $optmode);
  267. }
  268. function create_hidden_element($name, $value) {
  269. $result = '<INPUT TYPE="HIDDEN" '
  270. . 'NAME="' . $name . '" '
  271. . 'VALUE="' . $value . '">';
  272. return ($result);
  273. }
  274. function create_option_groups($optgrps, $optvals) {
  275. /* Build a simple array with which to start. */
  276. $result = array();
  277. /* Create option group for each option group name. */
  278. foreach ($optgrps as $grpkey => $grpname) {
  279. $result[$grpkey] = array();
  280. $result[$grpkey]['name'] = $grpname;
  281. $result[$grpkey]['options'] = array();
  282. }
  283. /* Create a new SquirrelOption for each set of option values. */
  284. foreach ($optvals as $grpkey => $grpopts) {
  285. foreach ($grpopts as $optset) {
  286. if (isset($optset['posvals'])) {
  287. /* Create a new option with all values given. */
  288. $next_option = new SquirrelOption(
  289. $optset['name'],
  290. $optset['caption'],
  291. $optset['type'],
  292. $optset['refresh'],
  293. $optset['posvals']
  294. );
  295. } else {
  296. /* Create a new option with all but possible values given. */
  297. $next_option = new SquirrelOption(
  298. $optset['name'],
  299. $optset['caption'],
  300. $optset['type'],
  301. $optset['refresh']
  302. );
  303. }
  304. /* If provided, set the size for this option. */
  305. if (isset($optset['size'])) {
  306. $next_option->setSize($optset['size']);
  307. }
  308. /* If provided, set the comment for this option. */
  309. if (isset($optset['comment'])) {
  310. $next_option->setComment($optset['comment']);
  311. }
  312. /* If provided, set the save function for this option. */
  313. if (isset($optset['save'])) {
  314. $next_option->setSaveFunction($optset['save']);
  315. }
  316. /* If provided, set the script for this option. */
  317. if (isset($optset['script'])) {
  318. $next_option->setScript($optset['script']);
  319. }
  320. /* Add this option to the option array. */
  321. $result[$grpkey]['options'][] = $next_option;
  322. }
  323. }
  324. /* Return our resulting array. */
  325. return ($result);
  326. }
  327. function print_option_groups($option_groups) {
  328. /* Print each option group. */
  329. foreach ($option_groups as $next_optgrp) {
  330. /* If it is not blank, print the name for this option group. */
  331. if ($next_optgrp['name'] != '') {
  332. echo '<TR><TD ALIGN=CENTER VALIGN=MIDDLE COLSPAN=2 NOWRAP><B>'
  333. . $next_optgrp['name']
  334. . "</B></TD></TR>\n";
  335. }
  336. /* Print each option in this option group. */
  337. foreach ($next_optgrp['options'] as $option) {
  338. if ($option->type != SMOPT_TYPE_HIDDEN) {
  339. echo "<TR>\n";
  340. echo ' <TD ALIGN="RIGHT" VALIGN="MIDDLE">'
  341. . $option->caption . ":</TD>\n";
  342. echo ' <TD>' . $option->createHTMLWidget() . "</TD>\n";
  343. echo "</TR>\n";
  344. } else {
  345. echo $option->createHTMLWidget();
  346. }
  347. }
  348. /* Print an empty row after this option group. */
  349. echo "<TR><TD COLSPAN=\"2\">&nbsp;</TD></TR>\n";
  350. }
  351. }
  352. function OptionSubmit( $name ) {
  353. echo '<tr><td>&nbsp;</td><td><input type="submit" value="' . _("Submit") . '" name="' . $name . '">' .
  354. '</td></tr>';
  355. }
  356. ?>