Switch layouts home | FAQ | | P1 | P2 | P3 | P4 | P5 |  | A1 | A2 | A3  | | B1

Working with the file system

1. Create a copy or a link to PB. It may be either a hard link or a symbolic link depending on your preferences and settings of the web server. The name of the link can be anything as long as the web server is going to run it as a cgi script. For example, a2.cgi
ln ../cgi-bin/pb.pl ./a2.cgi    or
ln -sf ../cgi-bin/pb.pl ./a2.cgi

2. Create a project configuration file. The name should be cgi-script's name + ".pm". For a2.cgi this file should be ./a2.cgi.pm
use vars qw($dbh $sth);
$templatesdir="sampledir";
$Parameter{Page}="home"; 
$Template{home}="$templatesdir/a2-home.html";
$Template{download}="$templatesdir/a2-download";

$top_dir="sampledir/";
$cur_dir=$top_dir;

$DEBUG{all}=DBG_ALL;
$DEBUG_CMD{all}=DBG_ALL;

sub a2_show_file
{
   my $file=shift | $Parameter{file};
   &CI_text_html;
   $file=~s/\.\.//g; $file=~s/\/\///g;$file=~s/\|//g;
   if($file!~/^$top_dir/) {$file="";}
   if( $file ne "" && -e $file )
   {
     print "<pre>";
     open(RD,"<$file");
     while( defined($l=<RD>)){ $l=~ s/&/$HTMLam/g; $l =~ s/</$HTMLlt/g; $l =~ s/>/$HTMLgt/g; $l =~ s/\"/$HTMLqt/g; print $l;}
     close RD;
     print "</pre>";
   }
   exit;
}

sub a2_download
{
  my $file = shift || $Parameter{file};
  my ($fname) = $file =~ /\/([^\/]*)/;
  my $s;
  $s = "";
  my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)= stat($file);
   $file=~s/\.\.//g; $file=~s/\/\///g; $file=~s/\|//g;
  if($file!~/^$top_dir/) {$file="";}
  if( $file ne "" && -e $file )
  {
    open(IN,"<$file");
    read(IN, $s, $size);
    close(IN);
    print "Content-type: application/octet-stream\n"
    ."Content-Disposition: Attachment; filename=\"$fname\"\n"
    ."Content-length: "."$size"."\n\n"
    ."$s";
  }
  else
  {
    &CI_text_html;
  }
  exit;
}
1;


3. Create an html-template file, a2-home.html
<!%if $Parameter{file}%><!%set &a2_show_file($Parameter{file})%><!%endif%>
<!%set 
$cur_dir=$Parameter{cur_dir}?$Parameter{cur_dir}:$cur_dir;
$cur_dir=($cur_dir=~m/^$top_dir/)?$cur_dir:$top_dir;
%>
<!%if(defined($Parameter{edit}))%>
<!%if $Parameter{save}%>"Save" is not allowed<br><!%endif%>
An attempt to save PB tags will cause a security alert
<FORM action="/PB/a2.cgi" method='POST'>
<input type=hidden name="save" value='save'>
<input type='submit' value='Save (not allowed)'> 
<a href="/PB/a2.cgi<!%show $Parameter{cur_dir}?'?cur_dir='."$Parameter{cur_dir}":""%>">ls</a> 
<a href="/PB/a2.cgi?edit=<!%show $Parameter{edit}%>">reload</a>
<br><input type=hidden name=edit value='<!%show $Parameter{edit}%>' size=132 maxsize=255 >
<!%show $Parameter{edit}%>
<!%set 
$file=$Parameter{edit};
$alert="";
if($file!~/^$top_dir/ || $file=~/\.\.\// || $file=~/\|/) 
{$file="";$alert="<br><font color=red>Oops!Interesting filename ...<br>Hey, if you want to explore more, contact us !</font>";}
else 
{ $str=&CI_include($file);$str=~s/\</\<\;/g;$str=~s/\>/\>\;/g; }
%><!%show $alert%>
<br><textarea name=my_edit cols=72 rows=30 width='70' wrap=off value=''>
<!%show $str%></textarea>
</FORM>
<!%else%>
Listing of "<!%show $cur_dir%>" directory:
<hr>
<!%set 
$alert="";
if($cur_dir!~/^$top_dir/ || $cur_dir=~/\.\.\// || $cur_dir=~/\|/) 
{$alert="<font color=red>Oops!Interesting cur_dir ... <br>Hey, if you want to explore more, contact us !</font>";$cur_dir=$top_dir;}
else 
{ $str=&CI_include($file);$str=~s/\</\<\;/g;$str=~s/\>/\>\;/g; }
@ls=split("\n",`ls -F $cur_dir`)
%><!%show $alert%>
<br>
<TABLE border=1 cellspacing=0>
<!%for($i=0;$ls[$i];$i++)%>
<TR>
<!%if $ls[$i]=~m/\/$/%>
<TD colspan=3></TD>
<TD><a href="/PB/a2.cgi?cur_dir=<!%show $cur_dir.$ls[$i]%>"><!%show $ls[$i]%></a></TD>
<!%else%>
<TD><a href="/PB/a2.cgi?file=<!%show $cur_dir.$ls[$i]%>">show</a></TD>
<TD><a href="/PB/a2.cgi?Page=download&file=<!%show $cur_dir.$ls[$i]%>">download</a></TD>
<TD><a href="/PB/a2.cgi?edit=<!%show $cur_dir.$ls[$i]%>&cur_dir=<!%show $cur_dir%>">edit</a></TD>
<TD><a href="<!%show $cur_dir.$ls[$i]%>"><!%show $ls[$i]%></a>
</TD>
<!%endif%>
<!%endfor%>
</TR>
</TABLE>
<!%endif%>

4. Create a download template file, a2-download
Content-type: application/octet-stream
<!%set &a2_download($Parameter{file})%>

5. This web project is complete. Check it out:
/PB/a2.cgi


2002-03