PluginProbe
Smart Forms – when you need more than just a contact form / 0.8
Smart Forms – when you need more than just a contact form v0.8
trunk 0.5 0.5.5 0.6 0.7 0.8 0.8.5 0.9.1
smart-forms / smart-forms-ajax.php

smart-forms-ajax.php in Smart Forms – when you need more than just a contact form 0.8, at smart-forms-ajax.php

343 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function GetPostValue($parameterName)
4 {
5 if(isset($_POST[$parameterName]))
6 return stripslashes($_POST[$parameterName]);
7
8 return "";
9 }
10
11 function rednao_smart_forms_save()
12 {
13 $form_id=GetPostValue("id");
14 $element_options=GetPostValue("element_options");
15 $form_options=GetPostValue("form_options");
16 $client_form_options=GetPostValue("client_form_options");
17
18 //$form_options=str_replace("\\\"","\"",$form_options);
19 $formParsedValues=json_decode($form_options);
20
21
22 if($formParsedValues->Name=="")
23 {
24 $message=__("Name is mandatory");
25 }else{
26
27 global $wpdb;
28 if($form_id=="0")
29 {
30 $count= $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM ".SMART_FORMS_TABLE_NAME." where form_name=%s",$formParsedValues->Name));
31
32 if($count>0)
33 {
34 $message=__("Form name already exists");
35
36 }else
37 {
38 $values=array('form_name'=>$formParsedValues->Name,
39 'element_options'=>$element_options,
40 'form_options'=>$form_options,
41 'client_form_options'=>$client_form_options
42 );
43
44 $wpdb->insert(SMART_FORMS_TABLE_NAME,$values);
45 $form_id=$wpdb->insert_id;
46 $message="saved";
47 delete_transient("rednao_smart_forms_$form_id");
48 }
49 }else
50 {
51 $wpdb->update(SMART_FORMS_TABLE_NAME,array(
52 'form_name'=>$formParsedValues->Name,
53 'element_options'=>$element_options,
54 'form_options'=>$form_options,
55 'client_form_options'=>$client_form_options
56 ),array("form_id"=>$form_id));
57 $message="saved";
58 delete_transient("rednao_smart_forms_$form_id");
59
60 }
61
62 }
63
64
65 echo "{\"FormId\":\"$form_id\",\"Message\":\"$message\"}";
66
67 die();
68 }
69
70
71 function rednao_smart_form_list()
72 {
73 if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
74 return;
75 }
76
77 global $wpdb;
78 $result=$wpdb->get_results("SELECT form_id,form_name FROM ".SMART_FORMS_TABLE_NAME);
79
80 echo "[{\"Id\":\"0\",\"Name\":\"Select a Form\"}";
81 foreach($result as $key=>$row)
82 {
83 echo ",{\"Id\":\"$row->form_id\",\"Name\":\"$row->form_name\"}";
84 }
85 echo"]";
86 die();
87 }
88
89
90 function rednao_smart_forms_save_form_values()
91 {
92 $form_id=GetPostValue("form_id");
93 $formString=GetPostValue("formString");
94
95 $entryData=json_decode($formString,true);
96
97 global $wpdb;
98 $result=$wpdb->get_results($wpdb->prepare("select form_options,element_options from ".SMART_FORMS_TABLE_NAME." where form_id=%d",$form_id));
99
100 $formOptions=null;
101 $elementOptions=null;
102
103 if(count($result)>0){
104 $formOptions=json_decode($result[0]->form_options,true);
105 $elementOptions=json_decode($result[0]->element_options,true);
106 }
107
108 if($formOptions["UsesCaptcha"]=="y")
109 {
110 if(!isset($_POST["captcha"]))
111 {
112 echo '{"message":"'.__("Invalid captcha.").'", "success":"n"}';
113 die();
114 }
115 $captchaPost=$_POST["captcha"];
116 $captcha=ARRAY();
117 $captcha["challenge"]=stripslashes($captchaPost["challenge"]);
118 $captcha["response"]=stripslashes($captchaPost["response"]);
119 $captcha["remoteip"]=$_SERVER['REMOTE_ADDR'];
120 $captcha["privatekey"]="6Lf2J-wSAAAAAOH6uSmSdx75ZLRpDIfvSeAdx9ST";
121
122 $args=Array();
123
124 $args['headers']=Array
125 (
126 'Content-Type'=>'application/x-www-form-urlencoded;',
127 'Method'=>'Post'
128 );
129 $args['body']=$captcha;
130 $res=wp_remote_post('http://www.google.com/recaptcha/api/verify',$args);
131 if(strpos($res["body"],"true")!==0)
132 {
133 echo '{"message":"'.__("Invalid captcha.").'","refreshCaptcha":"y","success":"n"}';
134 die();
135 }
136
137
138 }
139
140 $result=$wpdb->insert(SMART_FORMS_ENTRY,array(
141 'form_id'=>$form_id,
142 'date'=>date('Y-m-d H:i:s'),
143 'data'=>$formString,
144 'ip'=>$_SERVER['REMOTE_ADDR'],
145 ));
146
147 if($formOptions["SendNotificationEmail"]=="y")
148 send_form_email($formOptions["Emails"][0],$entryData,$elementOptions,false);
149
150 if($result==true)
151 echo '{"message":"'.__("Information submitted successfully.").'","success":"y"}';
152 else
153 echo '{"message":"'.__("An error occurred, please try again later.").'","success":"n"}';
154 die();
155 }
156
157 function send_form_email($formOptions,$entryData,$elementOptions,$useTestData)
158 {
159 include(SMART_FORMS_DIR.'string_renderer/rednao_string_builder.php');
160
161
162
163 $stringBuilder=new rednao_string_builder();
164 $EmailText=$formOptions["EmailText"];
165 $FromName=$formOptions["FromName"];
166 $FromEmail=$formOptions["FromEmail"];
167 $ToEmail=$formOptions["ToEmail"];
168 $EmailSubject=$formOptions["EmailSubject"];
169
170
171 if($FromName=="")
172 $FromName="Wordpress";
173
174 if($EmailSubject=="")
175 $EmailSubject="Form Submitted";
176
177 if($ToEmail=="")
178 $ToEmail=get_option("admin_email");
179
180 preg_match_all('/\\[field ([^\\]]+)/',$EmailText, $matches, PREG_PATTERN_ORDER);
181
182 foreach($matches[1] as $match)
183 {
184 $value=GetValueByField($stringBuilder,$match,$entryData,$elementOptions,$useTestData);
185 $EmailText=str_replace("[field $match]",$value,$EmailText);
186 }
187
188
189 /*
190
191
192 foreach($entryData as $key=>$value)
193 {
194
195 $element=null;
196 foreach($elementOptions as $item)
197 {
198 if($item->Id==$key)
199 {
200 $element=$item;
201 break;
202 }
203 }
204
205 $emailText.="<tr>". $stringBuilder->GetStringFromColumn($element,$value)."</tr>";
206 }
207 $emailText.="</table>";*/
208
209 $headers = 'MIME-Version: 1.0' . "\r\n";
210 $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
211 $headers.= "$FromName <$FromEmail>";
212 if(trim($ToEmail)!="")
213 {
214 return wp_mail($ToEmail, $EmailSubject, $EmailText, $headers);
215 }
216
217 return false;
218 }
219
220 function GetValueByField($stringBuilder,$match,$entryData,$elementOptions,$useTestData)
221 {
222 foreach($entryData as $key=>$value)
223 {
224 $element=null;
225 if($key!=$match)
226 continue;
227
228 $element=null;
229 foreach($elementOptions as $item)
230 {
231 if($item["Id"]==$key)
232 {
233 $element=$item;
234 break;
235 }
236 }
237 if($element==null)
238 continue;
239
240 $value= $stringBuilder->GetStringFromColumn($element,$value);
241 if($value==""&&$useTestData)
242 $value="sample text";
243 return $value;
244 }
245
246 if($useTestData)
247 return "sample text";
248 }
249
250 function rednao_smart_forms_entries_list()
251 {
252 $startDate=GetPostValue("startDate");
253 $endDate=GetPostValue("endDate");
254 $formId=GetPostValue("form_id");
255
256 $startDate=date('Y-m-d H:i:s', strtotime($startDate));
257 $endDate=date('Y-m-d H:i:s', strtotime($endDate .' +1 day'));
258
259 $query="select data from ".SMART_FORMS_ENTRY."
260 where date between '$startDate' and '$endDate' and form_id=$formId";
261
262
263 global $wpdb;
264 $result=$wpdb->get_results($query);
265 $isFirstRecord=true;
266
267 echo '{"entries":[';
268 foreach($result as $row)
269 {
270 if($isFirstRecord)
271 $isFirstRecord=false;
272 else
273 echo ",";
274
275 echo $row->data;
276
277 }
278 echo '],"formOptions":';
279
280 $query="select element_options from ".SMART_FORMS_TABLE_NAME." where form_id=".$formId;
281 $elementOptions=$wpdb->get_var($query);
282 echo $elementOptions.'}';
283
284
285 die();
286 }
287
288 function rednao_smart_form_send_test_email()
289 {
290 $FromEmail=GetPostValue("FromEmail");
291 $FromName=GetPostValue("FromName");
292 $ToEmail=GetPostValue("ToEmail");
293 $EmailSubject=GetPostValue("EmailSubject");
294 $EmailText=GetPostValue("EmailText");
295 $elementOptions=GetPostValue("element_options");
296
297 $elementOptions=json_decode($elementOptions);
298
299
300 $valueArray=Array(
301 "FromEmail"=>$FromEmail,
302 "FromName"=>$FromName,
303 "ToEmail"=>$ToEmail,
304 "EmailSubject"=>$EmailSubject,
305 "EmailText"=>$EmailText
306 );
307 $entryData=Array();
308
309
310 if($EmailText=="")
311 {
312 echo '{"Message":"'.__("Email text can't be empty").'"}';
313 die();
314 }
315
316 if(send_form_email($valueArray,$entryData,$elementOptions,true))
317 echo '{"Message":"'.__("Email sent successfully").'"}';
318 else
319 echo '{"Message":"'.__("There was an error sending the email, please check the configuration").'"}';
320 die();
321 }
322
323 function rednao_smart_forms_submit_license()
324 {
325 include_once(SMART_FORMS_DIR.'smart-forms-license.php');
326
327 $email=GetPostValue("email");
328 $key=GetPostValue("key");
329
330 if(smart_forms_check_license($email,$key,$error))
331 {
332 echo '{"IsValid":"y","Message":"'.__("License submitted successfully, thank you!!").'"}';
333 }else
334 {
335 if($error==null)
336 {
337 echo '{"IsValid":"n", "Message":"'.__("Invalid user or license").'"}';
338 }else
339 echo '{"IsValid":"n","Message":"'.__("An error occurred $error").'"}';
340 }
341
342 die();
343 }