PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/class.login.php

http://sudeep-co-in.googlecode.com/
PHP | 298 lines | 238 code | 19 blank | 41 comment | 37 complexity | 958290aa6b33e89403fbb74663579024 MD5 | raw file
  1. <?php
  2. //start session
  3. session_start();
  4. class logmein
  5. {
  6. //database setup
  7. //MAKE SURE TO FILL IN DATABASE INFO
  8. var $hostname_logon = 'localhost'; //Database server LOCATION
  9. var $database_logon = 'sudeep_27092010'; //Database NAME
  10. var $username_logon = 'root'; //Database USERNAME
  11. var $password_logon = 'kumars'; //Database PASSWORD
  12. //table fields
  13. var $user_table = 'users'; //Users table name
  14. var $user_column = 'User_Email_ID'; //USERNAME column (value MUST be valid email)
  15. var $pass_column = 'User_Password'; //PASSWORD column
  16. var $user_level = 'User_Access_Level'; //(optional) userlevel column
  17. //encryption
  18. var $encrypt = true; //set to true to use md5 encryption for the password
  19. //connect to database
  20. function dbconnect()
  21. {
  22. $connections = mysql_connect($this->hostname_logon, $this->username_logon, $this->password_logon) or die ('Unabale to connect to the database');
  23. mysql_select_db($this->database_logon) or die ('Unable to select database!');
  24. return;
  25. }
  26. //login function
  27. function login($table, $username, $password)
  28. {
  29. $this->dbconnect();
  30. //make sure table name is set
  31. if($this->user_table == "")
  32. {
  33. $this->user_table = $table;
  34. }
  35. //check if encryption is used
  36. if($this->encrypt == true){
  37. $password = md5($password);
  38. }
  39. //execute login via qry function that prevents MySQL injections
  40. $result = $this->qry("SELECT * FROM ".$this->user_table." WHERE ".$this->user_column."='?' AND ".$this->pass_column." = '?';" , $username, $password);
  41. $row=mysql_fetch_assoc($result);
  42. if($row != "Error")
  43. {
  44. if($row[$this->user_column] !="" && $row[$this->pass_column] !="")
  45. {
  46. //register sessions
  47. //you can add additional sessions here if needed
  48. $_SESSION['loggedin'] = $row[$this->pass_column];
  49. //userlevel session is optional. Use it if you have different user levels
  50. $_SESSION['userlevel'] = $row[$this->user_level];
  51. return true;
  52. }
  53. else
  54. {
  55. session_destroy();
  56. return false;
  57. }
  58. }
  59. else
  60. {
  61. return false;
  62. }
  63. }
  64. //prevent injection
  65. function qry($query)
  66. {
  67. $this->dbconnect();
  68. $args = func_get_args();
  69. $query = array_shift($args);
  70. $query = str_replace("?", "%s", $query);
  71. $args = array_map('mysql_real_escape_string', $args);
  72. array_unshift($args,$query);
  73. $query = call_user_func_array('sprintf',$args);
  74. $result = mysql_query($query) or die(mysql_error());
  75. if($result)
  76. {
  77. return $result;
  78. }
  79. else
  80. {
  81. $error = "Error";
  82. return $result;
  83. }
  84. }
  85. //logout function
  86. function logout()
  87. {
  88. session_destroy();
  89. return;
  90. }
  91. //check if loggedin
  92. function logincheck($logincode, $user_table, $pass_column, $user_column)
  93. {
  94. $this->dbconnect();
  95. //make sure password column and table are set
  96. if($this->pass_column == "")
  97. {
  98. $this->pass_column = $pass_column;
  99. }
  100. if($this->user_column == "")
  101. {
  102. $this->user_column = $user_column;
  103. }
  104. if($this->user_table == "")
  105. {
  106. $this->user_table = $user_table;
  107. }
  108. //exectue query
  109. $result = $this->qry("SELECT * FROM ".$this->user_table." WHERE ".$this->pass_column." = '?';" , $logincode);
  110. $rownum = mysql_num_rows($result);
  111. //return true if logged in and false if not
  112. if($row != "Error")
  113. {
  114. if($rownum > 0)
  115. {
  116. return true;
  117. }
  118. else
  119. {
  120. return false;
  121. }
  122. }
  123. }
  124. //reset password
  125. function passwordreset($username, $user_table, $pass_column, $user_column)
  126. {
  127. $this->dbconnect();
  128. //generate new password
  129. $newpassword = $this->createPassword();
  130. //make sure password column and table are set
  131. if($this->pass_column == "")
  132. {
  133. $this->pass_column = $pass_column;
  134. }
  135. if($this->user_column == "")
  136. {
  137. $this->user_column = $user_column;
  138. }
  139. if($this->user_table == "")
  140. {
  141. $this->user_table = $user_table;
  142. }
  143. //check if encryption is used
  144. if($this->encrypt == true)
  145. {
  146. $newpassword = md5($newpassword);
  147. }
  148. //update database with new password
  149. $qry = "UPDATE ".$this->user_table." SET ".$this->pass_column."='".$newpassword."' WHERE ".$this->user_column."='".stripslashes($username)."'";
  150. $result = mysql_query($qry) or die(mysql_error());
  151. $to = stripslashes($username);
  152. //some injection protection
  153. $illigals = array("n", "r","%0A","%0D","%0a","%0d","bcc:","Content-Type","BCC:","Bcc:","Cc:","CC:","TO:","To:","cc:","to:");
  154. $to = str_replace($illigals, "", $to);
  155. $getemail = explode("@",$to);
  156. //send only if there is one email
  157. if(sizeof($getemail) > 2)
  158. {
  159. return false;
  160. }
  161. else
  162. {
  163. //send email
  164. $from = $_SERVER['SERVER_NAME'];
  165. $subject = "Password Reset: ".$_SERVER['SERVER_NAME'];
  166. $msg = "<p>Your new password is: ".$newpassword."</p>";
  167. //now we need to set mail headers
  168. $headers = "MIME-Version: 1.0 rn" ;
  169. $headers .= "Content-Type: text/html; rn" ;
  170. $headers .= "From: $from rn" ;
  171. //now we are ready to send mail
  172. $sent = mail($to, $subject, $msg, $headers);
  173. if($sent)
  174. {
  175. return true;
  176. }
  177. else
  178. {
  179. return false;
  180. }
  181. }
  182. }
  183. //create random password with 8 alphanumerical characters
  184. function createPassword()
  185. {
  186. $chars = "abcdefghijkmnopqrstuvwxyz023456789";
  187. srand((double)microtime()*1000000);
  188. $i = 0;
  189. $pass = '' ;
  190. while ($i <= 7)
  191. {
  192. $num = rand() % 33;
  193. $tmp = substr($chars, $num, 1);
  194. $pass = $pass . $tmp;
  195. $i++;
  196. }
  197. return $pass;
  198. }
  199. //login form
  200. function loginform($formname, $formclass, $formaction)
  201. {
  202. $this->dbconnect();
  203. echo'<form name="'.$formname.'" method="post" id="'.$formname.'" class="'.$formclass.'" enctype="application/x-www-form-urlencoded" action="'.$formaction.'">
  204. <div><label for="username">Username</label>
  205. <input name="username" id="username" type="text"></div>
  206. <div><label for="password">Password</label>
  207. <input name="password" id="password" type="password"></div>
  208. <input name="action" id="action" value="login" type="hidden">
  209. <div><input name="submit" id="submit" value="Login" type="submit"></div>
  210. </form>';
  211. }
  212. //reset password form
  213. function resetform($formname, $formclass, $formaction)
  214. {
  215. $this->dbconnect();
  216. echo'<form name="'.$formname.'" method="post" id="'.$formname.'" class="'.$formclass.'" enctype="application/x-www-form-urlencoded" action="'.$formaction.'">
  217. <div><label for="username">Username</label>
  218. <input name="username" id="username" type="text"></div>
  219. <input name="action" id="action" value="resetlogin" type="hidden">
  220. <div><input name="submit" id="submit" value="Reset Password" type="submit"></div>
  221. </form>';
  222. }
  223. //function to install logon table
  224. function cratetable($tablename)
  225. {
  226. $this->dbconnect();
  227. $qry = "CREATE TABLE IF NOT EXISTS ".$tablename." (
  228. userid int(11) NOT NULL auto_increment,
  229. useremail varchar(50) NOT NULL default '',
  230. password varchar(50) NOT NULL default '',
  231. userlevel int(11) NOT NULL default '0',
  232. PRIMARY KEY (userid)
  233. )";
  234. $result = mysql_query($qry) or die(mysql_error());
  235. return;
  236. }
  237. //register function by Micah B-F.
  238. function register($table, $username, $password)
  239. {
  240. //conect to DB
  241. $this->dbconnect();
  242. //make sure table name is set
  243. if($this->user_table == "")
  244. {
  245. $this->user_table = $table;
  246. }
  247. //check if encryption is used
  248. if($this->encrypt == true)
  249. {
  250. $password = md5($password);
  251. }
  252. //execute registration via qry function that prevents MySQL injections
  253. $result = $this->qry("INSERT INTO ".$this->user_table." VALUES(DEFAULT,'?','?',DEFAULT)", $username, $password);
  254. $row=mysql_fetch_assoc($result);
  255. if($row != "Error")
  256. {
  257. if($row[$this->user_column] !="" && $row[$this->pass_column] !="")
  258. {
  259. //register sessions
  260. //you can add additional sessions here if needed
  261. $_SESSION['loggedin'] = $row[$this->pass_column];
  262. $_SESSION['username'] = $username;
  263. //userlevel session is optional.Use it if you have different user levels
  264. $_SESSION['userlevel'] = $row[$this->user_level];
  265. return true;
  266. }
  267. else
  268. {
  269. session_destroy();
  270. return false;
  271. }
  272. }
  273. else
  274. {
  275. return false;
  276. }
  277. }
  278. }
  279. ?>