PageRenderTime 113ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class_cms_permissions.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 1050 lines | 746 code | 173 blank | 131 comment | 132 complexity | fda0f11ac7324d1210961696b3815adb MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. if (!defined('IN_ICYPHOENIX'))
  11. {
  12. die('Hacking attempt');
  13. }
  14. class cms_permissions
  15. {
  16. var $u_action;
  17. var $permission_dropdown;
  18. var $id_type;
  19. /**
  20. * Construct
  21. */
  22. function __construct()
  23. {
  24. }
  25. function main($id, $mode)
  26. {
  27. global $db, $cache, $config, $user, $lang, $auth, $template, $cms_admin;
  28. add_permission_language();
  29. include_once(IP_ROOT_PATH . 'includes/class_auth_admin.' . PHP_EXT);
  30. $auth_admin = new auth_admin();
  31. // Set some vars
  32. $action = request_var('paction', array('' => 0));
  33. $action = key($action);
  34. $action = (isset($_POST['psubmit'])) ? 'apply_permissions' : $action;
  35. $all_forums = request_var('all_forums', 0);
  36. $subforum_id = request_var('subforum_id', 0);
  37. $forum_id = request_var('forum_id', array(0));
  38. $id_type = request_var('id_type', '');
  39. $id_types_array = array('forum', 'block', 'layout', 'layout_special');
  40. $id_type = in_array($id_type, $id_types_array) ? $id_type : $id_types_array[0];
  41. $this->id_type = $id_type;
  42. $username = request_var('username', array(''), true);
  43. $usernames = request_var('usernames', '', true);
  44. $user_id = request_var('user_id', array(0));
  45. $group_id = request_var('group_id', array(0));
  46. $select_all_groups = request_var('select_all_groups', 0);
  47. $form_name = 'acp_permissions';
  48. add_form_key($form_name);
  49. $this->tpl_name = 'cms_permissions.tpl';
  50. $this->u_action = append_sid($cms_admin->root . '?mode=auth&amp;pmode=' . $mode);
  51. /*
  52. // Trace has other vars
  53. if ($mode == 'trace')
  54. {
  55. $user_id = request_var('u', 0);
  56. $forum_id = request_var('f', 0);
  57. $permission = request_var('auth', '');
  58. $this->tpl_name = 'cms_permission_trace.tpl';
  59. if ($user_id && isset($auth_admin->acl_options['id'][$permission]))
  60. {
  61. $this->page_title = sprintf($user->lang['TRACE_PERMISSION'], $user->lang['acl_' . $permission]['lang']);
  62. $this->permission_trace($user_id, $forum_id, $permission);
  63. return;
  64. }
  65. trigger_error('NO_MODE', E_USER_ERROR);
  66. }
  67. */
  68. // If select all groups is set, we pre-build the group id array (this option is used for other screens to link to the permission settings screen)
  69. if ($select_all_groups)
  70. {
  71. $sql = 'SELECT group_id
  72. FROM ' . GROUPS_TABLE . '
  73. WHERE group_single_user = 0';
  74. $result = $db->sql_query($sql);
  75. while ($row = $db->sql_fetchrow($result))
  76. {
  77. $group_id[] = $row['group_id'];
  78. }
  79. $db->sql_freeresult($result);
  80. }
  81. // Map usernames to ids and vice versa
  82. if ($usernames)
  83. {
  84. $username = explode("\n", $usernames);
  85. }
  86. unset($usernames);
  87. if (sizeof($username) && !sizeof($user_id))
  88. {
  89. user_get_id_name($user_id, $username);
  90. if (!sizeof($user_id))
  91. {
  92. trigger_error($user->lang['SELECTED_USER_NOT_EXIST'] . page_back_link($this->u_action), E_USER_WARNING);
  93. }
  94. }
  95. unset($username);
  96. // Define some common variables for every mode
  97. $error = array();
  98. $permission_scope = (strpos($mode, '_global') !== false) ? 'global' : 'local';
  99. // Showing introductionary page?
  100. if ($mode == 'intro')
  101. {
  102. $this->page_title = 'CMS_PERMISSIONS';
  103. $template->assign_vars(array(
  104. 'S_INTRO' => true
  105. )
  106. );
  107. return;
  108. }
  109. switch ($mode)
  110. {
  111. case 'setting_admin_global':
  112. case 'setting_mod_global':
  113. $this->permission_dropdown = (strpos($mode, '_admin_') !== false) ? array('a_') : array('m_');
  114. $permission_victim = array('usergroup');
  115. $this->page_title = ($mode == 'setting_admin_global') ? 'CMS_PERMISSIONS_ADMINS' : 'CMS_PERMISSIONS_MODS';
  116. break;
  117. case 'setting_cms_user_global':
  118. case 'setting_cms_group_global':
  119. $this->permission_dropdown = array('cms_', 'pl_', 'u_');
  120. $permission_victim = array('usergroup');
  121. //$permission_victim = ($mode == 'setting_cms_user_global') ? array('user') : array('group');
  122. $this->page_title = ($mode == 'setting_cms_user_global') ? 'CMS_PERMISSIONS_CMS_USERS' : 'CMS_PERMISSIONS_CMS_GROUPS';
  123. break;
  124. case 'setting_cms_user_local':
  125. case 'setting_cms_group_local':
  126. $this->permission_dropdown = ($this->id_type == 'layout') ? array('cmsl_') : (($this->id_type == 'layout_special') ? array('cmss_') : array('cmsb_'));
  127. $permission_victim = array('cms', 'usergroup');
  128. //$permission_victim = ($mode == 'setting_cms_user_local') ? array('user') : array('group');
  129. $this->page_title = ($mode == 'setting_cms_user_local') ? 'CMS_PERMISSIONS_CMS_USERS' : 'CMS_PERMISSIONS_CMS_GROUPS';
  130. break;
  131. case 'setting_plugins_user_global':
  132. case 'setting_plugins_group_global':
  133. $this->permission_dropdown = array('pl_', 'cms_', 'u_');
  134. $permission_victim = array('usergroup');
  135. //$permission_victim = ($mode == 'setting_plugins_user_global') ? array('user') : array('group');
  136. $this->page_title = ($mode == 'setting_plugins_user_global') ? 'CMS_PERMISSIONS_PLUGINS_USERS' : 'CMS_PERMISSIONS_PLUGINS_GROUPS';
  137. break;
  138. case 'setting_user_global':
  139. case 'setting_group_global':
  140. $this->permission_dropdown = array('u_', 'cms_', 'pl_');
  141. $permission_victim = ($mode == 'setting_user_global') ? array('user') : array('group');
  142. $this->page_title = ($mode == 'setting_user_global') ? 'CMS_PERMISSIONS_USERS' : 'CMS_PERMISSIONS_GROUPS';
  143. break;
  144. case 'setting_user_local':
  145. case 'setting_group_local':
  146. $this->permission_dropdown = array('f_', 'm_');
  147. $permission_victim = ($mode == 'setting_user_local') ? array('user') : array('group');
  148. $this->page_title = ($mode == 'setting_user_local') ? 'ACP_USERS_FORUM_PERMISSIONS' : 'ACP_GROUPS_FORUM_PERMISSIONS';
  149. break;
  150. case 'setting_mod_local':
  151. case 'setting_forum_local':
  152. $this->permission_dropdown = ($mode == 'setting_mod_local') ? array('m_') : array('f_');
  153. $permission_victim = array('forums', 'usergroup');
  154. $this->page_title = ($mode == 'setting_mod_local') ? 'ACP_FORUM_MODERATORS' : 'ACP_FORUM_PERMISSIONS';
  155. break;
  156. case 'view_admin_global':
  157. case 'view_user_global':
  158. case 'view_mod_global':
  159. $this->permission_dropdown = ($mode == 'view_admin_global') ? array('a_') : (($mode == 'view_user_global') ? array('u_') : array('m_'));
  160. $permission_victim = array('usergroup_view');
  161. $this->page_title = ($mode == 'view_admin_global') ? 'CMS_PERMISSIONS_ADMINS' : (($mode == 'view_user_global') ? 'CMS_PERMISSIONS_USERS' : 'CMS_PERMISSIONS_MODS');
  162. break;
  163. case 'view_mod_local':
  164. case 'view_forum_local':
  165. $this->permission_dropdown = ($mode == 'view_mod_local') ? array('m_') : array('f_');
  166. $permission_victim = array('usergroup_view');
  167. $this->page_title = ($mode == 'view_mod_local') ? 'ACP_VIEW_FORUM_MOD_PERMISSIONS' : 'ACP_VIEW_FORUM_PERMISSIONS';
  168. break;
  169. default:
  170. trigger_error('NO_MODE', E_USER_ERROR);
  171. break;
  172. }
  173. $template->assign_vars(array(
  174. 'L_TITLE' => $lang[$this->page_title],
  175. 'L_EXPLAIN' => $lang[$this->page_title . '_EXPLAIN']
  176. )
  177. );
  178. // Get permission type
  179. $permission_type = request_var('type', $this->permission_dropdown[0]);
  180. if (!in_array($permission_type, $this->permission_dropdown))
  181. {
  182. trigger_error($user->lang['WRONG_PERMISSION_TYPE'] . page_back_link($this->u_action), E_USER_WARNING);
  183. }
  184. // Handle actions
  185. if ((strpos($mode, 'setting_') === 0) && $action)
  186. {
  187. switch ($action)
  188. {
  189. case 'delete':
  190. if (confirm_box(true))
  191. {
  192. // All users/groups selected?
  193. $all_users = (isset($_POST['all_users'])) ? true : false;
  194. $all_groups = (isset($_POST['all_groups'])) ? true : false;
  195. if ($all_users || $all_groups)
  196. {
  197. $items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
  198. if ($all_users && sizeof($items['user_ids']))
  199. {
  200. $user_id = $items['user_ids'];
  201. }
  202. elseif ($all_groups && sizeof($items['group_ids']))
  203. {
  204. $group_id = $items['group_ids'];
  205. }
  206. }
  207. if (sizeof($user_id) || sizeof($group_id))
  208. {
  209. $this->remove_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id, $forum_id);
  210. }
  211. else
  212. {
  213. trigger_error($user->lang['NO_USER_GROUP_SELECTED'] . page_back_link($this->u_action), E_USER_WARNING);
  214. }
  215. }
  216. else
  217. {
  218. if (isset($_POST['cancel']))
  219. {
  220. $u_redirect = $this->u_action . '&amp;type=' . $permission_type;
  221. foreach ($forum_id as $fid)
  222. {
  223. $u_redirect .= '&amp;forum_id[]=' . $fid;
  224. }
  225. redirect($u_redirect);
  226. }
  227. $s_hidden_fields = array(
  228. 'i' => $id,
  229. 'pmode' => $mode,
  230. 'paction' => array($action => 1),
  231. 'user_id' => $user_id,
  232. 'group_id' => $group_id,
  233. 'forum_id' => $forum_id,
  234. 'id_type' => $this->id_type,
  235. 'type' => $permission_type,
  236. );
  237. if (isset($_POST['all_users']))
  238. {
  239. $s_hidden_fields['all_users'] = 1;
  240. }
  241. if (isset($_POST['all_groups']))
  242. {
  243. $s_hidden_fields['all_groups'] = 1;
  244. }
  245. confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields($s_hidden_fields));
  246. }
  247. break;
  248. case 'apply_permissions':
  249. if (!isset($_POST['setting']))
  250. {
  251. trigger_error($user->lang['NO_AUTH_SETTING_FOUND'] . page_back_link($this->u_action), E_USER_WARNING);
  252. }
  253. if (!check_form_key($form_name))
  254. {
  255. trigger_error($user->lang['FORM_INVALID'] . page_back_link($this->u_action), E_USER_WARNING);
  256. }
  257. $this->set_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
  258. break;
  259. case 'apply_all_permissions':
  260. if (!isset($_POST['setting']))
  261. {
  262. trigger_error($user->lang['NO_AUTH_SETTING_FOUND'] . page_back_link($this->u_action), E_USER_WARNING);
  263. }
  264. if (!check_form_key($form_name))
  265. {
  266. trigger_error($user->lang['FORM_INVALID'] . page_back_link($this->u_action), E_USER_WARNING);
  267. }
  268. $this->set_all_permissions($mode, $permission_type, $auth_admin, $user_id, $group_id);
  269. break;
  270. }
  271. }
  272. // Setting permissions screen
  273. $s_hidden_fields_array = array(
  274. 'user_id' => $user_id,
  275. 'group_id' => $group_id,
  276. 'forum_id' => $forum_id,
  277. );
  278. $s_hidden_fields = build_hidden_fields($s_hidden_fields_array);
  279. $s_hidden_fields_extra = true;
  280. // Go through the screens/options needed and present them in correct order
  281. foreach ($permission_victim as $victim)
  282. {
  283. switch ($victim)
  284. {
  285. case 'cms':
  286. if (sizeof($forum_id))
  287. {
  288. $this->check_existence($this->id_type, $forum_id);
  289. continue 2;
  290. }
  291. // We don't have anything selected, so we need to unset these types, because they will be set properly in template...
  292. $s_hidden_fields_extra = false;
  293. unset($s_hidden_fields_array['id_type']);
  294. unset($s_hidden_fields_array['type']);
  295. $ids = array();
  296. $ids_data = array();
  297. $types_array = array(
  298. 'l' => 'layout',
  299. 's' => 'layout_special',
  300. 'b' => 'block'
  301. );
  302. foreach ($types_array as $type_key => $type_value)
  303. {
  304. $s_options = '';
  305. $ids_data = $this->check_existence($type_value, $ids, true);
  306. foreach ($ids_data as $id_data)
  307. {
  308. $option_name = $id_data['name'];
  309. if ($type_key == 's')
  310. {
  311. $option_name = isset($lang['auth_view_' . strtolower($option_name)]) ? $lang['auth_view_' . strtolower($option_name)] : (isset($lang['cms_page_name_' . strtolower($option_name)]) ? $lang['cms_page_name_' . strtolower($option_name)] : ucfirst($option_name));
  312. }
  313. $s_options .= '<option value="' . (int) $id_data['id'] . '">' . $option_name . ' [' . (int) $id_data['id'] . ']' . '</option>';
  314. }
  315. $template->assign_vars(array(
  316. 'S_CMS_' . strtoupper($type_key) . '_OPTIONS' => $s_options
  317. )
  318. );
  319. }
  320. $template->assign_vars(array(
  321. 'S_SELECT_CMS' => true,
  322. )
  323. );
  324. break;
  325. case 'user':
  326. case 'group':
  327. if (sizeof($user_id))
  328. {
  329. $this->check_existence('user', $user_id);
  330. continue 2;
  331. }
  332. if (sizeof($group_id))
  333. {
  334. $this->check_existence('group', $group_id);
  335. continue 2;
  336. }
  337. $template->assign_vars(array(
  338. 'S_SELECT_USER' => true,
  339. 'S_SELECT_GROUP' => true,
  340. 'U_FIND_USERNAME' => append_sid(IP_ROOT_PATH . CMS_PAGE_SEARCH . '?mode=searchuser'),
  341. 'S_GROUP_OPTIONS' => group_select_options(false, false, false), // Show all groups
  342. )
  343. );
  344. break;
  345. case 'usergroup':
  346. case 'usergroup_view':
  347. $all_users = (isset($_POST['all_users'])) ? true : false;
  348. $all_groups = (isset($_POST['all_groups'])) ? true : false;
  349. if ((sizeof($user_id) && !$all_users) || (sizeof($group_id) && !$all_groups))
  350. {
  351. if (sizeof($user_id))
  352. {
  353. $this->check_existence('user', $user_id);
  354. }
  355. if (sizeof($group_id))
  356. {
  357. $this->check_existence('group', $group_id);
  358. }
  359. continue 2;
  360. }
  361. // Now we check the users... because the "all"-selection is different here (all defined users/groups)
  362. $items = $this->retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type);
  363. if ($all_users && sizeof($items['user_ids']))
  364. {
  365. $user_id = $items['user_ids'];
  366. continue 2;
  367. }
  368. if ($all_groups && sizeof($items['group_ids']))
  369. {
  370. $group_id = $items['group_ids'];
  371. continue 2;
  372. }
  373. $template->assign_vars(array(
  374. 'S_SELECT_USERGROUP' => ($victim == 'usergroup') ? true : false,
  375. 'S_SELECT_USERGROUP_VIEW' => ($victim == 'usergroup_view') ? true : false,
  376. 'S_DEFINED_USER_OPTIONS' => $items['user_ids_options'],
  377. 'S_DEFINED_GROUP_OPTIONS' => $items['group_ids_options'],
  378. 'S_ADD_GROUP_OPTIONS' => group_select_options(false, $items['group_ids'], false), // Show all groups
  379. 'U_FIND_USERNAME' => append_sid(IP_ROOT_PATH . CMS_PAGE_SEARCH . '?mode=searchuser'),
  380. )
  381. );
  382. break;
  383. }
  384. // Let the item names being displayed
  385. if (sizeof($forum_id))
  386. {
  387. $ids_data = $this->check_existence($this->id_type, $forum_id);
  388. $forum_names = array();
  389. foreach ($ids_data as $id_data)
  390. {
  391. $forum_names[] = $id_data['name'];
  392. }
  393. $template->assign_vars(array(
  394. 'S_FORUM_NAMES' => (sizeof($forum_names)) ? true : false,
  395. 'FORUM_NAMES' => implode(', ', $forum_names)
  396. )
  397. );
  398. }
  399. if (!empty($s_hidden_fields_extra))
  400. {
  401. // Setting other hidden fields
  402. $s_hidden_fields_array = array(
  403. 'id_type' => $this->id_type,
  404. 'type' => $permission_type
  405. );
  406. $s_hidden_fields .= build_hidden_fields($s_hidden_fields_array);
  407. }
  408. // The S_ALLOW_SELECT parameter below is a measure to lower memory usage.
  409. // If there are more than 5 forums selected the admin is not able to select all users/groups too.
  410. // We need to see if the number of forums can be increased or need to be decreased.
  411. $template->assign_vars(array(
  412. 'U_ACTION' => $this->u_action,
  413. 'U_ACTION_USERS' => str_replace('_group_global', '_user_global', $this->u_action),
  414. 'U_ACTION_GROUPS' => str_replace('_user_global', '_group_global', $this->u_action),
  415. 'ANONYMOUS_USER_ID' => ANONYMOUS,
  416. 'S_SELECT_VICTIM' => true,
  417. 'S_ALLOW_ALL_SELECT' => false,
  418. /*
  419. 'S_CAN_SELECT_USER' => ($auth->acl_get('a_authusers')) ? true : false,
  420. 'S_CAN_SELECT_GROUP' => ($auth->acl_get('a_authgroups')) ? true : false,
  421. */
  422. 'S_CAN_SELECT_USER' => true,
  423. 'S_CAN_SELECT_GROUP' => true,
  424. 'S_HIDDEN_FIELDS' => $s_hidden_fields
  425. )
  426. );
  427. return;
  428. }
  429. $template->assign_vars(array(
  430. 'S_PERMISSION_DROPDOWN' => (sizeof($this->permission_dropdown) > 1) ? $this->build_permission_dropdown($this->permission_dropdown, $permission_type, $permission_scope) : false,
  431. 'L_PERMISSION_TYPE' => $user->lang['ACL_TYPE_' . strtoupper($permission_type)],
  432. 'U_ACTION' => $this->u_action,
  433. 'S_HIDDEN_FIELDS' => $s_hidden_fields
  434. )
  435. );
  436. if (strpos($mode, 'setting_') === 0)
  437. {
  438. $template->assign_vars(array(
  439. 'S_SETTING_PERMISSIONS' => true
  440. )
  441. );
  442. $hold_ary = $auth_admin->get_mask('set', (sizeof($user_id)) ? $user_id : false, (sizeof($group_id)) ? $group_id : false, (sizeof($forum_id)) ? $forum_id : false, $permission_type, $permission_scope, ACL_NO);
  443. $auth_admin->display_mask('set', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
  444. }
  445. else
  446. {
  447. $template->assign_vars(array(
  448. 'S_VIEWING_PERMISSIONS' => true
  449. )
  450. );
  451. $hold_ary = $auth_admin->get_mask('view', (sizeof($user_id)) ? $user_id : false, (sizeof($group_id)) ? $group_id : false, (sizeof($forum_id)) ? $forum_id : false, $permission_type, $permission_scope, ACL_NEVER);
  452. $auth_admin->display_mask('view', $permission_type, $hold_ary, ((sizeof($user_id)) ? 'user' : 'group'), (($permission_scope == 'local') ? true : false));
  453. }
  454. }
  455. /**
  456. * Build dropdown field for changing permission types
  457. */
  458. function build_permission_dropdown($options, $default_option, $permission_scope)
  459. {
  460. global $user, $auth;
  461. $s_dropdown_options = '';
  462. foreach ($options as $setting)
  463. {
  464. // Mighty Gorgon: we don't check permissions yet...
  465. /*
  466. if (!$auth->acl_get('a_' . str_replace('_', '', $setting) . 'auth'))
  467. {
  468. continue;
  469. }
  470. */
  471. $selected = ($setting == $default_option) ? ' selected="selected"' : '';
  472. $l_setting = (isset($user->lang['permission_type'][$permission_scope][$setting])) ? $user->lang['permission_type'][$permission_scope][$setting] : $user->lang['permission_type'][$setting];
  473. $s_dropdown_options .= '<option value="' . $setting . '"' . $selected . '>' . $l_setting . '</option>';
  474. }
  475. return $s_dropdown_options;
  476. }
  477. /**
  478. * Check if selected items exist. Remove not found ids and if empty return error.
  479. */
  480. function check_existence($mode, &$ids, $all_ids = false)
  481. {
  482. global $db, $user;
  483. switch ($mode)
  484. {
  485. case 'user':
  486. $table = USERS_TABLE;
  487. $sql_select = array(
  488. 'id' => 'user_id',
  489. 'name' => 'username'
  490. );
  491. $sql_where = '';
  492. break;
  493. case 'group':
  494. $table = GROUPS_TABLE;
  495. $sql_select = array(
  496. 'id' => 'group_id',
  497. 'name' => 'group_name'
  498. );
  499. $sql_where = ' group_single_user = 0 ';
  500. break;
  501. case 'layout':
  502. $table = CMS_LAYOUT_TABLE;
  503. $sql_select = array(
  504. 'id' => 'lid',
  505. 'name' => 'name'
  506. );
  507. $sql_where = '';
  508. break;
  509. case 'layout_special':
  510. $table = CMS_LAYOUT_SPECIAL_TABLE;
  511. $sql_select = array(
  512. 'id' => 'lsid',
  513. 'name' => 'name'
  514. );
  515. $sql_where = '';
  516. break;
  517. case 'block':
  518. $table = CMS_BLOCKS_TABLE;
  519. $sql_select = array(
  520. 'id' => 'bid',
  521. 'name' => 'title'
  522. );
  523. $sql_where = '';
  524. break;
  525. case 'forum':
  526. $table = FORUMS_TABLE;
  527. $sql_select = array(
  528. 'id' => 'forum_id',
  529. 'name' => 'forum_name'
  530. );
  531. $sql_where = '';
  532. break;
  533. }
  534. if (!empty($all_ids) || sizeof($ids))
  535. {
  536. if (!empty($all_ids))
  537. {
  538. $sql_where = !empty($sql_where) ? (" WHERE " . $sql_where) : "";
  539. }
  540. else
  541. {
  542. $sql_where = " WHERE " . $db->sql_in_set($sql_select['id'], $ids) . " " . (!empty($sql_where) ? (" AND " . $sql_where) : "");
  543. }
  544. $sql = "SELECT " . $sql_select['id'] . ", " . $sql_select['name'] . "
  545. FROM " . $table . "
  546. " . $sql_where . "
  547. ORDER BY LOWER(" . $sql_select['name'] . ") ASC";
  548. $result = $db->sql_query($sql);
  549. $ids = array();
  550. $ids_data = array();
  551. while ($row = $db->sql_fetchrow($result))
  552. {
  553. $ids[] = (int) $row[$sql_select['id']];
  554. $ids_data[] = array('id' => (int) $row[$sql_select['id']], 'name' => $row[$sql_select['name']]);
  555. }
  556. $db->sql_freeresult($result);
  557. }
  558. if (!sizeof($ids))
  559. {
  560. trigger_error($user->lang['SELECTED_' . strtoupper($mode) . '_NOT_EXIST'] . page_back_link($this->u_action), E_USER_WARNING);
  561. }
  562. return $ids_data;
  563. }
  564. /**
  565. * Apply permissions
  566. */
  567. function set_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
  568. {
  569. global $user, $auth;
  570. $psubmit = request_var('psubmit', array(0 => array(0 => 0)));
  571. // User or group to be set?
  572. $ug_type = (sizeof($user_id)) ? 'user' : 'group';
  573. // Mighty Gorgon: we need to decide if we wish to leave this check upon specific permissions in settings permissions
  574. /*
  575. // Check the permission setting again
  576. if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
  577. {
  578. trigger_error($user->lang['NO_AUTH_OPERATION'] . page_back_link($this->u_action), E_USER_WARNING);
  579. }
  580. */
  581. $ug_id = $forum_id = 0;
  582. // We loop through the auth settings defined in our submit
  583. list($ug_id, ) = each($psubmit);
  584. list($forum_id, ) = each($psubmit[$ug_id]);
  585. if (empty($_POST['setting']) || empty($_POST['setting'][$ug_id]) || empty($_POST['setting'][$ug_id][$forum_id]) || !is_array($_POST['setting'][$ug_id][$forum_id]))
  586. {
  587. trigger_error('WRONG_PERMISSION_SETTING_FORMAT' . page_back_link($this->u_action), E_USER_WARNING);
  588. }
  589. // We obtain and check $_POST['setting'][$ug_id][$forum_id] directly and not using request_var() because request_var()
  590. // currently does not support the amount of dimensions required. ;)
  591. // $auth_settings = request_var('setting', array(0 => array(0 => array('' => 0))));
  592. $auth_settings = array_map('intval', $_POST['setting'][$ug_id][$forum_id]);
  593. // Do we have a role we want to set?
  594. $assigned_role = (isset($_POST['role'][$ug_id][$forum_id])) ? (int) $_POST['role'][$ug_id][$forum_id] : 0;
  595. // Do the admin want to set these permissions to other items too?
  596. $inherit = request_var('inherit', array(0 => array(0)));
  597. $ug_id = array($ug_id);
  598. $forum_id = array($forum_id);
  599. if (sizeof($inherit))
  600. {
  601. foreach ($inherit as $_ug_id => $forum_id_ary)
  602. {
  603. // Inherit users/groups?
  604. if (!in_array($_ug_id, $ug_id))
  605. {
  606. $ug_id[] = $_ug_id;
  607. }
  608. // Inherit forums?
  609. $forum_id = array_merge($forum_id, array_keys($forum_id_ary));
  610. }
  611. }
  612. $forum_id = array_unique($forum_id);
  613. // If the auth settings differ from the assigned role, then do not set a role...
  614. if ($assigned_role)
  615. {
  616. if (!$this->check_assigned_role($assigned_role, $auth_settings))
  617. {
  618. $assigned_role = 0;
  619. }
  620. }
  621. // Update the permission set...
  622. $auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_settings, $assigned_role);
  623. // Do we need to recache the moderator lists?
  624. if ($permission_type == 'm_')
  625. {
  626. cache_moderators();
  627. }
  628. // Do we need to recache the moderator lists?
  629. if ($permission_type == 'm_')
  630. {
  631. cache_moderators();
  632. }
  633. // Remove users who are now moderators or admins from everyones foes list
  634. if (($permission_type == 'm_') || ($permission_type == 'a_'))
  635. {
  636. // MIGHTY GORGON: We don't use foes...
  637. //update_foes($group_id, $user_id);
  638. }
  639. $this->log_action($mode, 'add', $permission_type, $ug_type, $ug_id, $forum_id);
  640. $redirect_url = $this->u_action;
  641. meta_refresh(3, $redirect_url);
  642. trigger_error($user->lang['AUTH_UPDATED'] . page_back_link($this->u_action));
  643. }
  644. /**
  645. * Apply all permissions
  646. */
  647. function set_all_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id)
  648. {
  649. global $user, $auth;
  650. // User or group to be set?
  651. $ug_type = (sizeof($user_id)) ? 'user' : 'group';
  652. // Mighty Gorgon: we need to decide if we wish to leave this check upon specific permissions in settings permissions
  653. /*
  654. // Check the permission setting again
  655. if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
  656. {
  657. trigger_error($user->lang['NO_AUTH_OPERATION'] . page_back_link($this->u_action), E_USER_WARNING);
  658. }
  659. */
  660. $auth_settings = (isset($_POST['setting'])) ? $_POST['setting'] : array();
  661. $auth_roles = (isset($_POST['role'])) ? $_POST['role'] : array();
  662. $ug_ids = $forum_ids = array();
  663. // We need to go through the auth settings
  664. foreach ($auth_settings as $ug_id => $forum_auth_row)
  665. {
  666. $ug_id = (int) $ug_id;
  667. $ug_ids[] = $ug_id;
  668. foreach ($forum_auth_row as $forum_id => $auth_options)
  669. {
  670. $forum_id = (int) $forum_id;
  671. $forum_ids[] = $forum_id;
  672. // Check role...
  673. $assigned_role = (isset($auth_roles[$ug_id][$forum_id])) ? (int) $auth_roles[$ug_id][$forum_id] : 0;
  674. // If the auth settings differ from the assigned role, then do not set a role...
  675. if ($assigned_role)
  676. {
  677. if (!$this->check_assigned_role($assigned_role, $auth_options))
  678. {
  679. $assigned_role = 0;
  680. }
  681. }
  682. // Update the permission set...
  683. $auth_admin->acl_set($ug_type, $forum_id, $ug_id, $auth_options, $assigned_role, false);
  684. }
  685. }
  686. $auth_admin->acl_clear_prefetch();
  687. // Do we need to recache the moderator lists?
  688. if ($permission_type == 'm_')
  689. {
  690. cache_moderators();
  691. }
  692. $this->log_action($mode, 'add', $permission_type, $ug_type, $ug_ids, $forum_ids);
  693. $redirect_url = $this->u_action;
  694. meta_refresh(3, $redirect_url);
  695. if (($mode == 'setting_forum_local') || ($mode == 'setting_mod_local'))
  696. {
  697. trigger_error($user->lang['AUTH_UPDATED'] . page_back_link($this->u_action . '&amp;forum_id[]=' . implode('&amp;forum_id[]=', $forum_ids)));
  698. }
  699. else
  700. {
  701. trigger_error($user->lang['AUTH_UPDATED'] . page_back_link($this->u_action));
  702. }
  703. }
  704. /**
  705. * Compare auth settings with auth settings from role
  706. * returns false if they differ, true if they are equal
  707. */
  708. function check_assigned_role($role_id, &$auth_settings)
  709. {
  710. global $db;
  711. $sql = 'SELECT o.auth_option, r.auth_setting
  712. FROM ' . ACL_OPTIONS_TABLE . ' o, ' . ACL_ROLES_DATA_TABLE . ' r
  713. WHERE o.auth_option_id = r.auth_option_id
  714. AND r.role_id = ' . $role_id;
  715. $result = $db->sql_query($sql);
  716. $test_auth_settings = array();
  717. while ($row = $db->sql_fetchrow($result))
  718. {
  719. $test_auth_settings[$row['auth_option']] = $row['auth_setting'];
  720. }
  721. $db->sql_freeresult($result);
  722. // We need to add any ACL_NO setting from auth_settings to compare correctly
  723. foreach ($auth_settings as $option => $setting)
  724. {
  725. if ($setting == ACL_NO)
  726. {
  727. $test_auth_settings[$option] = $setting;
  728. }
  729. }
  730. if (sizeof(array_diff_assoc($auth_settings, $test_auth_settings)))
  731. {
  732. return false;
  733. }
  734. return true;
  735. }
  736. /**
  737. * Remove permissions
  738. */
  739. function remove_permissions($mode, $permission_type, &$auth_admin, &$user_id, &$group_id, &$forum_id)
  740. {
  741. global $db, $user, $auth;
  742. // User or group to be set?
  743. $ug_type = (sizeof($user_id)) ? 'user' : 'group';
  744. // Check the permission setting again
  745. if (!$auth->acl_get('a_' . str_replace('_', '', $permission_type) . 'auth') || !$auth->acl_get('a_auth' . $ug_type . 's'))
  746. {
  747. trigger_error($user->lang['NO_AUTH_OPERATION'] . page_back_link($this->u_action), E_USER_WARNING);
  748. }
  749. $auth_admin->acl_delete($ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : false), $permission_type);
  750. // Do we need to recache the moderator lists?
  751. if ($permission_type == 'm_')
  752. {
  753. cache_moderators();
  754. }
  755. $this->log_action($mode, 'del', $permission_type, $ug_type, (($ug_type == 'user') ? $user_id : $group_id), (sizeof($forum_id) ? $forum_id : array(0 => 0)));
  756. if (($mode == 'setting_forum_local') || ($mode == 'setting_mod_local'))
  757. {
  758. trigger_error($user->lang['AUTH_UPDATED'] . page_back_link($this->u_action . '&amp;forum_id[]=' . implode('&amp;forum_id[]=', $forum_id)));
  759. }
  760. else
  761. {
  762. trigger_error($user->lang['AUTH_UPDATED'] . page_back_link($this->u_action));
  763. }
  764. }
  765. /**
  766. * Log permission changes
  767. */
  768. function log_action($mode, $action, $permission_type, $ug_type, $ug_id, $forum_id)
  769. {
  770. global $db, $user;
  771. if (!is_array($ug_id))
  772. {
  773. $ug_id = array($ug_id);
  774. }
  775. if (!is_array($forum_id))
  776. {
  777. $forum_id = array($forum_id);
  778. }
  779. // Logging ... first grab user or groupnames ...
  780. $sql = ($ug_type == 'group') ? 'SELECT group_name as name, group_type FROM ' . GROUPS_TABLE . ' WHERE ' : 'SELECT username as name FROM ' . USERS_TABLE . ' WHERE ';
  781. $sql .= $db->sql_in_set(($ug_type == 'group') ? 'group_id' : 'user_id', array_map('intval', $ug_id));
  782. $sql .= ($ug_type == 'group') ? ' AND group_single_user = 0' : '';
  783. $result = $db->sql_query($sql);
  784. $l_ug_list = '';
  785. while ($row = $db->sql_fetchrow($result))
  786. {
  787. $l_ug_list .= (($l_ug_list != '') ? ', ' : '') . ((isset($row['group_type']) && $row['group_type'] == GROUP_SPECIAL) ? '<span class="sep">' . $user->lang['G_' . $row['name']] . '</span>' : $row['name']);
  788. }
  789. $db->sql_freeresult($result);
  790. $mode = str_replace('setting_', '', $mode);
  791. add_log('admin', 'LOG_ACL_' . strtoupper($action) . '_' . strtoupper($mode) . '_' . strtoupper($permission_type), $l_ug_list);
  792. }
  793. /**
  794. * Get already assigned users/groups
  795. */
  796. function retrieve_defined_user_groups($permission_scope, $forum_id, $permission_type)
  797. {
  798. global $db, $user;
  799. $sql_forum_id = ($permission_scope == 'global') ? 'AND a.forum_id = 0' : ((sizeof($forum_id)) ? 'AND ' . $db->sql_in_set('a.forum_id', $forum_id) : 'AND a.forum_id <> 0');
  800. // Permission options are only able to be a permission set... therefore we will pre-fetch the possible options and also the possible roles
  801. $option_ids = $role_ids = array();
  802. $sql = 'SELECT auth_option_id
  803. FROM ' . ACL_OPTIONS_TABLE . '
  804. WHERE auth_option ' . $db->sql_like_expression($permission_type . $db->any_char);
  805. $result = $db->sql_query($sql);
  806. while ($row = $db->sql_fetchrow($result))
  807. {
  808. $option_ids[] = (int) $row['auth_option_id'];
  809. }
  810. $db->sql_freeresult($result);
  811. if (sizeof($option_ids))
  812. {
  813. $sql = 'SELECT DISTINCT role_id
  814. FROM ' . ACL_ROLES_DATA_TABLE . '
  815. WHERE ' . $db->sql_in_set('auth_option_id', $option_ids);
  816. $result = $db->sql_query($sql);
  817. while ($row = $db->sql_fetchrow($result))
  818. {
  819. $role_ids[] = (int) $row['role_id'];
  820. }
  821. $db->sql_freeresult($result);
  822. }
  823. if (sizeof($option_ids) && sizeof($role_ids))
  824. {
  825. $sql_where = 'AND (' . $db->sql_in_set('a.auth_option_id', $option_ids) . ' OR ' . $db->sql_in_set('a.auth_role_id', $role_ids) . ')';
  826. }
  827. elseif (sizeof($role_ids))
  828. {
  829. $sql_where = 'AND ' . $db->sql_in_set('a.auth_role_id', $role_ids);
  830. }
  831. elseif (sizeof($option_ids))
  832. {
  833. $sql_where = 'AND ' . $db->sql_in_set('a.auth_option_id', $option_ids);
  834. }
  835. // Not ideal, due to the filesort, non-use of indexes, etc.
  836. $sql = 'SELECT DISTINCT u.user_id, u.username, u.username_clean, u.user_regdate
  837. FROM ' . USERS_TABLE . ' u, ' . ACL_USERS_TABLE . " a
  838. WHERE u.user_id = a.user_id
  839. $sql_forum_id
  840. $sql_where
  841. ORDER BY u.username_clean, u.user_regdate ASC";
  842. $result = $db->sql_query($sql);
  843. $s_defined_user_options = '';
  844. $defined_user_ids = array();
  845. while ($row = $db->sql_fetchrow($result))
  846. {
  847. $s_defined_user_options .= '<option value="' . $row['user_id'] . '">' . $row['username'] . '</option>';
  848. $defined_user_ids[] = $row['user_id'];
  849. }
  850. $db->sql_freeresult($result);
  851. $sql = 'SELECT DISTINCT g.group_type, g.group_name, g.group_id
  852. FROM ' . GROUPS_TABLE . ' g, ' . ACL_GROUPS_TABLE . " a
  853. WHERE g.group_id = a.group_id
  854. AND g.group_single_user = 0
  855. $sql_forum_id
  856. $sql_where
  857. ORDER BY g.group_name ASC";
  858. $result = $db->sql_query($sql);
  859. $s_defined_group_options = '';
  860. $defined_group_ids = array();
  861. while ($row = $db->sql_fetchrow($result))
  862. {
  863. $s_defined_group_options .= '<option value="' . $row['group_id'] . '">' . $row['group_name'] . '</option>';
  864. $defined_group_ids[] = $row['group_id'];
  865. }
  866. $db->sql_freeresult($result);
  867. return array(
  868. 'group_ids' => $defined_group_ids,
  869. 'group_ids_options' => $s_defined_group_options,
  870. 'user_ids' => $defined_user_ids,
  871. 'user_ids_options' => $s_defined_user_options
  872. );
  873. }
  874. }
  875. ?>