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.

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