PageRenderTime 71ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/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

Large files files are truncated, but you can click here to view the full file

  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 REPLACEMEN…

Large files files are truncated, but you can click here to view the full file