Subs-Members.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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 2012 Simple Machines
  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('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. 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 the logs...
  198. $smcFunc['db_query']('', '
  199. DELETE FROM {db_prefix}log_actions
  200. WHERE id_log = {int:log_type}
  201. AND id_member IN ({array_int:users})',
  202. array(
  203. 'log_type' => 2,
  204. 'users' => $users,
  205. )
  206. );
  207. $smcFunc['db_query']('', '
  208. DELETE FROM {db_prefix}log_boards
  209. WHERE id_member IN ({array_int:users})',
  210. array(
  211. 'users' => $users,
  212. )
  213. );
  214. $smcFunc['db_query']('', '
  215. DELETE FROM {db_prefix}log_comments
  216. WHERE id_recipient IN ({array_int:users})
  217. AND comment_type = {string:warntpl}',
  218. array(
  219. 'users' => $users,
  220. 'warntpl' => 'warntpl',
  221. )
  222. );
  223. $smcFunc['db_query']('', '
  224. DELETE FROM {db_prefix}log_group_requests
  225. WHERE id_member IN ({array_int:users})',
  226. array(
  227. 'users' => $users,
  228. )
  229. );
  230. $smcFunc['db_query']('', '
  231. DELETE FROM {db_prefix}log_karma
  232. WHERE id_target IN ({array_int:users})
  233. OR id_executor IN ({array_int:users})',
  234. array(
  235. 'users' => $users,
  236. )
  237. );
  238. $smcFunc['db_query']('', '
  239. DELETE FROM {db_prefix}log_mark_read
  240. WHERE id_member IN ({array_int:users})',
  241. array(
  242. 'users' => $users,
  243. )
  244. );
  245. $smcFunc['db_query']('', '
  246. DELETE FROM {db_prefix}log_notify
  247. WHERE id_member IN ({array_int:users})',
  248. array(
  249. 'users' => $users,
  250. )
  251. );
  252. $smcFunc['db_query']('', '
  253. DELETE FROM {db_prefix}log_online
  254. WHERE id_member IN ({array_int:users})',
  255. array(
  256. 'users' => $users,
  257. )
  258. );
  259. $smcFunc['db_query']('', '
  260. DELETE FROM {db_prefix}log_subscribed
  261. WHERE id_member IN ({array_int:users})',
  262. array(
  263. 'users' => $users,
  264. )
  265. );
  266. $smcFunc['db_query']('', '
  267. DELETE FROM {db_prefix}log_topics
  268. WHERE id_member IN ({array_int:users})',
  269. array(
  270. 'users' => $users,
  271. )
  272. );
  273. $smcFunc['db_query']('', '
  274. DELETE FROM {db_prefix}collapsed_categories
  275. WHERE id_member IN ({array_int:users})',
  276. array(
  277. 'users' => $users,
  278. )
  279. );
  280. // Make their votes appear as guest votes - at least it keeps the totals right.
  281. // @todo Consider adding back in cookie protection.
  282. $smcFunc['db_query']('', '
  283. UPDATE {db_prefix}log_polls
  284. SET id_member = {int:guest_id}
  285. WHERE id_member IN ({array_int:users})',
  286. array(
  287. 'guest_id' => 0,
  288. 'users' => $users,
  289. )
  290. );
  291. // Delete personal messages.
  292. require_once($sourcedir . '/PersonalMessage.php');
  293. deleteMessages(null, null, $users);
  294. $smcFunc['db_query']('', '
  295. UPDATE {db_prefix}personal_messages
  296. SET id_member_from = {int:guest_id}
  297. WHERE id_member_from IN ({array_int:users})',
  298. array(
  299. 'guest_id' => 0,
  300. 'users' => $users,
  301. )
  302. );
  303. // They no longer exist, so we don't know who it was sent to.
  304. $smcFunc['db_query']('', '
  305. DELETE FROM {db_prefix}pm_recipients
  306. WHERE id_member IN ({array_int:users})',
  307. array(
  308. 'users' => $users,
  309. )
  310. );
  311. // Delete avatar.
  312. require_once($sourcedir . '/ManageAttachments.php');
  313. removeAttachments(array('id_member' => $users));
  314. // It's over, no more moderation for you.
  315. $smcFunc['db_query']('', '
  316. DELETE FROM {db_prefix}moderators
  317. WHERE id_member IN ({array_int:users})',
  318. array(
  319. 'users' => $users,
  320. )
  321. );
  322. $smcFunc['db_query']('', '
  323. DELETE FROM {db_prefix}group_moderators
  324. WHERE id_member IN ({array_int:users})',
  325. array(
  326. 'users' => $users,
  327. )
  328. );
  329. // If you don't exist we can't ban you.
  330. $smcFunc['db_query']('', '
  331. DELETE FROM {db_prefix}ban_items
  332. WHERE id_member IN ({array_int:users})',
  333. array(
  334. 'users' => $users,
  335. )
  336. );
  337. // Remove individual theme settings.
  338. $smcFunc['db_query']('', '
  339. DELETE FROM {db_prefix}themes
  340. WHERE id_member IN ({array_int:users})',
  341. array(
  342. 'users' => $users,
  343. )
  344. );
  345. // These users are nobody's buddy nomore.
  346. $request = $smcFunc['db_query']('', '
  347. SELECT id_member, pm_ignore_list, buddy_list
  348. FROM {db_prefix}members
  349. WHERE FIND_IN_SET({raw:pm_ignore_list}, pm_ignore_list) != 0 OR FIND_IN_SET({raw:buddy_list}, buddy_list) != 0',
  350. array(
  351. 'pm_ignore_list' => implode(', pm_ignore_list) != 0 OR FIND_IN_SET(', $users),
  352. 'buddy_list' => implode(', buddy_list) != 0 OR FIND_IN_SET(', $users),
  353. )
  354. );
  355. while ($row = $smcFunc['db_fetch_assoc']($request))
  356. $smcFunc['db_query']('', '
  357. UPDATE {db_prefix}members
  358. SET
  359. pm_ignore_list = {string:pm_ignore_list},
  360. buddy_list = {string:buddy_list}
  361. WHERE id_member = {int:id_member}',
  362. array(
  363. 'id_member' => $row['id_member'],
  364. 'pm_ignore_list' => implode(',', array_diff(explode(',', $row['pm_ignore_list']), $users)),
  365. 'buddy_list' => implode(',', array_diff(explode(',', $row['buddy_list']), $users)),
  366. )
  367. );
  368. $smcFunc['db_free_result']($request);
  369. // Make sure no member's birthday is still sticking in the calendar...
  370. updateSettings(array(
  371. 'calendar_updated' => time(),
  372. ));
  373. // Integration rocks!
  374. call_integration_hook('integrate_delete_members', array($users));
  375. updateStats('member');
  376. require_once($sourcedir . '/Logging.php');
  377. logActions($log_changes);
  378. }
  379. /**
  380. * Registers a member to the forum.
  381. * Allows two types of interface: 'guest' and 'admin'. The first
  382. * includes hammering protection, the latter can perform the
  383. * registration silently.
  384. * The strings used in the options array are assumed to be escaped.
  385. * Allows to perform several checks on the input, e.g. reserved names.
  386. * The function will adjust member statistics.
  387. * If an error is detected will fatal error on all errors unless return_errors is true.
  388. *
  389. * @param array $regOptions
  390. * @param bool $return_errors - specify whether to return the errors
  391. * @return int, the ID of the newly created member
  392. */
  393. function registerMember(&$regOptions, $return_errors = false)
  394. {
  395. global $scripturl, $txt, $modSettings, $context, $sourcedir;
  396. global $user_info, $options, $settings, $smcFunc;
  397. loadLanguage('Login');
  398. // We'll need some external functions.
  399. require_once($sourcedir . '/Subs-Auth.php');
  400. require_once($sourcedir . '/Subs-Post.php');
  401. // Put any errors in here.
  402. $reg_errors = array();
  403. // Registration from the admin center, let them sweat a little more.
  404. if ($regOptions['interface'] == 'admin')
  405. {
  406. is_not_guest();
  407. isAllowedTo('moderate_forum');
  408. }
  409. // If you're an admin, you're special ;).
  410. elseif ($regOptions['interface'] == 'guest')
  411. {
  412. // You cannot register twice...
  413. if (empty($user_info['is_guest']))
  414. redirectexit();
  415. // Make sure they didn't just register with this session.
  416. if (!empty($_SESSION['just_registered']) && empty($modSettings['disableRegisterCheck']))
  417. fatal_lang_error('register_only_once', false);
  418. }
  419. // What method of authorization are we going to use?
  420. if (empty($regOptions['auth_method']) || !in_array($regOptions['auth_method'], array('password', 'openid')))
  421. {
  422. if (!empty($regOptions['openid']))
  423. $regOptions['auth_method'] = 'openid';
  424. else
  425. $regOptions['auth_method'] = 'password';
  426. }
  427. // Spaces and other odd characters are evil...
  428. $regOptions['username'] = preg_replace('~[\t\n\r\x0B\0' . ($context['utf8'] ? '\x{A0}' : '\xA0') . ']+~' . ($context['utf8'] ? 'u' : ''), ' ', $regOptions['username']);
  429. // @todo Separate the sprintf?
  430. 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)
  431. $reg_errors[] = array('done', sprintf($txt['valid_email_needed'], $smcFunc['htmlspecialchars']($regOptions['username'])));
  432. $username_validation_errors = validateUsername(0, $regOptions['username'], true, !empty($regOptions['check_reserved_name']));
  433. if (!empty($username_validation_errors))
  434. $reg_errors = array_merge($reg_errors, $username_validation_errors);
  435. // Generate a validation code if it's supposed to be emailed.
  436. $validation_code = '';
  437. if ($regOptions['require'] == 'activation')
  438. $validation_code = generateValidationCode();
  439. // If you haven't put in a password generate one.
  440. if ($regOptions['interface'] == 'admin' && $regOptions['password'] == '' && $regOptions['auth_method'] == 'password')
  441. {
  442. mt_srand(time() + 1277);
  443. $regOptions['password'] = generateValidationCode();
  444. $regOptions['password_check'] = $regOptions['password'];
  445. }
  446. // Does the first password match the second?
  447. elseif ($regOptions['password'] != $regOptions['password_check'] && $regOptions['auth_method'] == 'password')
  448. $reg_errors[] = array('lang', 'passwords_dont_match');
  449. // That's kind of easy to guess...
  450. if ($regOptions['password'] == '')
  451. {
  452. if ($regOptions['auth_method'] == 'password')
  453. $reg_errors[] = array('lang', 'no_password');
  454. else
  455. $regOptions['password'] = sha1(mt_rand());
  456. }
  457. // Now perform hard password validation as required.
  458. if (!empty($regOptions['check_password_strength']))
  459. {
  460. $passwordError = validatePassword($regOptions['password'], $regOptions['username'], array($regOptions['email']));
  461. // Password isn't legal?
  462. if ($passwordError != null)
  463. $reg_errors[] = array('lang', 'profile_error_password_' . $passwordError);
  464. }
  465. // If they are using an OpenID that hasn't been verified yet error out.
  466. // @todo Change this so they can register without having to attempt a login first
  467. if ($regOptions['auth_method'] == 'openid' && (empty($_SESSION['openid']['verified']) || $_SESSION['openid']['openid_uri'] != $regOptions['openid']))
  468. $reg_errors[] = array('lang', 'openid_not_verified');
  469. // You may not be allowed to register this email.
  470. if (!empty($regOptions['check_email_ban']))
  471. isBannedEmail($regOptions['email'], 'cannot_register', $txt['ban_register_prohibited']);
  472. // Check if the email address is in use.
  473. $request = $smcFunc['db_query']('', '
  474. SELECT id_member
  475. FROM {db_prefix}members
  476. WHERE email_address = {string:email_address}
  477. OR email_address = {string:username}
  478. LIMIT 1',
  479. array(
  480. 'email_address' => $regOptions['email'],
  481. 'username' => $regOptions['username'],
  482. )
  483. );
  484. // @todo Separate the sprintf?
  485. if ($smcFunc['db_num_rows']($request) != 0)
  486. $reg_errors[] = array('lang', 'email_in_use', false, array(htmlspecialchars($regOptions['email'])));
  487. $smcFunc['db_free_result']($request);
  488. // If we found any errors we need to do something about it right away!
  489. foreach ($reg_errors as $key => $error)
  490. {
  491. /* Note for each error:
  492. 0 = 'lang' if it's an index, 'done' if it's clear text.
  493. 1 = The text/index.
  494. 2 = Whether to log.
  495. 3 = sprintf data if necessary. */
  496. if ($error[0] == 'lang')
  497. loadLanguage('Errors');
  498. $message = $error[0] == 'lang' ? (empty($error[3]) ? $txt[$error[1]] : vsprintf($txt[$error[1]], $error[3])) : $error[1];
  499. // What to do, what to do, what to do.
  500. if ($return_errors)
  501. {
  502. if (!empty($error[2]))
  503. log_error($message, $error[2]);
  504. $reg_errors[$key] = $message;
  505. }
  506. else
  507. fatal_error($message, empty($error[2]) ? false : $error[2]);
  508. }
  509. // If there's any errors left return them at once!
  510. if (!empty($reg_errors))
  511. return $reg_errors;
  512. $reservedVars = array(
  513. 'actual_theme_url',
  514. 'actual_images_url',
  515. 'base_theme_dir',
  516. 'base_theme_url',
  517. 'default_images_url',
  518. 'default_theme_dir',
  519. 'default_theme_url',
  520. 'default_template',
  521. 'images_url',
  522. 'number_recent_posts',
  523. 'smiley_sets_default',
  524. 'theme_dir',
  525. 'theme_id',
  526. 'theme_layers',
  527. 'theme_templates',
  528. 'theme_url',
  529. );
  530. // Can't change reserved vars.
  531. if (isset($regOptions['theme_vars']) && count(array_intersect(array_keys($regOptions['theme_vars']), $reservedVars)) != 0)
  532. fatal_lang_error('no_theme');
  533. // Some of these might be overwritten. (the lower ones that are in the arrays below.)
  534. $regOptions['register_vars'] = array(
  535. 'member_name' => $regOptions['username'],
  536. 'email_address' => $regOptions['email'],
  537. 'passwd' => sha1(strtolower($regOptions['username']) . $regOptions['password']),
  538. 'password_salt' => substr(md5(mt_rand()), 0, 4) ,
  539. 'posts' => 0,
  540. 'date_registered' => time(),
  541. 'member_ip' => $regOptions['interface'] == 'admin' ? '127.0.0.1' : $user_info['ip'],
  542. 'member_ip2' => $regOptions['interface'] == 'admin' ? '127.0.0.1' : $_SERVER['BAN_CHECK_IP'],
  543. 'validation_code' => $validation_code,
  544. 'real_name' => $regOptions['username'],
  545. 'personal_text' => $modSettings['default_personal_text'],
  546. 'pm_email_notify' => 1,
  547. 'id_theme' => 0,
  548. 'id_post_group' => 4,
  549. 'lngfile' => '',
  550. 'buddy_list' => '',
  551. 'pm_ignore_list' => '',
  552. 'message_labels' => '',
  553. 'website_title' => '',
  554. 'website_url' => '',
  555. 'location' => '',
  556. 'icq' => '',
  557. 'aim' => '',
  558. 'yim' => '',
  559. 'msn' => '',
  560. 'time_format' => '',
  561. 'signature' => '',
  562. 'avatar' => '',
  563. 'usertitle' => '',
  564. 'secret_question' => '',
  565. 'secret_answer' => '',
  566. 'additional_groups' => '',
  567. 'ignore_boards' => '',
  568. 'smiley_set' => '',
  569. 'openid_uri' => (!empty($regOptions['openid']) ? $regOptions['openid'] : ''),
  570. );
  571. // Setup the activation status on this new account so it is correct - firstly is it an under age account?
  572. if ($regOptions['require'] == 'coppa')
  573. {
  574. $regOptions['register_vars']['is_activated'] = 5;
  575. // @todo This should be changed. To what should be it be changed??
  576. $regOptions['register_vars']['validation_code'] = '';
  577. }
  578. // Maybe it can be activated right away?
  579. elseif ($regOptions['require'] == 'nothing')
  580. $regOptions['register_vars']['is_activated'] = 1;
  581. // Maybe it must be activated by email?
  582. elseif ($regOptions['require'] == 'activation')
  583. $regOptions['register_vars']['is_activated'] = 0;
  584. // Otherwise it must be awaiting approval!
  585. else
  586. $regOptions['register_vars']['is_activated'] = 3;
  587. if (isset($regOptions['memberGroup']))
  588. {
  589. // Make sure the id_group will be valid, if this is an administator.
  590. $regOptions['register_vars']['id_group'] = $regOptions['memberGroup'] == 1 && !allowedTo('admin_forum') ? 0 : $regOptions['memberGroup'];
  591. // Check if this group is assignable.
  592. $unassignableGroups = array(-1, 3);
  593. $request = $smcFunc['db_query']('', '
  594. SELECT id_group
  595. FROM {db_prefix}membergroups
  596. WHERE min_posts != {int:min_posts}' . (allowedTo('admin_forum') ? '' : '
  597. OR group_type = {int:is_protected}'),
  598. array(
  599. 'min_posts' => -1,
  600. 'is_protected' => 1,
  601. )
  602. );
  603. while ($row = $smcFunc['db_fetch_assoc']($request))
  604. $unassignableGroups[] = $row['id_group'];
  605. $smcFunc['db_free_result']($request);
  606. if (in_array($regOptions['register_vars']['id_group'], $unassignableGroups))
  607. $regOptions['register_vars']['id_group'] = 0;
  608. }
  609. // ICQ cannot be zero.
  610. if (isset($regOptions['extra_register_vars']['icq']) && empty($regOptions['extra_register_vars']['icq']))
  611. $regOptions['extra_register_vars']['icq'] = '';
  612. // Integrate optional member settings to be set.
  613. if (!empty($regOptions['extra_register_vars']))
  614. foreach ($regOptions['extra_register_vars'] as $var => $value)
  615. $regOptions['register_vars'][$var] = $value;
  616. // Integrate optional user theme options to be set.
  617. $theme_vars = array();
  618. if (!empty($regOptions['theme_vars']))
  619. foreach ($regOptions['theme_vars'] as $var => $value)
  620. $theme_vars[$var] = $value;
  621. // Right, now let's prepare for insertion.
  622. $knownInts = array(
  623. 'date_registered', 'posts', 'id_group', 'last_login', 'instant_messages', 'unread_messages',
  624. 'new_pm', 'pm_prefs', 'gender', 'hide_email', 'show_online', 'pm_email_notify', 'karma_good', 'karma_bad',
  625. 'notify_announcements', 'notify_send_body', 'notify_regularity', 'notify_types',
  626. 'id_theme', 'is_activated', 'id_msg_last_visit', 'id_post_group', 'total_time_logged_in', 'warning',
  627. );
  628. $knownFloats = array(
  629. 'time_offset',
  630. );
  631. // Call an optional function to validate the users' input.
  632. call_integration_hook('integrate_register', array(&$regOptions, &$theme_vars, &$knownInts, &$knownFloats));
  633. $column_names = array();
  634. $values = array();
  635. foreach ($regOptions['register_vars'] as $var => $val)
  636. {
  637. $type = 'string';
  638. if (in_array($var, $knownInts))
  639. $type = 'int';
  640. elseif (in_array($var, $knownFloats))
  641. $type = 'float';
  642. elseif ($var == 'birthdate')
  643. $type = 'date';
  644. $column_names[$var] = $type;
  645. $values[$var] = $val;
  646. }
  647. // Register them into the database.
  648. $smcFunc['db_insert']('',
  649. '{db_prefix}members',
  650. $column_names,
  651. $values,
  652. array('id_member')
  653. );
  654. $memberID = $smcFunc['db_insert_id']('{db_prefix}members', 'id_member');
  655. // Update the number of members and latest member's info - and pass the name, but remove the 's.
  656. if ($regOptions['register_vars']['is_activated'] == 1)
  657. updateStats('member', $memberID, $regOptions['register_vars']['real_name']);
  658. else
  659. updateStats('member');
  660. // Theme variables too?
  661. if (!empty($theme_vars))
  662. {
  663. $inserts = array();
  664. foreach ($theme_vars as $var => $val)
  665. $inserts[] = array($memberID, $var, $val);
  666. $smcFunc['db_insert']('insert',
  667. '{db_prefix}themes',
  668. array('id_member' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  669. $inserts,
  670. array('id_member', 'variable')
  671. );
  672. }
  673. // If it's enabled, increase the registrations for today.
  674. trackStats(array('registers' => '+'));
  675. // Administrative registrations are a bit different...
  676. if ($regOptions['interface'] == 'admin')
  677. {
  678. if ($regOptions['require'] == 'activation')
  679. $email_message = 'admin_register_activate';
  680. elseif (!empty($regOptions['send_welcome_email']))
  681. $email_message = 'admin_register_immediate';
  682. if (isset($email_message))
  683. {
  684. $replacements = array(
  685. 'REALNAME' => $regOptions['register_vars']['real_name'],
  686. 'USERNAME' => $regOptions['username'],
  687. 'PASSWORD' => $regOptions['password'],
  688. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  689. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $memberID . ';code=' . $validation_code,
  690. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $memberID,
  691. 'ACTIVATIONCODE' => $validation_code,
  692. );
  693. $emaildata = loadEmailTemplate($email_message, $replacements);
  694. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  695. }
  696. // All admins are finished here.
  697. return $memberID;
  698. }
  699. // Can post straight away - welcome them to your fantastic community...
  700. if ($regOptions['require'] == 'nothing')
  701. {
  702. if (!empty($regOptions['send_welcome_email']))
  703. {
  704. $replacements = array(
  705. 'REALNAME' => $regOptions['register_vars']['real_name'],
  706. 'USERNAME' => $regOptions['username'],
  707. 'PASSWORD' => $regOptions['password'],
  708. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  709. 'OPENID' => !empty($regOptions['openid']) ? $regOptions['openid'] : '',
  710. );
  711. $emaildata = loadEmailTemplate('register_' . ($regOptions['auth_method'] == 'openid' ? 'openid_' : '') . 'immediate', $replacements);
  712. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  713. }
  714. // Send admin their notification.
  715. adminNotify('standard', $memberID, $regOptions['username']);
  716. }
  717. // Need to activate their account - or fall under COPPA.
  718. elseif ($regOptions['require'] == 'activation' || $regOptions['require'] == 'coppa')
  719. {
  720. $replacements = array(
  721. 'REALNAME' => $regOptions['register_vars']['real_name'],
  722. 'USERNAME' => $regOptions['username'],
  723. 'PASSWORD' => $regOptions['password'],
  724. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  725. 'OPENID' => !empty($regOptions['openid']) ? $regOptions['openid'] : '',
  726. );
  727. if ($regOptions['require'] == 'activation')
  728. $replacements += array(
  729. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $memberID . ';code=' . $validation_code,
  730. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $memberID,
  731. 'ACTIVATIONCODE' => $validation_code,
  732. );
  733. else
  734. $replacements += array(
  735. 'COPPALINK' => $scripturl . '?action=coppa;u=' . $memberID,
  736. );
  737. $emaildata = loadEmailTemplate('register_' . ($regOptions['auth_method'] == 'openid' ? 'openid_' : '') . ($regOptions['require'] == 'activation' ? 'activate' : 'coppa'), $replacements);
  738. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  739. }
  740. // Must be awaiting approval.
  741. else
  742. {
  743. $replacements = array(
  744. 'REALNAME' => $regOptions['register_vars']['real_name'],
  745. 'USERNAME' => $regOptions['username'],
  746. 'PASSWORD' => $regOptions['password'],
  747. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  748. 'OPENID' => !empty($regOptions['openid']) ? $regOptions['openid'] : '',
  749. );
  750. $emaildata = loadEmailTemplate('register_' . ($regOptions['auth_method'] == 'openid' ? 'openid_' : '') . 'pending', $replacements);
  751. sendmail($regOptions['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  752. // Admin gets informed here...
  753. adminNotify('approval', $memberID, $regOptions['username']);
  754. }
  755. // Okay, they're for sure registered... make sure the session is aware of this for security. (Just married :P!)
  756. $_SESSION['just_registered'] = 1;
  757. return $memberID;
  758. }
  759. /**
  760. * Check if a name is in the reserved words list.
  761. * (name, current member id, name/username?.)
  762. * - checks if name is a reserved name or username.
  763. * - if is_name is false, the name is assumed to be a username.
  764. * - the id_member variable is used to ignore duplicate matches with the
  765. * current member.
  766. *
  767. * @param string $name
  768. * @param int $current_ID_MEMBER
  769. * @param bool $is_name
  770. * @param bool $fatal
  771. */
  772. function isReservedName($name, $current_ID_MEMBER = 0, $is_name = true, $fatal = true)
  773. {
  774. global $user_info, $modSettings, $smcFunc, $context;
  775. // No cheating with entities please.
  776. $replaceEntities = create_function('$string', '
  777. $num = substr($string, 0, 1) === \'x\' ? hexdec(substr($string, 1)) : (int) $string;' . (empty($context['utf8']) ? '
  778. return $num < 0x20 ? \'\' : ($num < 0x80 ? chr($num) : \'&#\' . $string . \';\');' : '
  779. 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))));')
  780. );
  781. $name = preg_replace('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e', '$replaceEntities(\'\\2\')', $name);
  782. $checkName = $smcFunc['strtolower']($name);
  783. // Administrators are never restricted ;).
  784. if (!allowedTo('moderate_forum') && ((!empty($modSettings['reserveName']) && $is_name) || !empty($modSettings['reserveUser']) && !$is_name))
  785. {
  786. $reservedNames = explode("\n", $modSettings['reserveNames']);
  787. // Case sensitive check?
  788. $checkMe = empty($modSettings['reserveCase']) ? $checkName : $name;
  789. // Check each name in the list...
  790. foreach ($reservedNames as $reserved)
  791. {
  792. if ($reserved == '')
  793. continue;
  794. // The admin might've used entities too, level the playing field.
  795. $reservedCheck = preg_replace('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e', '$replaceEntities(\'\\2\')', $reserved);
  796. // Case sensitive name?
  797. if (empty($modSettings['reserveCase']))
  798. $reservedCheck = $smcFunc['strtolower']($reservedCheck);
  799. // If it's not just entire word, check for it in there somewhere...
  800. if ($checkMe == $reservedCheck || ($smcFunc['strpos']($checkMe, $reservedCheck) !== false && empty($modSettings['reserveWord'])))
  801. if ($fatal)
  802. fatal_lang_error('username_reserved', 'password', array($reserved));
  803. else
  804. return true;
  805. }
  806. $censor_name = $name;
  807. if (censorText($censor_name) != $name)
  808. if ($fatal)
  809. fatal_lang_error('name_censored', 'password', array($name));
  810. else
  811. return true;
  812. }
  813. // Characters we just shouldn't allow, regardless.
  814. foreach (array('*') as $char)
  815. if (strpos($checkName, $char) !== false)
  816. if ($fatal)
  817. fatal_lang_error('username_reserved', 'password', array($char));
  818. else
  819. return true;
  820. // Get rid of any SQL parts of the reserved name...
  821. $checkName = strtr($name, array('_' => '\\_', '%' => '\\%'));
  822. // Make sure they don't want someone else's name.
  823. $request = $smcFunc['db_query']('', '
  824. SELECT id_member
  825. FROM {db_prefix}members
  826. WHERE ' . (empty($current_ID_MEMBER) ? '' : 'id_member != {int:current_member}
  827. AND ') . '(real_name LIKE {string:check_name} OR member_name LIKE {string:check_name})
  828. LIMIT 1',
  829. array(
  830. 'current_member' => $current_ID_MEMBER,
  831. 'check_name' => $checkName,
  832. )
  833. );
  834. if ($smcFunc['db_num_rows']($request) > 0)
  835. {
  836. $smcFunc['db_free_result']($request);
  837. return true;
  838. }
  839. // Does name case insensitive match a member group name?
  840. $request = $smcFunc['db_query']('', '
  841. SELECT id_group
  842. FROM {db_prefix}membergroups
  843. WHERE group_name LIKE {string:check_name}
  844. LIMIT 1',
  845. array(
  846. 'check_name' => $checkName,
  847. )
  848. );
  849. if ($smcFunc['db_num_rows']($request) > 0)
  850. {
  851. $smcFunc['db_free_result']($request);
  852. return true;
  853. }
  854. // Okay, they passed.
  855. return false;
  856. }
  857. // Get a list of groups that have a given permission (on a given board).
  858. /**
  859. * Retrieves a list of membergroups that are allowed to do the given
  860. * permission. (on the given board)
  861. * If board_id is not null, a board permission is assumed.
  862. * The function takes different permission settings into account.
  863. *
  864. * @param string $permission
  865. * @param int $board_id = null
  866. * @return an array containing an array for the allowed membergroup ID's
  867. * and an array for the denied membergroup ID's.
  868. */
  869. function groupsAllowedTo($permission, $board_id = null)
  870. {
  871. global $modSettings, $board_info, $smcFunc;
  872. // Admins are allowed to do anything.
  873. $member_groups = array(
  874. 'allowed' => array(1),
  875. 'denied' => array(),
  876. );
  877. // Assume we're dealing with regular permissions (like profile_view_own).
  878. if ($board_id === null)
  879. {
  880. $request = $smcFunc['db_query']('', '
  881. SELECT id_group, add_deny
  882. FROM {db_prefix}permissions
  883. WHERE permission = {string:permission}',
  884. array(
  885. 'permission' => $permission,
  886. )
  887. );
  888. while ($row = $smcFunc['db_fetch_assoc']($request))
  889. $member_groups[$row['add_deny'] === '1' ? 'allowed' : 'denied'][] = $row['id_group'];
  890. $smcFunc['db_free_result']($request);
  891. }
  892. // Otherwise it's time to look at the board.
  893. else
  894. {
  895. // First get the profile of the given board.
  896. if (isset($board_info['id']) && $board_info['id'] == $board_id)
  897. $profile_id = $board_info['profile'];
  898. elseif ($board_id !== 0)
  899. {
  900. $request = $smcFunc['db_query']('', '
  901. SELECT id_profile
  902. FROM {db_prefix}boards
  903. WHERE id_board = {int:id_board}
  904. LIMIT 1',
  905. array(
  906. 'id_board' => $board_id,
  907. )
  908. );
  909. if ($smcFunc['db_num_rows']($request) == 0)
  910. fatal_lang_error('no_board');
  911. list ($profile_id) = $smcFunc['db_fetch_row']($request);
  912. $smcFunc['db_free_result']($request);
  913. }
  914. else
  915. $profile_id = 1;
  916. $request = $smcFunc['db_query']('', '
  917. SELECT bp.id_group, bp.add_deny
  918. FROM {db_prefix}board_permissions AS bp
  919. WHERE bp.permission = {string:permission}
  920. AND bp.id_profile = {int:profile_id}',
  921. array(
  922. 'profile_id' => $profile_id,
  923. 'permission' => $permission,
  924. )
  925. );
  926. while ($row = $smcFunc['db_fetch_assoc']($request))
  927. $member_groups[$row['add_deny'] === '1' ? 'allowed' : 'denied'][] = $row['id_group'];
  928. $smcFunc['db_free_result']($request);
  929. }
  930. // Denied is never allowed.
  931. $member_groups['allowed'] = array_diff($member_groups['allowed'], $member_groups['denied']);
  932. return $member_groups;
  933. }
  934. /**
  935. * Retrieves a list of members that have a given permission
  936. * (on a given board).
  937. * If board_id is not null, a board permission is assumed.
  938. * Takes different permission settings into account.
  939. * Takes possible moderators (on board 'board_id') into account.
  940. *
  941. * @param string $permission
  942. * @param int $board_id = null
  943. * @return an array containing member ID's.
  944. */
  945. function membersAllowedTo($permission, $board_id = null)
  946. {
  947. global $smcFunc;
  948. $member_groups = groupsAllowedTo($permission, $board_id);
  949. $include_moderators = in_array(3, $member_groups['allowed']) && $board_id !== null;
  950. $member_groups['allowed'] = array_diff($member_groups['allowed'], array(3));
  951. $exclude_moderators = in_array(3, $member_groups['denied']) && $board_id !== null;
  952. $member_groups['denied'] = array_diff($member_groups['denied'], array(3));
  953. $request = $smcFunc['db_query']('', '
  954. SELECT mem.id_member
  955. FROM {db_prefix}members AS mem' . ($include_moderators || $exclude_moderators ? '
  956. LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_member = mem.id_member AND mods.id_board = {int:board_id})' : '') . '
  957. 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']) ? '' : '
  958. 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)'),
  959. array(
  960. 'member_groups_allowed' => $member_groups['allowed'],
  961. 'member_groups_denied' => $member_groups['denied'],
  962. 'board_id' => $board_id,
  963. 'member_group_allowed_implode' => implode(', mem.additional_groups) != 0 OR FIND_IN_SET(', $member_groups['allowed']),
  964. 'member_group_denied_implode' => implode(', mem.additional_groups) != 0 OR FIND_IN_SET(', $member_groups['denied']),
  965. )
  966. );
  967. $members = array();
  968. while ($row = $smcFunc['db_fetch_assoc']($request))
  969. $members[] = $row['id_member'];
  970. $smcFunc['db_free_result']($request);
  971. return $members;
  972. }
  973. /**
  974. * This function is used to reassociate members with relevant posts.
  975. * Reattribute guest posts to a specified member.
  976. * Does not check for any permissions.
  977. * If add_to_post_count is set, the member's post count is increased.
  978. *
  979. * @param int $memID
  980. * @param string $email = false
  981. * @param string $membername = false
  982. * @param bool $post_count = false
  983. * @return nothing
  984. */
  985. function reattributePosts($memID, $email = false, $membername = false, $post_count = false)
  986. {
  987. global $smcFunc;
  988. // Firstly, if email and username aren't passed find out the members email address and name.
  989. if ($email === false && $membername === false)
  990. {
  991. $request = $smcFunc['db_query']('', '
  992. SELECT email_address, member_name
  993. FROM {db_prefix}members
  994. WHERE id_member = {int:memID}
  995. LIMIT 1',
  996. array(
  997. 'memID' => $memID,
  998. )
  999. );
  1000. list ($email, $membername) = $smcFunc['db_fetch_row']($request);
  1001. $smcFunc['db_free_result']($request);
  1002. }
  1003. // If they want the post count restored then we need to do some research.
  1004. if ($post_count)
  1005. {
  1006. $request = $smcFunc['db_query']('', '
  1007. SELECT COUNT(*)
  1008. FROM {db_prefix}messages AS m
  1009. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board AND b.count_posts = {int:count_posts})
  1010. WHERE m.id_member = {int:guest_id}
  1011. AND m.approved = {int:is_approved}
  1012. AND m.icon != {string:recycled_icon}' . (empty($email) ? '' : '
  1013. AND m.poster_email = {string:email_address}') . (empty($membername) ? '' : '
  1014. AND m.poster_name = {string:member_name}'),
  1015. array(
  1016. 'count_posts' => 0,
  1017. 'guest_id' => 0,
  1018. 'email_address' => $email,
  1019. 'member_name' => $membername,
  1020. 'is_approved' => 1,
  1021. 'recycled_icon' => 'recycled',
  1022. )
  1023. );
  1024. list ($messageCount) = $smcFunc['db_fetch_row']($request);
  1025. $smcFunc['db_free_result']($request);
  1026. updateMemberData($memID, array('posts' => 'posts + ' . $messageCount));
  1027. }
  1028. $query_parts = array();
  1029. if (!empty($email))
  1030. $query_parts[] = 'poster_email = {string:email_address}';
  1031. if (!empty($membername))
  1032. $query_parts[] = 'poster_name = {string:member_name}';
  1033. $query = implode(' AND ', $query_parts);
  1034. // Finally, update the posts themselves!
  1035. $smcFunc['db_query']('', '
  1036. UPDATE {db_prefix}messages
  1037. SET id_member = {int:memID}
  1038. WHERE ' . $query,
  1039. array(
  1040. 'memID' => $memID,
  1041. 'email_address' => $email,
  1042. 'member_name' => $membername,
  1043. )
  1044. );
  1045. // ...and the topics too!
  1046. $smcFunc['db_query']('', '
  1047. UPDATE {db_prefix}topics as t, {db_prefix}messages as m
  1048. SET t.id_member_started = {int:memID}
  1049. WHERE m.id_member = {int:memID}
  1050. AND t.id_first_msg = m.id_msg',
  1051. array(
  1052. 'memID' => $memID,
  1053. )
  1054. );
  1055. // Allow mods with their own post tables to reattribute posts as well :)
  1056. call_integration_hook('integrate_reattribute_posts', array(&$memID, &$email, &$membername, &$post_count));
  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 == '1' ? '1=1' : $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. * Find potential duplicate registation members based on the same IP address
  1150. *
  1151. * @param $members
  1152. */
  1153. function populateDuplicateMembers(&$members)
  1154. {
  1155. global $smcFunc;
  1156. // This will hold all the ip addresses.
  1157. $ips = array();
  1158. foreach ($members as $key => $member)
  1159. {
  1160. // Create the duplicate_members element.
  1161. $members[$key]['duplicate_members'] = array();
  1162. // Store the IPs.
  1163. if (!empty($member['member_ip']))
  1164. $ips[] = $member['member_ip'];
  1165. if (!empty($member['member_ip2']))
  1166. $ips[] = $member['member_ip2'];
  1167. }
  1168. $ips = array_unique($ips);
  1169. if (empty($ips))
  1170. return false;
  1171. // Fetch all members with this IP address, we'll filter out the current ones in a sec.
  1172. $request = $smcFunc['db_query']('', '
  1173. SELECT
  1174. id_member, member_name, email_address, member_ip, member_ip2, is_activated
  1175. FROM {db_prefix}members
  1176. WHERE member_ip IN ({array_string:ips})
  1177. OR member_ip2 IN ({array_string:ips})',
  1178. array(
  1179. 'ips' => $ips,
  1180. )
  1181. );
  1182. $duplicate_members = array();
  1183. $duplicate_ids = array();
  1184. while ($row = $smcFunc['db_fetch_assoc']($request))
  1185. {
  1186. //$duplicate_ids[] = $row['id_member'];
  1187. $member_context = array(
  1188. 'id' => $row['id_member'],
  1189. 'name' => $row['member_name'],
  1190. 'email' => $row['email_address'],
  1191. 'is_banned' => $row['is_activated'] > 10,
  1192. 'ip' => $row['member_ip'],
  1193. 'ip2' => $row['member_ip2'],
  1194. );
  1195. if (in_array($row['member_ip'], $ips))
  1196. $duplicate_members[$row['member_ip']][] = $member_context;
  1197. if ($row['member_ip'] != $row['member_ip2'] && in_array($row['member_ip2'], $ips))
  1198. $duplicate_members[$row['member_ip2']][] = $member_context;
  1199. }
  1200. $smcFunc['db_free_result']($request);
  1201. // Also try to get a list of messages using these ips.
  1202. $request = $smcFunc['db_query']('', '
  1203. SELECT
  1204. m.poster_ip, mem.id_member, mem.member_name, mem.email_address, mem.is_activated
  1205. FROM {db_prefix}messages AS m
  1206. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  1207. WHERE m.id_member != 0
  1208. ' . (!empty($duplicate_ids) ? 'AND m.id_member NOT IN ({array_int:duplicate_ids})' : '') . '
  1209. AND m.poster_ip IN ({array_string:ips})',
  1210. array(
  1211. 'duplicate_ids' => $duplicate_ids,
  1212. 'ips' => $ips,
  1213. )
  1214. );
  1215. $had_ips = array();
  1216. while ($row = $smcFunc['db_fetch_assoc']($request))
  1217. {
  1218. // Don't collect lots of the same.
  1219. if (isset($had_ips[$row['poster_ip']]) && in_array($row['id_member'], $had_ips[$row['poster_ip']]))
  1220. continue;
  1221. $had_ips[$row['poster_ip']][] = $row['id_member'];
  1222. $duplicate_members[$row['poster_ip']][] = array(
  1223. 'id' => $row['id_member'],
  1224. 'name' => $row['member_name'],
  1225. 'email' => $row['email_address'],
  1226. 'is_banned' => $row['is_activated'] > 10,
  1227. 'ip' => $row['poster_ip'],
  1228. 'ip2' => $row['poster_ip'],
  1229. );
  1230. }
  1231. $smcFunc['db_free_result']($request);
  1232. // Now we have all the duplicate members, stick them with their respective member in the list.
  1233. if (!empty($duplicate_members))
  1234. foreach ($members as $key => $member)
  1235. {
  1236. if (isset($duplicate_members[$member['member_ip']]))
  1237. $members[$key]['duplicate_members'] = $duplicate_members[$member['member_ip']];
  1238. if ($member['member_ip'] != $member['member_ip2'] && isset($duplicate_members[$member['member_ip2']]))
  1239. $members[$key]['duplicate_members'] = array_merge($member['duplicate_members'], $duplicate_members[$member['member_ip2']]);
  1240. // Check we don't have lots of the same member.
  1241. $member_track = array($member['id_member']);
  1242. foreach ($members[$key]['duplicate_members'] as $duplicate_id_member => $duplicate_member)
  1243. {
  1244. if (in_array($duplicate_member['id'], $member_track))
  1245. {
  1246. unset($members[$key]['duplicate_members'][$duplicate_id_member]);
  1247. continue;
  1248. }
  1249. $member_track[] = $duplicate_member['id'];
  1250. }
  1251. }
  1252. }
  1253. /**
  1254. * Generate a random validation code.
  1255. * @todo Err. Whatcha doin' here.
  1256. *
  1257. * @return type
  1258. */
  1259. function generateValidationCode()
  1260. {
  1261. global $smcFunc, $modSettings;
  1262. $request = $smcFunc['db_query']('get_random_number', '
  1263. SELECT RAND()',
  1264. array(
  1265. )
  1266. );
  1267. list ($dbRand) = $smcFunc['db_fetch_row']($request);
  1268. $smcFunc['db_free_result']($request);
  1269. return substr(preg_replace('/\W/', '', sha1(microtime() . mt_rand() . $dbRand . $modSettings['rand_seed'])), 0, 10);
  1270. }
  1271. ?>