Current location: Hot Scripts Forums » Programming Languages » Perl » Please I Need help before all my hair is gone!


Please I Need help before all my hair is gone!

Reply
  #1 (permalink)  
Old 11-18-03, 09:30 PM
LisatheNovice's Avatar
LisatheNovice LisatheNovice is offline
Newbie Coder
 
Join Date: Nov 2003
Location: New Jersey
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Question Please I Need help before all my hair is gone!

Ok i am trying to get this code to write to three seprate htpasswd files.

# The three .htpassword files
my $APASSWORD_FILE = '/home/samhassa/.htpasswds/Avaya/passwd';
my $NPASSWORD_FILE = '/home/samhassa/.htpasswds/Norstar/passwd';
my $TPASSWORD_FILE = '/home/samhassa/.htpasswds/Toshiba/passwd';


# Here is where it is supposed to add the one username and password to one #file and has been repeated two more time with the other passwd files.

sub add_user {
my $login = shift;
my $password = shift;
my $txn = shift;

file_open($APASSWORD_FILE, "+<");
# check to see if this user already exists
if (find_login($login)) {
error_notify("Username: $login already exists", "add user", 0, 1);
} else {
# seek to the end of the file
if (!seek(FILE, 0, 2)) {
error_notify("Unable to seek to the end of the file: $APASSWORD_FILE\n", "add user", 1, 1);
}
# add the necessary line
print FILE "$login\:$password\n";
}
file_close($APASSWORD_FILE);
file_open($TRANSACTION_FILE, "+<");
# seek to the end of the file
if (!seek(FILE, 0, 2)) {
error_notify("Unable to seek to the end of the file: $TRANSACTION_FILE\n", "add user", 1, 1);
}
# add the necessary line
print FILE "$txn\n";
file_close($TRANSACTION_FILE);
}


# write out all the users
foreach (@others) {
print FILE $_;
}

# truncate the file to the current position
truncate(FILE, tell(FILE));
}
file_close($APASSWORD_FILE)


Someone please tell me what i'm doing wrong.
Like my name i'm a novice at best have only been in the pearl remodeling thing for about a couple of day now.

Any help is really appericated.


Lisa
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-19-03, 10:29 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Lisa,

I went though your code but it looks you left some of it out of your post. If you have the rest of the code, can you post a like to it perhaps? I can't help much with what you've posted so far.

Also, can you rephrase your question? The more detail, the better, I'm a little dense some times I know you say that it doesn't work but I would like to know exactly what it's supposed to be doing.

If you can do that, I think I can help you out.

~Charlie
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 11-20-03, 07:18 AM
LisatheNovice's Avatar
LisatheNovice LisatheNovice is offline
Newbie Coder
 
Join Date: Nov 2003
Location: New Jersey
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Charlie,

Well here it is,the exact problem i'm having is.....it is not writing to the three password files

# my .htpassword files
my $APASSWORD_FILE = '/home/samhass/.htpasswds/Avaya/passwd';
my $NPASSWORD_FILE = '/home/samhass/.htpasswds/Norstar/passwd';
my $TPASSWORD_FILE = '/home/samhass/.htpasswds/Toshiba/passwd';
# Set this to the path of your processed_txns file
my $TRANSACTION_FILE = '/home/samhass/www/cgi-bin/processed_txns';
#my mail path
my $SENDMAIL_PATH = '/usr/sbin/sendmail';
# email notify in case stupid stuff happens
my $ADMIN_EMAIL = 'webmaster@samhassan.com';
# my paypsal email
my @PAYMENT_EMAILS = ('webmaster@samhassan.com');

# -------------------------Avaya Handling--------------------

main();
sub main {
# acknowlege the ipn from PayPal
if (ack_ipn()) {
# decide what to do with msg received
handle_ipn();
}
# IPN was successfully processed
respond(1);
}
sub ack_ipn {
# ack the ipn
my $ua = new LWP::UserAgent;

# build the request
my $req = new HTTP::Request("POST", $PAYPAL_URL);
$req->content_type("application/x-www-form-urlencoded");
$req->content(query_string() . "&cmd=_notify-validate");
# get the response
my $resp = $ua->request($req);
if (($resp->is_success) && ($resp->content eq "VERIFIED")) {
return 1;
} else {
# attempt to identify error
if (($resp->is_success) && ($resp->content eq "INVALID")) {
error_notify("Notification received was NOT from PayPal - Message was ignored.",
"acknowledge IPN", 0, 0);
}
else {
error_notify("Notification could not be acknowledged due to a network or PayPal issue. "
."PayPal will retry until it succeeds.", "acknowledge IPN", 1, 0);
}
return undef;
}
}
sub handle_ipn {
# handle the msg received
if ((param("txn_type") eq "subscr_signup") && (validate_signup())) {
# make sure a username was sent
if (!param("username")) {
error_notify("No username found. Check your subscription button or link.", "add user", 0, 1);
return;
}
# add subscriber to password file
add_user(param("username"), param("password"), param("subscr_id"));
} elsif (param("txn_type") eq "subscr_eot") {
# make sure a username was sent
if (!param("username")) {
error_notify("No username found. Check your subscription button or link.", "remove user", 0, 1);
return;
}
# remove subscriber from password file
remove_user(param("username"));
} else {
# ignore message
}

# validate the receiver email
my $valid = undef;
foreach (@PAYMENT_EMAILS) {
if (param("receiver_email") eq $_) {
$valid = 1;
}
}
if (!$valid)
{
error_notify("An IPN was received that did not match your primary email " .
"address - Message was ignored.",
"validate receiver email ", 0, 0);
return undef;
}

file_open($TRANSACTION_FILE, "+<");
# validate transaction id
if (find_txn(param("subscr_id"))) {
# transaction was previously processed
file_close($TRANSACTION_FILE);
error_notify("An IPN was received that was already processed " .
"- Message was ignored.", "validate subscription id", 0, 0);
return undef;
} else {
file_close($TRANSACTION_FILE);
}
return 1;
}
sub error_notify {
# sends notification that an error has occured
my $err_str = shift;
my $action = shift;
my $kill = shift;
my $req_action = shift;
my $message = "The following error message was generated while trying to $action: \n\t$err_str\n\n\n";
$message .= "User Information\n";
$message .= "\tSubscriber's Username: " . param("username") . "\n";
$message .= "\tSubscriber's Email: " . param("payer_email") . "\n";
$message .= "\tSubscription Number: " . param("subscr_id") . "\n";
$message .= "\tTransaction Type: " . param("txn_type") . "\n";
my $subject = "Subscription Error";
if ($req_action) {
$subject .= " - Requires Action";
} else {
$subject .= " - No Action Required";
}

# if an email is not specified write to error_log only
if (($ADMIN_EMAIL) && ($SENDMAIL_PATH)) {
my %mail = ( To => $ADMIN_EMAIL,
From => $ADMIN_EMAIL,
Subject => $subject,
Message => $message,
);
sendmail(%mail);
}
# put it into the error log
if ($kill) {
# IPN will retry
respond(0);
croak $message;
} else {
carp $message;
}
}
sub sendmail {
# send email using sendmail
my %mail;
my $key;
while (@_) {
$key = shift @_;
$mail{$key} = shift @_;
}
if (!open(SENDMAIL, "|$SENDMAIL_PATH -t")) {
carp "Unable to open sendmail pipe.";
}
print SENDMAIL "To: $mail{'To'}\n";
print SENDMAIL "From: $mail{'From'}\n";
print SENDMAIL "Subject: $mail{'Subject'}\n";
print SENDMAIL "Content-type: text/plain\n\n";
print SENDMAIL "$mail{'Message'}";
if (!close(SENDMAIL)) {
carp "Unable to close sendmail pipe.";
}
}
sub file_open {
my $open_file = shift;
my $open_str = shift;
# open the file
if (!open(FILE, "$open_str$open_file")) {
error_notify("Unable to access: $open_file - $!\n", "open file", 1, 1);
}
# lock access to this file
if (!flock(FILE, 2)) {
error_notify("Unable to get lock on file: $open_file\n", "open file", 1, 1);
}
if (!seek(FILE, 0, 0)) {
error_notify("Unable to seek to the start of the file: $open_file\n", "open file", 1, 1);
}
}
sub file_close {
my $close_file = shift;
# unlock the file
if (!flock(FILE, 8)) {
error_notify("Unable to unlock file: $close_file\n", "close file", 1, 1);
}
if (!close(FILE)) {
error_notify("Unable to close: $close_file - $!\n", "close file", 0, 0);
}
}
sub find_login {
my $new = shift;
my $login;
my $password;
my $remainder;
# for each line, break into parts
while(<FILE>) {
chop;
($login, $password, $remainder) = split(/:/, $_, 3);
if ($login eq $new) {
return 1;
}
}
return undef;
}
sub find_txn {
my $new_txn = shift;
# look for this txn id
while(<FILE>) {
if (/^$new_txn/) {
return 1;
}
}
return undef;
}
sub add_user {
my $login = shift;
my $password = shift;
my $txn = shift;


file_open($APASSWORD_FILE, "+<");
# check to see if this user already exists
if (find_login($login)) {
error_notify("Username: $login already exists", "add user", 0, 1);
} else {
# seek to the end of the file
if (!seek(FILE, 0, 2)) {
error_notify("Unable to seek to the end of the file: $APASSWORD_FILE\n", "add user", 1, 1);
}
# add the necessary line
print FILE "$login\:$password\n";
}
file_close($APASSWORD_FILE);

file_open($TRANSACTION_FILE, "+<");
# seek to the end of the file
if (!seek(FILE, 0, 2)) {
error_notify("Unable to seek to the end of the file: $TRANSACTION_FILE\n", "add user", 1, 1);
}
# add the necessary line
print FILE "$txn\n";
file_close($TRANSACTION_FILE);

}
sub remove_user {
my $login = shift;
my @others;

file_open($APASSWORD_FILE, "+<");
while(<FILE>) {
if (!/^$login\:/) {
# stuff lines into array
push(@others, $_);
}
}

if (@others) {
# seek to start of file
if (!seek(FILE, 0, 0)) {
error_notify("Unable to seek to the start of the file: $APASSWORD_FILE\n", "remove user", 1, 1);
}

# write out all the users
foreach (@others) {
print FILE $_;
}

# truncate the file to the current position
truncate(FILE, tell(FILE));
}

file_close($APASSWORD_FILE);
}

#----------Norstar Handling--------------------
this section has the same code as above ,except the password file is $NPASSWORD_FILE,and then repeated one more time for the $TPASSWORD_FILE.


after all of the handling this is the final section!
sub respond {
# handle the http reponse
my $is_success = shift;
if ($is_success) {
print header(-status=>('204 No Content'));
}
else {
print header(-status=>('500 Internal Server Error'));
}
}

Thanks in advance,
Lisa
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-20-03, 02:15 PM
YUPAPA's Avatar
YUPAPA YUPAPA is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Antarctica
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Have you found out the problem yet?
Can you attach the script here so I can take a look?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 11-21-03, 01:30 AM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
try not sending the file operator/command in the system array and instead just use:

file_open($APASSWORD_FILE);

and in the file_open sub:

sub file_open {
my $open_file = shift;
#my $open_str = shift;
# open the file
if (!open(FILE, "$open_file")) {


and see if that helps
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 11-21-03, 04:22 AM
LisatheNovice's Avatar
LisatheNovice LisatheNovice is offline
Newbie Coder
 
Join Date: Nov 2003
Location: New Jersey
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks to all

I just wanted to say thank-you to everyone who replied,And a huge thanks to YUPAPA......who solved the problem and rewrote the whole script for me.
Everything is running smoothly.

Thanks again everyone!



Lisa
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 11-22-03, 04:05 PM
YUPAPA's Avatar
YUPAPA YUPAPA is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Antarctica
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
You are welcome
__________________
YuPaPa.CoM is my home ~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 09:24 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.