Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 34.48 KB | None | 0 0
  1. <?php
  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 = ' <abbr class="required" title="'.__( 'required', 'avia_framework' ).'">*</abbr>';
  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 = ' <abbr class="required" title="'.__( 'required', 'avia_framework' ).'">*</abbr>';
  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.                     //  empty label - keep default placeholder
  435.                 if( ! empty( $element['label'] ) )
  436.                 {
  437.                     $placeholder_text = $element['label'];
  438.                 }
  439.                 if( ! empty( $element['check'] ) )
  440.                 {
  441.                     $extra = "*";
  442.                 }
  443.             }
  444.  
  445.             $placeholder = apply_filters('avf_datepicker_date_placeholder', $placeholder_text.$extra);
  446.  
  447.  
  448.  
  449.             $this->elements_html .= "<p class='".$p_class.$element['class']."' id='element_$id'>";
  450.             $form_el = ' <input name="'.$id.'" class="avia_datepicker text_input '.$element_class.'" type="text" id="'.$id.'" value="'.$value.'" placeholder="'.$placeholder.'" />';
  451.             $label = '<label for="'.$id.'">'.$element['label'].$required.'</label>';
  452.            
  453.             if($this->placeholder)
  454.             {
  455.                 $label = "";
  456.             }
  457.            
  458.             if(isset($this->form_params['label_first']))
  459.             {
  460.                 $this->elements_html .= $label.$form_el;
  461.             }
  462.             else
  463.             {
  464.                 $this->elements_html .= $form_el.$label;
  465.             }
  466.  
  467.             $this->elements_html .= "</p>";
  468.  
  469.  
  470.             // wp_enqueue_style('jquery-ui-datepicker'); <-- removed and added own styling to frontend css styles
  471.             wp_enqueue_script('jquery-ui-datepicker');
  472.  
  473.             $args = array(
  474.                 'closeText'         => __( 'Close', 'avia_framework' ),
  475.                 'currentText'       => __( 'Today', 'avia_framework' ),
  476.                 'nextText'          => __( 'Next', 'avia_framework' ),
  477.                 'prevText'          => __( 'Prev', 'avia_framework' ),
  478.                 'monthNames'        => $this->helper_strip_array_indices( $wp_locale->month ),
  479.                 'monthNamesShort'   => $this->helper_strip_array_indices( $wp_locale->month_abbrev ),
  480.                 'dayNames'          => $this->helper_strip_array_indices( $wp_locale->weekday ),
  481.                 'dayNamesShort'     => $this->helper_strip_array_indices( $wp_locale->weekday_abbrev ),
  482.                 'dayNamesMin'       => $this->helper_strip_array_indices( $wp_locale->weekday_initial ),
  483.                 'dateFormat'        => $date_format,
  484.                 'firstDay'          => get_option( 'start_of_week' ),
  485.                 'isRTL'             => $wp_locale->is_rtl()
  486.             );
  487.  
  488.             wp_localize_script( 'jquery-ui-datepicker', 'AviaDatepickerTranslation', $args );
  489.  
  490.             add_action('wp_footer', array(&$this, 'helper_print_datepicker_script'));
  491.         }
  492.  
  493.         function helper_print_datepicker_script()
  494.         {
  495.             echo "\n<script type='text/javascript'>\n";
  496.             echo 'jQuery(document).ready(function(){ jQuery(".avia_datepicker").datepicker({
  497.                beforeShowDay: function(date){
  498.        var string = jQuery.datepicker.formatDate('2017-06-18', date);
  499.        return [$.inArray(string, array) == -1];
  500.    }
  501.                 beforeShow: function(input, inst) {
  502.                    jQuery("#ui-datepicker-div").addClass(this.id);
  503.                    inst.dpDiv.addClass("avia-datepicker-div");
  504.                },
  505.                showButtonPanel: true,
  506.                closeText: AviaDatepickerTranslation.closeText,
  507.                currentText: AviaDatepickerTranslation.currentText,
  508.                nextText: AviaDatepickerTranslation.nextText,
  509.                prevText: AviaDatepickerTranslation.prevText,
  510.                monthNames: AviaDatepickerTranslation.monthNames,
  511.                monthNamesShort: AviaDatepickerTranslation.monthNamesShort,
  512.                dayName: AviaDatepickerTranslation.dayNames,
  513.                dayNamesShort: AviaDatepickerTranslation.dayNamesShort,
  514.                dayNamesMin: AviaDatepickerTranslation.dayNamesMin,
  515.                dayNames: AviaDatepickerTranslation.dayNames,
  516.                dateFormat: AviaDatepickerTranslation.dateFormat,
  517.                firstDay: AviaDatepickerTranslation.firstDay,
  518.                isRTL: AviaDatepickerTranslation.isRTL,
  519.                changeMonth: true,
  520.                 changeYear: true,
  521.                 yearRange: "c-80:c+10"
  522.            }); });';
  523.             echo "\n</script>\n";
  524.         }
  525.  
  526.         function helper_strip_array_indices( $ArrayToStrip ) {
  527.             foreach( $ArrayToStrip as $objArrayItem) {
  528.                 $NewArray[] = $objArrayItem;
  529.             }
  530.  
  531.             return( $NewArray );
  532.         }
  533.  
  534.  
  535.         /**
  536.          * checkbox
  537.          *
  538.          * The text method creates input elements with type checkbox, and prefills them with $_POST values if available.
  539.          * The method also checks against various input validation cases
  540.          * @param string $id holds the key of the element
  541.          * @param array $element data array of the element that should be created
  542.          */
  543.         function checkbox($id, $element)
  544.         {
  545.             $p_class = $required = $element_class = $checked = "";
  546.            
  547.             if( ! empty( $element['av_contact_preselect'] ) )
  548.             {
  549.                 $checked = 'checked="checked"';
  550.             }
  551.             else if(!empty($element['check']))
  552.             {
  553.                 if(!empty($_POST[$id])) $checked = 'checked="checked"';
  554.                 $required = ' <abbr class="required" title="'.__( 'required', 'avia_framework' ).'">*</abbr>';
  555.                 $element_class = $element['check'];
  556.                 $p_class = $this->check_element($id, $element);
  557.             }
  558.             if(empty($_POST[$id])) $_POST[$id] = "false";
  559.  
  560.  
  561.             $this->elements_html .= "<p class='".$p_class.$element['class']."' id='element_$id'>";
  562.             $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>';
  563.             $this->elements_html .= "</p>";
  564.         }
  565.  
  566.  
  567.         /**
  568.          * Select
  569.          *
  570.          * The select method creates a dropdown element with type select, and prefills them with $_POST values if available.
  571.          * The method also checks against various input validation cases
  572.          *
  573.          * @param string $id holds the key of the element
  574.          * @param array $element data array of the element that should be created
  575.          */
  576.         function select( $id, $element )
  577.         {
  578.  
  579.             if(empty($element['options'])) return;
  580.  
  581.             if(!is_array($element['options']))
  582.             {
  583.                 $element['options'] = str_replace( array( "\,", ',,' ), "&#44;", $element['options'] );
  584.                 $element['options'] = explode(',',$element['options'] );
  585.             }
  586.  
  587.             $p_class = $required = $element_class = $prefilled_value = $select = $extra  = "";
  588.  
  589.             if(!empty($element['check']))
  590.             {
  591.                 $extra = "*";
  592.                 $required = ' <abbr class="required" title="'.__( 'required', 'avia_framework' ).'">*</abbr>';
  593.                 $element_class = $element['check'];
  594.                 $p_class = $this->check_element($id, $element);
  595.             }
  596.            
  597.             if(isset($_POST[$id]))
  598.             {
  599.                 $prefilled_value = esc_html(urldecode($_POST[$id]));
  600.             }
  601.             else if( !empty( $element['value'] ) )
  602.             {
  603.                 $prefilled_value = $element['value'];
  604.             }
  605.            
  606.             if($this->placeholder)
  607.             {
  608.                 $label = array( $element['label'].$extra."|" );
  609.                 $element['options'] = array_merge($label,$element['options']);
  610.             }
  611.            
  612.            
  613.             foreach($element['options'] as $option)
  614.             {
  615.                 $key = $value = trim($option);
  616.                 $suboptions =  explode('|',$option);
  617.                
  618.                 if(is_array($suboptions) && isset($suboptions[1]))
  619.                 {
  620.                     $key = trim($suboptions[1]);
  621.                     $value = trim($suboptions[0]);
  622.                 }
  623.  
  624.  
  625.                 $active = $value == $prefilled_value ? "selected='selected'" : "";
  626.                 $select .= "<option $active value ='$key'>$value</option>";
  627.             }
  628.  
  629.             $multi = "";
  630.             if(!empty($element['multi_select']))
  631.             {
  632.                 $multi = "multiple";
  633.                 $element_class .= " av-multi-select";
  634.             }
  635.  
  636.  
  637.             $this->elements_html .= "<p class='".$p_class.$element['class']."' id='element_$id'>";
  638.             $form_el = ' <select '.$multi.' name="'.$id.'" class="select '.$element_class.'" id="'.$id.'">'.$select.'</select>';
  639.             $label = '<label for="'.$id.'">'.$element['label'].$required.'</label>';
  640.  
  641.             if($this->placeholder) $label = "";
  642.  
  643.             if(isset($this->form_params['label_first']))
  644.             {
  645.                 $this->elements_html .= $label.$form_el;
  646.             }
  647.             else
  648.             {
  649.                 $this->elements_html .= $form_el.$label;
  650.             }
  651.  
  652.             $this->elements_html .= "</p>";
  653.         }
  654.  
  655.  
  656.         /**
  657.          * textarea
  658.          *
  659.          * The textarea method creates textarea elements, and prefills them with $_POST values if available.
  660.          * The method also checks against various input validation cases
  661.          * @param string $id holds the key of the element
  662.          * @param array $element data array of the element that should be created
  663.          */
  664.         function textarea($id, $element)
  665.         {
  666.             $p_class = $required = $element_class = $value = $extra = "";
  667.  
  668.             if(!empty($element['check']))
  669.             {
  670.                 $extra = "*";
  671.                 $required = ' <abbr class="required" title="'.__( 'required', 'avia_framework' ).'">*</abbr>';
  672.                 $element_class = $element['check'];
  673.                 $p_class = $this->check_element($id, $element);
  674.             }
  675.  
  676.             if(isset($_POST[$id]))
  677.             {
  678.                 $value = esc_html(urldecode($_POST[$id]));
  679.             }
  680.             else if( !empty( $element['value'] ) )
  681.             {
  682.                 $value = $element['value'];
  683.             }
  684.            
  685.             $label = '   <label for="'.$id.'" class="textare_label hidden textare_label_'.$id.'">'.$element['label'].$required.'</label>';
  686.             $placeholder = "";
  687.            
  688.             if($this->placeholder)
  689.             {
  690.                 $label = "";
  691.                 $placeholder = " placeholder='".$element['label'].$extra."'" ;
  692.             }
  693.            
  694.  
  695.             $this->elements_html .= "<p class='".$p_class.$element['class']."' id='element_$id'>";
  696.             $this->elements_html .= $label;
  697.             $this->elements_html .= '    <textarea '.$placeholder.' name="'.$id.'" class="text_area '.$element_class.'" cols="40" rows="7" id="'.$id.'" >'.$value.'</textarea>';
  698.             $this->elements_html .= "</p>";
  699.         }
  700.  
  701.  
  702.  
  703.         /**
  704.          * decoy
  705.          *
  706.          * The decoy method creates input elements with type text but with an extra class that hides them
  707.          * 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
  708.          * @param string $id holds the key of the element
  709.          * @param array $element data array of the element that should be created
  710.          */
  711.         function decoy($id, $element)
  712.         {
  713.             $p_class = $required = $element_class = "";
  714.  
  715.             if(!empty($element['check']))
  716.             {
  717.                 $this->check_element($id, $element);
  718.             }
  719.  
  720.             $this->elements_html .= '<p class="hidden"><input type="text" name="'.$id.'" class="hidden '.$element_class.'" id="'.$id.'" value="" /></p>';
  721.         }
  722.  
  723.  
  724.  
  725.         /**
  726.          * Captcha
  727.          *
  728.          * The captcha method creates input element that needs to be filled  correctly to send the form
  729.          * @param string $id holds the key of the element
  730.          * @param array $element data array of the element that should be created
  731.          */
  732.         function captcha($id, $element)
  733.         {
  734.             $p_class = $required = $element_class = $value = $valueVer = "";
  735.  
  736.             if(!empty($element['check']))
  737.             {
  738.                 $required = ' <abbr class="required" title="'.__( 'required', 'avia_framework' ).'">*</abbr>';
  739.                 $element_class = $element['check'];
  740.                 $p_class = $this->check_element($id, $element);
  741.             }
  742.  
  743.             $p_class = $this-> auto_width();
  744.  
  745.             if(!empty($_POST[$id])) $value = esc_html(urldecode($_POST[$id]));
  746.             if(!empty($_POST[$id.'_verifier'])) $valueVer = esc_html(urldecode($_POST[$id.'_verifier']));
  747.  
  748.             if(!$valueVer) $valueVer    = str_replace('0','4', str_replace('9','7', rand(123456789, 999999999)));
  749.             $reverse    = strrev( $valueVer );
  750.             $enter      = $valueVer[$reverse[0]];
  751.             $number_1   = rand(0, $enter);
  752.             $number_2   = $enter - $number_1;
  753.  
  754.             $this->elements_html .= "<p class='".$p_class."' id='element_$id'>";
  755.             $this->elements_html .= "    <span class='value_verifier_label'>$number_1 + $number_2 = ?</span>";
  756.             $this->elements_html .= '    <input name="'.$id.'_verifier" type="hidden" id="'.$id.'_verifier" value="'.$valueVer.'"/>';
  757.             $form_el = '    <input name="'.$id.'" class="text_input '.$element_class.'" type="text" id="'.$id.'" value="'.$value.'"/>';
  758.             $label ='<label for="'.$id.'">'.$element['label'].$required.'</label>';
  759.  
  760.             if(isset($this->form_params['label_first']))
  761.             {
  762.                 $this->elements_html .= $label.$form_el;
  763.             }
  764.             else
  765.             {
  766.                 $this->elements_html .= $form_el.$label;
  767.             }
  768.  
  769.             $this->elements_html .= "</p>";
  770.         }
  771.  
  772.  
  773.  
  774.         /**
  775.          * hidden
  776.          *
  777.          * The hidden method creates input elements with type hidden, and prefills them with values if available.
  778.          * @param string $id holds the key of the element
  779.          * @param array $element data array of the element that should be created
  780.          */
  781.         function hidden($id, $element)
  782.         {
  783.             $this->elements_html .= '<input type="hidden" name="'.$id.'" id="'.$id.'" value="'.$element['value'].'" />';
  784.         }
  785.  
  786.  
  787.         /**
  788.          * Send the form
  789.          *
  790.          * The send method tries to send the form. It builds the necessary email and submits it via wp_mail
  791.          */
  792.         function send( $self_instance )
  793.         {  
  794.             $new_post = array();
  795.             foreach ($_POST as $key => $post)
  796.             {
  797.                 $new_post[str_replace('avia_','',$key)] = $post;
  798.             }
  799.            
  800.            
  801.             $mymail     = empty($this->form_params['myemail']) ? $new_post['myemail'] : $this->form_params['myemail'];
  802.             $myblogname = empty($this->form_params['myblogname']) ? $new_post['myblogname'] : $this->form_params['myblogname'];
  803.  
  804.             if(empty($new_post['subject_'.$this->formID]) && !empty($this->form_params['subject'])) $new_post['subject_'.$this->formID] = $this->form_params['subject'];
  805.             $subject    = empty($new_post['subject_'.$this->formID]) ? __("New Message", 'avia_framework') . " (".__('sent by contact form at','avia_framework')." ".$myblogname.")"  : $new_post['subject_'.$this->formID];
  806.  
  807.             $default_from = parse_url(home_url());
  808.  
  809.  
  810.             //hook to stop execution here and do something different with the data
  811.             $proceed = apply_filters( 'avf_form_send', true, $new_post, $this->form_params, $this );
  812.  
  813.             if( ! $proceed )
  814.             {
  815.                 if( is_null( $proceed ) )
  816.                 {
  817.                     return false;
  818.                 }
  819.                 else
  820.                 {
  821.                     return true;
  822.                 }
  823.             }
  824.  
  825.             //set the email adress
  826.             $from = "[email protected]";
  827.             $usermail = false;
  828.  
  829.             if(!empty($default_from['host'])) $from = "no-reply@".$default_from['host'];
  830.  
  831.             if(!empty($this->autoresponder[0]))
  832.             {
  833.                 $from = $_POST[$this->autoresponder[0]];
  834.                 $usermail = true;
  835.             }
  836.             else
  837.             {
  838.                 $email_variations = array( 'e-mail', 'email', 'mail' );
  839.  
  840.                 foreach($email_variations as $key)
  841.                 {
  842.                     foreach ($new_post as $current_key => $current_post)
  843.                     {
  844.                         if( strpos($current_key, $key) !== false)
  845.                         {
  846.                             $from = $new_post[$current_key];
  847.                             $usermail = true;
  848.                             break;
  849.                         }
  850.  
  851.                     }
  852.  
  853.                     if($usermail == true) break;
  854.                 }
  855.             }
  856.            
  857.            
  858.             $to = urldecode( $mymail );
  859.            
  860.             $delimiter = ",";
  861.             if(strpos($to, ',') === false && strpos($to, ' ') !== false) $delimiter = " ";
  862.            
  863.             $to = array_filter(array_map('trim', explode($delimiter, $to)));
  864.             $to = apply_filters("avf_form_sendto", $to, $new_post, $this->form_params);
  865.    
  866.             $from = urldecode( $from );
  867.             $from = apply_filters("avf_form_from", $from, $new_post, $this->form_params);
  868.  
  869.             $subject = urldecode( $subject );
  870.             $subject = apply_filters("avf_form_subject", $subject, $new_post, $this->form_params);
  871.  
  872.             $message = "";
  873.             $iterations = 0;
  874.  
  875.             foreach($this->form_elements as $key => $element)
  876.             {
  877.                 if(isset($element['id'])) $key = $element['id'];
  878.                
  879.                 $key = avia_backend_safe_string($key, '_', true);
  880.  
  881.                 if(empty($key) || !empty($this->form_params['numeric_names']) )
  882.                 {
  883.                     $iterations++;
  884.                     $key = $iterations;
  885.                 }
  886.  
  887.                 // substract 5 characters from the string length because we removed the avia_ prefix with 5 characters at the beginning of the send() function
  888.                 $key = avia_backend_truncate($key, $this->length - 5, "_", "", false, '', false);
  889.  
  890.                 $key .= $this->id_sufix;
  891.  
  892.                 if(!empty($new_post[$key]))
  893.                 {
  894.                     if($element['type'] != 'hidden' && $element['type'] != 'decoy')
  895.                     {
  896.                         if($element['type'] == 'textarea') $message .= " <br/>";
  897.                         $field_value = apply_filters( "avf_form_mail_field_values", nl2br(urldecode($new_post[$key])), $new_post, $this->form_elements, $this->form_params, $element, $key );
  898.                         $message .= $element['label'].": ".$field_value." <br/>";
  899.                         if($element['type'] == 'textarea') $message .= " <br/>";
  900.                     }
  901.                 }
  902.             }
  903.  
  904.  
  905.             $use_wpmail = apply_filters("avf_form_use_wpmail", true, $new_post, $this->form_params);
  906.  
  907.             //$header  = 'MIME-Version: 1.0' . "\r\n";
  908.             $header = 'Content-type: text/html; charset=utf-8' . "\r\n";
  909.             $header = apply_filters("avf_form_mail_header", $header, $new_post, $this->form_params);
  910.             $copy   = apply_filters("avf_form_copy", $to, $new_post, $this->form_params);
  911.            
  912.             $message = apply_filters("avf_form_message", stripslashes($message), $new_post, $this->form_params);
  913.            
  914.             foreach($copy as $send_to_mail)
  915.             {
  916.                 //if a demo email is mistakenly used change it to the admin url
  917.                 if( strpos( $send_to_mail, '@kriesi.at') !== false && isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] != "www.kriesi.at")
  918.                 {
  919.                     if(!defined('AV_TESTSERVER'))
  920.                     {
  921.                         $send_to_mail = get_option('admin_email');
  922.                     }
  923.                 }
  924.                
  925.                
  926.                 if($use_wpmail)
  927.                 {
  928.                     $header .= 'From: '. $from . " <".$from."> \r\n";
  929.                     wp_mail($send_to_mail, $subject, $message, $header);
  930.                 }
  931.                 else
  932.                 {
  933.                     $header .= 'From:'. $from . " \r\n";
  934.                     mail($send_to_mail, $subject, $message, $header);
  935.                 }
  936.             }
  937.  
  938.             /**
  939.              * Allow to add a custom autoresponder - must return complete HTML.
  940.              * The returned string is packed in a div and only HTML structure of string is validated.
  941.              *
  942.              * @since v4.0.6
  943.              */
  944.             $custom_autoresponser = apply_filters( 'avf_form_custom_autoresponder', '', $message, $this, $new_post );
  945.            
  946.             if( $usermail && ( ! empty( $this->form_params['autoresponder'] ) || ! empty( $custom_autoresponser ) ) )
  947.             {
  948.                 //$header  = 'MIME-Version: 1.0' . "\r\n";
  949.                 $header = 'Content-type: text/html; charset=utf-8' . "\r\n";
  950.                 $header = apply_filters("avf_form_mail_header", $header, $new_post, $this->form_params);
  951.  
  952.                 if( ! empty( $custom_autoresponser ) )
  953.                 {
  954.                     $message = '<div class="avia_custom_autoresponder">' . wp_kses_post( balanceTags( $custom_autoresponser, true ) ) . '</div>';
  955.                 }
  956.                 else
  957.                 {
  958.                     $autoresponder = nl2br( $this->form_params['autoresponder'] );
  959.                     $message = $autoresponder . "<br /><br /><br /><strong>" . __('Your Message:','avia_framework') . " </strong><br /><br />" . $message;
  960.                     $message = apply_filters("avf_form_autorespondermessage", $message);
  961.                 }
  962.                    
  963.                 $from = apply_filters( "avf_form_autoresponder_from", $from, $new_post, $this->form_params );
  964.  
  965.                 $this->form_params['autoresponder_email'] = array_filter( array_map( 'trim', explode( $delimiter, $this->form_params['autoresponder_email'] ) ) );
  966.                
  967.                 if( is_array( $this->form_params['autoresponder_email'] ) )
  968.                 {
  969.                     $this->form_params['autoresponder_email'] = $this->form_params['autoresponder_email'][0];
  970.                 }
  971.  
  972.                 if( $use_wpmail )
  973.                 {
  974.                     $header .= 'From:' . get_bloginfo('name') .' <'. urldecode( $this->form_params['autoresponder_email'] ) . "> \r\n";
  975.                     $result = wp_mail( $from, $this->form_params['autoresponder_subject'], $message, $header );
  976.                 }
  977.                 else
  978.                 {
  979.                     $header .= 'From:'. urldecode( $this->form_params['autoresponder_email'] ) . " \r\n";
  980.                     mail( $from, $this->form_params['autoresponder_subject'], $message, $header );
  981.                 }
  982.             }
  983.             unset($_POST);
  984.             return true;
  985.             //return wp_mail( $to, $subject, $message , $header);
  986.  
  987.  
  988.         }
  989.  
  990.  
  991.         /**
  992.          * Check the value of an element
  993.          *
  994.          * The check_element method creates checks if the submitted value of a post element is valid
  995.          * @param string $id holds the key of the element
  996.          * @param array $element data array of the element that should be created
  997.          */
  998.         function check_element($id, $element)
  999.         {
  1000.             if(isset($_POST) && count($_POST) && isset($_POST[$id]) && $this->do_checks)
  1001.             {
  1002.                 switch ($element['check'])
  1003.                 {
  1004.                     case 'is_empty':
  1005.  
  1006.                         if(!empty($_POST[$id]) || $_POST[$id] === "0") return "valid";
  1007.  
  1008.                     break;
  1009.  
  1010.                     case 'must_empty':
  1011.  
  1012.                         if(isset($_POST[$id]) && $_POST[$id] == "") return "valid";
  1013.  
  1014.                     break;
  1015.  
  1016.                     case 'is_email':
  1017.  
  1018.                         $this->autoresponder[] = $id;
  1019.                         if(preg_match("!^[\w|\.|\-]+@\w[\w|\.|\-]*\.[a-zA-Z]{2,20}$!", urldecode($_POST[$id]))) return "valid";
  1020.  
  1021.                     break;
  1022.  
  1023.                     case 'is_number':
  1024.  
  1025.                         if(preg_match("!^[1-9]\d*([\.|\,]\d+)?$!", urldecode($_POST[$id]))) return "valid";
  1026.  
  1027.                     break;
  1028.  
  1029.                     case 'is_phone':
  1030.  
  1031.                         if(preg_match("!^(\d|\s|\-|\/|\(|\)|\[|\]|e|x|t|ension|\.|\+|\_|\,|\:|\;){3,}$!", urldecode($_POST[$id]))) return "valid";
  1032.  
  1033.                     break;
  1034.                    
  1035.                     case 'is_url':
  1036.  
  1037.                         if(preg_match("!^(https?|ftp)://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?$!", urldecode($_POST[$id]))) return "valid";
  1038.  
  1039.                     break;
  1040.  
  1041.                     case 'captcha':
  1042.  
  1043.                         $ver = $_POST[$id.'_verifier'];
  1044.                         $reverse = strrev( $ver );
  1045.  
  1046.                         if($ver[$reverse[0]] == $_POST[$id])
  1047.                         {
  1048.                             unset($_POST[$id], $_POST[$id.'_verifier']);
  1049.                             return "valid";
  1050.                         }
  1051.                     break;
  1052.  
  1053.                 } //end switch
  1054.  
  1055.                 $this->submit_form = false;
  1056.                 return "error";
  1057.             }
  1058.         }
  1059.     }
  1060. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement