OSSP CVS Repository

ossp - ossp-pkg/tabea/tabea.cgi 1.26
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/tabea/tabea.cgi 1.26
#!/usr/opkg/bin/perl -w
##
##  OSSP tabea - Web Configuration Editor
##  Copyright (c) 2001-2002 The OSSP Project <http://www.ossp.org/>
##  Copyright (c) 2001-2002 Cable & Wireless Deutschland <http://www.cw.com/de/>
##
##  This file is part of OSSP tabea, a web configuration editor
##  which can be found at http://www.ossp.org/pkg/tool/tabea/.
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##
##  tabea.cgi: Tabea CGI (syntax: Perl)
##

require 5.000;
use strict;
use IO;
use CGI;

$|++;

my $cfgfile = "./tabea.cfg";

# establish my configuration
my $MY = {};
$MY->{PROGNAME} = ($0 =~ m|^.*?([^/]+?)(?:\.[^/.]+)?$|)[0];
$MY->{TEMPLATE} = '';
my $fh = new IO::File ("<".$MY->{PROGNAME}.".html") || die;
$MY->{TEMPLATE} .= $_ while (<$fh>);
$fh->close();

#   establish CGI query object
my $cgi = new CGI;

#   activate a general error handler
$SIG{__DIE__} = sub {
    my ($err) = @_;

    #$err =~ s|at\s+\S+\s+line\s+(\d+)|(line $1)|s;
    $err =~ s|\n|<br>\n|sg;
    print STDOUT $cgi->header(-expires => '+1s') .
        "<title>Tabea :: ERROR</title>",
        "<h1>Tabea :: ERROR</h1>\n" .
        "<pre>$err</pre>\n";
    exit(0);
};

#   update cookies
my $cookies = [];
foreach my $param (qw(username)) { # FIXME!
    if ($cgi->param($param) ne '' and ($cgi->param($param) ne $cgi->cookie($param))) {
        push(@{$cookies}, $cgi->cookie(
             -name    => $param,
             -value   => $cgi->param($param),
             -expires => '+1d',
             -path    => $cgi->url(-absolute => 1),
             -secure  => 0
        ));
    }
    if ($cgi->param($param) eq '' and $cgi->cookie($param) ne '') {
        $cgi->param(-name => $param, -value => [$cgi->cookie($param)]);
    }
}

# Read the configuration
my %cfghash = &read_config($cfgfile);

#   fetch parameters
my $dialog   = $cgi->param("dialog")   || "";
my $username = $cgi->param("username") || "";
my $password = $cgi->param("password") || "";
my $filename = $cgi->param("filename") || "";

my $form = '';

for my $name (qw(mainw copyt view new copyc edit del run confr newf changer copy save)) {
    my $val = $cgi->param("dialog_$name") || "";
    if ($val ne '') {
        $form = $name;
    }
}

NEXTFORM:
my $page = $cgi->startform( -method => "POST", -action => $cgi->url(-full => 1));

if ($form eq '') {
    $page .= &login();
}
elsif ($form eq 'mainw') {
    $page .= mainw();
}
elsif ($form eq 'edit') {
    $page .= &edit($cgi->param('configslist'));
}
elsif ($form eq 'save') {
    $page .= &save($cgi->param('editfile'), $cgi->param('editwindow'));
}
elsif ($form eq 'view') {
    $page .= &view($cgi->param('templatelist'));
}
elsif ($form eq 'new') {
    $page .= &new($cfghash{'BaseDir'} . $cfghash{'ConfigDir'}, $username);
}
elsif ($form eq 'newf') {
    $page .= &newfile($cfghash{'BaseDir'} . $cfghash{'ConfigDir'},
                      $username, $cgi->param('newfile'), $cgi->param('new_rights'));
}
elsif ($form eq 'confr') {
    $page .= &config_rights($cgi->param('configslist'));
}
elsif ($form eq 'changer') {
    $page .= &change_rights($cfghash{'BaseDir'} . $cfghash{'ConfigDir'} . "/"
                            . $cgi->param('filename'), $cgi->param('changerights') );
}
elsif ($form eq 'copyt') {
    $page .= &copy_file($cgi->param('templatelist') );
}
elsif ($form eq 'copyc') {
    $page .= &copy_file($cgi->param('configslist'));
}
elsif ($form eq 'copy' ) {
    $page .= &copy($cgi->param('srcfile'), $cfghash{'BaseDir'} . $cfghash{'ConfigDir'} . "/" .
                   $username . "/". $cgi->param('dstfile'));
}
elsif ($form eq 'del' ) {
    $page .= &del($cgi->param('configslist'));
}
elsif ($form eq 'run' ) {
    $page .= &run($cgi->param('configslist') );
}
else {
    die "no correct dialog found";
}

$page .= $cgi->hidden(-name => 'password', -default => $password);
$page .= $cgi->endform;

my $out = $MY->{TEMPLATE};
$out =~ s|%%BODY%%|$page|s;
$out =~ s|%%TITLE%%|Test|s;

print STDOUT $cgi->header(-cookie => $cookies, -expires => '+1s') . $out;

exit(0);

############### End of main part #######################################################

########################################################################################
sub login{
########################################################################################
    my $text = '';

    $text .=
    "<table cellspacing=0 cellpadding=0>" .
    "  <tr>" .
    "    <td>Username:</td>" .
    "    <td>" .
    $cgi->textfield(
        -name => 'username',
        -default => $username,
        -size => 20,
        -maxlength => 8
    ) .
    "    </td>" .
    "  </tr>" .
    "  <tr>" .
    "    <td>Password:</td>" .
    "    <td>" .
    $cgi->password_field(
        -name => 'password',
        -value => $password,
        -size => 20,
        -maxlength => 80
    ) .
    "    </td>" .
    "  </tr>" .
    "  <tr>" .
    "    <td colspan=2 align=right>" .
    $cgi->reset() .
    $cgi->submit(-name => 'dialog_mainw', -value => 'login') .
    "    </td>" .
    "  </tr>" .
    "</table>" ;

    return $text;
}
########################################################################################


########################################################################################
sub mainw {
########################################################################################
    my $text = '';

    &validpassword($username, $password, $cfghash{'BaseDir'}.$cfghash{'PasswdFile'})
        || print STDOUT $cgi->redirect(-uri => $cgi->url(-full => 1), -type => "text/html");

    my $templatesfiles = &get_name_files($cfghash{'BaseDir'} . $cfghash{'ConfigDir'}, $username, 4);
    my $configsfiles = &get_name_files($cfghash{'BaseDir'} . $cfghash{'ConfigDir'}, $username, 6);

    $configsfiles .= &get_own_files($cfghash{'BaseDir'} . $cfghash{'ConfigDir'}, $username);

    $templatesfiles = &strsort($templatesfiles);
    $configsfiles = &strsort($configsfiles);

    my @templatesarray = split(/ /,$templatesfiles);
    my @configsarray = split(/ /,$configsfiles);

    $text .=
    # Begin with templates part
    "<font size=+1>" .
    "<br> Active User: $username<br><br>\n" .
    "</font>" .
    "<table> <caption>Templates</caption>" .
    "  <td>" .
    "    <td>" .
    $cgi->scrolling_list(
        -name => 'templatelist',
        -value => [@templatesarray],
        -size => 3
    ) .
    "   </td>" .
    "  <td>" .
    "    <table>" .
    "   <td>" .
    "   <tr>" .
    "     <td>" .
    $cgi->submit(-name => 'dialog_view', -value => 'View') .
    "     </td>" .
    "   </tr>" .
    "   <tr>" .
    "     <td>" .
    $cgi->submit(-name => 'dialog_copyt', -value => 'Copy') .
    "     </td>" .
    "   </tr>" .
    "   </td>" .
    "   </table>" .
    "   </td>" .
    "   </table>" .
    " </td>" .
    "</table>" .
    # Now the configuration part
    "<br><br>" .
    "<table> <caption>Configuration</caption>" .
    "<td>" .
    "    <td>" .
    $cgi->scrolling_list(
        -name => 'configslist',
        -value => [@configsarray],
        -size => 10
    ) .
    "   </td>" .
    "   <td>" .
    "     <table>" .
    "      <tr><td>" .
    $cgi->submit(-name => 'dialog_new', -value => 'New') .
    "      </td></tr>" .
    "      <tr><td>" .
    $cgi->submit(-name => 'dialog_del', -value => 'Del') .
    "      </td></tr>" .
    "      <tr><td>" .
    $cgi->submit(-name => 'dialog_edit', -value => 'Edit') .
    "      </td></tr>" .
    "      <tr><td>" .
    $cgi->submit(-name => 'dialog_copyc', -value => 'Copy') .
    "      </td></tr>" .
    "      <tr><td>" .
    $cgi->submit(-name => 'dialog_confr', -value => 'Rights') .
    "      </td></tr>" .
    "      <tr><td>" .
    $cgi->submit(-name => 'dialog_run', -value => 'Run') .
    "      </td></tr>" .
    "    </table>" .
    "  </td>" .
    "<td>" .
    "</table>";

    return $text;
}
########################################################################################


########################################################################################
sub get_name_files {
########################################################################################
    my ($dirname, $user, $rights) = @_;
    my $filel = '';
    my @filelarray;
    my $filelist = '';
    my $subdir = '';
    my $filemode = '';
    my $entry = '';

    my $dirlist = &readsubdir($dirname . "/");
    my @dirarray = split (/ /, $dirlist);

    DIR: foreach $subdir (@dirarray) {
        next DIR if $subdir eq $user;
        $filel = &readdir($dirname . "/" . $subdir . "/");
        @filelarray = split (/ /, $filel);
        FILE: foreach $entry (@filelarray) {
            my $path = $dirname . "/" . $subdir . "/" . $entry;
            next FILE if  (((stat($dirname . "/" . $subdir . "/" . $entry))[2]) & 07)  !=  $rights;
            $filelist .= $subdir . "/" . $entry . " ";
        }
    }

    return $filelist;
}
########################################################################################


########################################################################################
sub get_own_files {
########################################################################################
    my ($dirname, $user) = @_;
    my $filelist = '';
    my $entry = '';

    my $flist = &readdir($dirname . "/" . $user . "/");
    my @flistarray = split (/ /,$flist);

    foreach $entry (@flistarray) {
        $filelist .= $user . "/" . $entry . " ";
    }

    return $filelist;
}
########################################################################################


########################################################################################
sub edit {
########################################################################################
    my ($editfile) = @_;
    my $text;

    if ($editfile) {
        my $filename = $cfghash{'BaseDir'} . $cfghash{'ConfigDir'} . "/" . $editfile;
        $text .= &editconfig($filename, 'w', $username) .
                 $cgi->hidden(-name => 'editfile', -default => $filename );
    } else {
        $text .= "<font color=red>Missing filename<br><br>\n" .
                 $cgi->submit(-name => 'dialog_mainw', -value => 'Back');
    }

    return $text;
}
########################################################################################


########################################################################################
sub save {
########################################################################################
    my ($file, $edittext) = @_;
    my $text = '';
    my $fileout;

    $text= "<font size=+2>" .
           "Save File: $file<br><br>\n" .
           "</font>" .
           "<br><br>";

    $fileout = IO::File->new(">$file");
    if (defined $fileout) {
        print $fileout $edittext;
        $fileout->close;
        $form = 'mainw';
        goto NEXTFORM;
    } else {
        $text .= "<font color=red>Cannot save file<br><br>\n";
    }

    $text .= $cgi->submit(-name => 'dialog_mainw', -value => 'Back');

    return $text;
}
########################################################################################


########################################################################################
sub view {
########################################################################################
    my ($file) = @_;
    my $text;

    if ($file) {
        my $filename = $cfghash{'BaseDir'} . $cfghash{'ConfigDir'} . "/" . $cgi->param('templatelist');
        $text .= &editconfig($filename, 'r', $username);
    } else {
        $text .= "<font color=red>Missing filename<br><br>\n" .
        $cgi->submit(-name => 'dialog_mainw', -value => 'Back');
    }
    return $text;
}
########################################################################################


#########################################################################################
sub new {
########################################################################################
    my ($dirname, $user) = @_;
    my $text = '';

    $text= "<font size=+2>" .
    "New File:<br><br>\n" .
    "</font>" .
    "<table spacing=0 cellpadding=0>" .
    "  <tr>" .
    "    <td>New Filename: $dirname " . "/" . "$user" . "/" . "</td>" .
    "   <td>" .
    $cgi->textfield(
        -name => 'newfile',
        -default => '',
        -size => 20,
        -maxlength => 30
    ) .
    " </td>" .
    " </tr>" .
    " <tr>" .
    "<tr><td><br></td></tr>" .
    "<td></td>" .
    " <td align=left>" .
    $cgi->scrolling_list(
       -name => 'new_rights',
       -value => ['private', 'protected', 'public'],
       -size => 1
    ) .
    " </td>" .
    " </tr>" .
    "<tr><td><br></td></tr>" .
    "  <tr>" .
    "<td></td>" .
    "    <td colspan=2 align=left>" .
    $cgi->submit(-name => 'dialog_mainw', -value => 'Back') .
    $cgi->submit(-name => 'dialog_newf', -value => 'Create') .
    "    </td>" .
    "  </tr>" .
    "</table>";

    return $text;
}

########################################################################################


########################################################################################
sub newfile {
########################################################################################
    my ($dirname, $user, $newfilename, $newrights) = @_;
    my $text = '';
    my $newpath = $dirname . "/" . $user . "/" . $newfilename;
    my $filesinpath = &readdir($dirname . "/" . $user . "/");
    my @filesinpatharray = split(/ /,$filesinpath);
    my $canwrite = 1;   # true
    my $filerights = '';

    $text= "<font size=+2>" .
           "Creating New File: $newfilename <br><br>" .
           "</font><br><br><br>";
    foreach (@filesinpatharray) {
        if ($_ eq $newfilename) {
            $canwrite = 0;
        };
    }
    if ($canwrite) {
        if ($newrights eq "private") {
            $filerights = '';
        } elsif ($newrights eq "protected") {
            $filerights = "r";
        } elsif ($newrights eq "public") {
            $filerights = "rw";
        } else {
            die "Unknown file rights";
        }
        $text .= "Creating the new file $newpath<br><br>";
        system("touch $newpath") && die "Cannot create new file";
        $text .= "Setting new file rights for $newpath<br><br>";
        system("chmod u=rw $newpath") && die "Cannot set new file rights";
        system("chmod o=$filerights $newpath") && die "Cannot set new file rights";
        $text .= "<br><br><br>";
        $form = 'mainw';
        goto NEXTFORM;
    } else {
        $text .= "<font color=red>Cannot write file. File exists<br><br>\n";
    }
    $text .= $cgi->submit(-name => 'dialog_mainw', -value => 'Back');

    return $text;
}
########################################################################################


########################################################################################
sub config_rights {
########################################################################################
    my ($namefile) = @_;
    my $text = '';
    my $defaultright = '';
    my $confname = '';

    my $filerights = (stat($cfghash{'BaseDir'} . $cfghash{'ConfigDir'} . "/" . $namefile))[2] & 07;

    if ($namefile ne '') {
        if ($filerights == 6) {
            $defaultright = "public" ;
        } elsif ($filerights == 4) {
            $defaultright = "protected";
        } else {
            $defaultright = "private";
        }

        $text .= "<font size=+2>" .
                 "Changing rights of file:<br><br>";

        $namefile =~ m{^([a-zA-Z0-9!"£$%^&*()-_=+#~]+)/(.+)}sg;
        if ($1 eq $username) {
            $text .= "<table>" .
            "<tr>" .
            " <td>" .
            "$namefile<br>" .
            " </td>" .
            " <td>" .
            $cgi->scrolling_list(
                -name => 'changerights',
                -value => ['private', 'protected', 'public'],
                -default => $defaultright,
                -size => 1
            ) .
            " </td>" .
            "</tr>" .
            "</table>" .
            "<br><br>" .
            $cgi->submit(-name => 'dialog_mainw', -value => 'Back') .
            $cgi->submit(-name => 'dialog_changer', -value => 'Set_rights');
            $confname = $cfghash{'BaseDir'} . $cfghash{'ConfigDir'} . "/" . $namefile;
            $text .= $cgi->hidden(-name => 'filename', -default => $namefile ) ;
        }
        else {
            $text .= "<font color=red>You can only change file rights in your own directory<br><br>\n" .
                     $cgi->submit(-name => 'dialog', -value => 'Back');
        }
    }
    else {
        $text .= "<font color=red>Missing filename<br><br>\n" .
                 $cgi->submit(-name => 'dialog_mainw', -value => 'Back');
    }

    return $text;
}
########################################################################################


########################################################################################
sub change_rights {
########################################################################################
    my ($filename, $newrights) = @_;
    my $text = '';
    my $filerights = '';

    $text = "<font size=+2>" .
            "Changing rights of file:<br>" .
            "</font>" .
            "<br><font size=+1> $filename </font> to <font size=+1> $newrights </font><br>";
    if ($newrights eq "private") {
        $filerights = '';
    }
    elsif ($newrights eq "protected") {
        $filerights = "r";
    }
    elsif ($newrights eq "public") {
        $filerights = "rw";
    }
    else {
        die "Unknown file rights";
    }
    system("chmod o=$filerights $filename") && die "Cannot set new file rights";
    $form = 'mainw';
    goto NEXTFORM;

    return $text;
}
########################################################################################


########################################################################################
sub copy_file {
########################################################################################
    my ($filename) = @_;
    my $text = '';

    if ($filename) {
        my $srcfile = $cfghash{'BaseDir'} . $cfghash{'ConfigDir'} . "/" . $filename;
        $text .= $cgi->hidden(-name => 'srcfile', -default => $srcfile ) .
        "<font size=+1>" .
        "Copying File:<br>" .
        "</font>" .
        "<br><br>" .
        "Active user: $username<br>" .
        "<br>" .
        "<table border=1 width=50%>" .
        " <tr>" .
        "  <td>Sourcefile :</td>" .
        "  <td>Destinationfile</td>" .
        " </tr>" .
        " <tr>" .
        "  <td>$srcfile</td>" .
        "  <td>" .
        "   <table>" .
        "   <tr>" .
        "    <td>" . $cfghash{'BaseDir'} . $cfghash{'ConfigDir'} . "/" . $username . "/</td>" .
        "    <td>" .
        $cgi->textfield(
            -name=>'dstfile',
            -default=>'',
            -size=>30,
            -maxlength=>80
        ) .
        "   </td>" .
        "   </tr>" .
        "   </table>" .
        "  </td>" .
        " </tr>" .
        "</table>" .
        "<br><br>" .
        "<table>" .
        " <td>" .
        " <td>" .
        $cgi->submit(-name => 'dialog_mainw', -value => 'Back').
        " </td>" .
        " <td>" .
        $cgi->submit(-name => 'dialog_copy', -value => 'Copy').
        " </td>" .
        " </td>" .
        "</table>" ;
    }
    else {
        $text .= "<font color=red>Missing filename<br><br>\n" .
                 $cgi->submit(-name => 'dialog_mainw', -value => 'Back') ;
    }

    return $text;
}
########################################################################################


########################################################################################
sub del {
########################################################################################
    my ($delfile) = @_;
    my $text = '';

    if ($delfile) {
        my $fpath = $cfghash{'BaseDir'} . $cfghash{'ConfigDir'} . "/" . $delfile;
        $text = "<font size=+2>" .
                "Delete File: $fpath<br><br>\n" .
                "</font>" .
                "<br><br>";
        $delfile =~ m{^([a-zA-Z0-9!"<A3>$%^&*()-_=+#~]+)/(.+)}sg;
        if ($1 eq $username) {
            system("rm $fpath") && die "Cannot delete file";
            $form = 'mainw';
            goto NEXTFORM;
        }
        else {
            $text .= "<font color=red>You can only delete file in your own directory<br><br>\n";
        }
    }
    else {
        $text .= "<font color=red>Missing filename<br><br>\n";
    }

    $text .= $cgi->submit(-name => 'dialog_mainw', -value => 'Back');

    return $text;
}
########################################################################################


########################################################################################
sub run {
########################################################################################
    my ($runfile) = @_;
    my $text = '';
    my $rpath = $cfghash{'BaseDir'} . $cfghash{'ConfigDir'} . "/" . $runfile;
    my $rcommand = $cfghash{'ExecuteCommand'};
    my $output = "/tmp/" .  $cfghash{'ExecuteTempFile'};
    my $fileoutput;
    my $outputtext = '';

    if ($runfile eq '') {
        $text .= "<font color=red>No configuration selected <br><br>\n" ;
        $text .= $cgi->submit(-name => 'dialog_mainw', -value => 'Back');
        return $text;
    }
    $rcommand =~ s|%{profile}%|$rpath|s;

    if (-e $output) {
        system("/bin/rm $output");
    }

    $text .= "<font size=+2>" .
             "Running Configuration: $rpath<br><br>\n" .
             "</font>" .
             "<br><br>" .
             "Run command: " . $rcommand . "<br>";

    system("$rcommand > $output 2>&1");

    $fileoutput = IO::File->new("<$output");
    if (defined $fileoutput) {
        while (<$fileoutput>) {
            $outputtext .= $_;
        }
        $fileoutput->close();

        my $outputhash = {
            -name => 'outputwindow',
            -default => $outputtext,
            -rows    => 40,
            -columns => 80
        };
        $outputhash->{readonly} = "";
        $text .= $cgi->textarea($outputhash) . "<br>";
    }
    else {
        $text .= "<font color=red>Run command not working<br><br>\n";
    }
    $text .= $cgi->submit(-name => 'dialog_mainw', -value => 'Back');

    return $text;
}
########################################################################################


########################################################################################
sub copy {
########################################################################################
    my ($srcf, $dstf) = @_;
    my $text = '';

    $text .= "<font size=+1>" .
             "Copying File:<br>" .
             "</font><br><br>" .
             "Copying from <font size=+1>$srcf</font> to <font size=+1>$dstf</font><br><br>";

    system("cp $srcf $dstf") && die "Cannot copy file";
    system("chmod o= $dstf") && die "Cannot change file rights";

    $form = 'mainw';
    goto NEXTFORM;

    return $text;
}
########################################################################################


########################################################################################
sub read_config {
########################################################################################
    my ($cfgfile) = @_;
    my %cfghash ;
    my $cfgin ;
    my $key;
    my $content;

    $cfgin = IO::File->new("<$cfgfile");
    if (defined $cfgin) {
        LOOP: while(<$cfgin>) {
            $_ =~ s|^\s+(.*?)|{$_=$1}|es;       # Remove whitespaces at the beginning
            next LOOP if $_ =~ /^\n/;           # Remove the first empty require
            next LOOP if $_ eq "";              # Remove empty line
            next LOOP if $_ =~/^#/;             # Remove comment lines

            $_ =~ s|^([^#]+)#(.*)$|{$_=$1}|es;  # Remove comments on the end
            $_ =~ s|(.*?)\s+$|{$_=$1}|es;       # Remove whitespaces at the end

            ($key, $content) =  /^(\w+)\s+(.*)$/;

            $cfghash{$key} = $content;
        }
        $cfgin->close;
    }
    else {
        die "Can't open config file";
    }
    return (%cfghash);
}
########################################################################################


########################################################################################
sub validpassword {
########################################################################################
    my ($user, $password, $pwdfile) = @_;
    my $returnvalue = 0;
    my $uname;
    my $pwd;
    my $pwdin;

    $pwdin = IO::File->new("<$pwdfile");
    if(defined $pwdin) {
        while (<$pwdin>) {
            $_ =~ s|^(\w+):(.+)$|{$uname=$1; $pwd=$2}|eg;
            ( $uname =~ $user ) && do {     # check the password
                if (crypt($password, $pwd) eq $pwd){
                    $returnvalue = 1;
                }
            }
        }
    }
    $pwdin->close();
    return $returnvalue;
}
########################################################################################


########################################################################################
sub readsubdir {
########################################################################################
    my ($dir) = @_;
    my $filestring = "";
    my $direntry = "";

    opendir(DIR, "$dir");
    foreach $direntry (readdir(DIR)) {
        next if $direntry eq ".";
        next if $direntry eq "..";
        if (-d "$dir/$direntry") {
            $filestring .= $direntry . " ";
        }
    }
    closedir(DIR);
    return $filestring;
}
########################################################################################


########################################################################################
sub readdir {
########################################################################################
    my ($dir) = @_;
    my $filestring = "";
    my $direntry = "";

    opendir(DIR, "$dir");
    foreach $direntry (readdir(DIR)) {
        next if $direntry eq ".";
        next if $direntry eq "..";
        if (-f "$dir/$direntry") {
            $filestring .= $direntry . " ";
        }
    }
    closedir(DIR);
    return $filestring;
}
########################################################################################


########################################################################################
sub strsort {
########################################################################################
    my ($sortstr) = @_;

    my @sortarray = split(/ /, $sortstr);

    @sortarray = reverse sort {$b cmp $a} @sortarray;
    $sortstr = join(" ", @sortarray);

    return $sortstr;
}
########################################################################################


########################################################################################
sub editconfig {
########################################################################################
    my ($file, $mode, $user) = @_;
    my $texte = '';
    my $filein ;
    my $editf = '';
    my $readonly ;
    my $titletext ;

    $filein = IO::File->new("<$file");
    if (defined $filein) {
        while(<$filein>) {
            $editf .= $_;
        }
        $filein->close();
    }
    else {
        die "Can't open $file";
    }

    if ($mode eq 'w') {
        $titletext = "Edit window";
    } elsif ($mode eq 'r') {
        $titletext = "View window";
    }

    my $textahash = {
        -name => 'editwindow',
        -default => $editf,
        -rows    => 40,
        -columns => 80
    };
    if ($mode eq 'r') {
        $textahash->{readonly}="";
    }
    $texte .= "<font size=+2>" .
              $titletext . ":   <br><br>\n" .
              "</font>" .
              "<font size=+1>" .
              "File: $file<br><br>\n" .
              "</font>" .
              $cgi->textarea($textahash).
              "<br><br>\n" .
              "<table>" .
              "  <td>" .
              "    <td>" .
              $cgi->submit(-name => 'dialog_mainw', -value => 'Back') .
              "   </td>";
    if ($mode eq 'w') {
        $texte .= "   <td>" .
                  $cgi->submit(-name => 'dialog_save', -value => 'Save') .
                  "   </td>";
    }
    $texte .= "  <td>" .
              " </td>" .
              "</table>";
    return $texte;
}
########################################################################################

CVSTrac 2.0.1