lilalo

view l3-cgi @ 62:c4bea959dbb1

Beautyfication of l3-agent code. Many lines were erased. Need to be tested
author devi
date Thu Jan 26 00:00:53 2006 +0200 (2006-01-26)
parents 187b6636a3be
children d499fa3b2af1
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 l3config::init_config();
12 };
14 my %filter;
16 for my $key (qw(login_from)) {
17 $filter{$key} = param($key) if param($key);
18 $filter{$key} = $ENV{$key} if defined $ENV{$key};
19 }
21 my %Trainings;
22 my $XMLTraining;
23 my %Machines;
24 my $print = "";
26 $l3config::Config{cgi2file} = $ENV{cgi2file} if defined($ENV{cgi2file});
27 $ENV{PATH_INFO} = $l3config::Config{cgi_path_info} if $l3config::Config{cgi_path_info};
29 my $is_not_cgi="";
30 $is_not_cgi = "1" if $l3config::Config{cgi2file};
31 # Пока что мы считаем, что вызов выполняется через CGI,
32 # если не установлена переменная cgi2file
33 # и не передан такой аргумент командной строки.
34 # Это неверно, но как временное решение - пойдёт
35 my $to_file = $l3config::Config{cgi2file};
37 $l3config::Config{frontend_css} =~ s@^/@@ if $is_not_cgi;
39 sub load_training
40 {
41 my $training_file;
43 $training_file = $l3config::Config{"path_classes"}.$_[0].$l3config::Config{"class_suffix"} if $_[0];
44 $training_file ||= $l3config::Config{"classfile"} ||
45 $l3config::Config{"path_classes"}.$l3config::Config{"class"}.$l3config::Config{"class_suffix"};
47 my $XMLTraining = XMLin($training_file , ForceArray => [ 'student' ] )
48 or die "Can't open file of the training ",$training_file,"\n";
50 for my $student (@{$XMLTraining->{"student"}}) {
51 $XMLTraining->{host}->{$student->{"host"}}=$student;
52 }
53 return $XMLTraining;
54 }
58 if ($ENV{PATH_INFO} eq "/index") {
59 # Показываем индекс курсов
60 my @training_files = glob($l3config::Config{"path_classes"}."/2*".$l3config::Config{"class_suffix"});
61 if (@training_files) {
62 for my $training_file (@training_files) {
63 $training_file =~ s@.*/@@;
64 $training_file =~ s@$l3config::Config{"class_suffix"}$@@;
65 my $training = load_training($training_file);
66 $Trainings{$training->{"date"}}=$training;
67 }
68 $print .= "<html>\n";
69 $print .= "<head>\n";
70 $print .= "<title>Журналы лабораторных работ</title>\n";
71 $print .= "</head>\n";
72 $print .= "<body>\n";
73 $print .= "<table>\n";
74 for my $tdate (reverse sort keys %Trainings) {
75 my $t = $Trainings{$tdate};
76 $print .= "<tr>";
77 $print .= "<td>".$t->{date}."</td>";
78 $print .= "<td>".$t->{course}."</td>";
79 $print .= "</tr>\n";
80 $print .= "<tr>";
81 $print .= "<td/>";
82 $print .= "<td><pre>";
83 for my $host (sort keys %{$t->{host}}) {
84 my $h = $t->{host}->{$host};
85 $print .= "$host";
86 $print .= " ".$h->{firstname}." ".$h->{surname}." ";
87 $print .= "<a href='/cgi-bin/l3/".$tdate."/".$host."/root'>root</a> ";
88 $print .= "<a href='/cgi-bin/l3/".$tdate."/".$host."/".$h->{user}."'>".$h->{user}."</a> ";
89 $print .= "\n";
90 }
91 $print .= "</pre><td>";
92 $print .= "</tr>\n";
93 }
94 $print .= "</table>\n";
95 $print .= "</body>\n";
96 $print .= "</html>\n";
97 }
98 else {
99 $print .= "No training-files found<br/>\n";
100 $print .= "Template to load files: ".$l3config::Config{"path_classes"}."*".$l3config::Config{"class_suffix"}."\n"
101 }
102 }
103 elsif ($ENV{PATH_INFO} eq "/current") {
104 open (FRONTEND, "./l3-frontend --output - --show_comments no --frontend_css $l3config::Config{frontend_css}|");
105 binmode FRONTEND, ":utf8";
106 while (<FRONTEND>) {
107 $print .= $_;
108 }
109 close(FRONTEND);
110 }
111 else {
112 # Вызов производится по URL
113 $ENV{PATH_INFO} = "/".$ENV{PATH_INFO} unless $ENV{PATH_INFO} =~ m@^/@;
114 my ($skip, $training, $host, $user) = split /\//,$ENV{PATH_INFO},4;
116 if (!$host || $host eq "index") {
117 # Нам неизвестен курс или явно указан просмотр индекса
118 # Просматриваем его
120 my $t = load_training($training);
121 $training ||= "current";
123 my $prefix = "/cgi-bin/l3/$training/";
124 my $suffix = "";
125 if ($is_not_cgi) {
126 $prefix = "";
127 $suffix = ".html";
128 }
129 my $path = $to_file;
130 $path = "" unless $path =~ s@/[^/]*$@@;
132 $print .= "<html>\n";
133 $print .= "<head>\n";
134 $print .= "<title>Журналы лабораторных работ</title>\n";
135 $print .= "<meta content='text/html; charset=utf-8' http-equiv='Content-Type' />";
136 $print .= "<link rel='stylesheet' href='".$l3config::Config{frontend_css}."' type='text/css'/>\n";
137 $print .= "</head>\n";
138 $print .= "<body>\n";
139 $print .= "<h1>Журналы лабораторных работ</h1>\n";
140 $print .= "<table>\n";
141 $print .= "<tr class='table_header'>\n";
142 $print .= "<td>"."Имя"."</td>";
143 $print .= "<td>Хост</td>";
144 $print .= "<td colspan='3'>Пользователь</td>";
145 $print .= "</tr>\n";
146 for my $host (sort keys %{$t->{host}}) {
149 $print .= "<tr>\n";
150 my $h = $t->{host}->{$host};
151 $print .= "<td>".$h->{firstname}." ".$h->{surname}."</td>";
152 $print .= "<td>$host</td>";
153 $print .= "<td><a href='".$prefix.$host."/root$suffix'>root</a></td>";
154 $print .= "<td><a href='".$prefix.$host."/".$h->{user}."$suffix'>".$h->{user}."</a></td>";
155 $print .= "<td><a href='".$prefix.$host."$suffix'>все</a></td>" if not $is_not_cgi;
156 $print .= "</td>\n";
157 $print .= "</tr>\n";
159 if ($is_not_cgi) {
160 # Это грязный хак
161 # Если мы чувствуем, что нас вызывают для генерения индексного файла,
162 # нам нужно создать и файлы, на которые он указывает
163 # Лучше было бы это сделать хотя бы через вызов функций
164 # Такой же хак чуть ниже
165 mkdir("$path/$host");
166 system("$0 --cgi2file $path/$prefix$host/root$suffix ".
167 "--cgi_path_info ".$training."/".$host."/root ".
168 "--frontend_css ../$l3config::Config{frontend_css}");
169 system("$0 --cgi2file $path/$prefix$host/".$h->{user}."$suffix ".
170 "--cgi_path_info ".$training."/".$host."/".$h->{user}." ".
171 "--frontend_css ../$l3config::Config{frontend_css}");
172 # system("$0 --cgi2file $path/$prefix$host$suffix ".
173 # "--cgi_path_info ".$training."/".$host." ".
174 # "--frontend_css ../$l3config::Config{frontend_css}");
175 }
177 }
178 # Такой же хак был чуть выше
179 if ($is_not_cgi) {
180 # $print .= "<td><a href='".$prefix."instructor.".$suffix."'>все</a></td>";
181 # system("$0 --cgi2file $path/$prefix$host$suffix"
182 # . " --cgi_path_info ".$training."?login_from=192.168.15.254".$host
183 # . " --frontend_css ../$l3config::Config{frontend_css}");
184 }
185 else {
186 $print .= "<tr>\n";
187 $print .= "<td>"."Инструктор"."</td>";
188 $print .= "<td>from linux.nt</td>";
189 $print .= "<td/>";
190 $print .= "<td/>";
191 $print .= "<td><a href='$training"."?login_from=192.168.15.254'>все</a></td>";
192 }
193 $print .= "</tr>\n";
194 $print .= "</table>\n";
195 $print .= "</body>\n";
196 $print .= "</html>\n";
197 }
198 else {
200 $l3config::Config{"class"}=$training if $training ne 'current';
201 $XMLTraining = load_training;
203 my @args=(
204 "--output" => "-",
205 "--show_comments" => "no",
206 "--course-center" => $XMLTraining->{center},
207 "--course-trainer" => $XMLTraining->{instructor}->{firstname}." ".$XMLTraining->{instructor}->{surname},
208 "--course-student" => $XMLTraining->{host}->{$host}->{firstname}." ".$XMLTraining->{host}->{$host}->{surname},
209 "--course-code" => $XMLTraining->{course},
210 "--course-date" => $XMLTraining->{date},
211 "--encoding" => $XMLTraining->{host}->{$host}->{charset},
212 );
213 if ($training ne 'current') {
214 push @args, ("--backend_datafile" => "/var/lilalo/lablogs-xml/$training/$host/$user.xml");
215 } else {
216 $filter{hostname} = $host if $host;
217 $filter{user} = $user if $user;
218 push @args, ("--filter" => join ("&", (map("$_=$filter{$_}", keys %filter))));
219 ##push @args, ("--filter" => "hostname=".$host."&user=".$user);
220 }
222 open (FRONTEND, "./l3-frontend --frontend_css $l3config::Config{frontend_css} ".join(" ",map("\"$_\"",@args))." |");
223 binmode FRONTEND, ":utf8";
224 while (<FRONTEND>) {
225 $print .= $_;
226 }
227 close(FRONTEND);
228 }
229 }
231 # Если задана переменная окружения l3_to_file,
232 # печатаем в файл с соответствующим именем,
233 # в противном случае -- выводим на стандартный поток вывода
234 if (not $to_file) {
235 binmode STDOUT, ":utf8";
236 print header(-charset => "utf-8");
237 print $print;
238 }
239 else {
240 open(FILE, ">:utf8", $to_file)
241 or die "Can't open $to_file for writing";
242 print FILE $print;
243 close(FILE);
244 }