#!/usr/bin/perl use strict; use CGI qw(:standard); use XML::Simple; use utf8; BEGIN { chdir("/etc/lilalo"); require l3config; l3config::init_config(); }; my %filter; for my $key (qw(login_from local_session_id action)) { $filter{$key} = param($key) if param($key); $filter{$key} = $ENV{$key} if defined $ENV{$key}; } my %Trainings; my $XMLTraining; my %Machines; my $print = ""; $l3config::Config{cgi2file} = $ENV{cgi2file} if defined($ENV{cgi2file}); $ENV{PATH_INFO} = $l3config::Config{cgi_path_info} if $l3config::Config{cgi_path_info}; my $is_not_cgi=""; $is_not_cgi = "1" if $l3config::Config{cgi2file}; # Пока что мы считаем, что вызов выполняется через CGI, # если не установлена переменная cgi2file # и не передан такой аргумент командной строки. # Это неверно, но как временное решение - пойдёт my $to_file = $l3config::Config{cgi2file}; $l3config::Config{frontend_css} =~ s@^/@@ if $is_not_cgi; sub load_training { my $training_file; $training_file = $_[0] || $l3config::Config{"classfile"} || $l3config::Config{"path_classes"}.$l3config::Config{"class"}.$l3config::Config{"class_suffix"}; my $XMLTraining = XMLin($training_file , ForceArray => [ 'student' ] ) or die "Can't open file of the training ",$training_file,"\n"; my $course_file = $l3config::Config{"courses_path"}.$XMLTraining->{"course"}.".xml"; my $XMLCourse; if (eval{$XMLCourse = XMLin($course_file)}) { $XMLTraining->{"course-name"} = $XMLCourse->{"fullname"}; } for my $student (@{$XMLTraining->{"student"}}) { $XMLTraining->{host}->{$student->{"host"}}=$student; } return $XMLTraining; } if ($ENV{PATH_INFO} eq "/index") { # Показываем индекс курсов my @training_files = glob($l3config::Config{"path_classes"}."/2*".$l3config::Config{"class_suffix"}); if (@training_files) { for my $training_file (@training_files) { $training_file =~ s@.*/@@; $training_file =~ s@$l3config::Config{"class_suffix"}$@@; my $training = load_training($training_file); $Trainings{$training->{"date"}}=$training; } $print .= "\n"; $print .= "\n"; $print .= "Журналы лабораторных работ\n"; $print .= "\n"; $print .= "\n"; $print .= "\n"; for my $tdate (reverse sort keys %Trainings) { my $t = $Trainings{$tdate}; $print .= ""; $print .= ""; $print .= ""; $print .= "\n"; for my $host (sort keys %{$t->{host}}) { my %row; my @row_keys = qw{ name email company host root user }; my $h = $t->{host}->{$host}; $row{name}= $h->{firstname}." ".$h->{surname}; $row{email}= $h->{email}; $row{company} = $h->{company}; $row{host} = "$host"; $row{root} = "root "; $row{user} = "".$h->{user}." "; $print .= "".join("", map("", @row_keys)).""; } } $print .= "
".$t->{date}."".$t->{course}."
$row{$_}
\n"; $print .= "\n"; $print .= "\n"; } else { $print .= "No training-files found
\n"; $print .= "Template to load files: ".$l3config::Config{"path_classes"}."*".$l3config::Config{"class_suffix"}."\n" } } elsif ($ENV{PATH_INFO} eq "/current") { open (FRONTEND, "./l3-frontend --output - --show_comments no --frontend_css $l3config::Config{frontend_css}|"); binmode FRONTEND, ":utf8"; while () { $print .= $_; } close(FRONTEND); } else { # Вызов производится по URL $ENV{PATH_INFO} = "/".$ENV{PATH_INFO} unless $ENV{PATH_INFO} =~ m@^/@; #my ($skip, $training, $host, $user) = split /\//,$ENV{PATH_INFO},4; my ($skip, $training, $host, $user) = split /\//,$ENV{PATH_INFO},4; if (!$host || $host eq "index") { # Нам неизвестен курс или явно указан просмотр индекса # Просматриваем его my $t; if ( -e "/var/lilalo/lablogs-xml/$training/class.xml" ) { $t = load_training("/var/lilalo/lablogs-xml/$training/class.xml"); } else { $l3config::Config{class}=$training; $t = load_training; } $training ||= "current"; my $prefix = "/cgi-bin/l3/$training/"; my $suffix = ""; if ($is_not_cgi) { $prefix = ""; $suffix = ".html"; } my $path = $to_file; $path = "" unless $path =~ s@/[^/]*$@@; $print .= "\n"; $print .= "\n"; $print .= "Журналы лабораторных работ\n"; $print .= ""; $print .= "\n"; $print .= "\n"; $print .= "\n"; $print .= "

Журналы лабораторных работ

\n"; $print .= "\n"; $print .= "\n"; $print .= ""; $print .= ""; $print .= ""; $print .= "\n"; for my $host (sort keys %{$t->{host}}) { $print .= "\n"; my $h = $t->{host}->{$host}; $print .= ""; $print .= ""; $print .= ""; $print .= ""; $print .= "" if not $is_not_cgi; $print .= "\n"; $print .= "\n"; if ($is_not_cgi) { # Это грязный хак # Если мы чувствуем, что нас вызывают для генерения индексного файла, # нам нужно создать и файлы, на которые он указывает # Лучше было бы это сделать хотя бы через вызов функций # Такой же хак чуть ниже mkdir("$path/$host"); system("$0 --cgi2file $path/$prefix$host/root$suffix ". "--cgi_path_info ".$training."/".$host."/root ". "--frontend_css ../$l3config::Config{frontend_css}"); system("$0 --cgi2file $path/$prefix$host/".$h->{user}."$suffix ". "--cgi_path_info ".$training."/".$host."/".$h->{user}." ". "--frontend_css ../$l3config::Config{frontend_css}"); # system("$0 --cgi2file $path/$prefix$host$suffix ". # "--cgi_path_info ".$training."/".$host." ". # "--frontend_css ../$l3config::Config{frontend_css}"); } } # Такой же хак был чуть выше if ($is_not_cgi) { # $print .= ""; # system("$0 --cgi2file $path/$prefix$host$suffix" # . " --cgi_path_info ".$training."?login_from=192.168.15.254".$host # . " --frontend_css ../$l3config::Config{frontend_css}"); } else { $print .= "\n"; $print .= ""; $print .= ""; $print .= ""; } $print .= "\n"; $print .= "
"."Имя"."ХостПользователь
".$h->{firstname}." ".$h->{surname}."$hostroot".$h->{user}."все
все
"."Инструктор"."from linux.nt"; $print .= ""; $print .= "все
\n"; $print .= "\n"; $print .= "\n"; } else { my @args=( "--output" => "-", "--show_comments" => "no", ); $l3config::Config{"class"}=$training if $training ne 'current'; if ( -e "/var/lilalo/lablogs-xml/$training/class.xml" ) { $XMLTraining = load_training("/var/lilalo/lablogs-xml/$training/class.xml"); } else { $XMLTraining = load_training; } if ($XMLTraining) { my @args=( "--course-center" => $XMLTraining->{center}, "--course-trainer" => $XMLTraining->{instructor}->{firstname}." ".$XMLTraining->{instructor}->{surname}, "--course-student" => $XMLTraining->{host}->{$host}->{firstname}." ".$XMLTraining->{host}->{$host}->{surname}, "--course-code" => $XMLTraining->{course}, "--course-date" => $XMLTraining->{date}, "--encoding" => $XMLTraining->{host}->{$host}->{charset}, ); if ($XMLTraining->{"course-name"}) { push @args, ("--course-name" => $XMLTraining->{"course-name"}); }; } if ($training ne 'current') { push @args, ("--backend_datafile" => "/var/lilalo/lablogs-xml/$training/$host/$user.xml"); } else { $filter{hostname} = $host if $host; $filter{user} = $user if $user; } push @args, ("--filter" => join ("&", (map("$_=$filter{$_}", keys %filter)))); #$print .= "./l3-frontend --frontend_css $l3config::Config{frontend_css} ".join(" ",map("\"$_\"",@args))." |
"; open (FRONTEND, "./l3-frontend --frontend_css $l3config::Config{frontend_css} ".join(" ",map("\"$_\"",@args))." |"); binmode FRONTEND, ":utf8"; while () { $print .= $_; } close(FRONTEND); } } # Если задана переменная окружения l3_to_file, # печатаем в файл с соответствующим именем, # в противном случае -- выводим на стандартный поток вывода if (not $to_file) { binmode STDOUT, ":utf8"; print header(-charset => "utf-8"); print $print; } else { open(FILE, ">:utf8", $to_file) or die "Can't open $to_file for writing"; print FILE $print; close(FILE); }