-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoPHP4.pl
64 lines (51 loc) · 2.2 KB
/
toPHP4.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Convert RPCServer.class.php from PHP5 into PHP4
# Weston Ruter <http://weston.ruter.net/>
# July 2007
open PHP, "RPCServer.class.php";
$source = join '', <PHP>;
close PHP;
open PHP, ">RPCServer.class.php4";
#Change the PHP version info
$source =~ s{(?<=JSON/XML-RPC Server in )PHP5}{PHP4};
$source =~ s{# //\$server->.+?\n}{# \$server->processRequest();\n};
$source =~ s{'RPCServer\.class\.php'}{'RPCServer.class.php4'}g;
$source =~ s{date_default_timezone_set\("UTC"\)}{putenv('TZ=UTC')};
#Remove "self"
$source =~ s{self(?=::)}{RPCServer}g;
#Replace constants
#%consts = ();
@constPairs = ($source =~ m{const\s+(\w+)\s*=\s*(.+?);}g);
for($i = 0; $i < @constPairs; $i+=2){
$constName = $constPairs[$i];
$constValue = $constPairs[$i+1];
$source =~ s{const\s+$constName}{var \$$constName}g;
#$source =~ s{::$constName\b}{::\$$constName}g;
$source =~ s{(RPCServer|self)::$constName\b}{\$this->$constName}g;
#$consts{$constName} = $constValue;
}
#Remove member visibility keywords for functions
$source =~ s{((?:public|private|protected|static)\s*)+(?=function)}{}g;
#Remove member visibility keywords for members
$source =~ s{((?:public|private|protected|static)\s*)+(?=\s\$(?!instance))}{var}g;
#Make callbacks to static PHP5 functions
$source =~ s{array\('RPCServer', }{array(&\$this, }g;
$source =~ s{RPCServer::\$charToJSON}{\$this->charToJSON}g;
#Remove PHP5 code
#$source =~ s{(\t| )/\*BEGIN PHP5\*/.+?/\*END PHP5\*/}{}gs;
$source =~ s{/\*BEGIN PHP5\*/}{/\*BEGIN PHP5\*\*}g;
$source =~ s{/\*END PHP5\*/}{\*\*END PHP5\*/}g;
$source =~ s{/\*BEGIN PHP4\*\*}{/\*BEGIN PHP4\*/}g;
$source =~ s{\*\*END PHP4\*/}{/\*END PHP4\*/}g;
#Allow for DOM XML Functions
$source =~ s{->documentElement}{->document_element()}g;
$source =~ s{->getElementsByTagName\b}{->get_elements_by_tagname}g;
$source =~ s{->item\(([^\)]+)\)}{[$1]}g;
$source =~ s{->firstChild}{->first_child}g;
$source =~ s{->nodeValue}{->node_value}g;
$source =~ s{->nextSibling}{->next_sibling}g;
$source =~ s{->nodeType}{->node_type}g;
$source =~ s{->nodeName}{->node_name}g;
$source =~ s{->childNodes}{->child_nodes}g;
$source =~ s{((?:\$?\w+)(?:->\$?\w+)*)->length}{count($1)}g;
print PHP $source;
close PHP;