-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSynologyabb.class.php
1278 lines (1090 loc) · 34.2 KB
/
Synologyabb.class.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
/**
*
* License for all code of this FreePBX module can be found in the license file inside the module directory
* @copyright 2021 Javier Pastor Garcia
*
*/
namespace FreePBX\modules;
include __DIR__."/vendor/autoload.php";
class Synologyabb extends \FreePBX_Helpers implements \BMO {
const LOCK_DIR = "/dev/shm/abbwrapper";
const LOCK_FILE = "/dev/shm/abbwrapper/run.lock";
const LOGS_FILE = "/dev/shm/abbwrapper/output.log";
const INFO_FILE = "/dev/shm/abbwrapper/info.json";
public static $default_agent_status_data = array(
'server' => '',
'user' => '',
'lastbackup' => '',
'nextbackup' => '',
'server_status' => '',
'portal' => '',
'html' => '',
'error' => ''
);
const STATUS_NULL = -1; // No state has been defined.
const STATUS_IDLE = 110; // (Idle) No copy has been made yet.
const STATUS_IDLE_COMPLETED = 120; // (Idle - Completed)
const STATUS_IDLE_CANCEL = 130; // (Idle - Canceled)
const STATUS_IDLE_FAILED = 140; // (Idel - Failed)
const STATUS_BACKUP_RUN = 300; // (Backing up... - 8.31 MB / 9.57 MB (576.00 KB/s))
const STATUS_NO_CONNECTION = 400; // (No connection found) Not connected to the server
const STATUS_ERR_DEV_REMOVED = 510; // (Error - The current device has been removed from the server. Please contact your administrator for further assistance.) Equipo eliminado del servidor.
const STATUS_UNKNOWN = 99990; //99990 - unknown status
const STATUS_IDLE_UNKNOWN = 99991; //99991 - Idel status unknown
const STATUS_ERR_UNKNOWN = 99992; //99992 - error status unknown
const ERROR_UNKNOWN = -2;
const ERROR_NOT_DEFINED = -1;
const ERROR_ALL_GOOD = 0;
const ERROR_AGENT_NOT_INSTALLED = 501;
const ERROR_AGENT_NOT_RETURN_INFO = 502;
const ERROR_AGENT_ENDED_IN_ERROR = 503;
const ERROR_AGENT_RETURN_UNCONTROLLED = 504;
const ERROR_AGENT_IS_INSTALLING = 505;
const ERROR_AGENT_ALREADY_CONNECTED = 520; // (Already connected)
const ERROR_AGENT_NOT_ALREADY_CONNECTED = 521; // (Not Already connected)
const ERROR_AGENT_SERVER_CHECK = 550;
const ERROR_AGENT_SERVER_AUTH_FAILED = 611;
const ERROR_AGENT_SERVER_AUTH_FAILED_USER_PASS = 612;
const ERROR_AGENT_SERVER_AUTH_FAILED_BAN_IP = 613;
const ERROR_MISSING_ARGS = 650;
const ERROR_HOOK_FILE_NOT_EXIST = 710;
const ERROR_HOOK_FILE_EMTRY = 715;
const ERROR_HOOK_FILE_TOEKN = 720;
const ERROR_HOOK_RUN_TIMEOUT = 725;
const DEFAULT_PORT = 5510; // Default port Active Backup for Business Server
const SYNOLOGY_URL_ARCHIVE = 'https://archive.synology.com/download/Utility/ActiveBackupBusinessAgent';
public function __construct($freepbx = null) {
if ($freepbx == null) {
throw new \Exception("Not given a FreePBX Object");
}
$this->FreePBX = $freepbx;
$this->db = $freepbx->Database;
$this->config = $freepbx->Config;
$this->logger = $freepbx->Logger()->getDriver('freepbx');
$this->module_name = join('', array_slice(explode('\\', get_class()), -1));
$this->astspooldir = $this->config->get("ASTSPOOLDIR");
$this->asttmpdir = $this->getAstSpoolDir() . "/tmp";
$this->ABBCliVersionMin = "2.2.0-2070"; // Minimum version supported
}
public function chownFreepbx() {
$files = array(
array(
'type' => 'execdir',
'path' => __DIR__."/hooks",
'perms' => 0755
),
);
return $files;
}
public function getAstSpoolDir() {
return $this->astspooldir;
}
public function getAstTmpDir() {
return $this->asttmpdir;
}
public function getABBCliPath() {
return $this->config->get('SYNOLOGYABFBABBCLI');
}
public function getHookFilename($hookname, $hooktoken) {
$return = $this->getAstTmpDir() . "/synology-cli";
if (! empty($hookname))
{
$return .= "-" . $hookname;
}
if (! empty($hooktoken))
{
$return .= "-" . $hooktoken;
}
$return .= ".hook";
return $return;
}
public function runHook($hookname, $params = false) {
// Runs a new style Syadmin hook
if (!file_exists("/etc/incron.d/sysadmin")) {
throw new \Exception("Sysadmin RPM not up to date, or not a known OS.");
}
$basedir = $this->getAstSpoolDir()."/incron";
if (!is_dir($basedir)) {
throw new \Exception("$basedir is not a directory");
}
// Does our hook actually exist?
if (!file_exists(__DIR__."/hooks/$hookname")) {
throw new \Exception("Hook $hookname doesn't exist");
}
// So this is the hook I want to run
$filename = sprintf("%s/%s.%s", "$basedir", strtolower($this->module_name), $hookname);
if (file_exists($filename)) {
throw new \Exception("Hook $hookname is already running");
}
// If we have a modern sysadmin_rpm, we can put the params
// INSIDE the hook file, rather than as part of the filename
if (file_exists("/etc/sysadmin_contents_max")) {
$fh = fopen("/etc/sysadmin_contents_max", "r");
if ($fh) {
$max = (int) fgets($fh);
fclose($fh);
}
} else {
$max = false;
}
if ($max > 65535 || $max < 128) {
$max = false;
}
// Do I have any params?
$contents = "";
if ($params) {
// Oh. I do. If it's an array, json encode and base64
if (is_array($params)) {
$b = base64_encode(gzcompress(json_encode($params)));
// Note we derp the base64, changing / to _, because this may be used as a filepath.
if ($max) {
if (strlen($b) > $max) {
throw new \Exception("Contents too big for current sysadmin-rpm. This is possibly a bug!");
}
$contents = $b;
$filename .= ".CONTENTS";
} else {
$filename .= ".".str_replace('/', '_', $b);
if (strlen($filename) > 200) {
throw new \Exception("Too much data, and old sysadmin rpm. Please run 'yum update'");
}
}
} elseif (is_object($params)) {
throw new \Exception("Can't pass objects to hooks");
} else {
// Cast it to a string if it's anything else, and then make sure
// it doesn't have any spaces.
$filename .= ".".preg_replace("/[[:blank:]]+/", "", (string) $params);
}
}
$fh = fopen($filename, "w+");
if ($fh === false) {
// WTF, unable to create file?
throw new \Exception("Unable to create hook trigger '$filename'");
}
// Put our contents there, if there are any.
fwrite($fh, $contents);
// As soon as we close it, incron does its thing.
fclose($fh);
// Wait for up to 10 seconds and make sure it's been deleted.
$maxloops = 20;
$deleted = false;
while ($maxloops--) {
if (!file_exists($filename)) {
$deleted = true;
break;
}
usleep(500000);
}
if (!$deleted) {
throw new \Exception("Hook file '$filename' was not picked up by Incron after 10 seconds. Is it not running?");
}
return true;
}
private function runHookCheck($hook_file, $hook_run, $hook_params = array(), $decode = true, $timeout = 30) {
$error_code = self::ERROR_NOT_DEFINED;
$hook_info = null;
$hook_token = uniqid('hook');
$file = $this->getHookFilename($hook_file, $hook_token);
$hook_params['hook_file'] = $hook_file;
$hook_params['hook_token'] = $hook_token;
$this->runHook($hook_run, $hook_params);
$hookTimeOut = true;
if ($timeout == null || $timeout < 0)
{
// We wait 10 seconds to see if the file with the data is created and if the status change to RUN or END
$maxloops = 10 * 4;
$sleeploop = 250000;
$status_continue = array("RUN", "END");
}
else
{
// We wait for the number of seconds (default 30) that we specify to see if the file with the data is created and if the status changes to END
$maxloops = $timeout * 4;
$sleeploop = 250000;
$status_continue = array("END");
}
while ($maxloops--)
{
if (file_exists($file))
{
$decode_info = $this->readFileHook($file, true);
if ( !empty($decode_info) && !empty($decode_info['hook']))
{
$info_status = isset($decode_info['hook']['status']) ? $decode_info['hook']['status'] : '';
$info_hook = isset($decode_info['hook']['token']) ? $decode_info['hook']['token'] : '';
if ( $hook_token == $info_hook && in_array($info_status, $status_continue))
{
$hookTimeOut = false;
break;
}
}
}
usleep($sleeploop);
}
if ($hookTimeOut)
{
$error_code = self::ERROR_HOOK_RUN_TIMEOUT;
}
else
{
if(! file_exists($file))
{
$error_code = self::ERROR_HOOK_FILE_NOT_EXIST;
}
else
{
$linesfilehook = file_get_contents($file);
unlink($file);
if (trim($linesfilehook) == false)
{
$error_code = self::ERROR_HOOK_FILE_EMTRY;
}
else
{
$hook_info = @json_decode($linesfilehook, true);
if ($hook_token != $hook_info['hook']['token'])
{
$error_code = self::ERROR_HOOK_FILE_TOEKN;
}
else
{
$error_code = ($hook_info['error']['code'] === self::ERROR_ALL_GOOD ? self::ERROR_ALL_GOOD : $hook_info['error']['code']);
if (! $decode)
{
$hook_info = $linesfilehook;
}
}
}
}
}
return array(
'hook_file' => $hook_file,
'hook_run' => $hook_run,
'hook_token' => $hook_token,
'hook_data' => $hook_info,
'hook_timeout' => $timeout,
'file' => $file,
'decode' => $decode,
'error' => $error_code,
);
}
public function writeFileHook($file, $data, $encode = true) {
if (trim($file) == false) {
return false;
}
file_put_contents($file, ($encode == true ? json_encode($data) : $data));
chown($file, 'asterisk');
return true;
}
public function readFileHook($file, $decode = true) {
$return = "";
if (trim($file) != false)
{
$return = file_get_contents($file);
if ($decode == true)
{
$return = @json_decode($return, true);
}
}
return $return;
}
public function install() {
outn(_("Upgrading configs.."));
$set = array();
$set['value'] = '/usr/bin/abb-cli';
$set['defaultval'] =& $set['value'];
$set['readonly'] = 0;
$set['hidden'] = 0;
$set['level'] = 0;
$set['module'] = $this->module_name; //Fix needed FREEPBX-22756
$set['category'] = 'Synology Active Backup for Business';
$set['emptyok'] = 1;
$set['name'] = _('Path for abb-cli');
$set['description'] = _('The default path to abb-cli. overwrite as needed.');
$set['type'] = CONF_TYPE_TEXT;
$this->config->define_conf_setting('SYNOLOGYABFBABBCLI', $set, true);
out(_("Done!"));
}
public function uninstall() {}
public function backup() {}
public function restore($backup) {}
public function doConfigPageInit($page) {}
public function getActionBar($request) {}
public function getRightNav($request) {}
public function dashboardService() {
$status = array(
'title' => _('Synology Active Backup'),
'order' => 3,
);
$data = $this->getAgentStatus(true, false, false);
if ($data['error']['code'] === self::ERROR_ALL_GOOD)
{
$status_code = $data['info_status']['code'];
$status_msg = $data['info_status']['msg'];
}
else
{
$status_code = $data['error']['code'];
$status_msg = $data['error']['msg'];
}
$AlertGlyphIcon = null;
switch($status_code)
{
case self::STATUS_IDLE_COMPLETED:
$AlertGlyphIcon = $this->genStatusIcon('completed', $status_msg);
break;
case self::STATUS_BACKUP_RUN:
$AlertGlyphIcon = $this->genStatusIcon('run', sprintf("%s - %s", $status_msg, $data['info_status']['progress']['all']));
break;
case self::STATUS_IDLE:
case self::STATUS_IDLE_CANCEL:
case self::STATUS_IDLE_FAILED:
$AlertGlyphIcon = $this->genStatusIcon('warning', $status_msg);
break;
default:
$AlertGlyphIcon = $this->genStatusIcon('error', $status_msg);
break;
}
$status = array_merge($status, $AlertGlyphIcon);
return array($status);
}
private function genStatusIcon($type, $msg)
{
$list_types = array(
'completed' => array(
'type' => 'ok',
'class' => "glyphicon-floppy-saved text-success",
),
'run' => array(
'type' => 'info',
'class' => "glyphicon-export text-info", // glyphicon-floppy-open
),
);
$data_return = array();
if (! array_key_exists($type, $list_types))
{
$data_return = $this->Dashboard()->genStatusIcon($type, $msg);
}
else
{
$data_return = array(
'type' => empty($list_types[$type]['type']) ? $type : $list_types[$type]['type'],
"tooltip" => htmlentities(\ForceUTF8\Encoding::fixUTF8($msg), ENT_QUOTES,"UTF-8"),
"glyph-class" => $list_types[$type]['class'],
);
}
return $data_return;
}
public function showPage($page, $params = array())
{
$page = trim($page);
$page_show = '';
$data = array(
"syno" => $this,
'request' => $_REQUEST,
'page' => $page,
);
$data = array_merge($data, $params);
switch ($page)
{
case "":
$page_show = 'main';
break;
default:
$page_show = $page;
}
if (! empty($page_show))
{
//clean up possible things that don't have to be here
$filename = strtolower(str_ireplace(array('..','\\','/'), "", $page_show));
$page_path = sprintf("%s/views/%s.php", __DIR__, $filename);
if (! file_exists($page_path))
{
$page_show = '';
}
else
{
$data_return = load_view($page_path, $data);
}
}
if (empty($page_show))
{
$data_return = sprintf(_("Page Not Found (%s)!!!!"), $page);
}
return $data_return;
}
public function ajaxRequest($req, &$setting) {
// ** Allow remote consultation with Postman **
// ********************************************
// $setting['authenticate'] = false;
// $setting['allowremote'] = true;
// return true;
// ********************************************
switch($req)
{
case "getagentversion":
case "getagentversiononline":
case "getagentstatus":
case "setagentcreateconnection":
case "setagentreconnect":
case "setagentlogout":
case "runautoinstall":
case "runautoinstallstatus":
return true;
break;
default:
return false;
}
return false;
}
public function ajaxHandler() {
$command = $this->getReq("command", "");
$data_return = false;
switch ($command)
{
case 'runautoinstall':
$data_return = array("status" => true, "data" => $this->runAutoInstallAgent());
break;
case 'runautoinstallstatus':
$data_return = array("status" => true, "data" => $this->runAutoInstallAgent(true));
break;
case 'getagentversiononline':
$data_return = array("status" => true, "data" => $this->getAgentVersionOnline(false));
break;
case 'getagentversion':
$data_return = array("status" => true, "data" => $this->getAgentVersion(true, false));
break;
case 'getagentstatus':
$status_info = $this->getAgentStatus();
$status_info['agent_version'] = $this->getAgentVersion(true, false);
$data_return = array("status" => true, "data" => $status_info);
break;
case 'setagentcreateconnection':
$agent_server = $this->getReq("ABBServer", "");
$agent_username = $this->getReq("ABBUser", "");
$agent_password = $this->getReq("ABBPassword", "");
$return_status = $this->setAgentConnection($agent_server, $agent_username, $agent_password);
$data_return = array("status" => true, "data" => $return_status);
break;
case 'setagentreconnect':
$data_return = array("status" => true, "data" => $this->setAgentReConnect());
break;
case 'setagentlogout':
$agent_username = $this->getReq("ABBUser", "");
$agent_password = $this->getReq("ABBPassword", "");
$data_return = array("status" => true, "data" => $this->setAgentLogOut($agent_username, $agent_password));
break;
default:
$data_return = array("status" => false, "message" => _("Command not found!"), "command" => $command);
}
return $data_return;
}
private function parseUnitConvert($data)
{
return ((int) filter_var($data, FILTER_SANITIZE_NUMBER_INT) == 0 ? 0 : $data);
}
public function isOSCompatibilityAutoInstall()
{
// return true; //Testing
// System Updates are only usable on FreePBX Distro style machines
$su = new \FreePBX\Builtin\SystemUpdates();
return $su->canDoSystemUpdates() ? true : false;
}
public function isAgentInstalled() {
return file_exists($this->getABBCliPath());
}
public function isAgentVersionOk() {
$version_minimal = $this->ABBCliVersionMin;
$version_installed = $this->getAgentVersion(true);
return version_compare($version_minimal, $version_installed['full'], '<=');
}
public function getAgentStatusDefault() {
return self::$default_agent_status_data;
}
public function getAgentStatus($return_error = true, $force = false, $gen_html = true)
{
if ($force == true) // We force to refresh the status data
{
$this->setAgentReConnect();
usleep(500000);
}
$hook = $this->runHookCheck("status", "get-cli-status");
$error_code = $hook['error'];
$return = $this->getAgentStatusDefault();
$t_html = array(
'force' => false,
'body' => "",
'args' => array(),
);
$error_code_array = null;
$status_code = self::STATUS_NULL;
if ($error_code === self::ERROR_ALL_GOOD)
{
$hook_data = $hook['hook_data']['data'];
$t_info = array();
$hook_data['lastbackup_date'] = \DateTime::createFromFormat('Y-m-d H:i', $hook_data['lastbackup']);
$hook_data['nextbackup_date'] = \DateTime::createFromFormat('Y-m-d H:i', $hook_data['nextbackup']);
$t_status_info = preg_split('/[-]+/', $hook_data['server_status']);
$t_status_info = array_map('trim', $t_status_info); //Trim All Elements Array
$t_status_info_type = trim($t_status_info[0], chr(194) . chr(160));
$t_status_info_msg = @$t_status_info[1];
switch (strtolower($t_status_info_type))
{
case strtolower("Idle"):
if ($t_status_info_msg == "")
{
//MSG: Idle
$status_code = self::STATUS_IDLE;
}
else
{
//set generic unknown error
$status_code = self::STATUS_IDLE_UNKNOWN;
$t_list_idle = array(
//MSG: Idle - Completed
self::STATUS_IDLE_COMPLETED => strtolower("Completed"),
//MSG: Idle - Canceled
self::STATUS_IDLE_CANCEL => strtolower("Canceled"),
//MSG: Idle - Failed
self::STATUS_IDLE_FAILED => strtolower("Failed"),
);
//We check if it is any of the errors that we have controlled
foreach ($t_list_idle as $key => $val)
{
if ( strpos(strtolower($t_status_info_msg), $val) !== false )
{
$status_code = $key;
break;
}
}
unset($t_list_idle);
}
break;
case strtolower("Error"):
$t_list_errors = array(
//MSG: Error - The current device has been removed from the server. Please contact your administrator for further assistance.
self::STATUS_ERR_DEV_REMOVED => strtolower("The current device has been removed from the server"),
);
//set generic unknown error
$status_code = self::STATUS_ERR_UNKNOWN;
//We check if it is any of the errors that we have controlled
foreach ($t_list_errors as $key => $val)
{
if ( strpos(strtolower($t_status_info_msg), $val) !== false )
{
$status_code = $key;
break;
}
}
unset($t_list_errors);
break;
case strtolower("Backing up..."): // Backing up... - 8.31 MB / 9.57 MB (576.00 KB/s)
$status_code = self::STATUS_BACKUP_RUN;
$t_status_info['progress'] = preg_split('/[\(\)]+/', $t_status_info_msg);
$t_status_info['progress'] = array_map('trim', $t_status_info['progress']);
$t_status_info['progressdata'] = preg_split('/[\/]+/', $t_status_info['progress'][0]);
$t_status_info['progressdata'] = array_map('trim', $t_status_info['progressdata']);
$t_status_info['dataparsed'] = array(
'send' => \ByteUnits\parse($this->parseUnitConvert($t_status_info['progressdata'][0]))->numberOfBytes(),
'total' => \ByteUnits\parse($this->parseUnitConvert($t_status_info['progressdata'][1]))->numberOfBytes(),
'percentage' => 0,
);
if ($t_status_info['dataparsed']['total'] != 0)
{
$t_status_info['dataparsed']['percentage'] = round((100 / $t_status_info['dataparsed']['total']) * $t_status_info['dataparsed']['send']);
}
$t_info['progress'] = array(
'all' => $t_status_info_msg,
'send' => $t_status_info['dataparsed']['send'],
'total' => $t_status_info['dataparsed']['total'],
'speed' => $t_status_info['progress'][1],
'percentage' => $t_status_info['dataparsed']['percentage'],
);
break;
case strtolower("No connection found"):
$status_code = self::STATUS_NO_CONNECTION;
break;
default:
$status_code = self::STATUS_UNKNOWN;
break;
}
if (! is_array($status_code))
{
$status_code = $this->getStatusMsgByCode($status_code, true);
}
$hook_data['info_status'] = array_merge($status_code, $t_info);
if ($status_code['code'] >= self::STATUS_UNKNOWN )
{
$this->logger->warning( sprintf("%s->%s - Code (%s): Status not controlled [%s]!", $this->module_name, __FUNCTION__, $status_code['code'], $hook_data['server_status']));
}
switch (strtolower($t_status_info_type))
{
case strtolower("Backing up..."):
$t_html['force'] = true;
case strtolower("Error"):
case strtolower("Idle"):
$t_html['body'] = "main.body.info";
$t_html['args'] = array(
'info' => $hook_data,
'status' => $status_code,
'status_type' => strtolower($t_status_info_type)
);
break;
case strtolower("No connection found"):
$t_html['body'] = "main.body.login";
break;
default:
$t_html['body'] = "main.body.error";
}
$return = $hook_data;
}
elseif ($error_code === self::ERROR_AGENT_NOT_INSTALLED || $error_code === self::ERROR_AGENT_IS_INSTALLING)
{
$t_html['body'] = "main.steps.install";
$t_html['args'] = array(
'runing_installation' => $error_code === self::ERROR_AGENT_IS_INSTALLING ? true : false,
'allow_auto_install' => $this->isOSCompatibilityAutoInstall(),
);
}
else
{
$t_html['body'] = "main.body.error";
}
$error_code_array = $this->getErrorMsgByErrorCode($error_code, true);
if ($gen_html)
{
if (! empty($t_html['body']))
{
$t_html['args_default'] = array(
'error_code' => $error_code,
'error_info' => $error_code_array,
'status_info' => $status_code,
);
$return['html'] = array(
'force' => $t_html['force'],
'body' => $this->showPage($t_html['body'], array_merge($t_html['args_default'], $t_html['args'])),
);
}
}
if ($return_error)
{
$return['error'] = $error_code_array;
}
return $return;
}
public function getAgentVersion($return_array = false, $return_error = true)
{
$hook = $this->runHookCheck("version", "get-cli-version");
$error_code = $hook['error'];
$return = "0.0.0-0";
if ($error_code === self::ERROR_ALL_GOOD)
{
$hook_data = $hook['hook_data']['data'];
$app_ver = $hook_data['version'];
if (! empty($app_ver))
{
$return = $app_ver;
}
}
if ($return_array)
{
$app_ver_array = explode(".", str_replace("-", ".", $return));
$return = array(
'major' => $app_ver_array[0],
'minor' => $app_ver_array[1],
'patch' => $app_ver_array[2],
'build' => $app_ver_array[3],
'full' => $return,
);
}
if ($return_array && $return_error)
{
$return['error'] = $this->getErrorMsgByErrorCode($error_code, true);
}
return $return;
}
public function setAgentConnection($server, $user, $pass)
{
$return = array();
$hook_params = array(
"server" => $server,
"username" => $user,
"password" => $pass,
);
$hook_params = array_map('trim', $hook_params);
$hook = $this->runHookCheck("createconnection", "set-cli-create-connection", $hook_params);
$error_code = $hook['error'];
$return['error'] = $this->getErrorMsgByErrorCode($error_code, true);
return $return;
}
public function setAgentReConnect()
{
$return = array();
$hook = $this->runHookCheck("reconnect", "set-cli-reconnect");
$return['error'] = $this->getErrorMsgByErrorCode($hook['error'], true);
return $return;
}
public function runAutoInstallAgent($readonly = false)
{
$return = array();
$hook_params = array(
"readonly" => $readonly,
);
$hook = $this->runHookCheck("autoinstall", "run-install-agent", $hook_params, true, "-1");
$error_code = $hook['error'];
if ($error_code === self::ERROR_ALL_GOOD)
{
$return['info'] = $hook['hook_data']['data'];
}
else
{
$return['info'] = $this->AutoInstallReadInfo();
$return['info']['out'] = $this->AutoInstallReadOut();
}
$return['error'] = $this->getErrorMsgByErrorCode($error_code, true);
return $return;
}
public function setAgentLogOut($user, $pass)
{
$return = array();
$hook_params = array(
"username" => $user,
"password" => $pass,
);
$hook_params = array_map('trim', $hook_params);
$hook = $this->runHookCheck("logout", "set-cli-logout", $hook_params);
$error_code = $hook['error'];
$return['error'] = $this->getErrorMsgByErrorCode($error_code, true);
return $return;
}
public function getStatusMsgByCode($status_code, $return_array = false)
{
$msg = "";
switch($status_code)
{
case self::STATUS_BACKUP_RUN:
$msg = _("Backup in Progress...");
break;
case self::STATUS_IDLE_CANCEL:
$msg = _("Canceled");
break;
case self::STATUS_IDLE_COMPLETED:
$msg = _("Completed");
break;
case self::STATUS_IDLE:
$msg = _("Pending First Copy");
break;
case self::STATUS_IDLE_FAILED:
$msg = _("Failed");
break;
case self::STATUS_NO_CONNECTION:
$msg = _("No Connection");
break;
case self::STATUS_ERR_DEV_REMOVED:
$msg = _("Device Removed From Server");
break;
case self::STATUS_UNKNOWN:
case self::STATUS_IDLE_UNKNOWN:
case self::STATUS_ERR_UNKNOWN:
$msg = _("Status Unknown");
break;
default:
$msg = sprintf(_("The status code (%s) is not controlled!"), $status_code);
}
return ($return_array ? array( 'code' => $status_code, 'msg' => $msg ) : $msg);
}
public function getErrorMsgByErrorCode($error_code, $return_array = false, $msg_alternative = null)
{
$msg = "";
switch($error_code)
{
case self::ERROR_NOT_DEFINED: //-1
$msg = _("Nothing has been defined yet.");
break;
case self::ERROR_ALL_GOOD: //0
$msg = _("No mistake, everything ok.");
break;
case self::ERROR_AGENT_NOT_INSTALLED: //501
$msg = _("Synology Agent not Installed!");
break;
case self::ERROR_AGENT_NOT_RETURN_INFO: //502
$msg = _("Synology Agent not return info!");
break;
case self::ERROR_AGENT_ENDED_IN_ERROR: //503
$msg = _("Synology Agent ended in error!");
break;
case self::ERROR_AGENT_RETURN_UNCONTROLLED: //504
$msg = _("Synology Agent returned uncontrolled information!");
break;
case self::ERROR_AGENT_IS_INSTALLING: //505
$msg = _("Synology Agent Is Installing...");
break;
case self::ERROR_AGENT_SERVER_CHECK: //5501
$msg = _("The server is not available!");
break;
case self::ERROR_AGENT_ALREADY_CONNECTED: //520
$msg = _("Synology Agent Already connected!");
break;
case self::ERROR_AGENT_NOT_ALREADY_CONNECTED: //521
$msg = _("Synology Agent Not Already Connected!");
break;
case self::ERROR_AGENT_SERVER_AUTH_FAILED: //511
$msg = _("The server returned an authentication failed error!");
break;
case self::ERROR_AGENT_SERVER_AUTH_FAILED_USER_PASS: //512
$msg = _("The username or password you entered is incorrect!");
break;
case self::ERROR_AGENT_SERVER_AUTH_FAILED_BAN_IP: //513
$msg = _("This IP address has been blocked because it has reached the maximum number of failed login attempts allowed within a specific time period!");
break;
case self::ERROR_HOOK_FILE_NOT_EXIST: //610
$msg = _("The file that returns the hook information does not exist!");
break;
case self::ERROR_HOOK_FILE_EMTRY: //615
$msg = _("Hook file is empty!");