dadslands

member php

Sep 28th, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.93 KB | None | 0 0
  1. <?PHP
  2. require_once("class.phpmailer.php");
  3. require_once("formvalidator.php");
  4.  
  5. class FGMembersite
  6. {
  7. var $admin_email;
  8. var $from_address;
  9.  
  10. var $username;
  11. var $pwd;
  12. var $database;
  13. var $tablename;
  14. var $connection;
  15. var $rand_key;
  16.  
  17. var $error_message;
  18.  
  19. //-----Initialization -------
  20. function FGMembersite()
  21. {
  22. $this->sitename = 'YourWebsiteName.com';
  23. $this->rand_key = '0iQx5oBk66oVZep';
  24. }
  25.  
  26. function InitDB($host,$uname,$pwd,$database,$tablename)
  27. {
  28. $this->db_host = $host;
  29. $this->username = $uname;
  30. $this->pwd = $pwd;
  31. $this->database = $database;
  32. $this->tablename = $tablename;
  33.  
  34. }
  35. function SetAdminEmail($email)
  36. {
  37. $this->admin_email = $email;
  38. }
  39.  
  40. function SetWebsiteName($sitename)
  41. {
  42. $this->sitename = $sitename;
  43. }
  44.  
  45. function SetRandomKey($key)
  46. {
  47. $this->rand_key = $key;
  48. }
  49.  
  50. //-------Main Operations ----------------------
  51. function RegisterUser()
  52. {
  53. if(!isset($_POST['submitted']))
  54. {
  55. return false;
  56. }
  57.  
  58. $formvars = array();
  59.  
  60. if(!$this->ValidateRegistrationSubmission())
  61. {
  62. return false;
  63. }
  64.  
  65. $this->CollectRegistrationSubmission($formvars);
  66.  
  67. if(!$this->SaveToDatabase($formvars))
  68. {
  69. return false;
  70. }
  71.  
  72. if(!$this->SendUserConfirmationEmail($formvars))
  73. {
  74. return false;
  75. }
  76.  
  77. $this->SendAdminIntimationEmail($formvars);
  78.  
  79. return true;
  80. }
  81.  
  82. function ConfirmUser()
  83. {
  84. if(empty($_GET['code'])||strlen($_GET['code'])<=10)
  85. {
  86. $this->HandleError("You did not enter the confirmation code!");
  87. return false;
  88. }
  89. $user_rec = array();
  90. if(!$this->UpdateDBRecForConfirmation($user_rec))
  91. {
  92. return false;
  93. }
  94.  
  95. $this->SendUserWelcomeEmail($user_rec);
  96.  
  97. $this->SendAdminIntimationOnRegComplete($user_rec);
  98.  
  99. return true;
  100. }
  101.  
  102. function Login()
  103. {
  104. if(empty($_POST['username']))
  105. {
  106. $this->HandleError("UserName is empty!");
  107. return false;
  108. }
  109.  
  110. if(empty($_POST['password']))
  111. {
  112. $this->HandleError("Password is empty!");
  113. return false;
  114. }
  115.  
  116. $username = trim($_POST['username']);
  117. $password = trim($_POST['password']);
  118.  
  119. if(!isset($_SESSION)){ session_start(); }
  120. if(!$this->CheckLoginInDB($username,$password))
  121. {
  122. return false;
  123. }
  124.  
  125. $_SESSION[$this->GetLoginSessionVar()] = $username;
  126.  
  127. return true;
  128. }
  129.  
  130. function CheckLogin()
  131. {
  132. if(!isset($_SESSION)){ session_start(); }
  133.  
  134. $sessionvar = $this->GetLoginSessionVar();
  135.  
  136. if(empty($_SESSION[$sessionvar]))
  137. {
  138. return false;
  139. }
  140. return true;
  141. }
  142.  
  143. function UserFullName()
  144. {
  145. return isset($_SESSION['name_of_user'])?$_SESSION['name_of_user']:'';
  146. }
  147.  
  148. function UserEmail()
  149. {
  150. return isset($_SESSION['email_of_user'])?$_SESSION['email_of_user']:'';
  151. }
  152.  
  153. function LogOut()
  154. {
  155. session_start();
  156.  
  157. $sessionvar = $this->GetLoginSessionVar();
  158.  
  159. $_SESSION[$sessionvar]=NULL;
  160.  
  161. unset($_SESSION[$sessionvar]);
  162. }
  163.  
  164. function EmailResetPasswordLink()
  165. {
  166. if(empty($_POST['email']))
  167. {
  168. $this->HandleError("Email is empty!");
  169. return false;
  170. }
  171. $user_rec = array();
  172. if(false === $this->GetUserFromEmail($_POST['email'], $user_rec))
  173. {
  174. return false;
  175. }
  176. if(false === $this->SendResetPasswordLink($user_rec))
  177. {
  178. return false;
  179. }
  180. return true;
  181. }
  182.  
  183. function ResetPassword()
  184. {
  185. if(empty($_GET['email']))
  186. {
  187. $this->HandleError("Email address to reset the password for was not provided!");
  188. return false;
  189. }
  190. if(empty($_GET['code']))
  191. {
  192. $this->HandleError("No reset code was supplied!");
  193. return false;
  194. }
  195. $email = trim($_GET['email']);
  196. $code = trim($_GET['code']);
  197.  
  198. if($this->GetResetPasswordCode($email) != $code)
  199. {
  200. $this->HandleError("Bad reset code!");
  201. return false;
  202. }
  203.  
  204. $user_rec = array();
  205. if(!$this->GetUserFromEmail($email,$user_rec))
  206. {
  207. return false;
  208. }
  209.  
  210. $new_password = $this->ResetUserPasswordInDB($user_rec);
  211. if(false === $new_password || empty($new_password))
  212. {
  213. $this->HandleError("Error updating new password");
  214. return false;
  215. }
  216.  
  217. if(false == $this->SendNewPassword($user_rec,$new_password))
  218. {
  219. $this->HandleError("Error sending new password");
  220. return false;
  221. }
  222. return true;
  223. }
  224.  
  225. function ChangePassword()
  226. {
  227. if(!$this->CheckLogin())
  228. {
  229. $this->HandleError("Not logged in!");
  230. return false;
  231. }
  232.  
  233. if(empty($_POST['oldpwd']))
  234. {
  235. $this->HandleError("Old password is empty!");
  236. return false;
  237. }
  238. if(empty($_POST['newpwd']))
  239. {
  240. $this->HandleError("New password is empty!");
  241. return false;
  242. }
  243.  
  244. $user_rec = array();
  245. if(!$this->GetUserFromEmail($this->UserEmail(),$user_rec))
  246. {
  247. return false;
  248. }
  249.  
  250. $pwd = trim($_POST['oldpwd']);
  251.  
  252. if($user_rec['password'] != md5($pwd))
  253. {
  254. $this->HandleError("Your current password you have entered is incorrect!");
  255. return false;
  256. }
  257. $newpwd = trim($_POST['newpwd']);
  258.  
  259. if(!$this->ChangePasswordInDB($user_rec, $newpwd))
  260. {
  261. return false;
  262. }
  263. return true;
  264. }
  265.  
  266. //-------Public Helper functions -------------
  267. function GetSelfScript()
  268. {
  269. return htmlentities($_SERVER['PHP_SELF']);
  270. }
  271.  
  272. function SafeDisplay($value_name)
  273. {
  274. if(empty($_POST[$value_name]))
  275. {
  276. return'';
  277. }
  278. return htmlentities($_POST[$value_name]);
  279. }
  280.  
  281. function RedirectToURL($url)
  282. {
  283. header("Location: $url");
  284. exit;
  285. }
  286.  
  287. function GetSpamTrapInputName()
  288. {
  289. return 'sp'.md5('KHGdnbvsgst'.$this->rand_key);
  290. }
  291.  
  292. function GetErrorMessage()
  293. {
  294. if(empty($this->error_message))
  295. {
  296. return '';
  297. }
  298. $errormsg = nl2br(htmlentities($this->error_message));
  299. return $errormsg;
  300. }
  301. //-------Private Helper functions-----------
  302.  
  303. function HandleError($err)
  304. {
  305. $this->error_message .= $err."\r\n";
  306. }
  307.  
  308. function HandleDBError($err)
  309. {
  310. $this->HandleError($err."\r\n mysqlerror:".mysql_error());
  311. }
  312.  
  313. function GetFromAddress()
  314. {
  315. if(!empty($this->from_address))
  316. {
  317. return $this->from_address;
  318. }
  319.  
  320. $host = $_SERVER['SERVER_NAME'];
  321.  
  322. $from ="noreply@$host";
  323. return $from;
  324. }
  325.  
  326. function GetLoginSessionVar()
  327. {
  328. $retvar = md5($this->rand_key);
  329. $retvar = 'usr_'.substr($retvar,0,10);
  330. return $retvar;
  331. }
  332.  
  333. function CheckLoginInDB($username,$password)
  334. {
  335. if(!$this->DBLogin())
  336. {
  337. $this->HandleError("Database login failed!");
  338. return false;
  339. }
  340. $username = $this->SanitizeForSQL($username);
  341. $pwdmd5 = md5($password);
  342. $qry = "Select name, email from $this->tablename where username='$username' and password='$pwdmd5' and confirmcode='y'";
  343.  
  344. $result = mysql_query($qry,$this->connection);
  345.  
  346. if(!$result || mysql_num_rows($result) <= 0)
  347. {
  348. $this->HandleError("Access Denied. The username or password do not match!");
  349. return false;
  350. }
  351.  
  352. $row = mysql_fetch_assoc($result);
  353.  
  354.  
  355. $_SESSION['name_of_user'] = $row['name'];
  356. $_SESSION['email_of_user'] = $row['email'];
  357.  
  358. return true;
  359. }
  360.  
  361. function UpdateDBRecForConfirmation(&$user_rec)
  362. {
  363. if(!$this->DBLogin())
  364. {
  365. $this->HandleError("Database login failed!");
  366. return false;
  367. }
  368. $confirmcode = $this->SanitizeForSQL($_GET['code']);
  369.  
  370. $result = mysql_query("Select name, email from $this->tablename where confirmcode='$confirmcode'",$this->connection);
  371. if(!$result || mysql_num_rows($result) <= 0)
  372. {
  373. $this->HandleError("You have provided an incorrect confirmation code.");
  374. return false;
  375. }
  376. $row = mysql_fetch_assoc($result);
  377. $user_rec['name'] = $row['name'];
  378. $user_rec['email']= $row['email'];
  379.  
  380. $qry = "Update $this->tablename Set confirmcode='y' Where confirmcode='$confirmcode'";
  381.  
  382. if(!mysql_query( $qry ,$this->connection))
  383. {
  384. $this->HandleDBError("Error inserting data to the table\nquery:$qry");
  385. return false;
  386. }
  387. return true;
  388. }
  389.  
  390. function ResetUserPasswordInDB($user_rec)
  391. {
  392. $new_password = substr(md5(uniqid()),0,10);
  393.  
  394. if(false == $this->ChangePasswordInDB($user_rec,$new_password))
  395. {
  396. return false;
  397. }
  398. return $new_password;
  399. }
  400.  
  401. function ChangePasswordInDB($user_rec, $newpwd)
  402. {
  403. $newpwd = $this->SanitizeForSQL($newpwd);
  404.  
  405. $qry = "Update $this->tablename Set password='".md5($newpwd)."' Where id_user=".$user_rec['id_user']."";
  406.  
  407. if(!mysql_query( $qry ,$this->connection))
  408. {
  409. $this->HandleDBError("Error updating the password \nquery:$qry");
  410. return false;
  411. }
  412. return true;
  413. }
  414.  
  415. function GetUserFromEmail($email,&$user_rec)
  416. {
  417. if(!$this->DBLogin())
  418. {
  419. $this->HandleError("Database login failed!");
  420. return false;
  421. }
  422. $email = $this->SanitizeForSQL($email);
  423.  
  424. $result = mysql_query("Select * from $this->tablename where email='$email'",$this->connection);
  425.  
  426. if(!$result || mysql_num_rows($result) <= 0)
  427. {
  428. $this->HandleError("There is no user with email: $email");
  429. return false;
  430. }
  431. $user_rec = mysql_fetch_assoc($result);
  432.  
  433.  
  434. return true;
  435. }
  436.  
  437. function SendUserWelcomeEmail(&$user_rec)
  438. {
  439. $mailer = new PHPMailer();
  440.  
  441. $mailer->CharSet = 'utf-8';
  442.  
  443. $mailer->AddAddress($user_rec['email'],$user_rec['name']);
  444.  
  445. $mailer->Subject = "Welcome to ".$this->sitename;
  446.  
  447. $mailer->From = $this->GetFromAddress();
  448.  
  449. $mailer->Body ="Hello ".$user_rec['name']."\r\n\r\n".
  450. "Welcome! Your registration with ".$this->sitename." is now completed.\r\n".
  451. "You may now login to access your account.\r\n".
  452. "\r\n".
  453. "---------------\r\n".
  454. "Donator Express - Minecraft Bukkit Plugin\r\n".
  455. "This email has been sent from: ".
  456. $this->sitename;
  457.  
  458. if(!$mailer->Send())
  459. {
  460. $this->HandleError("Failed sending user welcome email.");
  461. return false;
  462. }
  463. return true;
  464. }
  465.  
  466. function SendAdminIntimationOnRegComplete(&$user_rec)
  467. {
  468. if(empty($this->admin_email))
  469. {
  470. return false;
  471. }
  472. $mailer = new PHPMailer();
  473.  
  474. $mailer->CharSet = 'utf-8';
  475.  
  476. $mailer->AddAddress($this->admin_email);
  477.  
  478. $mailer->Subject = "Donator Express - Registration Completed: ".$user_rec['name'];
  479.  
  480. $mailer->From = $this->GetFromAddress();
  481.  
  482. $mailer->Body ="A new user has successfully confirmed their email at ".$this->sitename."\r\n\n".
  483. "Full Name: ".$user_rec['name']."\r\n".
  484. "Email Address: ".$user_rec['email']."\r\n".
  485. "\r\n".
  486. "---------------\r\n".
  487. "Donator Express - Minecraft Bukkit Plugin\r\n".
  488. "This email has been sent from: ".
  489. $this->sitename;
  490.  
  491. if(!$mailer->Send())
  492. {
  493. return false;
  494. }
  495. return true;
  496. }
  497.  
  498. function GetResetPasswordCode($email)
  499. {
  500. return substr(md5($email.$this->sitename.$this->rand_key),0,10);
  501. }
  502.  
  503. function SendResetPasswordLink($user_rec)
  504. {
  505. $email = $user_rec['email'];
  506.  
  507. $mailer = new PHPMailer();
  508.  
  509. $mailer->CharSet = 'utf-8';
  510.  
  511. $mailer->AddAddress($email,$user_rec['name']);
  512.  
  513. $mailer->Subject = "Password reset request at ".$this->sitename;
  514.  
  515. $mailer->From = $this->GetFromAddress();
  516.  
  517. $link = $this->GetAbsoluteURLFolder().
  518. '/resetpwd.php?email='.
  519. urlencode($email).'&code='.
  520. urlencode($this->GetResetPasswordCode($email));
  521.  
  522. $mailer->Body ="Hello ".$user_rec['name']."\r\n\r\n".
  523. "There was a request made to reset your password at ".$this->sitename."\r\n".
  524. "Please click the link below to complete the request: \r\n".$link."\r\n\n".
  525. "If you didn't make this request please ignore this email.\r\n\n".
  526. "---------------\r\n".
  527. "Donator Express - Minecraft Bukkit Plugin\r\n".
  528. "This email has been sent from: ".
  529. $this->sitename;
  530.  
  531. if(!$mailer->Send())
  532. {
  533. return false;
  534. }
  535. return true;
  536. }
  537.  
  538. function SendNewPassword($user_rec, $new_password)
  539. {
  540. $email = $user_rec['email'];
  541.  
  542. $mailer = new PHPMailer();
  543.  
  544. $mailer->CharSet = 'utf-8';
  545.  
  546. $mailer->AddAddress($email,$user_rec['name']);
  547.  
  548. $mailer->Subject = "Your new password for ".$this->sitename;
  549.  
  550. $mailer->From = $this->GetFromAddress();
  551.  
  552. $mailer->Body ="Hello ".$user_rec['name']."\r\n\r\n".
  553. "Your password has been reset successfully, ".
  554. "here is your updated login:\r\n\n".
  555. "Minecraft Username: ".$user_rec['username']."\r\n".
  556. "Password: $new_password\r\n".
  557. "\r\n".
  558. "You may login to your account here: ".$this->GetAbsoluteURLFolder()."/login.php\r\n".
  559. "\r\n".
  560. "---------------\r\n".
  561. "Donator Express - Minecraft Bukkit Plugin\r\n".
  562. "This email has been sent from: ".
  563. $this->sitename;
  564.  
  565. if(!$mailer->Send())
  566. {
  567. return false;
  568. }
  569. return true;
  570. }
  571.  
  572. function ValidateRegistrationSubmission()
  573. {
  574. //This is a hidden input field. Humans won't fill this field.
  575. if(!empty($_POST[$this->GetSpamTrapInputName()]) )
  576. {
  577. //The proper error is not given intentionally
  578. $this->HandleError("Automated submission prevention: case 2 failed");
  579. return false;
  580. }
  581.  
  582. $validator = new FormValidator();
  583. $validator->addValidation("name","req","Please fill in your Full Name");
  584. $validator->addValidation("email","email","Please enter a valid Email Address");
  585. $validator->addValidation("email","req","Please fill in your Email Address");
  586. $validator->addValidation("username","req","Please fill in your Minecraft Username");
  587. $validator->addValidation("password","req","Please fill in your Password");
  588.  
  589.  
  590. if(!$validator->ValidateForm())
  591. {
  592. $error='';
  593. $error_hash = $validator->GetErrors();
  594. foreach($error_hash as $inpname => $inp_err)
  595. {
  596. $error .= $inpname.':'.$inp_err."\n";
  597. }
  598. $this->HandleError($error);
  599. return false;
  600. }
  601. return true;
  602. }
  603.  
  604. function CollectRegistrationSubmission(&$formvars)
  605. {
  606. $formvars['name'] = $this->Sanitize($_POST['name']);
  607. $formvars['email'] = $this->Sanitize($_POST['email']);
  608. $formvars['username'] = $this->Sanitize($_POST['username']);
  609. $formvars['password'] = $this->Sanitize($_POST['password']);
  610. }
  611.  
  612. function SendUserConfirmationEmail(&$formvars)
  613. {
  614. $mailer = new PHPMailer();
  615.  
  616. $mailer->CharSet = 'utf-8';
  617.  
  618. $mailer->AddAddress($formvars['email'],$formvars['name']);
  619.  
  620. $mailer->Subject = "Your registration with ".$this->sitename;
  621.  
  622. $mailer->From = $this->GetFromAddress();
  623.  
  624. $confirmcode = $formvars['confirmcode'];
  625.  
  626. $confirm_url = $this->GetAbsoluteURLFolder().'/confirmreg.php?code='.$confirmcode;
  627.  
  628. $mailer->Body ="Hello ".$formvars['name']."\r\n\r\n".
  629. "Thank you for your registration with ".$this->sitename."\r\n".
  630. "Please click the link below to confirm and activate your account.\r\n".
  631. "$confirm_url\r\n".
  632. "\r\n".
  633. "---------------\r\n".
  634. "Donator Express - Minecraft Bukkit Plugin\r\n".
  635. "This email has been sent from: ".
  636. $this->sitename;
  637.  
  638. if(!$mailer->Send())
  639. {
  640. $this->HandleError("Failed sending registration confirmation email.");
  641. return false;
  642. }
  643. return true;
  644. }
  645. function GetAbsoluteURLFolder()
  646. {
  647. $scriptFolder = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) ? 'https://' : 'http://';
  648. $scriptFolder .= $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']);
  649. return $scriptFolder;
  650. }
  651.  
  652. function SendAdminIntimationEmail(&$formvars)
  653. {
  654. if(empty($this->admin_email))
  655. {
  656. return false;
  657. }
  658. $mailer = new PHPMailer();
  659.  
  660. $mailer->CharSet = 'utf-8';
  661.  
  662. $mailer->AddAddress($this->admin_email);
  663.  
  664. $mailer->Subject = "Donator Express - New Registration: ".$formvars['name'];
  665.  
  666. $mailer->From = $this->GetFromAddress();
  667.  
  668. $mailer->Body ="A new user registered at ".$this->sitename." details of their registeration are below\r\n\n".
  669. "Full Name: ".$formvars['name']."\r\n".
  670. "Email address: ".$formvars['email']."\r\n".
  671. "Minecraft Username: ".$formvars['username']."\r\n\n".
  672. "Please Note: The user still needs to activate their account by confirming their email.\r\n".
  673. "\r\n".
  674. "---------------\r\n".
  675. "Donator Express - Minecraft Bukkit Plugin\r\n".
  676. "This email has been sent from: ".
  677. $this->sitename;
  678.  
  679. if(!$mailer->Send())
  680. {
  681. return false;
  682. }
  683. return true;
  684. }
  685.  
  686. function SaveToDatabase(&$formvars)
  687. {
  688. if(!$this->DBLogin())
  689. {
  690. $this->HandleError("Database login failed!");
  691. return false;
  692. }
  693. if(!$this->Ensuretable())
  694. {
  695. return false;
  696. }
  697. if(!$this->IsFieldUnique($formvars,'email'))
  698. {
  699. $this->HandleError("This email is already registered");
  700. return false;
  701. }
  702.  
  703. if(!$this->IsFieldUnique($formvars,'username'))
  704. {
  705. $this->HandleError("This Minecraft Username already has an account. Please reset your password if you have forgotten it.");
  706. return false;
  707. }
  708. if(!$this->InsertIntoDB($formvars))
  709. {
  710. $this->HandleError("Inserting to Database failed!");
  711. return false;
  712. }
  713. return true;
  714. }
  715.  
  716. function IsFieldUnique($formvars,$fieldname)
  717. {
  718. $field_val = $this->SanitizeForSQL($formvars[$fieldname]);
  719. $qry = "select username from $this->tablename where $fieldname='".$field_val."'";
  720. $result = mysql_query($qry,$this->connection);
  721. if($result && mysql_num_rows($result) > 0)
  722. {
  723. return false;
  724. }
  725. return true;
  726. }
  727.  
  728. function DBLogin()
  729. {
  730.  
  731. $this->connection = mysql_connect($this->db_host,$this->username,$this->pwd);
  732.  
  733. if(!$this->connection)
  734. {
  735. $this->HandleDBError("Database Login failed! Please make sure that the DB login credentials provided are correct");
  736. return false;
  737. }
  738. if(!mysql_select_db($this->database, $this->connection))
  739. {
  740. $this->HandleDBError('Failed to select database: '.$this->database.' Please make sure that the database name provided is correct');
  741. return false;
  742. }
  743. if(!mysql_query("SET NAMES 'UTF8'",$this->connection))
  744. {
  745. $this->HandleDBError('Error setting utf8 encoding');
  746. return false;
  747. }
  748. return true;
  749. }
  750.  
  751. function Ensuretable()
  752. {
  753. $result = mysql_query("SHOW COLUMNS FROM $this->tablename");
  754. if(!$result || mysql_num_rows($result) <= 0)
  755. {
  756. return $this->CreateTable();
  757. }
  758. return true;
  759. }
  760.  
  761. function CreateTable()
  762. {
  763. $qry = "Create Table $this->tablename (".
  764. "id_user INT NOT NULL AUTO_INCREMENT ,".
  765. "name VARCHAR( 128 ) NOT NULL ,".
  766. "email VARCHAR( 64 ) NOT NULL ,".
  767. "tokens VARCHAR( 16 ) DEFAULT '0' ,".
  768. "username VARCHAR( 16 ) NOT NULL ,".
  769. "password VARCHAR( 32 ) NOT NULL ,".
  770. "confirmcode VARCHAR(32) ,".
  771. "is_admin VARCHAR( 5 ) DEFAULT 'false' ,".
  772. "PRIMARY KEY ( id_user )".
  773. ")";
  774.  
  775. if(!mysql_query($qry,$this->connection))
  776. {
  777. $this->HandleDBError("Error creating the table \nquery was\n $qry");
  778. return false;
  779. }
  780. return true;
  781. }
  782.  
  783. function InsertIntoDB(&$formvars)
  784. {
  785.  
  786. $confirmcode = $this->MakeConfirmationMd5($formvars['email']);
  787.  
  788. $formvars['confirmcode'] = $confirmcode;
  789.  
  790. $insert_query = 'insert into '.$this->tablename.'(
  791. name,
  792. email,
  793. username,
  794. password,
  795. confirmcode
  796. )
  797. values
  798. (
  799. "' . $this->SanitizeForSQL($formvars['name']) . '",
  800. "' . $this->SanitizeForSQL($formvars['email']) . '",
  801. "' . $this->SanitizeForSQL($formvars['username']) . '",
  802. "' . md5($formvars['password']) . '",
  803. "' . $confirmcode . '"
  804. )';
  805. if(!mysql_query( $insert_query ,$this->connection))
  806. {
  807. $this->HandleDBError("Error inserting data to the table\nquery:$insert_query");
  808. return false;
  809. }
  810. return true;
  811. }
  812. function MakeConfirmationMd5($email)
  813. {
  814. $randno1 = rand();
  815. $randno2 = rand();
  816. return md5($email.$this->rand_key.$randno1.''.$randno2);
  817. }
  818. function SanitizeForSQL($str)
  819. {
  820. if( function_exists( "mysql_real_escape_string" ) )
  821. {
  822. $ret_str = mysql_real_escape_string( $str );
  823. }
  824. else
  825. {
  826. $ret_str = addslashes( $str );
  827. }
  828. return $ret_str;
  829. }
  830.  
  831. /*
  832. Sanitize() function removes any potential threat from the
  833. data submitted. Prevents email injections or any other hacker attempts.
  834. if $remove_nl is true, newline chracters are removed from the input.
  835. */
  836. function Sanitize($str,$remove_nl=true)
  837. {
  838. $str = $this->StripSlashes($str);
  839.  
  840. if($remove_nl)
  841. {
  842. $injections = array('/(\n+)/i',
  843. '/(\r+)/i',
  844. '/(\t+)/i',
  845. '/(%0A+)/i',
  846. '/(%0D+)/i',
  847. '/(%08+)/i',
  848. '/(%09+)/i'
  849. );
  850. $str = preg_replace($injections,'',$str);
  851. }
  852.  
  853. return $str;
  854. }
  855. function StripSlashes($str)
  856. {
  857. if(get_magic_quotes_gpc())
  858. {
  859. $str = stripslashes($str);
  860. }
  861. return $str;
  862. }
  863. }
  864. ?>
Advertisement
Add Comment
Please, Sign In to add comment