Subs-Members.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. <?php
  2. /**
  3. * This file contains some useful functions for members and membergroups.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2011 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.0
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * Delete one or more members.
  18. * Requires profile_remove_own or profile_remove_any permission for
  19. * respectively removing your own account or any account.
  20. * Non-admins cannot delete admins.
  21. * The function:
  22. * - changes author of messages, topics and polls to guest authors.
  23. * - removes all log entries concerning the deleted members, except the
  24. * error logs, ban logs and moderation logs.
  25. * - removes these members' personal messages (only the inbox), avatars,
  26. * ban entries, theme settings, moderator positions, poll votes, and
  27. * karma votes.
  28. * - updates member statistics afterwards.
  29. *
  30. * @param array $users
  31. * @param bool $check_not_admin = false
  32. */
  33. function deleteMembers($users, $check_not_admin = false)
  34. {
  35. global $sourcedir, $modSettings, $user_info, $smcFunc;
  36. // Try give us a while to sort this out...
  37. @set_time_limit(600);
  38. // Try to get some more memory.
  39. if (@ini_get('memory_limit') < 128)
  40. @ini_set('memory_limit', '128M');
  41. // If it's not an array, make it so!
  42. if (!is_array($users))
  43. $users = array($users);
  44. else
  45. $users = array_unique($users);
  46. // Make sure there's no void user in here.
  47. $users = array_diff($users, array(0));
  48. // How many are they deleting?
  49. if (empty($users))
  50. return;
  51. elseif (count($users) == 1)
  52. {
  53. list ($user) = $users;
  54. if ($user == $user_info['id'])
  55. isAllowedTo('profile_remove_own');
  56. else
  57. isAllowedTo('profile_remove_any');
  58. }
  59. else
  60. {
  61. foreach ($users as $k => $v)
  62. $users[$k] = (int) $v;
  63. // Deleting more than one? You can't have more than one account...
  64. isAllowedTo('profile_remove_any');
  65. }
  66. // Get their names for logging purposes.
  67. $request = $smcFunc['db_query']('', '
  68. SELECT id_member, member_name, CASE WHEN id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0 THEN 1 ELSE 0 END AS is_admin
  69. FROM {db_prefix}members
  70. WHERE id_member IN ({array_int:user_list})
  71. LIMIT ' . count($users),
  72. array(
  73. 'user_list' => $users,
  74. 'admin_group' => 1,
  75. )
  76. );
  77. $admins = array();
  78. $user_log_details = array();
  79. while ($row = $smcFunc['db_fetch_assoc']($request))
  80. {
  81. if ($row['is_admin'])
  82. $admins[] = $row['id_member'];
  83. $user_log_details[$row['id_member']] = array($row['id_member'], $row['member_name']);
  84. }
  85. $smcFunc['db_free_result']($request);
  86. if (empty($user_log_details))
  87. return;
  88. // Make sure they aren't trying to delete administrators if they aren't one. But don't bother checking if it's just themself.
  89. if (!empty($admins) && ($check_not_admin || (!allowedTo('admin_forum') && (count($users) != 1 || $users[0] != $user_info['id']))))
  90. {
  91. $users = array_diff($users, $admins);
  92. foreach ($admins as $id)
  93. unset($user_log_details[$id]);
  94. }
  95. // No one left?
  96. if (empty($users))
  97. return;
  98. // Log the action - regardless of who is deleting it.
  99. $log_inserts = array();
  100. foreach ($user_log_details as $user)
  101. {
  102. // Integration rocks!
  103. call_integration_hook('integrate_delete_member', array($user[0]));
  104. // Add it to the administration log for future reference.
  105. $log_inserts[] = array(
  106. time(), 3, $user_info['id'], $user_info['ip'], 'delete_member',
  107. 0, 0, 0, serialize(array('member' => $user[0], 'name' => $user[1], 'member_acted' => $user_info['name'])),
  108. );
  109. // Remove any cached data if enabled.
  110. if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2)
  111. cache_put_data('user_settings-' . $user[0], null, 60);
  112. }
  113. // Do the actual logging...
  114. if (!empty($log_inserts) && !empty($modSettings['modlog_enabled']))
  115. $smcFunc['db_insert']('',
  116. '{db_prefix}log_actions',
  117. array(
  118. 'log_time' => 'int', 'id_log' => 'int', 'id_member' => 'int', 'ip' => 'string-16', 'action' => 'string',
  119. 'id_board' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'extra' => 'string-65534',
  120. ),
  121. $log_inserts,
  122. array('id_action')
  123. );
  124. // Make these peoples' posts guest posts.
  125. $smcFunc['db_query']('', '
  126. UPDATE {db_prefix}messages
  127. SET id_member = {int:guest_id}, poster_email = {string:blank_email}
  128. WHERE id_member IN ({array_int:users})',
  129. array(
  130. 'guest_id' => 0,
  131. 'blank_email' => '',
  132. 'users' => $users,
  133. )
  134. );
  135. $smcFunc['db_query']('', '
  136. UPDATE {db_prefix}polls
  137. SET id_member = {int:guest_id}
  138. WHERE id_member IN ({array_int:users})',
  139. array(
  140. 'guest_id' => 0,
  141. 'users' => $users,
  142. )
  143. );
  144. // Make these peoples' posts guest first posts and last posts.
  145. $smcFunc['db_query']('', '
  146. UPDATE {db_prefix}topics
  147. SET id_member_started = {int:guest_id}
  148. WHERE id_member_started IN ({array_int:users})',
  149. array(
  150. 'guest_id' => 0,
  151. 'users' => $users,
  152. )
  153. );
  154. $smcFunc['db_query']('', '
  155. UPDATE {db_prefix}topics
  156. SET id_member_updated = {int:guest_id}
  157. WHERE id_member_updated IN ({array_int:users})',
  158. array(
  159. 'guest_id' => 0,
  160. 'users' => $users,
  161. )
  162. );
  163. $smcFunc['db_query']('', '
  164. UPDATE {db_prefix}log_actions
  165. SET id_member = {int:guest_id}
  166. WHERE id_member IN ({array_int:users})',
  167. array(
  168. 'guest_id' => 0,
  169. 'users' => $users,
  170. )
  171. );
  172. $smcFunc['db_query']('', '
  173. UPDATE {db_prefix}log_banned
  174. SET id_member = {int:guest_id}
  175. WHERE id_member IN ({array_int:users})',
  176. array(
  177. 'guest_id' => 0,
  178. 'users' => $users,
  179. )
  180. );
  181. $smcFunc['db_query']('', '
  182. UPDATE {db_prefix}log_errors
  183. SET id_member = {int:guest_id}
  184. WHERE id_member IN ({array_int:users})',
  185. array(
  186. 'guest_id' => 0,
  187. 'users' => $users,
  188. )
  189. );
  190. // Delete the member.
  191. $smcFunc['db_query']('', '
  192. DELETE FROM {db_prefix}members
  193. WHERE id_member IN ({array_int:users})',
  194. array(
  195. 'users' => $users,
  196. )
  197. );
  198. // Delete the logs...
  199. $smcFunc['db_query']('', '
  200. DELETE FROM {db_prefix}log_actions
  201. WHERE id_log = {int:log_type}
  202. AND id_member IN ({array_int:users})',
  203. array(
  204. 'log_type' => 2,
  205. 'users' => $users,
  206. )
  207. );
  208. $smcFunc['db_query']('', '
  209. DELETE FROM {db_prefix}log_boards
  210. WHERE id_member IN ({array_int:users})',
  211. array(
  212. 'users' => $users,
  213. )
  214. );
  215. $smcFunc['db_query']('', '
  216. DELETE FROM {db_prefix}log_comments
  217. WHERE id_recipient IN ({array_int:users})
  218. AND comment_type = {string:warntpl}',
  219. array(
  220. 'users' => $users,
  221. 'warntpl' => 'warntpl',
  222. )
  223. );
  224. $smcFunc['db_query']('', '
  225. DELETE FROM {db_prefix}log_group_requests
  226. WHERE id_member IN ({array_int:users})',
  227. array(
  228. 'users' => $users,
  229. )
  230. );
  231. $smcFunc['db_query']('', '
  232. DELETE FROM {db_prefix}log_karma
  233. WHERE id_target IN ({array_int:users})
  234. OR id_executor IN ({array_int:users})',
  235. array(
  236. 'users' => $users,
  237. )
  238. );
  239. $smcFunc['db_query']('', '
  240. DELETE FROM {db_prefix}log_mark_read
  241. WHERE id_member IN ({array_int:users})',
  242. array(
  243. 'users' => $users,
  244. )
  245. );
  246. $smcFunc['db_query']('', '
  247. DELETE FROM {db_prefix}log_notify
  248. WHERE id_member IN ({array_int:users})',
  249. array(
  250. 'users' => $users,
  251. )
  252. );
  253. $smcFunc['db_query']('', '
  254. DELETE FROM {db_prefix}log_online
  255. WHERE id_member IN ({array_int:users})',
  256. array(
  257. 'users' => $users,
  258. )
  259. );
  260. $smcFunc['db_query']('', '
  261. DELETE FROM {db_prefix}log_subscribed
  262. WHERE id_member IN ({array_int:users})',
  263. array(
  264. 'users' => $users,
  265. )
  266. );
  267. $smcFunc['db_query']('', '
  268. DELETE FROM {db_prefix}log_topics
  269. WHERE id_member IN ({array_int:users})',
  270. array(
  271. 'users' => $users,
  272. )
  273. );
  274. $smcFunc['db_query']('', '
  275. DELETE FROM {db_prefix}collapsed_categories
  276. WHERE id_member IN ({array_int:users})',
  277. array(
  278. 'users' => $users,
  279. )
  280. );
  281. // Make their votes appear as guest votes - at least it keeps the totals right.
  282. // @todo Consider adding back in cookie protection.
  283. $smcFunc['db_query']('', '
  284. UPDATE {db_prefix}log_polls
  285. SET id_member = {int:guest_id}
  286. WHERE id_member IN ({array_int:users})',
  287. array(
  288. 'guest_id' => 0,
  289. 'users' => $users,
  290. )
  291. );
  292. // Delete personal messages.
  293. require_once($sourcedir . '/PersonalMessage.php');
  294. deleteMessages(null, null, $users);
  295. $smcFunc['db_query']('', '
  296. UPDATE {db_prefix}personal_messages
  297. SET id_member_from = {int:guest_id}
  298. WHERE id_member_from IN ({array_int:users})',
  299. array(
  300. 'guest_id' => 0,
  301. 'users' => $users,
  302. )
  303. );
  304. // They no longer exist, so we don't know who it was sent to.
  305. $smcFunc['db_query']('', '
  306. DELETE FROM {db_prefix}pm_recipients
  307. WHERE id_member IN ({array_int:users})',
  308. array(
  309. 'users' => $users,
  310. )
  311. );
  312. // Delete avatar.
  313. require_once($sourcedir . '/ManageAttachments.php');
  314. removeAttachments(array('id_member' => $users));
  315. // It's over, no more moderation for you.
  316. $smcFunc['db_query']('', '
  317. DELETE FROM {db_prefix}moderators
  318. WHERE id_member IN ({array_int:users})',
  319. array(
  320. 'users' => $users,
  321. )
  322. );
  323. $smcFunc['db_query']('', '
  324. DELETE FROM {db_prefix}group_moderators
  325. WHERE id_member IN ({array_int:users})',
  326. array(
  327. 'users' => $users,
  328. )
  329. );
  330. // If you don't exist we can't ban you.
  331. $smcFunc['db_query']('', '
  332. DELETE FROM {db_prefix}ban_items
  333. WHERE id_member IN ({array_int:users})',
  334. array(
  335. 'users' => $users,
  336. )
  337. );
  338. // Remove individual theme settings.
  339. $smcFunc['db_query']('', '
  340. DELETE FROM {db_prefix}themes
  341. WHERE id_member IN ({array_int:users})',
  342. array(
  343. 'users' => $users,
  344. )
  345. );
  346. // These users are nobody's buddy nomore.
  347. $request = $smcFunc['db_query']('', '
  348. SELECT id_member, pm_ignore_list, buddy_list
  349. FROM {db_prefix}members
  350. WHERE FIND_IN_SET({raw:pm_ignore_list}, pm_ignore_list) != 0 OR FIND_IN_SET({raw:buddy_list}, buddy_list) != 0',
  351. array(
  352. 'pm_ignore_list' => implode(', pm_ignore_list) != 0 OR FIND_IN_SET(', $users),
  353. 'buddy_list' => implode(', buddy_list) != 0 OR FIND_IN_SET(', $users),
  354. )
  355. );
  356. while ($row = $smcFunc['db_fetch_assoc']($request))
  357. $smcFunc['db_query']('', '
  358. UPDATE {db_prefix}members
  359. SET
  360. pm_ignore_list = {string:pm_ignore_list},
  361. buddy_list = {string:buddy_list}
  362. WHERE id_member = {int:id_member}',
  363. array(
  364. 'id_member' => $row['id_member'],
  365. 'pm_ignore_list' => implode(',', array_diff(explode(',', $row['pm_ignore_list']), $users)),
  366. 'buddy_list' => implode(',', array_diff(explode(',', $row['buddy_list']), $users)),
  367. )
  368. );
  369. $smcFunc['db_free_result']($request);
  370. // Make sure no member's birthday is still sticking in the calendar...
  371. updateSettings(array(
  372. 'calendar_updated' => time(),
  373. ));
  374. updateStats('member');
  375. }
  376. /**
  377. * Registers a member to the forum.
  378. * Allows two types of interface: 'guest' and 'admin'. The first
  379. * includes hammering protection, the latter can perform the
  380. * registration silently.
  381. * The strings used in the options array are assumed to be escaped.
  382. * Allows to perform several checks on the input, e.g. reserved names.
  383. * The function will adjust member statistics.
  384. * If an error is detected will fatal error on all errors unless return_errors is true.
  385. *
  386. * @param array $regOptions
  387. * @param bool $return_errors - specify whether to return the errors
  388. * @return int, the ID of the newly created member
  389. */
  390. function registerMember(&$regOptions, $return_errors = false)
  391. {
  392. global $scripturl, $txt, $modSettings, $context, $sourcedir;
  393. global $user_info, $options, $settings, $smcFunc;
  394. loadLanguage('Login');
  395. // We'll need some external functions.
  396. require_once($sourcedir . '/Subs-Auth.php');
  397. require_once($sourcedir . '/Subs-Post.php');
  398. // Put any errors in here.
  399. $reg_errors = array();
  400. // Registration from the admin center, let them sweat a little more.
  401. if ($regOptions['interface'] == 'admin')
  402. {
  403. is_not_guest();
  404. isAllowedTo('moderate_forum');
  405. }
  406. // If you're an admin, you're special ;).
  407. elseif ($regOptions['interface'] == 'guest')
  408. {
  409. // You cannot register twice...
  410. if (empty($user_info['is_guest']))
  411. redirectexit();
  412. // Make sure they didn't just register with this session.
  413. if (!empty($_SESSION['just_registered']) && empty($modSettings['disableRegisterCheck']))
  414. fatal_lang_error('register_only_once', false);
  415. }
  416. // What method of authorization are we going to use?
  417. if (empty($regOptions['auth_method']) || !in_array($regOptions['auth_method'], array('password', 'openid')))
  418. {
  419. if (!empty($regOptions['openid']))
  420. $regOptions['auth_method'] = 'openid';
  421. else
  422. $regOptions['auth_method'] = 'password';
  423. }
  424. // No name?! How can you register with no name?
  425. if (empty($regOptions['username']))
  426. $reg_errors[] = array('lang', 'need_username');
  427. // Spaces and other odd characters are evil...
  428. $regOptions['username'] = preg_replace('~[\t\n\r\x0B\0' . ($context['utf8'] ? ($context['server']['complex_preg_chars'] ? '\x{A0}' : "\xC2\xA0") : '\xA0') . ']+~' . ($context['utf8'] ? 'u' : ''), ' ', $regOptions['username']);
  429. // Don't use too long a name.
  430. if ($smcFunc['strlen']($regOptions['username']) > 25)
  431. $reg_errors[] = array('lang', 'error_long_name');
  432. // Only these characters are permitted.
  433. if (preg_match('~[<>&"\'=\\\\]~', preg_replace('~&#(?:\\d{1,7}|x[0-9a-fA-F]{1,6});~', '', $regOptions['username'])) != 0 || $regOptions['username'] == '_' || $regOptions['username'] == '|' || strpos($regOptions['username'], '[code') !== false || strpos($regOptions['username'], '[/code') !== false)
  434. $reg_errors[] = array('lang', 'error_invalid_characters_username');
  435. if ($smcFunc['strtolower']($regOptions['username']) === $smcFunc['strtolower']($txt['guest_title']))
  436. $reg_errors[] = array('lang', 'username_reserved', 'general', array($txt['guest_title']));
  437. // @todo Separate the sprintf?
  438. if (empty($regOptions['email']) || preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $regOptions['email']) === 0 || strlen($regOptions['email']) > 255)
  439. $reg_errors[] = array('done', sprintf($txt['valid_email_needed'], $smcFunc['htmlspecialchars']($regOptions['username'])));
  440. if (!empty($regOptions['check_reserved_name']) && isReservedName($regOptions['username'], 0, false))
  441. {
  442. if ($regOptions['password'] == 'chocolate cake')
  443. $reg_errors[] = array('done', 'Sorry, I don\'t take bribes... you\'ll need to come up with a different name.');
  444. $reg_errors[] = array('done', '(' . htmlspecialchars($regOptions['username']) . ') ' . $txt['name_in_use']);
  445. }
  446. // Generate a validation code if it's supposed to be emailed.
  447. $validation_code = '';
  448. if ($regOptions['require'] == 'activation')
  449. $validation_code = generateValidationCode();
  450. // If you haven't put in a password generate one.
  451. if ($regOptions['interface'] == 'admin' && $regOptions['password'] == '' && $regOptions['auth_method'] == 'password')
  452. {
  453. mt_srand(time() + 1277);
  454. $regOptions['password'] = generateValidationCode();
  455. $regOptions['password_check'] = $regOptions['password'];
  456. }
  457. // Does the first password match the second?
  458. elseif ($regOptions['password'] != $regOptions['password_check'] && $regOptions['auth_method'] == 'password')
  459. $reg_errors[] = array('lang', 'passwords_dont_match');
  460. // That's kind of easy to guess...
  461. if ($regOptions['password'] == '')
  462. {
  463. if ($regOptions['auth_method'] == 'password')
  464. $reg_errors[] = array('lang', 'no_password');
  465. else
  466. $regOptions['password'] = sha1(mt_rand());
  467. }
  468. // Now perform hard password validation as required.
  469. if (!empty($regOptions['check_password_strength']))
  470. {
  471. $passwordError = validatePassword($regOptions['password'], $regOptions['username'], array($regOptions['email']));
  472. // Password isn't legal?
  473. if ($passwordError != null)
  474. $reg_errors[] = array('lang', 'profile_error_password_' . $passwordError);
  475. }
  476. // If they are using an OpenID that hasn't been verified yet error out.
  477. // @todo Change this so they can register without having to attempt a login first
  478. if ($regOptions['auth_method'] == 'openid' && (empty($_SESSION['openid']['verified']) || $_SESSION['openid']['openid_uri'] != $regOptions['openid']))
  479. $reg_errors[] = array('lang', 'openid_not_verified');
  480. // You may not be allowed to register this email.
  481. if (!empty($regOptions['check_email_ban']))
  482. isBannedEmail($regOptions['email'], 'cannot_register', $txt['ban_register_prohibited']);
  483. // Check if the email address is in use.
  484. $request = $smcFunc['db_query']('', '
  485. SELECT id_member
  486. FROM {db_prefix}members
  487. WHERE email_address = {string:email_address}
  488. OR email_address = {string:username}
  489. LIMIT 1',
  490. array(
  491. 'email_address' => $regOptions['email'],
  492. 'username' => $regOptions['username'],
  493. )
  494. );
  495. // @todo Separate the sprintf?
  496. if ($smcFunc['db_num_rows']($request) != 0)
  497. $reg_errors[] = array('lang', 'email_in_use', false, array(htmlspecialchars($regOptions['email'])));
  498. $smcFunc['db_free_result']($request);
  499. // If we found any errors we need to do something about it right away!
  500. foreach ($reg_errors as $key => $error)
  501. {
  502. /* Note for each error:
  503. 0 = 'lang' if it's an index, 'done' if it's clear text.
  504. 1 = The text/index.
  505. 2 = Whether to log.
  506. 3 = sprintf data if necessary. */
  507. if ($error[0] == 'lang')
  508. loadLanguage('Errors');
  509. $message = $error[0] == 'lang' ? (empty($error[3]) ? $txt[$error[1]] : vsprintf($txt[$error[1]], $error[3])) : $error[1];
  510. // What to do, what to do, what to do.
  511. if ($return_errors)
  512. {
  513. if (!empty($error[2]))
  514. log_error($message, $error[2]);
  515. $reg_errors[$key] = $message;
  516. }
  517. else
  518. fatal_error($message, empty($error[2]) ? false : $error[2]);
  519. }
  520. // If there's any errors left return them at once!
  521. if (!empty($reg_errors))
  522. return $reg_errors;
  523. $reservedVars = array(
  524. 'actual_theme_url',
  525. 'actual_images_url',
  526. 'base_theme_dir',
  527. 'base_theme_url',
  528. 'default_images_url',
  529. 'default_theme_dir',
  530. 'default_theme_url',
  531. 'default_template',
  532. 'images_url',
  533. 'number_recent_posts',
  534. 'smiley_sets_default',
  535. 'theme_dir',
  536. 'theme_id',
  537. 'theme_layers',
  538. 'theme_templates',
  539. 'theme_url',
  540. );
  541. // Can't change reserved vars.
  542. if (isset($regOptions['theme_vars']) && array_intersect($regOptions['theme_vars'], $reservedVars) != array())
  543. fatal_lang_error('no_theme');
  544. // Some of these might be overwritten. (the lower ones that are in the arrays below.)
  545. $regOptions['register_vars'] = array(
  546. 'member_name' => $regOptions['username'],
  547. 'email_address' => $regOptions['email'],
  548. 'passwd' => sha1(strtolower($regOptions['username']) . $regOptions['password']),
  549. 'password_salt' => substr(md5(mt_rand()), 0, 4) ,
  550. 'posts' => 0,
  551. 'date_registered' => time(),
  552. 'member_ip' => $regOptions['interface'] == 'admin' ? '127.0.0.1' : $user_info['ip'],
  553. 'member_ip2' => $regOptions['interface'] == 'admin' ? '127.0.0.1' : $_SERVER['BAN_CHECK_IP'],
  554. 'validation_code' => $validation_code,
  555. 'real_name' => $regOptions['username'],
  556. 'personal_text' => $modSettings['default_personal_text'],
  557. 'pm_email_notify' => 1,
  558. 'id_theme' => 0,
  559. 'id_post_group' => 4,
  560. 'lngfile' => '',
  561. 'buddy_list' => '',
  562. 'pm_ignore_list' => '',
  563. 'message_labels' => '',
  564. 'website_title' => '',
  565. 'website_url' => '',
  566. 'location' => '',
  567. 'icq' => '',
  568. 'aim' => '',
  569. 'yim' => '',
  570. 'msn' => '',
  571. 'time_format' => '',
  572. 'signature' => '',
  573. 'avatar' => '',
  574. 'usertitle' => '',
  575. 'secret_question' => '',
  576. 'secret_answer' => '',
  577. 'additional_groups' => '',
  578. 'ignore_boards' => '',
  579. 'smiley_set' => '',
  580. 'openid_uri' => (!empty($regOptions['openid']) ? $regOptions['openid'] : ''),
  581. );
  582. // Setup the activation status on this new account so it is correct - firstly is it an under age account?
  583. if ($regOptions['require'] == 'coppa')
  584. {
  585. $regOptions['register_vars']['is_activated'] = 5;
  586. // @todo This should be changed. To what should be it be changed??
  587. $regOptions['register_vars']['validation_code'] = '';
  588. }
  589. // Maybe it can be activated right away?
  590. elseif ($regOptions['require'] == 'nothing')
  591. $regOptions['register_vars']['is_activated'] = 1;
  592. // Maybe it must be activated by email?
  593. elseif ($regOptions['require'] == 'activation')
  594. $regOptions['register_vars']['is_activated'] = 0;
  595. // Otherwise it must be awaiting approval!
  596. else
  597. $regOptions['register_vars']['is_activated'] = 3;
  598. if (isset($regOptions['memberGroup']))
  599. {
  600. // Make sure the id_group will be valid, if this is an administator.
  601. $regOptions['register_vars']['id_group'] = $regOptions['memberGroup'] == 1 && !allowedTo('admin_forum') ? 0 : $regOptions['memberGroup'];
  602. // Check if this group is assignable.
  603. $unassignableGroups = array(-1, 3);
  604. $request = $smcFunc['db_query']('', '
  605. SELECT id_group
  606. FROM {db_prefix}membergroups
  607. WHERE min_posts != {int:min_posts}' . (allowedTo('admin_forum') ? '' : '
  608. OR group_type = {int:is_protected}'),
  609. array(
  610. 'min_posts' => -1,
  611. 'is_protected' => 1,
  612. )
  613. );
  614. while ($row = $smcFunc['db_fetch_assoc']($request))
  615. $unassignableGroups[] = $row['id_group'];
  616. $smcFunc['db_free_result']($request);
  617. if (in_array($regOptions['register_vars']['id_group'], $unassignableGroups))
  618. $regOptions['register_vars']['id_group'] = 0;
  619. }
  620. // ICQ cannot be zero.
  621. if (isset($regOptions['extra_register_vars']['icq']) && empty($regOptions['extra_register_vars']['icq']))
  622. $regOptions['extra_register_vars']['icq'] = '';
  623. // Integrate optional member settings to be set.
  624. if (!empty($regOptions['extra_register_vars']))
  625. foreach ($regOptions['extra_register_vars'] as $var => $value)
  626. $regOptions['register_vars'][$var] = $value;
  627. // Integrate optional user theme options to be set.
  628. $theme_vars = array();
  629. if (!empty($regOptions['theme_vars']))
  630. foreach ($regOptions['theme_vars'] as $var => $value)
  631. $theme_vars[$var] = $value;
  632. // Call an optional function to validate the users' input.
  633. call_integration_hook('integrate_register', array(&$regOptions, &$theme_vars));
  634. // Right, now let's prepare for insertion.
  635. $knownInts = array(
  636. 'date_registered', 'posts', 'id_group', 'last_login', 'instant_messages', 'unread_messages',
  637. 'new_pm', 'pm_prefs', 'gender', 'hide_email', 'show_online', 'pm_email_notify', 'karma_good', 'karma_bad',
  638. 'notify_announcements', 'notify_send_body', 'notify_regularity', 'notify_types',
  639. 'id_theme', 'is_activated', 'id_msg_last_visit', 'id_post_group', 'total_time_logged_in', 'warning',
  640. );
  641. $knownFloats = array(
  642. 'time_offset',
  643. );
  644. $column_names = array();
  645. $values = array();
  646. foreach ($regOptions['register_vars'] as $var => $val)
  647. {
  648. $type = 'string';
  649. if (in_array($var, $knownInts))
  650. $type = 'int';
  651. elseif (in_array($var, $knownFloats))
  652. $type = 'float';
  653. elseif ($var == 'birthdate')
  654. $type = 'date';
  655. $column_names[$var] = $type;
  656. $values[$var] = $val;
  657. }
  658. // Register them into the database.
  659. $smcFunc['db_insert']('',
  660. '{db_prefix}members',
  661. $column_names,
  662. $values,
  663. array('id_member')
  664. );
  665. $memberID = $smcFunc['db_insert_id']('{db_prefix}members', 'id_member');
  666. // Update the number of members and latest member's info - and pass the name, but remove the 's.
  667. if ($regOptions['register_vars']['is_activated'] == 1)
  668. updateStats('member', $memberID, $regOptions['register_vars']['real_name']);
  669. else
  670. updateStats('member');
  671. // Theme variables too?
  672. if (!empty($theme_vars))
  673. {
  674. $inserts = array();
  675. foreach ($theme_vars as $var => $val)
  676. $inserts[] = array($memberID, $var, $val);
  677. $smcFunc['db_insert']('insert',
  678. '{db_prefix}themes',
  679. array('id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  680. $inserts,
  681. array('id_member', 'variable')
  682. );
  683. }
  684. // If it's enabled, increase the registrations for today.
  685. trackStats(array('registers' => '+'));
  686. // Administrative registrations are a bit different...
  687. if ($regOptions['interface'] == 'admin')
  688. {
  689. if ($regOptions['require'] == 'activation')
  690. $email_message = 'admin_register_activate';
  691. elseif (!empty($regOptions['send_welcome_email']))
  692. $email_message = 'admin_register_immediate';
  693. if (isset($email_message))
  694. {
  695. $replacements = array(
  696. 'REALNAME' => $regOptions['register_vars']['real_name'],
  697. 'USERNAME' => $regOptions['username'],
  698. 'PASSWORD' => $regOptions['password'],
  699. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  700. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $memberID . ';code=' . $validation_code,
  701. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $memberID,
  702. 'ACTIVATIONCODE' => $validation_code,
  703. );
  704. $emaildata = loadEmailTemplate($email_message, $replacements);
  705. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  706. }
  707. // All admins are finished here.
  708. return $memberID;
  709. }
  710. // Can post straight away - welcome them to your fantastic community...
  711. if ($regOptions['require'] == 'nothing')
  712. {
  713. if (!empty($regOptions['send_welcome_email']))
  714. {
  715. $replacements = array(
  716. 'REALNAME' => $regOptions['register_vars']['real_name'],
  717. 'USERNAME' => $regOptions['username'],
  718. 'PASSWORD' => $regOptions['password'],
  719. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  720. 'OPENID' => !empty($regOptions['openid']) ? $regOptions['openid'] : '',
  721. );
  722. $emaildata = loadEmailTemplate('register_' . ($regOptions['auth_method'] == 'openid' ? 'openid_' : '') . 'immediate', $replacements);
  723. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  724. }
  725. // Send admin their notification.
  726. adminNotify('standard', $memberID, $regOptions['username']);
  727. }
  728. // Need to activate their account - or fall under COPPA.
  729. elseif ($regOptions['require'] == 'activation' || $regOptions['require'] == 'coppa')
  730. {
  731. $replacements = array(
  732. 'REALNAME' => $regOptions['register_vars']['real_name'],
  733. 'USERNAME' => $regOptions['username'],
  734. 'PASSWORD' => $regOptions['password'],
  735. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  736. 'OPENID' => !empty($regOptions['openid']) ? $regOptions['openid'] : '',
  737. );
  738. if ($regOptions['require'] == 'activation')
  739. $replacements += array(
  740. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $memberID . ';code=' . $validation_code,
  741. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $memberID,
  742. 'ACTIVATIONCODE' => $validation_code,
  743. );
  744. else
  745. $replacements += array(
  746. 'COPPALINK' => $scripturl . '?action=coppa;u=' . $memberID,
  747. );
  748. $emaildata = loadEmailTemplate('register_' . ($regOptions['auth_method'] == 'openid' ? 'openid_' : '') . ($regOptions['require'] == 'activation' ? 'activate' : 'coppa'), $replacements);
  749. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  750. }
  751. // Must be awaiting approval.
  752. else
  753. {
  754. $replacements = array(
  755. 'REALNAME' => $regOptions['register_vars']['real_name'],
  756. 'USERNAME' => $regOptions['username'],
  757. 'PASSWORD' => $regOptions['password'],
  758. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  759. 'OPENID' => !empty($regOptions['openid']) ? $regOptions['openid'] : '',
  760. );
  761. $emaildata = loadEmailTemplate('register_' . ($regOptions['auth_method'] == 'openid' ? 'openid_' : '') . 'pending', $replacements);
  762. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  763. // Admin gets informed here...
  764. adminNotify('approval', $memberID, $regOptions['username']);
  765. }
  766. // Okay, they're for sure registered... make sure the session is aware of this for security. (Just married :P!)
  767. $_SESSION['just_registered'] = 1;
  768. return $memberID;
  769. }
  770. /**
  771. * Check if a name is in the reserved words list.
  772. * (name, current member id, name/username?.)
  773. * - checks if name is a reserved name or username.
  774. * - if is_name is false, the name is assumed to be a username.
  775. * - the id_member variable is used to ignore duplicate matches with the
  776. * current member.
  777. *
  778. * @param string $name
  779. * @param int $current_ID_MEMBER
  780. * @param bool $is_name
  781. * @param bool $fatal
  782. */
  783. function isReservedName($name, $current_ID_MEMBER = 0, $is_name = true, $fatal = true)
  784. {
  785. global $user_info, $modSettings, $smcFunc, $context;
  786. // No cheating with entities please.
  787. $replaceEntities = create_function('$string', '
  788. $num = substr($string, 0, 1) === \'x\' ? hexdec(substr($string, 1)) : (int) $string;' . (empty($context['utf8']) ? '
  789. return $num < 0x20 ? \'\' : ($num < 0x80 ? chr($num) : \'&#\' . $string . \';\');' : '
  790. return $num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) ? \'\' : ($num < 0x80 ? chr($num) : ($num < 0x800 ? chr(192 | $num >> 6) . chr(128 | $num & 63) : ($num < 0x10000 ? chr(224 | $num >> 12) . chr(128 | $num >> 6 & 63) . chr(128 | $num & 63) : chr(240 | $num >> 18) . chr(128 | $num >> 12 & 63) . chr(128 | $num >> 6 & 63) . chr(128 | $num & 63))));')
  791. );
  792. $name = preg_replace('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e', '$replaceEntities(\'\\2\')', $name);
  793. $checkName = $smcFunc['strtolower']($name);
  794. // Administrators are never restricted ;).
  795. if (!allowedTo('moderate_forum') && ((!empty($modSettings['reserveName']) && $is_name) || !empty($modSettings['reserveUser']) && !$is_name))
  796. {
  797. $reservedNames = explode("\n", $modSettings['reserveNames']);
  798. // Case sensitive check?
  799. $checkMe = empty($modSettings['reserveCase']) ? $checkName : $name;
  800. // Check each name in the list...
  801. foreach ($reservedNames as $reserved)
  802. {
  803. if ($reserved == '')
  804. continue;
  805. // The admin might've used entities too, level the playing field.
  806. $reservedCheck = preg_replace('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e', '$replaceEntities(\'\\2\')', $reserved);
  807. // Case sensitive name?
  808. if (empty($modSettings['reserveCase']))
  809. $reservedCheck = $smcFunc['strtolower']($reservedCheck);
  810. // If it's not just entire word, check for it in there somewhere...
  811. if ($checkMe == $reservedCheck || ($smcFunc['strpos']($checkMe, $reservedCheck) !== false && empty($modSettings['reserveWord'])))
  812. if ($fatal)
  813. fatal_lang_error('username_reserved', 'password', array($reserved));
  814. else
  815. return true;
  816. }
  817. $censor_name = $name;
  818. if (censorText($censor_name) != $name)
  819. if ($fatal)
  820. fatal_lang_error('name_censored', 'password', array($name));
  821. else
  822. return true;
  823. }
  824. // Characters we just shouldn't allow, regardless.
  825. foreach (array('*') as $char)
  826. if (strpos($checkName, $char) !== false)
  827. if ($fatal)
  828. fatal_lang_error('username_reserved', 'password', array($char));
  829. else
  830. return true;
  831. // Get rid of any SQL parts of the reserved name...
  832. $checkName = strtr($name, array('_' => '\\_', '%' => '\\%'));
  833. // Make sure they don't want someone else's name.
  834. $request = $smcFunc['db_query']('', '
  835. SELECT id_member
  836. FROM {db_prefix}members
  837. WHERE ' . (empty($current_ID_MEMBER) ? '' : 'id_member != {int:current_member}
  838. AND ') . '(real_name LIKE {string:check_name} OR member_name LIKE {string:check_name})
  839. LIMIT 1',
  840. array(
  841. 'current_member' => $current_ID_MEMBER,
  842. 'check_name' => $checkName,
  843. )
  844. );
  845. if ($smcFunc['db_num_rows']($request) > 0)
  846. {
  847. $smcFunc['db_free_result']($request);
  848. return true;
  849. }
  850. // Does name case insensitive match a member group name?
  851. $request = $smcFunc['db_query']('', '
  852. SELECT id_group
  853. FROM {db_prefix}membergroups
  854. WHERE group_name LIKE {string:check_name}
  855. LIMIT 1',
  856. array(
  857. 'check_name' => $checkName,
  858. )
  859. );
  860. if ($smcFunc['db_num_rows']($request) > 0)
  861. {
  862. $smcFunc['db_free_result']($request);
  863. return true;
  864. }
  865. // Okay, they passed.
  866. return false;
  867. }
  868. // Get a list of groups that have a given permission (on a given board).
  869. /**
  870. * Retrieves a list of membergroups that are allowed to do the given
  871. * permission. (on the given board)
  872. * If board_id is not null, a board permission is assumed.
  873. * The function takes different permission settings into account.
  874. *
  875. * @param string $permission
  876. * @param int $board_id = null
  877. * @return an array containing an array for the allowed membergroup ID's
  878. * and an array for the denied membergroup ID's.
  879. */
  880. function groupsAllowedTo($permission, $board_id = null)
  881. {
  882. global $modSettings, $board_info, $smcFunc;
  883. // Admins are allowed to do anything.
  884. $member_groups = array(
  885. 'allowed' => array(1),
  886. 'denied' => array(),
  887. );
  888. // Assume we're dealing with regular permissions (like profile_view_own).
  889. if ($board_id === null)
  890. {
  891. $request = $smcFunc['db_query']('', '
  892. SELECT id_group, add_deny
  893. FROM {db_prefix}permissions
  894. WHERE permission = {string:permission}',
  895. array(
  896. 'permission' => $permission,
  897. )
  898. );
  899. while ($row = $smcFunc['db_fetch_assoc']($request))
  900. $member_groups[$row['add_deny'] === '1' ? 'allowed' : 'denied'][] = $row['id_group'];
  901. $smcFunc['db_free_result']($request);
  902. }
  903. // Otherwise it's time to look at the board.
  904. else
  905. {
  906. // First get the profile of the given board.
  907. if (isset($board_info['id']) && $board_info['id'] == $board_id)
  908. $profile_id = $board_info['profile'];
  909. elseif ($board_id !== 0)
  910. {
  911. $request = $smcFunc['db_query']('', '
  912. SELECT id_profile
  913. FROM {db_prefix}boards
  914. WHERE id_board = {int:id_board}
  915. LIMIT 1',
  916. array(
  917. 'id_board' => $board_id,
  918. )
  919. );
  920. if ($smcFunc['db_num_rows']($request) == 0)
  921. fatal_lang_error('no_board');
  922. list ($profile_id) = $smcFunc['db_fetch_row']($request);
  923. $smcFunc['db_free_result']($request);
  924. }
  925. else
  926. $profile_id = 1;
  927. $request = $smcFunc['db_query']('', '
  928. SELECT bp.id_group, bp.add_deny
  929. FROM {db_prefix}board_permissions AS bp
  930. WHERE bp.permission = {string:permission}
  931. AND bp.id_profile = {int:profile_id}',
  932. array(
  933. 'profile_id' => $profile_id,
  934. 'permission' => $permission,
  935. )
  936. );
  937. while ($row = $smcFunc['db_fetch_assoc']($request))
  938. $member_groups[$row['add_deny'] === '1' ? 'allowed' : 'denied'][] = $row['id_group'];
  939. $smcFunc['db_free_result']($request);
  940. }
  941. // Denied is never allowed.
  942. $member_groups['allowed'] = array_diff($member_groups['allowed'], $member_groups['denied']);
  943. return $member_groups;
  944. }
  945. /**
  946. * Retrieves a list of members that have a given permission
  947. * (on a given board).
  948. * If board_id is not null, a board permission is assumed.
  949. * Takes different permission settings into account.
  950. * Takes possible moderators (on board 'board_id') into account.
  951. *
  952. * @param string $permission
  953. * @param int $board_id = null
  954. * @return an array containing member ID's.
  955. */
  956. function membersAllowedTo($permission, $board_id = null)
  957. {
  958. global $smcFunc;
  959. $member_groups = groupsAllowedTo($permission, $board_id);
  960. $include_moderators = in_array(3, $member_groups['allowed']) && $board_id !== null;
  961. $member_groups['allowed'] = array_diff($member_groups['allowed'], array(3));
  962. $exclude_moderators = in_array(3, $member_groups['denied']) && $board_id !== null;
  963. $member_groups['denied'] = array_diff($member_groups['denied'], array(3));
  964. $request = $smcFunc['db_query']('', '
  965. SELECT mem.id_member
  966. FROM {db_prefix}members AS mem' . ($include_moderators || $exclude_moderators ? '
  967. LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_member = mem.id_member AND mods.id_board = {int:board_id})' : '') . '
  968. WHERE (' . ($include_moderators ? 'mods.id_member IS NOT NULL OR ' : '') . 'mem.id_group IN ({array_int:member_groups_allowed}) OR FIND_IN_SET({raw:member_group_allowed_implode}, mem.additional_groups) != 0)' . (empty($member_groups['denied']) ? '' : '
  969. AND NOT (' . ($exclude_moderators ? 'mods.id_member IS NOT NULL OR ' : '') . 'mem.id_group IN ({array_int:member_groups_denied}) OR FIND_IN_SET({raw:member_group_denied_implode}, mem.additional_groups) != 0)'),
  970. array(
  971. 'member_groups_allowed' => $member_groups['allowed'],
  972. 'member_groups_denied' => $member_groups['denied'],
  973. 'board_id' => $board_id,
  974. 'member_group_allowed_implode' => implode(', mem.additional_groups) != 0 OR FIND_IN_SET(', $member_groups['allowed']),
  975. 'member_group_denied_implode' => implode(', mem.additional_groups) != 0 OR FIND_IN_SET(', $member_groups['denied']),
  976. )
  977. );
  978. $members = array();
  979. while ($row = $smcFunc['db_fetch_assoc']($request))
  980. $members[] = $row['id_member'];
  981. $smcFunc['db_free_result']($request);
  982. return $members;
  983. }
  984. /**
  985. * This function is used to reassociate members with relevant posts.
  986. * Reattribute guest posts to a specified member.
  987. * Does not check for any permissions.
  988. * If add_to_post_count is set, the member's post count is increased.
  989. *
  990. * @param int $memID
  991. * @param string $email = false
  992. * @param string $membername = false
  993. * @param bool $post_count = false
  994. * @return the number of successful reattributed posts.
  995. */
  996. function reattributePosts($memID, $email = false, $membername = false, $post_count = false)
  997. {
  998. global $smcFunc;
  999. // Firstly, if email and username aren't passed find out the members email address and name.
  1000. if ($email === false && $membername === false)
  1001. {
  1002. $request = $smcFunc['db_query']('', '
  1003. SELECT email_address, member_name
  1004. FROM {db_prefix}members
  1005. WHERE id_member = {int:memID}
  1006. LIMIT 1',
  1007. array(
  1008. 'memID' => $memID,
  1009. )
  1010. );
  1011. list ($email, $membername) = $smcFunc['db_fetch_row']($request);
  1012. $smcFunc['db_free_result']($request);
  1013. }
  1014. // If they want the post count restored then we need to do some research.
  1015. if ($post_count)
  1016. {
  1017. $request = $smcFunc['db_query']('', '
  1018. SELECT COUNT(*)
  1019. FROM {db_prefix}messages AS m
  1020. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND b.count_posts = {int:count_posts})
  1021. WHERE m.id_member = {int:guest_id}
  1022. AND m.approved = {int:is_approved}
  1023. AND m.icon != {string:recycled_icon}' . (empty($email) ? '' : '
  1024. AND m.poster_email = {string:email_address}') . (empty($membername) ? '' : '
  1025. AND m.poster_name = {string:member_name}'),
  1026. array(
  1027. 'count_posts' => 0,
  1028. 'guest_id' => 0,
  1029. 'email_address' => $email,
  1030. 'member_name' => $membername,
  1031. 'is_approved' => 1,
  1032. 'recycled_icon' => 'recycled',
  1033. )
  1034. );
  1035. list ($messageCount) = $smcFunc['db_fetch_row']($request);
  1036. $smcFunc['db_free_result']($request);
  1037. updateMemberData($memID, array('posts' => 'posts + ' . $messageCount));
  1038. }
  1039. $query_parts = array();
  1040. if (!empty($email))
  1041. $query_parts[] = 'poster_email = {string:email_address}';
  1042. if (!empty($membername))
  1043. $query_parts[] = 'poster_name = {string:member_name}';
  1044. $query = implode(' AND ', $query_parts);
  1045. // Finally, update the posts themselves!
  1046. $smcFunc['db_query']('', '
  1047. UPDATE {db_prefix}messages
  1048. SET id_member = {int:memID}
  1049. WHERE ' . $query,
  1050. array(
  1051. 'memID' => $memID,
  1052. 'email_address' => $email,
  1053. 'member_name' => $membername,
  1054. )
  1055. );
  1056. return $smcFunc['db_affected_rows']();
  1057. }
  1058. /**
  1059. * This simple function adds/removes the passed user from the current users buddy list.
  1060. * Requires profile_identity_own permission.
  1061. * Called by ?action=buddy;u=x;session_id=y.
  1062. * Redirects to ?action=profile;u=x.
  1063. */
  1064. function BuddyListToggle()
  1065. {
  1066. global $user_info;
  1067. checkSession('get');
  1068. isAllowedTo('profile_identity_own');
  1069. is_not_guest();
  1070. if (empty($_REQUEST['u']))
  1071. fatal_lang_error('no_access', false);
  1072. $_REQUEST['u'] = (int) $_REQUEST['u'];
  1073. // Remove if it's already there...
  1074. if (in_array($_REQUEST['u'], $user_info['buddies']))
  1075. $user_info['buddies'] = array_diff($user_info['buddies'], array($_REQUEST['u']));
  1076. // ...or add if it's not and if it's not you.
  1077. elseif ($user_info['id'] != $_REQUEST['u'])
  1078. $user_info['buddies'][] = (int) $_REQUEST['u'];
  1079. // Update the settings.
  1080. updateMemberData($user_info['id'], array('buddy_list' => implode(',', $user_info['buddies'])));
  1081. // Redirect back to the profile
  1082. redirectexit('action=profile;u=' . $_REQUEST['u']);
  1083. }
  1084. /**
  1085. * Callback for createList().
  1086. *
  1087. * @param $start
  1088. * @param $items_per_page
  1089. * @param $sort
  1090. * @param $where
  1091. * @param $where_params
  1092. * @param $get_duplicates
  1093. */
  1094. function list_getMembers($start, $items_per_page, $sort, $where, $where_params = array(), $get_duplicates = false)
  1095. {
  1096. global $smcFunc;
  1097. $request = $smcFunc['db_query']('', '
  1098. SELECT
  1099. mem.id_member, mem.member_name, mem.real_name, mem.email_address, mem.icq, mem.aim, mem.yim, mem.msn, mem.member_ip, mem.member_ip2, mem.last_login,
  1100. mem.posts, mem.is_activated, mem.date_registered, mem.id_group, mem.additional_groups, mg.group_name
  1101. FROM {db_prefix}members AS mem
  1102. LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
  1103. WHERE ' . $where . '
  1104. ORDER BY {raw:sort}
  1105. LIMIT {int:start}, {int:per_page}',
  1106. array_merge($where_params, array(
  1107. 'sort' => $sort,
  1108. 'start' => $start,
  1109. 'per_page' => $items_per_page,
  1110. ))
  1111. );
  1112. $members = array();
  1113. while ($row = $smcFunc['db_fetch_assoc']($request))
  1114. $members[] = $row;
  1115. $smcFunc['db_free_result']($request);
  1116. // If we want duplicates pass the members array off.
  1117. if ($get_duplicates)
  1118. populateDuplicateMembers($members);
  1119. return $members;
  1120. }
  1121. /**
  1122. * Callback for createList().
  1123. *
  1124. * @param $where
  1125. * @param $where_params
  1126. */
  1127. function list_getNumMembers($where, $where_params = array())
  1128. {
  1129. global $smcFunc, $modSettings;
  1130. // We know how many members there are in total.
  1131. if (empty($where) || $where == '1')
  1132. $num_members = $modSettings['totalMembers'];
  1133. // The database knows the amount when there are extra conditions.
  1134. else
  1135. {
  1136. $request = $smcFunc['db_query']('', '
  1137. SELECT COUNT(*)
  1138. FROM {db_prefix}members AS mem
  1139. WHERE ' . $where,
  1140. array_merge($where_params, array(
  1141. ))
  1142. );
  1143. list ($num_members) = $smcFunc['db_fetch_row']($request);
  1144. $smcFunc['db_free_result']($request);
  1145. }
  1146. return $num_members;
  1147. }
  1148. /**
  1149. *
  1150. * @param $members
  1151. */
  1152. function populateDuplicateMembers(&$members)
  1153. {
  1154. global $smcFunc;
  1155. // This will hold all the ip addresses.
  1156. $ips = array();
  1157. foreach ($members as $key => $member)
  1158. {
  1159. // Create the duplicate_members element.
  1160. $members[$key]['duplicate_members'] = array();
  1161. // Store the IPs.
  1162. if (!empty($member['member_ip']))
  1163. $ips[] = $member['member_ip'];
  1164. if (!empty($member['member_ip2']))
  1165. $ips[] = $member['member_ip2'];
  1166. }
  1167. $ips = array_unique($ips);
  1168. if (empty($ips))
  1169. return false;
  1170. // Fetch all members with this IP address, we'll filter out the current ones in a sec.
  1171. $request = $smcFunc['db_query']('', '
  1172. SELECT
  1173. id_member, member_name, email_address, member_ip, member_ip2, is_activated
  1174. FROM {db_prefix}members
  1175. WHERE member_ip IN ({array_string:ips})
  1176. OR member_ip2 IN ({array_string:ips})',
  1177. array(
  1178. 'ips' => $ips,
  1179. )
  1180. );
  1181. $duplicate_members = array();
  1182. $duplicate_ids = array();
  1183. while ($row = $smcFunc['db_fetch_assoc']($request))
  1184. {
  1185. //$duplicate_ids[] = $row['id_member'];
  1186. $member_context = array(
  1187. 'id' => $row['id_member'],
  1188. 'name' => $row['member_name'],
  1189. 'email' => $row['email_address'],
  1190. 'is_banned' => $row['is_activated'] > 10,
  1191. 'ip' => $row['member_ip'],
  1192. 'ip2' => $row['member_ip2'],
  1193. );
  1194. if (in_array($row['member_ip'], $ips))
  1195. $duplicate_members[$row['member_ip']][] = $member_context;
  1196. if ($row['member_ip'] != $row['member_ip2'] && in_array($row['member_ip2'], $ips))
  1197. $duplicate_members[$row['member_ip2']][] = $member_context;
  1198. }
  1199. $smcFunc['db_free_result']($request);
  1200. // Also try to get a list of messages using these ips.
  1201. $request = $smcFunc['db_query']('', '
  1202. SELECT
  1203. m.poster_ip, mem.id_member, mem.member_name, mem.email_address, mem.is_activated
  1204. FROM {db_prefix}messages AS m
  1205. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  1206. WHERE m.id_member != 0
  1207. ' . (!empty($duplicate_ids) ? 'AND m.id_member NOT IN ({array_int:duplicate_ids})' : '') . '
  1208. AND m.poster_ip IN ({array_string:ips})',
  1209. array(
  1210. 'duplicate_ids' => $duplicate_ids,
  1211. 'ips' => $ips,
  1212. )
  1213. );
  1214. $had_ips = array();
  1215. while ($row = $smcFunc['db_fetch_assoc']($request))
  1216. {
  1217. // Don't collect lots of the same.
  1218. if (isset($had_ips[$row['poster_ip']]) && in_array($row['id_member'], $had_ips[$row['poster_ip']]))
  1219. continue;
  1220. $had_ips[$row['poster_ip']][] = $row['id_member'];
  1221. $duplicate_members[$row['poster_ip']][] = array(
  1222. 'id' => $row['id_member'],
  1223. 'name' => $row['member_name'],
  1224. 'email' => $row['email_address'],
  1225. 'is_banned' => $row['is_activated'] > 10,
  1226. 'ip' => $row['poster_ip'],
  1227. 'ip2' => $row['poster_ip'],
  1228. );
  1229. }
  1230. $smcFunc['db_free_result']($request);
  1231. // Now we have all the duplicate members, stick them with their respective member in the list.
  1232. if (!empty($duplicate_members))
  1233. foreach ($members as $key => $member)
  1234. {
  1235. if (isset($duplicate_members[$member['member_ip']]))
  1236. $members[$key]['duplicate_members'] = $duplicate_members[$member['member_ip']];
  1237. if ($member['member_ip'] != $member['member_ip2'] && isset($duplicate_members[$member['member_ip2']]))
  1238. $members[$key]['duplicate_members'] = array_merge($member['duplicate_members'], $duplicate_members[$member['member_ip2']]);
  1239. // Check we don't have lots of the same member.
  1240. $member_track = array($member['id_member']);
  1241. foreach ($members[$key]['duplicate_members'] as $duplicate_id_member => $duplicate_member)
  1242. {
  1243. if (in_array($duplicate_member['id'], $member_track))
  1244. {
  1245. unset($members[$key]['duplicate_members'][$duplicate_id_member]);
  1246. continue;
  1247. }
  1248. $member_track[] = $duplicate_member['id'];
  1249. }
  1250. }
  1251. }
  1252. // Generate a random validation code.
  1253. // @todo Err. Whatcha doin' here.
  1254. function generateValidationCode()
  1255. {
  1256. global $smcFunc, $modSettings;
  1257. $request = $smcFunc['db_query']('get_random_number', '
  1258. SELECT RAND()',
  1259. array(
  1260. )
  1261. );
  1262. list ($dbRand) = $smcFunc['db_fetch_row']($request);
  1263. $smcFunc['db_free_result']($request);
  1264. return substr(preg_replace('/\W/', '', sha1(microtime() . mt_rand() . $dbRand . $modSettings['rand_seed'])), 0, 10);
  1265. }
  1266. ?>