2 /************************************************************************
3 * MXChange v0.2.1 Start: 07/16/2004 *
4 * =============== Last change: 10/27/2004 *
6 * -------------------------------------------------------------------- *
7 * File : user_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Special functions for user extension *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Spezielle Funktionen fuer die user-Erweiterung *
12 * -------------------------------------------------------------------- *
14 * $Date:: 2009-03-06 20:24:32 +0100 (Fr, 06. Mär 2009) $ *
15 * $Tag:: 0.2.1-FINAL $ *
16 * $Author:: stelzi $ *
17 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2008 by Roland Haeder *
21 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
45 // Add links for selecting some users
46 function alpha ($sortby, $colspan, $return=false) {
47 if (!REQUEST_ISSET_GET(('offset'))) REQUEST_SET_GET('offset', 0);
48 $ADD = "&page=".REQUEST_GET(('page'))."&offset=".REQUEST_GET(('offset'));
49 if (REQUEST_ISSET_GET(('mode'))) $ADD .= "&mode=".REQUEST_GET(('mode'));
51 /* Creates the list of letters and makes them a link. */
52 $alphabet = array(getMessage('_ALL2'),"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",getMessage('_OTHERS'));
53 $num = count($alphabet) - 1;
55 while (list($counter, $ltr) = each($alphabet)) {
56 if (REQUEST_GET('letter') == $ltr) {
57 // Current letter is letter from URL
58 $OUT .= "<strong>".$ltr."</strong>";
60 // Output link to letter
61 $OUT .= "<a href=\"{!URL!}/modules.php?module=admin&what=".$GLOBALS['what'];
62 if (REQUEST_ISSET_GET(('mode'))) $OUT .= "&mode=".REQUEST_GET(('mode'));
63 $OUT .= "&letter=".$ltr."&sortby=".$sortby.$ADD."\">".$ltr."</a>";
66 if ((($counter / getConfig('user_alpha')) == round($counter / getConfig('user_alpha'))) && ($counter > 0)) {
67 $OUT .= " ]<br />[ ";
68 } elseif ( $counter != $num ) {
69 $OUT .= " | ";
73 define('__ALPHA_LIST', $OUT);
76 $OUT = LOAD_TEMPLATE("admin_list_user_alpha", true);
77 if ($return === true) {
78 // Return generated code
81 // Output generated code
86 // Add links for sorting
87 function SortLinks ($letter, $sortby, $colspan, $return=false) {
89 if (!REQUEST_ISSET_GET(('offset'))) REQUEST_SET_GET('offset', 0);
90 if (!REQUEST_ISSET_GET(('page'))) REQUEST_SET_GET('page' , 0);
92 // Add page and offset
93 $ADD = "&page=".REQUEST_GET(('page'))."&offset=".REQUEST_GET(('offset'));
96 if (REQUEST_ISSET_GET(('status'))) $ADD .= "&mode=".REQUEST_GET(('status'));
97 elseif (REQUEST_ISSET_GET(('mode'))) $ADD .= "&mode=".REQUEST_GET(('mode'));
99 // Makes order by links..
100 if ($letter == "front") $letter = getMessage('_ALL2');
102 // Prepare array with all possible sorters
104 'userid' => getMessage('_UID'),
105 'family' => getMessage('FAMILY_NAME'),
106 'email' => getMessage('ADDY'),
107 'REMOTE_ADDR' => getMessage('REMOTE_IP')
110 // Add nickname if extension is installed
111 if (EXT_IS_ACTIVE("nickname")) {
112 $list['nickname'] = getMessage('NICKNAME');
115 foreach ($list as $sort => $title) {
116 if ($sortby == $sort) {
117 $OUT .= "<strong>".$title."</strong> | ";
119 $OUT .= "<a href=\"{!URL!}/modules.php?module=admin&what=list_user&letter=".$letter."&sortby=".$sort.$ADD."\">".$title."</a> | ";
123 define('__SORT_LIST', substr($OUT, 0, -13));
126 $OUT = LOAD_TEMPLATE("admin_list_user_sort", true);
127 if ($return === true) {
136 // Add page navigation
137 function ADD_PAGENAV ($PAGES, $offset, $show_form, $colspan,$return=false) {
140 define('__FORM_HEADER', "<tr><td colspan=\"".$colspan."\" class=\"seperator\"> </td></tr>");
142 // Load form for changing number of lines
143 define('__FORM_HEADER', LOAD_TEMPLATE("admin_list_user_sort_form", true));
147 // Add line with bottom border
148 define('__FORM_FOOTER', "<tr><td colspan=\"{!__COLSPAN2!}\" class=\"seperator bottom2\"> </td></tr>");
150 // Add line without bottom border
151 define('__FORM_FOOTER', "<tr><td colspan=\"{!__COLSPAN2!}\" class=\"seperator bottom2\"> </td></tr>");
155 for ($page = 1; $page <= $PAGES; $page++) {
156 if (($page == REQUEST_GET('page')) || ((!REQUEST_ISSET_GET(('page'))) && ($page == "1"))) {
159 if (!REQUEST_ISSET_GET(('letter'))) REQUEST_SET_GET('letter', getMessage('_ALL2'));
160 if (!REQUEST_ISSET_GET(('sortby'))) REQUEST_SET_GET('sortby', "userid");
163 $OUT .= "<a href=\"{!URL!}/modules.php?module=admin&what=".$GLOBALS['what'];
165 // Add status or mode
166 if (REQUEST_ISSET_GET(('status'))) $OUT .= "&mode=".REQUEST_GET(('status'));
167 elseif (REQUEST_ISSET_GET(('mode'))) $OUT .= "&mode=".REQUEST_GET(('mode'));
170 $OUT .= "&letter=".REQUEST_GET(('letter'))."&sortby=".REQUEST_GET(('sortby'))."&page=".$page."&offset=".$offset."\">";
175 if (($page == REQUEST_GET('page')) || ((!REQUEST_ISSET_GET(('page'))) && ($page == "1"))) {
176 $OUT .= "-</strong>";
181 if ($page < $PAGES) $OUT .= " | ";
184 define('__PAGENAV_LIST', $OUT);
187 $OUT = LOAD_TEMPLATE("admin_list_user_pagenav", true);
188 if ($return === true) {
197 // Create email link to user's account
198 function USER_CREATE_EMAIL_LINK($email, $mod="admin") {
199 // Show contact link only if user is confirmed by default
200 $locked = " AND `status`='CONFIRMED'";
202 // But admins shall always see it
203 if (IS_ADMIN()) $locked = "";
205 $result = SQL_QUERY_ESC("SELECT userid
206 FROM `{!_MYSQL_PREFIX!}_user_data`
207 WHERE email='%s'".$locked." LIMIT 1",
208 array($email), __FUNCTION__, __LINE__);
209 if (SQL_NUMROWS($result) == 1) {
211 list($uid) = SQL_FETCHROW($result);
213 // Rewrite email address to contact link
214 $email = "{!URL!}/modules.php?module=".$mod."&what=user_contct&uid=".bigintval($uid);
218 SQL_FREERESULT($result);
220 // Return rewritten (?) email address
224 // Selects a random user id as the new referal id if they have at least X confirmed mails in this run
225 function SELECT_RANDOM_REFID () {
226 // Default is zero refid
229 // Is the extension version fine?
230 if (GET_EXT_VERSION("user") >= "0.3.4") {
232 $totalUsers = GET_TOTAL_DATA("CONFIRMED", "user_data", "userid", "status", true, " AND `rand_confirmed` >= ".getConfig('user_min_confirmed')."");
234 // Do we have at least one?
235 if ($totalUsers > 0) {
236 // Then choose random number
237 $randNum = mt_rand(0, ($totalUsers - 1));
239 // Look for random user
240 $result = SQL_QUERY_ESC("SELECT `userid` FROM `{!_MYSQL_PREFIX!}_user_data` WHERE `status`='CONFIRMED' AND `rand_confirmed` >= %s ORDER BY `rand_confirmed` DESC LIMIT %s, 1",
241 array(getConfig('user_min_confirmed'), $randNum), __FUNCTION__, __LINE__);
243 // Do we have one entry there?
244 if (SQL_NUMROWS($result) == 1) {
245 // Use that userid as new referal id
246 list($refid) = SQL_FETCHROW($result);
248 // Reset this user's counter
249 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET `rand_confirmed`=0 WHERE userid=%s LIMIT 1",
250 array($refid), __FUNCTION__, __LINE__);
254 SQL_FREERESULT($result);
263 function USER_DO_LOGIN ($uid, $passwd) {
264 // Add last_login if available
266 if (GET_EXT_VERSION("sql_patches") >= "0.2.8") {
267 $lastOnline = ", last_login";
271 $password = ""; $uid2 = ""; $dmy = ""; $online = 0; $login = 0;
272 if ((EXT_IS_ACTIVE("nickname")) && (NICKNAME_PROBE_ON_USERID($uid))) {
274 $result = SQL_QUERY_ESC("SELECT userid, password, last_online".$lastOnline." FROM `{!_MYSQL_PREFIX!}_user_data` WHERE nickname='%s' AND `status`='CONFIRMED' LIMIT 1",
275 array($uid), __FUNCTION__, __LINE__);
276 list($uid2, $password, $online, $login) = SQL_FETCHROW($result);
277 if (!empty($uid2)) $uid = bigintval($uid2);
279 // Direct userid entered
280 $result = SQL_QUERY_ESC("SELECT userid, password, last_online".$lastOnline." FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s AND `status`='CONFIRMED' LIMIT 1",
281 array($uid, $hash), __FUNCTION__, __LINE__);
282 list($uid2, $password, $online, $login) = SQL_FETCHROW($result);
285 // Is there an entry?
286 if ((SQL_NUMROWS($result) == 1) && ((($probe_nickname) && (!empty($uid2))) || ($uid2 == $uid))) {
288 SQL_FREERESULT($result);
290 // By default the hash is empty
293 // Check for old MD5 passwords
294 if ((strlen($password) == 32) && (md5($passwd) == $password)) {
295 // Just set the hash to the password from DB... :)
298 // Hash password with improved way for comparsion
299 $hash = generateHash($passwd, substr($password, 0, -40));
302 if ($hash == $password) {
303 // New hashed password found so let's generate a new one
304 $hash = generateHash($passwd);
306 // ... and update database
307 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET password='%s' WHERE userid=%s AND `status`='CONFIRMED' LIMIT 1",
308 array($hash, $uid), __FUNCTION__, __LINE__);
310 // No login bonus by default
311 // @TODO Make this filter working: $ADDON = runFilterChain('post_login_update', array('login' => $login, 'online' => $online));
314 // Probe for last online timemark
315 $probe = time() - $online;
316 if (!empty($login)) $probe = time() - $login;
317 if ((GET_EXT_VERSION("bonus") >= "0.2.2") && ($probe >= getConfig('login_timeout'))) {
318 // Add login bonus to user's account
319 $ADD = sprintf(", login_bonus=login_bonus+%s",
320 (float)getConfig('login_bonus')
324 // Subtract login bonus from userid's account or jackpot
325 if ((GET_EXT_VERSION("bonus") >= "0.3.5") && (getConfig('bonus_mode') != "ADD")) BONUS_POINTS_HANDLER('login_bonus');
331 // Calculate new hash with the secret key and master salt together
332 $hash = generatePassString($hash);
334 // Update global array
335 // @TODO Make this filter working: $URL = runFilterChain('do_login', array('uid' => $uid, 'hash' => $hash, 'addon' => $ADDON));
336 $GLOBALS['userid'] = $uid;
338 // Try to set session data (which shall normally always work!)
339 if ((set_session('userid', $uid )) && (set_session('u_hash', $hash))) {
340 // Update database records
341 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET total_logins=total_logins+1".$ADD." WHERE userid=%s LIMIT 1",
342 array($uid), __FUNCTION__, __LINE__);
343 if (SQL_AFFECTEDROWS() == 1) {
344 // Procedure to checking for login data
345 if (($BONUS) && (EXT_IS_ACTIVE("bonus"))) {
346 // Bonus added (just displaying!)
347 $URL = "modules.php?module=chk_login&mode=bonus";
350 $URL = "modules.php?module=chk_login&mode=login";
353 // Cannot update counter!
354 $URL = "modules.php?module=index&what=login&login=".getCode('CNTR_FAILED');
357 // Cookies not setable!
358 $URL = "modules.php?module=index&what=login&login=".getCode('NO_COOKIES');
360 } elseif (GET_EXT_VERSION("sql_patches") >= "0.6.1") {
361 // Update failture counter
362 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET login_failures=login_failures+1,last_failure=NOW() WHERE userid=%s LIMIT 1",
363 array($uid), __FUNCTION__, __LINE__);
366 $URL = "modules.php?module=index&what=login&login=".getCode('WRONG_PASS');
368 } elseif ((($probe_nickname) && (!empty($uid2))) || ($uid2 == $uid)) {
369 // Other account status?
370 // @TODO Can this query be merged with above query?
371 $result = SQL_QUERY_ESC("SELECT status FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
372 array($uid), __FUNCTION__, __LINE__);
375 if (SQL_NUMROWS($result) == 1) {
377 list($status) = SQL_FETCHROW($result);
379 // Create an error code from given status
380 $ERROR = GEN_ERROR_CODE_FROM_ACCOUNT_STATUS($status);
383 $ERROR = getCode('WRONG_ID');
387 $URL = "modules.php?module=index&what=login&login=".$ERROR;
390 $URL = "modules.php?module=index&what=login&login=".getCode('WRONG_ID');
397 // Try to send a new password for the given user account
398 function USER_DO_NEW_PASSWORD ($email, $uid) {
399 // Compile email when found in address (only secure chars!)
400 if (!empty($email)) $email = str_replace("{DOT}", '.', $email);
402 // Init result and error
406 // Probe userid/nickname
407 if ((EXT_IS_ACTIVE("nickname")) && (NICKNAME_PROBE_ON_USERID($uid))) {
409 $result = SQL_QUERY_ESC("SELECT userid, status FROM `{!_MYSQL_PREFIX!}_user_data` WHERE nickname='%s' OR email='%s' LIMIT 1",
410 array($uid, $email), __FUNCTION__, __LINE__);
411 } elseif (($uid > 0) && (empty($email))) {
412 // Direct userid entered
413 $result = SQL_QUERY_ESC("SELECT userid, status FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
414 array(bigintval($uid)), __FUNCTION__, __LINE__);
415 } elseif (!empty($email)) {
417 $result = SQL_QUERY_ESC("SELECT userid, status FROM `{!_MYSQL_PREFIX!}_user_data` WHERE email='%s' LIMIT 1",
418 array($email), __FUNCTION__, __LINE__);
421 DEBUG_LOG(__FUNCTION__, __LINE__, "Userid is not set! BUG!");
422 $ERROR = getCode('WRONG_ID');
426 if (SQL_NUMROWS($result) == 1) {
427 // This data is valid, so we create a new pass... :-)
428 list($uid, $status) = SQL_FETCHROW($result);
430 if ($status == "CONFIRMED") {
431 // Ooppps, this was missing! ;-) We should update the database...
432 $NEW_PASS = GEN_PASS();
433 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET password='%s' WHERE userid=%s LIMIT 1",
434 array(generateHash($NEW_PASS), $uid), __FUNCTION__, __LINE__);
436 // Prepare data and message for email
437 $msg = LOAD_EMAIL_TEMPLATE("new-pass", array('new_pass' => $NEW_PASS), $uid);
439 // ... and send it away
440 SEND_EMAIL($uid, getMessage('GUEST_NEW_PASSWORD'), $msg);
442 // Output note to user
443 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('GUEST_NEW_PASSWORD_SEND'));
445 // Account is locked or unconfirmed
446 $ERROR = GEN_ERROR_CODE_FROM_ACCOUNT_STATUS($status);
449 LOAD_URL("modules.php?module=index&what=login&login=".$ERROR);
452 // ID or email is wrong
453 LOAD_TEMPLATE("admin_settings_saved", false, "<span class=\"guest_failed\">{--GUEST_WRONG_ID_EMAIL--}</span>");
456 // Return the error code