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

https://gitlab.com/webkod3r/tripolis · PHP · 297 lines · 221 code · 75 blank · 1 comment · 33 complexity · 09970e7f820c3b293e398c504e90ac02 MD5 · raw file

  1. <?php
  2. class WPCF7_Submission {
  3. private static $instance;
  4. private $contact_form;
  5. private $status = 'init';
  6. private $posted_data = array();
  7. private $uploaded_files = array();
  8. private $skip_mail = false;
  9. private $response = '';
  10. private $invalid_fields = array();
  11. private $meta = array();
  12. private function __construct() {}
  13. public static function get_instance( WPCF7_ContactForm $contact_form = null ) {
  14. if ( empty( self::$instance ) ) {
  15. if ( null == $contact_form ) {
  16. return null;
  17. }
  18. self::$instance = new self;
  19. self::$instance->contact_form = $contact_form;
  20. self::$instance->skip_mail = $contact_form->in_demo_mode();
  21. self::$instance->setup_posted_data();
  22. self::$instance->submit();
  23. } elseif ( null != $contact_form ) {
  24. return null;
  25. }
  26. return self::$instance;
  27. }
  28. public function get_status() {
  29. return $this->status;
  30. }
  31. public function is( $status ) {
  32. return $this->status == $status;
  33. }
  34. public function get_response() {
  35. return $this->response;
  36. }
  37. public function get_invalid_field( $name ) {
  38. if ( isset( $this->invalid_fields[$name] ) ) {
  39. return $this->invalid_fields[$name];
  40. } else {
  41. return false;
  42. }
  43. }
  44. public function get_invalid_fields() {
  45. return $this->invalid_fields;
  46. }
  47. public function get_posted_data( $name = '' ) {
  48. if ( ! empty( $name ) ) {
  49. if ( isset( $this->posted_data[$name] ) ) {
  50. return $this->posted_data[$name];
  51. } else {
  52. return null;
  53. }
  54. }
  55. return $this->posted_data;
  56. }
  57. private function setup_posted_data() {
  58. $posted_data = (array) $_POST;
  59. $posted_data = array_diff_key( $posted_data, array( '_wpnonce' => '' ) );
  60. $posted_data = $this->sanitize_posted_data( $posted_data );
  61. $tags = $this->contact_form->form_scan_shortcode();
  62. foreach ( (array) $tags as $tag ) {
  63. if ( empty( $tag['name'] ) ) {
  64. continue;
  65. }
  66. $name = $tag['name'];
  67. $value = '';
  68. if ( isset( $posted_data[$name] ) ) {
  69. $value = $posted_data[$name];
  70. }
  71. $pipes = $tag['pipes'];
  72. if ( WPCF7_USE_PIPE
  73. && $pipes instanceof WPCF7_Pipes
  74. && ! $pipes->zero() ) {
  75. if ( is_array( $value) ) {
  76. $new_value = array();
  77. foreach ( $value as $v ) {
  78. $new_value[] = $pipes->do_pipe( wp_unslash( $v ) );
  79. }
  80. $value = $new_value;
  81. } else {
  82. $value = $pipes->do_pipe( wp_unslash( $value ) );
  83. }
  84. }
  85. $posted_data[$name] = $value;
  86. }
  87. $this->posted_data = apply_filters( 'wpcf7_posted_data', $posted_data );
  88. return $this->posted_data;
  89. }
  90. private function sanitize_posted_data( $value ) {
  91. if ( is_array( $value ) ) {
  92. $value = array_map( array( $this, 'sanitize_posted_data' ), $value );
  93. } elseif ( is_string( $value ) ) {
  94. $value = wp_check_invalid_utf8( $value );
  95. $value = wp_kses_no_null( $value );
  96. }
  97. return $value;
  98. }
  99. private function submit() {
  100. if ( ! $this->is( 'init' ) ) {
  101. return $this->status;
  102. }
  103. $this->meta = array(
  104. 'remote_ip' => isset( $_SERVER['REMOTE_ADDR'] )
  105. ? preg_replace( '/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR'] )
  106. : '',
  107. 'user_agent' => isset( $_SERVER['HTTP_USER_AGENT'] )
  108. ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '',
  109. 'url' => preg_replace( '%(?<!:|/)/.*$%', '',
  110. untrailingslashit( home_url() ) ) . wpcf7_get_request_uri(),
  111. 'timestamp' => current_time( 'timestamp' ),
  112. 'unit_tag' => isset( $_POST['_wpcf7_unit_tag'] )
  113. ? $_POST['_wpcf7_unit_tag'] : '' );
  114. $contact_form = $this->contact_form;
  115. if ( ! $this->validate() ) { // Validation error occured
  116. $this->status = 'validation_failed';
  117. $this->response = $contact_form->message( 'validation_error' );
  118. } elseif ( ! $this->accepted() ) { // Not accepted terms
  119. $this->status = 'acceptance_missing';
  120. $this->response = $contact_form->message( 'accept_terms' );
  121. } elseif ( $this->spam() ) { // Spam!
  122. $this->status = 'spam';
  123. $this->response = $contact_form->message( 'spam' );
  124. } elseif ( $this->mail() ) {
  125. $this->status = 'mail_sent';
  126. $this->response = $contact_form->message( 'mail_sent_ok' );
  127. do_action( 'wpcf7_mail_sent', $contact_form );
  128. } else {
  129. $this->status = 'mail_failed';
  130. $this->response = $contact_form->message( 'mail_sent_ng' );
  131. do_action( 'wpcf7_mail_failed', $contact_form );
  132. }
  133. $this->remove_uploaded_files();
  134. return $this->status;
  135. }
  136. private function validate() {
  137. if ( $this->invalid_fields ) {
  138. return false;
  139. }
  140. require_once WPCF7_PLUGIN_DIR . '/includes/validation.php';
  141. $result = new WPCF7_Validation();
  142. $tags = $this->contact_form->form_scan_shortcode();
  143. foreach ( $tags as $tag ) {
  144. $result = apply_filters( 'wpcf7_validate_' . $tag['type'],
  145. $result, $tag );
  146. }
  147. $result = apply_filters( 'wpcf7_validate', $result, $tags );
  148. $this->invalid_fields = $result->get_invalid_fields();
  149. return $result->is_valid();
  150. }
  151. private function accepted() {
  152. return apply_filters( 'wpcf7_acceptance', true );
  153. }
  154. private function spam() {
  155. $spam = false;
  156. $user_agent = (string) $this->get_meta( 'user_agent' );
  157. if ( strlen( $user_agent ) < 2 ) {
  158. $spam = true;
  159. }
  160. if ( WPCF7_VERIFY_NONCE && ! $this->verify_nonce() ) {
  161. $spam = true;
  162. }
  163. if ( $this->blacklist_check() ) {
  164. $spam = true;
  165. }
  166. return apply_filters( 'wpcf7_spam', $spam );
  167. }
  168. private function verify_nonce() {
  169. return wpcf7_verify_nonce( $_POST['_wpnonce'], $this->contact_form->id() );
  170. }
  171. private function blacklist_check() {
  172. $target = wpcf7_array_flatten( $this->posted_data );
  173. $target[] = $this->get_meta( 'remote_ip' );
  174. $target[] = $this->get_meta( 'user_agent' );
  175. $target = implode( "\n", $target );
  176. return wpcf7_blacklist_check( $target );
  177. }
  178. /* Mail */
  179. private function mail() {
  180. $contact_form = $this->contact_form;
  181. do_action( 'wpcf7_before_send_mail', $contact_form );
  182. $skip_mail = $this->skip_mail || ! empty( $contact_form->skip_mail );
  183. $skip_mail = apply_filters( 'wpcf7_skip_mail', $skip_mail, $contact_form );
  184. if ( $skip_mail ) {
  185. return true;
  186. }
  187. $result = WPCF7_Mail::send( $contact_form->prop( 'mail' ), 'mail' );
  188. if ( $result ) {
  189. $additional_mail = array();
  190. if ( ( $mail_2 = $contact_form->prop( 'mail_2' ) ) && $mail_2['active'] ) {
  191. $additional_mail['mail_2'] = $mail_2;
  192. }
  193. $additional_mail = apply_filters( 'wpcf7_additional_mail',
  194. $additional_mail, $contact_form );
  195. foreach ( $additional_mail as $name => $template ) {
  196. WPCF7_Mail::send( $template, $name );
  197. }
  198. return true;
  199. }
  200. return false;
  201. }
  202. public function uploaded_files() {
  203. return $this->uploaded_files;
  204. }
  205. public function add_uploaded_file( $name, $file_path ) {
  206. $this->uploaded_files[$name] = $file_path;
  207. if ( empty( $this->posted_data[$name] ) ) {
  208. $this->posted_data[$name] = basename( $file_path );
  209. }
  210. }
  211. public function remove_uploaded_files() {
  212. foreach ( (array) $this->uploaded_files as $name => $path ) {
  213. @unlink( $path );
  214. @rmdir( dirname( $path ) ); // remove parent dir if it's removable (empty).
  215. }
  216. }
  217. public function get_meta( $name ) {
  218. if ( isset( $this->meta[$name] ) ) {
  219. return $this->meta[$name];
  220. }
  221. }
  222. }