CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Automated Customer Account Creation

 



TsuTsu
Novice

Sep 29, 2008, 7:00 PM

Post #1 of 16 (2253 views)
Automated Customer Account Creation Can't Post

While it is atypical of me to post to a forum for help, I am genuinely perplexed by this problem. Please forgive me if I mess up.

Problem: My script creates new accounts, but fails to populate the data fields (name, number, etc.)

Description:
I have been attempting to write a script which will extract data from an email and post it into a secure form. The email extractor is currently working and writing the data to a "|" delimited file which the account creation script reads from. The account creation script correctly reads that file and splits them into individual variables (tested with "print $var" for each). With this data, the script then uses WWW::Mechanize to login to the secure site and passes the cookies to LWP (since the site uses javascript for navigation). LWP then writes new POST headers to simulate submitting the form. While this does create a new user, none of the data fields (name, phone, etc.) are populated, even though I include them.

My Code:

Code
#!/usr/bin/perl 
use strict;
use WWW::Mechanize;
use HTTP::Cookies;
use LWP::UserAgent;
use LWP::Simple;

my $url = "http://www.storesonlinepro.com/account";
my $sendmail = "/usr/sbin/sendmail -t"; my $reply_to = "Reply-to: No-Reply\@1lwdropship.com\n";
my $userdir = "/home/XXXX/.newUsers";
my $usercount = 0;
my $cookie_jar = HTTP::Cookies->new(
File => "/tmp/1lw-cookie.txt",
AutoSave => 1,
ignore_discard => 1 );
my $errors = "\n\nScript Run on: "; $errors .= `date +%D-%H:%M`;
my $FILE; my $i; my $userTitle; my $userFirst; my $userMiddle; my $userLast; my $userDisplay; my $userCompany; my $userAddress; my $userCity; my $userCounty; my $userState; my $userZip; my $userCountry; my $userDayPhone; my $userEvePhone; my $userEmail; my $userFile;
my $ua = new LWP::UserAgent; my $mech = WWW::Mechanize->new();


$mech->cookie_jar($cookie_jar);
$mech->cookie_jar->clear;
$mech->get($url);
$mech->form_number(1);
$mech->field('license', "XXXXXXX");
$mech->field('password', "XXXXXXX");
$mech->click('login');

print "logging in\n";

$mech->cookie_jar->save;

print "Counting New Users...\n";
opendir(DIR, $userdir);
LINE: while($FILE = readdir(DIR)) {
next LINE if($FILE =~ /^\.\.?/);



if(-d "$FILE"){
$errors .= "Unexpected Directory: $FILE. Skipping.\n"
}
else {
$usercount++;
}
print $usercount . "\n";
}
closedir(DIR);
print "Done Counting\n";

for($i=1;$i<=$usercount;$i++) {
print "Processing user $i\n";
$userFile = $userdir."/user.".$i;

open USER, $userFile or $errors .= "Can't open $userFile: $!\n";
while (<USER>) {
chomp;
($userTitle,$userFirst,$userMiddle,$userLast,$userDisplay,$userCompany,$userAddress,$userCity,$userCounty,$userState,$userZip,$userCountry,$userDayPhone,$userEvePhone,$userEmail) = split /\|/;
}
close USER;
my $userLastLower = lc($userLast);

$ua->cookie_jar($cookie_jar);
$ua->cookie_jar->load;
#---
my $response = $ua->post('https://www.storesonlinepro.com/account',
Content_Type => 'application/x-www-form-urlencoded',
Content => [
source => 'Customer_Menu',
destination => '',
customerfield =>'1',
customervalue => '',
customergroup => '-1',
customersortby => '6',
customersortdir => 'd',
customer_viewstart => '0',
customer_viewsize => '20',
customers_selected => '',
customer_viewstart_b => '0',
customer_viewsize_b => '20',
add => 'Add',
account_id => 'XXXXXXX',
account_changed => '',
]);
my $content = $response->content;
$content =~ /Customer_Edit_(\d+)/;
my $formnumber = $1;

#--
$response = $ua->post('https://www.storesonlinepro.com/account',
{
source => "Customer_Edit_$formnumber",
destination => "Customer_Access_$formnumber",
title => "$userTitle",
first_name => "$userFirst",
middle_name => "$userMiddle",
last_name => "$userLast",
display_name => "$userDisplay",
company => "$userCompany",
address1 => "$userAddress",
city => "$userCity",
county => "$userCounty",
state => "$userState",
postal_code => "$userZip",
country => "$userCountry",
dayphone => "$userDayPhone",
evephone => "$userEvePhone",
email => "$userEmail",
account_id => 'XXXXXXX',
account_changed => 'destination%2Ctitle%2Cfirst_name%2Cmiddle_name%2Clast_name%2Cdisplay_name%2Ccompany%2Caddress1%2Caddress2%2Caddress3%2Ccity%2Ccounty%2Cstate%2Cpostal_code%2Ccountry%2Cdayphone%2Cevephone%2Cemail',
});
$content = $response->content;

#---
$response = $ua->post('https://www.storesonlinepro.com/account',
{
source => "Customer_Access_$formnumber",
destination => '',
email => $userEmail,
"groups_$formnumber" => "123",
"groups_$formnumber" => "105003",
"groups_$formnumber" => "105040",
new_password => $userLastLower,
confirm_password => $userLastLower,
done => 'Done',
account_id => 'XXXXXXX',
account_changed => 'groups_$formnumber%2Cgroups_$formnumber%2Cgroups_$formnumber%2Cnew_password%2Cconfirm_password',
});
$content = $response->content;
}
print "Writing Event Log\n";
open DATA, '>>event.log' or $errors .= "Can't open file: $!\n";
print DATA $errors;
close DATA;


Mockups of the Website Forms
http://digitaldata.securesites.net/~cscott/NewUserPage0.html
http://digitaldata.securesites.net/~cscott/NewUserPage1.html

I realize, I'm probably just overlooking something stupid, but I'm still overlooking it. Any help is much appreciated. Thank you very much.

-TsuTsu


(This post was edited by TsuTsu on Sep 29, 2008, 9:54 PM)


sycoogtit
User

Sep 29, 2008, 7:25 PM

Post #2 of 16 (2248 views)
Re: [TsuTsu] Automated Customer Account Creation [In reply to] Can't Post

There could be one of two things going on here. The first can be found out by doing


Code
if (!$response->is_success) { 
print $response->status_line;
} else {
# do stuff
}


after your form post. You need to install Crypt::SSLeay to use https addresses with LWP::UserAgent. If you don't have it installed, then the above code will tell you that.

Otherwise, maybe it's the fact that you get redirected when you go to https://www.storesonlinepro.com/account, then the form being submitted doesn't specify an action so it goes to that same redirected address? I'm just not sure how LWP handles that.

--
http://bunsooter.com


TsuTsu
Novice

Sep 29, 2008, 7:51 PM

Post #3 of 16 (2244 views)
Re: [sycoogtit] Automated Customer Account Creation [In reply to] Can't Post

Ah, I suppose I took that out of my code. Gomen nasai.
Yes, I already checked that and yes I made sure I had Crypt::SSLesy installed, so that's not the problem.

As for the redirect, I would suspect that except that the group fields are set on the blank user, indicating that at least some of the post is getting submitted

Thank you for you timely reply ^_^


KevinR
Veteran


Sep 29, 2008, 8:13 PM

Post #4 of 16 (2242 views)
Re: [TsuTsu] Automated Customer Account Creation [In reply to] Can't Post

where is this loop terminated?

for($i=1;$i<=$usercount;$i++) {
-------------------------------------------------


sycoogtit
User

Sep 29, 2008, 8:20 PM

Post #5 of 16 (2240 views)
Re: [TsuTsu] Automated Customer Account Creation [In reply to] Can't Post

So you have 3 posts happening here. Which one(s) are you having trouble with, and which fields are and aren't being populated? Are you sure it's not a problem with the cgi receiving the data?

--
http://bunsooter.com


TsuTsu
Novice

Sep 29, 2008, 9:52 PM

Post #6 of 16 (2236 views)
Re: [sycoogtit] Automated Customer Account Creation [In reply to] Can't Post

The for loop ought to terminate right before : print "Writing Event Log\n"; but it seems I accidentally removed that with the other extraneous comments that were there. It terminates correctly in the original script, I assure you. I'm sorry for my inaccurate transcription.

As for the three posts, I'm having problems with the second two. The first one doesn't submit new data as it retrieves the value for $formnumber, a number which changes each session.
The second post ought to submit the client's name and other info, but doesn't. The third post set which groups the client belongs to and ought to set their password. It sets groups correctly, but neither the password nor the email values seem to stick.

I don't suspect a misfunction of the CGI because I can manually enter the information using the website via my browser.

Fields in question:
"title"
"first_name"
"middle_name"
"last_name"
"display_name"
"company"
"address1"
"city"
"county"
"state"
"postal_code"
"country"
"dayphone"
"evephone"
"email"
"new_password"
"confirm_password"


KevinR
Veteran


Sep 29, 2008, 11:29 PM

Post #7 of 16 (2230 views)
Re: [TsuTsu] Automated Customer Account Creation [In reply to] Can't Post

Hard to say because there is no way to test your script.
-------------------------------------------------


sycoogtit
User

Sep 30, 2008, 4:23 AM

Post #8 of 16 (2226 views)
Re: [KevinR] Automated Customer Account Creation [In reply to] Can't Post

Indeed. Stupid question, but are you sure you've got the field names right for the misbehaving fields?

I've run across javascripted pages that, when confirming things such as email and password strength, actually write the confirmed values to hidden form fields and then those fields are the ones used in the CGI. Anything like that going on?

--
http://bunsooter.com


TsuTsu
Novice

Oct 1, 2008, 4:18 PM

Post #9 of 16 (2219 views)
Re: [sycoogtit] Automated Customer Account Creation [In reply to] Can't Post

Unfortunatly, as it is a client's account, I am not at liberty to divulge the account/password. I'm sorry for how inconvenient this makes things.

I'm pretty sure I got all the fields right since I based them off of data taken from the TamperData Firefox extension (Included below)
Because of this, though, I'm pretty sure I got all the fields, even the hidden ones.
Attachments: PostData.tar (20.0 KB)


sycoogtit
User

Oct 1, 2008, 6:28 PM

Post #10 of 16 (2216 views)
Re: [TsuTsu] Automated Customer Account Creation [In reply to] Can't Post

Hm. So you can't get any of the fields from the 2nd form besides account_id and account_changed, right?

I don't have any ideas yet. I'm just trying to make sure I understand what's going on.

--
http://bunsooter.com


TsuTsu
Novice

Oct 1, 2008, 10:08 PM

Post #11 of 16 (2209 views)
Re: [sycoogtit] Automated Customer Account Creation [In reply to] Can't Post

Well, I know that there are no errors in transmittion because it populated the $content variable. I also know that it correctly recieves the account_id field because it pulls up the correct page. I honestly have no idea what account_changed does. It has no effect when I use tamperdata to remove it when I use the browser, nor does it effect the outcome of the script when it is not included.
But yes, the values in the second post are not sticking. I had hoped it was just a stupid mistake in my code, but..
Thank you


KevinR
Veteran


Oct 2, 2008, 12:43 AM

Post #12 of 16 (2204 views)
Re: [TsuTsu] Automated Customer Account Creation [In reply to] Can't Post

I see a potential problem in this line (line 127 of the code you posted above):


Code
account_changed 		=> 'groups_$formnumber%2Cgroups_$formnumber%2Cgroups_$formnumber%2Cnew_password%2Cconfirm_password',


Looks like it should be double-quoted if those are scalars in the string: $formnumber
-------------------------------------------------


TsuTsu
Novice

Oct 2, 2008, 2:51 PM

Post #13 of 16 (2202 views)
Re: [KevinR] Automated Customer Account Creation [In reply to] Can't Post

An excellent point. Unfortunately, even with it corrected it does not alter the script's end result. I'm beginning to think "account_changed" is for auditing purposes as it seems to hold no sway in the submission of data. v__v


KevinR
Veteran


Oct 2, 2008, 2:54 PM

Post #14 of 16 (2200 views)
Re: [TsuTsu] Automated Customer Account Creation [In reply to] Can't Post

Well, since there is no way for anyone but you to test the script, it is just not possible to continue helping you beyond what I have tried to do already. I hope you get it figured out.

Regards,
Kevin
-------------------------------------------------


TsuTsu
Novice

Oct 2, 2008, 3:26 PM

Post #15 of 16 (2199 views)
Re: [KevinR] Automated Customer Account Creation [In reply to] Can't Post

Well that is unfortunate. Thank you for doing your best. If I may ask, though, would you certify that, to the best of your knowledge, my code is no longer miswritten?


KevinR
Veteran


Oct 2, 2008, 7:06 PM

Post #16 of 16 (2196 views)
Re: [TsuTsu] Automated Customer Account Creation [In reply to] Can't Post

The syntax of the code is OK.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives