PageRenderTime 61ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/wwwroot/phpbb/includes/functions_user.php

https://github.com/spring/spring-website
PHP | 3684 lines | 2601 code | 550 blank | 533 comment | 467 complexity | d2359e1a96343c8c47e34b133b055eeb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, LGPL-3.0, 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. /**
  21. * Obtain user_ids from usernames or vice versa. Returns false on
  22. * success else the error string
  23. *
  24. * @param array &$user_id_ary The user ids to check or empty if usernames used
  25. * @param array &$username_ary The usernames to check or empty if user ids used
  26. * @param mixed $user_type Array of user types to check, false if not restricting by user type
  27. */
  28. function user_get_id_name(&$user_id_ary, &$username_ary, $user_type = false)
  29. {
  30. global $db;
  31. // Are both arrays already filled? Yep, return else
  32. // are neither array filled?
  33. if ($user_id_ary && $username_ary)
  34. {
  35. return false;
  36. }
  37. else if (!$user_id_ary && !$username_ary)
  38. {
  39. return 'NO_USERS';
  40. }
  41. $which_ary = ($user_id_ary) ? 'user_id_ary' : 'username_ary';
  42. if (${$which_ary} && !is_array(${$which_ary}))
  43. {
  44. ${$which_ary} = array(${$which_ary});
  45. }
  46. $sql_in = ($which_ary == 'user_id_ary') ? array_map('intval', ${$which_ary}) : array_map('utf8_clean_string', ${$which_ary});
  47. unset(${$which_ary});
  48. $user_id_ary = $username_ary = array();
  49. // Grab the user id/username records
  50. $sql_where = ($which_ary == 'user_id_ary') ? 'user_id' : 'username_clean';
  51. $sql = 'SELECT user_id, username
  52. FROM ' . USERS_TABLE . '
  53. WHERE ' . $db->sql_in_set($sql_where, $sql_in);
  54. if ($user_type !== false && !empty($user_type))
  55. {
  56. $sql .= ' AND ' . $db->sql_in_set('user_type', $user_type);
  57. }
  58. $result = $db->sql_query($sql);
  59. if (!($row = $db->sql_fetchrow($result)))
  60. {
  61. $db->sql_freeresult($result);
  62. return 'NO_USERS';
  63. }
  64. do
  65. {
  66. $username_ary[$row['user_id']] = $row['username'];
  67. $user_id_ary[] = $row['user_id'];
  68. }
  69. while ($row = $db->sql_fetchrow($result));
  70. $db->sql_freeresult($result);
  71. return false;
  72. }
  73. /**
  74. * Get latest registered username and update database to reflect it
  75. */
  76. function update_last_username()
  77. {
  78. global $db;
  79. // Get latest username
  80. $sql = 'SELECT user_id, username, user_colour
  81. FROM ' . USERS_TABLE . '
  82. WHERE user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')
  83. ORDER BY user_id DESC';
  84. $result = $db->sql_query_limit($sql, 1);
  85. $row = $db->sql_fetchrow($result);
  86. $db->sql_freeresult($result);
  87. if ($row)
  88. {
  89. set_config('newest_user_id', $row['user_id'], true);
  90. set_config('newest_username', $row['username'], true);
  91. set_config('newest_user_colour', $row['user_colour'], true);
  92. }
  93. }
  94. /**
  95. * Updates a username across all relevant tables/fields
  96. *
  97. * @param string $old_name the old/current username
  98. * @param string $new_name the new username
  99. */
  100. function user_update_name($old_name, $new_name)
  101. {
  102. global $config, $db, $cache, $phpbb_dispatcher;
  103. $update_ary = array(
  104. FORUMS_TABLE => array('forum_last_poster_name'),
  105. MODERATOR_CACHE_TABLE => array('username'),
  106. POSTS_TABLE => array('post_username'),
  107. TOPICS_TABLE => array('topic_first_poster_name', 'topic_last_poster_name'),
  108. );
  109. foreach ($update_ary as $table => $field_ary)
  110. {
  111. foreach ($field_ary as $field)
  112. {
  113. $sql = "UPDATE $table
  114. SET $field = '" . $db->sql_escape($new_name) . "'
  115. WHERE $field = '" . $db->sql_escape($old_name) . "'";
  116. $db->sql_query($sql);
  117. }
  118. }
  119. if ($config['newest_username'] == $old_name)
  120. {
  121. set_config('newest_username', $new_name, true);
  122. }
  123. /**
  124. * Update a username when it is changed
  125. *
  126. * @event core.update_username
  127. * @var string old_name The old username that is replaced
  128. * @var string new_name The new username
  129. * @since 3.1.0-a1
  130. */
  131. $vars = array('old_name', 'new_name');
  132. extract($phpbb_dispatcher->trigger_event('core.update_username', compact($vars)));
  133. // Because some tables/caches use username-specific data we need to purge this here.
  134. $cache->destroy('sql', MODERATOR_CACHE_TABLE);
  135. }
  136. /**
  137. * Adds an user
  138. *
  139. * @param mixed $user_row An array containing the following keys (and the appropriate values): username, group_id (the group to place the user in), user_email and the user_type(usually 0). Additional entries not overridden by defaults will be forwarded.
  140. * @param string $cp_data custom profile fields, see custom_profile::build_insert_sql_array
  141. * @param array $notifications_data The notifications settings for the new user
  142. * @return the new user's ID.
  143. */
  144. function user_add($user_row, $cp_data = false, $notifications_data = null)
  145. {
  146. global $db, $user, $auth, $config, $phpbb_root_path, $phpEx;
  147. global $phpbb_dispatcher, $phpbb_container;
  148. if (empty($user_row['username']) || !isset($user_row['group_id']) || !isset($user_row['user_email']) || !isset($user_row['user_type']))
  149. {
  150. return false;
  151. }
  152. $username_clean = utf8_clean_string($user_row['username']);
  153. if (empty($username_clean))
  154. {
  155. return false;
  156. }
  157. $sql_ary = array(
  158. 'username' => $user_row['username'],
  159. 'username_clean' => $username_clean,
  160. 'user_password' => (isset($user_row['user_password'])) ? $user_row['user_password'] : '',
  161. 'user_email' => strtolower($user_row['user_email']),
  162. 'user_email_hash' => phpbb_email_hash($user_row['user_email']),
  163. 'group_id' => $user_row['group_id'],
  164. 'user_type' => $user_row['user_type'],
  165. );
  166. // These are the additional vars able to be specified
  167. $additional_vars = array(
  168. 'user_permissions' => '',
  169. 'user_timezone' => $config['board_timezone'],
  170. 'user_dateformat' => $config['default_dateformat'],
  171. 'user_lang' => $config['default_lang'],
  172. 'user_style' => (int) $config['default_style'],
  173. 'user_actkey' => '',
  174. 'user_ip' => '',
  175. 'user_regdate' => time(),
  176. 'user_passchg' => time(),
  177. 'user_options' => 230271,
  178. // We do not set the new flag here - registration scripts need to specify it
  179. 'user_new' => 0,
  180. 'user_inactive_reason' => 0,
  181. 'user_inactive_time' => 0,
  182. 'user_lastmark' => time(),
  183. 'user_lastvisit' => 0,
  184. 'user_lastpost_time' => 0,
  185. 'user_lastpage' => '',
  186. 'user_posts' => 0,
  187. 'user_colour' => '',
  188. 'user_avatar' => '',
  189. 'user_avatar_type' => '',
  190. 'user_avatar_width' => 0,
  191. 'user_avatar_height' => 0,
  192. 'user_new_privmsg' => 0,
  193. 'user_unread_privmsg' => 0,
  194. 'user_last_privmsg' => 0,
  195. 'user_message_rules' => 0,
  196. 'user_full_folder' => PRIVMSGS_NO_BOX,
  197. 'user_emailtime' => 0,
  198. 'user_notify' => 0,
  199. 'user_notify_pm' => 1,
  200. 'user_notify_type' => NOTIFY_EMAIL,
  201. 'user_allow_pm' => 1,
  202. 'user_allow_viewonline' => 1,
  203. 'user_allow_viewemail' => 1,
  204. 'user_allow_massemail' => 1,
  205. 'user_sig' => '',
  206. 'user_sig_bbcode_uid' => '',
  207. 'user_sig_bbcode_bitfield' => '',
  208. 'user_form_salt' => unique_id(),
  209. );
  210. // Now fill the sql array with not required variables
  211. foreach ($additional_vars as $key => $default_value)
  212. {
  213. $sql_ary[$key] = (isset($user_row[$key])) ? $user_row[$key] : $default_value;
  214. }
  215. // Any additional variables in $user_row not covered above?
  216. $remaining_vars = array_diff(array_keys($user_row), array_keys($sql_ary));
  217. // Now fill our sql array with the remaining vars
  218. if (sizeof($remaining_vars))
  219. {
  220. foreach ($remaining_vars as $key)
  221. {
  222. $sql_ary[$key] = $user_row[$key];
  223. }
  224. }
  225. /**
  226. * Use this event to modify the values to be inserted when a user is added
  227. *
  228. * @event core.user_add_modify_data
  229. * @var array user_row Array of user details submited to user_add
  230. * @var array cp_data Array of Custom profile fields submited to user_add
  231. * @var array sql_ary Array of data to be inserted when a user is added
  232. * @since 3.1.0-a1
  233. * @change 3.1.0-b5
  234. */
  235. $vars = array('user_row', 'cp_data', 'sql_ary');
  236. extract($phpbb_dispatcher->trigger_event('core.user_add_modify_data', compact($vars)));
  237. $sql = 'INSERT INTO ' . USERS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  238. $db->sql_query($sql);
  239. $user_id = $db->sql_nextid();
  240. // Insert Custom Profile Fields
  241. if ($cp_data !== false && sizeof($cp_data))
  242. {
  243. $cp_data['user_id'] = (int) $user_id;
  244. $cp = $phpbb_container->get('profilefields.manager');
  245. $sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' .
  246. $db->sql_build_array('INSERT', $cp->build_insert_sql_array($cp_data));
  247. $db->sql_query($sql);
  248. }
  249. // Place into appropriate group...
  250. $sql = 'INSERT INTO ' . USER_GROUP_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  251. 'user_id' => (int) $user_id,
  252. 'group_id' => (int) $user_row['group_id'],
  253. 'user_pending' => 0)
  254. );
  255. $db->sql_query($sql);
  256. // Now make it the users default group...
  257. group_set_user_default($user_row['group_id'], array($user_id), false);
  258. // Add to newly registered users group if user_new is 1
  259. if ($config['new_member_post_limit'] && $sql_ary['user_new'])
  260. {
  261. $sql = 'SELECT group_id
  262. FROM ' . GROUPS_TABLE . "
  263. WHERE group_name = 'NEWLY_REGISTERED'
  264. AND group_type = " . GROUP_SPECIAL;
  265. $result = $db->sql_query($sql);
  266. $add_group_id = (int) $db->sql_fetchfield('group_id');
  267. $db->sql_freeresult($result);
  268. if ($add_group_id)
  269. {
  270. global $phpbb_log;
  271. // Because these actions only fill the log unneccessarily we skip the add_log() entry.
  272. $phpbb_log->disable('admin');
  273. // Add user to "newly registered users" group and set to default group if admin specified so.
  274. if ($config['new_member_group_default'])
  275. {
  276. group_user_add($add_group_id, $user_id, false, false, true);
  277. $user_row['group_id'] = $add_group_id;
  278. }
  279. else
  280. {
  281. group_user_add($add_group_id, $user_id);
  282. }
  283. $phpbb_log->enable('admin');
  284. }
  285. }
  286. // set the newest user and adjust the user count if the user is a normal user and no activation mail is sent
  287. if ($user_row['user_type'] == USER_NORMAL || $user_row['user_type'] == USER_FOUNDER)
  288. {
  289. set_config('newest_user_id', $user_id, true);
  290. set_config('newest_username', $user_row['username'], true);
  291. set_config_count('num_users', 1, true);
  292. $sql = 'SELECT group_colour
  293. FROM ' . GROUPS_TABLE . '
  294. WHERE group_id = ' . (int) $user_row['group_id'];
  295. $result = $db->sql_query_limit($sql, 1);
  296. $row = $db->sql_fetchrow($result);
  297. $db->sql_freeresult($result);
  298. set_config('newest_user_colour', $row['group_colour'], true);
  299. }
  300. // Use default notifications settings if notifications_data is not set
  301. if ($notifications_data === null)
  302. {
  303. $notifications_data = array(
  304. array(
  305. 'item_type' => 'notification.type.post',
  306. 'method' => 'notification.method.email',
  307. ),
  308. array(
  309. 'item_type' => 'notification.type.topic',
  310. 'method' => 'notification.method.email',
  311. ),
  312. );
  313. }
  314. // Subscribe user to notifications if necessary
  315. if (!empty($notifications_data))
  316. {
  317. $phpbb_notifications = $phpbb_container->get('notification_manager');
  318. foreach ($notifications_data as $subscription)
  319. {
  320. $phpbb_notifications->add_subscription($subscription['item_type'], 0, $subscription['method'], $user_id);
  321. }
  322. }
  323. /**
  324. * Event that returns user id, user detals and user CPF of newly registared user
  325. *
  326. * @event core.user_add_after
  327. * @var int user_id User id of newly registared user
  328. * @var array user_row Array of user details submited to user_add
  329. * @var array cp_data Array of Custom profile fields submited to user_add
  330. * @since 3.1.0-b5
  331. */
  332. $vars = array('user_id', 'user_row', 'cp_data');
  333. extract($phpbb_dispatcher->trigger_event('core.user_add_after', compact($vars)));
  334. return $user_id;
  335. }
  336. /**
  337. * Remove User
  338. *
  339. * @param string $mode Either 'retain' or 'remove'
  340. * @param mixed $user_ids Either an array of integers or an integer
  341. * @param bool $retain_username
  342. * @return bool
  343. */
  344. function user_delete($mode, $user_ids, $retain_username = true)
  345. {
  346. global $cache, $config, $db, $user, $phpbb_dispatcher, $phpbb_container;
  347. global $phpbb_root_path, $phpEx;
  348. $db->sql_transaction('begin');
  349. $user_rows = array();
  350. if (!is_array($user_ids))
  351. {
  352. $user_ids = array($user_ids);
  353. }
  354. $user_id_sql = $db->sql_in_set('user_id', $user_ids);
  355. $sql = 'SELECT *
  356. FROM ' . USERS_TABLE . '
  357. WHERE ' . $user_id_sql;
  358. $result = $db->sql_query($sql);
  359. while ($row = $db->sql_fetchrow($result))
  360. {
  361. $user_rows[(int) $row['user_id']] = $row;
  362. }
  363. $db->sql_freeresult($result);
  364. if (empty($user_rows))
  365. {
  366. return false;
  367. }
  368. /**
  369. * Event before a user is deleted
  370. *
  371. * @event core.delete_user_before
  372. * @var string mode Mode of deletion (retain/delete posts)
  373. * @var array user_ids IDs of the deleted user
  374. * @var mixed retain_username True if username should be retained
  375. * or false if not
  376. * @since 3.1.0-a1
  377. */
  378. $vars = array('mode', 'user_ids', 'retain_username');
  379. extract($phpbb_dispatcher->trigger_event('core.delete_user_before', compact($vars)));
  380. // Before we begin, we will remove the reports the user issued.
  381. $sql = 'SELECT r.post_id, p.topic_id
  382. FROM ' . REPORTS_TABLE . ' r, ' . POSTS_TABLE . ' p
  383. WHERE ' . $db->sql_in_set('r.user_id', $user_ids) . '
  384. AND p.post_id = r.post_id';
  385. $result = $db->sql_query($sql);
  386. $report_posts = $report_topics = array();
  387. while ($row = $db->sql_fetchrow($result))
  388. {
  389. $report_posts[] = $row['post_id'];
  390. $report_topics[] = $row['topic_id'];
  391. }
  392. $db->sql_freeresult($result);
  393. if (sizeof($report_posts))
  394. {
  395. $report_posts = array_unique($report_posts);
  396. $report_topics = array_unique($report_topics);
  397. // Get a list of topics that still contain reported posts
  398. $sql = 'SELECT DISTINCT topic_id
  399. FROM ' . POSTS_TABLE . '
  400. WHERE ' . $db->sql_in_set('topic_id', $report_topics) . '
  401. AND post_reported = 1
  402. AND ' . $db->sql_in_set('post_id', $report_posts, true);
  403. $result = $db->sql_query($sql);
  404. $keep_report_topics = array();
  405. while ($row = $db->sql_fetchrow($result))
  406. {
  407. $keep_report_topics[] = $row['topic_id'];
  408. }
  409. $db->sql_freeresult($result);
  410. if (sizeof($keep_report_topics))
  411. {
  412. $report_topics = array_diff($report_topics, $keep_report_topics);
  413. }
  414. unset($keep_report_topics);
  415. // Now set the flags back
  416. $sql = 'UPDATE ' . POSTS_TABLE . '
  417. SET post_reported = 0
  418. WHERE ' . $db->sql_in_set('post_id', $report_posts);
  419. $db->sql_query($sql);
  420. if (sizeof($report_topics))
  421. {
  422. $sql = 'UPDATE ' . TOPICS_TABLE . '
  423. SET topic_reported = 0
  424. WHERE ' . $db->sql_in_set('topic_id', $report_topics);
  425. $db->sql_query($sql);
  426. }
  427. }
  428. // Remove reports
  429. $db->sql_query('DELETE FROM ' . REPORTS_TABLE . ' WHERE ' . $user_id_sql);
  430. $num_users_delta = 0;
  431. // Get auth provider collection in case accounts might need to be unlinked
  432. $provider_collection = $phpbb_container->get('auth.provider_collection');
  433. // Some things need to be done in the loop (if the query changes based
  434. // on which user is currently being deleted)
  435. $added_guest_posts = 0;
  436. foreach ($user_rows as $user_id => $user_row)
  437. {
  438. if ($user_row['user_avatar'] && $user_row['user_avatar_type'] == 'avatar.driver.upload')
  439. {
  440. avatar_delete('user', $user_row);
  441. }
  442. // Unlink accounts
  443. foreach ($provider_collection as $provider_name => $auth_provider)
  444. {
  445. $provider_data = $auth_provider->get_auth_link_data($user_id);
  446. if ($provider_data !== null)
  447. {
  448. $link_data = array(
  449. 'user_id' => $user_id,
  450. 'link_method' => 'user_delete',
  451. );
  452. // BLOCK_VARS might contain hidden fields necessary for unlinking accounts
  453. if (isset($provider_data['BLOCK_VARS']) && is_array($provider_data['BLOCK_VARS']))
  454. {
  455. foreach ($provider_data['BLOCK_VARS'] as $provider_service)
  456. {
  457. if (!array_key_exists('HIDDEN_FIELDS', $provider_service))
  458. {
  459. $provider_service['HIDDEN_FIELDS'] = array();
  460. }
  461. $auth_provider->unlink_account(array_merge($link_data, $provider_service['HIDDEN_FIELDS']));
  462. }
  463. }
  464. else
  465. {
  466. $auth_provider->unlink_account($link_data);
  467. }
  468. }
  469. }
  470. // Decrement number of users if this user is active
  471. if ($user_row['user_type'] != USER_INACTIVE && $user_row['user_type'] != USER_IGNORE)
  472. {
  473. --$num_users_delta;
  474. }
  475. switch ($mode)
  476. {
  477. case 'retain':
  478. if ($retain_username === false)
  479. {
  480. $post_username = $user->lang['GUEST'];
  481. }
  482. else
  483. {
  484. $post_username = $user_row['username'];
  485. }
  486. // If the user is inactive and newly registered
  487. // we assume no posts from the user, and save
  488. // the queries
  489. if ($user_row['user_type'] != USER_INACTIVE || $user_row['user_inactive_reason'] != INACTIVE_REGISTER || $user_row['user_posts'])
  490. {
  491. // When we delete these users and retain the posts, we must assign all the data to the guest user
  492. $sql = 'UPDATE ' . FORUMS_TABLE . '
  493. SET forum_last_poster_id = ' . ANONYMOUS . ", forum_last_poster_name = '" . $db->sql_escape($post_username) . "', forum_last_poster_colour = ''
  494. WHERE forum_last_poster_id = $user_id";
  495. $db->sql_query($sql);
  496. $sql = 'UPDATE ' . POSTS_TABLE . '
  497. SET poster_id = ' . ANONYMOUS . ", post_username = '" . $db->sql_escape($post_username) . "'
  498. WHERE poster_id = $user_id";
  499. $db->sql_query($sql);
  500. $sql = 'UPDATE ' . TOPICS_TABLE . '
  501. SET topic_poster = ' . ANONYMOUS . ", topic_first_poster_name = '" . $db->sql_escape($post_username) . "', topic_first_poster_colour = ''
  502. WHERE topic_poster = $user_id";
  503. $db->sql_query($sql);
  504. $sql = 'UPDATE ' . TOPICS_TABLE . '
  505. SET topic_last_poster_id = ' . ANONYMOUS . ", topic_last_poster_name = '" . $db->sql_escape($post_username) . "', topic_last_poster_colour = ''
  506. WHERE topic_last_poster_id = $user_id";
  507. $db->sql_query($sql);
  508. // Since we change every post by this author, we need to count this amount towards the anonymous user
  509. if ($user_row['user_posts'])
  510. {
  511. $added_guest_posts += $user_row['user_posts'];
  512. }
  513. }
  514. break;
  515. case 'remove':
  516. // there is nothing variant specific to deleting posts
  517. break;
  518. }
  519. }
  520. if ($num_users_delta != 0)
  521. {
  522. set_config_count('num_users', $num_users_delta, true);
  523. }
  524. // Now do the invariant tasks
  525. // all queries performed in one call of this function are in a single transaction
  526. // so this is kosher
  527. if ($mode == 'retain')
  528. {
  529. // Assign more data to the Anonymous user
  530. $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
  531. SET poster_id = ' . ANONYMOUS . '
  532. WHERE ' . $db->sql_in_set('poster_id', $user_ids);
  533. $db->sql_query($sql);
  534. $sql = 'UPDATE ' . USERS_TABLE . '
  535. SET user_posts = user_posts + ' . $added_guest_posts . '
  536. WHERE user_id = ' . ANONYMOUS;
  537. $db->sql_query($sql);
  538. }
  539. else if ($mode == 'remove')
  540. {
  541. if (!function_exists('delete_posts'))
  542. {
  543. include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  544. }
  545. // Delete posts, attachments, etc.
  546. // delete_posts can handle any number of IDs in its second argument
  547. delete_posts('poster_id', $user_ids);
  548. }
  549. $table_ary = array(USERS_TABLE, USER_GROUP_TABLE, TOPICS_WATCH_TABLE, FORUMS_WATCH_TABLE, ACL_USERS_TABLE, TOPICS_TRACK_TABLE, TOPICS_POSTED_TABLE, FORUMS_TRACK_TABLE, PROFILE_FIELDS_DATA_TABLE, MODERATOR_CACHE_TABLE, DRAFTS_TABLE, BOOKMARKS_TABLE, SESSIONS_KEYS_TABLE, PRIVMSGS_FOLDER_TABLE, PRIVMSGS_RULES_TABLE);
  550. // Delete the miscellaneous (non-post) data for the user
  551. foreach ($table_ary as $table)
  552. {
  553. $sql = "DELETE FROM $table
  554. WHERE " . $user_id_sql;
  555. $db->sql_query($sql);
  556. }
  557. $cache->destroy('sql', MODERATOR_CACHE_TABLE);
  558. // Change user_id to anonymous for posts edited by this user
  559. $sql = 'UPDATE ' . POSTS_TABLE . '
  560. SET post_edit_user = ' . ANONYMOUS . '
  561. WHERE ' . $db->sql_in_set('post_edit_user', $user_ids);
  562. $db->sql_query($sql);
  563. // Change user_id to anonymous for pms edited by this user
  564. $sql = 'UPDATE ' . PRIVMSGS_TABLE . '
  565. SET message_edit_user = ' . ANONYMOUS . '
  566. WHERE ' . $db->sql_in_set('message_edit_user', $user_ids);
  567. $db->sql_query($sql);
  568. // Change user_id to anonymous for posts deleted by this user
  569. $sql = 'UPDATE ' . POSTS_TABLE . '
  570. SET post_delete_user = ' . ANONYMOUS . '
  571. WHERE ' . $db->sql_in_set('post_delete_user', $user_ids);
  572. $db->sql_query($sql);
  573. // Change user_id to anonymous for topics deleted by this user
  574. $sql = 'UPDATE ' . TOPICS_TABLE . '
  575. SET topic_delete_user = ' . ANONYMOUS . '
  576. WHERE ' . $db->sql_in_set('topic_delete_user', $user_ids);
  577. $db->sql_query($sql);
  578. // Delete user log entries about this user
  579. $sql = 'DELETE FROM ' . LOG_TABLE . '
  580. WHERE ' . $db->sql_in_set('reportee_id', $user_ids);
  581. $db->sql_query($sql);
  582. // Change user_id to anonymous for this users triggered events
  583. $sql = 'UPDATE ' . LOG_TABLE . '
  584. SET user_id = ' . ANONYMOUS . '
  585. WHERE ' . $user_id_sql;
  586. $db->sql_query($sql);
  587. // Delete the user_id from the zebra table
  588. $sql = 'DELETE FROM ' . ZEBRA_TABLE . '
  589. WHERE ' . $user_id_sql . '
  590. OR ' . $db->sql_in_set('zebra_id', $user_ids);
  591. $db->sql_query($sql);
  592. // Delete the user_id from the banlist
  593. $sql = 'DELETE FROM ' . BANLIST_TABLE . '
  594. WHERE ' . $db->sql_in_set('ban_userid', $user_ids);
  595. $db->sql_query($sql);
  596. // Delete the user_id from the session table
  597. $sql = 'DELETE FROM ' . SESSIONS_TABLE . '
  598. WHERE ' . $db->sql_in_set('session_user_id', $user_ids);
  599. $db->sql_query($sql);
  600. // Clean the private messages tables from the user
  601. if (!function_exists('phpbb_delete_user_pms'))
  602. {
  603. include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
  604. }
  605. phpbb_delete_users_pms($user_ids);
  606. $phpbb_notifications = $phpbb_container->get('notification_manager');
  607. $phpbb_notifications->delete_notifications('notification.type.admin_activate_user', $user_ids);
  608. $db->sql_transaction('commit');
  609. /**
  610. * Event after a user is deleted
  611. *
  612. * @event core.delete_user_after
  613. * @var string mode Mode of deletion (retain/delete posts)
  614. * @var array user_ids IDs of the deleted user
  615. * @var mixed retain_username True if username should be retained
  616. * or false if not
  617. * @since 3.1.0-a1
  618. */
  619. $vars = array('mode', 'user_ids', 'retain_username');
  620. extract($phpbb_dispatcher->trigger_event('core.delete_user_after', compact($vars)));
  621. // Reset newest user info if appropriate
  622. if (in_array($config['newest_user_id'], $user_ids))
  623. {
  624. update_last_username();
  625. }
  626. return false;
  627. }
  628. /**
  629. * Flips user_type from active to inactive and vice versa, handles group membership updates
  630. *
  631. * @param string $mode can be flip for flipping from active/inactive, activate or deactivate
  632. */
  633. function user_active_flip($mode, $user_id_ary, $reason = INACTIVE_MANUAL)
  634. {
  635. global $config, $db, $user, $auth, $phpbb_dispatcher;
  636. $deactivated = $activated = 0;
  637. $sql_statements = array();
  638. if (!is_array($user_id_ary))
  639. {
  640. $user_id_ary = array($user_id_ary);
  641. }
  642. if (!sizeof($user_id_ary))
  643. {
  644. return;
  645. }
  646. $sql = 'SELECT user_id, group_id, user_type, user_inactive_reason
  647. FROM ' . USERS_TABLE . '
  648. WHERE ' . $db->sql_in_set('user_id', $user_id_ary);
  649. $result = $db->sql_query($sql);
  650. while ($row = $db->sql_fetchrow($result))
  651. {
  652. $sql_ary = array();
  653. if ($row['user_type'] == USER_IGNORE || $row['user_type'] == USER_FOUNDER ||
  654. ($mode == 'activate' && $row['user_type'] != USER_INACTIVE) ||
  655. ($mode == 'deactivate' && $row['user_type'] == USER_INACTIVE))
  656. {
  657. continue;
  658. }
  659. if ($row['user_type'] == USER_INACTIVE)
  660. {
  661. $activated++;
  662. }
  663. else
  664. {
  665. $deactivated++;
  666. // Remove the users session key...
  667. $user->reset_login_keys($row['user_id']);
  668. }
  669. $sql_ary += array(
  670. 'user_type' => ($row['user_type'] == USER_NORMAL) ? USER_INACTIVE : USER_NORMAL,
  671. 'user_inactive_time' => ($row['user_type'] == USER_NORMAL) ? time() : 0,
  672. 'user_inactive_reason' => ($row['user_type'] == USER_NORMAL) ? $reason : 0,
  673. );
  674. $sql_statements[$row['user_id']] = $sql_ary;
  675. }
  676. $db->sql_freeresult($result);
  677. /**
  678. * Check or modify activated/deactivated users data before submitting it to the database
  679. *
  680. * @event core.user_active_flip_before
  681. * @var string mode User type changing mode, can be: flip|activate|deactivate
  682. * @var int reason Reason for changing user type, can be: INACTIVE_REGISTER|INACTIVE_PROFILE|INACTIVE_MANUAL|INACTIVE_REMIND
  683. * @var int activated The number of users to be activated
  684. * @var int deactivated The number of users to be deactivated
  685. * @var array user_id_ary Array with user ids to change user type
  686. * @var array sql_statements Array with users data to submit to the database, keys: user ids, values: arrays with user data
  687. * @since 3.1.4-RC1
  688. */
  689. $vars = array('mode', 'reason', 'activated', 'deactivated', 'user_id_ary', 'sql_statements');
  690. extract($phpbb_dispatcher->trigger_event('core.user_active_flip_before', compact($vars)));
  691. if (sizeof($sql_statements))
  692. {
  693. foreach ($sql_statements as $user_id => $sql_ary)
  694. {
  695. $sql = 'UPDATE ' . USERS_TABLE . '
  696. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  697. WHERE user_id = ' . $user_id;
  698. $db->sql_query($sql);
  699. }
  700. $auth->acl_clear_prefetch(array_keys($sql_statements));
  701. }
  702. /**
  703. * Perform additional actions after the users have been activated/deactivated
  704. *
  705. * @event core.user_active_flip_after
  706. * @var string mode User type changing mode, can be: flip|activate|deactivate
  707. * @var int reason Reason for changing user type, can be: INACTIVE_REGISTER|INACTIVE_PROFILE|INACTIVE_MANUAL|INACTIVE_REMIND
  708. * @var int activated The number of users to be activated
  709. * @var int deactivated The number of users to be deactivated
  710. * @var array user_id_ary Array with user ids to change user type
  711. * @var array sql_statements Array with users data to submit to the database, keys: user ids, values: arrays with user data
  712. * @since 3.1.4-RC1
  713. */
  714. $vars = array('mode', 'reason', 'activated', 'deactivated', 'user_id_ary', 'sql_statements');
  715. extract($phpbb_dispatcher->trigger_event('core.user_active_flip_after', compact($vars)));
  716. if ($deactivated)
  717. {
  718. set_config_count('num_users', $deactivated * (-1), true);
  719. }
  720. if ($activated)
  721. {
  722. set_config_count('num_users', $activated, true);
  723. }
  724. // Update latest username
  725. update_last_username();
  726. }
  727. /**
  728. * Add a ban or ban exclusion to the banlist. Bans either a user, an IP or an email address
  729. *
  730. * @param string $mode Type of ban. One of the following: user, ip, email
  731. * @param mixed $ban Banned entity. Either string or array with usernames, ips or email addresses
  732. * @param int $ban_len Ban length in minutes
  733. * @param string $ban_len_other Ban length as a date (YYYY-MM-DD)
  734. * @param boolean $ban_exclude Exclude these entities from banning?
  735. * @param string $ban_reason String describing the reason for this ban
  736. * @return boolean
  737. */
  738. function user_ban($mode, $ban, $ban_len, $ban_len_other, $ban_exclude, $ban_reason, $ban_give_reason = '')
  739. {
  740. global $db, $user, $auth, $cache;
  741. // Delete stale bans
  742. $sql = 'DELETE FROM ' . BANLIST_TABLE . '
  743. WHERE ban_end < ' . time() . '
  744. AND ban_end <> 0';
  745. $db->sql_query($sql);
  746. $ban_list = (!is_array($ban)) ? array_unique(explode("\n", $ban)) : $ban;
  747. $ban_list_log = implode(', ', $ban_list);
  748. $current_time = time();
  749. // Set $ban_end to the unix time when the ban should end. 0 is a permanent ban.
  750. if ($ban_len)
  751. {
  752. if ($ban_len != -1 || !$ban_len_other)
  753. {
  754. $ban_end = max($current_time, $current_time + ($ban_len) * 60);
  755. }
  756. else
  757. {
  758. $ban_other = explode('-', $ban_len_other);
  759. if (sizeof($ban_other) == 3 && ((int) $ban_other[0] < 9999) &&
  760. (strlen($ban_other[0]) == 4) && (strlen($ban_other[1]) == 2) && (strlen($ban_other[2]) == 2))
  761. {
  762. $ban_end = max($current_time, $user->create_datetime()
  763. ->setDate((int) $ban_other[0], (int) $ban_other[1], (int) $ban_other[2])
  764. ->setTime(0, 0, 0)
  765. ->getTimestamp() + $user->timezone->getOffset(new DateTime('UTC')));
  766. }
  767. else
  768. {
  769. trigger_error('LENGTH_BAN_INVALID', E_USER_WARNING);
  770. }
  771. }
  772. }
  773. else
  774. {
  775. $ban_end = 0;
  776. }
  777. $founder = $founder_names = array();
  778. if (!$ban_exclude)
  779. {
  780. // Create a list of founder...
  781. $sql = 'SELECT user_id, user_email, username_clean
  782. FROM ' . USERS_TABLE . '
  783. WHERE user_type = ' . USER_FOUNDER;
  784. $result = $db->sql_query($sql);
  785. while ($row = $db->sql_fetchrow($result))
  786. {
  787. $founder[$row['user_id']] = $row['user_email'];
  788. $founder_names[$row['user_id']] = $row['username_clean'];
  789. }
  790. $db->sql_freeresult($result);
  791. }
  792. $banlist_ary = array();
  793. switch ($mode)
  794. {
  795. case 'user':
  796. $type = 'ban_userid';
  797. // At the moment we do not support wildcard username banning
  798. // Select the relevant user_ids.
  799. $sql_usernames = array();
  800. foreach ($ban_list as $username)
  801. {
  802. $username = trim($username);
  803. if ($username != '')
  804. {
  805. $clean_name = utf8_clean_string($username);
  806. if ($clean_name == $user->data['username_clean'])
  807. {
  808. trigger_error('CANNOT_BAN_YOURSELF', E_USER_WARNING);
  809. }
  810. if (in_array($clean_name, $founder_names))
  811. {
  812. trigger_error('CANNOT_BAN_FOUNDER', E_USER_WARNING);
  813. }
  814. $sql_usernames[] = $clean_name;
  815. }
  816. }
  817. // Make sure we have been given someone to ban
  818. if (!sizeof($sql_usernames))
  819. {
  820. trigger_error('NO_USER_SPECIFIED', E_USER_WARNING);
  821. }
  822. $sql = 'SELECT user_id
  823. FROM ' . USERS_TABLE . '
  824. WHERE ' . $db->sql_in_set('username_clean', $sql_usernames);
  825. // Do not allow banning yourself, the guest account, or founders.
  826. $non_bannable = array($user->data['user_id'], ANONYMOUS);
  827. if (sizeof($founder))
  828. {
  829. $sql .= ' AND ' . $db->sql_in_set('user_id', array_merge(array_keys($founder), $non_bannable), true);
  830. }
  831. else
  832. {
  833. $sql .= ' AND ' . $db->sql_in_set('user_id', $non_bannable, true);
  834. }
  835. $result = $db->sql_query($sql);
  836. if ($row = $db->sql_fetchrow($result))
  837. {
  838. do
  839. {
  840. $banlist_ary[] = (int) $row['user_id'];
  841. }
  842. while ($row = $db->sql_fetchrow($result));
  843. }
  844. else
  845. {
  846. $db->sql_freeresult($result);
  847. trigger_error('NO_USERS', E_USER_WARNING);
  848. }
  849. $db->sql_freeresult($result);
  850. break;
  851. case 'ip':
  852. $type = 'ban_ip';
  853. foreach ($ban_list as $ban_item)
  854. {
  855. if (preg_match('#^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[ ]*\-[ ]*([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$#', trim($ban_item), $ip_range_explode))
  856. {
  857. // This is an IP range
  858. // Don't ask about all this, just don't ask ... !
  859. $ip_1_counter = $ip_range_explode[1];
  860. $ip_1_end = $ip_range_explode[5];
  861. while ($ip_1_counter <= $ip_1_end)
  862. {
  863. $ip_2_counter = ($ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[2] : 0;
  864. $ip_2_end = ($ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[6];
  865. if ($ip_2_counter == 0 && $ip_2_end == 254)
  866. {
  867. $ip_2_counter = 256;
  868. $ip_2_fragment = 256;
  869. $banlist_ary[] = "$ip_1_counter.*";
  870. }
  871. while ($ip_2_counter <= $ip_2_end)
  872. {
  873. $ip_3_counter = ($ip_2_counter == $ip_range_explode[2] && $ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[3] : 0;
  874. $ip_3_end = ($ip_2_counter < $ip_2_end || $ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[7];
  875. if ($ip_3_counter == 0 && $ip_3_end == 254)
  876. {
  877. $ip_3_counter = 256;
  878. $ip_3_fragment = 256;
  879. $banlist_ary[] = "$ip_1_counter.$ip_2_counter.*";
  880. }
  881. while ($ip_3_counter <= $ip_3_end)
  882. {
  883. $ip_4_counter = ($ip_3_counter == $ip_range_explode[3] && $ip_2_counter == $ip_range_explode[2] && $ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[4] : 0;
  884. $ip_4_end = ($ip_3_counter < $ip_3_end || $ip_2_counter < $ip_2_end) ? 254 : $ip_range_explode[8];
  885. if ($ip_4_counter == 0 && $ip_4_end == 254)
  886. {
  887. $ip_4_counter = 256;
  888. $ip_4_fragment = 256;
  889. $banlist_ary[] = "$ip_1_counter.$ip_2_counter.$ip_3_counter.*";
  890. }
  891. while ($ip_4_counter <= $ip_4_end)
  892. {
  893. $banlist_ary[] = "$ip_1_counter.$ip_2_counter.$ip_3_counter.$ip_4_counter";
  894. $ip_4_counter++;
  895. }
  896. $ip_3_counter++;
  897. }
  898. $ip_2_counter++;
  899. }
  900. $ip_1_counter++;
  901. }
  902. }
  903. else if (preg_match('#^([0-9]{1,3})\.([0-9\*]{1,3})\.([0-9\*]{1,3})\.([0-9\*]{1,3})$#', trim($ban_item)) || preg_match('#^[a-f0-9:]+\*?$#i', trim($ban_item)))
  904. {
  905. // Normal IP address
  906. $banlist_ary[] = trim($ban_item);
  907. }
  908. else if (preg_match('#^\*$#', trim($ban_item)))
  909. {
  910. // Ban all IPs
  911. $banlist_ary[] = '*';
  912. }
  913. else if (preg_match('#^([\w\-_]\.?){2,}$#is', trim($ban_item)))
  914. {
  915. // hostname
  916. $ip_ary = gethostbynamel(trim($ban_item));
  917. if (!empty($ip_ary))
  918. {
  919. foreach ($ip_ary as $ip)
  920. {
  921. if ($ip)
  922. {
  923. if (strlen($ip) > 40)
  924. {
  925. continue;
  926. }
  927. $banlist_ary[] = $ip;
  928. }
  929. }
  930. }
  931. }
  932. if (empty($banlist_ary))
  933. {
  934. trigger_error('NO_IPS_DEFINED', E_USER_WARNING);
  935. }
  936. }
  937. break;
  938. case 'email':
  939. $type = 'ban_email';
  940. foreach ($ban_list as $ban_item)
  941. {
  942. $ban_item = trim($ban_item);
  943. if (preg_match('#^.*?@*|(([a-z0-9\-]+\.)+([a-z]{2,3}))$#i', $ban_item))
  944. {
  945. if (strlen($ban_item) > 100)
  946. {
  947. continue;
  948. }
  949. if (!sizeof($founder) || !in_array($ban_item, $founder))
  950. {
  951. $banlist_ary[] = $ban_item;
  952. }
  953. }
  954. }
  955. if (sizeof($ban_list) == 0)
  956. {
  957. trigger_error('NO_EMAILS_DEFINED', E_USER_WARNING);
  958. }
  959. break;
  960. default:
  961. trigger_error('NO_MODE', E_USER_WARNING);
  962. break;
  963. }
  964. // Fetch currently set bans of the specified type and exclude state. Prevent duplicate bans.
  965. $sql_where = ($type == 'ban_userid') ? 'ban_userid <> 0' : "$type <> ''";
  966. $sql = "SELECT $type
  967. FROM " . BANLIST_TABLE . "
  968. WHERE $sql_where
  969. AND ban_exclude = " . (int) $ban_exclude;
  970. $result = $db->sql_query($sql);
  971. // Reset $sql_where, because we use it later...
  972. $sql_where = '';
  973. if ($row = $db->sql_fetchrow($result))
  974. {
  975. $banlist_ary_tmp = array();
  976. do
  977. {
  978. switch ($mode)
  979. {
  980. case 'user':
  981. $banlist_ary_tmp[] = $row['ban_userid'];
  982. break;
  983. case 'ip':
  984. $banlist_ary_tmp[] = $row['ban_ip'];
  985. break;
  986. case 'email':
  987. $banlist_ary_tmp[] = $row['ban_email'];
  988. break;
  989. }
  990. }
  991. while ($row = $db->sql_fetchrow($result));
  992. $banlist_ary_tmp = array_intersect($banlist_ary, $banlist_ary_tmp);
  993. if (sizeof($banlist_ary_tmp))
  994. {
  995. // One or more entities are already banned/excluded, delete the existing bans, so they can be re-inserted with the given new length
  996. $sql = 'DELETE FROM ' . BANLIST_TABLE . '
  997. WHERE ' . $db->sql_in_set($type, $banlist_ary_tmp) . '
  998. AND ban_exclude = ' . (int) $ban_exclude;
  999. $db->sql_query($sql);
  1000. }
  1001. unset($banlist_ary_tmp);
  1002. }
  1003. $db->sql_freeresult($result);
  1004. // We have some entities to ban
  1005. if (sizeof($banlist_ary))
  1006. {
  1007. $sql_ary = array();
  1008. foreach ($banlist_ary as $ban_entry)
  1009. {
  1010. $sql_ary[] = array(
  1011. $type => $ban_entry,
  1012. 'ban_start' => (int) $current_time,
  1013. 'ban_end' => (int) $ban_end,
  1014. 'ban_exclude' => (int) $ban_exclude,
  1015. 'ban_reason' => (string) $ban_reason,
  1016. 'ban_give_reason' => (string) $ban_give_reason,
  1017. );
  1018. }
  1019. $db->sql_multi_insert(BANLIST_TABLE, $sql_ary);
  1020. // If we are banning we want to logout anyone matching the ban
  1021. if (!$ban_exclude)
  1022. {
  1023. switch ($mode)
  1024. {
  1025. case 'user':
  1026. $sql_where = 'WHERE ' . $db->sql_in_set('session_user_id', $banlist_ary);
  1027. break;
  1028. case 'ip':
  1029. $sql_where = 'WHERE ' . $db->sql_in_set('session_ip', $banlist_ary);
  1030. break;
  1031. case 'email':
  1032. $banlist_ary_sql = array();
  1033. foreach ($banlist_ary as $ban_entry)
  1034. {
  1035. $banlist_ary_sql[] = (string) str_replace('*', '%', $ban_entry);
  1036. }
  1037. $sql = 'SELECT user_id
  1038. FROM ' . USERS_TABLE . '
  1039. WHERE ' . $db->sql_in_set('user_email', $banlist_ary_sql);
  1040. $result = $db->sql_query($sql);
  1041. $sql_in = array();
  1042. if ($row = $db->sql_fetchrow($result))
  1043. {
  1044. do
  1045. {
  1046. $sql_in[] = $row['user_id'];
  1047. }
  1048. while ($row = $db->sql_fetchrow($result));
  1049. $sql_where = 'WHERE ' . $db->sql_in_set('session_user_id', $sql_in);
  1050. }
  1051. $db->sql_freeresult($result);
  1052. break;
  1053. }
  1054. if (isset($sql_where) && $sql_where)
  1055. {
  1056. $sql = 'DELETE FROM ' . SESSIONS_TABLE . "
  1057. $sql_where";
  1058. $db->sql_query($sql);
  1059. if ($mode == 'user')
  1060. {
  1061. $sql = 'DELETE FROM ' . SESSIONS_KEYS_TABLE . ' ' . ((in_array('*', $banlist_ary)) ? '' : 'WHERE ' . $db->sql_in_set('user_id', $banlist_ary));
  1062. $db->sql_query($sql);
  1063. }
  1064. }
  1065. }
  1066. // Update log
  1067. $log_entry = ($ban_exclude) ? 'LOG_BAN_EXCLUDE_' : 'LOG_BAN_';
  1068. // Add to admin log, moderator log and user notes
  1069. add_log('admin', $log_entry . strtoupper($mode), $ban_reason, $ban_list_log);
  1070. add_log('mod', 0, 0, $log_entry . strtoupper($mode), $ban_reason, $ban_list_log);
  1071. if ($mode == 'user')
  1072. {
  1073. foreach ($banlist_ary as $user_id)
  1074. {
  1075. add_log('user', $user_id, $log_entry . strtoupper($mode), $ban_reason, $ban_list_log);
  1076. }
  1077. }
  1078. $cache->destroy('sql', BANLIST_TABLE);
  1079. return true;
  1080. }
  1081. // There was nothing to ban/exclude. But destroying the cache because of the removal of stale bans.
  1082. $cache->destroy('sql', BANLIST_TABLE);
  1083. return false;
  1084. }
  1085. /**
  1086. * Unban User
  1087. */
  1088. function user_unban($mode, $ban)
  1089. {
  1090. global $db, $user, $auth, $cache;
  1091. // Delete stale bans
  1092. $sql = 'DELETE FROM ' . BANLIST_TABLE . '
  1093. WHERE ban_end < ' . time() . '
  1094. AND ban_end <> 0';
  1095. $db->sql_query($sql);
  1096. if (!is_array($ban))
  1097. {
  1098. $ban = array($ban);
  1099. }
  1100. $unban_sql = array_map('intval', $ban);
  1101. if (sizeof($unban_sql))
  1102. {
  1103. // Grab details of bans for logging information later
  1104. switch ($mode)
  1105. {
  1106. case 'user':
  1107. $sql = 'SELECT u.username AS unban_info, u.user_id
  1108. FROM ' . USERS_TABLE . ' u, ' . BANLIST_TABLE . ' b
  1109. WHERE ' . $db->sql_in_set('b.ban_id', $unban_sql) . '
  1110. AND u.user_id = b.ban_userid';
  1111. break;
  1112. case 'email':
  1113. $sql = 'SELECT ban_email AS unban_info
  1114. FROM ' . BANLIST_TABLE . '
  1115. WHERE ' . $db->sql_in_set('ban_id', $unban_sql);
  1116. break;
  1117. case 'ip':
  1118. $sql = 'SELECT ban_ip AS unban_info
  1119. FROM ' . BANLIST_TABLE . '
  1120. WHERE ' . $db->sql_in_set('ban_id', $unban_sql);
  1121. break;
  1122. }
  1123. $result = $db->sql_query($sql);
  1124. $l_unban_list = '';
  1125. $user_ids_ary = array();
  1126. while ($row = $db->sql_fetchrow($result))
  1127. {
  1128. $l_unban_list .= (($l_unban_list != '') ? ', ' : '') . $row['unban_info'];
  1129. if ($mode == 'user')
  1130. {
  1131. $user_ids_ary[] = $row['user_id'];
  1132. }
  1133. }
  1134. $db->sql_freeresult($result);
  1135. $sql = 'DELETE FROM ' . BANLIST_TABLE . '
  1136. WHERE ' . $db->sql_in_set('ban_id', $unban_sql);
  1137. $db->sql_query($sql);
  1138. // Add to moderator log, admin log and user notes
  1139. add_log('admin', 'LOG_UNBAN_' . strtoupper($mode), $l_unban_list);
  1140. add_log('mod', 0, 0, 'LOG_UNBAN_' . strtoupper($mode), $l_unban_list);
  1141. if ($mode == 'user')
  1142. {
  1143. foreach ($user_ids_ary as $user_id)
  1144. {
  1145. add_log('user', $user_id, 'LOG_UNBAN_' . strtoupper($mode), $l_unban_list);
  1146. }
  1147. }
  1148. }
  1149. $cache->destroy('sql', BANLIST_TABLE);
  1150. return false;
  1151. }
  1152. /**
  1153. * Internet Protocol Address Whois
  1154. * RFC3912: WHOIS Protocol Specification
  1155. *
  1156. * @param string $ip Ip address, either IPv4 or IPv6.
  1157. *
  1158. * @return string Empty string if not a valid ip address.
  1159. * Otherwise make_clickable()'ed whois result.
  1160. */
  1161. function user_ipwhois($ip)
  1162. {
  1163. if (empty($ip))
  1164. {
  1165. return '';
  1166. }
  1167. if (preg_match(get_preg_expression('ipv4'), $ip))
  1168. {
  1169. // IPv4 address
  1170. $whois_host = 'whois.arin.net.';
  1171. }
  1172. else if (preg_match(get_preg_expression('ipv6'), $ip))
  1173. {
  1174. // IPv6 address
  1175. $whois_host = 'whois.sixxs.net.';
  1176. }
  1177. else
  1178. {
  1179. return '';
  1180. }
  1181. $ipwhois = '';
  1182. if (($fsk = @fsockopen($whois_host, 43)))
  1183. {
  1184. // CRLF as per RFC3912
  1185. fputs($fsk, "$ip\r\n");
  1186. while (!feof($fsk))
  1187. {
  1188. $ipwhois .= fgets($fsk, 1024);
  1189. }
  1190. @fclose($fsk);
  1191. }
  1192. $match = array();
  1193. // Test for referrals from $whois_host to other whois databases, roll on rwhois
  1194. if (preg_match('#ReferralServer: whois://(.+)#im', $ipwhois, $match))
  1195. {
  1196. if (strpos($match[1], ':') !== false)
  1197. {
  1198. $pos = strrpos($match[1], ':');
  1199. $server = substr($match[1], 0, $pos);
  1200. $port = (int) substr($match[1], $pos + 1);
  1201. unset($pos);
  1202. }
  1203. else
  1204. {
  1205. $server = $match[1];
  1206. $port = 43;
  1207. }
  1208. $buffer = '';
  1209. if (($fsk = @fsockopen($server, $port)))
  1210. {
  1211. fputs($fsk, "$ip\r\n");
  1212. while (!feof($fsk))
  1213. {
  1214. $buffer .= fgets($fsk, 1024);
  1215. }
  1216. @fclose($fsk);
  1217. }
  1218. // Use the result from $whois_host if we don't get any result here
  1219. $ipwhois = (empty($buffer)) ? $ipwhois : $buffer;
  1220. }
  1221. $ipwhois = htmlspecialchars($ipwhois);
  1222. // Magic URL ;)
  1223. return trim(make_clickable($ipwhois, false, ''));
  1224. }
  1225. /**
  1226. * Data validation ... used primarily but not exclusively by ucp modules
  1227. *
  1228. * "Master" function for validating a range of data types
  1229. */
  1230. function validate_data($data, $val_ary)
  1231. {
  1232. global $user;
  1233. $error = array();
  1234. foreach ($val_ary as $var => $val_seq)
  1235. {
  1236. if (!is_array($val_seq[0]))
  1237. {
  1238. $val_seq = array($val_seq);
  1239. }
  1240. foreach ($val_seq as $validate)
  1241. {
  1242. $function = array_shift($validate);
  1243. array_unshift($validate, $data[$var]);
  1244. if (is_array($function))
  1245. {
  1246. $result = call_user_func_array(array($function[0], 'validate_' . $function[1]), $validate);
  1247. }
  1248. else
  1249. {
  1250. $function_prefix = (function_exists('phpbb_validate_' . $function)) ? 'phpbb_validate_' : 'validate_';
  1251. $result = call_user_func_array($function_prefix . $function, $validate);
  1252. }
  1253. if ($result)
  1254. {
  1255. // Since errors are checked later for their language file existence, we need to make sure custom errors are not adjusted.
  1256. $error[] = (empty($user->lang[$result . '_' . strtoupper($var)])) ? $result : $result . '_' . strtoupper($var);
  1257. }
  1258. }
  1259. }
  1260. return $error;
  1261. }
  1262. /**
  1263. * Validate String
  1264. *
  1265. * @return boolean|string Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1266. */
  1267. function validate_string($string, $optional = false, $min = 0, $max = 0)
  1268. {
  1269. if (empty($string) && $optional)
  1270. {
  1271. return false;
  1272. }
  1273. if ($min && utf8_strlen(htmlspecialchars_decode($string)) < $min)
  1274. {
  1275. return 'TOO_SHORT';
  1276. }
  1277. else if ($max && utf8_strlen(htmlspecialchars_decode($string)) > $max)
  1278. {
  1279. return 'TOO_LONG';
  1280. }
  1281. return false;
  1282. }
  1283. /**
  1284. * Validate Number
  1285. *
  1286. * @return boolean|string Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1287. */
  1288. function validate_num($num, $optional = false, $min = 0, $max = 1E99)
  1289. {
  1290. if (empty($num) && $optional)
  1291. {
  1292. return false;
  1293. }
  1294. if ($num < $min)
  1295. {
  1296. return 'TOO_SMALL';
  1297. }
  1298. else if ($num > $max)
  1299. {
  1300. return 'TOO_LARGE';
  1301. }
  1302. return false;
  1303. }
  1304. /**
  1305. * Validate Date
  1306. * @param String $string a date in the dd-mm-yyyy format
  1307. * @return boolean
  1308. */
  1309. function validate_date($date_string, $optional = false)
  1310. {
  1311. $date = explode('-', $date_string);
  1312. if ((empty($date) || sizeof($date) != 3) && $optional)
  1313. {
  1314. return false;
  1315. }
  1316. else if ($optional)
  1317. {
  1318. for ($field = 0; $field <= 1; $field++)
  1319. {
  1320. $date[$field] = (int) $date[$field];
  1321. if (empty($date[$field]))
  1322. {
  1323. $date[$field] = 1;
  1324. }
  1325. }
  1326. $date[2] = (int) $date[2];
  1327. // assume an arbitrary leap year
  1328. if (empty($date[2]))
  1329. {
  1330. $date[2] = 1980;
  1331. }
  1332. }
  1333. if (sizeof($date) != 3 || !checkdate($date[1], $date[0], $date[2]))
  1334. {
  1335. return 'INVALID';
  1336. }
  1337. return false;
  1338. }
  1339. /**
  1340. * Validate Match
  1341. *
  1342. * @return boolean|string Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1343. */
  1344. function validate_match($string, $optional = false, $match = '')
  1345. {
  1346. if (empty($string) && $optional)
  1347. {
  1348. return false;
  1349. }
  1350. if (empty($match))
  1351. {
  1352. return false;
  1353. }
  1354. if (!preg_match($match, $string))
  1355. {
  1356. return 'WRONG_DATA';
  1357. }
  1358. return false;
  1359. }
  1360. /**
  1361. * Validate Language Pack ISO Name
  1362. *
  1363. * Tests whether a language name is valid and installed
  1364. *
  1365. * @param string $lang_iso The language string to test
  1366. *
  1367. * @return bool|string Either false if validation succeeded or
  1368. * a string which will be used as the error message
  1369. * (with the variable name appended)
  1370. */
  1371. function validate_language_iso_name($lang_iso)
  1372. {
  1373. global $db;
  1374. $sql = 'SELECT lang_id
  1375. FROM ' . LANG_TABLE . "
  1376. WHERE lang_iso = '" . $db->sql_escape($lang_iso) . "'";
  1377. $result = $db->sql_query($sql);
  1378. $lang_id = (int) $db->sql_fetchfield('lang_id');
  1379. $db->sql_freeresult($result);
  1380. return ($lang_id) ? false : 'WRONG_DATA';
  1381. }
  1382. /**
  1383. * Validate Timezone Name
  1384. *
  1385. * Tests whether a timezone name is valid
  1386. *
  1387. * @param string $timezone The timezone string to test
  1388. *
  1389. * @return bool|string Either false if validation succeeded or
  1390. * a string which will be used as the error message
  1391. * (with the variable name appended)
  1392. */
  1393. function phpbb_validate_timezone($timezone)
  1394. {
  1395. return (in_array($timezone, phpbb_get_timezone_identifiers($timezone))) ? false : 'TIMEZONE_INVALID';
  1396. }
  1397. /**
  1398. * Check to see if the username has been taken, or if it is disallowed.
  1399. * Also checks if it includes the " character, which we don't allow in usernames.
  1400. * Used for registering, changing names, and posting anonymously with a username
  1401. *
  1402. * @param string $username The username to check
  1403. * @param string $allowed_username An allowed username, default being $user->data['username']
  1404. *
  1405. * @return mixed Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1406. */
  1407. function validate_username($username, $allowed_username = false)
  1408. {
  1409. global $config, $db, $user, $cache;
  1410. $clean_username = utf8_clean_string($username);
  1411. $allowed_username = ($allowed_username === false) ? $user->data['username_clean'] : utf8_clean_string($allowed_username);
  1412. if ($allowed_username == $clean_username)
  1413. {
  1414. return false;
  1415. }
  1416. // ... fast checks first.
  1417. if (strpos($username, '&quot;') !== false || strpos($username, '"') !== false || empty($clean_username))
  1418. {
  1419. return 'INVALID_CHARS';
  1420. }
  1421. $mbstring = $pcre = false;
  1422. // generic UTF-8 character types supported?
  1423. if (phpbb_pcre_utf8_support())
  1424. {
  1425. $pcre = true;
  1426. }
  1427. else if (function_exists('mb_ereg_match'))
  1428. {
  1429. mb_regex_encoding('UTF-8');
  1430. $mbstring = true;
  1431. }
  1432. switch ($config['allow_name_chars'])
  1433. {
  1434. case 'USERNAME_CHARS_ANY':
  1435. $pcre = true;
  1436. $regex = '.+';
  1437. break;
  1438. case 'USERNAME_ALPHA_ONLY':
  1439. $pcre = true;
  1440. $regex = '[A-Za-z0-9]+';
  1441. break;
  1442. case 'USERNAME_ALPHA_SPACERS':
  1443. $pcre = true;
  1444. $regex = '[A-Za-z0-9-[\]_+ ]+';
  1445. break;
  1446. case 'USERNAME_LETTER_NUM':
  1447. if ($pcre)
  1448. {
  1449. $regex = '[\p{Lu}\p{Ll}\p{N}]+';
  1450. }
  1451. else if ($mbstring)
  1452. {
  1453. $regex = '[[:upper:][:lower:][:digit:]]+';
  1454. }
  1455. else
  1456. {
  1457. $pcre = true;
  1458. $regex = '[a-zA-Z0-9]+';
  1459. }
  1460. break;
  1461. case 'USERNAME_LETTER_NUM_SPACERS':
  1462. if ($pcre)
  1463. {
  1464. $regex = '[-\]_+ [\p{Lu}\p{Ll}\p{N}]+';
  1465. }
  1466. else if ($mbstring)
  1467. {
  1468. $regex = '[-\]_+ \[[:upper:][:lower:][:digit:]]+';
  1469. }
  1470. else
  1471. {
  1472. $pcre = true;
  1473. $regex = '[-\]_+ [a-zA-Z0-9]+';
  1474. }
  1475. break;
  1476. case 'USERNAME_ASCII':
  1477. default:
  1478. $pcre = true;
  1479. $regex = '[\x01-\x7F]+';
  1480. break;
  1481. }
  1482. if ($pcre)
  1483. {
  1484. if (!preg_match('#^' . $regex . '$#u', $username))
  1485. {
  1486. return 'INVALID_CHARS';
  1487. }
  1488. }
  1489. else if ($mbstring)
  1490. {
  1491. mb_ereg_search_init($username, '^' . $regex . '$');
  1492. if (!mb_ereg_search())
  1493. {
  1494. return 'INVALID_CHARS';
  1495. }
  1496. }
  1497. $sql = 'SELECT username
  1498. FROM ' . USERS_TABLE . "
  1499. WHERE username_clean = '" . $db->sql_escape($clean_username) . "'";
  1500. $result = $db->sql_query($sql);
  1501. $row = $db->sql_fetchrow($result);
  1502. $db->sql_freeresult($result);
  1503. if ($row)
  1504. {
  1505. return 'USERNAME_TAKEN';
  1506. }
  1507. $sql = 'SELECT group_name
  1508. FROM ' . GROUPS_TABLE . "
  1509. WHERE LOWER(group_name) = '" . $db->sql_escape(utf8_strtolower($username)) . "'";
  1510. $result = $db->sql_query($sql);
  1511. $row = $db->sql_fetchrow($result);
  1512. $db->sql_freeresult($result);
  1513. if ($row)
  1514. {
  1515. return 'USERNAME_TAKEN';
  1516. }
  1517. $bad_usernames = $cache->obtain_disallowed_usernames();
  1518. foreach ($bad_usernames as $bad_username)
  1519. {
  1520. if (preg_match('#^' . $bad_username . '$#', $clean_username))
  1521. {
  1522. return 'USERNAME_DISALLOWED';
  1523. }
  1524. }
  1525. return false;
  1526. }
  1527. /**
  1528. * Check to see if the password meets the complexity settings
  1529. *
  1530. * @return boolean|string Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1531. */
  1532. function validate_password($password)
  1533. {
  1534. global $config;
  1535. if ($password === '' || $config['pass_complex'] === 'PASS_TYPE_ANY')
  1536. {
  1537. // Password empty or no password complexity required.
  1538. return false;
  1539. }
  1540. $pcre = $mbstring = false;
  1541. // generic UTF-8 character types supported?
  1542. if (phpbb_pcre_utf8_support())
  1543. {
  1544. $upp = '\p{Lu}';
  1545. $low = '\p{Ll}';
  1546. $num = '\p{N}';
  1547. $sym = '[^\p{Lu}\p{Ll}\p{N}]';
  1548. $pcre = true;
  1549. }
  1550. else if (function_exists('mb_ereg_match'))
  1551. {
  1552. mb_regex_encoding('UTF-8');
  1553. $upp = '[[:upper:]]';
  1554. $low = '[[:lower:]]';
  1555. $num = '[[:digit:]]';
  1556. $sym = '[^[:upper:][:lower:][:digit:]]';
  1557. $mbstring = true;
  1558. }
  1559. else
  1560. {
  1561. $upp = '[A-Z]';
  1562. $low = '[a-z]';
  1563. $num = '[0-9]';
  1564. $sym = '[^A-Za-z0-9]';
  1565. $pcre = true;
  1566. }
  1567. $chars = array();
  1568. switch ($config['pass_complex'])
  1569. {
  1570. // No break statements below ...
  1571. // We require strong passwords in case pass_complex is not set or is invalid
  1572. default:
  1573. // Require mixed case letters, numbers and symbols
  1574. case 'PASS_TYPE_SYMBOL':
  1575. $chars[] = $sym;
  1576. // Require mixed case letters and numbers
  1577. case 'PASS_TYPE_ALPHA':
  1578. $chars[] = $num;
  1579. // Require mixed case letters
  1580. case 'PASS_TYPE_CASE':
  1581. $chars[] = $low;
  1582. $chars[] = $upp;
  1583. }
  1584. if ($pcre)
  1585. {
  1586. foreach ($chars as $char)
  1587. {
  1588. if (!preg_match('#' . $char . '#u', $password))
  1589. {
  1590. return 'INVALID_CHARS';
  1591. }
  1592. }
  1593. }
  1594. else if ($mbstring)
  1595. {
  1596. foreach ($chars as $char)
  1597. {
  1598. if (mb_ereg($char, $password) === false)
  1599. {
  1600. return 'INVALID_CHARS';
  1601. }
  1602. }
  1603. }
  1604. return false;
  1605. }
  1606. /**
  1607. * Check to see if email address is a valid address and contains a MX record
  1608. *
  1609. * @param string $email The email to check
  1610. *
  1611. * @return mixed Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1612. */
  1613. function phpbb_validate_email($email, $config = null)
  1614. {
  1615. if ($config === null)
  1616. {
  1617. global $config;
  1618. }
  1619. $email = strtolower($email);
  1620. if (!preg_match('/^' . get_preg_expression('email') . '$/i', $email))
  1621. {
  1622. return 'EMAIL_INVALID';
  1623. }
  1624. // Check MX record.
  1625. // The idea for this is from reading the UseBB blog/announcement. :)
  1626. if ($config['email_check_mx'])
  1627. {
  1628. list(, $domain) = explode('@', $email);
  1629. if (phpbb_checkdnsrr($domain, 'A') === false && phpbb_checkdnsrr($domain, 'MX') === false)
  1630. {
  1631. return 'DOMAIN_NO_MX_RECORD';
  1632. }
  1633. }
  1634. return false;
  1635. }
  1636. /**
  1637. * Check to see if email address is banned or already present in the DB
  1638. *
  1639. * @param string $email The email to check
  1640. * @param string $allowed_email An allowed email, default being $user->data['user_email']
  1641. *
  1642. * @return mixed Either false if validation succeeded or a string which will be used as the error message (with the variable name appended)
  1643. */
  1644. function validate_user_email($email, $allowed_email = false)
  1645. {
  1646. global $config, $db, $user;
  1647. $email = strtolower($email);
  1648. $allowed_email = ($allowed_email === false) ? strtolower($user->data['user_email']) : strtolower($allowed_email);
  1649. if ($allowed_email == $email)
  1650. {
  1651. return false;
  1652. }
  1653. $validate_email = phpbb_validate_email($email, $config);
  1654. if ($validate_email)
  1655. {
  1656. return $validate_email;
  1657. }
  1658. if (($ban_reason = $user->check_ban(false, false, $email, true)) !== false)
  1659. {
  1660. return ($ban_reason === true) ? 'EMAIL_BANNED' : $ban_reason;
  1661. }
  1662. if (!$config['allow_emailreuse'])
  1663. {
  1664. $sql = 'SELECT user_email_hash
  1665. FROM ' . USERS_TABLE . "
  1666. WHERE user_email_hash = " . $db->sql_escape(phpbb_email_hash($email));
  1667. $result = $db->sql_query($sql);
  1668. $row = $db->sql_fetchrow($result);
  1669. $db->sql_freeresult($result);
  1670. if ($row)
  1671. {
  1672. return 'EMAIL_TAKEN';
  1673. }
  1674. }
  1675. return false;
  1676. }
  1677. /**
  1678. * Validate jabber address
  1679. * Taken from the jabber class within flyspray (see author notes)
  1680. *
  1681. * @author flyspray.org
  1682. */
  1683. function validate_jabber($jid)
  1684. {
  1685. if (!$jid)
  1686. {
  1687. return false;
  1688. }
  1689. $separator_pos = strpos($jid, '@');
  1690. if ($separator_pos === false)
  1691. {
  1692. return 'WRONG_DATA';
  1693. }
  1694. $username = substr($jid, 0, $separator_pos);
  1695. $realm = substr($jid, $separator_pos + 1);
  1696. if (strlen($username) == 0 || strlen($realm) < 3)
  1697. {
  1698. return 'WRONG_DATA';
  1699. }
  1700. $arr = explode('.', $realm);
  1701. if (sizeof($arr) == 0)
  1702. {
  1703. return 'WRONG_DATA';
  1704. }
  1705. foreach ($arr as $part)
  1706. {
  1707. if (substr($part, 0, 1) == '-' || substr($part, -1, 1) == '-')
  1708. {
  1709. return 'WRONG_DATA';
  1710. }
  1711. if (!preg_match("@^[a-zA-Z0-9-.]+$@", $part))
  1712. {
  1713. return 'WRONG_DATA';
  1714. }
  1715. }
  1716. $boundary = array(array(0, 127), array(192, 223), array(224, 239), array(240, 247), array(248, 251), array(252, 253));
  1717. // Prohibited Characters RFC3454 + RFC3920
  1718. $prohibited = array(
  1719. // Table C.1.1
  1720. array(0x0020, 0x0020), // SPACE
  1721. // Table C.1.2
  1722. array(0x00A0, 0x00A0), // NO-BREAK SPACE
  1723. array(0x1680, 0x1680), // OGHAM SPACE MARK
  1724. array(0x2000, 0x2001), // EN QUAD
  1725. array(0x2001, 0x2001), // EM QUAD
  1726. array(0x2002, 0x2002), // EN SPACE
  1727. array(0x2003, 0x2003), // EM SPACE
  1728. array(0x2004, 0x2004), // THREE-PER-EM SPACE
  1729. array(0x2005, 0x2005), // FOUR-PER-EM SPACE
  1730. array(0x2006, 0x2006), // SIX-PER-EM SPACE
  1731. array(0x2007, 0x2007), // FIGURE SPACE
  1732. array(0x2008, 0x2008), // PUNCTUATION SPACE
  1733. array(0x2009, 0x2009), // THIN SPACE
  1734. array(0x200A, 0x200A), // HAIR SPACE
  1735. array(0x200B, 0x200B), // ZERO WIDTH SPACE
  1736. array(0x202F, 0x202F), // NARROW NO-BREAK SPACE
  1737. array(0x205F, 0x205F), // MEDIUM MATHEMATICAL SPACE
  1738. array(0x3000, 0x3000), // IDEOGRAPHIC SPACE
  1739. // Table C.2.1
  1740. array(0x0000, 0x001F), // [CONTROL CHARACTERS]
  1741. array(0x007F, 0x007F), // DELETE
  1742. // Table C.2.2
  1743. array(0x0080, 0x009F), // [CONTROL CHARACTERS]
  1744. array(0x06DD, 0x06DD), // ARABIC END OF AYAH
  1745. array(0x070F, 0x070F), // SYRIAC ABBREVIATION MARK
  1746. array(0x180E, 0x180E), // MONGOLIAN VOWEL SEPARATOR
  1747. array(0x200C, 0x200C), // ZERO WIDTH NON-JOINER
  1748. array(0x200D, 0x200D), // ZERO WIDTH JOINER
  1749. array(0x2028, 0x2028), // LINE SEPARATOR
  1750. array(0x2029, 0x2029), // PARAGRAPH SEPARATOR
  1751. array(0x2060, 0x2060), // WORD JOINER
  1752. array(0x2061, 0x2061), // FUNCTION APPLICATION
  1753. array(0x2062, 0x2062), // INVISIBLE TIMES
  1754. array(0x2063, 0x2063), // INVISIBLE SEPARATOR
  1755. array(0x206A, 0x206F), // [CONTROL CHARACTERS]
  1756. array(0xFEFF, 0xFEFF), // ZERO WIDTH NO-BREAK SPACE
  1757. array(0xFFF9, 0xFFFC), // [CONTROL CHARACTERS]
  1758. array(0x1D173, 0x1D17A), // [MUSICAL CONTROL CHARACTERS]
  1759. // Table C.3
  1760. array(0xE000, 0xF8FF), // [PRIVATE USE, PLANE 0]
  1761. array(0xF0000, 0xFFFFD), // [PRIVATE USE, PLANE 15]
  1762. array(0x100000, 0x10FFFD), // [PRIVATE USE, PLANE 16]
  1763. // Table C.4
  1764. array(0xFDD0, 0xFDEF), // [NONCHARACTER CODE POINTS]
  1765. array(0xFFFE, 0xFFFF), // [NONCHARACTER CODE POINTS]
  1766. array(0x1FFFE, 0x1FFFF), // [NONCHARACTER CODE POINTS]
  1767. array(0x2FFFE, 0x2FFFF), // [NONCHARACTER CODE POINTS]
  1768. array(0x3FFFE, 0x3FFFF), // [NONCHARACTER CODE POINTS]
  1769. array(0x4FFFE, 0x4FFFF), // [NONCHARACTER CODE POINTS]
  1770. array(0x5FFFE, 0x5FFFF), // [NONCHARACTER CODE POINTS]
  1771. array(0x6FFFE, 0x6FFFF), // [NONCHARACTER CODE POINTS]
  1772. array(0x7FFFE, 0x7FFFF), // [NONCHARACTER CODE POINTS]
  1773. array(0x8FFFE, 0x8FFFF), // [NONCHARACTER CODE POINTS]
  1774. array(0x9FFFE, 0x9FFFF), // [NONCHARACTER CODE POINTS]
  1775. array(0xAFFFE, 0xAFFFF), // [NONCHARACTER CODE POINTS]
  1776. array(0xBFFFE, 0xBFFFF), // [NONCHARACTER CODE POINTS]
  1777. array(0xCFFFE, 0xCFFFF), // [NONCHARACTER CODE POINTS]
  1778. array(0xDFFFE, 0xDFFFF), // [NONCHARACTER CODE POINTS]
  1779. array(0xEFFFE, 0xEFFFF), // [NONCHARACTER CODE POINTS]
  1780. array(0xFFFFE, 0xFFFFF), // [NONCHARACTER CODE POINTS]
  1781. array(0x10FFFE, 0x10FFFF), // [NONCHARACTER CODE POINTS]
  1782. // Table C.5
  1783. array(0xD800, 0xDFFF), // [SURROGATE CODES]
  1784. // Table C.6
  1785. array(0xFFF9, 0xFFF9), // INTERLINEAR ANNOTATION ANCHOR
  1786. array(0xFFFA, 0xFFFA), // INTERLINEAR ANNOTATION SEPARATOR
  1787. array(0xFFFB, 0xFFFB), // INTERLINEAR ANNOTATION TERMINATOR
  1788. array(0xFFFC, 0xFFFC), // OBJECT REPLACEMENT CHARACTER
  1789. array(0xFFFD, 0xFFFD), // REPLACEMENT CHARACTER
  1790. // Table C.7
  1791. array(0x2FF0, 0x2FFB), // [IDEOGRAPHIC DESCRIPTION CHARACTERS]
  1792. // Table C.8
  1793. array(0x0340, 0x0340), // COMBINING GRAVE TONE MARK
  1794. array(0x0341, 0x0341), // COMBINING ACUTE TONE MARK
  1795. array(0x200E, 0x200E), // LEFT-TO-RIGHT MARK
  1796. array(0x200F, 0x200F), // RIGHT-TO-LEFT MARK
  1797. array(0x202A, 0x202A), // LEFT-TO-RIGHT EMBEDDING
  1798. array(0x202B, 0x202B), // RIGHT-TO-LEFT EMBEDDING
  1799. array(0x202C, 0x202C), // POP DIRECTIONAL FORMATTING
  1800. array(0x202D, 0x202D), // LEFT-TO-RIGHT OVERRIDE
  1801. array(0x202E, 0x202E), // RIGHT-TO-LEFT OVERRIDE
  1802. array(0x206A, 0x206A), // INHIBIT SYMMETRIC SWAPPING
  1803. array(0x206B, 0x206B), // ACTIVATE SYMMETRIC SWAPPING
  1804. array(0x206C, 0x206C), // INHIBIT ARABIC FORM SHAPING
  1805. array(0x206D, 0x206D), // ACTIVATE ARABIC FORM SHAPING
  1806. array(0x206E, 0x206E), // NATIONAL DIGIT SHAPES
  1807. array(0x206F, 0x206F), // NOMINAL DIGIT SHAPES
  1808. // Table C.9
  1809. array(0xE0001, 0xE0001), // LANGUAGE TAG
  1810. array(0xE0020, 0xE007F), // [TAGGING CHARACTERS]
  1811. // RFC3920
  1812. array(0x22, 0x22), // "
  1813. array(0x26, 0x26), // &
  1814. array(0x27, 0x27), // '
  1815. array(0x2F, 0x2F), // /
  1816. array(0x3A, 0x3A), // :
  1817. array(0x3C, 0x3C), // <
  1818. array(0x3E, 0x3E), // >
  1819. array(0x40, 0x40) // @
  1820. );
  1821. $pos = 0;
  1822. $result = true;
  1823. while ($pos < strlen($username))
  1824. {
  1825. $len = $uni = 0;
  1826. for ($i = 0; $i <= 5; $i++)
  1827. {
  1828. if (ord($username[$pos]) >= $boundary[$i][0] && ord($username[$pos]) <= $boundary[$i][1])
  1829. {
  1830. $len = $i + 1;
  1831. $uni = (ord($username[$pos]) - $boundary[$i][0]) * pow(2, $i * 6);
  1832. for ($k = 1; $k < $len; $k++)
  1833. {
  1834. $uni += (ord($username[$pos + $k]) - 128) * pow(2, ($i - $k) * 6);
  1835. }
  1836. break;
  1837. }
  1838. }
  1839. if ($len == 0)
  1840. {
  1841. return 'WRONG_DATA';
  1842. }
  1843. foreach ($prohibited as $pval)
  1844. {
  1845. if ($uni >= $pval[0] && $uni <= $pval[1])
  1846. {
  1847. $result = false;
  1848. break 2;
  1849. }
  1850. }
  1851. $pos = $pos + $len;
  1852. }
  1853. if (!$result)
  1854. {
  1855. return 'WRONG_DATA';
  1856. }
  1857. return false;
  1858. }
  1859. /**
  1860. * Validate hex colour value
  1861. *
  1862. * @param string $colour The hex colour value
  1863. * @param bool $optional Whether the colour value is optional. True if an empty
  1864. * string will be accepted as correct input, false if not.
  1865. * @return bool|string Error message if colour value is incorrect, false if it
  1866. * fits the hex colour code
  1867. */
  1868. function phpbb_validate_hex_colour($colour, $optional = false)
  1869. {
  1870. if ($colour === '')
  1871. {
  1872. return (($optional) ? false : 'WRONG_DATA');
  1873. }
  1874. if (!preg_match('/^([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/', $colour))
  1875. {
  1876. return 'WRONG_DATA';
  1877. }
  1878. return false;
  1879. }
  1880. /**
  1881. * Verifies whether a style ID corresponds to an active style.
  1882. *
  1883. * @param int $style_id The style_id of a style which should be checked if activated or not.
  1884. * @return boolean
  1885. */
  1886. function phpbb_style_is_active($style_id)
  1887. {
  1888. global $db;
  1889. $sql = 'SELECT style_active
  1890. FROM ' . STYLES_TABLE . '
  1891. WHERE style_id = '. (int) $style_id;
  1892. $result = $db->sql_query($sql);
  1893. $style_is_active = (bool) $db->sql_fetchfield('style_active');
  1894. $db->sql_freeresult($result);
  1895. return $style_is_active;
  1896. }
  1897. /**
  1898. * Remove avatar
  1899. */
  1900. function avatar_delete($mode, $row, $clean_db = false)
  1901. {
  1902. global $phpbb_root_path, $config, $db, $user;
  1903. // Check if the users avatar is actually *not* a group avatar
  1904. if ($mode == 'user')
  1905. {
  1906. if (strpos($row['user_avatar'], 'g') === 0 || (((int) $row['user_avatar'] !== 0) && ((int) $row['user_avatar'] !== (int) $row['user_id'])))
  1907. {
  1908. return false;
  1909. }
  1910. }
  1911. if ($clean_db)
  1912. {
  1913. avatar_remove_db($row[$mode . '_avatar']);
  1914. }
  1915. $filename = get_avatar_filename($row[$mode . '_avatar']);
  1916. if (file_exists($phpbb_root_path . $config['avatar_path'] . '/' . $filename))
  1917. {
  1918. @unlink($phpbb_root_path . $config['avatar_path'] . '/' . $filename);
  1919. return true;
  1920. }
  1921. return false;
  1922. }
  1923. /**
  1924. * Generates avatar filename from the database entry
  1925. */
  1926. function get_avatar_filename($avatar_entry)
  1927. {
  1928. global $config;
  1929. if ($avatar_entry[0] === 'g')
  1930. {
  1931. $avatar_group = true;
  1932. $avatar_entry = substr($avatar_entry, 1);
  1933. }
  1934. else
  1935. {
  1936. $avatar_group = false;
  1937. }
  1938. $ext = substr(strrchr($avatar_entry, '.'), 1);
  1939. $avatar_entry = intval($avatar_entry);
  1940. return $config['avatar_salt'] . '_' . (($avatar_group) ? 'g' : '') . $avatar_entry . '.' . $ext;
  1941. }
  1942. /**
  1943. * Returns an explanation string with maximum avatar settings
  1944. *
  1945. * @return string
  1946. */
  1947. function phpbb_avatar_explanation_string()
  1948. {
  1949. global $config, $user;
  1950. return $user->lang('AVATAR_EXPLAIN',
  1951. $user->lang('PIXELS', (int) $config['avatar_max_width']),
  1952. $user->lang('PIXELS', (int) $config['avatar_max_height']),
  1953. round($config['avatar_filesize'] / 1024));
  1954. }
  1955. //
  1956. // Usergroup functions
  1957. //
  1958. /**
  1959. * Add or edit a group. If we're editing a group we only update user
  1960. * parameters such as rank, etc. if they are changed
  1961. */
  1962. function group_create(&$group_id, $type, $name, $desc, $group_attributes, $allow_desc_bbcode = false, $allow_desc_urls = false, $allow_desc_smilies = false)
  1963. {
  1964. global $phpbb_root_path, $config, $db, $user, $file_upload, $phpbb_container;
  1965. $error = array();
  1966. // Attributes which also affect the users table
  1967. $user_attribute_ary = array('group_colour', 'group_rank', 'group_avatar', 'group_avatar_type', 'group_avatar_width', 'group_avatar_height');
  1968. // Check data. Limit group name length.
  1969. if (!utf8_strlen($name) || utf8_strlen($name) > 60)
  1970. {
  1971. $error[] = (!utf8_strlen($name)) ? $user->lang['GROUP_ERR_USERNAME'] : $user->lang['GROUP_ERR_USER_LONG'];
  1972. }
  1973. $err = group_validate_groupname($group_id, $name);
  1974. if (!empty($err))
  1975. {
  1976. $error[] = $user->lang[$err];
  1977. }
  1978. if (!in_array($type, array(GROUP_OPEN, GROUP_CLOSED, GROUP_HIDDEN, GROUP_SPECIAL, GROUP_FREE)))
  1979. {
  1980. $error[] = $user->lang['GROUP_ERR_TYPE'];
  1981. }
  1982. $group_teampage = !empty($group_attributes['group_teampage']);
  1983. unset($group_attributes['group_teampage']);
  1984. if (!sizeof($error))
  1985. {
  1986. $current_legend = \phpbb\groupposition\legend::GROUP_DISABLED;
  1987. $current_teampage = \phpbb\groupposition\teampage::GROUP_DISABLED;
  1988. $legend = $phpbb_container->get('groupposition.legend');
  1989. $teampage = $phpbb_container->get('groupposition.teampage');
  1990. if ($group_id)
  1991. {
  1992. try
  1993. {
  1994. $current_legend = $legend->get_group_value($group_id);
  1995. $current_teampage = $teampage->get_group_value($group_id);
  1996. }
  1997. catch (\phpbb\groupposition\exception $exception)
  1998. {
  1999. trigger_error($user->lang($exception->getMessage()));
  2000. }
  2001. }
  2002. if (!empty($group_attributes['group_legend']))
  2003. {
  2004. if (($group_id && ($current_legend == \phpbb\groupposition\legend::GROUP_DISABLED)) || !$group_id)
  2005. {
  2006. // Old group currently not in the legend or new group, add at the end.
  2007. $group_attributes['group_legend'] = 1 + $legend->get_group_count();
  2008. }
  2009. else
  2010. {
  2011. // Group stayes in the legend
  2012. $group_attributes['group_legend'] = $current_legend;
  2013. }
  2014. }
  2015. else if ($group_id && ($current_legend != \phpbb\groupposition\legend::GROUP_DISABLED))
  2016. {
  2017. // Group is removed from the legend
  2018. try
  2019. {
  2020. $legend->delete_group($group_id, true);
  2021. }
  2022. catch (\phpbb\groupposition\exception $exception)
  2023. {
  2024. trigger_error($user->lang($exception->getMessage()));
  2025. }
  2026. $group_attributes['group_legend'] = \phpbb\groupposition\legend::GROUP_DISABLED;
  2027. }
  2028. else
  2029. {
  2030. $group_attributes['group_legend'] = \phpbb\groupposition\legend::GROUP_DISABLED;
  2031. }
  2032. // Unset the objects, we don't need them anymore.
  2033. unset($legend);
  2034. $user_ary = array();
  2035. $sql_ary = array(
  2036. 'group_name' => (string) $name,
  2037. 'group_desc' => (string) $desc,
  2038. 'group_desc_uid' => '',
  2039. 'group_desc_bitfield' => '',
  2040. 'group_type' => (int) $type,
  2041. );
  2042. // Parse description
  2043. if ($desc)
  2044. {
  2045. generate_text_for_storage($sql_ary['group_desc'], $sql_ary['group_desc_uid'], $sql_ary['group_desc_bitfield'], $sql_ary['group_desc_options'], $allow_desc_bbcode, $allow_desc_urls, $allow_desc_smilies);
  2046. }
  2047. if (sizeof($group_attributes))
  2048. {
  2049. // Merge them with $sql_ary to properly update the group
  2050. $sql_ary = array_merge($sql_ary, $group_attributes);
  2051. }
  2052. // Setting the log message before we set the group id (if group gets added)
  2053. $log = ($group_id) ? 'LOG_GROUP_UPDATED' : 'LOG_GROUP_CREATED';
  2054. $query = '';
  2055. if ($group_id)
  2056. {
  2057. $sql = 'SELECT user_id
  2058. FROM ' . USERS_TABLE . '
  2059. WHERE group_id = ' . $group_id;
  2060. $result = $db->sql_query($sql);
  2061. while ($row = $db->sql_fetchrow($result))
  2062. {
  2063. $user_ary[] = $row['user_id'];
  2064. }
  2065. $db->sql_freeresult($result);
  2066. if (isset($sql_ary['group_avatar']))
  2067. {
  2068. remove_default_avatar($group_id, $user_ary);
  2069. }
  2070. if (isset($sql_ary['group_rank']))
  2071. {
  2072. remove_default_rank($group_id, $user_ary);
  2073. }
  2074. $sql = 'UPDATE ' . GROUPS_TABLE . '
  2075. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
  2076. WHERE group_id = $group_id";
  2077. $db->sql_query($sql);
  2078. // Since we may update the name too, we need to do this on other tables too...
  2079. $sql = 'UPDATE ' . MODERATOR_CACHE_TABLE . "
  2080. SET group_name = '" . $db->sql_escape($sql_ary['group_name']) . "'
  2081. WHERE group_id = $group_id";
  2082. $db->sql_query($sql);
  2083. // One special case is the group skip auth setting. If this was changed we need to purge permissions for this group
  2084. if (isset($group_attributes['group_skip_auth']))
  2085. {
  2086. // Get users within this group...
  2087. $sql = 'SELECT user_id
  2088. FROM ' . USER_GROUP_TABLE . '
  2089. WHERE group_id = ' . $group_id . '
  2090. AND user_pending = 0';
  2091. $result = $db->sql_query($sql);
  2092. $user_id_ary = array();
  2093. while ($row = $db->sql_fetchrow($result))
  2094. {
  2095. $user_id_ary[] = $row['user_id'];
  2096. }
  2097. $db->sql_freeresult($result);
  2098. if (!empty($user_id_ary))
  2099. {
  2100. global $auth;
  2101. // Clear permissions cache of relevant users
  2102. $auth->acl_clear_prefetch($user_id_ary);
  2103. }
  2104. }
  2105. }
  2106. else
  2107. {
  2108. $sql = 'INSERT INTO ' . GROUPS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  2109. $db->sql_query($sql);
  2110. }
  2111. // Remove the group from the teampage, only if unselected and we are editing a group,
  2112. // which is currently displayed.
  2113. if (!$group_teampage && $group_id && $current_teampage != \phpbb\groupposition\teampage::GROUP_DISABLED)
  2114. {
  2115. try
  2116. {
  2117. $teampage->delete_group($group_id);
  2118. }
  2119. catch (\phpbb\groupposition\exception $exception)
  2120. {
  2121. trigger_error($user->lang($exception->getMessage()));
  2122. }
  2123. }
  2124. if (!$group_id)
  2125. {
  2126. $group_id = $db->sql_nextid();
  2127. if (isset($sql_ary['group_avatar_type']) && $sql_ary['group_avatar_type'] == 'avatar.driver.upload')
  2128. {
  2129. group_correct_avatar($group_id, $sql_ary['group_avatar']);
  2130. }
  2131. }
  2132. try
  2133. {
  2134. if ($group_teampage && $current_teampage == \phpbb\groupposition\teampage::GROUP_DISABLED)
  2135. {
  2136. $teampage->add_group($group_id);
  2137. }
  2138. if ($group_teampage)
  2139. {
  2140. if ($current_teampage == \phpbb\groupposition\teampage::GROUP_DISABLED)
  2141. {
  2142. $teampage->add_group($group_id);
  2143. }
  2144. }
  2145. else if ($group_id && ($current_teampage != \phpbb\groupposition\teampage::GROUP_DISABLED))
  2146. {
  2147. $teampage->delete_group($group_id);
  2148. }
  2149. }
  2150. catch (\phpbb\groupposition\exception $exception)
  2151. {
  2152. trigger_error($user->lang($exception->getMessage()));
  2153. }
  2154. unset($teampage);
  2155. // Set user attributes
  2156. $sql_ary = array();
  2157. if (sizeof($group_attributes))
  2158. {
  2159. // Go through the user attributes array, check if a group attribute matches it and then set it. ;)
  2160. foreach ($user_attribute_ary as $attribute)
  2161. {
  2162. if (!isset($group_attributes[$attribute]))
  2163. {
  2164. continue;
  2165. }
  2166. // If we are about to set an avatar, we will not overwrite user avatars if no group avatar is set...
  2167. if (strpos($attribute, 'group_avatar') === 0 && !$group_attributes[$attribute])
  2168. {
  2169. continue;
  2170. }
  2171. $sql_ary[$attribute] = $group_attributes[$attribute];
  2172. }
  2173. }
  2174. if (sizeof($sql_ary) && sizeof($user_ary))
  2175. {
  2176. group_set_user_default($group_id, $user_ary, $sql_ary);
  2177. }
  2178. $name = ($type == GROUP_SPECIAL) ? $user->lang['G_' . $name] : $name;
  2179. add_log('admin', $log, $name);
  2180. group_update_listings($group_id);
  2181. }
  2182. return (sizeof($error)) ? $error : false;
  2183. }
  2184. /**
  2185. * Changes a group avatar's filename to conform to the naming scheme
  2186. */
  2187. function group_correct_avatar($group_id, $old_entry)
  2188. {
  2189. global $config, $db, $phpbb_root_path;
  2190. $group_id = (int) $group_id;
  2191. $ext = substr(strrchr($old_entry, '.'), 1);
  2192. $old_filename = get_avatar_filename($old_entry);
  2193. $new_filename = $config['avatar_salt'] . "_g$group_id.$ext";
  2194. $new_entry = 'g' . $group_id . '_' . substr(time(), -5) . ".$ext";
  2195. $avatar_path = $phpbb_root_path . $config['avatar_path'];
  2196. if (@rename($avatar_path . '/'. $old_filename, $avatar_path . '/' . $new_filename))
  2197. {
  2198. $sql = 'UPDATE ' . GROUPS_TABLE . '
  2199. SET group_avatar = \'' . $db->sql_escape($new_entry) . "'
  2200. WHERE group_id = $group_id";
  2201. $db->sql_query($sql);
  2202. }
  2203. }
  2204. /**
  2205. * Remove avatar also for users not having the group as default
  2206. */
  2207. function avatar_remove_db($avatar_name)
  2208. {
  2209. global $config, $db;
  2210. $sql = 'UPDATE ' . USERS_TABLE . "
  2211. SET user_avatar = '',
  2212. user_avatar_type = ''
  2213. WHERE user_avatar = '" . $db->sql_escape($avatar_name) . '\'';
  2214. $db->sql_query($sql);
  2215. }
  2216. /**
  2217. * Group Delete
  2218. */
  2219. function group_delete($group_id, $group_name = false)
  2220. {
  2221. global $db, $cache, $auth, $user, $phpbb_root_path, $phpEx, $phpbb_dispatcher, $phpbb_container;
  2222. if (!$group_name)
  2223. {
  2224. $group_name = get_group_name($group_id);
  2225. }
  2226. $start = 0;
  2227. do
  2228. {
  2229. $user_id_ary = $username_ary = array();
  2230. // Batch query for group members, call group_user_del
  2231. $sql = 'SELECT u.user_id, u.username
  2232. FROM ' . USER_GROUP_TABLE . ' ug, ' . USERS_TABLE . " u
  2233. WHERE ug.group_id = $group_id
  2234. AND u.user_id = ug.user_id";
  2235. $result = $db->sql_query_limit($sql, 200, $start);
  2236. if ($row = $db->sql_fetchrow($result))
  2237. {
  2238. do
  2239. {
  2240. $user_id_ary[] = $row['user_id'];
  2241. $username_ary[] = $row['username'];
  2242. $start++;
  2243. }
  2244. while ($row = $db->sql_fetchrow($result));
  2245. group_user_del($group_id, $user_id_ary, $username_ary, $group_name);
  2246. }
  2247. else
  2248. {
  2249. $start = 0;
  2250. }
  2251. $db->sql_freeresult($result);
  2252. }
  2253. while ($start);
  2254. // Delete group from legend and teampage
  2255. try
  2256. {
  2257. $legend = $phpbb_container->get('groupposition.legend');
  2258. $legend->delete_group($group_id);
  2259. unset($legend);
  2260. }
  2261. catch (\phpbb\groupposition\exception $exception)
  2262. {
  2263. // The group we want to delete does not exist.
  2264. // No reason to worry, we just continue the deleting process.
  2265. //trigger_error($user->lang($exception->getMessage()));
  2266. }
  2267. try
  2268. {
  2269. $teampage = $phpbb_container->get('groupposition.teampage');
  2270. $teampage->delete_group($group_id);
  2271. unset($teampage);
  2272. }
  2273. catch (\phpbb\groupposition\exception $exception)
  2274. {
  2275. // The group we want to delete does not exist.
  2276. // No reason to worry, we just continue the deleting process.
  2277. //trigger_error($user->lang($exception->getMessage()));
  2278. }
  2279. // Delete group
  2280. $sql = 'DELETE FROM ' . GROUPS_TABLE . "
  2281. WHERE group_id = $group_id";
  2282. $db->sql_query($sql);
  2283. // Delete auth entries from the groups table
  2284. $sql = 'DELETE FROM ' . ACL_GROUPS_TABLE . "
  2285. WHERE group_id = $group_id";
  2286. $db->sql_query($sql);
  2287. /**
  2288. * Event after a group is deleted
  2289. *
  2290. * @event core.delete_group_after
  2291. * @var int group_id ID of the deleted group
  2292. * @var string group_name Name of the deleted group
  2293. * @since 3.1.0-a1
  2294. */
  2295. $vars = array('group_id', 'group_name');
  2296. extract($phpbb_dispatcher->trigger_event('core.delete_group_after', compact($vars)));
  2297. // Re-cache moderators
  2298. if (!function_exists('phpbb_cache_moderators'))
  2299. {
  2300. include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  2301. }
  2302. phpbb_cache_moderators($db, $cache, $auth);
  2303. add_log('admin', 'LOG_GROUP_DELETE', $group_name);
  2304. // Return false - no error
  2305. return false;
  2306. }
  2307. /**
  2308. * Add user(s) to group
  2309. *
  2310. * @return mixed false if no errors occurred, else the user lang string for the relevant error, for example 'NO_USER'
  2311. */
  2312. function group_user_add($group_id, $user_id_ary = false, $username_ary = false, $group_name = false, $default = false, $leader = 0, $pending = 0, $group_attributes = false)
  2313. {
  2314. global $db, $auth, $phpbb_container;
  2315. // We need both username and user_id info
  2316. $result = user_get_id_name($user_id_ary, $username_ary);
  2317. if (!sizeof($user_id_ary) || $result !== false)
  2318. {
  2319. return 'NO_USER';
  2320. }
  2321. // Remove users who are already members of this group
  2322. $sql = 'SELECT user_id, group_leader
  2323. FROM ' . USER_GROUP_TABLE . '
  2324. WHERE ' . $db->sql_in_set('user_id', $user_id_ary) . "
  2325. AND group_id = $group_id";
  2326. $result = $db->sql_query($sql);
  2327. $add_id_ary = $update_id_ary = array();
  2328. while ($row = $db->sql_fetchrow($result))
  2329. {
  2330. $add_id_ary[] = (int) $row['user_id'];
  2331. if ($leader && !$row['group_leader'])
  2332. {
  2333. $update_id_ary[] = (int) $row['user_id'];
  2334. }
  2335. }
  2336. $db->sql_freeresult($result);
  2337. // Do all the users exist in this group?
  2338. $add_id_ary = array_diff($user_id_ary, $add_id_ary);
  2339. // If we have no users
  2340. if (!sizeof($add_id_ary) && !sizeof($update_id_ary))
  2341. {
  2342. return 'GROUP_USERS_EXIST';
  2343. }
  2344. $db->sql_transaction('begin');
  2345. // Insert the new users
  2346. if (sizeof($add_id_ary))
  2347. {
  2348. $sql_ary = array();
  2349. foreach ($add_id_ary as $user_id)
  2350. {
  2351. $sql_ary[] = array(
  2352. 'user_id' => (int) $user_id,
  2353. 'group_id' => (int) $group_id,
  2354. 'group_leader' => (int) $leader,
  2355. 'user_pending' => (int) $pending,
  2356. );
  2357. }
  2358. $db->sql_multi_insert(USER_GROUP_TABLE, $sql_ary);
  2359. }
  2360. if (sizeof($update_id_ary))
  2361. {
  2362. $sql = 'UPDATE ' . USER_GROUP_TABLE . '
  2363. SET group_leader = 1
  2364. WHERE ' . $db->sql_in_set('user_id', $update_id_ary) . "
  2365. AND group_id = $group_id";
  2366. $db->sql_query($sql);
  2367. }
  2368. if ($default)
  2369. {
  2370. group_user_attributes('default', $group_id, $user_id_ary, false, $group_name, $group_attributes);
  2371. }
  2372. $db->sql_transaction('commit');
  2373. // Clear permissions cache of relevant users
  2374. $auth->acl_clear_prefetch($user_id_ary);
  2375. if (!$group_name)
  2376. {
  2377. $group_name = get_group_name($group_id);
  2378. }
  2379. $log = ($leader) ? 'LOG_MODS_ADDED' : (($pending) ? 'LOG_USERS_PENDING' : 'LOG_USERS_ADDED');
  2380. add_log('admin', $log, $group_name, implode(', ', $username_ary));
  2381. group_update_listings($group_id);
  2382. if ($pending)
  2383. {
  2384. $phpbb_notifications = $phpbb_container->get('notification_manager');
  2385. foreach ($add_id_ary as $user_id)
  2386. {
  2387. $phpbb_notifications->add_notifications('notification.type.group_request', array(
  2388. 'group_id' => $group_id,
  2389. 'user_id' => $user_id,
  2390. 'group_name' => $group_name,
  2391. ));
  2392. }
  2393. }
  2394. // Return false - no error
  2395. return false;
  2396. }
  2397. /**
  2398. * Remove a user/s from a given group. When we remove users we update their
  2399. * default group_id. We do this by examining which "special" groups they belong
  2400. * to. The selection is made based on a reasonable priority system
  2401. *
  2402. * @return false if no errors occurred, else the user lang string for the relevant error, for example 'NO_USER'
  2403. */
  2404. function group_user_del($group_id, $user_id_ary = false, $username_ary = false, $group_name = false)
  2405. {
  2406. global $db, $auth, $config, $phpbb_dispatcher, $phpbb_container;
  2407. if ($config['coppa_enable'])
  2408. {
  2409. $group_order = array('ADMINISTRATORS', 'GLOBAL_MODERATORS', 'NEWLY_REGISTERED', 'REGISTERED_COPPA', 'REGISTERED', 'BOTS', 'GUESTS');
  2410. }
  2411. else
  2412. {
  2413. $group_order = array('ADMINISTRATORS', 'GLOBAL_MODERATORS', 'NEWLY_REGISTERED', 'REGISTERED', 'BOTS', 'GUESTS');
  2414. }
  2415. // We need both username and user_id info
  2416. $result = user_get_id_name($user_id_ary, $username_ary);
  2417. if (!sizeof($user_id_ary) || $result !== false)
  2418. {
  2419. return 'NO_USER';
  2420. }
  2421. $sql = 'SELECT *
  2422. FROM ' . GROUPS_TABLE . '
  2423. WHERE ' . $db->sql_in_set('group_name', $group_order);
  2424. $result = $db->sql_query($sql);
  2425. $group_order_id = $special_group_data = array();
  2426. while ($row = $db->sql_fetchrow($result))
  2427. {
  2428. $group_order_id[$row['group_name']] = $row['group_id'];
  2429. $special_group_data[$row['group_id']] = array(
  2430. 'group_colour' => $row['group_colour'],
  2431. 'group_rank' => $row['group_rank'],
  2432. );
  2433. // Only set the group avatar if one is defined...
  2434. if ($row['group_avatar'])
  2435. {
  2436. $special_group_data[$row['group_id']] = array_merge($special_group_data[$row['group_id']], array(
  2437. 'group_avatar' => $row['group_avatar'],
  2438. 'group_avatar_type' => $row['group_avatar_type'],
  2439. 'group_avatar_width' => $row['group_avatar_width'],
  2440. 'group_avatar_height' => $row['group_avatar_height'])
  2441. );
  2442. }
  2443. }
  2444. $db->sql_freeresult($result);
  2445. // Get users default groups - we only need to reset default group membership if the group from which the user gets removed is set as default
  2446. $sql = 'SELECT user_id, group_id
  2447. FROM ' . USERS_TABLE . '
  2448. WHERE ' . $db->sql_in_set('user_id', $user_id_ary);
  2449. $result = $db->sql_query($sql);
  2450. $default_groups = array();
  2451. while ($row = $db->sql_fetchrow($result))
  2452. {
  2453. $default_groups[$row['user_id']] = $row['group_id'];
  2454. }
  2455. $db->sql_freeresult($result);
  2456. // What special group memberships exist for these users?
  2457. $sql = 'SELECT g.group_id, g.group_name, ug.user_id
  2458. FROM ' . USER_GROUP_TABLE . ' ug, ' . GROUPS_TABLE . ' g
  2459. WHERE ' . $db->sql_in_set('ug.user_id', $user_id_ary) . "
  2460. AND g.group_id = ug.group_id
  2461. AND g.group_id <> $group_id
  2462. AND g.group_type = " . GROUP_SPECIAL . '
  2463. ORDER BY ug.user_id, g.group_id';
  2464. $result = $db->sql_query($sql);
  2465. $temp_ary = array();
  2466. while ($row = $db->sql_fetchrow($result))
  2467. {
  2468. if ($default_groups[$row['user_id']] == $group_id && (!isset($temp_ary[$row['user_id']]) || $group_order_id[$row['group_name']] < $temp_ary[$row['user_id']]))
  2469. {
  2470. $temp_ary[$row['user_id']] = $row['group_id'];
  2471. }
  2472. }
  2473. $db->sql_freeresult($result);
  2474. // sql_where_ary holds the new default groups and their users
  2475. $sql_where_ary = array();
  2476. foreach ($temp_ary as $uid => $gid)
  2477. {
  2478. $sql_where_ary[$gid][] = $uid;
  2479. }
  2480. unset($temp_ary);
  2481. foreach ($special_group_data as $gid => $default_data_ary)
  2482. {
  2483. if (isset($sql_where_ary[$gid]) && sizeof($sql_where_ary[$gid]))
  2484. {
  2485. remove_default_rank($group_id, $sql_where_ary[$gid]);
  2486. remove_default_avatar($group_id, $sql_where_ary[$gid]);
  2487. group_set_user_default($gid, $sql_where_ary[$gid], $default_data_ary);
  2488. }
  2489. }
  2490. unset($special_group_data);
  2491. /**
  2492. * Event before users are removed from a group
  2493. *
  2494. * @event core.group_delete_user_before
  2495. * @var int group_id ID of the group from which users are deleted
  2496. * @var string group_name Name of the group
  2497. * @var array user_id_ary IDs of the users which are removed
  2498. * @var array username_ary names of the users which are removed
  2499. * @since 3.1.0-a1
  2500. */
  2501. $vars = array('group_id', 'group_name', 'user_id_ary', 'username_ary');
  2502. extract($phpbb_dispatcher->trigger_event('core.group_delete_user_before', compact($vars)));
  2503. $sql = 'DELETE FROM ' . USER_GROUP_TABLE . "
  2504. WHERE group_id = $group_id
  2505. AND " . $db->sql_in_set('user_id', $user_id_ary);
  2506. $db->sql_query($sql);
  2507. // Clear permissions cache of relevant users
  2508. $auth->acl_clear_prefetch($user_id_ary);
  2509. if (!$group_name)
  2510. {
  2511. $group_name = get_group_name($group_id);
  2512. }
  2513. $log = 'LOG_GROUP_REMOVE';
  2514. if ($group_name)
  2515. {
  2516. add_log('admin', $log, $group_name, implode(', ', $username_ary));
  2517. }
  2518. group_update_listings($group_id);
  2519. $phpbb_notifications = $phpbb_container->get('notification_manager');
  2520. $phpbb_notifications->delete_notifications('notification.type.group_request', $user_id_ary, $group_id);
  2521. // Return false - no error
  2522. return false;
  2523. }
  2524. /**
  2525. * Removes the group avatar of the default group from the users in user_ids who have that group as default.
  2526. */
  2527. function remove_default_avatar($group_id, $user_ids)
  2528. {
  2529. global $db;
  2530. if (!is_array($user_ids))
  2531. {
  2532. $user_ids = array($user_ids);
  2533. }
  2534. if (empty($user_ids))
  2535. {
  2536. return false;
  2537. }
  2538. $user_ids = array_map('intval', $user_ids);
  2539. $sql = 'SELECT *
  2540. FROM ' . GROUPS_TABLE . '
  2541. WHERE group_id = ' . (int) $group_id;
  2542. $result = $db->sql_query($sql);
  2543. if (!$row = $db->sql_fetchrow($result))
  2544. {
  2545. $db->sql_freeresult($result);
  2546. return false;
  2547. }
  2548. $db->sql_freeresult($result);
  2549. $sql = 'UPDATE ' . USERS_TABLE . "
  2550. SET user_avatar = '',
  2551. user_avatar_type = '',
  2552. user_avatar_width = 0,
  2553. user_avatar_height = 0
  2554. WHERE group_id = " . (int) $group_id . "
  2555. AND user_avatar = '" . $db->sql_escape($row['group_avatar']) . "'
  2556. AND " . $db->sql_in_set('user_id', $user_ids);
  2557. $db->sql_query($sql);
  2558. }
  2559. /**
  2560. * Removes the group rank of the default group from the users in user_ids who have that group as default.
  2561. */
  2562. function remove_default_rank($group_id, $user_ids)
  2563. {
  2564. global $db;
  2565. if (!is_array($user_ids))
  2566. {
  2567. $user_ids = array($user_ids);
  2568. }
  2569. if (empty($user_ids))
  2570. {
  2571. return false;
  2572. }
  2573. $user_ids = array_map('intval', $user_ids);
  2574. $sql = 'SELECT *
  2575. FROM ' . GROUPS_TABLE . '
  2576. WHERE group_id = ' . (int) $group_id;
  2577. $result = $db->sql_query($sql);
  2578. if (!$row = $db->sql_fetchrow($result))
  2579. {
  2580. $db->sql_freeresult($result);
  2581. return false;
  2582. }
  2583. $db->sql_freeresult($result);
  2584. $sql = 'UPDATE ' . USERS_TABLE . '
  2585. SET user_rank = 0
  2586. WHERE group_id = ' . (int) $group_id . '
  2587. AND user_rank <> 0
  2588. AND user_rank = ' . (int) $row['group_rank'] . '
  2589. AND ' . $db->sql_in_set('user_id', $user_ids);
  2590. $db->sql_query($sql);
  2591. }
  2592. /**
  2593. * This is used to promote (to leader), demote or set as default a member/s
  2594. */
  2595. function group_user_attributes($action, $group_id, $user_id_ary = false, $username_ary = false, $group_name = false, $group_attributes = false)
  2596. {
  2597. global $db, $auth, $phpbb_root_path, $phpEx, $config, $phpbb_container;
  2598. // We need both username and user_id info
  2599. $result = user_get_id_name($user_id_ary, $username_ary);
  2600. if (!sizeof($user_id_ary) || $result !== false)
  2601. {
  2602. return 'NO_USERS';
  2603. }
  2604. if (!$group_name)
  2605. {
  2606. $group_name = get_group_name($group_id);
  2607. }
  2608. switch ($action)
  2609. {
  2610. case 'demote':
  2611. case 'promote':
  2612. $sql = 'SELECT user_id
  2613. FROM ' . USER_GROUP_TABLE . "
  2614. WHERE group_id = $group_id
  2615. AND user_pending = 1
  2616. AND " . $db->sql_in_set('user_id', $user_id_ary);
  2617. $result = $db->sql_query_limit($sql, 1);
  2618. $not_empty = ($db->sql_fetchrow($result));
  2619. $db->sql_freeresult($result);
  2620. if ($not_empty)
  2621. {
  2622. return 'NO_VALID_USERS';
  2623. }
  2624. $sql = 'UPDATE ' . USER_GROUP_TABLE . '
  2625. SET group_leader = ' . (($action == 'promote') ? 1 : 0) . "
  2626. WHERE group_id = $group_id
  2627. AND user_pending = 0
  2628. AND " . $db->sql_in_set('user_id', $user_id_ary);
  2629. $db->sql_query($sql);
  2630. $log = ($action == 'promote') ? 'LOG_GROUP_PROMOTED' : 'LOG_GROUP_DEMOTED';
  2631. break;
  2632. case 'approve':
  2633. // Make sure we only approve those which are pending ;)
  2634. $sql = 'SELECT u.user_id, u.user_email, u.username, u.username_clean, u.user_notify_type, u.user_jabber, u.user_lang
  2635. FROM ' . USERS_TABLE . ' u, ' . USER_GROUP_TABLE . ' ug
  2636. WHERE ug.group_id = ' . $group_id . '
  2637. AND ug.user_pending = 1
  2638. AND ug.user_id = u.user_id
  2639. AND ' . $db->sql_in_set('ug.user_id', $user_id_ary);
  2640. $result = $db->sql_query($sql);
  2641. $user_id_ary = array();
  2642. while ($row = $db->sql_fetchrow($result))
  2643. {
  2644. $user_id_ary[] = $row['user_id'];
  2645. }
  2646. $db->sql_freeresult($result);
  2647. if (!sizeof($user_id_ary))
  2648. {
  2649. return false;
  2650. }
  2651. $sql = 'UPDATE ' . USER_GROUP_TABLE . "
  2652. SET user_pending = 0
  2653. WHERE group_id = $group_id
  2654. AND " . $db->sql_in_set('user_id', $user_id_ary);
  2655. $db->sql_query($sql);
  2656. $phpbb_notifications = $phpbb_container->get('notification_manager');
  2657. $phpbb_notifications->add_notifications('notification.type.group_request_approved', array(
  2658. 'user_ids' => $user_id_ary,
  2659. 'group_id' => $group_id,
  2660. 'group_name' => $group_name,
  2661. ));
  2662. $phpbb_notifications->delete_notifications('notification.type.group_request', $user_id_ary, $group_id);
  2663. $log = 'LOG_USERS_APPROVED';
  2664. break;
  2665. case 'default':
  2666. // We only set default group for approved members of the group
  2667. $sql = 'SELECT user_id
  2668. FROM ' . USER_GROUP_TABLE . "
  2669. WHERE group_id = $group_id
  2670. AND user_pending = 0
  2671. AND " . $db->sql_in_set('user_id', $user_id_ary);
  2672. $result = $db->sql_query($sql);
  2673. $user_id_ary = $username_ary = array();
  2674. while ($row = $db->sql_fetchrow($result))
  2675. {
  2676. $user_id_ary[] = $row['user_id'];
  2677. }
  2678. $db->sql_freeresult($result);
  2679. $result = user_get_id_name($user_id_ary, $username_ary);
  2680. if (!sizeof($user_id_ary) || $result !== false)
  2681. {
  2682. return 'NO_USERS';
  2683. }
  2684. $sql = 'SELECT user_id, group_id
  2685. FROM ' . USERS_TABLE . '
  2686. WHERE ' . $db->sql_in_set('user_id', $user_id_ary, false, true);
  2687. $result = $db->sql_query($sql);
  2688. $groups = array();
  2689. while ($row = $db->sql_fetchrow($result))
  2690. {
  2691. if (!isset($groups[$row['group_id']]))
  2692. {
  2693. $groups[$row['group_id']] = array();
  2694. }
  2695. $groups[$row['group_id']][] = $row['user_id'];
  2696. }
  2697. $db->sql_freeresult($result);
  2698. foreach ($groups as $gid => $uids)
  2699. {
  2700. remove_default_rank($gid, $uids);
  2701. remove_default_avatar($gid, $uids);
  2702. }
  2703. group_set_user_default($group_id, $user_id_ary, $group_attributes);
  2704. $log = 'LOG_GROUP_DEFAULTS';
  2705. break;
  2706. }
  2707. // Clear permissions cache of relevant users
  2708. $auth->acl_clear_prefetch($user_id_ary);
  2709. add_log('admin', $log, $group_name, implode(', ', $username_ary));
  2710. group_update_listings($group_id);
  2711. return false;
  2712. }
  2713. /**
  2714. * A small version of validate_username to check for a group name's existence. To be called directly.
  2715. */
  2716. function group_validate_groupname($group_id, $group_name)
  2717. {
  2718. global $config, $db;
  2719. $group_name = utf8_clean_string($group_name);
  2720. if (!empty($group_id))
  2721. {
  2722. $sql = 'SELECT group_name
  2723. FROM ' . GROUPS_TABLE . '
  2724. WHERE group_id = ' . (int) $group_id;
  2725. $result = $db->sql_query($sql);
  2726. $row = $db->sql_fetchrow($result);
  2727. $db->sql_freeresult($result);
  2728. if (!$row)
  2729. {
  2730. return false;
  2731. }
  2732. $allowed_groupname = utf8_clean_string($row['group_name']);
  2733. if ($allowed_groupname == $group_name)
  2734. {
  2735. return false;
  2736. }
  2737. }
  2738. $sql = 'SELECT group_name
  2739. FROM ' . GROUPS_TABLE . "
  2740. WHERE LOWER(group_name) = '" . $db->sql_escape(utf8_strtolower($group_name)) . "'";
  2741. $result = $db->sql_query($sql);
  2742. $row = $db->sql_fetchrow($result);
  2743. $db->sql_freeresult($result);
  2744. if ($row)
  2745. {
  2746. return 'GROUP_NAME_TAKEN';
  2747. }
  2748. return false;
  2749. }
  2750. /**
  2751. * Set users default group
  2752. *
  2753. * @access private
  2754. */
  2755. function group_set_user_default($group_id, $user_id_ary, $group_attributes = false, $update_listing = false)
  2756. {
  2757. global $phpbb_container, $db, $phpbb_dispatcher;
  2758. if (empty($user_id_ary))
  2759. {
  2760. return;
  2761. }
  2762. $attribute_ary = array(
  2763. 'group_colour' => 'string',
  2764. 'group_rank' => 'int',
  2765. 'group_avatar' => 'string',
  2766. 'group_avatar_type' => 'string',
  2767. 'group_avatar_width' => 'int',
  2768. 'group_avatar_height' => 'int',
  2769. );
  2770. $sql_ary = array(
  2771. 'group_id' => $group_id
  2772. );
  2773. // Were group attributes passed to the function? If not we need to obtain them
  2774. if ($group_attributes === false)
  2775. {
  2776. $sql = 'SELECT ' . implode(', ', array_keys($attribute_ary)) . '
  2777. FROM ' . GROUPS_TABLE . "
  2778. WHERE group_id = $group_id";
  2779. $result = $db->sql_query($sql);
  2780. $group_attributes = $db->sql_fetchrow($result);
  2781. $db->sql_freeresult($result);
  2782. }
  2783. foreach ($attribute_ary as $attribute => $type)
  2784. {
  2785. if (isset($group_attributes[$attribute]))
  2786. {
  2787. // If we are about to set an avatar or rank, we will not overwrite with empty, unless we are not actually changing the default group
  2788. if ((strpos($attribute, 'group_avatar') === 0 || strpos($attribute, 'group_rank') === 0) && !$group_attributes[$attribute])
  2789. {
  2790. continue;
  2791. }
  2792. settype($group_attributes[$attribute], $type);
  2793. $sql_ary[str_replace('group_', 'user_', $attribute)] = $group_attributes[$attribute];
  2794. }
  2795. }
  2796. $updated_sql_ary = $sql_ary;
  2797. // Before we update the user attributes, we will update the rank for users that don't have a custom rank
  2798. if (isset($sql_ary['user_rank']))
  2799. {
  2800. $sql = 'UPDATE ' . USERS_TABLE . '
  2801. SET ' . $db->sql_build_array('UPDATE', array('user_rank' => $sql_ary['user_rank'])) . '
  2802. WHERE user_rank = 0
  2803. AND ' . $db->sql_in_set('user_id', $user_id_ary);
  2804. $db->sql_query($sql);
  2805. unset($sql_ary['user_rank']);
  2806. }
  2807. // Before we update the user attributes, we will update the avatar for users that don't have a custom avatar
  2808. $avatar_options = array('user_avatar', 'user_avatar_type', 'user_avatar_height', 'user_avatar_width');
  2809. if (isset($sql_ary['user_avatar']))
  2810. {
  2811. $avatar_sql_ary = array();
  2812. foreach ($avatar_options as $avatar_option)
  2813. {
  2814. if (isset($sql_ary[$avatar_option]))
  2815. {
  2816. $avatar_sql_ary[$avatar_option] = $sql_ary[$avatar_option];
  2817. }
  2818. }
  2819. $sql = 'UPDATE ' . USERS_TABLE . '
  2820. SET ' . $db->sql_build_array('UPDATE', $avatar_sql_ary) . "
  2821. WHERE user_avatar = ''
  2822. AND " . $db->sql_in_set('user_id', $user_id_ary);
  2823. $db->sql_query($sql);
  2824. }
  2825. // Remove the avatar options, as we already updated them
  2826. foreach ($avatar_options as $avatar_option)
  2827. {
  2828. unset($sql_ary[$avatar_option]);
  2829. }
  2830. if (!empty($sql_ary))
  2831. {
  2832. $sql = 'UPDATE ' . USERS_TABLE . '
  2833. SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
  2834. WHERE ' . $db->sql_in_set('user_id', $user_id_ary);
  2835. $db->sql_query($sql);
  2836. }
  2837. if (isset($sql_ary['user_colour']))
  2838. {
  2839. // Update any cached colour information for these users
  2840. $sql = 'UPDATE ' . FORUMS_TABLE . "
  2841. SET forum_last_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'
  2842. WHERE " . $db->sql_in_set('forum_last_poster_id', $user_id_ary);
  2843. $db->sql_query($sql);
  2844. $sql = 'UPDATE ' . TOPICS_TABLE . "
  2845. SET topic_first_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'
  2846. WHERE " . $db->sql_in_set('topic_poster', $user_id_ary);
  2847. $db->sql_query($sql);
  2848. $sql = 'UPDATE ' . TOPICS_TABLE . "
  2849. SET topic_last_poster_colour = '" . $db->sql_escape($sql_ary['user_colour']) . "'
  2850. WHERE " . $db->sql_in_set('topic_last_poster_id', $user_id_ary);
  2851. $db->sql_query($sql);
  2852. global $config;
  2853. if (in_array($config['newest_user_id'], $user_id_ary))
  2854. {
  2855. set_config('newest_user_colour', $sql_ary['user_colour'], true);
  2856. }
  2857. }
  2858. // Make all values available for the event
  2859. $sql_ary = $updated_sql_ary;
  2860. /**
  2861. * Event when the default group is set for an array of users
  2862. *
  2863. * @event core.user_set_default_group
  2864. * @var int group_id ID of the group
  2865. * @var array user_id_ary IDs of the users
  2866. * @var array group_attributes Group attributes which were changed
  2867. * @var array update_listing Update the list of moderators and foes
  2868. * @var array sql_ary User attributes which were changed
  2869. * @since 3.1.0-a1
  2870. */
  2871. $vars = array('group_id', 'user_id_ary', 'group_attributes', 'update_listing', 'sql_ary');
  2872. extract($phpbb_dispatcher->trigger_event('core.user_set_default_group', compact($vars)));
  2873. if ($update_listing)
  2874. {
  2875. group_update_listings($group_id);
  2876. }
  2877. // Because some tables/caches use usercolour-specific data we need to purge this here.
  2878. $phpbb_container->get('cache.driver')->destroy('sql', MODERATOR_CACHE_TABLE);
  2879. }
  2880. /**
  2881. * Get group name
  2882. */
  2883. function get_group_name($group_id)
  2884. {
  2885. global $db, $user;
  2886. $sql = 'SELECT group_name, group_type
  2887. FROM ' . GROUPS_TABLE . '
  2888. WHERE group_id = ' . (int) $group_id;
  2889. $result = $db->sql_query($sql);
  2890. $row = $db->sql_fetchrow($result);
  2891. $db->sql_freeresult($result);
  2892. if (!$row || ($row['group_type'] == GROUP_SPECIAL && empty($user->lang)))
  2893. {
  2894. return '';
  2895. }
  2896. return ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];
  2897. }
  2898. /**
  2899. * Obtain either the members of a specified group, the groups the specified user is subscribed to
  2900. * or checking if a specified user is in a specified group. This function does not return pending memberships.
  2901. *
  2902. * Note: Never use this more than once... first group your users/groups
  2903. */
  2904. function group_memberships($group_id_ary = false, $user_id_ary = false, $return_bool = false)
  2905. {
  2906. global $db;
  2907. if (!$group_id_ary && !$user_id_ary)
  2908. {
  2909. return true;
  2910. }
  2911. if ($user_id_ary)
  2912. {
  2913. $user_id_ary = (!is_array($user_id_ary)) ? array($user_id_ary) : $user_id_ary;
  2914. }
  2915. if ($group_id_ary)
  2916. {
  2917. $group_id_ary = (!is_array($group_id_ary)) ? array($group_id_ary) : $group_id_ary;
  2918. }
  2919. $sql = 'SELECT ug.*, u.username, u.username_clean, u.user_email
  2920. FROM ' . USER_GROUP_TABLE . ' ug, ' . USERS_TABLE . ' u
  2921. WHERE ug.user_id = u.user_id
  2922. AND ug.user_pending = 0 AND ';
  2923. if ($group_id_ary)
  2924. {
  2925. $sql .= ' ' . $db->sql_in_set('ug.group_id', $group_id_ary);
  2926. }
  2927. if ($user_id_ary)
  2928. {
  2929. $sql .= ($group_id_ary) ? ' AND ' : ' ';
  2930. $sql .= $db->sql_in_set('ug.user_id', $user_id_ary);
  2931. }
  2932. $result = ($return_bool) ? $db->sql_query_limit($sql, 1) : $db->sql_query($sql);
  2933. $row = $db->sql_fetchrow($result);
  2934. if ($return_bool)
  2935. {
  2936. $db->sql_freeresult($result);
  2937. return ($row) ? true : false;
  2938. }
  2939. if (!$row)
  2940. {
  2941. return false;
  2942. }
  2943. $return = array();
  2944. do
  2945. {
  2946. $return[] = $row;
  2947. }
  2948. while ($row = $db->sql_fetchrow($result));
  2949. $db->sql_freeresult($result);
  2950. return $return;
  2951. }
  2952. /**
  2953. * Re-cache moderators and foes if group has a_ or m_ permissions
  2954. */
  2955. function group_update_listings($group_id)
  2956. {
  2957. global $db, $cache, $auth;
  2958. $hold_ary = $auth->acl_group_raw_data($group_id, array('a_', 'm_'));
  2959. if (!sizeof($hold_ary))
  2960. {
  2961. return;
  2962. }
  2963. $mod_permissions = $admin_permissions = false;
  2964. foreach ($hold_ary as $g_id => $forum_ary)
  2965. {
  2966. foreach ($forum_ary as $forum_id => $auth_ary)
  2967. {
  2968. foreach ($auth_ary as $auth_option => $setting)
  2969. {
  2970. if ($mod_permissions && $admin_permissions)
  2971. {
  2972. break 3;
  2973. }
  2974. if ($setting != ACL_YES)
  2975. {
  2976. continue;
  2977. }
  2978. if ($auth_option == 'm_')
  2979. {
  2980. $mod_permissions = true;
  2981. }
  2982. if ($auth_option == 'a_')
  2983. {
  2984. $admin_permissions = true;
  2985. }
  2986. }
  2987. }
  2988. }
  2989. if ($mod_permissions)
  2990. {
  2991. if (!function_exists('phpbb_cache_moderators'))
  2992. {
  2993. global $phpbb_root_path, $phpEx;
  2994. include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  2995. }
  2996. phpbb_cache_moderators($db, $cache, $auth);
  2997. }
  2998. if ($mod_permissions || $admin_permissions)
  2999. {
  3000. if (!function_exists('phpbb_update_foes'))
  3001. {
  3002. global $phpbb_root_path, $phpEx;
  3003. include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  3004. }
  3005. phpbb_update_foes($db, $auth, array($group_id));
  3006. }
  3007. }
  3008. /**
  3009. * Funtion to make a user leave the NEWLY_REGISTERED system group.
  3010. * @access public
  3011. * @param $user_id The id of the user to remove from the group
  3012. */
  3013. function remove_newly_registered($user_id, $user_data = false)
  3014. {
  3015. global $db;
  3016. if ($user_data === false)
  3017. {
  3018. $sql = 'SELECT *
  3019. FROM ' . USERS_TABLE . '
  3020. WHERE user_id = ' . $user_id;
  3021. $result = $db->sql_query($sql);
  3022. $user_row = $db->sql_fetchrow($result);
  3023. $db->sql_freeresult($result);
  3024. if (!$user_row)
  3025. {
  3026. return false;
  3027. }
  3028. else
  3029. {
  3030. $user_data = $user_row;
  3031. }
  3032. }
  3033. if (empty($user_data['user_new']))
  3034. {
  3035. return false;
  3036. }
  3037. $sql = 'SELECT group_id
  3038. FROM ' . GROUPS_TABLE . "
  3039. WHERE group_name = 'NEWLY_REGISTERED'
  3040. AND group_type = " . GROUP_SPECIAL;
  3041. $result = $db->sql_query($sql);
  3042. $group_id = (int) $db->sql_fetchfield('group_id');
  3043. $db->sql_freeresult($result);
  3044. if (!$group_id)
  3045. {
  3046. return false;
  3047. }
  3048. // We need to call group_user_del here, because this function makes sure everything is correctly changed.
  3049. // A downside for a call within the session handler is that the language is not set up yet - so no log entry
  3050. group_user_del($group_id, $user_id);
  3051. // Set user_new to 0 to let this not be triggered again
  3052. $sql = 'UPDATE ' . USERS_TABLE . '
  3053. SET user_new = 0
  3054. WHERE user_id = ' . $user_id;
  3055. $db->sql_query($sql);
  3056. // The new users group was the users default group?
  3057. if ($user_data['group_id'] == $group_id)
  3058. {
  3059. // Which group is now the users default one?
  3060. $sql = 'SELECT group_id
  3061. FROM ' . USERS_TABLE . '
  3062. WHERE user_id = ' . $user_id;
  3063. $result = $db->sql_query($sql);
  3064. $user_data['group_id'] = $db->sql_fetchfield('group_id');
  3065. $db->sql_freeresult($result);
  3066. }
  3067. return $user_data['group_id'];
  3068. }
  3069. /**
  3070. * Gets user ids of currently banned registered users.
  3071. *
  3072. * @param array $user_ids Array of users' ids to check for banning,
  3073. * leave empty to get complete list of banned ids
  3074. * @param bool|int $ban_end Bool True to get users currently banned
  3075. * Bool False to only get permanently banned users
  3076. * Int Unix timestamp to get users banned until that time
  3077. * @return array Array of banned users' ids if any, empty array otherwise
  3078. */
  3079. function phpbb_get_banned_user_ids($user_ids = array(), $ban_end = true)
  3080. {
  3081. global $db;
  3082. $sql_user_ids = (!empty($user_ids)) ? $db->sql_in_set('ban_userid', $user_ids) : 'ban_userid <> 0';
  3083. // Get banned User ID's
  3084. // Ignore stale bans which were not wiped yet
  3085. $banned_ids_list = array();
  3086. $sql = 'SELECT ban_userid
  3087. FROM ' . BANLIST_TABLE . "
  3088. WHERE $sql_user_ids
  3089. AND ban_exclude <> 1";
  3090. if ($ban_end === true)
  3091. {
  3092. // Banned currently
  3093. $sql .= " AND (ban_end > " . time() . '
  3094. OR ban_end = 0)';
  3095. }
  3096. else if ($ban_end === false)
  3097. {
  3098. // Permanently banned
  3099. $sql .= " AND ban_end = 0";
  3100. }
  3101. else
  3102. {
  3103. // Banned until a specified time
  3104. $sql .= " AND (ban_end > " . (int) $ban_end . '
  3105. OR ban_end = 0)';
  3106. }
  3107. $result = $db->sql_query($sql);
  3108. while ($row = $db->sql_fetchrow($result))
  3109. {
  3110. $user_id = (int) $row['ban_userid'];
  3111. $banned_ids_list[$user_id] = $user_id;
  3112. }
  3113. $db->sql_freeresult($result);
  3114. return $banned_ids_list;
  3115. }
  3116. /**
  3117. * Function for assigning a template var if the zebra module got included
  3118. */
  3119. function phpbb_module_zebra($mode, &$module_row)
  3120. {
  3121. global $template;
  3122. $template->assign_var('S_ZEBRA_ENABLED', true);
  3123. if ($mode == 'friends')
  3124. {
  3125. $template->assign_var('S_ZEBRA_FRIENDS_ENABLED', true);
  3126. }
  3127. if ($mode == 'foes')
  3128. {
  3129. $template->assign_var('S_ZEBRA_FOES_ENABLED', true);
  3130. }
  3131. }