Subs-Members.php 46 KB

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