options.php 41 KB

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