PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/contact-form-7/includes/classes.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 589 lines | 427 code | 150 blank | 12 comment | 107 complexity | 3a193c55909160f2362d4a43db29148e MD5 | raw file
  1. <?php
  2. class WPCF7_ContactForm {
  3. var $initial = false;
  4. var $id;
  5. var $title;
  6. var $form;
  7. var $mail;
  8. var $mail_2;
  9. var $messages;
  10. var $additional_settings;
  11. var $unit_tag;
  12. var $responses_count = 0;
  13. var $scanned_form_tags;
  14. var $posted_data;
  15. var $uploaded_files;
  16. var $skip_mail = false;
  17. // Return true if this form is the same one as currently POSTed.
  18. function is_posted() {
  19. if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) )
  20. return false;
  21. if ( $this->unit_tag == $_POST['_wpcf7_unit_tag'] )
  22. return true;
  23. return false;
  24. }
  25. /* Generating Form HTML */
  26. function form_html() {
  27. $form = '<div class="wpcf7" id="' . $this->unit_tag . '">';
  28. $url = wpcf7_get_request_uri();
  29. if ( $frag = strstr( $url, '#' ) )
  30. $url = substr( $url, 0, -strlen( $frag ) );
  31. $url .= '#' . $this->unit_tag;
  32. $url = apply_filters( 'wpcf7_form_action_url', $url );
  33. $url = esc_url_raw( $url );
  34. $enctype = apply_filters( 'wpcf7_form_enctype', '' );
  35. $form .= '<form action="' . $url
  36. . '" method="post" class="wpcf7-form"' . $enctype . '>' . "\n";
  37. $form .= '<div style="display: none;">' . "\n";
  38. $form .= '<input type="hidden" name="_wpcf7" value="'
  39. . esc_attr( $this->id ) . '" />' . "\n";
  40. $form .= '<input type="hidden" name="_wpcf7_version" value="'
  41. . esc_attr( WPCF7_VERSION ) . '" />' . "\n";
  42. $form .= '<input type="hidden" name="_wpcf7_unit_tag" value="'
  43. . esc_attr( $this->unit_tag ) . '" />' . "\n";
  44. $form .= '</div>' . "\n";
  45. $form .= $this->form_elements();
  46. if ( ! $this->responses_count )
  47. $form .= $this->form_response_output();
  48. $form .= '</form>';
  49. $form .= '</div>';
  50. return $form;
  51. }
  52. function form_response_output() {
  53. $class = 'wpcf7-response-output';
  54. $content = '';
  55. if ( $this->is_posted() ) { // Post response output for non-AJAX
  56. if ( isset( $_POST['_wpcf7_mail_sent'] ) && $_POST['_wpcf7_mail_sent']['id'] == $this->id ) {
  57. if ( $_POST['_wpcf7_mail_sent']['ok'] ) {
  58. $class .= ' wpcf7-mail-sent-ok';
  59. $content = $_POST['_wpcf7_mail_sent']['message'];
  60. } else {
  61. $class .= ' wpcf7-mail-sent-ng';
  62. if ( $_POST['_wpcf7_mail_sent']['spam'] )
  63. $class .= ' wpcf7-spam-blocked';
  64. $content = $_POST['_wpcf7_mail_sent']['message'];
  65. }
  66. } elseif ( isset( $_POST['_wpcf7_validation_errors'] ) && $_POST['_wpcf7_validation_errors']['id'] == $this->id ) {
  67. $class .= ' wpcf7-validation-errors';
  68. $content = $this->message( 'validation_error' );
  69. }
  70. } else {
  71. $class .= ' wpcf7-display-none';
  72. }
  73. $class = ' class="' . $class . '"';
  74. return '<div' . $class . '>' . $content . '</div>';
  75. }
  76. function validation_error( $name ) {
  77. if ( $this->is_posted() && $ve = $_POST['_wpcf7_validation_errors']['messages'][$name] )
  78. return apply_filters( 'wpcf7_validation_error',
  79. '<span class="wpcf7-not-valid-tip-no-ajax">' . esc_html( $ve ) . '</span>',
  80. $name, $this );
  81. return '';
  82. }
  83. /* Form Elements */
  84. function form_do_shortcode() {
  85. global $wpcf7_shortcode_manager;
  86. $form = $this->form;
  87. $form = $wpcf7_shortcode_manager->do_shortcode( $form );
  88. $this->scanned_form_tags = $wpcf7_shortcode_manager->scanned_tags;
  89. if ( WPCF7_AUTOP )
  90. $form = wpcf7_autop( $form );
  91. return $form;
  92. }
  93. function form_scan_shortcode( $cond = null ) {
  94. global $wpcf7_shortcode_manager;
  95. if ( ! empty( $this->scanned_form_tags ) ) {
  96. $scanned = $this->scanned_form_tags;
  97. } else {
  98. $scanned = $wpcf7_shortcode_manager->scan_shortcode( $this->form );
  99. $this->scanned_form_tags = $scanned;
  100. }
  101. if ( empty( $scanned ) )
  102. return null;
  103. if ( ! is_array( $cond ) || empty( $cond ) )
  104. return $scanned;
  105. for ( $i = 0, $size = count( $scanned ); $i < $size; $i++ ) {
  106. if ( is_string( $cond['type'] ) && ! empty( $cond['type'] ) ) {
  107. if ( $scanned[$i]['type'] != $cond['type'] ) {
  108. unset( $scanned[$i] );
  109. continue;
  110. }
  111. } elseif ( is_array( $cond['type'] ) ) {
  112. if ( ! in_array( $scanned[$i]['type'], $cond['type'] ) ) {
  113. unset( $scanned[$i] );
  114. continue;
  115. }
  116. }
  117. if ( is_string( $cond['name'] ) && ! empty( $cond['name'] ) ) {
  118. if ( $scanned[$i]['name'] != $cond['name'] ) {
  119. unset ( $scanned[$i] );
  120. continue;
  121. }
  122. } elseif ( is_array( $cond['name'] ) ) {
  123. if ( ! in_array( $scanned[$i]['name'], $cond['name'] ) ) {
  124. unset( $scanned[$i] );
  125. continue;
  126. }
  127. }
  128. }
  129. return array_values( $scanned );
  130. }
  131. function form_elements() {
  132. $form = apply_filters( 'wpcf7_form_elements', $this->form_do_shortcode() );
  133. // Response output
  134. $response_regex = '%\[\s*response\s*\]%';
  135. $form = preg_replace_callback( $response_regex,
  136. array( &$this, 'response_replace_callback' ), $form );
  137. return $form;
  138. }
  139. function response_replace_callback( $matches ) {
  140. $this->responses_count += 1;
  141. return $this->form_response_output();
  142. }
  143. /* Validate */
  144. function validate() {
  145. $fes = $this->form_scan_shortcode();
  146. $result = array( 'valid' => true, 'reason' => array() );
  147. foreach ( $fes as $fe ) {
  148. $result = apply_filters( 'wpcf7_validate_' . $fe['type'], $result, $fe );
  149. }
  150. return $result;
  151. }
  152. /* Acceptance */
  153. function accepted() {
  154. $accepted = true;
  155. return apply_filters( 'wpcf7_acceptance', $accepted );
  156. }
  157. /* Akismet */
  158. function akismet() {
  159. global $akismet_api_host, $akismet_api_port;
  160. if ( ! function_exists( 'akismet_http_post' ) ||
  161. ! ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) )
  162. return false;
  163. $akismet_ready = false;
  164. $author = $author_email = $author_url = $content = '';
  165. $fes = $this->form_scan_shortcode();
  166. foreach ( $fes as $fe ) {
  167. if ( ! is_array( $fe['options'] ) ) continue;
  168. if ( preg_grep( '%^akismet:author$%', $fe['options'] ) && '' == $author ) {
  169. $author = $_POST[$fe['name']];
  170. $akismet_ready = true;
  171. }
  172. if ( preg_grep( '%^akismet:author_email$%', $fe['options'] ) && '' == $author_email ) {
  173. $author_email = $_POST[$fe['name']];
  174. $akismet_ready = true;
  175. }
  176. if ( preg_grep( '%^akismet:author_url$%', $fe['options'] ) && '' == $author_url ) {
  177. $author_url = $_POST[$fe['name']];
  178. $akismet_ready = true;
  179. }
  180. if ( '' != $content )
  181. $content .= "\n\n";
  182. $content .= $_POST[$fe['name']];
  183. }
  184. if ( ! $akismet_ready )
  185. return false;
  186. $c['blog'] = get_option( 'home' );
  187. $c['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
  188. $c['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  189. $c['referrer'] = $_SERVER['HTTP_REFERER'];
  190. $c['comment_type'] = 'contactform7';
  191. if ( $permalink = get_permalink() )
  192. $c['permalink'] = $permalink;
  193. if ( '' != $author )
  194. $c['comment_author'] = $author;
  195. if ( '' != $author_email )
  196. $c['comment_author_email'] = $author_email;
  197. if ( '' != $author_url )
  198. $c['comment_author_url'] = $author_url;
  199. if ( '' != $content )
  200. $c['comment_content'] = $content;
  201. $ignore = array( 'HTTP_COOKIE' );
  202. foreach ( $_SERVER as $key => $value )
  203. if ( ! in_array( $key, (array) $ignore ) )
  204. $c["$key"] = $value;
  205. $query_string = '';
  206. foreach ( $c as $key => $data )
  207. $query_string .= $key . '=' . urlencode( stripslashes( $data ) ) . '&';
  208. $response = akismet_http_post( $query_string, $akismet_api_host,
  209. '/1.1/comment-check', $akismet_api_port );
  210. if ( 'true' == $response[1] )
  211. return true;
  212. else
  213. return false;
  214. }
  215. /* Mail */
  216. function mail() {
  217. $fes = $this->form_scan_shortcode();
  218. foreach ( $fes as $fe ) {
  219. $name = $fe['name'];
  220. $pipes = $fe['pipes'];
  221. if ( empty( $name ) )
  222. continue;
  223. $value = $_POST[$name];
  224. if ( WPCF7_USE_PIPE && is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) {
  225. if ( is_array( $value) ) {
  226. $new_value = array();
  227. foreach ( $value as $v ) {
  228. $new_value[] = $pipes->do_pipe( stripslashes( $v ) );
  229. }
  230. $value = $new_value;
  231. } else {
  232. $value = $pipes->do_pipe( stripslashes( $value ) );
  233. }
  234. }
  235. $this->posted_data[$name] = $value;
  236. }
  237. if ( $this->in_demo_mode() )
  238. $this->skip_mail = true;
  239. do_action_ref_array( 'wpcf7_before_send_mail', array( &$this ) );
  240. if ( $this->skip_mail )
  241. return true;
  242. if ( $this->compose_and_send_mail( $this->mail ) ) {
  243. if ( $this->mail_2['active'] )
  244. $this->compose_and_send_mail( $this->mail_2 );
  245. return true;
  246. }
  247. return false;
  248. }
  249. function compose_and_send_mail( $mail_template ) {
  250. $regex = '/\[\s*([a-zA-Z_][0-9a-zA-Z:._-]*)\s*\]/';
  251. $callback = array( &$this, 'mail_callback' );
  252. $subject = preg_replace_callback( $regex, $callback, $mail_template['subject'] );
  253. $sender = preg_replace_callback( $regex, $callback, $mail_template['sender'] );
  254. $recipient = preg_replace_callback( $regex, $callback, $mail_template['recipient'] );
  255. $additional_headers =
  256. preg_replace_callback( $regex, $callback, $mail_template['additional_headers'] );
  257. if ( $mail_template['use_html'] ) {
  258. $callback_html = array( &$this, 'mail_callback_html' );
  259. $body = preg_replace_callback( $regex, $callback_html, $mail_template['body'] );
  260. } else {
  261. $body = preg_replace_callback( $regex, $callback, $mail_template['body'] );
  262. }
  263. extract( apply_filters( 'wpcf7_mail_components',
  264. compact( 'subject', 'sender', 'body', 'recipient', 'additional_headers' ) ) );
  265. $headers = "From: $sender\n";
  266. if ( $mail_template['use_html'] )
  267. $headers .= "Content-Type: text/html\n";
  268. $headers .= trim( $additional_headers ) . "\n";
  269. if ( $this->uploaded_files ) {
  270. $for_this_mail = array();
  271. foreach ( $this->uploaded_files as $name => $path ) {
  272. if ( false === strpos( $mail_template['attachments'], "[${name}]" ) )
  273. continue;
  274. $for_this_mail[] = $path;
  275. }
  276. return @wp_mail( $recipient, $subject, $body, $headers, $for_this_mail );
  277. } else {
  278. return @wp_mail( $recipient, $subject, $body, $headers );
  279. }
  280. }
  281. function mail_callback_html( $matches ) {
  282. return $this->mail_callback( $matches, true );
  283. }
  284. function mail_callback( $matches, $html = false ) {
  285. if ( isset( $this->posted_data[$matches[1]] ) ) {
  286. $submitted = $this->posted_data[$matches[1]];
  287. if ( is_array( $submitted ) )
  288. $replaced = join( ', ', $submitted );
  289. else
  290. $replaced = $submitted;
  291. if ( $html )
  292. $replaced = esc_html( $replaced );
  293. $replaced = apply_filters( 'wpcf7_mail_tag_replaced', $replaced, $submitted );
  294. return stripslashes( $replaced );
  295. }
  296. if ( $special = apply_filters( 'wpcf7_special_mail_tags', '', $matches[1] ) )
  297. return $special;
  298. return $matches[0];
  299. }
  300. /* Message */
  301. function message( $status ) {
  302. $messages = $this->messages;
  303. $message = $messages[$status];
  304. return apply_filters( 'wpcf7_display_message', $message );
  305. }
  306. /* Additional settings */
  307. function additional_setting( $name, $max = 1 ) {
  308. $tmp_settings = (array) explode( "\n", $this->additional_settings );
  309. $count = 0;
  310. $values = array();
  311. foreach ( $tmp_settings as $setting ) {
  312. if ( preg_match('/^([a-zA-Z0-9_]+)\s*:(.*)$/', $setting, $matches ) ) {
  313. if ( $matches[1] != $name )
  314. continue;
  315. if ( ! $max || $count < (int) $max ) {
  316. $values[] = trim( $matches[2] );
  317. $count += 1;
  318. }
  319. }
  320. }
  321. return $values;
  322. }
  323. function in_demo_mode() {
  324. $settings = $this->additional_setting( 'demo_mode', false );
  325. foreach ( $settings as $setting ) {
  326. if ( in_array( $setting, array( 'on', 'true', '1' ) ) )
  327. return true;
  328. }
  329. return false;
  330. }
  331. /* Upgrade */
  332. function upgrade() {
  333. if ( ! isset( $this->mail['recipient'] ) )
  334. $this->mail['recipient'] = get_option( 'admin_email' );
  335. if ( ! is_array( $this->messages ) )
  336. $this->messages = array();
  337. foreach ( wpcf7_messages() as $key => $arr ) {
  338. if ( ! isset( $this->messages[$key] ) )
  339. $this->messages[$key] = $arr['default'];
  340. }
  341. }
  342. /* Save */
  343. function save() {
  344. global $wpdb, $wpcf7;
  345. $fields = array(
  346. 'title' => maybe_serialize( stripslashes_deep( $this->title ) ),
  347. 'form' => maybe_serialize( stripslashes_deep( $this->form ) ),
  348. 'mail' => maybe_serialize( stripslashes_deep( $this->mail ) ),
  349. 'mail_2' => maybe_serialize ( stripslashes_deep( $this->mail_2 ) ),
  350. 'messages' => maybe_serialize( stripslashes_deep( $this->messages ) ),
  351. 'additional_settings' =>
  352. maybe_serialize( stripslashes_deep( $this->additional_settings ) ) );
  353. if ( $this->initial ) {
  354. $result = $wpdb->insert( $wpcf7->contactforms, $fields );
  355. if ( $result ) {
  356. $this->initial = false;
  357. $this->id = $wpdb->insert_id;
  358. do_action_ref_array( 'wpcf7_after_create', array( &$this ) );
  359. } else {
  360. return false; // Failed to save
  361. }
  362. } else { // Update
  363. if ( ! (int) $this->id )
  364. return false; // Missing ID
  365. $result = $wpdb->update( $wpcf7->contactforms, $fields,
  366. array( 'cf7_unit_id' => absint( $this->id ) ) );
  367. if ( false !== $result ) {
  368. do_action_ref_array( 'wpcf7_after_update', array( &$this ) );
  369. } else {
  370. return false; // Failed to save
  371. }
  372. }
  373. do_action_ref_array( 'wpcf7_after_save', array( &$this ) );
  374. return true; // Succeeded to save
  375. }
  376. function copy() {
  377. $new = new WPCF7_ContactForm();
  378. $new->initial = true;
  379. $new->title = $this->title . '_copy';
  380. $new->form = $this->form;
  381. $new->mail = $this->mail;
  382. $new->mail_2 = $this->mail_2;
  383. $new->messages = $this->messages;
  384. $new->additional_settings = $this->additional_settings;
  385. return $new;
  386. }
  387. function delete() {
  388. global $wpdb, $wpcf7;
  389. if ( $this->initial )
  390. return;
  391. $query = $wpdb->prepare(
  392. "DELETE FROM $wpcf7->contactforms WHERE cf7_unit_id = %d LIMIT 1",
  393. absint( $this->id ) );
  394. $wpdb->query( $query );
  395. $this->initial = true;
  396. $this->id = null;
  397. }
  398. }
  399. function wpcf7_contact_form( $id ) {
  400. global $wpdb, $wpcf7;
  401. $query = $wpdb->prepare( "SELECT * FROM $wpcf7->contactforms WHERE cf7_unit_id = %d", $id );
  402. if ( ! $row = $wpdb->get_row( $query ) )
  403. return false; // No data
  404. $contact_form = new WPCF7_ContactForm();
  405. $contact_form->id = $row->cf7_unit_id;
  406. $contact_form->title = maybe_unserialize( $row->title );
  407. $contact_form->form = maybe_unserialize( $row->form );
  408. $contact_form->mail = maybe_unserialize( $row->mail );
  409. $contact_form->mail_2 = maybe_unserialize( $row->mail_2 );
  410. $contact_form->messages = maybe_unserialize( $row->messages );
  411. $contact_form->additional_settings = maybe_unserialize( $row->additional_settings );
  412. $contact_form->upgrade();
  413. return $contact_form;
  414. }
  415. function wpcf7_contact_form_default_pack( $locale = null ) {
  416. global $l10n;
  417. if ( $locale && $locale != get_locale() ) {
  418. $mo_orig = $l10n['wpcf7'];
  419. unset( $l10n['wpcf7'] );
  420. if ( 'en_US' != $locale ) {
  421. $mofile = wpcf7_plugin_path( 'languages/wpcf7-' . $locale . '.mo' );
  422. if ( ! load_textdomain( 'wpcf7', $mofile ) ) {
  423. $l10n['wpcf7'] = $mo_orig;
  424. unset( $mo_orig );
  425. }
  426. }
  427. }
  428. $contact_form = new WPCF7_ContactForm();
  429. $contact_form->initial = true;
  430. $contact_form->title = __( 'Untitled', 'wpcf7' );
  431. $contact_form->form = wpcf7_default_form_template();
  432. $contact_form->mail = wpcf7_default_mail_template();
  433. $contact_form->mail_2 = wpcf7_default_mail_2_template();
  434. $contact_form->messages = wpcf7_default_messages_template();
  435. if ( isset( $mo_orig ) )
  436. $l10n['wpcf7'] = $mo_orig;
  437. return $contact_form;
  438. }
  439. ?>