PageRenderTime 68ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/saf/lib/MailForm/MailForm.php

https://github.com/cbrunet/sitellite
PHP | 1467 lines | 660 code | 103 blank | 704 comment | 155 complexity | 26c0b9df39c8de2f4b55ab877d919e37 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  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. * The context of the form. If this is set to 'action' then the title of
  496. * the form will be set by calling page_title() instead of outputting an
  497. * <h1> tag.
  498. */
  499. var $context = false;
  500. /**
  501. * Constructor Method. Action will be set to $PHP_SELF if it
  502. * is empty, unless a global $site object is defined in which case the
  503. * action with be $site->url . $PHP_SELF.
  504. *
  505. * @access public
  506. * @param string $action
  507. * @param string $method
  508. *
  509. */
  510. function MailForm ($action = '', $method = 'post') {
  511. $this->method = $method;
  512. if (empty ($action)) {
  513. if (function_exists ('site_current')) {
  514. $action = site_current ();
  515. } else {
  516. global $_SERVER;
  517. $action = $_SERVER['PHP_SELF'];
  518. }
  519. /*if (is_object ($GLOBALS['site'])) {
  520. global $site;
  521. $action = $site->url . $action;
  522. }*/
  523. }
  524. $this->action = $action;
  525. $GLOBALS['loader']->import ('saf.MailForm.Widget');
  526. // set up the $_attrs list
  527. $this->_attrs['action'] =& $this->action;
  528. $this->_attrs['method'] =& $this->method;
  529. $this->name = false;
  530. $this->_attrs['name'] =& $this->name;
  531. $this->handler = array (&$this, 'onSubmit');
  532. // attempt to automatically parse the settings.php file
  533. $cls = strtolower (get_class ($this));
  534. if ($cls != 'mailform') {
  535. if (function_exists ('site_current')) {
  536. $this->action = site_current ();
  537. } else {
  538. $this->action = $_SERVER['PHP_SELF'];
  539. }
  540. $this->_attrs['action'] =& $this->action;
  541. $app = loader_app ();
  542. if (strpos ($action, ':') === 1) {
  543. $action = str_replace ('\\', '/', $action);
  544. }
  545. list ($misc, $path) = explode ($app . '/forms/', $action);
  546. $box = dirname ($path);
  547. if (@file_exists ('inc/app/' . $app . '/forms/' . $box . '/settings.php')) {
  548. $this->parseSettings ('inc/app/' . $app . '/forms/' . $box . '/settings.php');
  549. }
  550. }
  551. }
  552. /**
  553. * Adds another widget to the form. If the $type is specified as
  554. * a loader path, it will import from the proper location outside of
  555. * saf.MailForm.Widget.*, and if you send an object as the $type value
  556. * addWidget() will use that object as the widget (so make sure it is one!),
  557. * as of version 3.8.
  558. *
  559. * @access public
  560. * @param string $type
  561. * @param string $name
  562. * @return object reference
  563. *
  564. */
  565. function &addWidget ($type, $name) {
  566. if (is_object ($type)) {
  567. $this->widgets[$name] =& $type;
  568. $this->widgets[$name]->form =& $this;
  569. return $this->widgets[$name];
  570. } elseif (strpos ($type, '.') !== false) {
  571. loader_import ($type);
  572. $pieces = explode ('.', $type);
  573. $cls = 'MF_Widget_' . strtolower (array_pop ($pieces));
  574. } else {
  575. $cls = 'MF_Widget_' . $type;
  576. loader_import ('saf.MailForm.Widget.' . ucfirst ($type));
  577. }
  578. $this->widgets[$name] = new $cls ($name);
  579. $this->widgets[$name]->form =& $this;
  580. return $this->widgets[$name];
  581. }
  582. /**
  583. * Validates the form values against a global $cgi object. Used in
  584. * the logic of "if the form is invalid then...". Also sets an internal
  585. * $invalid_field property. Returns true if the form is invalid or has not
  586. * been filled out yet. If the form passes (false returned), it also sets
  587. * the $submitted value to true.
  588. *
  589. * @access public
  590. * @return boolean
  591. *
  592. */
  593. function invalid () {
  594. global $cgi;
  595. $this->invalid = array ();
  596. $this->submitted = false;
  597. // determine if form has been submitted (based on submit buttons only)
  598. // making sure form still submits if there are more than one submit button
  599. $ret = true;
  600. $tpl = array ();
  601. foreach ($this->widgets as $k => $v) {
  602. if (in_array (strtolower (get_class ($v)), array ('mf_widget_submit', 'mf_widget_msubmit')) && isset ($cgi->{$k})) {
  603. $ret = false;
  604. break;
  605. } elseif (strtolower (get_class ($v)) == 'mf_widget_image' && isset ($cgi->{$k . '_x'})) {
  606. $ret = false;
  607. break;
  608. } elseif (strtolower (get_class ($v)) == 'mf_widget_template' && count ($v->submitButtons) > 0) {
  609. $tpl[$k] = $v->submitButtons;
  610. }
  611. }
  612. if ($ret) {
  613. foreach ($tpl as $k => $buttons) {
  614. foreach ($buttons as $button) {
  615. if (isset ($cgi->{$button})) {
  616. $ret = false;
  617. break;
  618. }
  619. }
  620. }
  621. if ($ret) {
  622. // form not yet submitted
  623. return true;
  624. }
  625. }
  626. $ret = false;
  627. foreach (array_keys ($this->widgets) as $key) {
  628. $widget =& $this->widgets[$key];
  629. // must compile value of widget here, so compound widgets can validate
  630. $widget_value = $widget->getValue ($cgi);
  631. if (in_array (strtolower (get_class ($widget)), array ('mf_widget_submit', 'mf_widget_msubmit', 'mf_widget_image'))) {
  632. // we've already checked submit buttons
  633. continue;
  634. } elseif (! $widget->passover_isset && ! isset ($cgi->{$key})) {
  635. // specified field is missing, assuming form not yet submitted
  636. return true;
  637. }
  638. //if ($key == 'password') {
  639. // echo 'aha!';
  640. //}
  641. if (! $widget->validate ($widget_value, $this, $cgi)) {
  642. if (! $ret) {
  643. $this->invalid_field = $widget->name;
  644. $ret = true;
  645. }
  646. $this->invalid[$widget->name] = $widget->error_message;
  647. }
  648. }
  649. $this->submitted = true;
  650. if ($ret) {
  651. return true;
  652. }
  653. if ($this->verify_sender) {
  654. if (! $this->verifyRequestMethod ()) {
  655. die ('Invalid request method!');
  656. }
  657. if (! $this->verifyReferer ()) {
  658. die ('Invalid referrer!');
  659. }
  660. }
  661. return false;
  662. }
  663. /**
  664. * Manually set a specific field to be invalid, including a custom error message.
  665. * Useful for setting an error message during the onSubmit() before calling
  666. * return $this->show(); again.
  667. *
  668. * @access public
  669. * @param string $field
  670. * @param string $message
  671. *
  672. */
  673. function setInvalid ($field, $message) {
  674. $this->invalid_field = $field;
  675. $this->invalid[$field] = $message;
  676. $this->widgets[$field]->error_message = $message;
  677. }
  678. /**
  679. * Sets the values of the form widgets from a provided CGI object.
  680. * The second parameter is deprecated.
  681. *
  682. * @access public
  683. * @param object $cgi
  684. * @param boolean $invalid
  685. *
  686. */
  687. function setValues ($cgi, $invalid = false) {
  688. foreach ($cgi->param as $key) {
  689. $value = $cgi->{$key};
  690. if (is_object ($this->widgets[$key])) {
  691. $this->widgets[$key]->setValue ($value);
  692. } elseif (ereg ('^(MF_)+([a-zA-Z0-9-]+)_([A-Z0-9_]+)$', $key, $regs)) {
  693. //echo '<pre>'; print_r ($regs); echo '</pre>';
  694. if (is_object ($this->widgets[$regs[2]])) {
  695. $this->widgets[$regs[2]]->setValue ($value, $regs[3]);
  696. }
  697. }
  698. }
  699. // loop through widgets who are not set but who have passover_isset set to true
  700. // and set their values to ''.
  701. if ($this->submitted) {
  702. foreach ($this->widgets as $key => $widget) {
  703. if ($widget->passover_isset && ! isset ($cgi->{$key})) {
  704. $this->widgets[$key]->setValue ('');
  705. }
  706. }
  707. }
  708. $this->cgi = $cgi;
  709. }
  710. /**
  711. * Generates the HTML form. You can provide an optional template
  712. * to customize the look of the form. Template directives (ie. ##field##)
  713. * must be provided for each form widget, as well as ##mailform_action## and
  714. * ##mailform_method##, which correspond to the action and method attributes
  715. * of the HTML form tag.
  716. *
  717. * @access public
  718. * @param string $template
  719. * @return string
  720. *
  721. */
  722. function show ($template = '') {
  723. /*if (session_pref ('browse_level') == 'normal') {
  724. foreach ($this->widgets as $key => $widget) {
  725. if ($widget->advanced) {
  726. $this->widgets[$key] =& $this->widgets[$key]->changeType ('hidden');
  727. }
  728. }
  729. }*/
  730. if (! empty ($template)) {
  731. // last minute template setting
  732. $this->template = $template;
  733. }
  734. if ($this->formhelp) {
  735. page_add_script (site_prefix () . '/js/formhelp-compressed.js');
  736. }
  737. if ($this->autosave) {
  738. global $cgi;
  739. loader_import ('saf.MailForm.Autosave');
  740. $a = new Autosave ();
  741. if ($a->has_draft ()) {
  742. if ($cgi->_autosave_start_from == intl_get ('Restore your previous editing session')) {
  743. $data = $a->retrieve ();
  744. foreach ($data as $k => $v) {
  745. if (is_object ($this->widgets[$k])) {
  746. $this->widgets[$k]->setValue ($v);
  747. }
  748. $cgi->{$k} = $v;
  749. $cgi->param[] = $k;
  750. }
  751. } elseif ($cgi->_autosave_start_from == intl_get ('Start from the currently saved version')) {
  752. $a->clear ();
  753. } else {
  754. // has an existing autosave version, prompt for action
  755. return '<p>' . intl_get ('An auto-saved edit of this form has been found from a previous editing session. Would you like to continue from your previous editing session, or start from the currently saved version?') . '</p>'
  756. . '<form method="post" action="' . $_SERVER['REQUEST_URI'] . '">'
  757. . '<p><input type="submit" name="_autosave_start_from" value="' . intl_get ('Restore your previous editing session') . '" /> &nbsp; &nbsp; '
  758. . '<input type="submit" name="_autosave_start_from" value="' . intl_get ('Start from the currently saved version') . '" /></p>'
  759. . '</form>';
  760. }
  761. }
  762. page_add_script (site_prefix () . '/js/rpc.js');
  763. page_add_script (site_prefix () . '/js/autosave.js');
  764. }
  765. if (! empty ($this->template)) {
  766. // use template to display form
  767. $form_contents = array ();
  768. // determine appropriate instructional message
  769. if (! empty ($this->invalid_field)) {
  770. if ($this->error_mode == 'single') {
  771. // display only the first error message (the default)
  772. if (! empty ($this->widgets[$this->invalid_field]->error_message)) {
  773. $form_contents['mailform_message'] = $this->widgets[$this->invalid_field]->error_message;
  774. } else {
  775. $form_contents['mailform_message'] = intl_get ('Oops! The following field was not filled in correctly:') . ' ' .
  776. $this->widgets[$this->invalid_field]->display_value .
  777. '. ' . intl_get ('Please fix this before continuing.');
  778. }
  779. $this->widgets[$this->invalid_field]->invalid = true;
  780. } else {
  781. // display all error messages
  782. $form_contents['mailform_message'] = intl_get ('Oops! The following information must be corrected in order to continue:') . NEWLINE;
  783. $form_contents['mailform_message'] .= '<ul>' . NEWLINE;
  784. foreach ($this->invalid as $name => $message) {
  785. $form_contents['mailform_message'] .= TAB . '<li>' . $message . '</li>' . NEWLINE;
  786. $this->widgets[$name]->invalid = true;
  787. }
  788. $form_contents['mailform_message'] .= '</ul>' . NEWLINE;
  789. }
  790. } else {
  791. $form_contents['mailform_message'] = $this->message;
  792. }
  793. foreach ($this->widgets as $key =>$widget) {
  794. //$form_contents[$key] = str_replace ('##', 'SITELLITE_DOUBLE_POUND_SUBSTITUTION', $widget->display (0));
  795. if (! is_object ($this->widgets[$key]->form)) {
  796. $this->widgets[$key]->form =& $this;
  797. }
  798. $form_contents[$key] = $this->widgets[$key]->display (0);
  799. }
  800. $form_contents['mailform_title'] = $this->title;
  801. $form_contents['mailform_method'] = $this->method;
  802. $form_contents['mailform_action'] = $this->action;
  803. $form_contents['mailform_extra'] = $this->extra;
  804. $form_contents['mailform_attrs'] = $this->getAttrs ();
  805. global $simple;
  806. //$loader->import ('saf.Template.Simple');
  807. //$tpl = new SimpleTemplate ('', SIMPLE_TEMPLATE_DELIM_POUND);
  808. //$simple = new SimpleTemplate ('');
  809. $return_data = $simple->fill ($this->template, $form_contents);
  810. //return str_replace ('SITELLITE_DOUBLE_POUND_SUBSTITUTION', '##', $return_data);
  811. return $return_data;
  812. } else {
  813. // determine appropriate instructional message
  814. $_message = '';
  815. if (! empty ($this->invalid_field)) {
  816. if ($this->error_mode == 'single') {
  817. if (! empty ($this->widgets[$this->invalid_field]->error_message)) {
  818. $_message .= '<p class="invalid">' . $this->widgets[$this->invalid_field]->error_message . "</p>\n";
  819. } else {
  820. $_message .= '<p class="invalid">' . intl_get ('Oops! The following field was not filled in correctly:') . ' ' .
  821. $this->widgets[$this->invalid_field]->display_value .
  822. '. ' . intl_get ('Please fix this before continuing.') . "</p>\n";
  823. }
  824. $this->widgets[$this->invalid_field]->invalid = true;
  825. } else {
  826. // display all error messages
  827. $_message .= '<p class="invalid">' . intl_get ('Oops! The following information must be corrected in order to continue:') . NEWLINE;
  828. $_message .= '<ul>' . NEWLINE;
  829. foreach ($this->invalid as $name => $message) {
  830. $_message .= TAB . '<li>' . $message . '</li>' . NEWLINE;
  831. $this->widgets[$name]->invalid = true;
  832. }
  833. $_message .= '</ul>' . NEWLINE . '</p>' . NEWLINEx2;
  834. }
  835. } elseif (! empty ($this->message)) {
  836. $_message .= '<p>' . $this->message . "</p>\n";
  837. }
  838. $_widgets = '';
  839. foreach ($this->widgets as $key => $widget) {
  840. if (! method_exists ($this->widgets[$key], 'display')) {
  841. die ('Widget "' . $key . '" has no display() method!');
  842. }
  843. if (! is_object ($this->widgets[$key]->form)) {
  844. $this->widgets[$key]->form =& $this;
  845. }
  846. $_widgets .= $this->widgets[$key]->display (1);
  847. }
  848. $attrstr = $this->getAttrs ();
  849. if (! empty ($this->name)) {
  850. $data = '<form ' . $attrstr . ' ' . $this->extra . '>' . "\n";
  851. } else {
  852. $data = '<form ' . $attrstr . ' ' . $this->extra . '>' . "\n";
  853. }
  854. if (! empty ($this->title)) {
  855. if ($this->context == 'action') {
  856. page_title ($this->title);
  857. } else {
  858. $data .= '<h1>' . $this->title . '</h1>';
  859. }
  860. }
  861. $data .= $_message;
  862. $align = empty ($this->align) ? 'center' : $this->align;
  863. $data .= '<table border="0" cellspacing="1" cellpadding="3" align="' . $align . '">' . "\n";
  864. $data .= $_widgets;
  865. return $data . '</table>' . "\n" . '</form>';
  866. }
  867. }
  868. /**
  869. * Returns the form values as an associative array. If $uploadFiles
  870. * is set to true, it will return the saved path or false for file widgets,
  871. * otherwise it will return the saf.CGI.UploadedFile object and not act
  872. * on the object for you.
  873. *
  874. * @access public
  875. * @param boolean $uploadFiles
  876. * @return associative array
  877. *
  878. */
  879. function getValues () {
  880. global $cgi;
  881. $uploadFiles = $this->uploadFiles;
  882. $return = array ();
  883. foreach ($this->widgets as $key => $obj) {
  884. if ($uploadFiles && strtolower (get_class ($obj)) == 'mf_widget_file') {
  885. $return[$key] = $obj->move ();
  886. } else if ($uploadFiles && strtolower (get_class ($obj)) == 'mf_widget_mfile') {
  887. $return[$key] = $obj->move ();
  888. } else {
  889. if (is_object ($cgi)) {
  890. $return[$key] = $obj->getValue ($cgi);
  891. } else {
  892. $return[$key] = $obj->getValue ();
  893. }
  894. }
  895. }
  896. return $return;
  897. }
  898. /**
  899. * Sets the function to use to handle the output of the current form.
  900. * To specify a method of an object, pass it an array with an object reference
  901. * as the first element and the method name as the second. The default handler
  902. * is the internal onSubmit(). The handler is called using call_user_func()
  903. * in the run() method.
  904. *
  905. * @access public
  906. * @param mixed $func
  907. *
  908. */
  909. function setHandler ($func) {
  910. $this->handler = $func;
  911. }
  912. /**
  913. * Runs the form and returns either the rendered form or the output
  914. * of the handler function. $uploadFiles can be set to false to cause the
  915. * getValues() method not to call move() on File widgets. This is useful
  916. * for situations when you need to do something other than simply save the
  917. * file to a predetermined folder. Please note: The $uploadFiles parameter
  918. * is deprecated in favour of the $uploadFiles property of the MailForm
  919. * class. This allows the setting to be managed via a settings.php file.
  920. *
  921. * @access public
  922. * @param boolean $uploadFiles
  923. * @return string
  924. *
  925. */
  926. function run ($uploadFiles = true) {
  927. global $cgi;
  928. if (! $uploadFiles) {
  929. $this->uploadFiles = $uploadFiles;
  930. }
  931. if ($this->invalid ($cgi)) {
  932. $this->setValues ($cgi);
  933. if ($this->verify_session) {
  934. @session_start ();
  935. $_SESSION['mf_verify_session'] = 'mf_verified';
  936. }
  937. return $this->show ();
  938. } else {
  939. if ($this->verify_session) {
  940. @session_start ();
  941. if ($_SESSION['mf_verify_session'] != 'mf_verified') {
  942. die ('This form requires that you enable cookies in your browser, which helps us to prevent abuse of our forms by automated spam systems.');
  943. }
  944. }
  945. if ($this->blacklist) {
  946. if (db_shift ('select count(*) from sitellite_form_blacklist where ip_address = ?', $_SERVER['REMOTE_ADDR'])) {
  947. 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.');
  948. }
  949. }
  950. if ($this->autosave) {
  951. loader_import ('saf.MailForm.Autosave');
  952. $a = new Autosave ();
  953. $a->clear ($_SERVER['HTTP_REFERER']);
  954. }
  955. $this->setValues ($cgi);
  956. $vals = $this->getValues ();
  957. if ($this->clean_input) {
  958. foreach ($vals as $k => $v) {
  959. $vals[$k] = strip_tags ($v);
  960. }
  961. }
  962. return call_user_func ($this->handler, $vals);
  963. }
  964. }
  965. /**
  966. * This is the default handler function. It is called via run()
  967. * and can be overridden via subclassing.
  968. *
  969. * @access public
  970. * @param array $vals
  971. * @return string
  972. *
  973. */
  974. function onSubmit ($vals) {
  975. if (! empty ($this->sendTo) || ! empty ($this->email)) {
  976. $email = $this->sendTo;
  977. $subject = $this->sendSubject;
  978. $from_field = $this->sendFrom;
  979. $extra = $this->sendExtra;
  980. global $site;
  981. $message_body = intl_get ('The following information has been sent from') . ' ' . $site->url . $GLOBALS['PHP_SELF'] . ":\n\n";
  982. foreach ($vals as $key => $value) {
  983. if ($this->widgets[$key]->type == 'separator') {
  984. $message_body .= "\n----------\n\n";
  985. } elseif ($this->widgets[$key]->type == 'section') {
  986. $message_body .= $this->widgets[$key]->title . "\n\n";
  987. } else {
  988. if (strlen ($this->widgets[$key]->alt) > 30) {
  989. $alt = substr ($this->widgets[$key]->alt, 0, 27) . '...';
  990. } else {
  991. $alt = $this->widgets[$key]->alt;
  992. }
  993. $message_body .= str_pad ($alt, 35) . ": " . $value . "\n";
  994. }
  995. }
  996. if (! empty ($from_field)) {
  997. if (strpos ($vars[$from_field], "\n") !== false) {
  998. die ('Invalid from field value.');
  999. }
  1000. $from_field = 'From: ' . $vals[$from_field] . "\r\n";
  1001. }
  1002. //echo '<pre>' . htmlentities_compat ($message_body) . '</pre>';
  1003. if (@mail ($email, $subject, $message_body, $from_field . $extra)) {
  1004. return $this->screenReply;
  1005. } else {
  1006. $this->error_message = intl_get ('Email could not sent. Please verify that your mail daemon is functioning correctly.');
  1007. return $this->error_message;
  1008. }
  1009. }
  1010. $this->error_message = intl_get ('No email address set. Please check your form settings.');
  1011. return $this->error_message;
  1012. }
  1013. /**
  1014. * Note: Deprecated in favour of the setHandler() and run() methods
  1015. * and subclassing. Handles the form, once it has been satisfactorily completed.
  1016. * If the first parameter points to a file (ie. 'inc/forms/contact.php'), it will
  1017. * use that file as an "action file" to handle the form. Otherwise, the first
  1018. * parameter must be an email address, as handle () will simply send an email
  1019. * of the form contents to the specified email address. Note: Extra will be
  1020. * passed to the PHP mail () function as a fourth parameter, or can be used for
  1021. * any purpose you'd like in an action file.
  1022. *
  1023. * @access public
  1024. * @param string $email
  1025. * @param string $subject
  1026. * @param string $from_field
  1027. * @param string $extra
  1028. * @return boolean
  1029. *
  1030. */
  1031. function handle ($email, $subject = 'Mail Form', $from_field = '', $extra = '') {
  1032. if (! strstr ($email, '@')) {
  1033. //echo 'using actionfile: ' . $email . '<br />';
  1034. // $email is an action file, so give it some action! ;)
  1035. if (strstr ($email, '..')) {
  1036. // we don't like file paths with .. in them. they really should never be needed.
  1037. $this->error_message = intl_get ('Sorry, no action file names with ".." in them.');
  1038. return false;
  1039. } elseif (! @file_exists ($email)) {
  1040. $this->error_message = intl_get ('Sorry, the action file you have specified could not be found.');
  1041. return false;
  1042. } else {
  1043. include_once ($email);
  1044. return true;
  1045. }
  1046. } else {
  1047. // send email
  1048. //echo 'using email address: ' . $email . '<br />';
  1049. $vars = $this->getValues ();
  1050. global $site;
  1051. $message_body = $this->formatEmail ($vars);
  1052. if (! empty ($from_field)) {
  1053. if (strpos ($vars[$from_field], "\n") !== false) {
  1054. die ('Invalid from field value.');
  1055. }
  1056. $from_field = 'From: ' . $vars[$from_field] . "\r\n";
  1057. }
  1058. //echo '<pre>' . htmlentities_compat ($message_body) . '</pre>';
  1059. if (@mail ($email, $subject, $message_body, $from_field . $extra)) {
  1060. return true;
  1061. } else {
  1062. $this->error_message = intl_get ('Email could not sent. Please verify that your mail daemon is functioning correctly.');
  1063. return false;
  1064. }
  1065. //echo "<pre>mail ('$email', '$subject', '$message_body', '$from_field$extra')</pre>";
  1066. //return true;
  1067. }
  1068. }
  1069. function formatEmail ($vals) {
  1070. global $site;
  1071. $message_body = intl_get ('The following information has been sent from') . ' ' . $site->url . $GLOBALS['PHP_SELF'] . ":\n\n";
  1072. foreach ($vals as $key => $value) {
  1073. if ($this->widgets[$key]->type == 'separator') {
  1074. $message_body .= "\n----------\n\n";
  1075. } elseif ($this->widgets[$key]->type == 'section') {
  1076. $message_body .= $this->widgets[$key]->title . "\n\n";
  1077. } else {
  1078. if (strlen ($this->widgets[$key]->alt) > 30) {
  1079. $alt = substr ($this->widgets[$key]->alt, 0, 27) . '...';
  1080. } else {
  1081. $alt = $this->widgets[$key]->alt;
  1082. }
  1083. $message_body .= str_pad ($alt, 35) . ": " . $value . "\n";
  1084. }
  1085. }
  1086. return $message_body;
  1087. }
  1088. /**
  1089. * Parses the specified file using the parse_ini_file()
  1090. * function. Sections in the file correspond to the names of
  1091. * widgets you wish to create, in addition to a [Form] section
  1092. * that sets properties for the form itself. The values in
  1093. * each section correspond to properties or methods of the
  1094. * widgets. This method can be used to simplify the process
  1095. * of defining and customizing a form.
  1096. *
  1097. * @access public
  1098. * @param string $file
  1099. * @return boolean
  1100. *
  1101. */
  1102. function parseSettings ($file) {
  1103. if (! @file_exists ($file)) {
  1104. return false;
  1105. }
  1106. ini_add_filter ('ini_filter_split_comma_single', array (
  1107. 'rule 0', 'rule 1', 'rule 2', 'rule 3', 'rule 4', 'rule 5', 'rule 6', 'rule 7', 'rule 8',
  1108. 'button 0', 'button 1', 'button 2', 'button 3', 'button 4', 'button 5', 'button 6', 'button 7', 'button 8',
  1109. 'submitButtons',
  1110. ));
  1111. $conf = ini_parse ($file, true);
  1112. ini_clear ();
  1113. if (count ($conf) == 0) {
  1114. return false;
  1115. }
  1116. // form properties, optional
  1117. if (is_array ($conf['Form'])) {
  1118. foreach ($conf['Form'] as $key => $value) {
  1119. if (($key == 'title' || $key == 'message') && function_exists ('intl_get')) {
  1120. $value = intl_get ($value);
  1121. }
  1122. $this->{$key} = $value;
  1123. }
  1124. unset ($conf['Form']);
  1125. }
  1126. foreach ($conf as $name => $data) {
  1127. $this->createWidget ($name, $data);
  1128. }
  1129. return true;
  1130. }
  1131. /**
  1132. * Creates a widget from a name and an array, usually taken from a parsed
  1133. * settings.php (ini formatted) file.
  1134. *
  1135. * @access public
  1136. * @param string
  1137. * @param array hash
  1138. */
  1139. function &createWidget ($name, $data) {// create widget
  1140. $type = $data['type'];
  1141. unset ($data['type']);
  1142. $widget =& $this->addWidget ($type, $name);
  1143. // handle setValues
  1144. if (! empty ($data['setValues'])) {
  1145. if (strpos ($data['setValues'], 'eval:') === 0) {
  1146. eval (CLOSE_TAG . OPEN_TAG . ' $widget->setValues (' . substr ($data['setValues'], 5) . '); ' . CLOSE_TAG);
  1147. } elseif (preg_match ('/, ?/', $data['setValues'])) {
  1148. $widget->setValues (preg_split ('/, ?/', $data['setValues']));
  1149. // eval (CLOSE_TAG . OPEN_TAG . ' $widget->setValues (array (' . $data['setValues'] . ')); ' . CLOSE_TAG);
  1150. } else {
  1151. if (strpos ($data['setValues'], '\'') === 0 && strrpos ($data['setValues'], '\'') == strlen ($data['setValues']) - 1) {
  1152. $data['setValues'] = substr ($data['setValues'], 1, strlen ($data['setValues']) - 2);
  1153. }
  1154. $widget->setValues ($data['setValues']);
  1155. }
  1156. // eval (CLOSE_TAG . OPEN_TAG . ' $widget->setValues (' . $data['setValues'] . '); ' . CLOSE_TAG);
  1157. }
  1158. unset ($data['setValues']);
  1159. if (! empty ($data['setDefault'])) {
  1160. if (strpos ($data['setDefault'], 'eval:') === 0) {
  1161. eval (CLOSE_TAG . OPEN_TAG . ' $widget->setDefault (' . substr ($data['setDefault'], 5) . '); ' . CLOSE_TAG);
  1162. } else {
  1163. $widget->setDefault ($data['setDefault']);
  1164. }
  1165. }
  1166. if (! empty ($data['setValue'])) {
  1167. if (strpos ($data['setValue'], 'eval:') === 0) {
  1168. eval (CLOSE_TAG . OPEN_TAG . ' $widget->setValue (' . substr ($data['setValue'], 5) . '); ' . CLOSE_TAG);
  1169. } else {
  1170. $widget->setValue ($data['setValue']);
  1171. }
  1172. }
  1173. if (! empty ($data['setRules'])) {
  1174. if (strpos ($data['setRules'], 'eval:') === 0) {
  1175. eval (CLOSE_TAG . OPEN_TAG . ' $widget->rules = ' . substr ($data['setRules'], 5) . '); ' . CLOSE_TAG);
  1176. }
  1177. }
  1178. // handle rules
  1179. //foreach (preg_split ('/, ?/', $data['rules'], -1, PREG_SPLIT_NO_EMPTY) as $rule) {
  1180. // $widget->addRule ($rule);
  1181. //}
  1182. //unset ($data['rules']);
  1183. // widget properties
  1184. foreach ($data as $key => $value) {
  1185. if (strpos ($key, 'rule ') === 0) {
  1186. if (is_array ($value)) {
  1187. if (function_exists ('intl_get')) {
  1188. $value[1] = intl_get ($value[1]);
  1189. }
  1190. $widget->addRule ($value[0], $value[1]);
  1191. } else {
  1192. $widget->addRule ($value);
  1193. }
  1194. } elseif (strpos ($key, 'value ') === 0) {
  1195. $widget->setValues ($value, $value);
  1196. } elseif ($type == 'msubmit' && strpos ($key, 'button ') === 0) {
  1197. $b =& $widget->getButton ();
  1198. if (empty ($b->value)) {
  1199. if (is_array ($value)) {
  1200. if (function_exists ('intl_get')) {
  1201. $value[0] = intl_get ($value[0]);
  1202. }
  1203. $b->setValues ($value[0]);
  1204. $b->extra = $value[1];
  1205. } else {
  1206. if (function_exists ('intl_get')) {
  1207. $value = intl_get ($value);
  1208. }
  1209. $b->setValues ($value);
  1210. }
  1211. } else {
  1212. if (is_array ($value)) {
  1213. if (function_exists ('intl_get')) {
  1214. $value[0] = intl_get ($value[0]);
  1215. }
  1216. $b =& $widget->addButton ($name, $value[0]);
  1217. $b->extra = $value[1];
  1218. } else {
  1219. if (function_exists ('intl_get')) {
  1220. $value = intl_get ($value);
  1221. }
  1222. $widget->addButton ($name, $value);
  1223. }
  1224. }
  1225. } elseif ($key == 'alt') {
  1226. if (function_exists ('intl_get')) {
  1227. $widget->{$key} = intl_get ($value);
  1228. } else {
  1229. $widget->{$key} = $value;
  1230. }
  1231. } elseif (method_exists ($widget, $key)) {
  1232. $widget->{$key} ($value);
  1233. } else {
  1234. $widget->{$key} = $value;
  1235. }
  1236. }
  1237. if (! empty ($widget->formhelp) && session_pref ('form_help') == 'on') {
  1238. $widget->extra .= ' onfocus="formhelp_show (this, \'' . addslashes (intl_get ($widget->formhelp)) . '\')" onblur="formhelp_hide ()"';
  1239. }
  1240. unset ($widget);
  1241. return $this->widgets[$name];
  1242. }
  1243. /**
  1244. * Takes a non-associative array and creates an associative array
  1245. * out of its values. This is used to send non-associative arrays to the
  1246. * setValues() method of the Widget objects.
  1247. *
  1248. * @access public
  1249. * @param array $list
  1250. * @return associative array
  1251. *
  1252. */
  1253. function makeAssoc ($list) {
  1254. $new = array ();
  1255. foreach ($list as $key => $value) {
  1256. if (! is_int ($key)) {
  1257. $new[$key] = $value;
  1258. } else {
  1259. $new[$value] = $value;
  1260. }
  1261. }
  1262. return $new;
  1263. }
  1264. /**
  1265. * Takes a non-associative array listing the names of each field
  1266. * from $cgi you want to "remember", and creates hidden fields for each
  1267. * of them, so you don't have to hard-code lists of hidden fields in
  1268. * multi-screen forms.
  1269. *
  1270. * @access public
  1271. * @param array $list
  1272. *
  1273. */
  1274. function rememberFields ($list) {
  1275. foreach ($list as $field) {
  1276. $this->addWidget ('hidden', $field);
  1277. }
  1278. }
  1279. /**
  1280. * This is the accessor method for setting and getting the value of
  1281. * any attribute of the form tag, including 'method' and 'action'. This will
  1282. * replace the $extra property, which is henceforth deprecated. If you call
  1283. * this method and provide no $value, you are using it as a 'getter', as in
  1284. * you are getting the current value. If you provide a value, the new value
  1285. * will be set, so you are acting as a 'setter'. If you simply specify that
  1286. * the $value be true, then it will appear filled with its own name (useful
  1287. * for things like the checked="checked" attribute of a checkbox input field,
  1288. * even though this isn't a checkbox field).
  1289. *
  1290. * @access public
  1291. * @param string $key
  1292. * @param string $key
  1293. * @return string
  1294. *
  1295. */
  1296. function attr ($key, $value = false) {
  1297. if ($value === false) {
  1298. return $this->_attrs[$key];
  1299. } else {
  1300. $this->_attrs[$key] = $value;
  1301. return $value;
  1302. }
  1303. }
  1304. /**
  1305. * Use this method to remove an attribute from the form tag
  1306. * attribute list. Use this instead of passing a false value to attr(),
  1307. * because a false value essentially means "return the current value"
  1308. * in that method. This method returns the old value of the attribute
  1309. * being unset.
  1310. *
  1311. * @access public
  1312. * @param string $key
  1313. * @return string
  1314. *
  1315. */
  1316. function unsetAttr ($key) {
  1317. $old = $this->_attrs[$key];
  1318. unset ($this->_attrs[$key]);
  1319. return $old;
  1320. }
  1321. /**
  1322. * Returns a list of all of the attributes of this object's form tag
  1323. * in a string ready to be concatenated into the actual rendered tag output.
  1324. *
  1325. * @access public
  1326. * @return string
  1327. *
  1328. */
  1329. function getAttrs () {
  1330. $res = '';
  1331. foreach ($this->_attrs as $key => $value) {
  1332. if ($value === false) {
  1333. continue;
  1334. } elseif ($value === true) {
  1335. $res .= $key . '="' . $key . '" ';
  1336. } else {
  1337. $res .= $key . '="' . $value . '" ';
  1338. }
  1339. }
  1340. return $res;
  1341. }
  1342. function verifyRequestMethod () {
  1343. if (strtoupper ($_SERVER['REQUEST_METHOD']) != strtoupper ($this->method)) {
  1344. return false;
  1345. }
  1346. return true;
  1347. }
  1348. function verifyReferer () {
  1349. if (strpos ($_SERVER['HTTP_REFERER'], site_url ()) !== 0) {
  1350. return false;
  1351. }
  1352. return true;
  1353. }
  1354. }
  1355. ?>