Guest User

Untitled

a guest
Aug 17th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. <?php
  2.  
  3. class SessionHandler {
  4.  
  5. function __construct() {
  6.  
  7. session_start();
  8.  
  9. }
  10.  
  11. function set_val($vname, $vvalue) {
  12.  
  13. $_SESSION['{$vname}'] = $vvalue;
  14.  
  15. }
  16.  
  17. function get_val($vname) {
  18.  
  19. echo $_SESSION['{$vname}'];
  20.  
  21. }
  22.  
  23. function writeAndCloseSession() {
  24.  
  25. session_write_close();
  26.  
  27. }
  28.  
  29. function getSessionKey() {
  30.  
  31. $key = session_id();
  32. return $key;
  33. }
  34.  
  35. function endSession() {
  36.  
  37. $_SESSION = array();
  38. session_destroy();
  39. header('Location:index.php');
  40.  
  41. }
  42.  
  43. function serializeObject($Obj) {
  44.  
  45. $sObj = serialize($Obj);
  46. file_put_contents('storeobject',$sObj);
  47.  
  48. }
  49.  
  50. function unserializeObject() {
  51.  
  52. $sObj = file_get_contents('storeobject');
  53. $Obj = unserialize($sObj);
  54. return $Obj;
  55. }
  56.  
  57.  
  58. } // close SessionHandler class.
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. class Connection extends User {
  73.  
  74. protected $conn;
  75.  
  76. private $dbhost;
  77. private $dbuser;
  78. private $dbpass;
  79. private $dbname;
  80. private $tblname;
  81.  
  82. function __construct($dbhost,$dbuser,$dbpass,$dbname) {
  83.  
  84. $this->dbhost = $dbhost;
  85. $this->dbuser = $dbuser;
  86. $this->dbpass = $dbpass;
  87. $this->dbname = $dbname;
  88. $this->conn = $this->connect();
  89.  
  90. } // close __construct function.
  91.  
  92. private function connect() {
  93.  
  94. $this->conn = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass)or die("Cannot connect to host. Error: " . mysql_error());
  95. mysql_select_db($this->dbname,$this->conn)or die("Cannot select database. Error: " . mysql_error());
  96.  
  97. } // close connect function.
  98.  
  99. function __sleep() {
  100.  
  101. return array($this->dbhost,$this->dbuser,$this->dbpass,$this->dbname,$this->conn);
  102.  
  103. } // close __sleep function.
  104.  
  105. function __wakeup() {
  106.  
  107. $this->conn = $this->connect();
  108.  
  109. } // close __wakeup function.
  110.  
  111. function getResource() {
  112.  
  113. return $this->conn;
  114.  
  115. }
  116.  
  117. } // close Connection class.
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. class User {
  132.  
  133. public $userFootprint;
  134.  
  135. protected $conn;
  136. protected $userSession;
  137. protected $userID;
  138. protected $username;
  139. protected $email;
  140. protected $password;
  141. protected $name;
  142. protected $classID;
  143. protected $user_pathtophoto;
  144. protected $myMsgs;
  145. protected $myComments;
  146.  
  147. function __construct($conn) {
  148.  
  149. $this->conn = $conn;
  150.  
  151. }
  152.  
  153. function authenticateUser($username,$password) {
  154. $this->username = mysql_real_escape_string($username);
  155. $this->password = mysql_real_escape_string($password);
  156. $sql = mysql_query("SELECT * FROM userData WHERE loginusername='{$this->username}' AND password='{$this->password}'",$this->conn);
  157. $result = mysql_fetch_array($sql);
  158. $count = mysql_num_rows($sql);
  159.  
  160. if ($count != 1) {
  161. return false;
  162. } else {
  163. $this->userID = $result['userID'];
  164. $this->setUserData();
  165. $this->setSessionData();
  166. return true;
  167. }
  168.  
  169. }
  170.  
  171. private function setUserData() {
  172. $sql = mysql_query("SELECT * FROM userData WHERE userID='{$this->userID}'",$this->conn);
  173. print_r($sql);
  174. while ($results = mysql_fetch_array($sql)) {
  175. $this->email = $results['email'];
  176. $this->name = $results['name'];
  177. $this->classID = $results['classID'];
  178. $this->user_pathtophoto = $results['user_pathtophoto'];
  179. }
  180. $this->userFootprint = array($this->userID,$this->username,$this->email,$this->name,$this->classID,$this->user_pathtophoto);
  181.  
  182. if (isset($error)) {
  183. return $error; }
  184. else {
  185. return true; }
  186. } // close function setUserData.
  187.  
  188. private function setSessionData() {
  189.  
  190. if (!isset($sess)) {
  191. $sess = new SessionHandler();
  192.  
  193. }
  194.  
  195.  
  196. $_SESSION['user'] = $this->username;
  197. $_SESSION['userID'] = $this->userID;
  198. $_SESSION['name'] = $this->name;
  199. $_SESSION['classID'] = $this->classID;
  200. $_SESSION['id'] = session_id();
  201. session_write_close();
  202.  
  203. } // close function setSessionData.
  204.  
  205. function getUserFootprint() {
  206. if (isset($this->userFootprint)) {
  207. return $this->userFootprint; }
  208. else {
  209. if (setUserData()) {
  210. return $this->userFootprint;
  211. } else {
  212. return false;
  213. }
  214. }
  215. } // close function getUserFootprint.
  216.  
  217. function get_UserVal($val) {
  218.  
  219. echo "$this->'{$val}'";
  220.  
  221. }
  222.  
  223.  
  224. function registerUser($username,$password,$email,$name) {
  225. $this->username = $username;
  226. $this->password = $password;
  227. $this->email = $email;
  228. $this->name = $name;
  229. $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);
  230. return $regUser;
  231. } // close function registerUser.
  232.  
  233. function hasPerm($reqPerm) {
  234. if ($this->classID > $regPerm) {
  235. return true;
  236. } else {
  237. return false;
  238. }
  239.  
  240.  
  241. } // close function hasPerm.
  242.  
  243. function isLoggedIn() {
  244. if ($_SESSION['user'] == $this->username) {
  245. return true;
  246. } else {
  247. return false;
  248. }
  249. } // close function isLoggedIn.
  250.  
  251. function logOut() {
  252.  
  253. if (!isset($sess)) {
  254. $sess = new SessionHandler();
  255. } else {
  256. $sess->endSession();
  257. }
  258.  
  259. }
  260.  
  261.  
  262.  
  263. } // close User class.
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286. /*
  287. Post Class:
  288.  
  289. This is specifically for Posts/Articles.
  290.  
  291.  
  292. */
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303. class Post extends User {
  304.  
  305. protected $conn;
  306.  
  307. protected $postID;
  308. protected $postDate;
  309. protected $postCategory;
  310. protected $postSubject;
  311. protected $postBody;
  312. protected $postExcerpt;
  313. protected $postUser;
  314. protected $postBinRef;
  315. public $postData;
  316.  
  317. function __construct($conn) {
  318.  
  319. $this->conn = $conn;
  320.  
  321. }
  322.  
  323.  
  324. function set_postAttribute($pname,$pvalue) {
  325.  
  326. $this->{$pname} = $pvalue;
  327.  
  328. }
  329.  
  330. function getPostAttributes($postID) {
  331. $this->postID = $postID;
  332. $sql = mysql_query("SELECT * FROM posts WHERE postID='{$this->postID}'")or die("There was an error with mysql_query" . mysql_error());
  333. $row = mysql_fetch_assoc($sql)or die("There was an error with mysql_fetch_assoc" . mysql_error());
  334. extract($row, EXTR_PREFIX_SAME, "p");
  335. $this->postID = $postID;
  336. $this->postDate = $postDate;
  337. $this->postCategory = $postCategory;
  338. $this->postSubject = $postSubject;
  339. $this->postBody = $postBody;
  340. $this->postExcerpt = $postExcerpt;
  341. $this->postUser = $postUser;
  342. $this->postBinRef = $postBinRef;
  343. return array($this->postID,$this->postDate,$this->postCategory,$this->postSubject,$this->postBody,$this->postExcerpt,$this->postUser,$this->postBinRef);
  344. }
  345.  
  346.  
  347. function postArticle($postCategory,$postSubject,$postBody) {
  348. $this->postDate = date("Y-m-d");
  349. $this->postCategory = $postCategory;
  350. $this->postSubject = $postSubject;
  351. $this->postBody = $postBody;
  352. $this->postExcerpt = mb_substr($this->postBody, 0, 300) . "...";
  353. $this->postUser = parent::$userID;
  354.  
  355. if (parent::hasPerm(10)) {
  356.  
  357. $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}')");
  358. } else {
  359. echo "Invalid permissions.";
  360. }
  361. }
  362.  
  363. function updateArticle($postID,$postCategory,$postSubject,$postBody,$postExcerpt) {
  364. $this->postID = $postID;
  365. $this->postCategory = $postCategory;
  366. $this->postSubject = $postSubject;
  367. $this->postBody = $postBody;
  368. $this->postExcerpt = mb_substr($this->postBody, 0, 300) . "...";
  369. $this->postUser = parent::$userID;
  370.  
  371.  
  372.  
  373.  
  374. }
  375.  
  376. function removeArticle($postID) {
  377. if (parent::hasPerm(10)) {
  378. }
  379.  
  380. }
  381.  
  382.  
  383. function __toString() {
  384. if (isset($this->postBody)) {
  385. echo $this->postBody;
  386. } else {
  387. echo parent::$userID;
  388. }
  389. }
  390.  
  391. function categoryFormatter($category) {
  392. $this->postCategory = $category;
  393. switch($this->postCategory) {
  394.  
  395. case 1: $this->postCategory = "Entertainment"; return $this->postCategory;
  396. case 2: $this->postCategory = "Business"; return $this->postCategory;
  397. case 3: $this->postCategory = "Science & Health"; return $this->postCategory;
  398. case 4: $this->postCategory = "Technology"; return $this->postCategory;
  399. case 5: $this->postCategory = "Art & Culture"; return $this->postCategory;
  400. case 6: $this->postCategory = "Society & Crime"; return $this->postCategory;
  401. case 7: $this->postCategory = "Politics"; return $this->postCategory;
  402. case 8: $this->postCategory = "Sports"; return $this->postCategory;
  403. case 9: $this->postCategory = "Columns"; return $this->postCategory;
  404. case 10: $this->postCategory = "Military & War"; return $this->postCategory;
  405. } // close switch.
  406.  
  407. } // close categoryFormatter function.
  408.  
  409. function getUserName($userID) {
  410. $sql = mysql_query("SELECT name FROM userData WHERE userID='{$userID}' LIMIT 1");
  411. $results = mysql_fetch_array($sql);
  412. $this->postUser = $results['name'];
  413.  
  414. return $this->postUser;
  415. }
  416.  
  417.  
  418. } // close Article class.
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433. class Comment extends User{
  434.  
  435. public $commentID;
  436.  
  437.  
  438. protected $conn;
  439. protected $cmntID;
  440. protected $cmntUserID;
  441. protected $cmntPostID;
  442. protected $cmntSubject;
  443. protected $cmntBody;
  444.  
  445. function __construct($conn) {
  446. $this->conn = $conn;
  447. }
  448.  
  449. function postComment($cmntUserID,$cmntPostID,$cmntSubject,$cmntBody) {
  450. $this->cmntUserID = $cmntUserID;
  451. $this->cmntPostID = $cmntPostID;
  452. $this->cmntSubject = $cmntSubject;
  453. $this->cmntBody = $cmntBody;
  454.  
  455. $sql = mysql_query("INSERT INTO comments('cmntID','cmntUserID','cmntPostID','cmntSubject','cmntBody') VALUES('','{$this->cmntUserID}','{$this->cmntPostID}','{$this->cmntSubject}','{$this->cmntBody}')");
  456. if ($sql) {
  457. return true;
  458. } else {
  459. return false;
  460. }
  461. }
  462.  
  463.  
  464.  
  465.  
  466.  
  467. }
  468.  
  469.  
  470. ?>
Add Comment
Please, Sign In to add comment