lilalo
view l3-cgi @ 54:f1ba68510fed
Обработка элемента ipaddress в элементе host
author | devi |
---|---|
date | Thu Dec 22 08:30:38 2005 +0200 (2005-12-22) |
parents | 568bab7090fc |
children | 187b6636a3be |
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"}."/*".$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 $print .= "<html>\n";
124 $print .= "<head>\n";
125 $print .= "<title>Журналы лабораторных работ</title>\n";
126 $print .= "<link rel='stylesheet' href='".$l3config::Config{frontend_css}."' type='text/css'/>\n";
127 $print .= "</head>\n";
128 $print .= "<body>\n";
129 $print .= "<h1>Журналы лабораторных работ</h1>\n";
130 $print .= "<table>\n";
131 $print .= "<tr class='table_header'>\n";
132 $print .= "<td>"."Имя"."</td>";
133 $print .= "<td>Хост</td>";
134 $print .= "<td colspan='3'>Пользователь</td>";
135 $print .= "</tr>\n";
136 for my $host (sort keys %{$t->{host}}) {
138 my $prefix = "/cgi-bin/l3/$training/";
139 my $suffix = "";
140 if ($is_not_cgi) {
141 $prefix = "";
142 $suffix = ".html";
143 }
145 $print .= "<tr>\n";
146 my $h = $t->{host}->{$host};
147 $print .= "<td>".$h->{firstname}." ".$h->{surname}."</td>";
148 $print .= "<td>$host</td>";
149 $print .= "<td><a href='".$prefix.$host."/root$suffix'>root</a></td>";
150 $print .= "<td><a href='".$prefix.$host."/".$h->{user}."$suffix'>".$h->{user}."</a></td>";
151 $print .= "<td><a href='".$prefix.$host."$suffix'>все</a></td>";
152 $print .= "</td>\n";
153 $print .= "</tr>\n";
155 if ($is_not_cgi) {
156 # Это грязный хак
157 # Если мы чувствуем, что нас вызывают для генерения индексного файла,
158 # нам нужно создать и файлы, на которые он указывает
159 # Лучше было бы это сделать хотя бы через вызов функций
160 my $path = $to_file;
161 $path = "" unless $path =~ s@/[^/]*$@@;
162 mkdir("$path/$host");
163 system("$0 --cgi2file $path/$prefix$host/root$suffix ".
164 "--cgi_path_info ".$training."/".$host."/root ".
165 "--frontend_css ../$l3config::Config{frontend_css}");
166 system("$0 --cgi2file $path/$prefix$host/".$h->{user}."$suffix ".
167 "--cgi_path_info ".$training."/".$host."/".$h->{user}." ".
168 "--frontend_css ../$l3config::Config{frontend_css}");
169 }
171 }
172 $print .= "<tr>\n";
173 $print .= "<td>"."Инструктор"."</td>";
174 $print .= "<td>from linux.nt</td>";
175 $print .= "<td/>";
176 $print .= "<td/>";
177 $print .= "<td><a href='/cgi-bin/l3/current"."?login_from=192.168.15.254'>все</a></td>";
178 $print .= "</tr>\n";
179 $print .= "</table>\n";
180 $print .= "</body>\n";
181 $print .= "</html>\n";
182 }
183 else {
185 $l3config::Config{"class"}=$training if $training ne 'current';
186 $XMLTraining = load_training;
188 my @args=(
189 "--output" => "-",
190 "--show_comments" => "no",
191 "--course-center" => $XMLTraining->{center},
192 "--course-trainer" => $XMLTraining->{instructor}->{firstname}." ".$XMLTraining->{instructor}->{surname},
193 "--course-student" => $XMLTraining->{host}->{$host}->{firstname}." ".$XMLTraining->{host}->{$host}->{surname},
194 "--course-code" => $XMLTraining->{course},
195 "--course-date" => $XMLTraining->{date},
196 "--encoding" => $XMLTraining->{host}->{$host}->{charset},
197 );
198 if ($training ne 'current') {
199 push @args, ("--backend_datafile" => "/var/lilalo/lablogs-xml/$training/$host/$user.xml");
200 } else {
201 $filter{hostname} = $host if $host;
202 $filter{user} = $user if $user;
203 push @args, ("--filter" => join ("&", (map("$_=$filter{$_}", keys %filter))));
204 ##push @args, ("--filter" => "hostname=".$host."&user=".$user);
205 }
207 open (FRONTEND, "./l3-frontend --frontend_css $l3config::Config{frontend_css} ".join(" ",map("\"$_\"",@args))." |");
208 binmode FRONTEND, ":utf8";
209 while (<FRONTEND>) {
210 $print .= $_;
211 }
212 close(FRONTEND);
213 }
214 }
216 # Если задана переменная окружения l3_to_file,
217 # печатаем в файл с соответствующим именем,
218 # в противном случае -- выводим на стандартный поток вывода
219 if (not $to_file) {
220 binmode STDOUT, ":utf8";
221 print header(-charset => "utf-8");
222 print $print;
223 }
224 else {
225 open(FILE, ">:utf8", $to_file)
226 or die "Can't open $to_file for writing";
227 print FILE $print;
228 close(FILE);
229 }