Guest User

php-1

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