config-validator.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. <?php
  2. class WPCF7_ConfigValidator {
  3. const error = 100;
  4. const error_maybe_empty = 101;
  5. const error_invalid_mailbox_syntax = 102;
  6. const error_email_not_in_site_domain = 103;
  7. const error_html_in_message = 104;
  8. const error_multiple_controls_in_label = 105;
  9. const error_file_not_found = 106;
  10. const error_unavailable_names = 107;
  11. const error_invalid_mail_header = 108;
  12. const error_deprecated_settings = 109;
  13. const error_file_not_in_content_dir = 110;
  14. const error_unavailable_html_elements = 111;
  15. const error_attachments_overweight = 112;
  16. public static function get_doc_link( $error_code = '' ) {
  17. $url = __( 'https://contactform7.com/configuration-errors/',
  18. 'contact-form-7' );
  19. if ( '' !== $error_code ) {
  20. $error_code = strtr( $error_code, '_', '-' );
  21. $url = sprintf( '%s/%s', untrailingslashit( $url ), $error_code );
  22. }
  23. return esc_url( $url );
  24. }
  25. private $contact_form;
  26. private $errors = array();
  27. public function __construct( WPCF7_ContactForm $contact_form ) {
  28. $this->contact_form = $contact_form;
  29. }
  30. public function contact_form() {
  31. return $this->contact_form;
  32. }
  33. public function is_valid() {
  34. return ! $this->count_errors();
  35. }
  36. public function count_errors( $args = '' ) {
  37. $args = wp_parse_args( $args, array(
  38. 'section' => '',
  39. 'code' => '',
  40. ) );
  41. $count = 0;
  42. foreach ( $this->errors as $key => $errors ) {
  43. if ( preg_match( '/^mail_[0-9]+\.(.*)$/', $key, $matches ) ) {
  44. $key = sprintf( 'mail.%s', $matches[1] );
  45. }
  46. if ( $args['section']
  47. and $key != $args['section']
  48. and preg_replace( '/\..*$/', '', $key, 1 ) != $args['section'] ) {
  49. continue;
  50. }
  51. foreach ( $errors as $error ) {
  52. if ( empty( $error ) ) {
  53. continue;
  54. }
  55. if ( $args['code'] and $error['code'] != $args['code'] ) {
  56. continue;
  57. }
  58. $count += 1;
  59. }
  60. }
  61. return $count;
  62. }
  63. public function collect_error_messages() {
  64. $error_messages = array();
  65. foreach ( $this->errors as $section => $errors ) {
  66. $error_messages[$section] = array();
  67. foreach ( $errors as $error ) {
  68. if ( empty( $error['args']['message'] ) ) {
  69. $message = $this->get_default_message( $error['code'] );
  70. } elseif ( empty( $error['args']['params'] ) ) {
  71. $message = $error['args']['message'];
  72. } else {
  73. $message = $this->build_message(
  74. $error['args']['message'],
  75. $error['args']['params'] );
  76. }
  77. $link = '';
  78. if ( ! empty( $error['args']['link'] ) ) {
  79. $link = $error['args']['link'];
  80. }
  81. $error_messages[$section][] = array(
  82. 'message' => $message,
  83. 'link' => esc_url( $link ),
  84. );
  85. }
  86. }
  87. return $error_messages;
  88. }
  89. public function build_message( $message, $params = '' ) {
  90. $params = wp_parse_args( $params, array() );
  91. foreach ( $params as $key => $val ) {
  92. if ( ! preg_match( '/^[0-9A-Za-z_]+$/', $key ) ) { // invalid key
  93. continue;
  94. }
  95. $placeholder = '%' . $key . '%';
  96. if ( false !== stripos( $message, $placeholder ) ) {
  97. $message = str_ireplace( $placeholder, $val, $message );
  98. }
  99. }
  100. return $message;
  101. }
  102. public function get_default_message( $code ) {
  103. switch ( $code ) {
  104. case self::error_maybe_empty:
  105. return __( "There is a possible empty field.", 'contact-form-7' );
  106. case self::error_invalid_mailbox_syntax:
  107. return __( "Invalid mailbox syntax is used.", 'contact-form-7' );
  108. case self::error_email_not_in_site_domain:
  109. return __( "Sender email address does not belong to the site domain.", 'contact-form-7' );
  110. case self::error_html_in_message:
  111. return __( "HTML tags are used in a message.", 'contact-form-7' );
  112. case self::error_multiple_controls_in_label:
  113. return __( "Multiple form controls are in a single label element.", 'contact-form-7' );
  114. case self::error_invalid_mail_header:
  115. return __( "There are invalid mail header fields.", 'contact-form-7' );
  116. case self::error_deprecated_settings:
  117. return __( "Deprecated settings are used.", 'contact-form-7' );
  118. default:
  119. return '';
  120. }
  121. }
  122. public function add_error( $section, $code, $args = '' ) {
  123. $args = wp_parse_args( $args, array(
  124. 'message' => '',
  125. 'params' => array(),
  126. ) );
  127. if ( ! isset( $this->errors[$section] ) ) {
  128. $this->errors[$section] = array();
  129. }
  130. $this->errors[$section][] = array( 'code' => $code, 'args' => $args );
  131. return true;
  132. }
  133. public function remove_error( $section, $code ) {
  134. if ( empty( $this->errors[$section] ) ) {
  135. return;
  136. }
  137. foreach ( (array) $this->errors[$section] as $key => $error ) {
  138. if ( isset( $error['code'] )
  139. and $error['code'] == $code ) {
  140. unset( $this->errors[$section][$key] );
  141. }
  142. }
  143. if ( empty( $this->errors[$section] ) ) {
  144. unset( $this->errors[$section] );
  145. }
  146. }
  147. public function validate() {
  148. $this->errors = array();
  149. $this->validate_form();
  150. $this->validate_mail( 'mail' );
  151. $this->validate_mail( 'mail_2' );
  152. $this->validate_messages();
  153. $this->validate_additional_settings();
  154. do_action( 'wpcf7_config_validator_validate', $this );
  155. return $this->is_valid();
  156. }
  157. public function save() {
  158. if ( $this->contact_form->initial() ) {
  159. return;
  160. }
  161. delete_post_meta( $this->contact_form->id(), '_config_errors' );
  162. if ( $this->errors ) {
  163. update_post_meta( $this->contact_form->id(), '_config_errors',
  164. $this->errors );
  165. }
  166. }
  167. public function restore() {
  168. $config_errors = get_post_meta(
  169. $this->contact_form->id(), '_config_errors', true );
  170. foreach ( (array) $config_errors as $section => $errors ) {
  171. if ( empty( $errors ) ) {
  172. continue;
  173. }
  174. if ( ! is_array( $errors ) ) { // for back-compat
  175. $code = $errors;
  176. $this->add_error( $section, $code );
  177. } else {
  178. foreach ( (array) $errors as $error ) {
  179. if ( ! empty( $error['code'] ) ) {
  180. $code = $error['code'];
  181. $args = isset( $error['args'] ) ? $error['args'] : '';
  182. $this->add_error( $section, $code, $args );
  183. }
  184. }
  185. }
  186. }
  187. }
  188. public function replace_mail_tags_with_minimum_input( $matches ) {
  189. // allow [[foo]] syntax for escaping a tag
  190. if ( $matches[1] == '[' && $matches[4] == ']' ) {
  191. return substr( $matches[0], 1, -1 );
  192. }
  193. $tag = $matches[0];
  194. $tagname = $matches[2];
  195. $values = $matches[3];
  196. $mail_tag = new WPCF7_MailTag( $tag, $tagname, $values );
  197. $field_name = $mail_tag->field_name();
  198. $example_email = 'example@example.com';
  199. $example_text = 'example';
  200. $example_blank = '';
  201. $form_tags = $this->contact_form->scan_form_tags(
  202. array( 'name' => $field_name ) );
  203. if ( $form_tags ) {
  204. $form_tag = new WPCF7_FormTag( $form_tags[0] );
  205. $is_required = ( $form_tag->is_required() || 'radio' == $form_tag->type );
  206. if ( ! $is_required ) {
  207. return $example_blank;
  208. }
  209. if ( wpcf7_form_tag_supports( $form_tag->type, 'selectable-values' ) ) {
  210. if ( $form_tag->pipes instanceof WPCF7_Pipes ) {
  211. if ( $mail_tag->get_option( 'do_not_heat' ) ) {
  212. $before_pipes = $form_tag->pipes->collect_befores();
  213. $last_item = array_pop( $before_pipes );
  214. } else {
  215. $after_pipes = $form_tag->pipes->collect_afters();
  216. $last_item = array_pop( $after_pipes );
  217. }
  218. } else {
  219. $last_item = array_pop( $form_tag->values );
  220. }
  221. if ( $last_item and wpcf7_is_mailbox_list( $last_item ) ) {
  222. return $example_email;
  223. } else {
  224. return $example_text;
  225. }
  226. }
  227. if ( 'email' == $form_tag->basetype ) {
  228. return $example_email;
  229. } else {
  230. return $example_text;
  231. }
  232. } else { // maybe special mail tag
  233. // for back-compat
  234. $field_name = preg_replace( '/^wpcf7\./', '_', $field_name );
  235. if ( '_site_admin_email' == $field_name ) {
  236. return get_bloginfo( 'admin_email', 'raw' );
  237. } elseif ( '_user_agent' == $field_name ) {
  238. return $example_text;
  239. } elseif ( '_user_email' == $field_name ) {
  240. return $this->contact_form->is_true( 'subscribers_only' )
  241. ? $example_email
  242. : $example_blank;
  243. } elseif ( '_user_' == substr( $field_name, 0, 6 ) ) {
  244. return $this->contact_form->is_true( 'subscribers_only' )
  245. ? $example_text
  246. : $example_blank;
  247. } elseif ( '_' == substr( $field_name, 0, 1 ) ) {
  248. return '_email' == substr( $field_name, -6 )
  249. ? $example_email
  250. : $example_text;
  251. }
  252. }
  253. return $tag;
  254. }
  255. public function validate_form() {
  256. $section = 'form.body';
  257. $form = $this->contact_form->prop( 'form' );
  258. $this->detect_multiple_controls_in_label( $section, $form );
  259. $this->detect_unavailable_names( $section, $form );
  260. $this->detect_unavailable_html_elements( $section, $form );
  261. }
  262. public function detect_multiple_controls_in_label( $section, $content ) {
  263. $pattern = '%<label(?:[ \t\n]+.*?)?>(.+?)</label>%s';
  264. if ( preg_match_all( $pattern, $content, $matches ) ) {
  265. $form_tags_manager = WPCF7_FormTagsManager::get_instance();
  266. foreach ( $matches[1] as $insidelabel ) {
  267. $tags = $form_tags_manager->scan( $insidelabel );
  268. $fields_count = 0;
  269. foreach ( $tags as $tag ) {
  270. $is_multiple_controls_container = wpcf7_form_tag_supports(
  271. $tag->type, 'multiple-controls-container' );
  272. $is_zero_controls_container = wpcf7_form_tag_supports(
  273. $tag->type, 'zero-controls-container' );
  274. if ( $is_multiple_controls_container ) {
  275. $fields_count += count( $tag->values );
  276. if ( $tag->has_option( 'free_text' ) ) {
  277. $fields_count += 1;
  278. }
  279. } elseif ( $is_zero_controls_container ) {
  280. $fields_count += 0;
  281. } elseif ( ! empty( $tag->name ) ) {
  282. $fields_count += 1;
  283. }
  284. if ( 1 < $fields_count ) {
  285. return $this->add_error( $section,
  286. self::error_multiple_controls_in_label, array(
  287. 'link' => self::get_doc_link( 'multiple_controls_in_label' ),
  288. )
  289. );
  290. }
  291. }
  292. }
  293. }
  294. return false;
  295. }
  296. public function detect_unavailable_names( $section, $content ) {
  297. $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat',
  298. 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence',
  299. 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order',
  300. 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second',
  301. 'name', 'category_name', 'tag', 'feed', 'author_name', 'static',
  302. 'pagename', 'page_id', 'error', 'attachment', 'attachment_id',
  303. 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term',
  304. 'cpage', 'post_type', 'embed' );
  305. $form_tags_manager = WPCF7_FormTagsManager::get_instance();
  306. $ng_named_tags = $form_tags_manager->filter( $content,
  307. array( 'name' => $public_query_vars ) );
  308. $ng_names = array();
  309. foreach ( $ng_named_tags as $tag ) {
  310. $ng_names[] = sprintf( '"%s"', $tag->name );
  311. }
  312. if ( $ng_names ) {
  313. $ng_names = array_unique( $ng_names );
  314. return $this->add_error( $section,
  315. self::error_unavailable_names,
  316. array(
  317. 'message' =>
  318. /* translators: %names%: a list of form control names */
  319. __( "Unavailable names (%names%) are used for form controls.", 'contact-form-7' ),
  320. 'params' => array( 'names' => implode( ', ', $ng_names ) ),
  321. 'link' => self::get_doc_link( 'unavailable_names' ),
  322. )
  323. );
  324. }
  325. return false;
  326. }
  327. public function detect_unavailable_html_elements( $section, $content ) {
  328. $pattern = '%(?:<form[\s\t>]|</form>)%i';
  329. if ( preg_match( $pattern, $content ) ) {
  330. return $this->add_error( $section,
  331. self::error_unavailable_html_elements,
  332. array(
  333. 'message' => __( "Unavailable HTML elements are used in the form template.", 'contact-form-7' ),
  334. 'link' => self::get_doc_link( 'unavailable_html_elements' ),
  335. )
  336. );
  337. }
  338. return false;
  339. }
  340. public function validate_mail( $template = 'mail' ) {
  341. $components = (array) $this->contact_form->prop( $template );
  342. if ( ! $components ) {
  343. return;
  344. }
  345. if ( 'mail' != $template
  346. and empty( $components['active'] ) ) {
  347. return;
  348. }
  349. $components = wp_parse_args( $components, array(
  350. 'subject' => '',
  351. 'sender' => '',
  352. 'recipient' => '',
  353. 'additional_headers' => '',
  354. 'body' => '',
  355. 'attachments' => '',
  356. ) );
  357. $callback = array( $this, 'replace_mail_tags_with_minimum_input' );
  358. $subject = $components['subject'];
  359. $subject = new WPCF7_MailTaggedText( $subject,
  360. array( 'callback' => $callback ) );
  361. $subject = $subject->replace_tags();
  362. $subject = wpcf7_strip_newline( $subject );
  363. $this->detect_maybe_empty( sprintf( '%s.subject', $template ), $subject );
  364. $sender = $components['sender'];
  365. $sender = new WPCF7_MailTaggedText( $sender,
  366. array( 'callback' => $callback ) );
  367. $sender = $sender->replace_tags();
  368. $sender = wpcf7_strip_newline( $sender );
  369. if ( ! $this->detect_invalid_mailbox_syntax( sprintf( '%s.sender', $template ), $sender )
  370. and ! wpcf7_is_email_in_site_domain( $sender ) ) {
  371. $this->add_error( sprintf( '%s.sender', $template ),
  372. self::error_email_not_in_site_domain, array(
  373. 'link' => self::get_doc_link( 'email_not_in_site_domain' ),
  374. )
  375. );
  376. }
  377. $recipient = $components['recipient'];
  378. $recipient = new WPCF7_MailTaggedText( $recipient,
  379. array( 'callback' => $callback ) );
  380. $recipient = $recipient->replace_tags();
  381. $recipient = wpcf7_strip_newline( $recipient );
  382. $this->detect_invalid_mailbox_syntax(
  383. sprintf( '%s.recipient', $template ), $recipient );
  384. $additional_headers = $components['additional_headers'];
  385. $additional_headers = new WPCF7_MailTaggedText( $additional_headers,
  386. array( 'callback' => $callback ) );
  387. $additional_headers = $additional_headers->replace_tags();
  388. $additional_headers = explode( "\n", $additional_headers );
  389. $mailbox_header_types = array( 'reply-to', 'cc', 'bcc' );
  390. $invalid_mail_header_exists = false;
  391. foreach ( $additional_headers as $header ) {
  392. $header = trim( $header );
  393. if ( '' === $header ) {
  394. continue;
  395. }
  396. if ( ! preg_match( '/^([0-9A-Za-z-]+):(.*)$/', $header, $matches ) ) {
  397. $invalid_mail_header_exists = true;
  398. } else {
  399. $header_name = $matches[1];
  400. $header_value = trim( $matches[2] );
  401. if ( in_array( strtolower( $header_name ), $mailbox_header_types ) ) {
  402. $this->detect_invalid_mailbox_syntax(
  403. sprintf( '%s.additional_headers', $template ),
  404. $header_value, array(
  405. 'message' =>
  406. __( "Invalid mailbox syntax is used in the %name% field.", 'contact-form-7' ),
  407. 'params' => array( 'name' => $header_name ) ) );
  408. } elseif ( empty( $header_value ) ) {
  409. $invalid_mail_header_exists = true;
  410. }
  411. }
  412. }
  413. if ( $invalid_mail_header_exists ) {
  414. $this->add_error( sprintf( '%s.additional_headers', $template ),
  415. self::error_invalid_mail_header, array(
  416. 'link' => self::get_doc_link( 'invalid_mail_header' ),
  417. )
  418. );
  419. }
  420. $body = $components['body'];
  421. $body = new WPCF7_MailTaggedText( $body,
  422. array( 'callback' => $callback ) );
  423. $body = $body->replace_tags();
  424. $this->detect_maybe_empty( sprintf( '%s.body', $template ), $body );
  425. if ( '' !== $components['attachments'] ) {
  426. $attachables = array();
  427. $tags = $this->contact_form->scan_form_tags(
  428. array( 'type' => array( 'file', 'file*' ) )
  429. );
  430. foreach ( $tags as $tag ) {
  431. $name = $tag->name;
  432. if ( false === strpos( $components['attachments'], "[{$name}]" ) ) {
  433. continue;
  434. }
  435. $limit = (int) $tag->get_limit_option();
  436. if ( empty( $attachables[$name] )
  437. or $attachables[$name] < $limit ) {
  438. $attachables[$name] = $limit;
  439. }
  440. }
  441. $total_size = array_sum( $attachables );
  442. $has_file_not_found = false;
  443. $has_file_not_in_content_dir = false;
  444. foreach ( explode( "\n", $components['attachments'] ) as $line ) {
  445. $line = trim( $line );
  446. if ( '' === $line
  447. or '[' == substr( $line, 0, 1 ) ) {
  448. continue;
  449. }
  450. $has_file_not_found = $this->detect_file_not_found(
  451. sprintf( '%s.attachments', $template ), $line
  452. );
  453. if ( ! $has_file_not_found
  454. and ! $has_file_not_in_content_dir ) {
  455. $has_file_not_in_content_dir = $this->detect_file_not_in_content_dir(
  456. sprintf( '%s.attachments', $template ), $line
  457. );
  458. }
  459. if ( ! $has_file_not_found ) {
  460. $path = path_join( WP_CONTENT_DIR, $line );
  461. $total_size += (int) @filesize( $path );
  462. }
  463. }
  464. $max = 25 * 1024 * 1024; // 25 MB
  465. if ( $max < $total_size ) {
  466. $this->add_error( sprintf( '%s.attachments', $template ),
  467. self::error_attachments_overweight,
  468. array(
  469. 'message' => __( "The total size of attachment files is too large.", 'contact-form-7' ),
  470. 'link' => self::get_doc_link( 'attachments_overweight' ),
  471. )
  472. );
  473. }
  474. }
  475. }
  476. public function detect_invalid_mailbox_syntax( $section, $content, $args = '' ) {
  477. $args = wp_parse_args( $args, array(
  478. 'link' => self::get_doc_link( 'invalid_mailbox_syntax' ),
  479. 'message' => '',
  480. 'params' => array(),
  481. ) );
  482. if ( ! wpcf7_is_mailbox_list( $content ) ) {
  483. return $this->add_error( $section,
  484. self::error_invalid_mailbox_syntax, $args );
  485. }
  486. return false;
  487. }
  488. public function detect_maybe_empty( $section, $content ) {
  489. if ( '' === $content ) {
  490. return $this->add_error( $section,
  491. self::error_maybe_empty, array(
  492. 'link' => self::get_doc_link( 'maybe_empty' ),
  493. )
  494. );
  495. }
  496. return false;
  497. }
  498. public function detect_file_not_found( $section, $content ) {
  499. $path = path_join( WP_CONTENT_DIR, $content );
  500. if ( ! is_readable( $path )
  501. or ! is_file( $path ) ) {
  502. return $this->add_error( $section,
  503. self::error_file_not_found,
  504. array(
  505. 'message' =>
  506. __( "Attachment file does not exist at %path%.", 'contact-form-7' ),
  507. 'params' => array( 'path' => $content ),
  508. 'link' => self::get_doc_link( 'file_not_found' ),
  509. )
  510. );
  511. }
  512. return false;
  513. }
  514. public function detect_file_not_in_content_dir( $section, $content ) {
  515. $path = path_join( WP_CONTENT_DIR, $content );
  516. if ( ! wpcf7_is_file_path_in_content_dir( $path ) ) {
  517. return $this->add_error( $section,
  518. self::error_file_not_in_content_dir,
  519. array(
  520. 'message' =>
  521. __( "It is not allowed to use files outside the wp-content directory.", 'contact-form-7' ),
  522. 'link' => self::get_doc_link( 'file_not_in_content_dir' ),
  523. )
  524. );
  525. }
  526. return false;
  527. }
  528. public function validate_messages() {
  529. $messages = (array) $this->contact_form->prop( 'messages' );
  530. if ( ! $messages ) {
  531. return;
  532. }
  533. if ( isset( $messages['captcha_not_match'] )
  534. and ! wpcf7_use_really_simple_captcha() ) {
  535. unset( $messages['captcha_not_match'] );
  536. }
  537. foreach ( $messages as $key => $message ) {
  538. $section = sprintf( 'messages.%s', $key );
  539. $this->detect_html_in_message( $section, $message );
  540. }
  541. }
  542. public function detect_html_in_message( $section, $content ) {
  543. $stripped = wp_strip_all_tags( $content );
  544. if ( $stripped != $content ) {
  545. return $this->add_error( $section,
  546. self::error_html_in_message,
  547. array(
  548. 'link' => self::get_doc_link( 'html_in_message' ),
  549. )
  550. );
  551. }
  552. return false;
  553. }
  554. public function validate_additional_settings() {
  555. $deprecated_settings_used =
  556. $this->contact_form->additional_setting( 'on_sent_ok' ) ||
  557. $this->contact_form->additional_setting( 'on_submit' );
  558. if ( $deprecated_settings_used ) {
  559. return $this->add_error( 'additional_settings.body',
  560. self::error_deprecated_settings,
  561. array(
  562. 'link' => self::get_doc_link( 'deprecated_settings' ),
  563. )
  564. );
  565. }
  566. }
  567. }