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.

611 lines
19 KiB

  1. <?php
  2. /**
  3. * @package uForum2
  4. * @file inc/controllers/AdminController.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 AdminController 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. if ($_SERVER['REQUEST_SCHEME'] == 'http')
  24. $this->forward(buildURL($_SERVER['REQUEST_URI']));
  25. if (!$this->getModel('SessionModel')->isLogged())
  26. {
  27. $this->getView('MainView')->forum_message('You are not logged.', 'index.php?mode=login', true);
  28. $lockv = true;
  29. }
  30. if ($this->getModel('SessionModel')->getRank() == RANK_USER && !isset($lockv))
  31. {
  32. $this->getView('MainView')->forum_message('You are not admin', 'index.php');
  33. $lockv = true;
  34. }
  35. if (!isset($lockv))
  36. return true;
  37. else
  38. return false;
  39. }
  40. public function main()
  41. {
  42. if ($this->loadDependencies())
  43. {
  44. $this->getView('MainView')->admin_main();
  45. }
  46. }
  47. public function eduser()
  48. {
  49. if ($this->loadDependencies())
  50. {
  51. $this->loadModel('UsersModel');
  52. $user_info = $this->getModel('UsersModel')->getUserInformation($_GET['id'], true);
  53. if ($user_info == null)
  54. {
  55. $this->getView('MainView')->forum_message('User does not exist!', 'index.php?mode=admin&amp;submode=users');
  56. $lockv = true;
  57. }
  58. else
  59. {
  60. $msg = '';
  61. if (isset($_POST['nick'], $_POST['passwd'], $_POST['passwd_confirm'], $_POST['email']))
  62. {
  63. //secure pools
  64. $_POST['nick'] = trim(strip_tags($this->db->real_escape_string($_POST['nick'])));
  65. $_POST['passwd'] = trim($_POST['passwd']);
  66. $_POST['passwd_confirm'] = trim($_POST['passwd_confirm']);
  67. $_POST['email'] = trim(strip_tags($this->db->real_escape_string($_POST['email'])));
  68. $_POST['location'] = trim(htmlspecialchars($this->db->real_escape_string($_POST['location'])));
  69. $_POST['signature'] = trim(htmlspecialchars($this->db->real_escape_string($_POST['signature'])));
  70. $_POST['user_rank'] = trim(strip_tags($this->db->real_escape_string($_POST['user_rank'])));
  71. if ($_POST['passwd'] != '')
  72. {
  73. if (strlen($_POST['passwd']) < 8)
  74. $msg .= 'Password is too short (min 8 characters)<br>';
  75. if ($_POST['passwd'] != $_POST['passwd_confirm'])
  76. $msg .= 'Password do not match!<br>';
  77. }
  78. if ($_GET['id'] == $this->getModel('SessionModel')->getID() && $_POST['user_rank'] != RANK_ADMIN)
  79. {
  80. $msg .= 'You cannot set rank for your profile<br>';
  81. $_POST['user_rank'] = RANK_ADMIN;
  82. }
  83. if ($this->getModel('UsersModel')->nickExists($_POST['nick']) == true && $_POST['nick'] != $user_info['nick'])
  84. $msg .= 'Nick is in use. Type another one.<br>';
  85. if (strlen($_POST['nick']) < 3)
  86. $msg .= 'Nick is too short (min 3 characters)<br>';
  87. if ($_POST['user_rank'] > RANK_ADMIN || $_POST['user_rank'] < RANK_USER)
  88. $msg .= 'Rank is not valid!<br>';
  89. //check if avatar is uploaded
  90. if ($_FILES['avatar']['tmp_name'] != null)
  91. {
  92. global $allowed_avatars;
  93. $image_size = @getimagesize($_FILES['avatar']['tmp_name']);
  94. if ($image_size == null)
  95. $msg .= 'Type of uploaded file are not allowed.<br>';
  96. else
  97. if (!in_array($image_size['mime'], $allowed_avatars))
  98. $msg .= 'Type of uploaded avatar is not supported.<br>';
  99. else
  100. if ($image_size[0] > 120 || $image_size[1] > 150)
  101. $msg .= 'Uploaded avatar is too big (maximum 120x150 px).<br>';
  102. }
  103. if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
  104. $msg .= 'Email is incorrect<br>';
  105. if ($msg == '')
  106. {
  107. if ($_FILES['avatar']['tmp_name'] != null && !isset($_POST['delete_avatar'])) //change an avatar
  108. {
  109. if ($user_info['avatar'] != '')
  110. unlink('./'.$user_info['avatar']);
  111. $ext = pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION);
  112. $av = 'images/avatars/'.$this->getModel('SessionModel')->getID().'.'.$ext;
  113. move_uploaded_file($_FILES['avatar']['tmp_name'], './'.$av);
  114. }
  115. else
  116. if (isset($_POST['delete_avatar']))
  117. {
  118. unlink('./'.$user_info['avatar']);
  119. $av = '';
  120. }
  121. else
  122. $av = $user_info['avatar']; //if new avatar is not set
  123. if ($_POST['passwd'] != '')
  124. $this->getModel('UsersModel')->changeUserPassword($_GET['id'], $user_info['nick'], $_POST['passwd']);
  125. $this->getModel('UsersModel')->changeUserRank($_GET['id'], $_POST['user_rank']);
  126. $this->getModel('UsersModel')->updateUserProfile($_GET['id'], $_POST['nick'], $_POST['email'], $_POST['location'], $_POST['signature'], $av);
  127. $this->getView('MainView')->forum_message('User profile has changed.', 'index.php?mode=admin&amp;submode=users');
  128. $lockv = true;
  129. }
  130. }
  131. $_POST['nick'] = (isset($_POST['nick'])) ? stripslashes($_POST['nick']) : $user_info['nick'];
  132. $_POST['email'] = (isset($_POST['email'])) ? stripslashes($_POST['email']) : $user_info['email'];
  133. $_POST['location'] = (isset($_POST['location'])) ? stripslashes($_POST['location']) : $user_info['location'];
  134. $_POST['signature'] = (isset($_POST['signature'])) ? stripslashes($_POST['signature']) : $user_info['signature'];
  135. $_POST['user_rank'] = (isset($_POST['user_rank'])) ? $_POST['user_rank'] : $user_info['rank'];
  136. $this->getView('MainView')->putExistingModel('UsersModel', $this->getModel('UsersModel'));
  137. if (!isset($lockv))
  138. $this->getView('MainView')->edprofile_form($msg, true);
  139. }
  140. }
  141. }
  142. public function users()
  143. {
  144. if ($this->loadDependencies())
  145. {
  146. if (isset($_GET['rank']))
  147. {
  148. switch ($_GET['rank'])
  149. {
  150. case 'admin':
  151. $_GET['rank'] = RANK_ADMIN;
  152. break;
  153. case 'mod':
  154. $_GET['rank'] = RANK_MOD;
  155. break;
  156. case 'user':
  157. $_GET['rank'] = RANK_USER;
  158. break;
  159. default:
  160. $_GET['rank'] = '';
  161. break;
  162. }
  163. }
  164. else
  165. $_GET['rank'] = '';
  166. $_POST['sort_type'] = (isset($_POST['sort_type'])) ? $this->db->real_escape_string($_POST['sort_type']) : 'regdate';
  167. $allowed_sorting = array('regdate', 'lastvisit', 'nick', 'post_count');
  168. if (!in_array($_POST['sort_type'], $allowed_sorting))
  169. $_POST['sort_type'] = '';
  170. $_POST['sort_desc'] = (isset($_POST['sort_desc'])) ? 'DESC' : 'ASC';
  171. $this->getView('MainView')->admin_userlist();
  172. }
  173. }
  174. public function deluser()
  175. {
  176. if ($this->loadDependencies())
  177. {
  178. $this->loadModel('UsersModel');
  179. $this->getView('MainView')->putExistingModel('UsersModel', $this->getModel('UsersModel'));
  180. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  181. $user_info = $this->getModel('UsersModel')->getUserInformation($_GET['id']);
  182. if ($user_info == null)
  183. {
  184. $this->getView('MainView')->forum_message('User does not exist!', 'index.php?mode=admin&amp;submode=users');
  185. $lockv = true;
  186. }
  187. else
  188. {
  189. if ($_GET['id'] == $this->getModel('SessionModel')->getID())
  190. {
  191. $this->getView('MainView')->forum_message('You cannot delete own profile!', 'index.php?mode=admin&amp;submode=users');
  192. $lockv = true;
  193. }
  194. }
  195. if (isset($_POST['confirmed']) && !isset($lockv))
  196. {
  197. if (!isset($_POST['rejected']))
  198. {
  199. $this->getModel('UsersModel')->deleteUser($_GET['id']);
  200. if ($user_info['avatar'] != null) //delete user's avatar
  201. unlink('./'.$user_info['avatar']);
  202. $this->getView('MainView')->forum_message('Profile deleted. Redirecting to users list...', 'index.php?mode=admin&amp;submode=users');
  203. $lockv = true;
  204. }
  205. else
  206. {
  207. $this->forward('index.php?mode=admin&submode=users');
  208. }
  209. }
  210. if (!isset($lockv))
  211. $this->getView('MainView')->confirm_action('Do you want delete user <span style="font-weight: bold">'.$user_info['nick'].'</span>? This operation cannot undone.');
  212. }
  213. }
  214. public function config()
  215. {
  216. if ($this->loadDependencies())
  217. {
  218. $msg = '';
  219. if (isset($_POST['forum_name'], $_POST['forum_desc']))
  220. {
  221. $_POST['forum_name'] = trim(htmlspecialchars($this->db->real_escape_string($_POST['forum_name'])));
  222. $_POST['forum_desc'] = trim(htmlspecialchars($this->db->real_escape_string($_POST['forum_desc'])));
  223. if (strlen($_POST['forum_name']) < 3)
  224. {
  225. $msg .= 'Forum name is too short (min 3 characters)!<br>';
  226. }
  227. if (strlen($_POST['forum_name']) > 30)
  228. {
  229. $msg .= 'Forum name is too long (max 30 characters)!<br>';
  230. }
  231. if (strlen($_POST['forum_desc']) > 50)
  232. {
  233. $msg .= 'Forum description is too long (max 50 characters)!<br>';
  234. }
  235. if ($msg == '')
  236. {
  237. if ($_POST['forum_name'] != $this->getModel('ConfigModel')->getConf('forum_name'))
  238. $this->getModel('ConfigModel')->updateConf('forum_name', $_POST['forum_name']);
  239. if ($_POST['forum_desc'] != $this->getModel('ConfigModel')->getConf('forum_desc'))
  240. $this->getModel('ConfigModel')->updateConf('forum_desc', $_POST['forum_desc']);
  241. $this->getView('MainView')->forum_message('Forum configuration updated. Redirecting...', 'index.php?mode=admin&amp;submode=config');
  242. $lockv = true;
  243. }
  244. }
  245. $_POST['forum_name'] = (isset($_POST['forum_name'])) ? stripslashes($_POST['forum_name']) : $this->getModel('ConfigModel')->getConf('forum_name');
  246. $_POST['forum_desc'] = (isset($_POST['forum_desc'])) ? stripslashes($_POST['forum_desc']) : $this->getModel('ConfigModel')->getConf('forum_desc');
  247. if (!isset($lockv))
  248. {
  249. $this->getView('MainView')->admin_config($msg);
  250. }
  251. }
  252. }
  253. public function forums()
  254. {
  255. if ($this->loadDependencies())
  256. {
  257. $this->getView('MainView')->admin_forums();
  258. }
  259. }
  260. public function addcat()
  261. {
  262. if ($this->loadDependencies())
  263. {
  264. $this->modify_cat('add');
  265. }
  266. }
  267. public function edcat()
  268. {
  269. if ($this->loadDependencies())
  270. {
  271. $this->modify_cat('edit');
  272. }
  273. }
  274. public function addforum()
  275. {
  276. if ($this->loadDependencies())
  277. {
  278. $this->modify_forum('add');
  279. }
  280. }
  281. public function edforum()
  282. {
  283. if ($this->loadDependencies())
  284. {
  285. $this->modify_forum('edit');
  286. }
  287. }
  288. public function delforum()
  289. {
  290. if ($this->loadDependencies())
  291. {
  292. $this->loadModel('ForumsModel');
  293. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  294. $forum_info = $this->getModel('ForumsModel')->getForum($_GET['id']);
  295. if ($forum_info == null)
  296. {
  297. $this->getView('MainView')->forum_message('Forum does not exist!', 'index.php?mode=admin&amp;submode=forums');
  298. $lockv = true;
  299. }
  300. if (isset($_POST['confirmed']) && !isset($lockv))
  301. {
  302. if (!isset($_POST['rejected']))
  303. {
  304. $this->getModel('ForumsModel')->deleteForum($_GET['id']);
  305. $this->getView('MainView')->forum_message('Forum deleted. Redirecting...', 'index.php?mode=admin&amp;submode=forums');
  306. $lockv = true;
  307. }
  308. else
  309. $this->forward('index.php?mode=admin&submode=forums');
  310. }
  311. if (!isset($lockv))
  312. $this->getView('MainView')->confirm_action('Do you REALLY want delete forum <span style="font-weight: bold">'.$forum_info['name'].'</span> with ALL CONTENT? <span style="text-decoration: underline">This operation cannot undone!</span>');
  313. }
  314. }
  315. public function delcat()
  316. {
  317. if ($this->loadDependencies())
  318. {
  319. $this->loadModel('ForumsModel');
  320. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  321. $cat_info = $this->getModel('ForumsModel')->getCat($_GET['id']);
  322. if ($cat_info == null)
  323. {
  324. $this->getView('MainView')->forum_message('Category does not exist!', 'index.php?mode=admin&amp;submode=forums');
  325. $lockv = true;
  326. }
  327. if (isset($_POST['confirmed']) && !isset($lockv))
  328. {
  329. if (!isset($_POST['rejected']))
  330. {
  331. $this->getModel('ForumsModel')->deleteCat($_GET['id']);
  332. $this->getView('MainView')->forum_message('Category deleted. Redirecting...', 'index.php?mode=admin&amp;submode=forums');
  333. $lockv = true;
  334. }
  335. else
  336. $this->forward('index.php?mode=admin&submode=forums');
  337. }
  338. if (!isset($lockv))
  339. $this->getView('MainView')->confirm_action('Do you REALLY want delete category <span style="font-weight: bold">'.$cat_info['name'].'</span> with ALL FORUMS AND CONTENT? <span style="text-decoration: underline">This operation cannot undone!</span>');
  340. }
  341. }
  342. private function modify_cat($m)
  343. {
  344. $msg = '';
  345. $this->loadModel('ForumsModel');
  346. if ($m == 'edit')
  347. {
  348. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  349. $cat_info = $this->getModel('ForumsModel')->getCat($_GET['id']);
  350. if ($cat_info == null)
  351. {
  352. $this->getView('MainView')->forum_message('Category does not exist!', 'index.php?mode=admin&amp;submode=forums');
  353. $lockv = true;
  354. }
  355. }
  356. if (isset($_POST['name']) && !isset($lockv))
  357. {
  358. $_POST['name'] = trim(htmlspecialchars($this->db->real_escape_string($_POST['name'])));
  359. if (strlen($_POST['name']) < 3)
  360. $msg .= 'Category name is too short (min 3 characters)!<br>';
  361. if ($msg == '')
  362. {
  363. if ($m == 'add')
  364. {
  365. $this->getModel('ForumsModel')->addCat($_POST['name']);
  366. $this->getView('MainView')->forum_message('Category added. Redirecting...', 'index.php?mode=admin&amp;submode=forums');
  367. $lockv = true;
  368. }
  369. else
  370. {
  371. $this->getModel('ForumsModel')->changeCat($_GET['id'], $_POST['name']);
  372. $this->getView('MainView')->forum_message('Category updated. Redirecting...', 'index.php?mode=admin&amp;submode=forums');
  373. $lockv = true;
  374. }
  375. }
  376. }
  377. if (!isset($lockv))
  378. {
  379. if ($m == 'add')
  380. $_POST['name'] = (isset($_POST['name'])) ? stripslashes($_POST['name']) : '';
  381. else
  382. $_POST['name'] = (isset($_POST['name'])) ? stripslashes($_POST['name']) : $cat_info['name'];
  383. $this->getView('MainView')->putExistingModel('ForumsModel', $this->getModel('ForumsModel'));
  384. $this->getView('MainView')->admin_cat_form($msg, $m);
  385. }
  386. }
  387. private function modify_forum($m)
  388. {
  389. $msg = '';
  390. $this->loadModel('ForumsModel');
  391. if ($m == 'edit')
  392. {
  393. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  394. $forum_info = $this->getModel('ForumsModel')->getForum($_GET['id']);
  395. if ($forum_info == null)
  396. {
  397. $this->getView('MainView')->forum_message('Forum does not exist!', 'index.php?mode=admin&amp;submode=forums');
  398. $lockv = true;
  399. }
  400. }
  401. if (isset($_POST['name']) && !isset($lockv))
  402. {
  403. $_POST['name'] = trim(htmlspecialchars($this->db->real_escape_string($_POST['name'])));
  404. $_POST['desc'] = trim(htmlspecialchars($this->db->real_escape_string($_POST['desc'])));
  405. $_POST['category_id'] = trim(strip_tags($this->db->real_escape_string($_POST['category_id'])));
  406. $_POST['locked'] = trim(strip_tags($this->db->real_escape_string($_POST['locked'])));
  407. $_POST['locked'] = ($_POST['locked'] == true) ? true : false;
  408. if (strlen($_POST['name']) < 3)
  409. $msg .= 'Forum name is too short (min 3 characters)!<br>';
  410. $c = $this->getModel('ForumsModel')->getCat($_POST['category_id']);
  411. if ($c == null)
  412. $msg .= 'Category does not exist!<br>';
  413. if ($msg == '')
  414. {
  415. if ($m == 'add')
  416. {
  417. $this->getModel('ForumsModel')->addForum($_POST['name'], $_POST['desc'], $_POST['category_id'], $_POST['locked']);
  418. $this->getView('MainView')->forum_message('Forum added. Redirecting...', 'index.php?mode=admin&amp;submode=forums');
  419. $lockv = true;
  420. }
  421. else
  422. {
  423. $this->getModel('ForumsModel')->changeForum($_GET['id'], $_POST['name'], $_POST['desc'], $_POST['category_id'], $_POST['locked']);
  424. $this->getView('MainView')->forum_message('Forum updated. Redirecting...', 'index.php?mode=admin&amp;submode=forums');
  425. $lockv = true;
  426. }
  427. }
  428. }
  429. if (!isset($lockv))
  430. {
  431. if ($m == 'add')
  432. {
  433. $_POST['name'] = (isset($_POST['name'])) ? stripslashes($_POST['name']) : '';
  434. $_POST['desc'] = (isset($_POST['desc'])) ? stripslashes($_POST['desc']) : '';
  435. $_POST['category_id'] = (isset($_POST['category_id'])) ? $_POST['category_id'] : '';
  436. $_POST['locked'] = (isset($_POST['locked'])) ? $_POST['locked'] : '';
  437. }
  438. else
  439. {
  440. $_POST['name'] = (isset($_POST['name'])) ? stripslashes($_POST['name']) : $forum_info['name'];
  441. $_POST['desc'] = (isset($_POST['desc'])) ? stripslashes($_POST['desc']) : $forum_info['desc'];
  442. $_POST['category_id'] = (isset($_POST['category_id'])) ? $_POST['category_id'] : $forum_info['category_id'];
  443. $_POST['locked'] = (isset($_POST['locked'])) ? $_POST['locked'] : $forum_info['locked'];
  444. }
  445. $this->getView('MainView')->putExistingModel('ForumsModel', $this->getModel('ForumsModel'));
  446. $this->getView('MainView')->admin_forum_form($msg, $m);
  447. }
  448. }
  449. public function banlist()
  450. {
  451. if ($this->loadDependencies())
  452. $this->getView('MainView')->admin_banlist();
  453. }
  454. public function delban()
  455. {
  456. if ($this->loadDependencies())
  457. {
  458. $this->loadModel('BansModel');
  459. $_GET['id'] = (isset($_GET['id'])) ? trim(strip_tags($this->db->real_escape_string($_GET['id']))) : 0;
  460. $ban_info = $this->getModel('BansModel')->getBan($_GET['id']);
  461. if ($ban_info == null)
  462. {
  463. $this->getView('MainView')->forum_message('Ban does not exist!', 'index.php?mode=admin&amp;submode=banlist');
  464. $lockv = true;
  465. }
  466. if (isset($_POST['confirmed']) && !isset($lockv))
  467. {
  468. if (!isset($_POST['rejected']))
  469. {
  470. $this->getModel('BansModel')->deleteBan($_GET['id']);
  471. $this->getView('MainView')->forum_message('Ban deleted. Redirecting...', 'index.php?mode=admin&amp;submode=banlist');
  472. $lockv = true;
  473. }
  474. else
  475. $this->forward('index.php?mode=admin&submode=banlist');
  476. }
  477. if (!isset($lockv))
  478. $this->getView('MainView')->confirm_action('Do you want delete ban for user <span style="font-weight: bold">'.$ban_info['nick'].'</span>?');
  479. }
  480. }
  481. public function addban()
  482. {
  483. if ($this->loadDependencies())
  484. {
  485. $msg = '';
  486. $this->loadModel('BansModel');
  487. $this->loadModel('UsersModel');
  488. if (isset($_POST['user_id'], $_POST['reason']))
  489. {
  490. $_POST['user_id'] = trim(strip_tags($this->db->real_escape_string($_POST['user_id'])));
  491. $_POST['reason'] = trim(htmlspecialchars($this->db->real_escape_string($_POST['reason'])));
  492. if ($_POST['user_id'] == $this->getModel('SessionModel')->getID())
  493. $msg .= 'You cannot ban your profile!<br>';
  494. if ($this->getModel('BansModel')->getUserBan($_POST['user_id']) != null)
  495. $msg .= 'This user has already been banned!<br>';
  496. if ($this->getModel('UsersModel')->getUserInformation($_POST['user_id']) == null)
  497. $msg .= 'User does not exist!<br>';
  498. if ($msg == '')
  499. {
  500. $this->getModel('BansModel')->addBan($_POST['user_id'], $_POST['reason']);
  501. $this->getView('MainView')->forum_message('Ban added. Redirecting...', 'index.php?mode=admin&amp;submode=banlist');
  502. $lockv = true;
  503. }
  504. }
  505. if (!isset($lockv))
  506. {
  507. $_POST['user_id'] = (isset($_POST['user_id'])) ? $_POST['user_id'] : '';
  508. $_POST['reason'] = (isset($_POST['reason'])) ? stripslashes($_POST['reason']) : '';
  509. $this->getView('MainView')->admin_ban_form($msg);
  510. }
  511. }
  512. }
  513. }
  514. ?>