lilalo

diff 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
line diff
     1.1 --- a/l3-cgi	Wed Nov 23 00:18:36 2005 +0200
     1.2 +++ b/l3-cgi	Sat Nov 26 16:15:52 2005 +0200
     1.3 @@ -8,6 +8,7 @@
     1.4  BEGIN {
     1.5  	chdir("/home/devi/cvs/lilalo");
     1.6  	require l3config;
     1.7 +	l3config::init_config();
     1.8  };
     1.9  
    1.10  my %filter;
    1.11 @@ -22,23 +23,29 @@
    1.12  my %Machines;
    1.13  my $print = "";
    1.14  
    1.15 +$l3config::Config{cgi2file} = $ENV{cgi2file}	if defined($ENV{cgi2file});
    1.16 +$ENV{PATH_INFO} = $l3config::Config{cgi_path_info} if $l3config::Config{cgi_path_info};
    1.17 +
    1.18  my $is_not_cgi="";
    1.19 -$is_not_cgi = "1" if defined($ENV{l3_to_file});
    1.20 +$is_not_cgi = "1" if $l3config::Config{cgi2file};
    1.21  # Пока что мы считаем, что вызов выполняется через CGI, 
    1.22 -# если не установлена переменная l3_to_file 
    1.23 +# если не установлена переменная cgi2file 
    1.24 +# и не передан такой аргумент командной строки.
    1.25  # Это неверно, но как временное решение - пойдёт
    1.26 +my $to_file = $l3config::Config{cgi2file};
    1.27  
    1.28  $l3config::Config{frontend_css} =~ s@^/@@ if $is_not_cgi;
    1.29  
    1.30  sub load_training
    1.31  {
    1.32 -        my $classfile = 
    1.33 -	$_[0]||
    1.34 -        $l3config::Config{"classfile"} || 
    1.35 -        $l3config::Config{"path_classes"}."/".$l3config::Config{"class"}.$l3config::Config{"class_suffix"};
    1.36 +        my $training_file;
    1.37 +	
    1.38 +	$training_file = $l3config::Config{"path_classes"}.$_[0].$l3config::Config{"class_suffix"} if $_[0];
    1.39 +        $training_file ||= $l3config::Config{"classfile"} || 
    1.40 +        $l3config::Config{"path_classes"}.$l3config::Config{"class"}.$l3config::Config{"class_suffix"};
    1.41  
    1.42 -        my $XMLTraining = XMLin($classfile , ForceArray => [ 'student' ] )  
    1.43 -                or die "Can't open file of the training ",$classfile,"\n";
    1.44 +        my $XMLTraining = XMLin($training_file , ForceArray => [ 'student' ] )  
    1.45 +                or die "Can't open file of the training ",$training_file,"\n";
    1.46  
    1.47          for my $student (@{$XMLTraining->{"student"}}) {
    1.48  		$XMLTraining->{host}->{$student->{"host"}}=$student;
    1.49 @@ -53,6 +60,8 @@
    1.50  	my @training_files = glob($l3config::Config{"path_classes"}."/*".$l3config::Config{"class_suffix"});
    1.51  	if (@training_files) {
    1.52  		for my $training_file (@training_files) {
    1.53 +			$training_file =~ s@.*/@@;
    1.54 +			$training_file =~ s@$l3config::Config{"class_suffix"}$@@;
    1.55  			my $training = load_training($training_file);
    1.56  			$Trainings{$training->{"date"}}=$training;
    1.57  		}
    1.58 @@ -91,54 +100,9 @@
    1.59  		$print .= "Template to load files: ".$l3config::Config{"path_classes"}."*".$l3config::Config{"class_suffix"}."\n"
    1.60  	}
    1.61  }
    1.62 -elsif ($ENV{PATH_INFO} eq "/current/index" || $ENV{PATH_INFO} eq "") {
    1.63 -	my $t = load_training();
    1.64 -
    1.65 -	$print .= "<html>\n";
    1.66 -	$print .= "<head>\n";
    1.67 -	$print .= "<title>Журналы лабораторных работ</title>\n";
    1.68 -	$print .= "<link rel='stylesheet' href='".$l3config::Config{frontend_css}."' type='text/css'/>\n";
    1.69 -	$print .= "</head>\n";
    1.70 -	$print .= "<body>\n";
    1.71 -	$print .= "<h1>Журналы лабораторных работ</h1>\n";
    1.72 -	$print .= "<table>\n";
    1.73 -	$print .= "<tr class='table_header'>\n";
    1.74 -	$print .= "<td>"."Имя"."</td>";
    1.75 -	$print .= "<td>Хост</td>";
    1.76 -	$print .= "<td colspan='3'>Пользователь</td>";
    1.77 -	$print .= "</tr>\n";
    1.78 -	for my $host (sort keys %{$t->{host}}) {
    1.79 -
    1.80 -		my $prefix = "/cgi-bin/l3/current/";
    1.81 -		my $suffix = "";
    1.82 -		if ($is_not_cgi) {
    1.83 -			$prefix = "";
    1.84 -			$suffix = ".html";
    1.85 -		}
    1.86 -		
    1.87 -		$print .= "<tr>\n";
    1.88 -		my $h = $t->{host}->{$host};
    1.89 -		$print .= "<td>".$h->{firstname}." ".$h->{surname}."</td>";
    1.90 -		$print .= "<td>$host</td>";
    1.91 -		$print .= "<td><a href='".$prefix.$host."/root$suffix'>root</a></td>";
    1.92 -		$print .= "<td><a href='".$prefix.$host."/".$h->{user}."$suffix'>".$h->{user}."</a></td>";
    1.93 -		$print .= "<td><a href='".$prefix.$host."$suffix'>все</a></td>";
    1.94 -		$print .= "</td>\n";
    1.95 -		$print .= "</tr>\n";
    1.96 -	}
    1.97 -	$print .= "<tr>\n";
    1.98 -	$print .= "<td>"."Инструктор"."</td>";
    1.99 -	$print .= "<td>from linux.nt</td>";
   1.100 -	$print .= "<td/>";
   1.101 -	$print .= "<td/>";
   1.102 -	$print .= "<td><a href='/cgi-bin/l3/current"."?login_from=192.168.15.254'>все</a></td>";
   1.103 -	$print .= "</tr>\n";
   1.104 -	$print .= "</table>\n";
   1.105 -	$print .= "</body>\n";
   1.106 -	$print .= "</html>\n";
   1.107 -}
   1.108  elsif ($ENV{PATH_INFO} eq "/current") {
   1.109 -	open (FRONTEND, "./l3-frontend --output - --show_comments no |");
   1.110 +	open (FRONTEND, "./l3-frontend --output - --show_comments no --frontend_css $l3config::Config{frontend_css}|");
   1.111 +	binmode FRONTEND, ":utf8";
   1.112  	while (<FRONTEND>) {
   1.113  		$print .= $_;
   1.114  	}
   1.115 @@ -146,48 +110,120 @@
   1.116  }
   1.117  else {
   1.118  	# Вызов производится по URL
   1.119 -	my ($skip, $course, $host, $user) = split /\//,$ENV{PATH_INFO},4;
   1.120 +	$ENV{PATH_INFO} = "/".$ENV{PATH_INFO} unless $ENV{PATH_INFO} =~ m@^/@;
   1.121 +	my ($skip, $training, $host, $user) = split /\//,$ENV{PATH_INFO},4;
   1.122  
   1.123 -	$l3config::Config{"class"}=$course if $course ne 'current';
   1.124 -	$XMLTraining = load_training;
   1.125 +	if (!$host || $host eq "index") {
   1.126 +		# Нам неизвестен курс или явно указан просмотр индекса
   1.127 +		# Просматриваем его
   1.128  
   1.129 -	my @args=(
   1.130 -		"--output" 		=>	"-",
   1.131 -		"--show_comments" 	=>	"no",
   1.132 -		"--course-center"	=>	$XMLTraining->{center},
   1.133 -		"--course-trainer"	=>	$XMLTraining->{instructor}->{firstname}." ".$XMLTraining->{instructor}->{surname},
   1.134 -		"--course-student"	=>	$XMLTraining->{host}->{$host}->{firstname}." ".$XMLTraining->{host}->{$host}->{surname},
   1.135 -		"--course-code"		=>	$XMLTraining->{course},
   1.136 -		"--course-date"		=>	$XMLTraining->{date},
   1.137 -		"--encoding"		=>	$XMLTraining->{host}->{$host}->{charset},
   1.138 -	);
   1.139 -	if ($course ne 'current') {
   1.140 -		push @args, 	("--backend_datafile"	=>	"/var/lilalo/lablogs-xml/$course/$host/$user.xml");
   1.141 -	} else {
   1.142 -		$filter{hostname} = $host if $host;
   1.143 -		$filter{user} = $user if $user;
   1.144 -		push @args, ("--filter" => join ("&", (map("$_=$filter{$_}", keys %filter))));
   1.145 -		#push @args, ("--filter" => "hostname=".$host."&user=".$user);
   1.146 +		my $t = load_training($training);
   1.147 +		$training ||= "current";
   1.148 +
   1.149 +		$print .= "<html>\n";
   1.150 +		$print .= "<head>\n";
   1.151 +		$print .= "<title>Журналы лабораторных работ</title>\n";
   1.152 +		$print .= "<link rel='stylesheet' href='".$l3config::Config{frontend_css}."' type='text/css'/>\n";
   1.153 +		$print .= "</head>\n";
   1.154 +		$print .= "<body>\n";
   1.155 +		$print .= "<h1>Журналы лабораторных работ</h1>\n";
   1.156 +		$print .= "<table>\n";
   1.157 +		$print .= "<tr class='table_header'>\n";
   1.158 +		$print .= "<td>"."Имя"."</td>";
   1.159 +		$print .= "<td>Хост</td>";
   1.160 +		$print .= "<td colspan='3'>Пользователь</td>";
   1.161 +		$print .= "</tr>\n";
   1.162 +		for my $host (sort keys %{$t->{host}}) {
   1.163 +
   1.164 +			my $prefix = "/cgi-bin/l3/$training/";
   1.165 +			my $suffix = "";
   1.166 +			if ($is_not_cgi) {
   1.167 +				$prefix = "";
   1.168 +				$suffix = ".html";
   1.169 +			}
   1.170 +			
   1.171 +			$print .= "<tr>\n";
   1.172 +			my $h = $t->{host}->{$host};
   1.173 +			$print .= "<td>".$h->{firstname}." ".$h->{surname}."</td>";
   1.174 +			$print .= "<td>$host</td>";
   1.175 +			$print .= "<td><a href='".$prefix.$host."/root$suffix'>root</a></td>";
   1.176 +			$print .= "<td><a href='".$prefix.$host."/".$h->{user}."$suffix'>".$h->{user}."</a></td>";
   1.177 +			$print .= "<td><a href='".$prefix.$host."$suffix'>все</a></td>";
   1.178 +			$print .= "</td>\n";
   1.179 +			$print .= "</tr>\n";
   1.180 +
   1.181 +			if ($is_not_cgi) {
   1.182 +				# Это грязный хак
   1.183 +				# Если мы чувствуем, что нас вызывают для генерения индексного файла,
   1.184 +				# нам нужно создать и файлы, на которые он указывает
   1.185 +				# Лучше было бы это сделать хотя бы через вызов функций
   1.186 +				my $path = $to_file;
   1.187 +				$path = "" unless $path =~ s@/[^/]*$@@;
   1.188 +				mkdir("$path/$host");
   1.189 +				system("$0 --cgi2file $path/$prefix$host/root$suffix  ".
   1.190 +					  "--cgi_path_info ".$training."/".$host."/root ".
   1.191 +					  "--frontend_css ../$l3config::Config{frontend_css}");
   1.192 +				system("$0 --cgi2file $path/$prefix$host/".$h->{user}."$suffix ".
   1.193 +					  "--cgi_path_info ".$training."/".$host."/".$h->{user}." ".
   1.194 +					  "--frontend_css ../$l3config::Config{frontend_css}");
   1.195 +			}
   1.196 +			
   1.197 +		}
   1.198 +		$print .= "<tr>\n";
   1.199 +		$print .= "<td>"."Инструктор"."</td>";
   1.200 +		$print .= "<td>from linux.nt</td>";
   1.201 +		$print .= "<td/>";
   1.202 +		$print .= "<td/>";
   1.203 +		$print .= "<td><a href='/cgi-bin/l3/current"."?login_from=192.168.15.254'>все</a></td>";
   1.204 +		$print .= "</tr>\n";
   1.205 +		$print .= "</table>\n";
   1.206 +		$print .= "</body>\n";
   1.207 +		$print .= "</html>\n";
   1.208  	}
   1.209 +	else {
   1.210  
   1.211 -	open (FRONTEND, "./l3-frontend ".join(" ",map("\"$_\"",@args))." |");
   1.212 -	while (<FRONTEND>) {
   1.213 -		$print .= $_;
   1.214 +		$l3config::Config{"class"}=$training if $training ne 'current';
   1.215 +		$XMLTraining = load_training;
   1.216 +
   1.217 +		my @args=(
   1.218 +			"--output" 		=>	"-",
   1.219 +			"--show_comments" 	=>	"no",
   1.220 +			"--course-center"	=>	$XMLTraining->{center},
   1.221 +			"--course-trainer"	=>	$XMLTraining->{instructor}->{firstname}." ".$XMLTraining->{instructor}->{surname},
   1.222 +			"--course-student"	=>	$XMLTraining->{host}->{$host}->{firstname}." ".$XMLTraining->{host}->{$host}->{surname},
   1.223 +			"--course-code"		=>	$XMLTraining->{course},
   1.224 +			"--course-date"		=>	$XMLTraining->{date},
   1.225 +			"--encoding"		=>	$XMLTraining->{host}->{$host}->{charset},
   1.226 +		);
   1.227 +		if ($training ne 'current') {
   1.228 +			push @args, 	("--backend_datafile"	=>	"/var/lilalo/lablogs-xml/$training/$host/$user.xml");
   1.229 +		} else {
   1.230 +			$filter{hostname} = $host if $host;
   1.231 +			$filter{user} = $user if $user;
   1.232 +			push @args, ("--filter" => join ("&", (map("$_=$filter{$_}", keys %filter))));
   1.233 +			#push @args, ("--filter" => "hostname=".$host."&user=".$user);
   1.234 +		}
   1.235 +
   1.236 +		open (FRONTEND, "./l3-frontend --frontend_css $l3config::Config{frontend_css} ".join(" ",map("\"$_\"",@args))." |");
   1.237 +		binmode FRONTEND, ":utf8";
   1.238 +		while (<FRONTEND>) {
   1.239 +			$print .= $_;
   1.240 +		}
   1.241 +		close(FRONTEND);
   1.242  	}
   1.243 -	close(FRONTEND);
   1.244  }
   1.245  
   1.246  # Если задана переменная окружения l3_to_file, 
   1.247  # печатаем в файл с соответствующим именем,
   1.248  # в противном случае -- выводим на стандартный поток вывода
   1.249 -if (not defined $ENV{l3_to_file}) {
   1.250 +if (not $to_file) {
   1.251    binmode STDOUT, ":utf8";
   1.252    print header(-charset => "utf-8");
   1.253    print $print;
   1.254  }
   1.255  else {
   1.256 -  open(FILE, ">:utf8", $ENV{l3_to_file})
   1.257 -    or die "Can't open $ENV{l3_to_file} for writing";
   1.258 +  open(FILE, ">:utf8", $to_file)
   1.259 +    or die "Can't open $to_file for writing";
   1.260    print FILE $print;
   1.261    close(FILE);
   1.262  }