Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class SessionHandler {
- function __construct() {
- session_start();
- }
- function set_val($vname, $vvalue) {
- $_SESSION['{$vname}'] = $vvalue;
- }
- function get_val($vname) {
- echo $_SESSION['{$vname}'];
- }
- function writeAndCloseSession() {
- session_write_close();
- }
- function getSessionKey() {
- $key = session_id();
- return $key;
- }
- function endSession() {
- $_SESSION = array();
- session_destroy();
- header('Location:index.php');
- }
- function serializeObject($Obj) {
- $sObj = serialize($Obj);
- file_put_contents('storeobject',$sObj);
- }
- function unserializeObject() {
- $sObj = file_get_contents('storeobject');
- $Obj = unserialize($sObj);
- return $Obj;
- }
- } // close SessionHandler class.
- class Connection extends User {
- protected $conn;
- private $dbhost;
- private $dbuser;
- private $dbpass;
- private $dbname;
- private $tblname;
- function __construct($dbhost,$dbuser,$dbpass,$dbname) {
- $this->dbhost = $dbhost;
- $this->dbuser = $dbuser;
- $this->dbpass = $dbpass;
- $this->dbname = $dbname;
- $this->conn = $this->connect();
- } // close __construct function.
- private function connect() {
- $this->conn = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass)or die("Cannot connect to host. Error: " . mysql_error());
- mysql_select_db($this->dbname,$this->conn)or die("Cannot select database. Error: " . mysql_error());
- } // close connect function.
- function __sleep() {
- return array($this->dbhost,$this->dbuser,$this->dbpass,$this->dbname,$this->conn);
- } // close __sleep function.
- function __wakeup() {
- $this->conn = $this->connect();
- } // close __wakeup function.
- function getResource() {
- return $this->conn;
- }
- } // close Connection class.
- class User {
- public $userFootprint;
- protected $conn;
- protected $userSession;
- protected $userID;
- protected $username;
- protected $email;
- protected $password;
- protected $name;
- protected $classID;
- protected $user_pathtophoto;
- protected $myMsgs;
- protected $myComments;
- function __construct($conn) {
- $this->conn = $conn;
- }
- function authenticateUser($username,$password) {
- $this->username = mysql_real_escape_string($username);
- $this->password = mysql_real_escape_string($password);
- $sql = mysql_query("SELECT * FROM userData WHERE loginusername='{$this->username}' AND password='{$this->password}'",$this->conn);
- $result = mysql_fetch_array($sql);
- $count = mysql_num_rows($sql);
- if ($count != 1) {
- return false;
- } else {
- $this->userID = $result['userID'];
- $this->setUserData();
- $this->setSessionData();
- return true;
- }
- }
- private function setUserData() {
- $sql = mysql_query("SELECT * FROM userData WHERE userID='{$this->userID}'",$this->conn);
- print_r($sql);
- while ($results = mysql_fetch_array($sql)) {
- $this->email = $results['email'];
- $this->name = $results['name'];
- $this->classID = $results['classID'];
- $this->user_pathtophoto = $results['user_pathtophoto'];
- }
- $this->userFootprint = array($this->userID,$this->username,$this->email,$this->name,$this->classID,$this->user_pathtophoto);
- if (isset($error)) {
- return $error; }
- else {
- return true; }
- } // close function setUserData.
- private function setSessionData() {
- if (!isset($sess)) {
- $sess = new SessionHandler();
- }
- $_SESSION['user'] = $this->username;
- $_SESSION['userID'] = $this->userID;
- $_SESSION['name'] = $this->name;
- $_SESSION['classID'] = $this->classID;
- $_SESSION['id'] = session_id();
- session_write_close();
- } // close function setSessionData.
- function getUserFootprint() {
- if (isset($this->userFootprint)) {
- return $this->userFootprint; }
- else {
- if (setUserData()) {
- return $this->userFootprint;
- } else {
- return false;
- }
- }
- } // close function getUserFootprint.
- function get_UserVal($val) {
- echo "$this->'{$val}'";
- }
- function registerUser($username,$password,$email,$name) {
- $this->username = $username;
- $this->password = $password;
- $this->email = $email;
- $this->name = $name;
- $regUser = mysql_query("INSERT INTO userData(userID, loginusername, email, password, name, classID) VALUES('','{$this->username}', '{$this->email}', '{$this->password}', '{$this->name}', '{INIT_CLASSID}')",$this->conn);
- return $regUser;
- } // close function registerUser.
- function hasPerm($reqPerm) {
- if ($this->classID > $regPerm) {
- return true;
- } else {
- return false;
- }
- } // close function hasPerm.
- function isLoggedIn() {
- if ($_SESSION['user'] == $this->username) {
- return true;
- } else {
- return false;
- }
- } // close function isLoggedIn.
- function logOut() {
- if (!isset($sess)) {
- $sess = new SessionHandler();
- } else {
- $sess->endSession();
- }
- }
- } // close User class.
- /*
- Post Class:
- This is specifically for Posts/Articles.
- */
- class Post extends User {
- protected $conn;
- protected $postID;
- protected $postDate;
- protected $postCategory;
- protected $postSubject;
- protected $postBody;
- protected $postExcerpt;
- protected $postUser;
- protected $postBinRef;
- public $postData;
- function __construct($conn) {
- $this->conn = $conn;
- }
- function set_postAttribute($pname,$pvalue) {
- $this->{$pname} = $pvalue;
- }
- function getPostAttributes($postID) {
- $this->postID = $postID;
- $sql = mysql_query("SELECT * FROM posts WHERE postID='{$this->postID}'")or die("There was an error with mysql_query" . mysql_error());
- $row = mysql_fetch_assoc($sql)or die("There was an error with mysql_fetch_assoc" . mysql_error());
- extract($row, EXTR_PREFIX_SAME, "p");
- $this->postID = $postID;
- $this->postDate = $postDate;
- $this->postCategory = $postCategory;
- $this->postSubject = $postSubject;
- $this->postBody = $postBody;
- $this->postExcerpt = $postExcerpt;
- $this->postUser = $postUser;
- $this->postBinRef = $postBinRef;
- return array($this->postID,$this->postDate,$this->postCategory,$this->postSubject,$this->postBody,$this->postExcerpt,$this->postUser,$this->postBinRef);
- }
- function postArticle($postCategory,$postSubject,$postBody) {
- $this->postDate = date("Y-m-d");
- $this->postCategory = $postCategory;
- $this->postSubject = $postSubject;
- $this->postBody = $postBody;
- $this->postExcerpt = mb_substr($this->postBody, 0, 300) . "...";
- $this->postUser = parent::$userID;
- if (parent::hasPerm(10)) {
- $sql = mysql_query("INSERT INTO Posts ('postID', 'postDate', 'postCategory', 'postSubject', 'postBody', 'postExcerpt', 'postUser') VALUES('', '{$this->postDate}', '{$this->postCategory}', '{$this->postSubject}', '{$this->postBody}', '{$this->postExcerpt}', '{$this->postUser}')");
- } else {
- echo "Invalid permissions.";
- }
- }
- function updateArticle($postID,$postCategory,$postSubject,$postBody,$postExcerpt) {
- $this->postID = $postID;
- $this->postCategory = $postCategory;
- $this->postSubject = $postSubject;
- $this->postBody = $postBody;
- $this->postExcerpt = mb_substr($this->postBody, 0, 300) . "...";
- $this->postUser = parent::$userID;
- }
- function removeArticle($postID) {
- if (parent::hasPerm(10)) {
- }
- }
- function __toString() {
- if (isset($this->postBody)) {
- echo $this->postBody;
- } else {
- echo parent::$userID;
- }
- }
- function categoryFormatter($category) {
- $this->postCategory = $category;
- switch($this->postCategory) {
- case 1: $this->postCategory = "Entertainment"; return $this->postCategory;
- case 2: $this->postCategory = "Business"; return $this->postCategory;
- case 3: $this->postCategory = "Science & Health"; return $this->postCategory;
- case 4: $this->postCategory = "Technology"; return $this->postCategory;
- case 5: $this->postCategory = "Art & Culture"; return $this->postCategory;
- case 6: $this->postCategory = "Society & Crime"; return $this->postCategory;
- case 7: $this->postCategory = "Politics"; return $this->postCategory;
- case 8: $this->postCategory = "Sports"; return $this->postCategory;
- case 9: $this->postCategory = "Columns"; return $this->postCategory;
- case 10: $this->postCategory = "Military & War"; return $this->postCategory;
- } // close switch.
- } // close categoryFormatter function.
- function getUserName($userID) {
- $sql = mysql_query("SELECT name FROM userData WHERE userID='{$userID}' LIMIT 1");
- $results = mysql_fetch_array($sql);
- $this->postUser = $results['name'];
- return $this->postUser;
- }
- } // close Article class.
- class Comment extends User{
- public $commentID;
- protected $conn;
- protected $cmntID;
- protected $cmntUserID;
- protected $cmntPostID;
- protected $cmntSubject;
- protected $cmntBody;
- function __construct($conn) {
- $this->conn = $conn;
- }
- function postComment($cmntUserID,$cmntPostID,$cmntSubject,$cmntBody) {
- $this->cmntUserID = $cmntUserID;
- $this->cmntPostID = $cmntPostID;
- $this->cmntSubject = $cmntSubject;
- $this->cmntBody = $cmntBody;
- $sql = mysql_query("INSERT INTO comments('cmntID','cmntUserID','cmntPostID','cmntSubject','cmntBody') VALUES('','{$this->cmntUserID}','{$this->cmntPostID}','{$this->cmntSubject}','{$this->cmntBody}')");
- if ($sql) {
- return true;
- } else {
- return false;
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment