###########################################################################
#
# Copyright (C) 2005
# Stefano Visconti - Neptuny s.r.l. Milano IT - visconti@neptuny.it
#
# Metadot Portal Server Platform software is free software; you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# Metadot Portal Server Platform software is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Metadot Portal Server Platform; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# For more information, please contact info@metadot.com.
#
############################################################################
package RemoteDir;
use strict;
use Metadot::GizmoContainer;
use Metadot::UploadsManager;
use Metadot qw( $USER %FORM $SESSION $HTTP_HEADER_SENT $PARAMS $DISPLAY $CACHE $CGI $ACCESS_BROKER );
use vars qw(@ISA);
@ISA=qw(Metadot::GizmoContainer);
my $version = "1.2";
sub get_version
{
return $version;
}
my $ac = RemoteDir->SUPER::get_default_permissions();
$ac->add_op('DISP', 'rd_download');
$ac->add_op('DISP', 'rd_upload');
$ac->add_op('DISP', 'rd_createdir');
sub get_default_permissions
{
return $ac;
}
sub new
{
my $proto = shift;
my $id = shift;
my $class = ref($proto) || $proto;
my $self;
if (defined ($id))
{
$self = $class->SUPER::new($id);
}
else
{
$self = $class->SUPER::new();
}
$self->{is_a} = __PACKAGE__;
$self->_form_initialize();
$self->set_field_info (
"name", "Directory Name", 1,
"description", "Description",0,
"c1", "Directory path (will be appended to 'private' directory", 1,
"c2", "Type", 1
);
bless ($self, $class);
return $self;
}
sub _form_initialize
{
my $self = shift;
$self->{fields}->{c1}->{formtype} = "text";
$self->{fields}->{c2}->{formtype} = "select";
$self->{fields}->{c2}->{selectoptions} = 'Contacts,Files';
my $ts = "This Gizmo will allow the user to manage the content of a remote directory";
$self->set_intro_text($ts);
}
sub get_type {
my $self = shift;
my $res = $self->get_c2();
}
sub get_gizmo_name
{
my $self = shift;
return "Remote Directory";
}
sub get_path
{
my $self = shift;
my $res = $self->get_c1();
$res =~ s/\<\!--.*--\>//gi;
$res = $PARAMS->{absolutepathfileuploaddir}."/".$res;
#$res = MetadotConfig->get_public_uploads_dir() . "/". $res;
return $res;
}
sub _build_buttonbar {
my $self = shift;
my $file = shift;
my $s = '';
my $iid = $self->get_iid();
$s .= '
';
return $s;
}
sub show
{
#this displays the thumbnails in a $row_size wide table
my $self = shift;
my $s = '';
my $buttons = $self->get_buttons();
my $iid = $self->get_iid();
if ($FORM{subop} eq "upload_frm"){
my $dir = $FORM{dir};
return $self->show_upload_frm($dir);
}
if ($FORM{subop} eq "create_dir_frm"){
my $dir = $FORM{dir};
return $self->show_createdir_frm($dir);
}
if ($FORM{subop} eq "find_frm"){
my $dir = $FORM{dir};
return $self->show_find_frm($dir);
}
my $dir;
my $child;
my $name = $self->get_name();
my $path=$self->get_path;
my $file = "/".$FORM{"file"};
$file =~ s/\.\.//gi; ##avoid hacking of root directory
my $searchexpr=$FORM{"searchexpr"};
$searchexpr="*" if (!defined($searchexpr) or $searchexpr eq "");
$s .= "Remote directory " if ($searchexpr eq "*");
$s .= "Search result for directory " if ($searchexpr ne "*");
my $sh=$file;
$sh =~ s/\/?(.*)/$1/gi;
$s .= $name.$sh;
$s .= "
\n";
my $mes=$FORM{message};
if ($mes){
$s.="
$mes
";
}
$s .= $self->_build_buttonbar($file);
$s .= "
";
my @childs=();
my $prependpath="";
if ($searchexpr eq "*"){
$prependpath=$path.$file."/";
opendir($dir,$path.$file) or return "Can't open directory $path"."$file: $!";
@childs=sort(readdir($dir));
closedir($dir);
}
else {
#my $cmd="find $path"."$file -name $searchexpr";
#my $cmd="c:/cygwin/bin/find.exe $path"."$file -name $searchexpr";
#my $out=`$cmd`;
#$s .= "OUT=|$cmd|";
my $dir=$FORM{dir};
$prependpath=$path;
$searchexpr =~ s/\*/.*/gi;
$searchexpr =~ s/\?/./gi;
$searchexpr=".*$searchexpr.*";
@childs = $self->_searchrecurse($searchexpr,$path.$dir,@childs);
}
$s .= $self->_list_files($prependpath,@childs);
return ($s);
}
sub _searchrecurse{
my $self=shift;
my $searchexpr=shift;
my $dirnm=shift;
my @list=@_;
my $dir;
opendir($dir,$dirnm) or return "Can't open directory $dirnm: $!";
my @orig=sort(readdir($dir));
closedir($dir);
my @childs=grep(/$searchexpr/i,@orig);
my $child;
for $child(@childs){
next if ($child eq ".");
next if ($child eq "..");
push(@list,"$dirnm/$child");
}
for $child(@orig){
next if ($child eq ".");
next if ($child eq "..");
if (-d "$dirnm/$child"){
@list = $self->_searchrecurse($searchexpr,"$dirnm/$child",@list);
}
}
return @list;
}
sub _list_files{
my $self=shift;
my $prependpath=shift;
my @childs=@_;
my $iid = $self->get_iid();
my $pref = $self->get_path();
my $s='';
my $child;
$s .= '
';
$s .= '| | ';
if ($self->get_type ne "Contacts"){
$s .= 'filename | size [KB] | last modified | ';
}
#else {
# $s .= ' | e-mail | phone | ';
#}
$s .= '
';
for $child(@childs)
{
next if ($child eq ".");
next if ($child eq "..");
my $vis = $child;
if ($vis =~ /$pref(.*)/){
$vis = $1;
}
$vis =~ s/\/?(.*)/$1/gi;
my $dir = $prependpath;
if ($dir =~ /$pref(.*)/){
$dir = $1;
}
$dir =~ s/\/?(.*)\/?/$1/gi;
$s .= '';
#$s .= "PREPENDPATH=$prependpath ";
#$s .= "CHILD=$child ";
#$s .= "VIS=$vis ";
#$s .= "DIR=$dir ";
if (-d $prependpath.$vis){
$s .= '';
$s .= ' | '.$vis . ' | ';
}
else {
my @fstat=stat($prependpath.$child);
$s .= '';
if ($vis =~ /\.vcf/i){
$vis =~ s/\.vcf//gi;
$s .= '
' .$vis . ' | ';
}
else {
$s .= '
' .$vis . ' | ';
}
if ($self->get_type ne "Contacts"){
$s .= ''.sprintf("%.2f",($fstat[7]/1024))." | ".localtime($fstat[9])." | \n";
}
#else {
# my @tmp=$self->_readcontact("$dir","$vis");
# $s .= ''.$tmp[0]." | ".$tmp[1]." | \n";
#}
}
$s .= '
';
}
$s .= '
';
return $s;
}
sub _readcontact{
my $self=shift;
my $dir=shift;
my $filename=shift;
my $path = $self->get_path();
my $read_file = $path;
if ($dir ne ""){
$read_file .=$dir;
}
if ($filename ne ""){
$read_file .= $filename;
}
$read_file.=".vcf";
my $mail=`cat '$read_file' | grep 'EMAIL;PREF;INTERNET'`;
$mail =~ s/EMAIL;PREF;INTERNET://gi;
my $tmp1=`cat '$read_file' | grep 'TEL;WORK;VOICE'`;
$tmp1 =~ s/TEL;WORK;VOICE://gi;
$tmp1.=";" if ($tmp1 ne "");
my $tmp2=`cat '$read_file' | grep 'TEL;CELL;VOICE'`;
$tmp2 =~ s/TEL;CELL;VOICE://gi;
#$mail = "cat '$read_file' | grep 'EMAIL;PREF;INTERNET'";
return ($mail,$tmp1.$tmp2);
}
sub show_summary
{
#this shows in list view. Just the name and description of the gallery
my $self = shift;
my $s = '';
my $iid = $self->get_iid();
my $name=$self->get_name();
my $dirnm=$self->get_path();
$s .= $self->get_buttons() . '
' . "\n";
#$s .= "DIRNM=$dirnm
\n";
if (-d "$dirnm"){
$s .= '
';
}
else {
my $fn=$dirnm;
$fn = substr($fn,length($PARAMS->{absolutepathfileuploaddir}));
$s .= '
';
}
return ($s);
}
sub rd_download {
my $self = shift;
my $path = $self->get_path();
my ($filename,$read_file);
$filename = $FORM{file};
my $onlyfn=$filename;
$onlyfn =~ s/.*\///gi;
if ($onlyfn eq ""){
$onlyfn=$path;
$onlyfn =~ s/.*\///gi;
}
my $dir = $FORM{dir};
$read_file = $path;
if ($dir ne ""){
$read_file .="/".$dir;
}
if ($filename ne ""){
$read_file .= "/".$filename;
}
Metadot::UploadsManager::transmit_file("RemoteDir",
file => $onlyfn,
read_file => $read_file,
);
#return "FILE=".$FORM{file}.",ELAB=$filename\n";
}
sub rd_upload {
my $self = shift;
my $iid = $self->get_iid();
my $s = '';
my $path = $self->get_path;
my $dir=$CGI->param('dir');
$dir="/" if ($dir eq "");
$dir=$path.$dir;
my $file=$CGI->param('file');
my $name=$file;
$name=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the filename
$name = $2;
$name = $file if ($name eq "");
if (-e "$dir/$name" and $CGI->param('overw') ne "on"){
#the file exists, if the user has not specified to overwrite, complains
$s="Could not save file path=$path, dir=$dir, file=$file, name=$name, the file already exists. Please specify overwrite option if you want to overwrite it.";
#$s.="OVERW=".$FORM{'overw'};
}
else {
my $finalpath = $dir."/".$name;
# This will strip double / in single /
$finalpath =~ s/\/\//\//gi;
my $content = $CGI->upload('file');
my %result = Metadot::UploadsManager::upload_file("RemoteDir",
source_file => $content,
write_file => $finalpath
);
$s = "File successfully saved! ";
#(source_file = $file, write_file = $finalpath, SIZE=".$result{size}.") ";
if ($result{error}){
$s="Error saving file: ".$result{error};
}
}
$DISPLAY->md_redirect("index.pl?iid=$iid&isa=RemoteDir&op=show&dir=$dir&message=$s");
return $s;
}
sub rd_createdir {
my $self = shift;
my $iid = $self->get_iid();
my $s = '';
my $path = $self->get_path;
my $dir=$FORM{dir};
$dir="/" if ($dir eq "");
$dir=$path.$dir;
my $newdir=$FORM{newdir};
my $result = mkdir("$dir/$newdir");
$s = "Directory successfully created!";
if (!$result){
$s="Error creating directory $dir/$newdir: $!";
}
$DISPLAY->md_redirect("index.pl?iid=$iid&isa=RemoteDir&op=show&dir=$dir&message=$s");
return $s;
}
sub show_upload_frm {
my $self = shift;
my $dir = shift;
my $iid = $self->get_iid();
my $s = '';
my $path = $self->get_path;
$s .= 'Upload file to directory '.$self->get_name.$dir.'
';
$s .= '';
return $s;
}
sub show_createdir_frm {
my $self = shift;
my $dir = shift;
my $iid = $self->get_iid();
my $s = '';
my $path = $self->get_path;
$s .= 'Create new directory into directory '.$self->get_name.$dir.'
';
$s .= 'Directory name:';
$s .= '
';
return $s;
}
sub show_find_frm {
my $self = shift;
my $dir = shift;
my $iid = $self->get_iid();
my $s = '';
my $path = $self->get_path;
my $sh=$dir;
$sh =~ s/\/?(.*)/$1/gi;
$s .= 'Search into directory '.$self->get_name.$sh.'
';
$s .= 'Search file or folders containing in the name this text:';
$s .= '
';
return $s;
}
1;
=head1 RemoteDir
=cut