-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-my-pma.php
225 lines (194 loc) · 8.94 KB
/
update-my-pma.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
<?php
/**
* Update My PMA (C) Pavel Krejci 2015,2022 v1.2
* includes PMA version checking from http://www.phpmyadmin.net/home_page/version.json
*/
error_reporting(E_ALL & ~E_NOTICE); //debug
ini_set('display_errors', 1); //debug
//---------- EDIT THESE VALUES TO CONFIGURE ---------------------------------
// PMA paths:
$tmp_folder = 'c:\\inetpub\\wwwroot\\phpmyadmin\\upload\\'; // where the zip file will be downloaded
$target_folder = 'c:\\inetpub\\wwwroot\\phpmyadmin'; // where is your PMA, ie. '/var/www/phpmyadmin' on Linux
// Curl SSL setup
define("ALLOW_NON_SSL_FALLBACK", false);
$ca_certs = ""; //path to file with SSL CA certificate(s), set if not defined globally in php.ini (curl.cainfo). You can get one here http://curl.haxx.se/docs/caextract.html
// Notification e-mail parameters:
define("SEND_NOTIFICATION_EMAIL", false);
$Name = "Web name"; //senders name
$email = "noreply@yourdomain.com"; //senders e-mail adress
$recipient = "yourmail@yourdomain.com";
//-----------------------------------------------------------------------------
function getLatestVersion() { //adapted from PMA_Util::getLatestVersion() @Util.class.php
// wait 3s at most for server response, it's enough to get information
// from a working server
$connection_timeout = 3;
$file = 'https://www.phpmyadmin.net/home_page/version.json';
$curl_handle = curl_init($file);
curl_setopt($curl_handle, CURLOPT_HEADER, false);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, $connection_timeout);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, false);
$response = curl_exec($curl_handle);
curl_close($curl_handle);
$data = json_decode($response);
return $data;
}
function getCurrentVersion($target_folder) {
$ConfigClassFile = $target_folder . "/libraries/classes/Version.php";
$VersionCode = 'public const VERSION = ';
$VersionCode2 = '. VERSION_SUFFIX;';
$ConfigClass = file_get_contents($ConfigClassFile, NULL);
$position = strpos($ConfigClass,$VersionCode);
$position2 = strpos($ConfigClass,$VersionCode2);
$VersionLen = $position2 - ($position + strlen($VersionCode));
if ($position === false || $position2 === false || $VersionLen < 3 ) { //check 1
$version = false;
} else {
$version_tmp = trim( substr($ConfigClass,$position + strlen($VersionCode),$VersionLen) , " \t\n\r\0\x0B;,)'/*" );
if ((0 !== preg_match('/(\d+\.?)+/', $version_tmp, $pregmatched))) { //check 2
$version = $pregmatched[0];
} else {
//$version = trim( substr($ConfigClass,$position + strlen($VersionCode)) , " \t\n\r\0\x0B;,)'/*" ); //DEBUG
$version = false;
}
}
unset($ConfigClass); //clear mem
return $version;
}
function ZipStatusString( $status ) {
switch( (int) $status )
{
case ZipArchive::ER_OK : return 'N No error';
case ZipArchive::ER_MULTIDISK : return 'N Multi-disk zip archives not supported';
case ZipArchive::ER_RENAME : return 'S Renaming temporary file failed';
case ZipArchive::ER_CLOSE : return 'S Closing zip archive failed';
case ZipArchive::ER_SEEK : return 'S Seek error';
case ZipArchive::ER_READ : return 'S Read error';
case ZipArchive::ER_WRITE : return 'S Write error';
case ZipArchive::ER_CRC : return 'N CRC error';
case ZipArchive::ER_ZIPCLOSED : return 'N Containing zip archive was closed';
case ZipArchive::ER_NOENT : return 'N No such file';
case ZipArchive::ER_EXISTS : return 'N File already exists';
case ZipArchive::ER_OPEN : return 'S Can\'t open file';
case ZipArchive::ER_TMPOPEN : return 'S Failure to create temporary file';
case ZipArchive::ER_ZLIB : return 'Z Zlib error';
case ZipArchive::ER_MEMORY : return 'N Malloc failure';
case ZipArchive::ER_CHANGED : return 'N Entry has been changed';
case ZipArchive::ER_COMPNOTSUPP : return 'N Compression method not supported';
case ZipArchive::ER_EOF : return 'N Premature EOF';
case ZipArchive::ER_INVAL : return 'N Invalid argument';
case ZipArchive::ER_NOZIP : return 'N Not a zip archive';
case ZipArchive::ER_INTERNAL : return 'N Internal error';
case ZipArchive::ER_INCONS : return 'N Zip archive inconsistent';
case ZipArchive::ER_REMOVE : return 'S Can\'t remove file';
case ZipArchive::ER_DELETED : return 'N Entry has been deleted';
default: return sprintf('Unknown status %s', $status );
}
}
function download_pma($url,$certs="") {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
if (file_exists($certs)) {
curl_setopt($curl, CURLOPT_CAINFO, $certs);
}
$rawout = curl_exec($curl);
if ($rawout === false) {
echo 'Curl error: ' . curl_error($curl)."\n";
}
curl_close($curl);
return $rawout;
}
//main
$version = getLatestVersion();
$currentversion = getCurrentVersion($target_folder);
if (is_object($version)) {
echo("Debug: version check ok!\n");
} else {
//send mail
$mail_body = "PMA version check failed: ".$version; //mail body
$subject = "PMA version check failed"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
if (SEND_NOTIFICATION_EMAIL) mail($recipient, $subject, $mail_body, $header);
die("Fatal Error: PMA version check failed!\n");
}
echo("<pre>\n"); //for readable browser output
print_r(
array(
'new_version' => $version->version,
'date' => $version->date,
'current' => $currentversion,
'update' => (!empty($currentversion) && !empty($version->version) && $version->version <> $currentversion) ? true : false
)
);
if (!empty($currentversion) && !empty($version->version) && ($version->version <> $currentversion)) { //DEBUG!! && false
echo ("New version detected - starting Update\n");
$newpmaname = 'phpMyAdmin-'.$version->version.'-all-languages'; //multilingual version
$newpmazipfilename = $newpmaname.'.zip'; //only ZIP version supported
$newpmazipurl = 'https://files.phpmyadmin.net/phpMyAdmin/'.$version->version.'/'.$newpmazipfilename; //URL since 2015-07
//download start
$dl_file = download_pma($newpmazipurl,$ca_certs);
if ($dl_file === false) {
echo ("Warning: Error downloading PMA\n");
if (strpos($newpmazipurl,"https://") !== false && ALLOW_NON_SSL_FALLBACK) {
echo ("Warning: Falling back to non-SSL URL...\n");
$dl_file = download_pma(str_replace("https://","http://",$newpmazipurl));
}
if ( $dl_file === false) {
die("Fatal Error: Download Failed.");
}
}
if (strlen($dl_file) > 1000000) { //size sanity check
$newpmazip = fopen ($tmp_folder.$newpmazipfilename, 'w+'); //target file for download
fwrite($newpmazip, $dl_file);
fclose($newpmazip);
echo("Download OK.\n");
} else {
die("Fatal Error: Downloaded file too small!\n");
}
unset($dl_file); //clear mem
//download end
//process ZIP file
$zip = new ZipArchive();
$res = $zip->open($tmp_folder.$newpmazipfilename, ZipArchive::CHECKCONS); // ZipArchive::CHECKCONS will enforce additional consistency checks
if ($res === true) {
echo ( "ZIP OK\n");
$listzip = "";
for($i = 0; $i < $zip->numFiles; $i++) {
$listzip = $listzip . ($zip->getNameIndex($i) . "\n");
}
file_put_contents($tmp_folder.$newpmazipfilename.".filelist.log", $listzip); //log filelist
unset($listzip); //clear mem
//rename folder
$i=0;
while($item_name = $zip->getNameIndex($i)){
$newitemname = str_replace( $newpmaname."/", "", $item_name );
if (!empty($newitemname)) {
$zip->renameIndex( $i, $newitemname );
}
$i++;
}
$zip->deleteName($newpmaname."/"); //delete last empty dir
$zip->close(); //synch updated folder structure
//extract ZIP
$res = $zip->open($tmp_folder.$newpmazipfilename);
echo 'Extraction ' . ($zip->extractTo($target_folder) ? "OK\n" : "Error\n"); //modifies archive file modification times
$zip->close();
//send mail
$mail_body = "PMA auto update executed to target ver ".$version->version." released on ".$version->date; //mail body
$subject = "PMA Update executed to ".$version->version; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
if (SEND_NOTIFICATION_EMAIL) mail($recipient, $subject, $mail_body, $header);
} else {
echo ( ZipStatusString( $res ) ."\n");
die("Error opening ZIP file.");
}
} else { //do update
echo ("PMA Update not detected. Nothing to do.");
}
?>