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.

606 lines
19 KiB

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