| Conditions | 38 |
| Paths | > 20000 |
| Total Lines | 202 |
| Code Lines | 135 |
| 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 |
||
| 205 | public static function fc_hash_html($html) { |
||
| 206 | // Initialize some counting |
||
| 207 | $count['temp'] = 0; // temp counter |
||
| 208 | $count['links'] = 0; |
||
| 209 | $count['forms'] = 0; |
||
| 210 | $count['inputs'] = 0; |
||
| 211 | $count['lists'] = 0; |
||
| 212 | $count['textareas'] = 0; |
||
| 213 | |||
| 214 | // Find and sign all the links |
||
| 215 | preg_match_all('%<a .*?href=([\'"])'.preg_quote(self::$cart_url).'(?:\.php)?\?(.+?)\1.*?>%i', $html, $querystrings); |
||
| 216 | self::$log[] = '<strong>Querystrings: </strong><pre>' . htmlspecialchars(print_r($querystrings, true)) . '</pre>'; |
||
| 217 | // print_r($querystrings); |
||
| 218 | foreach ($querystrings[2] as $querystring) { |
||
| 219 | // If it's already signed, skip it. |
||
| 220 | if (strpos($querystring, '||')) { |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | $pattern = '%(href=([\'"]))'.preg_quote(self::$cart_url, '%').'(?:\.php)?\?'.preg_quote($querystring, '%').'\2%i'; |
||
| 224 | $signed = self::fc_hash_querystring($querystring, FALSE); |
||
| 225 | $html = preg_replace($pattern, '$1'.$signed.'$2', $html, -1, $count['temp']); |
||
| 226 | $count['links'] += $count['temp']; |
||
| 227 | } |
||
| 228 | unset($querystrings); |
||
| 229 | |||
| 230 | // Find and sign all form values |
||
| 231 | preg_match_all('%<form [^>]*?action=[\'"]'.preg_quote(self::$cart_url).'(?:\.php)?[\'"].*?>(.+?)</form>%is', $html, $forms); |
||
| 232 | foreach ($forms[1] as $form) { |
||
| 233 | $count['forms']++; |
||
| 234 | self::$log[] = '<strong>Signing form</strong> with data: '.htmlspecialchars(substr($form, 0, 150)).'...'; |
||
| 235 | |||
| 236 | // Store the original form so we can replace it when we're done |
||
| 237 | $form_original = $form; |
||
| 238 | |||
| 239 | // Check for the "code" input, set the matches in $codes |
||
| 240 | if (!preg_match_all('%<[^>]*?name=([\'"])([0-9]{1,3}:)?code\1[^>]*?>%i', $form, $codes, PREG_SET_ORDER)) { |
||
| 241 | self::$log[] = '<strong style="color:#600;">No code found</strong> for the above form.'; |
||
| 242 | continue; |
||
| 243 | } |
||
| 244 | // For each code found, sign the appropriate inputs |
||
| 245 | foreach ($codes as $code) { |
||
| 246 | // If the form appears to be hashed already, don't bother |
||
| 247 | if (strpos($code[0], '||')) { |
||
| 248 | self::$log[] = '<strong>Form appears to be signed already</strong>: '.htmlspecialchars($code[0]); |
||
| 249 | continue; |
||
| 250 | } |
||
| 251 | // Get the code and the prefix |
||
| 252 | $prefix = (isset($code[2])) ? $code[2] : ''; |
||
| 253 | preg_match('%<[^>]*?value=([\'"])(.+?)\1[^>]*?>%i', $code[0], $code); |
||
| 254 | $code = trim($code[2]); |
||
| 255 | self::$log[] = '<strong>Prefix for '.htmlspecialchars($code).'</strong>: '.htmlspecialchars($prefix); |
||
| 256 | if (!$code) { // If the code is empty, skip this form or specific prefixed elements |
||
| 257 | continue; |
||
| 258 | } |
||
| 259 | |||
| 260 | // Sign all <input /> elements with matching prefix |
||
| 261 | preg_match_all('%<input [^>]*?name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(?:.+?)\1[^>]*>%i', $form, $inputs); |
||
| 262 | |||
| 263 | // get parent codes if they exist and append them to our code |
||
| 264 | $parent_code_index = false; |
||
| 265 | foreach ($inputs[0] as $key => $item) { |
||
| 266 | if (strpos($item, 'parent_code') !== false) { |
||
| 267 | $parent_code_index = $key; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | if ($parent_code_index !== false) { |
||
| 271 | if (preg_match('%value=([\'"])(.*?)\1%i', $inputs[0][$parent_code_index], $value)) { |
||
| 272 | $code .= $value[2]; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | foreach ($inputs[0] as $input) { |
||
| 277 | $count['inputs']++; |
||
| 278 | // Test to make sure both name and value attributes are found |
||
| 279 | if (preg_match('%name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1%i', $input, $name) > 0) { |
||
| 280 | preg_match('%value=([\'"])(.*?)\1%i', $input, $value); |
||
| 281 | $value = (count($value) > 0) ? $value : array('', '', ''); |
||
| 282 | preg_match('%type=([\'"])(.*?)\1%i', $input, $type); |
||
| 283 | $type = (count($type) > 0) ? $type : array('', '', ''); |
||
| 284 | // Skip the cart excludes |
||
| 285 | $include_input = true; |
||
| 286 | if (in_array($prefix.$name[2], self::$cart_excludes)) { |
||
| 287 | $include_input = false; |
||
| 288 | } |
||
| 289 | foreach (self::$cart_excludes_prefixes as $exclude_prefix) { |
||
| 290 | if (substr(strtolower($prefix.$name[2]), 0, strlen($exclude_prefix)) == $exclude_prefix) { |
||
| 291 | $include_input = false; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | if (!$include_input) { |
||
| 295 | self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or prefix "'.$prefix.$name[2].'" = '.$value[2]; |
||
| 296 | continue; |
||
| 297 | } |
||
| 298 | self::$log[] = '<strong>INPUT['.$type[2].']:</strong> Name: <strong>'.$prefix.htmlspecialchars(preg_quote($name[2])).'</strong>'; |
||
| 299 | $value[2] = ($value[2] == '') ? '--OPEN--' : $value[2]; |
||
| 300 | if ($type[2] == 'radio') { |
||
| 301 | self::$log[] = '<strong>Replacement Pattern:</strong> ([\'"])'.$prefix.preg_quote($value[2]).'\1'; |
||
| 302 | $input_signed = preg_replace('%([\'"])'.preg_quote($value[2]).'\1%', '${1}'.self::fc_hash_value($code, $name[2], $value[2], 'value', FALSE)."$1", $input); |
||
| 303 | } else { |
||
| 304 | self::$log[] = '<strong>Replacement Pattern:</strong> name=([\'"])'.$prefix.preg_quote($name[2]).'\1'; |
||
| 305 | $input_signed = preg_replace('%name=([\'"])'.$prefix.preg_quote($name[2]).'\1%', 'name=${1}'.$prefix.self::fc_hash_value($code, $name[2], $value[2], 'name', FALSE)."$1", $input); |
||
| 306 | } |
||
| 307 | self::$log[] = '<strong>INPUT:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
||
| 308 | '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$name[2]). |
||
| 309 | '</strong> :: Value: <strong>'.htmlspecialchars($value[2]). |
||
| 310 | '</strong><br />Initial input: '.htmlspecialchars($input). |
||
| 311 | '<br />Signed: <span style="color:#060;">'.htmlspecialchars($input_signed).'</span>'; |
||
| 312 | $form = str_replace($input, $input_signed, $form); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | self::$log[] = '<strong>FORM after INPUTS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
||
| 316 | |||
| 317 | // Sign all <option /> elements |
||
| 318 | preg_match_all('%<select [^>]*name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1[^>]*>(.+?)</select>%is', $form, $lists, PREG_SET_ORDER); |
||
| 319 | foreach ($lists as $list) { |
||
| 320 | $count['lists']++; |
||
| 321 | // Skip the cart excludes |
||
| 322 | $include_input = true; |
||
| 323 | if (in_array($prefix.$list[2], self::$cart_excludes)) { |
||
| 324 | $include_input = false; |
||
| 325 | } |
||
| 326 | foreach (self::$cart_excludes_prefixes as $exclude_prefix) { |
||
| 327 | if (substr(strtolower($prefix.$list[2]), 0, strlen($exclude_prefix)) == $exclude_prefix) { |
||
| 328 | $include_input = false; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | if (!$include_input) { |
||
| 332 | self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or prefix "'.$prefix.$list[2]; |
||
| 333 | continue; |
||
| 334 | } |
||
| 335 | preg_match_all('%<option [^>]*value=([\'"])(.+?)\1[^>]*>(?:.*?)</option>%i', $list[0], $options, PREG_SET_ORDER); |
||
| 336 | self::$log[] = '<strong>Options:</strong> <pre>'.htmlspecialchars(print_r($options, true)).'</pre>'; |
||
| 337 | unset( $form_part_signed ); |
||
| 338 | foreach ($options as $option) { |
||
| 339 | if( !isset($form_part_signed) ) $form_part_signed = $list[0]; |
||
| 340 | $option_signed = preg_replace( |
||
| 341 | '%'.preg_quote($option[1]).preg_quote($option[2]).preg_quote($option[1]).'%', |
||
| 342 | $option[1].self::fc_hash_value($code, $list[2], $option[2], 'value', FALSE).$option[1], |
||
| 343 | $option[0]); |
||
| 344 | $form_part_signed = str_replace($option[0], $option_signed, $form_part_signed ); |
||
| 345 | self::$log[] = '<strong>OPTION:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
||
| 346 | '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$list[2]). |
||
| 347 | '</strong> :: Value: <strong>'.htmlspecialchars($option[2]). |
||
| 348 | '</strong><br />Initial option: '.htmlspecialchars($option[0]). |
||
| 349 | '<br />Signed: <span style="color:#060;">'.htmlspecialchars($option_signed).'</span>'; |
||
| 350 | } |
||
| 351 | $form = str_replace($list[0], $form_part_signed, $form); |
||
| 352 | } |
||
| 353 | self::$log[] = '<strong>FORM after OPTIONS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
||
| 354 | |||
| 355 | // Sign all <textarea /> elements |
||
| 356 | preg_match_all('%<textarea [^>]*name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1[^>]*>(.*?)</textarea>%is', $form, $textareas, PREG_SET_ORDER); |
||
| 357 | // echo "\n\nTextareas: ".print_r($textareas, true); |
||
| 358 | foreach ($textareas as $textarea) { |
||
| 359 | $count['textareas']++; |
||
| 360 | // Skip the cart excludes |
||
| 361 | $include_input = true; |
||
| 362 | if (in_array($prefix.$textarea[2], self::$cart_excludes)) { |
||
| 363 | $include_input = false; |
||
| 364 | } |
||
| 365 | foreach (self::$cart_excludes_prefixes as $exclude_prefix) { |
||
| 366 | if (substr(strtolower($prefix.$textarea[2]), 0, strlen($exclude_prefix)) == $exclude_prefix) { |
||
| 367 | $include_input = false; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | if (!$include_input) { |
||
| 371 | self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or prefix "'.$prefix.$textarea[2]; |
||
| 372 | continue; |
||
| 373 | } |
||
| 374 | // Tackle implied "--OPEN--" first, if textarea is empty |
||
| 375 | $textarea[3] = ($textarea[3] == '') ? '--OPEN--' : $textarea[3]; |
||
| 376 | $textarea_signed = preg_replace('%name=([\'"])'.preg_quote($prefix.$textarea[2]).'\1%', "name=$1".self::fc_hash_value($code, $textarea[2], $textarea[3], 'name', FALSE)."$1", $textarea[0]); |
||
| 377 | $form = str_replace($textarea[0], $textarea_signed, $form); |
||
| 378 | self::$log[] = '<strong>TEXTAREA:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
||
| 379 | '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$textarea[2]). |
||
| 380 | '</strong> :: Value: <strong>'.htmlspecialchars($textarea[3]). |
||
| 381 | '</strong><br />Initial textarea: '.htmlspecialchars($textarea[0]). |
||
| 382 | '<br />Signed: <span style="color:#060;">'.htmlspecialchars($textarea_signed).'</span>'; |
||
| 383 | } |
||
| 384 | self::$log[] = '<strong>FORM after TEXTAREAS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
||
| 385 | |||
| 386 | // Exclude all <button> elements |
||
| 387 | $form = preg_replace('%<button ([^>]*)name=([\'"])(.*?)\1([^>]*>.*?</button>)%i', "<button $1name=$2x:$3$4", $form); |
||
| 388 | |||
| 389 | } |
||
| 390 | // Replace the entire form |
||
| 391 | self::$log[] = '<strong>FORM after ALL:</strong> <pre>'.htmlspecialchars($form).'</pre>'.'replacing <pre>'.htmlspecialchars($form_original).'</pre>'; |
||
| 392 | $html = str_replace($form_original, $form, $html); |
||
| 393 | self::$log[] = '<strong>FORM end</strong><hr />'; |
||
| 394 | } |
||
| 395 | |||
| 396 | // Return the signed output |
||
| 397 | $output = ''; |
||
| 398 | if (self::$debug) { |
||
| 399 | self::$log['Summary'] = $count['links'].' links signed. '.$count['forms'].' forms signed. '.$count['inputs'].' inputs signed. '.$count['lists'].' lists signed. '.$count['textareas'].' textareas signed.'; |
||
| 400 | $output .= '<div style="background:#fff;"><h3>FoxyCart HMAC Debugging:</h3><ul>'; |
||
| 401 | foreach (self::$log as $name => $value) { |
||
| 402 | $output .= '<li><strong>'.$name.':</strong> '.$value.'</li>'."\n"; |
||
| 403 | } |
||
| 404 | $output .= '</ul><hr />'; |
||
| 405 | } |
||
| 406 | return $output.$html; |
||
| 407 | } |
||
| 409 | } |