-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.synchronize.php
1139 lines (1026 loc) · 38.9 KB
/
class.synchronize.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* syncData
*
* @author Ralf Hertsch ([email protected])
* @link http://phpmanufaktur.de
* @copyright 2011
* @license GNU GPL (http://www.gnu.org/licenses/gpl.html)
* @version $Id$
*/
// include LEPTON class.secure.php to protect this file and the whole CMS!
$class_secure = '../../framework/class.secure.php';
if (file_exists($class_secure)) {
include($class_secure);
}
else {
trigger_error(sprintf("[ <b>%s</b> ] Can't include LEPTON class.secure.php!", $_SERVER['SCRIPT_NAME']), E_USER_ERROR);
}
// include language file for syncData
if(!file_exists(WB_PATH .'/modules/'.basename(dirname(__FILE__)).'/languages/' .LANGUAGE .'.php')) {
require_once(WB_PATH .'/modules/'.basename(dirname(__FILE__)).'/languages/EN.php');
if (!defined('SYNC_DATA_LANGUAGE')) define('SYNC_DATA_LANGUAGE', 'EN');
}
else {
require_once(WB_PATH .'/modules/'.basename(dirname(__FILE__)).'/languages/' .LANGUAGE .'.php');
if (!defined('SYNC_DATA_LANGUAGE')) define('SYNC_DATA_LANGUAGE', LANGUAGE);
}
if (!class_exists('Dwoo')) {
// try to load regular Dwoo
if (file_exists(WB_PATH.'/modules/dwoo/include.php')) {
require_once WB_PATH.'/modules/dwoo/include.php';
}
else {
// load Dwoo from include directory
require_once WB_PATH.'/modules/'.basename(dirname(__FILE__)).'/include/dwoo/dwooAutoload.php';
}
}
$cache_path = WB_PATH.'/temp/cache';
if (!file_exists($cache_path)) mkdir($cache_path, 0777, true);
$compiled_path = WB_PATH.'/temp/compiled';
if (!file_exists($compiled_path)) mkdir($compiled_path, 0777, true);
global $parser;
if (!is_object($parser)) $parser = new Dwoo($compiled_path, $cache_path);
if (!class_exists('dbconnectle')) {
// try to load regular dbConnect_LE
if (file_exists(WB_PATH.'/modules/dbconnect_le/include.php')) {
require_once WB_PATH.'/modules/dbconnect_le/include.php';
}
else {
// load dbConnect_LE from include directory
require_once WB_PATH.'/modules/'.basename(dirname(__FILE__)).'/include/dbconnect_le/include.php';
}
}
if (!class_exists('kitToolsLibrary')) {
// try to load required kitTools
if (file_exists(WB_PATH.'/modules/kit_tools/class.tools.php')) {
require_once WB_PATH.'/modules/kit_tools/class.tools.php';
}
else {
// load embedded kitTools library
require_once WB_PATH.'/modules/'.basename(dirname(__FILE__)).'/class.tools.php';
}
}
global $kitTools;
if (!is_object($kitTools)) $kitTools = new kitToolsLibrary();
if (file_exists(WB_PATH.'/modules/pclzip/pclzip.lib.php')) {
// LEPTON 1.x
require_once WB_PATH.'/modules/pclzip/pclzip.lib.php';
}
elseif (file_exists(WB_PATH.'/modules/lib_pclzip/pclzip.lib.php')) {
// LEPTON 2.x
require_once WB_PATH.'/modules/lib_pclzip/pclzip.lib.php';
}
elseif (file_exists(WB_PATH.'/include/pclzip/pclzip.lib.php')) {
// WebsiteBaker
require_once WB_PATH.'/include/pclzip/pclzip.lib.php';
}
else {
trigger_error(sprintf("[ <b>%s</b> ] Unable to find pclzip!", $_SERVER['SCRIPT_NAME']), E_USER_ERROR);
}
// set temporary directory for pclzip
if (!defined('PCLZIP_TEMPORARY_DIR')) define('PCLZIP_TEMPORARY_DIR', WB_PATH.'/temp/');
require_once WB_PATH.'/modules/'.basename(dirname(__FILE__)).'/class.syncdata.php';
global $dbSyncDataCfg;
if (!is_object($dbSyncDataCfg)) $dbSyncDataCfg = new dbSyncDataCfg();
global $dbSyncDataJob;
if (!is_object($dbSyncDataJob)) $dbSyncDataJob = new dbSyncDataJobs();
global $dbSyncDataArchive;
if (!is_object($dbSyncDataArchive)) $dbSyncDataArchive = new dbSyncDataArchives();
require_once WB_PATH.'/framework/functions.php';
require_once WB_PATH.'/modules/'.basename(dirname(__FILE__)).'/class.interface.php';
class syncServer {
const request_action = 'act';
const request_archive_id = 'id';
const request_archive_number = 'no';
const action_default = 'def';
const action_connect = 'con';
const action_info = 'inf';
const result_status = 'status';
const result_message = 'message';
const result_archive_id = 'archive_id';
const result_archive_number = 'archive_number';
const result_archive_file = 'archive_file';
const result_archive_md5 = 'archive_md5';
const result_archive_size = 'archive_size';
const result_archive_timestamp = 'archive_timestamp';
const status_ok = 1;
const status_error = 0;
private $backup_path = '';
public function __construct() {
$this->backup_path = WB_PATH.MEDIA_DIRECTORY.'/sync_data/backup/';
} // __construct()
/**
* Return Version of Module
*
* @return FLOAT
*/
public function getVersion() {
// read info.php into array
$info_text = file(WB_PATH.'/modules/'.basename(dirname(__FILE__)).'/info.php');
if ($info_text == false) {
return -1;
}
// walk through array
foreach ($info_text as $item) {
if (strpos($item, '$module_version') !== false) {
// split string $module_version
$value = explode('=', $item);
// return floatval
return floatval(preg_replace('([\'";,\(\)[:space:][:alpha:]])', '', $value[1]));
}
}
return -1;
} // getVersion()
/**
* Verhindert XSS Cross Site Scripting
*
* @param REFERENCE $_REQUEST Array
* @return $request
*/
public function xssPrevent(&$request) {
if (is_string($request)) {
$request = html_entity_decode($request);
$request = strip_tags($request);
$request = trim($request);
$request = stripslashes($request);
}
return $request;
} // xssPrevent()
public function action() {
$html_allowed = array();
foreach ($_REQUEST as $key => $value) {
if (!in_array($key, $html_allowed)) {
$_REQUEST[$key] = $this->xssPrevent($value);
}
}
$action = isset($_REQUEST[self::request_action]) ? $_REQUEST[self::request_action] : self::action_default;
switch ($action):
case self::action_info:
$result = $this->actionInfo();
break;
case self::action_connect:
$result = $this->actionConnect();
break;
default:
$result = $this->actionForbidden();
break;
endswitch;
// return serialized result array
echo serialize($result);
} // action()
public function actionForbidden() {
$result = array(
self::result_status => self::status_error,
self::result_message => sync_error_sync_action_forbidden
);
return $result;
} // actionForbidden()
private function actionConnect() {
global $dbSyncDataCfg;
global $dbSyncDataJob;
global $dbSyncDataArchive;
$server_active = $dbSyncDataCfg->getValue(dbSyncDataCfg::cfgServerActive);
if ($server_active == 0) {
// the server is not active
$result = array(
self::result_status => self::status_error,
self::result_message => sync_error_sync_server_inactive
);
return $result;
}
$archive_id = $dbSyncDataCfg->getValue(dbSyncDataCfg::cfgServerArchiveID);
if (empty($archive_id)) {
// server is active but there is no archive ID defined!
$result = array(
self::result_status => self::status_error,
self::result_message => sync_error_sync_archive_id_missing
);
return $result;
}
// check if an archive file exists
$SQL = sprintf( "SELECT * FROM %s,%s WHERE %s=%s AND %s=%s AND %s='%s' AND %s='%s' ORDER BY %s DESC LIMIT 1",
$dbSyncDataJob->getTableName(),
$dbSyncDataArchive->getTableName(),
dbSyncDataJobs::field_archive_id,
dbSyncDataArchives::field_archive_id,
dbSyncDataJobs::field_archive_number,
dbSyncDataArchives::field_archive_number,
dbSyncDataJobs::field_archive_id,
$archive_id,
dbSyncDataJobs::field_status,
dbSyncDataJobs::status_finished,
dbSyncDataJobs::field_archive_number);
if (!$dbSyncDataJob->sqlExec($SQL, $job)) {
// error requesting data
$result = array(
self::result_status => self::status_error,
self::result_message => sprintf('[%s - %s] %s', __METHOD__, __LINE__, $dbSyncDataJob->getError())
);
return $result;
}
if (count($job) < 1) {
// no job for this archive ID available
$result = array(
self::result_status => self::status_error,
self::result_message => sprintf(sync_error_sync_archive_id_invalid, $archive_id)
);
return $result;
}
// check job and archive
$job = $job[0];
$archive_file = page_filename($job[dbSyncDataArchives::field_archive_name].'.zip');
if (!file_exists($this->backup_path.$archive_file)) {
// backup archive ZIP file does not exists
$result = array(
self::result_status => self::status_error,
self::result_message => sprintf(sync_error_sync_archive_file_missing, $archive_file)
);
return $result;
}
if (false === ($md5 = md5_file($this->backup_path.$archive_file))) {
// error getting md5 checksum
$result = array(
self::result_status => self::status_error,
self::result_message => sprintf(sync_error_sync_archive_file_get_md5, $archive_file)
);
return $result;
}
if (false ===($size = filesize($this->backup_path.$archive_file))) {
// error getting filesize
$result = array(
self::result_status => self::status_error,
self::result_message => sprintf(sync_error_sync_archive_filesize, $archive_file)
);
}
$result = array(
self::result_status => self::status_ok,
self::result_message => '',
self::result_archive_id => $archive_id,
self::result_archive_md5 => $md5,
self::result_archive_file => $archive_file,
self::result_archive_number => $job[dbSyncDataJobs::field_archive_number],
self::result_archive_size => $size,
self::result_archive_timestamp=> $job[dbSyncDataJobs::field_timestamp]
);
return $result;
} // actionConnect()
/**
* Return the informations for the requested Archive by Archive ID
* and Archive Number
*
* @return ARRAY $result
*/
public function actionInfo() {
global $dbSyncDataArchive;
global $dbSyncDataJob;
if (!isset($_REQUEST[self::request_archive_id]) || !isset($_REQUEST[self::request_archive_number])) {
$result = array(
self::result_status => self::status_error,
self::result_message => sync_error_sync_missing_params
);
return $result;
}
$SQL = sprintf( "SELECT * FROM %s,%s WHERE %s=%s AND %s=%s AND %s='%s' AND %s='%s' AND %s='%s'",
$dbSyncDataArchive->getTableName(),
$dbSyncDataJob->getTableName(),
dbSyncDataArchives::field_archive_id,
dbSyncDataJobs::field_archive_id,
dbSyncDataArchives::field_archive_number,
dbSyncDataJobs::field_archive_number,
dbSyncDataJobs::field_archive_id,
$_REQUEST[self::request_archive_id],
dbSyncDataJobs::field_archive_number,
$_REQUEST[self::request_archive_number],
dbSyncDataJobs::field_status,
dbSyncDataJobs::status_finished);
$job = array();
if (!$dbSyncDataJob->sqlExec($SQL, $job)) {
$result = array(
self::result_status => self::status_error,
self::result_message => sprintf('[%s - %s] %s', __METHOD__, __LINE__, $dbSyncDataJob->getError())
);
return $result;
}
if (count($job) < 1) {
// requested archive does not exists
$result = array(
self::result_status => self::status_error,
self::result_message => sprintf(sync_error_archive_id_invalid, $_REQUEST[self::request_archive_id])
);
return $result;
}
$job = $job[0];
$archive_file = page_filename($job[dbSyncDataArchives::field_archive_name].'.zip');
if (!file_exists($this->backup_path.$archive_file)) {
// backup archive ZIP file does not exists
$result = array(
self::result_status => self::status_error,
self::result_message => sprintf(sync_error_sync_archive_file_missing, $archive_file)
);
return $result;
}
if (false === ($md5 = md5_file($this->backup_path.$archive_file))) {
// error getting md5 checksum
$result = array(
self::result_status => self::status_error,
self::result_message => sprintf(sync_error_sync_archive_file_get_md5, $archive_file)
);
return $result;
}
if (false ===($size = filesize($this->backup_path.$archive_file))) {
// error getting filesize
$result = array(
self::result_status => self::status_error,
self::result_message => sprintf(sync_error_sync_archive_filesize, $archive_file)
);
}
$result = array(
self::result_status => self::status_ok,
self::result_message => '',
self::result_archive_id => $job[dbSyncDataJobs::field_archive_id],
self::result_archive_md5 => $md5,
self::result_archive_file => $archive_file,
self::result_archive_number => $job[dbSyncDataJobs::field_archive_number],
self::result_archive_size => $size,
self::result_archive_timestamp=> $job[dbSyncDataJobs::field_timestamp]
);
return $result;
} // actionInfo()
} // class syncServer
/**
* The class for the syncData CLIENT - called by the droplet sync_client
*
* @author Ralf Hertsch
*
*/
class syncClient {
const request_action = 'act';
const request_job_id = 'job';
const action_default = 'def';
const action_check_for_updates = 'cup';
const action_update_download = 'udl';
const action_update_start = 'ust';
const action_update_continue = 'upc';
private $page_link = '';
private $template_path = '';
private $error = '';
private $message = '';
private $temp_path = '';
private $image_url = '';
const param_preset = 'preset';
//const param_server = 'server';
const param_css = 'css';
private $params = array(
self::param_preset => 1,
//self::param_server => '',
self::param_css => true,
);
private $server_url = '';
const session_server_request = 'sync_server_request';
const session_server_url = 'sync_server_url';
const session_further_update = 'sync_server_further_update';
public function __construct() {
global $kitTools;
global $dbSyncDataCfg;
$url = '';
$_SESSION['FRONTEND'] = true;
$kitTools->getPageLinkByPageID(PAGE_ID, $url);
$this->page_link = $url;
$this->template_path = WB_PATH.'/modules/'.basename(dirname(__FILE__)).'/templates/'.$this->params[self::param_preset].'/'.SYNC_DATA_LANGUAGE.'/' ;
date_default_timezone_set(sync_cfg_time_zone);
$this->temp_path = WB_PATH.'/temp/';
$this->image_url = WB_URL.'/modules/'.basename(dirname(__FILE__)).'/images/';
$this->memory_limit = $dbSyncDataCfg->getValue(dbSyncDataCfg::cfgMemoryLimit);
ini_set("memory_limit",$this->memory_limit);
$this->max_execution_time = $dbSyncDataCfg->getValue(dbSyncDataCfg::cfgMaxExecutionTime);
$this->limit_execution_time = $dbSyncDataCfg->getValue(dbSyncDataCfg::cfgLimitExecutionTime);
set_time_limit($this->max_execution_time);
// setting server URL
$server_url = $dbSyncDataCfg->getValue(dbSyncDataCfg::cfgServerURL);
$this->server_url = (empty($server_url)) ? WB_URL : $server_url;
} // __construct()
/**
* Set $this->error to $error
*
* @param STR $error
*/
public function setError($error) {
$this->error = $error;
} // setError()
/**
* Get Error from $this->error;
*
* @return STR $this->error
*/
public function getError() {
return $this->error;
} // getError()
/**
* Check if $this->error is empty
*
* @return BOOL
*/
public function isError() {
return (bool) !empty($this->error);
} // isError
/**
* Reset Error to empty String
*/
public function clearError() {
$this->error = '';
}
/** Set $this->message to $message
*
* @param STR $message
*/
public function setMessage($message) {
$this->message = $message;
} // setMessage()
/**
* Get Message from $this->message;
*
* @return STR $this->message
*/
public function getMessage() {
return $this->message;
} // getMessage()
/**
* Check if $this->message is empty
*
* @return BOOL
*/
public function isMessage() {
return (bool) !empty($this->message);
} // isMessage
/**
* Return Version of Module
*
* @return FLOAT
*/
public function getVersion() {
// read info.php into array
$info_text = file(WB_PATH.'/modules/'.basename(dirname(__FILE__)).'/info.php');
if ($info_text == false) {
return -1;
}
// walk through array
foreach ($info_text as $item) {
if (strpos($item, '$module_version') !== false) {
// split string $module_version
$value = explode('=', $item);
// return floatval
return floatval(preg_replace('([\'";,\(\)[:space:][:alpha:]])', '', $value[1]));
}
}
return -1;
} // getVersion()
/**
* Get the desired $template within the template path, fills in the
* $template_data and return the template output
*
* @param STR $template
* @param ARRAY $template_data
* @return STR|BOOL template or FALSE on error
*/
public function getTemplate($template, $template_data) {
global $parser;
$result = 'TEMPLATE ERROR!';
try {
$result = $parser->get($this->template_path.$template, $template_data);
} catch (Exception $e) {
$this->setError(sprintf(sync_error_template_error, $template, $e->getMessage()));
return false;
}
return $result;
} // getTemplate()
/**
* Get the params
*
* @return ARRAY self::params
*/
public function getParams() {
return $this->params;
} // getParams()
/**
* Set parameters
*
* @param ARRAY $params
* @return BOOL
*/
public function setParams($params = array()) {
$this->params = $params;
$this->template_path = WB_PATH.'/modules/'.basename(dirname(__FILE__)).'/templates/'.$this->params[self::param_preset].'/'.SYNC_DATA_LANGUAGE.'/';
if (!file_exists($this->template_path)) {
$this->setError(sprintf(sync_error_preset_not_exists, '/modules/'.basename(dirname(__FILE__)).'/templates/'.$this->params[self::param_preset].'/'.SYNC_DATA_LANGUAGE.'/'));
return false;
}
return true;
} // setParams()
/**
* Verhindert XSS Cross Site Scripting
*
* @param REFERENCE $_REQUEST Array
* @return $request
*/
public function xssPrevent(&$request) {
if (is_string($request)) {
$request = html_entity_decode($request);
$request = strip_tags($request);
$request = trim($request);
$request = stripslashes($request);
}
return $request;
} // xssPrevent()
/**
* Action handler of the class
*
* @return STR dialog or message
*/
public function action() {
$html_allowed = array();
foreach ($_REQUEST as $key => $value) {
if (!in_array($key, $html_allowed)) {
$_REQUEST[$key] = $this->xssPrevent($value);
}
}
//if (($this->params[self::param_server] == WB_URL) && (!isset($_SESSION[self::session_server_url]))) {
if (($this->server_url == WB_URL) && (!isset($_SESSION[self::session_server_url]))) {
// don't execute the droplet at the server!
// it is possible that the server param is replaced by the update process, so check the session too!
return '<div class="sync_data_inactive"></div>';
}
$action = isset($_REQUEST[self::request_action]) ? $_REQUEST[self::request_action] : self::action_default;
switch ($action):
case self::action_update_continue:
$result = $this->updateContinue();
break;
case self::action_update_start:
$result = $this->updateStart();
break;
case self::action_update_download:
$result = $this->updateDownload();
break;
case self::action_check_for_updates:
$result = $this->checkForUpdates();
break;
default:
$result = $this->dlgWelcome();
break;
endswitch;
if ($this->isError()) {
$data = array('error' => $this->getError());
$result = $this->getTemplate('error.lte', $data);
}
return $result;
} // action
/**
* Save the desired $url to the path $save_to
*
* @param STR $url
* @param STR $save_to
* @return BOOL
*/
public function saveURL($url, $save_to) {
if (in_array('curl', get_loaded_extensions())) {
// preferred method: cUrl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (false === ($data = curl_exec($ch))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, curl_error($ch)));
curl_close($ch);
return false;
}
curl_close($ch);
}
elseif (ini_get('allow_url_fopen') == 1) {
// use file_get_contents() instead
if (false === ($data = file_get_contents($url))) {
$this->setError(sprintf('[%s %s] %s', __METHOD__, __LINE__, sprintf(sync_error_file_get_contents, $url)));
return false;
}
}
else {
// no method found
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sync_error_allow_url_fopen));
return false;
}
// sace the data local
if (!file_put_contents($save_to, $data)) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(sync_error_file_put_contents, $save_to)));
return false;
}
return true;
} // saveURL()
/**
* Return the contents of the desired $url
*
* @param STR $url
* @return MIXED STR $data on success BOOL FALSE on error
*/
public function getURL($url) {
if (in_array('curl', get_loaded_extensions())) {
// preferred method: cUrl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (false === ($data = curl_exec($ch))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, curl_error($ch)));
curl_close($ch);
return false;
}
curl_close($ch);
return $data;
}
elseif (ini_get('allow_url_fopen') == 1) {
if (false !== ($data = @file_get_contents($url))) {
return $data;
}
else {
$this->setError(sprintf('[%s %s] %s', __METHOD__, __LINE__, sprintf(sync_error_file_get_contents, $url)));
return false;
}
}
else {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sync_error_allow_url_fopen));
return false;
}
} // getURL
/**
* Check if an internet connection is established
*
* @param STR $url
* @return BOOL $result
*/
public function checkConnection($url) {
if (in_array('curl', get_loaded_extensions())) {
// preferred method: cUrl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (false === ($data = curl_exec($ch))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, curl_error($ch)));
curl_close($ch);
return false;
}
curl_close($ch);
return true;
}
elseif (ini_get('allow_url_fopen') == 1) {
// use file_get_contents()
return (false !== ($data = file_get_contents($url))) ? true : false;
}
else {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sync_error_allow_url_fopen));
return false;
}
} // checkConnection()
/**
* Display a welcome dialog to the user
*
* @return MIXED STR dialog on success BOOL FALSE on error
*/
public function dlgWelcome() {
//if (empty($this->params[self::param_server])) {
if (empty($this->server_url)) {
// es ist kein Server angegeben
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sync_error_param_missing_server));
return false;
}
$data = array('action_link' => sprintf('%s?%s', $this->page_link, http_build_query(array(self::request_action => self::action_check_for_updates))));
return $this->getTemplate('welcome.lte', $data);
} // dlgWelcome()
/**
* Check the desired Server URL wether updates exists or not
*
* @return MIXED STR dialog on success or BOOL FALSE on error
*/
public function checkForUpdates() {
global $dbSyncDataJob;
//if (!$this->checkConnection($this->params[self::param_server])) {
if (!$this->checkConnection($this->server_url)) {
// es kann keine Verbindung zu dem Server aufgebaut werden
$data = array(
'server_url' => $this->server_url, //$this->params[self::param_server],
'action_link' => sprintf('%s?%s', $this->page_link, http_build_query(array(self::request_action => self::action_check_for_updates)))
);
return $this->getTemplate('offline.lte', $data);
}
// check if confirmation logs must be transmitted
if (file_exists(WB_PATH.'/modules/tool_confirmation_log/interface.php')) {
require_once WB_PATH.'/modules/tool_confirmation_log/interface.php';
$status = '';
if (!transmit($this->server_url, $status)) {
$data = array('message' => sprintf(sync_msg_confirmation_transmit_failed, $status));
return $this->getTemplate('message.lte', data);
}
}
// get the main params for the archive file
//if (false ===($response = $this->getURL(sprintf('%s/modules/sync_data/response.php?%s', $this->params[self::param_server], http_build_query(array(syncServer::request_action => syncServer::action_connect)))))) {
if (false ===($response = $this->getURL(sprintf('%s/modules/sync_data/response.php?%s', $this->server_url, http_build_query(array(syncServer::request_action => syncServer::action_connect)))))) {
$data = array('message' => sprintf(sync_msg_sync_connect_failed, $this->page_link));
return $this->getTemplate('message.lte', $data);
}
// unserialize the request
$request = unserialize($response);
// check the status and message key of the response
if (!isset($request[syncServer::result_status]) || !isset($request[syncServer::result_message])) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(sync_error_sync_response_invalid, $response)));
return false;
}
// syncData Server error?
if ($request[syncServer::result_status] == syncServer::status_error) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $request[syncServer::result_message]));
return false;
}
// all keys complete?
if (!isset($request[syncServer::result_archive_file]) || !isset($request[syncServer::result_archive_id]) ||
!isset($request[syncServer::result_archive_md5]) || !isset($request[syncServer::result_archive_number]) ||
!isset($request[syncServer::result_archive_size]) || !isset($request[syncServer::result_archive_timestamp])) {
// missing keys in the $request array
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sync_error_sync_missing_keys));
return false;
}
// check if the archive exists and get the last archive number
$SQL = sprintf( "SELECT * FROM %s WHERE %s='%s' AND %s='%s' ORDER BY %s DESC LIMIT 1",
$dbSyncDataJob->getTableName(),
dbSyncDataJobs::field_archive_id,
$request[syncServer::result_archive_id],
dbSyncDataJobs::field_status,
dbSyncDataJobs::status_finished,
dbSyncDataJobs::field_archive_number);
$archive = array();
if (!$dbSyncDataJob->sqlExec($SQL, $archive)) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $dbSyncDataJob->getError()));
return false;
}
if (count($archive) < 1) {
// this archive does not exist (missing initial restore)
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(sync_error_sync_missing_initial_restore, $request[syncServer::result_archive_id])));
return false;
}
$archive = $archive[0];
if ($archive[dbSyncDataJobs::field_archive_number] == $request[syncServer::result_archive_number]) {
// this installation is up to date...
$data = array();
return $this->getTemplate('update.uptodate.lte', $data);
}
elseif ($archive[dbSyncDataJobs::field_archive_number]+1 == $request[syncServer::result_archive_number]) {
// ok - this is the next update which should be installed
return $this->dlgExecUpdate($request);
}
elseif ($archive[dbSyncDataJobs::field_archive_number] < $request[syncServer::result_archive_number]) {
// missing one or more updates - load the next update!
// get the main params for the archive file
if (false ===($response = $this->getURL(sprintf('%s/modules/sync_data/response.php?%s', $this->server_url, //$this->params[self::param_server],
http_build_query(array(
syncServer::request_action => syncServer::action_info,
syncServer::request_archive_id => $request[syncServer::result_archive_id],
syncServer::request_archive_number => $archive[dbSyncDataJobs::field_archive_number]+1)))))) {
$data = array('message' => sprintf(sync_msg_sync_connect, $this->page_link));
return $this->getTemplate('message.lte', $data);
}
// unserialize the request
$request = unserialize($response);
// check the status and message key of the response
if (!isset($request[syncServer::result_status]) || !isset($request[syncServer::result_message])) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(sync_error_sync_response_invalid, $response)));
return false;
}
// syncData Server error?
if ($request[syncServer::result_status] == syncServer::status_error) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $request[syncServer::result_message]));
return false;
}
// all keys complete?
if (!isset($request[syncServer::result_archive_file]) || !isset($request[syncServer::result_archive_id]) ||
!isset($request[syncServer::result_archive_md5]) || !isset($request[syncServer::result_archive_number]) ||
!isset($request[syncServer::result_archive_size]) || !isset($request[syncServer::result_archive_timestamp])) {
// missing keys in the $request array
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sync_error_sync_missing_keys));
return false;
}
// set SESSION to mark that a further update is available!
$_SESSION[self::session_further_update] = true;
return $this->dlgExecUpdate($request);
}
else {
// Oooops - data corrupt?
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sync_error_sync_data_corrupt, $request[syncServer::request_archive_id]));
return false;
}
} // checkForUpdates()
/**
* Exec an dialog for processing an available update
*
* @param ARRAY $request
* @return STR dialog
*/
public function dlgExecUpdate($request) {
$_SESSION[self::session_server_request] = $request;
$_SESSION[self::session_server_url] = $this->server_url; //$this->params[self::param_server];
$data = array(
'action_link' => sprintf('%s?%s', $this->page_link, http_build_query(array(self::request_action => self::action_update_download))),
'img_url' => $this->image_url
);
return $this->getTemplate('update.available.lte', $data);
} // execUpdate()
/**
* Process the download from update server and check the MD5 of the archive
*
* @return MIXED STR update dialog or BOOL FALSE on error
*/
public function updateDownload() {
$request = $_SESSION[self::session_server_request];
// download the archive file from syncServer to the TEMP directory
if (!$this->saveURL(sprintf('%s/media/sync_data/backup/%s', $this->server_url, $request[syncServer::result_archive_file]), $this->temp_path.$request[syncServer::result_archive_file])) {
@unlink($this->temp_path.$request[syncServer::result_archive_file]);
$data = array('message' => sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(sync_error_sync_download_archive_file, $request[syncServer::result_archive_file], $this->page_link)));
return $this->getTemplate('message.lte', $data);
}
if (false === ($md5 = md5_file($this->temp_path.$request[syncServer::result_archive_file]))) {
// error getting md5 checksum
@unlink($this->temp_path.$request[syncServer::result_archive_file]);
$data = array('message' => sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(sync_error_sync_archive_file_get_md5, $request[syncServer::result_archive_file], $this->page_link)));
return $this->getTemplate('message.lte', $data);
}
if ($md5 != $request[syncServer::result_archive_md5]) {
// md5 checksum differ!
// delete archive from TEMP dir, ignore possible errors
@unlink($this->temp_path.$request[syncServer::result_archive_file]);
$data = array('message' => sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(sync_error_sync_md5_checksum_differ, $request[syncServer::result_archive_file], $this->page_link)));
return $this->getTemplate('message.lte', $data);
}
// ok - all checks done, archive is valid, move it to the regular directory
if (!file_exists(WB_PATH.'/media/sync_data/backup')) {
if (!mkdir(WB_PATH.'/media/sync_data/backup', 0755, true)) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(sync_error_mkdir, '/media/sync_data/backup')));
return false;
}
}
if (!rename($this->temp_path.$request[syncServer::result_archive_file], WB_PATH.'/media/sync_data/backup/'.$request[syncServer::result_archive_file])) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(sync_error_file_rename, $request[syncServer::result_archive_file])));
return false;
}
$data = array(
'action_link' => sprintf('%s?%s', $this->page_link, http_build_query(array(self::request_action => self::action_update_start))),
'img_url' => $this->image_url
);
return $this->getTemplate('update.start.lte', $data);
} // updateDownload()
/**
* Start the update process with the available update archive
*
* @return MIXED STR process dialog or BOOL FALSE on error
*/
public function updateStart() {
global $interface;
$request = $_SESSION[self::session_server_request];
$backup_archive = '/media/sync_data/backup/'.$request[syncServer::result_archive_file];
// get the content of sync_data.ini into the $ini_data array
$ini_data = array();
if (!$interface->restoreInfo($backup_archive, $ini_data)) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, $interface->getError()));
return false;
}
// existiert die Dateiliste im /temp Verzeichnis?
if (false === ($list = unserialize(file_get_contents($this->temp_path.'/sync_data/'.syncDataInterface::archive_list)))) {
$this->setError(sprintf('[%s - %s] %s', __METHOD__, __LINE__, sprintf(sync_error_file_read, $this->temp_path.self::archive_list)));
return false;
}
// pruefen ob Dateien wiederhergestellt werden sollen
$restore_info = $interface->array_search($list, 'filename', 'files/', true);
$restore_files = (count($restore_info) > 0) ? true : false;
$restore_info = $interface->array_search($list, 'filename', 'sql/', true);
$restore_tables = (count($restore_info) > 0) ? true : false;