-
Notifications
You must be signed in to change notification settings - Fork 6
/
auto-upgrade.php
266 lines (264 loc) · 10.8 KB
/
auto-upgrade.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
<?php
/*
* Copyright © 2005-2009 Cosmopoly Europe EOOD (http://netenberg.com).
* All Rights Reserved.
*
* This Cosmopoly Europe EOOD work (including software, documents, or
* other related items) is being provided by the copyright holder under
* the following license. By obtaining, using and/or copying this work,
* you (the licensee) agree that you have read, understood, and will
* comply with the following terms and conditions:
*
* Permission to use, copy, modify, and distribute this software and its
* documentation, with or without modification, for any purpose and
* without fee or royalty is hereby granted, provided that you include
* the following on ALL copies of the software and documentation or
* portions thereof, including modifications, that you make:
*
* 1. The full text of this NOTICE in a location viewable to users of the
* redistributed or derivative work.
*
* 2. A short notice of the following form (hypertext is preferred, text
* is permitted) should be used within the body of any redistributed or
* derivative code: "Copyright © 2005-2009 Cosmopoly Europe EOOD
* (http://netenberg.com). All Rights Reserved."
*
* 3. Notice of any changes or modifications to the W3C files, including
* the date changes were made. (We recommend you provide URIs to the
* location from which the code is derived.)
*
* THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT
* HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR FITNESS
* FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE OR
* DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
* TRADEMARKS OR OTHER RIGHTS.
* COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL
* OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR
* DOCUMENTATION.
*
* The name and trademarks of copyright holders may NOT be used in
* advertising or publicity pertaining to the software without specific,
* written prior permission. Title to copyright in this software and any
* associated documentation will at all times remain with copyright
* holders.
*/
require_once realpath(dirname(__FILE__)) . "/" . "init.php";
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{
echo "This feature is not supported in Windows ";
exit;
}
for ($i= 1; $i < $argc; $i++)
{
parse_str($argv[$i]);
}
$arguments = compact('force');
$force = (isset ($arguments['force']) && trim($arguments['force']) == 'uncheck') ? 1 : 0;
$uname = `uname`;
$FREEBSD= preg_match("/freebsd/i", $uname);
$BASE = realpath(dirname(__FILE__)) . "/";
$PARENT = realpath(dirname(__FILE__) . "/../") . "/";
require_once dirname(__FILE__) . "/system/config/version.php";
$current_version = $ALPversion;
$latest_version = getLatestVersionNo("http://files.betaservant.com/files/accountlab_plus/free/version.txt");
$current_version_array = splitVersionNo($current_version);
$latest_version_array = splitVersionNo($latest_version);
$file_name = "accountlab_plus.tar.bz2";
$tgz_file = "http://softlayer.dl.sourceforge.net/project/accountlabplus/" . $file_name;
$cmds = array ();
$cmds['mkdir'] = "";
$cmds['tar'] = "";
$cmds['cp'] = "";
$cmds['rm'] = "";
$cmds['mysqldump']= "";
if (!$force)
{
if (!checkCmds($cmds))
exit;
}
if ($current_version != $latest_version && ($latest_version_array[0] > $current_version_array[0] || ($latest_version_array[0] == $current_version_array[0] && $latest_version_array[1] > $current_version_array[1])))
{
echo "Starting AccountLab plus automatic upgrade utility \n\r";
echo "====================================================\n\r";
echo "Current installation is version : " . $current_version . "\n\rLastest version is : " . $latest_version . "\n\r";
echo "Downloading latest accountlab plus files ...\n\r";
$download_complete= download($tgz_file, $PARENT . $file_name);
if (!is_readable($PARENT . $file_name))
{
echo "Error: The downloaded file is not readable!\n\r";
exit;
}
if ($download_complete)
{
echo "File saved as: ";
echo $PARENT . $file_name . "\n\r";
require_once $BASE . "elements/default/sysvar/db.php";
$commands1[]= "echo \"Extracting new AccountLab Plus files ... \"";
$commands1[]= "mkdir -p " . $PARENT . "alpnew_temporary/";
$commands1[]= "tar -xzpf " . $PARENT . $file_name . " -C " . $PARENT . "alpnew_temporary/";
$commands1[]= "echo \"Creating backup archive of existing installation ...\"";
$commands1[]= "echo \"Dumping mysql database...\"";
$commands1[]= "mysqldump -Q --add-drop-table -c -u " . $db_user . " -p" . $db_pass . " -h " . $db_host . " " . $db_name . " > " . $db_name . time() . ".sql";
$commands1[]= "echo \"Creating backup...\"";
$commands1[]= "mkdir -p " . $PARENT . "alpbkp_temporary/";
$commands1[]= (($FREEBSD) ? "cp -rfp " : "cp -rfpd ") . $BASE . "* " . $PARENT . "alpbkp_temporary/";
$commands1[]= "tar -czpf " . $PARENT . "accountlab_plus_" . str_replace(" ", "_", $current_version) . "_" . time() . "_bkp.tgz " . $PARENT . "alpbkp_temporary/";
$commands2[]= "echo \"Restoring db.php ...\"";
$commands2[]= "cp " . $PARENT . "alpbkp_temporary/elements/default/sysvar/db.php " . $BASE . "elements/default/sysvar/";
$commands2[]= "chmod 644 " . $BASE . "elements/default/sysvar/db.php";
$commands1[]= "echo \"Coping new AccountLab Plus files to install directory ...\"";
$commands1[]= (($FREEBSD) ? "cp -rfp " : "cp -rfpd ") . $PARENT . "alpnew_temporary/accountlab_plus/* " . $BASE;
$commands3[]= "echo \"Removing temporary files ...\"";
$commands3[]= "rm -rf " . $PARENT . "alpbkp_temporary/";
$commands3[]= "rm -rf " . $PARENT . "alpnew_temporary/";
$commands3[]= "rm " . $PARENT . $file_name;
$commands3[]= "rm " . $BASE . "/install.php";
echo "Running unix commands...\n\r";
echo "====================================================\n\r";
foreach ($commands1 as $c1)
{
echo `$c1`;
}
foreach ($commands2 as $c2)
{
echo `$c2`;
}
echo "====================================================\n\r";
echo "New files for AccountLab Plus " . $latest_version . " has been copied.\n\r\n\r";
foreach ($commands3 as $c3)
{
echo `$c3`;
}
echo "Initiating Upgrade process...\n\r";
error_reporting(0);
require_once realpath(dirname(__FILE__)) . "/" . "init.php";
$just_upgraded = $BL->installer->checkandupgrade();
echo "\n\r#################################################################################\n\r";
if($just_upgraded=="OK")
{
echo "Database Upgrade Complete.";
}
elseif($just_upgraded=="NO_CONNECT")
{
echo "Please access your AccountLab Plus admin backend from a browser, to complete the upgrade process.";
}
else
{
echo "Database Upgrade Failed.";
}
echo "\n\r#################################################################################\n\r";
exit;
}
}
function splitVersionNo($version_no)
{
if (preg_match("/r/", $version_no))
{
$temp= explode("r", $version_no, 2);
$version[0]= trim($temp[0]);
$version[1]= trim($temp[1]);
}
else
{
$version[0]= trim($version_no);
$version[1]= 0;
}
return $version;
}
function checkCmds($commands)
{
$return= true;
$msg= "\n\rFollowing shell commands are not installed, or may not have permissions to execute.\n\r";
foreach ($commands as $k => $v)
{
$command_file= "";
$t= "whereis " . $k;
$str= `$t`;
$str= trim($str);
$p1= explode(" ", $str);
foreach ($p1 as $p2)
{
$p2= trim($p2);
$p3= explode("/", $p2);
$c= count($p3);
$p4= $p3[($c -1)];
if (trim($p4) == $k)
{
$command_file= $p2;
}
}
if (!is_executable($command_file) && !is_executable($k))
{
$msg .= "Command: '" . $k . "' is not executable\n\r";
$return= false;
}
}
if (!$return)
{
echo $msg . "\n\r";
exit;
}
return $return;
}
function checkZend($file)
{
$fp= fopen($file, "ab+");
$str= substr(fgets($fp, 5), 0, 4);
fclose($fp);
return ($str == "Zend") ? true : false;
}
function getLatestVersionNo($filename)
{
$fd= fopen($filename, "r");
if (!$fd)
{
echo "Error: Can not open a connection to AccountLab Plus official server!\n\r";
exit;
}
$content= fread($fd, 1024);
fclose($fd);
if (empty ($content))
{
echo "Error: Latest version number can not be resolved!\n\r";
exit;
}
return $content;
}
function download($file_source, $file_target)
{
$rh= fopen($file_source, 'rb');
$wh= fopen($file_target, 'wb');
if ($rh === false || $wh === false)
{
if ($rh === false)
echo "\n\rDownload error: Cannot read from file (" . $file_source . ")\n\r";
else
echo "\n\rDownload error: Cannot write to file (" . $file_target . ")\n\r";
return false;
}
$i= 0;
$j= array (
"|",
"/",
"-",
"\\"
);
while (!feof($rh))
{
if (fwrite($wh, fread($rh, 1024)) === FALSE)
{
echo "\n\rDownload error: Cannot write to file (" . $file_target . ")\n\r";
return false;
}
$i++;
$l= $i -4 * floor($i / 4);
echo "( " . $j[$l] . " )" . $i . " Kilo bytes downloaded so far" . "\r";
}
fclose($rh);
fclose($wh);
echo "\n\rDownload complete. \n\r";
return true;
}
exit;
?>