forked from mx504/freepbx-endpointmanager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Endpointman_Config.class.php
1704 lines (1486 loc) · 75.8 KB
/
Endpointman_Config.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
/**
* Endpoint Manager Object Module - Sec Config
*
* @author Javier Pastor
* @license MPL / GPLv2 / LGPL
* @package Provisioner
*/
namespace FreePBX\modules;
class Endpointman_Config
{
public $UPDATE_PATH;
public $MODULES_PATH;
public $LOCAL_PATH;
public $PHONE_MODULES_PATH;
public $PROVISIONER_BASE;
public function __construct($freepbx = null, $cfgmod = null, $system = null)
{
$this->freepbx = $freepbx;
$this->db = $freepbx->Database;
$this->config = $freepbx->Config;
$this->configmod = $cfgmod;
$this->system = $system;
$this->UPDATE_PATH = $this->configmod->get('update_server');
$this->MODULES_PATH = $this->config->get('AMPWEBROOT') . '/admin/modules/';
if (file_exists($this->MODULES_PATH . "endpointman/")) {
$this->LOCAL_PATH = $this->MODULES_PATH . "endpointman/";
} else {
die("Can't Load Local Endpoint Manager Directory!");
}
if (file_exists($this->MODULES_PATH . "_ep_phone_modules/")) {
$this->PHONE_MODULES_PATH = $this->MODULES_PATH . "_ep_phone_modules/";
} else {
$this->PHONE_MODULES_PATH = $this->MODULES_PATH . "_ep_phone_modules/";
if (!file_exists($this->PHONE_MODULES_PATH)) {
mkdir($this->PHONE_MODULES_PATH, 0775);
}
if (file_exists($this->PHONE_MODULES_PATH . "setup.php")) {
unlink($this->PHONE_MODULES_PATH . "setup.php");
}
if (!file_exists($this->MODULES_PATH . "_ep_phone_modules/")) {
die('Endpoint Manager can not create the modules folder!');
}
}
}
public function myShowPage(&$pagedata) {
}
public function ajaxRequest($req, &$setting) {
$arrVal = array("saveconfig", "list_all_brand", "list_brand_model_hide");
if (in_array($req, $arrVal)) {
$setting['authenticate'] = true;
$setting['allowremote'] = false;
return true;
}
return false;
}
public function ajaxHandler($module_tab = "", $command = "")
{
$txt = array(
'ayuda_model' => _("If we can activate the model set terminals of the models.<br /> If this model is disabled will not appear in the list of models that can be configured for PBX."),
'ayuda_producto' => _('The button "Install Firmware" installs the necessary files to the server for the terminal alone are updated via TFTP or HTTP.<br /> The button "Remove frimware" delete files server products.<br /> The button "Update frimware" appears if a newer frimware detected on the server and asks if you want to update.<br /> The "Update" button appears when a new version of this model pack is detected.'),
'ayuda_marca' => _('The "Install" button installs the configuration package brand models we selected.<br /> The "Uninstall" button removes the package configuration models of the brand selected.<br /> The "Update" button appears if a new version of the package that is already installed to upgrade to the latest version is detected.'),
'new_pack_mod' => _("New Package Modified"),
'pack_last_mod' => _("Package Last Modified"),
'check_update' => _("Check for Update "),
'check_online' => _("Check Online "),
'install' => _("Install"),
'uninstall' => _("Uninstall"),
'update' => _("Update"),
'fw_install' => _('FW Install'),
'fw_uninstall' => _('FW Delete'),
'fw_update' => _('FW Update'),
'enable' => _('Enable'),
'disable' => _('Disable'),
'show' => _("Show"),
'hide' => _("Hide"),
'ready' => _("Ready!"),
'error' => _("Error!"),
'title_update' => _("Update!"),
'save_changes' => _("Saving Changes..."),
'save_changes_ok' => _("Saving Changes... Ok!"),
'err_upload_content' => _("Upload Content!"),
'check' => _("Check for Updates..."),
'check_ok' => _("Check for Updates... Ok!"),
'update_content' => _("Update Content..."),
'opt_invalid' => _("Invalid Option!")
);
switch ($command)
{
case "saveconfig":
$retarr = $this->epm_config_manager_saveconfig();
break;
case "list_all_brand":
$retarr = array("status" => true, "message" => "OK", "datlist" => $this->epm_config_manager_hardware_get_list_all());
break;
case "list_brand_model_hide":
$retarr = array("status" => true, "message" => "OK", "datlist" => $this->epm_config_manager_hardware_get_list_all_hide_show());
break;
default:
$retarr = array("status" => false, "message" => _("Command not found!") . " [" .$command. "]");
break;
}
$retarr['txt'] = $txt;
return $retarr;
}
public function doConfigPageInit($module_tab = "", $command = "") {
switch ($command) {
case "check_for_updates":
$this->epm_config_manager_check_for_updates();
echo "<br /><hr><br />";
exit;
break;
case "manual_install":
$this->epm_config_manual_install();
echo "<br /><hr><br />";
exit;
break;
case "firmware":
$this->epm_config_manager_firmware();
echo "<br /><hr><br />";
exit;
break;
case "brand":
$this->epm_config_manager_brand();
echo "<br /><hr><br />";
exit;
break;
}
}
public function getRightNav($request) {
return "";
}
public function getActionBar($request) {
return "";
}
/**
* Get info models by product id selected.
* @param int $id_product product ID
* @param bool $show_all True return all, False return hidden = 0
* @return array
*/
public function epm_config_hardware_get_list_models($id_product=NULL, $show_all = true, $byorder = "model")
{
if(! is_numeric($id_product)) { throw new \Exception( _("ID Producto not is number")." (".$id_product.")"); }
if($show_all == true) { $sql = 'SELECT * FROM endpointman_model_list WHERE product_id = '.$id_product.' ORDER BY '.$byorder.' ASC'; }
else { $sql = 'SELECT * FROM endpointman_model_list WHERE hidden = 0 AND product_id = '.$id_product.' ORDER BY '.$byorder.' ASC'; }
$result = sql($sql, 'getAll', DB_FETCHMODE_ASSOC);
return $result;
}
/**
* Get info product by brand id selected.
* @param int $id_brand brand ID
* @param bool $show_all True return all, FAlse return hidde = 0
* @return array
*/
public function epm_config_hardware_get_list_product($id_brand=NULL, $show_all = true, $byorder = "long_name")
{
if(! is_numeric($id_brand)) { throw new \Exception(_("ID Brand not is numbre")." (".$id_brand.")"); }
if ($show_all == true) { $sql = 'SELECT * FROM endpointman_product_list WHERE brand = '.$id_brand.' ORDER BY '.$byorder.' ASC'; }
else { $sql = 'SELECT * FROM endpointman_product_list WHERE hidden = 0 AND brand = '.$id_brand.' ORDER BY '.$byorder.' ASC'; }
$result = sql($sql, 'getAll', DB_FETCHMODE_ASSOC);
return $result;
}
/**
* Get info all brands.
* @param bool $show_all True return all, False return hidde = 0
* @return array
*/
public function epm_config_hardware_get_list_brand($show_all = true, $byorder = "id") {
if ($show_all == true) { $sql = "SELECT * from endpointman_brand_list WHERE id > 0 ORDER BY " . $byorder . " ASC "; }
else { $sql = "SELECT * from endpointman_brand_list WHERE id > 0 AND hidden = 0 ORDER BY " . $byorder . " ASC "; }
$result = sql($sql, 'getAll', DB_FETCHMODE_ASSOC);
return $result;
}
/**** FUNCIONES SEC MODULO "epm_config\editor" ****/
/**
* Get info all brdans, prodics, models.
* @return array
*/
/*
SE DESACTIVA A VERS SI NO SE USA EN NINGUN SITIO.
public function epm_config_editor_hardware_get_list_all ()
{
$row_out = array();
$i = 0;
foreach ($this->epm_config_hardware_get_list_brand(true, "name") as $row)
{
$row_out[$i] = $row;
$row_out[$i]['count'] = $i;
if ($row['installed'])
{
$j = 0;
foreach ($this->epm_config_hardware_get_list_product($row['id'], true) as $row2)
{
$row_out[$i]['products'][$j] = $row2;
$k = 0;
foreach ($this->epm_config_hardware_get_list_models($row2['id'], true) as $row3) {
$row_out[$i]['products'][$j]['models'][$k] = $row3;
$k++;
}
$j++;
}
}
$i++;
}
return $row_out;
}
*/
/*** END SEC FUNCTIONS ***/
/**** FUNCIONES SEC MODULO "epm_config\manager" ****/
private function epm_config_manager_check_for_updates ()
{
out("<h3>Update data...</h3>");
$this->update_check(true);
out ("All Done!");
}
private function epm_config_manager_brand()
{
$arrVal['VAR_REQUEST'] = array("command_sub", "idfw");
foreach ($arrVal['VAR_REQUEST'] as $valor) {
if (! array_key_exists($valor, $_REQUEST)) {
out (_("Error: No send value!")." [".$valor."]");
return false;
}
}
$arrVal['VAR_IS_NUM'] = array("idfw");
foreach ($arrVal['VAR_IS_NUM'] as $valor) {
if (! is_numeric($_REQUEST[$valor])) {
out (_("Error: Value send is not number!")." [".$valor."]");
return false;
}
}
$dget['command'] = strtolower($_REQUEST['command_sub']);
$dget['id'] = $_REQUEST['idfw'];
switch($dget['command']) {
case "brand_install":
case "brand_update":
$this->download_brand($dget['id']);
break;
case "brand_uninstall":
$this->remove_brand($dget['id']);
break;
default:
out (_("Error: Command not found!")." [" . $dget['command'] . "]");
}
$this->update_check();
unset ($dget);
return true;
}
private function epm_config_manager_firmware()
{
$arrVal['VAR_REQUEST'] = array("command_sub", "idfw");
foreach ($arrVal['VAR_REQUEST'] as $valor) {
if (! array_key_exists($valor, $_REQUEST)) {
out (_("Error: No send value!")." [".$valor."]");
return false;
}
}
$arrVal['VAR_IS_NUM'] = array("idfw");
foreach ($arrVal['VAR_IS_NUM'] as $valor) {
if (! is_numeric($_REQUEST[$valor])) {
out (_("Error: Value send is not number!")." [".$valor."]");
return false;
}
}
$dget['command'] = strtolower($_REQUEST['command_sub']);
$dget['id'] = $_REQUEST['idfw'];
switch($dget['command']) {
case "fw_install":
case "fw_update":
$this->install_firmware($dget['id']);
break;
case "fw_uninstall":
$this->remove_firmware($dget['id']);
break;
default:
out (_("Error: Command not found!")." [" . $dget['command'] . "]");
}
unset ($dget);
return true;
}
private function epm_config_manager_saveconfig()
{
$arrVal['VAR_REQUEST'] = array("typesavecfg", "value", "idtype", "idbt");
foreach ($arrVal['VAR_REQUEST'] as $valor) {
if (! array_key_exists($valor, $_REQUEST)) {
return array("status" => false, "message" => _("No send value!")." [".$valor."]");
}
}
$arrVal['VAR_IS_NUM'] = array("value", "idbt");
foreach ($arrVal['VAR_IS_NUM'] as $valor) {
if (! is_numeric($_REQUEST[$valor])) {
return array("status" => false, "message" => _("Value send is not number!")." [".$valor."]");
}
}
$dget['typesavecfg'] = strtolower($_REQUEST['typesavecfg']);
$dget['value'] = strtolower($_REQUEST['value']);
$dget['idtype'] = strtolower($_REQUEST['idtype']);
$dget['id'] = $_REQUEST['idbt'];
if (! in_array($dget['typesavecfg'], array("hidden", "enabled"))) {
return array("status" => false, "message" => _("Type Save Config is not valid!")." [".$dget['typesavecfg']."]");
}
if (($dget['value'] > 1 ) and ($dget['value'] < 0)) {
return array("status" => false, "message" => _("Invalid Value!"));
}
if ($dget['typesavecfg'] == "enabled") {
if (($dget['idtype']) == "modelo") {
$sql = "UPDATE endpointman_model_list SET enabled = " .$dget['value']. " WHERE id = '".$dget['id']."'";
}
else {
$retarr = array("status" => false, "message" => _("IdType not valid to typesavecfg!"));
}
}
else {
switch($dget['idtype']) {
case "marca":
$sql = "UPDATE endpointman_brand_list SET hidden = '".$dget['value'] ."' WHERE id = '".$dget['id']."'";
break;
case "producto":
$sql = "UPDATE endpointman_product_list SET hidden = '". $dget['value'] ."' WHERE id = '".$dget['id']."'";
break;
case "modelo":
$sql = "UPDATE endpointman_model_list SET hidden = '". $dget['value'] ."' WHERE id = '".$dget['id']."'";
break;
default:
$retarr = array("status" => false, "message" => _("IDType invalid: ") . $dget['idtype'] );
}
}
if (isset($sql)) {
sql($sql);
$retarr = array("status" => true, "message" => "OK", "typesavecfg" => $dget['typesavecfg'], "value" => $dget['value'], "idtype" => $dget['idtype'], "id" => $dget['id']);
unset($sql);
}
unset($dget);
return $retarr;
}
public function epm_config_manager_hardware_get_list_all_hide_show()
{
$row_out = array();
$i = 0;
$brand_list = $this->epm_config_hardware_get_list_brand(true, "name");
foreach ($brand_list as $row)
{
//$row_out[$i] = $row;
$row_out[$i]['id'] = $row['id'];
$row_out[$i]['name'] = $row['name'];
$row_out[$i]['directory'] = $row['directory'];
$row_out[$i]['installed'] = $row['installed'];
$row_out[$i]['hidden'] = $row['hidden'];
$row_out[$i]['count'] = $i;
$row_out[$i]['products'] = "";
if ($row['hidden'] == 1)
{
$i++;
continue;
}
$j = 0;
$product_list = $this->epm_config_hardware_get_list_product($row['id'], true);
foreach($product_list as $row2) {
//$row_out[$i]['products'][$j] = $row2;
$row_out[$i]['products'][$j]['id'] = $row2['id'];
$row_out[$i]['products'][$j]['brand'] = $row2['brand'];
$row_out[$i]['products'][$j]['long_name'] = $row2['long_name'];
$row_out[$i]['products'][$j]['short_name'] = $row2['short_name'];
$row_out[$i]['products'][$j]['hidden'] = $row2['hidden'];
$row_out[$i]['products'][$j]['count'] = $j;
$row_out[$i]['products'][$j]['models'] = "";
if ($row2['hidden'] == 1)
{
$j++;
continue;
}
$k = 0;
$model_list = $this->epm_config_hardware_get_list_models($row2['id'], true);
foreach($model_list as $row3)
{
//$row_out[$i]['products'][$j]['models'][$k] = $row3;
$row_out[$i]['products'][$j]['models'][$k]['id'] = $row3['id'];
$row_out[$i]['products'][$j]['models'][$k]['brand'] = $row3['brand'];
$row_out[$i]['products'][$j]['models'][$k]['model'] = $row3['model'];
$row_out[$i]['products'][$j]['models'][$k]['product_id'] = $row3['product_id'];
$row_out[$i]['products'][$j]['models'][$k]['enabled'] = $row3['enabled'];
$row_out[$i]['products'][$j]['models'][$k]['hidden'] = $row3['hidden'];
$row_out[$i]['products'][$j]['models'][$k]['count'] = $k;
$k++;
}
$j++;
}
$i++;
}
//echo "<textarea>" . print_r($row_out, true) . "</textarea>";
return $row_out;
}
//TODO: PENDIENTE ACTUALIZAR Y ELIMINAR DATOS NO NECESARIOS (TEMPLATES)
//http://pbx.cerebelum.lan/admin/ajax.php?module=endpointman&module_sec=epm_config&module_tab=manager&command=list_all_brand
public function epm_config_manager_hardware_get_list_all()
{
$row_out = array();
$i = 0;
$brand_list = $this->epm_config_hardware_get_list_brand(true, "name");
//FIX: https://github.com/FreePBX-ContributedModules/endpointman/commit/2ad929d0b38f05c9da1b847426a4094c3314be3b
foreach ($brand_list as $row)
{
$row_out[$i] = $row;
$row_out[$i]['count'] = $i;
$row_out[$i]['cfg_ver_datetime'] = $row['cfg_ver'];
$row_out[$i]['cfg_ver_datetime_txt'] = date("c",$row['cfg_ver']);
$row_mod = $this->brand_update_check($row['directory']);
$row_out[$i]['update'] = $row_mod['update'];
if(isset($row_mod['update_vers'])) {
$row_out[$i]['update_vers'] = $row_mod['update_vers'];
$row_out[$i]['update_vers_txt'] = date("c",$row_mod['update_vers']);
}
if (! isset($row_out[$i]['update'])) { $row_out[$i]['update'] = ""; }
if (! isset($row_out[$i]['update_vers'])) { $row_out[$i]['update_vers'] = $row_out[$i]['cfg_ver_datetime']; }
if (! isset($row_out[$i]['update_vers_txt'])) { $row_out[$i]['update_vers_txt'] = $row_out[$i]['cfg_ver_datetime_txt']; }
if ($row['hidden'] == 1)
{
$i++;
continue;
}
$j = 0;
$product_list = $this->epm_config_hardware_get_list_product($row['id'], true);
foreach($product_list as $row2) {
$row_out[$i]['products'][$j] = $row2;
if((array_key_exists('firmware_vers', $row2)) AND ($row2['firmware_vers'] > 0)) {
$temp = $this->firmware_update_check($row2['id']);
$row_out[$i]['products'][$j]['update_fw'] = 1;
$row_out[$i]['products'][$j]['update_vers_fw'] = $temp['data']['firmware_ver'];
} else {
$row_out[$i]['products'][$j]['update_fw'] = 0;
$row_out[$i]['products'][$j]['update_vers_fw'] = "";
}
if (! isset($row_out[$i]['products'][$j]['update_fw'])) { $row_out[$i]['products'][$j]['update_fw'] = 0; }
if (! isset($row_out[$i]['products'][$j]['update_vers_fw'])) { $row_out[$i]['products'][$j]['update_vers_fw'] = ""; }
$row_out[$i]['products'][$j]['fw_type'] = $this->firmware_local_check($row2['id']);
$row_out[$i]['products'][$j]['count'] = $j;
if ($row2['hidden'] == 1)
{
$j++;
continue;
}
$k = 0;
$model_list = $this->epm_config_hardware_get_list_models($row2['id'], true);
foreach($model_list as $row3)
{
$row_out[$i]['products'][$j]['models'][$k] = $row3;
unset ($row_out[$i]['products'][$j]['models'][$k]['template_list']);
unset ($row_out[$i]['products'][$j]['models'][$k]['template_data']);
if($row_out[$i]['products'][$j]['models'][$k]['enabled']){
$row_out[$i]['products'][$j]['models'][$k]['enabled_checked'] = 'checked';
}
$row_out[$i]['products'][$j]['models'][$k]['count'] = $k;
$k++;
}
$j++;
}
$i++;
}
//echo "<textarea>".print_r($row_out,true)."</textarea>";
return $row_out;
}
/*** END SEC FUNCTIONS ***/
function brand_update_check($brand_name_find = NULL)
{
if ($brand_name_find == NULL) { return $this->brand_update_check_all(); }
$sql = "SELECT * FROM endpointman_brand_list WHERE directory = '" . $brand_name_find . "'";
$row = sql($sql, 'getRow', DB_FETCHMODE_ASSOC);
$out = array();
if (! isset($row['directory']))
{
$out['update'] = -2;
}
else
{
if (file_exists($this->PHONE_MODULES_PATH . "endpoint/" . $row['directory'] . "/brand_data.json"))
{
$temp = $this->file2json($this->PHONE_MODULES_PATH . "endpoint/" . $row['directory'] . "/brand_data.json");
$temp = $temp['data']['brands'];
$version = $temp['last_modified'];
$last_mod = "";
foreach ($temp['family_list'] as $list) {
$last_mod = max($last_mod, $list['last_modified']);
}
$last_mod = max($last_mod, $version);
$version = $last_mod;
if ($row['cfg_ver'] < $version) {
$out['update'] = 1;
$out['update_vers'] = $version;
} else {
$out['update'] = NULL;
$out['update_vers'] = $version;
}
}
else {
$out['update'] = -1;
}
}
return $out;
}
function brand_update_check_all()
{
$temp = $this->file2json($this->PHONE_MODULES_PATH . 'endpoint/master.json');
$endpoint_package = $temp['data']['package'];
$endpoint_last_mod = $temp['data']['last_modified'];
$version = array();
$out = $temp['data']['brands'];
foreach ($out as $data) {
if (file_exists($this->PHONE_MODULES_PATH . "endpoint/" . $data['directory'] . "/brand_data.json")) {
$temp = $this->file2json($this->PHONE_MODULES_PATH . "endpoint/" . $data['directory'] . "/brand_data.json");
$temp = $temp['data']['brands'];
$brand_name = $temp['directory'];
$version[$brand_name] = $temp['last_modified'];
$last_mod = "";
foreach ($temp['family_list'] as $list) {
$last_mod = max($last_mod, $list['last_modified']);
}
$last_mod = max($last_mod, $version[$brand_name]);
$version[$brand_name] = $last_mod;
}
}
$sql = 'SELECT * FROM endpointman_brand_list WHERE id > 0';
$row = sql($sql, 'getAll', DB_FETCHMODE_ASSOC);
foreach ($row as $ava_brands) {
$key = $this->system->arraysearchrecursive($ava_brands['directory'], $out, 'directory');
if ($key === FALSE) {
$tmp = $ava_brands;
$tmp['update'] = -1;
$out[] = $tmp;
}
else {
$key = $key[0];
$brand_name = $ava_brands['directory'];
if (! isset($version[$brand_name])) { $version[$brand_name] = 0; }
if ($ava_brands['cfg_ver'] < $version[$brand_name]) {
$out[$key]['update'] = 1;
$out[$key]['update_vers'] = $version[$brand_name];
} else {
$out[$key]['update'] = NULL;
}
}
}
return $out;
}
/**
* Check for new packges for brands. These packages will include phone models and such which the user can remove if they want
* This function will alos auto-update the provisioner.net library incase anything has changed
* @return array An array of all the brands/products/models and information about what's enabled, installed or otherwise
*/
function update_check($echomsg = false, &$error=array()) {
$temp_location = $this->system->sys_get_temp_dir() . "/epm_temp/";
if (!$this->configmod->get('use_repo')) {
if ($echomsg == true) {
$master_result = $this->system->download_file_with_progress_bar($this->UPDATE_PATH . "master.json", $this->PHONE_MODULES_PATH . "endpoint/master.json");
} else {
$master_result = $this->system->download_file($this->UPDATE_PATH . "master.json", $this->PHONE_MODULES_PATH . "endpoint/master.json");
}
if (!$master_result || !file_exists($this->PHONE_MODULES_PATH . "endpoint/master.json")) {
$error['brand_update_check_master'] = _("Error: Not able to connect to repository. Using local master file instead.");
if ($echomsg == true ) {
out($error['brand_update_check_master']);
}
}
$temp = $this->file2json($this->PHONE_MODULES_PATH . 'endpoint/master.json');
$endpoint_package = $temp['data']['package'];
$endpoint_last_mod = $temp['data']['last_modified'];
$sql = "SELECT value FROM endpointman_global_vars WHERE var_name LIKE 'endpoint_vers'";
$data = sql($sql, 'getOne');
$contents = file_get_contents($this->UPDATE_PATH . "/update_status");
if ($contents != '1') {
if (($data == "") OR ($data <= $endpoint_last_mod)) {
if ((!$master_result) OR (!$this->system->download_file($this->UPDATE_PATH . '/' . $endpoint_package, $temp_location . $endpoint_package)))
{
$error['brand_update_check_json'] = _("Not able to connect to repository. Using local Provisioner.net Package");
if ($echomsg == true ) {
out($error['brand_update_check_json']);
}
} else {
exec("tar -xvf " . $temp_location . $endpoint_package . " -C " . $temp_location);
if (!file_exists($this->PHONE_MODULES_PATH . "endpoint")) {
mkdir($this->PHONE_MODULES_PATH . "endpoint");
}
//TODO: Automate this somehow...
rename($temp_location . "setup.php", $this->PHONE_MODULES_PATH . "autoload.php");
rename($temp_location . "endpoint/base.php", $this->PHONE_MODULES_PATH . "endpoint/base.php");
rename($temp_location . "endpoint/global_template_data.json", $this->PHONE_MODULES_PATH . "endpoint/global_template_data.json");
$sql = "UPDATE endpointman_global_vars SET value = '" . $endpoint_last_mod . "' WHERE var_name = 'endpoint_vers'";
sql($sql);
}
}
$out = $temp['data']['brands'];
//Assume that if we can't connect and find the master.xml file then why should we try to find every other file.
if ($master_result) {
$sql = 'SELECT * FROM endpointman_brand_list WHERE id > 0';
$row = sql($sql, 'getAll', DB_FETCHMODE_ASSOC);
foreach ($out as $data) {
$local = sql("SELECT local FROM endpointman_brand_list WHERE directory = '" . $data['directory'] . "'", 'getOne');
if (!$local) {
if ($echomsg == true) {
out(sprintf(_("Update Brand (%s):"), $data['name']));
$result = $this->system->download_file_with_progress_bar($this->UPDATE_PATH . $data['directory'] . "/" . $data['directory'] . ".json", $this->PHONE_MODULES_PATH . "endpoint/" . $data['directory'] . "/brand_data.json");
} else {
$result = $this->system->download_file($this->UPDATE_PATH . $data['directory'] . "/" . $data['directory'] . ".json", $this->PHONE_MODULES_PATH . "endpoint/" . $data['directory'] . "/brand_data.json");
}
if (!$result) {
$error['brand_update_check'] = sprintf(_("Not able to connect to repository. Using local brand [%s] file instead."), $data['name']);
if ($echomsg == true ) {
out($error['brand_update_check']);
}
}
}
if (file_exists($this->PHONE_MODULES_PATH . "endpoint/" . $data['directory'] . "/brand_data.json")) {
$temp = $this->file2json($this->PHONE_MODULES_PATH . "endpoint/" . $data['directory'] . "/brand_data.json");
$temp = $temp['data']['brands'];
if (array_key_exists('oui_list', $temp)) {
foreach ($temp['oui_list'] as $oui) {
$sql = "REPLACE INTO endpointman_oui_list (`oui`, `brand`, `custom`) VALUES ('" . $oui . "', '" . $temp['brand_id'] . "', '0')";
sql($sql);
}
}
$brand_name = $temp['directory'];
$version[$brand_name] = $temp['last_modified'];
$last_mod = "";
foreach ($temp['family_list'] as $list) {
$last_mod = max($last_mod, $list['last_modified']);
}
$last_mod = max($last_mod, $version[$brand_name]);
$version[$brand_name] = $last_mod;
if (!($this->system->arraysearchrecursive($brand_name, $row, 'directory')))
{
$sql = 'SELECT directory FROM endpointman_brand_list where id = "'.$temp['brand_id'].'"';
$datoif = sql($sql, 'getOne');
if ($datoif != "") {
$error['brand_update_id_exist_other_brand'] = sprintf(_("You can not add the mark (%s) as the ID (%d) already exists in the database!"), $temp['name'], $temp['brand_id']);
if ($echomsg == true ) {
out($error['brand_update_id_exist_other_brand']);
}
}
else {
$sql = "INSERT INTO endpointman_brand_list (id, name, directory, cfg_ver) VALUES ('" . $temp['brand_id'] . "', '" . $temp['name'] . "', '" . $temp['directory'] . "', '" . $version[$brand_name] . "')";
sql($sql);
}
} else {
//in database already!
}
} else {
$error['brand_update_check_local_file'] = sprintf(_("Error: No Local File for %s !"), $data['name'])."<br />"._("Learn how to manually upload packages here (it's easy!):")."<a href='http://wiki.provisioner.net/index.php/Endpoint_manager_manual_upload' target='_blank'>"._("Click Here!")."</a>";
if ($echomsg == true ) {
out($error['brand_update_check_local_file']);
}
}
}
foreach ($row as $ava_brands) {
$key = $this->system->arraysearchrecursive($ava_brands['directory'], $out, 'directory');
if ($key === FALSE) {
$this->remove_brand($ava_brands['id']);
} else {
$key = $key[0];
$brand_name = $ava_brands['directory'];
//TODO: This seems old
if ($ava_brands['cfg_ver'] < $version[$brand_name]) {
$out[$key]['update'] = 1;
$out[$key]['update_vers'] = $version[$brand_name];
} else {
$out[$key]['update'] = NULL;
}
}
}
} else {
$error['brand_update_check_master_file'] = _("Error: Aborting Brand Downloads. Can't Get Master File, Assuming Timeout Issues!")."<br />"._("Learn how to manually upload packages here (it's easy!):")."<a href='http://wiki.provisioner.net/index.php/Endpoint_manager_manual_upload' target='_blank'>"._("Click Here!")."</a>";
if ($echomsg == true ) {
out($error['brand_update_check_local_file']);
}
}
return $out;
} else {
$error['remote_server'] = _("Error: The Remote Server Is Currently Syncing With the Master Server, Please try again later");
if ($echomsg == true ) {
out($error['remote_server']);
}
}
} else {
$o = getcwd();
chdir(dirname($this->PHONE_MODULES_PATH));
$path = $this->has_git();
exec($path . ' git pull', $output);
//exec($path . ' git checkout master', $output); //Why am I doing this?
chdir($o);
$temp = $this->file2json($this->PHONE_MODULES_PATH . 'endpoint/master.json');
$endpoint_package = $temp['data']['package'];
$endpoint_last_mod = $temp['data']['last_modified'];
$sql = "UPDATE endpointman_global_vars SET value = '" . $endpoint_last_mod . "' WHERE var_name = 'endpoint_vers'";
sql($sql);
$out = $temp['data']['brands'];
$row = sql('SELECT * FROM endpointman_brand_list WHERE id > 0', 'getAll', DB_FETCHMODE_ASSOC);
foreach ($out as $data) {
$temp = $this->file2json($this->PHONE_MODULES_PATH . 'endpoint/' . $data['directory'] . '/brand_data.json');
if (key_exists('directory', $temp['data']['brands'])) {
//Pull in all variables
$directory = $temp['data']['brands']['directory'];
$brand_name = $temp['data']['brands']['name'];
$brand_id = $temp['data']['brands']['brand_id'];
$brand_version = $temp['data']['brands']['last_modified'];
$b_data = sql("SELECT id FROM endpointman_brand_list WHERE id = '" . $brand_id . "'", 'getOne');
if ($b_data) {
$sql = "UPDATE endpointman_brand_list SET local = '1', name = '" . $brand_name . "', cfg_ver = '" . $brand_version . "', installed = 1, hidden = 0 WHERE id = " . $brand_id;
sql($sql);
} else {
$sql = "INSERT INTO endpointman_brand_list (id, name, directory, cfg_ver, local, installed) VALUES ('" . $brand_id . "', '" . $brand_name . "', '" . $directory . "', '" . $brand_version . "', '1', '1')";
sql($sql);
}
$last_mod = "";
foreach ($temp['data']['brands']['family_list'] as $family_list) {
$last_mod = max($last_mod, $family_list['last_modified']);
$family_line_xml = $this->file2json($this->PHONE_MODULES_PATH . '/endpoint/' . $directory . '/' . $family_list['directory'] . '/family_data.json');
$family_line_xml['data']['last_modified'] = isset($family_line_xml['data']['last_modified']) ? $family_line_xml['data']['last_modified'] : '';
/* DONT DO THIS YET
$require_firmware = NULL;
if ((key_exists('require_firmware', $family_line_xml['data'])) && ($remote) && ($family_line_xml['data']['require_firmware'] == "TRUE")) {
echo "Firmware Requirment Detected!..........<br/>";
$this->install_firmware($family_line_xml['data']['id']);
}
*
*/
$data = sql("SELECT id FROM endpointman_product_list WHERE id='" . $brand_id . $family_line_xml['data']['id'] . "'", 'getOne');
$short_name = preg_replace("/\[(.*?)\]/si", "", $family_line_xml['data']['name']);
if ($data) {
$sql = "UPDATE endpointman_product_list SET short_name = '" . $short_name . "', long_name = '" . $family_line_xml['data']['name'] . "', cfg_ver = '" . $family_line_xml['data']['version'] . "', config_files='" . $family_line_xml['data']['configuration_files'] . "' WHERE id = '" . $brand_id . $family_line_xml['data']['id'] . "'";
} else {
$sql = "INSERT INTO endpointman_product_list (`id`, `brand`, `short_name`, `long_name`, `cfg_dir`, `cfg_ver`, `config_files`, `hidden`) VALUES ('" . $brand_id . $family_line_xml['data']['id'] . "', '" . $brand_id . "', '" . $short_name . "', '" . $family_line_xml['data']['name'] . "', '" . $family_line_xml['data']['directory'] . "', '" . $family_line_xml['data']['last_modified'] . "','" . $family_line_xml['data']['configuration_files'] . "', '0')";
}
sql($sql);
foreach ($family_line_xml['data']['model_list'] as $model_list) {
$template_list = implode(",", $model_list['template_data']);
$m_data = sql("SELECT id FROM endpointman_model_list WHERE id='" . $brand_id . $family_line_xml['data']['id'] . $model_list['id'] . "'", 'getOne');
if ($m_data) {
$sql = "UPDATE endpointman_model_list SET max_lines = '" . $model_list['lines'] . "', model = '" . $model_list['model'] . "', template_list = '" . $template_list . "' WHERE id = '" . $brand_id . $family_line_xml['data']['id'] . $model_list['id'] . "'";
} else {
$sql = "INSERT INTO endpointman_model_list (`id`, `brand`, `model`, `max_lines`, `product_id`, `template_list`, `enabled`, `hidden`) VALUES ('" . $brand_id . $family_line_xml['data']['id'] . $model_list['id'] . "', '" . $brand_id . "', '" . $model_list['model'] . "', '" . $model_list['lines'] . "', '" . $brand_id . $family_line_xml['data']['id'] . "', '" . $template_list . "', '0', '0')";
}
sql($sql);
if (!$this->sync_model($brand_id . $family_line_xml['data']['id'] . $model_list['id'], $errsync_modal)) {
$error['sync_module_error'] = sprintf(_("Error: System Error in Sync Model [%s] Function, Load Failure!"), $model_list['model']);
if ($echomsg == true ) {
out($error['sync_module_error']);
foreach ($errsync_modal as $v) {
out($v);
}
}
}
unset($errsync_modal);
}
//Phone Models Move Here
$family_id = $brand_id . $family_line_xml['data']['id'];
$sql = "SELECT * FROM endpointman_model_list WHERE product_id = " . $family_id;
$products = sql($sql, 'getall', DB_FETCHMODE_ASSOC);
foreach ($products as $data) {
if (!$this->system->arraysearchrecursive($data['model'], $family_line_xml['data']['model_list'], 'model')) {
if ($echomsg == true ) {
outn(sprintf(_("Moving/Removing Model '%s' not present in JSON file......"), $data['model']));
}
$model_name = $data['model'];
$sql = 'DELETE FROM endpointman_model_list WHERE id = ' . $data['id'];
sql($sql);
$sql = "SELECT id FROM endpointman_model_list WHERE model LIKE '" . $model_name . "'";
$new_model_id = sql($sql, 'getOne');
if ($new_model_id) {
$sql = "UPDATE endpointman_mac_list SET model = '" . $new_model_id . "' WHERE model = '" . $data['id'] . "'";
} else {
$sql = "UPDATE endpointman_mac_list SET model = '0' WHERE model = '" . $data['id'] . "'";
}
sql($sql);
out (_("Done!"));
}
}
}
foreach ($temp['data']['brands']['oui_list'] as $oui) {
$sql = "REPLACE INTO endpointman_oui_list (`oui`, `brand`, `custom`) VALUES ('" . $oui . "', '" . $brand_id . "', '0')";
sql($sql);
}
}
}
}
}
/**
* Sync the XML files (incuding all template files) from the hard drive with the database
* @param int $model Model ID
* @return boolean True on sync completed. False on sync failed
*/
function sync_model($model, &$error = array()) {
if ((!empty($model)) OR ($model > 0)) {
$sql = "SELECT * FROM endpointman_model_list WHERE id='" . $model . "'";
$model_row = sql($sql, 'getrow', DB_FETCHMODE_ASSOC);
$sql = "SELECT * FROM endpointman_product_list WHERE id='" . $model_row['product_id'] . "'";
$product_row = sql($sql, 'getRow', DB_FETCHMODE_ASSOC);
$sql = "SELECT * FROM endpointman_brand_list WHERE id=" . $model_row['brand'];
$brand_row = sql($sql, 'getRow', DB_FETCHMODE_ASSOC);
$path_brand_dir = $this->PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'];
$path_brand_dir_cfg = $path_brand_dir . '/' . $product_row['cfg_dir'];
$path_brand_dir_cfg_json = $path_brand_dir_cfg . '/family_data.json';
if (!file_exists($path_brand_dir)) {
$error['sync_model'] = sprintf(_("Brand Directory '%s' Doesn't Exist! (%s)"), $brand_row['directory'], $path_brand_dir);
return(FALSE);
}
if (!file_exists($path_brand_dir_cfg)) {
$error['sync_model'] = sprintf(_("Product Directory '%s' Doesn't Exist! (%s)"), $product_row['cfg_dir'], $path_brand_dir_cfg);
return(FALSE);
}
if (!file_exists($path_brand_dir_cfg_json)) {
$error['sync_model'] = sprintf(_("File 'family_data.json' Doesn't exist in directory: %s"), $path_brand_dir_cfg);
return(FALSE);
}
$family_line_json = $this->file2json($path_brand_dir_cfg_json);
//TODO: Add local file checks to avoid slow reloading on PHP < 5.3
$key = $this->system->arraysearchrecursive($model_row['model'], $family_line_json['data']['model_list'], 'model');
if ($key === FALSE) {
$error['sync_model'] = "Can't locate model in family JSON file";
return(FALSE);
} else {
$template_list = implode(",", $family_line_json['data']['model_list'][$key[0]]['template_data']);
$template_list_array = $family_line_json['data']['model_list'][$key[0]]['template_data'];
}
$maxlines = $family_line_json['data']['model_list'][$key[0]]['lines'];
$sql = "UPDATE endpointman_model_list SET max_lines = '" . $maxlines . "', template_list = '" . $template_list . "' WHERE id = '" . $model . "'";
sql($sql);
$version = isset($family_line_json['data']['last_modified']) ? $family_line_json['data']['last_modified'] : '';
$long_name = $family_line_json['data']['name'];
$short_name = preg_replace("/\[(.*?)\]/si", "", $family_line_json['data']['name']);
$configuration_files = $family_line_json['data']['configuration_files'];
$sql = "UPDATE endpointman_product_list SET long_name = '" . str_replace("'", "''", $long_name) . "', short_name = '" . str_replace("'", "''", $short_name) . "' , cfg_ver = '" . $version . "' WHERE id = '" . $product_row['id'] . "'";
sql($sql);
$template_data_array = array();
$template_data_array = $this->merge_data($this->PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/', $template_list_array);
$sql = "UPDATE endpointman_model_list SET template_data = '" . serialize($template_data_array) . "' WHERE id = '" . $model . "'";
sql($sql);
return(TRUE);
} else {
return(FALSE);
}
}
/**
* This will download the xml & brand package remotely
* @param integer $id Brand ID
*/
function download_brand($id) {
out(_("Install/Update Brand..."));
if (!$this->configmod->get('use_repo')) {
$temp_directory = $this->system->sys_get_temp_dir() . "/epm_temp/";
if (!file_exists($temp_directory)) {
out(_("Creating EPM temp directory"));
if (! mkdir($temp_directory)) {
out(sprintf(_("Error: Failed to create the directory '%s', please Check Permissions!"), $temp_directory));
return false;
}
}
outn(_("Downloading Brand JSON..... "));
$row = sql('SELECT * FROM endpointman_brand_list WHERE id =' . $id, 'getAll', DB_FETCHMODE_ASSOC);
$result = $this->system->download_file($this->UPDATE_PATH . $row[0]['directory'] . "/" . $row[0]['directory'] . ".json", $this->PHONE_MODULES_PATH . "endpoint/" . $row[0]['directory'] . "/brand_data.json");
if ($result) {
out(_("Done!"));
$temp = $this->file2json($this->PHONE_MODULES_PATH . 'endpoint/' . $row[0]['directory'] . '/brand_data.json');
$package = $temp['data']['brands']['package'];
out(_("Downloading Brand Package..."));
if ($this->system->download_file_with_progress_bar($this->UPDATE_PATH . $row[0]['directory'] . '/' . $package, $temp_directory . $package))
{