Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /******************* COPYRIGHT ******************/
- /* DO NOT REMOVE */
- /* Copyright © made by Jean-Luc Tallis */
- /************************************************/
- /******************* VERSIONS *******************/
- /* - Version 1 Beta */
- /* Released at May 2010 */
- /************************************************/
- /********************* USAGE ********************/
- /* - Restrict information to logged in users */
- /* if($session->logged_in == true) { Do this } */
- /* */
- /* - To log a user out */
- /* session->logout(); */
- /* */
- /* - Check User Login and return array of data */
- /* $user = session->checkLogin; */
- /************************************************/
- class Session extends User {
- public $time;
- public $logged_in = NULL;
- public $ip;
- public function __construct() {
- $this->time = time();
- $this->ip = $_SERVER['REMOTE_ADDR'];
- $this->startSession();
- }
- public function startSession() {
- session_start();
- }
- public function checkLogin($secret_key) {
- global $db;
- if (isset($_COOKIE['HORBLECOOKIE'])) :
- $data = explode('-', $_COOKIE['HORBLECOOKIE']);
- $_SESSION['uid'] = $data[1];
- $_SESSION['hash'] = $data[0];
- endif;
- $this->uid = $_SESSION['uid'];
- $this->hashkey = $_SESSION['hash'];
- if (!isset($this->uid) || !isset($this->hashkey)) {
- $this->logout();
- } else {
- $check = sha1($this->uid . $this->ip . $secret_key);
- if ($check != $this->hashkey) {
- $this->logout();
- } else {
- $query = $db->execute("SELECT * FROM users WHERE id='".$this->uid."'") or die(mysql_error());
- $userarray = $db->fetchassoc($query);
- if ($db->numrows($query) == 0) {
- $this->logout();
- }
- foreach($userarray as $key=>$value) {
- $user->$key = $value;
- }
- $this->logged_in = 1;
- return $user;
- }
- }
- }
- public function login($email, $password, $keepmein, $secret_key) {
- global $msgError;
- $this->email = clean($email, 1, 1, 3);
- $this->password = clean($password, 1 , 1, 0);
- if (empty($this->email) || empty($this->password)) {
- $msgError = "You have left empty fields!";
- return;
- }
- $result = User::confirmUserPass($this->email, $this->password);
- if ($result == 1 || $result == 3) {
- $msgError = "Please enter valid email and password.";
- return;
- } elseif ($result == 2) {
- $msgError = "Your user account has not been activated yet!";
- return;
- }
- if (empty($msgError)) {
- $this->userinfo = User::getUserInfo('users', 'email', $this->email);
- $this->id = $_SESSION['uid'] = $this->userinfo['id'];
- $this->hashkey = $_SESSION['hash'] = sha1($this->id . $this->ip . $secret_key);
- User::updateUserField('users', 'displayname', $this->displayname, "last_login", $this->time);
- User::updateUserField('users', 'displayname', $this->displayname, "ip", $this->ip);
- User::updateUserField('users', 'displayname', $this->displayname, "times_logged", $this->userinfo['times_logged'] + 1);
- if ($keepmein) {
- setcookie("HORBLECOOKIE", $this->hashkey . '-' . $this->id, $this->time + COOKIE_EXPIRE);
- }
- $this->logged_in = 1;
- return true;
- } else {
- return false;
- }
- }
- public function logout() {
- if (isset($_COOKIE['HORBLECOOKIE'])) {
- setcookie("HORBLECOOKIE", "", $this->time - COOKIE_EXPIRE);
- }
- session_unset();
- session_destroy();
- $this->logged_in = 0;
- redirect("/new/index.php");
- }
- public function register($name, $displayname, $email, $password, $verifypass, $gender, $dob, $captcha) { //Etc Etc
- global $mailer, $msgError, $msgOk, $showMsg;
- $this->name = clean($name,1,0,3);
- $this->displayname = clean($displayname,1,1,2);
- $this->email = clean($email,1,1,3);
- $this->password = clean($password,1,1,0);
- $this->verifypass = clean($verifypass,1,1,0);
- $this->gender = clean($gender,1,0,3);
- $this->dob = clean($dob,1,1,0);
- $this->captcha = $captcha;
- $this->msgs = array();
- $namecheck = trim($this->name);
- if (empty($namecheck) || empty($this->email) || empty($this->password) || empty($this->verifypass) || empty($this->gender) || empty($this->dob) || empty($this->captcha)) {
- $this->msgs[] = "You have left empty fields";
- } else {
- $name = explode(' ', $name);
- if(strlen($name[0]) > 15) {
- $this->msgs[] = "First name is too long";
- }
- if (User::isValidEmail($this->email) == true) {
- $value = User::emailExists($this->email);
- if ($value == 1) :
- $this->msgs[] = "Sorry, this Email already exists";
- endif;
- } else {
- $this->msgs[] = "Incorrect Email Format";
- }
- if(empty($this->displayname)) {
- $this->displayname = $name[0] . rand(1,999);
- }
- if (strlen($this->password) < 6) {
- $this->msgs[] = "Password is too short (less than 6 characters long)";
- } elseif(!preg_match("/^([0-9a-z])+$/i", $this->password)) {
- $this->msgs[] = "Password entered is not alphanumeric";
- } elseif ($this->password != $this->verifypass) {
- $this->msgs[] = "Your password did not match the confirmed password!";
- }
- if(getAge($this->dob) < 13) {
- $this->msgs[] = "You must be of 13 age or over to register";
- } else {
- $this->dob = date("D-M-Y", strtotime($this->dob));
- }
- if(strtolower($this->captcha) != strtolower($_SESSION['ckey'])) {
- $this->msgs[] = "You captcha image was incorrect!";
- }
- }
- if (empty($this->msgs)) {
- $this->newpassword = md5($this->email . $this->password);
- $this->actkey = $this->generateRandID();
- User::addNewUser($this->email, $this->displayname, $this->newpassword, $this->name, $this->gender, $this->dob, $this->time, $this->ip, $this->actkey);
- $link = $_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']) .
- "/index.php?action=activate&mail=" . urlencode($this->email) . "&key=" . $this->actkey;
- $sendMail = $mailer->sendWelcome(ucwords($this->name), $this->email, $this->password, $link);
- if ($sendMail) {
- $msgOk = "Registered";
- }
- } else {
- $showMsg = '<div class="error"><span>Error(s):</span><ul>';
- foreach ($this->msgs as $msg) { $showMsg .= "<li>" . $msg . "</li>\n"; }
- $showMsg .= '</ul></div>';
- }
- }
- public function forgotPass($email) {
- global $mailer, $msgError, $msgOk;
- $this->email = clean($email, 1, 1, 3);
- if (empty($this->email)) {
- $msgError = 'You have left empty fields!';
- return;
- } else {
- if (User::isValidEmail($this->email) == false) {
- $msgError = "Entered Email Address is not valid.";
- return;
- } else {
- if(User::emailExists($this->email) == false) {
- $msgError = "That email does not exist";
- return;
- }
- }
- }
- if (empty($msgError)) {
- $this->pass = $this->generateRandStr(8);
- $this->encrypted_pass = md5($this->email . $this->pass);
- $this->userinfo = User::getUserInfo('users', 'email', $this->email);
- $mail = $mailer->sendNewPass($this->userinfo['name'], $this->email, $this->pass);
- if ($mail) {
- User::updateUserField('users', 'email', $this->email, 'password', $this->encrypted_pass);
- $msgOk = "Success!";
- }
- }
- }
- public function resendAct($email) {
- global $mailer, $msgError, $msgOk;
- $this->email = clean($email, 1, 1, 3);
- if (empty($this->email)) {
- $msgError = 'You have left empty fields!';
- return;
- } else {
- if (User::emailExists($this->email) == false) {
- $msgError = "That email does not exist";
- return;
- }
- }
- if (empty($msgError)) {
- $this->userinfo = User::getUserInfo('users', 'email', $this->email);
- $act_key = $this->userinfo['actkey'];
- $link = $_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']) . "/index.php?action=activate&mail=" . urlencode($this->email) . "&key=" . $act_key;
- $mail = $mailer->sendActivation(ucwords($this->userinfo['name']), $this->email, $link);
- $msgOk = 'Success. You should have recieved your activation email';
- }
- }
- public function activateUser($email, $act_key) {
- global $msgAlert, $msgOk, $msgError;
- $this->email = clean($email,1,0,3);
- $this->act_key = clean($act_key,1,0,0);
- if(empty($this->email) || empty($this->act_key)) {
- $msgError = "Your activation link must contain your email and activation key";
- return;
- }
- $validation = User::validateActivation($this->email, $this->act_key);
- if($validation == 1) {
- $msgError = "The email address is not valid";
- return;
- } elseif($validation == 2) {
- $msgError = "You are already activated";
- return;
- } elseif($validation == 3) {
- $msgError = "There are two accounts under the same email address, and activation key. Please contact us";
- return;
- } elseif($validation == 4) {
- $msgError = "The email address or activation code is not valid";
- return;
- } else {
- if(User::setUserActive($this->email, $this->act_key)) {
- $msgOk = "<span>Success!</span> Thank you. Your account is now active. You may now log in.";
- return;
- } else {
- $msgError = "There was an error processing your request. Please try again";
- return;
- }
- }
- }
- public function buyFeature($user) {
- global $secret_key, $db;
- if($user->credits < '1000')
- {
- $error = '<span style="color:#e11919;">Not enough credits!</span>';
- return;
- }
- else
- {
- $query = $db->execute("SELECT id, user_id FROM featured_member ORDER BY date DESC LIMIT 10");
- if($db->numrows($query) > 1)
- {
- while($row = $db->fetchassoc($query))
- {
- if($row['user_id'] == $user->id)
- {
- $delfeature = $db->execute("DELETE FROM featured_member WHERE id = '" . $row['id'] . "'");
- }
- }
- }
- else
- {
- if($db->numrows($query) == 1)
- {
- $row = $db->fetchassoc($query);
- if($row['user_id'] == $user->id)
- {
- $delfeature = $db->execute("DELETE FROM featured_member WHERE id = '" . $row['id'] . "'");
- }
- }
- }
- $time = time();
- $query = $db->execute("INSERT INTO featured_member (user_id, views, date) VALUES ('$user->id', '0', '$time')");
- if($query)
- {
- $credits = $user->credits - 1000;
- $query = $db->execute("UPDATE users SET credits = '$credits' WHERE id = '$user->id'");
- $user = $session->checkLogin($secret_key);
- }
- }
- }
- public function postStatus($status, $user) {
- if(!empty($status)) : $status = bbcode_format($status,1); endif;
- $status = clean($status,1,0,0);
- $query = mysql_query("insert into `user_status` (user_id, status, posted) VALUES ('$user->id', '$status', '$this->time')");
- if($query) : echo '<span style="color:#e11919;">Status Updated!</span>'; endif;
- return;
- }
- public function getStatus($user) {
- global $db;
- $query = $db->execute("SELECT `status` FROM `user_status` WHERE `user_id` = '$user->id' ORDER BY `posted` DESC LIMIT 1");
- $row = $db->fetcharray($query);
- if(!empty($row['status'])) :
- return $row['status'];
- else :
- return '<em>Has no status.</em>';
- endif;
- }
- public function generateRandID() {
- return md5($this->generateRandStr(16));
- }
- /* Session::generateRandStr() */
- public function generateRandStr($length) {
- $randstr = "";
- for ($i = 0; $i < $length; $i++) {
- $randnum = mt_rand(0, 61);
- if ($randnum < 10) {
- $randstr .= chr($randnum + 48);
- } elseif ($randnum < 36) {
- $randstr .= chr($randnum + 55);
- } else {
- $randstr .= chr($randnum + 61);
- }
- }
- return $randstr;
- }
- }
- function clean($foo, $a, $b, $c) {
- if($b == '1') : $foo = trim($foo); endif;
- if(get_magic_quotes_gpc()) : $foo = stripslashes($foo); endif;
- if($a == '1') : $foo = mysql_real_escape_string($foo); endif;
- if($c == '1') : $foo = ucwords($foo); endif;
- if($c == '2') : $foo = ucfirst($foo); endif;
- if($c == '3') : $foo = strtolower($foo); endif;
- if($c == '4') : $foo = strtoupper($foo); endif;
- return $foo;
- }
- function display_msg($mode) {
- if($mode == 'ERROR') {
- global $msgError, $showMsg;
- if (!empty($msgError)) :
- echo '<div class="error">' . $msgError . '</div>';
- endif;
- if (!empty($showMsg)) :
- echo $showMsg;
- endif;
- }
- if($mode == 'SUCCESS') {
- global $msgOk;
- if (!empty($msgOk)) :
- echo '<div class="success">' . $msgOk . '</div>';
- endif;
- }
- }
- function redirect($location) {
- if(!headers_sent()) :
- header('Location: ' . $location);
- else :
- echo '<script type="text/javascript">window.location.href="' . $location . '";</script>';
- echo '<noscript><meta http-equiv="refresh" content="0;url=' . $location . '" /></noscript>';
- endif;
- }
- function getAge($iTimestamp) {
- $iTimestamp = strtotime($iTimestamp);
- $iDiffYear = date('Y') - date('Y', $iTimestamp);
- $iDiffMonth = date('n') - date('n', $iTimestamp);
- $iDiffDay = date('j') - date('j', $iTimestamp);
- // If birthday has not happen yet for this year, subtract 1.
- if ($iDiffMonth < 0 || ($iDiffMonth == 0 && $iDiffDay < 0)) {
- $iDiffYear--;
- }
- return $iDiffYear;
- }
- function bbcode_format ($str, $type) {
- // Do simple BBCode's
- if($type == '1') {
- $str = strip_tags($str);
- $str = htmlentities($str);
- $bbcode = array(
- //Bold
- '/\[b\](.*?)\[\/b\]/is',
- //Italic
- '/\[i\](.*?)\[\/i\]/is',
- //Underline
- '/\[u\](.*?)\[\/u\]/is',
- //Font Family
- '/\[font\=(.*?)\](.*?)\[\/font\]/is',
- //Colors
- '/\[c\=(.*?)\](.*?)\[\/c\]/is',
- //Code presentation
- '/\[code\](.*?)\[\/code\]/is'
- );
- $htmlcode = array(
- //Bold
- '<strong>$1</strong>',
- //Italic
- '<em>$1</em>',
- //Underline
- '<u>$1</u>',
- //Font Family
- '<span style="font-family: $1;">$2</span>',
- //Colors
- '<span style="color: $1;">$2</span>',
- //Code presentation
- '<pre class="code">$1</pre>'
- );
- $str = preg_replace($bbcode, $htmlcode, $str);
- } else {
- if($type == '2') {
- $bbcode = array(
- //Bold
- '[b]$1[/b]',
- //Italic
- '[i]$1[/i]',
- //Underline
- '[u]$1[/u]',
- //Font Family
- '[font=$1]$2[/font]',
- //Colors
- '[c=$1]$2[/c]',
- //Code presentation
- '[code]$1[/code]'
- );
- $htmlcode = array(
- //Bold
- '/\<strong\>(.*?)\<\/strong\>/is',
- //Italic
- '/\<em\>(.*?)\<\/em\>/is',
- //Underline
- '/\<u\>(.*?)\<\/u\>/is',
- //Font Family
- '/\<span style=\"font-family: (.*?);\"\>(.*?)\<\/span\>/is',
- //Colors
- '/\<span style=\"color: (.*?);\">(.*?)\<\/span\>/is',
- //Code presentation
- '/\<pre class=\"code\"\>(.*?)\<\/pre\>/is'
- );
- $str = preg_replace($htmlcode, $bbcode, $str);
- }
- }
- return $str;
- }
- function time_since($original) {
- // array of time period chunks
- $chunks = array(
- array(60 * 60 * 24 * 365 , 'year'),
- array(60 * 60 * 24 * 30 , 'month'),
- array(60 * 60 * 24 * 7, 'week'),
- array(60 * 60 * 24 , 'day'),
- array(60 * 60 , 'hour'),
- array(60 , 'minute'),
- );
- $today = time(); /* Current unix time */
- $since = $today - $original;
- if($since > 604800) {
- $print = date("F jS", $original);
- if($since > 31536000) {
- $print .= ", " . date("Y", $original);
- }
- return $print;
- }
- // $j saves performing the count function each time around the loop
- for ($i = 0, $j = count($chunks); $i < $j; $i++) {
- $seconds = $chunks[$i][0];
- $name = $chunks[$i][1];
- // finding the biggest chunk (if the chunk fits, break)
- if (($count = floor($since / $seconds)) != 0) {
- // DEBUG print "<!-- It's $name -->\n";
- break;
- }
- }
- $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
- return $print . " ago";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement