options.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. <?php
  2. /**
  3. * options.php
  4. *
  5. * Functions needed to display the options pages.
  6. *
  7. * @copyright 1999-2017 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. * @subpackage prefs
  12. */
  13. /**
  14. * SquirrelOption: An option for SquirrelMail.
  15. *
  16. * @package squirrelmail
  17. * @subpackage prefs
  18. */
  19. class SquirrelOption {
  20. /**
  21. * The original option configuration array
  22. * @var array
  23. */
  24. var $raw_option_array;
  25. /**
  26. * The name of this setting
  27. * @var string
  28. */
  29. var $name;
  30. /**
  31. * The text that prefaces setting on the preferences page
  32. * @var string
  33. */
  34. var $caption;
  35. /**
  36. * Whether or not the caption text is allowed to wrap
  37. * @var boolean
  38. */
  39. var $caption_wrap;
  40. /**
  41. * The type of INPUT element
  42. *
  43. * See SMOPT_TYPE_* defines
  44. * @var integer
  45. */
  46. var $type;
  47. /**
  48. * Indicates if a link should be shown to refresh part
  49. * or all of the window
  50. *
  51. * See SMOPT_REFRESH_* defines
  52. * @var integer
  53. */
  54. var $refresh_level;
  55. /**
  56. * Specifies the size of certain input items
  57. *
  58. * See SMOPT_SIZE_* defines
  59. * @var integer
  60. */
  61. var $size;
  62. /**
  63. * Text that follows a text input or
  64. * select list input on the preferences page
  65. *
  66. * useful for indicating units, meanings of special values, etc.
  67. * @var string
  68. */
  69. var $trailing_text;
  70. /**
  71. * Indicates that the widget's "trailing text"
  72. * should be displayed in a smaller sized font
  73. *
  74. * @var boolean
  75. */
  76. var $trailing_text_small;
  77. /**
  78. * Indicates that the widget's "trailing text"
  79. * contains HTML and should not thus be
  80. * sanitized (encoded)
  81. *
  82. * @var boolean
  83. */
  84. var $trailing_text_is_html;
  85. /**
  86. * Text that overrides the "Yes" label for boolean
  87. * radio option widgets
  88. *
  89. * @var string
  90. */
  91. var $yes_text;
  92. /**
  93. * Text that overrides the "No" label for boolean
  94. * radio option widgets
  95. *
  96. * @var string
  97. */
  98. var $no_text;
  99. /**
  100. * Some widgets support more than one layout type
  101. *
  102. * @var int
  103. */
  104. var $layout_type;
  105. /**
  106. * Indicates if the Add widget should be included
  107. * with edit lists.
  108. *
  109. * @var boolean
  110. */
  111. var $use_add_widget;
  112. /**
  113. * Indicates if the Delete widget should be included
  114. * with edit lists.
  115. *
  116. * @var boolean
  117. */
  118. var $use_delete_widget;
  119. /**
  120. * associative array, treated the same as $possible_values
  121. * (see its documentation below), but usually expected to
  122. * have its first value contain a list of IMAP folders, an
  123. * array itself in the format as passed back by
  124. * sqimap_mailbox_list(). Used to display folder selector
  125. * for possible values of an associative edit list option
  126. * widget
  127. *
  128. * @since 1.5.2
  129. * @var array
  130. */
  131. var $poss_value_folders;
  132. /**
  133. * text displayed to the user
  134. *
  135. * Used with SMOPT_TYPE_COMMENT options
  136. * @var string
  137. */
  138. var $comment;
  139. /**
  140. * additional javascript or other widget attributes added to the
  141. * user input; must be an array where keys are attribute names
  142. * ("onclick", etc) and values are the attribute values.
  143. * @var array
  144. */
  145. var $aExtraAttribs;
  146. /**
  147. * script (usually Javascript) that will be placed after (outside of)
  148. * the INPUT tag
  149. * @var string
  150. */
  151. var $post_script;
  152. /**
  153. * The name of the Save Function for this option.
  154. * @var string
  155. */
  156. var $save_function;
  157. /* The various 'values' for this options. */
  158. /**
  159. * default/preselected value for this option
  160. * @var mixed
  161. */
  162. var $value;
  163. /**
  164. * new option value
  165. * @var mixed
  166. */
  167. var $new_value;
  168. /**
  169. * associative array, where each key is an actual input value
  170. * and the corresponding value is what is displayed to the user
  171. * for that list item in the drop-down list
  172. * @var array
  173. */
  174. var $possible_values;
  175. /**
  176. * disables html sanitizing.
  177. *
  178. * WARNING - don't use it, if user input is possible in option
  179. * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST.
  180. * @var bool
  181. */
  182. var $htmlencoded=false;
  183. /**
  184. * Controls folder list limits in SMOPT_TYPE_FLDRLIST and
  185. * SMOPT_TYPE_FLDRLIST_MULTI widgets as well as the optional
  186. * embedded folder lists provided for inputting values for
  187. * the SMOPT_TYPE_EDIT_LIST and SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE
  188. * :idgets.
  189. * See $flag argument in sqimap_mailbox_option_list() function.
  190. * @var string
  191. * @since 1.5.1
  192. */
  193. var $folder_filter='noselect';
  194. /**
  195. * Constructor (PHP5 style, required in some future version of PHP)
  196. * @param array $raw_option_array
  197. * @param string $name
  198. * @param string $caption
  199. * @param integer $type
  200. * @param integer $refresh_level
  201. * @param mixed $initial_value
  202. * @param array $possible_values
  203. * @param bool $htmlencoded
  204. */
  205. function __construct
  206. ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
  207. /* Set the basic stuff. */
  208. $this->raw_option_array = $raw_option_array;
  209. $this->name = $name;
  210. $this->caption = $caption;
  211. $this->caption_wrap = TRUE;
  212. $this->type = $type;
  213. $this->refresh_level = $refresh_level;
  214. $this->possible_values = $possible_values;
  215. $this->htmlencoded = $htmlencoded;
  216. $this->size = SMOPT_SIZE_NORMAL;
  217. $this->trailing_text = '';
  218. $this->trailing_text_small = FALSE;
  219. $this->trailing_text_is_html = FALSE;
  220. $this->yes_text = '';
  221. $this->no_text = '';
  222. $this->comment = '';
  223. $this->layout_type = 0;
  224. $this->use_add_widget = TRUE;
  225. $this->use_delete_widget = TRUE;
  226. $this->poss_value_folders = '';
  227. $this->aExtraAttribs = array();
  228. $this->post_script = '';
  229. //Check for a current value.
  230. if (isset($GLOBALS[$name])) {
  231. $this->value = $GLOBALS[$name];
  232. } else if (!empty($initial_value)) {
  233. $this->value = $initial_value;
  234. } else {
  235. $this->value = '';
  236. }
  237. /* Check for a new value. */
  238. if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
  239. $this->new_value = NULL;
  240. }
  241. /* Set the default save function. */
  242. if ($type != SMOPT_TYPE_HIDDEN
  243. && $type != SMOPT_TYPE_INFO
  244. && $type != SMOPT_TYPE_COMMENT) {
  245. $this->save_function = SMOPT_SAVE_DEFAULT;
  246. } else {
  247. $this->save_function = SMOPT_SAVE_NOOP;
  248. }
  249. }
  250. /**
  251. * Constructor (PHP4 style, kept for compatibility reasons)
  252. * @param array $raw_option_array
  253. * @param string $name
  254. * @param string $caption
  255. * @param integer $type
  256. * @param integer $refresh_level
  257. * @param mixed $initial_value
  258. * @param array $possible_values
  259. * @param bool $htmlencoded
  260. */
  261. function SquirrelOption
  262. ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
  263. self::__construct($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value, $possible_values, $htmlencoded);
  264. }
  265. /** Convenience function that identifies which types of
  266. widgets are stored as (serialized) array values. */
  267. function is_multiple_valued() {
  268. return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI
  269. || $this->type == SMOPT_TYPE_STRLIST_MULTI
  270. || $this->type == SMOPT_TYPE_EDIT_LIST
  271. || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE);
  272. }
  273. /**
  274. * Set the value for this option.
  275. * @param mixed $value
  276. */
  277. function setValue($value) {
  278. $this->value = $value;
  279. }
  280. /**
  281. * Set the new value for this option.
  282. * @param mixed $new_value
  283. */
  284. function setNewValue($new_value) {
  285. $this->new_value = $new_value;
  286. }
  287. /**
  288. * Set whether the caption is allowed to wrap for this option.
  289. * @param boolean $caption_wrap
  290. */
  291. function setCaptionWrap($caption_wrap) {
  292. $this->caption_wrap = $caption_wrap;
  293. }
  294. /**
  295. * Set the size for this option.
  296. * @param integer $size
  297. */
  298. function setSize($size) {
  299. $this->size = $size;
  300. }
  301. /**
  302. * Set the trailing_text for this option.
  303. * @param string $trailing_text
  304. */
  305. function setTrailingText($trailing_text) {
  306. $this->trailing_text = $trailing_text;
  307. }
  308. /**
  309. * Set the trailing_text_small for this option.
  310. * @param boolean $trailing_text_small
  311. */
  312. function setTrailingTextSmall($trailing_text_small) {
  313. $this->trailing_text_small = $trailing_text_small;
  314. }
  315. /**
  316. * Set the trailing_text_is_html for this option.
  317. * @param boolean $trailing_text_is_html
  318. */
  319. function setTrailingTextIsHtml($trailing_text_is_html) {
  320. $this->trailing_text_is_html = $trailing_text_is_html;
  321. }
  322. /**
  323. * Set the yes_text for this option.
  324. * @param string $yes_text
  325. */
  326. function setYesText($yes_text) {
  327. $this->yes_text = $yes_text;
  328. }
  329. /**
  330. * Set the no_text for this option.
  331. * @param string $no_text
  332. */
  333. function setNoText($no_text) {
  334. $this->no_text = $no_text;
  335. }
  336. /* Set the "use add widget" value for this option. */
  337. function setUseAddWidget($use_add_widget) {
  338. $this->use_add_widget = $use_add_widget;
  339. }
  340. /* Set the "use delete widget" value for this option. */
  341. function setUseDeleteWidget($use_delete_widget) {
  342. $this->use_delete_widget = $use_delete_widget;
  343. }
  344. /* Set the "poss value folders" value for this option.
  345. See the associative edit list widget, which uses this
  346. to offer folder list selection for the values */
  347. function setPossValueFolders($poss_value_folders) {
  348. $this->poss_value_folders = $poss_value_folders;
  349. }
  350. /**
  351. * Set the layout type for this option.
  352. * @param int $layout_type
  353. */
  354. function setLayoutType($layout_type) {
  355. $this->layout_type = $layout_type;
  356. }
  357. /**
  358. * Set the comment for this option.
  359. * @param string $comment
  360. */
  361. function setComment($comment) {
  362. $this->comment = $comment;
  363. }
  364. /**
  365. * Set the extra attributes for this option.
  366. * @param array $aExtraAttribs
  367. */
  368. function setExtraAttributes($aExtraAttribs) {
  369. $this->aExtraAttribs = $aExtraAttribs;
  370. }
  371. /**
  372. * Set the "post script" for this option.
  373. * @param string $post_script
  374. */
  375. function setPostScript($post_script) {
  376. $this->post_script = $post_script;
  377. }
  378. /**
  379. * Set the save function for this option.
  380. * @param string $save_function
  381. */
  382. function setSaveFunction($save_function) {
  383. $this->save_function = $save_function;
  384. }
  385. /**
  386. * Set the folder_filter for this option.
  387. * @param string $folder_filter
  388. * @since 1.5.1
  389. */
  390. function setFolderFilter($folder_filter) {
  391. $this->folder_filter = $folder_filter;
  392. }
  393. /**
  394. * Creates fields on option pages according to option type
  395. *
  396. * This is the function that calls all other createWidget* functions.
  397. *
  398. * @return string The formated option field
  399. *
  400. */
  401. function createWidget() {
  402. global $color;
  403. // Use new value if available
  404. if (!is_null($this->new_value)) {
  405. $tempValue = $this->value;
  406. $this->value = $this->new_value;
  407. }
  408. /* Get the widget for this option type. */
  409. switch ($this->type) {
  410. case SMOPT_TYPE_PASSWORD:
  411. $result = $this->createWidget_String(TRUE);
  412. break;
  413. case SMOPT_TYPE_STRING:
  414. $result = $this->createWidget_String();
  415. break;
  416. case SMOPT_TYPE_STRLIST:
  417. $result = $this->createWidget_StrList();
  418. break;
  419. case SMOPT_TYPE_TEXTAREA:
  420. $result = $this->createWidget_TextArea();
  421. break;
  422. case SMOPT_TYPE_INTEGER:
  423. $result = $this->createWidget_Integer();
  424. break;
  425. case SMOPT_TYPE_FLOAT:
  426. $result = $this->createWidget_Float();
  427. break;
  428. case SMOPT_TYPE_BOOLEAN:
  429. $result = $this->createWidget_Boolean();
  430. break;
  431. case SMOPT_TYPE_BOOLEAN_CHECKBOX:
  432. $result = $this->createWidget_Boolean(TRUE);
  433. break;
  434. case SMOPT_TYPE_BOOLEAN_RADIO:
  435. $result = $this->createWidget_Boolean(FALSE);
  436. break;
  437. case SMOPT_TYPE_HIDDEN:
  438. $result = $this->createWidget_Hidden();
  439. break;
  440. case SMOPT_TYPE_COMMENT:
  441. $result = $this->createWidget_Comment();
  442. break;
  443. case SMOPT_TYPE_FLDRLIST:
  444. $result = $this->createWidget_FolderList();
  445. break;
  446. case SMOPT_TYPE_FLDRLIST_MULTI:
  447. $result = $this->createWidget_FolderList(TRUE);
  448. break;
  449. case SMOPT_TYPE_EDIT_LIST:
  450. $result = $this->createWidget_EditList();
  451. break;
  452. case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE:
  453. $result = $this->createWidget_EditListAssociative();
  454. break;
  455. case SMOPT_TYPE_STRLIST_MULTI:
  456. $result = $this->createWidget_StrList(TRUE);
  457. break;
  458. case SMOPT_TYPE_STRLIST_RADIO:
  459. $result = $this->createWidget_StrList(FALSE, TRUE);
  460. break;
  461. case SMOPT_TYPE_SUBMIT:
  462. $result = $this->createWidget_Submit();
  463. break;
  464. case SMOPT_TYPE_INFO:
  465. $result = $this->createWidget_Info();
  466. break;
  467. default:
  468. error_box (
  469. sprintf(_("Option Type '%s' Not Found"), $this->type)
  470. );
  471. }
  472. /* Add the "post script" for this option. */
  473. $result .= $this->post_script;
  474. // put correct value back if need be
  475. if (!is_null($this->new_value)) {
  476. $this->value = $tempValue;
  477. }
  478. /* Now, return the created widget. */
  479. return $result;
  480. }
  481. /**
  482. * Creates info block
  483. * @return string html formated output
  484. */
  485. function createWidget_Info() {
  486. // return sq_htmlspecialchars($this->value);
  487. // like COMMENT, allow HTML here
  488. return $this->value;
  489. }
  490. /**
  491. * Create string field
  492. *
  493. * @param boolean $password When TRUE, the text in the input
  494. * widget will be obscured (OPTIONAL;
  495. * default = FALSE).
  496. *
  497. * @return string html formated option field
  498. *
  499. */
  500. function createWidget_String($password=FALSE) {
  501. switch ($this->size) {
  502. case SMOPT_SIZE_TINY:
  503. $width = 5;
  504. break;
  505. case SMOPT_SIZE_SMALL:
  506. $width = 12;
  507. break;
  508. case SMOPT_SIZE_LARGE:
  509. $width = 38;
  510. break;
  511. case SMOPT_SIZE_HUGE:
  512. $width = 50;
  513. break;
  514. case SMOPT_SIZE_NORMAL:
  515. default:
  516. $width = 25;
  517. }
  518. //TODO: might be better to have a separate template file for all widgets, because then the layout of the widget and the "trailing text" can be customized - they are still hard coded here (also, we have <small> tags here; don't want HTML here!)
  519. if ($password)
  520. return addPwField('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
  521. else
  522. return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . ' ' . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
  523. }
  524. /**
  525. * Create selection box or radio button group
  526. *
  527. * When $this->htmlencoded is TRUE, the keys and values in
  528. * $this->possible_values are assumed to be display-safe.
  529. * Use with care!
  530. *
  531. * Note that when building radio buttons instead of a select
  532. * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
  533. * radio buttons will be output one after another without
  534. * linebreaks between them. Otherwise, each radio button
  535. * goes on a line of its own.
  536. *
  537. * @param boolean $multiple_select When TRUE, the select widget
  538. * will allow multiple selections
  539. * (OPTIONAL; default is FALSE
  540. * (single select list))
  541. * @param boolean $radio_buttons When TRUE, the widget will
  542. * instead be built as a group
  543. * of radio buttons (and
  544. * $multiple_select will be
  545. * forced to FALSE) (OPTIONAL;
  546. * default is FALSE (select widget))
  547. *
  548. * @return string html formated selection box or radio buttons
  549. *
  550. */
  551. function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
  552. //FIXME: Currently, $this->htmlencoded is ignored here -- was removed when changing to template-based output; a fix is available as part of proposed centralized sanitizing patch
  553. // radio buttons instead of select widget?
  554. //
  555. if ($radio_buttons) {
  556. global $br, $nbsp;
  557. $result = '';
  558. foreach ($this->possible_values as $real_value => $disp_value) {
  559. $result .= addRadioBox('new_' . $this->name, ($this->value == $real_value), $real_value, array_merge(array('id' => 'new_' . $this->name . '_' . $real_value), $this->aExtraAttribs)) . $nbsp . create_label($disp_value, 'new_' . $this->name . '_' . $real_value);
  560. if ($this->size != SMOPT_SIZE_TINY)
  561. $result .= $br;
  562. }
  563. return $result;
  564. }
  565. // everything below applies to select widgets
  566. //
  567. switch ($this->size) {
  568. //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
  569. case SMOPT_SIZE_TINY:
  570. $height = 3;
  571. break;
  572. case SMOPT_SIZE_SMALL:
  573. $height = 8;
  574. break;
  575. case SMOPT_SIZE_LARGE:
  576. $height = 15;
  577. break;
  578. case SMOPT_SIZE_HUGE:
  579. $height = 25;
  580. break;
  581. case SMOPT_SIZE_NORMAL:
  582. default:
  583. $height = 5;
  584. }
  585. return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
  586. }
  587. /**
  588. * Create folder selection box
  589. *
  590. * @param boolean $multiple_select When TRUE, the select widget
  591. * will allow multiple selections
  592. * (OPTIONAL; default is FALSE
  593. * (single select list))
  594. *
  595. * @return string html formated selection box
  596. *
  597. */
  598. function createWidget_FolderList($multiple_select=FALSE) {
  599. switch ($this->size) {
  600. //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
  601. case SMOPT_SIZE_TINY:
  602. $height = 3;
  603. break;
  604. case SMOPT_SIZE_SMALL:
  605. $height = 8;
  606. break;
  607. case SMOPT_SIZE_LARGE:
  608. $height = 15;
  609. break;
  610. case SMOPT_SIZE_HUGE:
  611. $height = 25;
  612. break;
  613. case SMOPT_SIZE_NORMAL:
  614. default:
  615. $height = 5;
  616. }
  617. // possible values might include a nested array of
  618. // possible values (list of folders)
  619. //
  620. $option_list = array();
  621. foreach ($this->possible_values as $value => $text) {
  622. // list of folders (boxes array)
  623. //
  624. if (is_array($text)) {
  625. $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
  626. // just one option here
  627. //
  628. } else {
  629. $option_list = array_merge($option_list, array($value => $text));
  630. }
  631. }
  632. if (empty($option_list))
  633. $option_list = array('ignore' => _("unavailable"));
  634. return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
  635. }
  636. /**
  637. * Creates textarea
  638. * @return string html formated textarea field
  639. */
  640. function createWidget_TextArea() {
  641. switch ($this->size) {
  642. case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
  643. case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
  644. case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
  645. case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
  646. case SMOPT_SIZE_NORMAL:
  647. default: $rows = 5; $cols = 50;
  648. }
  649. return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
  650. }
  651. /**
  652. * Creates field for integer
  653. *
  654. * Difference from createWidget_String is visible only when javascript is enabled
  655. * @return string html formated option field
  656. */
  657. function createWidget_Integer() {
  658. // add onChange javascript handler to a regular string widget
  659. // which will strip out all non-numeric chars
  660. if (checkForJavascript())
  661. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  662. . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
  663. . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
  664. . 'this.value=newVal;';
  665. return $this->createWidget_String();
  666. }
  667. /**
  668. * Creates field for floating number
  669. * Difference from createWidget_String is visible only when javascript is enabled
  670. * @return string html formated option field
  671. */
  672. function createWidget_Float() {
  673. // add onChange javascript handler to a regular string widget
  674. // which will strip out all non-numeric (period also OK) chars
  675. if (checkForJavascript())
  676. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  677. . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
  678. . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
  679. . 'newVal += origVal.charAt(i); } this.value=newVal;';
  680. return $this->createWidget_String();
  681. }
  682. /**
  683. * Create boolean widget
  684. *
  685. * When creating Yes/No radio buttons, the "yes_text"
  686. * and "no_text" option attributes are used to override
  687. * the typical "Yes" and "No" text.
  688. *
  689. * @param boolean $checkbox When TRUE, the widget will be
  690. * constructed as a checkbox,
  691. * otherwise it will be a set of
  692. * Yes/No radio buttons (OPTIONAL;
  693. * default is TRUE (checkbox)).
  694. *
  695. * @return string html formated boolean widget
  696. *
  697. */
  698. function createWidget_Boolean($checkbox=TRUE) {
  699. global $oTemplate, $nbsp;
  700. // checkbox...
  701. //
  702. if ($checkbox) {
  703. //TODO: Why isn't trailing_text being sanitized with sm_encode_special_chars()??? If this is a bug, add that, then add the option to display unsanitized if $this->trailing_text_is_html is enabled
  704. $result = addCheckbox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name), $this->aExtraAttribs)) . $nbsp . create_label(($this->trailing_text_small ? '<small>' : '') . $this->trailing_text . ($this->trailing_text_small ? '</small>' : ''), 'new_' . $this->name);
  705. }
  706. // radio buttons...
  707. //
  708. else {
  709. /* Build the yes choice. */
  710. $yes_option = addRadioBox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name . '_yes'), $this->aExtraAttribs)) . $nbsp . create_label((!empty($this->yes_text) ? $this->yes_text : _("Yes")), 'new_' . $this->name . '_yes');
  711. /* Build the no choice. */
  712. $no_option = addRadioBox('new_' . $this->name, ($this->value == SMPREF_NO), SMPREF_NO, array_merge(array('id' => 'new_' . $this->name . '_no'), $this->aExtraAttribs)) . $nbsp . create_label((!empty($this->no_text) ? $this->no_text : _("No")), 'new_' . $this->name . '_no');
  713. /* Build the combined "boolean widget". */
  714. $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
  715. }
  716. return ($result);
  717. }
  718. /**
  719. * Creates hidden field
  720. * @return string html formated hidden input field
  721. */
  722. function createWidget_Hidden() {
  723. return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
  724. }
  725. /**
  726. * Creates comment
  727. * @return string comment
  728. */
  729. function createWidget_Comment() {
  730. $result = $this->comment;
  731. return ($result);
  732. }
  733. /**
  734. * Creates a (non-associative) edit list
  735. *
  736. * Note that multiple layout types are supported for this widget.
  737. * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
  738. * constants.
  739. *
  740. * @return string html formated list of edit fields and
  741. * their associated controls
  742. */
  743. function createWidget_EditList() {
  744. global $oTemplate;
  745. switch ($this->size) {
  746. case SMOPT_SIZE_TINY:
  747. $height = 3;
  748. break;
  749. case SMOPT_SIZE_SMALL:
  750. $height = 8;
  751. break;
  752. case SMOPT_SIZE_MEDIUM:
  753. $height = 15;
  754. break;
  755. case SMOPT_SIZE_LARGE:
  756. $height = 25;
  757. break;
  758. case SMOPT_SIZE_HUGE:
  759. $height = 40;
  760. break;
  761. case SMOPT_SIZE_NORMAL:
  762. default:
  763. $height = 5;
  764. }
  765. if (empty($this->possible_values)) $this->possible_values = array();
  766. if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
  767. //FIXME: $this->aExtraAttribs probably should only be used in one place
  768. $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs));
  769. $oTemplate->assign('use_input_widget', $this->use_add_widget);
  770. $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
  771. $oTemplate->assign('trailing_text', $this->trailing_text);
  772. $oTemplate->assign('trailing_text_small', $this->trailing_text_small);
  773. $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html);
  774. $oTemplate->assign('possible_values', $this->possible_values);
  775. $oTemplate->assign('current_value', $this->value);
  776. $oTemplate->assign('select_widget', addSelect('new_' . $this->name, $this->possible_values, $this->value, FALSE, !checkForJavascript() ? $this->aExtraAttribs : array_merge(array('onchange' => 'if (typeof(window.addinput_' . $this->name . ') == \'undefined\') { var f = document.forms.length; var i = 0; var pos = -1; while( pos == -1 && i < f ) { var e = document.forms[i].elements.length; var j = 0; while( pos == -1 && j < e ) { if ( document.forms[i].elements[j].type == \'text\' && document.forms[i].elements[j].name == \'add_' . $this->name . '\' ) { pos = j; i=f-1; j=e-1; } j++; } i++; } if( pos >= 0 ) { window.addinput_' . $this->name . ' = document.forms[i-1].elements[pos]; } } for (x = 0; x < this.length; x++) { if (this.options[x].selected) { window.addinput_' . $this->name . '.value = this.options[x].text; break; } }'), $this->aExtraAttribs), TRUE, $height));
  777. // NOTE: i=f-1; j=e-1 is in lieu of break 2
  778. $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
  779. $oTemplate->assign('name', $this->name);
  780. switch ($this->layout_type) {
  781. case SMOPT_EDIT_LIST_LAYOUT_SELECT:
  782. return $oTemplate->fetch('edit_list_widget.tpl');
  783. case SMOPT_EDIT_LIST_LAYOUT_LIST:
  784. return $oTemplate->fetch('edit_list_widget_list_style.tpl');
  785. default:
  786. error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $this->layout_type));
  787. }
  788. }
  789. /**
  790. * Creates an associative edit list
  791. *
  792. * Note that multiple layout types are supported for this widget.
  793. * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
  794. * constants.
  795. *
  796. * @return string html formated list of edit fields and
  797. * their associated controls
  798. */
  799. function createWidget_EditListAssociative() {
  800. global $oTemplate;
  801. switch ($this->size) {
  802. case SMOPT_SIZE_TINY:
  803. $height = 3;
  804. break;
  805. case SMOPT_SIZE_SMALL:
  806. $height = 8;
  807. break;
  808. case SMOPT_SIZE_MEDIUM:
  809. $height = 15;
  810. break;
  811. case SMOPT_SIZE_LARGE:
  812. $height = 25;
  813. break;
  814. case SMOPT_SIZE_HUGE:
  815. $height = 40;
  816. break;
  817. case SMOPT_SIZE_NORMAL:
  818. default:
  819. $height = 5;
  820. }
  821. // ensure correct format of current value(s)
  822. //
  823. if (empty($this->possible_values)) $this->possible_values = array();
  824. if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
  825. $oTemplate->assign('name', $this->name);
  826. $oTemplate->assign('current_value', $this->value);
  827. $oTemplate->assign('possible_values', $this->possible_values);
  828. $oTemplate->assign('poss_value_folders', $this->poss_value_folders);
  829. $oTemplate->assign('folder_filter', $this->folder_filter);
  830. $oTemplate->assign('use_input_widget', $this->use_add_widget);
  831. $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
  832. $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
  833. //FIXME: $this->aExtraAttribs probably should only be used in one place
  834. $oTemplate->assign('input_key_widget', addInput('add_' . $this->name . '_key', '', 22, 0, $this->aExtraAttribs));
  835. $oTemplate->assign('input_value_widget', addInput('add_' . $this->name . '_value', '', 12, 0, $this->aExtraAttribs));
  836. $oTemplate->assign('select_height', $height);
  837. $oTemplate->assign('aAttribs', $this->aExtraAttribs);
  838. $oTemplate->assign('trailing_text', $this->trailing_text);
  839. $oTemplate->assign('trailing_text_small', $this->trailing_text_small);
  840. $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html);
  841. switch ($this->layout_type) {
  842. case SMOPT_EDIT_LIST_LAYOUT_SELECT:
  843. return $oTemplate->fetch('edit_list_associative_widget.tpl');
  844. case SMOPT_EDIT_LIST_LAYOUT_LIST:
  845. return $oTemplate->fetch('edit_list_associative_widget_list_style.tpl');
  846. default:
  847. error_box(sprintf(_("Associative Edit List Layout Type '%s' Not Found"), $this->layout_type));
  848. }
  849. }
  850. /**
  851. * Creates a submit button
  852. *
  853. * @return string html formated submit button widget
  854. *
  855. */
  856. function createWidget_Submit() {
  857. return addSubmit($this->comment, $this->name, $this->aExtraAttribs) . ($this->trailing_text_small ? '<small>' : '') . ($this->trailing_text_is_html ? $this->trailing_text : sm_encode_html_special_chars($this->trailing_text)) . ($this->trailing_text_small ? '</small>' : '');
  858. }
  859. /**
  860. *
  861. */
  862. function save() {
  863. $function = $this->save_function;
  864. $function($this);
  865. }
  866. /**
  867. *
  868. */
  869. function changed() {
  870. // edit lists have a lot going on, so we'll always process them
  871. //
  872. if ($this->type == SMOPT_TYPE_EDIT_LIST
  873. || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE)
  874. return TRUE;
  875. return ($this->value != $this->new_value);
  876. }
  877. } /* End of SquirrelOption class*/
  878. /**
  879. * Saves the option value (this is the default save function
  880. * unless overridden by the user)
  881. *
  882. * @param object $option object that holds option name and new_value
  883. */
  884. function save_option($option) {
  885. // Can't save the pref if we don't have the username
  886. //
  887. if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  888. return;
  889. }
  890. // if the widget is a selection list, make sure the new
  891. // value is actually in the selection list and is not an
  892. // injection attack
  893. //
  894. if ($option->type == SMOPT_TYPE_STRLIST
  895. && !array_key_exists($option->new_value, $option->possible_values))
  896. return;
  897. // all other widgets except TEXTAREAs should never be allowed to have newlines
  898. //
  899. else if ($option->type != SMOPT_TYPE_TEXTAREA)
  900. $option->new_value = str_replace(array("\r", "\n"), '', $option->new_value);
  901. global $data_dir;
  902. // edit lists: first add new elements to list, then
  903. // remove any selected ones (note that we must add
  904. // before deleting because the javascript that populates
  905. // the "add" textbox when selecting items in the list
  906. // (for deletion))
  907. //
  908. if ($option->type == SMOPT_TYPE_EDIT_LIST) {
  909. if (empty($option->possible_values)) $option->possible_values = array();
  910. if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
  911. // add element if given
  912. //
  913. if ((isset($option->use_add_widget) && $option->use_add_widget)
  914. && sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
  915. $new_element = trim($new_element);
  916. if (!empty($new_element)
  917. && !in_array($new_element, $option->possible_values))
  918. $option->possible_values[] = $new_element;
  919. }
  920. // delete selected elements if needed
  921. //
  922. if ((isset($option->use_delete_widget) && $option->use_delete_widget)
  923. && is_array($option->new_value)
  924. && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST))
  925. $option->possible_values = array_diff($option->possible_values, $option->new_value);
  926. // save full list (stored in "possible_values")
  927. //
  928. setPref($data_dir, $username, $option->name, serialize($option->possible_values));
  929. // associative edit lists are handled similar to
  930. // non-associative ones
  931. //
  932. } else if ($option->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE) {
  933. if (empty($option->possible_values)) $option->possible_values = array();
  934. if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
  935. // add element if given
  936. //
  937. $new_element_key = '';
  938. $new_element_value = '';
  939. $retrieve_key = sqGetGlobalVar('add_' . $option->name . '_key', $new_element_key, SQ_POST);
  940. $retrieve_value = sqGetGlobalVar('add_' . $option->name . '_value', $new_element_value, SQ_POST);
  941. if ((isset($option->use_add_widget) && $option->use_add_widget)
  942. && ($retrieve_key || $retrieve_value)) {
  943. $new_element_key = trim($new_element_key);
  944. $new_element_value = trim($new_element_value);
  945. if ($option->poss_value_folders && empty($new_element_key))
  946. $new_element_value = '';
  947. if (!empty($new_element_key) || !empty($new_element_value)) {
  948. if (empty($new_element_key)) $new_element_key = '0';
  949. $option->possible_values[$new_element_key] = $new_element_value;
  950. }
  951. }
  952. // delete selected elements if needed
  953. //
  954. if ((isset($option->use_delete_widget) && $option->use_delete_widget)
  955. && is_array($option->new_value)
  956. && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST)) {
  957. if ($option->layout_type == SMOPT_EDIT_LIST_LAYOUT_SELECT) {
  958. foreach ($option->new_value as $key)
  959. unset($option->possible_values[urldecode($key)]);
  960. }
  961. else
  962. $option->possible_values = array_diff($option->possible_values, $option->new_value);
  963. }
  964. // save full list (stored in "possible_values")
  965. //
  966. setPref($data_dir, $username, $option->name, serialize($option->possible_values));
  967. // Certain option types need to be serialized because
  968. // they are not scalar
  969. //
  970. } else if ($option->is_multiple_valued())
  971. setPref($data_dir, $username, $option->name, serialize($option->new_value));
  972. // Checkboxes, when unchecked, don't submit anything in
  973. // the POST, so set to SMPREF_OFF if not found
  974. //
  975. else if (($option->type == SMOPT_TYPE_BOOLEAN
  976. || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
  977. && empty($option->new_value))
  978. setPref($data_dir, $username, $option->name, SMPREF_OFF);
  979. // For integer fields, make sure we only have digits...
  980. // We'll be nice and instead of just converting to an integer,
  981. // we'll physically remove each non-digit in the string.
  982. //
  983. else if ($option->type == SMOPT_TYPE_INTEGER) {
  984. $option->new_value = preg_replace('/[^0-9]/', '', $option->new_value);
  985. setPref($data_dir, $username, $option->name, $option->new_value);
  986. }
  987. else
  988. setPref($data_dir, $username, $option->name, $option->new_value);
  989. // if a checkbox or multi select is zeroed/cleared out, it
  990. // needs to have an empty value pushed into its "new_value" slot
  991. //
  992. if (($option->type == SMOPT_TYPE_STRLIST_MULTI
  993. || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
  994. && is_null($option->new_value))
  995. $option->new_value = '';
  996. }
  997. /**
  998. * save function that does not save
  999. * @param object $option
  1000. */
  1001. function save_option_noop($option) {
  1002. /* Do nothing here... */
  1003. }
  1004. /**
  1005. * Create hidden 'optpage' input field with value set by argument
  1006. * @param string $optpage identification of option page
  1007. * @return string html formated hidden input field
  1008. */
  1009. function create_optpage_element($optpage) {
  1010. return addHidden('optpage', $optpage);
  1011. }
  1012. /**
  1013. * Create hidden 'optmode' input field with value set by argument
  1014. * @param string $optmode
  1015. * @return string html formated hidden input field
  1016. */
  1017. function create_optmode_element($optmode) {
  1018. return addHidden('optmode', $optmode);
  1019. }
  1020. /**
  1021. * @param array $optgrps
  1022. * @param array $optvals
  1023. * @return array
  1024. */
  1025. function create_option_groups($optgrps, $optvals) {
  1026. /* Build a simple array with which to start. */
  1027. $result = array();
  1028. /* Create option group for each option group name. */
  1029. foreach ($optgrps as $grpkey => $grpname) {
  1030. $result[$grpkey] = array();
  1031. $result[$grpkey]['name'] = $grpname;
  1032. $result[$grpkey]['options'] = array();
  1033. }
  1034. /* Create a new SquirrelOption for each set of option values. */
  1035. foreach ($optvals as $grpkey => $grpopts) {
  1036. foreach ($grpopts as $optset) {
  1037. /* Create a new option with all values given. */
  1038. $next_option = new SquirrelOption(
  1039. $optset,
  1040. $optset['name'],
  1041. $optset['caption'],
  1042. $optset['type'],
  1043. (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  1044. (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
  1045. (isset($optset['posvals']) ? $optset['posvals'] : ''),
  1046. (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
  1047. );
  1048. /* If provided, set if the caption is allowed to wrap for this option. */
  1049. if (isset($optset['caption_wrap'])) {
  1050. $next_option->setCaptionWrap($optset['caption_wrap']);
  1051. }
  1052. /* If provided, set the size for this option. */
  1053. if (isset($optset['size'])) {
  1054. $next_option->setSize($optset['size']);
  1055. }
  1056. /* If provided, set the trailing_text for this option. */
  1057. if (isset($optset['trailing_text'])) {
  1058. $next_option->setTrailingText($optset['trailing_text']);
  1059. }
  1060. /* If provided, set the trailing_text_small for this option. */
  1061. if (isset($optset['trailing_text_small'])) {
  1062. $next_option->setTrailingTextSmall($optset['trailing_text_small']);
  1063. }
  1064. /* If provided, set the trailing_text_is_html for this option. */
  1065. if (isset($optset['trailing_text_is_html'])) {
  1066. $next_option->setTrailingTextIsHtml($optset['trailing_text_is_html']);
  1067. }
  1068. /* If provided, set the yes_text for this option. */
  1069. if (isset($optset['yes_text'])) {
  1070. $next_option->setYesText($optset['yes_text']);
  1071. }
  1072. /* If provided, set the no_text for this option. */
  1073. if (isset($optset['no_text'])) {
  1074. $next_option->setNoText($optset['no_text']);
  1075. }
  1076. /* If provided, set the poss_value_folders value for this option. */
  1077. if (isset($optset['poss_value_folders'])) {
  1078. $next_option->setPossValueFolders($optset['poss_value_folders']);
  1079. }
  1080. /* If provided, set the layout type for this option. */
  1081. if (isset($optset['layout_type'])) {
  1082. $next_option->setLayoutType($optset['layout_type']);
  1083. }
  1084. /* If provided, set the use_add_widget value for this option. */
  1085. if (isset($optset['use_add_widget'])) {
  1086. $next_option->setUseAddWidget($optset['use_add_widget']);
  1087. }
  1088. /* If provided, set the use_delete_widget value for this option. */
  1089. if (isset($optset['use_delete_widget'])) {
  1090. $next_option->setUseDeleteWidget($optset['use_delete_widget']);
  1091. }
  1092. /* If provided, set the comment for this option. */
  1093. if (isset($optset['comment'])) {
  1094. $next_option->setComment($optset['comment']);
  1095. }
  1096. /* If provided, set the save function for this option. */
  1097. if (isset($optset['save'])) {
  1098. $next_option->setSaveFunction($optset['save']);
  1099. }
  1100. /* If provided, set the extra attributes for this option. */
  1101. if (isset($optset['extra_attributes'])) {
  1102. $next_option->setExtraAttributes($optset['extra_attributes']);
  1103. }
  1104. /* If provided, set the "post script" for this option. */
  1105. if (isset($optset['post_script'])) {
  1106. $next_option->setPostScript($optset['post_script']);
  1107. }
  1108. /* If provided, set the folder_filter for this option. */
  1109. if (isset($optset['folder_filter'])) {
  1110. $next_option->setFolderFilter($optset['folder_filter']);
  1111. }
  1112. /* Add this option to the option array. */
  1113. $result[$grpkey]['options'][] = $next_option;
  1114. }
  1115. }
  1116. /* Return our resulting array. */
  1117. return ($result);
  1118. }