Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.99 KB | None | 0 0
  1. <?php if ( ! defined( 'AVIA_FW' ) ) exit( 'No direct script access allowed' );
  2. /**
  3. * This file holds the avia_form class which is needed to build contact and other forms for the website
  4. *
  5. * @todo: improve backend so users can build forms on the fly, add aditional elements like selects, checkboxes and radio buttons
  6. *
  7. * @author Christian "Kriesi" Budschedl
  8. * @copyright Copyright ( c ) Christian Budschedl
  9. * @link http://kriesi.at
  10. * @link http://aviathemes.com
  11. * @since Version 1.0
  12. * @package AviaFramework
  13. */
  14.  
  15.  
  16. /**
  17. * AVIA Form
  18. * A simple class that is able to build and submit contact forms with the help of simple arrays that are passed to the form
  19. * It is build in a way that ajax sending is easily possible, but also works without javascript
  20. *
  21. */
  22.  
  23. if( ! class_exists( 'avia_form' ) )
  24. {
  25. class avia_form
  26. {
  27. /**
  28. * This array holds some default parameters for each form that gets created
  29. * @var array
  30. */
  31. var $form_params;
  32.  
  33.  
  34. /**
  35. * This array holds the form elements that where set by the create elements function
  36. * @var array
  37. */
  38. var $form_elements;
  39.  
  40. /**
  41. * This string holds the fnal html output
  42. * @var string
  43. */
  44. var $output = "";
  45.  
  46.  
  47. /**
  48. * This string holds the html output for elements that gets merged with the final output in case an error occured or no submission took place
  49. * @var string
  50. */
  51. var $elements_html = "";
  52.  
  53.  
  54. /**
  55. * This variable holds the information if we should display the form or not. it has to be displayed if an error occurs wihle validating or if no submission took place yet
  56. * @var bool
  57. */
  58. var $submit_form = true;
  59.  
  60. /**
  61. * This variable holds the information if we should check the form elements or not
  62. * @var bool
  63. */
  64. var $do_checks = true;
  65.  
  66. /**
  67. * Array that holds the auto responder field
  68. * @var bool
  69. */
  70. var $autoresponder = array();
  71.  
  72. /**
  73. * Static var that counts the numbers of forms and if one is submitted makes sure that the others arent checked
  74. * @var bool
  75. */
  76. static $form_id = 1;
  77.  
  78. /**
  79. * Stores the length of the field names and $_POST variable length
  80. * @var int
  81. */
  82. var $length = 20;
  83.  
  84. /**
  85. * Stores the width of the current row of elements
  86. * @var int
  87. */
  88. var $width = 1;
  89.  
  90. /**
  91. * Show the element names either as label or as placeholder attribute
  92. * @var int
  93. */
  94. var $placeholder = false;
  95.  
  96. /**
  97. * array that translates the passed width to a numeric value
  98. * @var int
  99. */
  100. var $width_translate = array('fullwidth'=>1, 'element_half' => 0.5, 'element_fourth' => 0.25, 'element_third' => 0.3, 'element_two_third' => 0.6, 'element_three_fourth' => 0.75);
  101.  
  102. /*overwrite the send function*/
  103. var $execute = "";
  104.  
  105. /*error message that can be displayed in front of form*/
  106. var $error_msg;
  107.  
  108.  
  109. /**
  110. * Constructor
  111. *
  112. * The constructor sets up the default params
  113. * @param array $params array with default form information such as submit button label, heading and success message
  114. */
  115. function __construct($params)
  116. {
  117. add_filter('avf_safe_string_trans', array(&$this,'remove_invalid_chars'), 10, 3);
  118.  
  119. $this->form_params = $params;
  120. $this->formID = avia_form::$form_id ++;
  121. $this->form_params['avia_formID'] = $this->formID;
  122. $this->id_sufix = isset($params['multiform']) ? "_".$this->formID : "";
  123. $this->placeholder = !empty($params['placeholder']) ? true : false;
  124.  
  125. $extraClass = isset($params['form_class']) ? $params['form_class'] : "";
  126. $redirect = isset($params['redirect']) ? "data-avia-redirect='".$params['redirect']."'" : "";
  127.  
  128. $form_class = apply_filters('avf_ajax_form_class', 'ajax_form', $this->formID, $this->form_params);
  129. $form_class .= $this->placeholder ? " av-form-labels-hidden " : " av-form-labels-visible ";
  130. $form_data = "";
  131.  
  132. if(isset($this->form_params['form_data']))
  133. {
  134. foreach($this->form_params['form_data'] as $datakey => $dataval)
  135. {
  136. $form_data .= " data-{$datakey}='{$dataval}'" ;
  137. }
  138. }
  139.  
  140. $this->output = '<form action="'.$params['action'].'" method="post" '.$form_data.' class="'.$form_class.' '.$extraClass.'" data-avia-form-id="'.$this->formID.'" '.$redirect.'><fieldset>';
  141. $this->output .= $params['heading'];
  142.  
  143. $this->length = apply_filters('avf_form_el_name_length', 30, $this->formID, $this->form_params);
  144. $this->length = (int)$this->length;
  145.  
  146.  
  147. if(!isset($_POST) || !count($_POST) || empty($_POST['avia_generated_form'.$this->formID]))
  148. {
  149. $this->submit_form = false; //dont submit the form
  150. $this->do_checks = false; //dont do any checks on the form elements
  151. }
  152.  
  153. if(!empty( $params['custom_send'] ))
  154. {
  155. $this->execute = $params['custom_send'];
  156. }
  157. else
  158. {
  159. $this->execute = array($this, 'send');
  160. }
  161.  
  162. $this->submit_attr = apply_filters('avf_contact_form_submit_button_attr', '', $this->formID, $this->form_params);
  163. }
  164.  
  165. /**
  166. * remove additional characters with the save_string filter function which won't work if used for the field names
  167. */
  168. function remove_invalid_chars($trans, $string, $replace)
  169. {
  170. $trans['\.'] = '';
  171. return $trans;
  172. }
  173.  
  174. /**
  175. * get a custom for button or captcha element based on the current $width
  176. */
  177. function auto_width()
  178. {
  179. $class = "";
  180. if($this->width <= 0.75) { $class = 'form_element_fourth'; }
  181. if($this->width <= 0.6) { $class = 'form_element_third'; }
  182. if($this->width <= 0.5) { $class = 'form_element_half'; }
  183. if($this->width <= 0.3) { $class = 'form_element_two_third'; }
  184. if($this->width <= 0.25) { $class = 'form_element_three_fourth'; }
  185.  
  186. if(!empty($class)) $this->width = 1;
  187. return $class;
  188. }
  189.  
  190.  
  191. /**
  192. * create_elements
  193. *
  194. * The create_elements method iterates over a set of elements passed and creates the according form element in the frontend
  195. * @param array $elements array with elements that should eb created
  196. */
  197. function create_elements($elements)
  198. {
  199. $this->form_elements = $elements;
  200. $iterations = 0;
  201. $width = "";
  202. $counter = 0;
  203. $el_count = count($elements) - 1;
  204.  
  205. foreach($elements as $key => $element)
  206. {
  207. $counter ++;
  208. if(isset( $element['id'] )) $key = $element['id'];
  209.  
  210. if(isset($element['type']) && method_exists($this, $element['type']))
  211. {
  212. $element_id = avia_backend_safe_string('avia_'.$key, '_', true);
  213.  
  214.  
  215. if($element_id == "avia_" || !empty($this->form_params['numeric_names']) )
  216. {
  217. $iterations ++;
  218. $element_id = "avia_".$iterations;
  219. }
  220.  
  221. $element_id = avia_backend_truncate($element_id, $this->length, "_", "", false, '', false);
  222.  
  223. if(empty($element['class'])) $element['class'] = "";
  224. if(empty($element['width'])) $element['width'] = "fullwidth";
  225. $add = $this->width_translate[$element['width']];
  226.  
  227. if($element['type'] != "decoy" && $element['type'] != "captcha")
  228. {
  229. $this->width += $add;
  230. if($this->width > 1) { $this->width = $add; $element['class'] .= " first_form ";}
  231. }
  232.  
  233. $element['class'] .= !empty($element['width']) ? " form_element form_".$element['width'] : "";
  234.  
  235. if($el_count - $counter === 0)
  236. {
  237. $element['class'] .= " av-last-visible-form-element";
  238. }
  239.  
  240.  
  241. $element = apply_filters('avf_form_el_filter', $element, $this->formID, $this->form_params);
  242. $this->{$element['type']}($element_id.$this->id_sufix, $element);
  243. }
  244. }
  245. }
  246.  
  247.  
  248. /**
  249. * display_form
  250. *
  251. * Checks if an error occured and if the user tried to send, if thats the case, and if sending worked display a success message, otherwise display the whole form
  252. */
  253. function display_form($return = false)
  254. {
  255. $success = '<div id="ajaxresponse'.$this->id_sufix.'" class="ajaxresponse ajaxresponse'.$this->id_sufix.' hidden"></div>';
  256.  
  257. $call_instance = $this->execute[0];
  258. $call_function = $this->execute[1];
  259.  
  260.  
  261. if($this->submit_form && $call_instance->$call_function( $this ) && empty($this->error_msg))
  262. {
  263. $success = '<div id="ajaxresponse'.$this->id_sufix.'" class="ajaxresponse ajaxresponse'.$this->id_sufix.'">'.$this->form_params['success'].'</div>';
  264. }
  265. else
  266. {
  267. $this->output .= $this->error_msg;
  268. $this->output .= $this->elements_html;
  269.  
  270. if(empty($this->button_html))
  271. {
  272. $this->button(false, array()); // generate a default button is none are defined via the form builder
  273. $this->output .= $this->button_html;
  274. }
  275. }
  276.  
  277.  
  278. $this->output .= '</fieldset></form>'.$success;
  279.  
  280. if($return)
  281. {
  282. return $this->output;
  283. }
  284. else
  285. {
  286. echo $this->output;
  287. }
  288. }
  289.  
  290.  
  291.  
  292.  
  293. /**
  294. * html
  295. *
  296. * The html method creates custom html output for descriptions headings etc
  297. * @param string $id holds the key of the element
  298. * @param array $element data array of the element that should be created
  299. */
  300. function html($id, $element)
  301. {
  302. if(!empty($element['content']))
  303. {
  304. $this->elements_html .= "<div id='{$id}' class='av-form-text'>".$element['content']."</div>";
  305. $this->width = 1;
  306. }
  307. }
  308.  
  309.  
  310. function button($id = "", $element = array())
  311. {
  312. if(!empty( $this->button_html )) return;
  313.  
  314. $submit_label = isset( $element['label'] ) ? $element['label'] : $this->form_params['submit'];
  315. $class = isset($element['class']) ? $element['class'] : $this-> auto_width();
  316. if(!empty($class)) $class .= " modified_width";
  317. if(!empty( $element['disabled']) ) $class .= " av-hidden-submit";
  318.  
  319.  
  320. $this->button_html = '<p class="form_element '.$class.'">';
  321. $this->button_html .= '<input type="hidden" value="1" name="avia_generated_form'.$this->formID.'" />';
  322. $this->button_html .= '<input type="submit" value="'.$submit_label.'" class="button" '.$this->submit_attr.' data-sending-label="'.__('Sending','avia_framework').'"/>';
  323. $this->button_html .= '</p>';
  324.  
  325. if($id)
  326. {
  327. $this->elements_html .= $this->button_html;
  328. }
  329. else
  330. {
  331. return $this->button_html;
  332. }
  333. }
  334.  
  335.  
  336. function number($id, $element)
  337. {
  338. $this->text($id, $element, 'number');
  339. }
  340.  
  341. /**
  342. * text
  343. *
  344. * The text method creates input elements with type text, and prefills them with $_POST values if available.
  345. * The method also checks against various input validation cases
  346. * @param string $id holds the key of the element
  347. * @param array $element data array of the element that should be created
  348. */
  349.  
  350. function text($id, $element, $type = 'text')
  351. {
  352.  
  353. $p_class = $required = $element_class = $value = $extra = "";
  354.  
  355. // if($element['check'] == "is_email") $type = 'email'; //cant use this because of ie8 + 9
  356.  
  357. if(!empty($element['check']))
  358. {
  359. $extra = "";
  360. $required = '';
  361. $element_class = $element['check'];
  362. $p_class = $this->check_element($id, $element);
  363. }
  364.  
  365. if(isset($_POST[$id]))
  366. {
  367. $value = esc_html(urldecode($_POST[$id]));
  368. }
  369. else if( !empty( $element['value'] ) )
  370. {
  371. $value = $element['value'];
  372. }
  373.  
  374. $this->elements_html .= "<p class='".$p_class.$element['class']."' id='element_$id'>";
  375. $label = '<label for="'.$id.'">'.$element['label'].$required.'</label>';
  376. $placeholder = "";
  377.  
  378. if($this->placeholder)
  379. {
  380. $label = "";
  381. $placeholder = " placeholder='".$element['label'].$extra."'" ;
  382. }
  383.  
  384. $form_el = ' <input name="'.$id.'" class="text_input '.$element_class.'" type="'.$type.'" id="'.$id.'" value="'.$value.'" '.$placeholder.'/>';
  385.  
  386.  
  387. if(isset($this->form_params['label_first']))
  388. {
  389. $this->elements_html .= $label.$form_el;
  390. }
  391. else
  392. {
  393. $this->elements_html .= $form_el.$label;
  394. }
  395.  
  396. $this->elements_html .= "</p>";
  397. }
  398.  
  399. /**
  400. * datepicker
  401. *
  402. * The text method creates input elements with type datepicker, and prefills them with $_POST values if available.
  403. * The method also checks against various input validation cases
  404. * @param string $id holds the key of the element
  405. * @param array $element data array of the element that should be created
  406. */
  407. function datepicker($id, $element)
  408. {
  409. global $wp_locale;
  410.  
  411. $p_class = $required = $element_class = $value = $extra = "";
  412. $date_format = apply_filters('avf_datepicker_dateformat', 'dd / mm / yy');
  413.  
  414. $placeholder_text = 'DD / MM / YY';
  415.  
  416. if(!empty($element['check']))
  417. {
  418. $required = '';
  419. $element_class = $element['check'];
  420. $p_class = $this->check_element($id, $element);
  421. }
  422.  
  423. if(isset($_POST[$id]))
  424. {
  425. $value = esc_html(urldecode($_POST[$id]));
  426. }
  427. else if( !empty( $element['value'] ) )
  428. {
  429. $value = $element['value'];
  430. }
  431.  
  432. if($this->placeholder)
  433. {
  434. $placeholder_text = $element['label'];
  435. $extra = "";
  436. }
  437.  
  438. $placeholder = apply_filters('avf_datepicker_date_placeholder', $placeholder_text.$extra);
  439.  
  440.  
  441.  
  442. $this->elements_html .= "<p class='".$p_class.$element['class']."' id='element_$id'>";
  443. $form_el = ' <input name="'.$id.'" class="avia_datepicker text_input '.$element_class.'" type="text" id="'.$id.'" value="'.$value.'" placeholder="'.$placeholder.'" />';
  444. $label = '<label for="'.$id.'">'.$element['label'].$required.'</label>';
  445.  
  446. if($this->placeholder)
  447. {
  448. $label = "";
  449. }
  450.  
  451. if(isset($this->form_params['label_first']))
  452. {
  453. $this->elements_html .= $label.$form_el;
  454. }
  455. else
  456. {
  457. $this->elements_html .= $form_el.$label;
  458. }
  459.  
  460. $this->elements_html .= "</p>";
  461.  
  462.  
  463. // wp_enqueue_style('jquery-ui-datepicker'); <-- removed and added own styling to frontend css styles
  464. wp_enqueue_script('jquery-ui-datepicker');
  465.  
  466. $args = array(
  467. 'closeText' => __( 'Close', 'avia_framework' ),
  468. 'currentText' => __( 'Today', 'avia_framework' ),
  469. 'nextText' => __( 'Next', 'avia_framework' ),
  470. 'prevText' => __( 'Prev', 'avia_framework' ),
  471. 'monthNames' => $this->helper_strip_array_indices( $wp_locale->month ),
  472. 'monthNamesShort' => $this->helper_strip_array_indices( $wp_locale->month_abbrev ),
  473. 'dayNames' => $this->helper_strip_array_indices( $wp_locale->weekday ),
  474. 'dayNamesShort' => $this->helper_strip_array_indices( $wp_locale->weekday_abbrev ),
  475. 'dayNamesMin' => $this->helper_strip_array_indices( $wp_locale->weekday_initial ),
  476. 'dateFormat' => $date_format,
  477. 'firstDay' => get_option( 'start_of_week' ),
  478. 'isRTL' => $wp_locale->is_rtl()
  479. );
  480.  
  481. wp_localize_script( 'jquery-ui-datepicker', 'AviaDatepickerTranslation', $args );
  482.  
  483. add_action('wp_footer', array(&$this, 'helper_print_datepicker_script'));
  484. }
  485.  
  486. function helper_print_datepicker_script()
  487. {
  488. echo "\n<script type='text/javascript'>\n";
  489. echo 'jQuery(document).ready(function(){ jQuery(".avia_datepicker").datepicker({
  490. beforeShow: function(input, inst) {
  491. jQuery("#ui-datepicker-div").addClass(this.id);
  492. inst.dpDiv.addClass("avia-datepicker-div");
  493. },
  494. showButtonPanel: true,
  495. closeText: AviaDatepickerTranslation.closeText,
  496. currentText: AviaDatepickerTranslation.currentText,
  497. nextText: AviaDatepickerTranslation.nextText,
  498. prevText: AviaDatepickerTranslation.prevText,
  499. monthNames: AviaDatepickerTranslation.monthNames,
  500. monthNamesShort: AviaDatepickerTranslation.monthNamesShort,
  501. dayName: AviaDatepickerTranslation.dayNames,
  502. dayNamesShort: AviaDatepickerTranslation.dayNamesShort,
  503. dayNamesMin: AviaDatepickerTranslation.dayNamesMin,
  504. dayNames: AviaDatepickerTranslation.dayNames,
  505. dateFormat: AviaDatepickerTranslation.dateFormat,
  506. firstDay: AviaDatepickerTranslation.firstDay,
  507. isRTL: AviaDatepickerTranslation.isRTL,
  508. changeMonth: true,
  509. changeYear: true,
  510. yearRange: "c-80:c+10"
  511. }); });';
  512. echo "\n</script>\n";
  513. }
  514.  
  515. function helper_strip_array_indices( $ArrayToStrip ) {
  516. foreach( $ArrayToStrip as $objArrayItem) {
  517. $NewArray[] = $objArrayItem;
  518. }
  519.  
  520. return( $NewArray );
  521. }
  522.  
  523.  
  524. /**
  525. * checkbox
  526. *
  527. * The text method creates input elements with type checkbox, and prefills them with $_POST values if available.
  528. * The method also checks against various input validation cases
  529. * @param string $id holds the key of the element
  530. * @param array $element data array of the element that should be created
  531. */
  532. function checkbox($id, $element)
  533. {
  534. $p_class = $required = $element_class = $checked = "";
  535.  
  536. if(!empty($element['check']))
  537. {
  538. if(!empty($_POST[$id])) $checked = 'checked="checked"';
  539. $required = '';
  540. $element_class = $element['check'];
  541. $p_class = $this->check_element($id, $element);
  542. }
  543. if(empty($_POST[$id])) $_POST[$id] = "false";
  544.  
  545.  
  546. $this->elements_html .= "<p class='".$p_class.$element['class']."' id='element_$id'>";
  547. $this->elements_html .= ' <input '.$checked.' name="'.$id.'" class="input_checkbox '.$element_class.'" type="checkbox" id="'.$id.'" value="true"/><label class="input_checkbox_label" for="'.$id.'">'.$element['label'].$required.'</label>';
  548. $this->elements_html .= "</p>";
  549. }
  550.  
  551.  
  552. /**
  553. * Select
  554. *
  555. * The select method creates a dropdown element with type select, and prefills them with $_POST values if available.
  556. * The method also checks against various input validation cases
  557. * @param string $id holds the key of the element
  558. * @param array $element data array of the element that should be created
  559. */
  560. function select($id, $element)
  561. {
  562.  
  563. if(empty($element['options'])) return;
  564.  
  565. if(!is_array($element['options']))
  566. {
  567. $element['options'] = str_replace("\,", "&#44;", $element['options'] );
  568. $element['options'] = explode(',',$element['options']);
  569. }
  570.  
  571. $p_class = $required = $element_class = $prefilled_value = $select = $extra = "";
  572.  
  573. if(!empty($element['check']))
  574. {
  575. $extra = "";
  576. $required = '';
  577. $element_class = $element['check'];
  578. $p_class = $this->check_element($id, $element);
  579. }
  580.  
  581. if(isset($_POST[$id]))
  582. {
  583. $prefilled_value = esc_html(urldecode($_POST[$id]));
  584. }
  585. else if( !empty( $element['value'] ) )
  586. {
  587. $prefilled_value = $element['value'];
  588. }
  589.  
  590. if($this->placeholder)
  591. {
  592. $label = array( $element['label'].$extra."|" );
  593. $element['options'] = array_merge($label,$element['options']);
  594. }
  595.  
  596.  
  597. foreach($element['options'] as $option)
  598. {
  599. $key = $value = trim($option);
  600. $suboptions = explode('|',$option);
  601.  
  602. if(is_array($suboptions) && isset($suboptions[1]))
  603. {
  604. $key = trim($suboptions[1]);
  605. $value = trim($suboptions[0]);
  606. }
  607.  
  608.  
  609. $active = $value == $prefilled_value ? "selected='selected'" : "";
  610. $select .= "<option $active value ='$key'>$value</option>";
  611. }
  612.  
  613. $multi = "";
  614. if(!empty($element['multi_select']))
  615. {
  616. $multi = "multiple";
  617. $element_class .= " av-multi-select";
  618. }
  619.  
  620.  
  621. $this->elements_html .= "<p class='".$p_class.$element['class']."' id='element_$id'>";
  622. $form_el = ' <select '.$multi.' name="'.$id.'" class="select '.$element_class.'" id="'.$id.'">'.$select.'</select>';
  623. $label = '<label for="'.$id.'">'.$element['label'].$required.'</label>';
  624.  
  625. if($this->placeholder) $label = "";
  626.  
  627. if(isset($this->form_params['label_first']))
  628. {
  629. $this->elements_html .= $label.$form_el;
  630. }
  631. else
  632. {
  633. $this->elements_html .= $form_el.$label;
  634. }
  635.  
  636. $this->elements_html .= "</p>";
  637. }
  638.  
  639.  
  640. /**
  641. * textarea
  642. *
  643. * The textarea method creates textarea elements, and prefills them with $_POST values if available.
  644. * The method also checks against various input validation cases
  645. * @param string $id holds the key of the element
  646. * @param array $element data array of the element that should be created
  647. */
  648. function textarea($id, $element)
  649. {
  650. $p_class = $required = $element_class = $value = $extra = "";
  651.  
  652. if(!empty($element['check']))
  653. {
  654. $extra = "";
  655. $required = '';
  656. $element_class = $element['check'];
  657. $p_class = $this->check_element($id, $element);
  658. }
  659.  
  660. if(isset($_POST[$id]))
  661. {
  662. $value = esc_html(urldecode($_POST[$id]));
  663. }
  664. else if( !empty( $element['value'] ) )
  665. {
  666. $value = $element['value'];
  667. }
  668.  
  669. $label = ' <label for="'.$id.'" class="textare_label hidden textare_label_'.$id.'">'.$element['label'].$required.'</label>';
  670. $placeholder = "";
  671.  
  672. if($this->placeholder)
  673. {
  674. $label = "";
  675. $placeholder = " placeholder='".$element['label'].$extra."'" ;
  676. }
  677.  
  678.  
  679. $this->elements_html .= "<p class='".$p_class.$element['class']."' id='element_$id'>";
  680. $this->elements_html .= $label;
  681. $this->elements_html .= ' <textarea '.$placeholder.' name="'.$id.'" class="text_area '.$element_class.'" cols="40" rows="7" id="'.$id.'" >'.$value.'</textarea>';
  682. $this->elements_html .= "</p>";
  683. }
  684.  
  685.  
  686.  
  687. /**
  688. * decoy
  689. *
  690. * The decoy method creates input elements with type text but with an extra class that hides them
  691. * The method is used to fool bots into filling the form element. Upon submission we check if the element contains any value, if so we dont submit the form
  692. * @param string $id holds the key of the element
  693. * @param array $element data array of the element that should be created
  694. */
  695. function decoy($id, $element)
  696. {
  697. $p_class = $required = $element_class = "";
  698.  
  699. if(!empty($element['check']))
  700. {
  701. $this->check_element($id, $element);
  702. }
  703.  
  704. $this->elements_html .= '<p class="hidden"><input type="text" name="'.$id.'" class="hidden '.$element_class.'" id="'.$id.'" value="" /></p>';
  705. }
  706.  
  707.  
  708.  
  709. /**
  710. * Captcha
  711. *
  712. * The captcha method creates input element that needs to be filled correctly to send the form
  713. * @param string $id holds the key of the element
  714. * @param array $element data array of the element that should be created
  715. */
  716. function captcha($id, $element)
  717. {
  718. $p_class = $required = $element_class = $value = $valueVer = "";
  719.  
  720. if(!empty($element['check']))
  721. {
  722. $required = '';
  723. $element_class = $element['check'];
  724. $p_class = $this->check_element($id, $element);
  725. }
  726.  
  727. $p_class = $this-> auto_width();
  728.  
  729. if(!empty($_POST[$id])) $value = esc_html(urldecode($_POST[$id]));
  730. if(!empty($_POST[$id.'_verifier'])) $valueVer = esc_html(urldecode($_POST[$id.'_verifier']));
  731.  
  732. if(!$valueVer) $valueVer = str_replace('0','4', str_replace('9','7', rand(123456789, 999999999)));
  733. $reverse = strrev( $valueVer );
  734. $enter = $valueVer[$reverse[0]];
  735. $number_1 = rand(0, $enter);
  736. $number_2 = $enter - $number_1;
  737.  
  738. $this->elements_html .= "<p class='".$p_class."' id='element_$id'>";
  739. $this->elements_html .= " <span class='value_verifier_label'>$number_1 + $number_2 = ?</span>";
  740. $this->elements_html .= ' <input name="'.$id.'_verifier" type="hidden" id="'.$id.'_verifier" value="'.$valueVer.'"/>';
  741. $form_el = ' <input name="'.$id.'" class="text_input '.$element_class.'" type="text" id="'.$id.'" value="'.$value.'"/>';
  742. $label ='<label for="'.$id.'">'.$element['label'].$required.'</label>';
  743.  
  744. if(isset($this->form_params['label_first']))
  745. {
  746. $this->elements_html .= $label.$form_el;
  747. }
  748. else
  749. {
  750. $this->elements_html .= $form_el.$label;
  751. }
  752.  
  753. $this->elements_html .= "</p>";
  754. }
  755.  
  756.  
  757.  
  758. /**
  759. * hidden
  760. *
  761. * The hidden method creates input elements with type hidden, and prefills them with values if available.
  762. * @param string $id holds the key of the element
  763. * @param array $element data array of the element that should be created
  764. */
  765. function hidden($id, $element)
  766. {
  767. $this->elements_html .= '<input type="hidden" name="'.$id.'" id="'.$id.'" value="'.$element['value'].'" />';
  768. }
  769.  
  770.  
  771. /**
  772. * Send the form
  773. *
  774. * The send method tries to send the form. It builds the necessary email and submits it via wp_mail
  775. */
  776. function send( $self_instance )
  777. {
  778. $new_post = array();
  779. foreach ($_POST as $key => $post)
  780. {
  781. $new_post[str_replace('avia_','',$key)] = $post;
  782. }
  783.  
  784.  
  785. $mymail = empty($this->form_params['myemail']) ? $new_post['myemail'] : $this->form_params['myemail'];
  786. $myblogname = empty($this->form_params['myblogname']) ? $new_post['myblogname'] : $this->form_params['myblogname'];
  787.  
  788. if(empty($new_post['subject_'.$this->formID]) && !empty($this->form_params['subject'])) $new_post['subject_'.$this->formID] = $this->form_params['subject'];
  789. $subject = empty($new_post['subject_'.$this->formID]) ? __("New Message", 'avia_framework') . " (".__('sent by contact form at','avia_framework')." ".$myblogname.")" : $new_post['subject_'.$this->formID];
  790.  
  791. $default_from = parse_url(home_url());
  792.  
  793.  
  794. //hook to stop execution here and do something different with the data
  795. $proceed = apply_filters( 'avf_form_send', true, $new_post, $this->form_params, $this );
  796.  
  797. if( ! $proceed )
  798. {
  799. if( is_null( $proceed ) )
  800. {
  801. return false;
  802. }
  803. else
  804. {
  805. return true;
  806. }
  807. }
  808.  
  809. //set the email adress
  810. $from = "[email protected]";
  811. $usermail = false;
  812.  
  813. if(!empty($default_from['host'])) $from = "no-reply@".$default_from['host'];
  814.  
  815. if(!empty($this->autoresponder[0]))
  816. {
  817. $from = $_POST[$this->autoresponder[0]];
  818. $usermail = true;
  819. }
  820. else
  821. {
  822. $email_variations = array( 'e-mail', 'email', 'mail' );
  823.  
  824. foreach($email_variations as $key)
  825. {
  826. foreach ($new_post as $current_key => $current_post)
  827. {
  828. if( strpos($current_key, $key) !== false)
  829. {
  830. $from = $new_post[$current_key];
  831. $usermail = true;
  832. break;
  833. }
  834.  
  835. }
  836.  
  837. if($usermail == true) break;
  838. }
  839. }
  840.  
  841.  
  842. $to = urldecode( $mymail );
  843.  
  844. $delimiter = ",";
  845. if(strpos($to, ',') === false && strpos($to, ' ') !== false) $delimiter = " ";
  846.  
  847. $to = array_filter(array_map('trim', explode($delimiter, $to)));
  848. $to = apply_filters("avf_form_sendto", $to, $new_post, $this->form_params);
  849.  
  850. $from = urldecode( $from );
  851. $from = apply_filters("avf_form_from", $from, $new_post, $this->form_params);
  852.  
  853. $subject = urldecode( $subject );
  854. $subject = apply_filters("avf_form_subject", $subject, $new_post, $this->form_params);
  855.  
  856. $message = "";
  857. $iterations = 0;
  858.  
  859. foreach($this->form_elements as $key => $element)
  860. {
  861. if(isset($element['id'])) $key = $element['id'];
  862.  
  863. $key = avia_backend_safe_string($key, '_', true);
  864.  
  865. if(empty($key) || !empty($this->form_params['numeric_names']) )
  866. {
  867. $iterations++;
  868. $key = $iterations;
  869. }
  870.  
  871. // substract 5 characters from the string length because we removed the avia_ prefix with 5 characters at the beginning of the send() function
  872. $key = avia_backend_truncate($key, $this->length - 5, "_", "", false, '', false);
  873.  
  874. $key .= $this->id_sufix;
  875.  
  876. if(!empty($new_post[$key]))
  877. {
  878. if($element['type'] != 'hidden' && $element['type'] != 'decoy')
  879. {
  880. if($element['type'] == 'textarea') $message .= " <br/>";
  881. $field_value = apply_filters( "avf_form_mail_field_values", nl2br(urldecode($new_post[$key])), $new_post, $this->form_elements, $this->form_params, $element, $key );
  882. $message .= $element['label'].": ".$field_value." <br/>";
  883. if($element['type'] == 'textarea') $message .= " <br/>";
  884. }
  885. }
  886. }
  887.  
  888.  
  889. $use_wpmail = apply_filters("avf_form_use_wpmail", true, $new_post, $this->form_params);
  890.  
  891. //$header = 'MIME-Version: 1.0' . "\r\n";
  892. $header = 'Content-type: text/html; charset=utf-8' . "\r\n";
  893. $header = apply_filters("avf_form_mail_header", $header, $new_post, $this->form_params);
  894. $copy = apply_filters("avf_form_copy", $to, $new_post, $this->form_params);
  895.  
  896. $message = apply_filters("avf_form_message", stripslashes($message), $new_post, $this->form_params);
  897.  
  898. foreach($copy as $send_to_mail)
  899. {
  900. //if a demo email is mistakenly used change it to the admin url
  901. if( strpos( $send_to_mail, '@kriesi.at') !== false && isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] != "www.kriesi.at")
  902. {
  903. if(!defined('AV_TESTSERVER'))
  904. {
  905. $send_to_mail = get_admin_url();
  906. }
  907. }
  908.  
  909.  
  910. if($use_wpmail)
  911. {
  912. $header .= 'From: '. $from . " <".$from."> \r\n";
  913. wp_mail($send_to_mail, $subject, $message, $header);
  914. }
  915. else
  916. {
  917. $header .= 'From:'. $from . " \r\n";
  918. mail($send_to_mail, $subject, $message, $header);
  919. }
  920. }
  921.  
  922. //autoresponder?
  923. if($usermail && !empty($this->form_params['autoresponder']))
  924. {
  925. //$header = 'MIME-Version: 1.0' . "\r\n";
  926. $header = 'Content-type: text/html; charset=utf-8' . "\r\n";
  927. $header = apply_filters("avf_form_mail_header", $header, $new_post, $this->form_params);
  928.  
  929.  
  930. $message = nl2br($this->form_params['autoresponder'])."<br/><br/><br/><strong>".__('Your Message:','avia_framework')." </strong><br/><br/>".$message;
  931. $message = apply_filters("avf_form_autorespondermessage", $message);
  932.  
  933. $from = apply_filters("avf_form_autoresponder_from", $from, $new_post, $this->form_params);
  934.  
  935.  
  936. $this->form_params['autoresponder_email'] = array_filter(array_map('trim', explode($delimiter, $this->form_params['autoresponder_email'])));
  937.  
  938. if(is_array($this->form_params['autoresponder_email']))
  939. {
  940. $this->form_params['autoresponder_email'] = $this->form_params['autoresponder_email'][0];
  941. }
  942.  
  943.  
  944.  
  945. if($use_wpmail)
  946. {
  947. $header .= 'From:' . get_bloginfo('name') .' <'. urldecode( $this->form_params['autoresponder_email']) . "> \r\n";
  948. $result = wp_mail($from, $this->form_params['autoresponder_subject'], $message, $header);
  949. }
  950. else
  951. {
  952. $header .= 'From:'. urldecode( $this->form_params['autoresponder_email']) . " \r\n";
  953. mail($from, $this->form_params['autoresponder_subject'], $message, $header);
  954. }
  955. }
  956. unset($_POST);
  957. return true;
  958. //return wp_mail( $to, $subject, $message , $header);
  959.  
  960.  
  961. }
  962.  
  963.  
  964. /**
  965. * Check the value of an element
  966. *
  967. * The check_element method creates checks if the submitted value of a post element is valid
  968. * @param string $id holds the key of the element
  969. * @param array $element data array of the element that should be created
  970. */
  971. function check_element($id, $element)
  972. {
  973. if(isset($_POST) && count($_POST) && isset($_POST[$id]) && $this->do_checks)
  974. {
  975. switch ($element['check'])
  976. {
  977. case 'is_empty':
  978.  
  979. if(!empty($_POST[$id]) || $_POST[$id] === "0") return "valid";
  980.  
  981. break;
  982.  
  983. case 'must_empty':
  984.  
  985. if(isset($_POST[$id]) && $_POST[$id] == "") return "valid";
  986.  
  987. break;
  988.  
  989. case 'is_email':
  990.  
  991. $this->autoresponder[] = $id;
  992. if(preg_match("!^[\w|\.|\-]+@\w[\w|\.|\-]*\.[a-zA-Z]{2,20}$!", urldecode($_POST[$id]))) return "valid";
  993.  
  994. break;
  995.  
  996. case 'is_number':
  997.  
  998. if(preg_match("!^[1-9]\d*([\.|\,]\d+)?$!", urldecode($_POST[$id]))) return "valid";
  999.  
  1000. break;
  1001.  
  1002. case 'is_phone':
  1003.  
  1004. if(preg_match("!^(\d|\s|\-|\/|\(|\)|\[|\]|e|x|t|ension|\.|\+|\_|\,|\:|\;){3,}$!", urldecode($_POST[$id]))) return "valid";
  1005.  
  1006. break;
  1007.  
  1008. case 'is_url':
  1009.  
  1010. if(preg_match("!^(https?|ftp)://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?$!", urldecode($_POST[$id]))) return "valid";
  1011.  
  1012. break;
  1013.  
  1014. case 'captcha':
  1015.  
  1016. $ver = $_POST[$id.'_verifier'];
  1017. $reverse = strrev( $ver );
  1018.  
  1019. if($ver[$reverse[0]] == $_POST[$id])
  1020. {
  1021. unset($_POST[$id], $_POST[$id.'_verifier']);
  1022. return "valid";
  1023. }
  1024. break;
  1025.  
  1026. } //end switch
  1027.  
  1028. $this->submit_form = false;
  1029. return "error";
  1030. }
  1031. }
  1032. }
  1033. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement