forked from mescon/Muximux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
muximux.php
1235 lines (1151 loc) · 48.7 KB
/
muximux.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
/*
* DO NOT CHANGE THIS FILE!
*/
defined("CONFIG") ? null : define('CONFIG', 'settings.ini.php');
defined("CONFIGEXAMPLE") ? null : define('CONFIGEXAMPLE', 'settings.ini.php-example');
defined("SECRET") ? null : define('SECRET', 'secret.txt');
require dirname(__FILE__) . '/vendor/autoload.php';
// Check if this is an old installation that needs upgrading.
if (file_exists('config.ini.php')) {
copy('config.ini.php', 'backup.ini.php');
unlink('config.ini.php');
$upgrade = true;
write_log('Converting configuration file from previous Muximux installation.','D');
} else {
$upgrade = false;
}
// Check if this is our first run and do some things.
if(!file_exists(CONFIG)){
copy(CONFIGEXAMPLE, CONFIG);
checksetSHA();
}
// First what we're gonna do - save or read
if (sizeof($_POST) > 0) {
if(!isset($_POST['username'])){
write_ini();
}
}
// Check if we can open a file.
function openFile($file, $mode) {
if ((file_exists($file) && (!is_writable(dirname($file)) || !is_writable($file))) || !is_writable(dirname($file))) { // If file exists, check both file and directory writeable, else check that the directory is writeable.
$message = 'Either the file '. $file .' and/or it\'s parent directory is not writable by the PHP process. Check the permissions & ownership and try again.';
if (PHP_SHLIB_SUFFIX === "so") { //Check for POSIX systems.
$message .= " Current permission mode of ". $file. " is " .decoct(fileperms($file) & 0777);
$message .= " Current owner of " . $file . " is ". posix_getpwuid(fileowner($file))['name'];
$message .= " Refer to the README on instructions how to change permissions on the aforementioned files.";
} else if (PHP_SHLIB_SUFFIX === "dll") {
$message .= " Detected Windows system, refer to guides on how to set appropriate permissions."; //Can't get fileowner in a trivial manner.
}
write_log($message,'E');
setStatus($message);
exit;
}
return fopen($file, $mode);
}
// Create a secret for communication to the server
function createSecret() {
$text = uniqid("muximux-", true);
$file = openFile(SECRET, "w");
fwrite($file, $text);
fclose($file);
return $text;
}
// Save our settings on submit
function write_ini()
{
$config = new Config_Lite(CONFIG);
$oldHash = getPassHash();
$oldBranch = getBranch();
$terminate = false;
$authentication = $config->getBool('general','authentication',false);
// Double check that a username post didn't sneak through
foreach ($_POST as $parameter => $value) {
$splitParameter = explode('_-_', $parameter);
if ($splitParameter[1] == "username") {
die;
}
}
unlink(CONFIG);
$config = new Config_Lite(CONFIG);
foreach ($_POST as $parameter => $value) {
$splitParameter = explode('_-_', $parameter);
$value = (($value == "on") ? "true" : $value );
switch ($splitParameter[1]) {
case "password":
if ($value != $oldHash) {
write_log('Successfully updated password.','I');
$value = password_hash($value, PASSWORD_BCRYPT);
$terminate = true;
}
break;
case "authentication":
if ($value != $authentication) {
$terminate = true;
}
break;
case "theme":
$value = strtolower($value);
break;
case "branch":
if ($value != $oldBranch) {
$config->set('settings','branch_changed',true);
$config->set('settings','sha','00');
} else {
$config->set('settings','branch_changed',false);
}
break;
}
$config->set($splitParameter[0], $splitParameter[1], $value);
}
// save object to file
saveConfig($config);
if ($terminate) {
session_start();
session_destroy();
}
}
// set $_SERVER['HTTP_HOST'] when {self}
function selfURL_managment ($url) {
if (strpos($url,'{self}') !== FALSE) {
$url = str_replace('{self}',$_SERVER['HTTP_HOST'],$url);
}
return $url;
}
// Parse settings.php and create the Muximux elements
function parse_ini()
{
mapIcons('css/font-muximux.css','.muximux-');
$config = new Config_Lite(CONFIG);
checksetSHA();
fetchBranches(false);
$branchArray = getBranches();
$branchList = "";
$css = getThemeFile();
$tabColorEnabled = $config->getBool('general', 'tabcolor', false);
$enableDropDown = $config->getBool('general', 'enabledropdown', false);
$updatePopup = $config->getBool('general', 'updatepopup', false);
$mobileOverride = $config->getBool('general', 'mobileoverride', false);
$cssColor = ((parseCSS($css,'.colorgrab','color') != false) ? parseCSS($css,'.colorgrab','color') : '#FFFFFF');
$themeColor = $config->get('general','color',$cssColor);
$autoHide = $config->getBool('general', 'autohide', false);
$splashScreen = $config->getBool('general', 'splashscreen', false);
$userName = $config->get('general', 'userNameInput', 'admin');
$passHash = $config->get('general', 'password', 'Muximux');
$authentication = $config->getBool('general', 'authentication', false);
$rss = $config->getBool('general', 'rss', false);
$rssUrl = $config->get('general','rssUrl','https://www.wired.com/feed/');
$myBranch = getBranch();
foreach ($branchArray as $branchName => $shaSum ) {
$branchList .= "
<option value='".$branchName."' ".(($myBranch == $branchName) ? 'selected' : '' ).">". $branchName ."</option>";
}
$title = $config->get('general', 'title', 'Muximux - Application Management Console');
$pageOutput = "<form class='form-inline'>
<div class='applicationContainer row generalContainer' style='cursor:default;'>
<h2>General</h2>
<div class='appDiv form-group'>
<label for='titleInput' class='col-xs-6 col-sm-4 col-md-4 control-label left-label'>Main Title: </label>
<div class='col-xs-6 col-sm-8 col-md-8'>
<input id='titleInput' type='text' class='form-control form-control-sm' general_-_value' name='general_-_title' value='" . $title . "'>
</div>
</div>
<div class='appDiv form-group'>
<label for='branch' class='col-xs-6 col-sm-5 col-md-5 control-label left-label'>Git branch: </label>
<div class='col-xs-6 col-sm-2 col-md-2'>
<select id='branch' class='form-control form-control-sm custom-select' name='general_-_branch'>".
$branchList ."
</select>
</div>
</div>
<div class='appDiv form-group'>
<label for='theme' class='col-xs-6 col-sm-4 col-md-4 control-label left-label'>Theme: </label>
<div class='col-xs-6 col-sm-2 col-md-2'>
<select id='theme' class='form-control form-control-sm custom-select general_-_value' name='general_-_theme'>".
listThemes() ."
</select>
</div>
</div>
<div class='appDiv form-group'>
<label for='general_-_color' class='col-xs-6 col-sm-4 col-md-5 control-label left-label'>Color: </label>
<div class='col-xs-6 col-sm-7 col-md-7'>
<input type='text' id='general_-_default' class='appsColor generalColor general_-_color' value='".$themeColor."' name='general_-_color'>
</div>
</div>
<div class='hidden-xl-up'>
<br>
</div>
<div class='appDiv form-group'>
<label for='updatepopupCheckbox' class='col-xs-6 col-sm-12 control-label col-form-label form-check-inline'>Update alerts:
<input id='updatepopupCheckbox' class='form-check-input form-control general_-_value' name='general_-_updatepopup' type='checkbox' ".($updatePopup ? 'checked' : '') .">
</label>
</div>
<div class='appDiv form-group'>
<label for='splashscreenCheckbox' class='col-xs-6 col-sm-12 control-label col-form-label form-check-inline'>Splash screen:
<input id='splashscreenCheckbox' class='form-check-input form-control general_-_value' name='general_-_splashscreen' type='checkbox' ".($splashScreen ? 'checked' : '') .">
</label>
</div>
<div class='appDiv form-group'>
<label for='mobileoverrideCheckbox' class='col-xs-6 col-sm-12 control-label col-form-label form-check-inline'>Mobile override:
<input id='mobileoverrideCheckbox' class='form-check-input form-control general_-_value' name='general_-_mobileoverride' type='checkbox' ".($mobileOverride ? 'checked' : '').">
</label>
</div>
<div class='appDiv form-group'>
<label for='tabcolorCheckbox' class='col-xs-6 col-sm-12 control-label col-form-label form-check-inline'>App colors:
<input id='tabcolorCheckbox' class='form-check-input form-control general_-_value' name='general_-_tabcolor' type='checkbox' " . ($tabColorEnabled ? 'checked' : '').">
</label>
</div>
<div class='appDiv form-group'>
<label for='autohideCheckbox' class='col-xs-6 col-sm-12 control-label col-form-label form-check-inline'>Auto-hide bar:
<input id='autohideCheckbox' class='form-check-input form-control general_-_value' name='general_-_autohide' type='checkbox' ".($autoHide ? 'checked' : '').">
</label>
</div>
<div class='appDiv form-group'>
<label for='authenticationCheckbox' class='col-xs-6 col-sm-12 control-label col-form-label form-check-inline'>Authentication:
<input id='authenticationCheckbox' class='form-check-input form-control general_-_value' name='general_-_authentication' type='checkbox' ".($authentication ? 'checked' : '').">
</label>
</div>
<div class='appDiv form-group rssGroup'>
<label for='rssCheckbox' class='col-xs-12 col-sm-12 control-label col-form-label form-check-inline'>Splash RSS:
<input id='rssCheckbox' class='form-check-input form-control general_-_value' name='general_-_rss' type='checkbox' ".($rss ? 'checked' : '').">
</label>
</div>
<div class='userinput appDiv form-group rssUrlGroup'>
<label for='rssUrlInput' class='col-xs-4 control-label right-label'>Feed Url: </label>
<div class='col-xs-7 col-sm-5 col-md-8'>
<input id='rssUrlInput' type='text' class='form-control' general_-_value' name='general_-_rssUrl' value='" . $rssUrl . "'>
</div>
</div>
<div class='inputdiv appDiv form-group'>
<div class='userinput appDiv form-group'>
<label for='userName' class='col-xs-4 control-label right-label'>Username: </label>
<div class='col-xs-7 col-sm-5 col-md-8'>
<input id='userNameInput' type='text' class='form-control' general_-_value' name='general_-_userNameInput' value='" . $userName . "'>
</div>
</div>
<div class='userinput appDiv form-group'>
<label for='password' class='col-xs-4 control-label right-label'>Password: </label>
<div class='col-xs-7 col-sm-5 col-md-8'>
<input id='passwordInput' type='password' autocomplete='new-password' class='form-control' general_-_value' name='general_-_password' value='" . $passHash . "'>
</div>
</div>
</div>
</div>
<input type='hidden' class='settings_-_value' name='settings_-_sha' value='".$mySha."'>
<input type='hidden' class='settings_-_value' name='settings_-_enabled' value='true'>
<input type='hidden' class='settings_-_value' name='settings_-_default' value='false'>
<input type='hidden' class='settings_-_value' name='settings_-_name' value='Settings'>
<input type='hidden' class='settings_-_value' name='settings_-_url' value='muximux.php'>
<input type='hidden' class='settings_-_value' name='settings_-_landingpage' value='false'>
<input type='hidden' class='settings_-_value' name='settings_-_icon' value='fa-cog'>
<input type='hidden' class='settings_-_value' name='settings_-_dd' value='true'>
<div id='sortable'>";
foreach ($config as $section => $name) {
if (is_array($name) && $section != "settings" && $section != "general") {
$name = $config->get($section, 'name', '');
$url = $config->get($section, 'url', 'http://www.plex.com');
$color = $config->get($section, 'color', '#000');
$icon = $config->get($section, 'icon', 'muximux-play');
$icon = str_replace('fa-','muximux-',$icon);
$scale = $config->get($section, 'scale', '1');
$default = $config->getBool($section, 'default', false);
$enabled = $config->getBool($section, 'enabled', true);
$landingpage = $config->getBool($section, 'landingpage', false);
$dd = $config->getBool($section, 'dd', false);
$scaleRange = "0";
$scaleRange = buildScale($scale);
$pageOutput .= "
<div class='applicationContainer' id='" . $section . "'>
<span class='bars fa fa-bars'></span>
<div class='appDiv form-group'>
<label for='" . $section . "_-_name' class='col-xs-4 col-md-4 control-label right-label'>Name: </label>
<div class='col-xs-7 col-md-8'>
<input class='form-control form-control-sm appName " . $section . "_-_value' was='" . $section . "' name='" . $section . "_-_name' type='text' value='" . $name . "'>
</div>
</div>
<div class='appDiv form-group'>
<label for='" . $section . "_-_url' class='col-xs-4 control-label right-label'>URL: </label>
<div class='col-xs-7 col-md-8'>
<input class='form-control form-control-sm " . $section . "_-_value' name='" . $section . "_-_url' type='text' value='" . $url . "'>
</div>
</div>
<div class='appDiv form-group'>
<label for='" . $section . "_-_scale' class='col-xs-4 col-md-5 control-label col-form-label right-label'>Zoom: </label>
<div class='col-xs-7 col-md-5'>
<select id='" . $section . "_-_scale' class='form-control custom-select form-control-sm ' name='" . $section . "_-_scale'>". $scaleRange ."</select>
</div>
</div>
<div class='appDiv form-group'>
<label for='" . $section . "_-_icon' class='col-xs-4 control-label right-label'>Icon: </label>
<div class='col-xs-7 col-md-5'>
<button role='iconpicker' class='form-control form-control-sm iconpicker btn btn-default' name='" . $section . "_-_icon' data-rows='4' data-cols='6' data-search='true' data-search-text='Search...' data-iconset='muximux' data-placement='left' data-icon='" . $icon . "'></button>
</div>
</div>
<div class='appDiv form-group colorDiv'>
<label for='" . $section . "_-_color' class='col-xs-4 col-md-5 control-label color-label right-label'>Color: </label>
<div class='col-xs-7'>
<input type='text' id='" . $section . "_-_color' class='form-control form-control-sm appsColor " . $section . "_-_color' value='" . $color . "' name='" . $section . "_-_color'>
</div>
</div>
<div class='hidden-xl-up'>
<br>
</div>
<div class='appDiv form-group'>
<label for='" . $section . "_-_enabled' class='col-xs-6 col-md-12 control-label col-form-label form-check-inline'>Enabled:
<input type='checkbox' class='form-check-input form-control " . $section . "_-_value' id='" . $section . "_-_enabled' name='" . $section . "_-_enabled'".($enabled ? 'checked' : '') .">
</label>
</div>
<div class='appDiv form-group'>
<label for='" . $section . "_-_landingpage' class='col-xs-6 col-md-12 control-label col-form-label form-check-inline'>Landing:
<input type='checkbox' class='form-check-input form-control " . $section . "_-_value' id='" . $section . "_-_landingpage' name='" . $section . "_-_landingpage'".($landingpage ? 'checked' : '') .">
</label>
</div>
<div class='appDiv form-group'>
<label for='" . $section . "_-_dd' class='col-xs-6 col-md-12 control-label col-form-label form-check-inline'>Dropdown:
<input type='checkbox' class='form-check-input form-control " . $section . "_-_value' id='" . $section . "_-_dd' name='" . $section . "_-_dd'".($dd ? 'checked' : '') .">
</label>
</div>
<div class='appDiv form-group'>
<label for='" . $section . "_-_default' class='col-xs-6 col-md-12 control-label col-form-label form-check-inline'>Default:
<input type='radio' class='form-check-input form-control " . $section . "_-_value' id='" . $section . "_-_default' name='" . $section . "_-_default'".($default ? 'checked' : '') .">
</label>
</div>
<button type='button' class='form-control form-control-sm removeButton btn btn-danger btn-xs' value='Remove' id='remove-" . $section . "'>Remove</button>
</div>";
}
}
$pageOutput .= "
</div>
<div class='text-center' style='margin-top: 15px;'>
<div class='btn-group' role='group' aria-label='Buttons'>
<a class='btn btn-primary btn-md' id='addApplication'><span class='fa fa-plus'></span> Add new</a>
<a class='btn btn-danger btn-md' id='removeAll'><span class='fa fa-trash'></span> Remove all</a>
</div>
</div>
</form>";
return $pageOutput;
}
// Generate our splash screen contents (basically a very little version of parse_ini).
function splashScreen() {
$config = new Config_Lite(CONFIG);
$css = getThemeFile();
$cssColor = ((parseCSS($css,'.colorgrab','color') != false) ? parseCSS($css,'.colorgrab','color') : '#FFFFFF');
$themeColor = $config->get('general','color',$cssColor);
$tabColor = $config->getBool('general','tabcolor',false);
$splash = "";
foreach ($config as $keyname => $section) {
$enabled = $config->getBool($keyname,'enabled',false);
if (($keyname != "general") && ($keyname != "settings") && $enabled) {
$color = ($tabColor===true ? $section["color"] : $themeColor);
$icon = $config->get($keyname,'icon','fa-play');
$icon = str_replace('fa-','muximux-',$icon);
$splash .= "
<div class='btnWrap'>
<div class='well splashBtn' data-content='" . $keyname . "'>
<a class='panel-heading' data-title='" . $section["name"] . "'>
<br><i class='fa fa-5x " . $icon . "' style='color:".$color."'></i><br>
<p class='splashBtnTitle' style='color:#ddd'>".$section["name"]."</p>
</a>
</div>
</div>";
}
}
return $splash;
}
// Generate the contents of the log
function log_contents() {
$out = '<ul>
<div id="logContainer">
';
$filename = 'muximux.log';
$file = file($filename);
$file = array_reverse($file);
$lineOut = "";
$concat = false;
foreach($file as $line){
$lvl = substr($line,0,2);
if (substr($lvl,1,1) == "/") {
switch ($lvl) {
case "E/":
$color = 'alert alert-danger';
break;
case "D/":
$color = 'alert alert-warning';
break;
case "I/":
$color = 'alert alert-success';
break;
case "":
$color = 'alert alert-info';
break;
}
if ($concat === true) {
$out .='
<li class="logLine alert alert-info">'.
$lineOut.'
</li>';
}
$lineOut = substr($line,2);
$concat = false;
} else {
$lineOut .= $line;
$concat = true;
}
if ($concat === false) {
$out .='
<li class="logLine '.$color.'">'.
$lineOut.'
</li>';
}
}
$out .= '</div>
</ul>
';
return $out;
}
// Check if the user changes tracking branch, which will change the SHA and trigger an update notification
function checkBranchChanged() {
$config = new Config_Lite(CONFIG);
if ($config->getBool('settings', 'branch_changed', false)) {
saveConfig($config);
checksetSHA();
return true;
} else {
return false;
}
}
// Build a custom scale using our set value, show it as selected
function buildScale($selectValue)
{
$f=10;
$scaleRange = "";
while($f<251) {
$pra = $f / 100;
$scaleRange .= "
<option value='" . $pra ."' ".(($pra == $selectValue ? 'selected' : '')).">". $f ."%</option>";
$f++;
}
return $scaleRange;
}
// Quickie to get the theme from settings
function getTheme()
{
$config = new Config_Lite(CONFIG);
$item = $config->get('general', 'theme', 'classic');
return strtolower($item);
}
function getThemeFile() {
$config = new Config_Lite(CONFIG);
$item = $config->get('general', 'theme', 'classic');
if (file_exists('css/theme/'.$item.'.css')) {
return 'css/theme/'.$item.'.css';
die;
} else {
$item=strtolower($item);
}
if (file_exists('css/theme/'.$item.'.css')) {
return 'css/theme/'.$item.'.css';
die;
} else {
$item=ucfirst($item);
}
if (file_exists('css/theme/'.$item.'.css')) {
return 'css/theme/'.$item.'.css';
die;
} else {
$item='theme_default.css';
return 'css/'.$item.'.css';
die;
}
}
// List all available themes in directory
function listThemes() {
$dir = './css/theme';
$themelist ="";
$themes = scandir($dir);
$themeCurrent = getTheme();
foreach($themes as $value){
$splitName = explode('.', $value);
if (!empty($splitName[0])) {
$name = ucfirst($splitName[0]);
$themelist .="
<option value='".$name."' ".(($name == ucfirst(getTheme())) ? 'selected' : '').">".$name."</option>";
}
}
return $themelist;
}
// Build the contents of our menu
function menuItems() {
$config = new Config_Lite(CONFIG);
$standardmenu = "<ul class='cd-tabs-navigation'>
<nav>";
$dropdownmenu = "
<li>
<a data-toggle='modal' data-target='#settingsModal' data-title='Settings'>
<span class='fa fa-cog'></span>Settings
</a>
</li>
<li>
<a id='logModalBtn' data-toggle='modal' data-target='#logModal' data-title='Log Viewer'>
<span class='fa fa-file-text-o'></span> Log
</a>
</li>";
$int = 0;
$autohide = $config->getBool('general', 'autohide', false);
$dropdown = $config->getBool('general', 'enabledropdown', true);
$mobileoverride = $config->getBool('general', 'mobileoverride', false);
$authentication = $config->getBool('general', 'authentication', false);
foreach ($config as $keyname => $section) {
if (($keyname != "general") && ($keyname != "settings")) {
$name = $config->get($keyname, 'name', '');
$url = $config->get($keyname, 'url', 'http://www.plex.com');
$color = $config->get($keyname, 'color', '#000');
$icon = $config->get($keyname, 'icon', 'fa-play');
$icon = str_replace('fa-','muximux-',$icon);
$scale = $config->get($keyname, 'scale', '1');
$default = $config->getBool($keyname, 'default', false);
$enabled = $config->getBool($keyname, 'enabled', false);
$landingpage = $config->getBool($keyname, 'landingpage', false);
$dd = $config->getBool($keyname, 'dd', false);
if ($enabled) {
if ($dropdown) {
if (!$dd) {
$standardmenu .= "
<li class='cd-tab' data-index='".$int."'>
<a data-content='" . $keyname . "' data-title='" . $section["name"] . "' data-color='" . $section["color"] . "' class='".($default ? 'selected' : '')."'>
<span class='fa " . $icon . " fa-lg'></span> " . $section["name"] . "
</a>
</li>";
$int++;
} else {
$dropdownmenu .= "
<li>
<a data-content='" . $keyname . "' data-title='" . $section["name"] . "'>
<span class='fa " . $icon . "'></span> " . $section["name"] . "
</a>
</li>";
}
}
}
}
}
$standardmenu .= "</nav>
</ul>";
$splashScreen = $config->getBool('general', 'splashscreen', false);
$moButton = "
<ul class='main-nav'>
<li class='navbtn ".(($mobileoverride == "true") ? '' : 'hidden')."'>
<a id='override' title='Click this button to disable mobile scaling on tablets or other large-resolution devices.'>
<span class='fa muximux-mobile muximux-navbtn-icon fa-lg'></span>
</a>
</li>
<li class='navbtn ".(($splashScreen == "true") ? '' : 'hidden')."'>
<a id='showSplash' data-toggle='modal' data-target='#splashModal' data-title='Show Splash'>
<span class='fa muximux-home4 muximux-navbtn-icon fa-lg'></span>
</a>
</li>
<li class='navbtn ".(($authentication == "true") ? '' : 'hidden')."'>
<a id='logout' title='Click this button to log out of Muximux.'>
<span class='fa muximux-sign-out muximux-navbtn-icon fa-lg'></span>
</a>
</li>
<li class='navbtn'>
<a id='reload' title='Double click your app in the menu, or press this button to refresh the current app.'>
<span class='fa muximux-refresh muximux-navbtn-icon fa-lg'></span>
</a>
</li>
";
$drawerdiv .= "<div class='cd-tabs-bar ".(($autohide == "true")? 'drawer' : '')."'>";
if ($dropdown == "true") {
$item =
$drawerdiv .
$moButton ."
<li class='dd navbtn'>
<a id='hamburger'>
<span class='fa fa-bars muximux-navbtn-icon fa-lg'></span>
</a>
<ul class='drop-nav'>" .
$dropdownmenu ."
</ul>
</li>
</ul>".
$standardmenu ."
</div>
";
} else {
$item =
$drawerdiv .
$moButton .
$standardmenu;
}
return $item;
}
// Quickie fetch the main title
function getTitle() {
$config = new Config_Lite(CONFIG);
$item = $config->get('general', 'title', 'Muximux - Application Management Console');
return $item;
}
// Quickie fetch of the current selected branch
function getBranch() {
$config = new Config_Lite(CONFIG);
$branch = $config->get('general', 'branch', 'master');
return $branch;
}
// Reads for "branches" from settings. If not found, fetches list from github, saves, parses, and returns
function getBranches() {
$config = new Config_Lite(CONFIG);
$branches = [];
$branches = $config->get('settings', 'branches',$branches);
if ($branches == []) {
fetchBranches(true);
} else {
$branches = $config->get('settings', 'branches');
}
return $branches;
}
// Fetch a list of branches from github, along with their current SHA
function fetchBranches($skip) {
$config = new Config_Lite(CONFIG);
$last = $config->get('settings', 'last_check', "0");
if ((time() >= $last + 3600) || $skip) { // Check to make sure we haven't checked in an hour or so, to avoid making GitHub mad
if (time() >= $last + 3600) {
write_log('Refreshing branches from github - automatically triggered.');
} else {
write_log('Refreshing branches from github - manually triggered.');
}
$url = 'https://api.github.com/repos/mescon/Muximux/branches';
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad
)
);
$context = stream_context_create($options);
$json = file_get_contents($url,false,$context);
if ($json == false) {
write_log('Error fetching JSON from Github.','E');
$result = false;
} else {
$array = json_decode($json,true);
$i = 0;
$names = array();
$shas = array();
foreach ($array as $value) {
foreach ($value as $key => $value2) {
if ($key == "name") {
array_push($names,$value2);
} else {
foreach ($value2 as $key2 => $value3) {
if ($key2 == "sha" ) {
$shaVal = $value3;
array_push($shas,$value3);
}
}
}
}
}
$outP = array_combine($names,$shas);
$config ->set('settings','branches',$outP);
$config ->set('settings','last_check',time());
saveConfig($config);
$result = true;
}
} else {
$result = false;
}
return $result;
}
// Echos php information to the java console
function console_log( $data ) {
$output = "<script>console.log( 'PHP debugger: ";
$output .= json_encode(print_r($data, true));
$output .= "' );</script>";
echo $output;
}
// This checks whether we have a SHA, and if not, whether we are using git or zip updates and stores
// the data accordingly
function checksetSHA() {
$config = new Config_Lite(CONFIG);
$shaIn = $config->get('settings','sha','0');
$branchIn = getBranch();
$git = can_git();
if ($git !== false) {
$shaOut = exec('git rev-parse HEAD');
$branchOut = exec('git rev-parse --abbrev-ref HEAD');
} else {
if (shaIn == '0') {
$branchArray = getBranches();
$branchOut = $branchIn();
foreach ($branchArray as $branchName => $shaVal) {
if ($branchName==$branchOut) {
$shaOut = $shaVal;
}
}
}
}
$changed = false;
if ($branchIn != $branchOut) {
$config->set('settings', 'branch', $branchOut);
$changed = true;
}
if ($shaIn != $shaOut) {
$config->set('settings', 'sha', $shaOut);
$changed = true;
}
if ($changed) {
saveConfig($config);
}
}
// Read SHA from settings and return it's value.
function getSHA() {
$config = new Config_Lite(CONFIG);
$item = $config->get('settings', 'sha', '00');
return $item;
}
// Retrieve password hash from settings and return it for "stuff".
function getPassHash() {
$config = new Config_Lite(CONFIG);
$item = $config->get('general', 'password', 'foo');
return $item;
}
// This little gem helps us replace a whome bunch of AJAX calls by sorting out the info and
// writing it to meta-tags at the bottom of the page. Might want to look at calling this via one AJAX call.
function metaTags() {
$config = new Config_Lite(CONFIG);
$standardmenu = "";
$dropdownmenu = "";
$authentication = ($config->getBool('general', 'authentication', false) ? 'true' : 'false');
$autohide = ($config->getBool('general', 'autohide', false) ? 'true' : 'false');
$branch = $config->get('general', 'branch', 'master');
$branchUrl = "https://api.github.com/repos/mescon/Muximux/commits?sha=" . $branch;
$popupdate = ($config->getBool('general', 'updatepopup', false) ? 'true' : 'false');
$enabledropdown = ($config->getBool('settings', 'enabledropdown', true) ? 'true' : 'false');
$maintitle = $config->get('general', 'title', 'Muximux');
$tabcolor = ($config->getBool('general', 'tabcolor', false) ? 'true' : 'false');
$splashScreen = ($config->getBool('general', 'splashscreen', false) ? 'true' : 'false');
$css = getThemeFile();
$cssColor = ((parseCSS($css,'.colorgrab','color') != false) ? parseCSS($css,'.colorgrab','color') : '#FFFFFF');
$themeColor = $config->get('general','color',$cssColor);
$rss = ($config->getBool('general', 'rss', false) ? 'true' : 'false');
$rssUrl = $config->get('general','rssUrl','https://trace.corrupt-net.org/live.php');
$inipath = php_ini_loaded_file();
if ($inipath) {
$inipath;
} else {
$inipath = "php.ini";
}
$created = filectime(CONFIG);
$branchChanged = (checkBranchChanged() ? 'true' : 'false');
$secret = file_get_contents(SECRET);
$tags = "
<meta id='dropdown-data' data='".$enabledropdown."'>
<meta id='branch-data' data='". $branch . "'>
<meta id='branch-changed' data='". $branchChanged . "'>
<meta id='popupdate' data='". $popupdate . "'>
<meta id='drawer' data='". $autohide . "'>
<meta id='tabcolor' data='". $tabcolor . "'>
<meta id='maintitle' data='". $maintitle . "'>
<meta id='gitdirectory-data' data='". $gitdir . "'>
<meta id='cwd-data' data='". getcwd() . "'>
<meta id='phpini-data' data='". $inipath . "'>
<meta id='title-data' data='". $maintitle . "'>
<meta id='created-data' data='". $created . "'>
<meta id='sha-data' data='". getSHA() . "'>
<meta id='secret' data='". $secret . "'>
<meta id='themeColor-data' data='". $themeColor . "'>
<meta id='splashScreen-data' data='". $splashScreen . "'>
<meta id='authentication-data' data='". $authentication . "'>
<meta id='rss-data' data='". $rss . "'>
<meta id='rssUrl-data' data='". $rssUrl . "'>
";
return $tags;
}
// Set up the actual iFrame contents, as the name implies.
function frameContent() {
$config = new Config_Lite(CONFIG);
if (empty($item)) $item = '';
foreach ($config as $keyname => $section) {
$landingpage = $config->getBool($keyname,'landingpage',false);
$enabled = $config->getBool($keyname,'enabled',true);
$default = $config->getBool($keyname,'default',false);
$scale = $config->get($keyname,'scale',1);
$url = $section["url"];
$url = selfURL_managment($url);
$url=($landingpage ? "?landing=" . $keyname: $url);
if ($enabled && ($keyname != 'settings') && ($keyname != 'general')) {
$item .= "
<li data-content='" . $keyname . "' data-scale='" . $section["scale"] ."' ".($default ? "class='selected'" : '').">
<iframe sandbox='allow-forms allow-same-origin allow-pointer-lock allow-scripts allow-downloads allow-popups allow-modals allow-top-navigation'
allowfullscreen='true' webkitallowfullscreen='true' mozallowfullscreen='true' scrolling='auto' data-title='" . $section["name"] . "' ".($default ? 'src' : 'data-src')."='" . $url . "'></iframe>
</li>";
}
}
return $item;
}
// Build a landing page.
function landingPage($keyname) {
$config = new Config_Lite(CONFIG);
$item = "
<html lang='en'>
<head>
<title>" . $config->get($keyname, 'name') . "</title>
<link rel='stylesheet' href='css/landing.css'>
</head>
<body>
<div class='login'>
<div class='heading'>
<h2><span class='fa " . $config->get($keyname, 'icon') . " fa-3x'></span></h2>
<section>
<a href='" . $config->get($keyname, 'url') . "' target='_self' title='Launch " . $config->get($keyname, 'name') . "!'><button class='float'>Launch " . $config->get($keyname, 'name') . "</button></a>
</section>
</div>
</div>
</body></html>";
if (empty($item)) $item = '';
return $item;
}
// This method checks whether we can execute, if the directory is a git, and if git is installed
function can_git()
{
if ((exec_enabled() == true) && (file_exists('.git'))) {
$whereIsCommand = (PHP_OS == 'WINNT') ? 'where git' : 'which git'; // Establish the command for our OS
$gitPath = shell_exec($whereIsCommand); // Find where git is
$git = (empty($gitPath) ? false : true); // Make sure we have a path
if ($git) { // Double-check git is here and executable
exec($gitPath . ' --version', $output);
preg_match('#^(git version)#', current($output), $matches);
$git = (empty($matches[0]) ? $gitPath : false); // If so, return path. If not, return false.
}
} else {
$git = false;
}
return $git;
}
// Can we execute commands?
function exec_enabled() {
$disabled = explode(', ', ini_get('disable_functions'));
return !in_array('exec', $disabled);
}
// URL parameters
if (isset($_GET['landing'])) {
$keyname = $_GET['landing'];
echo landingPage($keyname);
die();
}
// This is where the JavaScript reads the contents of the secret file. This gets re-generated on each page load.
if (isset($_GET['get']) && $_GET['get'] == 'secret') {
$secret = file_get_contents(SECRET) or die("Unable to open " . SECRET);
echo $secret;
die();
}
// Things wrapped inside this are protected by a secret hash.
if(isset($_GET['secret']) && $_GET['secret'] == file_get_contents(SECRET)) {
// This lets us create a new secret when we leave the page.
if (isset($_GET['set']) && $_GET['set'] == 'secret') {
createSecret();
die();
}
if (isset($_GET['get']) && $_GET['get'] == 'hash') {
if (exec_enabled() == true) {
$git = can_git();
if ($git !== false) {
$hash = 'unknown';
} else {
$hash = exec($git . ' log --pretty="%H" -n1 HEAD');
}
} else {
$hash = 'noexec';
}
echo $hash;
die();
}
if(isset($_GET['remove']) && $_GET['remove'] == "backup") {
unlink('backup.ini.php');
echo "deleted";
die();
}
if(isset($_GET['action']) && $_GET['action'] == "update") {
$sha = $_GET['sha'];
$results = downloadUpdate($sha);
if ($results === true) {
echo $results;
die();
} else {
$data = array('type' => 'error', 'message' => $results);
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($data);
die();
}
}
if(isset($_GET['action']) && $_GET['action'] == "branches") {
$results = fetchBranches(true);
echo $results;
die();
}
if(isset($_GET['action']) && $_GET['action'] == "log") {
echo log_contents();
die();
}
if(isset($_GET['action']) && $_GET['action'] == "writeLog") {
$msg = $_GET['msg'];
if(isset($_GET['lvl'])) {
$lvl = $_GET['lvl'];
write_log($msg,$lvl);
} else {
write_log($msg);
}
die();
}
}
// End protected get-calls
if(empty($_GET)) {
createSecret();
}
// This downloads updates from git if available and able, otherwise, from zip.
function downloadUpdate($sha) {
$git = can_git();
if ($git !== false) {
$branch = getBranch();
if ($sha == $branch) {
$resultshort = exec('git status');
$result = (preg_match('/clean/',$resultshort));
if ($result !== true) {
$resultmsg = shell_exec('git status');
$result ='Install Failed! Local instance has files that will interfer with branch change - please manually stash changes and try again. Result message: "' . $resultshort.'"';
write_log($result ,'E');
$result ='Install Failed! Local instance has files that will interfer with branch changed - please manually stash changes and try again. See log for details.';
return $result;
}
$result = exec('git checkout '. $branch);
write_log('Changing git branch, command result is ' . $result,'D');
$result = (preg_match('/up-to-date/',$result));
if ($result) {
$mySha = exec('git rev-parse HEAD');
$config = new Config_Lite(CONFIG);
if (!preg_match('/about a specific subcommand/',$mySha)) { // Something went wrong with the command to get our SHA, fall back to using the passed value.
$config->set('settings','sha',$mySha);
$config->set("settings","branch_changed",false);