Subs-Members.php 44 KB

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