Guest User

Untitled

a guest
May 15th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.11 KB | None | 0 0
  1. <?php
  2. // This is just because SMF in general hates magic quotes at runtime.
  3. @set_magic_quotes_runtime(0);
  4.  
  5. // Hopefully the forum is in the same place as this script.
  6. //require_once(dirname(__FILE__) . '/Settings.php');
  7.  
  8. function DebugNDie($param){
  9. var_dump($param);
  10. die();
  11. }
  12.  
  13. global $smf_settings, $smf_user_info, $smf_connection;
  14.  
  15. $smf_settings = array();
  16. $smf_settings['cookiename'] = $cookiename;
  17. $smf_settings['language'] = $language;
  18. $smf_settings['forum_name'] = $mbname;
  19. $smf_settings['forum_url'] = $boardurl;
  20. $smf_settings['webmaster_email'] = $webmaster_email;
  21. $smf_settings['db_prefix'] = '`' . $db_name . '`.' . $db_prefix;
  22.  
  23. //echo "SMSSTT:"; var_dump($smf_settings); die();
  24. // If $maintenance is set to 2, don't connect to the database at all.
  25. if ($maintenance != 2)
  26. {
  27. // Ignore connection errors, because this is just an API file.
  28. if (empty($db_persist))
  29. $smf_connection = @mysql_connect($db_server, $db_user, $db_passwd);
  30. else
  31. $smf_connection = @mysql_pconnect($db_server, $db_user, $db_passwd);
  32.  
  33. $request = smf_query("
  34. SELECT variable, value
  35. FROM $smf_settings[db_prefix]settings", __FILE__, __LINE__);
  36.  
  37. while ($row = @mysql_fetch_row($request))
  38. $smf_settings[$row[0]] = $row[1];
  39.  
  40. mysql_free_result($request);
  41. }
  42.  
  43. // Load stuff from the Settings.php file into $smf_settings.
  44.  
  45.  
  46. // Actually set the login cookie...
  47. function smf_setLoginCookie($cookie_length, $id, $password = '', $encrypted = true)
  48. {
  49. // This should come from Settings.php, hopefully.
  50. global $smf_connection, $smf_settings;
  51.  
  52.  
  53. // The $id is not numeric; it's probably a username.
  54. if (!$smf_connection)
  55. return false;
  56.  
  57. // It wasn't found, after all?
  58. if (empty($id))
  59. {
  60. $id = (int) $username;
  61. unset($username);
  62. }
  63.  
  64. // Oh well, I guess it just was not to be...
  65. if (empty($id)) return false;
  66.  
  67. // The password isn't encrypted, do so.
  68. if (!$encrypted)
  69. {
  70. if (!$smf_connection)
  71. return false;
  72.  
  73. $result = smf_query("
  74. SELECT memberName, password_salt
  75. FROM $smf_settings[db_prefix]members
  76. WHERE ID_MEMBER = '" . (int) $id . "'
  77. LIMIT 1", __FILE__, __LINE__);
  78. list ($username, $salt) = mysql_fetch_row($result);
  79. mysql_free_result($result);
  80.  
  81. if (empty($username))
  82. return false;
  83.  
  84. //$password = sha1(sha1(strtolower($username) . $password) . $salt);
  85. $password = sha1($password.$salt);
  86. }
  87.  
  88. function smf_cookie_url($local, $global)
  89. {
  90. // Use PHP to parse the URL, hopefully it does its job.
  91. global $smf_settings;
  92. $parsed_url = parse_url($smf_settings['forum_url']);
  93.  
  94. // Set the cookie to the forum's path only?
  95. if (empty($parsed_url['path']) || !$local)
  96. $parsed_url['path'] = '';
  97.  
  98. // This is probably very likely for apis and such, no?
  99. if ($global)
  100. {
  101. // Try to figure out where to set the cookie; this can be confused, though.
  102. if (preg_match('~(?:[^\.]+\.)?(.+)\z~i', $parsed_url['host'], $parts) == 1)
  103. $parsed_url['host'] = '.' . $parts[1];
  104. }
  105. // If both options are off, just use no host and /.
  106. elseif (!$local)
  107. $parsed_url['host'] = '';
  108. return $parsed_url;
  109. }
  110.  
  111. // The cookie may already exist, and have been set with different options.
  112. $cookie_state = (empty($smf_settings['localCookies']) ? 0 : 1) | (empty($smf_settings['globalCookies']) ? 0 : 2);
  113. if (isset($_COOKIE[$smf_settings['cookiename']]))
  114. {
  115. $array = @unserialize($_COOKIE[$smf_settings['cookiename']]);
  116.  
  117. if (isset($array[3]) && $array[3] != $cookie_state)
  118. {
  119. $cookie_url = smf_cookie_url($array[3] & 1 > 0, $array[3] & 2 > 0);
  120. setcookie($smf_settings['cookiename'], serialize(array(0, '', 0)), time() - 3600, $parsed_url['path'] . '/', $parsed_url['host'], 0);
  121. }
  122. }
  123.  
  124. // Get the data and path to set it on.
  125. $data = serialize(empty($id) ? array(0, '', 0) : array($id, $password, time() + $cookie_length));
  126.  
  127. $parsed_url = smf_cookie_url(!empty($smf_settings['localCookies']), !empty($smf_settings['globalCookies']));
  128.  
  129. // var_dump($smf_settings);
  130.  
  131. // Set the cookie, $_COOKIE, and session variable.
  132. setcookie($smf_settings['cookiename'], $data, time() + $cookie_length, $parsed_url['path'] . '/', $parsed_url['host'], 0);
  133. $_COOKIE[$smf_settings['cookiename']] = $data;
  134. $_SESSION['login_' . $smf_settings['cookiename']] = $data;
  135.  
  136. return true;
  137. }
  138.  
  139. function smf_authenticateUser()
  140. {
  141. global $smf_connection, $smf_settings, $smf_user_info;
  142. //Empty $smf_user_info
  143. //var_dump($smf_user_info); die();
  144. // No connection, no authentication!
  145. if (!$smf_connection)
  146. return false;
  147.  
  148. // Check first the cookie, then the session.
  149. //var_dump($_COOKIE[$smf_settings['cookiename']]); die();
  150. if (isset($_COOKIE[$smf_settings['cookiename']]))
  151. {
  152. $_COOKIE[$smf_settings['cookiename']] = stripslashes($_COOKIE[$smf_settings['cookiename']]);
  153.  
  154.  
  155. // Fix a security hole in PHP 4.3.9 and below...
  156. if (preg_match('~^a:[34]:\{i:0;(i:\d{1,6}|s:[1-8]:"\d{1,8}");i:1;s:(0|40):"([a-fA-F0-9]{40})?";i:2;[id]:\d{1,14};(i:3;i:\d;)?\}$~', $_COOKIE[$smf_settings['cookiename']]) == 1)
  157. {
  158. list ($ID_MEMBER, $password) = @unserialize($_COOKIE[$smf_settings['cookiename']]);
  159. $ID_MEMBER = !empty($ID_MEMBER) ? (int) $ID_MEMBER : 0;
  160. }
  161. else
  162. $ID_MEMBER = 0;
  163. }
  164. elseif (isset($_SESSION['login_' . $smf_settings['cookiename']]))
  165. {
  166. list ($ID_MEMBER, $password, $login_span) = @unserialize(stripslashes($_SESSION['login_' . $smf_settings['cookiename']]));
  167. $ID_MEMBER = !empty($ID_MEMBER) && $login_span > time() ? (int) $ID_MEMBER : 0;
  168. }
  169. else
  170. $ID_MEMBER = 0;
  171.  
  172. //var_dump($ID_MEMBER); var_dump($password); var_dump($login_span); die();
  173.  
  174. // Don't even bother if they have no authentication data.
  175. if (!empty($ID_MEMBER))
  176. {
  177. $request = smf_query("
  178. SELECT *
  179. FROM $smf_settings[db_prefix]members
  180. WHERE ID_MEMBER = $ID_MEMBER
  181. LIMIT 1", __FILE__, __LINE__);
  182. // Did we find 'im? If not, junk it.
  183. if (mysql_num_rows($request) != 0)
  184. {
  185. // The base settings array.
  186. $smf_user_info = mysql_fetch_assoc($request);
  187.  
  188. if (strlen($password) == 40)
  189. $check = sha1($smf_user_info['passwd'] . $smf_user_info['password_salt']) == $password;
  190. else
  191. $check = false;
  192.  
  193. // Wrong password or not activated - either way, you're going nowhere.
  194. $ID_MEMBER = $check && ($smf_user_info['is_activated'] == 1 || $smf_user_info['is_activated'] == 11) ? $smf_user_info['ID_MEMBER'] : 0;
  195. }
  196. else
  197. $ID_MEMBER = 0;
  198. mysql_free_result($request);
  199. }
  200.  
  201.  
  202. /* if (empty($ID_MEMBER))
  203. $smf_user_info = array('groups' => array(-1));
  204. else
  205. {
  206. if (empty($smf_user_info['additionalGroups']))
  207. $smf_user_info['groups'] = array($smf_user_info['ID_GROUP'], $smf_user_info['ID_POST_GROUP']);
  208. else
  209. $smf_user_info['groups'] = array_merge(
  210. array($smf_user_info['ID_GROUP'], $smf_user_info['ID_POST_GROUP']),
  211. explode(',', $smf_user_info['additionalGroups'])
  212. );
  213. }
  214.  
  215. // A few things to make life easier...
  216. $smf_user_info['id'] = &$smf_user_info['ID_MEMBER'];
  217. $smf_user_info['username'] = &$smf_user_info['memberName'];
  218. $smf_user_info['name'] = &$smf_user_info['realName'];
  219. $smf_user_info['email'] = &$smf_user_info['emailAddress'];
  220. $smf_user_info['messages'] = &$smf_user_info['instantMessages'];
  221. $smf_user_info['unread_messages'] = &$smf_user_info['unreadMessages'];
  222. $smf_user_info['language'] = empty($smf_user_info['lngfile']) || empty($smf_settings['userLanguage']) ? $smf_settings['language'] : $smf_user_info['lngfile'];
  223. $smf_user_info['is_guest'] = $ID_MEMBER == 0;
  224. $smf_user_info['is_admin'] = in_array(1, $smf_user_info['groups']);
  225.  
  226. // This might be set to "forum default"...
  227. if (empty($smf_user_info['timeFormat']))
  228. $smf_user_info['timeFormat'] = $smf_settings['time_format'];
  229.  
  230. return !$smf_user_info['is_guest'];*/
  231. return $check;
  232. }
  233.  
  234. function smf_registerMember($username, $email, $password, $extra_fields = array(), $theme_options = array())
  235. {
  236. global $smf_settings, $smf_connection;
  237.  
  238. // No connection means no registrations...
  239. if (!$smf_connection)
  240. return false;
  241.  
  242. // Can't use that username.
  243. if (preg_match('~[<>&"\'=\\\]~', $username) === 1 || $username === '_' || $username === '|' || strpos($username, '[code') !== false || strpos($username, '[/code') !== false || strlen($username) > 25)
  244. return false;
  245.  
  246. // Make sure the email is valid too.
  247. if (empty($email) || preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $email) === 0 || strlen($email) > 255)
  248. return false;
  249.  
  250. // !!! Validate username isn't already used? Validate reserved, etc.?
  251.  
  252. $register_vars = array(
  253. 'memberName' => "'$username'",
  254. 'realName' => "'$username'",
  255. 'emailAddress' => "'" . addslashes($email) . "'",
  256. 'passwd' => "'" . sha1(strtolower($username) . $password) . "'",
  257. 'password_salt' => "'" . substr(md5(mt_rand()), 0, 4) . "'",
  258. 'posts' => '0',
  259. 'dateRegistered' => (string) time(),
  260. 'is_activated' => '1',
  261. 'personalText' => "'" . addslashes($smf_settings['default_personalText']) . "'",
  262. 'pm_email_notify' => '1',
  263. 'ID_THEME' => '0',
  264. 'ID_POST_GROUP' => '4',
  265. 'lngfile' => "''",
  266. 'buddy_list' => "''",
  267. 'pm_ignore_list' => "''",
  268. 'messageLabels' => "''",
  269. 'websiteTitle' => "''",
  270. 'websiteUrl' => "''",
  271. 'location' => "''",
  272. 'ICQ' => "''",
  273. 'AIM' => "''",
  274. 'YIM' => "''",
  275. 'MSN' => "''",
  276. 'timeFormat' => "''",
  277. 'signature' => "''",
  278. 'avatar' => "''",
  279. 'usertitle' => "''",
  280. 'memberIP' => "''",
  281. 'memberIP2' => "''",
  282. 'secretQuestion' => "''",
  283. 'secretAnswer' => "''",
  284. 'validation_code' => "''",
  285. 'additionalGroups' => "''",
  286. 'smileySet' => "''",
  287. 'password_salt' => "''",
  288. );
  289.  
  290. $register_vars = $extra_fields + $register_vars;
  291.  
  292. smf_query("
  293. INSERT INTO $smf_settings[db_prefix]members
  294. (" . implode(', ', array_keys($register_vars)) . ")
  295. VALUES (" . implode(', ', $register_vars) . ')', __FILE__, __LINE__);
  296. $ID_MEMBER = smf_insert_id();
  297.  
  298. smf_query("
  299. UPDATE $smf_settings[db_prefix]settings
  300. SET value = value + 1
  301. WHERE variable = 'totalMembers'
  302. LIMIT 1", __FILE__, __LINE__);
  303. smf_query("
  304. REPLACE INTO $smf_settings[db_prefix]settings
  305. (variable, value)
  306. VALUES ('latestMember', $ID_MEMBER),
  307. ('latestRealName', '$username')", __FILE__, __LINE__);
  308. smf_query("
  309. UPDATE {$db_prefix}log_activity
  310. SET registers = registers + 1
  311. WHERE date = '" . strftime('%Y-%m-%d') . "'
  312. LIMIT 1", __FILE__, __LINE__);
  313. if (smf_affected_rows() == 0)
  314. smf_query("
  315. INSERT IGNORE INTO {$db_prefix}log_activity
  316. (date, registers)
  317. VALUES ('" . strftime('%Y-%m-%d') . "', 1)", __FILE__, __LINE__);
  318.  
  319. // Theme variables too?
  320. if (!empty($theme_options))
  321. {
  322. $setString = '';
  323. foreach ($theme_options as $var => $val)
  324. $setString .= "
  325. ($memberID, SUBSTRING('$var', 1, 255), SUBSTRING('$val', 1, 65534)),";
  326. smf_query("
  327. INSERT INTO $smf_settings[db_prefix]themes
  328. (ID_MEMBER, variable, value)
  329. VALUES " . substr($setString, 0, -1), __FILE__, __LINE__);
  330. }
  331.  
  332. return $ID_MEMBER;
  333. }
  334.  
  335. // Log the current user online.
  336. function smf_logOnline($action = null)
  337. {
  338. global $smf_settings, $smf_connection, $smf_user_info;
  339.  
  340. if (!$smf_connection)
  341. return false;
  342.  
  343. // Determine number of seconds required.
  344. $lastActive = $smf_settings['lastActive'] * 60;
  345.  
  346. // Don't mark them as online more than every so often.
  347. if (empty($_SESSION['log_time']) || $_SESSION['log_time'] < (time() - 8))
  348. $_SESSION['log_time'] = time();
  349. else
  350. return;
  351.  
  352. $serialized = $_GET;
  353. $serialized['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];
  354. unset($serialized['sesc']);
  355. if ($action !== null)
  356. $serialized['action'] = $action;
  357.  
  358. $serialized = addslashes(serialize($serialized));
  359.  
  360. // Guests use 0, members use ID_MEMBER.
  361. if ($smf_user_info['is_guest'])
  362. {
  363. smf_query("
  364. DELETE FROM $smf_settings[db_prefix]log_online
  365. WHERE logTime < NOW() - INTERVAL $lastActive SECOND OR session = 'ip$_SERVER[REMOTE_ADDR]'", __FILE__, __LINE__);
  366. smf_query("
  367. INSERT IGNORE INTO $smf_settings[db_prefix]log_online
  368. (session, ID_MEMBER, ip, url)
  369. VALUES ('ip$_SERVER[REMOTE_ADDR]', 0, IFNULL(INET_ATON('$_SERVER[REMOTE_ADDR]'), 0), '$serialized')", __FILE__, __LINE__);
  370. }
  371. else
  372. {
  373. smf_query("
  374. DELETE FROM $smf_settings[db_prefix]log_online
  375. WHERE logTime < NOW() - INTERVAL $lastActive SECOND OR ID_MEMBER = $smf_user_info[id] OR session = '" . @session_id() . "'", __FILE__, __LINE__);
  376. smf_query("
  377. INSERT IGNORE INTO $smf_settings[db_prefix]log_online
  378. (session, ID_MEMBER, ip, url)
  379. VALUES ('" . @session_id() . "', $smf_user_info[id], IFNULL(INET_ATON('$_SERVER[REMOTE_ADDR]'), 0), '$serialized')", __FILE__, __LINE__);
  380. }
  381. }
  382.  
  383. function smf_isOnline($user)
  384. {
  385. global $smf_settings, $smf_connection;
  386.  
  387. if (!$smf_connection)
  388. return false;
  389.  
  390. $result = smf_query("
  391. SELECT lo.ID_MEMBER
  392. FROM $smf_settings[db_prefix]log_online AS lo" . (!is_integer($user) ? "
  393. LEFT JOIN $smf_settings[db_prefix]members AS mem ON (mem.ID_MEMBER = lo.ID_MEMBER)" : '') . "
  394. WHERE lo.ID_MEMBER = " . (int) $user . (!is_integer($user) ? " OR mem.memberName = '$user'" : '') . "
  395. LIMIT 1", __FILE__, __LINE__);
  396. $return = mysql_num_rows($result) != 0;
  397. mysql_free_result($result);
  398.  
  399. return $return;
  400. }
  401.  
  402. // Log an error, if the option is on.
  403. function smf_logError($error_message, $file = null, $line = null)
  404. {
  405. global $smf_settings, $smf_connection;
  406.  
  407. // Check if error logging is actually on and we're connected...
  408. if (empty($smf_settings['enableErrorLogging']) || !$smf_connection)
  409. return $error_message;
  410.  
  411. // Basically, htmlspecialchars it minus &. (for entities!)
  412. $error_message = strtr($error_message, array('<' => '<', '>' => '>', '"' => '"'));
  413. $error_message = strtr($error_message, array('<br />' => '<br />', '<b>' => '<b>', '</b>' => '</b>', "\n" => '<br />'));
  414.  
  415. // Add a file and line to the error message?
  416. if ($file != null)
  417. $error_message .= '<br />' . $file;
  418. if ($line != null)
  419. $error_message .= '<br />' . $line;
  420.  
  421. // Just in case there's no ID_MEMBER or IP set yet.
  422. if (empty($smf_user_info['id']))
  423. $smf_user_info['id'] = 0;
  424.  
  425. // Insert the error into the database.
  426. smf_query("
  427. INSERT INTO $smf_settings[db_prefix]log_errors
  428. (ID_MEMBER, logTime, ip, url, message, session)
  429. VALUES ($smf_user_info[id], " . time() . ", SUBSTRING('$_SERVER[REMOTE_ADDR]', 1, 16), SUBSTRING('" . (empty($_SERVER['QUERY_STRING']) ? '' : addslashes(htmlspecialchars('?' . $_SERVER['QUERY_STRING']))) . "', 1, 65534), SUBSTRING('" . addslashes($error_message) . "', 1, 65534), SUBSTRING('" . @session_id() . "', 1, 32))", __FILE__, __LINE__);
  430.  
  431. // Return the message to make things simpler.
  432. return $error_message;
  433. }
  434.  
  435. // Format a time to make it look purdy.
  436. function smf_formatTime($logTime)
  437. {
  438. global $smf_user_info, $smf_settings;
  439.  
  440. // Offset the time - but we can't have a negative date!
  441. $time = max($logTime + (@$smf_user_info['timeOffset'] + $smf_settings['time_offset']) * 3600, 0);
  442.  
  443. // Format some in caps, and then any other characters..
  444. return strftime(strtr(!empty($smf_user_info['timeFormat']) ? $smf_user_info['timeFormat'] : $smf_settings['time_format'], array('%a' => ucwords(strftime('%a', $time)), '%A' => ucwords(strftime('%A', $time)), '%b' => ucwords(strftime('%b', $time)), '%B' => ucwords(strftime('%B', $time)))), $time);
  445. }
  446.  
  447. // Do a query, and if it fails log an error in the SMF error log.
  448. function smf_query($string, $file, $line)
  449. {
  450. global $smf_settings, $smf_connection;
  451.  
  452. if (!$smf_connection)
  453. return false;
  454.  
  455. $smf_settings['db_count'] = @$smf_settings['db_count'] + 1;
  456.  
  457. $ret = mysql_query($string, $smf_connection);
  458.  
  459. if ($ret === false)
  460. smf_logError(mysql_error($smf_connection), $file, $line);
  461.  
  462. return $ret;
  463. }
  464.  
  465. function smf_affected_rows()
  466. {
  467. global $smf_connection;
  468.  
  469. return mysql_affected_rows($smf_connection);
  470. }
  471.  
  472. function smf_insert_id()
  473. {
  474. global $smf_connection;
  475.  
  476. return mysql_insert_id($smf_connection);
  477. }
  478.  
  479. // Mother, may I?
  480. function smf_allowedTo($permission)
  481. {
  482. global $smf_settings, $smf_user_info, $smf_connection;
  483.  
  484. if (!$smf_connection)
  485. return null;
  486.  
  487. // Administrators can do all, and everyone can do nothing.
  488. if ($smf_user_info['is_admin'] || empty($permission))
  489. return true;
  490.  
  491. if (!isset($smf_user_info['permissions']))
  492. {
  493. $result = smf_query("
  494. SELECT permission, addDeny
  495. FROM $smf_settings[db_prefix]permissions
  496. WHERE ID_GROUP IN (" . implode(', ', $smf_user_info['groups']) . ")", __FILE__, __LINE__);
  497. $removals = array();
  498. $smf_user_info['permissions'] = array();
  499. while ($row = mysql_fetch_assoc($result))
  500. {
  501. if (empty($row['addDeny']))
  502. $removals[] = $row['permission'];
  503. else
  504. $smf_user_info['permissions'][] = $row['permission'];
  505. }
  506. mysql_free_result($result);
  507.  
  508. // And now we get rid of the removals ;).
  509. if (!empty($smf_settings['permission_enable_deny']))
  510. $smf_user_info['permissions'] = array_diff($smf_user_info['permissions'], $removals);
  511. }
  512.  
  513. // So.... can you?
  514. if (!is_array($permission) && in_array($permission, $smf_user_info['permissions']))
  515. return true;
  516. elseif (is_array($permission) && count(array_intersect($permission, $smf_user_info['permissions'])) != 0)
  517. return true;
  518. else
  519. return false;
  520. }
  521.  
  522. function smf_loadThemeData($ID_THEME = 0)
  523. {
  524. global $smf_settings, $smf_user_info, $smf_connection;
  525.  
  526. if (!$smf_connection)
  527. return null;
  528.  
  529. // The theme was specified by parameter.
  530. if (!empty($ID_THEME))
  531. $theme = (int) $ID_THEME;
  532. // The theme was specified by REQUEST.
  533. elseif (!empty($_REQUEST['theme']))
  534. {
  535. $theme = (int) $_REQUEST['theme'];
  536. $_SESSION['ID_THEME'] = $theme;
  537. }
  538. // The theme was specified by REQUEST... previously.
  539. elseif (!empty($_SESSION['ID_THEME']))
  540. $theme = (int) $_SESSION['ID_THEME'];
  541. // The theme is just the user's choice. (might use ?board=1;theme=0 to force board theme.)
  542. elseif (!empty($smf_user_info['theme']) && !isset($_REQUEST['theme']))
  543. $theme = $smf_user_info['theme'];
  544. // The theme is the forum's default.
  545. else
  546. $theme = $smf_settings['theme_guests'];
  547.  
  548. // Verify the ID_THEME... no foul play.
  549. if (empty($smf_settings['theme_default']) && $theme == 1 && $ID_THEME != 1)
  550. $theme = $smf_settings['theme_guests'];
  551. elseif (!empty($smf_settings['knownThemes']) && !empty($smf_settings['theme_allow']))
  552. {
  553. $themes = explode(',', $smf_settings['knownThemes']);
  554. if (!in_array($theme, $themes))
  555. $theme = $smf_settings['theme_guests'];
  556. else
  557. $theme = (int) $theme;
  558. }
  559. else
  560. $theme = (int) $theme;
  561.  
  562. $member = empty($smf_user_info['id']) ? -1 : $smf_user_info['id'];
  563.  
  564. // Load variables from the current or default theme, global or this user's.
  565. $result = smf_query("
  566. SELECT variable, value, ID_MEMBER, ID_THEME
  567. FROM $smf_settings[db_prefix]themes
  568. WHERE ID_MEMBER IN (-1, 0, $member)
  569. AND ID_THEME" . ($theme == 1 ? ' = 1' : " IN ($theme, 1)"), __FILE__, __LINE__);
  570. // Pick between $smf_settings['theme'] and $smf_user_info['theme'] depending on whose data it is.
  571. $themeData = array(0 => array(), $member => array());
  572. while ($row = mysql_fetch_assoc($result))
  573. {
  574. // If this is the themedir of the default theme, store it.
  575. if (in_array($row['variable'], array('theme_dir', 'theme_url', 'images_url')) && $row['ID_THEME'] == '1' && empty($row['ID_MEMBER']))
  576. $themeData[0]['default_' . $row['variable']] = $row['value'];
  577.  
  578. // If this isn't set yet, is a theme option, or is not the default theme..
  579. if (!isset($themeData[$row['ID_MEMBER']][$row['variable']]) || $row['ID_THEME'] != '1')
  580. $themeData[$row['ID_MEMBER']][$row['variable']] = substr($row['variable'], 0, 5) == 'show_' ? $row['value'] == '1' : $row['value'];
  581. }
  582. mysql_free_result($result);
  583.  
  584. $smf_settings['theme'] = $themeData[0];
  585. $smf_user_info['theme'] = $themeData[$member];
  586.  
  587. if (!empty($themeData[-1]))
  588. foreach ($themeData[-1] as $k => $v)
  589. {
  590. if (!isset($smf_user_info['theme'][$k]))
  591. $smf_user_info['theme'][$k] = $v;
  592. }
  593.  
  594. $smf_settings['theme']['theme_id'] = $theme;
  595.  
  596. $smf_settings['theme']['actual_theme_url'] = $smf_settings['theme']['theme_url'];
  597. $smf_settings['theme']['actual_images_url'] = $smf_settings['theme']['images_url'];
  598. $smf_settings['theme']['actual_theme_dir'] = $smf_settings['theme']['theme_dir'];
  599. }
  600.  
  601. // Attempt to start the session, unless it already has been.
  602. function smf_loadSession()
  603. {
  604. global $HTTP_SESSION_VARS, $smf_connection, $smf_settings, $smf_user_info;
  605.  
  606. // Attempt to change a few PHP settings.
  607. @ini_set('session.use_cookies', true);
  608. @ini_set('session.use_only_cookies', false);
  609. @ini_set('arg_separator.output', '&');
  610.  
  611. // If it's already been started... probably best to skip this.
  612. if ((@ini_get('session.auto_start') == 1 && !empty($smf_settings['databaseSession_enable'])) || session_id() == '')
  613. {
  614. // Attempt to end the already-started session.
  615. if (@ini_get('session.auto_start') == 1)
  616. @session_write_close();
  617.  
  618. // This is here to stop people from using bad junky PHPSESSIDs.
  619. if (isset($_REQUEST[session_name()]) && preg_match('~^[A-Za-z0-9]{32}$~', $_REQUEST[session_name()]) == 0 && !isset($_COOKIE[session_name()]))
  620. $_COOKIE[session_name()] = md5(md5('smf_sess_' . time()) . mt_rand());
  621.  
  622. // Use database sessions?
  623. if (!empty($smf_settings['databaseSession_enable']) && $smf_connection)
  624. session_set_save_handler('smf_sessionOpen', 'smf_sessionClose', 'smf_sessionRead', 'smf_sessionWrite', 'smf_sessionDestroy', 'smf_sessionGC');
  625. elseif (@ini_get('session.gc_maxlifetime') <= 1440 && !empty($smf_settings['databaseSession_lifetime']))
  626. @ini_set('session.gc_maxlifetime', max($smf_settings['databaseSession_lifetime'], 60));
  627.  
  628. session_start();
  629. }
  630.  
  631. // While PHP 4.1.x should use $_SESSION, it seems to need this to do it right.
  632. if (@version_compare(PHP_VERSION, '4.2.0') == -1)
  633. $HTTP_SESSION_VARS['smf_php_412_bugfix'] = true;
  634.  
  635. // Set the randomly generated code.
  636. if (!isset($_SESSION['rand_code']))
  637. $_SESSION['rand_code'] = md5(session_id() . mt_rand());
  638. $smf_user_info['session_id'] = &$_SESSION['rand_code'];
  639.  
  640. if (!isset($_SESSION['USER_AGENT']))
  641. $_SESSION['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];
  642. }
  643.  
  644. function smf_sessionOpen($save_path, $session_name)
  645. {
  646. return true;
  647. }
  648.  
  649. function smf_sessionClose()
  650. {
  651. return true;
  652. }
  653.  
  654. function smf_sessionRead($session_id)
  655. {
  656. global $smf_settings;
  657.  
  658. if (preg_match('~^[A-Za-z0-9]{16,32}$~', $session_id) == 0)
  659. return false;
  660.  
  661. // Look for it in the database.
  662. $result = smf_query("
  663. SELECT data
  664. FROM $smf_settings[db_prefix]sessions
  665. WHERE session_id = '" . addslashes($session_id) . "'
  666. LIMIT 1", __FILE__, __LINE__);
  667. list ($sess_data) = mysql_fetch_row($result);
  668. mysql_free_result($result);
  669.  
  670. return $sess_data;
  671. }
  672.  
  673. function smf_sessionWrite($session_id, $data)
  674. {
  675. global $smf_settings, $smf_connection;
  676.  
  677. if (preg_match('~^[A-Za-z0-9]{16,32}$~', $session_id) == 0)
  678. return false;
  679.  
  680. // First try to update an existing row...
  681. $result = smf_query("
  682. UPDATE $smf_settings[db_prefix]sessions
  683. SET data = '" . addslashes($data) . "', last_update = " . time() . "
  684. WHERE session_id = '" . addslashes($session_id) . "'
  685. LIMIT 1", __FILE__, __LINE__);
  686.  
  687. // If that didn't work, try inserting a new one.
  688. if (mysql_affected_rows($smf_connection) == 0)
  689. $result = smf_query("
  690. INSERT IGNORE INTO $smf_settings[db_prefix]sessions
  691. (session_id, data, last_update)
  692. VALUES ('" . addslashes($session_id) . "', '" . addslashes($data) . "', " . time() . ")", __FILE__, __LINE__);
  693.  
  694. return $result;
  695. }
  696.  
  697. function smf_sessionDestroy($session_id)
  698. {
  699. global $smf_settings;
  700.  
  701. if (preg_match('~^[A-Za-z0-9]{16,32}$~', $session_id) == 0)
  702. return false;
  703.  
  704. // Just delete the row...
  705. return smf_query("
  706. DELETE FROM $smf_settings[db_prefix]sessions
  707. WHERE session_id = '" . addslashes($session_id) . "'
  708. LIMIT 1", __FILE__, __LINE__);
  709. }
  710.  
  711. function smf_sessionGC($max_lifetime)
  712. {
  713. global $smf_settings;
  714.  
  715. // Just set to the default or lower? Ignore it for a higher value. (hopefully)
  716. if ($max_lifetime <= 1440 && !empty($smf_settings['databaseSession_lifetime']))
  717. $max_lifetime = max($smf_settings['databaseSession_lifetime'], 60);
  718.  
  719. // Clean up ;).
  720. return smf_query("
  721. DELETE FROM $smf_settings[db_prefix]sessions
  722. WHERE last_update < " . (time() - $max_lifetime), __FILE__, __LINE__);
  723. }
  724.  
  725. // Define the sha1 function, if it doesn't exist (but the built in one would be faster.)
  726. if (!function_exists('sha1'))
  727. {
  728. function sha1($str)
  729. {
  730. // If we have mhash loaded in, use it instead!
  731. if (function_exists('mhash') && defined('MHASH_SHA1'))
  732. return bin2hex(mhash(MHASH_SHA1, $str));
  733.  
  734. $nblk = (strlen($str) + 8 >> 6) + 1;
  735. $blks = array_pad(array(), $nblk * 16, 0);
  736.  
  737. for ($i = 0; $i < strlen($str); $i++)
  738. $blks[$i >> 2] |= ord($str{$i}) << (24 - ($i % 4) * 8);
  739.  
  740. $blks[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8);
  741.  
  742. return sha1_core($blks, strlen($str) * 8);
  743. }
  744.  
  745. // This is the core SHA-1 calculation routine, used by sha1().
  746. function sha1_core($x, $len)
  747. {
  748. @$x[$len >> 5] |= 0x80 << (24 - $len % 32);
  749. $x[(($len + 64 >> 9) << 4) + 15] = $len;
  750.  
  751. $w = array();
  752. $a = 1732584193;
  753. $b = -271733879;
  754. $c = -1732584194;
  755. $d = 271733878;
  756. $e = -1009589776;
  757.  
  758. for ($i = 0, $n = count($x); $i < $n; $i += 16)
  759. {
  760. $olda = $a;
  761. $oldb = $b;
  762. $oldc = $c;
  763. $oldd = $d;
  764. $olde = $e;
  765.  
  766. for ($j = 0; $j < 80; $j++)
  767. {
  768. if ($j < 16)
  769. $w[$j] = @$x[$i + $j];
  770. else
  771. $w[$j] = sha1_rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1);
  772.  
  773. $t = sha1_rol($a, 5) + sha1_ft($j, $b, $c, $d) + $e + $w[$j] + sha1_kt($j);
  774. $e = $d;
  775. $d = $c;
  776. $c = sha1_rol($b, 30);
  777. $b = $a;
  778. $a = $t;
  779. }
  780.  
  781. $a += $olda;
  782. $b += $oldb;
  783. $c += $oldc;
  784. $d += $oldd;
  785. $e += $olde;
  786. }
  787.  
  788. return dechex($a) . dechex($b) . dechex($c) . dechex($d) . dechex($e);
  789. }
  790.  
  791. function sha1_ft($t, $b, $c, $d)
  792. {
  793. if ($t < 20)
  794. return ($b & $c) | ((~$b) & $d);
  795. if ($t < 40)
  796. return $b ^ $c ^ $d;
  797. if ($t < 60)
  798. return ($b & $c) | ($b & $d) | ($c & $d);
  799.  
  800. return $b ^ $c ^ $d;
  801. }
  802.  
  803. function sha1_kt($t)
  804. {
  805. return $t < 20 ? 1518500249 : ($t < 40 ? 1859775393 : ($t < 60 ? -1894007588 : -899497514));
  806. }
  807.  
  808. function sha1_rol($num, $cnt)
  809. {
  810. $z = 0x80000000;
  811. if ($z & $num)
  812. $a = ($num >> 1 & (~$z | 0x40000000)) >> (31 - $cnt);
  813. else
  814. $a = $num >> (32 - $cnt);
  815.  
  816. return ($num << $cnt) | $a;
  817. }
  818. }
  819.  
  820. // Log in user by user name - Added by Jwall
  821. function smf_LoginById($username, $cookieLength = 3600){
  822.  
  823. global $smf_connection, $smf_settings;
  824.  
  825. // enable binary look up for MODx workaround - Raymond
  826. $binaryLookup = '';
  827.  
  828. $sql = "SELECT *
  829. FROM $smf_settings[db_prefix]members
  830. WHERE $binaryLookup member_name = '".mysql_escape_string($username)."'
  831. LIMIT 1";
  832. $request = smf_query($sql, __FILE__, __LINE__);
  833. $smf_user = mysql_fetch_assoc($request);
  834.  
  835. //Now login
  836. smf_setLoginCookie($cookieLength, $smf_user['id_member'], sha1($smf_user['passwd'] . $smf_user['password_salt']));
  837. return smf_authenticateUser();
  838. }
  839.  
  840. // Log out user
  841. function smf_LogoutByIdMember($id_member){
  842.  
  843. global $smf_connection, $smf_settings;
  844.  
  845. function smf_cookie_url($local, $global)
  846. {
  847. global $smf_settings;
  848. // Use PHP to parse the URL, hopefully it does its job.
  849. $parsed_url = parse_url($smf_settings['forum_url']);
  850.  
  851. // Set the cookie to the forum's path only?
  852. if (empty($parsed_url['path']) || !$local)
  853. $parsed_url['path'] = '';
  854.  
  855. // This is probably very likely for apis and such, no?
  856. if ($global)
  857. {
  858. // Try to figure out where to set the cookie; this can be confused, though.
  859. if (preg_match('~(?:[^\.]+\.)?(.+)\z~i', $parsed_url['host'], $parts) == 1)
  860. $parsed_url['host'] = '.' . $parts[1];
  861. }
  862. // If both options are off, just use no host and /.
  863. elseif (!$local)
  864. $parsed_url['host'] = '';
  865.  
  866. return $parsed_url;
  867. }
  868.  
  869. // shouldn't have to do this but it works like charm !!
  870. $sql = "DELETE FROM $smf_settings[db_prefix]log_online WHERE ID_MEMBER = '$id_member' LIMIT 1";
  871. $request = smf_query($sql, __FILE__, __LINE__);
  872.  
  873. unset($_SESSION['login_' . $smf_settings['cookiename']]);
  874.  
  875. $PHPSESSID = $HTTP_COOKIE_VARS["PHPSESSID"];
  876.  
  877. $parsed_url = smf_cookie_url(!empty($smf_settings['localCookies']), !empty($smf_settings['globalCookies']));
  878.  
  879. setcookie("PHPSESSID", $PHPSESSID, time() - 3600, $parsed_url['path'] . '/', $parsed_url['host'], 0);
  880. setcookie($smf_settings['cookiename'], "", time() - 3600, $parsed_url['path'] . '/', $parsed_url['host'], 0);
  881.  
  882. }
  883.  
  884. function smf_authenticate_password($username,$password){
  885. global $smf_connection, $smf_settings;
  886.  
  887. // enable binary look up for MODx workaround - Raymond
  888. $binaryLookup = '';
  889.  
  890. $sql = "SELECT *
  891. FROM $smf_settings[db_prefix]members
  892. WHERE $binaryLookup member_name = '".mysql_escape_string($username)."'";
  893. $request = smf_query($sql, __FILE__, __LINE__);
  894. if($request) {
  895. $smf_user = mysql_fetch_assoc($request);
  896.  
  897. if (sha1(strtolower($username) . $password) == $smf_user['passwd']) return true;
  898.  
  899. //For joomla
  900. if (md5($password) == $smf_user['passwd']) return true;
  901.  
  902. list($ahash, $asalt) = explode(':', $smf_user['passwd']);
  903. if (strpos($smf_user['passwd'], ':') !== false)
  904. if ($ahash == md5($password.$asalt)) return true;
  905.  
  906. }
  907. return false;
  908. }
  909. ?>
Add Comment
Please, Sign In to add comment