options.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. return ($this->createWidget_String());
  239. }
  240. function createWidget_Float() {
  241. return ($this->createWidget_String());
  242. }
  243. function createWidget_Boolean() {
  244. /* Do the whole current value thing. */
  245. if ($this->value != SMPREF_NO) {
  246. $yes_chk = ' checked';
  247. $no_chk = '';
  248. } else {
  249. $yes_chk = '';
  250. $no_chk = ' checked';
  251. }
  252. /* Build the yes choice. */
  253. $yes_option = '<input type="radio" name="new_' . $this->name
  254. . '" value="' . SMPREF_YES . "\"$yes_chk>&nbsp;"
  255. . _("Yes");
  256. /* Build the no choice. */
  257. $no_option = '<input type="radio" name="new_' . $this->name
  258. . '" value="' . SMPREF_NO . "\"$no_chk>&nbsp;"
  259. . _("No");
  260. /* Build and return the combined "boolean widget". */
  261. $result = "$yes_option&nbsp;&nbsp;&nbsp;&nbsp;$no_option";
  262. return ($result);
  263. }
  264. function createWidget_Hidden() {
  265. $result = '<input type="hidden" name="new_' . $this->name
  266. . '" value="' . $this->value . '">';
  267. return ($result);
  268. }
  269. function createWidget_Comment() {
  270. $result = $this->comment;
  271. return ($result);
  272. }
  273. function save() {
  274. $function = $this->save_function;
  275. $function($this);
  276. }
  277. function changed() {
  278. return ($this->value != $this->new_value);
  279. }
  280. }
  281. function save_option($option) {
  282. if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  283. /* Can't save the pref if we don't have the username */
  284. return;
  285. }
  286. global $data_dir;
  287. setPref($data_dir, $username, $option->name, $option->new_value);
  288. }
  289. function save_option_noop($option) {
  290. /* Do nothing here... */
  291. }
  292. function create_optpage_element($optpage) {
  293. return create_hidden_element('optpage', $optpage);
  294. }
  295. function create_optmode_element($optmode) {
  296. return create_hidden_element('optmode', $optmode);
  297. }
  298. function create_hidden_element($name, $value) {
  299. $result = '<input type="hidden" '
  300. . 'name="' . $name . '" '
  301. . 'value="' . $value . '">';
  302. return ($result);
  303. }
  304. function create_option_groups($optgrps, $optvals) {
  305. /* Build a simple array with which to start. */
  306. $result = array();
  307. /* Create option group for each option group name. */
  308. foreach ($optgrps as $grpkey => $grpname) {
  309. $result[$grpkey] = array();
  310. $result[$grpkey]['name'] = $grpname;
  311. $result[$grpkey]['options'] = array();
  312. }
  313. /* Create a new SquirrelOption for each set of option values. */
  314. foreach ($optvals as $grpkey => $grpopts) {
  315. foreach ($grpopts as $optset) {
  316. if (isset($optset['posvals'])) {
  317. /* Create a new option with all values given. */
  318. $next_option = new SquirrelOption(
  319. $optset['name'],
  320. $optset['caption'],
  321. $optset['type'],
  322. $optset['refresh'],
  323. $optset['posvals']
  324. );
  325. } else {
  326. /* Create a new option with all but possible values given. */
  327. $next_option = new SquirrelOption(
  328. $optset['name'],
  329. $optset['caption'],
  330. $optset['type'],
  331. $optset['refresh']
  332. );
  333. }
  334. /* If provided, set the size for this option. */
  335. if (isset($optset['size'])) {
  336. $next_option->setSize($optset['size']);
  337. }
  338. /* If provided, set the comment for this option. */
  339. if (isset($optset['comment'])) {
  340. $next_option->setComment($optset['comment']);
  341. }
  342. /* If provided, set the save function for this option. */
  343. if (isset($optset['save'])) {
  344. $next_option->setSaveFunction($optset['save']);
  345. }
  346. /* If provided, set the script for this option. */
  347. if (isset($optset['script'])) {
  348. $next_option->setScript($optset['script']);
  349. }
  350. /* Add this option to the option array. */
  351. $result[$grpkey]['options'][] = $next_option;
  352. }
  353. }
  354. /* Return our resulting array. */
  355. return ($result);
  356. }
  357. function print_option_groups($option_groups) {
  358. /* Print each option group. */
  359. foreach ($option_groups as $next_optgrp) {
  360. /* If it is not blank, print the name for this option group. */
  361. if ($next_optgrp['name'] != '') {
  362. echo html_tag( 'tr', "\n".
  363. html_tag( 'td',
  364. '<b>' . $next_optgrp['name'] . '</b>' ,
  365. 'center' ,'', 'valign="middle" colspan="2" nowrap' )
  366. ) ."\n";
  367. }
  368. /* Print each option in this option group. */
  369. foreach ($next_optgrp['options'] as $option) {
  370. if ($option->type != SMOPT_TYPE_HIDDEN) {
  371. echo html_tag( 'tr', "\n".
  372. html_tag( 'td', $option->caption . ':', 'right' ,'', 'valign="middle"' ) .
  373. html_tag( 'td', $option->createHTMLWidget(), 'left' )
  374. ) ."\n";
  375. } else {
  376. echo $option->createHTMLWidget();
  377. }
  378. }
  379. /* Print an empty row after this option group. */
  380. echo html_tag( 'tr',
  381. html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' )
  382. ) . "\n";
  383. }
  384. }
  385. function OptionSubmit( $name ) {
  386. echo html_tag( 'tr',
  387. html_tag( 'td', '&nbsp;', 'left', '', 'colspan="2"' ) .
  388. html_tag( 'td', '<input type="submit" value="' . _("Submit") . '" name="' . $name . '">', 'left', '', 'colspan="2"' )
  389. ) . "\n";
  390. }
  391. ?>