lilalo

annotate l3-cgi @ 48:568bab7090fc

l3-cgi:
Журналы могут быть сохранены в файл,

l3:
Отправка файлов журналов по электронной почте.
l3 do send-logs


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