/usr/sbin/dwww-convert is in dwww 1.12.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | #!/usr/bin/perl
# vim:ft=perl:cindent:ts=4:sts=4:sw=4:et:fdm=marker:cms=\ #\ %s
#
# dwww-convert -- convert docs to HTML if necessary
#
# Some types of files (e.g. UNIX man pages) are typically not handled by most
# webbrowser setups; we convert these to HTML. Other types (e.g. PDF files
# or PNG images) are best handled by or via the user's webbrowser.
#
# Simple usage: $0 <type> <location>
# <type> is document type: text, man, html, ps, and so on
# <location> is full pathname to original document
#
# Part of the Debian dwww package. Written by Lars Wirzenius.
# Modified by Robert Luberda.
# "@(#)dwww:$Id: dwww-convert 520 2009-01-15 20:44:47Z robert $"
use strict;
use Debian::Dwww::Utils;
use Debian::Dwww::Initialize;
use MIME::Types;
use Cwd qw(realpath);
#$|=1;
#
# Setup defaults.
#
################################################################
#
# Initialization
#
my $dwwwvars = &DwwwInitialize("/etc/dwww/dwww.conf");
my $templates_dir = "/usr/share/dwww/templates";
my $template_start = "$templates_dir/dwww-convert.start";
my $template_end = "$templates_dir/dwww-convert.end";
my $template_dir_start = "$templates_dir/dwww-convert.dir.start";
my $template_dir_end = "$templates_dir/dwww-convert.dir.end";
my $dwww_use_cache = lc($dwwwvars->{'DWWW_USE_CACHE'}) eq "yes";
my $dwww_usefileurl = lc($dwwwvars->{'DWWW_USEFILEURL'}) eq "yes";
################################################################
#
# Local dwww-convert functions
#
#
# Get Last Modification date
#
#######################################################################
#
# Builtin convertFunctions
#
sub OpenPipe() { # {{{
my $program = shift;
my $mode = shift;
my $FH = undef;
if ($mode eq "r") {
return \*STDIN unless defined $program;
$program = "$program |";
} elsif ($mode eq "w") {
return \*STDOUT unless defined $program;
$program = "| $program";
} else {
die("OpenPipe: Unknown mode: $mode\n");
}
open($FH, $program) or die "Can't open pipe for $program: $!\n";
return $FH;
} # }}}
sub ClosePipe() { # {{{
my $FH = shift;
# &fflush($FH) or die("Can't flush: $!");
return if ($FH == \*STDIN || $FH == \*STDOUT);
close($FH) or die("Cannot close filehandle: $!\n");
} # }}}
sub DecompressFile() { # {{{
my $filename = shift;
my $OUT_FH = shift;
my $add_filtr_prog = shift;
my $decompressor = undef;
my $IN_FH = shift;
if ($filename =~ /\.gz$/) {
$decompressor = "zcat \"$filename\"";
} elsif ($filename =~ /\.bz2$/) {
$decompressor = "bzcat \"$filename\"";
}
if (defined $add_filtr_prog) {
if (defined $decompressor) {
$decompressor .= " | $add_filtr_prog";
} else {
$decompressor = "$add_filtr_prog < \"$filename\"";
}
}
if (defined $decompressor) {
if (defined $OUT_FH) {
$IN_FH = &OpenPipe($decompressor, "r")
} else {
system($decompressor);
return;
}
} else {
open($IN_FH, $filename) or die("Can't open file $filename: $!\n");
$OUT_FH = \*STDOUT unless defined $OUT_FH;
}
my $s=$/;
$/=4096;
while (<$IN_FH>) {
print $OUT_FH $_;
}
$/=$s;
&ClosePipe($IN_FH);
} # }}}
#
# Create a directory listing in HTML.
#
sub IsTopLevelDir() { # {{{
my $dir = shift;
my $dwww_docpath = $dwwwvars->{'DWWW_DOCPATH'};
# file does exist, check if it match any files in @dwww_docpath
foreach my $path (@$dwww_docpath) {
if ( -d $path ) {
$path = &realpath( $path );
return 1 if ($dir eq $path);
}
}
return 0;
} # }}}
sub BuiltinDir2Html() { # {{{
my $dir = shift;
my $cacheProg = shift;
my $charset = shift;
my $f = undef;
my @filelist = ();
my @dirlist = ();
my $OUT_FH = &OpenPipe($cacheProg, "w");
my $html_dir = &HTMLEncode($dir);
my $url_dir = &URLEncode($dir);
print $OUT_FH &TemplateFile($template_dir_start,
{ 'TITLE' => "Files in $html_dir",
'MENUADD' => &IsTopLevelDir($dir) ? "" : " | <a href=\"../?type=dir\">Up one level</a> ",
'FILEURL' => $url_dir });
opendir (DIR, $dir) or die "Can't open directory $dir:$!\n";
while ($f = readdir(DIR)) {
next if $f eq ".";
next if $f eq "..";
if ( -d $dir . "/" . $f ) {
push(@dirlist, $f);
} elsif ( -f _ ) {
push(@filelist, $f);
}
}
closedir(DIR);
if ($#filelist > -1 ) {
@filelist = sort(@filelist);
my $table = &BeginTable($OUT_FH, "", 3);
foreach $f (@filelist) {
if ($dwww_usefileurl && ($f =~ /\.htm.*$/)) {
&AddToTable($OUT_FH, $table,
"<a href=\"file://localhost/" . $url_dir . '/' . &URLEncode($f) . "\">" . &HTMLEncode($f) . "</a>");
} else {
&AddToTable($OUT_FH, $table,
"<a href=\"" . &URLEncode($f) . "\">" . &HTMLEncode($f) . "</a>");
}
}
&EndTable($OUT_FH, $table);
}
if ($#dirlist > -1 ) {
@dirlist = sort(@dirlist);
my $table = &BeginTable($OUT_FH, "<FONT SIZE=\"+2\">Subdirectories:</FONT>", 3);
foreach $f (@dirlist) {
if ($dwww_usefileurl && defined (my $g = &GuessFileName("$dir/$f/index", 1 ))) {
&AddToTable($OUT_FH, $table,
"<a href=\"file://localhost" . &URLEncode($g) .
"\">" . &HTMLEncode($f) . "</a>");
} else {
&AddToTable($OUT_FH, $table,
"<a href=\"" .&URLEncode($f)
. "/?type=dir\">" . &HTMLEncode($f) . "</a>");
}
}
&EndTable($OUT_FH, $table);
}
print $OUT_FH &TemplateFile($template_dir_end, { } );
&ClosePipe($OUT_FH);
} # }}}
#
# Convert a manual page source code file to HTML.
#
sub BuiltinMan2Html() { # {{{
my $filename = shift;
my $cacheProg = shift;
my $charset = shift;
my $title = undef;
die "Internal error: invalid charset\n" unless $charset eq "UTF-8";
my $OUT_FH = &OpenPipe($cacheProg, "w");
($title = $filename) =~ s/^.*\/(.+)\.(\d[^\.\\]*)(\.gz|\.bz2)?$/$1($2)/;
print $OUT_FH &TemplateFile($template_start,
{ 'TITLE' => "$title - Man pages",
'MENUADD' => " | <a href=\"/dwww/man/\">Manual pages</a> ",
'FILEURL' => &URLEncode($filename)
});
my $dir = $filename;
$dir =~ s/\/[^\/]*$//;
chdir ("$dir/..") or die "Can't chdir!\n";
$ENV{'MAN_KEEP_FORMATTING'} = 1;
my $IN_FH = &OpenPipe("man -EUTF-8 -P/bin/cat -l \"$filename\" 2>/dev/null | dwww-txt2html --man --utf8", "r");
chdir ("/");
while (<$IN_FH>) {
print $OUT_FH $_;
}
print $OUT_FH &TemplateFile($template_end, { });
&ClosePipe($IN_FH);
&ClosePipe($OUT_FH);
} # }}}
#
# Convert plain text to HTML. This is really trivial, and buggy.
# Input from stdin.
#
sub BuiltinText2Html() { # {{{
my $filename = shift;
my $cacheProg = shift;
my $charset = shift;
my $OUT_FH = &OpenPipe($cacheProg, "w");
print $OUT_FH &TemplateFile($template_start,
{ 'TITLE' => &HTMLEncode($filename),
'MENUADD' => " | <a href=\"./?type=dir\"> Show directory contents </a> ",
'FILEURL' => &URLEncode($filename)
});
if ($charset && $charset eq "UTF-8") {
&DecompressFile($filename, $OUT_FH, "dwww-txt2html --utf8");
} else {
&DecompressFile($filename, $OUT_FH, "dwww-txt2html");
}
print $OUT_FH &TemplateFile($template_end, { }) or die "Can't print\n";
&ClosePipe($OUT_FH);
} # }}}
#
# Convert info file to HTML using info2www
#
sub BuiltinInfo2Html() { # {{{
my $filename = shift;
my $cacheProg = shift;
my $charset = shift;
my $cachePipe = defined $cacheProg ? " | $cacheProg" : "";
system("/usr/lib/cgi-bin/info2www \"$filename\"" . $cachePipe);
} # }}}
sub GuessFileName() { # {{{
my $basefile = shift;
my $is_html = shift;
foreach my $lang ( ("", ".en") ) {
foreach my $comp ( ("", ".gz", ".bz2") ) {
if ($is_html) {
foreach my $suff (".html", ".htm" ) {
my $f = "${basefile}${suff}${lang}${comp}";
return $f if (-f $f and -r $f);
}
} else {
my $f = "${basefile}${lang}${comp}";
return $f if (-f $f and -r $f);
}
}
}
return undef;
} # }}}
sub PrintHeaders() { # {{{
my $filename = shift;
my $base_name = shift;
my $mime_type = shift;
my $mime_charset= shift;
print "Content-type: $mime_type" . (defined $mime_charset ? "; charset=$mime_charset\n" : "\n");
my @stat = stat( $filename );
my $mtime = $stat[9];
print "Last modified: " . gmtime($mtime) . "\n";
print "Content-Disposition: inline; filename=\"$base_name\"\n";
print "\n";
} # }}}
################################################################
#
# Main program
#
my $uses_path_info = 1;
while ($ARGV[0] =~ m/^--(.*)$/) {
shift @ARGV;
if ($1 eq "no-path-info") { $uses_path_info = 0 }
elsif (not $1) { last; }
else {
print STDERR "Error: unknown option `$ARGV[0]'\n";
print STDERR "Usage: $0 [--no-path-info] <type> <location>\n";
exit(1);
}
}
if ( $#ARGV < 1 || $ARGV[0] eq "" || $ARGV[1] eq "" ) {
print STDERR "Error: invalid arguments\n";
print STDERR "Usage: $0 [--no-path-info] <type> <location>\n";
exit 1;
}
my $type = $ARGV[0];
my $file = $ARGV[1];
my $orig_file=$file;
# remove anchor
$file =~ s/#.*$//;
if ( ! -e $file ) {
if ( $type eq "runman" ) {
if ( $file =~ /^(.+)\/([^\/]+)$/ ) {
my $name = $1;
my $sect = $2;
my @files = &GetCommandOutput("man", "--location", "-e", $sect, $name);
$file = $files[0] unless $#files < 0;
$file =~ s/\s.*$//;
if ( $file eq "" or ! -e $file ) {
&ErrorMsg ( "404 Man page not found" ,
"Man page not found" ,
"dwww could not find the man page $name($sect)" );
}
}
} elsif ( $type eq "html" || $type eq "text/html" ) {
# A link may have referred to a .html file
# when only a .html.gz file exists. So check
# to see if alternate file exists, and use
# that one if it does
my $tmp=$file;
$tmp =~ s/(.*)\.htm.*$//; # remove suffix
if (defined ( $tmp = &GuessFileName($tmp, 1) ) ) {
$file = $tmp;
}
} elsif ( defined ( my $tmp = &GuessFileName($file) ) ) {
$file = $tmp;
}
}
# Check if we can show file
$file = &CheckAccess( $dwwwvars, $file, $orig_file );
die "Internal error" if $file eq ""; # CheckAccess should have written the error message and exited.
# Check if we should send redirect
if ( -d $file && $orig_file !~ /\/$/) {
$orig_file .= "/";
$uses_path_info = $FALSE;
}
if (!$uses_path_info && $type ne "runman") {
&RedirectToURL("/cgi-bin/dwww" . &URLEncode($orig_file) . "?type=" . &URLEncode($type));
exit(0);
}
if ( -d $file ) {
if ( $type ne "dir" ) {
$type = "dir";
if (defined (my $f = &GuessFileName( "$file/index", 1 ) ) ){
if ($f ne $file) {
$type = "html";
$file = $f;
}
}
}
} elsif ( $type eq "dir" ) {
$type = "file";
}
if ( $type eq "info" && ! -x "/usr/lib/cgi-bin/info2www" ) {
&ErrorMsg ("500 dwww error",
"Cannot convert info files",
"dwww could not find the <strong>info2www</strong> program, which is required to conver the info files" );
# NOT REACHED
}
(my $base_name = $file) =~ s/^.*\///g;
# identify the compression algorithm used for non-directories
# then, calculate the name of the uncompressed file (in extension)
if ( ! -d $file ) {
$base_name =~ s/\.(gz|bz2|en)$//;
# identify the file type from the file extension
# or using the "file" command.
if ( $type eq "file" ) {
# first find the file extension
my $extension = ($base_name =~ /\.([^\/\.]+)$/) ? $1 : "";
# then guess the file type depending on the extension
# if the extension is of size 0 or more than 4
# text is assumed.
if ($extension eq "" || $extension eq "txt" || $extension eq "text") {
$type = "text/plain";
} elsif ($extension eq "htm" || $extension eq "html" || $extension eq "shtml") {
$type = "text/html";
} elsif ($extension eq "css") {
$type = "text/css";
} elsif ($extension =~ /^[1-9][a-z]*/ && $file =~ /\/man\//) {
# if file is located in */man/* directory, assume it's a man page
$type = "man";
} elsif ($extension =~ /^info.*/ && $file =~ /\/info\//) {
$type = "info";
} elsif (length($extension) > 4) {
$type = "text/plain";
} else {
# use MIME:Types to guess extension
my $mimetypes = MIME::Types->new();
if (defined (my $t = $mimetypes->mimeTypeOf($extension))) {
$type = $t;
} else {
$type = "text/plain"; # default
}
}
}
}
my $convertFunction = undef;
my $mime_type = "text/html";
my $mime_charset=undef;
# Ok, now we can convert the file
if ($type eq "man" || $type eq "runman") {
$convertFunction = \&BuiltinMan2Html;
$mime_charset = "UTF-8";
} elsif ($type eq "dir") {
$convertFunction = \&BuiltinDir2Html;
} elsif ($type eq "info") {
$convertFunction = \&BuiltinInfo2Html;
} elsif ($type eq "text/plain") {
$convertFunction = \&BuiltinText2Html;
} elsif ($type ne "html") {
$mime_type = $type;
}
# Do not set mime type for html docs. Either they have a header or we can
# mostly trust the server and browsers to guess correctly.
if (!$mime_charset and $mime_type =~ /^text\// and $type !~ /^(?:text\/)?html$/) {
$mime_charset="UTF-8";
if ($file =~ /(^\/usr\/share\/doc\/[^\/]+\/)([^\/]+)$/o) {
if (($2 eq "changelog.Debian.gz") || ($2 eq "copyright") ||
($2 eq "changelog.gz" && ! -e "$1/changelog.Debian.gz")) {
$mime_charset="UTF-8";
}
}
}
&PrintHeaders($file, $base_name, $mime_type, $mime_charset);
if ( not defined $convertFunction ) {
&DecompressFile($file);
exit 0;
}
my $cacheProg = undef;
if ($dwww_use_cache) {
# Store the file in the cache unless it is already stored
if (system ("dwww-cache --lookup \"$type\" \"$file\"") == 0) {
exit 0;
} else {
$cacheProg = "dwww-cache --store \"$type\" \"$file\"";
}
}
&$convertFunction( $file, $cacheProg , $mime_charset );
exit 0
|