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