= 3 && $age <= DAY_IN_SECONDS; } /** * "48.86, 2.29" or "48.86 2.29" to [lat, lng], or null. */ function parse_coords( string $raw ): ?array { if ( ! preg_match( '/(-?\d{1,2}(?:\.\d+)?)\s*[,;\s]\s*(-?\d{1,3}(?:\.\d+)?)/', $raw, $m ) ) { return null; } $lat = (float) $m[1]; $lng = (float) $m[2]; if ( $lat < -90 || $lat > 90 || $lng < -180 || $lng > 180 ) { return null; } return array( round( $lat, 6 ), round( $lng, 6 ) ); } add_action( 'admin_post_nopriv_raplace_submit', __NAMESPACE__ . '\handle_submit' ); add_action( 'admin_post_raplace_submit', __NAMESPACE__ . '\handle_submit' ); function handle_submit(): void { // phpcs:disable WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended -- anonymous form, see file header. // In the URL rather than the form, so it is still there when PHP drops an oversized form. $wants_json = isset( $_GET['ajax'] ); $back = wp_get_referer() ? wp_get_referer() : home_url( '/' ); $anchor = sanitize_html_class( wp_unslash( $_POST['form_id'] ?? '' ) ); // $note: an extra line for the visitor, such as a photo that was skipped. $respond = static function ( bool $ok, string $message, int $status = 200, string $note = '' ) use ( $wants_json, $back, $anchor ) { if ( $wants_json ) { $body = array( 'message' => $message, 'note' => $note, ); $ok ? wp_send_json_success( $body ) : wp_send_json_error( $body, $status ); } $url = add_query_arg( 'raplace', $ok ? 'sent' : 'error', remove_query_arg( 'raplace', $back ) ); wp_safe_redirect( $url . ( $anchor ? '#' . $anchor : '' ) ); exit; }; // Above post_max_size PHP empties the form and the files, so nothing else can be checked. $limit = max_request_bytes(); if ( $limit && absint( wp_unslash( $_SERVER['CONTENT_LENGTH'] ?? 0 ) ) > $limit ) { $respond( false, photos_too_large(), 413 ); } // Bots fill the hidden field; pretend it worked. if ( ! empty( $_POST['website'] ) ) { $respond( true, __( 'Thank you! Your report has been sent.', 'spotsend' ) ); } if ( ! token_ok( sanitize_text_field( wp_unslash( $_POST['t'] ?? '' ) ) ) ) { $respond( false, __( 'This form has expired. Reload the page and try again.', 'spotsend' ), 400 ); } $ip_key = 'raplace_rl_' . md5( sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? '' ) ) ); $hits = (int) get_transient( $ip_key ); if ( $hits >= (int) setting( 'per_hour' ) ) { $respond( false, __( 'Too many reports. Please try again in an hour.', 'spotsend' ), 429 ); } $place = mb_substr( sanitize_text_field( wp_unslash( $_POST['place'] ?? '' ) ), 0, 200 ); if ( '' === $place ) { $respond( false, __( 'Please say where the place is.', 'spotsend' ), 422 ); } $chosen = array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['category'] ?? array() ) ); $categories = array_values( array_intersect( categories(), $chosen ) ); $desc = mb_substr( sanitize_textarea_field( wp_unslash( $_POST['description'] ?? '' ) ), 0, 2000 ); $reporter = mb_substr( sanitize_text_field( wp_unslash( $_POST['reporter'] ?? '' ) ), 0, 200 ); $email = sanitize_email( wp_unslash( $_POST['email'] ?? '' ) ); $email = is_email( $email ) ? $email : ''; $lat = sanitize_text_field( wp_unslash( $_POST['lat'] ?? '' ) ); $lng = sanitize_text_field( wp_unslash( $_POST['lng'] ?? '' ) ); $geo = parse_coords( '' !== $lat ? "$lat, $lng" : $place ); // phpcs:enable $post_id = wp_insert_post( array( 'post_type' => POST_TYPE, 'post_status' => 'pending', 'post_title' => $place, ), true ); if ( is_wp_error( $post_id ) ) { $respond( false, __( 'The report could not be saved. Please try again.', 'spotsend' ), 500 ); } update_post_meta( $post_id, 'raplace_categories', $categories ); update_post_meta( $post_id, 'raplace_description', $desc ); update_post_meta( $post_id, 'raplace_reporter', $reporter ); if ( '' !== $email ) { update_post_meta( $post_id, 'raplace_email', $email ); } if ( $geo ) { update_post_meta( $post_id, 'raplace_lat', $geo[0] ); update_post_meta( $post_id, 'raplace_lng', $geo[1] ); } $photo_note = attach_photos( $post_id ); set_transient( $ip_key, $hits + 1, HOUR_IN_SECONDS ); notify( $post_id ); confirm( $post_id ); /** * Fires after a visitor's report is saved and the emails sent. * * @param int $post_id The new report. */ do_action( 'spotsend_submitted', $post_id ); $respond( true, __( 'Thank you! Your report has been sent.', 'spotsend' ), 200, $photo_note ); } function photos_too_large(): string { return __( 'The photos are too large together. Choose fewer or smaller photos and try again.', 'spotsend' ); } /** * Saves the uploaded photos to the report, up to max_photos(), each as its own `raplace_photo` * row. Returns a note for the visitor when some were skipped. */ function attach_photos( int $post_id ): string { // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput -- see file header; checked by media_handle_upload(). $upload = $_FILES['photo'] ?? array(); // One entry per file from the nested photo[] arrays, without the empty one an unused input sends. $files = array(); foreach ( (array) ( $upload['error'] ?? array() ) as $i => $error ) { if ( UPLOAD_ERR_NO_FILE !== $error ) { $files[] = array( 'name' => (string) ( $upload['name'][ $i ] ?? '' ), 'type' => (string) ( $upload['type'][ $i ] ?? '' ), 'tmp_name' => (string) ( $upload['tmp_name'][ $i ] ?? '' ), 'error' => (int) $error, 'size' => (int) ( $upload['size'][ $i ] ?? 0 ), ); } } if ( ! $files ) { return ''; } require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $max = max_photos(); $over_limit = max( 0, count( $files ) - $max ); $too_large = 0; $wrong_type = 0; foreach ( array_slice( $files, 0, $max ) as $file ) { if ( UPLOAD_ERR_OK !== $file['error'] || $file['size'] > max_bytes() ) { ++$too_large; continue; } // Location and camera details go before WordPress stores the file, or the photo is not kept. if ( ! is_uploaded_file( $file['tmp_name'] ) || ! strip_photo_meta( $file['tmp_name'] ) ) { ++$wrong_type; continue; } // media_handle_upload() reads one file from $_FILES by its key. $_FILES['raplace_photo'] = $file; $attachment_id = media_handle_upload( 'raplace_photo', $post_id, array(), array( 'test_form' => false, 'mimes' => array( 'jpg|jpeg|jpe' => 'image/jpeg', 'png' => 'image/png', 'webp' => 'image/webp', ), ) ); if ( is_wp_error( $attachment_id ) ) { ++$wrong_type; continue; } add_post_meta( $post_id, 'raplace_photo', $attachment_id ); } $notes = array(); if ( $too_large ) { /* translators: 1: number of photos, 2: largest allowed photo size, e.g. "8 MB" */ $notes[] = sprintf( _n( '%1$d photo was not added: it is larger than %2$s.', '%1$d photos were not added: they are larger than %2$s.', $too_large, 'spotsend' ), $too_large, size_format( max_bytes() ) ); } if ( $wrong_type ) { /* translators: %d: number of photos */ $notes[] = sprintf( _n( '%d photo was not added: use JPG, PNG or WEBP.', '%d photos were not added: use JPG, PNG or WEBP.', $wrong_type, 'spotsend' ), $wrong_type ); } if ( $over_limit ) { /* translators: 1: number of photos left out, 2: how many photos a report takes */ $notes[] = sprintf( _n( '%1$d photo was not added: a report takes up to %2$d.', '%1$d photos were not added: a report takes up to %2$d.', $over_limit, 'spotsend' ), $over_limit, $max ); } return implode( ' ', $notes ); } function notify( int $post_id ): void { $report = get_report( $post_id ); $none = __( 'not given', 'spotsend' ); $lines = array( __( 'A new report has arrived.', 'spotsend' ), '', __( 'Place:', 'spotsend' ) . ' ' . get_the_title( $post_id ), __( 'Categories:', 'spotsend' ) . ' ' . ( $report['categories'] ? implode( ', ', $report['categories'] ) : $none ), __( 'Description:', 'spotsend' ) . ' ' . ( '' !== $report['description'] ? $report['description'] : $none ), __( 'Reported by:', 'spotsend' ) . ' ' . ( '' !== $report['reporter'] ? $report['reporter'] : $none ), __( 'Email:', 'spotsend' ) . ' ' . ( '' !== $report['email'] ? $report['email'] : $none ), ); if ( '' !== $report['lat'] ) { $lines[] = __( 'On the map:', 'spotsend' ) . ' ' . map_link( $report['lat'], $report['lng'] ); } if ( $report['photos'] ) { $lines[] = __( 'Photos:', 'spotsend' ); foreach ( $report['photos'] as $photo ) { $lines[] = (string) wp_get_attachment_url( $photo ); } } $lines[] = ''; $lines[] = __( 'Open:', 'spotsend' ) . ' ' . admin_url( 'post.php?post=' . $post_id . '&action=edit' ); /* translators: %s: the place as the visitor described it */ $subject = sprintf( __( 'New report: %s', 'spotsend' ), get_the_title( $post_id ) ); // Replying to the notification reaches the visitor. The address passed is_email() on submit. $headers = '' !== $report['email'] ? array( 'Reply-To: ' . $report['email'] ) : array(); wp_mail( recipient(), $subject, implode( "\n", $lines ), $headers ); } /** * Tells the visitor their report arrived, when they gave an email address and confirmations are on. * * The anonymous form must not become a way to send arbitrary text from this site, so the email * holds only the site's own wording and the report number, never what the visitor typed. */ function confirm( int $post_id ): void { $email = get_report( $post_id )['email']; if ( '' === $email || ! setting( 'confirm' ) ) { return; } $site = wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ); $lines = array( confirm_message(), '', /* translators: %d: the report's number */ sprintf( __( 'Report number: %d', 'spotsend' ), $post_id ), '', $site, home_url( '/' ), ); wp_mail( $email, confirm_subject(), implode( "\n", $lines ) ); }