Advertisement
Guest User

Untitled

a guest
Sep 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.89 KB | None | 0 0
  1. <?php
  2.  
  3. Class PostsController extends AppController
  4. {
  5.  
  6. public function vote()
  7. {
  8. $post_id = $_GET['post_id'];
  9. $table = "vote_" . $_GET['vote'];
  10. if ($_GET && $_GET['post_id'] && $_GET['vote'] && ($_GET['vote'] == "true" || $_GET['vote'] == "false"))
  11. {
  12. if (!isset($_SESSION['posts_votes'][date("Y-m-d")][$post_id]) && !isset($_COOKIE[date("Ymd").$post_id]))
  13. {
  14. $this->SQL->query("UPDATE posts SET $table=$table+1 WHERE id='$post_id'");
  15. $salida = $this->SQL->query("SELECT $table FROM posts WHERE id='$post_id'");
  16. $_SESSION['posts_votes'][date("Y-m-d")][$post_id] = "1";
  17. setcookie(date("Ymd").$post_id, "cookie", time() + 86400);
  18. echo "Gracias (".$salida['0'][$table].")";
  19. }else{
  20. $salida = $this->SQL->query("SELECT $table FROM posts WHERE id='$post_id'");
  21. echo "Ya has votado (".$salida['0'][$table].")";
  22. }
  23. }
  24. exit;
  25. }
  26.  
  27. public function report_comment()
  28. {
  29. if ($_GET && $_SESSION['login'] == "1")
  30. {
  31. if (!isset($_SESSION['report_comments'][date("Y-m-d")][$comment_id]) && !isset($_COOKIE[date("Ymd")."-c-".$comment_id]))
  32. {
  33. $comment_id = $_GET['comment_id'];
  34. $this->SQL->query("UPDATE comments SET report=1 WHERE id='$comment_id'");
  35. $_SESSION['report_comments'][date("Y-m-d")][$comment_id] = "1";
  36. setcookie(date("Ymd")."-c-".$comment_id, "cookie", time() + 86400);
  37. }
  38. echo "Gracias. En breve revisaremos este comentario";
  39. }else if ($_SESSION['login'] != "1"){
  40. echo "Debes estar registrado para denunciar";
  41. }
  42. exit;
  43. }
  44.  
  45. public function index($params)
  46. {
  47. global $mysql_username;
  48. global $mysql_password;
  49. global $mysql_database;
  50.  
  51. if ($mysql_username == "" || $mysql_password == "" || $mysql_database == "")
  52. {
  53. header("Location: /install/step1");
  54. }
  55.  
  56. $config = $this->SQL->query("SELECT * FROM config");
  57. $tmp=array();
  58. foreach ($config as $c)
  59. {
  60. $tmp[$c['entry']]=$c['value'];
  61. }
  62.  
  63. $baned = $this->SQL->query("SELECT word FROM baned_words");
  64. $_SESSION['site_config']=$tmp;
  65. $_SESSION['site_config']['baned_words']=$baned;
  66.  
  67. $pagina = $params['page'];
  68. $registrosAMostrar = "12";
  69.  
  70. if ($pagina != "")
  71. {
  72. $registrosAEmpezar = ($pagina - 1) * $registrosAMostrar;
  73. $PagAct = $pagina;
  74. } else
  75. {
  76. $registrosAEmpezar = "0";
  77. $PagAct = 1;
  78. }
  79.  
  80. $posts = $this->SQL->query("
  81. SELECT
  82. Post.id as post_id,
  83. Post.date as post_date,
  84. Post.*,
  85. User.*,
  86. Cat.name as category_name
  87. FROM
  88. categories Cat,
  89. posts Post,
  90. users User
  91. WHERE
  92. Post.category_id = Cat.id AND
  93. Post.user_id = User.id
  94. ORDER BY Post.id DESC LIMIT $registrosAEmpezar,$registrosAMostrar
  95. ");
  96.  
  97. $tmp = $this->SQL->query("
  98. SELECT
  99. COUNT(*) as num
  100. FROM
  101. categories Cat,
  102. posts Post,
  103. users User
  104. WHERE
  105. Post.category_id = Cat.id AND
  106. Post.user_id = User.id");
  107.  
  108. $NroRegistros = $tmp[0]['num'];
  109.  
  110. $PagUlt = $NroRegistros / $registrosAMostrar;
  111. $Res = $NroRegistros % $registrosAMostrar;
  112. if ($Res > 0)
  113. $PagUlt = floor($PagUlt) + 1;
  114. if ($PagUlt == 0)
  115. {
  116. $PagUlt = 1;
  117. }
  118. $this->set('totalPages',$PagUlt);
  119.  
  120. $Posts = array();
  121.  
  122. foreach ($posts as $post)
  123. {
  124. $comments = $this->SQL->count_records("comments","post_id",$post['post_id']);
  125. $post['comments'] = $comments;
  126. $Posts[] = $post;
  127. }
  128.  
  129. $this->set('Posts',$Posts);
  130. $this->set('PageTitle',$_SESSION['site_config']['site_name']);
  131. $this->set('MetaDescription',$_SESSION['site_config']['site_description']);
  132. $this->set('MetaKeywords',$_SESSION['site_config']['meta_tags']);
  133. }
  134.  
  135. public function filter($params)
  136. {
  137. $pagina = $params['page'];
  138. $registrosAMostrar = "12";
  139.  
  140. if ($pagina != "")
  141. {
  142. $registrosAEmpezar = ($pagina - 1) * $registrosAMostrar;
  143. $PagAct = $pagina;
  144. } else
  145. {
  146. $registrosAEmpezar = "0";
  147. $PagAct = 1;
  148. }
  149.  
  150. if (!$_POST)
  151. {
  152. if ($params[0] == "" && $_SERVER['REQUEST_URI'] != "/suerte")
  153. {
  154. header("Location: /");
  155. }
  156. if ($_SERVER['REQUEST_URI'] == "/suerte")
  157. {
  158. $extra = "";
  159. $order = "RAND()";
  160. }else{
  161. $extra = "Cat.uri='{$params[0]}' AND ";
  162. $order = "Post.id DESC";
  163. }
  164.  
  165. $posts = $this->SQL->query("
  166. SELECT
  167. Post.id as post_id,
  168. Post.date as post_date,
  169. Post.*,
  170. User.*,
  171. Cat.name as category_name
  172. FROM
  173. categories Cat,
  174. posts Post,
  175. users User
  176. WHERE ".$extra."Post.category_id = Cat.id AND
  177. Post.user_id = User.id
  178. ORDER BY $order LIMIT $registrosAEmpezar,$registrosAMostrar");
  179.  
  180. $tmp = $this->SQL->query("
  181. SELECT
  182. COUNT(*) as num
  183. FROM
  184. categories Cat,
  185. posts Post,
  186. users User
  187. WHERE ".$extra."Post.category_id = Cat.id AND
  188. Post.user_id = User.id");
  189. }else{
  190. if ($_POST['text'] == "" || strlen($_POST['text']) < 3)
  191. {
  192. header("Location: /");
  193. }
  194. $posts = $this->SQL->query("
  195. SELECT
  196. Post.id as post_id,
  197. Post.date as post_date,
  198. Post.*,
  199. User.*,
  200. Cat.name as category_name
  201. FROM
  202. categories Cat,
  203. posts Post,
  204. users User
  205. WHERE
  206. Post.post LIKE '%".$_POST['text']."%' AND
  207. Post.category_id = Cat.id AND
  208. Post.user_id = User.id
  209. ORDER BY Post.id DESC LIMIT $registrosAEmpezar,$registrosAMostrar");
  210.  
  211. $tmp = $this->SQL->query("
  212. SELECT
  213. COUNT(*) as num
  214. FROM
  215. categories Cat,
  216. posts Post,
  217. users User
  218. WHERE
  219. Post.post LIKE '%".$_POST['text']."%' AND
  220. Post.category_id = Cat.id AND
  221. Post.user_id = User.id");
  222. }
  223.  
  224. $NroRegistros = $tmp[0]['num'];
  225. $PagUlt = $NroRegistros / $registrosAMostrar;
  226. $Res = $NroRegistros % $registrosAMostrar;
  227. if ($Res > 0)
  228. $PagUlt = floor($PagUlt) + 1;
  229. if ($PagUlt == 0)
  230. {
  231. $PagUlt = 1;
  232. }
  233. $this->set('totalPages',$PagUlt);
  234.  
  235.  
  236. $Posts = array();
  237.  
  238. foreach ($posts as $post)
  239. {
  240. $comments = $this->SQL->count_records("comments","post_id",$post['post_id']);
  241. $post['comments'] = $comments;
  242. $Posts[] = $post;
  243. }
  244.  
  245. if (!$_POST)
  246. {
  247. if (count($Posts) > 0)
  248. {
  249. $Title = $Posts['0']['category_name'];
  250. }else{
  251. $pt = $this->SQL->query("
  252. SELECT
  253. name
  254. FROM
  255. categories
  256. WHERE
  257. uri='{$params[0]}'");
  258. $Title = $pt['0']['name'];
  259.  
  260. }
  261. }else{
  262. $Title = $_POST['text'];
  263. }
  264. $this->set('PageTitle',"{$Title}");
  265.  
  266. $this->set('Posts',$Posts);
  267. }
  268.  
  269. public function details($params)
  270. {
  271. if ($params['action'] == "comment")
  272. {
  273. $this->checkAuth("?URI=/entrada/" . $params[0]);
  274. $this->set('leave_a_comment',"true");
  275. }else{
  276. $this->set('leave_a_comment',"false");
  277. }
  278.  
  279. if ($_POST)
  280. {
  281. $comment_id = $this->SQL->insert("comments",$_POST['data']);
  282. $this->SQL->query("UPDATE users SET comments=comments + 1 WHERE id='".$_SESSION['user']['id']."'");
  283. unset($_POST);
  284. }
  285.  
  286. $posts = $this->SQL->query("
  287. SELECT
  288. Post.id as post_id,
  289. Post.date as post_date,
  290. Post.*,
  291. User.*,
  292. Cat.name as category_name
  293. FROM
  294. categories Cat,
  295. posts Post,
  296. users User
  297. WHERE
  298. Post.category_id = Cat.id AND
  299. Post.user_id = User.id AND
  300. Post.id = '{$params[0]}'
  301. ");
  302.  
  303. $Posts = array();
  304.  
  305. foreach ($posts as $post)
  306. {
  307. $comments = $this->SQL->query("
  308. SELECT
  309. Comment.date as comment_date,
  310. Comment.comments as comment,
  311. Comment.id as comment_id,
  312. Comment.*,
  313. User.*
  314. FROM
  315. comments Comment,
  316. users User
  317. WHERE
  318. Comment.post_id = '".$post['post_id']."' AND
  319. Comment.user_id = User.id
  320. ");
  321.  
  322. $post['comments'] = $comments;
  323. $Posts = $post;
  324. }
  325.  
  326. $this->set('PageTitle',$Posts['title']);
  327. $this->set('Post',$Posts);
  328. }
  329.  
  330. public function add($params)
  331. {
  332. $this->checkAuth("?URI=/publicar");
  333. if ($_POST){
  334.  
  335. if ($resp->is_valid) {
  336.  
  337. $post_id = $this->SQL->insert("posts",$_POST['data']);
  338. $cat_id = $_POST['data']['category_id'];
  339. $cat = $this->SQL->query("SELECT uri FROM categories WHERE id='$cat_id'");
  340. $this->SQL->query("UPDATE users SET posts=posts + 1 WHERE id='".$_SESSION['user']['id']."'");
  341. header("Location: /categoria/" . $cat[0]['uri']);
  342.  
  343. } else {
  344. die('Codigo incorrecto.');
  345. }
  346.  
  347. }else{
  348. $categorias = $this->SQL->query("SELECT * FROM categories");
  349. $this->set('categories',$categorias);
  350. }
  351. $this->set('PageTitle',"Publicar");
  352. }
  353. }
  354.  
  355. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement