The Open Source Swiss Army Knife

/miscellany/alex/
/miscellany/alex/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /miscellany   /alex 
Permalink: CookieSupport.pm
Title: add
article options : please login   |  raw source view  

package Apache::CookieSupport;
use Apache::Constants qw(:common);
use strict;
use Apache::Filter; 
use File::Basename;
use Apache::URI();
use CGI;
use CGI::Upload;
use CGI::Cookie();
use Apache::DbiHandle;
use Apache::LostPassword;

sub handler {
	my $r = shift;
        $r = $r->filter_register();  # Required
        my $server_name = $r->get_server_name;

	# populate NOTES objects for down-the-road filters to see

	my $uri=$r->uri;
	if ($uri =~ m/(remove|fdsesearch|newuser)/) {
		$uri =~ s/$1//;
	}
	$r->notes('uri' => "$uri");
	my $file = $r->filename;
	$r->notes('req_filename' => "$file");
	my ($file_section,$path,$file_extension) = fileparse("$file",'\..*');
	my $new_filename = $file_section .  $file_extension;
	$path =~ s!/var/sirfsup!!;
	$r->notes('uri_path_only' => "$path");
	$r->notes('filename_only' => "$new_filename");

	# for CGI which comes from POST only once

	my $user_location = CGI::param('user_location');
	$r->notes('user_location' => "$user_location");
	#if (CGI::param('create_account')) {
		$r->notes('create_account' => CGI::param('create_account'));
		$r->notes('user'=> CGI::param('user'));
		$r->notes('email'=>CGI::param('email'));
		$r->notes('password'=>CGI::param('password'));
		$r->notes('password2'=>CGI::param('password2'));
		$r->notes('nocopy'=>CGI::param('nocopy'));
	#}
	#if (CGI::param('lost_password')) {
	#	$r->notes('user' => CGI::param('user'));
	#	$r->notes('email'=>CGI::param('email'));
	#}

	# NewUpload
	($file_section,$path,$file_extension) = fileparse("$user_location",'\..*');
	$r->notes('user_location_path' => $path);
	$r->notes('newupload' => CGI::param('newupload'));  

	my $upload = CGI::Upload->new;
	my $newupload_filename = $upload->file_name('newupload');
        $r->notes('newupload_filename' => "$newupload_filename");
        my $file_type = $upload->file_type('newupload');
        my $file_handle = $upload->file_handle('newupload');
	my $newupload_filebody;
	while (<$file_handle>) {
		$newupload_filebody .= $_;	
	}
	$r->notes('newupload_filebody' => $newupload_filebody);
	# $r->notes('file_to_edit' => CGI::param('file_to_edit'));  
	# $r->notes('filebody' => CGI::param('filebody'));
	# GrepSearch
	$r->notes('query' => CGI::param('query'));
	$r->notes('recursivity' => CGI::param('recursivity'));
	$r->notes('path' => CGI::param('path')); 

	# FileEdit
	$r->notes('filebody' => CGI::param('filebody'));
	$r->notes('submit' => CGI::param('submit'));
	
	# FileTitle
	$r->notes('file_to_edit' => CGI::param('file_to_edit'));  
	$r->notes('title' => CGI::param('title'));
	$r->notes('update' => CGI::param('update'));
	login_logout ($r);


}


sub login_logout {
	my ($r) = @_;
	my $submit = CGI::param('submit')? CGI::param('submit') : '';
	my $cookie_uname = parse_mycookie($r);
	$r->notes('cookie_uname' => $cookie_uname);
	if ((defined $submit) && ($submit eq "login")) {
		login($r);
	} elsif ((defined $submit) && $submit eq "logout") {
		logout($r);
	} elsif ((defined $cookie_uname) && ($cookie_uname ne "")) {
		if ($cookie_uname ne "anonymous") {
			$r->notes('login_message' => "hello $cookie_uname");
			$r->notes('login_name' => $cookie_uname);
			$r->notes('logged_in' => '1');
		} # if ($cookies{'user'} ne "") 
		else {
			$r->notes('login_message' => "pleas login");
			$r->notes('login_name' => 'anonymous');
			$r->notes('logged_in' => '0');
			$r->filename($r->document_root . $r->notes->{'uri'});
		} # if ($cookies{'user'} eq "") 
	}
}


sub login {
	my ($r) = shift;
	my ($user) = shift;
	my ($password) = shift;
	my $user = $user ? $user: CGI::param('user');
	my $password = $password ? $password : CGI::param('password');
	$r->log_error("inside CookieSupport: user=$user, password=$password");
	my $cookie;
	if (($user ne "") && ($password ne "")) {
		my $result = authenticate($r, $user, $password);
		if ($result) {
			$cookie = CGI::Cookie->new(-name=>'sirfsup',-expires=>'+3M',-path=>'/',-domain=>'sirfsup.com',-value=>$user);
			unless ($cookie) {
				$r->log_error("CookieSupport: login: error in cookie making");
			} # unless ($my_cookie)
			$r->header_out('Set-Cookie' => $cookie);
			$r->notes('logged_in' => '1');
		} else { # if undef $result
			$r->notes('logged_in' => '0');
			$r->filename($r->document_root . $r->notes->{'uri'});
		}
	} # if ($user and $password) 
	# "no user nor pass";
} # sub login

sub logout {
	my $r = shift;
	my $cookie;
	$cookie = CGI::Cookie->new(-name=>'sirfsup',-expires=>'+3M',-path=>'/',-domain=>'sirfsup.com',-value=>'anonymous');
	unless ($cookie) {
		$r->log_error("CookieSupport:logout: error in cookie making");
	} # unless ($my_cookie)
	$r->header_out('Set-Cookie' => $cookie);
	$r->notes('login_message' => "please login");
	$r->notes('logged_in' => '0');

} # sub logout

sub authenticate  {
	my ($r, $user, $passwd) = @_;
	# get configuration information
	my ($email1, $email2);
	my $Handle = Apache::DbiHandle->new();
	my $dbh = $Handle->get_handle();
	my $sth=$dbh->prepare("select email from users where username='$user'");
	$sth->execute();
	$sth->bind_columns(undef, \$email1);
	$sth->fetch();
	if ($email1 eq "") {
		$r->notes('login_message' => "is $user a valid user?");
		return 0;
	}
	$sth=$dbh->prepare("select email from users where username='$user' and passwd='$passwd'");
	$sth->execute();
	$sth->bind_columns(undef, \$email2);
	$sth->fetch();
	if ($email2 eq "") {
		$r->notes('login_message' => "is $passwd valid?");
		return 0;
	}
	$dbh->disconnect();
	$r->notes('login_message' => "hello $user");
	return 1;
}

sub parse_mycookie {
	my ($r) = @_;
	my %cookies = CGI::Cookie->parse($r->header_in('Cookie'));
	my $cookie_uname = $cookies{'sirfsup'} ? $cookies{'sirfsup'} : "";
	if ((defined $cookie_uname) && ($cookie_uname ne "")) {
		$cookie_uname =~ /sirfsup=(.*); path=\//;
		$cookie_uname = $1;
		return $cookie_uname;
	} # if ($cookie_uname ne "")
	else {
		return "";
	} # if ($cookie_uname ne "")
}

1;
__END__
(#687) poster : anonymous (owner)date: 2008-10-09

E7deeG pmxmwjfkdqbe, [url=http://bvhtzppwffwb.com/]bvhtzppwffwb[/url], [link=http://egwtbdzhzapp.com/]egwtbdzhzapp[/link], http://hcxuabbvllnr.com/


Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top