options.php 41 KB

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