Skip to content

Commit 58254c1

Browse files
committed
Fix for issue 24 - Method j_parseUrl crashing. Completely rewrote function.
Thanks to mikegillis677 for finding. Updated version info and copyright dates on all files.
1 parent bbffc0f commit 58254c1

File tree

9 files changed

+44
-52
lines changed

9 files changed

+44
-52
lines changed

INSTALL

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
phpGSB - PHP Google Safe Browsing Implementation
22
Released under New BSD License (see LICENSE)
3-
Copyright (c) 2010-2011, Sam Cleaver (Beaver6813, Beaver6813.com)
3+
Copyright (c) 2010-2012, Sam Cleaver (Beaver6813, Beaver6813.com)
44
All rights reserved.
55
------------------------------------------------------------
66
Install Steps

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2010-2011, Sam Cleaver
1+
Copyright (c) 2010-2012, Sam Cleaver
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

UPGRADE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
phpGSB - PHP Google Safe Browsing Implementation
22
Released under New BSD License (see LICENSE)
3-
Copyright (c) 2010-2011, Sam Cleaver (Beaver6813, Beaver6813.com)
3+
Copyright (c) 2010-2012, Sam Cleaver (Beaver6813, Beaver6813.com)
44
All rights reserved.
55
------------------------------------------------------------
66
Upgrade Steps

install.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
phpGSB - PHP Google Safe Browsing Implementation
44
Released under New BSD License (see LICENSE)
5-
Copyright (c) 2010-2011, Sam Cleaver (Beaver6813, Beaver6813.com)
5+
Copyright (c) 2010-2012, Sam Cleaver (Beaver6813, Beaver6813.com)
66
All rights reserved.
77
88
INITIAL INSTALLER - RUN ONCE (or more than once if you're adding a new list!)

listupdater.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
phpGSB - PHP Google Safe Browsing Implementation
44
Released under New BSD License (see LICENSE)
5-
Copyright (c) 2010-2011, Sam Cleaver (Beaver6813, Beaver6813.com)
5+
Copyright (c) 2010-2012, Sam Cleaver (Beaver6813, Beaver6813.com)
66
All rights reserved.
77
88
UPDATER EXAMPLE

lookup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
phpGSB - PHP Google Safe Browsing Implementation
44
Released under New BSD License (see LICENSE)
5-
Copyright (c) 2010-2011, Sam Cleaver (Beaver6813, Beaver6813.com)
5+
Copyright (c) 2010-2012, Sam Cleaver (Beaver6813, Beaver6813.com)
66
All rights reserved.
77
88
LOOKUP EXAMPLE

phpgsb.class.php

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?php
22
/*
33
phpGSB - PHP Google Safe Browsing Implementation
4-
Version 0.2.3
4+
Version 0.2.4
55
Released under New BSD License (see LICENSE)
6-
Copyright (c) 2010-2011, Sam Cleaver (Beaver6813, Beaver6813.com)
6+
Copyright (c) 2010-2012, Sam Cleaver (Beaver6813, Beaver6813.com)
77
All rights reserved.
88
*/
99
ob_start();
1010
class phpGSB
1111
{
1212
var $apikey = "";
1313
var $version = "0.2";
14-
var $realversion= "0.2.3";
14+
var $realversion= "0.2.4";
1515
//DO NOT CHANGE API VERSION
1616
var $apiversion = "2.2";
1717

@@ -671,50 +671,42 @@ function validateMethod()
671671

672672
}
673673
}
674-
/*From PHP.NET thanks to FredLudd at gmail dot com and theoriginalmarksimpson at gmail dot com
675-
Parses URL into its component parts, decided to use this against inbuilt function as it
676-
appears to be more accurate and flexible*/
674+
/*Special thanks Steven Levithan (stevenlevithan.com) for the ridiculously complicated regex
675+
required to parse urls. This is used over parse_url as it robustly provides access to
676+
port, userinfo etc and handles mangled urls very well.
677+
Expertly integrated into phpGSB by Sam Cleaver ;)
678+
Thanks to mikegillis677 for finding the seg. fault issue in the old function.
679+
Passed validateMethod() check on 17/01/12*/
677680
function j_parseUrl($url)
678681
{
679-
$r = "(?:([a-z0-9+-._]+)://)?";
680-
$r .= "(?:";
681-
$r .= "(?:((?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9a-f]{2})*)@)?";
682-
$r .= "(?:\[((?:[a-z0-9:])*)\])?";
683-
$r .= "((?:[a-z0-9-._~!$&'()*+,;=]|%[0-9a-f]{2})*)";
684-
$r .= "(?::(\d*))?";
685-
$r .= "(/(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9a-f]{2})*)?";
686-
$r .= "|";
687-
$r .= "(/?";
688-
$r .= "(?:[a-z0-9-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+";
689-
$r .= "(?:[a-z0-9-._~!$&'()*+,;=:@\/]|%[0-9a-f]{2})*";
690-
$r .= ")?";
691-
$r .= ")";
692-
$r .= "(?:\?((?:[a-z0-9-._~!$&'()*+,;=:\/?@]|%[0-9a-f]{2})*))?";
693-
$r .= "(?:#((?:[a-z0-9-._~!$&'()*+,;=:\/?@]|%[0-9a-f]{2})*))?";
694-
preg_match("`$r`i", $url, $match);
695-
$parts = array(
696-
"scheme"=>'',
697-
"userinfo"=>'',
698-
"authority"=>'',
699-
"host"=> '',
700-
"port"=>'',
701-
"path"=>'',
702-
"query"=>'',
703-
"fragment"=>'');
704-
switch (count ($match)) {
705-
case 10: $parts['fragment'] = $match[9];
706-
case 9: $parts['query'] = $match[8];
707-
case 8: $parts['path'] = $match[7];
708-
case 7: $parts['path'] = $match[6] . $parts['path'];
709-
case 6: $parts['port'] = $match[5];
710-
case 5: $parts['host'] = $match[3]?"[".$match[3]."]":$match[4];
711-
case 4: $parts['userinfo'] = $match[2];
712-
case 3: $parts['scheme'] = $match[1];
682+
$strict = '/^(?:([^:\/?#]+):)?(?:\/\/\/?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?(((?:\/(\w:))?((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/';
683+
$loose = '/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((?:\/(\w:))?(\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/';
684+
preg_match($loose, $url, $match);
685+
if(empty($match))
686+
{
687+
//As odd as its sounds, we'll fall back to strict (as technically its more correct and so may salvage completely mangled urls)
688+
unset($match);
689+
preg_match($strict, $url, $match);
690+
}
691+
$parts = array("source"=>'',"scheme"=>'',"authority"=>'',"userinfo"=>'',"user"=>'',"password"=>'',"host"=>'',"port"=>'',"relative"=>'',"path"=>'',"drive"=>'',"directory"=>'',"file"=>'',"query"=>'',"fragment"=>'');
692+
switch (count ($match)) {
693+
case 15: $parts['fragment'] = $match[14];
694+
case 14: $parts['query'] = $match[13];
695+
case 13: $parts['file'] = $match[12];
696+
case 12: $parts['directory'] = $match[11];
697+
case 11: $parts['drive'] = $match[10];
698+
case 10: $parts['path'] = $match[9];
699+
case 9: $parts['relative'] = $match[8];
700+
case 8: $parts['port'] = $match[7];
701+
case 7: $parts['host'] = $match[6];
702+
case 6: $parts['password'] = $match[5];
703+
case 5: $parts['user'] = $match[4];
704+
case 4: $parts['userinfo'] = $match[3];
705+
case 3: $parts['authority'] = $match[2];
706+
case 2: $parts['scheme'] = $match[1];
707+
case 1: $parts['source'] = $match[0];
713708
}
714-
$parts['authority'] = ($parts['userinfo']?$parts['userinfo']."@":"").
715-
$parts['host'].
716-
($parts['port']?":".$parts['port']:"");
717-
return $parts;
709+
return $parts;
718710
}
719711
/*Regex to check if its a numerical IP address*/
720712
function is_ip($ip)

upgrade-0.1.3_to_2.0.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
phpGSB - PHP Google Safe Browsing Implementation
44
Released under New BSD License (see LICENSE)
5-
Copyright (c) 2010-2011, Sam Cleaver (Beaver6813, Beaver6813.com)
5+
Copyright (c) 2010-2012, Sam Cleaver (Beaver6813, Beaver6813.com)
66
All rights reserved.
77
88
UPGRADER FROM 0.1.3 to 0.2 - RUN ONCE

upgrade-0.2.0_to_0.2.1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
phpGSB - PHP Google Safe Browsing Implementation
44
Released under New BSD License (see LICENSE)
5-
Copyright (c) 2010-2011, Sam Cleaver (Beaver6813, Beaver6813.com)
5+
Copyright (c) 2010-2012, Sam Cleaver (Beaver6813, Beaver6813.com)
66
All rights reserved.
77
88
UPGRADER FROM 0.2 to 0.2.1 - RUN ONCE

0 commit comments

Comments
 (0)