update.js
8.15 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* @package AkeebaCMSUpdate
* @copyright Copyright (c)2010-2014 Nicholas K. Dionysopoulos
* @license GNU General Public License version 3, or later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var stat_total = 0;
var stat_files = 0;
var stat_inbytes = 0;
var stat_outbytes = 0;
/**
* An extremely simple error handler, dumping error messages to screen
*
* @param error The error message string
*/
function error_callback(error)
{
alert("ERROR:\n"+error);
}
/**
* Performs an encrypted AJAX request and returns the parsed JSON output.
* The window.ajax_url is used as the AJAX proxy URL.
* If there is no errorCallback, the window.error_callback is used.
*
* @param object data An object with the query data, e.g. a serialized form
* @param function successCallback A function accepting a single object parameter, called on success
* @param function errorCallback A function accepting a single string parameter, called on failure
*/
doEncryptedAjax = function(data, successCallback, errorCallback)
{
var json = JSON.stringify(data);
if( joomlaupdate_password.length > 0 )
{
json = AesCtr.encrypt( json, joomlaupdate_password, 128 );
}
var post_data = {
'json': json
};
var structure =
{
type: "POST",
url: joomlaupdate_ajax_url,
cache: false,
data: post_data,
timeout: 600000,
success: function(msg, responseXML)
{
// Initialize
var junk = null;
var message = "";
// Get rid of junk before the data
var valid_pos = msg.indexOf('###');
if( valid_pos == -1 )
{
// Valid data not found in the response
msg = 'Invalid AJAX data:\n' + msg;
if (errorCallback == null)
{
if(error_callback != null)
{
error_callback(msg);
}
}
else
{
errorCallback(msg);
}
return;
}
else if( valid_pos != 0 )
{
// Data is prefixed with junk
junk = msg.substr(0, valid_pos);
message = msg.substr(valid_pos);
}
else
{
message = msg;
}
message = message.substr(3); // Remove triple hash in the beginning
// Get of rid of junk after the data
var valid_pos = message.lastIndexOf('###');
message = message.substr(0, valid_pos); // Remove triple hash in the end
// Decrypt if required
var data = null;
if( joomlaupdate_password.length > 0 )
{
try
{
var data = JSON.parse(message);
}
catch(err)
{
message = AesCtr.decrypt(message, joomlaupdate_password, 128);
}
}
try
{
if (empty(data))
{
data = JSON.parse(message);
}
}
catch(err)
{
var msg = err.message + "\n<br/>\n<pre>\n" + message + "\n</pre>";
if (errorCallback == null)
{
if (error_callback != null)
{
error_callback(msg);
}
}
else
{
errorCallback(msg);
}
return;
}
// Call the callback function
successCallback(data);
},
error: function(req)
{
var message = 'AJAX Loading Error: ' + req.statusText;
if(errorCallback == null)
{
if (error_callback != null)
{
error_callback(message);
}
}
else
{
errorCallback(message);
}
}
};
jQuery.ajax( structure );
};
/**
* Pings the update script (making sure its executable)
*/
pingExtract = function()
{
// Reset variables
this.stat_files = 0;
this.stat_inbytes = 0;
this.stat_outbytes = 0;
// Do AJAX post
var post = {task : 'ping'};
this.doEncryptedAjax(post,
function(data) {
startExtract(data);
});
};
startExtract = function()
{
// Reset variables
this.stat_files = 0;
this.stat_inbytes = 0;
this.stat_outbytes = 0;
var post = { task : 'startRestore' };
this.doEncryptedAjax(post, function(data){
stepExtract(data);
});
};
stepExtract = function(data)
{
if(data.status == false)
{
// handle failure
error_callback(data.message);
return;
}
if( !empty(data.Warnings) )
{
// @todo Handle warnings
/**
$.each(data.Warnings, function(i, item){
$('#warnings').append(
$(document.createElement('div'))
.html(item)
);
$('#warningsBox').show('fast');
});
/**/
}
if (!empty(data.factory))
{
extract_factory = data.factory;
}
if(data.done)
{
finalizeUpdate();
}
else
{
// Add data to variables
stat_inbytes += data.bytesIn;
stat_percent = (stat_inbytes * 100) / joomlaupdate_totalsize;
// Update GUI
stat_outbytes += data.bytesOut;
stat_files += data.files;
if (stat_percent < 100)
{
jQuery('#progress-bar').css('width', stat_percent + '%').attr('aria-valuenow', stat_percent);
}
else if (stat_percent > 100)
{
stat_percent = 100;
jQuery('#progress-bar').css('width', stat_percent + '%').attr('aria-valuenow', stat_percent);
}
else
{
jQuery('#progress-bar').removeClass('bar-success');
}
jQuery('#extpercent').text(stat_percent.toFixed(1) + '%');
jQuery('#extbytesin').text(stat_inbytes);
jQuery('#extbytesout').text(stat_outbytes);
jQuery('#extfiles').text(stat_files);
// Do AJAX post
post = {
task: 'stepRestore',
factory: data.factory
};
doEncryptedAjax(post, function(data){
stepExtract(data);
});
}
};
finalizeUpdate = function ()
{
// Do AJAX post
var post = { task : 'finalizeRestore', factory: window.factory };
doEncryptedAjax(post, function(data){
window.location = joomlaupdate_return_url;
});
};
/**
* Is a variable empty?
*
* Part of php.js
*
* @see http://phpjs.org/
*
* @param mixed mixed_var The variable
*
* @returns boolean True if empty
*/
function empty (mixed_var)
{
var key;
if (mixed_var === "" ||
mixed_var === 0 ||
mixed_var === "0" ||
mixed_var === null ||
mixed_var === false ||
typeof mixed_var === 'undefined'
){
return true;
}
if (typeof mixed_var == 'object')
{
for (key in mixed_var)
{
return false;
}
return true;
}
return false;
}
/**
* Is the variable an array?
*
* Part of php.js
*
* @see http://phpjs.org/
*
* @param mixed mixed_var The variable
*
* @returns boolean True if it is an array or an object
*/
function is_array (mixed_var)
{
var key = '';
var getFuncName = function (fn) {
var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
if (!name) {
return '(Anonymous)';
}
return name[1];
};
if (!mixed_var)
{
return false;
}
// BEGIN REDUNDANT
this.php_js = this.php_js || {};
this.php_js.ini = this.php_js.ini || {};
// END REDUNDANT
if (typeof mixed_var === 'object')
{
if (this.php_js.ini['phpjs.objectsAsArrays'] && // Strict checking for being a JavaScript array (only check this way if call ini_set('phpjs.objectsAsArrays', 0) to disallow objects as arrays)
(
(this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase &&
this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase() === 'off') ||
parseInt(this.php_js.ini['phpjs.objectsAsArrays'].local_value, 10) === 0)
) {
return mixed_var.hasOwnProperty('length') && // Not non-enumerable because of being on parent class
!mixed_var.propertyIsEnumerable('length') && // Since is own property, if not enumerable, it must be a built-in function
getFuncName(mixed_var.constructor) !== 'String'; // exclude String()
}
if (mixed_var.hasOwnProperty)
{
for (key in mixed_var) {
// Checks whether the object has the specified property
// if not, we figure it's not an object in the sense of a php-associative-array.
if (false === mixed_var.hasOwnProperty(key)) {
return false;
}
}
}
// Read discussion at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_is_array/
return true;
}
return false;
}