forked from easy-wi/developer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud.php
885 lines (659 loc) · 47 KB
/
cloud.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
<?php
/**
* File: cloud.php.
* Author: Ulrich Block
* Date: 21.10.12
* Time: 10:24
* Contact: <[email protected]>
*
* This file is part of Easy-WI.
*
* Easy-WI 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.
*
* Easy-WI 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 Easy-WI. If not, see <http://www.gnu.org/licenses/>.
*
* Diese Datei ist Teil von Easy-WI.
*
* Easy-WI ist Freie Software: Sie koennen es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder spaeteren
* veroeffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Easy-WI wird in der Hoffnung, dass es nuetzlich sein wird, aber
* OHNE JEDE GEWAEHELEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewaehrleistung der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License fuer weitere Details.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*/
define('EASYWIDIR', dirname(__FILE__));
if (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
$timelimit = (isset($_GET['timeout']) and is_numeric($_GET['timeout'])) ? (int) $_GET['timeout'] : ini_get('max_execution_time') - 10;
} else {
$timelimit = 600;
}
set_time_limit($timelimit);
include(EASYWIDIR . '/stuff/methods/vorlage.php');
include(EASYWIDIR . '/stuff/methods/functions.php');
include(EASYWIDIR . '/stuff/methods/class_validator.php');
include(EASYWIDIR . '/stuff/methods/class_ts3.php');
include(EASYWIDIR . '/stuff/settings.php');
include(EASYWIDIR . '/stuff/methods/functions_ssh_exec.php');
include(EASYWIDIR . '/stuff/keyphrasefile.php');
include(EASYWIDIR . '/stuff/methods/class_ftp.php');
include(EASYWIDIR . '/stuff/methods/functions_gs.php');
include(EASYWIDIR . '/stuff/methods/class_app.php');
if (!isset($ip) or $ui->escaped('SERVER_ADDR', 'server') == $ip or in_array($ip, ipstoarray($rSA['cronjob_ips']))) {
function printText ($text) {
echo $text."\r\n";
}
function getParam ($v) {
global $value;
// need for triming as some interfaces do not validate and sanitize
return (isset($value->$v)) ? trim($value->$v) : '';
}
printText('Cloud jobs started');
printText('File include and parameters fetched. Start connecting to external systems.');
$query = $sql->prepare("SELECT * FROM `api_import` WHERE `active`='Y'");
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$resellerID = $row['resellerID'];
$start = 0;
if ($row['ssl'] == 'Y') {
$ssl = 'https://';
$port = 443;
} else {
$ssl = 'http://';
$port = 80;
}
printText('Connect to: '.$ssl.$row['domain']);
// Users first
// prepare queries ahead to avoid overhead
$query2 = $sql->prepare("UPDATE `userdata` SET `salutation`=?,`mail`=?,`cname`=?,`name`=?,`vname`=?,`birthday`=?,`country`=?,`phone`=?,`fax`=?,`handy`=?,`city`=?,`cityn`=?,`street`=?,`streetn`=? WHERE `sourceSystemID`=? AND `externalID`=? AND `resellerid`=? LIMIT 1");
$query3 = $sql->prepare("INSERT INTO `userdata` (`accounttype`,`salutation`,`mail`,`cname`,`vname`,`name`,`birthday`,`country`,`phone`,`fax`,`handy`,`city`,`cityn`,`street`,`streetn`,`sourceSystemID`,`externalID`,`security`,`resellerid`) VALUES ('u',?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$query4 = $sql->prepare("SELECT COUNT(`id`) AS `amount` FROM `userdata` WHERE `sourceSystemID`=? AND `externalID`=? LIMIT 1");
$query5 = $sql->prepare("SELECT COUNT(`id`) AS `amount` FROM `userdata` WHERE LOWER(`mail`)=? AND LOWER(`cname`)=? LIMIT 1");
$query6 = $sql->prepare("UPDATE `userdata` SET `salutation`=?,`mail`=?,`cname`=?,`vname`=?,`name`=?,`birthday`=?,`country`=?,`phone`=?,`fax`=?,`handy`=?,`city`=?,`cityn`=?,`street`=?,`streetn`=? WHERE LOWER(`mail`)=? AND LOWER(`cname`)=? AND `resellerid`=? LIMIT 1");
$query7 = $sql->prepare("UPDATE `api_import` SET `lastCheck`=?,`lastID`=? WHERE `importID`=? LIMIT 1");
$query8 = $sql->prepare("INSERT INTO `userdata_groups` (`userID`,`groupID`,`resellerID`) VALUES (?,?,?) ON DUPLICATE KEY UPDATE `userID`=`userID`");
while (!isset($left) or $left > 0) {
$getRequest = '/' . $row['file'] . '?passwordToken=' . urlencode($row['token']) . '&list=user&start=' . urlencode($start) . '&chunkSize=' . urlencode($row['chunkSize']) . '&lastID=' . urlencode($row['lastID']) . '&updateTime=' . urlencode($row['lastCheck']);
$rawResponse = webhostRequest($row['domain'], 'https://easy-wi.com', $getRequest, null, $port);
$response = cleanFsockOpenRequest($rawResponse, '{', '}');
$decoded = json_decode($response);
unset($response);
if ($decoded and isset($decoded->error)) {
$left = 0;
if (is_array($decoded->error)) {
printText('Error: ' . implode(', ', $decoded->error));
} else {
printText('Error: ' . $decoded->error);
}
} else if ($decoded and isset($decoded->total)) {
if (isset($left)) {
$left -= $row['chunkSize'];
} else {
$left = $decoded->total - $row['chunkSize'];
}
$start += $row['chunkSize'];
unset($lastID);
foreach ($decoded->entries as $value) {
if (isset($value->externalID)) {
$query4->execute(array(json_encode(array('I' => $row['importID'])), $value->externalID));
$checkAmount = $query4->fetchColumn();
if ($checkAmount > 0 and $row['fetchUpdates'] == 'Y') {
$query2->execute(array(getParam('salutation'), strtolower(getParam('email')), getParam('loginName'), getParam('lastName'), getParam('firstName'), getParam('birthday'), getParam('country'), getParam('phone'), getParam('fax'), getParam('handy'), getParam('city'), getParam('cityn'), getParam('street'), getParam('streetn'),json_encode(array('I' => $row['importID'])), getParam('externalID'), $row['resellerID']));
printText('User updated. Loginname: ' . $value->loginName.' e-mail: ' . strtolower($value->email));
} else if ($checkAmount > 0) {
printText('User update skipped. Loginname: ' . $value->loginName.' e-mail: ' . strtolower($value->email));
} else {
$query5->execute(array(strtolower($value->email), strtolower($value->loginName)));
if ($query5->fetchColumn()>0 and $row['fetchUpdates'] == 'Y') {
$query6->execute(array(getParam('salutation'), strtolower(getParam('email')), getParam('loginName'), getParam('firstName'), getParam('lastName'), getParam('birthday'), getParam('country'), getParam('phone'), getParam('fax'), getParam('handy'), getParam('city'), getParam('cityn'), getParam('street'), getParam('streetn'), strtolower($value->email), strtolower($value->loginName), $row['resellerID']));
printText('User updated. Loginname: ' . $value->loginName.' e-mail: ' . strtolower($value->email));
} else if ($checkAmount > 0) {
printText('User update skipped because source system differ. Loginname: ' . $value->loginName.' e-mail: ' . strtolower($value->email));
} else {
printText('Import user. Loginname: ' . $value->loginName.' e-mail: ' . strtolower($value->email));
$query3->execute(array(getParam('salutation'), strtolower(getParam('email')), getParam('loginName'), getParam('firstName'), getParam('lastName'), getParam('birthday'), getParam('country'), getParam('phone'), getParam('fax'), getParam('handy'), getParam('city'), getParam('cityn'), getParam('street'), getParam('streetn'), json_encode(array('I' => $row['importID'])), getParam('externalID'), getParam('password'), $row['resellerID']));
$query8->execute(array($sql->lastInsertId(), $row['groupID'], $row['resellerID']));
}
}
if (getParam('updatetime') != '' and (isset($lastCheck) and strtotime(getParam('updatetime')) > strtotime($lastCheck)) or !isset($lastCheck)) {
$lastCheck = getParam('updatetime');
}
$lastID = $value->externalID;
}
}
if (isset($lastID)) {
if (!isset($lastCheck)) {
$lastCheck=date('Y-m-d H:i:s');
}
$query7->execute(array($lastCheck, $lastID, $row['importID']));
}
if ($left > 0){
printText('Total amount is: ' . $decoded->total . ' User left: ' . $left . ' need to make another run');
sleep(1);
} else {
printText('Total amount is: ' . $decoded->total . ' No user left.');
}
} else if ($decoded) {
printText('JSON Response does not contain expected values');
$left = 0;
} else {
if (strpos(strtolower($rawResponse), 'file not found') === false) {
printText('No Json Response. Will retry.');
printText('Raw response was:' . $rawResponse);
} else {
$left = 0;
printText('404: File not found');
}
}
}
// Get available Gameroot IDs and map to their Easy-WI IDs
$gameRootIPs = array();
$query2 = $sql->prepare("SELECT *,AES_DECRYPT(`user`,?) AS `duser`,AES_DECRYPT(`pass`,?) AS `dpass` FROM `rserverdata` WHERE `resellerid`=?");
$query2->execute(array($aeskey, $aeskey, $resellerID));
while ($row2 = $query2->fetch(PDO::FETCH_ASSOC)) {
if (($row2['publickey'] != 'N' and strlen($row2['keyname']) > 0) or ($row2['publickey'] == 'N' and strlen($row2['dpass']) > 0)) {
$gameRootIPs[$row2['ip']] = array('id' => $row2['id'], 'ftpPort' => $row2['ftpport'], 'user' => $row2['duser'], 'sourceSystemID' => $row2['sourceSystemID'], 'externalID' => $row2['externalID']);
foreach (ipstoarray($row2['altips']) as $ip) {
$gameRootIPs[$ip] = array('id' => $row2['id'], 'ftpPort' => $row2['ftpport'], 'user' => $row2['duser'], 'sourceSystemID' => $row2['sourceSystemID'], 'externalID' => $row2['externalID']);
}
}
}
// Game Rootserver
unset($left);
$start = 0;
// Prepare queries only once to avoid overhead
$query2 = $sql->prepare("UPDATE `rserverdata` SET `userID`=?,`ip`=?,`altips`=?,`port`=AES_ENCRYPT(?,?),`ftpport`=?,`cores`=?,`ram`=? WHERE `sourceSystemID`=? AND `externalID`=? LIMIT 1");
$query3 = $sql->prepare("INSERT INTO `rserverdata` (`userID`,`ip`,`altips`,`port`,`ftpport`,`cores`,`ram`,`sourceSystemID`,`externalID`,`resellerid`) VALUES (?,?,?,AES_ENCRYPT(?,?),?,?,?,?,?,?)");
while (!isset($left) or $left > 0) {
$getRequest = '/' . $row['file'] . '?passwordToken=' . urlencode($row['token']) . '&list=rootserver&start=' . urlencode($start) . '&chunkSize=' . urlencode($row['chunkSize']) . '&lastID=' . urlencode($row['lastID']) . '&updateTime=' . urlencode($row['lastCheck']);
$rawResponse = webhostRequest($row['domain'], 'https://easy-wi.com', $getRequest, null, $port);
$response = cleanFsockOpenRequest($rawResponse, '{', '}');
$decoded = json_decode($response);
unset($response);
if ($decoded and isset($decoded->error)) {
$left = 0;
if (is_array($decoded->error)) {
printText('Error: ' . implode(', ', $decoded->error));
} else {
printText('Error: ' . $decoded->error);
}
} else if ($decoded and isset($decoded->total)) {
if (isset($left)) {
$left -= $row['chunkSize'];
} else {
$left = $decoded->total - $row['chunkSize'];
}
$start += $row['chunkSize'];
foreach ($decoded->entries as $value) {
if (isset($value->externalID)) {
// Check if rootserver entry already exists at easy-wi
unset($gameRootID);
foreach ($value->ips as $ip) {
if (isset($gameRootIPs[$ip]['id'])) {
$gameRootID = $gameRootIPs[$ip]['id'];
$sourceSystemID = $gameRootIPs[$ip]['sourceSystemID'];
$externalID = $gameRootIPs[$ip]['externalID'];
break;
}
}
// IPs need to converted in Easy-WI format
$ip = $value->ips[0];
unset($value->ips[0]);
$ips = implode("\r\n", $value->ips);
if (isset($gameRootID)) {
if (json_encode(array('I' => $row['importID'])) == $sourceSystemID and getParam('externalID') == $externalID and $row['fetchUpdates'] == 'Y') {
$query2->execute(array(getParam('belongsToID'), $ip, $ips, getParam('sshPort'), $aeskey, getParam('ftpPort'), getParam('cores'), getParam('ram'), json_encode(array('I' => $row['importID'])), getParam('externalID')));
printText('Rootserver updated. IP: ' . $ip);
} else if (json_encode(array('I' => $row['importID'])) == $sourceSystemID and getParam('externalID') == $externalID) {
printText('Rootserver found but update skipped since in import only mode. IP: ' . $ip);
} else {
printText('Rootserver found but update skipped because source system differ. IP: ' . $ip);
}
} else {
$query3->execute(array(getParam('belongsToID'), $ip, $ips, getParam('sshPort'), $aeskey, getParam('ftpPort'), getParam('cores'), getParam('ram'), json_encode(array('I' => $row['importID'])), getParam('externalID'), $resellerID));
$gameRootIPs[$ip] = array('id' => $sql->lastInsertId(), 'ftpPort' => getParam('sshPort'), 'user' => '', 'sourceSystemID' => json_encode(array('I' => $row['importID'])), 'externalID' => getParam('externalID'));
printText('Import rootserver. IP: ' . $ip);
}
}
}
if ($left > 0){
printText('Total amount is: ' . $decoded->total . ' rootservers left: ' . $left . ' need to make another run');
sleep(1);
} else {
printText('Total amount is: ' . $decoded->total . ' No rootservers left.');
}
} else if ($decoded) {
printText('JSON Response does not contain expected values');
$left = 0;
} else {
if (strpos(strtolower($rawResponse), 'file not found') === false) {
printText('No Json Response. Will retry.');
} else {
$left = 0;
printText('404: File not found');
}
}
}
// Gameserver
unset($left);
$start = 0;
$gameRootCmds = array();
// Prepare queries only once to avoid overhead
$query2 = $sql->prepare("SELECT t.`id`,t.`modfolder`,t.`gamebinary`,t.`map` FROM `servertypes` t INNER JOIN `rservermasterg` m ON t.`id`=m.`servertypeid` WHERE t.`shorten`=? AND t.`resellerid`=? AND m.`serverid`=? AND m.`updating`='N' LIMIT 1");
$query3 = $sql->prepare("SELECT `id`,`sourceSystemID`,`externalID` FROM `gsswitch` WHERE `serverip`=? AND `port`=? AND `resellerid`=? LIMIT 1");
$query4 = $sql->prepare("SELECT `id`,`cname` FROM `userdata` WHERE `sourceSystemID`=? AND `externalID`=? AND `resellerid`=? LIMIT 1");
$query5 = $sql->prepare("INSERT INTO `gsswitch` (`stopped`,`ftppassword`,`userid`,`rootID`,`serverip`,`port`,`port2`,`slots`,`taskset`,`cores`,`pallowed`,`sourceSystemID`,`externalID`,`resellerid`) VALUES ('Y',AES_ENCRYPT(?,?),?,?,?,?,?,?,?,?,?,?,?,?)");
$query6 = $sql->prepare("INSERT INTO `serverlist` (`tic`,`map`,`switchID`,`servertype`,`usermap`,`resellerid`) VALUES (?,?,?,?,'Y',?)");
$query7 = $sql->prepare("UPDATE `gsswitch` SET `serverid`=? WHERE `id`=? LIMIT 1");
$query8 = $sql->prepare("UPDATE `gsswitch` SET `slots`=?,`taskset`=?,`cores`=?,`pallowed`=? WHERE `id`=? LIMIT 1");
while (!isset($left) or $left > 0) {
$getRequest = '/' . $row['file'] . '?passwordToken=' . urlencode($row['token']) . '&list=gameserver&start=' . urlencode($start) . '&chunkSize=' . urlencode($row['chunkSize']) . '&lastID=' . urlencode($row['lastID']) . '&updateTime=' . urlencode($row['lastCheck']);
$rawResponse = webhostRequest($row['domain'], 'https://easy-wi.com', $getRequest, null, $port);
$response = cleanFsockOpenRequest($rawResponse, '{', '}');
$decoded = json_decode($response);
unset($response);
if ($decoded and isset($decoded->error)) {
$left = 0;
if (is_array($decoded->error)) {
printText('Error: ' . implode(', ', $decoded->error));
} else {
printText('Error: ' . $decoded->error);
}
} else if ($decoded and isset($decoded->total)) {
if (isset($left)) {
$left -= $row['chunkSize'];
} else {
$left = $decoded->total - $row['chunkSize'];
}
$start += $row['chunkSize'];
foreach ($decoded->entries as $value) {
if (isset($value->externalID)) {
// Check if a rootserver entry already exists at easy-wi with the used IP
$arrayIP = getParam('ip');
if (isset($gameRootIPs[$arrayIP]['id'])) {
unset($servertypeID, $servertypeModFolder, $switchID);
// Check if the rootserver has a masterserver with this shorten
$query2->execute(array(getParam('shorten'), $row['resellerID'], $gameRootIPs[$arrayIP]['id']));
while ($row2 = $query2->fetch(PDO::FETCH_ASSOC)) {
$servertypeID = $row2['id'];
$defaultMap = $row2['map'];
// If no srcds or hlds game we will work with the root folder.
// If yes than we need to set or all files will be downloaded into incorrect subfolder.
$servertypeModFolder = ($row2['gamebinary'] == 'srcds_run' or $row2['gamebinary'] == 'hlds_run') ? $row2['modfolder'] . '/' : '';
}
if (isset($servertypeID) and isid($servertypeID, 11)) {
if (isid(getParam('assignedCore'), 11)) {
$taskset = 'Y';
$core = getParam('assignedCore');
} else {
$taskset = 'N';
$core = '';
}
$query3->execute(array(getParam('ip'), getParam('port'), $resellerID));
while ($row2 = $query3->fetch(PDO::FETCH_ASSOC)) {
$switchID = $row2['id'];
$sourceSystemID = $row2['sourceSystemID'];
$externalID = $row2['externalID'];
}
if (isset($switchID) and isid($switchID, 11)) {
if (json_encode(array('I' => $row['importID'])) == $sourceSystemID and getParam('externalID') == $externalID and $row['fetchUpdates'] == 'Y') {
$query8->execute(array(getParam('slots'), $taskset, $core, getParam('protectionMode'), $switchID));
printText('Gameserver found and updated. Address: ' . getParam('ip') . ':' . getParam('port') . ' (' . getParam('shorten') . ')');
} else if (json_encode(array('I' => $row['importID'])) == $sourceSystemID and getParam('externalID') == $externalID) {
printText('Gameserver found but update skipped since import only mode. Address: ' . getParam('ip') . ':' . getParam('port') . ' (' . getParam('shorten') . ')');
} else {
printText('Gameserver found but update skipped because source system differ. Address: ' . getParam('ip') . ':' . getParam('port') . ' (' . getParam('shorten') . ')');
}
} else {
unset($internalUserID, $customer);
$query4->execute(array(json_encode(array('I' => $row['importID'])), getParam('belongsToID'), $resellerID));
foreach($query4->fetchAll(PDO::FETCH_ASSOC) as $row2) {
$internalUserID = $row2['id'];
$customer = $row2['cname'];
}
if (isset($internalUserID) and isset($customer) and isid($internalUserID, 11)) {
$passwordGenerate = passwordgenerate(10);
$query5->execute(array($passwordGenerate, $aeskey, $internalUserID, $gameRootIPs[$arrayIP]['id'], getParam('ip'), getParam('port'), getParam('port2'), getParam('slots'), $taskset, $core, getParam('protectionMode'), json_encode(array('I' => $row['importID'])), getParam('externalID'), $resellerID));
$switchID = $sql->lastInsertId();
$tickrate = (getParam('tickrate') > 0) ? getParam('tickrate') : 66;
$startMap = (strlen(getParam('startMap')) > 0) ? getParam('startMap') : $defaultMap;
$query6->execute(array($tickrate, $startMap, $switchID, $servertypeID, $resellerID));
$query7->execute(array($sql->lastInsertId(), $switchID));
$ftpConnectString = 'ftp://' . str_replace('//', '/', getParam('ip') . ':' . $gameRootIPs[$arrayIP]['ftpPort'] . '/' . getParam('path') . '/' . $servertypeModFolder);
$appServer = new AppServer($gameRootIPs[$arrayIP]['id']);
$appServer->getAppServerDetails($switchID);
$appServer->migrateToEasyWi(array('user' => getParam('ftpUser'), 'password' => getParam('ftpPass'), 'path' => '/' . getParam('path') . '/' . $servertypeModFolder, 'connectString' => $ftpConnectString), getParam('shorten'), $servertypeModFolder);
$appServer->execute();
printText('Import Gameserver. Address: ' . getParam('ip') . ':' . getParam('port') . '. And shorten:' . getParam('shorten'));
} else {
printText('Error: Import skipped since no user with external userID ' . getParam('belongsToID') . ' for gameserver with address: ' . getParam('ip') . ':' . getParam('port') . ' and shorten:' . getParam('shorten'));
}
}
} else {
printText('Error: No masterserver with the shorten ' . getParam('shorten') . ' found. Gameserver update skipped for address: ' . getParam('ip') . ':' . getParam('port'));
}
} else {
printText('Error: No game rootserver found with the IP ' . getParam('ip') . '. Gameserver not imported: ' . getParam('ip') . ':' . getParam('port') . ' (' . getParam('shorten') . ')');
}
}
}
if ($left > 0){
printText('Total amount is: ' . $decoded->total . ' Gameservers left: ' . $left . ' need to make another run');
sleep(1);
} else {
printText('Total amount is: ' . $decoded->total . ' No Gameservers left.');
}
} else if ($decoded) {
printText('JSON Response does not contain expected values');
$left = 0;
} else {
if (strpos(strtolower($rawResponse), 'file not found') === false) {
printText('No Json Response. Will retry.');
} else {
$left = 0;
printText('404: File not found');
}
}
}
// Start the migration of newly imported gameservers
foreach ($gameRootCmds as $k => $v) {
ssh2_execute('gs', $k, $v);
}
// Set to null instead of unset() because PHP garbage collector does not work very efficient
$gameRootCmds = null;
$gameRootIPs = null;
// TS3 Master server array
$ts3MasterIPs = array();
$query2 = $sql->prepare("SELECT `id`,`ssh2ip`,`ips`,`sourceSystemID`,`externalID` FROM `voice_masterserver` WHERE `resellerid`=?");
$query2->execute(array($resellerID));
while ($row2 = $query2->fetch(PDO::FETCH_ASSOC)) {
$ts3MasterIPs[$row2['ssh2ip']] = array('id' => $row2['id'], 'sourceSystemID' => $row2['sourceSystemID'], 'externalID' => $row2['externalID']);
foreach (ipstoarray($row2['ips']) as $ip) {
$ts3MasterIPs[$ip] = array('id' => $row2['id'], 'sourceSystemID' => $row2['sourceSystemID'], 'externalID' => $row2['externalID']);
}
}
// TS3 Masterserver
unset($left);
$start = 0;
// Prepare queries only once to avoid overhead
$query2 = $sql->prepare("UPDATE `voice_masterserver` SET `ssh2ip`=?,`defaultdns`=?,`queryport`=?,`querypassword`=AES_ENCRYPT(?,?),`ssh2user`=AES_ENCRYPT(?,?),`serverdir`=? WHERE `id`=? LIMIT 1");
$query3 = $sql->prepare("INSERT INTO `voice_masterserver` (`addedby`,`usedns`,`ssh2ip`,`defaultdns`,`queryport`,`querypassword`,`ssh2user`,`serverdir`,`sourceSystemID`,`externalID`,`resellerid`) VALUES (2,'N',?,?,?,AES_ENCRYPT(?,?),AES_ENCRYPT(?,?),?,?,?,?)");
while (!isset($left) or $left > 0) {
$getRequest = '/' . $row['file'] . '?passwordToken=' . urlencode($row['token']) . '&list=voicemaster&start=' . urlencode($start) . '&chunkSize=' . urlencode($row['chunkSize']) . '&lastID=' . urlencode($row['lastID']) . '&updateTime=' . urlencode($row['lastCheck']);
$rawResponse = webhostRequest($row['domain'], 'https://easy-wi.com', $getRequest, null, $port);
$response = cleanFsockOpenRequest($rawResponse, '{', '}');
$decoded = json_decode($response);
unset($response);
if ($decoded and isset($decoded->error)) {
$left = 0;
if (is_array($decoded->error)) {
printText('Error: ' . implode(', ', $decoded->error));
} else {
printText('Error: ' . $decoded->error);
}
} else if ($decoded and isset($decoded->total)) {
if (isset($left)) {
$left -= $row['chunkSize'];
} else {
$left = $decoded->total - $row['chunkSize'];
}
$start += $row['chunkSize'];
foreach ($decoded->entries as $value) {
if (isset($value->externalID)) {
// Check if rootserver entry already exists at easy-wi
if (isset($ts3MasterIPs[getParam('ip')]['id'])) {
if (json_encode(array('I' => $row['importID'])) == $sourceSystemID and getParam('externalID') == $externalID and $row['fetchUpdates'] == 'Y') {
$query2->execute(array(getParam('ip'), getParam('dns'), getParam('port'), getParam('queryPassword'), $aeskey, getParam('sshUser'), $aeskey, getParam('path'), $ts3MasterIPs[getParam('ip')]['id']));
printText('TS3 masterserver updated. IP: ' . getParam('ip'));
} else if (json_encode(array('I' => $row['importID'])) == $sourceSystemID and getParam('externalID') == $externalID) {
printText('TS3 masterserver found but update skipped since in import only mode. IP: ' . getParam('ip'));
} else {
printText('TS3 masterserver found but update skipped because source system differ. IP: ' . getParam('ip'));
}
} else {
$query3->execute(array(getParam('ip'), getParam('dns'), getParam('port'), getParam('queryPassword'), $aeskey, getParam('sshUser'), $aeskey, getParam('path'), json_encode(array('I' => $row['importID'])), getParam('externalID'), $resellerID));
$keyIP = getParam('ip');
$ts3MasterIPs[$keyIP][] = array('id' => $sql->lastInsertId(), 'sourceSystemID' => json_encode(array('I' => $row['importID'])), 'externalID' => getParam('externalID'));
printText('TS3 masterserver impororted. IP: ' . getParam('ip'));
}
}
}
if ($left > 0){
printText('Total amount is: ' . $decoded->total . ' TS3 masterserver left: ' . $left . ' need to make another run');
sleep(1);
} else {
printText('Total amount is: ' . $decoded->total . ' TS3 masterserver left.');
}
} else if ($decoded) {
printText('JSON Response does not contain expected values');
$left = 0;
} else {
if (strpos(strtolower($rawResponse), 'file not found') === false) {
printText('No Json Response. Will retry.');
} else {
$left = 0;
printText('404: File not found');
}
}
}
// TS3 virtual server
unset($left);
$start = 0;
// Prepare queries only once to avoid overhead
$query2 = $sql->prepare("SELECT `id`,`sourceSystemID`,`externalID` FROM `voice_server` WHERE `ip` =? AND `port`=? AND `resellerid`=? LIMIT 1");
$query3 = $sql->prepare("UPDATE `voice_server` SET `ip`=?,`port`=?,`dns`=?,`slots`=? WHERE `id`=? LIMIT 1");
$query4 = $sql->prepare("SELECT `id` FROM `userdata` WHERE `sourceSystemID`=? AND `externalID`=? AND `resellerid`=? LIMIT 1");
$query5 = $sql->prepare("INSERT INTO `voice_server` (`ip`,`port`,`dns`,`slots`,`userid`,`masterserver`,`sourceSystemID`,`externalID`,`resellerid`) VALUES (?,?,?,?,?,?,?,?,?)");
while (!isset($left) or $left > 0) {
$getRequest = '/' . $row['file'] . '?passwordToken=' . urlencode($row['token']) . '&list=voiceserver&start=' . urlencode($start) . '&chunkSize=' . urlencode($row['chunkSize']) . '&lastID=' . urlencode($row['lastID']) . '&updateTime=' . urlencode($row['lastCheck']);
$rawResponse = webhostRequest($row['domain'], 'https://easy-wi.com', $getRequest, null, $port);
$response = cleanFsockOpenRequest($rawResponse, '{', '}');
$decoded = json_decode($response);
unset($response);
if ($decoded and isset($decoded->error)) {
$left = 0;
if (is_array($decoded->error)) {
printText('Error: ' . implode(', ', $decoded->error));
} else {
printText('Error: ' . $decoded->error);
}
} else if ($decoded and isset($decoded->total)) {
if (isset($left)) {
$left -= $row['chunkSize'];
} else {
$left = $decoded->total - $row['chunkSize'];
}
$start += $row['chunkSize'];
foreach ($decoded->entries as $value) {
if (isset($value->externalID)) {
$ts3MasterIP = getParam('ip');
// Check if TS3 masterserver entry exists at easy-wi
if (isset($ts3MasterIPs[$ts3MasterIP]['id'])) {
unset($ts3ID);
// Get TS3 data if server exists
$query2->execute(array(getParam('ip'), getParam('port'), $resellerID));
while ($row2 = $query2->fetch(PDO::FETCH_ASSOC)) {
$ts3ID = $row2['id'];
$sourceSystemID = $row2['sourceSystemID'];
$externalID = $row2['externalID'];
}
if (isset($ts3ID) and isid($ts3ID, 11) and json_encode(array('I' => $row['importID'])) == $sourceSystemID and getParam('externalID') == $externalID and $row['fetchUpdates'] == 'Y') {
$query3->execute(array(getParam('ip'), getParam('port'), getParam('dns'), getParam('slots'), $ts3ID));
printText('TS3 server updated. Address: ' . getParam('ip') . ':'. getParam('port') . ' '. getParam('dns'));
} else if (isset($ts3ID) and isid($ts3ID, 11) and json_encode(array('I' => $row['importID'])) == $sourceSystemID and getParam('externalID') == $externalID) {
printText('TS3 server update skipped because import only mode. Address: ' . getParam('ip') . ':'. getParam('port') . ' '. getParam('dns'));
} else if (isset($ts3ID) and isid($ts3ID, 11)) {
printText('TS3 server update skipped because Source System ID differ. Address: ' . getParam('ip') . ':'. getParam('port') . ' '. getParam('dns'));
} else {
$query4->execute(array(json_encode(array('I' => $row['importID'])), getParam('belongsToID'), $resellerID));
$userID = $query4->fetchColumn();
if (isid($userID, 11)) {
$query5->execute(array(getParam('ip'), getParam('port'), getParam('dns'), getParam('slots'), $userID, $ts3MasterIPs[$ts3MasterIP]['id'], json_encode(array('I' => $row['importID'])), getParam('belongsToID'), $resellerID));
printText('Imported TS3 server. Address + DNS: ' . getParam('ip') . ':'. getParam('port') . ' '. getParam('dns'));
} else {
printText('Error: Cannot import TS3 server due to missing user with external userID ' . getParam('belongsToID') . '. Address + DNS: ' . getParam('ip') . ':'. getParam('port') . ' '. getParam('dns'));
}
}
} else {
printText('Error: Cannot import TS3 server due to missing masterserver. Address + DNS: ' . getParam('ip') . ':'. getParam('ip') . ' '. getParam('dns'));
}
} else {
printText('Error: externalID not set');
}
}
if ($left > 0){
printText('Total amount is: ' . $decoded->total . ' TS3 virtual server left: ' . $left . ' need to make another run');
sleep(1);
} else {
printText('Total amount is: ' . $decoded->total . ' No TS3 virtual server left.');
}
} else if ($decoded) {
printText('JSON Response does not contain expected values');
$left = 0;
} else {
if (strpos(strtolower($rawResponse), 'file not found') === false) {
printText('No Json Response. Will retry.');
} else {
$left = 0;
printText('404: File not found');
}
}
}
// As we cannot import the virtual server IDs we need to get them afterwards
$query = $sql->prepare("SELECT DISTINCT(`masterserver`) FROM `voice_server` WHERE `resellerid`=? AND (`localserverid`<1 OR `localserverid` IS NULL)");
$query2 = $sql->prepare("SELECT *,AES_DECRYPT(`querypassword`,:aeskey) AS `decryptedquerypassword`,AES_DECRYPT(`ssh2port`,:aeskey) AS `decryptedssh2port`,AES_DECRYPT(`ssh2user`,:aeskey) AS `decryptedssh2user`,AES_DECRYPT(`ssh2password`,:aeskey) AS `decryptedssh2password` FROM `voice_masterserver` WHERE `active`='Y' AND `id`=:id LIMIT 1");
$query3 = $sql->prepare("UPDATE `voice_server` SET `localserverid`=? WHERE `masterserver`=? AND `resellerid`=? AND `port`=? AND (`localserverid`<1 OR `localserverid` IS NULL)");
$query->execute(array($resellerID));
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$query2->execute(array(':aeskey' => $aeskey, ':id' => $row2['masterserver']));
while ($row3 = $query2->fetch(PDO::FETCH_ASSOC)) {
$ts3 = new TS3($row3['ssh2ip'], $row3['queryport'], 'serveradmin', $row3['decryptedquerypassword'], false);
if (strpos($ts3->errorcode, 'error id=0') !== false) {
$serverlist = $ts3->ServerList();
if (!isset($serverlist[0]['id']) or $serverlist[0]['id'] == 0) {
foreach ($serverlist as $server) {
$query3->execute(array($server['virtualserver_id'], $row2['masterserver'], $resellerID, $server['virtualserver_port']));
}
}
}
}
}
// Substitutes
unset($left);
$start = 0;
// Prepare queries only once to avoid overhead
$query2 = $sql->prepare("SELECT `sID` FROM `userdata_substitutes` WHERE `sourceSystemID`=? AND `externalID`=? AND `resellerID`=? LIMIT 1");
$query4 = $sql->prepare("SELECT `id` FROM `userdata` WHERE `sourceSystemID`=? AND `externalID`=? AND `resellerID`=? LIMIT 1");
$query3 = $sql->prepare("UPDATE `userdata_substitutes` SET `loginName`=?,`name`=?,`vname`=? WHERE `sID`=? LIMIT 1");
$query5 = $sql->prepare("INSERT INTO `userdata_substitutes` (`userID`,`loginName`,`name`,`vname`,`passwordHashed`,`sourceSystemID`,`externalID`,`resellerID`) VALUES (?,?,?,?,?,?,?,?)");
$query6 = $sql->prepare("SELECT `id` FROM `gsswitch` WHERE `sourceSystemID`=? AND `externalID`=? AND `userid`=? AND `resellerid`=? LIMIT 1");
$query7 = $sql->prepare("INSERT INTO `userdata_substitutes_servers` (`sID`,`oType`,`oID`,`resellerID`) VALUE (?,'gs',?,?) ON DUPLICATE KEY UPDATE `resellerID`=`resellerID`");
$query8 = $sql->prepare("SELECT `id` FROM `voice_server` WHERE `sourceSystemID`=? AND `externalID`=? AND `userid`=? AND `resellerid`=? LIMIT 1");
$query9 = $sql->prepare("INSERT INTO `userdata_substitutes_servers` (`sID`,`oType`,`oID`,`resellerID`) VALUE (?,'vo',?,?) ON DUPLICATE KEY UPDATE `resellerID`=`resellerID`");
while (!isset($left) or $left > 0) {
$getRequest = '/' . $row['file'] . '?passwordToken=' . urlencode($row['token']) . '&list=substitutes&start=' . urlencode($start) . '&chunkSize=' . urlencode($row['chunkSize']) . '&lastID=' . urlencode($row['lastID']) . '&updateTime=' . urlencode($row['lastCheck']);
$rawResponse = webhostRequest($row['domain'], 'https://easy-wi.com', $getRequest, null, $port);
$response = cleanFsockOpenRequest($rawResponse, '{', '}');
$decoded = json_decode($response);
unset($response);
if ($decoded and isset($decoded->error)) {
$left = 0;
if (is_array($decoded->error)) {
printText('Error: ' . implode(', ', $decoded->error));
} else {
printText('Error: ' . $decoded->error);
}
} else if ($decoded and isset($decoded->total)) {
if (isset($left)) {
$left -= $row['chunkSize'];
} else {
$left = $decoded->total - $row['chunkSize'];
}
$start += $row['chunkSize'];
foreach ($decoded->entries as $value) {
if (isset($value->externalID)) {
$substituteImported = true;
$query2->execute(array(json_encode(array('I' => $row['importID'])), getParam('externalID'), $resellerID));
$localID = (int) $query2->fetchColumn();
$query4->execute(array(json_encode(array('I' => $row['importID'])), getParam('belongsToID'), $resellerID));
$belongsToLocalID = (int) $query4->fetchColumn();
// Check if substitute exists at easy-wi
if (!isid($localID, 10) and isid($belongsToLocalID, 11)) {
$query5->execute(array($belongsToLocalID, getParam('loginName'), getParam('lastName'), getParam('firstName'), getParam('password'), json_encode(array('I' => $row['importID'])), getParam('externalID'), $resellerID));
$localID = $sql->lastInsertId();
printText('Imported substitute. Loginname: ' . getParam('loginName') . ', externalID: ' .getParam('externalID'). ' Belongs to external ' .getParam('belongsToID') . ' and internal ' . $belongsToLocalID);
} else if (isid($localID, 11) and isid($belongsToLocalID, 11) and $row['fetchUpdates'] == 'Y') {
$query3->execute(array(getParam('loginName'), getParam('lastName'), getParam('firstName'), $localID));
printText('Substitute updated. Loginname: ' . getParam('loginName'). ', localID: ' . $localID . ' externalID: ' .getParam('externalID'). ' Belongs to external ' .getParam('belongsToID') . ' and internal ' . $belongsToLocalID);
} else if (isset($localID) and isid($localID, 11) and $row['fetchUpdates'] == 'N') {
$substituteImported = false;
printText('Substitute update skipped because import only mode. Loginname: ' . getParam('loginName'). ', externalID: ' .getParam('externalID'). ' Belongs to external ' .getParam('belongsToID') . ' and internal ' . $belongsToLocalID);
} else {
$substituteImported = false;
printText('Error: Cannot import substitute ' . getParam('loginName') . 'because there is no user with external ID ' . getParam('belongsToID'));
}
if ($substituteImported === true and isid($belongsToLocalID, 11)) {
$serverAccess = (array) $value->serverAccess;
foreach ($serverAccess['gs'] as $externalGsID) {
$query6->execute(array(json_encode(array('I' => $row['importID'])), $externalGsID, $belongsToLocalID, $resellerID));
$localGsID = $query6->fetchColumn();
if (isid($localGsID, 10)) {
$query7->execute(array($localID, $localGsID, $resellerID));
printText('Imported substitute gameserver server access. Local gameserverid is: ' . $localGsID);
} else {
printText('Error: Importing substitute gameserver server access. No local gameserver');
}
}
foreach ($serverAccess['vo'] as $externalVoID) {
$query8->execute(array(json_encode(array('I' => $row['importID'])), $externalVoID, $belongsToLocalID, $resellerID));
$localVoID = $query8->fetchColumn();
if (isid($localVoID, 10)) {
$query9->execute(array($localID, $localVoID, $resellerID));
printText('Imported substitute gameserver server access. Local gameserverid is: ' . $localVoID);
} else {
printText('Error: Importing substitute gameserver server access. No local voiceserver');
}
}
}
}
}
if ($left > 0){
printText('Total amount is: ' . $decoded->total . ' substitutes left: ' . $left . ' need to make another run');
sleep(1);
} else {
printText('Total amount is: ' . $decoded->total . ' No substitute left.');
}
} else if ($decoded) {
printText('JSON Response does not contain expected values');
$left = 0;
} else {
if (strpos(strtolower($rawResponse), 'file not found') === false) {
printText('No Json Response. Will retry.');
} else {
$left = 0;
printText('404: File not found');
}
}
}
}
$query = $sql->prepare("UPDATE `settings` SET `lastCronCloud`=UNIX_TIMESTAMP()");
$query->execute();
} else {
header('Location: login.php');
die('Cloud can only be run via console and or a cronjob');
}