PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/acp/acp_reasons.php

https://bitbucket.org/cmwdosp/cmwbb3
PHP | 394 lines | 298 code | 66 blank | 30 comment | 56 complexity | d16d37b3f96d419b237ad397dc20f7cf MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. /**
  14. * @ignore
  15. */
  16. if (!defined('IN_PHPBB'))
  17. {
  18. exit;
  19. }
  20. class acp_reasons
  21. {
  22. var $u_action;
  23. function main($id, $mode)
  24. {
  25. global $db, $user, $template;
  26. global $request, $phpbb_log;
  27. $user->add_lang(array('mcp', 'acp/posting'));
  28. // Set up general vars
  29. $action = $request->variable('action', '');
  30. $submit = (isset($_POST['submit'])) ? true : false;
  31. $reason_id = $request->variable('id', 0);
  32. $this->tpl_name = 'acp_reasons';
  33. $this->page_title = 'ACP_REASONS';
  34. $form_name = 'acp_reason';
  35. add_form_key('acp_reason');
  36. $error = array();
  37. switch ($action)
  38. {
  39. case 'add':
  40. case 'edit':
  41. $reason_row = array(
  42. 'reason_title' => $request->variable('reason_title', '', true),
  43. 'reason_description' => $request->variable('reason_description', '', true),
  44. );
  45. if ($submit)
  46. {
  47. if (!check_form_key($form_name))
  48. {
  49. $error[] = $user->lang['FORM_INVALID'];
  50. }
  51. // Reason specified?
  52. if (!$reason_row['reason_title'] || !$reason_row['reason_description'])
  53. {
  54. $error[] = $user->lang['NO_REASON_INFO'];
  55. }
  56. $check_double = ($action == 'add') ? true : false;
  57. if ($action == 'edit')
  58. {
  59. $sql = 'SELECT reason_title
  60. FROM ' . REPORTS_REASONS_TABLE . "
  61. WHERE reason_id = $reason_id";
  62. $result = $db->sql_query($sql);
  63. $row = $db->sql_fetchrow($result);
  64. $db->sql_freeresult($result);
  65. if (strtolower($row['reason_title']) == 'other' || strtolower($reason_row['reason_title']) == 'other')
  66. {
  67. $reason_row['reason_title'] = 'other';
  68. }
  69. if ($row['reason_title'] != $reason_row['reason_title'])
  70. {
  71. $check_double = true;
  72. }
  73. }
  74. // Check for same reason if adding it...
  75. if ($check_double)
  76. {
  77. $sql = 'SELECT reason_id
  78. FROM ' . REPORTS_REASONS_TABLE . "
  79. WHERE reason_title = '" . $db->sql_escape($reason_row['reason_title']) . "'";
  80. $result = $db->sql_query($sql);
  81. $row = $db->sql_fetchrow($result);
  82. $db->sql_freeresult($result);
  83. if ($row || ($action == 'add' && strtolower($reason_row['reason_title']) == 'other'))
  84. {
  85. $error[] = $user->lang['REASON_ALREADY_EXIST'];
  86. }
  87. }
  88. if (!sizeof($error))
  89. {
  90. // New reason?
  91. if ($action == 'add')
  92. {
  93. // Get new order...
  94. $sql = 'SELECT MAX(reason_order) as max_reason_order
  95. FROM ' . REPORTS_REASONS_TABLE;
  96. $result = $db->sql_query($sql);
  97. $max_order = (int) $db->sql_fetchfield('max_reason_order');
  98. $db->sql_freeresult($result);
  99. $sql_ary = array(
  100. 'reason_title' => (string) $reason_row['reason_title'],
  101. 'reason_description' => (string) $reason_row['reason_description'],
  102. 'reason_order' => $max_order + 1
  103. );
  104. $db->sql_query('INSERT INTO ' . REPORTS_REASONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  105. $log = 'ADDED';
  106. }
  107. else if ($reason_id)
  108. {
  109. $sql_ary = array(
  110. 'reason_title' => (string) $reason_row['reason_title'],
  111. 'reason_description' => (string) $reason_row['reason_description'],
  112. );
  113. $db->sql_query('UPDATE ' . REPORTS_REASONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  114. WHERE reason_id = ' . $reason_id);
  115. $log = 'UPDATED';
  116. }
  117. $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_REASON_' . $log, false, array($reason_row['reason_title']));
  118. trigger_error($user->lang['REASON_' . $log] . adm_back_link($this->u_action));
  119. }
  120. }
  121. else if ($reason_id)
  122. {
  123. $sql = 'SELECT *
  124. FROM ' . REPORTS_REASONS_TABLE . '
  125. WHERE reason_id = ' . $reason_id;
  126. $result = $db->sql_query($sql);
  127. $reason_row = $db->sql_fetchrow($result);
  128. $db->sql_freeresult($result);
  129. if (!$reason_row)
  130. {
  131. trigger_error($user->lang['NO_REASON'] . adm_back_link($this->u_action), E_USER_WARNING);
  132. }
  133. }
  134. $l_title = ($action == 'edit') ? 'EDIT' : 'ADD';
  135. $translated = false;
  136. // If the reason is defined within the language file, we will use the localized version, else just use the database entry...
  137. if (isset($user->lang['report_reasons']['TITLE'][strtoupper($reason_row['reason_title'])]) && isset($user->lang['report_reasons']['DESCRIPTION'][strtoupper($reason_row['reason_title'])]))
  138. {
  139. $translated = true;
  140. }
  141. $template->assign_vars(array(
  142. 'L_TITLE' => $user->lang['REASON_' . $l_title],
  143. 'U_ACTION' => $this->u_action . "&amp;id=$reason_id&amp;action=$action",
  144. 'U_BACK' => $this->u_action,
  145. 'ERROR_MSG' => (sizeof($error)) ? implode('<br />', $error) : '',
  146. 'REASON_TITLE' => $reason_row['reason_title'],
  147. 'REASON_DESCRIPTION' => $reason_row['reason_description'],
  148. 'TRANSLATED_TITLE' => ($translated) ? $user->lang['report_reasons']['TITLE'][strtoupper($reason_row['reason_title'])] : '',
  149. 'TRANSLATED_DESCRIPTION'=> ($translated) ? $user->lang['report_reasons']['DESCRIPTION'][strtoupper($reason_row['reason_title'])] : '',
  150. 'S_AVAILABLE_TITLES' => implode($user->lang['COMMA_SEPARATOR'], array_map('htmlspecialchars', array_keys($user->lang['report_reasons']['TITLE']))),
  151. 'S_EDIT_REASON' => true,
  152. 'S_TRANSLATED' => $translated,
  153. 'S_ERROR' => (sizeof($error)) ? true : false,
  154. )
  155. );
  156. return;
  157. break;
  158. case 'delete':
  159. $sql = 'SELECT *
  160. FROM ' . REPORTS_REASONS_TABLE . '
  161. WHERE reason_id = ' . $reason_id;
  162. $result = $db->sql_query($sql);
  163. $reason_row = $db->sql_fetchrow($result);
  164. $db->sql_freeresult($result);
  165. if (!$reason_row)
  166. {
  167. trigger_error($user->lang['NO_REASON'] . adm_back_link($this->u_action), E_USER_WARNING);
  168. }
  169. if (strtolower($reason_row['reason_title']) == 'other')
  170. {
  171. trigger_error($user->lang['NO_REMOVE_DEFAULT_REASON'] . adm_back_link($this->u_action), E_USER_WARNING);
  172. }
  173. // Let the deletion be confirmed...
  174. if (confirm_box(true))
  175. {
  176. $sql = 'SELECT reason_id
  177. FROM ' . REPORTS_REASONS_TABLE . "
  178. WHERE LOWER(reason_title) = 'other'";
  179. $result = $db->sql_query($sql);
  180. $other_reason_id = (int) $db->sql_fetchfield('reason_id');
  181. $db->sql_freeresult($result);
  182. switch ($db->get_sql_layer())
  183. {
  184. // The ugly one!
  185. case 'mysqli':
  186. case 'mysql4':
  187. case 'mysql':
  188. // Change the reports using this reason to 'other'
  189. $sql = 'UPDATE ' . REPORTS_TABLE . '
  190. SET reason_id = ' . $other_reason_id . ", report_text = CONCAT('" . $db->sql_escape($reason_row['reason_description']) . "\n\n', report_text)
  191. WHERE reason_id = $reason_id";
  192. break;
  193. // Standard? What's that?
  194. case 'mssql_odbc':
  195. case 'mssqlnative':
  196. // Change the reports using this reason to 'other'
  197. $sql = "DECLARE @ptrval binary(16)
  198. SELECT @ptrval = TEXTPTR(report_text)
  199. FROM " . REPORTS_TABLE . "
  200. WHERE reason_id = " . $reason_id . "
  201. UPDATETEXT " . REPORTS_TABLE . ".report_text @ptrval 0 0 '" . $db->sql_escape($reason_row['reason_description']) . "\n\n'
  202. UPDATE " . REPORTS_TABLE . '
  203. SET reason_id = ' . $other_reason_id . "
  204. WHERE reason_id = $reason_id";
  205. break;
  206. // Teh standard
  207. case 'postgres':
  208. case 'oracle':
  209. case 'sqlite3':
  210. // Change the reports using this reason to 'other'
  211. $sql = 'UPDATE ' . REPORTS_TABLE . '
  212. SET reason_id = ' . $other_reason_id . ", report_text = '" . $db->sql_escape($reason_row['reason_description']) . "\n\n' || report_text
  213. WHERE reason_id = $reason_id";
  214. break;
  215. }
  216. $db->sql_query($sql);
  217. $db->sql_query('DELETE FROM ' . REPORTS_REASONS_TABLE . ' WHERE reason_id = ' . $reason_id);
  218. $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_REASON_REMOVED', false, array($reason_row['reason_title']));
  219. trigger_error($user->lang['REASON_REMOVED'] . adm_back_link($this->u_action));
  220. }
  221. else
  222. {
  223. confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
  224. 'i' => $id,
  225. 'mode' => $mode,
  226. 'action' => $action,
  227. 'id' => $reason_id))
  228. );
  229. }
  230. break;
  231. case 'move_up':
  232. case 'move_down':
  233. if (!check_link_hash($request->variable('hash', ''), 'acp_reasons'))
  234. {
  235. trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
  236. }
  237. $sql = 'SELECT reason_order
  238. FROM ' . REPORTS_REASONS_TABLE . "
  239. WHERE reason_id = $reason_id";
  240. $result = $db->sql_query($sql);
  241. $order = $db->sql_fetchfield('reason_order');
  242. $db->sql_freeresult($result);
  243. if ($order === false || ($order == 0 && $action == 'move_up'))
  244. {
  245. break;
  246. }
  247. $order = (int) $order;
  248. $order_total = $order * 2 + (($action == 'move_up') ? -1 : 1);
  249. $sql = 'UPDATE ' . REPORTS_REASONS_TABLE . '
  250. SET reason_order = ' . $order_total . ' - reason_order
  251. WHERE reason_order IN (' . $order . ', ' . (($action == 'move_up') ? $order - 1 : $order + 1) . ')';
  252. $db->sql_query($sql);
  253. if ($request->is_ajax())
  254. {
  255. $json_response = new \phpbb\json_response;
  256. $json_response->send(array(
  257. 'success' => (bool) $db->sql_affectedrows(),
  258. ));
  259. }
  260. break;
  261. }
  262. // By default, check that order is valid and fix it if necessary
  263. $sql = 'SELECT reason_id, reason_order
  264. FROM ' . REPORTS_REASONS_TABLE . '
  265. ORDER BY reason_order';
  266. $result = $db->sql_query($sql);
  267. if ($row = $db->sql_fetchrow($result))
  268. {
  269. $order = 0;
  270. do
  271. {
  272. ++$order;
  273. if ($row['reason_order'] != $order)
  274. {
  275. $sql = 'UPDATE ' . REPORTS_REASONS_TABLE . "
  276. SET reason_order = $order
  277. WHERE reason_id = {$row['reason_id']}";
  278. $db->sql_query($sql);
  279. }
  280. }
  281. while ($row = $db->sql_fetchrow($result));
  282. }
  283. $db->sql_freeresult($result);
  284. $template->assign_vars(array(
  285. 'U_ACTION' => $this->u_action,
  286. )
  287. );
  288. // Reason count
  289. $sql = 'SELECT reason_id, COUNT(reason_id) AS reason_count
  290. FROM ' . REPORTS_TABLE . '
  291. GROUP BY reason_id';
  292. $result = $db->sql_query($sql);
  293. $reason_count = array();
  294. while ($row = $db->sql_fetchrow($result))
  295. {
  296. $reason_count[$row['reason_id']] = $row['reason_count'];
  297. }
  298. $db->sql_freeresult($result);
  299. $sql = 'SELECT *
  300. FROM ' . REPORTS_REASONS_TABLE . '
  301. ORDER BY reason_order ASC';
  302. $result = $db->sql_query($sql);
  303. while ($row = $db->sql_fetchrow($result))
  304. {
  305. $translated = false;
  306. $other_reason = ($row['reason_title'] == 'other') ? true : false;
  307. // If the reason is defined within the language file, we will use the localized version, else just use the database entry...
  308. if (isset($user->lang['report_reasons']['TITLE'][strtoupper($row['reason_title'])]) && isset($user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])]))
  309. {
  310. $row['reason_description'] = $user->lang['report_reasons']['DESCRIPTION'][strtoupper($row['reason_title'])];
  311. $row['reason_title'] = $user->lang['report_reasons']['TITLE'][strtoupper($row['reason_title'])];
  312. $translated = true;
  313. }
  314. $template->assign_block_vars('reasons', array(
  315. 'REASON_TITLE' => $row['reason_title'],
  316. 'REASON_DESCRIPTION' => $row['reason_description'],
  317. 'REASON_COUNT' => (isset($reason_count[$row['reason_id']])) ? $reason_count[$row['reason_id']] : 0,
  318. 'S_TRANSLATED' => $translated,
  319. 'S_OTHER_REASON' => $other_reason,
  320. 'U_EDIT' => $this->u_action . '&amp;action=edit&amp;id=' . $row['reason_id'],
  321. 'U_DELETE' => (!$other_reason) ? $this->u_action . '&amp;action=delete&amp;id=' . $row['reason_id'] : '',
  322. 'U_MOVE_UP' => $this->u_action . '&amp;action=move_up&amp;id=' . $row['reason_id'] . '&amp;hash=' . generate_link_hash('acp_reasons'),
  323. 'U_MOVE_DOWN' => $this->u_action . '&amp;action=move_down&amp;id=' . $row['reason_id'] . '&amp;hash=' . generate_link_hash('acp_reasons'))
  324. );
  325. }
  326. $db->sql_freeresult($result);
  327. }
  328. }