-
Notifications
You must be signed in to change notification settings - Fork 4
/
vdradmind.pl
executable file
·7464 lines (6640 loc) · 282 KB
/
vdradmind.pl
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
#!/usr/bin/perl
# vim:et:sw=4:ts=4:
#
# VDRAdmin-AM 2005 - 2014 by Andreas Mair <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Or, point your browser to http://www.gnu.org/copyleft/gpl.html
#
# 08.10.2001
#
#
require 5.004;
use vars qw($PROCNAME);
use constant {
EV_CHANNEL_NAME => 0,
EV_START => 1,
EV_STOP => 2,
EV_TITLE => 3,
EV_SUBTITLE => 4,
EV_SUMMARY => 5,
EV_VPS => 6,
EV_ID => 7,
EV_VDR_ID => 8,
EV_EVENT_ID => 9,
EV_STREAM_INFO => 10,
};
my $VERSION = "3.6.13";
my $BASENAME;
my $EXENAME;
BEGIN {
$PROCNAME = "vdradmind";
$0 =~ /(^.*\/)/;
$EXENAME = $0;
$BASENAME = $1;
$0 = $PROCNAME;
unshift(@INC, "/usr/share/vdradmin/lib");
unshift(@INC, $BASENAME . "lib/");
}
my $localemod;
if (eval { require Locale::gettext }) {
$localemod = 'Locale::gettext';
} elsif (eval { require Locale::Messages }) {
$localemod = 'Locale::Messages';
} else {
die("Locale::gettext or Locale::Messages is required: $@");
}
my $can_use_bind_textdomain_codeset = 1;
eval {
$localemod->import(qw(gettext bindtextdomain textdomain bind_textdomain_codeset));
};
if ($@) {
$localemod->import(qw(gettext bindtextdomain textdomain));
print("Not using bind_textdomain_codeset(). Please update your Locale::gettext perl module!\n");
$can_use_bind_textdomain_codeset = undef;
}
require File::Temp;
use locale;
use CGI qw(:no_debug);
use HTTP::Date qw(time2str);
use IO::Socket;
use Template;
use Time::Local qw(timelocal);
use POSIX qw(:sys_wait_h strftime mktime locale_h);
use MIME::Base64 ();
use File::Temp ();
use File::Find ();
use URI ();
use URI::Escape qw(uri_escape uri_unescape);
use HTTP::Tiny;
use IO::Select;
my $can_use_encode = 1;
$can_use_encode = undef unless (eval { require Encode });
my $InetSocketModule = 'IO::Socket::INET';
my $VdrSocketModule = 'IO::Socket::INET';
my $can_use_net_smtp = 1;
$can_use_net_smtp = undef unless (eval { require Net::SMTP });
my $can_use_smtpauth = 1;
$can_use_smtpauth = undef unless (eval { require Authen::SASL });
#Authen::SASL->import(qw(Perl)) if($can_use_smtpauth);
# Some users have problems if the LANGUAGE env variable is set
# so it's cleared here.
$ENV{LANGUAGE} = "";
$SIG{CHLD} = sub { wait };
use strict;
#use warnings;
my $SEARCH_FILES_IN_SYSTEM = 0;
my $VDR_MAX_SVDRP_LENGTH = 10000; # validate this value
my $SUPPORTED_LOCALE_PREFIXES = "^(cs|de|en|es|fi|fr|hu|it|nl|ru)_";
my $TOOL_AUTOTIMER = 0;
my $TOOL_EPGSEARCH = 1;
my $AT_BY_EVENT_ID = 2;
my $AT_BY_TIME = 1;
my $AT_OFF = 0;
my $CHAN_FULL = 0;
my $CHAN_WANTED = 1;
my $CHAN_TV = 2;
my $CHAN_RADIO = 3;
# Channel groups start with this number (to be sorted like strings)
my $CHAN_GROUPS = 40;
sub true () { 1 }
sub false () { 0 }
sub CRLF () { "\r\n" }
# [ internal log level, syslog priority ]
sub LOG_FATALERROR () { [ 2, "crit" ] }
sub LOG_ERROR () { [ 3, "err" ] }
sub LOG_WARNING () { [ 4, "warning" ] }
sub LOG_NOTICE () { [ 5, "notice" ] }
sub LOG_INFO () { [ 6, "info" ] }
sub LOG_DEBUG () { [ 7, "debug" ] }
my (%CONFIG, %CONFIG_TEMP);
$CONFIG{LOGLEVEL} = 5; #LOG_NOTICE
$CONFIG{LOGGING} = 0;
$CONFIG{LOGFILE} = "syslog";
$CONFIG{MOD_GZIP} = 0;
$CONFIG{CACHE_BG_UPDATE} = 1;
$CONFIG{CACHE_TIMEOUT} = 60;
$CONFIG{CACHE_LASTUPDATE} = 0;
$CONFIG{CACHE_REC_TIMEOUT} = 60;
$CONFIG{CACHE_REC_LASTUPDATE} = 0;
$CONFIG{CACHE_REC_ENABLED} = 0;
$CONFIG{AUTO_SAVE_CONFIG} = 1;
$CONFIG{HTTP_KEEPALIVE_TIMEOUT} = 10;
#
$CONFIG{VDR_HOST} = "localhost";
$CONFIG{VDR_PORT} = 2001; # will be set to 6419 in initial --config if locally installed VDR is >= 1.7.15
$CONFIG{SERVERHOST} = "0.0.0.0";
$CONFIG{SERVERPORT} = 8001;
$CONFIG{LOCAL_NET} = "0.0.0.0/32";
$CONFIG{VIDEODIR} = "/video";
$CONFIG{VDRCONFDIR} = "$CONFIG{VIDEODIR}";
$CONFIG{EPGIMAGES} = "$CONFIG{VIDEODIR}/epgimages";
#
$CONFIG{TEMPLATE} = "default";
$CONFIG{SKIN} = "default";
$CONFIG{LOGINPAGE} = 0;
{ my @a = </dev/dvb/adapter[0-9]>; $CONFIG{RECORDINGS} = scalar(@a) || 1; }
$CONFIG{LANG} = "";
#
$CONFIG{USERNAME} = "linvdr";
$CONFIG{PASSWORD} = "linvdr";
$CONFIG{GUEST_ACCOUNT} = 0;
$CONFIG{USERNAME_GUEST} = "guest";
$CONFIG{PASSWORD_GUEST} = "guest";
$CONFIG{LOCAL_NET_ONLY} = 0;
#
$CONFIG{ZEITRAHMEN} = 1;
$CONFIG{TIMES} = "18:00, 20:00, 21:00, 22:00";
$CONFIG{TL_TOOLTIP} = 1;
#
$CONFIG{EPG_SUMMARY} = 0;
$CONFIG{EPG_SUBTITLE} = 1;
$CONFIG{EPG_START_TIME} = "00:00";
#
$CONFIG{AT_OFFER} = 0;
$CONFIG{AT_FUNC} = 1;
$CONFIG{AT_LIFETIME} = 99;
$CONFIG{AT_PRIORITY} = 99;
$CONFIG{AT_MARGIN_BEGIN} = 10;
$CONFIG{AT_MARGIN_END} = 10;
$CONFIG{AT_TOOLTIP} = 1;
$CONFIG{AT_SORTBY} = "pattern";
$CONFIG{AT_DESC} = 0;
#
$CONFIG{ES_SORTBY} = "pattern";
$CONFIG{ES_DESC} = 0;
$CONFIG{TM_LIFETIME} = 99;
$CONFIG{TM_PRIORITY} = 99;
$CONFIG{TM_MARGIN_BEGIN} = 10;
$CONFIG{TM_MARGIN_END} = 10;
$CONFIG{TM_TT_TIMELINE} = 1;
$CONFIG{TM_TT_LIST} = 1;
$CONFIG{TM_SORTBY} = "day";
$CONFIG{TM_DESC} = 0;
#
$CONFIG{ST_FUNC} = 1;
$CONFIG{ST_REC_ON} = 0;
$CONFIG{ST_LIVE_ON} = 1;
$CONFIG{ST_URL} = "";
$CONFIG{ST_STREAMDEV_HOST} = ""; # streamdev/xineliboutput host
$CONFIG{ST_STREAMDEV_PORT} = 3000;
$CONFIG{ST_XINELIB_PORT} = 37890;
$CONFIG{ST_VIDEODIR} = "";
$CONFIG{ST_DIRECT_LINKS_ON} = 0;
$CONFIG{ST_REC_STREAMDEV} = 1;
#
$CONFIG{EPG_PRUNE} = 0;
$CONFIG{NO_EVENTID} = 0;
$CONFIG{NO_EVENTID_ON} = "";
#
$CONFIG{AT_SENDMAIL} = 0; # set to 1 and set all the "MAIL_" things if you want email notification on new autotimers.
chomp($CONFIG{MAIL_FROM} = 'autotimer@' . (`hostname -f 2>/dev/null` || "localhost.localdomain"));
$CONFIG{MAIL_TO} = "you\@example.org";
$CONFIG{MAIL_SERVER} = "localhost";
$CONFIG{MAIL_AUTH_USER} = "";
$CONFIG{MAIL_AUTH_PASS} = "";
#
$CONFIG{CHANNELS_WANTED} = "";
$CONFIG{CHANNELS_WANTED_AUTOTIMER} = 0;
$CONFIG{CHANNELS_WANTED_PRG} = 0;
$CONFIG{CHANNELS_WANTED_PRG2} = 0;
$CONFIG{CHANNELS_WANTED_TIMELINE} = 0;
$CONFIG{CHANNELS_WANTED_SUMMARY} = 0;
$CONFIG{CHANNELS_WANTED_WATCHTV} = 0;
$CONFIG{CHANNELS_WITHOUT_EPG} = 0;
#
$CONFIG{PROG_SUMMARY_COLS} = 3;
#
$CONFIG{TV_MIMETYPE} = "video/x-mpegurl";
$CONFIG{TV_EXT} = "m3u";
$CONFIG{TV_INTERVAL} = "5";
$CONFIG{TV_SIZE} = "half";
$CONFIG{REC_MIMETYPE} = "video/x-mpegurl";
$CONFIG{REC_EXT} = "m3u";
$CONFIG{REC_SORTBY} = "name";
$CONFIG{REC_DESC} = 0;
#
$CONFIG{PS_VIEW} = "ext";
#
$CONFIG{CMD_LINES} = 20;
#
$CONFIG{GUI_POPUP_WIDTH} = 550;
$CONFIG{GUI_POPUP_HEIGHT} = 550;
#
my %FEATURES;
$FEATURES{STREAMDEV} = 0; # streamdev plugin available?
$FEATURES{XINELIB} = 0; # xineliboutput plugin available?
$FEATURES{REC_RENAME} = 0; # RENR/MOVR patch available?
$FEATURES{AUTOTIMER} = 0; # use autotimer feature?
$FEATURES{MYVERSION_HR} = "$VERSION"; # Human readable VDRAdmin-AM version, e.g. 3.6.5
$FEATURES{VDRVERSION} = 0; # Numeric VDR version, e.g. 10344
$FEATURES{VDRVERSION_HR} = ''; # Human readable VDR version, e.g. 1.3.44
$FEATURES{EPGSEARCH_VERSION} = 0; # Numeric epgsearch plugin version, e.g. 924
$FEATURES{EPGSEARCH_VERSION_HR} = ''; # Human readable epgsearch plugin version, e.g. 0.9.24
my %EPGSEARCH_SETTINGS;
my $SERVERVERSION = "vdradmind/$VERSION";
my %ERROR_MESSAGE;
my $MY_ENCODING = '';
my ($TEMPLATEDIR, $TEMPLATECACHE, $CONFFILE, $LOGGING, $LOGDIR, $LOGFILE, $LOGLEVEL, $PIDFILE, $AT_FILENAME, $DONE_FILENAME, $BL_FILENAME, $ETCDIR, $CERTSDIR, $USER_CSS);
if (!$SEARCH_FILES_IN_SYSTEM) {
$ETCDIR = "${BASENAME}";
$CERTSDIR = "${ETCDIR}/certs";
$TEMPLATEDIR = "${BASENAME}template";
$TEMPLATECACHE = "${BASENAME}cache";
$CONFFILE = "${BASENAME}vdradmind.conf";
$LOGDIR = "${BASENAME}";
$PIDFILE = "${BASENAME}vdradmind.pid";
$AT_FILENAME = "${BASENAME}vdradmind.at";
$DONE_FILENAME = "${BASENAME}vdradmind.done";
$BL_FILENAME = "${BASENAME}vdradmind.bl";
$USER_CSS = "${BASENAME}user.css";
bindtextdomain("vdradmin", "${BASENAME}locale");
} else {
$ETCDIR = "/etc/vdradmin";
$CERTSDIR = "${ETCDIR}/certs";
$TEMPLATEDIR = "/usr/share/vdradmin/template";
$TEMPLATECACHE = "/var/cache/vdradmin";
$LOGDIR = "/var/log/vdradmin";
$PIDFILE = "/var/run/vdradmin/vdradmind.pid";
$CONFFILE = "${ETCDIR}/vdradmind.conf";
$AT_FILENAME = "${ETCDIR}/vdradmind.at";
$DONE_FILENAME = "${ETCDIR}/vdradmind.done";
$BL_FILENAME = "${ETCDIR}/vdradmind.bl";
$USER_CSS = "${ETCDIR}/user.css";
bindtextdomain("vdradmin", "/usr/share/locale");
}
my $DONE = &DONE_Read || {};
textdomain("vdradmin");
my $UserCSS;
$UserCSS = "user.css" if (-e "$USER_CSS");
my $USE_SHELL_GZIP = false; # set on false to use the gzip library
my (%EPG, %CHAN, %CHAN_TABLES, $q, $ACCEPT_GZIP, $SVDRP, $low_time, @RECORDINGS);
my (%RECORDING_FOLDERS, %RECORDING_BY_ID);
my (%mimehash) = (html => "text/html",
png => "image/png",
gif => "image/gif",
jpg => "image/jpeg",
css => "text/css",
ico => "image/x-icon",
js => "text/javascript",
swf => "application/x-shockwave-flash"
);
my @LOGINPAGES = qw(prog_summary prog_list2 prog_timeline prog_list timer_list rec_list);
$SIG{INT} = \&Shutdown;
$SIG{TERM} = \&Shutdown;
$SIG{HUP} = \&HupSignal;
$SIG{PIPE} = 'IGNORE';
#
my ($UseSSL, $UseIPv6);
my $DAEMON = 1;
for (my $i = 0 ; $i < scalar(@ARGV) ; $i++) {
$_ = $ARGV[$i];
if (/^(-h|--help)$/) {
$DAEMON = 0;
print("Usage $EXENAME [OPTION]...\n");
print("A perl client for the Linux Video Disk Recorder.\n\n");
print(" -n --nofork don't fork\n");
print(" -c --config run configuration dialog\n");
print(" -d [dir] --cfgdir [dir] use [dir] for configuration files\n");
print(" -k --kill kill a forked vdradmind[.pl]\n");
print(" -p [name] --pid [name] name of pidfile (ignored with -n)\n");
print(" -6 --ipv6 use IPv6\n");
print(" -s --ssl only accept https:// connections\n");
print(" -l [level] --log [level] set log level for this session [0 - 7]\n");
print(" -L [file] --logfile [file] set log file for this session\n");
print(" -h --help this message\n");
exit(0);
}
if (/^(--nofork|-n)$/) {
$DAEMON = 0;
next;
}
if (/^(--cfgdir|-d)$/) {
$ETCDIR = $ARGV[ ++$i ];
$CONFFILE = "${ETCDIR}/vdradmind.conf";
$AT_FILENAME = "${ETCDIR}/vdradmind.at";
$DONE_FILENAME = "${ETCDIR}/vdradmind.done";
$BL_FILENAME = "${ETCDIR}/vdradmind.bl";
$USER_CSS = "${ETCDIR}/user.css";
next;
}
if (/^(--config|-c)$/) {
$DAEMON = 0;
if (-e $CONFFILE) {
ReadConfig();
} elsif ($CONFIG{VDR_PORT} == 2001 &&
`vdr --version 2>/dev/null` =~ /^vdr\s+.*?(\d+)\.(\d+)\.(\d+)/mi) {
my $vdrversion = $1 * 10000 + $2 * 100 + $3;
$CONFIG{VDR_PORT} = 6419 if ($vdrversion >= 10715);
}
LoadTranslation();
$CONFIG{VDR_HOST} = Question(gettext("What's your VDR hostname (e.g video.intra.net)?"), $CONFIG{VDR_HOST});
$CONFIG{VDR_PORT} = Question(gettext("On which port does VDR listen to SVDRP queries?"), $CONFIG{VDR_PORT});
$CONFIG{SERVERHOST} = Question(gettext("On which address should VDRAdmin-AM listen (0.0.0.0 for any)?"), $CONFIG{SERVERHOST});
$CONFIG{SERVERPORT} = Question(gettext("On which port should VDRAdmin-AM listen?"), $CONFIG{SERVERPORT});
$CONFIG{USERNAME} = Question(gettext("Username?"), $CONFIG{USERNAME});
$CONFIG{PASSWORD} = Question(gettext("Password?"), $CONFIG{PASSWORD});
$CONFIG{VIDEODIR} = Question(gettext("Where are your recordings stored?"), $CONFIG{VIDEODIR});
$CONFIG{VDRCONFDIR} = Question(gettext("Where are your VDR's configuration files located?"), $CONFIG{VDRCONFDIR});
(my $err = WriteConfig()) =~ s|<br\s*/?>$||gi;
if ($err) {
Log(LOG_ERROR, $err);
exit(1);
}
print(gettext("Config file written successfully.") . "\n");
exit(0);
}
if (/^(--kill|-k)$/) {
$DAEMON = 0;
exit(1) unless (-e $PIDFILE);
my $pid = getPID($PIDFILE);
my $killed = defined($pid) ? kill(2, $pid) : -1;
sleep(1);
if ($killed > 0 && -e $PIDFILE) { # Not deleted by kill/Shutdown()?
unlink($PIDFILE) or Log(LOG_WARNING, "Can't delete pid file '$PIDFILE': $!");
}
exit($killed > 0 ? 0 : 1);
}
if (/^(--pid|-p)$/) {
$PIDFILE = $ARGV[ ++$i ];
next;
}
if (/^(--ipv6-all)$/) {
$UseIPv6 = 2;
next;
}
if (/^(--ipv6|-6)$/) {
$UseIPv6 = 1;
next;
}
if (/^(--ssl|-s)$/) {
$UseSSL = 1;
next;
}
if (/^(--log|-l)$/) {
$LOGLEVEL = $ARGV[ ++$i ];
$LOGGING = 1;
next;
}
if (/^(--logfile|-L)$/) {
$LOGFILE = $ARGV[ ++$i ];
if ($LOGFILE =~ m|/|) {
($LOGDIR, $LOGFILE) = $LOGFILE =~ m|^(.+)/([^/]*)$|;
} else {
$LOGDIR = ".";
}
$LOGGING = 1;
next;
}
if (/^(--displaycall|-i)$/) {
$DAEMON = 0;
for (my $z = 0 ; $z < 5 ; $z++) {
DisplayMessage($ARGV[ $i + 1 ]);
sleep(3);
}
CloseSocket();
exit(0);
}
if (/^(--message|-m)$/) {
$DAEMON = 0;
DisplayMessage($ARGV[ $i + 1 ]);
CloseSocket();
exit(0);
}
if (/^-u$/) {
# Don't use user.css
$UserCSS = undef;
}
}
check_permissions() or exit 1;
if ($UseIPv6) {
if (eval { require IO::Socket::INET6 }) {
$InetSocketModule = 'IO::Socket::INET6';
$VdrSocketModule = 'IO::Socket::INET6' if ($UseIPv6 == 2);
} else {
die("ERROR: Can't load module IO::Socket::INET6: $@");
}
}
ReadConfig();
$LOGGING ||= $CONFIG{LOGGING};
$LOGFILE ||= $CONFIG{LOGFILE};
LoadTranslation();
#use Template::Constants qw( :debug );
# IMHO a better Template Modul ;-)
# some useful options (see below for full list)
my $Xtemplate_vars = { usercss => $UserCSS,
charset => $MY_ENCODING,
gettext => sub{ $_[0] =~ s/\n\s+//g; return gettext($_[0]); },
config => \%CONFIG,
features => \%FEATURES
};
my $Xconfig = {
START_TAG => '\<\?\%', # tag style
END_TAG => '\%\?\>', # tag style
INCLUDE_PATH => $TEMPLATEDIR, # or list ref
INTERPOLATE => 0, # expand "$var" in plain text
PRE_CHOMP => 1, # cleanup whitespace
POST_CHOMP => 1, # cleanup whitespace
EVAL_PERL => 1, # evaluate Perl code blocks
COMPILE_EXT => 'cache', # tuning for templates
COMPILE_DIR => $TEMPLATECACHE, # tuning for templates
STAT_TTL => 3600, # tuning for templates
VARIABLES => $Xtemplate_vars,
# Developer options:
#CACHE_SIZE => 0,
#STAT_TTL => 1,
#DEBUG => DEBUG_ALL,
};
# create Template object
my $Xtemplate;
eval {
$Xtemplate = Template->new($Xconfig);
};
if ($@) {
# Perhaps a cache dir problem, try without it
chomp(my $err = $@);
delete @$Xconfig{qw(COMPILE_DIR COMPILE_EXT)};
$Xtemplate = Template->new($Xconfig);
Log(LOG_WARNING, "Problem setting up dir for compiled templates, expect degraded performance: $err");
}
# ---- End new template section ----
my $LOG_TO_SYSLOG = 0;
if ($LOGGING && $LOGFILE eq "syslog") {
eval {
require Sys::Syslog;
Sys::Syslog->import(qw(:standard));
openlog($PROCNAME, "cons,pid", "daemon");
} and $LOG_TO_SYSLOG = 1;
if ($@) {
Log(LOG_WARNING,
"Error setting up syslog logging, falling back to stderr: $@");
}
}
if (!$LOG_TO_SYSLOG) {
*closelog = sub {}; # for Shutdown()
}
if ($CONFIG{MOD_GZIP}) {
# lib gzipping
require Compress::Zlib;
}
if (-e "$PIDFILE") {
my $pid = getPID($PIDFILE);
if ($pid) {
Log(LOG_ERROR, "There's already a copy of this program running! (pid: $pid)\n");
chomp(my $pidproc = `ps -p $pid -o comm=` || "");
if ($pidproc eq $PROCNAME) {
Log(LOG_ERROR, "If you feel this is an error, remove $PIDFILE!\n");
exit(1);
}
Log(LOG_ERROR, "The pid $pid is not a running $PROCNAME process, so I'll start anyway.\n");
} else {
Log(LOG_ERROR, "$PIDFILE exists, but is empty or contains bogus data, so I'll start anyway.\n");
}
}
if ($DAEMON) {
open(STDIN, "</dev/null");
defined(my $pid = fork) or die "Cannot fork: $!\n";
if ($pid) {
Log(LOG_NOTICE, sprintf(gettext("%s %s started with pid %d."), $EXENAME, $VERSION, $pid));
writePID($PIDFILE, $pid);
exit(0);
}
} else {
Log(LOG_NOTICE, sprintf("%s %s started", $EXENAME, $VERSION));
}
my ($Daemon, $Client, $Request);
if ($UseSSL) {
if (eval { require HTTP::Daemon::SSL; }) {
my $CERT_FILE = "$CERTSDIR/server-cert.pem";
die("ERROR: $CERT_FILE missing. Please create it!\n") unless (-e $CERT_FILE);
my $KEY_FILE = "$CERTSDIR/server-key.pem";
die("ERROR: $KEY_FILE missing. Please create it!\n") unless (-e $KEY_FILE);
my $CA_PATH = "$CERTSDIR/ca";
$CA_PATH = undef unless (-d $CA_PATH);
my $CA_FILE = "$CERTSDIR/my-ca.pem";
$CA_FILE = undef unless (-f $CA_FILE);
$Daemon = HTTP::Daemon::SSL->new(
LocalPort => $CONFIG{SERVERPORT},
LocalAddr => $CONFIG{SERVERHOST},
Listen => 10,
ReuseAddr => 1,
ReusePort => 1,
Reuse => 1,
SSL_cert_file => "$CERT_FILE",
SSL_key_file => "$KEY_FILE",
SSL_ca_file => "$CA_FILE",
SSL_ca_path => "$CA_PATH"
);
*{HTTP::Daemon::SSL::product_tokens} = sub {return $SERVERVERSION;};
} else {
die("ERROR: Can't load module HTTP::Daemon::SSL: $@");
}
} else {
if (eval { require HTTP::Daemon; }) {
$Daemon = HTTP::Daemon->new(
LocalPort => $CONFIG{SERVERPORT},
LocalAddr => $CONFIG{SERVERHOST},
Listen => 10,
ReuseAddr => 1,
ReusePort => 1,
Reuse => 1
);
*{HTTP::Daemon::product_tokens} = sub {return $SERVERVERSION;};
} else {
die("ERROR: Can't load module HTTP::Daemon: $@");
}
}
if (!$Daemon) {
my $host = $CONFIG{SERVERHOST} || '(SERVERHOST missing)';
my $port = $CONFIG{SERVERPORT} || '(SERVERPORT missing)';
die("Can't start server at $host:$port: $@\n");
}
$SIG{__DIE__} = \&SigDieHandler;
my @reccmds = loadCommandsConf("$CONFIG{VDRCONFDIR}/reccmds.conf");
my @vdrcmds = loadCommandsConf("$CONFIG{VDRCONFDIR}/commands.conf");
##
# Mainloop
##
my ($MyURL, $Referer, $Query, $Guest);
my @GUEST_USER = qw(prog_detail prog_list prog_list2 prog_timeline timer_list at_timer_list epgsearch_list
prog_summary rec_list rec_detail show_top toolbar show_help about);
my @TRUSTED_USER = (
@GUEST_USER, qw(prog_detail_form prog_detail_aktion at_timer_edit at_timer_new at_timer_save at_timer_test at_timer_delete
epgsearch_upds epgsearch_edit epgsearch_save epgsearch_save_template epgsearch_delete_template epgsearch_delete epgsearch_toggle timer_new_form timer_add timer_delete timer_toggle rec_delete rec_rename rec_edit
config prog_switch rc_show rc_hitk grab_picture at_timer_toggle tv_show tv_switch
live_stream rec_stream rec_stream_folder rec_play rec_cut force_update vdr_cmds export_channels_m3u epgsearch_config epgsearch_bl_edit epgsearch_bl_save epgsearch_bl_delete)
);
my $MyStreamBase = "./vdradmin.";
$MyURL = "./vdradmin.pl";
my @Connections = ();
$CONFIG{CACHE_LASTUPDATE} = 0;
while (true) {
@Connections = grep {$_->{socket}->connected} @Connections;
my $fd_set = '';
vec($fd_set, $Daemon->fileno, 1) = 1;
foreach my $c (@Connections) {
vec($fd_set, $c->{socket}->fileno, 1) = 1;
}
my $n_ready = select($fd_set, undef, undef, 2);
Log(LOG_DEBUG, sprintf("[DAEMON] select() -> %s", printVec($fd_set))) if $n_ready > 0;
my $now = time();
# update EPG
if ($n_ready == 0
&& ($CONFIG{CACHE_BG_UPDATE} == 1
|| $CONFIG{AT_FUNC} && $FEATURES{AUTOTIMER}
)) {
if (UptoDate() != 0) {
Log(LOG_DEBUG, "[EPG] Updating EPG data in the background FAILED, trying again in 60secs.");
$CONFIG{CACHE_LASTUPDATE} = $now + 60 - $CONFIG{CACHE_TIMEOUT} * 60;
}
}
if (vec($fd_set, $Daemon->fileno, 1)) {
my $con = $Daemon->accept();
if ($con) {
Log(LOG_DEBUG, sprintf("[DAEMON] accepted fd=%d, peer=%s", $con->fileno, $con->peerhost));
push(@Connections, {socket => $con, ttl => undef});
}
next;
}
my @new = ();
my $found = undef;
foreach my $c (@Connections) {
if (vec($fd_set, $c->{socket}->fileno, 1)) {
$c->{ttl} = undef;
if (!$found) {
$found = $c;
# don't copy
} else {
push(@new, $c);
}
} else {
# no incoming data
if ($c->{ttl} && $now >= $c->{ttl}) {
Log(LOG_DEBUG, sprintf("[CLIENT(%d)] keep-alive timeout\n", $c->{socket}->fileno));
close($c->{socket});
} else {
$c->{ttl} = $now + $CONFIG{HTTP_KEEPALIVE_TIMEOUT} unless $c->{ttl};
push(@new, $c);
}
}
}
@Connections = @new;
next unless $found;
# move to the end
push(@Connections, $found);
$Client = $found->{socket};
undef $found;
if ($CONFIG{LOCAL_NET_ONLY} && !subnetcheck($Client->peerhost, $CONFIG{LOCAL_NET})) {
closeClient();
next;
}
$Client->timeout(2);
my $req = $Client->get_request();
if ($req) {
processRequest($req);
} else {
Log(LOG_DEBUG, sprintf("[CLIENT(%d)] get_request() failed: %s", $Client->fileno, $Client->reason));
closeClient();
}
}
#############################################################################
#############################################################################
sub printVec {
my $vec = shift;
my @fdarr = ();
for (my $i = 0; $i < length($vec) * 8; $i++) {
push(@fdarr, $i) if (vec($vec, $i, 1));
}
return join(',', @fdarr);
}
sub closeClient {
if ($Client) {
Log(LOG_DEBUG, sprintf("CLIENT(%d)] close()", $Client->fileno));
@Connections = grep {$_->{socket} != $Client} @Connections;
close($Client);
undef $Client;
}
}
sub processRequest {
my $req = shift;
$ACCEPT_GZIP = 0;
#print("REQUEST: $raw_request\n");
my $raw_request = $1 if ($req->as_string =~ /^([^\r\n]++)\r*\n/);
Log(LOG_DEBUG, sprintf("[CLIENT(%d)] $raw_request\n", $Client->fileno));
if ($req->uri =~ /(\/[\w\.\/-\:]*)(?:\?([\w=&\.\+\%-\:\!\@\~\#]+))?$/) {
($Request, $Query) = ($1, $2);
} else {
Error("404", gettext("Not found"), gettext("The requested URL was not found on this server!"));
return;
}
$Request =~ s|^/+|/|;
local $ENV{HTTP_HOST};
# parse header
my ($username, $password, $http_useragent);
$Referer = $req->header("Referer");
$ENV{HTTP_HOST} = $req->header("Host");
($username, $password) = split(":", MIME::Base64::decode_base64($1), 2) if ($req->header("Authorization") =~ /basic (.*)/i);
$http_useragent = $req->header("User-Agent");
$ACCEPT_GZIP = 1 if ($req->header("Accept-Encoding") =~ /gzip/);
my ($http_status, $bytes_transfered);
# authenticate
#print("Username: $username / Password: $password\n");
my $checkpass = defined($username) && defined($password);
if (($checkpass && $CONFIG{USERNAME} eq $username && $CONFIG{PASSWORD} eq $password)
|| subnetcheck($Client->peerhost, $CONFIG{LOCAL_NET})) {
$Guest = 0;
} elsif (($checkpass && $CONFIG{USERNAME_GUEST} eq $username && $CONFIG{PASSWORD_GUEST} eq $password) && $CONFIG{GUEST_ACCOUNT}) {
$Guest = 1;
} else {
($http_status, $bytes_transfered) = headerNoAuth();
Log(LOG_INFO, "[ACCESS] " . access_log($Client->peerhost, $username, $raw_request, $http_status, $bytes_transfered, $Request, $http_useragent));
return;
}
# serve request
$SVDRP = SVDRP->new;
$MyURL = "." . $Request;
if ($Request eq "/vdradmin.pl" || $Request eq "/vdradmin." . $CONFIG{TV_EXT} || $Request eq "/vdradmin." . $CONFIG{REC_EXT}) {
$q = CGI->new($Query);
my $aktion;
my ($real_aktion, $dummy) = split("#", $q->param("aktion"), 2);
if ($real_aktion eq "at_timer_aktion") {
$real_aktion = "at_timer_save";
$real_aktion = "at_timer_delete" if ($q->param("at_delete"));
$real_aktion = "force_update" if ($q->param("at_force"));
$real_aktion = "at_timer_test" if ($q->param("test"));
} elsif ($real_aktion eq "timer_aktion") {
$real_aktion = "timer_delete" if ($q->param("timer_delete"));
$real_aktion = "timer_toggle" if ($q->param("timer_active") || $q->param("timer_inactive"));
} elsif ($real_aktion eq "epgsearch_aktion") {
$real_aktion = "epgsearch_save";
$real_aktion = "epgsearch_save_template" if ($q->param("save_template"));
$real_aktion = "epgsearch_delete_template" if ($q->param("delete_template"));
$real_aktion = "epgsearch_delete" if ($q->param("delete"));
$real_aktion = "epgsearch_edit" if ($q->param("single_test"));
$real_aktion = "epgsearch_list" if ($q->param("execute"));
$real_aktion = "epgsearch_list" if ($q->param("favorites"));
$real_aktion = "epgsearch_list" if ($q->param("exit"));
$real_aktion = "epgsearch_upds" if ($q->param("upds"));
} elsif ($real_aktion eq "epgsearch_config_aktion") {
$real_aktion = "epgsearch_config";
$real_aktion = "epgsearch_bl_delete" if ($q->param("delete"));
} elsif ($real_aktion eq "epgsearch_bl_aktion") {
$real_aktion = "epgsearch_bl_save";
$real_aktion = "epgsearch_config" if ($q->param("exit"));
}
my @ALLOWED_FUNCTIONS;
$Guest ? (@ALLOWED_FUNCTIONS = @GUEST_USER) : (@ALLOWED_FUNCTIONS = @TRUSTED_USER);
for (@ALLOWED_FUNCTIONS) {
($aktion = $real_aktion) if ($real_aktion eq $_);
}
if ($aktion) {
eval("(\$http_status, \$bytes_transfered) = $aktion();");
} else {
# XXX redirect to no access template
Error("403", gettext("Forbidden"), gettext("You don't have permission to access this function!"));
next;
}
} elsif ($Request eq "/") {
$MyURL = "./vdradmin.pl";
($http_status, $bytes_transfered) = show_index();
} elsif ($Request eq "/navigation.html") {
($http_status, $bytes_transfered) = show_navi();
} else {
($http_status, $bytes_transfered) = SendFile($Request);
}
Log(LOG_INFO, "[ACCESS] " . access_log($Client->peerhost, $username, $raw_request, $http_status, $bytes_transfered, $Request, $http_useragent));
$SVDRP->close;
}
sub check_permissions {
my $rc = 1;
check_rw_dir($ETCDIR) or $rc = 0;
check_rw_dir($CERTSDIR) if ($UseSSL);
check_rw_dir($TEMPLATECACHE) or $rc = 0;
# Don't check permissions when logging to syslog or stderr
!$LOGFILE
or $LOGFILE eq "syslog" or $LOGFILE eq "stderr"
or check_rw_dir($LOGDIR) or $rc = 0;
check_rw_file($PIDFILE) or $rc = 0 if $DAEMON;
check_rw_file($CONFFILE) or $rc = 0;
if ($CONFIG{AT_FUNC} || $FEATURES{AUTOTIMER}) {
check_rw_file($AT_FILENAME) or $rc = 0;
check_rw_file($DONE_FILENAME) or $rc = 0;
check_rw_file($BL_FILENAME) or $rc = 0;
}
return $rc;
}
sub check_rw_dir {
my $dir = shift;
# print "Checking directory '$dir':\n";
if (-e "$dir") {
if (! -d _) {
print "ERROR: '$dir' is NOT a directory!\n";
return 0;
}
if (-r _) {
# print "directory '$dir' is readable!\n";
} else {
print "ERROR: directory '$dir' is NOT readable!\n";
return 0;
}
if (-w _) {
# print "directory '$dir' is writable!\n";
} else {
print "ERROR: directory '$dir' is NOT writable!\n";
return 0;
}
} else {
print "ERROR: directory '$dir' is missing!\n";
return 0;
}
return 1;
}
sub check_rw_file {
my $file = shift;
# print "Checking file '$file':\n";
if (-e "$file") {
if (-d _) {
print "ERROR: '$file' is a directory!\n";
return 0;
}
if (-r _) {
# print "file '$file' is readable!\n";
} else {
print "ERROR: file '$file' is NOT readable!\n";
return 0;
}
if (-w _) {
# print "file '$file' is writable!\n";
} else {
print "ERROR: file '$file' is NOT writable!\n";
return 0;
}
} else {
$file =~ /(^.*)\/[^\/]*$/;
return check_rw_dir($1);
}
return 1;
}
sub GetChannelDesc { #TODO: unused
my (%hash);
for (@{$CHAN{$CHAN_FULL}->{channels}}) {
$hash{ $_->{id} } = $_->{name};
}
return (%hash);
}
sub GetChannelDescByNumber {
my $vdr_id = shift;
if ($vdr_id) {
for (@{$CHAN{$CHAN_FULL}->{channels}}) {
if ($_->{vdr_id} == $vdr_id) {
return ($_->{name});
}
}
} else {
return (0);
}
}
sub GetChannelUniqIdByNumber {
my $vdr_id = shift;
if ($vdr_id) {
for (@{$CHAN{$CHAN_FULL}->{channels}}) {
if ($_->{vdr_id} == $vdr_id) {
return ($_->{uniq_id});
}
}
} else {
return (0);
}
}
sub include {
my $file = shift;
if ($file) {
eval(ReadFile($file));
}
}
sub ReadFile {
my $file = shift;
return if (!$file);
open(I18N, $file) || HTMLError(sprintf($ERROR_MESSAGE{cant_open}, $file));
my $buf = join("", <I18N>);
close(I18N);
return ($buf);
}
sub GetChannelID { #TODO: unused
my ($sid) = $_[0];
for (@{$CHAN{$CHAN_FULL}->{channels}}) {
if ($_->{id} == $sid) {
return ($_->{number});
}
}