Subs-Members.php 44 KB

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