A new, object-oriented, better vesion of μForum
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

723 lines
23 KiB

  1. <?php
  2. require ('./inc/controller.class.php');
  3. class MainController extends Controller
  4. {
  5. public function loadDefault()
  6. {
  7. $this->main();
  8. }
  9. private function loadDependencies() // zależności (sesje itp)
  10. {
  11. $this->loadModel('SessionModel'); //aktywacja sesji
  12. $this->loadModel('ConfigModel'); //konfiguracja ogólna skryptu
  13. $this->loadView('MainView');
  14. $this->getView('MainView')->putExistingModel('SessionModel', $this->getModel('SessionModel'));
  15. $this->getView('MainView')->putExistingModel('ConfigModel', $this->getModel('ConfigModel'));
  16. }
  17. public function main()
  18. {
  19. $this->loadDependencies();
  20. $this->getView('MainView')->main();
  21. }
  22. public function viewforum()
  23. {
  24. $this->loadDependencies();
  25. $this->loadModel('ForumsModel');
  26. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  27. $f = $this->getModel('ForumsModel')->getForum($_GET['id']);
  28. if ($f == null)
  29. $this->getView('MainView')->forum_message('Forum does not exist!', 'index.php');
  30. else
  31. {
  32. $this->getView('MainView')->putExistingModel('ForumsModel', $this->getModel('ForumsModel'));
  33. $this->getView('MainView')->viewforum();
  34. }
  35. }
  36. public function userlist()
  37. {
  38. $this->loadDependencies();
  39. if (isset($_GET['rank']))
  40. {
  41. switch ($_GET['rank'])
  42. {
  43. case 'admin':
  44. $_GET['rank'] = RANK_ADMIN;
  45. break;
  46. case 'mod':
  47. $_GET['rank'] = RANK_MOD;
  48. break;
  49. case 'user':
  50. $_GET['rank'] = RANK_USER;
  51. break;
  52. default:
  53. $_GET['rank'] = '';
  54. break;
  55. }
  56. }
  57. else
  58. $_GET['rank'] = '';
  59. $_POST['sort_type'] = (isset($_POST['sort_type'])) ? $this->db->real_escape_string($_POST['sort_type']) : 'regdate';
  60. $allowed_sorting = array('regdate', 'lastvisit', 'nick', 'post_count');
  61. if (!in_array($_POST['sort_type'], $allowed_sorting))
  62. $_POST['sort_type'] = '';
  63. $_POST['sort_desc'] = (isset($_POST['sort_desc'])) ? 'DESC' : 'ASC';
  64. $this->getView('MainView')->userlist();
  65. }
  66. public function viewtopic()
  67. {
  68. $this->loadDependencies();
  69. $this->loadModel('PostsModel');
  70. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  71. $t = $this->getModel('PostsModel')->getTopic($_GET['id']);
  72. if ($t == null)
  73. $this->getView('MainView')->forum_message('Topic does not exist!', 'index.php');
  74. else
  75. {
  76. $this->getView('MainView')->putExistingModel('PostsModel', $this->getModel('PostsModel'));
  77. $this->getView('MainView')->viewtopic();
  78. }
  79. }
  80. public function newtopic()
  81. {
  82. $this->posting(POSTING_NEWTOPIC);
  83. }
  84. public function reply()
  85. {
  86. $this->posting(POSTING_REPLY);
  87. }
  88. public function editpost()
  89. {
  90. $this->posting(POSTING_EDIT);
  91. }
  92. public function quote()
  93. {
  94. $this->posting(POSTING_QUOTE);
  95. }
  96. public function moderate()
  97. {
  98. $this->loadDependencies();
  99. $this->loadModel('PostsModel');
  100. $this->loadModel('ForumsModel');
  101. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  102. $_GET['submode'] = (isset($_GET['submode'])) ? trim(strip_tags($this->db->real_escape_string($_GET['submode']))) : 0;
  103. if (!$this->getModel('SessionModel')->isLogged())
  104. {
  105. $this->getView('MainView')->forum_message('You are not logged.', 'index.php?mode=login');
  106. $lockv = true;
  107. }
  108. if ($this->getModel('SessionModel')->getRank() == RANK_USER && !isset($lockv))
  109. {
  110. $this->getView('MainView')->forum_message('Only mods have access to this menu', 'index.php');
  111. $lockv = true;
  112. }
  113. //sprawdź czy wątek/post istnieje
  114. if (!isset($lockv))
  115. switch($_GET['submode'])
  116. {
  117. case 'deletetopic':
  118. case 'locktopic':
  119. case 'movetopic':
  120. $t = $this->getModel('PostsModel')->getTopic($_GET['id']);
  121. if ($t == null)
  122. {
  123. $this->getView('MainView')->forum_message('Topic does not exist!', 'index.php');
  124. $lockv = true;
  125. }
  126. break;
  127. case 'deletepost':
  128. $p = $this->getModel('PostsModel')->getPost($_GET['id']);
  129. if ($p == null)
  130. {
  131. $this->getView('MainView')->forum_message('Post does not exist!', 'index.php');
  132. $lockv = true;
  133. }
  134. else
  135. {
  136. $t = $this->getModel('PostsModel')->getTopic($p['topic_id']);
  137. if ($t['post_count'] == 1)
  138. {
  139. $this->getView('MainView')->forum_message('If topic has only one post, use <span style="font-weight: bold">delete topic</span> option.', 'index.php?mode=viewtopic&amp;id='.$p['topic_id'], 3);
  140. $lockv = true;
  141. }
  142. }
  143. break;
  144. default:
  145. $this->getView('MainView')->forum_message('Invalid mode', 'index.php');
  146. $lockv = true;
  147. break;
  148. }
  149. //wysyłanie formularza
  150. if (isset($_POST['confirmed']) && !isset($lockv))
  151. {
  152. if (!isset($_POST['rejected']))
  153. {
  154. switch($_GET['submode'])
  155. {
  156. case 'deletepost':
  157. $this->getModel('PostsModel')->deletePost($_GET['id']);
  158. $this->getView('MainView')->forum_message('Post deleted. Redirecting...', 'index.php?mode=viewtopic&amp;id='.$p['topic_id']);
  159. $lockv = true;
  160. break;
  161. case 'deletetopic':
  162. $this->getModel('PostsModel')->deleteTopic($_GET['id']);
  163. $this->getView('MainView')->forum_message('Topic deleted. Redirecting...', 'index.php?mode=viewforum&amp;id='.$t['forum_id']);
  164. $lockv = true;
  165. break;
  166. case 'locktopic':
  167. if ($t['topic_locked'] == false)
  168. {
  169. $this->getModel('PostsModel')->lockTopic($_GET['id']);
  170. $this->getView('MainView')->forum_message('Topic locked. Redirecting...', 'index.php?mode=viewtopic&amp;id='.$_GET['id']);
  171. }
  172. else
  173. {
  174. $this->getModel('PostsModel')->lockTopic($_GET['id'], false);
  175. $this->getView('MainView')->forum_message('Topic unlocked. Redirecting...', 'index.php?mode=viewtopic&amp;id='.$_GET['id']);
  176. }
  177. $lockv = true;
  178. break;
  179. case 'movetopic':
  180. if ($this->getModel('ForumsModel')->getForum($_POST['forum_id']) == null)
  181. $this->getView('MainView')->forum_message('Forum does not exist!', 'index.php?mode=viewtopic&amp;id='.$_GET['id']);
  182. else
  183. {
  184. $this->getModel('PostsModel')->moveTopic($_GET['id'], $_POST['forum_id']);
  185. $this->getView('MainView')->forum_message('Topic moved. Redirecting...', 'index.php?mode=viewtopic&amp;id='.$_GET['id']);
  186. }
  187. $lockv = true;
  188. break;
  189. }
  190. }
  191. else
  192. {
  193. switch ($_GET['submode'])
  194. {
  195. case 'deletetopic':
  196. case 'locktopic':
  197. case 'movetopic':
  198. $this->forward('index.php?mode=viewtopic&id='.$_GET['id']);
  199. break;
  200. case 'deletepost':
  201. $this->forward('index.php?mode=viewtopic&id='.$p['topic_id']);
  202. }
  203. }
  204. }
  205. if (!isset($lockv))
  206. switch($_GET['submode'])
  207. {
  208. case 'deletepost':
  209. $this->getView('MainView')->confirm_action('Do you really want delete post <span style="font-weight: bold">#'.$_GET['id'].'</span>?');
  210. break;
  211. case 'deletetopic':
  212. $this->getView('MainView')->confirm_action('Do you really want delete topic <span style="font-weight: bold">#'.$_GET['id'].'</span> with all posts? This operation cannot undone.');
  213. break;
  214. case 'locktopic':
  215. if ($t['topic_locked'] == false)
  216. $this->getView('MainView')->confirm_action('Do you want lock topic <span style="font-weight: bold">#'.$_GET['id'].'</span>?');
  217. else
  218. $this->getView('MainView')->confirm_action('Do you want unlock topic <span style="font-weight: bold">#'.$_GET['id'].'</span>?');
  219. break;
  220. case 'movetopic':
  221. $this->getView('MainView')->putExistingModel('PostsModel', $this->getModel('PostsModel'));
  222. $this->getView('MainView')->move_topic();
  223. break;
  224. }
  225. }
  226. public function posting($type)
  227. {
  228. $this->loadDependencies();
  229. $msg = '';
  230. $this->loadModel('PostsModel');
  231. $this->loadModel('ForumsModel');
  232. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  233. if (!$this->getModel('SessionModel')->isLogged())
  234. {
  235. $this->getView('MainView')->forum_message('You are not logged.', 'index.php?mode=login');
  236. $lockv = true;
  237. }
  238. //SPRAWDZANIE CZY TEMAT/FORUM ISTNIEJE I CZY NIE ZABLOKOWANE
  239. if (!isset($lockv))
  240. switch($type)
  241. {
  242. case POSTING_NEWTOPIC: //sprawdzenie czy forum istnieje i czy nie zablokowane
  243. $f = $this->getModel('ForumsModel')->getForum($_GET['id']);
  244. if ($f == null)
  245. {
  246. $this->getView('MainView')->forum_message('Forum does not exist!', 'index.php');
  247. $lockv = true;
  248. }
  249. else
  250. if ($f['locked'] == true)
  251. {
  252. $this->getView('MainView')->forum_message('Forum is locked', 'index.php?mode=viewforum&amp;id='.$_GET['id']);
  253. $lockv = true;
  254. }
  255. break;
  256. case POSTING_REPLY: //sprawdzenie czy temat istnieje
  257. case POSTING_QUOTE:
  258. $t = $this->getModel('PostsModel')->getTopic($_GET['id']);
  259. if ($t == null)
  260. {
  261. $this->getView('MainView')->forum_message('Topic does not exist!', 'index.php');
  262. $lockv = true;
  263. }
  264. else
  265. {
  266. if ($t['forum_locked'] == true && $this->getModel('SessionModel')->getRank() < RANK_MOD)
  267. {
  268. $this->getView('MainView')->forum_message('Forum is locked', 'index.php?mode=viewtopic&amp;id='.$t['topic_id']);
  269. $lockv = true;
  270. }
  271. if ($t['topic_locked'] == true && $this->getModel('SessionModel')->getRank() < RANK_MOD)
  272. {
  273. $this->getView('MainView')->forum_message('Topic is locked', 'index.php?mode=viewtopic&amp;id='.$t['topic_id']);
  274. $lockv = true;
  275. }
  276. if ($type == POSTING_QUOTE)
  277. {
  278. $_GET['q'] = (isset($_GET['q'])) ? trim(strip_tags($this->db->real_escape_string($_GET['q']))) : 0;
  279. $qp = $this->getModel('PostsModel')->getPost($_GET['q']);
  280. if ($qp == null)
  281. {
  282. $this->getView('MainView')->forum_message('Invalid quoted post', 'index.php?mode=viewtopic&amp;id='.$t['topic_id']);
  283. $lockv = true;
  284. }
  285. else
  286. {
  287. if ($qp['topic_id'] != $_GET['id'])
  288. {
  289. $this->getView('MainView')->forum_message('Invalid quoted post', 'index.php?mode=viewtopic&amp;id='.$t['topic_id']);
  290. $lockv = true;
  291. }
  292. }
  293. }
  294. }
  295. break;
  296. case POSTING_EDIT:
  297. $p = $this->getModel('PostsModel')->getPost($_GET['id']);
  298. if ($p == null)
  299. {
  300. $this->getView('MainView')->forum_message('Post does not exist!', 'index.php');
  301. $lockv = true;
  302. }
  303. else
  304. {
  305. $t = $this->getModel('PostsModel')->getTopic($p['topic_id']);
  306. if ($t['forum_locked'] == true && $this->getModel('SessionModel')->getRank() < RANK_MOD)
  307. {
  308. $this->getView('MainView')->forum_message('Forum is locked', 'index.php?mode=viewtopic&amp;id='.$t['topic_id']);
  309. $lockv = true;
  310. }
  311. if ($t['topic_locked'] == true && $this->getModel('SessionModel')->getRank() < RANK_MOD)
  312. {
  313. $this->getView('MainView')->forum_message('Topic is locked', 'index.php?mode=viewtopic&amp;id='.$t['topic_id']);
  314. $lockv = true;
  315. }
  316. //sprawdzić czy edycja tematu
  317. // i ustawić opdowiednie parametry $type = POSTING_EDITTOPIC
  318. $first = $this->getModel('PostsModel')->getFirstPost($t['topic_id']);
  319. if ($first['post_id'] == $_GET['id'])
  320. $type = POSTING_EDITTOPIC;
  321. if ($p['user_id'] != $this->getModel('SessionModel')->getID() && $this->getModel('SessionModel')->getRank() < RANK_MOD)
  322. {
  323. $this->getView('MainView')->forum_message('You can edit only own posts', 'index.php?mode=viewtopic&amp;id='.$t['topic_id']);
  324. $lockv = true;
  325. }
  326. }
  327. break;
  328. }
  329. //przesłanie formularza --------------------------------------------------------------------------------
  330. if (isset($_POST['post']) && !isset($_POST['preview']) && !isset($lockv))
  331. {
  332. $_POST['post'] = trim(htmlspecialchars($this->db->real_escape_string($_POST['post'])));
  333. if ($type == POSTING_NEWTOPIC || $type == POSTING_EDITTOPIC) //walidacja tytułu tematu (add, edit)
  334. {
  335. $_POST['topic'] = trim(strip_tags($this->db->real_escape_string($_POST['topic'])));
  336. if (strlen($_POST['topic']) < 3)
  337. $msg .= 'Topic title is too short (min 3 characters)<br>';
  338. }
  339. if (strlen($_POST['post']) < 3)
  340. $msg .= 'Post content is too short (min 3 characters)<br>';
  341. if ($msg == null)
  342. {
  343. switch ($type)
  344. {
  345. case POSTING_NEWTOPIC: //akcje dodania nowego tematu
  346. $topic_id = $this->getModel('PostsModel')->addTopic($_POST['topic'], $_POST['post'], $_GET['id'], $this->getModel('SessionModel')->getID());
  347. if ($topic_id != null)
  348. {
  349. $this->getView('MainView')->forum_message('Topic created, Redirecting...', 'index.php?mode=viewtopic&amp;id='.$topic_id);
  350. $lockv = true;
  351. }
  352. else
  353. $msg .= 'Something went wrong, try again.';
  354. break;
  355. case POSTING_EDITTOPIC:
  356. case POSTING_EDIT:
  357. $this->getModel('PostsModel')->changePost($_GET['id'], $_POST['post']);
  358. if ($type == POSTING_EDITTOPIC)
  359. $this->getModel('PostsModel')->changeTopic($t['topic_id'], $_POST['topic']);
  360. $this->getView('MainView')->forum_message('Post edited. Redirecting to topic...', 'index.php?mode=viewtopic&amp;id='.$t['topic_id']);
  361. $lockv = true;
  362. break;
  363. case POSTING_QUOTE:
  364. case POSTING_REPLY:
  365. $this->getModel('PostsModel')->addPost($_GET['id'], $this->getModel('SessionModel')->getID(), $_POST['post']);
  366. $this->getView('MainView')->forum_message('Reply saved. Redirecting to topic...', 'index.php?mode=viewtopic&amp;id='.$_GET['id']);
  367. $lockv = true;
  368. break;
  369. }
  370. }
  371. }
  372. if (!isset($lockv))
  373. {
  374. switch ($type)
  375. {
  376. case POSTING_NEWTOPIC:
  377. case POSTING_REPLY:
  378. $_POST['post'] = (isset($_POST['post'])) ? $_POST['post'] : '';
  379. break;
  380. case POSTING_EDITTOPIC:
  381. $_POST['post'] = (isset($_POST['post'])) ? $_POST['post'] : $p['content'];
  382. $_POST['topic'] = (isset($_POST['topic'])) ? $_POST['topic'] : $t['topic_title'];
  383. break;
  384. case POSTING_EDIT:
  385. $_POST['post'] = (isset($_POST['post'])) ? $_POST['post'] : $p['content'];
  386. break;
  387. case POSTING_QUOTE:
  388. $quote = ($qp['nick'] != null) ? '='.$qp['nick'] : '';
  389. $_POST['post'] = (isset($_POST['post'])) ? $_POST['post'] : '[quote'.$quote.']'.$qp['content'].'[/quote]';
  390. break;
  391. }
  392. if ($type == POSTING_NEWTOPIC)
  393. $_POST['topic'] = (isset($_POST['topic'])) ? $_POST['topic'] : ''; //tylko edycja/tworzenie tematu
  394. $this->getView('MainView')->putExistingModel('PostsModel', $this->getModel('PostsModel'));
  395. $this->getView('MainView')->putExistingModel('ForumsModel', $this->getModel('ForumsModel'));
  396. $this->getView('MainView')->posting_form($type, $msg);
  397. }
  398. }
  399. public function myprofile()
  400. {
  401. $this->loadDependencies();
  402. if (!$this->getModel('SessionModel')->isLogged())
  403. $this->forward('index.php');
  404. else
  405. $this->forward('index.php?mode=viewprofile&id='.$this->getModel('SessionModel')->getID());
  406. }
  407. public function viewprofile()
  408. {
  409. $this->loadDependencies();
  410. $this->loadModel('UsersModel');
  411. $this->getView('MainView')->putExistingModel('UsersModel', $this->getModel('UsersModel'));
  412. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  413. if ($this->getModel('UsersModel')->getUserInformation($_GET['id']) == null)
  414. $this->getView('MainView')->forum_message('User does not exist!', 'index.php');
  415. else
  416. {
  417. //$this->getView('MainView')->putExistingModel('PostsModel', $this->getModel('PostsModel'));
  418. $this->getView('MainView')->viewprofile();
  419. }
  420. }
  421. public function editprofile()
  422. {
  423. $this->loadDependencies();
  424. $this->loadModel('UsersModel');
  425. $user_info = $this->getModel('UsersModel')->getUserInformation($this->getModel('SessionModel')->getID(), true);
  426. if (!$this->getModel('SessionModel')->isLogged())
  427. {
  428. $this->getView('MainView')->forum_message('You are not logged.', 'index.php?mode=login');
  429. }
  430. else
  431. {
  432. $msg = '';
  433. if (isset($_POST['nick'], $_POST['passwd'], $_POST['passwd_confirm'], $_POST['email']))
  434. {
  435. //secure pools
  436. $_POST['nick'] = trim(strip_tags($this->db->real_escape_string($_POST['nick'])));
  437. $_POST['passwd_old'] = trim(strip_tags($_POST['passwd_old']));
  438. $_POST['passwd'] = trim(strip_tags($_POST['passwd']));
  439. $_POST['passwd_confirm'] = trim(strip_tags($_POST['passwd_confirm']));
  440. $_POST['email'] = trim(strip_tags($this->db->real_escape_string($_POST['email'])));
  441. $_POST['location'] = trim(strip_tags($this->db->real_escape_string($_POST['location'])));
  442. $_POST['signature'] = trim(strip_tags($this->db->real_escape_string($_POST['signature'])));
  443. if ($_POST['email'] != $user_info['email'] || $_POST['passwd'] != '')
  444. {
  445. if (sha1($_POST['passwd_old']) != $user_info['password'])
  446. $msg .= 'Old password is incorrect!<br>';
  447. }
  448. if ($_POST['passwd'] != '')
  449. {
  450. if (strlen($_POST['passwd']) < 8)
  451. $msg .= 'Password is too short (min 8 characters)<br>';
  452. if ($_POST['passwd'] != $_POST['passwd_confirm'])
  453. $msg .= 'Password do not match!<br>';
  454. }
  455. //check if avatar is uploaded
  456. if ($_FILES['avatar']['tmp_name'] != null)
  457. {
  458. global $allowed_avatars;
  459. $image_size = @getimagesize($_FILES['avatar']['tmp_name']);
  460. if ($image_size == null)
  461. $msg .= 'Type of uploaded file are not allowed.<br>';
  462. else
  463. if (!in_array($image_size['mime'], $allowed_avatars))
  464. $msg .= 'Type of uploaded avatar is not supported.<br>';
  465. else
  466. if ($image_size[0] > 120 || $image_size[1] > 150)
  467. $msg .= 'Uploaded avatar is too big (maximum 120x150 px).<br>';
  468. }
  469. if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
  470. $msg .= 'Email is incorrect<br>';
  471. if ($msg == '')
  472. {
  473. if ($_FILES['avatar']['tmp_name'] != null && !isset($_POST['delete_avatar'])) //change an avatar
  474. {
  475. if ($user_info['avatar'] != '')
  476. unlink('./'.$user_info['avatar']);
  477. $ext = pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION);
  478. $av = 'images/avatars/'.$this->getModel('SessionModel')->getID().'.'.$ext;
  479. move_uploaded_file($_FILES['avatar']['tmp_name'], './'.$av);
  480. }
  481. else
  482. if (isset($_POST['delete_avatar']))
  483. {
  484. unlink('./'.$user_info['avatar']);
  485. $av = '';
  486. }
  487. else
  488. $av = $user_info['avatar']; //if new avatar is not set
  489. if ($_POST['passwd'] != '')
  490. $this->getModel('UsersModel')->changeUserPassword($this->getModel('SessionModel')->getID(), sha1($_POST['passwd']));
  491. $this->getModel('UsersModel')->updateUserProfile($this->getModel('SessionModel')->getID(), '', $_POST['email'], $_POST['location'], $_POST['signature'], $av);
  492. $this->getView('MainView')->forum_message('Your profile has changed.', 'index.php?mode=viewprofile&amp;id='.$this->getModel('SessionModel')->getID());
  493. $lockv = true;
  494. }
  495. }
  496. $_POST['nick'] = (isset($_POST['nick'])) ? $_POST['nick'] : $user_info['nick'];
  497. $_POST['email'] = (isset($_POST['email'])) ? $_POST['email'] : $user_info['email'];
  498. $_POST['location'] = (isset($_POST['location'])) ? $_POST['location'] : $user_info['location'];
  499. $_POST['signature'] = (isset($_POST['signature'])) ? $_POST['signature'] : $user_info['signature'];
  500. $this->getView('MainView')->putExistingModel('UsersModel', $this->getModel('UsersModel'));
  501. if (!isset($lockv))
  502. $this->getView('MainView')->edprofile_form($msg);
  503. }
  504. }
  505. public function logout()
  506. {
  507. $this->loadDependencies();
  508. if (!$this->getModel('SessionModel')->isLogged())
  509. $this->forward('index.php');
  510. $this->getModel('SessionModel')->deleteSession();
  511. $this->getView('MainView')->forum_message('You are logged out.', 'index.php');
  512. }
  513. public function login()
  514. {
  515. $this->loadDependencies();
  516. $this->loadModel('BansModel');
  517. if ($_SERVER['REQUEST_SCHEME'] == 'http')
  518. $this->forward('https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
  519. if ($this->getModel('SessionModel')->isLogged())
  520. $this->forward('index.php');
  521. $msg = '';
  522. if (isset($_POST['nick'], $_POST['passwd']))
  523. {
  524. //secure pools
  525. $_POST['nick'] = trim(strip_tags($this->db->real_escape_string($_POST['nick'])));
  526. $_POST['passwd'] = sha1(trim(strip_tags($this->db->real_escape_string($_POST['passwd']))));
  527. $userinfo = $this->getModel('SessionModel')->tryGetUser($_POST['nick'], $_POST['passwd']);
  528. if (count($userinfo) == 0)
  529. $msg = 'Invalid username or password.';
  530. if ($msg == '')
  531. {
  532. $ban_info = $this->getModel('BansModel')->getUserBan($userinfo['user_id']);
  533. if ($ban_info == null)
  534. {
  535. $this->getModel('SessionModel')->registerNewSession($userinfo['user_id']);
  536. $this->getView('MainView')->forum_message('You are logged as: <span style="font-weight: bold">'.$userinfo['nick'].'</span>', 'index.php');
  537. }
  538. else
  539. {
  540. $reason = ($ban_info['reason'] != '') ? '<br>Reason: <span style="font-style: italic">'.$ban_info['reason'].'</span>' : '';
  541. $this->getView('MainView')->forum_message('You are banned!'.$reason);
  542. }
  543. $lockv = true;
  544. }
  545. }
  546. $_POST['nick'] = (isset($_POST['nick'])) ? $_POST['nick'] : '';
  547. if (!isset($lockv))
  548. $this->getView('MainView')->login_form($msg);
  549. }
  550. public function register()
  551. {
  552. $this->loadDependencies();
  553. $this->loadModel('UsersModel');
  554. if ($this->getModel('SessionModel')->isLogged())
  555. $this->forward('index.php');
  556. if ($_SERVER['REQUEST_SCHEME'] == 'http')
  557. $this->forward('https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
  558. $msg = '';
  559. if (isset($_POST['nick'], $_POST['passwd'], $_POST['passwd_confirm'], $_POST['email']))
  560. {
  561. //secure pools
  562. $_POST['nick'] = trim(strip_tags($this->db->real_escape_string($_POST['nick'])));
  563. $_POST['passwd'] = trim(strip_tags($_POST['passwd']));
  564. $_POST['passwd_confirm'] = trim(strip_tags($_POST['passwd_confirm']));
  565. $_POST['email'] = trim(strip_tags($this->db->real_escape_string($_POST['email'])));
  566. if (strlen($_POST['nick']) < 3)
  567. $msg .= 'Nick is too short (min 3 characters)<br>';
  568. if (strlen($_POST['passwd']) < 8)
  569. $msg .= 'Password is too short (min 8 characters)<br>';
  570. if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
  571. $msg .= 'Email is incorrect<br>';
  572. if ($this->getModel('UsersModel')->nickExists($_POST['nick']) == true)
  573. $msg .= 'Nick is in use. Type another one.<br>';
  574. if ($_POST['passwd'] != $_POST['passwd_confirm'])
  575. $msg .= 'Password do not match';
  576. if ($msg == '')
  577. {
  578. $this->getModel('UsersModel')->createNewUser($_POST['nick'], sha1($_POST['passwd']), $_POST['email']);
  579. $this->getView('MainView')->forum_message('Your account has created. Log in to write new posts.', 'index.php');
  580. $lockv = true;
  581. }
  582. }
  583. $_POST['nick'] = (isset($_POST['nick'])) ? $_POST['nick'] : '';
  584. $_POST['email'] = (isset($_POST['email'])) ? $_POST['email'] : '';
  585. if (!isset($lockv))
  586. $this->getView('MainView')->register_form($msg);
  587. }
  588. public function checknick()
  589. {
  590. $this->loadModel('UsersModel');
  591. if (!isset($_GET['nick']))
  592. $_GET['nick'] = '';
  593. $_GET['nick'] = trim($this->db->real_escape_string(strip_tags($_GET['nick'])));
  594. if ($this->getModel('UsersModel')->nickExists($_GET['nick']) == true)
  595. echo 'true';
  596. else
  597. echo 'false';
  598. }
  599. }
  600. ?>