/wp-content/plugins/contact-form-7/includes/config-validator.php

https://gitlab.com/webkod3r/tripolis · PHP · 286 lines · 223 code · 62 blank · 1 comment · 47 complexity · 3521da852a1b3a452b1b1d4bea5052f4 MD5 · raw file

  1. <?php
  2. class WPCF7_ConfigValidator {
  3. const error_maybe_empty = 101;
  4. const error_invalid_syntax = 102;
  5. const error_email_not_in_site_domain = 103;
  6. const error_html_in_message = 104;
  7. private $contact_form;
  8. private $errors = array();
  9. public function __construct( WPCF7_ContactForm $contact_form ) {
  10. $this->contact_form = $contact_form;
  11. $this->errors = (array) get_post_meta(
  12. $this->contact_form->id(), '_config_errors', true );
  13. $this->errors = array_filter( $this->errors );
  14. }
  15. public function is_valid() {
  16. return ! $this->errors;
  17. }
  18. public function get_errors() {
  19. return $this->errors;
  20. }
  21. public function get_error( $section ) {
  22. if ( isset( $this->errors[$section] ) ) {
  23. return $this->errors[$section];
  24. }
  25. return null;
  26. }
  27. public function get_error_message( $section ) {
  28. $code = $this->get_error( $section );
  29. switch ( $code ) {
  30. case self::error_maybe_empty:
  31. return __( "This field can be empty depending on user input.", 'contact-form-7' );
  32. case self::error_invalid_syntax:
  33. return __( "This field has syntax errors.", 'contact-form-7' );
  34. case self::error_email_not_in_site_domain:
  35. return __( "This email address does not belong to the same domain as the site.", 'contact-form-7' );
  36. case self::error_html_in_message:
  37. return __( "HTML tags are not allowed in a message.", 'contact-form-7' );
  38. default:
  39. return '';
  40. }
  41. }
  42. private function add_error( $section, $error ) {
  43. $this->errors[$section] = $error;
  44. }
  45. public function validate() {
  46. $this->errors = array();
  47. $this->validate_mail( 'mail' );
  48. $this->validate_mail( 'mail_2' );
  49. $this->validate_messages();
  50. delete_post_meta( $this->contact_form->id(), '_config_errors' );
  51. if ( $this->errors ) {
  52. update_post_meta( $this->contact_form->id(), '_config_errors',
  53. $this->errors );
  54. return false;
  55. }
  56. return true;
  57. }
  58. public function validate_mail( $template = 'mail' ) {
  59. $components = (array) $this->contact_form->prop( $template );
  60. if ( ! $components ) {
  61. return;
  62. }
  63. if ( 'mail' != $template && empty( $components['active'] ) ) {
  64. return;
  65. }
  66. $components = wp_parse_args( $components, array(
  67. 'subject' => '',
  68. 'sender' => '',
  69. 'recipient' => '',
  70. 'additional_headers' => '',
  71. 'body' => '' ) );
  72. $callback = array( $this, 'replace_mail_tags_with_minimum_input' );
  73. $subject = $components['subject'];
  74. $subject = new WPCF7_MailTaggedText( $subject,
  75. array( 'callback' => $callback ) );
  76. $subject = $subject->replace_tags();
  77. $subject = wpcf7_strip_newline( $subject );
  78. if ( '' === $subject ) {
  79. $this->add_error( sprintf( '%s.subject', $template ),
  80. self::error_maybe_empty );
  81. }
  82. $sender = $components['sender'];
  83. $sender = new WPCF7_MailTaggedText( $sender,
  84. array( 'callback' => $callback ) );
  85. $sender = $sender->replace_tags();
  86. $sender = wpcf7_strip_newline( $sender );
  87. if ( ! wpcf7_is_mailbox_list( $sender ) ) {
  88. $this->add_error( sprintf( '%s.sender', $template ),
  89. self::error_invalid_syntax );
  90. } elseif ( ! wpcf7_is_email_in_site_domain( $sender ) ) {
  91. $this->add_error( sprintf( '%s.sender', $template ),
  92. self::error_email_not_in_site_domain );
  93. }
  94. $recipient = $components['recipient'];
  95. $recipient = new WPCF7_MailTaggedText( $recipient,
  96. array( 'callback' => $callback ) );
  97. $recipient = $recipient->replace_tags();
  98. $recipient = wpcf7_strip_newline( $recipient );
  99. if ( ! wpcf7_is_mailbox_list( $recipient ) ) {
  100. $this->add_error( sprintf( '%s.recipient', $template ),
  101. self::error_invalid_syntax );
  102. }
  103. $additional_headers = $components['additional_headers'];
  104. $additional_headers = new WPCF7_MailTaggedText( $additional_headers,
  105. array( 'callback' => $callback ) );
  106. $additional_headers = $additional_headers->replace_tags();
  107. if ( ! $this->test_additional_headers_syntax( $additional_headers ) ) {
  108. $this->add_error( sprintf( '%s.additional_headers', $template ),
  109. self::error_invalid_syntax );
  110. }
  111. $body = $components['body'];
  112. $body = new WPCF7_MailTaggedText( $body,
  113. array( 'callback' => $callback ) );
  114. $body = $body->replace_tags();
  115. if ( '' === $body ) {
  116. $this->add_error( sprintf( '%s.body', $template ),
  117. self::error_maybe_empty );
  118. }
  119. }
  120. public function test_additional_headers_syntax( $content ) {
  121. $headers = explode( "\n", $content );
  122. foreach ( $headers as $header ) {
  123. $header = trim( $header );
  124. if ( '' === $header ) {
  125. continue;
  126. }
  127. if ( ! preg_match( '/^([0-9A-Za-z-]+):(.+)$/', $header, $matches ) ) {
  128. return false;
  129. }
  130. $is_mailbox_list_field = in_array( strtolower( $matches[1] ),
  131. array( 'reply-to', 'cc', 'bcc' ) );
  132. if ( $is_mailbox_list_field
  133. && ! wpcf7_is_mailbox_list( $matches[2] ) ) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. public function validate_messages() {
  140. $messages = (array) $this->contact_form->prop( 'messages' );
  141. if ( ! $messages ) {
  142. return;
  143. }
  144. if ( isset( $messages['captcha_not_match'] )
  145. && ! wpcf7_use_really_simple_captcha() ) {
  146. unset( $messages['captcha_not_match'] );
  147. }
  148. foreach ( $messages as $key => $message ) {
  149. $stripped = wp_strip_all_tags( $message );
  150. if ( $stripped != $message ) {
  151. $this->add_error( sprintf( 'messages.%s', $key ),
  152. self::error_html_in_message );
  153. }
  154. }
  155. }
  156. public function replace_mail_tags_with_minimum_input( $matches ) {
  157. // allow [[foo]] syntax for escaping a tag
  158. if ( $matches[1] == '[' && $matches[4] == ']' ) {
  159. return substr( $matches[0], 1, -1 );
  160. }
  161. $tag = $matches[0];
  162. $tagname = $matches[2];
  163. $values = $matches[3];
  164. if ( ! empty( $values ) ) {
  165. preg_match_all( '/"[^"]*"|\'[^\']*\'/', $values, $matches );
  166. $values = wpcf7_strip_quote_deep( $matches[0] );
  167. }
  168. $do_not_heat = false;
  169. if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) {
  170. $tagname = trim( $matches[1] );
  171. $do_not_heat = true;
  172. }
  173. $format = '';
  174. if ( preg_match( '/^_format_(.+)$/', $tagname, $matches ) ) {
  175. $tagname = trim( $matches[1] );
  176. $format = $values[0];
  177. }
  178. $example_email = 'example@example.com';
  179. $example_text = 'example';
  180. $example_blank = '';
  181. $form_tags = $this->contact_form->form_scan_shortcode(
  182. array( 'name' => $tagname ) );
  183. if ( $form_tags ) {
  184. $form_tag = new WPCF7_Shortcode( $form_tags[0] );
  185. $is_required = ( $form_tag->is_required() || 'radio' == $form_tag->type );
  186. if ( ! $is_required ) {
  187. return $example_blank;
  188. }
  189. $is_selectable = in_array( $form_tag->basetype,
  190. array( 'radio', 'checkbox', 'select' ) );
  191. if ( $is_selectable ) {
  192. if ( $form_tag->pipes instanceof WPCF7_Pipes ) {
  193. if ( $do_not_heat ) {
  194. $before_pipes = $form_tag->pipes->collect_befores();
  195. $last_item = array_pop( $before_pipes );
  196. } else {
  197. $after_pipes = $form_tag->pipes->collect_afters();
  198. $last_item = array_pop( $after_pipes );
  199. }
  200. } else {
  201. $last_item = array_pop( $form_tag->values );
  202. }
  203. if ( $last_item && wpcf7_is_mailbox_list( $last_item ) ) {
  204. return $example_email;
  205. } else {
  206. return $example_text;
  207. }
  208. }
  209. if ( 'email' == $form_tag->basetype ) {
  210. return $example_email;
  211. } else {
  212. return $example_text;
  213. }
  214. } else {
  215. $tagname = preg_replace( '/^wpcf7\./', '_', $tagname ); // for back-compat
  216. if ( '_post_author_email' == $tagname ) {
  217. return $example_email;
  218. } elseif ( '_' == substr( $tagname, 0, 1 ) ) { // maybe special mail tag
  219. return $example_text;
  220. }
  221. }
  222. return $tag;
  223. }
  224. }