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.

600 lines
18 KiB

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