PluginProbe ʕ •ᴥ•ʔ
Contact Form 7 / 1.9.5
Contact Form 7 v1.9.5
6.1.6 5.0.2 5.0.3 5.0.4 5.0.5 5.1 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.2 5.2.1 5.2.2 5.3 5.3.1 5.3.2 5.4 5.4.1 5.4.2 5.5 5.5.1 5.5.2 5.5.3 5.5.4 5.5.5 5.5.6 5.5.6.1 5.6 5.6.1 5.6.2 5.6.3 5.6.4 5.7 5.7.1 5.7.2 5.7.3 5.7.4 5.7.5 5.7.5.1 5.7.6 5.7.7 5.8 5.8.1 5.8.2 5.8.3 5.8.4 5.8.5 5.8.6 5.8.7 5.9 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 6.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.0.6 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 trunk 1.1 1.10 1.10.0.1 1.10.1 1.2 1.3 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.5 1.6 1.6.1 1.7 1.7.1 1.7.2 1.7.4 1.7.5 1.7.6 1.7.6.1 1.7.7 1.7.7.1 1.7.8 1.8 1.8.0.1 1.8.0.2 1.8.0.3 1.8.0.4 1.8.1 1.8.1.1 1.9 1.9.1 1.9.2 1.9.2.1 1.9.2.2 1.9.3 1.9.4 1.9.5 1.9.5.1 2.0 2.0-beta 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1 2.1.1 2.1.2 2.2 2.2.1 2.3 2.3.1 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 3.0 3.0-beta 3.0.1 3.0.2 3.0.2.1 3.1 3.1.1 3.1.2 3.2 3.2.1 3.3 3.3.1 3.3.2 3.3.3 3.4 3.4.1 3.4.2 3.5 3.5.1 3.5.2 3.5.3 3.5.4 3.6 3.7 3.7.1 3.7.2 3.8 3.8.1 3.9 3.9-beta 3.9.1 3.9.2 3.9.3 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1-beta 4.1.1 4.1.2 4.2 4.2-beta 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6 4.6.1 4.7 4.8 4.8.1 4.9 4.9.1 4.9.2 5.0 5.0.1
contact-form-7 / wp-contact-form-7.php
contact-form-7 Last commit date
admin 17 years ago images 18 years ago includes 17 years ago languages 17 years ago README.txt 17 years ago contact-form-7.js 17 years ago screenshot-1.png 17 years ago stylesheet-rtl.css 17 years ago stylesheet.css 18 years ago wp-contact-form-7.php 17 years ago
wp-contact-form-7.php
1844 lines
1 <?php
2 /*
3 Plugin Name: Contact Form 7
4 Plugin URI: http://ideasilo.wordpress.com/2007/04/30/contact-form-7/
5 Description: Just another contact form plugin. Simple but flexible.
6 Author: Takayuki Miyoshi
7 Version: 1.9.5
8 Author URI: http://ideasilo.wordpress.com/
9 */
10
11 /* Copyright 2007-2009 Takayuki Miyoshi (email: takayukister at gmail.com)
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28 define('WPCF7_VERSION', '1.9.5');
29
30 if (! defined('WP_CONTENT_DIR'))
31 define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
32 if (! defined('WP_CONTENT_URL'))
33 define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
34
35 if (! defined('WP_PLUGIN_DIR'))
36 define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins');
37 if (! defined('WP_PLUGIN_URL'))
38 define('WP_PLUGIN_URL', WP_CONTENT_URL . '/plugins');
39
40 if (! defined('WPCF7_PLUGIN_DIR'))
41 define('WPCF7_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . plugin_basename(dirname(__FILE__)));
42 if (! defined('WPCF7_PLUGIN_URL'))
43 define('WPCF7_PLUGIN_URL', WP_PLUGIN_URL . '/' . plugin_basename(dirname(__FILE__)));
44
45 if (! defined('WPCF7_AUTOP'))
46 define('WPCF7_AUTOP', true);
47
48 if (! defined('WPCF7_USE_PIPE'))
49 define('WPCF7_USE_PIPE', true);
50
51 if (! function_exists('wpcf7_version')) {
52 function wpcf7_version() { return WPCF7_VERSION; }
53 }
54
55 if (! function_exists('wpcf7_read_capability')) {
56 function wpcf7_read_capability() { return 'edit_posts'; }
57 }
58
59 if (! function_exists('wpcf7_read_write_capability')) {
60 function wpcf7_read_write_capability() { return 'publish_pages'; }
61 }
62
63 class tam_contact_form_seven {
64
65 var $contact_forms;
66 var $captcha;
67 var $posted_data;
68
69 function tam_contact_form_seven() {
70 add_action('activate_' . plugin_basename(__FILE__), array(&$this, 'set_initial'));
71 add_action('init', array(&$this, 'load_plugin_textdomain'));
72 add_action('admin_menu', array(&$this, 'add_pages'));
73 add_action('admin_head', array(&$this, 'admin_head'));
74 add_action('wp_head', array(&$this, 'wp_head'));
75 add_action('wp_print_scripts', array(&$this, 'load_js'));
76 add_action('init', array(&$this, 'init_switch'), 11);
77 add_filter('the_content', array(&$this, 'the_content_filter'), 9);
78 add_filter('widget_text', array(&$this, 'widget_text_filter'), 9);
79
80 add_shortcode('contact-form', array(&$this, 'contact_form_tag_func'));
81 }
82
83 function init_switch() {
84 if ('POST' == $_SERVER['REQUEST_METHOD'] && 1 == (int) $_POST['_wpcf7_is_ajax_call']) {
85 $this->ajax_json_echo();
86 exit();
87 } elseif (! is_admin()) {
88 $this->process_nonajax_submitting();
89 $this->cleanup_captcha_files();
90 $this->cleanup_upload_files();
91 }
92 }
93
94 function ajax_json_echo() {
95 $echo = '';
96
97 if (isset($_POST['_wpcf7'])) {
98 $id = (int) $_POST['_wpcf7'];
99 $unit_tag = $_POST['_wpcf7_unit_tag'];
100 $contact_forms = $this->contact_forms();
101 if ($cf = $contact_forms[$id]) {
102 $cf = stripslashes_deep($cf);
103 $validation = $this->validate($cf);
104
105 $handled_uploads = $this->handle_uploads($cf);
106 if (! $handled_uploads['validation']['valid'])
107 $validation['valid'] = false;
108 $validation['reason'] = array_merge($validation['reason'], $handled_uploads['validation']['reason']);
109
110 $captchas = $this->refill_captcha($cf);
111 if (! empty($captchas)) {
112 $captchas_js = array();
113 foreach ($captchas as $name => $cap) {
114 $captchas_js[] = '"' . $name . '": "' . $cap . '"';
115 }
116 $captcha = '{ ' . join(', ', $captchas_js) . ' }';
117 } else {
118 $captcha = 'null';
119 }
120
121 $quizzes = $this->refill_quiz($cf);
122 if (! empty($quizzes)) {
123 $quizzes_js = array();
124 foreach ($quizzes as $name => $q) {
125 $quizzes_js[] = '"' . $name . '": [ "' . js_escape($q[0]) . '", "' . $q[1] . '" ]';
126 }
127 $quiz = '{ ' . join(', ', $quizzes_js) . ' }';
128 } else {
129 $quiz = 'null';
130 }
131
132 if (! $validation['valid']) { // Validation error occured
133 $invalids = array();
134 foreach ($validation['reason'] as $name => $reason) {
135 $invalids[] = '{ into: "span.wpcf7-form-control-wrap.' . $name . '", message: "' . js_escape($reason) . '" }';
136 }
137 $invalids = '[' . join(', ', $invalids) . ']';
138 $echo = '{ mailSent: 0, message: "' . js_escape($this->message($cf, 'validation_error')) . '", into: "#' . $unit_tag . '", invalids: ' . $invalids . ', captcha: ' . $captcha . ', quiz: ' . $quiz . ' }';
139 } elseif (! $this->acceptance($cf)) { // Not accepted terms
140 $echo = '{ mailSent: 0, message: "' . js_escape($this->message($cf, 'accept_terms')) . '", into: "#' . $unit_tag . '", captcha: ' . $captcha . ', quiz: ' . $quiz . ' }';
141 } elseif ($this->akismet($cf)) { // Spam!
142 $echo = '{ mailSent: 0, message: "' . js_escape($this->message($cf, 'akismet_says_spam')) . '", into: "#' . $unit_tag . '", spam: 1, captcha: ' . $captcha . ', quiz: ' . $quiz . ' }';
143 } elseif ($this->mail($cf, $handled_uploads['files'])) {
144 $echo = '{ mailSent: 1, message: "' . js_escape($this->message($cf, 'mail_sent_ok')) . '", into: "#' . $unit_tag . '", captcha: ' . $captcha . ', quiz: ' . $quiz . ' }';
145 } else {
146 $echo = '{ mailSent: 0, message: "' . js_escape($this->message($cf, 'mail_sent_ng')) . '", into: "#' . $unit_tag . '", captcha: ' . $captcha . ', quiz: ' . $quiz . ' }';
147 }
148
149 // remove upload files
150 foreach ($handled_uploads['files'] as $name => $path) {
151 @unlink($path);
152 }
153 }
154 }
155
156 if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
157 @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
158 echo $echo;
159 } else {
160 @header('Content-Type: text/html; charset=' . get_option('blog_charset'));
161 echo '<textarea>' . $echo . '</textarea>';
162 }
163 }
164
165 function handle_uploads($contact_form) {
166 $files = array();
167 $valid = true;
168 $reason = array();
169
170 $this->init_uploads(); // Confirm upload dir
171 $uploads_dir = $this->upload_tmp_dir();
172
173 $fes = $this->form_elements($contact_form['form'], false);
174
175 foreach ($fes as $fe) {
176 if ('file' != $fe['type'] && 'file*' != $fe['type'])
177 continue;
178
179 $name = $fe['name'];
180 $options = (array) $fe['options'];
181
182 $file = $_FILES[$name];
183
184 if (empty($file['tmp_name']) && 'file*' == $fe['type']) {
185 $valid = false;
186 $reason[$name] = $this->message($contact_form, 'invalid_required');
187 continue;
188 }
189
190 if (! is_uploaded_file($file['tmp_name']))
191 continue;
192
193 /* File type validation */
194
195 $pattern = '';
196 if ($allowed_types_options = preg_grep('%^filetypes:%', $options)) {
197 foreach ($allowed_types_options as $allowed_types_option) {
198 if (preg_match('%^filetypes:(.+)$%', $allowed_types_option, $matches)) {
199 $file_types = explode('|', $matches[1]);
200 foreach ($file_types as $file_type) {
201 $file_type = trim($file_type, '.');
202 $file_type = str_replace(array('.', '+', '*', '?'), array('\.', '\+', '\*', '\?'), $file_type);
203 $pattern .= '|' . $file_type;
204 }
205 }
206 }
207 }
208
209 // Default file-type restriction
210 if ('' == $pattern)
211 $pattern = 'jpg|jpeg|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv';
212
213 $pattern = trim($pattern, '|');
214 $pattern = '(' . $pattern . ')';
215 $pattern = '/\.' . $pattern . '$/i';
216 if (! preg_match($pattern, $file['name'])) {
217 $valid = false;
218 $reason[$name] = $this->message($contact_form, 'upload_file_type_invalid');
219 continue;
220 }
221
222 /* File size validation */
223
224 $allowed_size = 1048576; // default size 1 MB
225 if ($allowed_size_options = preg_grep('%^limit:%', $options)) {
226 $allowed_size_option = array_shift($allowed_size_options);
227 preg_match('/^limit:([1-9][0-9]*)$/', $allowed_size_option, $matches);
228 $allowed_size = (int) $matches[1];
229 }
230
231 if ($file['size'] > $allowed_size) {
232 $valid = false;
233 $reason[$name] = $this->message($contact_form, 'upload_file_too_large');
234 continue;
235 }
236
237 $filename = wp_unique_filename($uploads_dir, $file['name']);
238
239 // If you get script file, it's a danger. Make it TXT file.
240 if (preg_match('/\.(php|pl|py|rb|cgi)\d?$/', $filename))
241 $filename .= '.txt';
242
243 $new_file = trailingslashit($uploads_dir) . $filename;
244 if (false === @move_uploaded_file($file['tmp_name'], $new_file)) {
245 $valid = false;
246 $reason[$name] = $this->message($contact_form, 'upload_failed');
247 continue;
248 }
249
250 // Make sure the uploaded file is only readable for the owner process
251 chmod($new_file, 0400);
252
253 $files[$name] = $new_file;
254 }
255
256 $validation = compact('valid', 'reason');
257
258 return compact('files', 'validation');
259 }
260
261 function mail($contact_form, $files = array()) {
262 global $wp_version;
263
264 $contact_form = $this->upgrade($contact_form);
265
266 $this->posted_data = $_POST;
267
268 if (WPCF7_USE_PIPE) {
269 $this->pipe_all_posted($contact_form);
270 }
271
272 if ($this->compose_and_send_mail($contact_form['mail'], $files)) {
273 if ($contact_form['mail_2']['active'])
274 $this->compose_and_send_mail($contact_form['mail_2'], $files);
275
276 return true;
277 }
278
279 return false;
280 }
281
282 function compose_and_send_mail($mail_template, $attachments = array()) {
283 $regex = '/\[\s*([a-zA-Z][0-9a-zA-Z:._-]*)\s*\]/';
284 $callback = array(&$this, 'mail_callback');
285 $mail_subject = preg_replace_callback($regex, $callback, $mail_template['subject']);
286 $mail_sender = preg_replace_callback($regex, $callback, $mail_template['sender']);
287 $mail_body = preg_replace_callback($regex, $callback, $mail_template['body']);
288 $mail_recipient = preg_replace_callback($regex, $callback, $mail_template['recipient']);
289 $mail_headers = "From: $mail_sender\n";
290
291 if ($mail_template['use_html'])
292 $mail_headers .= "Content-Type: text/html\n";
293
294 if ($attachments) {
295 $for_this_mail = array();
296 foreach ($attachments as $name => $path) {
297 if (false === strpos($mail_template['attachments'], "[${name}]"))
298 continue;
299 $for_this_mail[] = $path;
300 }
301 return @wp_mail($mail_recipient, $mail_subject, $mail_body, $mail_headers, $for_this_mail);
302 } else {
303 return @wp_mail($mail_recipient, $mail_subject, $mail_body, $mail_headers);
304 }
305 }
306
307 function mail_callback($matches) {
308 if (isset($this->posted_data[$matches[1]])) {
309 $submitted = $this->posted_data[$matches[1]];
310
311 if (is_array($submitted))
312 $submitted = join(', ', $submitted);
313 return stripslashes($submitted);
314 } else {
315
316 // Special [wpcf7.remote_ip] tag
317 if ('wpcf7.remote_ip' == $matches[1])
318 return preg_replace('/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR']);
319
320 return $matches[0];
321 }
322 }
323
324 function akismet($contact_form) {
325 global $akismet_api_host, $akismet_api_port;
326
327 if (! function_exists('akismet_http_post') || ! (get_option('wordpress_api_key') || $wpcom_api_key))
328 return false;
329
330 $akismet_ready = false;
331 $author = $author_email = $author_url = $content = '';
332 $fes = $this->form_elements($contact_form['form'], false);
333
334 foreach ($fes as $fe) {
335 if (! is_array($fe['options'])) continue;
336
337 if (preg_grep('%^akismet:author$%', $fe['options']) && '' == $author) {
338 $author = $_POST[$fe['name']];
339 $akismet_ready = true;
340 }
341 if (preg_grep('%^akismet:author_email$%', $fe['options']) && '' == $author_email) {
342 $author_email = $_POST[$fe['name']];
343 $akismet_ready = true;
344 }
345 if (preg_grep('%^akismet:author_url$%', $fe['options']) && '' == $author_url) {
346 $author_url = $_POST[$fe['name']];
347 $akismet_ready = true;
348 }
349
350 if ('' != $content)
351 $content .= "\n\n";
352 $content .= $_POST[$fe['name']];
353 }
354
355 if (! $akismet_ready)
356 return false;
357
358 $c['blog'] = get_option('home');
359 $c['user_ip'] = preg_replace('/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR']);
360 $c['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
361 $c['referrer'] = $_SERVER['HTTP_REFERER'];
362 $c['comment_type'] = 'contactform7';
363 if ($permalink = get_permalink())
364 $c['permalink'] = $permalink;
365 if ('' != $author)
366 $c['comment_author'] = $author;
367 if ('' != $author_email)
368 $c['comment_author_email'] = $author_email;
369 if ('' != $author_url)
370 $c['comment_author_url'] = $author_url;
371 if ('' != $content)
372 $c['comment_content'] = $content;
373
374 $ignore = array('HTTP_COOKIE');
375
376 foreach ($_SERVER as $key => $value)
377 if (! in_array($key, (array) $ignore))
378 $c["$key"] = $value;
379
380 $query_string = '';
381 foreach ($c as $key => $data)
382 $query_string .= $key . '=' . urlencode(stripslashes($data)) . '&';
383
384 $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
385 if ('true' == $response[1])
386 return true;
387 else
388 return false;
389 }
390
391 function acceptance($contact_form) {
392 $fes = $this->form_elements($contact_form['form'], false);
393
394 $accepted = true;
395
396 foreach ($fes as $fe) {
397 if ('acceptance' != $fe['type'])
398 continue;
399
400 $invert = (bool) preg_grep('%^invert$%', $fe['options']);
401
402 if ($invert && $_POST[$fe['name']] || ! $invert && ! $_POST[$fe['name']])
403 $accepted = false;
404 }
405
406 return $accepted;
407 }
408
409 function set_initial() {
410 $this->load_plugin_textdomain();
411
412 $wpcf7 = get_option('wpcf7');
413 if (! is_array($wpcf7))
414 $wpcf7 = array();
415
416 $contact_forms = $wpcf7['contact_forms'];
417 if (! is_array($contact_forms))
418 $contact_forms = array();
419
420 if (0 == count($contact_forms))
421 $contact_forms[1] = $this->default_pack(__('Contact form', 'wpcf7') . ' 1');
422
423 $wpcf7['contact_forms'] = $contact_forms;
424 update_option('wpcf7', $wpcf7);
425 }
426
427 function load_plugin_textdomain() { // l10n
428 global $wp_version;
429
430 if (version_compare($wp_version, '2.6', '<')) // Using old WordPress
431 load_plugin_textdomain('wpcf7', 'wp-content/plugins/contact-form-7/languages');
432 else
433 load_plugin_textdomain('wpcf7', 'wp-content/plugins/contact-form-7/languages', 'contact-form-7/languages');
434 }
435
436 function contact_forms() {
437 if (is_array($this->contact_forms))
438 return $this->contact_forms;
439 $wpcf7 = get_option('wpcf7');
440 $this->contact_forms = $wpcf7['contact_forms'];
441 if (! is_array($this->contact_forms))
442 $this->contact_forms = array();
443 return $this->contact_forms;
444 }
445
446 function update_contact_forms($contact_forms) {
447 $wpcf7 = get_option('wpcf7');
448 $wpcf7['contact_forms'] = $contact_forms;
449
450 update_option('wpcf7', $wpcf7);
451 }
452
453 function upgrade($contact_form) {
454 if (empty($contact_form))
455 return $contact_form;
456
457 $contact_form = $this->upgrade_160($contact_form);
458 $contact_form = $this->upgrade_181($contact_form);
459 $contact_form = $this->upgrade_190($contact_form);
460 $contact_form = $this->upgrade_192($contact_form);
461 return $contact_form;
462 }
463
464 function upgrade_160($contact_form) {
465 if (! isset($contact_form['mail']['recipient']))
466 $contact_form['mail']['recipient'] = $contact_form['options']['recipient'];
467 return $contact_form;
468 }
469
470 function upgrade_181($contact_form) {
471 if (! isset($contact_form['messages']))
472 $contact_form['messages'] = array(
473 'mail_sent_ok' => $this->default_message('mail_sent_ok'),
474 'mail_sent_ng' => $this->default_message('mail_sent_ng'),
475 'akismet_says_spam' => $this->default_message('akismet_says_spam'),
476 'validation_error' => $this->default_message('validation_error'),
477 'accept_terms' => $this->default_message('accept_terms'),
478 'invalid_email' => $this->default_message('invalid_email'),
479 'invalid_required' => $this->default_message('invalid_required'),
480 'captcha_not_match' => $this->default_message('captcha_not_match')
481 );
482 return $contact_form;
483 }
484
485 function upgrade_190($contact_form) {
486 if (! isset($contact_form['messages']) || ! is_array($contact_form['messages']))
487 $contact_form['messages'] = array();
488
489 if (! isset($contact_form['messages']['upload_failed']))
490 $contact_form['messages']['upload_failed'] = $this->default_message('upload_failed');
491
492 if (! isset($contact_form['messages']['upload_file_type_invalid']))
493 $contact_form['messages']['upload_file_type_invalid'] = $this->default_message('upload_file_type_invalid');
494
495 if (! isset($contact_form['messages']['upload_file_too_large']))
496 $contact_form['messages']['upload_file_too_large'] = $this->default_message('upload_file_too_large');
497
498 return $contact_form;
499 }
500
501 function upgrade_192($contact_form) {
502 if (! isset($contact_form['messages']) || ! is_array($contact_form['messages']))
503 $contact_form['messages'] = array();
504
505 if (! isset($contact_form['messages']['quiz_answer_not_correct']))
506 $contact_form['messages']['quiz_answer_not_correct'] = $this->default_message('quiz_answer_not_correct');
507
508 return $contact_form;
509 }
510
511 /* Admin panel */
512
513 function admin_menu_parent() {
514 global $wp_version;
515 if (version_compare($wp_version, '2.7', '>='))
516 return 'tools.php';
517 else
518 return 'edit.php';
519 }
520
521 function add_pages() {
522 if (function_exists('admin_url')) {
523 $base_url = admin_url($this->admin_menu_parent());
524 } else {
525 $base_url = get_option('siteurl') . '/wp-admin/' . $this->admin_menu_parent();
526 }
527 $page = str_replace('\\', '%5C', plugin_basename(__FILE__));
528 $contact_forms = $this->contact_forms();
529
530 if (isset($_POST['wpcf7-save']) && $this->has_edit_cap()) {
531 $id = $_POST['wpcf7-id'];
532 check_admin_referer('wpcf7-save_' . $id);
533
534 $title = trim($_POST['wpcf7-title']);
535 $form = trim($_POST['wpcf7-form']);
536 $mail = array(
537 'subject' => trim($_POST['wpcf7-mail-subject']),
538 'sender' => trim($_POST['wpcf7-mail-sender']),
539 'body' => trim($_POST['wpcf7-mail-body']),
540 'recipient' => trim($_POST['wpcf7-mail-recipient']),
541 'attachments' => trim($_POST['wpcf7-mail-attachments']),
542 'use_html' => (1 == $_POST['wpcf7-mail-use-html']) ? true : false
543 );
544 $mail_2 = array(
545 'active' => (1 == $_POST['wpcf7-mail-2-active']) ? true : false,
546 'subject' => trim($_POST['wpcf7-mail-2-subject']),
547 'sender' => trim($_POST['wpcf7-mail-2-sender']),
548 'body' => trim($_POST['wpcf7-mail-2-body']),
549 'recipient' => trim($_POST['wpcf7-mail-2-recipient']),
550 'attachments' => trim($_POST['wpcf7-mail-2-attachments']),
551 'use_html' => (1 == $_POST['wpcf7-mail-2-use-html']) ? true : false
552 );
553 $messages = array(
554 'mail_sent_ok' => trim($_POST['wpcf7-message-mail-sent-ok']),
555 'mail_sent_ng' => trim($_POST['wpcf7-message-mail-sent-ng']),
556 'akismet_says_spam' => trim($_POST['wpcf7-message-akismet-says-spam']),
557 'validation_error' => trim($_POST['wpcf7-message-validation-error']),
558 'accept_terms' => trim($_POST['wpcf7-message-accept-terms']),
559 'invalid_email' => trim($_POST['wpcf7-message-invalid-email']),
560 'invalid_required' => trim($_POST['wpcf7-message-invalid-required']),
561 'quiz_answer_not_correct' => trim($_POST['wpcf7-message-quiz-answer-not-correct']),
562 'captcha_not_match' => trim($_POST['wpcf7-message-captcha-not-match']),
563 'upload_failed' => trim($_POST['wpcf7-message-upload-failed']),
564 'upload_file_type_invalid' => trim($_POST['wpcf7-message-upload-file-type-invalid']),
565 'upload_file_too_large' => trim($_POST['wpcf7-message-upload-file-too-large'])
566 );
567 $options = array(
568 'recipient' => trim($_POST['wpcf7-options-recipient']) // For backward compatibility.
569 );
570
571 if (array_key_exists($id, $contact_forms)) {
572 $contact_forms[$id] = compact('title', 'form', 'mail', 'mail_2', 'messages', 'options');
573 $redirect_to = $base_url . '?page=' . $page . '&contactform=' . $id . '&message=saved';
574 } else {
575 $key = (empty($contact_forms)) ? 1 : max(array_keys($contact_forms)) + 1;
576 $contact_forms[$key] = compact('title', 'form', 'mail', 'mail_2', 'messages', 'options');
577 $redirect_to = $base_url . '?page=' . $page . '&contactform=' . $key . '&message=created';
578 }
579 $this->update_contact_forms($contact_forms);
580
581 wp_redirect($redirect_to);
582 exit();
583 } elseif (isset($_POST['wpcf7-copy']) && $this->has_edit_cap()) {
584 $id = $_POST['wpcf7-id'];
585 check_admin_referer('wpcf7-copy_' . $id);
586
587 if (array_key_exists($id, $contact_forms)) {
588 $key = max(array_keys($contact_forms)) + 1;
589 $contact_forms[$key] = $contact_forms[$id];
590 $contact_forms[$key]['title'] .= '_copy';
591 $this->update_contact_forms($contact_forms);
592 $redirect_to = $base_url . '?page=' . $page . '&contactform=' . $key . '&message=created';
593 } else {
594 $redirect_to = $base_url . '?page=' . $page . '&contactform=' . $id;
595 }
596
597 wp_redirect($redirect_to);
598 exit();
599 } elseif (isset($_POST['wpcf7-delete']) && $this->has_edit_cap()) {
600 $id = $_POST['wpcf7-id'];
601 check_admin_referer('wpcf7-delete_' . $id);
602
603 unset($contact_forms[$id]);
604 $this->update_contact_forms($contact_forms);
605
606 wp_redirect($base_url . '?page=' . $page . '&message=deleted');
607 exit();
608 }
609
610 add_management_page(__('Contact Form 7', 'wpcf7'), __('Contact Form 7', 'wpcf7'), wpcf7_read_capability(), __FILE__, array(&$this, 'management_page'));
611 }
612
613 function admin_head() {
614 global $plugin_page, $wp_version;
615
616 if (isset($plugin_page) && $plugin_page == plugin_basename(__FILE__)) {
617
618 $admin_stylesheet_url = WPCF7_PLUGIN_URL . '/admin/admin-stylesheet.css';
619 $javascript_url = WPCF7_PLUGIN_URL . '/admin/wpcf7-admin.js';
620
621 echo '<link rel="stylesheet" href="' . $admin_stylesheet_url . '" type="text/css" />';
622
623 if ('rtl' == get_bloginfo('text_direction')) {
624 $admin_stylesheet_rtl_url = WPCF7_PLUGIN_URL . '/admin/admin-stylesheet-rtl.css';
625 echo '<link rel="stylesheet" href="' . $admin_stylesheet_rtl_url . '" type="text/css" />';
626 }
627
628 ?>
629 <script type="text/javascript">
630 //<![CDATA[
631 var _wpcf7 = {
632 l10n: {
633 optional: "<?php echo js_escape(__('optional', 'wpcf7')); ?>",
634 generateTag: "<?php echo js_escape(__('Generate Tag', 'wpcf7')); ?>",
635 textField: "<?php echo js_escape(__('Text field', 'wpcf7')); ?>",
636 emailField: "<?php echo js_escape(__('Email field', 'wpcf7')); ?>",
637 textArea: "<?php echo js_escape(__('Text area', 'wpcf7')); ?>",
638 menu: "<?php echo js_escape(__('Drop-down menu', 'wpcf7')); ?>",
639 checkboxes: "<?php echo js_escape(__('Checkboxes', 'wpcf7')); ?>",
640 radioButtons: "<?php echo js_escape(__('Radio buttons', 'wpcf7')); ?>",
641 acceptance: "<?php echo js_escape(__('Acceptance', 'wpcf7')); ?>",
642 isAcceptanceDefaultOn: "<?php echo js_escape(__("Make this checkbox checked by default?", 'wpcf7')); ?>",
643 isAcceptanceInvert: "<?php echo js_escape(__("Make this checkbox work inversely?", 'wpcf7')); ?>",
644 isAcceptanceInvertMeans: "<?php echo js_escape(__("* That means visitor who accepts the term unchecks it.", 'wpcf7')); ?>",
645 captcha: "<?php echo js_escape(__('CAPTCHA', 'wpcf7')); ?>",
646 quiz: "<?php echo js_escape(__('Quiz', 'wpcf7')); ?>",
647 quizzes: "<?php echo js_escape(__('Quizzes', 'wpcf7')); ?>",
648 quizFormatDesc: "<?php echo js_escape(__("* quiz|answer (e.g. 1+1=?|2)", 'wpcf7')); ?>",
649 fileUpload: "<?php echo js_escape(__('File upload', 'wpcf7')); ?>",
650 bytes: "<?php echo js_escape(__('bytes', 'wpcf7')); ?>",
651 submit: "<?php echo js_escape(__('Submit button', 'wpcf7')); ?>",
652 tagName: "<?php echo js_escape(__('Name', 'wpcf7')); ?>",
653 isRequiredField: "<?php echo js_escape(__('Required field?', 'wpcf7')); ?>",
654 allowsMultipleSelections: "<?php echo js_escape(__('Allow multiple selections?', 'wpcf7')); ?>",
655 insertFirstBlankOption: "<?php echo js_escape(__('Insert a blank item as the first option?', 'wpcf7')); ?>",
656 makeCheckboxesExclusive: "<?php echo js_escape(__('Make checkboxes exclusive?', 'wpcf7')); ?>",
657 menuChoices: "<?php echo js_escape(__('Choices', 'wpcf7')); ?>",
658 label: "<?php echo js_escape(__('Label', 'wpcf7')); ?>",
659 defaultValue: "<?php echo js_escape(__('Default value', 'wpcf7')); ?>",
660 akismet: "<?php echo js_escape(__('Akismet', 'wpcf7')); ?>",
661 akismetAuthor: "<?php echo js_escape(__("This field requires author's name", 'wpcf7')); ?>",
662 akismetAuthorUrl: "<?php echo js_escape(__("This field requires author's URL", 'wpcf7')); ?>",
663 akismetAuthorEmail: "<?php echo js_escape(__("This field requires author's email address", 'wpcf7')); ?>",
664 generatedTag: "<?php echo js_escape(__("Copy this code and paste it into the form left.", 'wpcf7')); ?>",
665 fgColor: "<?php echo js_escape(__("Foreground color", 'wpcf7')); ?>",
666 bgColor: "<?php echo js_escape(__("Background color", 'wpcf7')); ?>",
667 imageSize: "<?php echo js_escape(__("Image size", 'wpcf7')); ?>",
668 imageSizeSmall: "<?php echo js_escape(__("Small", 'wpcf7')); ?>",
669 imageSizeMedium: "<?php echo js_escape(__("Medium", 'wpcf7')); ?>",
670 imageSizeLarge: "<?php echo js_escape(__("Large", 'wpcf7')); ?>",
671 imageSettings: "<?php echo js_escape(__("Image settings", 'wpcf7')); ?>",
672 inputFieldSettings: "<?php echo js_escape(__("Input field settings", 'wpcf7')); ?>",
673 tagForImage: "<?php echo js_escape(__("For image", 'wpcf7')); ?>",
674 tagForInputField: "<?php echo js_escape(__("For input field", 'wpcf7')); ?>",
675 oneChoicePerLine: "<?php echo js_escape(__("* One choice per line.", 'wpcf7')); ?>",
676 show: "<?php echo js_escape(__("Show", 'wpcf7')); ?>",
677 hide: "<?php echo js_escape(__("Hide", 'wpcf7')); ?>",
678 fileSizeLimit: "<?php echo js_escape(__("File size limit", 'wpcf7')); ?>",
679 acceptableFileTypes: "<?php echo js_escape(__("Acceptable file types", 'wpcf7')); ?>",
680 needReallySimpleCaptcha: "<?php echo js_escape(__("Note: To use CAPTCHA, you need Really Simple CAPTCHA plugin installed.", 'wpcf7')); ?>"
681 },
682 captchaMod: <?php echo (class_exists('ReallySimpleCaptcha')) ? 'true' : 'false' ?>
683 };
684 //]]>
685 </script>
686 <script type='text/javascript' src='<?php echo $javascript_url; ?>'></script>
687 <?php
688 }
689 }
690
691 function has_edit_cap() {
692 return current_user_can(wpcf7_read_write_capability());
693 }
694
695 function management_page() {
696 global $wp_version;
697
698 if (function_exists('admin_url')) {
699 $base_url = admin_url($this->admin_menu_parent());
700 } else {
701 $base_url = get_option('siteurl') . '/wp-admin/' . $this->admin_menu_parent();
702 }
703 $page = plugin_basename(__FILE__);
704
705 switch ($_GET['message']) {
706 case 'created':
707 $updated_message = __('Contact form created.', 'wpcf7');
708 break;
709 case 'saved':
710 $updated_message = __('Contact form saved.', 'wpcf7');
711 break;
712 case 'deleted':
713 $updated_message = __('Contact form deleted.', 'wpcf7');
714 break;
715 }
716
717 $contact_forms = $this->contact_forms();
718
719 $id = $_POST['wpcf7-id'];
720
721 if ('new' == $_GET['contactform']) {
722 $unsaved = true;
723 $current = -1;
724 $cf = $this->default_pack(__('Untitled', 'wpcf7'), true);
725 } elseif (array_key_exists($_GET['contactform'], $contact_forms)) {
726 $current = (int) $_GET['contactform'];
727 $cf = stripslashes_deep($contact_forms[$current]);
728 $cf = $this->upgrade($cf);
729 } else {
730 $current = (int) array_shift(array_keys($contact_forms));
731 $cf = stripslashes_deep($contact_forms[$current]);
732 $cf = $this->upgrade($cf);
733 }
734
735 require_once WPCF7_PLUGIN_DIR . '/admin/admin-panel.php';
736 }
737
738 function default_pack($title, $initial = false) {
739 $cf = array('title' => $title,
740 'form' => $this->default_form_template(),
741 'mail' => $this->default_mail_template(),
742 'mail_2' => $this->default_mail_2_template(),
743 'messages' => $this->default_messages_template(),
744 'options' => $this->default_options_template());
745 if ($initial)
746 $cf['initial'] = true;
747 return $cf;
748 }
749
750 function default_form_template() {
751 $template .= '<p>' . __('Your Name', 'wpcf7') . ' ' . __('(required)', 'wpcf7') . '<br />' . "\n";
752 $template .= ' [text* your-name] </p>' . "\n\n";
753 $template .= '<p>' . __('Your Email', 'wpcf7') . ' ' . __('(required)', 'wpcf7') . '<br />' . "\n";
754 $template .= ' [email* your-email] </p>' . "\n\n";
755 $template .= '<p>' . __('Subject', 'wpcf7') . '<br />' . "\n";
756 $template .= ' [text your-subject] </p>' . "\n\n";
757 $template .= '<p>' . __('Your Message', 'wpcf7') . '<br />' . "\n";
758 $template .= ' [textarea your-message] </p>' . "\n\n";
759 $template .= '<p>[submit "' . __('Send', 'wpcf7') . '"]</p>';
760 return $template;
761 }
762
763 function default_mail_template() {
764 $subject = '[your-subject]';
765 $sender = '[your-name] <[your-email]>';
766 $body = '[your-message]';
767 $recipient = get_option('admin_email');
768 return compact('subject', 'sender', 'body', 'recipient');
769 }
770
771 function default_mail_2_template() {
772 $active = false;
773 $subject = '[your-subject]';
774 $sender = '[your-name] <[your-email]>';
775 $body = '[your-message]';
776 $recipient = '[your-email]';
777 return compact('active', 'subject', 'sender', 'body', 'recipient');
778 }
779
780 function default_messages_template() {
781 $mail_sent_ok = $this->default_message('mail_sent_ok');
782 $mail_sent_ng = $this->default_message('mail_sent_ng');
783 $akismet_says_spam = $this->default_message('akismet_says_spam');
784 $validation_error = $this->default_message('validation_error');
785 $accept_terms = $this->default_message('accept_terms');
786 $invalid_email = $this->default_message('invalid_email');
787 $invalid_required = $this->default_message('invalid_required');
788 $quiz_answer_not_correct = $this->default_message('quiz_answer_not_correct');
789 $captcha_not_match = $this->default_message('captcha_not_match');
790 $upload_failed = $this->default_message('upload_failed');
791 $upload_file_type_invalid = $this->default_message('upload_file_type_invalid');
792 $upload_file_too_large = $this->default_message('upload_file_too_large');
793 return compact('mail_sent_ok', 'mail_sent_ng', 'akismet_says_spam',
794 'validation_error', 'accept_terms', 'invalid_email', 'invalid_required', 'quiz_answer_not_correct',
795 'captcha_not_match', 'upload_failed', 'upload_file_type_invalid', 'upload_file_too_large');
796 }
797
798 function default_options_template() {
799 $recipient = get_option('admin_email'); // For backward compatibility.
800 return compact('recipient');
801 }
802
803 function message($contact_form, $status) {
804 if (! isset($contact_form['messages']) || ! isset($contact_form['messages'][$status]))
805 return $this->default_message($status);
806
807 return $contact_form['messages'][$status];
808 }
809
810 function default_message($status) {
811 switch ($status) {
812 case 'mail_sent_ok':
813 return __('Your message was sent successfully. Thanks.', 'wpcf7');
814 case 'mail_sent_ng':
815 return __('Failed to send your message. Please try later or contact administrator by other way.', 'wpcf7');
816 case 'akismet_says_spam':
817 return __('Failed to send your message. Please try later or contact administrator by other way.', 'wpcf7');
818 case 'validation_error':
819 return __('Validation errors occurred. Please confirm the fields and submit it again.', 'wpcf7');
820 case 'accept_terms':
821 return __('Please accept the terms to proceed.', 'wpcf7');
822 case 'invalid_email':
823 return __('Email address seems invalid.', 'wpcf7');
824 case 'invalid_required':
825 return __('Please fill the required field.', 'wpcf7');
826 case 'captcha_not_match':
827 return __('Your entered code is incorrect.', 'wpcf7');
828 case 'quiz_answer_not_correct':
829 return __('Your answer is not correct.', 'wpcf7');
830 case 'upload_failed':
831 return __('Failed to upload file.', 'wpcf7');
832 case 'upload_file_type_invalid':
833 return __('This file type is not allowed.', 'wpcf7');
834 case 'upload_file_too_large':
835 return __('This file is too large.', 'wpcf7');
836 }
837 }
838
839 function process_nonajax_submitting() {
840 if (! isset($_POST['_wpcf7']))
841 return;
842
843 $id = (int) $_POST['_wpcf7'];
844 $contact_forms = $this->contact_forms();
845 if ($cf = $contact_forms[$id]) {
846 $cf = stripslashes_deep($cf);
847 $validation = $this->validate($cf);
848
849 $handled_uploads = $this->handle_uploads($cf);
850 if (! $handled_uploads['validation']['valid'])
851 $validation['valid'] = false;
852 $validation['reason'] = array_merge($validation['reason'], $handled_uploads['validation']['reason']);
853
854 if (! $validation['valid']) {
855 $_POST['_wpcf7_validation_errors'] = array('id' => $id, 'messages' => $validation['reason']);
856 } elseif (! $this->acceptance($cf)) { // Not accepted terms
857 $_POST['_wpcf7_mail_sent'] = array('id' => $id, 'ok' => false, 'message' => $this->message($cf, 'accept_terms'));
858 } elseif ($this->akismet($cf)) { // Spam!
859 $_POST['_wpcf7_mail_sent'] = array('id' => $id, 'ok' => false, 'message' => $this->message($cf, 'akismet_says_spam'), 'spam' => true);
860 } elseif ($this->mail($cf, $handled_uploads['files'])) {
861 $_POST['_wpcf7_mail_sent'] = array('id' => $id, 'ok' => true, 'message' => $this->message($cf, 'mail_sent_ok'));
862 } else {
863 $_POST['_wpcf7_mail_sent'] = array('id' => $id, 'ok' => false, 'message' => $this->message($cf, 'mail_sent_ng'));
864 }
865
866 // remove upload files
867 foreach ($handled_uploads['files'] as $name => $path) {
868 @unlink($path);
869 }
870 }
871 }
872
873 /* Post content filtering */
874
875 var $processing_unit_tag;
876 var $processing_within;
877 var $unit_count;
878 var $widget_count;
879
880 function the_content_filter($content) {
881 $this->processing_within = 'p' . get_the_ID();
882 $this->unit_count = 0;
883
884 return $content;
885 }
886
887 function widget_text_filter($content) {
888 $this->widget_count += 1;
889 $this->processing_within = 'w' . $this->widget_count;
890 $this->unit_count = 0;
891
892 $regex = '/\[\s*contact-form\s+(\d+(?:\s+.*)?)\]/';
893 return preg_replace_callback($regex, array(&$this, 'widget_text_filter_callback'), $content);
894 }
895
896 function widget_text_filter_callback($matches) {
897 return $this->contact_form_tag_func($matches[1]);
898 }
899
900 function contact_form_tag_func($atts) {
901 if (is_string($atts))
902 $atts = explode(' ', $atts, 2);
903
904 $atts = (array) $atts;
905
906 $id = (int) array_shift($atts);
907
908 $contact_forms = $this->contact_forms();
909
910 if (! ($cf = $contact_forms[$id]))
911 return '[contact-form 404 "Not Found"]';
912
913 $cf = stripslashes_deep($cf);
914
915 $this->unit_count += 1;
916 $unit_tag = 'wpcf7-f' . $id . '-' . $this->processing_within . '-o' . $this->unit_count;
917 $this->processing_unit_tag = $unit_tag;
918
919 $form = '<div class="wpcf7" id="' . $unit_tag . '">';
920
921 $url = parse_url($_SERVER['REQUEST_URI']);
922 $url = $url['path'] . (empty($url['query']) ? '' : '?' . $url['query']) . '#' . $unit_tag;
923
924 $form_elements = $this->form_elements($cf['form'], false);
925 $multipart = false;
926 foreach ($form_elements as $form_element) {
927 if (preg_match('/^file[*]?$/', $form_element['type'])) {
928 $multipart = true;
929 break;
930 }
931 }
932 $enctype = $multipart ? ' enctype="multipart/form-data"' : '';
933
934 $form .= '<form action="' . $url . '" method="post" class="wpcf7-form"' . $enctype . '>';
935 $form .= '<div style="display: none;">';
936 $form .= '<input type="hidden" name="_wpcf7" value="' . $id . '" />';
937 $form .= '<input type="hidden" name="_wpcf7_version" value="' . wpcf7_version() . '" />';
938 $form .= '<input type="hidden" name="_wpcf7_unit_tag" value="' . $unit_tag . '" />';
939 $form .= '</div>';
940 $form .= $this->form_elements($cf['form']);
941 $form .= '</form>';
942
943 // Post response output for non-AJAX
944 $class = 'wpcf7-response-output';
945
946 if ($this->processing_unit_tag == $_POST['_wpcf7_unit_tag']) {
947 if (isset($_POST['_wpcf7_mail_sent']) && $_POST['_wpcf7_mail_sent']['id'] == $id) {
948 if ($_POST['_wpcf7_mail_sent']['ok']) {
949 $class .= ' wpcf7-mail-sent-ok';
950 $content = $_POST['_wpcf7_mail_sent']['message'];
951 } else {
952 $class .= ' wpcf7-mail-sent-ng';
953 if ($_POST['_wpcf7_mail_sent']['spam'])
954 $class .= ' wpcf7-spam-blocked';
955 $content = $_POST['_wpcf7_mail_sent']['message'];
956 }
957 } elseif (isset($_POST['_wpcf7_validation_errors']) && $_POST['_wpcf7_validation_errors']['id'] == $id) {
958 $class .= ' wpcf7-validation-errors';
959 $content = $this->message($cf, 'validation_error');
960 }
961 }
962
963 $class = ' class="' . $class . '"';
964
965 $form .= '<div' . $class . '>' . $content . '</div>';
966
967 $form .= '</div>';
968
969 $this->processing_unit_tag = null;
970
971 if (WPCF7_AUTOP)
972 $form = $this->wpautop_substitute($form);
973
974 return $form;
975 }
976
977 function wpautop_substitute($pee, $br = 1) {
978 $pee = $pee . "\n"; // just to make things a little easier, pad the end
979 $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
980 // Space things out a little
981 $allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr)';
982 $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
983 $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
984 $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
985 if ( strpos($pee, '<object') !== false ) {
986 $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
987 $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
988 }
989 $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
990 // make paragraphs, including one at the end
991 $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
992 $pee = '';
993 foreach ( $pees as $tinkle )
994 $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
995 $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
996 $pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
997 $pee = preg_replace( '|<p>|', "$1<p>", $pee );
998 $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
999 $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
1000 $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
1001 $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
1002 $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
1003 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
1004 if ($br) {
1005 $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee);
1006 $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
1007 $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
1008 }
1009 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
1010 $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
1011 if (strpos($pee, '<pre') !== false)
1012 $pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', 'clean_pre', $pee );
1013 $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
1014
1015 if (function_exists('get_shortcode_regex'))
1016 $pee = preg_replace('/<p>\s*?(' . get_shortcode_regex() . ')\s*<\/p>/s', '$1', $pee); // don't auto-p wrap shortcodes that stand alone
1017
1018 return $pee;
1019 }
1020
1021 function validate($contact_form) {
1022 $fes = $this->form_elements($contact_form['form'], false);
1023 $valid = true;
1024 $reason = array();
1025
1026 foreach ($fes as $fe) {
1027 $type = $fe['type'];
1028 $name = $fe['name'];
1029 $values = $fe['values'];
1030 $raw_values = $fe['raw_values'];
1031
1032 // Before validation corrections
1033 if (preg_match('/^(?:text|email|captchar|textarea)[*]?$/', $type))
1034 $_POST[$name] = (string) $_POST[$name];
1035
1036 if (preg_match('/^(?:text|email)[*]?$/', $type))
1037 $_POST[$name] = trim(strtr($_POST[$name], "\n", " "));
1038
1039 if (preg_match('/^(?:select|checkbox|radio)[*]?$/', $type)) {
1040 if (is_array($_POST[$name])) {
1041 foreach ($_POST[$name] as $key => $value) {
1042 $value = stripslashes($value);
1043 if (! in_array($value, (array) $values)) // Not in given choices.
1044 unset($_POST[$name][$key]);
1045 }
1046 } else {
1047 $value = stripslashes($_POST[$name]);
1048 if (! in_array($value, (array) $values)) // Not in given choices.
1049 $_POST[$name] = '';
1050 }
1051 }
1052
1053 if ('acceptance' == $type)
1054 $_POST[$name] = $_POST[$name] ? 1 : 0;
1055
1056 // Required item (*)
1057 if (preg_match('/^(?:text|textarea)[*]$/', $type)) {
1058 if (! isset($_POST[$name]) || '' == $_POST[$name]) {
1059 $valid = false;
1060 $reason[$name] = $this->message($contact_form, 'invalid_required');
1061 }
1062 }
1063
1064 if ('checkbox*' == $type) {
1065 if (empty($_POST[$name])) {
1066 $valid = false;
1067 $reason[$name] = $this->message($contact_form, 'invalid_required');
1068 }
1069 }
1070
1071 if ('select*' == $type) {
1072 if (empty($_POST[$name]) ||
1073 ! is_array($_POST[$name]) && '---' == $_POST[$name] ||
1074 is_array($_POST[$name]) && 1 == count($_POST[$name]) && '---' == $_POST[$name][0]) {
1075 $valid = false;
1076 $reason[$name] = $this->message($contact_form, 'invalid_required');
1077 }
1078 }
1079
1080 if (preg_match('/^email[*]?$/', $type)) {
1081 if ('*' == substr($type, -1) && (! isset($_POST[$name]) || '' == $_POST[$name])) {
1082 $valid = false;
1083 $reason[$name] = $this->message($contact_form, 'invalid_required');
1084 } elseif (isset($_POST[$name]) && '' != $_POST[$name] && ! is_email($_POST[$name])) {
1085 $valid = false;
1086 $reason[$name] = $this->message($contact_form, 'invalid_email');
1087 }
1088 }
1089
1090 if (preg_match('/^captchar$/', $type)) {
1091 $captchac = '_wpcf7_captcha_challenge_' . $name;
1092 if (! $this->check_captcha($_POST[$captchac], $_POST[$name])) {
1093 $valid = false;
1094 $reason[$name] = $this->message($contact_form, 'captcha_not_match');
1095 }
1096 $this->remove_captcha($_POST[$captchac]);
1097 }
1098
1099 if ('quiz' == $type) {
1100 $answer = $this->canonicalize($_POST[$name]);
1101 $answer_hash = wp_hash($answer, 'wpcf7_quiz');
1102 $expected_hash = $_POST['_wpcf7_quiz_answer_' . $name];
1103 if ($answer_hash != $expected_hash) {
1104 $valid = false;
1105 $reason[$name] = $this->message($contact_form, 'quiz_answer_not_correct');
1106 }
1107 }
1108 }
1109 return compact('valid', 'reason');
1110 }
1111
1112 function refill_captcha($contact_form) {
1113 $fes = $this->form_elements($contact_form['form'], false);
1114 $refill = array();
1115
1116 foreach ($fes as $fe) {
1117 $type = $fe['type'];
1118 $name = $fe['name'];
1119 $options = $fe['options'];
1120 if ('captchac' == $type) {
1121 $op = $this->captchac_options($options);
1122 if ($filename = $this->generate_captcha($op)) {
1123 $captcha_url = trailingslashit($this->captcha_tmp_url()) . $filename;
1124 $refill[$name] = $captcha_url;
1125 }
1126 }
1127 }
1128 return $refill;
1129 }
1130
1131 function refill_quiz($contact_form) {
1132 $fes = $this->form_elements($contact_form['form'], false);
1133 $refill = array();
1134
1135 foreach ($fes as $fe) {
1136 $type = $fe['type'];
1137 $name = $fe['name'];
1138 $values = $fe['values'];
1139 $raw_values = $fe['raw_values'];
1140
1141 if ('quiz' != $type)
1142 continue;
1143
1144 if (count($values) == 0)
1145 continue;
1146
1147 if (count($values) == 1)
1148 $question = $values[0];
1149 else
1150 $question = $values[array_rand($values)];
1151
1152 $pipes = $this->get_pipes($raw_values);
1153 $answer = $this->pipe($pipes, $question);
1154 $answer = $this->canonicalize($answer);
1155
1156 $refill[$name] = array($question, wp_hash($answer, 'wpcf7_quiz'));
1157 }
1158
1159 return $refill;
1160 }
1161
1162 function wp_head() {
1163 $stylesheet_url = WPCF7_PLUGIN_URL . '/stylesheet.css';
1164 echo '<link rel="stylesheet" href="' . $stylesheet_url . '" type="text/css" />';
1165
1166 if ('rtl' == get_bloginfo('text_direction')) {
1167 $stylesheet_rtl_url = WPCF7_PLUGIN_URL . '/stylesheet-rtl.css';
1168 echo '<link rel="stylesheet" href="' . $stylesheet_rtl_url . '" type="text/css" />';
1169 }
1170
1171 $javascript_url = WPCF7_PLUGIN_URL . '/contact-form-7.js';
1172 ?>
1173 <script type='text/javascript' src='<?php echo $javascript_url; ?>'></script>
1174 <?php
1175 }
1176
1177 function load_js() {
1178 global $pagenow;
1179 if (is_admin() && $this->admin_menu_parent() == $pagenow && false !== strpos($_GET['page'], 'contact-form-7'))
1180 wp_enqueue_script('jquery');
1181 if (! is_admin())
1182 wp_enqueue_script('jquery-form');
1183 }
1184
1185 /* Processing form element placeholders */
1186
1187 function form_elements($form, $replace = true) {
1188 $types = 'text[*]?|email[*]?|textarea[*]?|select[*]?|checkbox[*]?|radio|acceptance|captchac|captchar|file[*]?|quiz';
1189 $regex = '%\[\s*(' . $types . ')(\s+[a-zA-Z][0-9a-zA-Z:._-]*)([-0-9a-zA-Z:#_/|\s]*)?((?:\s*(?:"[^"]*"|\'[^\']*\'))*)?\s*\]%';
1190 $submit_regex = '%\[\s*submit(\s[-0-9a-zA-Z:#_/\s]*)?(\s+(?:"[^"]*"|\'[^\']*\'))?\s*\]%';
1191 if ($replace) {
1192 $form = preg_replace_callback($regex, array(&$this, 'form_element_replace_callback'), $form);
1193 // Submit button
1194 $form = preg_replace_callback($submit_regex, array(&$this, 'submit_replace_callback'), $form);
1195 return $form;
1196 } else {
1197 $results = array();
1198 preg_match_all($regex, $form, $matches, PREG_SET_ORDER);
1199 foreach ($matches as $match) {
1200 $results[] = (array) $this->form_element_parse($match);
1201 }
1202 return $results;
1203 }
1204 }
1205
1206 function form_element_replace_callback($matches) {
1207 extract((array) $this->form_element_parse($matches)); // $type, $name, $options, $values, $raw_values
1208
1209 if ($this->processing_unit_tag == $_POST['_wpcf7_unit_tag']) {
1210 $validation_error = $_POST['_wpcf7_validation_errors']['messages'][$name];
1211 $validation_error = $validation_error ? '<span class="wpcf7-not-valid-tip-no-ajax">' . $validation_error . '</span>' : '';
1212 } else {
1213 $validation_error = '';
1214 }
1215
1216 $atts = '';
1217 $options = (array) $options;
1218
1219 $id_array = preg_grep('%^id:[-0-9a-zA-Z_]+$%', $options);
1220 if ($id = array_shift($id_array)) {
1221 preg_match('%^id:([-0-9a-zA-Z_]+)$%', $id, $id_matches);
1222 if ($id = $id_matches[1])
1223 $atts .= ' id="' . $id . '"';
1224 }
1225
1226 $class_att = "";
1227 $class_array = preg_grep('%^class:[-0-9a-zA-Z_]+$%', $options);
1228 foreach ($class_array as $class) {
1229 preg_match('%^class:([-0-9a-zA-Z_]+)$%', $class, $class_matches);
1230 if ($class = $class_matches[1])
1231 $class_att .= ' ' . $class;
1232 }
1233
1234 if (preg_match('/^email[*]?$/', $type))
1235 $class_att .= ' wpcf7-validates-as-email';
1236 if (preg_match('/[*]$/', $type))
1237 $class_att .= ' wpcf7-validates-as-required';
1238
1239 if (preg_match('/^checkbox[*]?$/', $type))
1240 $class_att .= ' wpcf7-checkbox';
1241
1242 if ('radio' == $type)
1243 $class_att .= ' wpcf7-radio';
1244
1245 if (preg_match('/^captchac$/', $type))
1246 $class_att .= ' wpcf7-captcha-' . $name;
1247
1248 if ('acceptance' == $type) {
1249 $class_att .= ' wpcf7-acceptance';
1250 if (preg_grep('%^invert$%', $options))
1251 $class_att .= ' wpcf7-invert';
1252 }
1253
1254 if ($class_att)
1255 $atts .= ' class="' . trim($class_att) . '"';
1256
1257 // Value.
1258 if ($this->processing_unit_tag == $_POST['_wpcf7_unit_tag']) {
1259 if (isset($_POST['_wpcf7_mail_sent']) && $_POST['_wpcf7_mail_sent']['ok'])
1260 $value = '';
1261 elseif ('captchar' == $type)
1262 $value = '';
1263 else
1264 $value = $_POST[$name];
1265 } else {
1266 $value = $values[0];
1267 }
1268
1269 // Default selected/checked for select/checkbox/radio
1270 if (preg_match('/^(?:select|checkbox|radio)[*]?$/', $type)) {
1271 $scr_defaults = array_values(preg_grep('/^default:/', $options));
1272 preg_match('/^default:([0-9_]+)$/', $scr_defaults[0], $scr_default_matches);
1273 $scr_default = explode('_', $scr_default_matches[1]);
1274 }
1275
1276 switch ($type) {
1277 case 'text':
1278 case 'text*':
1279 case 'email':
1280 case 'email*':
1281 case 'captchar':
1282 if (is_array($options)) {
1283 $size_maxlength_array = preg_grep('%^[0-9]*[/x][0-9]*$%', $options);
1284 if ($size_maxlength = array_shift($size_maxlength_array)) {
1285 preg_match('%^([0-9]*)[/x]([0-9]*)$%', $size_maxlength, $sm_matches);
1286 if ($size = (int) $sm_matches[1])
1287 $atts .= ' size="' . $size . '"';
1288 else
1289 $atts .= ' size="40"';
1290 if ($maxlength = (int) $sm_matches[2])
1291 $atts .= ' maxlength="' . $maxlength . '"';
1292 } else {
1293 $atts .= ' size="40"';
1294 }
1295 }
1296 $html = '<input type="text" name="' . $name . '" value="' . attribute_escape($value) . '"' . $atts . ' />';
1297 $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
1298 return $html;
1299 break;
1300 case 'textarea':
1301 case 'textarea*':
1302 if (is_array($options)) {
1303 $cols_rows_array = preg_grep('%^[0-9]*[x/][0-9]*$%', $options);
1304 if ($cols_rows = array_shift($cols_rows_array)) {
1305 preg_match('%^([0-9]*)[x/]([0-9]*)$%', $cols_rows, $cr_matches);
1306 if ($cols = (int) $cr_matches[1])
1307 $atts .= ' cols="' . $cols . '"';
1308 else
1309 $atts .= ' cols="40"';
1310 if ($rows = (int) $cr_matches[2])
1311 $atts .= ' rows="' . $rows . '"';
1312 else
1313 $atts .= ' rows="10"';
1314 } else {
1315 $atts .= ' cols="40" rows="10"';
1316 }
1317 }
1318 $html = '<textarea name="' . $name . '"' . $atts . '>' . $value . '</textarea>';
1319 $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
1320 return $html;
1321 break;
1322 case 'select':
1323 case 'select*':
1324 $multiple = (preg_grep('%^multiple$%', $options)) ? true : false;
1325 $include_blank = preg_grep('%^include_blank$%', $options);
1326
1327 if ($empty_select = empty($values) || $include_blank)
1328 array_unshift($values, '---');
1329
1330 $html = '';
1331 foreach ($values as $key => $value) {
1332 $selected = '';
1333 if (! $empty_select && in_array($key + 1, (array) $scr_default))
1334 $selected = ' selected="selected"';
1335 if ($this->processing_unit_tag == $_POST['_wpcf7_unit_tag'] && (
1336 $multiple && in_array($value, (array) $_POST[$name]) ||
1337 ! $multiple && $_POST[$name] == $value))
1338 $selected = ' selected="selected"';
1339 $html .= '<option value="' . attribute_escape($value) . '"' . $selected . '>' . $value . '</option>';
1340 }
1341
1342 if ($multiple)
1343 $atts .= ' multiple="multiple"';
1344
1345 $html = '<select name="' . $name . ($multiple ? '[]' : '') . '"' . $atts . '>' . $html . '</select>';
1346 $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
1347 return $html;
1348 break;
1349 case 'checkbox':
1350 case 'checkbox*':
1351 case 'radio':
1352 $multiple = (preg_match('/^checkbox[*]?$/', $type) && ! preg_grep('%^exclusive$%', $options)) ? true : false;
1353 $html = '';
1354
1355 if (preg_match('/^checkbox[*]?$/', $type) && ! $multiple)
1356 $onclick = ' onclick="wpcf7ExclusiveCheckbox(this);"';
1357
1358 $input_type = rtrim($type, '*');
1359
1360 foreach ($values as $key => $value) {
1361 $checked = '';
1362 if (in_array($key + 1, (array) $scr_default))
1363 $checked = ' checked="checked"';
1364 if ($this->processing_unit_tag == $_POST['_wpcf7_unit_tag'] && (
1365 $multiple && in_array($value, (array) $_POST[$name]) ||
1366 ! $multiple && $_POST[$name] == $value))
1367 $checked = ' checked="checked"';
1368 if (preg_grep('%^label[_-]?first$%', $options)) { // put label first, input last
1369 $item = '<span class="wpcf7-list-item-label">' . $value . '</span>&nbsp;';
1370 $item .= '<input type="' . $input_type . '" name="' . $name . ($multiple ? '[]' : '') . '" value="' . attribute_escape($value) . '"' . $checked . $onclick . ' />';
1371 } else {
1372 $item = '<input type="' . $input_type . '" name="' . $name . ($multiple ? '[]' : '') . '" value="' . attribute_escape($value) . '"' . $checked . $onclick . ' />';
1373 $item .= '&nbsp;<span class="wpcf7-list-item-label">' . $value . '</span>';
1374 }
1375 $item = '<span class="wpcf7-list-item">' . $item . '</span>';
1376 $html .= $item;
1377 }
1378
1379 $html = '<span' . $atts . '>' . $html . '</span>';
1380 $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
1381 return $html;
1382 break;
1383 case 'quiz':
1384 if (count($raw_values) == 0 && count($values) == 0) { // default quiz
1385 $raw_values[] = '1+1=?|2';
1386 $values[] = '1+1=?';
1387 }
1388
1389 $pipes = $this->get_pipes($raw_values);
1390
1391 if (count($values) == 0) {
1392 break;
1393 } elseif (count($values) == 1) {
1394 $value = $values[0];
1395 } else {
1396 $value = $values[array_rand($values)];
1397 }
1398
1399 $answer = $this->pipe($pipes, $value);
1400 $answer = $this->canonicalize($answer);
1401
1402 if (is_array($options)) {
1403 $size_maxlength_array = preg_grep('%^[0-9]*[/x][0-9]*$%', $options);
1404 if ($size_maxlength = array_shift($size_maxlength_array)) {
1405 preg_match('%^([0-9]*)[/x]([0-9]*)$%', $size_maxlength, $sm_matches);
1406 if ($size = (int) $sm_matches[1])
1407 $atts .= ' size="' . $size . '"';
1408 else
1409 $atts .= ' size="40"';
1410 if ($maxlength = (int) $sm_matches[2])
1411 $atts .= ' maxlength="' . $maxlength . '"';
1412 } else {
1413 $atts .= ' size="40"';
1414 }
1415 }
1416
1417 $html = '<span class="wpcf7-quiz-label">' . $value . '</span>&nbsp;';
1418 $html .= '<input type="text" name="' . $name . '"' . $atts . ' />';
1419 $html .= '<input type="hidden" name="_wpcf7_quiz_answer_' . $name . '" value="' . wp_hash($answer, 'wpcf7_quiz') . '" />';
1420 $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
1421 return $html;
1422 break;
1423 case 'acceptance':
1424 $invert = (bool) preg_grep('%^invert$%', $options);
1425 $default = (bool) preg_grep('%^default:on$%', $options);
1426
1427 $onclick = ' onclick="wpcf7ToggleSubmit(this.form);"';
1428 $checked = $default ? ' checked="checked"' : '';
1429 $html = '<input type="checkbox" name="' . $name . '" value="1"' . $atts . $onclick . $checked . ' />';
1430 return $html;
1431 break;
1432 case 'captchac':
1433 if (! class_exists('ReallySimpleCaptcha')) {
1434 return '<em>' . __('To use CAPTCHA, you need <a href="http://wordpress.org/extend/plugins/really-simple-captcha/">Really Simple CAPTCHA</a> plugin installed.', 'wpcf7') . '</em>';
1435 break;
1436 }
1437
1438 $op = array();
1439 // Default
1440 $op['img_size'] = array(72, 24);
1441 $op['base'] = array(6, 18);
1442 $op['font_size'] = 14;
1443 $op['font_char_width'] = 15;
1444
1445 $op = array_merge($op, $this->captchac_options($options));
1446
1447 if (! $filename = $this->generate_captcha($op)) {
1448 return '';
1449 break;
1450 }
1451 if (is_array($op['img_size']))
1452 $atts .= ' width="' . $op['img_size'][0] . '" height="' . $op['img_size'][1] . '"';
1453 $captcha_url = trailingslashit($this->captcha_tmp_url()) . $filename;
1454 $html = '<img alt="captcha" src="' . $captcha_url . '"' . $atts . ' />';
1455 $ref = substr($filename, 0, strrpos($filename, '.'));
1456 $html = '<input type="hidden" name="_wpcf7_captcha_challenge_' . $name . '" value="' . $ref . '" />' . $html;
1457 return $html;
1458 break;
1459 case 'file':
1460 case 'file*':
1461 $html = '<input type="file" name="' . $name . '"' . $atts . ' value="1" />';
1462 $html = '<span class="wpcf7-form-control-wrap ' . $name . '">' . $html . $validation_error . '</span>';
1463 return $html;
1464 break;
1465 }
1466 }
1467
1468 function submit_replace_callback($matches) {
1469 $atts = '';
1470 $options = preg_split('/[\s]+/', trim($matches[1]));
1471
1472 $id_array = preg_grep('%^id:[-0-9a-zA-Z_]+$%', $options);
1473 if ($id = array_shift($id_array)) {
1474 preg_match('%^id:([-0-9a-zA-Z_]+)$%', $id, $id_matches);
1475 if ($id = $id_matches[1])
1476 $atts .= ' id="' . $id . '"';
1477 }
1478
1479 $class_att = '';
1480 $class_array = preg_grep('%^class:[-0-9a-zA-Z_]+$%', $options);
1481 foreach ($class_array as $class) {
1482 preg_match('%^class:([-0-9a-zA-Z_]+)$%', $class, $class_matches);
1483 if ($class = $class_matches[1])
1484 $class_att .= ' ' . $class;
1485 }
1486
1487 if ($class_att)
1488 $atts .= ' class="' . trim($class_att) . '"';
1489
1490 if ($matches[2])
1491 $value = $this->strip_quote($matches[2]);
1492 if (empty($value))
1493 $value = __('Send', 'wpcf7');
1494 $ajax_loader_image_url = WPCF7_PLUGIN_URL . '/images/ajax-loader.gif';
1495
1496 $html = '<input type="submit" value="' . $value . '"' . $atts . ' />';
1497 $html .= ' <img class="ajax-loader" style="visibility: hidden;" alt="ajax loader" src="' . $ajax_loader_image_url . '" />';
1498 return $html;
1499 }
1500
1501 function canonicalize($text) {
1502 if (function_exists('mb_convert_kana') && 'UTF-8' == get_option('blog_charset'))
1503 $text = mb_convert_kana($text, 'asKV', 'UTF-8');
1504
1505 $text = strtolower($text);
1506 $text = trim($text);
1507 return $text;
1508 }
1509
1510 function form_element_parse($element) {
1511 $type = trim($element[1]);
1512 $name = trim($element[2]);
1513 $options = preg_split('/[\s]+/', trim($element[3]));
1514
1515 preg_match_all('/"[^"]*"|\'[^\']*\'/', $element[4], $matches);
1516 $raw_values = $this->strip_quote_deep($matches[0]);
1517
1518 if (WPCF7_USE_PIPE && preg_match('/^(select[*]?|checkbox[*]?|radio)$/', $type) || 'quiz' == $type) {
1519 $pipes = $this->get_pipes($raw_values);
1520 $values = $this->get_pipe_ins($pipes);
1521 } else {
1522 $values =& $raw_values;
1523 }
1524
1525 return compact('type', 'name', 'options', 'values', 'raw_values');
1526 }
1527
1528 function strip_quote($text) {
1529 $text = trim($text);
1530 if (preg_match('/^"(.*)"$/', $text, $matches))
1531 $text = $matches[1];
1532 elseif (preg_match("/^'(.*)'$/", $text, $matches))
1533 $text = $matches[1];
1534 return $text;
1535 }
1536
1537 function strip_quote_deep($arr) {
1538 if (is_string($arr))
1539 return $this->strip_quote($arr);
1540 if (is_array($arr)) {
1541 $result = array();
1542 foreach ($arr as $key => $text) {
1543 $result[$key] = $this->strip_quote($text);
1544 }
1545 return $result;
1546 }
1547 }
1548
1549 function init_uploads() {
1550 $dir = $this->upload_tmp_dir();
1551 wp_mkdir_p(trailingslashit($dir));
1552 @chmod($dir, 0733);
1553 }
1554
1555 function cleanup_upload_files() {
1556 $dir = $this->upload_tmp_dir();
1557 $dir = trailingslashit($dir);
1558 if ($handle = opendir($dir)) {
1559 while (false !== ($file = readdir($handle))) {
1560 if ($file == "." || $file == "..")
1561 continue;
1562
1563 $stat = stat($dir . $file);
1564 if ($stat['mtime'] + 60 < time()) // 60 secs
1565 @ unlink($dir . $file);
1566 }
1567 closedir($handle);
1568 }
1569 }
1570
1571 function init_captcha() {
1572 if (! class_exists('ReallySimpleCaptcha'))
1573 return false;
1574
1575 if (! is_object($this->captcha))
1576 $this->captcha = new ReallySimpleCaptcha();
1577 $captcha =& $this->captcha;
1578
1579 $captcha->tmp_dir = trailingslashit($this->captcha_tmp_dir());
1580 wp_mkdir_p($captcha->tmp_dir);
1581 return true;
1582 }
1583
1584 function generate_captcha($options = null) {
1585 if (! $this->init_captcha())
1586 return false;
1587 $captcha =& $this->captcha;
1588
1589 if (! is_dir($captcha->tmp_dir) || ! is_writable($captcha->tmp_dir))
1590 return false;
1591
1592 $img_type = imagetypes();
1593 if ($img_type & IMG_PNG)
1594 $captcha->img_type = 'png';
1595 elseif ($img_type & IMG_GIF)
1596 $captcha->img_type = 'gif';
1597 elseif ($img_type & IMG_JPG)
1598 $captcha->img_type = 'jpeg';
1599 else
1600 return false;
1601
1602 if (is_array($options)) {
1603 if (isset($options['img_size']))
1604 $captcha->img_size = $options['img_size'];
1605 if (isset($options['base']))
1606 $captcha->base = $options['base'];
1607 if (isset($options['font_size']))
1608 $captcha->font_size = $options['font_size'];
1609 if (isset($options['font_char_width']))
1610 $captcha->font_char_width = $options['font_char_width'];
1611 if (isset($options['fg']))
1612 $captcha->fg = $options['fg'];
1613 if (isset($options['bg']))
1614 $captcha->bg = $options['bg'];
1615 }
1616
1617 $prefix = mt_rand();
1618 $captcha_word = $captcha->generate_random_word();
1619 return $captcha->generate_image($prefix, $captcha_word);
1620 }
1621
1622 function check_captcha($prefix, $response) {
1623 if (! $this->init_captcha())
1624 return false;
1625 $captcha =& $this->captcha;
1626
1627 return $captcha->check($prefix, $response);
1628 }
1629
1630 function remove_captcha($prefix) {
1631 if (! $this->init_captcha())
1632 return false;
1633 $captcha =& $this->captcha;
1634
1635 $captcha->remove($prefix);
1636 }
1637
1638 function cleanup_captcha_files() {
1639 if (! $this->init_captcha())
1640 return false;
1641 $captcha =& $this->captcha;
1642
1643 $tmp_dir = $captcha->tmp_dir;
1644
1645 if (! is_dir($tmp_dir) || ! is_writable($tmp_dir))
1646 return false;
1647
1648 if ($handle = opendir($tmp_dir)) {
1649 while (false !== ($file = readdir($handle))) {
1650 if (! preg_match('/^[0-9]+\.(php|png|gif|jpeg)$/', $file))
1651 continue;
1652 $stat = stat($tmp_dir . $file);
1653 if ($stat['mtime'] + 21600 < time()) // 21600 secs == 6 hours
1654 @ unlink($tmp_dir . $file);
1655 }
1656 closedir($handle);
1657 }
1658 }
1659
1660 function captchac_options($options) {
1661 if (! is_array($options))
1662 return array();
1663
1664 $op = array();
1665 $image_size_array = preg_grep('%^size:[smlSML]$%', $options);
1666 if ($image_size = array_shift($image_size_array)) {
1667 preg_match('%^size:([smlSML])$%', $image_size, $is_matches);
1668 switch (strtolower($is_matches[1])) {
1669 case 's':
1670 $op['img_size'] = array(60, 20);
1671 $op['base'] = array(6, 15);
1672 $op['font_size'] = 11;
1673 $op['font_char_width'] = 13;
1674 break;
1675 case 'l':
1676 $op['img_size'] = array(84, 28);
1677 $op['base'] = array(6, 20);
1678 $op['font_size'] = 17;
1679 $op['font_char_width'] = 19;
1680 break;
1681 case 'm':
1682 default:
1683 $op['img_size'] = array(72, 24);
1684 $op['base'] = array(6, 18);
1685 $op['font_size'] = 14;
1686 $op['font_char_width'] = 15;
1687 }
1688 }
1689 $fg_color_array = preg_grep('%^fg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $options);
1690 if ($fg_color = array_shift($fg_color_array)) {
1691 preg_match('%^fg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $fg_color, $fc_matches);
1692 if (3 == strlen($fc_matches[1])) {
1693 $r = substr($fc_matches[1], 0, 1);
1694 $g = substr($fc_matches[1], 1, 1);
1695 $b = substr($fc_matches[1], 2, 1);
1696 $op['fg'] = array(hexdec($r . $r), hexdec($g . $g), hexdec($b . $b));
1697 } elseif (6 == strlen($fc_matches[1])) {
1698 $r = substr($fc_matches[1], 0, 2);
1699 $g = substr($fc_matches[1], 2, 2);
1700 $b = substr($fc_matches[1], 4, 2);
1701 $op['fg'] = array(hexdec($r), hexdec($g), hexdec($b));
1702 }
1703 }
1704 $bg_color_array = preg_grep('%^bg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $options);
1705 if ($bg_color = array_shift($bg_color_array)) {
1706 preg_match('%^bg:#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$%', $bg_color, $bc_matches);
1707 if (3 == strlen($bc_matches[1])) {
1708 $r = substr($bc_matches[1], 0, 1);
1709 $g = substr($bc_matches[1], 1, 1);
1710 $b = substr($bc_matches[1], 2, 1);
1711 $op['bg'] = array(hexdec($r . $r), hexdec($g . $g), hexdec($b . $b));
1712 } elseif (6 == strlen($bc_matches[1])) {
1713 $r = substr($bc_matches[1], 0, 2);
1714 $g = substr($bc_matches[1], 2, 2);
1715 $b = substr($bc_matches[1], 4, 2);
1716 $op['bg'] = array(hexdec($r), hexdec($g), hexdec($b));
1717 }
1718 }
1719
1720 return $op;
1721 }
1722
1723 function pipe($pipes, $value) {
1724 if (is_array($value)) {
1725 $results = array();
1726 foreach ($value as $k => $v) {
1727 $results[$k] = $this->pipe($pipes, $v);
1728 }
1729 return $results;
1730 }
1731
1732 foreach ($pipes as $p) {
1733 if ($p[0] == $value)
1734 return $p[1];
1735 }
1736
1737 return $value;
1738 }
1739
1740 function get_pipe_ins($pipes) {
1741 $ins = array();
1742 foreach ($pipes as $pipe) {
1743 $in = $pipe[0];
1744 if (! in_array($in, $ins))
1745 $ins[] = $in;
1746 }
1747 return $ins;
1748 }
1749
1750 function get_pipes($values) {
1751 $pipes = array();
1752
1753 foreach ($values as $value) {
1754 $pipe_pos = strpos($value, '|');
1755 if (false === $pipe_pos) {
1756 $before = $after = $value;
1757 } else {
1758 $before = substr($value, 0, $pipe_pos);
1759 $after = substr($value, $pipe_pos + 1);
1760 }
1761
1762 $pipes[] = array($before, $after);
1763 }
1764
1765 return $pipes;
1766 }
1767
1768 function pipe_all_posted($contact_form) {
1769 $all_pipes = array();
1770
1771 $fes = $this->form_elements($contact_form['form'], false);
1772 foreach ($fes as $fe) {
1773 $type = $fe['type'];
1774 $name = $fe['name'];
1775 $raw_values = $fe['raw_values'];
1776
1777 if (! preg_match('/^(select[*]?|checkbox[*]?|radio)$/', $type))
1778 continue;
1779
1780 $pipes = $this->get_pipes($raw_values);
1781
1782 $all_pipes[$name] = array_merge($pipes, (array) $all_pipes[$name]);
1783 }
1784
1785 foreach ($all_pipes as $name => $pipes) {
1786 if (isset($this->posted_data[$name]))
1787 $this->posted_data[$name] = $this->pipe($pipes, $this->posted_data[$name]);
1788 }
1789 }
1790
1791 function captcha_tmp_dir() {
1792 if (defined('WPCF7_CAPTCHA_TMP_DIR'))
1793 return WPCF7_CAPTCHA_TMP_DIR;
1794 else
1795 return $this->upload_dir('dir') . '/wpcf7_captcha';
1796 }
1797
1798 function captcha_tmp_url() {
1799 if (defined('WPCF7_CAPTCHA_TMP_URL'))
1800 return WPCF7_CAPTCHA_TMP_URL;
1801 else
1802 return $this->upload_dir('url') . '/wpcf7_captcha';
1803 }
1804
1805 function upload_tmp_dir() {
1806 if (defined('WPCF7_UPLOADS_TMP_DIR'))
1807 return WPCF7_UPLOADS_TMP_DIR;
1808 else
1809 return $this->upload_dir('dir') . '/wpcf7_uploads';
1810 }
1811
1812 function upload_dir($type = false) {
1813 $siteurl = get_option('siteurl');
1814 $upload_path = trim(get_option('upload_path'));
1815 if (empty($upload_path))
1816 $dir = WP_CONTENT_DIR . '/uploads';
1817 else
1818 $dir = $upload_path;
1819
1820 $dir = path_join(ABSPATH, $dir);
1821
1822 if (! $url = get_option('upload_url_path')) {
1823 if (empty($upload_path) || $upload_path == $dir)
1824 $url = WP_CONTENT_URL . '/uploads';
1825 else
1826 $url = trailingslashit($siteurl) . $upload_path;
1827 }
1828
1829 if (defined('UPLOADS')) {
1830 $dir = ABSPATH . UPLOADS;
1831 $url = trailingslashit($siteurl) . UPLOADS;
1832 }
1833
1834 if ('dir' == $type)
1835 return $dir;
1836 if ('url' == $type)
1837 return $url;
1838 return array('dir' => $dir, 'url' => $url);
1839 }
1840 }
1841
1842 $wpcf7 = new tam_contact_form_seven();
1843
1844 ?>