PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/include/login/controller/class.logincontroller.inc

https://bitbucket.org/cs1193/itrix2013
PHP | 563 lines | 504 code | 59 blank | 0 comment | 56 complexity | 94017c14584e49dbc70d169bb16d2523 MD5 | raw file
  1. <?
  2. class LoginController
  3. {
  4. var $username;
  5. var $password;
  6. var $hostname;
  7. var $database;
  8. var $connection;
  9. var $tablename;
  10. var $errormessage;
  11. var $from_address;
  12. function IntiateDatabase($hostname,$username,$password,$database,$tablename)
  13. {
  14. $this->hostname = $hostname;
  15. $this->username = $username;
  16. $this->password = $password;
  17. $this->database = $database;
  18. $this->tablename = $tablename;
  19. }
  20. function HandleError($err)
  21. {
  22. $this->error_message .= $err."\r\n";
  23. }
  24. function HandleDBError($err)
  25. {
  26. $this->HandleError($err."\r\n mysqlerror:".mysql_error());
  27. }
  28. function DBLogin()
  29. {
  30. $this->connection = mysql_connect($this->hostname,$this->username,$this->password);
  31. if(!$this->connection)
  32. {
  33. $this->HandleDBError("Database Login failed! Please make sure that the DB login credentials provided are correct");
  34. return false;
  35. }
  36. if(!mysql_select_db($this->database, $this->connection))
  37. {
  38. $this->HandleDBError('Failed to select database: '.$this->database.' Please make sure that the database name provided is correct');
  39. return false;
  40. }
  41. if(!mysql_query("SET NAMES 'UTF8'",$this->connection))
  42. {
  43. $this->HandleDBError('Error setting utf8 encoding');
  44. return false;
  45. }
  46. return true;
  47. }
  48. function Ensuretable()
  49. {
  50. $result = mysql_query("SHOW COLUMNS FROM $this->tablename");
  51. if(!$result || mysql_num_rows($result) <= 0)
  52. {
  53. return $this->CreateTable();
  54. }
  55. return true;
  56. }
  57. function CreateTable()
  58. {
  59. $qry = "CREATE TABLE IF NOT EXISTS $this->tablename (".
  60. "id int(6) NOT NULL AUTO_INCREMENT,".
  61. "facebookid varchar(1000) NOT NULL DEFAULT '',".
  62. "name varchar(1000) NOT NULL DEFAULT '',".
  63. "email varchar(1000) NOT NULL DEFAULT '',".
  64. "gender varchar(1000) NOT NULL DEFAULT '',".
  65. "birthday varchar(1000) NOT NULL DEFAULT '',".
  66. "timezone varchar(1000) NOT NULL DEFAULT '',".
  67. "PRIMARY KEY (id),".
  68. "UNIQUE KEY id (id),".
  69. "UNIQUE KEY email (email)".
  70. ")ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
  71. if(!mysql_query($qry,$this->connection))
  72. {
  73. $this->HandleDBError("Error creating the table \nquery was\n $qry");
  74. return false;
  75. }
  76. return true;
  77. }
  78. function SanitizeForSQL($str)
  79. {
  80. if( function_exists( "mysql_real_escape_string" ) )
  81. {
  82. $ret_str = mysql_real_escape_string( $str );
  83. }
  84. else
  85. {
  86. $ret_str = addslashes( $str );
  87. }
  88. return $ret_str;
  89. }
  90. function Sanitize($str,$remove_nl=true)
  91. {
  92. $str = $this->StripSlashes($str);
  93. if($remove_nl)
  94. {
  95. $injections = array('/(\n+)/i','/(\r+)/i','/(\t+)/i','/(%0A+)/i','/(%0D+)/i','/(%08+)/i','/(%09+)/i');
  96. $str = preg_replace($injections,'',$str);
  97. }
  98. return $str;
  99. }
  100. function StripSlashes($str)
  101. {
  102. if(get_magic_quotes_gpc())
  103. {
  104. $str = stripslashes($str);
  105. }
  106. return $str;
  107. }
  108. function SaveToDatabase(&$formvars)
  109. {
  110. if(!$this->DBLogin())
  111. {
  112. $this->HandleError("Database login failed!");
  113. return false;
  114. }
  115. if(!$this->Ensuretable())
  116. {
  117. return false;
  118. }
  119. if(!$this->IsFieldUnique($formvars,'email'))
  120. {
  121. $this->HandleError("This email is already registered");
  122. return false;
  123. }
  124. if(!$this->InsertIntoDB($formvars))
  125. {
  126. $this->HandleError("Inserting to Database failed!");
  127. return false;
  128. }
  129. if(!$this->SendUserWelcomeEmail($formvars))
  130. {
  131. return false;
  132. }
  133. return true;
  134. }
  135. function IsFieldUnique($formvars,$fieldname)
  136. {
  137. $field_val = $this->SanitizeForSQL($formvars[$fieldname]);
  138. $qry = "SELECT email FROM $this->tablename WHERE $fieldname='".$field_val."'";
  139. $result = mysql_query($qry,$this->connection);
  140. if($result && mysql_num_rows($result) > 0)
  141. {
  142. return false;
  143. }
  144. return true;
  145. }
  146. function InsertIntoDB(&$formvars)
  147. {
  148. $uniqueid = $this->GetUniqueId();
  149. $insert_query = 'INSERT INTO '.$this->tablename.'(itrixid, name, email, gender) VALUES ("'.$uniqueid.'","' . $this->SanitizeForSQL($formvars['name']) . '","' . $this->SanitizeForSQL($formvars['email']) . '","' . $this->SanitizeForSQL($formvars['gender']) . '")';
  150. if(!mysql_query( $insert_query ,$this->connection))
  151. {
  152. $this->HandleDBError("Error inserting data to the table\nquery:$insert_query");
  153. return false;
  154. }
  155. return true;
  156. }
  157. function SendUserWelcomeEmail(&$user_rec)
  158. {
  159. $To=$user_rec['email'];
  160. $Subject = "Welcome to ITRIX 2013";
  161. $From = $this->GetFromAddress();
  162. $Body ="<html>".
  163. "<head>".
  164. "<title>Welcome to ITRIX 2013</title>".
  165. "</head>".
  166. "<body style=\"background-color: rgba(0,0,0,0.5);\">".
  167. "<img src=\"http://test.itrix2013.heliohost.org/images/logo.png\" class=\"logo\" width=\"250\" height=\"100\" style=\"position: relative; display: block; float: left; width: 250px; height: 100px; padding: 10px;\"><br>".
  168. "<div style=\"float: left; width:100%; min-height: 200px; overflow: auto; background-color: white; \">".
  169. "Hello ".$user_rec['name'].",<br><br>".
  170. "Welcome! Your registration with ITRIX 2013 is completed.<br>".
  171. "<br>".
  172. "Regards,<br>".
  173. "Web Coordinator<br>".
  174. "</div>".
  175. "</body>".
  176. "</html>";
  177. $Headers = "MIME-Version: 1.0\r\n";
  178. $Headers .= "Content-type: text/html; charset=utf-8\r\n";
  179. $Headers .= "From: ITRIX 2013 <$From> \r\n";
  180. $Headers .= "Reply-To: $From \r\n";
  181. $Headers .= "Return-Path: $From\r\n";
  182. $Headers .= "X-Mailer: PHP \r\n";
  183. if(!mail($To,$Subject,$Body,$Headers))
  184. {
  185. $this->HandleError("Failed sending user welcome email.");
  186. return false;
  187. }
  188. return true;
  189. }
  190. function GetFromAddress()
  191. {
  192. if(!empty($this->from_address))
  193. {
  194. return $this->from_address;
  195. }
  196. $from ="webmaster@itrix.in";
  197. return $from;
  198. }
  199. function UpdateIntoField(&$formvars,$fieldname,$fieldvalue)
  200. {
  201. if(!$this->DBLogin())
  202. {
  203. $this->HandleError("Database login failed!");
  204. return false;
  205. }
  206. $insert_query = "UPDATE ".$this->tablename." SET ".$fieldname." = '" . $this->SanitizeForSQL($fieldvalue) . "' WHERE email='".$formvars['email']."'";
  207. if(!mysql_query( $insert_query ,$this->connection))
  208. {
  209. $this->HandleDBError("Error inserting data to the table\nquery:$insert_query");
  210. return false;
  211. }
  212. return true;
  213. }
  214. function GetLoginCount(&$formvars)
  215. {
  216. $emailaddress = $formvars['email'];
  217. $get_query = "SELECT * FROM $this->tablename WHERE email = '".$emailaddress."'";
  218. $get_result = mysql_query( $get_query ,$this->connection);
  219. $get_row = mysql_fetch_assoc($get_result);
  220. return $get_row['logincount'];
  221. }
  222. function SetLoginCount(&$formvars)
  223. {
  224. $counter = $this->GetLoginCount(&$formvars);
  225. $emailaddress = $formvars['email'];
  226. $counter=$counter+1;
  227. $update_query = "UPDATE $this->tablename SET logincount =".$counter." WHERE email = '".$emailaddress."'";
  228. if(!mysql_query( $update_query ,$this->connection))
  229. {
  230. $this->HandleDBError("Error inserting data to the table\nquery:$update_query");
  231. return false;
  232. }
  233. return true;
  234. }
  235. function GetUserData($key,$returnvalue)
  236. {
  237. $get_query = "SELECT * FROM $this->tablename WHERE email = '".$key."'";
  238. $get_result = mysql_query( $get_query ,$this->connection);
  239. $get_row = mysql_fetch_assoc($get_result);
  240. return $get_row[$returnvalue];
  241. }
  242. function RandomKey()
  243. {
  244. $guid = '';
  245. $uid = uniqid("", true);
  246. $data = $namespace;
  247. $data .= $_SERVER['REQUEST_TIME'];
  248. $data .= $_SERVER['HTTP_USER_AGENT'];
  249. $data .= $_SERVER['LOCAL_ADDR'];
  250. $data .= $_SERVER['LOCAL_PORT'];
  251. $data .= $_SERVER['REMOTE_ADDR'];
  252. $data .= $_SERVER['REMOTE_PORT'];
  253. $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
  254. $guid = '{' .
  255. substr($hash, 0, 8) .
  256. '-' .
  257. substr($hash, 8, 4) .
  258. '-' .
  259. substr($hash, 12, 4) .
  260. '-' .
  261. substr($hash, 16, 4) .
  262. '-' .
  263. substr($hash, 20, 12) .
  264. '}';
  265. return $guid;
  266. }
  267. function GetUniqueId()
  268. {
  269. $id = $this->RandomKey();
  270. $id = md5($id);
  271. $id = substr($id, 0, 5);
  272. $id = "ITRIX".$id;
  273. return $id;
  274. }
  275. function CheckWorkshopRegistration($emailaddress,$workshop)
  276. {
  277. $emailaddress = $this->SanitizeForSQL($emailaddress);
  278. $workshop = $this->SanitizeForSQL($workshop);
  279. $qry = "SELECT * FROM $workshop WHERE email='".$emailaddress."'";
  280. $result = mysql_query($qry,$this->connection);
  281. if($result && mysql_num_rows($result) > 0)
  282. {
  283. return true;
  284. }
  285. return false;
  286. }
  287. function InsertIntoWorkshop(&$formvars,$workshopname)
  288. {
  289. $workshopname = $this->SanitizeForSQL($workshopname);
  290. $insert_query = 'INSERT INTO '.$workshopname.'(email, bank, branch, ddnumber, amount) VALUES ("'.$this->SanitizeForSQL($formvars['email']).'","' . $this->SanitizeForSQL($formvars['bank']) . '","' . $this->SanitizeForSQL($formvars['branch']) . '","' . $this->SanitizeForSQL($formvars['ddnumber']) . '","' . $this->SanitizeForSQL($formvars['amount']) . '")';
  291. if(!mysql_query( $insert_query ,$this->connection))
  292. {
  293. $this->HandleDBError("Error inserting data to the table\nquery:$insert_query");
  294. return false;
  295. }
  296. return true;
  297. }
  298. function SaveToNormalLogin(&$formvars)
  299. {
  300. if(!$this->DBLogin())
  301. {
  302. $this->HandleError("Database login failed!");
  303. return false;
  304. }
  305. if(!$this->IsFieldUnique($formvars,'email'))
  306. {
  307. $this->HandleError("This email is already registered");
  308. return false;
  309. }
  310. if(!$this->InsertIntoNormalDB($formvars))
  311. {
  312. $this->HandleError("Inserting to Database failed!");
  313. return false;
  314. }
  315. if(!$this->SendUserWelcomeEmail($formvars))
  316. {
  317. return false;
  318. }
  319. return true;
  320. }
  321. function InsertIntoNormalDB(&$formvars)
  322. {
  323. $uniqueid = $this->GetUniqueId();
  324. $insert_query = 'INSERT INTO '.$this->tablename.'(itrixid, name, email, password, gender) VALUES ("'.$uniqueid.'","' . $this->SanitizeForSQL($formvars['name']) . '","' . $this->SanitizeForSQL($formvars['email']) . '","' . $this->SanitizeForSQL($formvars['password']) . '","' . $this->SanitizeForSQL($formvars['gender']) . '")';
  325. if(!mysql_query( $insert_query ,$this->connection))
  326. {
  327. $this->HandleDBError("Error inserting data to the table\nquery:$insert_query");
  328. return false;
  329. }
  330. return true;
  331. }
  332. function CheckLoginInDB(&$formvars)
  333. {
  334. if(!$this->DBLogin())
  335. {
  336. $this->HandleError("Database login failed!");
  337. return false;
  338. }
  339. $formvars['email'] = $this->SanitizeForSQL($formvars['email']);
  340. $formvars['password'] = $this->SanitizeForSQL($formvars['password']);
  341. if(empty($formvars['email']))
  342. {
  343. $this->HandleError("Email Address not given");
  344. return false;
  345. }
  346. if(empty($formvars['password']))
  347. {
  348. $this->HandleError("Password empty");
  349. return false;
  350. }
  351. $qry = "SELECT * FROM ".$this->tablename." WHERE email='".$formvars['email']."' AND password='".$formvars['password']."'";
  352. $result = mysql_query($qry,$this->connection);
  353. if(!$result || mysql_num_rows($result) <= 0)
  354. {
  355. $this->HandleError("Error logging in. The username or password does not match");
  356. return false;
  357. }
  358. return true;
  359. }
  360. function GetNormalUserInformation(&$formvars)
  361. {
  362. if(!$this->DBLogin())
  363. {
  364. $this->HandleError("Database login failed!");
  365. return false;
  366. }
  367. $formvars['email'] = $this->SanitizeForSQL($formvars['email']);
  368. $qry = "SELECT * FROM ".$this->tablename." WHERE email='".$formvars['email']."'";
  369. $result = mysql_query($qry,$this->connection);
  370. if(!$result || mysql_num_rows($result) <= 0)
  371. {
  372. $this->HandleError("Error logging in. The username or password does not match");
  373. return false;
  374. }
  375. $row = mysql_fetch_assoc($result);
  376. return $row;
  377. }
  378. function CheckIfProfileComplete(&$formvars)
  379. {
  380. if(!$this->DBLogin())
  381. {
  382. $this->HandleError("Database login failed!");
  383. return false;
  384. }
  385. $formvars['email'] = $this->SanitizeForSQL($formvars['email']);
  386. $qry = "SELECT * FROM ".$this->tablename." WHERE email='".$formvars['email']."'";
  387. $result = mysql_query($qry,$this->connection);
  388. if(!$result || mysql_num_rows($result) <= 0)
  389. {
  390. $this->HandleError("Error logging in. The username or password does not match");
  391. return false;
  392. }
  393. $row = mysql_fetch_assoc($result);
  394. if($row['name'] == '' || $row['phonenumber'] == '' || $row['organisationname'] == '')
  395. {
  396. $this->HandleError("Error");
  397. return false;
  398. }
  399. return true;
  400. }
  401. function SendBigDataWorkshopEmail(&$user_rec)
  402. {
  403. $To=$user_rec['email'];
  404. $Subject = "Registration for Big Data Workshop | ITRIX 2013";
  405. $From = $this->GetFromAddress();
  406. $Body ="<html>".
  407. "<head>".
  408. "<title>BIG DATA Workshop</title>".
  409. "</head>".
  410. "<body style=\"background-color: rgba(0,0,0,0.5);\">".
  411. "<img src=\"http://test.itrix2013.heliohost.org/images/logo.png\" class=\"logo\" width=\"250\" height=\"100\" style=\"position: relative; display: block; float: left; width: 250px; height: 100px; padding: 10px;\"><br>".
  412. "<div style=\"float: left; width:100%; min-height: 200px; overflow: auto; background-color: white; \">".
  413. "Hello ".$user_rec['name'].",<br><br>".
  414. "Thank you for registering with Big Data Workshop<br>".
  415. "Upon receipt of your DD, we will send confirmation mail.<br>".
  416. "<br>".
  417. "Regards,<br>".
  418. "Web Coordinator<sub>(for BIG DATA Workshop)</sub><br>".
  419. "</div>".
  420. "</body>".
  421. "</html>";
  422. $Headers = "MIME-Version: 1.0\r\n";
  423. $Headers .= "Content-type: text/html; charset=utf-8\r\n";
  424. $Headers .= "From: ITRIX 2013 <$From> \r\n";
  425. $Headers .= "Reply-To: $From \r\n";
  426. $Headers .= "Return-Path: $From\r\n";
  427. $Headers .= "X-Mailer: PHP \r\n";
  428. if(!mail($To,$Subject,$Body,$Headers))
  429. {
  430. $this->HandleError("Failed sending user welcome email.");
  431. return false;
  432. }
  433. return true;
  434. }
  435. function SendCyberForensicsWorkshopEmail(&$user_rec)
  436. {
  437. $To=$user_rec['email'];
  438. $Subject = "Registration for Cyber Forensics Workshop | ITRIX 2013";
  439. $From = $this->GetFromAddress();
  440. $Body ="<html>".
  441. "<head>".
  442. "<title>CYBER FORENSICS Workshop</title>".
  443. "</head>".
  444. "<body style=\"background-color: rgba(0,0,0,0.5);\">".
  445. "<img src=\"http://test.itrix2013.heliohost.org/images/logo.png\" class=\"logo\" width=\"250\" height=\"100\" style=\"position: relative; display: block; float: left; width: 250px; height: 100px; padding: 10px;\"><br>".
  446. "<div style=\"float: left; width:100%; min-height: 200px; overflow: auto; background-color: white; \">".
  447. "Hello ".$user_rec['name'].",<br><br>".
  448. "Thank you for registering with Cyber Forensics Workshop<br>".
  449. "Upon receipt of your DD, we will send confirmation mail.<br>".
  450. "<br>".
  451. "Regards,<br>".
  452. "Web Coordinator<sub>(for CYBER FORENSICS Workshop)</sub><br>".
  453. "</div>".
  454. "</body>".
  455. "</html>";
  456. $Headers = "MIME-Version: 1.0\r\n";
  457. $Headers .= "Content-type: text/html; charset=utf-8\r\n";
  458. $Headers .= "From: ITRIX 2013 <$From> \r\n";
  459. $Headers .= "Reply-To: $From \r\n";
  460. $Headers .= "Return-Path: $From\r\n";
  461. $Headers .= "X-Mailer: PHP \r\n";
  462. if(!mail($To,$Subject,$Body,$Headers))
  463. {
  464. $this->HandleError("Failed sending user welcome email.");
  465. return false;
  466. }
  467. return true;
  468. }
  469. function SendWin8AppDevWorkshopEmail(&$user_rec)
  470. {
  471. $To=$user_rec['email'];
  472. $Subject = "Registration for Windows 8 App Development Workshop | ITRIX 2013";
  473. $From = $this->GetFromAddress();
  474. $Body ="<html>".
  475. "<head>".
  476. "<title>WINDOWS 8 APP DEVELOPMENT Workshop</title>".
  477. "</head>".
  478. "<body style=\"background-color: rgba(0,0,0,0.5);\">".
  479. "<img src=\"http://test.itrix2013.heliohost.org/images/logo.png\" class=\"logo\" width=\"250\" height=\"100\" style=\"position: relative; display: block; float: left; width: 250px; height: 100px; padding: 10px;\"><br>".
  480. "<div style=\"float: left; width:100%; min-height: 200px; overflow: auto; background-color: white; \">".
  481. "Hello ".$user_rec['name'].",<br><br>".
  482. "Thank you for registering with Windows 8 App Development<br>".
  483. "Upon receipt of your DD, we will send confirmation mail.<br>".
  484. "<br>".
  485. "Regards,<br>".
  486. "Web Coordinator<sub>(for WINDOWS 8 APP DEVELOPMENT Workshop)</sub><br>".
  487. "</div>".
  488. "</body>".
  489. "</html>";
  490. $Headers = "MIME-Version: 1.0\r\n";
  491. $Headers .= "Content-type: text/html; charset=utf-8\r\n";
  492. $Headers .= "From: ITRIX 2013 <$From> \r\n";
  493. $Headers .= "Reply-To: $From \r\n";
  494. $Headers .= "Return-Path: $From\r\n";
  495. $Headers .= "X-Mailer: PHP \r\n";
  496. if(!mail($To,$Subject,$Body,$Headers))
  497. {
  498. $this->HandleError("Failed sending user welcome email.");
  499. return false;
  500. }
  501. return true;
  502. }
  503. }
  504. ?>