<?php /* --------------------------------------------------- Class: Community Author: Adam Flatau of Reflect Studios Date: 03/14/08 Description: Class to manage comunity features for a light com soft soultion. --------------------------------------------------*/ /* --------------------------------------------------- Database Layout -------------------------------------------------- id INT AUTO INT uname VARCHAR(16) UNIQE pword VARCHAR(32) name VARCHAR(64) picType VARCHAR(5) sex BOOL - TINYINT(1) dob mediumint(8) UNSIGNED profession varchar(255) linksID int bio text location varchar(255) email VARCHAR(128) UNIQE MSULR VARCHAR(128) BINID INT BlogID INT activated BOOL - TINYINT(1) registered TIMESTAMP - CURRENT_TIMESTAMP views int(11) -------------------------------------------------*/ class community{ public $userTable = array(); public $userProfileTable = array(); public $formTable = array(); public $capTable; public $publicError = ''; public $showLogin =1; function __construct(){ /* ---------------------------------------- Our construtor we will require that memory caching is enabled on this system. After we define our memory caching option we will go ahead and build up our user table. User table will be rebuild every 10 mins and stored into array userTable After we build our userTable and userProfileTable we will build the follwing tables. captcha - Table of captcha images/keys --------------------------------------- formTable - Table of all our custom form data that needs to be updated. Two fields, formTags and formData --------------------------------------- ----------------------------------------*/ // first we willl check our system cache // we will store our userTable and // userProfileTable into our memcache engine $cacher = new cacher(1); $UT = $cacher->get(md5('CMP-USER-TABLE'),'MEM'); if($UT[0]){ $this->userTable = $UT[1]; $UPT = $cacher->get(md5('CMP-USER-PROFILE-TABLE'),'MEM'); if($UPT[0]){ $this->userProfileTable = $UPT[1]; }else{ $this->p_error(0,'Error with getting user profile table from the APC cache system'); } }else{ // ok it is not in the cache time to query // it from the database. $dbSlinger = new dbSlinger(); $sql = 'SELECT * FROM ' . DB_NAME . '.user'; $DBQ = $dbSlinger->db_query($sql,"MYFETCHASC"); if($DBQ[0]){ $uData = $DBQ[1]; $uTable = array(); $upTable = array(); $uTable['time'] = time() + (CACHE_TIME); $upTable['time'] = time() + (CACHE_TIME); for($i=0;$i<count($uData);$i++){ $uTable[$uData[$i]['id']]['uname'] = $uData[$i]['uname']; $uTable[$uData[$i]['id']]['pword'] = $uData[$i]['pword']; $uTable[$uData[$i]['id']]['email'] = $uData[$i]['email']; $uTable[$uData[$i]['id']]['linksID'] = $uData[$i]['linksID']; $uTable[$uData[$i]['id']]['BINID'] = $uData[$i]['BINID']; $uTable[$uData[$i]['id']]['BlogID'] = $uData[$i]['BlogID']; $uTable[$uData[$i]['id']]['activated'] = $uData[$i]['activated']; $uTable[$uData[$i]['id']]['views'] = $uData[$i]['views']; $uTable[$uData[$i]['id']]['picType'] = $uData[$i]['picType']; $upTable[$uData[$i]['id']]['uname'] = $uData[$i]['uname']; $upTable[$uData[$i]['id']]['name'] = $uData[$i]['name']; $upTable[$uData[$i]['id']]['picType'] = $uData[$i]['picType']; $upTable[$uData[$i]['id']]['sex'] = $uData[$i]['sex']; $upTable[$uData[$i]['id']]['dob'] = $uData[$i]['dob']; $upTable[$uData[$i]['id']]['pro'] = $uData[$i]['profession']; $upTable[$uData[$i]['id']]['linksID'] = $uData[$i]['linksID']; $upTable[$uData[$i]['id']]['bio'] = $uData[$i]['bio']; $upTable[$uData[$i]['id']]['MSURL'] = $uData[$i]['MSURL']; $upTable[$uData[$i]['id']]['BINID'] = $uData[$i]['BINID']; $upTable[$uData[$i]['id']]['BlogID'] = $uData[$i]['BlogID']; $upTable[$uData[$i]['id']]['location'] = $uData[$i]['location']; } $addIt = $cacher->add(md5('CMP-USER-TABLE'),$uTable,'MEM',CACHE_TIME); if($addIt[0]){ // ok we now need to add our user profile table to // our system cache. We will store it into memcache $addIt = $cacher->add(md5('CMP-USER-PROFILE-TABLE'),$upTable,'MEM',CACHE_TIME); if($addIt[0]){ $this->userTable = $uTable; $this->userProfileTable = $upTable; }else{ $this->publicError = "Sorry, there was an error please try your request again"; } }else{ // error with storing data into system cache $this->publicError = "Sorry, there was an error , please try your request again"; } }else{ $this->p_error(0,"Error building userTable the data base, check database error logs"); $this->publicError = "Sorry, there was an error , please try your request again"; } } // our captcha table we will store into APC $CT = $cacher->get(md5('CMP-CAPTCHA-TABLE'),'APC'); if($CT[0]){ $this->capTable = $CT[1]; }else{ $Go = $this->buildCap(); if($Go[0]){ $addIt = $cacher->add(md5('CMP-CAPTCHA-TABLE'),$this->capTable,'APC',CACHE_TIME); } } // our form table we will store into APC. $FT = $cacher->get(md5('CMP-FORM-TABLE'),'APC'); if($FT[0]){ $this->formTable = $FT[1]; }else{ $Go = $this->buildForms(); if($Go[0]){ $addIt = $cacher->add(md5('CMP-FORM-TABLE'),$this->formTable,'APC',CACHE_TIME); } } } function userSignUp($userData){ /* ----------------------------------------- Our function to manage users regestration sign up. It Will go trough a step process to make sure the current user name is not already taken and or that the submited user email is not already in the system. userData will be a build array with the following setup userData [userName] = Users Name [passWord] = user password (md5) [email] = User Info [MySpace] = MySpace URL -----------------------------------------*/ $pass = 0; $fail = ''; $userData['userName'] = addslashes($userData['userName']); $userData['passWord'] = addslashes($userData['passWord']); $userData['email'] = addslashes($userData['email']); $userData['fName'] = addslashes($userData['fName']); $userData['MySpace'] = ''; $dbSlinger = new dbSlinger(1,1); $sql = 'SELECT uname,email FROM ' . DB_NAME . '.user WHERE uname = "' . $userData['userName'] . '" OR email = "' . $userData['email'] . '"'; $DBQ = $dbSlinger->db_query($sql,"MYFETCHASC"); if($DBQ[0]){ // we have a result. // time to check to see if they are valid $UN = $DBQ[1][0]['uname']; $UE = $DBQ[1][0]['email']; if(!empty($UN)){ $pass = 0; $fail = 'uname'; }else{ $pass = 1; } if($pass){ if(!empty($UE)){ $pass = 0; $fail = 'email'; } } }else{ $pass = 1; } if($pass){ // ok it passed our user name and email check // we will insert the user info into the database . // before we do that we need to collect the new BlogID // linksID and BlogID $sql = 'SELECT MAX(BINID)+1,MAX(BlogID)+1,MAX(linksID)+1 FROM ' . DB_NAME . '.user'; $DBQ = $dbSlinger->db_query($sql,"MYFETCHALL"); if($DBQ[0]){ $newBINID = $DBQ[1][0]; $newBlogID = $DBQ[1][1]; $newLinksID = $DBQ[1][2]; if(empty($newBINID) && empty($newBlogID) && empty($newLinksID)){ // if all data is returned null // then its a net set of data // set all to one by default $newBINID = 1; $newBlogID = 1; $newLinksID = 1; } if(empty($newBINID)){ $pass = 0; $fail = 'BINID'; } if($pass){ if(empty($newBlogID)){ $pass = 0; $fail = 'BlogID'; } } if($pass){ if(empty($newLinksID)){ $pass = 0; $fail = 'LinksID'; } } }else{ $pass = 0; $fail = 'DB'; } if($pass){ // ok we have all of our new data // time to insert it into the database $sql = 'INSERT INTO ' . DB_NAME . '.user SET uname = "' . $userData['userName'] . '", dob = "' . $userData['dob'] . '", pword = "' . $userData['passWord'] . '", name = "' . $userData['fName'] . '", email="' . $userData['email'] . '", MSURL = "' . $userData['MySpace'] . '"'; $sql .= ', BINID = \'' . $newBINID . '\', BlogID = \'' . $newBlogID . '\', linksID = \'' . $newLinksID . '\''; $DBQ = $dbSlinger->db_query($sql,"DBINSERT"); if($DBQ[0]){ // ok we got our new user // time to send activation email $userID = $dbSlinger->lastID; $this->sendActivationEmail($userID,$userData['email']); return array(1,1); }else{ // error with inserting new user into the database? $this->p_error(0,"Error with adding new user. check DB logs"); return array(0,"Sorry we could not process this account. Please try again later."); } }else{ // ok there was an error with trying to // collect to new data. switch($fail){ case 'BINID': $this->p_error(0,"Error BINID was empty, check sql results on: " . $sql); return array(0,"Sorry we could not process this account. Please try again later."); break; case 'BlogID': $this->p_error(0,"Error blogID was empty, check sql results on: " . $sql); return array(0,"Sorry we could not process this account. Please try again later."); break; case 'LinksID'; $this->p_error(0,"Error linksID was empty, check sql results on: " . $sql); return array(0,"Sorry we could not process this account. Please try again later."); break; case 'DB'; $this->p_error(0,"Error with running SQL Query on community->userSignUp(). Check DB Logs"); return array(0,"Sorry we could not process this account. Please try again later."); break; } } }else{ switch($fail){ case "uname": return array(0,"Sorry, but this user name or email is already taken, Please try another"); break; case "email": return array(0,"Sorry , but this the suplied email address is already in use"); break; } } } function sendActivationEmail($userID,$userEmail){ /* ----------------------------------------- Our function to send a new user the activation email. To activate there account. -----------------------------------------*/ // to generate our email we will open up a template // file and use that as our template draft to send // our email file . $userEmail = stripslashes($userEmail); $pass = 1; $template = TEMPLATE_DIR . 'email.html'; $fo = @fopen($template,'r'); if($fo){ @flock($fo,LOCK_EX); $tempContent = @fread($fo,filesize($template)); if(!$tempContent){ // error with reading template file. $pass = 0; $this->p_error(0,"Error with reading content from teplate: " . $template); } @flock($fo,LOCK_UN); @fclose($fo); }else{ // error with opening template file $pass = 0; $this->p_error(0,"Error with opening template file. Template: " . $template); } if($pass){ // ok we have our template content. // we will simply just replace our key // with a md5 has of the userID // Key = [USER-HASH] $tempContent = str_replace('[USER-ID]',$userID,$tempContent); $tempContent = str_replace('[USER-HASH]',md5($userEmail),$tempContent); // prepare our email $subject = ACTIVATION_SUBJECT; $to = $userEmail; $headers = "From: do-not-reply@custommyspaceprofiles.com\r\n"; // send our email if(mail($to,$subject,$tempContent,$headers)){ // successfully sent email }else{ // error with sending email $this->p_error(0,"Error with sending activation email to:" . $userEmail); } } } function activateAccount($userID,$hash){ /* ----------------------------------------- Function to activate account in the database. We will check the user id supplied with the md5 hash of the users email, if both check out activat account -----------------------------------------*/ // we will first check the userTable cache // to see if the user is in the cache yet if($this->userTable[$userID]['uname']){ // ok good the user is in the cache. // just check the md5 email from cache. if($hash == md5($this->userTable[$userID]['email'])){ // ok link is valid time to update // the database and activate account. $dbSlinger = new dbSlinger(); $sql = 'UPDATE ' . DB_NAME . '.user SET activated = \'1\' WHERE id = \'' . $userID . '\''; $DBQ = $dbSlinger->db_query($sql); if($DBQ[0]){ // ok we are good to go return array(1,1); }else{ // error with updating account info . $this->p_error(0,"Error with updating activation field for user:" . $userID . " check DB log"); return array(0,"Sorry there was an error while trying to activate your account. Please try again later"); } }else{ // ok link is not valid return array(0,"Sorry, but that is not a valid activation"); } }else{ // ok user is not in cache yet. // will check from database. $dbSlinger = new dbSlinger(); $sql = 'SELECT email FROM ' . DB_NAME . '.user WHERE id = \'' . $userID . '\''; $DBQ = $dbSlinger->db_query($sql,"MYFETCHASC"); if($DBQ[0]){ $email = $DBQ[1][0]['email']; if($hash == md5($email)){ // ok link is valid time to update // the database and activate account. $sql = 'UPDATE ' . DB_NAME . '.user SET activated = \'1\' WHERE id = \'' . $userID . '\''; $DBQ = $dbSlinger->db_query($sql); if($DBQ[0]){ // ok we are good to go return array(1,1); }else{ // error with updating account info . $this->p_error(0,"Error with updating activation field for user:" . $userID . " check DB log"); return array(0,"Sorry there was an error while trying to activate your account. Please try again later"); } }else{ // ok link is not valid return array(0,"Sorry, but that is not a valid activation"); } }else{ // error with getting user email $this->p_error(0,"Error with getting users email address from the database for activation. Check DB logs"); return array(0,"Sorry, but we could not activate your account succesfully."); } } } function userLogIn($userName,$password){ /* -------------------------------------------- Function to handle user login transactions $password will be md5 hashed before it is submited to function -------------------------------------------*/ $userName = addslashes($userName); $dbSlinger = new dbSlinger(1,1); $sql = 'SELECT id,pword FROM ' . DB_NAME . '.user WHERE uname = \'' . $userName . '\''; $DBQ = $dbSlinger->db_query($sql,"MYFETCHASC"); if($DBQ[0]){ if($password == $DBQ[1][0]['pword']){ return array(1,$DBQ[1][0]['id']); }else{ return array(0,"Sorry, but you have entered a invalid username and or password"); } }else{ $this->p_error(0,"Error with getting user password for " . $userName . " Check DB logs"); return false; } } function userUpdateInfo($userData){ /* -------------------------------------------- Function to update the user info table in the database. $userData is assoicated array of data to update; Will NOT update userName password or email. -------------------------------------------*/ if(!empty($userData)){ if(!empty($_SESSION['userID'])){ $userData['name'] = addslashes($userData['userName'],ENT_QUOTES); $userData['profession'] = addslashes($userData['profession'],ENT_QUOTES); $userData['bio'] = htmlentities($userData['bio']); $userData['MSURL'] = htmlentities($userData['MSURL']); $dbSlinger = new dbSlinger(1,1); $sql = 'UPDATE ' . DB_NAME . '.user SET '; $keys = array_keys($userData); for($i=0;$i<count($keys);$i++){ $sql .= $keys[$i] . '="' . $userData[$keys[$i]] . '"'; if($i < count($keys)-1){ $sql .= ', '; } } $sql .= ' WHERE id = \'' . $_SESSION['userID'] . '\''; $DBQ = $dbSlinger->db_query($sql); if($DBQ[0]){ // ok updated the info succesfully return array(1,"Updated information succesfully"); }else{ $this->p_error(0,"Error with updating user information, SQL:" . $sql); return array(0,"Sorry, there was an error while trying to update your information. Please try again later"); } }else{ return array(0,"Sorry you must be logged in for this"); } }else{ return array(0,"Sorry, supplied data was empty"); } } function userChangeEmail($newEmail){ /* -------------------------------------------- Function to change users email addy. We will update that user info then de-activate the account and re-send a confirmation email. -------------------------------------------*/ if(!empty($newEmail)){ $dbSlinger = new dbSlinger(); $sql = 'UPDATE ' . DB_NAME . '.user SET email = \'' . addslashes($newEmail,ENT_QUOTES) . '\', activated= 0 WHERE id = \'' . $_SESSION['userID'] . '\''; $DBQ = $dbSlinger->db_query($sql); if($DBQ[0]){ // ok updated the account successfully // time to send activation again . $this->sendActivationEmail($_SESSION['userID'],$newEmail); return array(1,1); }else{ $this->p_error(0,"Error witih updating user email address. "); return array(0,"Sorry, there was an error while trying to update email address"); } }else{ // suplied email was empty return array(0,"Sorry, there was an error while trying to update email address try again"); } } function userChangePW($newPW,$oldPW){ /* -------------------------------------------- Function to change the user's password. pretty obvious -------------------------------------------*/ if(!empty($newPW)){ if($oldPW == $_SESSION['pword']){ // ok it checks out time to update $dbSlinger = new dbSlinger(); $sql = 'UPDATE ' . DB_NAME . '.user SET pword = "' . $newPW . '" WHERE id = \'' . $_SESSION['userID'] .'\''; $DBQ = $dbSlinger->db_query($sql); if($DBQ[0]){ // updated password successfully $_SESSION['pword'] = $newPW; return array(1,'Updated password succesfully'); }else{ $this->p_error(0,"Error with updating users password, check DB logs"); return array(0,"Sorry, there was an error while trying to udpate your password"); } }else{ return array(0,"Incorrect password"); } }else{ // new passowrd was empty } } function buildCap(){ /* -------------------------------------------- Function to get our captcha data from the db and store it into the cap array. -------------------------------------------*/ $dbSlinger = new dbSlinger(); $sql = 'SELECT * FROM ' . DB_NAME . '.captcha'; $DBQ = $dbSlinger->db_query($sql,"MYFETCHASC"); if($DBQ[0]){ // ok we have our data time to return it $this->capTable = $DBQ[1]; return array(1,1); }else{ // error with getting data from db $this->p_error(0,"Error with getting captcha data from database, check sql logs"); return array(0,"Error with building captcha table"); } } function buildForms(){ /* -------------------------------------------- Function to get our form dataout of the db and store it into the formTable array. -------------------------------------------*/ $dbSlinger = new dbSlinger(); $sql = 'SELECT * FROM ' . DB_NAME . '.forms'; $DBQ = $dbSlinger->db_query($sql,"MYFETCHASC"); if($DBQ[0]){ for($i=0;$i<count($DBQ[1]);$i++){ $this->formTable[$DBQ[1][$i]['id']]['formKeys'] = $DBQ[1][$i]['formKeys']; $this->formTable[$DBQ[1][$i]['id']]['formHTML'] = $DBQ[1][$i]['formHTML']; } return array(1,1); }else{ $this->p_error(0,"Error with building form data from database, check sql logs"); return array(0,"Error with building form table"); } } function formBuilder($formID,$keyData = array()){ /* ----------------------------------------- Our functio to produce forms build from our formTable templates. There will be a set of key tags that are replaced with data and re-rendered for the final HTML produced form. $keyData will be an asc array that will contain the key name and value. Example: keyData ['var'] = value ['var2'] = value2 -----------------------------------------*/ // first have to get our data //$formKeys = $this->formTable[$formID]['formKeys']; // $formHTML = html_entity_decode($this->formTable[$formID]['formHTML']); // once we have our data time to break out our // formKeys into an array //$arrayKeys = split(','$formKeys); } function accountNav(){ /* -------------------------------------------- Function to spit out the navagation for login. this should be in a template format, Will update this at a later time. -------------------------------------------*/ if($this->showLogin){ echo '<div id="nav2">'; echo ' <div id="logInForm1">'; echo ' <form name="login1" method="post" action="http://app.custommyspaceprofiles.com/membership/login.php">'; echo ' <span>User name:</span><br />'; echo ' <input type="text" class="txt1" id="un1" onfocus="changeInput(\'un1\')" onblur="resetInput(\'un1\')" name="username" /><br />'; echo ' <div class="spacer1"></div>'; echo ' <span>Password:</span><br />'; echo ' <input type="password" class="txt1" id="pw1" onkeypress="checkEnter(event,1)" onfocus="changeInput(\'pw1\')" onblur="resetInput(\'pw1\')" name="password" /><br />'; echo ' </form>'; echo ' <div style="float:right; margin:5px 0px 0px 0px;">'; echo ' <img src="http://app.custommyspaceprofiles.com/images/buttons/login1.jpg" border="0" alt="Log In" />'; echo ' </div>'; echo ' <div style="clear:both;"></div>'; echo ' <a href="#" title="Forgot Password">Forgot password?</a><br />'; echo ' <a href="#" title="Troubles Loging In">Troubles loging in?</a><br />'; echo ' <div class="spacer1"></div>'; echo ' <center>'; echo ' <span class="plug1">Not A Memeber?</span><br />'; echo ' <a href="#" class="signup1" title="Sign Up Now!">Sign Up Now</a><br />'; echo ' <div class="spacer2"></div>'; echo ' <ul>'; echo ' <li>It\'s Fast!</li>'; echo ' <li>It\'s Easy!</li>'; echo ' <li>It\'s FREE!</li>'; echo ' </ul>'; echo ' </center>'; echo ' </div>'; echo ' <div style="float:left; margin-top:17px; padding-bottom:3px; margin-left:2px;"><img src="http://app.custommyspaceprofiles.com/images/buttons/more-info1.jpg" border="0" alt="More Info" /></div>'; echo ' <div style="float:right;margin-top:17px; padding-bottom:3px; margin-right:2px;"><img src="http://app.custommyspaceprofiles.com/images/buttons/sign-up1.jpg" border="0" alt="Sign Up" /></div>'; echo ' <div style="clear:both;"></div>'; echo '</div>'; }else{ echo ' <div id="accountHolder">'; echo ' <div id="myAccount">'; echo ' <div id="accountHeader">'; echo ' <div style="float:left">'; echo ' <span style="font-size:12px;"><strong>Welcome,</strong></span><br />'; echo ' <span style="font-size:12px;">' . $_SESSION['uname'] . '</span>'; echo ' </div>'; echo ' <div style="float:right">'; echo ' <img src="http://app.custommyspaceprofiles.com/users/images/'; if($this->userTable[$_SESSION['userID']]['picType']){ echo md5('MICRO-' . $_SESSION['userID']) . '.jpg" border="0" alt="' . $_SESSION['uname'] . '" />'; }else{ echo md5('DEFAULT-MICRO') . '.jpg" border="0" alt = "' . $_SESSION['uname'] . '"/>'; } echo ' </div>'; echo ' <div style="clear:both; padding-top:8px;"></div>'; echo ' <span>View Your <a href="#" title="Profile">Profile</a> | <a href="#" title="Blog">Blog</a></span><br />'; echo ' <div style="clear:both; padding-top:6px;"></div>'; echo ' <span>Profile Views: ' . $this->userTable[$_SESSION['userID']]['views'] . '</span><br />'; echo ' <span>Blog Comments: ' . $blah . '</span><br />'; echo ' </div>'; echo ' <div id="alerts">'; echo ' </div>'; echo ' <div id="accountNav">'; echo ' <ul>'; echo ' <li><a href="membership/myaccount.php" target="_self" title="My Profile"><img src="http://app.custommyspaceprofiles.com/images/buttons/myProfile.jpg" border="0" alt="My Profile" /></a></li>'; echo ' <li><img src="http://app.custommyspaceprofiles.com/images/buttons/myBlog.jpg" border="0" alt="My Blog" /></li>'; echo ' <li><img src="http://app.custommyspaceprofiles.com/images/buttons/myFavorites.jpg" border="0" alt="My Favorites" /></li>'; echo ' <li><img src="http://app.custommyspaceprofiles.com/images/buttons/accountSettings.jpg" border="0" alt="Account Settings" /></li>'; echo ' </ul>'; echo ' </div>'; echo ' <div style="clear:both;"></div>'; echo ' </div>'; echo '</div>'; } } function updateUserTable(){ return $this->userTable; } function p_error($err,$sql = ""){ switch($err){ case 0: // custom error $err = "0 - " . date("m-j-y") . " Custom Error: " . $sql; write_error("COMMUNITY",$err); break; default: break; } } }?>