lilalo

annotate l3-cgi @ 75:58ea78973bbb

Вывод таблицы с командами переведён на div'ы.
Наведён относительный порядок с таблицами стилей.
Если прошло > 1 часа, временной интервал выводится в часах
author devi
date Fri Feb 10 23:35:24 2006 +0200 (2006-02-10)
parents 440d12c122d5
children d28dda8ea18f
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@61 60 my @training_files = glob($l3config::Config{"path_classes"}."/2*".$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@71 87 $print .= " ".$h->{company}." ";
devi@42 88 $print .= "<a href='/cgi-bin/l3/".$tdate."/".$host."/root'>root</a> ";
devi@42 89 $print .= "<a href='/cgi-bin/l3/".$tdate."/".$host."/".$h->{user}."'>".$h->{user}."</a> ";
devi@42 90 $print .= "\n";
devi@30 91 }
devi@42 92 $print .= "</pre><td>";
devi@42 93 $print .= "</tr>\n";
devi@30 94 }
devi@42 95 $print .= "</table>\n";
devi@42 96 $print .= "</body>\n";
devi@42 97 $print .= "</html>\n";
devi@30 98 }
devi@30 99 else {
devi@42 100 $print .= "No training-files found<br/>\n";
devi@42 101 $print .= "Template to load files: ".$l3config::Config{"path_classes"}."*".$l3config::Config{"class_suffix"}."\n"
devi@30 102 }
devi@30 103 }
devi@31 104 elsif ($ENV{PATH_INFO} eq "/current") {
devi@48 105 open (FRONTEND, "./l3-frontend --output - --show_comments no --frontend_css $l3config::Config{frontend_css}|");
devi@48 106 binmode FRONTEND, ":utf8";
devi@31 107 while (<FRONTEND>) {
devi@43 108 $print .= $_;
devi@31 109 }
devi@31 110 close(FRONTEND);
devi@31 111 }
devi@30 112 else {
devi@30 113 # Вызов производится по URL
devi@48 114 $ENV{PATH_INFO} = "/".$ENV{PATH_INFO} unless $ENV{PATH_INFO} =~ m@^/@;
devi@48 115 my ($skip, $training, $host, $user) = split /\//,$ENV{PATH_INFO},4;
devi@29 116
devi@48 117 if (!$host || $host eq "index") {
devi@48 118 # Нам неизвестен курс или явно указан просмотр индекса
devi@48 119 # Просматриваем его
devi@30 120
devi@48 121 my $t = load_training($training);
devi@48 122 $training ||= "current";
devi@48 123
devi@57 124 my $prefix = "/cgi-bin/l3/$training/";
devi@57 125 my $suffix = "";
devi@57 126 if ($is_not_cgi) {
devi@57 127 $prefix = "";
devi@57 128 $suffix = ".html";
devi@57 129 }
devi@57 130 my $path = $to_file;
devi@57 131 $path = "" unless $path =~ s@/[^/]*$@@;
devi@57 132
devi@48 133 $print .= "<html>\n";
devi@48 134 $print .= "<head>\n";
devi@48 135 $print .= "<title>Журналы лабораторных работ</title>\n";
devi@57 136 $print .= "<meta content='text/html; charset=utf-8' http-equiv='Content-Type' />";
devi@48 137 $print .= "<link rel='stylesheet' href='".$l3config::Config{frontend_css}."' type='text/css'/>\n";
devi@48 138 $print .= "</head>\n";
devi@48 139 $print .= "<body>\n";
devi@48 140 $print .= "<h1>Журналы лабораторных работ</h1>\n";
devi@48 141 $print .= "<table>\n";
devi@48 142 $print .= "<tr class='table_header'>\n";
devi@48 143 $print .= "<td>"."Имя"."</td>";
devi@48 144 $print .= "<td>Хост</td>";
devi@48 145 $print .= "<td colspan='3'>Пользователь</td>";
devi@48 146 $print .= "</tr>\n";
devi@48 147 for my $host (sort keys %{$t->{host}}) {
devi@48 148
devi@48 149
devi@48 150 $print .= "<tr>\n";
devi@48 151 my $h = $t->{host}->{$host};
devi@48 152 $print .= "<td>".$h->{firstname}." ".$h->{surname}."</td>";
devi@48 153 $print .= "<td>$host</td>";
devi@48 154 $print .= "<td><a href='".$prefix.$host."/root$suffix'>root</a></td>";
devi@48 155 $print .= "<td><a href='".$prefix.$host."/".$h->{user}."$suffix'>".$h->{user}."</a></td>";
devi@57 156 $print .= "<td><a href='".$prefix.$host."$suffix'>все</a></td>" if not $is_not_cgi;
devi@48 157 $print .= "</td>\n";
devi@48 158 $print .= "</tr>\n";
devi@48 159
devi@48 160 if ($is_not_cgi) {
devi@48 161 # Это грязный хак
devi@48 162 # Если мы чувствуем, что нас вызывают для генерения индексного файла,
devi@48 163 # нам нужно создать и файлы, на которые он указывает
devi@48 164 # Лучше было бы это сделать хотя бы через вызов функций
devi@57 165 # Такой же хак чуть ниже
devi@48 166 mkdir("$path/$host");
devi@48 167 system("$0 --cgi2file $path/$prefix$host/root$suffix ".
devi@48 168 "--cgi_path_info ".$training."/".$host."/root ".
devi@48 169 "--frontend_css ../$l3config::Config{frontend_css}");
devi@48 170 system("$0 --cgi2file $path/$prefix$host/".$h->{user}."$suffix ".
devi@48 171 "--cgi_path_info ".$training."/".$host."/".$h->{user}." ".
devi@48 172 "--frontend_css ../$l3config::Config{frontend_css}");
devi@57 173 # system("$0 --cgi2file $path/$prefix$host$suffix ".
devi@57 174 # "--cgi_path_info ".$training."/".$host." ".
devi@57 175 # "--frontend_css ../$l3config::Config{frontend_css}");
devi@48 176 }
devi@48 177
devi@48 178 }
devi@57 179 # Такой же хак был чуть выше
devi@57 180 if ($is_not_cgi) {
devi@57 181 # $print .= "<td><a href='".$prefix."instructor.".$suffix."'>все</a></td>";
devi@57 182 # system("$0 --cgi2file $path/$prefix$host$suffix"
devi@57 183 # . " --cgi_path_info ".$training."?login_from=192.168.15.254".$host
devi@57 184 # . " --frontend_css ../$l3config::Config{frontend_css}");
devi@57 185 }
devi@57 186 else {
devi@57 187 $print .= "<tr>\n";
devi@57 188 $print .= "<td>"."Инструктор"."</td>";
devi@57 189 $print .= "<td>from linux.nt</td>";
devi@57 190 $print .= "<td/>";
devi@57 191 $print .= "<td/>";
devi@57 192 $print .= "<td><a href='$training"."?login_from=192.168.15.254'>все</a></td>";
devi@57 193 }
devi@48 194 $print .= "</tr>\n";
devi@48 195 $print .= "</table>\n";
devi@48 196 $print .= "</body>\n";
devi@48 197 $print .= "</html>\n";
devi@32 198 }
devi@48 199 else {
devi@30 200
devi@48 201 $l3config::Config{"class"}=$training if $training ne 'current';
devi@48 202 $XMLTraining = load_training;
devi@48 203
devi@48 204 my @args=(
devi@48 205 "--output" => "-",
devi@48 206 "--show_comments" => "no",
devi@48 207 "--course-center" => $XMLTraining->{center},
devi@48 208 "--course-trainer" => $XMLTraining->{instructor}->{firstname}." ".$XMLTraining->{instructor}->{surname},
devi@48 209 "--course-student" => $XMLTraining->{host}->{$host}->{firstname}." ".$XMLTraining->{host}->{$host}->{surname},
devi@48 210 "--course-code" => $XMLTraining->{course},
devi@48 211 "--course-date" => $XMLTraining->{date},
devi@48 212 "--encoding" => $XMLTraining->{host}->{$host}->{charset},
devi@48 213 );
devi@48 214 if ($training ne 'current') {
devi@48 215 push @args, ("--backend_datafile" => "/var/lilalo/lablogs-xml/$training/$host/$user.xml");
devi@48 216 } else {
devi@48 217 $filter{hostname} = $host if $host;
devi@48 218 $filter{user} = $user if $user;
devi@48 219 push @args, ("--filter" => join ("&", (map("$_=$filter{$_}", keys %filter))));
devi@52 220 ##push @args, ("--filter" => "hostname=".$host."&user=".$user);
devi@48 221 }
devi@48 222
devi@48 223 open (FRONTEND, "./l3-frontend --frontend_css $l3config::Config{frontend_css} ".join(" ",map("\"$_\"",@args))." |");
devi@48 224 binmode FRONTEND, ":utf8";
devi@48 225 while (<FRONTEND>) {
devi@48 226 $print .= $_;
devi@48 227 }
devi@48 228 close(FRONTEND);
devi@30 229 }
devi@27 230 }
devi@27 231
devi@47 232 # Если задана переменная окружения l3_to_file,
devi@47 233 # печатаем в файл с соответствующим именем,
devi@47 234 # в противном случае -- выводим на стандартный поток вывода
devi@48 235 if (not $to_file) {
devi@47 236 binmode STDOUT, ":utf8";
devi@47 237 print header(-charset => "utf-8");
devi@47 238 print $print;
devi@47 239 }
devi@47 240 else {
devi@48 241 open(FILE, ">:utf8", $to_file)
devi@48 242 or die "Can't open $to_file for writing";
devi@47 243 print FILE $print;
devi@47 244 close(FILE);
devi@47 245 }
devi@47 246
devi@47 247