// dynaold #include #include #include #include #include #include struct Customer { string mobileNo, name, passWord, birth, address, city, eMail; string amountOutstanding; }; Customer getDetails(string m) { Customer c; char *qbuff; qbuff = new char[255]; MYSQL mysql; MYSQL_RES *res; MYSQL_ROW row; char mobileNo[11]; int mlen = m.length(); for(int ctr = 0; ctr < mlen; ctr++) { mobileNo[ctr] = m.at(ctr); } mobileNo[11] = '\0'; if(!mysql_init(&mysql)) cout << endl << "Problem initializing MYSQL" << mysql_error(&mysql) << endl; if(!mysql_real_connect(&mysql, NULL, "root", NULL, "Dyna", 0, 0, 0)) cout << endl << "Can Not connect to MYSQL" << mysql_error(&mysql) << endl; sprintf(qbuff, "select * from Customer where cMobileNo = '%s'", mobileNo); if(!mysql_query(&mysql, qbuff)) cout << endl << mysql_error(&mysql) << endl; res = mysql_store_result(&mysql); row = mysql_fetch_row(res); c.mobileNo = row[0]; c.name = row[1]; c.passWord = row[2]; c.birth = row[3]; c.address = row[4]; c.city = row[5]; c.eMail = row[6]; c.amountOutstanding = row[7]; // c.amountOutstanding = 0.0f; mysql_close(&mysql); delete [] qbuff; return c; } bool isPasswordValid(string m, string p) { char *qbuff; qbuff = new char[255]; MYSQL mysql; MYSQL_RES *res; MYSQL_ROW row; char mobileNo[11]; string pass; int mlen = m.length(); for(int ctr = 0; ctr < mlen; ctr++) { mobileNo[ctr] = m.at(ctr); } mobileNo[11] = '\0'; // cout << endl << "mobileNo = " << mobileNo << endl; // cout << endl << "password given = " << p << endl; if(!mysql_init(&mysql)) cout << endl << "Can not initialize MYSQL" << mysql_error(&mysql); if(!mysql_real_connect(&mysql, NULL, "root", NULL, "Dyna", 0, 0, 0)) cout << endl << "Can not connect to MYSQL." << mysql_error(&mysql); sprintf(qbuff, "select * from Customer where cMobileNo = '%s'", mobileNo); if(!mysql_query(&mysql, qbuff)) cout << endl << mysql_error(&mysql) << endl; res = mysql_store_result(&mysql); row = mysql_fetch_row(res); pass = row[2]; // cout << endl << "DB password = " << pass << endl; mysql_close(&mysql); delete [] qbuff; if(pass == p) { return true; } else { return false; } } int isNumberAlreadyUsed(string c) { char *qbuff; qbuff = new char[255]; MYSQL mysql; MYSQL_RES *res; MYSQL_ROW row; char mobileNo[11]; int len = c.length(); int numRows = 0; if(len == 0) { // cout << endl << "return 10" << endl; return 10; } for(int ctr = 0; ctr < len; ctr++) { mobileNo[ctr] = c.at(ctr); } mobileNo[11] = '\0'; // cout << endl << "mobileNo = " << mobileNo << endl; if(!mysql_init(&mysql)) cout << endl << "Can not initialize MYSQL." << mysql_error(&mysql) << endl; if(!mysql_real_connect(&mysql, NULL, "root", NULL, "Dyna", 0, 0, 0)) cout << endl << "Can not connect to MYSQL" << mysql_error(&mysql) << endl; sprintf(qbuff, "select * from Customer where cMobileNo = '%s'", mobileNo); if(!mysql_query(&mysql, qbuff)) cout << endl << mysql_error(&mysql) << endl; res = mysql_store_result(&mysql); // cout << endl << "after res" << endl; // cout << endl << "affected rows = " << mysql_affected_rows(&mysql); numRows = mysql_num_rows(res); // cout << endl << "numRows = " << numRows << endl; if(numRows == 1) { // cout << endl << "return 20" << endl; return 20; } else { // cout << endl << "return 30" << endl; return 30; } mysql_close(&mysql); delete [] qbuff; } class WebProcess { string input, dest[7], errorMessage; Customer cust; public: void get() { input.assign(getenv("QUERY_STRING")); cout << "Content-type: text/html" << endl << endl; } void replaceAll(const string &findstr, const string &replstr) { int found = input.find(findstr); while(found != string::npos) { input.replace(found, findstr.length(), replstr); found = input.find(findstr); } } void urlDecode() { replaceAll("%2C", ","); // replaceAll("%27", "\''"); replaceAll("%40", "@"); replaceAll("+", " "); split(); } void split() { int count = 0; char *p; p = strtok(input.begin(), "&"); while(p != NULL) { dest[count] = p; count++; p = strtok(0, "&"); } for(int counter = 0; counter != count; counter++) { dest[counter].erase(0, dest[counter].find("=") + 1); cust.mobileNo = dest[0]; cust.name = dest[1]; cust.passWord = dest[2]; cust.birth = dest[3]; cust.address = dest[4]; cust.city = dest[5]; cust.eMail = dest[6]; cust.amountOutstanding = dest[7]; } } void checkRecord() { int msg = 0; msg = isNumberAlreadyUsed(cust.mobileNo); if(msg == 10) { displayStart("Mobile phone number cannot be empty."); } else { if(msg == 20) { if(isPasswordValid(cust.mobileNo, cust.name) == true) { cust = getDetails(cust.mobileNo); displayDetails(); } else { displayStart("Password is invalid."); } } else { displayStart("Mobile phone number not found."); } } } void displayStart(string msg) { cout << ""; cout << "

Dyna Telecommunications Inc.

"; cout << "

" << msg << "

"; cout << endl << "
"; cout << "Welcome to the Dyna Telecommunications web Site. To begin the transaction, please choose from the two options given below."; cout << "

New Customer

"; cout << "
"; cout << "
          Mobile Telephone Number: 
"; cout << "
"; cout << endl << "
"; cout << "

Existing Customer

"; cout << "
"; cout << "
          Mobile Telephone Number: 
Password:
"; cout << "
"; cout << ""; } void displayDetails() { cout << ""; cout << "

Dyna Telecommunications Inc.

"; cout << "Please update the information given below. Birth date should be entered as yyyy-mm-dd."; cout << "

Update Form

"; cout << "
"; cout << "
"
         << "         Mobile Telephone Number: 
\n" << " Customer Name:
\n" << " Password:
\n" << " Date of Birth:   yyyy-mm-dd
\n" << " Address:
\n" << " City:
\n" << " Email:
\n" << " Amount Outstanding:
\n" << "
"; cout << "
"; cout << ""; } }; int main() { WebProcess web; web.get(); web.urlDecode(); web.split(); web.checkRecord(); return 0; }