PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/public/codeCore/Classes/php/Post.php

https://github.com/IAmCorbin/MooKit
PHP | 313 lines | 197 code | 0 blank | 116 comment | 41 complexity | 1702cb1cfbf0b82d1602e86f33a33b8f MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * contains Post Class
  4. * @package MooKit
  5. */
  6. /**
  7. * Post Class
  8. *
  9. * A Class representing a set of related html elements that serve a certain purpose (blog post, static page, game, video, etc...)
  10. *
  11. * @author Corbin Tarrant
  12. * @copyright March 16th, 2010
  13. * @link http://www.IAmCorbin.net
  14. * @package MooKit
  15. */
  16. class Post {
  17. /** @var DB_MySQLi $DB database object */
  18. var $DB;
  19. /** @var string $json_status stores the status (success/error) of post manipulation, and any variables to be sent back to javascript */
  20. var $json_status = NULL;
  21. /** @var int $post_id the post id */
  22. var $post_id;
  23. /** @var int $creator_id post creator's id */
  24. var $creator_id;
  25. /** @var string $title title of this post */
  26. var $title;
  27. /** @var string $html html for this post */
  28. var $html;
  29. /** @var date $dateTime date and time post was created */
  30. var $createTime;
  31. /** @var date $dateTime date and time post was last modified */
  32. var $modTime;
  33. /** Constructor
  34. * @param array $userInput array filled with user input : if creating a new post pass keys{ title, html }, if updating a post pass keys{ post_id, title, html }
  35. * @param bool $newPost switch to create or update a post
  36. * @param function $newUserCallback function that will be called if a new post is successfully added
  37. */
  38. function __construct($userInput, $newPost=TRUE, $newPostCallback=NULL) {
  39. //make sure $userInput is an array
  40. if(!is_array($userInput)) {
  41. $this->json_status = json_encode(array('status'=>'E_MISSING_DATA'));
  42. return;
  43. }
  44. //check for valid passed data
  45. if(!array_keys_exist(array('title','html'),$userInput)) {
  46. $this->json_status = json_encode(array('status'=>'E_MISSING_DATA'));
  47. return;
  48. }
  49. //Filter User Input
  50. $inputFilter = new Filters;
  51. $this->title = $inputFilter->htmLawed($inputFilter->text($userInput['title']));
  52. $this->html = $inputFilter->htmLawed($userInput['html']);
  53. //Check for Errors
  54. if($inputFilter->ERRORS()) {
  55. $this->json_status = json_encode(array('status'=>"E_FILTERS",'title'=>$this->title));
  56. return;
  57. }
  58. //connect to database
  59. $this->DB = new DB_MySQLi;
  60. if($newPost) {
  61. $this->creator_id = $_SESSION['user_id'];
  62. //add post
  63. if($this->addNew()) { //Fire New User Callback if it was passed
  64. $this->json_status = json_encode(array('status'=>'1','title'=>$this->title));
  65. if(is_callable($newPostCallback))
  66. call_user_func($newPostCallback);
  67. return;
  68. } else {
  69. $this->json_status = json_encode(array('status'=>"E_INSERT",'title'=>$this->title));
  70. return;
  71. }
  72. } else {
  73. //check for valid passed data
  74. if(!array_key_exists('post_id',$userInput)) {
  75. $this->json_status = json_encode(array('status'=>'E_MISSING_DATA'));
  76. return;
  77. }
  78. //Filter id
  79. $post_id = $inputFilter->number($userInput['post_id']);
  80. if($inputFilter->ERRORS()) {
  81. $this->json_status = json_encode(array('status'=>"E_FILTERS",'title'=>$this->title));
  82. return;
  83. }
  84. //update post
  85. if($this->update($post_id)) {
  86. $this->json_status = json_encode(array('status'=>'1','title'=>$this->title,'modTime'=>date('Y-m-d H:i:s')));
  87. return;
  88. } else {
  89. $this->json_status = json_encode(array('status'=>"E_UPDATE",'title'=>$this->title));
  90. return;
  91. }
  92. }
  93. }
  94. /**
  95. * Add a new post to the database, using this objects data
  96. * @returns the number of rows affected
  97. */
  98. public function addNew() {
  99. return $this->DB->insert("INSERT INTO `posts`(`creator_id`,`title`,`html`,`createTime`) VALUES(?,?,?,NOW());",
  100. 'iss',array($this->creator_id, $this->title, $this->html));
  101. }
  102. /**
  103. * Updates a post in the database
  104. * @param int $post_id - the post to update
  105. * @returns int - number of rows affected
  106. */
  107. public function update($post_id) {
  108. return $this->DB->update("UPDATE `posts` SET `title`=?, `html`=?, `modTime`=NOW() WHERE `post_id`=?;",
  109. 'ssi', array($this->title, $this->html, $post_id));
  110. }
  111. /**
  112. * Grab a post from the database
  113. * @param int $id the user id to get posts for
  114. * @param string $title the title to search for - optional
  115. * @param string $title the id of the post to grab - optional
  116. * @param string $rType the return type for the posts
  117. * @returns mixed results
  118. */
  119. public static function get($user_id,$title=NULL,$post_id=NULL,$rType="object") {
  120. //filter input
  121. $inputFilter = new Filters;
  122. $user_id = $inputFilter->number($user_id);
  123. if($title) $title = $inputFilter->alphnum_($title,FALSE,TRUE);
  124. if($post_id) $post_id = $inputFilter->number($post_id);
  125. if($inputFilter->ERRORS()) {
  126. return json_encode(array('status'=>"E_FILTERS",'user_id'=>$user_id,'title'=>$title,'post_id'=>$post_id));
  127. }
  128. //connect to Database
  129. $DB = new DB_MySQLi;
  130. //set columns to grab
  131. $columns = "`posts`.`post_id`, `users`.`alias` AS creator, `posts`.`title`, `posts`.`html`, `posts`.`createTime`, `posts`.`modTime`";
  132. if($post_id) {
  133. //grab a single post by ID
  134. return $DB->get_row("SELECT $columns FROM `posts`,`users` WHERE `posts`.`creator_id`=`users`.`user_id` AND `posts`.`post_id`=? LIMIT 1;",
  135. 'i',array($post_id),$rType);
  136. } else {
  137. //grab all the user's posts
  138. $posts = $DB->get_rows("SELECT $columns FROM `posts`,`users` WHERE `posts`.`creator_id`=`users`.`user_id` AND `posts`.`title` LIKE CONCAT('%',?,'%') AND `posts`.`creator_id`=? LIMIT 30;",
  139. 'si',array($title,$user_id),$rType);
  140. //grab all the posts the user has specific permissions for
  141. $otherPosts = $DB->get_rows("SELECT $columns FROM (`posts` JOIN `users` ON `posts`.`creator_id`=`users`.`user_id` AND `posts`.`title` LIKE CONCAT('%',?,'%'))
  142. JOIN `postUserPermissions` ON `posts`.`post_id`=`postUserPermissions`.`post_id`
  143. AND `postUserPermissions`.`access_level`&".ACCESS_WRITE.
  144. " AND `postUserPermissions`.`user_id`=?;",
  145. 'si',array($title,$user_id),$rType);
  146. //merge results
  147. if(is_array($otherPosts))
  148. return array_merge($posts, $otherPosts);
  149. else
  150. return $posts;
  151. }
  152. }
  153. /**
  154. * Remove a post from the database
  155. * @param int $post_id the id of the post to delete
  156. * @returns json_status
  157. */
  158. public static function delete($post_id) {
  159. //Filter id
  160. $inputFilter = new Filters;
  161. $post_id = $inputFilter->number($post_id);
  162. if($inputFilter->ERRORS()) {
  163. return json_encode(array('status'=>"E_FILTERS"));
  164. }
  165. //connect to database
  166. $DB = new DB_MySQLi;
  167. //turn off mysqli autocommit to process as a transaction
  168. $DB->mysqli->autocommit(FALSE);
  169. //remove all sublinks
  170. $DB->delete("DELETE FROM `postUserPermissions` WHERE `post_id`=?;",
  171. 'i',array($post_id));
  172. //remove link
  173. $DB->delete("DELETE FROM `postGroupPermissions` WHERE `post_id`=?;",
  174. 'i',array($post_id));
  175. //delete post
  176. $DB->delete("DELETE FROM `posts` WHERE `post_id`=?;",
  177. 'i',array($post_id));
  178. //rollback or commit
  179. if($DB->STATUS !== "1") {
  180. $DB->mysqli->rollback();
  181. } else if($DB->STATUS === "1")
  182. $DB->mysqli->commit();
  183. //close the database connection
  184. $DB->close();
  185. return json_encode(array('status'=>$DB->STATUS));
  186. }
  187. /**
  188. * Change post user and group permissions
  189. * @param int $post_id the post_id to change permissions for
  190. * @param int $id the user or group id to add
  191. * @param int $access_level the new bitwise permission level - write=2, deny=1
  192. * @param string $U_G user or group permissions - should pass 'user' or 'group'
  193. * @return bool the number of rows affected
  194. */
  195. public static function chmod($post_id, $id, $access_level, $U_G="user") {
  196. if($U_G === 'user') {
  197. $q['table'] = 'postUserPermissions';
  198. $q['id'] = 'user_id';
  199. } else if($U_G ==='group') {
  200. $q['table'] = 'postGroupPermissions';
  201. $q['id'] = 'group_id';
  202. } else return false;
  203. //filter input
  204. $inputFilter = new Filters;
  205. $post_id = $inputFilter->number($post_id);
  206. $id = $inputFilter->number($id);
  207. $access_level = $inputFilter->number($access_level);
  208. if($inputFilter->ERRORS()) {
  209. $this->json_status = json_encode(array('status'=>"E_FILTERS"));
  210. return;
  211. }
  212. //connect to database
  213. $DB = new DB_MySQLi;
  214. //if setting $access_level to 0, just delete the row
  215. if($access_level == 0) {
  216. if($DB->delete("DELETE FROM `".$q['table']."` WHERE `".$q['id']."`=? AND `post_id`=?;",
  217. 'ii',array($id,$post_id))) {
  218. $this->json_status = json_encode(array('status'=>'1'));
  219. return;
  220. } else
  221. $this->json_status = json_encode(array('status'=>'E_DELETE'));
  222. return;
  223. }
  224. //check current access state
  225. if($old_access = $DB->get_row("SELECT `access_level` FROM `".$q['table']."` WHERE `post_id`=? AND `".$q['id']."`=?;",
  226. 'ii',array($post_id,$id))) {
  227. //Access Exists, Update access_level
  228. if($this->DB->update("UPDATE `".$q['table']."` SET `".$q['id']."`=?, `access_level`=? WHERE `post_id`=?;",
  229. 'iii',array($id, $access_level, $post_id))) {
  230. $this->json_status = json_encode(array('status'=>'1'));
  231. return;
  232. } else {
  233. $this->json_status = json_encode(array('status'=>'E_UPDATE'));
  234. return;
  235. }
  236. } else {
  237. //Access Does Not Exist, Insert new row
  238. if($this->DB->insert("INSERT INTO `".$q['table']."`(`".$q['id']."`,`post_id`,`access_level`) VALUES(?,?,?);",
  239. 'iii',array($id,$post_id,$access_level))) {
  240. $this->json_status = json_encode(array('status'=>'1'));
  241. return;
  242. } else {
  243. $this->json_status = json_encode(array('status'=>'E_INSERT'));
  244. return;
  245. }
  246. }
  247. }
  248. /**
  249. * Add a new user permission for a post
  250. * @param int $user_id the user id to addpermissions for
  251. * @param int $post_id the post id to add permissions for
  252. * @param int $access_level the permission level
  253. * @param string $rType the return type for the permissions
  254. * @returns int status
  255. */
  256. public static function addUserPerm($user_id, $post_id, $access_level, $rType="object") {
  257. //filter input
  258. $inputFilter = new Filters;
  259. $user_id = $inputFilter->number($user_id);
  260. $post_id = $inputFilter->number($post_id);
  261. $access_level = $inputFilter->number($access_level);
  262. if($inputFilter->ERRORS()) { return json_encode(array('status'=>"E_FILTERS")); }
  263. //connect to Database
  264. $DB = new DB_MySQLi;
  265. //make sure permission does not already exist
  266. if($existCheck = $DB->get_row("SELECT `user_id` FROM `postUserPermissions` WHERE `user_id`=? AND `post_id`=?;",
  267. 'ii', array($user_id, $post_id))) {
  268. if($rType === "json")
  269. return json_encode(array('status'=>'0'));
  270. else
  271. return $existCheck;
  272. }
  273. //Add new user permission
  274. return $DB->insert("INSERT INTO `postUserPermissions`(`user_id`,`post_id`,`access_level`) VALUES(?,?,?);",
  275. 'iii' ,array($user_id, $post_id, $access_level), $rType);
  276. }
  277. /**
  278. * Grab a post's User Permissions from the database
  279. * @param int $post_id the post id to get permissions for
  280. * @param string $rType the return type for the permissions
  281. * @returns results
  282. */
  283. public static function getUserPerms($post_id=NULL,$rType="object") {
  284. //filter input
  285. $inputFilter = new Filters;
  286. $post_id = $inputFilter->number($post_id);
  287. if($inputFilter->ERRORS()) { return json_encode(array('status'=>"E_FILTERS")); }
  288. //connect to Database
  289. $DB = new DB_MySQLi;
  290. return $DB->get_rows("SELECT `perms`.`user_id`, `users`.`alias`, `perms`.`access_level` FROM `postUserPermissions` AS `perms`, `users`
  291. WHERE `perms`.`user_id`=`users`.`user_id` AND `perms`.`post_id`=?;",
  292. 'i' ,array($post_id), $rType);
  293. }
  294. /**
  295. * Remove a user permission for a post
  296. * @param int $user_id the user id to addpermissions for
  297. * @param int $post_id the post id to add permissions for
  298. * @param string $rType the return type for the permissions
  299. * @returns bool status
  300. */
  301. public static function deleteUserPerm($user_id, $post_id, $rType="object") {
  302. //filter input
  303. $inputFilter = new Filters;
  304. $user_id = $inputFilter->number($user_id);
  305. $post_id = $inputFilter->number($post_id);
  306. if($inputFilter->ERRORS()) { return json_encode(array('status'=>"E_FILTERS")); }
  307. //connect to Database
  308. $DB = new DB_MySQLi;
  309. return $DB->delete("DELETE FROM `postUserPermissions` WHERE `user_id`=? AND `post_id`=?;",
  310. 'ii' ,array($user_id, $post_id), $rType);
  311. }
  312. }
  313. ?>