/
functions.php
306 lines (278 loc) · 12.8 KB
/
functions.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/*
TODO:
-Make a "favorites" zip section
-Add a cron job for creating those favorites
-Make it XHTML valid
-Fix it to die gracefully if curse isn't providing the xml correctly
-Updated blah, but blah was missing
-Use the server's tmp directory
-return stuff in arrays
-Improve session messaging system
*/
class ParseXML{
function GetChildren($vals, &$i) {
$children = array(); // Contains node data
if (isset($vals[$i]['value'])){
$children['VALUE'] = $vals[$i]['value'];
}
while (++$i < count($vals)){
switch ($vals[$i]['type']){
case 'cdata':
if (isset($children['VALUE'])){
$children['VALUE'] .= $vals[$i]['value'];
} else {
$children['VALUE'] = $vals[$i]['value'];
}
break;
case 'complete':
if (isset($vals[$i]['attributes'])) {
$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($children[$vals[$i]['tag']])-1;
if (isset($vals[$i]['value'])){
$children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value'];
} else {
$children[$vals[$i]['tag']][$index]['VALUE'] = '';
}
} else {
if (isset($vals[$i]['value'])){
$children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value'];
} else {
$children[$vals[$i]['tag']][]['VALUE'] = '';
}
}
break;
case 'open':
if (isset($vals[$i]['attributes'])) {
$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($children[$vals[$i]['tag']])-1;
$children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],$this->GetChildren($vals, $i));
} else {
$children[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
}
break;
case 'close':
return $children;
}
}
}
function GetXMLTree($xmlloc){
$data = $xmlloc;
$parser = xml_parser_create('ISO-8859-1');
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parser_free($parser);
$tree = array();
$i = 0;
if (isset($vals[$i]['attributes'])) {
$tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($tree[$vals[$i]['tag']])-1;
$tree[$vals[$i]['tag']][$index] = array_merge($tree[$vals[$i]['tag']][$index], $this->GetChildren($vals, $i));
} else {
$tree[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
}
return $tree;
}
}
function getCurseSessionID(){
/*This function is used to store the Session ID, I got from Curse using a packet sniffer and their
original curse client. I've been using this ID for a while and it hasn't expired. Here's to hoping
it never will.*/
return "VNIBQDUBUCEUPATP";
}
function fork($shellCmd){
$shellCmd = addslashes($shellCmd);
exec("nice sh -c \"$shellCmd\" > /dev/null 2>&1 &");
}
function fetchAddonXML($curseAddonID){
/*This will get the XML file that is associated with the curseAddonID. It contains everything we need
including the URL to the addon and the addon's zip files. Eventually I'd like to make this return the
contents of the file in XML format, rather than a filename of where it's stored.*/
//This registers the $baseURL and $debug variables as global variables so that they can be used inside
//(or outside) this function. If I don't make them global, then only what I return is accessable.
global $baseURL, $debug;
$curseSessionID = getCurseSessionID();
//This is what actually get's the xml file and saves it.
$ch = curl_init('http://addonservice.curse.com/AddOnService.asmx/GetAddOn?pAddOnId='.$curseAddonID.'&pSession='.$curseSessionID);
if (!$ch) die( "Cannot allocate a new PHP-CURL handle" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$xml = curl_exec($ch);
curl_close($ch); //Free up resources that curl was using
$xml = substr($xml, 1);
return $xml;
}
function parseXML($data){
$ParseXML = new ParseXML();
$xmlarray = $ParseXML->GetXMLTree($data);
$latestFile = count($xmlarray['PSYN'][0]['PROJECT'][0]['FILES'][0]['FILE'])-1;
$addonName = $xmlarray['PSYN'][0]['PROJECT'][0]['NAME'][0]['VALUE'];
$addonURL = $xmlarray['PSYN'][0]['PROJECT'][0]['URL'][0]['VALUE'];
$currentDownloadID = $xmlarray['PSYN'][0]['PROJECT'][0]['FILES'][0]['FILE'][$latestFile]['ATTRIBUTES']['ID'];
$currentVersion = $xmlarray['PSYN'][0]['PROJECT'][0]['FILES'][0]['FILE'][$latestFile]['NAME'][0]['VALUE'];
$zipURL = $xmlarray['PSYN'][0]['PROJECT'][0]['FILES'][0]['FILE'][$latestFile]['URL'][0]['VALUE'];
$result = array($addonName, $addonURL, $currentDownloadID, $currentVersion, $zipURL);
return $result;
}
function getContentLength($url){
/*Checks the content length of a file. Used for ajax progress updates.*/
$contentLength = shell_exec('curl -s -I '.$url.' | grep Content-Length');
$contentLength = explode(" ", $contentLength);
$contentLength = $contentLength[1];
return $contentLength;
}
function getIdFromURL($url){
/*Grabs the ID from the "Install via Curse Client" link using a bookmarklet.*/
global $message;
$pieces = explode("=", $url);
if(count($pieces)!=2 && substr($url,0,4) != "psyn"){
$message .= "Invalid URL passed.";
return false;
}
return trim($pieces[1]);
}
function checkForUpdateCompletion($curseAddonID){
/*Checks to see if the "InProgress" file exists - used for ajax queries*/
global $baseURL;
if(file_exists($baseURL.$curseAddonID.'InProgress')) return false;
return true;
}
function checkDownloadProgress($addonName){
global $baseURL;
$sizeInBytes = shell_exec('ls -l '.$baseURL.'cachedZips/'.$addonName.'.zip | awk \'{print $5}\'');
return $sizeInBytes;
}
function addonExists($curseAddonID){
global $debug, $baseURL;
require($baseURL.'config.php');
$query = "SELECT id from amz_addonsList WHERE curseAddonID=".$curseAddonID;
$result = mysql_query($query);
if(mysql_num_rows($result)) return true;
return false;
}
function getVersionsFromZip($userZipLocation, $userExtractLocation){
global $debug, $baseURL;
$zipFilename = "AddonPack-".date('Ymd-His');
if(file_exists($zipFilename)){
$zipFilename .= "-".rand().".zip";
}else{
$zipFilename .= ".zip";
}
shell_exec('unzip "'.$userZipLocation.'" -d "'.$userExtractLocation.'"');
unlink($userZipLocation);
$directoryListing = scandir($userExtractLocation.'/versions');
foreach($directoryListing as $thisFilename){
if($thisFilename != "." || $thisFilename != ".."){
$pieces = explode(".", $thisFilename);
if($pieces[0]){
$addonNames[] = $pieces[0];
$addonHashes[] = file_get_contents($userExtractLocation.'/versions/'.$thisFilename);
}
}
}
$i=0;
foreach($addonNames as $thisAddonName){
if($addonHashes[$i] != file_get_contents($baseURL.'cachedZips/'.$thisAddonName.'.dir/versions/'.$thisAddonName.'.md5')){
$updated = true;
shell_exec('cd "'.$baseURL.'" && cd "cachedZips/'.$thisAddonName.'.dir" && zip -r "../../customZips/'.$zipFilename.'" * && cd ../..');
}else {
shell_exec('cd "'.$baseURL.'" && cd "cachedZips/'.$thisAddonName.'.dir" && zip -r "../../customZips/'.$zipFilename.'" versions && cd ../..');
}
$i++;
}
shell_exec('rm -rf "'.$userExtractLocation.'"');
if($updated) return 'customZips/'.$zipFilename;
return false;
}
function md5Addon($addonName){
global $debug, $baseURL;
$md5Hash = md5_file($baseURL.'cachedZips/'.$addonName.'.zip');
$md5Location = $baseURL.'cachedZips/'.$addonName.'.dir/versions/'.$addonName.'.md5';
$md5Directory = $baseURL.'cachedZips/'.$addonName.'.dir/versions/';
if(!file_exists($md5Directory)) mkdir($md5Directory);
//die('Dir: '.$md5Directory . ' | Loc: ' . $md5Location . ' | Hash: ' . $md5Hash);
file_put_contents($md5Location, $md5Hash);
return;
}
function getDateTime(){
$lastDownloadDateTime = trim(date('Y-m-d H:i:s'));
$lastDownloadDateTimeHuman = trim(date('M j, Y \a\t g:i a'));
return array($lastDownloadDateTime, $lastDownloadDateTimeHuman);
}
function updateAddon($curseAddonID){
global $debug, $baseURL, $currentDateTime, $addonName, $ourAddonName, $zipURL, $addonURL, $currentVersion, $currentDownloadID;
require('config.php');
$addonInfo = parseXML(fetchAddonXML($curseAddonID));
$addonName = $addonInfo[0];
$addonURL = $addonInfo[1];
$currentDownloadID = $addonInfo[2];
$currentVersion = $addonInfo[3];
$zipURL = $addonInfo[4];
$currentDateTime = getDateTime();
$query = "SELECT lastDownloadID from amz_addonsList WHERE curseAddonID=".$curseAddonID;
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$lastDownloadID = trim($row['lastDownloadID']);
}
$query = "UPDATE amz_addonsList SET addonName='$addonName', addonURL='$addonURL', version='$currentVersion', lastUpdateDateTime='$currentDateTime[0]', lastUpdateDateTimeHuman='$currentDateTime[1]' WHERE curseAddonID=$curseAddonID";
if($debug){ echo $query."<br />"; }
$updatesult = mysql_query($query);
if($debug && !$updateResult) die('Invalid query: ' . mysql_error());
if($currentDownloadID != $lastDownloadID) $updatedNeeded = true;
if(!file_exists($baseURL.'cachedZips/'.$addonName.'.zip')) $updatedNeeded = true;
if(!file_exists($baseURL.'cachedZips/'.$addonName.'.dir') && !$updatedNeeded) fork('unzip -d "'.$baseURL.'cachedZips/'.$addonName.'.dir" "'.$baseURL.'cachedZips/'.$addonName.'.zip"');
if(!file_exists($baseURL.'cachedZips/'.$addonName.'.dir/versions/'.$addonName.'.md5') && !$updatedNeeded) md5Addon($addonName);
if(!$updatedNeeded) return false;
$_SESSION['addonName'] = $addonName;
$_SESSION['curseAddonID'] = $curseAddonID;
$_SESSION['addonSize'] = getContentLength($zipURL);
touch($baseURL.$curseAddonID."InProgress");
fork('wget -O "'.$baseURL.'cachedZips/'.$addonName.'.zip" '.$zipURL.' && rm '.$baseURL.$curseAddonID.'InProgress && rm -rf "'.$baseURL.'cachedZips/'.$addonName.'.dir"; unzip -d "'.$baseURL.'cachedZips/'.$addonName.'.dir" "'.$baseURL.'cachedZips/'.$addonName.'.zip" && md5 "'.$baseURL.'cachedZips/'.$addonName.'.zip" -out "'.$baseURL.'cachedZips/'.$addonName.'.dir/md5checksum.txt"');
if(addonExists($curseAddonID)){
$query = "UPDATE amz_addonsList SET addonName='$addonName', version='$currentVersion', addonURL='$addonURL', lastDownloadID=$currentDownloadID, lastDownloadDateTime='$currentDateTime[0]', lastDownloadDateTimeHuman='$currentDateTime[1]', lastUpdateDateTime='$currentDateTime[0]', lastUpdateDateTimeHuman='$currentDateTime[1]' WHERE curseAddonID=$curseAddonID";
}else{
$query = "INSERT INTO amz_addonsList (curseAddonID, addonName, version, addonURL, lastDownloadID, lastDownloadDateTime, lastDownloadDateTimeHuman, lastUpdateDateTime, lastUpdateDateTimeHuman) VALUES ($curseAddonID, '$addonName', '$currentVersion', '$addonURL', $currentDownloadID, '$currentDateTime[0]', '$currentDateTime[1]', '$currentDateTime[0]', '$currentDateTime[1]')";
}
$result = mysql_query($query);
if($debug && !$result) die('Invalid query: ' . mysql_error());
return true;
}
function deleteAddon($curseAddonID){
global $debug, $baseURL, $addonName, $message;
if($curseAddonID == null || !is_numeric($curseAddonID)){
$message .= "Addon ID must contain numbers only. Please select update from the list below.";
return false;
}
require('config.php');
$query = "SELECT addonName from amz_addonsList WHERE curseAddonID=".$curseAddonID;
$result = mysql_query($query);
if(!result) return false;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$addonName = trim($row['addonName']);
}
if(file_exists($baseURL.'cachedZips/'.$addonName.'.dir')){
shell_exec('rm -rf "'.$baseURL.'cachedZips/'.$addonName.'.dir"');
}
if(file_exists($baseURL.'cachedZips/'.$addonName.'.zip')){
shell_exec('rm -rf "'.$baseURL.'cachedZips/'.$addonName.'.zip"');
}
$query = "DELETE FROM amz_addonsList WHERE curseAddonID=".$curseAddonID;
$deleteResult = mysql_query($query);
if(!$deleteResult) return false;
return true;
}
function oldParseXML($xml){
if(!isset($xml)) return false;
global $debug, $lastDownloadDateTime, $lastDownloadDateTimeHuman, $addonName, $zipURL, $addonURL, $currentVersion, $currentDownloadID;
//$line = explode("<name>",$xml);
///// YOU WERE HERE
//die(print_r($line));
$addonName = addslashes(trim(shell_exec('cat '.$xml.' | tr -s \'><\' \'\n\' | grep -a -m 1 -A 1 name | tail -1')));
$zipURL = trim(shell_exec('cat '.$xml.' | tr -s \'><\' \'\n\' | grep -a -B 2 date | grep zip | tail -1'));
$addonURL = trim(shell_exec('cat '.$xml.' | tr -s \'><\' \'\n\' | grep -a -m 1 aspx'));
$currentVersion = trim(shell_exec('cat '.$xml.' | tr -s \'><\' \'\n\' | grep -a -A 2 \'file id\' | tail -1'));
$currentDownloadID = trim(shell_exec('cat '.$xml.' | tr -s \'><\' \'\n\' | grep -a \'file id\' | tail -1 | awk -F\'"\' \'{print $2}\''));
unlink($xml);
return true;
}
?>