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.

744 lines
23 KiB

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