options.php 12 KB

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