options.php 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. <?php
  2. /**
  3. * options.php
  4. *
  5. * Functions needed to display the options pages.
  6. *
  7. * @copyright 1999-2025 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. // TODO: This code should be something more like the following, but who knows what would break if it was changed at this point
  233. // } else if (initial_value !== '') {
  234. } else if (!empty($initial_value)) {
  235. $this->value = $initial_value;
  236. } else {
  237. $this->value = '';
  238. }
  239. /* Check for a new value. */
  240. if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) {
  241. $this->new_value = NULL;
  242. }
  243. /* Set the default save function. */
  244. if ($type != SMOPT_TYPE_HIDDEN
  245. && $type != SMOPT_TYPE_INFO
  246. && $type != SMOPT_TYPE_COMMENT) {
  247. $this->save_function = SMOPT_SAVE_DEFAULT;
  248. } else {
  249. $this->save_function = SMOPT_SAVE_NOOP;
  250. }
  251. }
  252. /**
  253. * Constructor (PHP4 style, kept for compatibility reasons)
  254. * @param array $raw_option_array
  255. * @param string $name
  256. * @param string $caption
  257. * @param integer $type
  258. * @param integer $refresh_level
  259. * @param mixed $initial_value
  260. * @param array $possible_values
  261. * @param bool $htmlencoded
  262. */
  263. function SquirrelOption
  264. ($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) {
  265. self::__construct($raw_option_array, $name, $caption, $type, $refresh_level, $initial_value, $possible_values, $htmlencoded);
  266. }
  267. /** Convenience function that identifies which types of
  268. widgets are stored as (serialized) array values. */
  269. function is_multiple_valued() {
  270. return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI
  271. || $this->type == SMOPT_TYPE_STRLIST_MULTI
  272. || $this->type == SMOPT_TYPE_EDIT_LIST
  273. || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE);
  274. }
  275. /**
  276. * Set the value for this option.
  277. * @param mixed $value
  278. */
  279. function setValue($value) {
  280. $this->value = $value;
  281. }
  282. /**
  283. * Set the new value for this option.
  284. * @param mixed $new_value
  285. */
  286. function setNewValue($new_value) {
  287. $this->new_value = $new_value;
  288. }
  289. /**
  290. * Set whether the caption is allowed to wrap for this option.
  291. * @param boolean $caption_wrap
  292. */
  293. function setCaptionWrap($caption_wrap) {
  294. $this->caption_wrap = $caption_wrap;
  295. }
  296. /**
  297. * Set the size for this option.
  298. * @param integer $size
  299. */
  300. function setSize($size) {
  301. $this->size = $size;
  302. }
  303. /**
  304. * Set the trailing_text for this option.
  305. * @param string $trailing_text
  306. */
  307. function setTrailingText($trailing_text) {
  308. $this->trailing_text = $trailing_text;
  309. }
  310. /**
  311. * Set the trailing_text_small for this option.
  312. * @param boolean $trailing_text_small
  313. */
  314. function setTrailingTextSmall($trailing_text_small) {
  315. $this->trailing_text_small = $trailing_text_small;
  316. }
  317. /**
  318. * Set the trailing_text_is_html for this option.
  319. * @param boolean $trailing_text_is_html
  320. */
  321. function setTrailingTextIsHtml($trailing_text_is_html) {
  322. $this->trailing_text_is_html = $trailing_text_is_html;
  323. }
  324. /**
  325. * Set the yes_text for this option.
  326. * @param string $yes_text
  327. */
  328. function setYesText($yes_text) {
  329. $this->yes_text = $yes_text;
  330. }
  331. /**
  332. * Set the no_text for this option.
  333. * @param string $no_text
  334. */
  335. function setNoText($no_text) {
  336. $this->no_text = $no_text;
  337. }
  338. /* Set the "use add widget" value for this option. */
  339. function setUseAddWidget($use_add_widget) {
  340. $this->use_add_widget = $use_add_widget;
  341. }
  342. /* Set the "use delete widget" value for this option. */
  343. function setUseDeleteWidget($use_delete_widget) {
  344. $this->use_delete_widget = $use_delete_widget;
  345. }
  346. /* Set the "poss value folders" value for this option.
  347. See the associative edit list widget, which uses this
  348. to offer folder list selection for the values */
  349. function setPossValueFolders($poss_value_folders) {
  350. $this->poss_value_folders = $poss_value_folders;
  351. }
  352. /**
  353. * Set the layout type for this option.
  354. * @param int $layout_type
  355. */
  356. function setLayoutType($layout_type) {
  357. $this->layout_type = $layout_type;
  358. }
  359. /**
  360. * Set the comment for this option.
  361. * @param string $comment
  362. */
  363. function setComment($comment) {
  364. $this->comment = $comment;
  365. }
  366. /**
  367. * Set the extra attributes for this option.
  368. * @param array $aExtraAttribs
  369. */
  370. function setExtraAttributes($aExtraAttribs) {
  371. $this->aExtraAttribs = $aExtraAttribs;
  372. }
  373. /**
  374. * Set the "post script" for this option.
  375. * @param string $post_script
  376. */
  377. function setPostScript($post_script) {
  378. $this->post_script = $post_script;
  379. }
  380. /**
  381. * Set the save function for this option.
  382. * @param string $save_function
  383. */
  384. function setSaveFunction($save_function) {
  385. $this->save_function = $save_function;
  386. }
  387. /**
  388. * Set the folder_filter for this option.
  389. * @param string $folder_filter
  390. * @since 1.5.1
  391. */
  392. function setFolderFilter($folder_filter) {
  393. $this->folder_filter = $folder_filter;
  394. }
  395. /**
  396. * Creates fields on option pages according to option type
  397. *
  398. * This is the function that calls all other createWidget* functions.
  399. *
  400. * @return string The formated option field
  401. *
  402. */
  403. function createWidget() {
  404. global $color;
  405. // Use new value if available
  406. if (!is_null($this->new_value)) {
  407. $tempValue = $this->value;
  408. $this->value = $this->new_value;
  409. }
  410. /* Get the widget for this option type. */
  411. switch ($this->type) {
  412. case SMOPT_TYPE_PASSWORD:
  413. $result = $this->createWidget_String(TRUE);
  414. break;
  415. case SMOPT_TYPE_STRING:
  416. $result = $this->createWidget_String();
  417. break;
  418. case SMOPT_TYPE_STRLIST:
  419. $result = $this->createWidget_StrList();
  420. break;
  421. case SMOPT_TYPE_TEXTAREA:
  422. $result = $this->createWidget_TextArea();
  423. break;
  424. case SMOPT_TYPE_INTEGER:
  425. $result = $this->createWidget_Integer();
  426. break;
  427. case SMOPT_TYPE_FLOAT:
  428. $result = $this->createWidget_Float();
  429. break;
  430. case SMOPT_TYPE_BOOLEAN:
  431. $result = $this->createWidget_Boolean();
  432. break;
  433. case SMOPT_TYPE_BOOLEAN_CHECKBOX:
  434. $result = $this->createWidget_Boolean(TRUE);
  435. break;
  436. case SMOPT_TYPE_BOOLEAN_RADIO:
  437. $result = $this->createWidget_Boolean(FALSE);
  438. break;
  439. case SMOPT_TYPE_HIDDEN:
  440. $result = $this->createWidget_Hidden();
  441. break;
  442. case SMOPT_TYPE_COMMENT:
  443. $result = $this->createWidget_Comment();
  444. break;
  445. case SMOPT_TYPE_FLDRLIST:
  446. $result = $this->createWidget_FolderList();
  447. break;
  448. case SMOPT_TYPE_FLDRLIST_MULTI:
  449. $result = $this->createWidget_FolderList(TRUE);
  450. break;
  451. case SMOPT_TYPE_EDIT_LIST:
  452. $result = $this->createWidget_EditList();
  453. break;
  454. case SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE:
  455. $result = $this->createWidget_EditListAssociative();
  456. break;
  457. case SMOPT_TYPE_STRLIST_MULTI:
  458. $result = $this->createWidget_StrList(TRUE);
  459. break;
  460. case SMOPT_TYPE_STRLIST_RADIO:
  461. $result = $this->createWidget_StrList(FALSE, TRUE);
  462. break;
  463. case SMOPT_TYPE_SUBMIT:
  464. $result = $this->createWidget_Submit();
  465. break;
  466. case SMOPT_TYPE_INFO:
  467. $result = $this->createWidget_Info();
  468. break;
  469. default:
  470. error_box (
  471. sprintf(_("Option Type '%s' Not Found"), $this->type)
  472. );
  473. }
  474. /* Add the "post script" for this option. */
  475. $result .= $this->post_script;
  476. // put correct value back if need be
  477. if (!is_null($this->new_value)) {
  478. $this->value = $tempValue;
  479. }
  480. /* Now, return the created widget. */
  481. return $result;
  482. }
  483. /**
  484. * Creates info block
  485. * @return string html formated output
  486. */
  487. function createWidget_Info() {
  488. // return sq_htmlspecialchars($this->value);
  489. // like COMMENT, allow HTML here
  490. return $this->value;
  491. }
  492. /**
  493. * Create string field
  494. *
  495. * @param boolean $password When TRUE, the text in the input
  496. * widget will be obscured (OPTIONAL;
  497. * default = FALSE).
  498. *
  499. * @return string html formated option field
  500. *
  501. */
  502. function createWidget_String($password=FALSE) {
  503. switch ($this->size) {
  504. case SMOPT_SIZE_TINY:
  505. $width = 5;
  506. break;
  507. case SMOPT_SIZE_SMALL:
  508. $width = 12;
  509. break;
  510. case SMOPT_SIZE_LARGE:
  511. $width = 38;
  512. break;
  513. case SMOPT_SIZE_HUGE:
  514. $width = 50;
  515. break;
  516. case SMOPT_SIZE_NORMAL:
  517. default:
  518. $width = 25;
  519. }
  520. //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!)
  521. if ($password)
  522. 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>' : '');
  523. else
  524. 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>' : '');
  525. }
  526. /**
  527. * Create selection box or radio button group
  528. *
  529. * When $this->htmlencoded is TRUE, the keys and values in
  530. * $this->possible_values are assumed to be display-safe.
  531. * Use with care!
  532. *
  533. * Note that when building radio buttons instead of a select
  534. * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
  535. * radio buttons will be output one after another without
  536. * linebreaks between them. Otherwise, each radio button
  537. * goes on a line of its own.
  538. *
  539. * @param boolean $multiple_select When TRUE, the select widget
  540. * will allow multiple selections
  541. * (OPTIONAL; default is FALSE
  542. * (single select list))
  543. * @param boolean $radio_buttons When TRUE, the widget will
  544. * instead be built as a group
  545. * of radio buttons (and
  546. * $multiple_select will be
  547. * forced to FALSE) (OPTIONAL;
  548. * default is FALSE (select widget))
  549. *
  550. * @return string html formated selection box or radio buttons
  551. *
  552. */
  553. function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
  554. //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
  555. // radio buttons instead of select widget?
  556. //
  557. if ($radio_buttons) {
  558. global $br, $nbsp;
  559. $result = '';
  560. foreach ($this->possible_values as $real_value => $disp_value) {
  561. $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);
  562. if ($this->size != SMOPT_SIZE_TINY)
  563. $result .= $br;
  564. }
  565. return $result;
  566. }
  567. // everything below applies to select widgets
  568. //
  569. switch ($this->size) {
  570. //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
  571. case SMOPT_SIZE_TINY:
  572. $height = 3;
  573. break;
  574. case SMOPT_SIZE_SMALL:
  575. $height = 8;
  576. break;
  577. case SMOPT_SIZE_LARGE:
  578. $height = 15;
  579. break;
  580. case SMOPT_SIZE_HUGE:
  581. $height = 25;
  582. break;
  583. case SMOPT_SIZE_NORMAL:
  584. default:
  585. $height = 5;
  586. }
  587. 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>' : '');
  588. }
  589. /**
  590. * Create folder selection box
  591. *
  592. * @param boolean $multiple_select When TRUE, the select widget
  593. * will allow multiple selections
  594. * (OPTIONAL; default is FALSE
  595. * (single select list))
  596. *
  597. * @return string html formated selection box
  598. *
  599. */
  600. function createWidget_FolderList($multiple_select=FALSE) {
  601. switch ($this->size) {
  602. //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
  603. case SMOPT_SIZE_TINY:
  604. $height = 3;
  605. break;
  606. case SMOPT_SIZE_SMALL:
  607. $height = 8;
  608. break;
  609. case SMOPT_SIZE_LARGE:
  610. $height = 15;
  611. break;
  612. case SMOPT_SIZE_HUGE:
  613. $height = 25;
  614. break;
  615. case SMOPT_SIZE_NORMAL:
  616. default:
  617. $height = 5;
  618. }
  619. // possible values might include a nested array of
  620. // possible values (list of folders)
  621. //
  622. $option_list = array();
  623. foreach ($this->possible_values as $value => $text) {
  624. // list of folders (boxes array)
  625. //
  626. if (is_array($text)) {
  627. $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter));
  628. // just one option here
  629. //
  630. } else {
  631. $option_list = array_merge($option_list, array($value => $text));
  632. }
  633. }
  634. if (empty($option_list))
  635. $option_list = array('ignore' => _("unavailable"));
  636. 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>' : '');
  637. }
  638. /**
  639. * Creates textarea
  640. * @return string html formated textarea field
  641. */
  642. function createWidget_TextArea() {
  643. switch ($this->size) {
  644. case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break;
  645. case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break;
  646. case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break;
  647. case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break;
  648. case SMOPT_SIZE_NORMAL:
  649. default: $rows = 5; $cols = 50;
  650. }
  651. return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs);
  652. }
  653. /**
  654. * Creates field for integer
  655. *
  656. * Difference from createWidget_String is visible only when javascript is enabled
  657. * @return string html formated option field
  658. */
  659. function createWidget_Integer() {
  660. // add onChange javascript handler to a regular string widget
  661. // which will strip out all non-numeric chars
  662. if (checkForJavascript())
  663. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  664. . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' '
  665. . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } '
  666. . 'this.value=newVal;';
  667. return $this->createWidget_String();
  668. }
  669. /**
  670. * Creates field for floating number
  671. * Difference from createWidget_String is visible only when javascript is enabled
  672. * @return string html formated option field
  673. */
  674. function createWidget_Float() {
  675. // add onChange javascript handler to a regular string widget
  676. // which will strip out all non-numeric (period also OK) chars
  677. if (checkForJavascript())
  678. $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; '
  679. . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' '
  680. . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') '
  681. . 'newVal += origVal.charAt(i); } this.value=newVal;';
  682. return $this->createWidget_String();
  683. }
  684. /**
  685. * Create boolean widget
  686. *
  687. * When creating Yes/No radio buttons, the "yes_text"
  688. * and "no_text" option attributes are used to override
  689. * the typical "Yes" and "No" text.
  690. *
  691. * @param boolean $checkbox When TRUE, the widget will be
  692. * constructed as a checkbox,
  693. * otherwise it will be a set of
  694. * Yes/No radio buttons (OPTIONAL;
  695. * default is TRUE (checkbox)).
  696. *
  697. * @return string html formated boolean widget
  698. *
  699. */
  700. function createWidget_Boolean($checkbox=TRUE) {
  701. global $oTemplate, $nbsp;
  702. // checkbox...
  703. //
  704. if ($checkbox) {
  705. //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
  706. $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);
  707. }
  708. // radio buttons...
  709. //
  710. else {
  711. /* Build the yes choice. */
  712. $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');
  713. /* Build the no choice. */
  714. $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');
  715. /* Build the combined "boolean widget". */
  716. $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option " . ($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>' : '');
  717. }
  718. return ($result);
  719. }
  720. /**
  721. * Creates hidden field
  722. * @return string html formated hidden input field
  723. */
  724. function createWidget_Hidden() {
  725. return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs);
  726. }
  727. /**
  728. * Creates comment
  729. * @return string comment
  730. */
  731. function createWidget_Comment() {
  732. $result = $this->comment;
  733. return ($result);
  734. }
  735. /**
  736. * Creates a (non-associative) edit list
  737. *
  738. * Note that multiple layout types are supported for this widget.
  739. * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
  740. * constants.
  741. *
  742. * @return string html formated list of edit fields and
  743. * their associated controls
  744. */
  745. function createWidget_EditList() {
  746. global $oTemplate;
  747. switch ($this->size) {
  748. case SMOPT_SIZE_TINY:
  749. $height = 3;
  750. break;
  751. case SMOPT_SIZE_SMALL:
  752. $height = 8;
  753. break;
  754. case SMOPT_SIZE_MEDIUM:
  755. $height = 15;
  756. break;
  757. case SMOPT_SIZE_LARGE:
  758. $height = 25;
  759. break;
  760. case SMOPT_SIZE_HUGE:
  761. $height = 40;
  762. break;
  763. case SMOPT_SIZE_NORMAL:
  764. default:
  765. $height = 5;
  766. }
  767. if (empty($this->possible_values)) $this->possible_values = array();
  768. if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
  769. //FIXME: $this->aExtraAttribs probably should only be used in one place
  770. $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs));
  771. $oTemplate->assign('use_input_widget', $this->use_add_widget);
  772. $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
  773. $oTemplate->assign('trailing_text', $this->trailing_text);
  774. $oTemplate->assign('trailing_text_small', $this->trailing_text_small);
  775. $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html);
  776. $oTemplate->assign('possible_values', $this->possible_values);
  777. $oTemplate->assign('current_value', $this->value);
  778. $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));
  779. // NOTE: i=f-1; j=e-1 is in lieu of break 2
  780. $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
  781. $oTemplate->assign('name', $this->name);
  782. switch ($this->layout_type) {
  783. case SMOPT_EDIT_LIST_LAYOUT_SELECT:
  784. return $oTemplate->fetch('edit_list_widget.tpl');
  785. case SMOPT_EDIT_LIST_LAYOUT_LIST:
  786. return $oTemplate->fetch('edit_list_widget_list_style.tpl');
  787. default:
  788. error_box(sprintf(_("Edit List Layout Type '%s' Not Found"), $this->layout_type));
  789. }
  790. }
  791. /**
  792. * Creates an associative edit list
  793. *
  794. * Note that multiple layout types are supported for this widget.
  795. * $this->layout_type must be one of the SMOPT_EDIT_LIST_LAYOUT_*
  796. * constants.
  797. *
  798. * @return string html formated list of edit fields and
  799. * their associated controls
  800. */
  801. function createWidget_EditListAssociative() {
  802. global $oTemplate;
  803. switch ($this->size) {
  804. case SMOPT_SIZE_TINY:
  805. $height = 3;
  806. break;
  807. case SMOPT_SIZE_SMALL:
  808. $height = 8;
  809. break;
  810. case SMOPT_SIZE_MEDIUM:
  811. $height = 15;
  812. break;
  813. case SMOPT_SIZE_LARGE:
  814. $height = 25;
  815. break;
  816. case SMOPT_SIZE_HUGE:
  817. $height = 40;
  818. break;
  819. case SMOPT_SIZE_NORMAL:
  820. default:
  821. $height = 5;
  822. }
  823. // ensure correct format of current value(s)
  824. //
  825. if (empty($this->possible_values)) $this->possible_values = array();
  826. if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values);
  827. $oTemplate->assign('name', $this->name);
  828. $oTemplate->assign('current_value', $this->value);
  829. $oTemplate->assign('possible_values', $this->possible_values);
  830. $oTemplate->assign('poss_value_folders', $this->poss_value_folders);
  831. $oTemplate->assign('folder_filter', $this->folder_filter);
  832. $oTemplate->assign('use_input_widget', $this->use_add_widget);
  833. $oTemplate->assign('use_delete_widget', $this->use_delete_widget);
  834. $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs)));
  835. //FIXME: $this->aExtraAttribs probably should only be used in one place
  836. $oTemplate->assign('input_key_widget', addInput('add_' . $this->name . '_key', '', 22, 0, $this->aExtraAttribs));
  837. $oTemplate->assign('input_value_widget', addInput('add_' . $this->name . '_value', '', 12, 0, $this->aExtraAttribs));
  838. $oTemplate->assign('select_height', $height);
  839. $oTemplate->assign('aAttribs', $this->aExtraAttribs);
  840. $oTemplate->assign('trailing_text', $this->trailing_text);
  841. $oTemplate->assign('trailing_text_small', $this->trailing_text_small);
  842. $oTemplate->assign('trailing_text_is_html', $this->trailing_text_is_html);
  843. switch ($this->layout_type) {
  844. case SMOPT_EDIT_LIST_LAYOUT_SELECT:
  845. return $oTemplate->fetch('edit_list_associative_widget.tpl');
  846. case SMOPT_EDIT_LIST_LAYOUT_LIST:
  847. return $oTemplate->fetch('edit_list_associative_widget_list_style.tpl');
  848. default:
  849. error_box(sprintf(_("Associative Edit List Layout Type '%s' Not Found"), $this->layout_type));
  850. }
  851. }
  852. /**
  853. * Creates a submit button
  854. *
  855. * @return string html formated submit button widget
  856. *
  857. */
  858. function createWidget_Submit() {
  859. 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>' : '');
  860. }
  861. /**
  862. *
  863. */
  864. function save() {
  865. $function = $this->save_function;
  866. $function($this);
  867. }
  868. /**
  869. *
  870. */
  871. function changed() {
  872. // edit lists have a lot going on, so we'll always process them
  873. //
  874. if ($this->type == SMOPT_TYPE_EDIT_LIST
  875. || $this->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE)
  876. return TRUE;
  877. return ($this->value != $this->new_value);
  878. }
  879. } /* End of SquirrelOption class*/
  880. /**
  881. * Saves the option value (this is the default save function
  882. * unless overridden by the user)
  883. *
  884. * @param object $option object that holds option name and new_value
  885. */
  886. function save_option($option) {
  887. // Can't save the pref if we don't have the username
  888. //
  889. if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) {
  890. return;
  891. }
  892. // if the widget is a selection list, make sure the new
  893. // value is actually in the selection list and is not an
  894. // injection attack
  895. //
  896. if ($option->type == SMOPT_TYPE_STRLIST
  897. && !array_key_exists($option->new_value, $option->possible_values))
  898. return;
  899. // all other widgets except TEXTAREAs should never be allowed to have newlines
  900. //
  901. else if ($option->type != SMOPT_TYPE_TEXTAREA && !empty($option->new_value))
  902. $option->new_value = str_replace(array("\r", "\n"), '', $option->new_value);
  903. global $data_dir;
  904. // edit lists: first add new elements to list, then
  905. // remove any selected ones (note that we must add
  906. // before deleting because the javascript that populates
  907. // the "add" textbox when selecting items in the list
  908. // (for deletion))
  909. //
  910. if ($option->type == SMOPT_TYPE_EDIT_LIST) {
  911. if (empty($option->possible_values)) $option->possible_values = array();
  912. if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
  913. // add element if given
  914. //
  915. if ((isset($option->use_add_widget) && $option->use_add_widget)
  916. && sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) {
  917. $new_element = trim($new_element);
  918. if (!empty($new_element)
  919. && !in_array($new_element, $option->possible_values))
  920. $option->possible_values[] = $new_element;
  921. }
  922. // delete selected elements if needed
  923. //
  924. if ((isset($option->use_delete_widget) && $option->use_delete_widget)
  925. && is_array($option->new_value)
  926. && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST))
  927. $option->possible_values = array_diff($option->possible_values, $option->new_value);
  928. // save full list (stored in "possible_values")
  929. //
  930. setPref($data_dir, $username, $option->name, serialize($option->possible_values));
  931. // associative edit lists are handled similar to
  932. // non-associative ones
  933. //
  934. } else if ($option->type == SMOPT_TYPE_EDIT_LIST_ASSOCIATIVE) {
  935. if (empty($option->possible_values)) $option->possible_values = array();
  936. if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values);
  937. // add element if given
  938. //
  939. $new_element_key = '';
  940. $new_element_value = '';
  941. $retrieve_key = sqGetGlobalVar('add_' . $option->name . '_key', $new_element_key, SQ_POST);
  942. $retrieve_value = sqGetGlobalVar('add_' . $option->name . '_value', $new_element_value, SQ_POST);
  943. if ((isset($option->use_add_widget) && $option->use_add_widget)
  944. && ($retrieve_key || $retrieve_value)) {
  945. $new_element_key = trim($new_element_key);
  946. $new_element_value = trim($new_element_value);
  947. if ($option->poss_value_folders && empty($new_element_key))
  948. $new_element_value = '';
  949. if (!empty($new_element_key) || !empty($new_element_value)) {
  950. if (empty($new_element_key)) $new_element_key = '0';
  951. $option->possible_values[$new_element_key] = $new_element_value;
  952. }
  953. }
  954. // delete selected elements if needed
  955. //
  956. if ((isset($option->use_delete_widget) && $option->use_delete_widget)
  957. && is_array($option->new_value)
  958. && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST)) {
  959. if ($option->layout_type == SMOPT_EDIT_LIST_LAYOUT_SELECT) {
  960. foreach ($option->new_value as $key)
  961. unset($option->possible_values[urldecode($key)]);
  962. }
  963. else
  964. $option->possible_values = array_diff($option->possible_values, $option->new_value);
  965. }
  966. // save full list (stored in "possible_values")
  967. //
  968. setPref($data_dir, $username, $option->name, serialize($option->possible_values));
  969. // Certain option types need to be serialized because
  970. // they are not scalar
  971. //
  972. } else if ($option->is_multiple_valued())
  973. setPref($data_dir, $username, $option->name, serialize($option->new_value));
  974. // Checkboxes, when unchecked, don't submit anything in
  975. // the POST, so set to SMPREF_OFF if not found
  976. //
  977. else if (($option->type == SMOPT_TYPE_BOOLEAN
  978. || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
  979. && empty($option->new_value))
  980. setPref($data_dir, $username, $option->name, SMPREF_OFF);
  981. // For integer fields, make sure we only have digits...
  982. // We'll be nice and instead of just converting to an integer,
  983. // we'll physically remove each non-digit in the string.
  984. //
  985. else if ($option->type == SMOPT_TYPE_INTEGER) {
  986. $option->new_value = preg_replace('/[^0-9]/', '', $option->new_value);
  987. setPref($data_dir, $username, $option->name, $option->new_value);
  988. }
  989. else
  990. setPref($data_dir, $username, $option->name, $option->new_value);
  991. // if a checkbox or multi select is zeroed/cleared out, it
  992. // needs to have an empty value pushed into its "new_value" slot
  993. //
  994. if (($option->type == SMOPT_TYPE_STRLIST_MULTI
  995. || $option->type == SMOPT_TYPE_BOOLEAN_CHECKBOX)
  996. && is_null($option->new_value))
  997. $option->new_value = '';
  998. }
  999. /**
  1000. * save function that does not save
  1001. * @param object $option
  1002. */
  1003. function save_option_noop($option) {
  1004. /* Do nothing here... */
  1005. }
  1006. /**
  1007. * Create hidden 'optpage' input field with value set by argument
  1008. * @param string $optpage identification of option page
  1009. * @return string html formated hidden input field
  1010. */
  1011. function create_optpage_element($optpage) {
  1012. return addHidden('optpage', $optpage);
  1013. }
  1014. /**
  1015. * Create hidden 'optmode' input field with value set by argument
  1016. * @param string $optmode
  1017. * @return string html formated hidden input field
  1018. */
  1019. function create_optmode_element($optmode) {
  1020. return addHidden('optmode', $optmode);
  1021. }
  1022. /**
  1023. * @param array $optgrps
  1024. * @param array $optvals
  1025. * @return array
  1026. */
  1027. function create_option_groups($optgrps, $optvals) {
  1028. /* Build a simple array with which to start. */
  1029. $result = array();
  1030. /* Create option group for each option group name. */
  1031. foreach ($optgrps as $grpkey => $grpname) {
  1032. $result[$grpkey] = array();
  1033. $result[$grpkey]['name'] = $grpname;
  1034. $result[$grpkey]['options'] = array();
  1035. }
  1036. /* Create a new SquirrelOption for each set of option values. */
  1037. foreach ($optvals as $grpkey => $grpopts) {
  1038. foreach ($grpopts as $optset) {
  1039. /* Create a new option with all values given. */
  1040. $next_option = new SquirrelOption(
  1041. $optset,
  1042. $optset['name'],
  1043. $optset['caption'],
  1044. $optset['type'],
  1045. (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
  1046. (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
  1047. (isset($optset['posvals']) ? $optset['posvals'] : ''),
  1048. (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
  1049. );
  1050. /* If provided, set if the caption is allowed to wrap for this option. */
  1051. if (isset($optset['caption_wrap'])) {
  1052. $next_option->setCaptionWrap($optset['caption_wrap']);
  1053. }
  1054. /* If provided, set the size for this option. */
  1055. if (isset($optset['size'])) {
  1056. $next_option->setSize($optset['size']);
  1057. }
  1058. /* If provided, set the trailing_text for this option. */
  1059. if (isset($optset['trailing_text'])) {
  1060. $next_option->setTrailingText($optset['trailing_text']);
  1061. }
  1062. /* If provided, set the trailing_text_small for this option. */
  1063. if (isset($optset['trailing_text_small'])) {
  1064. $next_option->setTrailingTextSmall($optset['trailing_text_small']);
  1065. }
  1066. /* If provided, set the trailing_text_is_html for this option. */
  1067. if (isset($optset['trailing_text_is_html'])) {
  1068. $next_option->setTrailingTextIsHtml($optset['trailing_text_is_html']);
  1069. }
  1070. /* If provided, set the yes_text for this option. */
  1071. if (isset($optset['yes_text'])) {
  1072. $next_option->setYesText($optset['yes_text']);
  1073. }
  1074. /* If provided, set the no_text for this option. */
  1075. if (isset($optset['no_text'])) {
  1076. $next_option->setNoText($optset['no_text']);
  1077. }
  1078. /* If provided, set the poss_value_folders value for this option. */
  1079. if (isset($optset['poss_value_folders'])) {
  1080. $next_option->setPossValueFolders($optset['poss_value_folders']);
  1081. }
  1082. /* If provided, set the layout type for this option. */
  1083. if (isset($optset['layout_type'])) {
  1084. $next_option->setLayoutType($optset['layout_type']);
  1085. }
  1086. /* If provided, set the use_add_widget value for this option. */
  1087. if (isset($optset['use_add_widget'])) {
  1088. $next_option->setUseAddWidget($optset['use_add_widget']);
  1089. }
  1090. /* If provided, set the use_delete_widget value for this option. */
  1091. if (isset($optset['use_delete_widget'])) {
  1092. $next_option->setUseDeleteWidget($optset['use_delete_widget']);
  1093. }
  1094. /* If provided, set the comment for this option. */
  1095. if (isset($optset['comment'])) {
  1096. $next_option->setComment($optset['comment']);
  1097. }
  1098. /* If provided, set the save function for this option. */
  1099. if (isset($optset['save'])) {
  1100. $next_option->setSaveFunction($optset['save']);
  1101. }
  1102. /* If provided, set the extra attributes for this option. */
  1103. if (isset($optset['extra_attributes'])) {
  1104. $next_option->setExtraAttributes($optset['extra_attributes']);
  1105. }
  1106. /* If provided, set the "post script" for this option. */
  1107. if (isset($optset['post_script'])) {
  1108. $next_option->setPostScript($optset['post_script']);
  1109. }
  1110. /* If provided, set the folder_filter for this option. */
  1111. if (isset($optset['folder_filter'])) {
  1112. $next_option->setFolderFilter($optset['folder_filter']);
  1113. }
  1114. /* Add this option to the option array. */
  1115. $result[$grpkey]['options'][] = $next_option;
  1116. }
  1117. }
  1118. /* Return our resulting array. */
  1119. return ($result);
  1120. }