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

/system/expressionengine/third_party/freeform/mod.freeform.php

https://bitbucket.org/studiobreakfast/sync
PHP | 3621 lines | 2148 code | 670 blank | 803 comment | 272 complexity | 5d3ca1dbaaa9b76e0758cdc55ddf847f MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Solspace - Freeform
  4. *
  5. * @package Solspace:Freeform
  6. * @author Solspace DevTeam
  7. * @copyright Copyright (c) 2008-2012, Solspace, Inc.
  8. * @link http://solspace.com/docs/addon/c/Freeform/
  9. * @version 4.0.8
  10. * @filesource ./system/expressionengine/third_party/freeform/
  11. */
  12. /**
  13. * Freeform - User Side
  14. *
  15. * @package Solspace:Freeform
  16. * @author Solspace DevTeam
  17. * @filesource ./system/expressionengine/third_party/freeform/mod.freeform.php
  18. */
  19. // EE 2.0's Wizard might not set this constant
  20. if ( ! defined('APP_VER')) define('APP_VER', '2.0');
  21. if ( ! class_exists('Module_builder_freeform'))
  22. {
  23. require_once 'addon_builder/module_builder.php';
  24. }
  25. class Freeform extends Module_builder_freeform
  26. {
  27. public $return_data = '';
  28. public $disabled = FALSE;
  29. public $multipart = FALSE;
  30. public $params = array();
  31. public $params_id = 0;
  32. public $form_id = 0;
  33. // --------------------------------------------------------------------
  34. /**
  35. * Constructor
  36. *
  37. * @access public
  38. * @return null
  39. */
  40. public function __construct ()
  41. {
  42. parent::__construct('freeform');
  43. // -------------------------------------
  44. // Module Installed and Up to Date?
  45. // -------------------------------------
  46. if ($this->database_version() == FALSE OR
  47. $this->version_compare($this->database_version(), '<', FREEFORM_VERSION)
  48. OR ! $this->extensions_enabled())
  49. {
  50. $this->disabled = TRUE;
  51. trigger_error(lang('freeform_module_disabled'), E_USER_NOTICE);
  52. }
  53. ee()->load->helper(array('text', 'form', 'url', 'string'));
  54. //avoids AR collisions
  55. $this->data->get_module_preferences();
  56. $this->data->get_global_module_preferences();
  57. $this->data->show_all_sites();
  58. }
  59. // END __construct()
  60. // --------------------------------------------------------------------
  61. /**
  62. * Form Info
  63. *
  64. * @access public
  65. * @return string parsed tagdata
  66. */
  67. public function form_info ()
  68. {
  69. $form_ids = $this->form_id(TRUE);
  70. ee()->load->model('freeform_form_model');
  71. if ($form_ids)
  72. {
  73. ee()->freeform_form_model->where_in('form_id', $form_ids);
  74. }
  75. // -------------------------------------
  76. // site ids
  77. // -------------------------------------
  78. //if its star, allow all
  79. if (ee()->TMPL->fetch_param('site_id') !== '*')
  80. {
  81. $site_id = $this->parse_numeric_array_param('site_id');
  82. //if this isn't false, its single or an array
  83. if ($site_id !== FALSE)
  84. {
  85. if (empty($site_id['ids']))
  86. {
  87. ee()->freeform_form_model->reset();
  88. return $this->no_results_error();
  89. }
  90. else if ($site_id['not'])
  91. {
  92. ee()->freeform_form_model->where_not_in('site_id', $site_id['ids']);
  93. }
  94. else
  95. {
  96. ee()->freeform_form_model->where_in('site_id', $site_id['ids']);
  97. }
  98. }
  99. //default
  100. else
  101. {
  102. ee()->freeform_form_model->where('site_id', ee()->config->item('site_id'));
  103. }
  104. }
  105. // -------------------------------------
  106. // form data
  107. // -------------------------------------
  108. $form_data = ee()->freeform_form_model
  109. ->select(
  110. 'form_id, site_id, ' .
  111. 'form_name, form_label, ' .
  112. 'form_description, author_id, ' .
  113. 'entry_date, edit_date'
  114. )
  115. ->order_by('form_id', 'asc')
  116. ->get();
  117. if ( ! $form_data)
  118. {
  119. return $this->no_results_error(($form_ids) ? 'invalid_form_id' : NULL);
  120. }
  121. // -------------------------------------
  122. // author data
  123. // -------------------------------------
  124. $author_ids = array();
  125. $author_data = array();
  126. foreach ($form_data as $row)
  127. {
  128. $author_ids[] = $row['author_id'];
  129. }
  130. $a_query = ee()->db->select('member_id, username, screen_name')
  131. ->from('members')
  132. ->where_in('member_id', array_unique($author_ids))
  133. ->get();
  134. if ($a_query->num_rows() > 0)
  135. {
  136. $author_data = $this->prepare_keyed_result(
  137. $a_query,
  138. 'member_id'
  139. );
  140. }
  141. // -------------------------------------
  142. // output
  143. // -------------------------------------
  144. $variables = array();
  145. ee()->load->model('freeform_entry_model');
  146. foreach ($form_data as $row)
  147. {
  148. $new_row = array();
  149. foreach ($row as $key => $value)
  150. {
  151. $new_row['freeform:' . $key] = $value;
  152. }
  153. $new_row['freeform:total_entries'] = ee()->freeform_entry_model
  154. ->id($row['form_id'])
  155. ->where('complete', 'y')
  156. ->count();
  157. $new_row['freeform:author'] = (
  158. isset($author_data[$row['author_id']]) ?
  159. (
  160. isset($author_data[$row['author_id']]['screen_name']) ?
  161. $author_data[$row['author_id']]['screen_name'] :
  162. $author_data[$row['author_id']]['username']
  163. ) :
  164. lang('n_a')
  165. );
  166. $variables[] = $new_row;
  167. }
  168. $prefixed_tags = array(
  169. 'count',
  170. 'switch',
  171. 'total_results'
  172. );
  173. $tagdata = ee()->TMPL->tagdata;
  174. $tagdata = $this->tag_prefix_replace('freeform:', $prefixed_tags, $tagdata);
  175. //this should handle backspacing as well
  176. $tagdata = ee()->TMPL->parse_variables($tagdata, $variables);
  177. $tagdata = $this->tag_prefix_replace('freeform:', $prefixed_tags, $tagdata, TRUE);
  178. return $tagdata;
  179. }
  180. //END form_info
  181. // --------------------------------------------------------------------
  182. /**
  183. * Freeform:Entries
  184. * {exp:freeform:entries}
  185. *
  186. * @access public
  187. * @return string tagdata
  188. */
  189. public function entries ()
  190. {
  191. // -------------------------------------
  192. // form id
  193. // -------------------------------------
  194. $form_ids = $this->form_id(TRUE);
  195. if ( ! $form_ids)
  196. {
  197. return $this->no_results_error('invalid_form_id');
  198. }
  199. if ( ! is_array($form_ids))
  200. {
  201. $form_ids = array($form_ids);
  202. }
  203. // -------------------------------------
  204. // libs, models, helper
  205. // -------------------------------------
  206. ee()->load->model('freeform_form_model');
  207. ee()->load->model('freeform_entry_model');
  208. ee()->load->model('freeform_field_model');
  209. ee()->load->library('freeform_forms');
  210. ee()->load->library('freeform_fields');
  211. // -------------------------------------
  212. // start cache for count and result
  213. // -------------------------------------
  214. $forms_data = ee()->freeform_form_model
  215. ->key('form_id')
  216. ->get(array('form_id' => $form_ids));
  217. $statuses = array_keys($this->data->get_form_statuses());
  218. // -------------------------------------
  219. // field data
  220. // -------------------------------------
  221. $all_field_ids = array();
  222. $all_order_ids = array();
  223. foreach ($forms_data as $form_data)
  224. {
  225. //this should always be true, but NEVER TRUST AN ELF
  226. if (isset($form_data['field_ids']) AND
  227. is_array($form_data['field_ids']))
  228. {
  229. $all_field_ids = array_merge($all_field_ids, $form_data['field_ids']);
  230. $all_order_ids = array_merge(
  231. $all_order_ids,
  232. $this->actions()->pipe_split($form_data['field_order'])
  233. );
  234. }
  235. }
  236. $all_field_ids = array_unique($all_field_ids);
  237. $all_order_ids = array_unique($all_order_ids);
  238. sort($all_field_ids);
  239. // -------------------------------------
  240. // get field data
  241. // -------------------------------------
  242. $all_field_data = FALSE;
  243. if ( ! empty($all_field_ids))
  244. {
  245. $all_field_data = ee()->freeform_field_model
  246. ->key('field_id')
  247. ->where_in('field_id', $all_field_ids)
  248. ->get();
  249. }
  250. $field_data = array();
  251. if ($all_field_data)
  252. {
  253. foreach ($all_field_data as $row)
  254. {
  255. $field_data[$row['field_id']] = $row;
  256. }
  257. }
  258. // -------------------------------------
  259. // set tables
  260. // -------------------------------------
  261. ee()->freeform_entry_model->id($form_ids);
  262. // -------------------------------------
  263. // replace CURRENT_USER before we get
  264. // started because the minute we don't
  265. // someone is going to figure out
  266. // a way to need it in site_id=""
  267. // -------------------------------------
  268. $this->replace_current_user();
  269. // -------------------------------------
  270. // site ids
  271. // -------------------------------------
  272. //if its star, allow all
  273. if (ee()->TMPL->fetch_param('site_id') !== '*')
  274. {
  275. $site_id = $this->parse_numeric_array_param('site_id');
  276. //if this isn't false, its single or an array
  277. if ($site_id !== FALSE)
  278. {
  279. if (empty($site_id['ids']))
  280. {
  281. ee()->freeform_entry_model->reset();
  282. return $this->no_results_error();
  283. }
  284. else if ($site_id['not'])
  285. {
  286. ee()->freeform_entry_model->where_not_in('site_id', $site_id['ids']);
  287. }
  288. else
  289. {
  290. ee()->freeform_entry_model->where_in('site_id', $site_id['ids']);
  291. }
  292. }
  293. //default
  294. else
  295. {
  296. ee()->freeform_entry_model->where('site_id', ee()->config->item('site_id'));
  297. }
  298. }
  299. // -------------------------------------
  300. // entry ids
  301. // -------------------------------------
  302. $entry_id = $this->parse_numeric_array_param('entry_id');
  303. if ($entry_id !== FALSE)
  304. {
  305. if (empty($entry_id['ids']))
  306. {
  307. ee()->freeform_entry_model->reset();
  308. return $this->no_results_error();
  309. }
  310. else if ($entry_id['not'])
  311. {
  312. ee()->freeform_entry_model->where_not_in('entry_id', $entry_id['ids']);
  313. }
  314. else
  315. {
  316. ee()->freeform_entry_model->where_in('entry_id', $entry_id['ids']);
  317. }
  318. }
  319. // -------------------------------------
  320. // author ids
  321. // -------------------------------------
  322. $author_id = $this->parse_numeric_array_param('author_id');
  323. if ($author_id !== FALSE)
  324. {
  325. if (empty($author_id['ids']))
  326. {
  327. ee()->freeform_entry_model->reset();
  328. return $this->no_results_error();
  329. }
  330. else if ($author_id['not'])
  331. {
  332. ee()->freeform_entry_model->where_not_in('author_id', $author_id['ids']);
  333. }
  334. else
  335. {
  336. ee()->freeform_entry_model->where_in('author_id', $author_id['ids']);
  337. }
  338. }
  339. // -------------------------------------
  340. // freeform:all_form_fields
  341. // -------------------------------------
  342. $tagdata = $this->replace_all_form_fields(
  343. ee()->TMPL->tagdata,
  344. $field_data,
  345. $all_order_ids
  346. );
  347. // -------------------------------------
  348. // get standard columns and labels
  349. // -------------------------------------
  350. $standard_columns = array_keys(
  351. ee()->freeform_form_model->default_form_table_columns
  352. );
  353. $standard_columns[] = 'author';
  354. $column_labels = array();
  355. //keyed labels for the front end
  356. foreach ($standard_columns as $column_name)
  357. {
  358. $column_labels[$column_name] = lang($column_name);
  359. }
  360. // -------------------------------------
  361. // available fields
  362. // -------------------------------------
  363. //this makes the keys and values the same
  364. $available_fields = array_combine($standard_columns, $standard_columns);
  365. $custom_fields = array();
  366. $field_descriptions = array();
  367. foreach ($field_data as $field_id => $f_data)
  368. {
  369. $fid = ee()->freeform_form_model->form_field_prefix . $field_id;
  370. //field_name => field_id_1, etc
  371. $available_fields[$f_data['field_name']] = $fid;
  372. //field_id_1 => field_id_1, etc
  373. $available_fields[$fid] = $fid;
  374. $custom_fields[] = $f_data['field_name'];
  375. //labels
  376. $column_labels[$f_data['field_name']] = $f_data['field_label'];
  377. $column_labels[$fid] = $f_data['field_label'];
  378. $field_descriptions[
  379. 'freeform:description:' . $f_data['field_name']
  380. ] = $f_data['field_description'];
  381. }
  382. // -------------------------------------
  383. // search:field_name="kittens"
  384. // -------------------------------------
  385. foreach (ee()->TMPL->tagparams as $key => $value)
  386. {
  387. if (substr($key, 0, 7) == 'search:')
  388. {
  389. $search_key = substr($key, 7);
  390. if (isset($available_fields[$search_key]))
  391. {
  392. ee()->freeform_entry_model->add_search(
  393. $available_fields[$search_key],
  394. $value
  395. );
  396. }
  397. }
  398. }
  399. // -------------------------------------
  400. // date range
  401. // -------------------------------------
  402. $date_range = ee()->TMPL->fetch_param('date_range');
  403. $date_range_start = ee()->TMPL->fetch_param('date_range_start');
  404. $date_range_end = ee()->TMPL->fetch_param('date_range_end');
  405. ee()->freeform_entry_model->date_where(
  406. $date_range,
  407. $date_range_start,
  408. $date_range_end
  409. );
  410. // -------------------------------------
  411. // complete
  412. // -------------------------------------
  413. $show_incomplete = ee()->TMPL->fetch_param('show_incomplete');
  414. if ($show_incomplete === 'only')
  415. {
  416. ee()->freeform_entry_model->where('complete', 'n');
  417. }
  418. else if ( ! $this->check_yes($show_incomplete))
  419. {
  420. ee()->freeform_entry_model->where('complete', 'y');
  421. }
  422. // -------------------------------------
  423. // status
  424. // -------------------------------------
  425. $status = ee()->TMPL->fetch_param('status', 'open');
  426. if ($status !== 'all')
  427. {
  428. if (in_array($status, $statuses))
  429. {
  430. ee()->freeform_entry_model->where('status', $status);
  431. }
  432. }
  433. // -------------------------------------
  434. // orderby/sort
  435. // -------------------------------------
  436. $sort = ee()->TMPL->fetch_param('sort');
  437. $orderby = ee()->TMPL->fetch_param('orderby');
  438. if ($orderby !== FALSE AND trim($orderby) !== '')
  439. {
  440. $orderby = $this->actions()->pipe_split(strtolower(trim($orderby)));
  441. array_walk($orderby, 'trim');
  442. // -------------------------------------
  443. // sort
  444. // -------------------------------------
  445. if ($sort !== FALSE AND trim($sort) !== '')
  446. {
  447. $sort = $this->actions()->pipe_split(strtolower(trim($sort)));
  448. array_walk($sort, 'trim');
  449. //correct sorts
  450. foreach ($sort as $key => $value)
  451. {
  452. if ( ! in_array($value, array('asc', 'desc')))
  453. {
  454. $sort[$key] = 'asc';
  455. }
  456. }
  457. }
  458. else
  459. {
  460. $sort = array('asc');
  461. }
  462. // -------------------------------------
  463. // add sorts and orderbys
  464. // -------------------------------------
  465. foreach ($orderby as $key => $value)
  466. {
  467. if (isset($available_fields[$value]))
  468. {
  469. //if the sort is not set, just use the first
  470. //really this should teach people to be more specific :p
  471. $temp_sort = isset($sort[$key]) ? $sort[$key] : $sort[0];
  472. ee()->freeform_entry_model->order_by(
  473. $available_fields[$value],
  474. $temp_sort
  475. );
  476. }
  477. }
  478. }
  479. //--------------------------------------
  480. // pagination start vars
  481. //--------------------------------------
  482. $limit = ee()->TMPL->fetch_param('limit', 50);
  483. $offset = ee()->TMPL->fetch_param('offset', 0);
  484. $row_count = 0;
  485. $total_entries = ee()->freeform_entry_model->count(array(), FALSE);
  486. $current_page = 0;
  487. if ($total_entries == 0)
  488. {
  489. ee()->freeform_entry_model->reset();
  490. return $this->no_results_error();
  491. }
  492. // -------------------------------------
  493. // pagination?
  494. // -------------------------------------
  495. $prefix = stristr($tagdata, LD . 'freeform:paginate' . RD);
  496. if ($limit > 0 AND ($total_entries - $offset) > $limit)
  497. {
  498. //get pagination info
  499. $pagination_data = $this->universal_pagination(array(
  500. 'total_results' => $total_entries,
  501. 'tagdata' => $tagdata,
  502. 'limit' => $limit,
  503. 'offset' => $offset,
  504. 'uri_string' => ee()->uri->uri_string,
  505. 'prefix' => 'freeform:',
  506. 'auto_paginate' => TRUE
  507. ));
  508. //if we paginated, sort the data
  509. if ($pagination_data['paginate'] === TRUE)
  510. {
  511. $tagdata = $pagination_data['tagdata'];
  512. $current_page = $pagination_data['pagination_page'];
  513. }
  514. }
  515. else
  516. {
  517. $this->paginate = FALSE;
  518. }
  519. ee()->freeform_entry_model->limit($limit, $current_page + $offset);
  520. // -------------------------------------
  521. // get data
  522. // -------------------------------------
  523. $result_array = ee()->freeform_entry_model->get();
  524. if (empty($result_array))
  525. {
  526. ee()->freeform_entry_model->reset();
  527. return $this->no_results_error();
  528. }
  529. $output_labels = array();
  530. //column labels for output
  531. foreach ($column_labels as $key => $value)
  532. {
  533. $output_labels['freeform:label:' . $key] = $value;
  534. }
  535. $count = $row_count;
  536. $variable_rows = array();
  537. $replace_tagdata = '';
  538. // -------------------------------------
  539. // allow pre_process
  540. // -------------------------------------
  541. $entry_ids = array();
  542. foreach ($result_array as $row)
  543. {
  544. if ( ! isset($entry_ids[$row['form_id']]))
  545. {
  546. $entry_ids[$row['form_id']] = array();
  547. }
  548. $entry_ids[$row['form_id']][] = $row['entry_id'];
  549. }
  550. foreach ($entry_ids as $f_form_id => $f_entry_ids)
  551. {
  552. ee()->freeform_fields->apply_field_method(array(
  553. 'method' => 'pre_process_entries',
  554. 'form_id' => $f_form_id,
  555. 'form_data' => $forms_data,
  556. 'entry_id' => $f_entry_ids,
  557. 'field_data' => $field_data
  558. ));
  559. }
  560. // -------------------------------------
  561. // output
  562. // -------------------------------------
  563. $to_prefix = array(
  564. 'absolute_count',
  565. 'absolute_results',
  566. 'author_id',
  567. 'author',
  568. 'complete',
  569. 'edit_date',
  570. 'entry_date',
  571. 'entry_id',
  572. 'form_id',
  573. 'form_name',
  574. 'ip_address',
  575. 'reverse_count'
  576. );
  577. $absolute_count = $current_page + $offset;
  578. $total_results = count($result_array);
  579. $count = 0;
  580. foreach ($result_array as $row)
  581. {
  582. //apply replace tag to our field data
  583. $field_parse = ee()->freeform_fields->apply_field_method(array(
  584. 'method' => 'replace_tag',
  585. 'form_id' => $row['form_id'],
  586. 'entry_id' => $row['entry_id'],
  587. 'form_data' => $forms_data,
  588. 'field_data' => $field_data,
  589. 'field_input_data' => $row,
  590. 'tagdata' => $tagdata
  591. ));
  592. $row = array_merge(
  593. $output_labels,
  594. $field_descriptions,
  595. $row,
  596. $field_parse['variables']
  597. );
  598. if ($replace_tagdata == '')
  599. {
  600. $replace_tagdata = $field_parse['tagdata'];
  601. }
  602. $row['freeform:form_name'] = $forms_data[$row['form_id']]['form_name'];
  603. $row['freeform:form_label'] = $forms_data[$row['form_id']]['form_label'];
  604. //prefix
  605. foreach ($row as $key => $value)
  606. {
  607. if ( ! preg_match('/^freeform:/', $key))
  608. {
  609. if (in_array($key, $custom_fields) AND
  610. ! isset($row['freeform:field:' . $key]))
  611. {
  612. $row['freeform:field:' . $key] = $value;
  613. }
  614. else if ( ! isset($row['freeform:' . $key]))
  615. {
  616. $row['freeform:' . $key] = $value;
  617. }
  618. unset($row[$key]);
  619. }
  620. }
  621. // -------------------------------------
  622. // other counts
  623. // -------------------------------------
  624. $row['freeform:reverse_count'] = $total_results - $count++;
  625. $row['freeform:absolute_count'] = ++$absolute_count;
  626. $row['freeform:absolute_results'] = $total_entries;
  627. $variable_rows[] = $row;
  628. }
  629. $tagdata = $replace_tagdata;
  630. $prefixed_tags = array(
  631. 'count',
  632. 'switch',
  633. 'total_results'
  634. );
  635. $tagdata = $this->tag_prefix_replace('freeform:', $prefixed_tags, $tagdata);
  636. //this should handle backspacing as well
  637. $tagdata = ee()->TMPL->parse_variables($tagdata, $variable_rows);
  638. $tagdata = $this->tag_prefix_replace('freeform:', $prefixed_tags, $tagdata, TRUE);
  639. // -------------------------------------
  640. // add pagination
  641. // -------------------------------------
  642. //prefix or no prefix?
  643. if ($prefix)
  644. {
  645. $tagdata = $this->parse_pagination(array(
  646. 'prefix' => 'freeform:',
  647. 'tagdata' => $tagdata
  648. ));
  649. }
  650. else
  651. {
  652. $tagdata = $this->parse_pagination(array(
  653. 'tagdata' => $tagdata
  654. ));
  655. }
  656. return $tagdata;
  657. }
  658. //END entries
  659. // --------------------------------------------------------------------
  660. /**
  661. * Freeform:Form
  662. * {exp:freeform:form}
  663. *
  664. * @access public
  665. * @param bool $edit edit mode? external for security
  666. * @param bool $preview preview mode?
  667. * @param mixed $preview_fields extra preview fields?
  668. * @return string tagdata
  669. */
  670. public function form ( $edit = FALSE, $preview = FALSE, $preview_fields = FALSE)
  671. {
  672. if ($this->check_yes(ee()->TMPL->fetch_param('require_logged_in')) AND
  673. ee()->session->userdata['member_id'] == '0')
  674. {
  675. return $this->no_results_error('not_logged_in');
  676. }
  677. // -------------------------------------
  678. // form id
  679. // -------------------------------------
  680. $form_id = $this->form_id();
  681. if ( ! $form_id)
  682. {
  683. return $this->no_results_error('invalid_form_id');
  684. }
  685. // -------------------------------------
  686. // libs, helpers, etc
  687. // -------------------------------------
  688. ee()->load->model('freeform_form_model');
  689. ee()->load->model('freeform_field_model');
  690. ee()->load->library('freeform_forms');
  691. ee()->load->library('freeform_fields');
  692. ee()->load->helper('form');
  693. // -------------------------------------
  694. // get prefs early to avoid query mess
  695. // -------------------------------------
  696. $this->data->get_module_preferences();
  697. $this->data->get_global_module_preferences();
  698. // -------------------------------------
  699. // build query
  700. // -------------------------------------
  701. $form_data = $this->data->get_form_info($form_id);
  702. // -------------------------------------
  703. // preview fields? (composer preview)
  704. // -------------------------------------
  705. if ( ! empty($preview_fields))
  706. {
  707. ee()->load->model('freeform_field_model');
  708. $valid_preview_fields = ee()->freeform_field_model
  709. ->where_in('field_id', $preview_fields)
  710. ->key('field_id')
  711. ->get();
  712. if ($valid_preview_fields)
  713. {
  714. foreach ($valid_preview_fields as $p_field_id => $p_field_data)
  715. {
  716. $p_field_data['preview'] = TRUE;
  717. $form_data['fields'][$p_field_id] = $p_field_data;
  718. }
  719. }
  720. }
  721. // -------------------------------------
  722. // form data
  723. // -------------------------------------
  724. $this->params['form_id'] = $form_id;
  725. // -------------------------------------
  726. // edit?
  727. // -------------------------------------
  728. $entry_id = 0;
  729. $edit_data = array();
  730. $this->params['edit'] = $edit;
  731. $this->params['entry_id'] = $entry_id;
  732. // -------------------------------------
  733. // replace CURRENT_USER everywhere
  734. // -------------------------------------
  735. $this->replace_current_user();
  736. // -------------------------------------
  737. // default params
  738. // -------------------------------------
  739. $default_mp_page_marker = 'page';
  740. $params_with_defaults = array(
  741. //security
  742. 'secure_action' => FALSE,
  743. 'secure_return' => FALSE,
  744. 'require_captcha' => (
  745. $this->check_yes(ee()->config->item('captcha_require_members')) OR
  746. (
  747. $this->check_no(ee()->config->item('captcha_require_members')) AND
  748. ee()->session->userdata('member_id') == 0
  749. )
  750. ),
  751. 'require_ip' => ! $this->check_no(
  752. ee()->config->item("require_ip_for_posting")
  753. ),
  754. 'return' => ee()->uri->uri_string,
  755. 'inline_error_return' => ee()->uri->uri_string,
  756. 'error_page' => '',
  757. 'ajax' => TRUE,
  758. 'restrict_edit_to_author' => TRUE,
  759. 'inline_errors' => FALSE,
  760. //dupe prevention
  761. 'prevent_duplicate_on' => '',
  762. 'prevent_duplicate_per_site' => FALSE,
  763. 'secure_duplicate_redirect' => FALSE,
  764. 'duplicate_redirect' => '',
  765. 'error_on_duplicate' => FALSE,
  766. //required or matching fields
  767. 'required' => '',
  768. 'matching_fields' => '',
  769. //multipage
  770. 'last_page' => TRUE,
  771. 'multipage' => FALSE,
  772. 'redirect_on_timeout' => TRUE,
  773. 'redirect_on_timeout_to' => '',
  774. 'page_marker' => $default_mp_page_marker,
  775. 'multipage_page' => '',
  776. 'paging_url' => '',
  777. 'multipage_page_names' => '',
  778. //notifications
  779. 'admin_notify' => $form_data['admin_notification_email'],
  780. 'admin_cc_notify' => '',
  781. 'admin_bcc_notify' => '',
  782. 'notify_user' => $this->check_yes($form_data['notify_user']),
  783. 'notify_admin' => $this->check_yes($form_data['notify_admin']),
  784. 'notify_on_edit' => FALSE,
  785. 'user_email_field' => $form_data['user_email_field'],
  786. //dynamic_recipients
  787. 'recipients' => FALSE,
  788. 'recipients_limit' => '3',
  789. //user inputted recipients
  790. 'recipient_user_input' => FALSE,
  791. 'recipient_user_limit' => '3',
  792. //templates
  793. 'recipient_template' => "",
  794. 'recipient_user_template' => "",
  795. 'admin_notification_template' => $form_data['admin_notification_id'],
  796. 'user_notification_template' => $form_data['user_notification_id'],
  797. 'status' => $form_data['default_status'],
  798. 'allow_status_edit' => FALSE,
  799. );
  800. foreach ($params_with_defaults as $p_name => $p_default)
  801. {
  802. //if the default is a boolean value
  803. if ( is_bool($p_default))
  804. {
  805. //and if there is a template param version of the param
  806. if (ee()->TMPL->fetch_param($p_name) !== FALSE)
  807. {
  808. //and if the default is boolean true
  809. if ($p_default === TRUE)
  810. {
  811. //and if the template param uses an indicator of the
  812. //'false' variety, we want to override the default
  813. //of TRUE and set FALSE.
  814. $this->params[$p_name] = ! $this->check_no(
  815. ee()->TMPL->fetch_param($p_name)
  816. );
  817. }
  818. //but if the default is boolean false
  819. else
  820. {
  821. //and the template param is trying to turn the feature
  822. //on through a 'y', 'yes', or 'on' value, then we want
  823. //to convert the FALSE to a TRUE
  824. $this->params[$p_name] = $this->check_yes(
  825. ee()->TMPL->fetch_param($p_name)
  826. );
  827. }
  828. }
  829. //there is no template param version of this default so the default stands
  830. else
  831. {
  832. $this->params[$p_name] = $p_default;
  833. }
  834. }
  835. //other wise check for the param or fallback on default
  836. else
  837. {
  838. $this->params[$p_name] = trim(
  839. ee()->TMPL->fetch_param($p_name, $p_default)
  840. );
  841. }
  842. }
  843. // ----------------------------------------
  844. // Check for duplicate
  845. // ----------------------------------------
  846. $duplicate = FALSE;
  847. //we can only prevent dupes on entry like this
  848. if ( ! $edit AND $this->params['prevent_duplicate_on'])
  849. {
  850. if ( in_array(
  851. $this->params['prevent_duplicate_on'],
  852. array('member_id', 'ip_address'),
  853. TRUE
  854. ))
  855. {
  856. $duplicate = ee()->freeform_forms->check_duplicate(
  857. $form_id,
  858. $this->params['prevent_duplicate_on'],
  859. '',
  860. $this->params['prevent_duplicate_per_site']
  861. );
  862. }
  863. }
  864. // ----------------------------------------
  865. // duplicate?
  866. // ----------------------------------------
  867. if ($duplicate)
  868. {
  869. if ($this->params['duplicate_redirect'] !== '')
  870. {
  871. ee()->functions->redirect(
  872. $this->prep_url(
  873. $this->params['duplicate_redirect'],
  874. $this->params['secure_duplicate_redirect']
  875. )
  876. );
  877. exit();
  878. }
  879. else if ($this->params['error_on_duplicate'])
  880. {
  881. return $this->no_results_error('no_duplicates');
  882. }
  883. /*else if (preg_match(
  884. '/' . LD . 'if freeform_duplicate' . RD . '(*?)' '/',
  885. ee()->TMPL->tagdata, ))
  886. {
  887. }*/
  888. }
  889. // -------------------------------------
  890. // check user email field
  891. // if this is from form prefs, its an ID
  892. // -------------------------------------
  893. $valid_user_email_field = FALSE;
  894. foreach ($form_data['fields'] as $field_id => $field_data)
  895. {
  896. if ($this->params['user_email_field'] == $field_data['field_name'] OR
  897. $this->params['user_email_field'] == $field_id)
  898. {
  899. $valid_user_email_field = TRUE;
  900. //in case the setting is an id
  901. $this->params['user_email_field'] = $field_data['field_name'];
  902. break;
  903. }
  904. }
  905. // if it doesn't exist in the form, lets blank it
  906. $this->params['user_email_field'] = (
  907. $valid_user_email_field ?
  908. $this->params['user_email_field'] :
  909. ''
  910. );
  911. // ----------------------------------------
  912. // 'freeform_module_form_begin' hook.
  913. // - This allows developers to change data before form processing.
  914. // ----------------------------------------
  915. if (ee()->extensions->active_hook('freeform_module_form_begin') === TRUE)
  916. {
  917. $edata = ee()->extensions->universal_call(
  918. 'freeform_module_form_begin',
  919. $this
  920. );
  921. if (ee()->extensions->end_script === TRUE) return;
  922. }
  923. // ----------------------------------------
  924. // -------------------------------------
  925. // start form
  926. // -------------------------------------
  927. $tagdata = ee()->TMPL->tagdata;
  928. $return = '';
  929. $hidden_fields = array();
  930. $outer_template_vars = array();
  931. $variables = array();
  932. $multipage = $this->params['multipage'];
  933. $last_page = TRUE;
  934. $page_total = 1;
  935. $current_page = 0;
  936. // -------------------------------------
  937. // check if this is multi-page
  938. // -------------------------------------
  939. $current_page = 1;
  940. // -------------------------------------
  941. // check again for captcha now that
  942. // tagdata has been adjusted
  943. // -------------------------------------
  944. if ($this->params['require_captcha'])
  945. {
  946. $this->params['require_captcha'] = (stristr($tagdata, LD . 'freeform:captcha' . RD) != FALSE);
  947. }
  948. // -------------------------------------
  949. // other random vars
  950. // -------------------------------------
  951. $variables['freeform:submit'] = form_submit('submit', lang('submit'));
  952. $variables['freeform:duplicate'] = $duplicate;
  953. $variables['freeform:not_duplicate'] = ! $duplicate;
  954. $variables['freeform:form_label'] = $form_data['form_label'];
  955. $variables['freeform:form_description'] = $form_data['form_description'];
  956. // -------------------------------------
  957. // recipient emails from multipage?
  958. // -------------------------------------
  959. $variables['freeform:mp_data:user_recipient_emails'] = '';
  960. if (isset($previous_inputs['hash_stored_data']['user_recipient_emails']) AND
  961. is_array($previous_inputs['hash_stored_data']['user_recipient_emails']))
  962. {
  963. $variables['freeform:mp_data:user_recipient_emails'] = implode(
  964. ', ',
  965. $previous_inputs['hash_stored_data']['user_recipient_emails']
  966. );
  967. }
  968. // -------------------------------------
  969. // display fields
  970. // -------------------------------------
  971. $field_error_data = array();
  972. $general_error_data = array();
  973. $field_input_data = array();
  974. // -------------------------------------
  975. // inline errors?
  976. // -------------------------------------
  977. if ($this->params['inline_errors'] AND
  978. $this->is_positive_intlike(ee()->session->flashdata('freeform_errors')))
  979. {
  980. ee()->load->model('freeform_param_model');
  981. $error_query = ee()->freeform_param_model->get_row(
  982. ee()->session->flashdata('freeform_errors')
  983. );
  984. if ($error_query !== FALSE)
  985. {
  986. $potential_error_data = json_decode($error_query['data'], TRUE);
  987. if (isset($potential_error_data['field_errors']))
  988. {
  989. $field_error_data = $potential_error_data['field_errors'];
  990. }
  991. if (isset($potential_error_data['general_errors']))
  992. {
  993. $general_error_data = $potential_error_data['general_errors'];
  994. }
  995. if (isset($potential_error_data['inputs']))
  996. {
  997. $field_input_data = $potential_error_data['inputs'];
  998. }
  999. }
  1000. }
  1001. foreach ($form_data['fields'] as $field_id => $field_data)
  1002. {
  1003. // -------------------------------------
  1004. // label?
  1005. // -------------------------------------
  1006. $error = '';
  1007. if (isset($field_error_data[$field_data['field_name']]))
  1008. {
  1009. $error = is_array($field_error_data[$field_data['field_name']]) ?
  1010. implode(', ', $field_error_data[$field_data['field_name']]) :
  1011. $field_error_data[$field_data['field_name']];
  1012. }
  1013. $variables['freeform:error:' . $field_data['field_name']] = $error;
  1014. $variables['freeform:label:' . $field_data['field_name']] = $field_data['field_label'];
  1015. $variables['freeform:description:' . $field_data['field_name']] = $field_data['field_description'];
  1016. // -------------------------------------
  1017. // values?
  1018. // -------------------------------------
  1019. $col_name = ee()->freeform_form_model->form_field_prefix . $field_id;
  1020. // -------------------------------------
  1021. // multipage previous inputs?
  1022. // -------------------------------------
  1023. $variables['freeform:mp_data:' . $field_data['field_name']] = (
  1024. isset($previous_inputs[$col_name]) ?
  1025. $previous_inputs[$col_name] :
  1026. (
  1027. isset($previous_inputs[$field_data['field_name']]) ?
  1028. $previous_inputs[$field_data['field_name']] :
  1029. ''
  1030. )
  1031. );
  1032. }
  1033. //END foreach ($form_data['fields'] as $field_id => $field_data)
  1034. if ( ! empty($edit_data))
  1035. {
  1036. $field_input_data = $edit_data;
  1037. }
  1038. else if ( ! empty($previous_inputs))
  1039. {
  1040. $field_input_data = $previous_inputs;
  1041. }
  1042. // -------------------------------------
  1043. // freeform:all_form_fields
  1044. // -------------------------------------
  1045. $tagdata = $this->replace_all_form_fields(
  1046. $tagdata,
  1047. $form_data['fields'],
  1048. $form_data['field_order'],
  1049. $field_input_data
  1050. );
  1051. // -------------------------------------
  1052. // general errors
  1053. // -------------------------------------
  1054. if ( ! empty($general_error_data))
  1055. {
  1056. //the error array might have sub arrays
  1057. //so we need to flatten
  1058. $_general_error_data = array();
  1059. foreach ($general_error_data as $error_set => $error_data)
  1060. {
  1061. if (is_array($error_data))
  1062. {
  1063. foreach ($error_data as $sub_key => $sub_error)
  1064. {
  1065. $_general_error_data[] = array('freeform:error_message' => $sub_error);
  1066. }
  1067. }
  1068. else
  1069. {
  1070. $_general_error_data[] = array('freeform:error_message' => $error_data);
  1071. }
  1072. }
  1073. $general_error_data = $_general_error_data;
  1074. }
  1075. $variables['freeform:general_errors'] = $general_error_data;
  1076. //have to do this so the conditional will work,
  1077. //seems that parse variables doesn't think a non-empty array = YES
  1078. $tagdata = ee()->functions->prep_conditionals(
  1079. $tagdata,
  1080. array('freeform:general_errors' => ! empty($general_error_data))
  1081. );
  1082. // -------------------------------------
  1083. // apply replace tag to our field data
  1084. // -------------------------------------
  1085. $field_parse = ee()->freeform_fields->apply_field_method(array(
  1086. 'method' => 'display_field',
  1087. 'form_id' => $form_id,
  1088. 'entry_id' => $entry_id,
  1089. 'form_data' => $form_data,
  1090. 'field_input_data' => $field_input_data,
  1091. 'tagdata' => $tagdata
  1092. ));
  1093. $this->multipart = $field_parse['multipart'];
  1094. $variables = array_merge($variables, $field_parse['variables']);
  1095. $tagdata = $field_parse['tagdata'];
  1096. // -------------------------------------
  1097. // dynamic recipient list
  1098. // -------------------------------------
  1099. $this->params['recipients'] = (
  1100. ! in_array(ee()->TMPL->fetch_param('recipients'), array(FALSE, ''))
  1101. );
  1102. //preload list with usable info if so
  1103. $this->params['recipients_list'] = array();
  1104. if ( $this->params['recipients'] )
  1105. {
  1106. $i = 1;
  1107. $while_limit = 1000;
  1108. $counter = 0;
  1109. while ( ! in_array(ee()->TMPL->fetch_param('recipient' . $i), array(FALSE, '')) )
  1110. {
  1111. $recipient = explode('|', ee()->TMPL->fetch_param('recipient' . $i));
  1112. //has a name?
  1113. if ( count($recipient) > 1)
  1114. {
  1115. $recipient_name = trim($recipient[0]);
  1116. $recipient_email = trim($recipient[1]);
  1117. }
  1118. //no name, we assume its just an email
  1119. //(though, this makes little sense, it needs a name to be useful)
  1120. else
  1121. {
  1122. $recipient_name = '';
  1123. $recipient_email = trim($recipient[0]);
  1124. }
  1125. $recipient_selected = FALSE;
  1126. if (isset($previous_inputs['hash_stored_data']['recipient_emails']) AND
  1127. is_array($previous_inputs['hash_stored_data']['recipient_emails']))
  1128. {
  1129. $recipient_selected = in_array(
  1130. $recipient_email,
  1131. $previous_inputs['hash_stored_data']['recipient_emails']
  1132. );
  1133. }
  1134. //add to list
  1135. $this->params['recipients_list'][$i] = array(
  1136. 'name' => $recipient_name,
  1137. 'email' => $recipient_email,
  1138. 'key' => uniqid(),
  1139. 'selected' => $recipient_selected
  1140. );
  1141. $i++;
  1142. //extra protection because while loops are scary
  1143. if (++$counter >= $while_limit)
  1144. {
  1145. break;
  1146. }
  1147. }
  1148. //if we end up with nothing, then lets not attempt later
  1149. if (empty($this->params['recipients_list']))
  1150. {
  1151. $this->params['recipients'] = FALSE;
  1152. }
  1153. }
  1154. // ----------------------------------------
  1155. // parse {captcha}
  1156. // ----------------------------------------
  1157. $variables['freeform:captcha'] = FALSE;
  1158. if ($this->params['require_captcha'])
  1159. {
  1160. $variables['freeform:captcha'] = ee()->functions->create_captcha();
  1161. }
  1162. // -------------------------------------
  1163. // dynamic recipient tagdata
  1164. // -------------------------------------
  1165. if ( $this->params['recipients'] AND
  1166. count($this->params['recipients_list']) > 0)
  1167. {
  1168. $variables['freeform_recipients'] = array();
  1169. $recipient_list = $this->params['recipients_list'];
  1170. //dynamic above starts with 1, so does this
  1171. for ( $i = 1, $l = count($recipient_list); $i <= $l; $i++ )
  1172. {
  1173. $variables['freeform:recipient_name' . $i] = $recipient_list[$i]['name'];
  1174. $variables['freeform:recipient_value' . $i] = $recipient_list[$i]['key'];
  1175. $variables['freeform:recipient_selected' . $i] = $recipient_list[$i]['selected'];
  1176. $variables['freeform:recipients'][] = array(
  1177. 'freeform:recipient_name' => $recipient_list[$i]['name'],
  1178. 'freeform:recipient_value' => $recipient_list[$i]['key'],
  1179. 'freeform:recipient_count' => $i,
  1180. //selected from hash data from multipages
  1181. 'freeform:recipient_selected' => $recipient_list[$i]['selected']
  1182. );
  1183. }
  1184. }
  1185. // -------------------------------------
  1186. // status pairs
  1187. // -------------------------------------
  1188. $tagdata = $this->parse_status_tags($tagdata);
  1189. // ----------------------------------------
  1190. // 'freeform_module_pre_form_parse' hook.
  1191. // - This allows developers to change data before tagdata processing.
  1192. // ----------------------------------------
  1193. $this->variables = $variables;
  1194. if (ee()->extensions->active_hook('freeform_module_pre_form_parse') === TRUE)
  1195. {
  1196. $tagdata = ee()->extensions->universal_call(
  1197. 'freeform_module_pre_form_parse',
  1198. $tagdata,
  1199. $this
  1200. );
  1201. if (ee()->extensions->end_script === TRUE) return;
  1202. }
  1203. // ----------------------------------------
  1204. //extra precaution in case someone hoses this
  1205. if (isset($this->variables) AND is_array($this->variables))
  1206. {
  1207. $variables = $this->variables;
  1208. }
  1209. // -------------------------------------
  1210. // parse external vars
  1211. // -------------------------------------
  1212. $outer_template_vars['freeform:form_page'] = $current_page;
  1213. $outer_template_vars['freeform:form_page_total'] = $page_total;
  1214. $outer_template_vars['freeform:form_name'] = $form_data['form_name'];
  1215. $outer_template_vars['freeform:form_label'] = $form_data['form_label'];
  1216. ee()->TMPL->template = ee()->functions->prep_conditionals(
  1217. ee()->TMPL->template,
  1218. $outer_template_vars
  1219. );
  1220. ee()->TMPL->template = ee()->functions->var_swap(
  1221. ee()->TMPL->template,
  1222. $outer_template_vars
  1223. );
  1224. // -------------------------------------
  1225. // parse all vars
  1226. // -------------------------------------
  1227. $tagdata = ee()->TMPL->parse_variables(
  1228. $tagdata,
  1229. array(array_merge($outer_template_vars,$variables))
  1230. );
  1231. // -------------------------------------
  1232. // this doesn't force ana ajax request
  1233. // but instead forces it _not_ to be
  1234. // if the ajax param = 'no'
  1235. // -------------------------------------
  1236. if ( ! $this->params['ajax'])
  1237. {
  1238. $hidden_fields['ajax_request'] = 'no';
  1239. }
  1240. //-------------------------------------
  1241. // build form
  1242. //-------------------------------------
  1243. $return .= $this->build_form(array(
  1244. 'action' => $this->get_action_url('save_form'),
  1245. 'method' => 'POST',
  1246. 'hidden_fields' => array_merge($hidden_fields, array(
  1247. // no more params can be set after this
  1248. 'params_id' => $this->insert_params(),
  1249. )),
  1250. 'tagdata' => $tagdata
  1251. ));
  1252. // ----------------------------------------
  1253. // 'freeform_module_form_end' hook.
  1254. // - This allows developers to change the form before output.
  1255. // ----------------------------------------
  1256. if (ee()->extensions->active_hook('freeform_module_form_end') === TRUE)
  1257. {
  1258. $return = ee()->extensions->universal_call(
  1259. 'freeform_module_form_end',
  1260. $return,
  1261. $this
  1262. );
  1263. if (ee()->extensions->end_script === TRUE) return;
  1264. }
  1265. // ----------------------------------------
  1266. return $return;
  1267. }
  1268. //END form
  1269. // -------------------------------------
  1270. // action requests
  1271. // -------------------------------------
  1272. // --------------------------------------------------------------------
  1273. /**
  1274. * ajax_validate
  1275. *
  1276. * does a save form that stops after validation
  1277. *
  1278. * @access public
  1279. * @return mixed ajax request
  1280. */
  1281. public function ajax_validate_form ()
  1282. {
  1283. return $this->save_form(TRUE);
  1284. }
  1285. //END ajax_validate
  1286. // --------------------------------------------------------------------
  1287. /**
  1288. * save_form
  1289. *
  1290. * form save from front_end/action request
  1291. *
  1292. * @access public
  1293. * @param bool validate only
  1294. * @return null
  1295. */
  1296. public function save_form ($validate_only = FALSE)
  1297. {
  1298. if ( ! $validate_only AND REQ !== 'ACTION')
  1299. {
  1300. return;
  1301. }
  1302. ee()->load->library('freeform_forms');
  1303. ee()->load->library('freeform_fields');
  1304. ee()->load->model('freeform_form_model');
  1305. // -------------------------------------
  1306. // require logged in?
  1307. // -------------------------------------
  1308. if ($this->param('require_logged_in') AND
  1309. ee()->session->userdata['member_id'] == '0')
  1310. {
  1311. $this->pre_validation_error(
  1312. lang('not_authorized') . ' - ' .
  1313. lang('not_logged_in')
  1314. );
  1315. }
  1316. // -------------------------------------
  1317. // blacklist, banned
  1318. // -------------------------------------
  1319. if (ee()->session->userdata['is_banned'] OR (
  1320. $this->check_yes(ee()->blacklist->blacklisted) AND
  1321. $this->check_no(ee()->blacklist->whitelisted)
  1322. )
  1323. )
  1324. {
  1325. $this->pre_validation_error(
  1326. lang('not_authorized') . ' - ' .
  1327. lang('reason_banned')
  1328. );
  1329. }
  1330. // -------------------------------------
  1331. // require ip? (except admin)
  1332. // -------------------------------------
  1333. if ($this->param('require_ip'))
  1334. {
  1335. if (ee()->input->ip_address() == '0.0.0.0')
  1336. {
  1337. $this->pre_validation_error(
  1338. lang('not_authorized') . ' - ' .
  1339. lang('reason_ip_required')
  1340. );
  1341. }
  1342. }
  1343. // -------------------------------------
  1344. // Is the nation of the user banned?
  1345. // -------------------------------------
  1346. if ($this->nation_ban_check(FALSE))
  1347. {
  1348. $this->pre_validation_error(
  1349. lang('not_authorized') . ' - ' .
  1350. ee()->config->item('ban_message')
  1351. );
  1352. }
  1353. // -------------------------------------
  1354. // valid form id
  1355. // -------------------------------------
  1356. $form_id = $this->form_id();
  1357. if ( ! $form_id)
  1358. {
  1359. $this->pre_validation_error(lang('invalid_form_id'));
  1360. }
  1361. // -------------------------------------
  1362. // is this an edit? entry_id
  1363. // -------------------------------------
  1364. $entry_id = $this->entry_id();
  1365. $edit = ($entry_id AND $entry_id != 0);
  1366. // -------------------------------------
  1367. // for multipage check later
  1368. // -------------------------------------
  1369. $multipage = $this->param('multipage');
  1370. $current_page = $this->param('current_page');
  1371. $last_page = $this->param('last_page');
  1372. $previous_inputs = array();
  1373. // -------------------------------------
  1374. // form data
  1375. // -------------------------------------
  1376. $form_data = $this->data->get_form_info($form_id);
  1377. $field_labels = array();
  1378. $valid_fields = array();
  1379. foreach ( $form_data['fields'] as $row)
  1380. {
  1381. $field_labels[$row['field_name']] = $row['field_label'];
  1382. $valid_fields[] = $row['field_name'];
  1383. }
  1384. // -------------------------------------
  1385. // for hooks
  1386. // -------------------------------------
  1387. $this->edit = $edit;
  1388. $this->multipage = $multipage;
  1389. $this->last_page = $last_page;
  1390. // -------------------------------------
  1391. // user email max/spam count
  1392. // -------------------------------------
  1393. ee()->load->library('freeform_notifications');
  1394. if ($last_page AND ($this->param('recipient_user_input') OR
  1395. $this->param('recipients')) AND
  1396. ee()->freeform_notifications->check_spam_interval($form_id)
  1397. )
  1398. {
  1399. $this->pre_validation_error(
  1400. lang('not_authorized') . ' - ' .
  1401. lang('email_limit_exceeded')
  1402. );
  1403. }
  1404. // -------------------------------------
  1405. // Check for duplicate
  1406. // -------------------------------------
  1407. $duplicate = FALSE;
  1408. if ($this->param('prevent_duplicate_on') AND
  1409. ! in_array(
  1410. $this->param('prevent_duplicate_on'),
  1411. array('member_id', 'ip_address'),
  1412. TRUE
  1413. ))
  1414. {
  1415. $duplicate = ee()->freeform_forms->check_duplicate(
  1416. $form_id,
  1417. $this->param('prevent_duplicate_on'),
  1418. ee()->input->get_post(
  1419. $this->param('prevent_duplicate_on'),
  1420. TRUE
  1421. ),
  1422. $this->param('prevent_duplicate_per_site')
  1423. );
  1424. }
  1425. if ($duplicate)
  1426. {
  1427. $this->pre_validation_error(lang('no_duplicates'));
  1428. }
  1429. // -------------------------------------
  1430. // pre xid check
  1431. // -------------------------------------
  1432. // we aren't going to delete just yet
  1433. // because if they have input errors
  1434. // then we want to keep this xid for a bit
  1435. // and only delete xid on success
  1436. // -------------------------------------
  1437. if ( $this->check_yes(ee()->config->item('secure_forms')) )
  1438. {
  1439. ee()->db->from('security_hashes');
  1440. ee()->db->where(array(
  1441. 'hash' => ee()->input->post('XID'),
  1442. 'ip_address' => ee()->input->ip_address(),
  1443. 'date >' => ee()->localize->now - 7200
  1444. ));
  1445. if (ee()->db->count_all_results() == 0)
  1446. {
  1447. $this->pre_validation_error(
  1448. lang('not_authorized') . ' - ' .
  1449. lang('reason_secure_form_timeout')
  1450. );
  1451. }
  1452. }
  1453. // -------------------------------------
  1454. // pre-validate hook
  1455. // -------------------------------------
  1456. $errors = array();
  1457. //have to do this weird for backward compat
  1458. $this->field_errors = array();
  1459. if (ee()->extensions->active_hook('freeform_module_validate_begin') === TRUE)
  1460. {
  1461. $errors = ee()->extensions->universal_call(
  1462. 'freeform_module_validate_begin',
  1463. $errors,
  1464. $this
  1465. );
  1466. if (ee()->extensions->end_script === TRUE) return;
  1467. }
  1468. // -------------------------------------
  1469. // require fields
  1470. // -------------------------------------
  1471. if ($this->param('required'))
  1472. {
  1473. $required = $this->actions()->pipe_split($this->param('required'));
  1474. foreach ($required as $required_field)
  1475. {
  1476. //just in case someone misspelled a require
  1477. //or removes a field after making the require list
  1478. if ( ! in_array($required_field, $valid_fields))
  1479. {
  1480. continue;
  1481. }
  1482. if ( (
  1483. (
  1484. is_array( ee()->input->get_post($required_field) ) AND
  1485. count(ee()->input->get_post($required_field)) < 1
  1486. ) OR
  1487. trim((string) ee()->input->get_post($required_field)) === ''
  1488. )
  1489. //required field could be a file
  1490. AND ! isset($_FILES[$required_field])
  1491. )
  1492. {
  1493. $this->field_errors[
  1494. $required_field
  1495. ] = lang('required_field_missing');
  1496. //only want the postfixing of errors
  1497. //if we are sending to general errors screen
  1498. //or an error page
  1499. //the second conditional is for people requesting
  1500. //the custom error page via ajax
  1501. if ( ! $this->param('inline_errors') AND
  1502. ! ($this->is_ajax_request() AND
  1503. ! trim($this->param('error_page'))))
  1504. {
  1505. $this->field_errors[$required_field] .= ': '.
  1506. $field_labels[$required_field];
  1507. }
  1508. }
  1509. }
  1510. }
  1511. // -------------------------------------
  1512. // matching fields
  1513. // -------------------------------------
  1514. if ($this->param('matching_fields'))
  1515. {
  1516. $matching_fields = $this->actions()->pipe_split($this->param('matching_fields'));
  1517. foreach ($matching_fields as $match_field)
  1518. {
  1519. //just in case someone misspelled a require
  1520. //or removes a field after making the require list
  1521. if ( ! in_array($match_field, $valid_fields))
  1522. {
  1523. continue;
  1524. }
  1525. //array comparison is correct in PHP and this should work
  1526. //no matter what.
  1527. //normal validation will fix other issues
  1528. if ( ee()->input->get_post($match_field) == FALSE OR
  1529. ee()->input->get_post($match_field . '_confirm') == FALSE OR
  1530. ee()->input->get_post($match_field) !==
  1531. ee()->input->get_post($match_field . '_confirm')
  1532. )
  1533. {
  1534. $this->field_errors[$match_field] = lang('fields_do_not_match') .
  1535. $field_labels[$match_field] .
  1536. ' | ' .
  1537. $field_labels[$match_field] .
  1538. ' ' .
  1539. lang('confirm');
  1540. }
  1541. }
  1542. }
  1543. // -------------------------------------
  1544. // validate dynamic recipients
  1545. // no actual validation errors
  1546. // will throw here, but in case we do
  1547. // in the future
  1548. // -------------------------------------
  1549. $recipient_emails = array();
  1550. if ($this->param('recipients'))
  1551. {
  1552. $recipient_email_input = ee()->input->get_post('recipient_email');
  1553. if ( ! in_array($recipient_email_input, array(FALSE, ''), TRUE))
  1554. {
  1555. if ( ! is_array($recipient_email_input))
  1556. {
  1557. $recipient_email_input = array($recipient_email_input);
  1558. }
  1559. // recipients are encoded, so lets check for keys
  1560. // since dynamic recipients are dev inputted
  1561. // we aren't going to error on invalid ones
  1562. // but rather just accept if present, and move on if not
  1563. $recipients_list = $this->param('recipients_list');
  1564. $field_out = '';
  1565. foreach($recipients_list as $i => $r_data)
  1566. {
  1567. if (in_array($r_data['key'], $recipient_email_input))
  1568. {
  1569. $recipient_emails[] = $r_data['email'];
  1570. $field_out .= $r_data['name'] . ' <' . $r_data['email'] . '>' . "\n";
  1571. }
  1572. }
  1573. //THE ENGLISH ARE TOO MANY!
  1574. if (count($recipient_emails) > $this->param('recipients_limit'))
  1575. {
  1576. $errors['recipient_email'] = lang('over_recipient_limit');
  1577. }
  1578. //does the user have a recipient_email custom field?
  1579. else if (in_array('recipient_email', $valid_fields))
  1580. {
  1581. $_POST['recipient_email'] = trim($field_out);
  1582. }
  1583. }
  1584. //if there is previous recipient emails
  1585. if (empty($recipient_emails) AND
  1586. isset($previous_inputs['hash_stored_data']['recipient_emails']))
  1587. {
  1588. $recipient_emails = $previous_inputs['hash_stored_data']['recipient_emails'];
  1589. }
  1590. }
  1591. // -------------------------------------
  1592. // validate user inputted emails
  1593. // -------------------------------------
  1594. $user_recipient_emails = array();
  1595. if ($this->param('recipient_user_input'))
  1596. {

Large files files are truncated, but you can click here to view the full file