PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/saf/lib/MailForm/MailForm.php

https://github.com/lux/siteforge
PHP | 1455 lines | 616 code | 111 blank | 728 comment | 164 complexity | 4f839b82804d11c9fc69a5ceaa4237d5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | Sitellite - Content Management System |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2007 Simian Systems |
  7. // +----------------------------------------------------------------------+
  8. // | This software is released under the GNU General Public License (GPL) |
  9. // | Please see the accompanying file docs/LICENSE for licensing details. |
  10. // | |
  11. // | You should have received a copy of the GPL Software License along |
  12. // | with this program; if not, write to Simian Systems, 242 Lindsay, |
  13. // | Winnipeg, MB, R3N 1H1, CANADA. The License is also available at |
  14. // | the following web site address: |
  15. // | <http://www.sitellite.org/index/license> |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: John Luxford <lux@simian.ca> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // MailForm provides methods for generating, validating and handling HTML
  21. // forms.
  22. //
  23. $GLOBALS['loader']->import ('saf.MailForm.Rule');
  24. /**
  25. * MailForm provides methods for generating, validating, and handling web
  26. * forms. Forms can be handled automatically (sent to an email address), or handled
  27. * using "action files". Action files are passed to the handle () method, and can be
  28. * used to do anything you want to the form values. MailForm will generate forms
  29. * for you, using a basic HTML tabled layout, which is okay for most applications,
  30. * or you can have complete visual customization through form templates. MailForm
  31. * also exposes an EasyText tag, which makes it much quicker to create fully
  32. * validating forms in minutes.
  33. *
  34. * New in 2.0:
  35. * - 16 widgets, including date and time widgets, a directory listing widget, and more.
  36. * - 11 different validation rules, including a regular expression rule, cross
  37. * form widget comparisons (good for password verification widgets), and calling
  38. * to pre-defined functions.
  39. * - Many speed improvements, such as dynamic loading of widgets, so your form
  40. * only loads the necessary widgets.
  41. * - MailForm 2.0 is a complete rewrite, which has a much cleaner API, although it
  42. * is not backward compatible with 1.0, other than through its EasyText tag.
  43. *
  44. * Widget Types:
  45. * - checkbox
  46. * - date
  47. * - datetime
  48. * - dirlist
  49. * - file
  50. * - hidden
  51. * - image
  52. * - multiple (multi-line select box)
  53. * - password
  54. * - radio
  55. * - reset
  56. * - select
  57. * - submit
  58. * - text
  59. * - textarea
  60. * - time
  61. *
  62. * Validation Rules:
  63. * - is "value"
  64. * - contains "some value"
  65. * - regex "some regex"
  66. * - equals "anotherfield"
  67. * - not empty
  68. * - length "6+" (eg: 6, 6+, 6-12, 12-)
  69. * - gt "value"
  70. * - ge "value"
  71. * - lt "value"
  72. * - le "value"
  73. * - func "func_name" (or function "func_name")
  74. *
  75. * Miscellaneous:
  76. * - Do not use underscores (_) in the naming of complex widgets (ie. the datetime
  77. * widget).
  78. *
  79. * New in 2.2:
  80. * - Added an 'Extra' parameter to the EasyText =MailForm tag parameter list, so
  81. * that the File widget can be used without having to resort to coding the form
  82. * in PHP instead.
  83. * - Fixed a bug in the EasyText() method, where the value of the Email property
  84. * wasn't being passed on.
  85. *
  86. * New in 2.4:
  87. * - Changed a reference to "$GLOBALS['PHP_SELF']" to "global $_SERVER; $_SERVER['PHP_SELF']"
  88. * so that it works with register_globals off.
  89. * - Added File widgets to the list of widgets skipped on the isset() condition in invalid(),
  90. * because some browsers don't send file fields at all if there is no file. This may inhibit
  91. * file field validation, but it's necessary due to inconsistencies across browsers.
  92. *
  93. * New in 2.6:
  94. * - Moved the validation layer into the Widget level. See saf.MailForm.Widget and
  95. * saf.MailForm.Rule for more info.
  96. * - Added a template example to the DocReader docs below.
  97. *
  98. * New in 2.8:
  99. * - Fixed a bug in getValues() that caused the $vars passed to a validation function
  100. * to be blank.
  101. * - Fixed EasyText() to use the new addRule() method instead of validation(), and
  102. * added the ability to include multiple rules for the same widget through EasyText()
  103. * using commas as separators.
  104. * - Added a $submitted property which is used by setValues() to keep an accurate
  105. * reading on widgets whose $passover_isset property is set to true.
  106. *
  107. * New in 3.0:
  108. * - Added an $_attrs property and three new methods, attr(), unsetAttr(), and getAttrs().
  109. * - Deprecated the $extra property in favour of the new property and methods just
  110. * added.
  111. * - Improved the email output formatting in handle().
  112. *
  113. * New in 3.2:
  114. * - Deprecated the handle() method in favour of a setHandler(), run(), and onSubmit()
  115. * methods. These methods make it easier to subclass MailForm to unify the
  116. * location of the form definition and handling.
  117. * - Added $sendTo, $sendFrom, $sendExtra, $screenReply, and $handler support
  118. * properties to the new methods.
  119. * - Added a parseSettings() method, which makes it much easier to create new
  120. * forms.
  121. * - getValues() and invalid() now do not use a passed $cgi object, and instead
  122. * both rely on a global $cgi object, which is set automatically in the Sitellite
  123. * Content Server, Content Manager, and the init.php script in SAF itself, so
  124. * it's reasonable to assume it will be available. This doesn't affect code
  125. * that still passes the object to invalid(), and the parameter was deprecated
  126. * in getValues() already anyway.
  127. * - getValues() now uses a new property called $uploadFiles, which tells it to
  128. * upload files from file widgets for you automatically. This breaks backward
  129. * compatibility as a default, but you can pass a false value to achieve the
  130. * old behaviour.
  131. *
  132. * New in 3.4:
  133. * - Added makeAssoc() and rememberFields() methods, for use with the Sitellite
  134. * Content Server form API.
  135. * - Added a $title property, which will show in a top-level header above the form
  136. * if provided.
  137. * - Added a global formdata_get() function which returns a key/value list from
  138. * the global $formdata array defined in the application property files.
  139. * - Added a $uploadFile parameter to run(), which allows finer control over
  140. * file upload handling.
  141. *
  142. * New in 3.6:
  143. * - Removed the EasyText() and EasyTextInit() methods.
  144. * - Removed the saf.MailForm.Wizard package, since SCS now provides a more
  145. * flexible, elegant, and clean, and less buggy way of accomplishing the
  146. * same effect.
  147. * - Removed the EasyText widget.
  148. * - Added the ability to report all invalid rules at once, instead of just one.
  149. * However, just one is still the default.
  150. *
  151. * New in 3.8:
  152. * - Added the ability to call addWidget() by specifying the type as a loader path
  153. * to an alternate location. This does not affect backward compatibility in
  154. * any way.
  155. * - Added the ability to call addWidget () by passing a widget object as the
  156. * $type parameter.
  157. *
  158. * New in 4.0:
  159. * - FormHelp ins now integrated into MailForm. In settings.php, you can
  160. * specify 'formhelp = yes' under [Form] and 'formhelp = Message' under
  161. * any widget and the display of it is automatic.
  162. *
  163. * <code>
  164. * <?php
  165. *
  166. * <?php
  167. *
  168. * $form = new MailForm;
  169. * $form->template = 'mf2template.spt';
  170. * $form->message = 'Please take a moment to fill out our form.';
  171. *
  172. * // old way to set attributes
  173. * //$form->extra = 'enctype="multipart/form-data"';
  174. *
  175. * // new way to set attributes
  176. * $form->attr ('enctype', 'multipart/form-data');
  177. *
  178. * // build the form
  179. *
  180. * // standard usage:
  181. * $form->addWidget ('text', 'username');
  182. * $form->widgets['username']->display_value = 'Username (min. 6 chars)';
  183. * $form->widgets['username']->addRule ('length "6-24"', 'Your username must be between 6 and 24 characters in length. Please fix this to continue.');
  184. *
  185. * // or if you prefer:
  186. * $password =& $form->addWidget ('password', 'password');
  187. * $password->addRule ('length "6-24"', 'Your password must be between 6 and 24 characters in length. Please fix this to continue.');
  188. *
  189. * $verify =& $form->addWidget ('password', 'verify');
  190. * $verify->display_value = 'Verify Password';
  191. * $verify->addRule ('equals "password"', 'Your passwords did not match. Please fix this to continue.');
  192. *
  193. * $form->addWidget ('text', 'firstname');
  194. * $form->widgets['firstname']->validation ('not empty');
  195. * $form->addWidget ('text', 'lastname');
  196. * $form->widgets['lastname']->validation ('not empty');
  197. *
  198. * $province =& $form->addWidget ('select', 'province');
  199. * $province->setValues (array (
  200. * 'BC' => 'British Columbia',
  201. * 'MB' => 'Manitoba',
  202. * 'ON' => 'Ontario',
  203. * ));
  204. * $province->default_value = 'MB';
  205. *
  206. * // the new 'dirlist' widget type
  207. * $dlist =& $form->addWidget ('dirlist', 'dltest');
  208. * $dlist->directory = 'pix';
  209. * $dlist->extensions = array ('jpg', 'gif', 'png');
  210. *
  211. * // the new 'date' widget type
  212. * $form->addWidget ('date', 'birth-date');
  213. * $form->widgets['birth-date']->display_value = 'Birth Date';
  214. *
  215. * $textarea =& $form->addWidget ('textarea', 'comments');
  216. * $textarea->setValue ('hello world!');
  217. * $textarea->addRule ('not empty', 'You must enter a comment. Please fix this to continue.');
  218. *
  219. * $send =& $form->addWidget ('submit', 'send');
  220. * $send->setValues ('Submit!');
  221. *
  222. * if ($form->invalid ($cgi)) {
  223. *
  224. * // form is invalid or has not been set yet
  225. * $form->setValues ($cgi, $invalid_field);
  226. * echo $form->show ('inc/html/formtemplate.spt');
  227. *
  228. * } else {
  229. *
  230. * // handle submitted form
  231. *
  232. * if (! $form->handle ('lux@simian.ca', 'Mail Form')) {
  233. * echo 'Error: ' . $form->error_message;
  234. * }
  235. *
  236. * }
  237. *
  238. * ? >
  239. *
  240. * -----
  241. * inc/html/formtemplate.spt (Note: Replace ** with { and }):
  242. *
  243. * <form method="**mailform_method**" action="**mailform_action**" **mailform_extra**>
  244. * <p>**mailform_message**</p>
  245. *
  246. * <p>Username<br />**username**</p>
  247. * <p>Password<br />**password**</p>
  248. * <p>Verify Password<br />**verify**</p>
  249. * <p>First Name<br />**firstname**</p>
  250. * <p>Last Name<br />**lastname**</p>
  251. * <p>Province<br />**province**</p>
  252. * <p>Pick an image<br />**dltest**</p>
  253. * <p>Birthday<br />**birth-date**</p>
  254. * <p>Comments<br />**comments**</p>
  255. * <p>**send**</p>
  256. *
  257. * </form>
  258. *
  259. *
  260. * ? >
  261. * </code>
  262. *
  263. * @package MailForm
  264. * @author John Luxford <lux@simian.ca>
  265. * @copyright Copyright (C) 2001-2003, Simian Systems Inc.
  266. * @license http://www.sitellite.org/index/license Simian Open Software License
  267. * @version 4.0, 2004-01-29, $Id: MailForm.php,v 1.18 2008/05/20 05:43:57 lux Exp $
  268. * @access public
  269. *
  270. */
  271. class MailForm {
  272. /**
  273. * Contains the name of the widget that did not validate during
  274. * the last call to the invalid () method.
  275. *
  276. * @access public
  277. *
  278. */
  279. var $invalid_field = '';
  280. /**
  281. * The value of the method attribute of the HTML form tag.
  282. * $method is actually an alias for $_attrs['method'].
  283. *
  284. * @access public
  285. *
  286. */
  287. var $method;
  288. /**
  289. * The value of the action attribute of the HTML form tag.
  290. * $action is actually an alias for $_attrs['action'].
  291. *
  292. * @access public
  293. *
  294. */
  295. var $action;
  296. /**
  297. * An array of form widgets.
  298. *
  299. * @access public
  300. *
  301. */
  302. var $widgets = array ();
  303. /**
  304. * The optional template file or data used to customize the look
  305. * of the form.
  306. *
  307. * @access public
  308. *
  309. */
  310. var $template;
  311. /**
  312. * The title to be displayed at the top of the form.
  313. *
  314. * @access public
  315. *
  316. */
  317. var $title;
  318. /**
  319. * The initial message to be displayed at the top of the form.
  320. *
  321. * @access public
  322. *
  323. */
  324. var $message;
  325. /**
  326. * A way to pass extra parameters to the HTML form tag, for
  327. * example 'enctype="multipart/formdata"'. Notice: This property is
  328. * deprecated in favour of the $_attrs list and the attr() and unset()
  329. * methods.
  330. *
  331. * @access public
  332. *
  333. */
  334. var $extra;
  335. /**
  336. * Contains the message from any internal errors.
  337. *
  338. * @access public
  339. *
  340. */
  341. var $error_message;
  342. /**
  343. * Determines the way in which error messages are displayed.
  344. * The default ('single') is to display the error message for the first
  345. * invalid field only. The other ('all') is to display a list of all
  346. * invalid fields with their corresponding error messages. Please note
  347. * that $error_mode 'all' assumes that a custom error message is provided
  348. * for every rule.
  349. *
  350. * @access public
  351. *
  352. */
  353. var $error_mode = 'single';
  354. /**
  355. * A list of all invalid fields in the form, and their corresponding
  356. * error messages.
  357. *
  358. * @access public
  359. *
  360. */
  361. var $invalid = array ();
  362. /**
  363. * Contains a true or false value as to whether the form has been
  364. * submitted successfully or not. An invalid form will contain false.
  365. * This value is used internally by the setValues() method.
  366. *
  367. * @access private
  368. *
  369. */
  370. var $submitted = false;
  371. /**
  372. * The name of this form. Optional. $name is actually an alias
  373. * for $_attrs['name'].
  374. *
  375. * @access public
  376. *
  377. */
  378. var $name;
  379. /**
  380. * This contains a list of attributes of the HTML form tag.
  381. *
  382. * @access private
  383. *
  384. */
  385. var $_attrs = array ();
  386. /**
  387. * The email address to send the form to in the default handler.
  388. *
  389. * @access public
  390. *
  391. */
  392. var $sendTo;
  393. /**
  394. * The email address to send the form from in the default handler.
  395. *
  396. * @access public
  397. *
  398. */
  399. var $sendFrom = '';
  400. /**
  401. * Any extra parameters for the mail() function in the default handler.
  402. *
  403. * @access public
  404. *
  405. */
  406. var $sendExtra = '';
  407. /**
  408. * The subject line of the email to send from the default handler.
  409. *
  410. * @access public
  411. *
  412. */
  413. var $sendSubject = 'Mail Form';
  414. /**
  415. * The response to return upon a successfully submitted form in the
  416. * default handler. Defaults to "Thank you. Your form has been sent."
  417. *
  418. * @access public
  419. *
  420. */
  421. var $screenReply = 'Thank you. Your form has been sent.';
  422. /**
  423. * The function or object method to use to handle the submitted form.
  424. * This function or method will be called by the run() method. Use the
  425. * setHandler() method to change this setting.
  426. *
  427. * @access public
  428. *
  429. */
  430. var $handler;
  431. /**
  432. * Whether to upload files automatically or to leave them for a custom
  433. * saving mechanism.
  434. *
  435. * @access public
  436. *
  437. */
  438. var $uploadFiles = true;
  439. /**
  440. * Whether to verify the REQUEST_METHOD and HTTP_REFERER headers to make
  441. * it more difficult (although not impossible) for spammers to abuse your
  442. * form. Note that this can be enabled in a form's settings file under
  443. * the [Form] block via: verify_sender = yes
  444. *
  445. * @access public
  446. *
  447. */
  448. var $verify_sender = false;
  449. /**
  450. * Whether to strip all HTML and PHP tags/code from all input parameters.
  451. * This is off by default because it would break forms with the Xed editor
  452. * by default, so it must be enabled as needed. Note that this can be
  453. * enabled in a form's settings file under the [Form] block via:
  454. * clean_input = yes
  455. *
  456. * @access public
  457. *
  458. */
  459. var $clean_input = false;
  460. /**
  461. * Whether to verify the remote address of the form submitter against
  462. * a list of invalid IP addresses in the database table
  463. * sitellite_form_blacklist so as to prevent repeated abuse from a single
  464. * source.
  465. *
  466. * @access public
  467. *
  468. */
  469. var $blacklist = true;
  470. /**
  471. * Whether to verify that the submitter can accept session data, which
  472. * helps ensure they are a legitimate human user. Passing session
  473. * verification requires cookies to be enabled for the submitter, which
  474. * may help prevent abuse in combination with the other abuse-prevention
  475. * techniques because a spambot may ignore the cookie, however this
  476. * restricts forms for legitimate visitors who have cookies disabled
  477. * in their browser (only a very small number of users disable cookies,
  478. * but some do). To disable for a single form, add verify_session = no
  479. * to its settings.php form.
  480. *
  481. * @access public
  482. *
  483. */
  484. var $verify_session = true;
  485. /**
  486. * Whether this form should tie into Sitellite's autosave capabilities.
  487. * Please note that the autosave handler is only available to authenticated
  488. * users and not to anonymous forms.
  489. *
  490. * @access public
  491. *
  492. */
  493. var $autosave = false;
  494. /**
  495. * Constructor Method. Action will be set to $PHP_SELF if it
  496. * is empty, unless a global $site object is defined in which case the
  497. * action with be $site->url . $PHP_SELF.
  498. *
  499. * @access public
  500. * @param string $action
  501. * @param string $method
  502. *
  503. */
  504. function MailForm ($action = '', $method = 'post') {
  505. $this->method = $method;
  506. if (empty ($action)) {
  507. if (function_exists ('site_current')) {
  508. $action = site_current ();
  509. } else {
  510. global $_SERVER;
  511. $action = $_SERVER['PHP_SELF'];
  512. }
  513. /*if (is_object ($GLOBALS['site'])) {
  514. global $site;
  515. $action = $site->url . $action;
  516. }*/
  517. }
  518. $this->action = $action;
  519. $GLOBALS['loader']->import ('saf.MailForm.Widget');
  520. // set up the $_attrs list
  521. $this->_attrs['action'] =& $this->action;
  522. $this->_attrs['method'] =& $this->method;
  523. $this->name = false;
  524. $this->_attrs['name'] =& $this->name;
  525. $this->handler = array (&$this, 'onSubmit');
  526. // attempt to automatically parse the settings.php file
  527. $cls = strtolower (get_class ($this));
  528. if ($cls != 'mailform') {
  529. if (function_exists ('site_current')) {
  530. $this->action = site_current ();
  531. } else {
  532. $this->action = $_SERVER['PHP_SELF'];
  533. }
  534. $this->_attrs['action'] =& $this->action;
  535. $app = loader_app ();
  536. list ($misc, $path) = explode ($app . '/forms/', $action);
  537. $box = dirname ($path);
  538. if (@file_exists ('inc/app/' . $app . '/forms/' . $box . '/settings.php')) {
  539. $this->parseSettings ('inc/app/' . $app . '/forms/' . $box . '/settings.php');
  540. }
  541. }
  542. }
  543. /**
  544. * Adds another widget to the form. If the $type is specified as
  545. * a loader path, it will import from the proper location outside of
  546. * saf.MailForm.Widget.*, and if you send an object as the $type value
  547. * addWidget() will use that object as the widget (so make sure it is one!),
  548. * as of version 3.8.
  549. *
  550. * @access public
  551. * @param string $type
  552. * @param string $name
  553. * @return object reference
  554. *
  555. */
  556. function &addWidget ($type, $name) {
  557. if (is_object ($type)) {
  558. $this->widgets[$name] =& $type;
  559. $this->widgets[$name]->form =& $this;
  560. return $this->widgets[$name];
  561. } elseif (strpos ($type, '.') !== false) {
  562. loader_import ($type);
  563. $pieces = explode ('.', $type);
  564. $cls = 'MF_Widget_' . strtolower (array_pop ($pieces));
  565. } else {
  566. $cls = 'MF_Widget_' . $type;
  567. loader_import ('saf.MailForm.Widget.' . ucfirst ($type));
  568. }
  569. $this->widgets[$name] = new $cls ($name);
  570. $this->widgets[$name]->form =& $this;
  571. return $this->widgets[$name];
  572. }
  573. /**
  574. * Validates the form values against a global $cgi object. Used in
  575. * the logic of "if the form is invalid then...". Also sets an internal
  576. * $invalid_field property. Returns true if the form is invalid or has not
  577. * been filled out yet. If the form passes (false returned), it also sets
  578. * the $submitted value to true.
  579. *
  580. * @access public
  581. * @return boolean
  582. *
  583. */
  584. function invalid () {
  585. global $cgi;
  586. $this->invalid = array ();
  587. $this->submitted = false;
  588. // determine if form has been submitted (based on submit buttons only)
  589. // making sure form still submits if there are more than one submit button
  590. $ret = true;
  591. $tpl = array ();
  592. foreach ($this->widgets as $k => $v) {
  593. if (in_array (strtolower (get_class ($v)), array ('mf_widget_submit', 'mf_widget_msubmit')) && isset ($cgi->{$k})) {
  594. $ret = false;
  595. break;
  596. } elseif (strtolower (get_class ($v)) == 'mf_widget_image' && isset ($cgi->{$k . '_x'})) {
  597. $ret = false;
  598. break;
  599. } elseif (strtolower (get_class ($v)) == 'mf_widget_template' && count ($v->submitButtons) > 0) {
  600. $tpl[$k] = $v->submitButtons;
  601. }
  602. }
  603. if ($ret) {
  604. foreach ($tpl as $k => $buttons) {
  605. foreach ($buttons as $button) {
  606. if (isset ($cgi->{$button})) {
  607. $ret = false;
  608. break;
  609. }
  610. }
  611. }
  612. if ($ret) {
  613. // form not yet submitted
  614. return true;
  615. }
  616. }
  617. $ret = false;
  618. foreach (array_keys ($this->widgets) as $key) {
  619. $widget =& $this->widgets[$key];
  620. // must compile value of widget here, so compound widgets can validate
  621. $widget_value = $widget->getValue ($cgi);
  622. if (in_array (strtolower (get_class ($widget)), array ('mf_widget_submit', 'mf_widget_msubmit', 'mf_widget_image'))) {
  623. // we've already checked submit buttons
  624. continue;
  625. } elseif (! $widget->passover_isset && ! isset ($cgi->{$key})) {
  626. // specified field is missing, assuming form not yet submitted
  627. return true;
  628. }
  629. //if ($key == 'password') {
  630. // echo 'aha!';
  631. //}
  632. if (! $widget->validate ($widget_value, $this, $cgi)) {
  633. if (! $ret) {
  634. $this->invalid_field = $widget->name;
  635. $ret = true;
  636. }
  637. $this->invalid[$widget->name] = $widget->error_message;
  638. }
  639. }
  640. $this->submitted = true;
  641. if ($ret) {
  642. return true;
  643. }
  644. if ($this->verify_sender) {
  645. if (! $this->verifyRequestMethod ()) {
  646. die ('Invalid request method!');
  647. }
  648. if (! $this->verifyReferer ()) {
  649. die ('Invalid referrer!');
  650. }
  651. }
  652. return false;
  653. }
  654. /**
  655. * Manually set a specific field to be invalid, including a custom error message.
  656. * Useful for setting an error message during the onSubmit() before calling
  657. * return $this->show(); again.
  658. *
  659. * @access public
  660. * @param string $field
  661. * @param string $message
  662. *
  663. */
  664. function setInvalid ($field, $message) {
  665. $this->invalid_field = $field;
  666. $this->invalid[$field] = $message;
  667. $this->widgets[$field]->error_message = $message;
  668. }
  669. /**
  670. * Sets the values of the form widgets from a provided CGI object.
  671. * The second parameter is deprecated.
  672. *
  673. * @access public
  674. * @param object $cgi
  675. * @param boolean $invalid
  676. *
  677. */
  678. function setValues ($cgi, $invalid = false) {
  679. foreach ($cgi->param as $key) {
  680. $value = $cgi->{$key};
  681. if (is_object ($this->widgets[$key])) {
  682. $this->widgets[$key]->setValue ($value);
  683. } elseif (ereg ('^(MF_)+([a-zA-Z0-9-]+)_([A-Z0-9_]+)$', $key, $regs)) {
  684. //echo '<pre>'; print_r ($regs); echo '</pre>';
  685. if (is_object ($this->widgets[$regs[2]])) {
  686. $this->widgets[$regs[2]]->setValue ($value, $regs[3]);
  687. }
  688. }
  689. }
  690. // loop through widgets who are not set but who have passover_isset set to true
  691. // and set their values to ''.
  692. if ($this->submitted) {
  693. foreach ($this->widgets as $key => $widget) {
  694. if ($widget->passover_isset && ! isset ($cgi->{$key})) {
  695. $this->widgets[$key]->setValue ('');
  696. }
  697. }
  698. }
  699. $this->cgi = $cgi;
  700. }
  701. /**
  702. * Generates the HTML form. You can provide an optional template
  703. * to customize the look of the form. Template directives (ie. ##field##)
  704. * must be provided for each form widget, as well as ##mailform_action## and
  705. * ##mailform_method##, which correspond to the action and method attributes
  706. * of the HTML form tag.
  707. *
  708. * @access public
  709. * @param string $template
  710. * @return string
  711. *
  712. */
  713. function show ($template = '') {
  714. /*if (session_pref ('browse_level') == 'normal') {
  715. foreach ($this->widgets as $key => $widget) {
  716. if ($widget->advanced) {
  717. $this->widgets[$key] =& $this->widgets[$key]->changeType ('hidden');
  718. }
  719. }
  720. }*/
  721. if (! empty ($template)) {
  722. // last minute template setting
  723. $this->template = $template;
  724. }
  725. if ($this->formhelp) {
  726. page_add_script (site_prefix () . '/js/formhelp-compressed.js');
  727. }
  728. if ($this->autosave) {
  729. global $cgi;
  730. loader_import ('saf.MailForm.Autosave');
  731. $a = new Autosave ();
  732. if ($a->has_draft ()) {
  733. if ($cgi->_autosave_start_from == intl_get ('Start from your previous draft')) {
  734. $data = $a->retrieve ();
  735. foreach ($data as $k => $v) {
  736. if (is_object ($this->widgets[$k])) {
  737. $this->widgets[$k]->setValue ($v);
  738. }
  739. $cgi->{$k} = $v;
  740. $cgi->param[] = $k;
  741. }
  742. } elseif ($cgi->_autosave_start_from == intl_get ('Start from the currently saved version')) {
  743. $a->clear ();
  744. } else {
  745. // has an existing autosave version, prompt for action
  746. return '<p>' . intl_get ('A draft version of this form has been found from a previous editing session. Would you like to continue from your previous draft or the current version?') . '</p>'
  747. . '<form method="post" action="' . $_SERVER['REQUEST_URI'] . '">'
  748. . '<p><input type="submit" name="_autosave_start_from" value="' . intl_get ('Start from your previous draft') . '" /> &nbsp; &nbsp; '
  749. . '<input type="submit" name="_autosave_start_from" value="' . intl_get ('Start from the currently saved version') . '" /></p>'
  750. . '</form>';
  751. }
  752. }
  753. page_add_script (site_prefix () . '/js/rpc.js');
  754. page_add_script (site_prefix () . '/js/autosave.js');
  755. }
  756. if (! empty ($this->template)) {
  757. // use template to display form
  758. $form_contents = array ();
  759. // determine appropriate instructional message
  760. if (! empty ($this->invalid_field)) {
  761. if ($this->error_mode == 'single') {
  762. // display only the first error message (the default)
  763. if (! empty ($this->widgets[$this->invalid_field]->error_message)) {
  764. $form_contents['mailform_message'] = $this->widgets[$this->invalid_field]->error_message;
  765. } else {
  766. $form_contents['mailform_message'] = intl_get ('Oops! The following field was not filled in correctly:') . ' ' .
  767. $this->widgets[$this->invalid_field]->display_value .
  768. '. ' . intl_get ('Please fix this before continuing.');
  769. }
  770. $this->widgets[$this->invalid_field]->invalid = true;
  771. } else {
  772. // display all error messages
  773. $form_contents['mailform_message'] = intl_get ('Oops! The following information must be corrected in order to continue:') . NEWLINE;
  774. $form_contents['mailform_message'] .= '<ul>' . NEWLINE;
  775. foreach ($this->invalid as $name => $message) {
  776. $form_contents['mailform_message'] .= TAB . '<li>' . $message . '</li>' . NEWLINE;
  777. $this->widgets[$name]->invalid = true;
  778. }
  779. $form_contents['mailform_message'] .= '</ul>' . NEWLINE;
  780. }
  781. } else {
  782. $form_contents['mailform_message'] = $this->message;
  783. }
  784. foreach ($this->widgets as $key =>$widget) {
  785. //$form_contents[$key] = str_replace ('##', 'SITELLITE_DOUBLE_POUND_SUBSTITUTION', $widget->display (0));
  786. if (! is_object ($this->widgets[$key]->form)) {
  787. $this->widgets[$key]->form =& $this;
  788. }
  789. $form_contents[$key] = $this->widgets[$key]->display (0);
  790. }
  791. $form_contents['mailform_title'] = $this->title;
  792. $form_contents['mailform_method'] = $this->method;
  793. $form_contents['mailform_action'] = $this->action;
  794. $form_contents['mailform_extra'] = $this->extra;
  795. $form_contents['mailform_attrs'] = $this->getAttrs ();
  796. global $simple;
  797. //$loader->import ('saf.Template.Simple');
  798. //$tpl = new SimpleTemplate ('', SIMPLE_TEMPLATE_DELIM_POUND);
  799. //$simple = new SimpleTemplate ('');
  800. $return_data = $simple->fill ($this->template, $form_contents);
  801. //return str_replace ('SITELLITE_DOUBLE_POUND_SUBSTITUTION', '##', $return_data);
  802. return $return_data;
  803. } else {
  804. // determine appropriate instructional message
  805. $_message = '';
  806. if (! empty ($this->invalid_field)) {
  807. if ($this->error_mode == 'single') {
  808. if (! empty ($this->widgets[$this->invalid_field]->error_message)) {
  809. $_message .= '<p class="invalid">' . $this->widgets[$this->invalid_field]->error_message . "</p>\n";
  810. } else {
  811. $_message .= '<p class="invalid">' . intl_get ('Oops! The following field was not filled in correctly:') . ' ' .
  812. $this->widgets[$this->invalid_field]->display_value .
  813. '. ' . intl_get ('Please fix this before continuing.') . "</p>\n";
  814. }
  815. $this->widgets[$this->invalid_field]->invalid = true;
  816. } else {
  817. // display all error messages
  818. $_message .= '<p class="invalid">' . intl_get ('Oops! The following information must be corrected in order to continue:') . NEWLINE;
  819. $_message .= '<ul>' . NEWLINE;
  820. foreach ($this->invalid as $name => $message) {
  821. $_message .= TAB . '<li>' . $message . '</li>' . NEWLINE;
  822. $this->widgets[$name]->invalid = true;
  823. }
  824. $_message .= '</ul>' . NEWLINE . '</p>' . NEWLINEx2;
  825. }
  826. } elseif (! empty ($this->message)) {
  827. $_message .= '<p>' . $this->message . "</p>\n";
  828. }
  829. $_widgets = '';
  830. foreach ($this->widgets as $key => $widget) {
  831. if (! method_exists ($this->widgets[$key], 'display')) {
  832. die ('Widget "' . $key . '" has no display() method!');
  833. }
  834. if (! is_object ($this->widgets[$key]->form)) {
  835. $this->widgets[$key]->form =& $this;
  836. }
  837. $_widgets .= $this->widgets[$key]->display (1);
  838. }
  839. $attrstr = $this->getAttrs ();
  840. if (! empty ($this->name)) {
  841. $data = '<form ' . $attrstr . ' ' . $this->extra . '>' . "\n";
  842. } else {
  843. $data = '<form ' . $attrstr . ' ' . $this->extra . '>' . "\n";
  844. }
  845. if (! empty ($this->title)) {
  846. $data .= '<h1>' . $this->title . '</h1>';
  847. }
  848. $data .= $_message;
  849. $align = empty ($this->align) ? 'center' : $this->align;
  850. $data .= '<table border="0" cellspacing="1" cellpadding="3" align="' . $align . '">' . "\n";
  851. $data .= $_widgets;
  852. return $data . '</table>' . "\n" . '</form>';
  853. }
  854. }
  855. /**
  856. * Returns the form values as an associative array. If $uploadFiles
  857. * is set to true, it will return the saved path or false for file widgets,
  858. * otherwise it will return the saf.CGI.UploadedFile object and not act
  859. * on the object for you.
  860. *
  861. * @access public
  862. * @param boolean $uploadFiles
  863. * @return associative array
  864. *
  865. */
  866. function getValues () {
  867. global $cgi;
  868. $uploadFiles = $this->uploadFiles;
  869. $return = array ();
  870. foreach ($this->widgets as $key => $obj) {
  871. if (is_object ($cgi)) {
  872. if ($uploadFiles && strtolower (get_class ($obj)) == 'mf_widget_file') {
  873. $return[$key] = $obj->move ();
  874. } else {
  875. $return[$key] = $obj->getValue ($cgi);
  876. }
  877. } else {
  878. if ($uploadFiles && strtolower (get_class ($obj)) == 'mf_widget_file') {
  879. $return[$key] = $obj->move ();
  880. } else {
  881. $return[$key] = $obj->getValue ();
  882. }
  883. }
  884. }
  885. return $return;
  886. }
  887. /**
  888. * Sets the function to use to handle the output of the current form.
  889. * To specify a method of an object, pass it an array with an object reference
  890. * as the first element and the method name as the second. The default handler
  891. * is the internal onSubmit(). The handler is called using call_user_func()
  892. * in the run() method.
  893. *
  894. * @access public
  895. * @param mixed $func
  896. *
  897. */
  898. function setHandler ($func) {
  899. $this->handler = $func;
  900. }
  901. /**
  902. * Runs the form and returns either the rendered form or the output
  903. * of the handler function. $uploadFiles can be set to false to cause the
  904. * getValues() method not to call move() on File widgets. This is useful
  905. * for situations when you need to do something other than simply save the
  906. * file to a predetermined folder. Please note: The $uploadFiles parameter
  907. * is deprecated in favour of the $uploadFiles property of the MailForm
  908. * class. This allows the setting to be managed via a settings.php file.
  909. *
  910. * @access public
  911. * @param boolean $uploadFiles
  912. * @return string
  913. *
  914. */
  915. function run ($uploadFiles = true) {
  916. global $cgi;
  917. if (! $uploadFiles) {
  918. $this->uploadFiles = $uploadFiles;
  919. }
  920. if ($this->invalid ($cgi)) {
  921. $this->setValues ($cgi);
  922. if ($this->verify_session) {
  923. @session_start ();
  924. $_SESSION['mf_verify_session'] = 'mf_verified';
  925. }
  926. return $this->show ();
  927. } else {
  928. if ($this->verify_session) {
  929. @session_start ();
  930. if ($_SESSION['mf_verify_session'] != 'mf_verified') {
  931. die ('This form requires that you enable cookies in your browser, which helps us to prevent abuse of our forms by automated spam systems.');
  932. }
  933. }
  934. if ($this->blacklist) {
  935. if (db_shift ('select count(*) from sitellite_form_blacklist where ip_address = ?', $_SERVER['REMOTE_ADDR'])) {
  936. die ('The IP address submitting this form has been blacklisted due to abuse. If you feel this has been done in error, please contact the website owner.');
  937. }
  938. }
  939. if ($this->autosave) {
  940. loader_import ('saf.MailForm.Autosave');
  941. $a = new Autosave ();
  942. $a->clear ($_SERVER['HTTP_REFERER']);
  943. }
  944. $this->setValues ($cgi);
  945. $vals = $this->getValues ();
  946. if ($this->clean_input) {
  947. foreach ($vals as $k => $v) {
  948. $vals[$k] = strip_tags ($v);
  949. }
  950. }
  951. return call_user_func ($this->handler, $vals);
  952. }
  953. }
  954. /**
  955. * This is the default handler function. It is called via run()
  956. * and can be overridden via subclassing.
  957. *
  958. * @access public
  959. * @param array $vals
  960. * @return string
  961. *
  962. */
  963. function onSubmit ($vals) {
  964. if (! empty ($this->sendTo) || ! empty ($this->email)) {
  965. $email = $this->sendTo;
  966. $subject = $this->sendSubject;
  967. $from_field = $this->sendFrom;
  968. $extra = $this->sendExtra;
  969. global $site;
  970. $message_body = intl_get ('The following information has been sent from') . ' ' . $site->url . $GLOBALS['PHP_SELF'] . ":\n\n";
  971. foreach ($vals as $key => $value) {
  972. if ($this->widgets[$key]->type == 'separator') {
  973. $message_body .= "\n----------\n\n";
  974. } elseif ($this->widgets[$key]->type == 'section') {
  975. $message_body .= $this->widgets[$key]->title . "\n\n";
  976. } else {
  977. if (strlen ($this->widgets[$key]->alt) > 30) {
  978. $alt = substr ($this->widgets[$key]->alt, 0, 27) . '...';
  979. } else {
  980. $alt = $this->widgets[$key]->alt;
  981. }
  982. $message_body .= str_pad ($alt, 35) . ": " . $value . "\n";
  983. }
  984. }
  985. if (! empty ($from_field)) {
  986. if (strpos ($vars[$from_field], "\n") !== false) {
  987. die ('Invalid from field value.');
  988. }
  989. $from_field = 'From: ' . $vals[$from_field] . "\r\n";
  990. }
  991. //echo '<pre>' . htmlentities_compat ($message_body) . '</pre>';
  992. if (@mail ($email, $subject, $message_body, $from_field . $extra)) {
  993. return $this->screenReply;
  994. } else {
  995. $this->error_message = intl_get ('Email could not sent. Please verify that your mail daemon is functioning correctly.');
  996. return $this->error_message;
  997. }
  998. }
  999. $this->error_message = intl_get ('No email address set. Please check your form settings.');
  1000. return $this->error_message;
  1001. }
  1002. /**
  1003. * Note: Deprecated in favour of the setHandler() and run() methods
  1004. * and subclassing. Handles the form, once it has been satisfactorily completed.
  1005. * If the first parameter points to a file (ie. 'inc/forms/contact.php'), it will
  1006. * use that file as an "action file" to handle the form. Otherwise, the first
  1007. * parameter must be an email address, as handle () will simply send an email
  1008. * of the form contents to the specified email address. Note: Extra will be
  1009. * passed to the PHP mail () function as a fourth parameter, or can be used for
  1010. * any purpose you'd like in an action file.
  1011. *
  1012. * @access public
  1013. * @param string $email
  1014. * @param string $subject
  1015. * @param string $from_field
  1016. * @param string $extra
  1017. * @return boolean
  1018. *
  1019. */
  1020. function handle ($email, $subject = 'Mail Form', $from_field = '', $extra = '') {
  1021. if (! strstr ($email, '@')) {
  1022. //echo 'using actionfile: ' . $email . '<br />';
  1023. // $email is an action file, so give it some action! ;)
  1024. if (strstr ($email, '..')) {
  1025. // we don't like file paths with .. in them. they really should never be needed.
  1026. $this->error_message = intl_get ('Sorry, no action file names with ".." in them.');
  1027. return false;
  1028. } elseif (! @file_exists ($email)) {
  1029. $this->error_message = intl_get ('Sorry, the action file you have specified could not be found.');
  1030. return false;
  1031. } else {
  1032. include_once ($email);
  1033. return true;
  1034. }
  1035. } else {
  1036. // send email
  1037. //echo 'using email address: ' . $email . '<br />';
  1038. $vars = $this->getValues ();
  1039. global $site;
  1040. $message_body = $this->formatEmail ($vars);
  1041. if (! empty ($from_field)) {
  1042. if (strpos ($vars[$from_field], "\n") !== false) {
  1043. die ('Invalid from field value.');
  1044. }
  1045. $from_field = 'From: ' . $vars[$from_field] . "\r\n";
  1046. }
  1047. //echo '<pre>' . htmlentities_compat ($message_body) . '</pre>';
  1048. if (@mail ($email, $subject, $message_body, $from_field . $extra)) {
  1049. return true;
  1050. } else {
  1051. $this->error_message = intl_get ('Email could not sent. Please verify that your mail daemon is functioning correctly.');
  1052. return false;
  1053. }
  1054. //echo "<pre>mail ('$email', '$subject', '$message_body', '$from_field$extra')</pre>";
  1055. //return true;
  1056. }
  1057. }
  1058. function formatEmail ($vals) {
  1059. global $site;
  1060. $message_body = intl_get ('The following information has been sent from') . ' ' . $site->url . $GLOBALS['PHP_SELF'] . ":\n\n";
  1061. foreach ($vals as $key => $value) {
  1062. if ($this->widgets[$key]->type == 'separator') {
  1063. $message_body .= "\n----------\n\n";
  1064. } elseif ($this->widgets[$key]->type == 'section') {
  1065. $message_body .= $this->widgets[$key]->title . "\n\n";
  1066. } else {
  1067. if (strlen ($this->widgets[$key]->alt) > 30) {
  1068. $alt = substr ($this->widgets[$key]->alt, 0, 27) . '...';
  1069. } else {
  1070. $alt = $this->widgets[$key]->alt;
  1071. }
  1072. $message_body .= str_pad ($alt, 35) . ": " . $value . "\n";
  1073. }
  1074. }
  1075. return $message_body;
  1076. }
  1077. /**
  1078. * Parses the specified file using the parse_ini_file()
  1079. * function. Sections in the file correspond to the names of
  1080. * widgets you wish to create, in addition to a [Form] section
  1081. * that sets properties for the form itself. The values in
  1082. * each section correspond to properties or methods of the
  1083. * widgets. This method can be used to simplify the process
  1084. * of defining and customizing a form.
  1085. *
  1086. * @access public
  1087. * @param string $file
  1088. * @return boolean
  1089. *
  1090. */
  1091. function parseSettings ($file) {
  1092. if (! @file_exists ($file)) {
  1093. return false;
  1094. }
  1095. ini_add_filter ('ini_filter_split_comma_single', array (
  1096. 'rule 0', 'rule 1', 'rule 2', 'rule 3', 'rule 4', 'rule 5', 'rule 6', 'rule 7', 'rule 8',
  1097. 'button 0', 'button 1', 'button 2', 'button 3', 'button 4', 'button 5', 'button 6', 'button 7', 'button 8',
  1098. 'submitButtons',
  1099. ));
  1100. $conf = ini_parse ($file, true);
  1101. ini_clear ();
  1102. if (count ($conf) == 0) {
  1103. return false;
  1104. }
  1105. // form properties, optional
  1106. if (is_array ($conf['Form'])) {
  1107. foreach ($conf['Form'] as $key => $value) {
  1108. if (($key == 'title' || $key == 'message') && function_exists ('intl_get')) {
  1109. $value = intl_get ($value);
  1110. }
  1111. $this->{$key} = $value;
  1112. }
  1113. unset ($conf['Form']);
  1114. }
  1115. foreach ($conf as $name => $data) {
  1116. $this->createWidget ($name, $data);
  1117. }
  1118. return true;
  1119. }
  1120. /**
  1121. * Creates a widget from a name and an array, usually taken from a parsed
  1122. * settings.php (ini formatted) file.
  1123. *
  1124. * @access public
  1125. * @param string
  1126. * @param array hash
  1127. */
  1128. function &createWidget ($name, $data) {// create widget
  1129. $type = $data['type'];
  1130. unset ($data['type']);
  1131. $widget =& $this->addWidget ($type, $name);
  1132. // handle setValues
  1133. if (! empty ($data['setValues'])) {
  1134. if (strpos ($data['setValues'], 'eval:') === 0) {
  1135. eval (CLOSE_TAG . OPEN_TAG . ' $widget->setValues (' . substr ($data['setValues'], 5) . '); ' . CLOSE_TAG);
  1136. } elseif (preg_match ('/, ?/', $data['setValues'])) {
  1137. $widget->setValues (preg_split ('/, ?/', $data['setValues']));
  1138. // eval (CLOSE_TAG . OPEN_TAG . ' $widget->setValues (array (' . $data['setValues'] . ')); ' . CLOSE_TAG);
  1139. } else {
  1140. if (strpos ($data['setValues'], '\'') === 0 && strrpos ($data['setValues'], '\'') == strlen ($data['setValues']) - 1) {
  1141. $data['setValues'] = substr ($data['setValues'], 1, strlen ($data['setValues']) - 2);
  1142. }
  1143. $widget->setValues ($data['setValues']);
  1144. }
  1145. // eval (CLOSE_TAG . OPEN_TAG . ' $widget->setValues (' . $data['setValues'] . '); ' . CLOSE_TAG);
  1146. }
  1147. unset ($data['setValues']);
  1148. if (! empty ($data['setDefault'])) {
  1149. if (strpos ($data['setDefault'], 'eval:') === 0) {
  1150. eval (CLOSE_TAG . OPEN_TAG . ' $widget->setDefault (' . substr ($data['setDefault'], 5) . '); ' . CLOSE_TAG);
  1151. } else {
  1152. $widget->setDefault ($data['setDefault']);
  1153. }
  1154. }
  1155. if (! empty ($data['setValue'])) {
  1156. if (strpos ($data['setValue'], 'eval:') === 0) {
  1157. eval (CLOSE_TAG . OPEN_TAG . ' $widget->setValue (' . substr ($data['setValue'], 5) . '); ' . CLOSE_TAG);
  1158. } else {
  1159. $widget->setValue ($data['setValue']);
  1160. }
  1161. }
  1162. if (! empty ($data['setRules'])) {
  1163. if (strpos ($data['setRules'], 'eval:') === 0) {
  1164. eval (CLOSE_TAG . OPEN_TAG . ' $widget->rules = ' . substr ($data['setRules'], 5) . '); ' . CLOSE_TAG);
  1165. }
  1166. }
  1167. // handle rules
  1168. //foreach (preg_split ('/, ?/', $data['rules'], -1, PREG_SPLIT_NO_EMPTY) as $rule) {
  1169. // $widget->addRule ($rule);
  1170. //}
  1171. //unset ($data['rules']);
  1172. // widget properties
  1173. foreach ($data as $key => $value) {
  1174. if (strpos ($key, 'rule ') === 0) {
  1175. if (is_array ($value)) {
  1176. if (function_exists ('intl_get')) {
  1177. $value[1] = intl_get ($value[1]);
  1178. }
  1179. $widget->addRule ($value[0], $value[1]);
  1180. } else {
  1181. $widget->addRule ($value);
  1182. }
  1183. } elseif (strpos ($key, 'value ') === 0) {
  1184. $widget->setValues ($value, $value);
  1185. } elseif ($type == 'msubmit' && strpos ($key, 'button ') === 0) {
  1186. $b =& $widget->getButton ();
  1187. if (empty ($b->value)) {
  1188. if (is_array ($value)) {
  1189. if (function_exists ('intl_get')) {
  1190. $value[0] = intl_get ($value[0]);
  1191. }
  1192. $b->setValues ($value[0]);
  1193. $b->extra = $value[1];
  1194. } else {
  1195. if (function_exists ('intl_get')) {
  1196. $value = intl_get ($value);
  1197. }
  1198. $b->setValues ($value);
  1199. }
  1200. } else {
  1201. if (is_array ($value)) {
  1202. if (function_exists ('intl_get')) {
  1203. $value[0] = intl_get ($value[0]);
  1204. }
  1205. $b =& $widget->addButton ($name, $value[0]);
  1206. $b->extra = $value[1];
  1207. } else {
  1208. if (function_exists ('intl_get')) {
  1209. $value = intl_get ($value);
  1210. }
  1211. $widget->addButton ($name, $value);
  1212. }
  1213. }
  1214. } elseif ($key == 'alt') {
  1215. if (function_exists ('intl_get')) {
  1216. $widget->{$key} = intl_get ($value);
  1217. } else {
  1218. $widget->{$key} = $value;
  1219. }
  1220. } elseif (method_exists ($widget, $key)) {
  1221. $widget->{$key} ($value);
  1222. } else {
  1223. $widget->{$key} = $value;
  1224. }
  1225. }
  1226. if (! empty ($widget->formhelp) && session_pref ('form_help') == 'on') {
  1227. $widget->extra .= ' onfocus="formhelp_show (this, \'' . addslashes ($widget->formhelp) . '\')" onblur="formhelp_hide ()"';
  1228. }
  1229. unset ($widget);
  1230. return $this->widgets[$name];
  1231. }
  1232. /**
  1233. * Takes a non-associative array and creates an associative array
  1234. * out of its values. This is used to send non-associative arrays to the
  1235. * setValues() method of the Widget objects.
  1236. *
  1237. * @access public
  1238. * @param array $list
  1239. * @return associative array
  1240. *
  1241. */
  1242. function makeAssoc ($list) {
  1243. $new = array ();
  1244. foreach ($list as $key => $value) {
  1245. if (! is_int ($key)) {
  1246. $new[$key] = $value;
  1247. } else {
  1248. $new[$value] = $value;
  1249. }
  1250. }
  1251. return $new;
  1252. }
  1253. /**
  1254. * Takes a non-associative array listing the names of each field
  1255. * from $cgi you want to "remember", and creates hidden fields for each
  1256. * of them, so you don't have to hard-code lists of hidden fields in
  1257. * multi-screen forms.
  1258. *
  1259. * @access public
  1260. * @param array $list
  1261. *
  1262. */
  1263. function rememberFields ($list) {
  1264. foreach ($list as $field) {
  1265. $this->addWidget ('hidden', $field);
  1266. }
  1267. }
  1268. /**
  1269. * This is the accessor method for setting and getting the value of
  1270. * any attribute of the form tag, including 'method' and 'action'. This will
  1271. * replace the $extra property, which is henceforth deprecated. If you call
  1272. * this method and provide no $value, you are using it as a 'getter', as in
  1273. * you are getting the current value. If you provide a value, the new value
  1274. * will be set, so you are acting as a 'setter'. If you simply specify that
  1275. * the $value be true, then it will appear filled with its own name (useful
  1276. * for things like the checked="checked" attribute of a checkbox input field,
  1277. * even though this isn't a checkbox field).
  1278. *
  1279. * @access public
  1280. * @param string $key
  1281. * @param string $key
  1282. * @return string
  1283. *
  1284. */
  1285. function attr ($key, $value = false) {
  1286. if ($value === false) {
  1287. return $this->_attrs[$key];
  1288. } else {
  1289. $this->_attrs[$key] = $value;
  1290. return $value;
  1291. }
  1292. }
  1293. /**
  1294. * Use this method to remove an attribute from the form tag
  1295. * attribute list. Use this instead of passing a false value to attr(),
  1296. * because a false value essentially means "return the current value"
  1297. * in that method. This method returns the old value of the attribute
  1298. * being unset.
  1299. *
  1300. * @access public
  1301. * @param string $key
  1302. * @return string
  1303. *
  1304. */
  1305. function unsetAttr ($key) {
  1306. $old = $this->_attrs[$key];
  1307. unset ($this->_attrs[$key]);
  1308. return $old;
  1309. }
  1310. /**
  1311. * Returns a list of all of the attributes of this object's form tag
  1312. * in a string ready to be concatenated into the actual rendered tag output.
  1313. *
  1314. * @access public
  1315. * @return string
  1316. *
  1317. */
  1318. function getAttrs () {
  1319. $res = '';
  1320. foreach ($this->_attrs as $key => $value) {
  1321. if ($value === false) {
  1322. continue;
  1323. } elseif ($value === true) {
  1324. $res .= $key . '="' . $key . '" ';
  1325. } else {
  1326. $res .= $key . '="' . $value . '" ';
  1327. }
  1328. }
  1329. return $res;
  1330. }
  1331. function verifyRequestMethod () {
  1332. if (strtoupper ($_SERVER['REQUEST_METHOD']) != strtoupper ($this->method)) {
  1333. return false;
  1334. }
  1335. return true;
  1336. }
  1337. function verifyReferer () {
  1338. if (strpos ($_SERVER['HTTP_REFERER'], site_url ()) !== 0) {
  1339. return false;
  1340. }
  1341. return true;
  1342. }
  1343. }
  1344. ?>