PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/includes/acp/acp_ranks.php

http://github.com/phpbb/phpbb
PHP | 285 lines | 192 code | 53 blank | 40 comment | 24 complexity | b5df131c1c8430d5e4fa9d87c5f31125 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  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_ranks
  21. {
  22. var $u_action;
  23. function main($id, $mode)
  24. {
  25. global $db, $user, $template, $cache, $request, $phpbb_dispatcher;
  26. global $config, $phpbb_root_path, $phpbb_admin_path, $phpbb_log;
  27. $user->add_lang('acp/posting');
  28. // Set up general vars
  29. $action = $request->variable('action', '');
  30. $action = (isset($_POST['add'])) ? 'add' : $action;
  31. $action = (isset($_POST['save'])) ? 'save' : $action;
  32. $rank_id = $request->variable('id', 0);
  33. $this->tpl_name = 'acp_ranks';
  34. $this->page_title = 'ACP_MANAGE_RANKS';
  35. $form_name = 'acp_ranks';
  36. add_form_key($form_name);
  37. switch ($action)
  38. {
  39. case 'save':
  40. if (!check_form_key($form_name))
  41. {
  42. trigger_error($user->lang['FORM_INVALID']. adm_back_link($this->u_action), E_USER_WARNING);
  43. }
  44. $rank_title = $request->variable('title', '', true);
  45. $special_rank = $request->variable('special_rank', 0);
  46. $min_posts = ($special_rank) ? 0 : max(0, $request->variable('min_posts', 0));
  47. $rank_image = $request->variable('rank_image', '');
  48. // The rank image has to be a jpg, gif or png
  49. if ($rank_image != '' && !preg_match('#(\.gif|\.png|\.jpg|\.jpeg)$#i', $rank_image))
  50. {
  51. $rank_image = '';
  52. }
  53. if (!$rank_title)
  54. {
  55. trigger_error($user->lang['NO_RANK_TITLE'] . adm_back_link($this->u_action), E_USER_WARNING);
  56. }
  57. $sql_ary = array(
  58. 'rank_title' => $rank_title,
  59. 'rank_special' => $special_rank,
  60. 'rank_min' => $min_posts,
  61. 'rank_image' => htmlspecialchars_decode($rank_image)
  62. );
  63. /**
  64. * Modify the SQL array when saving a rank
  65. *
  66. * @event core.acp_ranks_save_modify_sql_ary
  67. * @var int rank_id The ID of the rank (if available)
  68. * @var array sql_ary Array with the rank's data
  69. * @since 3.1.0-RC3
  70. */
  71. $vars = array('rank_id', 'sql_ary');
  72. extract($phpbb_dispatcher->trigger_event('core.acp_ranks_save_modify_sql_ary', compact($vars)));
  73. if ($rank_id)
  74. {
  75. $sql = 'UPDATE ' . RANKS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " WHERE rank_id = $rank_id";
  76. $message = $user->lang['RANK_UPDATED'];
  77. $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_RANK_UPDATED', false, array($rank_title));
  78. }
  79. else
  80. {
  81. $sql = 'INSERT INTO ' . RANKS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  82. $message = $user->lang['RANK_ADDED'];
  83. $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_RANK_ADDED', false, array($rank_title));
  84. }
  85. $db->sql_query($sql);
  86. $cache->destroy('_ranks');
  87. trigger_error($message . adm_back_link($this->u_action));
  88. break;
  89. case 'delete':
  90. if (!$rank_id)
  91. {
  92. trigger_error($user->lang['MUST_SELECT_RANK'] . adm_back_link($this->u_action), E_USER_WARNING);
  93. }
  94. if (confirm_box(true))
  95. {
  96. $sql = 'SELECT rank_title
  97. FROM ' . RANKS_TABLE . '
  98. WHERE rank_id = ' . $rank_id;
  99. $result = $db->sql_query($sql);
  100. $rank_title = (string) $db->sql_fetchfield('rank_title');
  101. $db->sql_freeresult($result);
  102. $sql = 'DELETE FROM ' . RANKS_TABLE . "
  103. WHERE rank_id = $rank_id";
  104. $db->sql_query($sql);
  105. $sql = 'UPDATE ' . USERS_TABLE . "
  106. SET user_rank = 0
  107. WHERE user_rank = $rank_id";
  108. $db->sql_query($sql);
  109. $cache->destroy('_ranks');
  110. $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_RANK_REMOVED', false, array($rank_title));
  111. if ($request->is_ajax())
  112. {
  113. $json_response = new \phpbb\json_response;
  114. $json_response->send(array(
  115. 'MESSAGE_TITLE' => $user->lang['INFORMATION'],
  116. 'MESSAGE_TEXT' => $user->lang['RANK_REMOVED'],
  117. 'REFRESH_DATA' => array(
  118. 'time' => 3
  119. )
  120. ));
  121. }
  122. }
  123. else
  124. {
  125. confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
  126. 'i' => $id,
  127. 'mode' => $mode,
  128. 'rank_id' => $rank_id,
  129. 'action' => 'delete',
  130. )));
  131. }
  132. break;
  133. case 'edit':
  134. case 'add':
  135. $ranks = $existing_imgs = array();
  136. $sql = 'SELECT *
  137. FROM ' . RANKS_TABLE . '
  138. ORDER BY rank_min ASC, rank_special ASC';
  139. $result = $db->sql_query($sql);
  140. while ($row = $db->sql_fetchrow($result))
  141. {
  142. $existing_imgs[] = $row['rank_image'];
  143. if ($action == 'edit' && $rank_id == $row['rank_id'])
  144. {
  145. $ranks = $row;
  146. }
  147. }
  148. $db->sql_freeresult($result);
  149. $imglist = filelist($phpbb_root_path . $config['ranks_path'], '');
  150. $edit_img = $filename_list = '';
  151. foreach ($imglist as $path => $img_ary)
  152. {
  153. sort($img_ary);
  154. foreach ($img_ary as $img)
  155. {
  156. $img = $path . $img;
  157. if ($ranks && $img == $ranks['rank_image'])
  158. {
  159. $selected = ' selected="selected"';
  160. $edit_img = $img;
  161. }
  162. else
  163. {
  164. $selected = '';
  165. }
  166. if (strlen($img) > 255)
  167. {
  168. continue;
  169. }
  170. $filename_list .= '<option value="' . htmlspecialchars($img) . '"' . $selected . '>' . $img . ((in_array($img, $existing_imgs)) ? ' ' . $user->lang['RANK_IMAGE_IN_USE'] : '') . '</option>';
  171. }
  172. }
  173. $filename_list = '<option value=""' . (($edit_img == '') ? ' selected="selected"' : '') . '>----------</option>' . $filename_list;
  174. unset($existing_imgs, $imglist);
  175. $tpl_ary = array(
  176. 'S_EDIT' => true,
  177. 'U_BACK' => $this->u_action,
  178. 'RANKS_PATH' => $phpbb_root_path . $config['ranks_path'],
  179. 'U_ACTION' => $this->u_action . '&amp;id=' . $rank_id,
  180. 'RANK_TITLE' => (isset($ranks['rank_title'])) ? $ranks['rank_title'] : '',
  181. 'S_FILENAME_LIST' => $filename_list,
  182. 'RANK_IMAGE' => ($edit_img) ? $phpbb_root_path . $config['ranks_path'] . '/' . $edit_img : htmlspecialchars($phpbb_admin_path) . 'images/spacer.gif',
  183. 'S_SPECIAL_RANK' => (isset($ranks['rank_special']) && $ranks['rank_special']) ? true : false,
  184. 'MIN_POSTS' => (isset($ranks['rank_min']) && !$ranks['rank_special']) ? $ranks['rank_min'] : 0,
  185. );
  186. /**
  187. * Modify the template output array for editing/adding ranks
  188. *
  189. * @event core.acp_ranks_edit_modify_tpl_ary
  190. * @var array ranks Array with the rank's data
  191. * @var array tpl_ary Array with the rank's template data
  192. * @since 3.1.0-RC3
  193. */
  194. $vars = array('ranks', 'tpl_ary');
  195. extract($phpbb_dispatcher->trigger_event('core.acp_ranks_edit_modify_tpl_ary', compact($vars)));
  196. $template->assign_vars($tpl_ary);
  197. return;
  198. break;
  199. }
  200. $template->assign_vars(array(
  201. 'U_ACTION' => $this->u_action)
  202. );
  203. $sql = 'SELECT *
  204. FROM ' . RANKS_TABLE . '
  205. ORDER BY rank_special DESC, rank_min ASC, rank_title ASC';
  206. $result = $db->sql_query($sql);
  207. while ($row = $db->sql_fetchrow($result))
  208. {
  209. $rank_row = array(
  210. 'S_RANK_IMAGE' => ($row['rank_image']) ? true : false,
  211. 'S_SPECIAL_RANK' => ($row['rank_special']) ? true : false,
  212. 'RANK_IMAGE' => $phpbb_root_path . $config['ranks_path'] . '/' . $row['rank_image'],
  213. 'RANK_TITLE' => $row['rank_title'],
  214. 'MIN_POSTS' => $row['rank_min'],
  215. 'U_EDIT' => $this->u_action . '&amp;action=edit&amp;id=' . $row['rank_id'],
  216. 'U_DELETE' => $this->u_action . '&amp;action=delete&amp;id=' . $row['rank_id'],
  217. );
  218. /**
  219. * Modify the template output array for each listed rank
  220. *
  221. * @event core.acp_ranks_list_modify_rank_row
  222. * @var array row Array with the rank's data
  223. * @var array rank_row Array with the rank's template data
  224. * @since 3.1.0-RC3
  225. */
  226. $vars = array('row', 'rank_row');
  227. extract($phpbb_dispatcher->trigger_event('core.acp_ranks_list_modify_rank_row', compact($vars)));
  228. $template->assign_block_vars('ranks', $rank_row);
  229. }
  230. $db->sql_freeresult($result);
  231. }
  232. }