PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/phpBB/includes/functions_user.php

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