Conditions | 24 |
Paths | 3204 |
Total Lines | 149 |
Code Lines | 102 |
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 |
||
177 | public static function fc_hash_html($html) { |
||
178 | // Initialize some counting |
||
179 | $count['temp'] = 0; // temp counter |
||
180 | $count['links'] = 0; |
||
181 | $count['forms'] = 0; |
||
182 | $count['inputs'] = 0; |
||
183 | $count['lists'] = 0; |
||
184 | $count['textareas'] = 0; |
||
185 | |||
186 | // Find and sign all the links |
||
187 | preg_match_all('%<a .*?href=[\'"]'.preg_quote(self::$cart_url).'(?:\.php)?\?(.+?)[\'"].*?>%i', $html, $querystrings); |
||
188 | // print_r($querystrings); |
||
189 | foreach ($querystrings[1] as $querystring) { |
||
190 | // If it's already signed, skip it. |
||
191 | if (preg_match('%&(?:amp;)?hash=%i', $querystring)) { |
||
192 | continue; |
||
193 | } |
||
194 | $pattern = '%(href=[\'"])'.preg_quote(self::$cart_url, '%').'(?:\.php)?\?'.preg_quote($querystring, '%').'([\'"])%i'; |
||
195 | $signed = self::fc_hash_querystring($querystring, FALSE); |
||
196 | $html = preg_replace($pattern, '$1'.$signed.'$2', $html, -1, $count['temp']); |
||
197 | $count['links'] += $count['temp']; |
||
198 | } |
||
199 | unset($querystrings); |
||
200 | |||
201 | // Find and sign all form values |
||
202 | preg_match_all('%<form [^>]*?action=[\'"]'.preg_quote(self::$cart_url).'?[\'"].*?>(.+?)</form>%is', $html, $forms); |
||
203 | foreach ($forms[1] as $form) { |
||
204 | $count['forms']++; |
||
205 | self::$log[] = '<strong>Signing form</strong> with data: '.htmlspecialchars(substr($form, 0, 150)).'...'; |
||
206 | |||
207 | // Store the original form so we can replace it when we're done |
||
208 | $form_original = $form; |
||
209 | |||
210 | // Check for the "code" input, set the matches in $codes |
||
211 | if (!preg_match_all('%<[^>]*?name=([\'"])([0-9]{1,3}:)?code\1[^>]*?>%i', $form, $codes, PREG_SET_ORDER)) { |
||
212 | self::$log[] = '<strong style="color:#600;">No code found</strong> for the above form.'; |
||
213 | continue; |
||
214 | } |
||
215 | // For each code found, sign the appropriate inputs |
||
216 | foreach ($codes as $code) { |
||
217 | // If the form appears to be hashed already, don't bother |
||
218 | if (strpos($code[0], '||')) { |
||
219 | self::$log[] = '<strong>Form appears to be signed already</strong>: '.htmlspecialchars($code[0]); |
||
220 | continue; |
||
221 | } |
||
222 | // Get the code and the prefix |
||
223 | $prefix = (isset($code[2])) ? $code[2] : ''; |
||
224 | preg_match('%<[^>]*?value=([\'"])(.+?)\1[^>]*?>%i', $code[0], $code); |
||
225 | $code = trim($code[2]); |
||
226 | self::$log[] = '<strong>Prefix for '.htmlspecialchars($code).'</strong>: '.htmlspecialchars($prefix); |
||
227 | if (!$code) { // If the code is empty, skip this form or specific prefixed elements |
||
228 | continue; |
||
229 | } |
||
230 | |||
231 | // Sign all <input /> elements with matching prefix |
||
232 | preg_match_all('%<input [^>]*?name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(?:.+?)\1[^>]*>%i', $form, $inputs); |
||
233 | foreach ($inputs[0] as $input) { |
||
234 | $count['inputs']++; |
||
235 | // Test to make sure both name and value attributes are found |
||
236 | if (preg_match('%name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1%i', $input, $name) > 0) { |
||
237 | preg_match('%value=([\'"])(.*?)\1%i', $input, $value); |
||
238 | $value = (count($value) > 0) ? $value : array('', '', ''); |
||
239 | preg_match('%type=([\'"])(.*?)\1%i', $input, $type); |
||
240 | $type = (count($type) > 0) ? $type : array('', '', ''); |
||
241 | // Skip the cart excludes |
||
242 | if (in_array($prefix.$name[2], self::$cart_excludes) || in_array(substr($prefix.$name[2], 0, 2), self::$cart_excludes_prefixes)) { |
||
243 | self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or prefix "'.$prefix.$name[2].'" = '.$value[2]; |
||
244 | continue; |
||
245 | } |
||
246 | self::$log[] = '<strong>INPUT['.$type[2].']:</strong> Name: <strong>'.$prefix.htmlspecialchars(preg_quote($name[2])).'</strong>'; |
||
247 | self::$log[] = '<strong>Replacement Pattern:</strong> ([\'"])'.$prefix.preg_quote($name[2]).'\1'; |
||
248 | $value[2] = ($value[2] == '') ? '--OPEN--' : $value[2]; |
||
249 | if ($type[2] == 'radio') { |
||
250 | $input_signed = preg_replace('%([\'"])'.preg_quote($value[2]).'\1%', '${1}'.self::fc_hash_value($code, $name[2], $value[2], 'value', FALSE)."$1", $input); |
||
251 | } else { |
||
252 | $input_signed = preg_replace('%([\'"])'.$prefix.preg_quote($name[2]).'\1%', '${1}'.$prefix.self::fc_hash_value($code, $name[2], $value[2], 'name', FALSE)."$1", $input); |
||
253 | } |
||
254 | self::$log[] = '<strong>INPUT:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
||
255 | '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$name[2]). |
||
256 | '</strong> :: Value: <strong>'.htmlspecialchars($value[2]). |
||
257 | '</strong><br />Initial input: '.htmlspecialchars($input). |
||
258 | '<br />Signed: <span style="color:#060;">'.htmlspecialchars($input_signed).'</span>'; |
||
259 | $form = str_replace($input, $input_signed, $form); |
||
260 | } |
||
261 | } |
||
262 | self::$log[] = '<strong>FORM after INPUTS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
||
263 | |||
264 | // Sign all <option /> elements |
||
265 | preg_match_all('%<select [^>]*name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1[^>]*>(.+?)</select>%is', $form, $lists, PREG_SET_ORDER); |
||
266 | foreach ($lists as $list) { |
||
267 | $count['lists']++; |
||
268 | preg_match_all('%<option [^>]*value=([\'"])(.+?)\1[^>]*>(?:.*?)</option>%i', $list[0], $options, PREG_SET_ORDER); |
||
269 | self::$log[] = '<strong>Options:</strong> <pre>'.htmlspecialchars(print_r($options, true)).'</pre>'; |
||
270 | unset( $form_part_signed ); |
||
271 | foreach ($options as $option) { |
||
272 | if( !isset($form_part_signed) ) $form_part_signed = $list[0]; |
||
273 | $option_signed = preg_replace( |
||
274 | '%'.preg_quote($option[1]).preg_quote($option[2]).preg_quote($option[1]).'%', |
||
275 | $option[1].self::fc_hash_value($code, $list[2], $option[2], 'value', FALSE).$option[1], |
||
276 | $option[0]); |
||
277 | $form_part_signed = str_replace($option[0], $option_signed, $form_part_signed ); |
||
278 | self::$log[] = '<strong>OPTION:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
||
279 | '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$list[2]). |
||
280 | '</strong> :: Value: <strong>'.htmlspecialchars($option[2]). |
||
281 | '</strong><br />Initial option: '.htmlspecialchars($option[0]). |
||
282 | '<br />Signed: <span style="color:#060;">'.htmlspecialchars($option_signed).'</span>'; |
||
283 | } |
||
284 | $form = str_replace($list[0], $form_part_signed, $form); |
||
285 | } |
||
286 | self::$log[] = '<strong>FORM after OPTIONS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
||
287 | |||
288 | // Sign all <textarea /> elements |
||
289 | preg_match_all('%<textarea [^>]*name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1[^>]*>(.*?)</textarea>%is', $form, $textareas, PREG_SET_ORDER); |
||
290 | // echo "\n\nTextareas: ".print_r($textareas, true); |
||
291 | foreach ($textareas as $textarea) { |
||
292 | $count['textareas']++; |
||
293 | // Tackle implied "--OPEN--" first, if textarea is empty |
||
294 | $textarea[3] = ($textarea[3] == '') ? '--OPEN--' : $textarea[3]; |
||
295 | $textarea_signed = preg_replace('%([\'"])'.preg_quote($prefix.$textarea[2]).'\1%', "$1".self::fc_hash_value($code, $textarea[2], $textarea[3], 'name', FALSE)."$1", $textarea[0]); |
||
296 | $form = str_replace($textarea[0], $textarea_signed, $form); |
||
297 | self::$log[] = '<strong>TEXTAREA:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
||
298 | '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$textarea[2]). |
||
299 | '</strong> :: Value: <strong>'.htmlspecialchars($textarea[3]). |
||
300 | '</strong><br />Initial textarea: '.htmlspecialchars($textarea[0]). |
||
301 | '<br />Signed: <span style="color:#060;">'.htmlspecialchars($textarea_signed).'</span>'; |
||
302 | } |
||
303 | self::$log[] = '<strong>FORM after TEXTAREAS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
||
304 | |||
305 | // Exclude all <button> elements |
||
306 | $form = preg_replace('%<button ([^>]*)name=([\'"])(.*?)\1([^>]*>.*?</button>)%i', "<button $1name=$2x:$3$4", $form); |
||
307 | |||
308 | } |
||
309 | // Replace the entire form |
||
310 | self::$log[] = '<strong>FORM after ALL:</strong> <pre>'.htmlspecialchars($form).'</pre>'.'replacing <pre>'.htmlspecialchars($form_original).'</pre>'; |
||
311 | $html = str_replace($form_original, $form, $html); |
||
312 | self::$log[] = '<strong>FORM end</strong><hr />'; |
||
313 | } |
||
314 | |||
315 | // Return the signed output |
||
316 | $output = ''; |
||
317 | if (self::$debug) { |
||
318 | self::$log['Summary'] = $count['links'].' links signed. '.$count['forms'].' forms signed. '.$count['inputs'].' inputs signed. '.$count['lists'].' lists signed. '.$count['textareas'].' textareas signed.'; |
||
319 | $output .= '<h3>FoxyCart HMAC Debugging:</h3><ul>'; |
||
320 | foreach (self::$log as $name => $value) { |
||
321 | $output .= '<li><strong>'.$name.':</strong> '.$value.'</li>'; |
||
322 | } |
||
323 | $output .= '</ul><hr />'; |
||
324 | } |
||
325 | return $output.$html; |
||
326 | } |
||
328 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.