Advertisement
Guest User

Untitled

a guest
May 21st, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.46 KB | None | 0 0
  1. <?php
  2. /**
  3. * Session.php
  4. *
  5. * The Session class is meant to simplify the task of keeping
  6. * track of logged in users and also guests.
  7. *
  8. * Written by: Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
  9. * Last Updated: June 15, 2011 by Ivan Novak
  10. */
  11. include("database.php");
  12. include("mailer.php");
  13. include("form.php");
  14.  
  15. class Session
  16. {
  17. var $username; //Username given on sign-up
  18. var $userid; //Random value generated on current login
  19. var $userlevel; //The level to which the user pertains
  20. var $time; //Time user was last active (page loaded)
  21. var $logged_in; //True if user is logged in, false otherwise
  22. var $userinfo = array(); //The array holding all user info
  23. var $url; //The page url current being viewed
  24. var $referrer; //Last recorded site page viewed
  25. /**
  26. * Note: referrer should really only be considered the actual
  27. * page referrer in process.php, any other time it may be
  28. * inaccurate.
  29. */
  30.  
  31. /* Class constructor */
  32. function Session(){
  33. $this->time = time();
  34. $this->startSession();
  35. }
  36.  
  37. /**
  38. * startSession - Performs all the actions necessary to
  39. * initialize this session object. Tries to determine if the
  40. * the user has logged in already, and sets the variables
  41. * accordingly. Also takes advantage of this page load to
  42. * update the active visitors tables.
  43. */
  44. function startSession(){
  45. global $database; //The database connection
  46. session_start(); //Tell PHP to start the session
  47.  
  48. /* Determine if user is logged in */
  49. $this->logged_in = $this->checkLogin();
  50.  
  51. /**
  52. * Set guest value to users not logged in, and update
  53. * active guests table accordingly.
  54. */
  55. if(!$this->logged_in){
  56. $this->username = $_SESSION['username'] = GUEST_NAME;
  57. $this->userlevel = GUEST_LEVEL;
  58. $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
  59. }
  60. /* Update users last active timestamp */
  61. else{
  62. $database->addActiveUser($this->username, $this->time);
  63. }
  64.  
  65. /* Remove inactive visitors from database */
  66. $database->removeInactiveUsers();
  67. $database->removeInactiveGuests();
  68.  
  69. /* Set referrer page */
  70. if(isset($_SESSION['url'])){
  71. $this->referrer = $_SESSION['url'];
  72. }else{
  73. $this->referrer = "/";
  74. }
  75.  
  76. /* Set current url */
  77. $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
  78. }
  79.  
  80. /**
  81. * checkLogin - Checks if the user has already previously
  82. * logged in, and a session with the user has already been
  83. * established. Also checks to see if user has been remembered.
  84. * If so, the database is queried to make sure of the user's
  85. * authenticity. Returns true if the user has logged in.
  86. */
  87. function checkLogin(){
  88. global $database; //The database connection
  89. /* Check if user has been remembered */
  90. if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
  91. $this->username = $_SESSION['username'] = $_COOKIE['cookname'];
  92. $this->userid = $_SESSION['userid'] = $_COOKIE['cookid'];
  93. }
  94.  
  95. /* Username and userid have been set and not guest */
  96. if(isset($_SESSION['username']) && isset($_SESSION['userid']) &&
  97. $_SESSION['username'] != GUEST_NAME){
  98. /* Confirm that username and userid are valid */
  99. if($database->confirmUserID($_SESSION['username'], $_SESSION['userid']) != 0){
  100. /* Variables are incorrect, user not logged in */
  101. unset($_SESSION['username']);
  102. unset($_SESSION['userid']);
  103. return false;
  104. }
  105.  
  106. /* User is logged in, set class variables */
  107. $this->userinfo = $database->getUserInfo($_SESSION['username']);
  108. $this->username = $this->userinfo['username'];
  109. $this->userid = $this->userinfo['userid'];
  110. $this->userlevel = $this->userinfo['userlevel'];
  111.  
  112. /* auto login hash expires in three days */
  113. if($this->userinfo['hash_generated'] < (time() - (60*60*24*3))){
  114. /* Update the hash */
  115. $database->updateUserField($this->userinfo['username'], 'hash', $this->generateRandID());
  116. $database->updateUserField($this->userinfo['username'], 'hash_generated', time());
  117. }
  118.  
  119. return true;
  120. }
  121. /* User not logged in */
  122. else{
  123. return false;
  124. }
  125. }
  126.  
  127. /**
  128. * login - The user has submitted his username and password
  129. * through the login form, this function checks the authenticity
  130. * of that information in the database and creates the session.
  131. * Effectively logging in the user if all goes well.
  132. */
  133. function login($subuser, $subpass, $subremember){
  134. global $database, $form; //The database and form object
  135.  
  136. /* Username error checking */
  137. $field = "user"; //Use field name for username
  138. $q = "SELECT valid FROM ".TBL_USERS." WHERE username='$subuser'";
  139. $valid = $database->query($q);
  140. $valid = mysql_fetch_array($valid);
  141.  
  142. if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  143. $form->setError($field, "* Username not entered");
  144. }
  145. else{
  146. /* Check if username is not alphanumeric */
  147. if(!ctype_alnum($subuser)){
  148. $form->setError($field, "* Username not alphanumeric");
  149. }
  150. }
  151.  
  152. /* Password error checking */
  153. $field = "pass"; //Use field name for password
  154. if(!$subpass){
  155. $form->setError($field, "* Password not entered");
  156. }
  157.  
  158. /* Return if form errors exist */
  159. if($form->num_errors > 0){
  160. return false;
  161. }
  162.  
  163. /* Checks that username is in database and password is correct */
  164. $subuser = stripslashes($subuser);
  165. $result = $database->confirmUserPass($subuser, md5($subpass));
  166.  
  167. /* Check error codes */
  168. if($result == 1){
  169. $field = "user";
  170. $form->setError($field, "* Username not found");
  171. }
  172. else if($result == 2){
  173. $field = "pass";
  174. $form->setError($field, "* Invalid password");
  175. }
  176.  
  177. /* Return if form errors exist */
  178. if($form->num_errors > 0){
  179. return false;
  180. }
  181.  
  182.  
  183. if(EMAIL_WELCOME){
  184. if($valid['valid'] == 0){
  185. $form->setError($field, "* User's account has not yet been confirmed.");
  186. }
  187. }
  188.  
  189. /* Return if form errors exist */
  190. if($form->num_errors > 0){
  191. return false;
  192. }
  193.  
  194.  
  195.  
  196. /* Username and password correct, register session variables */
  197. $this->userinfo = $database->getUserInfo($subuser);
  198. $this->username = $_SESSION['username'] = $this->userinfo['username'];
  199. $this->userid = $_SESSION['userid'] = $this->generateRandID();
  200. $this->userlevel = $this->userinfo['userlevel'];
  201.  
  202. /* Insert userid into database and update active users table */
  203. $database->updateUserField($this->username, "userid", $this->userid);
  204. $database->addActiveUser($this->username, $this->time);
  205. $database->removeActiveGuest($_SERVER['REMOTE_ADDR']);
  206.  
  207. /**
  208. * This is the cool part: the user has requested that we remember that
  209. * he's logged in, so we set two cookies. One to hold his username,
  210. * and one to hold his random value userid. It expires by the time
  211. * specified in constants.php. Now, next time he comes to our site, we will
  212. * log him in automatically, but only if he didn't log out before he left.
  213. */
  214. if($subremember){
  215. setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
  216. setcookie("cookid", $this->userid, time()+COOKIE_EXPIRE, COOKIE_PATH);
  217. }
  218.  
  219. /* Login completed successfully */
  220. return true;
  221. }
  222.  
  223. /**
  224. * logout - Gets called when the user wants to be logged out of the
  225. * website. It deletes any cookies that were stored on the users
  226. * computer as a result of him wanting to be remembered, and also
  227. * unsets session variables and demotes his user level to guest.
  228. */
  229. function logout(){
  230. global $database; //The database connection
  231. /**
  232. * Delete cookies - the time must be in the past,
  233. * so just negate what you added when creating the
  234. * cookie.
  235. */
  236. if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
  237. setcookie("cookname", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
  238. setcookie("cookid", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
  239. }
  240.  
  241. /* Unset PHP session variables */
  242. unset($_SESSION['username']);
  243. unset($_SESSION['userid']);
  244.  
  245. /* Reflect fact that user has logged out */
  246. $this->logged_in = false;
  247.  
  248. /**
  249. * Remove from active users table and add to
  250. * active guests tables.
  251. */
  252. $database->removeActiveUser($this->username);
  253. $database->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
  254.  
  255. /* Set user level to guest */
  256. $this->username = GUEST_NAME;
  257. $this->userlevel = GUEST_LEVEL;
  258. }
  259.  
  260. /**
  261. * register - Gets called when the user has just submitted the
  262. * registration form. Determines if there were any errors with
  263. * the entry fields, if so, it records the errors and returns
  264. * 1. If no errors were found, it registers the new user and
  265. * returns 0. Returns 2 if registration failed.
  266. */
  267. function register($subuser, $subpass, $subemail, $subname){
  268.  
  269. global $database, $form, $mailer; //The database, form and mailer object
  270.  
  271. /* Username error checking */
  272. $field = "user"; //Use field name for username
  273. if(!$subuser || strlen($subuser = trim($subuser)) == 0){
  274. $form->setError($field, "* Username not entered");
  275. }
  276. else{
  277. /* Spruce up username, check length */
  278. $subuser = stripslashes($subuser);
  279. if(strlen($subuser) < 5){
  280. $form->setError($field, "* Username below 5 characters");
  281. }
  282. else if(strlen($subuser) > 30){
  283. $form->setError($field, "* Username above 30 characters");
  284. }
  285. /* Check if username is not alphanumeric */
  286. else if(!ctype_alnum($subuser)){
  287. $form->setError($field, "* Username not alphanumeric");
  288. }
  289. /* Check if username is reserved */
  290. else if(strcasecmp($subuser, GUEST_NAME) == 0){
  291. $form->setError($field, "* Username reserved word");
  292. }
  293. /* Check if username is already in use */
  294. else if($database->usernameTaken($subuser)){
  295. $form->setError($field, "* Username already in use");
  296. }
  297. /* Check if username is banned */
  298. else if($database->usernameBanned($subuser)){
  299. $form->setError($field, "* Username banned");
  300. }
  301. }
  302.  
  303. /* Password error checking */
  304. $field = "pass"; //Use field name for password
  305. if(!$subpass){
  306. $form->setError($field, "* Password not entered");
  307. }
  308. else{
  309. /* Spruce up password and check length*/
  310. $subpass = stripslashes($subpass);
  311. if(strlen($subpass) < 4){
  312. $form->setError($field, "* Password too short");
  313. }
  314. /* Check if password is not alphanumeric */
  315. else if(!ctype_alnum(($subpass = trim($subpass)))){
  316. $form->setError($field, "* Password not alphanumeric");
  317. }
  318. /**
  319. * Note: I trimmed the password only after I checked the length
  320. * because if you fill the password field up with spaces
  321. * it looks like a lot more characters than 4, so it looks
  322. * kind of stupid to report "password too short".
  323. */
  324. }
  325.  
  326. /* Email error checking */
  327. $field = "email"; //Use field name for email
  328. if(!$subemail || strlen($subemail = trim($subemail)) == 0){
  329. $form->setError($field, "* Email not entered");
  330. }
  331. else{
  332. /* Check if valid email address */
  333. if(filter_var($subemail, FILTER_VALIDATE_EMAIL) == FALSE){
  334. $form->setError($field, "* Email invalid");
  335. }
  336. /* Check if email is already in use */
  337. if($database->emailTaken($subemail)){
  338. $form->setError($field, "* Email already in use");
  339. }
  340.  
  341. $subemail = stripslashes($subemail);
  342. }
  343.  
  344. /* Name error checking */
  345. $field = "name";
  346. if(!$subname || strlen($subname = trim($subname)) == 0){
  347. $form->setError($field, "* Name not entered");
  348. } else {
  349. $subname = stripslashes($subname);
  350. }
  351.  
  352. $randid = $this->generateRandID();
  353.  
  354. /* Errors exist, have user correct them */
  355. if($form->num_errors > 0){
  356. return 1; //Errors with form
  357. }
  358. /* No errors, add the new account to the */
  359. else{
  360. if($database->addNewUser($subuser, md5($subpass), $subemail, $randid, $subname)){
  361. if(EMAIL_WELCOME){
  362. $mailer->sendWelcome($subuser,$subemail,$subpass,$randid);
  363. }
  364. return 0; //New user added succesfully
  365. }else{
  366. return 2; //Registration attempt failed
  367. }
  368. }
  369. }
  370.  
  371. /**
  372. * editAccount - Attempts to edit the user's account information
  373. * including the password, which it first makes sure is correct
  374. * if entered, if so and the new password is in the right
  375. * format, the change is made. All other fields are changed
  376. * automatically.
  377. */
  378. function editAccount($subcurpass, $subnewpass, $subemail, $subname){
  379. global $database, $form; //The database and form object
  380. /* New password entered */
  381. if($subnewpass){
  382. /* Current Password error checking */
  383. $field = "curpass"; //Use field name for current password
  384. if(!$subcurpass){
  385. $form->setError($field, "* Current Password not entered");
  386. }
  387. else{
  388. /* Check if password too short or is not alphanumeric */
  389. $subcurpass = stripslashes($subcurpass);
  390. if(strlen($subcurpass) < 4 ||
  391. !preg_match("^([0-9a-z])+$", ($subcurpass = trim($subcurpass)))){
  392. $form->setError($field, "* Current Password incorrect");
  393. }
  394. /* Password entered is incorrect */
  395. if($database->confirmUserPass($this->username,md5($subcurpass)) != 0){
  396. $form->setError($field, "* Current Password incorrect");
  397. }
  398. }
  399.  
  400. /* New Password error checking */
  401. $field = "newpass"; //Use field name for new password
  402. /* Spruce up password and check length*/
  403. $subpass = stripslashes($subnewpass);
  404. if(strlen($subnewpass) < 4){
  405. $form->setError($field, "* New Password too short");
  406. }
  407. /* Check if password is not alphanumeric */
  408. else if(!preg_match("^([0-9a-z])+$", ($subnewpass = trim($subnewpass)))){
  409. $form->setError($field, "* New Password not alphanumeric");
  410. }
  411. }
  412. /* Change password attempted */
  413. else if($subcurpass){
  414. /* New Password error reporting */
  415. $field = "newpass"; //Use field name for new password
  416. $form->setError($field, "* New Password not entered");
  417. }
  418.  
  419. /* Email error checking */
  420. $field = "email"; //Use field name for email
  421. if($subemail && strlen($subemail = trim($subemail)) > 0){
  422. /* Check if valid email address */
  423. if(filter_var($subemail, FILTER_VALIDATE_EMAIL) == FALSE){
  424. $form->setError($field, "* Email invalid");
  425. }
  426. $subemail = stripslashes($subemail);
  427. }
  428.  
  429. /* Name error checking */
  430. $field = "name";
  431. if(!$subname || strlen($subname = trim($subname)) == 0){
  432. $form->setError($field, "* Name not entered");
  433. } else {
  434. $subname = stripslashes($subname);
  435. }
  436.  
  437. /* Errors exist, have user correct them */
  438. if($form->num_errors > 0){
  439. return false; //Errors with form
  440. }
  441.  
  442. /* Update password since there were no errors */
  443. if($subcurpass && $subnewpass){
  444. $database->updateUserField($this->username,"password",md5($subnewpass));
  445. }
  446.  
  447. /* Change Email */
  448. if($subemail){
  449. $database->updateUserField($this->username,"email",$subemail);
  450. }
  451.  
  452. /* Change Name */
  453. if($subname){
  454. $database->updateUserField($this->username,"name",$subname);
  455. }
  456.  
  457. /* Success! */
  458. return true;
  459. }
  460.  
  461. /**
  462. * isAdmin - Returns true if currently logged in user is
  463. * an administrator, false otherwise.
  464. */
  465. function isAdmin(){
  466. return ($this->userlevel == ADMIN_LEVEL ||
  467. $this->username == ADMIN_NAME);
  468. }
  469.  
  470. /**
  471. * isAuthor - Returns true if currently logged in user is
  472. * an author or an administrator, false otherwise.
  473. */
  474. function isAuthor(){
  475. return ($this->userlevel == AUTHOR_LEVEL ||
  476. $this->userlevel == ADMIN_LEVEL);
  477. }
  478.  
  479. /**
  480. * generateRandID - Generates a string made up of randomized
  481. * letters (lower and upper case) and digits and returns
  482. * the md5 hash of it to be used as a userid.
  483. */
  484. function generateRandID(){
  485. return md5($this->generateRandStr(16));
  486. }
  487.  
  488. /**
  489. * generateRandStr - Generates a string made up of randomized
  490. * letters (lower and upper case) and digits, the length
  491. * is a specified parameter.
  492. */
  493. function generateRandStr($length){
  494. $randstr = "";
  495. for($i=0; $i<$length; $i++){
  496. $randnum = mt_rand(0,61);
  497. if($randnum < 10){
  498. $randstr .= chr($randnum+48);
  499. }else if($randnum < 36){
  500. $randstr .= chr($randnum+55);
  501. }else{
  502. $randstr .= chr($randnum+61);
  503. }
  504. }
  505. return $randstr;
  506. }
  507.  
  508. function cleanInput($post = array()) {
  509. foreach($post as $k => $v){
  510. $post[$k] = trim(htmlspecialchars($v));
  511. }
  512. return $post;
  513. }
  514. };
  515.  
  516.  
  517. /**
  518. * Initialize session object - This must be initialized before
  519. * the form object because the form uses session variables,
  520. * which cannot be accessed unless the session has started.
  521. */
  522. $session = new Session;
  523.  
  524. /* Initialize form object */
  525. $form = new Form;
  526.  
  527. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement