lilalo

annotate l3-cgi @ 60:0b1553611bb5

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