PageRenderTime 32ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/includes/functions_user.php

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