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.

730 lines
23 KiB

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