| Conditions | 32 |
| Paths | > 20000 |
| Total Lines | 194 |
| Code Lines | 110 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 17 | function validateUrlSyntax($urladdr, $options = '') |
||
| 18 | { |
||
| 19 | // Force Options parameter to be lower case |
||
| 20 | // DISABLED PERMAMENTLY - OK to remove from code |
||
| 21 | // $options = strtolower($options); |
||
| 22 | |||
| 23 | // Check Options Parameter |
||
| 24 | if (!preg_match('/^([sHSEFRuPaIpfqr][+?-])*$/', $options)) { |
||
| 25 | trigger_error('Options attribute malformed', E_USER_ERROR); |
||
| 26 | } |
||
| 27 | |||
| 28 | // Set Options Array, set defaults if options are not specified |
||
| 29 | // Scheme |
||
| 30 | if (!str_contains($options, 's')) { |
||
| 31 | $aOptions['s'] = '?'; |
||
|
|
|||
| 32 | } else { |
||
| 33 | $aOptions['s'] = substr($options, strpos($options, 's') + 1, 1); |
||
| 34 | } |
||
| 35 | // http:// |
||
| 36 | if (!str_contains($options, 'H')) { |
||
| 37 | $aOptions['H'] = '?'; |
||
| 38 | } else { |
||
| 39 | $aOptions['H'] = substr($options, strpos($options, 'H') + 1, 1); |
||
| 40 | } |
||
| 41 | // https:// (SSL) |
||
| 42 | if (!str_contains($options, 'S')) { |
||
| 43 | $aOptions['S'] = '?'; |
||
| 44 | } else { |
||
| 45 | $aOptions['S'] = substr($options, strpos($options, 'S') + 1, 1); |
||
| 46 | } |
||
| 47 | // mailto: (email) |
||
| 48 | if (!str_contains($options, 'E')) { |
||
| 49 | $aOptions['E'] = '-'; |
||
| 50 | } else { |
||
| 51 | $aOptions['E'] = substr($options, strpos($options, 'E') + 1, 1); |
||
| 52 | } |
||
| 53 | // ftp:// |
||
| 54 | if (!str_contains($options, 'F')) { |
||
| 55 | $aOptions['F'] = '-'; |
||
| 56 | } else { |
||
| 57 | $aOptions['F'] = substr($options, strpos($options, 'F') + 1, 1); |
||
| 58 | } |
||
| 59 | // rtmp:// |
||
| 60 | if (!str_contains($options, 'R')) { |
||
| 61 | $aOptions['R'] = '-'; |
||
| 62 | } else { |
||
| 63 | $aOptions['R'] = substr($options, strpos($options, 'R') + 1, 1); |
||
| 64 | } |
||
| 65 | // User section |
||
| 66 | if (!str_contains($options, 'u')) { |
||
| 67 | $aOptions['u'] = '?'; |
||
| 68 | } else { |
||
| 69 | $aOptions['u'] = substr($options, strpos($options, 'u') + 1, 1); |
||
| 70 | } |
||
| 71 | // Password in user section |
||
| 72 | if (!str_contains($options, 'P')) { |
||
| 73 | $aOptions['P'] = '?'; |
||
| 74 | } else { |
||
| 75 | $aOptions['P'] = substr($options, strpos($options, 'P') + 1, 1); |
||
| 76 | } |
||
| 77 | // Address Section |
||
| 78 | if (!str_contains($options, 'a')) { |
||
| 79 | $aOptions['a'] = '+'; |
||
| 80 | } else { |
||
| 81 | $aOptions['a'] = substr($options, strpos($options, 'a') + 1, 1); |
||
| 82 | } |
||
| 83 | // IP Address in address section |
||
| 84 | if (!str_contains($options, 'I')) { |
||
| 85 | $aOptions['I'] = '?'; |
||
| 86 | } else { |
||
| 87 | $aOptions['I'] = substr($options, strpos($options, 'I') + 1, 1); |
||
| 88 | } |
||
| 89 | // Port number |
||
| 90 | if (!str_contains($options, 'p')) { |
||
| 91 | $aOptions['p'] = '?'; |
||
| 92 | } else { |
||
| 93 | $aOptions['p'] = substr($options, strpos($options, 'p') + 1, 1); |
||
| 94 | } |
||
| 95 | // File Path |
||
| 96 | if (!str_contains($options, 'f')) { |
||
| 97 | $aOptions['f'] = '?'; |
||
| 98 | } else { |
||
| 99 | $aOptions['f'] = substr($options, strpos($options, 'f') + 1, 1); |
||
| 100 | } |
||
| 101 | // Query Section |
||
| 102 | if (!str_contains($options, 'q')) { |
||
| 103 | $aOptions['q'] = '?'; |
||
| 104 | } else { |
||
| 105 | $aOptions['q'] = substr($options, strpos($options, 'q') + 1, 1); |
||
| 106 | } |
||
| 107 | // Fragment (Anchor) |
||
| 108 | if (!str_contains($options, 'r')) { |
||
| 109 | $aOptions['r'] = '?'; |
||
| 110 | } else { |
||
| 111 | $aOptions['r'] = substr($options, strpos($options, 'r') + 1, 1); |
||
| 112 | } |
||
| 113 | |||
| 114 | // Loop through options array, to search for and replace "-" to "{0}" and "+" to "" |
||
| 115 | foreach ($aOptions as $key => $value) { |
||
| 116 | if ('-' == $value) { |
||
| 117 | $aOptions[$key] = '{0}'; |
||
| 118 | } |
||
| 119 | if ('+' == $value) { |
||
| 120 | $aOptions[$key] = ''; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | // DEBUGGING - Unescape following line to display to screen current option values |
||
| 125 | // echo '<pre>'; print_r($aOptions); echo '</pre>'; |
||
| 126 | |||
| 127 | // Preset Allowed Characters |
||
| 128 | $alphanum = '[a-zA-Z0-9]'; // Alpha Numeric |
||
| 129 | $unreserved = '[a-zA-Z0-9_.!~*\'()-]'; |
||
| 130 | $escaped = '(%[0-9a-fA-F]{2})'; // Escape sequence - In Hex - %6d would be a 'm' |
||
| 131 | $reserved = '[;/?:@&=+$,]'; // Special characters in the URI |
||
| 132 | |||
| 133 | // Beginning Regular Expression |
||
| 134 | // Scheme - Allows for 'http://', 'https://', 'mailto:', 'ftp://' or 'rtmp://' |
||
| 135 | $scheme = '('; |
||
| 136 | if ('' === $aOptions['H']) { |
||
| 137 | $scheme .= 'http://'; |
||
| 138 | } elseif ('' === $aOptions['S']) { |
||
| 139 | $scheme .= 'https://'; |
||
| 140 | } elseif ('' === $aOptions['E']) { |
||
| 141 | $scheme .= 'mailto:'; |
||
| 142 | } elseif ('' === $aOptions['F']) { |
||
| 143 | $scheme .= 'ftp://'; |
||
| 144 | } elseif ('' === $aOptions['R']) { |
||
| 145 | $scheme .= 'rtmp://'; |
||
| 146 | } else { |
||
| 147 | if ('?' === $aOptions['H']) { |
||
| 148 | $scheme .= '|(http://)'; |
||
| 149 | } |
||
| 150 | if ('?' === $aOptions['S']) { |
||
| 151 | $scheme .= '|(https://)'; |
||
| 152 | } |
||
| 153 | if ('?' === $aOptions['E']) { |
||
| 154 | $scheme .= '|(mailto:)'; |
||
| 155 | } |
||
| 156 | if ('?' === $aOptions['F']) { |
||
| 157 | $scheme .= '|(ftp://)'; |
||
| 158 | } |
||
| 159 | if ('?' === $aOptions['R']) { |
||
| 160 | $scheme .= '|(rtmp://)'; |
||
| 161 | } |
||
| 162 | $scheme = str_replace('(|', '(', $scheme); // fix first pipe |
||
| 163 | } |
||
| 164 | $scheme .= ')'.$aOptions['s']; |
||
| 165 | // End setting scheme |
||
| 166 | |||
| 167 | // User Info - Allows for 'username@' or 'username:password@'. Note: contrary to rfc, I removed ':' from username section, allowing it only in password. |
||
| 168 | $userinfo = '(('.$unreserved.'|'.$escaped.'|[;&=+$,])+(:('.$unreserved.'|'.$escaped.'|[;:&=+$,])+)'.$aOptions['P'].'@)'.$aOptions['u']; |
||
| 169 | |||
| 170 | // IP ADDRESS - Allows 0.0.0.0 to 255.255.255.255 |
||
| 171 | $ipaddress = '((((2(([0-4][0-9])|(5[0-5])))|([01]?[0-9]?[0-9]))\.){3}((2(([0-4][0-9])|(5[0-5])))|([01]?[0-9]?[0-9])))'; |
||
| 172 | |||
| 173 | // Tertiary Domain(s) - Optional - Multi - Although some sites may use other characters, the RFC says tertiary domains have the same naming restrictions as second level domains |
||
| 174 | $domain_tertiary = '('.$alphanum.'(([a-zA-Z0-9-]{0,62})'.$alphanum.')?\.)*'; |
||
| 175 | $domain_toplevel = '([a-zA-Z](([a-zA-Z0-9-]*)[a-zA-Z0-9])?)'; |
||
| 176 | |||
| 177 | if ('{0}' === $aOptions['I']) { // IP Address Not Allowed |
||
| 178 | $address = '('.$domain_tertiary. /* MDL-9295 $domain_secondary . */ $domain_toplevel.')'; |
||
| 179 | } elseif ('' === $aOptions['I']) { // IP Address Required |
||
| 180 | $address = '('.$ipaddress.')'; |
||
| 181 | } else { // IP Address Optional |
||
| 182 | $address = '(('.$ipaddress.')|('.$domain_tertiary. /* MDL-9295 $domain_secondary . */ $domain_toplevel.'))'; |
||
| 183 | } |
||
| 184 | $address .= $aOptions['a']; |
||
| 185 | |||
| 186 | // Port Number - :80 or :8080 or :65534 Allows range of :0 to :65535 |
||
| 187 | // (0-59999) |(60000-64999) |(65000-65499) |(65500-65529) |(65530-65535) |
||
| 188 | $port_number = '(:(([0-5]?[0-9]{1,4})|(6[0-4][0-9]{3})|(65[0-4][0-9]{2})|(655[0-2][0-9])|(6553[0-5])))'.$aOptions['p']; |
||
| 189 | |||
| 190 | // Path - Can be as simple as '/' or have multiple folders and filenames |
||
| 191 | $path = '(/((;)?('.$unreserved.'|'.$escaped.'|[:@&=+$,])+(/)?)*)'.$aOptions['f']; |
||
| 192 | |||
| 193 | // Query Section - Accepts ?var1=value1&var2=value2 or ?2393,1221 and much more |
||
| 194 | $querystring = '(\?('.$reserved.'|'.$unreserved.'|'.$escaped.')*)'.$aOptions['q']; |
||
| 195 | |||
| 196 | // Fragment Section - Accepts anchors such as #top |
||
| 197 | $fragment = '(\#('.$reserved.'|'.$unreserved.'|'.$escaped.')*)'.$aOptions['r']; |
||
| 198 | |||
| 199 | // Building Regular Expression |
||
| 200 | $regexp = '#^'.$scheme.$userinfo.$address.$port_number.$path.$querystring.$fragment.'$#i'; |
||
| 201 | |||
| 202 | // DEBUGGING - Uncomment Line Below To Display The Regular Expression Built |
||
| 203 | // echo '<pre>' . htmlentities(wordwrap($regexp,70,"\n",1)) . '</pre>'; |
||
| 204 | |||
| 205 | // Running the regular expression |
||
| 206 | if (preg_match($regexp, $urladdr)) { |
||
| 207 | return true; // The domain passed |
||
| 208 | } |
||
| 209 | |||
| 210 | return false; // The domain didn't pass the expression |
||
| 211 | } |
||
| 429 |