Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners Cgi > Huge cgi!Help!p...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 1421 of 1476
Post > Topic >>

Huge cgi!Help!pop3 Client

by Zazen85@[EMAIL PROTECTED] (Zazen) Oct 7, 2007 at 08:29 AM

Hi dude! I have an orrible trouble with this poor cgi: is a client
pop3 web based gateway.The function "connetti()" never been called and
i don't know why!!The functions in the bottom of the script
load,save,restore the state of the session by save the
user,pass,host,id in a file.if you try to execute the script all stop
when you click on the submit button.I'm italian so i apologize for the
bad english.
I hope there is a good soul who help me.

There is the code:

#!/usr/bin/perl -w

use Mail::POP3Client;
use CGI qw(:all);
#use CGIBook::Error;
#use HTML::Template;

local $MAX_FILES =3D 1000;
local $DATA_DIR  =3D 'usr/lib/cgi-bin';

my $q =3D new CGI;
my $this_script_name =3D 'popGem.cgi';
my $id =3D get_id($q);
my $action =3D ( $q->param("action") ) || 'start';

if ( $action eq "start") {

start($q,$id);

}

if ( $action eq "connetti" ) {

connetti($q,$id);

}

sub start {
my ($q ,$id) =3D @[EMAIL PROTECTED]
        $q-> header(),
        $q-> start_html(-title =3D> "PopGem pop3 web based reader"),
        $q-> start_form(-action =3D> $this_script_name ,-method =3D>
"post"),
        $q-> table(
                 {-border =3D> "1"},
                  $q->caption("PopGem pop3 web based reader!"),
                  $q->Tr(
                        $q-> th("Nome Utente:"),
                        $q-> th( textfield(-name =3D> "user_name",-size
=3D> "30") )
                        ),
                  $q-> Tr(
                        $q-> th("Password:"),
                        $q-> th( password_field(-name =3D> "password",-
size =3D> "30") )
                        ),
                  $q-> Tr(
                        $q-> th("Nome Server:"),
                        $q-> th( textfield(-name =3D> "domain_name",-
size =3D> "30") )
                        ),
                  $q-> Tr(
                        $q-> th({-rowspan =3D> "2"},
                        $q-> submit(-value =3D> "connetti") )
                        ),
                $q->hidden(
              -name     =3D> "id",
              -default  =3D> $id,
              -override =3D> 1
                        ),
                $q->hidden(
              -name     =3D> "action",
              -default  =3D> "connetti",
              -override =3D> 1
                        )
                        ),
        $q-> end_form(),
        $q-> end_html();
        save_state($q);

}

sub connetti {

my ($q,$id) =3D @[EMAIL PROTECTED]
 $user_name   =3D param('user_name');
my $password    =3D param('password');
my $domain_name =3D param('domani_name');
#per ogni messaggio che =E8 presente nella mailbox stampo una riga di
una tabella
#con le informazioni utili: mittente,oggetto,ecc...

my $pop =3D new Mail::POP3Client ( USER         =3D> $user_name,
                                 PASSWORD     =3D> $password,
                                 HOST         =3D> $domain_name,
                                 AUTH_MODE    =3D> 'PASS' );

for ($i =3D 1; $i <=3D $pop->Count(); $i++) {

foreach my $message ( $pop->Head($i) ){

my $date    =3D ($message =3D~ /^Date:\s+/i);
my $from    =3D ($message =3D~ /^From:\s+/i);
my $to      =3D ($message =3D~ /^To:\s+/i);
my $subject =3D ($message =3D~ /^Subject:\s+/i);
print      $q-> header(),
           $q-> start_html(-title =3D> "Ecco i messaggi"),
           $q-> table(
                 {-border =3D> "1"},
                  $q->caption("Informazioni del messaggio $i:"),
                  $q->Tr(
                        $q-> th("Date:"),
                        $q-> th("From:"),
                        $q-> th("To:"),
                        $q-> th("Subject:")
                        ),
                  $q->Tr(
                        $q-> th("$date"),
                        $q-> th("$from"),
                        $q-> th("$to"),
                        $q-> th("$subject")
                        )
                        ),
           $q-> end_html();
        $q-> save_state($q);

}
}
}

sub get_id {
    my $q =3D ****ft;
    my $id;

    my $unsafe_id =3D $q->param( "id" ) || '';
    $unsafe_id =3D~ s/[^\dA-Fa-f]//g;

    if ( $unsafe_id =3D~ /^(.+)$/ ) {
        $id =3D $1;
        load_state( $q, $id );
    }
    else {
        $id =3D unique_id(  );
        $q->param( -name =3D> "id", -value =3D> $id );
    }

    return $id;

}

# Loads the current CGI object's default parameters from the saved
state
sub load_state {
    my( $q, $id ) =3D @[EMAIL PROTECTED]
    my $saved =3D get_state( $id ) or return;

    foreach ( $saved->param ) {
        $q->param( $_ =3D> $saved->param($_) ) unless defined $q-

>param($_);
    }
}

# Reads a saved CGI object from disk and returns its params as a hash
ref
sub get_state {
    my $id =3D ****ft;
    my $session =3D session_filename( $id );
    local *FILE;

    -e $session or return;
    open FILE, $session or die "Cannot open $session: $!";
    my $q_saved =3D new CGI( \*FILE ) or
        error( $q, "Unable to restore saved state." );
    close FILE;

    return $q_saved;

}

# Saves the current CGI object to disk
sub save_state {
    my $q =3D ****ft;
    my $session =3D session_filename( $id );
    local( *FILE, *DIR );

    # Avoid DoS attacks by limiting the number of data files
    my $num_files =3D 0;
    opendir DIR, $DATA_DIR;
    $num_files++ while readdir DIR;
    closedir DIR;

    # Compare the file count against the max
    if ( $num_files > $MAX_FILES ) {
        error( $q, "We cannot save your request because the directory
" .
                   "is full. Please try again later" );
    }

    # Save the current CGI object to disk
    open FILE, ">> $session" or return die "Cannot write to $session:
$!";
    $q->save( \*FILE );
    close FILE;

}

# Separated from other code in case this changes in the future
sub session_filename {
    my $id =3D ****ft;
    return "/$DATA_DIR/$id";

}

sub unique_id {
    # Use Apache's mod_unique_id if available
    return $ENV{UNIQUE_ID} if exists $ENV{UNIQUE_ID};

    require Digest::MD5;

    my $md5 =3D new Digest::MD5;
    my $remote =3D $ENV{REMOTE_ADDR} . $ENV{REMOTE_****T};

    # Note this is intended to be unique, and not unguessable
    # It should not be used for generating keys to sensitive data
    my $id =3D $md5->md5_base64( time, $$, $remote );
    $id =3D~ tr|+/=3D|-_.|;  # Make non-word chars URL-friendly
    return $id;

}
 




 2 Posts in Topic:
Huge cgi!Help!pop3 Client
Zazen85@[EMAIL PROTECTED]  2007-10-07 08:29:23 
Re: Huge cgi!Help!pop3 Client
noreply@[EMAIL PROTECTED]  2007-10-07 17:49:53 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Jul 6 6:39:18 CDT 2008.