#!/usr/bin/perl -w
#
# script smb2www-2.cgi : provide web interface to access smb filesystem
# with Filesys::SmbClient and libsmclient.so
# Copyright 2001 A.Barbet alian@alianwebserver.com. All rights reserved.
#
# $Revision: 1.5 $
# $Date: 2002/10/18 13:02:09 $
# $Author: alian $
#------------------------------------------------------------------------------
use CGI qw/:standard :html3 :netscape escape unescape/;
use CGI::Carp qw/fatalsToBrowser/;
use strict;
use Filesys::SmbClient;
# ------ Config --------------------------------------------------------------#
my $SN = $ENV{SCRIPT_NAME} || "smb2www-2.cgi";
my $mimetype = "/etc/mime.types";
my $user = "alian";
my $password = "password";
my $workgroup = "alian"; # optional
my $maskFile = 0666; # for upload file
my $maskDir = 0755; # for created dir
my $css = "http://saturne/smb2www.css"; # for fun
# ------ End config ----------------------------------------------------------#
my $smb = new Filesys::SmbClient(username => $user,
password => $password,
workgroup => $workgroup,
debug => 10)
|| die "Can't connect:$!\n";
&main();
sub main
{
my $buffer;
# browse a share or a dir
if (param('browse'))
{ print browse(param('browse')); }
# read a file
elsif (param('readfile'))
{ print read_file(param('readfile')); }
# put a file
elsif (param('filename'))
{ print upload_file(param('filename'), param('dir')); }
# create a dir
elsif (param('directory'))
{
my $dir = param('dir').'/'.param('directory');
$smb->mkdir($dir, $maskDir) ||
print header,"Can't create ", $dir, ":$!\n";
print browse(param('dir'));
}
# delete a file
elsif (param('delete') || param('deleteDir'))
{
print header;
my $dir;
foreach my $f (param('delete'))
{
if (unescape($f)=~/^(.*)\/[^\/]*$/) { $dir = $1; }
my $res = $smb->unlink(unescape($f));
if ($res) { print unescape($f)," deleted
\n"; }
else { print "Can't delete ",unescape($f),":$!
\n"; }
}
foreach my $f (param('deleteDir'))
{
if (unescape($f)=~/^(.*)\/[^\/]*$/) { $dir = $1; }
my $res = $smb->rmdir_recurse(unescape($f));
if ($res) { print unescape($f)," deleted
\n"; }
else { print "Can't delete ",unescape($f),":$!
\n"; }
}
print "Back to $dir",
end_html;
}
# first form
else
{
if ($workgroup)
{ print browse("smb://".$workgroup); }
else
{
print header,
start_html
( -'title' => 'smb2www2',
-'author' => 'alian@alianwebserver.com',
-'meta' => {'keywords' => 'smb',
-'copyright'=>'Copyright 2001 AlianWebServer'},
-'style' => {'src' => $css},
-'dtd' => '-//W3C//DTD HTML 4.0 Transitional//EN"'.
' "http://www.w3.org/TR/REC-html40/loose.dtd')."\n";
}
print h1("smb2www - the come back"),
start_form.
textfield(-name=>'browse')."(ex: smb://my_smb_server or
smb://my_workgroup)
".
submit."\n".end_form;
}
print end_html,"\n";
}
#------------------------------------------------------------------------------
# Method that browse content of $rep
#------------------------------------------------------------------------------
sub browse
{
my ($rep) = @_;
my ($i,$j,$tf,@lf,@lr,@ls,@lm,$buffer, $style)=(0,0,0);
chop($rep) if ($rep=~/\/$/);
return undef if (!$rep);
# Read directory
my $D = $smb->opendir($rep) || die "Can't read $rep:$!\n";
my @f = $smb->readdir_struct($D);
$smb->close($D);
# Sort file by name
@f = sort { $a->[1] cmp $b->[1] } @f;
# For each item ...
foreach my $f (@f)
{
# Reformat url for dir . and ..
# and build new url in $ref
my $ref;
if ($f->[1] eq ".") { $ref = $rep; }
elsif ($f->[1] eq "..")
{ if ($rep=~/(.*)\/[^\/]*$/) { $ref = $1; } }
elsif ($f->[0] == SMBC_SERVER) { $ref = "smb://".$f->[1];}
else { $ref = $rep.'/'.$f->[1]; }
my $refe = escape($ref);
# A directory
if ($f->[0] == SMBC_DIR)
{
# modulo for css style
if (++$i % 2) { $style = "style1"; }
else { $style = "style2"; }
my $z="";
# dont show delete for . and ..
if ($f->[1] eq '.' || $f->[1] eq '..') { $z=" "; }
my $item = "