Conditions | 38 |
Paths | > 20000 |
Total Lines | 273 |
Code Lines | 193 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
242 | public static function fc_hash_html($html) |
||
243 | { |
||
244 | // Initialize some counting |
||
245 | $count['temp'] = 0; // temp counter |
||
246 | $count['links'] = 0; |
||
247 | $count['forms'] = 0; |
||
248 | $count['inputs'] = 0; |
||
249 | $count['lists'] = 0; |
||
250 | $count['textareas'] = 0; |
||
251 | |||
252 | // Find and sign all the links |
||
253 | preg_match_all( |
||
254 | '%<a .*?href=([\'"])'.preg_quote(self::$cart_url).'(?:\.php)?\?(.+?)\1.*?>%i', |
||
255 | $html, |
||
256 | $querystrings |
||
257 | ); |
||
258 | self::$log[] = '<strong>Querystrings: </strong><pre>' . htmlspecialchars(print_r($querystrings, true)) . |
||
259 | '</pre>'; |
||
260 | // print_r($querystrings); |
||
261 | foreach ($querystrings[2] as $querystring) { |
||
262 | // If it's already signed, skip it. |
||
263 | if (strpos($querystring, '||')) { |
||
264 | continue; |
||
265 | } |
||
266 | $pattern = '%(href=([\'"]))'.preg_quote(self::$cart_url, '%').'(?:\.php)?\?'. |
||
267 | preg_quote($querystring, '%').'\2%i'; |
||
268 | $signed = self::fc_hash_querystring($querystring, false); |
||
269 | $html = preg_replace($pattern, '$1'.$signed.'$2', $html, -1, $count['temp']); |
||
270 | $count['links'] += $count['temp']; |
||
271 | } |
||
272 | unset($querystrings); |
||
273 | |||
274 | // Find and sign all form values |
||
275 | preg_match_all( |
||
276 | '%<form [^>]*?action=[\'"]'.preg_quote(self::$cart_url).'(?:\.php)?[\'"].*?>(.+?)</form>%is', |
||
277 | $html, |
||
278 | $forms |
||
279 | ); |
||
280 | foreach ($forms[1] as $form) { |
||
281 | $count['forms']++; |
||
282 | self::$log[] = '<strong>Signing form</strong> with data: '. |
||
283 | htmlspecialchars(substr($form, 0, 150)).'...'; |
||
284 | |||
285 | // Store the original form so we can replace it when we're done |
||
286 | $form_original = $form; |
||
287 | |||
288 | // Check for the "code" input, set the matches in $codes |
||
289 | if (!preg_match_all( |
||
290 | '%<[^>]*?name=([\'"])([0-9]{1,3}:)?code\1[^>]*?>%i', |
||
291 | $form, |
||
292 | $codes, |
||
293 | PREG_SET_ORDER |
||
294 | )) { |
||
295 | self::$log[] = '<strong style="color:#600;">No code found</strong> for the above form.'; |
||
296 | continue; |
||
297 | } |
||
298 | // For each code found, sign the appropriate inputs |
||
299 | foreach ($codes as $code) { |
||
300 | // If the form appears to be hashed already, don't bother |
||
301 | if (strpos($code[0], '||')) { |
||
302 | self::$log[] = '<strong>Form appears to be signed already</strong>: '.htmlspecialchars($code[0]); |
||
303 | continue; |
||
304 | } |
||
305 | // Get the code and the prefix |
||
306 | $prefix = (isset($code[2])) ? $code[2] : ''; |
||
307 | preg_match('%<[^>]*?value=([\'"])(.+?)\1[^>]*?>%i', $code[0], $code); |
||
308 | $code = trim($code[2]); |
||
309 | self::$log[] = '<strong>Prefix for '.htmlspecialchars($code).'</strong>: '.htmlspecialchars($prefix); |
||
310 | if (!$code) { // If the code is empty, skip this form or specific prefixed elements |
||
311 | continue; |
||
312 | } |
||
313 | |||
314 | // Sign all <input /> elements with matching prefix |
||
315 | preg_match_all( |
||
316 | '%<input [^>]*?name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(?:.+?)\1[^>]*>%i', |
||
317 | $form, |
||
318 | $inputs |
||
319 | ); |
||
320 | |||
321 | // get parent codes if they exist and append them to our code |
||
322 | $parent_code_index = false; |
||
323 | foreach ($inputs[0] as $key => $item) { |
||
324 | if (strpos($item, 'parent_code') !== false) { |
||
325 | $parent_code_index = $key; |
||
326 | } |
||
327 | } |
||
328 | if ($parent_code_index !== false) { |
||
329 | if (preg_match('%value=([\'"])(.*?)\1%i', $inputs[0][$parent_code_index], $value)) { |
||
330 | $code .= $value[2]; |
||
331 | } |
||
332 | } |
||
333 | |||
334 | foreach ($inputs[0] as $input) { |
||
335 | $count['inputs']++; |
||
336 | // Test to make sure both name and value attributes are found |
||
337 | if (preg_match( |
||
338 | '%name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1%i', |
||
339 | $input, |
||
340 | $name |
||
341 | ) > 0) { |
||
342 | preg_match('%value=([\'"])(.*?)\1%i', $input, $value); |
||
343 | $value = (count($value) > 0) ? $value : array('', '', ''); |
||
344 | preg_match('%type=([\'"])(.*?)\1%i', $input, $type); |
||
345 | $type = (count($type) > 0) ? $type : array('', '', ''); |
||
346 | // Skip the cart excludes |
||
347 | $include_input = true; |
||
348 | if (in_array($prefix.$name[2], self::$cart_excludes)) { |
||
349 | $include_input = false; |
||
350 | } |
||
351 | foreach (self::$cart_excludes_prefixes as $exclude_prefix) { |
||
352 | if (substr(strtolower($prefix.$name[2]), 0, strlen($exclude_prefix)) == $exclude_prefix) { |
||
353 | $include_input = false; |
||
354 | } |
||
355 | } |
||
356 | if (!$include_input) { |
||
357 | self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or |
||
358 | prefix "'.$prefix.$name[2].'" = '.$value[2]; |
||
359 | continue; |
||
360 | } |
||
361 | self::$log[] = '<strong>INPUT['.$type[2].']:</strong> Name: <strong>'.$prefix. |
||
362 | htmlspecialchars(preg_quote($name[2])).'</strong>'; |
||
363 | $value[2] = ($value[2] == '') ? '--OPEN--' : $value[2]; |
||
364 | if ($type[2] == 'radio') { |
||
365 | self::$log[] = '<strong>Replacement Pattern:</strong> ([\'"])'. |
||
366 | $prefix.preg_quote($value[2]).'\1'; |
||
367 | $input_signed = preg_replace( |
||
368 | '%([\'"])'.preg_quote($value[2]).'\1%', |
||
369 | '${1}'.self::fc_hash_value($code, $name[2], $value[2], 'value', false). |
||
370 | "$1", |
||
371 | $input |
||
372 | ); |
||
373 | } else { |
||
374 | self::$log[] = '<strong>Replacement Pattern:</strong> name=([\'"])'. |
||
375 | $prefix.preg_quote($name[2]).'\1'; |
||
376 | $input_signed = preg_replace( |
||
377 | '%name=([\'"])'.$prefix.preg_quote($name[2]).'\1%', |
||
378 | 'name=${1}'.$prefix.self::fc_hash_value( |
||
379 | $code, |
||
380 | $name[2], |
||
381 | $value[2], |
||
382 | 'name', |
||
383 | false |
||
384 | )."$1", |
||
385 | $input |
||
386 | ); |
||
387 | } |
||
388 | self::$log[] = '<strong>INPUT:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
||
389 | '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$name[2]). |
||
390 | '</strong> :: Value: <strong>'.htmlspecialchars($value[2]). |
||
391 | '</strong><br />Initial input: '.htmlspecialchars($input). |
||
392 | '<br />Signed: <span style="color:#060;">'.htmlspecialchars($input_signed).'</span>'; |
||
393 | $form = str_replace($input, $input_signed, $form); |
||
394 | } |
||
395 | } |
||
396 | self::$log[] = '<strong>FORM after INPUTS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
||
397 | |||
398 | // Sign all <option /> elements |
||
399 | preg_match_all( |
||
400 | '%<select [^>]*name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1[^>]*>(.+?)</select>%is', |
||
401 | $form, |
||
402 | $lists, |
||
403 | PREG_SET_ORDER |
||
404 | ); |
||
405 | foreach ($lists as $list) { |
||
406 | $count['lists']++; |
||
407 | // Skip the cart excludes |
||
408 | $include_input = true; |
||
409 | if (in_array($prefix.$list[2], self::$cart_excludes)) { |
||
410 | $include_input = false; |
||
411 | } |
||
412 | foreach (self::$cart_excludes_prefixes as $exclude_prefix) { |
||
413 | if (substr(strtolower($prefix.$list[2]), 0, strlen($exclude_prefix)) == $exclude_prefix) { |
||
414 | $include_input = false; |
||
415 | } |
||
416 | } |
||
417 | if (!$include_input) { |
||
418 | self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or |
||
419 | prefix "'.$prefix.$list[2]; |
||
420 | continue; |
||
421 | } |
||
422 | preg_match_all( |
||
423 | '%<option [^>]*value=([\'"])(.+?)\1[^>]*>(?:.*?)</option>%i', |
||
424 | $list[0], |
||
425 | $options, |
||
426 | PREG_SET_ORDER |
||
427 | ); |
||
428 | self::$log[] = '<strong>Options:</strong> <pre>'.htmlspecialchars(print_r($options, true)).'</pre>'; |
||
429 | unset($form_part_signed); |
||
430 | foreach ($options as $option) { |
||
431 | if (!isset($form_part_signed)) { |
||
432 | $form_part_signed = $list[0]; |
||
433 | } |
||
434 | $option_signed = preg_replace( |
||
435 | '%'.preg_quote($option[1]).preg_quote($option[2]).preg_quote($option[1]).'%', |
||
436 | $option[1].self::fc_hash_value($code, $list[2], $option[2], 'value', false). |
||
437 | $option[1], |
||
438 | $option[0] |
||
439 | ); |
||
440 | $form_part_signed = str_replace($option[0], $option_signed, $form_part_signed); |
||
441 | self::$log[] = '<strong>OPTION:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
||
442 | '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$list[2]). |
||
443 | '</strong> :: Value: <strong>'.htmlspecialchars($option[2]). |
||
444 | '</strong><br />Initial option: '.htmlspecialchars($option[0]). |
||
445 | '<br />Signed: <span style="color:#060;">'.htmlspecialchars($option_signed).'</span>'; |
||
446 | } |
||
447 | $form = str_replace($list[0], $form_part_signed, $form); |
||
448 | } |
||
449 | self::$log[] = '<strong>FORM after OPTIONS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
||
450 | |||
451 | // Sign all <textarea /> elements |
||
452 | preg_match_all('%<textarea [^>]*name=([\'"])'.preg_quote($prefix). |
||
453 | '(?![0-9]{1,3})(.+?)\1[^>]*>(.*?)</textarea>%is', $form, $textareas, PREG_SET_ORDER); |
||
454 | // echo "\n\nTextareas: ".print_r($textareas, true); |
||
455 | foreach ($textareas as $textarea) { |
||
456 | $count['textareas']++; |
||
457 | // Skip the cart excludes |
||
458 | $include_input = true; |
||
459 | if (in_array($prefix.$textarea[2], self::$cart_excludes)) { |
||
460 | $include_input = false; |
||
461 | } |
||
462 | foreach (self::$cart_excludes_prefixes as $exclude_prefix) { |
||
463 | if (substr(strtolower($prefix.$textarea[2]), 0, strlen($exclude_prefix)) == $exclude_prefix) { |
||
464 | $include_input = false; |
||
465 | } |
||
466 | } |
||
467 | if (!$include_input) { |
||
468 | self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or |
||
469 | prefix "'. $prefix.$textarea[2]; |
||
470 | continue; |
||
471 | } |
||
472 | // Tackle implied "--OPEN--" first, if textarea is empty |
||
473 | $textarea[3] = ($textarea[3] == '') ? '--OPEN--' : $textarea[3]; |
||
474 | $textarea_signed = preg_replace( |
||
475 | '%name=([\'"])'.preg_quote($prefix.$textarea[2]).'\1%', |
||
476 | "name=$1".self::fc_hash_value($code, $textarea[2], $textarea[3], 'name', false)."$1", |
||
477 | $textarea[0] |
||
478 | ); |
||
479 | $form = str_replace($textarea[0], $textarea_signed, $form); |
||
480 | self::$log[] = '<strong>TEXTAREA:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
||
481 | '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$textarea[2]). |
||
482 | '</strong> :: Value: <strong>'.htmlspecialchars($textarea[3]). |
||
483 | '</strong><br />Initial textarea: '.htmlspecialchars($textarea[0]). |
||
484 | '<br />Signed: <span style="color:#060;">'.htmlspecialchars($textarea_signed).'</span>'; |
||
485 | } |
||
486 | self::$log[] = '<strong>FORM after TEXTAREAS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
||
487 | |||
488 | // Exclude all <button> elements |
||
489 | $form = preg_replace( |
||
490 | '%<button ([^>]*)name=([\'"])(.*?)\1([^>]*>.*?</button>)%i', |
||
491 | "<button $1name=$2x:$3$4", |
||
492 | $form |
||
493 | ); |
||
494 | } |
||
495 | // Replace the entire form |
||
496 | self::$log[] = '<strong>FORM after ALL:</strong> <pre>'.htmlspecialchars($form).'</pre>'.'replacing <pre>'. |
||
497 | htmlspecialchars($form_original).'</pre>'; |
||
498 | $html = str_replace($form_original, $form, $html); |
||
499 | self::$log[] = '<strong>FORM end</strong><hr />'; |
||
500 | } |
||
501 | |||
502 | // Return the signed output |
||
503 | $output = ''; |
||
504 | if (self::$debug) { |
||
505 | self::$log['Summary'] = $count['links'].' links signed. '.$count['forms'].' forms signed. '. |
||
506 | $count['inputs'].' inputs signed. '.$count['lists'].' lists signed. '.$count['textareas']. |
||
507 | ' textareas signed.'; |
||
508 | $output .= '<div style="background:#fff;"><h3>FoxyCart HMAC Debugging:</h3><ul>'; |
||
509 | foreach (self::$log as $name => $value) { |
||
510 | $output .= '<li><strong>'.$name.':</strong> '.$value.'</li>'."\n"; |
||
511 | } |
||
512 | $output .= '</ul><hr />'; |
||
513 | } |
||
514 | return $output.$html; |
||
515 | } |
||
517 |