lilalo

view l3-cgi @ 47:fcd63bc2bfa0

Печать индекса в файл при установленной переменной l3_to_file
author devi
date Wed Nov 23 00:18:36 2005 +0200 (2005-11-23)
parents f13cd5b4f2d0
children 568bab7090fc
line source
1 #!/usr/bin/perl
3 use strict;
4 use CGI qw(:standard);
5 use XML::Simple;
6 use utf8;
8 BEGIN {
9 chdir("/home/devi/cvs/lilalo");
10 require l3config;
11 };
13 my %filter;
15 for my $key (qw(login_from)) {
16 $filter{$key} = param($key) if param($key);
17 $filter{$key} = $ENV{$key} if defined $ENV{$key};
18 }
20 my %Trainings;
21 my $XMLTraining;
22 my %Machines;
23 my $print = "";
25 my $is_not_cgi="";
26 $is_not_cgi = "1" if defined($ENV{l3_to_file});
27 # Пока что мы считаем, что вызов выполняется через CGI,
28 # если не установлена переменная l3_to_file
29 # Это неверно, но как временное решение - пойдёт
31 $l3config::Config{frontend_css} =~ s@^/@@ if $is_not_cgi;
33 sub load_training
34 {
35 my $classfile =
36 $_[0]||
37 $l3config::Config{"classfile"} ||
38 $l3config::Config{"path_classes"}."/".$l3config::Config{"class"}.$l3config::Config{"class_suffix"};
40 my $XMLTraining = XMLin($classfile , ForceArray => [ 'student' ] )
41 or die "Can't open file of the training ",$classfile,"\n";
43 for my $student (@{$XMLTraining->{"student"}}) {
44 $XMLTraining->{host}->{$student->{"host"}}=$student;
45 }
46 return $XMLTraining;
47 }
51 if ($ENV{PATH_INFO} eq "/index") {
52 # Показываем индекс курсов
53 my @training_files = glob($l3config::Config{"path_classes"}."/*".$l3config::Config{"class_suffix"});
54 if (@training_files) {
55 for my $training_file (@training_files) {
56 my $training = load_training($training_file);
57 $Trainings{$training->{"date"}}=$training;
58 }
59 $print .= "<html>\n";
60 $print .= "<head>\n";
61 $print .= "<title>Журналы лабораторных работ</title>\n";
62 $print .= "</head>\n";
63 $print .= "<body>\n";
64 $print .= "<table>\n";
65 for my $tdate (reverse sort keys %Trainings) {
66 my $t = $Trainings{$tdate};
67 $print .= "<tr>";
68 $print .= "<td>".$t->{date}."</td>";
69 $print .= "<td>".$t->{course}."</td>";
70 $print .= "</tr>\n";
71 $print .= "<tr>";
72 $print .= "<td/>";
73 $print .= "<td><pre>";
74 for my $host (sort keys %{$t->{host}}) {
75 my $h = $t->{host}->{$host};
76 $print .= "$host";
77 $print .= " ".$h->{firstname}." ".$h->{surname}." ";
78 $print .= "<a href='/cgi-bin/l3/".$tdate."/".$host."/root'>root</a> ";
79 $print .= "<a href='/cgi-bin/l3/".$tdate."/".$host."/".$h->{user}."'>".$h->{user}."</a> ";
80 $print .= "\n";
81 }
82 $print .= "</pre><td>";
83 $print .= "</tr>\n";
84 }
85 $print .= "</table>\n";
86 $print .= "</body>\n";
87 $print .= "</html>\n";
88 }
89 else {
90 $print .= "No training-files found<br/>\n";
91 $print .= "Template to load files: ".$l3config::Config{"path_classes"}."*".$l3config::Config{"class_suffix"}."\n"
92 }
93 }
94 elsif ($ENV{PATH_INFO} eq "/current/index" || $ENV{PATH_INFO} eq "") {
95 my $t = load_training();
97 $print .= "<html>\n";
98 $print .= "<head>\n";
99 $print .= "<title>Журналы лабораторных работ</title>\n";
100 $print .= "<link rel='stylesheet' href='".$l3config::Config{frontend_css}."' type='text/css'/>\n";
101 $print .= "</head>\n";
102 $print .= "<body>\n";
103 $print .= "<h1>Журналы лабораторных работ</h1>\n";
104 $print .= "<table>\n";
105 $print .= "<tr class='table_header'>\n";
106 $print .= "<td>"."Имя"."</td>";
107 $print .= "<td>Хост</td>";
108 $print .= "<td colspan='3'>Пользователь</td>";
109 $print .= "</tr>\n";
110 for my $host (sort keys %{$t->{host}}) {
112 my $prefix = "/cgi-bin/l3/current/";
113 my $suffix = "";
114 if ($is_not_cgi) {
115 $prefix = "";
116 $suffix = ".html";
117 }
119 $print .= "<tr>\n";
120 my $h = $t->{host}->{$host};
121 $print .= "<td>".$h->{firstname}." ".$h->{surname}."</td>";
122 $print .= "<td>$host</td>";
123 $print .= "<td><a href='".$prefix.$host."/root$suffix'>root</a></td>";
124 $print .= "<td><a href='".$prefix.$host."/".$h->{user}."$suffix'>".$h->{user}."</a></td>";
125 $print .= "<td><a href='".$prefix.$host."$suffix'>все</a></td>";
126 $print .= "</td>\n";
127 $print .= "</tr>\n";
128 }
129 $print .= "<tr>\n";
130 $print .= "<td>"."Инструктор"."</td>";
131 $print .= "<td>from linux.nt</td>";
132 $print .= "<td/>";
133 $print .= "<td/>";
134 $print .= "<td><a href='/cgi-bin/l3/current"."?login_from=192.168.15.254'>все</a></td>";
135 $print .= "</tr>\n";
136 $print .= "</table>\n";
137 $print .= "</body>\n";
138 $print .= "</html>\n";
139 }
140 elsif ($ENV{PATH_INFO} eq "/current") {
141 open (FRONTEND, "./l3-frontend --output - --show_comments no |");
142 while (<FRONTEND>) {
143 $print .= $_;
144 }
145 close(FRONTEND);
146 }
147 else {
148 # Вызов производится по URL
149 my ($skip, $course, $host, $user) = split /\//,$ENV{PATH_INFO},4;
151 $l3config::Config{"class"}=$course if $course ne 'current';
152 $XMLTraining = load_training;
154 my @args=(
155 "--output" => "-",
156 "--show_comments" => "no",
157 "--course-center" => $XMLTraining->{center},
158 "--course-trainer" => $XMLTraining->{instructor}->{firstname}." ".$XMLTraining->{instructor}->{surname},
159 "--course-student" => $XMLTraining->{host}->{$host}->{firstname}." ".$XMLTraining->{host}->{$host}->{surname},
160 "--course-code" => $XMLTraining->{course},
161 "--course-date" => $XMLTraining->{date},
162 "--encoding" => $XMLTraining->{host}->{$host}->{charset},
163 );
164 if ($course ne 'current') {
165 push @args, ("--backend_datafile" => "/var/lilalo/lablogs-xml/$course/$host/$user.xml");
166 } else {
167 $filter{hostname} = $host if $host;
168 $filter{user} = $user if $user;
169 push @args, ("--filter" => join ("&", (map("$_=$filter{$_}", keys %filter))));
170 #push @args, ("--filter" => "hostname=".$host."&user=".$user);
171 }
173 open (FRONTEND, "./l3-frontend ".join(" ",map("\"$_\"",@args))." |");
174 while (<FRONTEND>) {
175 $print .= $_;
176 }
177 close(FRONTEND);
178 }
180 # Если задана переменная окружения l3_to_file,
181 # печатаем в файл с соответствующим именем,
182 # в противном случае -- выводим на стандартный поток вывода
183 if (not defined $ENV{l3_to_file}) {
184 binmode STDOUT, ":utf8";
185 print header(-charset => "utf-8");
186 print $print;
187 }
188 else {
189 open(FILE, ">:utf8", $ENV{l3_to_file})
190 or die "Can't open $ENV{l3_to_file} for writing";
191 print FILE $print;
192 close(FILE);
193 }