2 /************************************************************************
3 * MXChange v0.2.1 Start: 08/26/2003 *
4 * =============== Last change: 11/29/2004 *
6 * -------------------------------------------------------------------- *
7 * File : mysql-manager.php *
8 * -------------------------------------------------------------------- *
9 * Short description : All MySQL-related functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Alle MySQL-Relevanten Funktionen *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
23 * This program is distributed in the hope that it will be useful, *
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26 * GNU General Public License for more details. *
28 * You should have received a copy of the GNU General Public License *
29 * along with this program; if not, write to the Free Software *
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
32 ************************************************************************/
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
36 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
41 function ADD_MODULE_TITLE($mod) {
42 global $cacheArray, $_CONFIG;
43 $name = ""; $result = false;
45 // Is the script installed?
46 if (isBooleanConstantAndTrue('mxchange_installed')) {
47 if ((GET_EXT_VERSION("cache") >= "0.1.2") && (isset($cacheArray['modules']['module'])) && (is_array($cacheArray['modules']['module'])) && (isset($cacheArray['modules']['module'][$mod]))) {
49 $name = $cacheArray['modules']['title'][$mod];
52 $_CONFIG['cache_hits']++;
55 $result = SQL_QUERY_ESC("SELECT title FROM "._MYSQL_PREFIX."_mod_reg WHERE module='%s' LIMIT 1", array($mod), __FILE__, __LINE__);
56 list($name) = SQL_FETCHROW($result);
57 SQL_FREERESULT($result);
64 // Still no luck or empty title?
67 $name = LANG_UNKNOWN_MODULE." (".$mod.")";
68 if (SQL_NUMROWS($result) == 0) {
69 // Add module to database
70 $dummy = CHECK_MODULE($mod);
76 // Check validity of a given module name (no file extension)
77 function CHECK_MODULE($mod) {
78 // We need them now here...
79 global $cacheArray, $_CONFIG, $cacheInstance;
81 // Filter module name (names with low chars and underlines are fine!)
82 $mod = preg_replace("/[^a-z_]/", "", $mod);
84 // Check for prefix is a extension...
85 $modSplit = explode("_", $mod);
86 $extension = ""; $mod_chk = $mod;
87 //* DEBUG: */ echo __LINE__."*".count($modSplit)."*/".$mod."*<br />";
88 if (count($modSplit) == 2) {
89 // Okay, there is a seperator (_) in the name so is the first part a module?
90 //* DEBUG: */ echo __LINE__."*".$modSplit[0]."*<br />";
91 if (EXT_IS_ACTIVE($modSplit[0])) {
92 // The prefix is an extension's name, so let's set it
93 $extension = $modSplit[0]; $mod = $modSplit[1];
97 // Major error in module registry is the default
100 // Check if script is installed if not return a "done" to prevent some errors
101 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (!isBooleanConstantAndTrue('admin_registered'))) return "done";
103 // Check if cache is latest version
104 $locked = "Y"; $hidden = "N"; $admin = "N"; $mem = "N"; $found = false;
105 if ((GET_EXT_VERSION("cache") >= "0.1.2") && (isset($cacheArray['modules']['module'])) && (is_array($cacheArray['modules']['module']))) {
106 // Is the module cached?
107 if (isset($cacheArray['modules']['locked'][$mod_chk])) {
109 $locked = $cacheArray['modules']['locked'][$mod_chk];
110 $hidden = $cacheArray['modules']['hidden'][$mod_chk];
111 $admin = $cacheArray['modules']['admin_only'][$mod_chk];
112 $mem = $cacheArray['modules']['mem_only'][$mod_chk];
115 $_CONFIG['cache_hits']++;
118 // No, then we have to update it!
122 // Check for module in database
123 $result = SQL_QUERY_ESC("SELECT locked, hidden, admin_only, mem_only FROM "._MYSQL_PREFIX."_mod_reg WHERE module='%s' LIMIT 1", array($mod_chk), __FILE__, __LINE__);
124 if (SQL_NUMROWS($result) == 1) {
126 list($locked, $hidden, $admin, $mem) = SQL_FETCHROW($result);
127 SQL_FREERESULT($result);
132 // Check returned values against current access permissions
134 // Admin access ----- Guest access ----- --- Guest or member? ---
135 if ((IS_ADMIN()) || (($locked == "N") && ($admin == "N") && (($mem == "N") || (IS_MEMBER())))) {
136 // If you are admin you are welcome for everything!
138 } elseif ($locked == "Y") {
141 } elseif (($mem == "Y") && (!IS_MEMBER())) {
142 // You have to login first!
144 } elseif (($admin == "Y") && (!IS_ADMIN())) {
145 // Only the Admin is allowed to enter this module!
149 // Still no luck or not found?
150 if (($ret == "major") || ($ret == "cache_miss") || (!$found)) {
151 // ----- Legacy module ----- ---- Module in base folder ---- --- Module with extension's name ---
152 if ((FILE_READABLE(sprintf("%sinc/modules/%s.php", PATH, $mod))) || (FILE_READABLE(sprintf("%s%s.php", PATH, $mod))) || (FILE_READABLE(sprintf("%s%s/%s.php", PATH, $extension, $mod)))) {
153 // Data is missing so we add it
154 if (GET_EXT_VERSION("sql_patches") >= "0.3.6") {
155 // Since 0.3.6 we have a has_menu column, this took me a half hour
156 // to find a loop here... *sigh*
157 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_mod_reg
158 (module, locked, hidden, mem_only, admin_only, has_menu) VALUES
159 ('%s', 'Y', 'N', 'N', 'N', 'N')", array($mod_chk), __FILE__, __LINE__);
161 // Wrong/missing sql_patches!
162 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_mod_reg
163 (module, locked, hidden, mem_only, admin_only) VALUES
164 ('%s', 'Y', 'N', 'N', 'N')", array($mod_chk), __FILE__, __LINE__);
167 // Everthing is fine?
168 if (SQL_AFFECTEDROWS() == 0) {
169 // Something bad happend!
173 // Destroy cache here
174 if (GET_EXT_VERSION("cache") >= "0.1.2") {
175 if ($cacheInstance->cache_file("mod_reg", true)) $cacheInstance->cache_destroy();
176 unset($cacheArray['modules']);
180 $ret = CHECK_MODULE($mod_chk);
182 // Module not found we don't add it to the database
191 // Add menu description pending on given file name (without path!)
192 function ADD_DESCR($ACC_LVL, $file, $return = false, $output = true) {
193 global $DEPTH, $_CONFIG;
194 $LINK_ADD = ""; $OUT = ""; $AND = "";
195 // First we have to do some analysis...
196 if (ereg("action-", $file)) {
197 // This is an action file!
199 $search = substr($file, 7);
203 $MOD_CHECK = "admin";
209 $MOD_CHECK = $GLOBALS['module'];
212 $AND = " AND (what='' OR what IS NULL)";
213 } elseif (ereg("what-", $file)) {
214 // This is an admin what file!
216 $search = substr($file, 5);
221 $MOD_CHECK = "admin";
226 $MOD_CHECK = $GLOBALS['module'];
228 $AND = " AND visible='Y' AND locked='N'";
232 $dummy = substr($search, 0, -4);
233 $AND .= " AND action='".GET_ACTION($ACC_LVL, $dummy)."'";
234 } elseif (($ACC_LVL == "sponsor") || ($ACC_LVL == "engine")) {
235 // Sponsor / engine menu
238 $MOD_CHECK = $GLOBALS['module'];
244 $MOD_CHECK = $GLOBALS['module'];
247 if ((!isset($DEPTH)) && (!$return)) {
249 $prefix = "<DIV class=\"you_are_here\">".YOU_ARE_HERE." <STRONG><A class=\"you_are_here\" href=\"".URL."/modules.php?module=".$GLOBALS['module'].$LINK_ADD."\">Home</A></STRONG>";
251 if (!$return) $DEPTH++;
255 $prefix .= " -> ";
257 if (ereg(".php", $search)) {
258 $search = substr($search, 0, strpos($search, ".php"));
261 $result = SQL_QUERY_ESC("SELECT title FROM "._MYSQL_PREFIX."_%s_menu WHERE %s='%s' ".$AND." LIMIT 1",
262 array($ACC_LVL, $type, $search), __FILE__, __LINE__);
264 if (SQL_NUMROWS($result) == 1) {
265 list($ret) = SQL_FETCHROW($result);
266 SQL_FREERESULT($result);
270 } elseif (((GET_EXT_VERSION("sql_patches") >= "0.2.3") && ($_CONFIG['youre_here'] == "Y")) || ((IS_ADMIN()) && ($MOD_CHECK == "admin"))) {
272 $OUT = $prefix."<STRONG><A class=\"you_are_here\" href=\"".URL."/modules.php?module=".$MOD_CHECK."&".$type."=".$search.$LINK_ADD."\">".$ret."</A></STRONG>\n";
273 //* DEBUG: */ echo __LINE__."*".$type."/".$GLOBALS['what']."*<br />\n";
274 if (($type == "what") || (($type == "action") && (!isset($_GET['what'])) && ($GLOBALS['what'] != "welcome"))) {
275 //* DEBUG: */ echo __LINE__."+".$type."+<br />\n";
276 $OUT .= "</DIV><br />\n";
282 // Return or output HTML code?
284 // Output HTML code here
292 function ADD_MENU($MODE, $act, $wht) {
295 // Init some variables
301 if (!VALIDATE_MENU_ACTION($MODE, $act, $wht, true)) return CODE_MENU_NOT_VALID;
303 // Non-admin shall not see all menus
305 $AND = "AND visible='Y' AND locked='N'";
308 // Load SQL data and add the menu to the output stream...
309 $result_main = SQL_QUERY_ESC("SELECT title, action FROM "._MYSQL_PREFIX."_%s_menu WHERE (what='' OR what IS NULL) ".$AND." ORDER BY sort",
310 array($MODE), __FILE__, __LINE__);
311 //* DEBUG: */ echo __LINE__."/".$main_cnt."/".$main_action."/".$sub_what.":".$GLOBALS['what']."*<br />\n";
312 if (SQL_NUMROWS($result_main) > 0) {
313 OUTPUT_HTML("<TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"".$MODE."_menu\">");
314 // There are menus available, so we simply display them... :)
315 while (list($main_title, $main_action) = SQL_FETCHROW($result_main)) {
316 //* DEBUG: */ echo __LINE__."/".$main_cnt."/".$main_action."/".$sub_what.":".$GLOBALS['what']."*<br />\n";
317 // Load menu header template
318 $BLOCK_MODE = false; $act = $main_action;
319 LOAD_TEMPLATE($MODE."_menu_title", false, $main_title);
321 $result_sub = SQL_QUERY_ESC("SELECT title, what FROM "._MYSQL_PREFIX."_%s_menu WHERE action='%s' AND what != '' ".$AND." ORDER BY sort",
322 array($MODE, $main_action), __FILE__, __LINE__);
323 $ctl = SQL_NUMROWS($result_sub);
326 while (list($sub_title, $sub_what) = SQL_FETCHROW($result_sub)) {
330 // Full file name for checking menu
331 //* DEBUG: */ echo __LINE__.":!!!!".$sub_what."!!!<br />\n";
332 $test_inc = sprintf("%sinc/modules/%s/what-%s.php", PATH, $MODE, $sub_what);
333 $test = (FILE_READABLE($test_inc));
335 if ((!empty($wht)) && (($wht == $sub_what))) {
336 $content = "<STRONG>";
340 $content .= "<A name=\"menu\" class=\"menu_blur\" href=\"".URL."/modules.php?module=".$GLOBALS['module']."&what=".$sub_what.ADD_URL_DATA("")."\" target=\"_self\">";
346 $content .= $_CONFIG['menu_blur_spacer'].$sub_title;
354 if ((!empty($wht)) && (($wht == $sub_what))) {
355 $content .= "</STRONG>";
357 $wht = $sub_what; $cnt++;
359 LOAD_TEMPLATE($MODE."_menu_row", false, $content);
361 LOAD_TEMPLATE($MODE."_menu_bottom", false, $content);
365 // This is a menu block... ;-)
367 $INC_BLOCK = sprintf("%sinc/modules/%s/action-%s.php", PATH, $MODE, $main_action);
368 if (FILE_READABLE($INC_BLOCK)) {
370 if ((!EXT_IS_ACTIVE($main_action)) || ($main_action == "online")) OUTPUT_HTML("<TR>
371 <TD class=\"".$MODE."_menu_whats\">");
372 //* DEBUG: */ echo __LINE__."/".$main_cnt."/".$main_action."/".$sub_what.":".$GLOBALS['what']."*<br />\n";
373 include ($INC_BLOCK);
374 //* DEBUG: */ echo __LINE__."/".$main_cnt."/".$main_action."/".$sub_what.":".$GLOBALS['what']."*<br />\n";
375 if ((!EXT_IS_ACTIVE($main_action)) || ($main_action == "online")) OUTPUT_HTML(" </TD>
378 //* DEBUG: */ echo __LINE__."/".$main_cnt."/".$main_action."/".$sub_what.":".$GLOBALS['what']."*<br />\n";
381 //* DEBUG: */ echo __LINE__."/".$main_cnt."/".$main_action."/".$sub_what.":".$GLOBALS['what']."*<br />\n";
382 if (SQL_NUMROWS($result_main) > $main_cnt) OUTPUT_HTML("<TR><TD class=\"".$MODE."_menu_seperator\"></TD></TR>");
386 SQL_FREERESULT($result_main);
389 //* DEBUG: */ echo __LINE__."/".$main_cnt."/".$main_action."/".$sub_what.":".$GLOBALS['what']."*<br />\n";
390 OUTPUT_HTML("</TABLE>");
393 // This patched function will reduce many SELECT queries for the specified or current admin login
394 function IS_ADMIN($admin="")
396 global $cacheArray, $_CONFIG;
397 $ret = false; $passCookie = ""; $valPass = "";
398 //* DEBUG: */ echo __LINE__."ADMIN:".$admin."<br />";
400 // If admin login is not given take current from cookies...
401 if ((empty($admin)) && (isSessionVariableSet('admin_login')) && (isSessionVariableSet('admin_md5'))) {
402 // Get admin login and password from session/cookies
403 $admin = get_session('admin_login');
404 $passCookie = get_session('admin_md5');
406 //* DEBUG: */ echo __LINE__."ADMIN:".$admin."/".$passCookie."<br />";
408 // Search in array for entry
409 if ((!empty($passCookie)) && (isset($cacheArray['admins']['password'][$admin])) && (!empty($admin))) {
411 $_CONFIG['cache_hits']++;
413 // Login data is valid or not?
414 $valPass = generatePassString($cacheArray['admins']['password'][$admin]);
415 } elseif (!empty($admin)) {
417 $result = SQL_QUERY_ESC("SELECT HIGH_PRIORITY password FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
418 array($admin), __FILE__, __LINE__);
422 if (SQL_NUMROWS($result) == 1) {
423 // Admin login was found so let's load password from DB
424 list($passDB) = SQL_FETCHROW($result);
426 // Generate password hash
427 $valPass = generatePassString($passDB);
431 SQL_FREERESULT($result);
434 if (!empty($valPass)) {
435 // Check if password is valid
436 //* DEBUG: */ echo __FUNCTION__."*".$valPass."/".$passCookie."*<br />\n";
437 $ret = (($valPass == $passCookie) || ((strlen($valPass) == 32) && ($valPass == md5($passCookie))) || (($valPass == "*FAILED*") && (!EXT_IS_ACTIVE("cache"))));
440 // Return result of comparision
441 //* DEBUG: */ if (!$ret) echo __LINE__."OK!<br>";
445 function ADD_MAX_RECEIVE_LIST($MODE, $default="", $return=false)
452 // Guests (in the registration form) are not allowed to select 0 mails per day.
453 $result = SQL_QUERY("SELECT value, comment FROM "._MYSQL_PREFIX."_max_receive WHERE value > 0 ORDER BY value", __FILE__, __LINE__);
454 if (SQL_NUMROWS($result) > 0)
457 while (list($value, $comment) = SQL_FETCHROW($result))
459 $OUT .= " <OPTION value=\"".$value."\"";
460 if ($_POST['max_mails'] == $value) $OUT .= " selected=\"selected\"";
461 $OUT .= ">".$value." ".PER_DAY;
462 if (!empty($comment)) $OUT .= " (".$comment.")";
463 $OUT .= "</OPTION>\n";
465 define('__MAX_RECEIVE_OPTIONS', $OUT);
468 SQL_FREERESULT($result);
469 $OUT = LOAD_TEMPLATE("guest_receive_table", true);
473 // Maybe the admin has to setup some maximum values?
478 // Members are allowed to set to zero mails per day (we will change this soon!)
479 $result = SQL_QUERY("SELECT value, comment FROM "._MYSQL_PREFIX."_max_receive ORDER BY value", __FILE__, __LINE__);
480 if (SQL_NUMROWS($result) > 0)
483 while (list($value, $comment) = SQL_FETCHROW($result))
485 $OUT .= " <OPTION value=\"".$value."\"";
486 if ($default == $value) $OUT .= " selected=\"selected\"";
487 $OUT .= ">".$value." ".PER_DAY;
488 if (!empty($comment)) $OUT .= " (".$comment.")";
489 $OUT .= "</OPTION>\n";
491 define('__MAX_RECEIVE_OPTIONS', $OUT);
492 SQL_FREERESULT($result);
493 $OUT = LOAD_TEMPLATE("member_receive_table", true);
497 // Maybe the admin has to setup some maximum values?
498 $OUT = LOAD_TEMPLATE("admin_settings_saved", true, NO_MAX_VALUES);
504 // Return generated HTML code
509 // Output directly (default)
514 function SEARCH_EMAIL_USERTAB($email)
517 $result = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE email LIKE '{PER}%s{PER}' LIMIT 1", array($email), __FILE__, __LINE__);
518 if (SQL_NUMROWS($result) == 1) $ret = true;
519 SQL_FREERESULT($result);
523 function WHAT_IS_VALID($act, $wht, $type="guest")
527 // Everything is valid to the admin :-)
533 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_%s_menu WHERE action='%s' AND what='%s' AND locked='N' LIMIT 1", array($type, $act, $wht), __FILE__, __LINE__);
535 if (SQL_NUMROWS($result) == 1) $ret = true;
536 SQL_FREERESULT($result);
543 global $status, $LAST;
544 if (!is_array($LAST)) $LAST = array();
547 // Fix "deleted" cookies first
548 FIX_DELETED_COOKIES(array('userid', 'u_hash', 'lifetime'));
551 if ((!empty($GLOBALS['userid'])) && (isSessionVariableSet('u_hash')) && (isSessionVariableSet('lifetime')) && (defined('COOKIE_PATH')))
553 // Cookies are set with values, but are they valid?
554 $result = SQL_QUERY_ESC("SELECT password, status, last_module, last_online FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
555 array($GLOBALS['userid']), __FILE__, __LINE__);
556 if (SQL_NUMROWS($result) == 1)
558 // Load data from cookies
559 list($password, $status, $mod, $onl) = SQL_FETCHROW($result);
561 // Validate password by created the difference of it and the secret key
562 $valPass = generatePassString($password);
564 // Transfer last module and online time
565 if ((!empty($mod)) && (empty($LAST['module']))) { $LAST['module'] = $mod; $LAST['online'] = $onl; }
567 // So did we now have valid data and an unlocked user?
568 //* DEBUG: */ echo $valPass."<br>".get_session('u_hash')."<br>";
569 if (($status == "CONFIRMED") && ($valPass == get_session('u_hash'))) {
570 // Account is confirmed and all cookie data is valid so he is definely logged in! :-)
573 // Maybe got locked etc.
574 //* DEBUG: */ echo __LINE__."!!!<br>";
575 destroy_user_session();
577 // Remove array elements to prevent errors
578 unset($GLOBALS['userid']);
581 // Cookie data is invalid!
582 //* DEBUG: */ echo __LINE__."***<br>";
584 // Remove array elements to prevent errors
585 unset($GLOBALS['userid']);
589 SQL_FREERESULT($result);
593 // Cookie data is invalid!
594 //* DEBUG: */ echo __LINE__."///<br>";
595 destroy_user_session();
597 // Remove array elements to prevent errors
598 unset($GLOBALS['userid']);
603 function UPDATE_LOGIN_DATA ($UPDATE=true) {
605 if (!is_array($LAST)) $LAST = array();
607 // Are the required cookies set?
608 if ((!isset($GLOBALS['userid'])) || (!isSessionVariableSet('u_hash')) || (!isSessionVariableSet('lifetime'))) {
609 // Nope, then return here to caller function
613 $GLOBALS['userid'] = bigintval(get_session('userid'));
616 // Extract last online time (life) and how long is auto-login valid (time)
617 $newl = time() + bigintval(get_session('lifetime'));
619 // Recheck if logged in
620 if (!IS_MEMBER()) return false;
622 // Load last module and last online time
623 $result = SQL_QUERY_ESC("SELECT last_module, last_online FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1", array($GLOBALS['userid']), __FILE__, __LINE__);
624 if (SQL_NUMROWS($result) == 1) {
625 // Load last module and online time
626 list($mod, $onl) = SQL_FETCHROW($result);
627 SQL_FREERESULT($result);
629 // Maybe first login time?
630 if (empty($mod)) $mod = "login";
632 if (set_session("userid", $GLOBALS['userid'], $newl, COOKIE_PATH) && set_session("u_hash", get_session('u_hash'), $newl, COOKIE_PATH) && set_session("lifetime", bigintval(get_session('lifetime')), $newl, COOKIE_PATH)) {
633 // This will be displayed on welcome page! :-)
634 if (empty($LAST['module'])) {
635 $LAST['module'] = $mod; $LAST['online'] = $onl;
637 if (empty($GLOBALS['what'])) {
638 $GLOBALS['what'] = "welcome";
641 // Update last module / online time
642 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET last_module='%s', last_online=UNIX_TIMESTAMP() WHERE userid=%s LIMIT 1",
643 array($GLOBALS['what'], $GLOBALS['userid']), __FILE__, __LINE__);
646 // Destroy session, we cannot update!
647 destroy_user_session();
651 function VALIDATE_MENU_ACTION ($MODE, $act, $wht, $UPDATE=false)
655 if ((!IS_ADMIN()) && ($MODE != "admin")) $ADD = " AND locked='N'";
656 //* DEBUG: */ echo __LINE__.":".$MODE."/".$act."/".$wht."*<br />\n";
657 if (($MODE != "admin") && ($UPDATE))
659 // Update guest or member menu
660 $SQL = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_%s_menu SET counter=counter+1 WHERE action='%s' AND what='%s'".$ADD." LIMIT 1",
661 array($MODE, $act, $wht), __FILE__, __LINE__, false);
663 elseif ($wht != "overview")
666 $SQL = SQL_QUERY_ESC("SELECT id, what FROM "._MYSQL_PREFIX."_%s_menu WHERE action='%s'".$ADD." ORDER BY action DESC LIMIT 1",
667 array($MODE, $act), __FILE__, __LINE__, false);
671 // Admin login overview
672 $SQL = SQL_QUERY_ESC("SELECT id, what FROM "._MYSQL_PREFIX."_%s_menu WHERE action='%s' AND (what='' OR what IS NULL)".$ADD." ORDER BY action DESC LIMIT 1",
673 array($MODE, $act), __FILE__, __LINE__, false);
677 $result = SQL_QUERY($SQL, __FILE__, __LINE__);
680 if (SQL_AFFECTEDROWS() == 1) $ret = true;
681 //* DEBUG: */ debug_print_backtrace();
685 if (SQL_NUMROWS($result) == 1) {
686 list($id, $wht2) = SQL_FETCHROW($result);
687 //* DEBUG: */ echo __LINE__."+".$SQL."+<br />\n";
688 //* DEBUG: */ echo __LINE__."*".$id."/".$wht."/".$wht2."*<br />\n";
694 SQL_FREERESULT($result);
700 function GET_MOD_DESCR($MODE, $wht)
702 if (empty($wht)) $wht = "welcome";
703 $ret = "??? (".$wht.")";
704 $result = SQL_QUERY_ESC("SELECT title FROM "._MYSQL_PREFIX."_%s_menu WHERE what='%s' LIMIT 1", array($MODE, $wht), __FILE__, __LINE__);
705 if (SQL_NUMROWS($result) == 1)
707 list($ret) = SQL_FETCHROW($result);
708 SQL_FREERESULT($result);
713 function SEND_MODE_MAILS($mod, $modes)
715 global $_CONFIG, $DATA;
718 $result_main = SQL_QUERY_ESC("SELECT password FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
719 array($GLOBALS['userid']), __FILE__, __LINE__);
720 if (SQL_NUMROWS($result_main) == 1) {
721 // Load hash from database
722 list($hashDB) = SQL_FETCHROW($result_main);
724 // Extract salt from cookie
725 $salt = substr(get_session('u_hash'), 0, -40);
727 // Now let's compare passwords
728 $hash = generatePassString($hashDB);
729 if (($hash == get_session('u_hash')) || ($_POST['pass1'] == $_POST['pass2'])) {
731 $result = SQL_QUERY_ESC("SELECT gender, surname, family, street_nr, country, zip, city, email FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s AND password='%s' LIMIT 1",
732 array($GLOBALS['userid'], $hashDB), __FILE__, __LINE__);
733 if (SQL_NUMROWS($result) == 1) {
735 $DATA = SQL_FETCHROW($result);
738 SQL_FREERESULT($result);
741 $DATA[0] = TRANSLATE_GENDER($DATA[0]);
743 // Clear/init the content variable
750 foreach ($modes as $mode) {
753 case "normal": break; // Do not add any special lines
755 case "email": // Email was changed!
756 $content = MEMBER_CHANGED_EMAIL.": ".$_POST['old_addy']."\n";
759 case "pass": // Password was changed
760 $content = MEMBER_CHANGED_PASS."\n";
764 $content = MEMBER_UNKNOWN_MODE.": ".$mode."\n\n";
769 if (EXT_IS_ACTIVE("country")) {
770 // Replace code with description
771 $DATA[4] = COUNTRY_GENERATE_INFO($_POST['country_code']);
775 $msg = LOAD_EMAIL_TEMPLATE("member_mydata_notify", $content, $GLOBALS['userid']);
777 if ($_CONFIG['admin_notify'] == "Y") {
778 // The admin needs to be notified about a profile change
779 $msg_admin = "admin_mydata_notify";
780 $sub_adm = ADMIN_CHANGED_DATA;
788 $sub_mem = MEMBER_CHANGED_DATA;
790 // Output success message
791 $content = "<STRONG><SPAN class=\"member_done\">".MYDATA_MAIL_SENT."</SPAN></STRONG>";
795 $content = "<STRONG><SPAN class=\"member_failed\">".UNKNOWN_MODULE."</SPAN></STRONG>";
799 // Could not load profile data
800 $content = "<STRONG><SPAN class=\"member_failed\">".MEMBER_CANNOT_LOAD_PROFILE."</SPAN></STRONG>";
803 // Passwords mismatch
804 $content = "<STRONG><SPAN class=\"member_failed\">".MEMBER_PASSWORD_ERROR."</SPAN></STRONG>";
807 // Could not load profile
808 $content = "<STRONG><SPAN class=\"member_failed\">".MEMBER_CANNOT_LOAD_PROFILE."</SPAN></STRONG>";
811 // Send email to user if required
812 if ((!empty($sub_mem)) && (!empty($msg))) {
814 SEND_EMAIL($DATA[7], $sub_mem, $msg);
817 // Send only if no other error has occured
818 if (empty($content)) {
819 if ((!empty($sub_adm)) && (!empty($msg_admin))) {
821 SEND_ADMIN_NOTIFICATION($sub_adm, $msg_admin, $content, $GLOBALS['userid']);
822 } elseif ($_CONFIG['admin_notify'] == "Y") {
823 // Cannot send mails to admin!
824 $content = CANNOT_SEND_ADMIN_MAILS;
827 $content = "<STRONG><SPAN class=\"member_done\">".MYDATA_MAIL_SENT."</SPAN></STRONG>";
832 LOAD_TEMPLATE("admin_settings_saved", false, $content);
834 // Update module counter
835 function COUNT_MODULE($mod)
839 // Do count all other modules but not accesses on CSS file css.php!
840 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_mod_reg SET clicks=clicks+1 WHERE module='%s' LIMIT 1",
841 array($mod), __FILE__, __LINE__);
844 // Get action value from mode (admin/guest/member) and what-value
845 function GET_ACTION ($MODE, &$wht)
847 global $ret; $ret = "";
848 //* DEBUG: */ echo __LINE__."=".$MODE."/".$wht."/".$GLOBALS['action']."=<br>";
849 if ((empty($wht)) && ($MODE != "admin"))
853 if ($MODE == "admin")
855 // Action value for admin area
856 if (!empty($GLOBALS['action']))
858 // Get it directly from URL
859 return $GLOBALS['action'];
861 elseif (($wht == "overview") || (empty($GLOBALS['what'])))
863 // Default value for admin area
867 elseif (!empty($GLOBALS['action']))
870 if (empty($wht)) $wht = "welcome";
871 return $GLOBALS['action'];
875 // Everything else will be touched after checking the module has a menu assigned
877 //* DEBUG: */ echo __LINE__."*".$ret."*<br />\n";
879 if (MODULE_HAS_MENU($MODE))
881 // Rewriting modules to menu
884 case "index": $MODE = "guest"; break;
885 case "login": $MODE = "member"; break;
889 // Guest and member menu is "main" as the default
890 if (empty($ret)) $ret = "main";
892 // Load from database
893 $result = SQL_QUERY_ESC("SELECT action FROM "._MYSQL_PREFIX."_%s_menu WHERE what='%s' LIMIT 1",
894 array($MODE, $wht), __FILE__, __LINE__);
895 if (SQL_NUMROWS($result) == 1)
897 // Load action value and pray that this one is the right you want... ;-)
898 list($ret) = SQL_FETCHROW($result);
902 SQL_FREERESULT($result);
905 // Return action value
909 function GET_CATEGORY ($cid) {
910 // Default is not found
911 $ret = _CATEGORY_404;
913 // Is the category id set?
916 // Lookup the category
917 $result = SQL_QUERY_ESC("SELECT cat FROM "._MYSQL_PREFIX."_cats WHERE id=%s LIMIT 1",
918 array(bigintval($cid)), __FILE__, __LINE__);
919 if (SQL_NUMROWS($result) == 1) {
920 // Category found... :-)
921 list($ret) = SQL_FETCHROW($result);
925 SQL_FREERESULT($result);
932 function GET_PAYMENT ($pid, $full=false) {
933 // Default is not found
937 $result = SQL_QUERY_ESC("SELECT mail_title, price FROM "._MYSQL_PREFIX."_payments WHERE id=%s LIMIT 1",
938 array(bigintval($pid)), __FILE__, __LINE__);
939 if (SQL_NUMROWS($result) == 1) {
940 // Payment type found... :-)
943 list($ret) = SQL_FETCHROW($result);
945 // Return title and price
946 list($t, $p) = SQL_FETCHROW($result);
947 $ret = $t." / ".TRANSLATE_COMMA($p)." ".POINTS;
952 SQL_FREERESULT($result);
958 function GET_PAY_POINTS($pid, $lookFor="price")
961 $result = SQL_QUERY_ESC("SELECT %s FROM "._MYSQL_PREFIX."_payments WHERE id=%s LIMIT 1",
962 array($lookFor, $pid), __FILE__, __LINE__);
963 if (SQL_NUMROWS($result) == 1)
965 // Payment type found... :-)
966 list($ret) = SQL_FETCHROW($result);
967 SQL_FREERESULT($result);
971 // Remove a receiver's ID from $ARRAY and add a link for him to confirm
972 function REMOVE_RECEIVER(&$ARRAY, $key, $uid, $pool_id, $stats_id="", $bonus=false)
977 // Remove entry from array
980 // Is there already a line for this user available?
983 // Only when we got a real stats ID continue searching for the entry
984 $type = "NORMAL"; $rowName = "stats_id";
985 if ($bonus) { $type = "BONUS"; $rowName = "bonus_id"; }
986 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_links WHERE %s='%s' AND userid=%s AND link_type='%s' LIMIT 1",
987 array($rowName, $stats_id, bigintval($uid), $type), __FILE__, __LINE__);
988 if (SQL_NUMROWS($result) == 0)
990 // No, so we add one!
991 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_links (%s, userid, link_type) VALUES ('%s', '%s', '%s')",
992 array($rowName, $stats_id, bigintval($uid), $type), __FILE__, __LINE__);
1002 SQL_FREERESULT($result);
1005 // Return status for sending routine
1009 function GET_TOTAL_DATA($search, $tableName, $lookFor, $whereStatement="userid", $onlyRows=false)
1014 $result = SQL_QUERY_ESC("SELECT COUNT(%s) FROM "._MYSQL_PREFIX."_%s WHERE %s='%s'",
1015 array($lookFor, $tableName, $whereStatement, $search), __FILE__, __LINE__);
1018 $result = SQL_QUERY_ESC("SELECT SUM(%s) FROM "._MYSQL_PREFIX."_%s WHERE %s='%s'",
1019 array($lookFor, $tableName, $whereStatement, $search), __FILE__, __LINE__);
1023 list($ret) = SQL_FETCHROW($result);
1024 //* DEBUG: */ echo __LINE__."*".$DATA."/".$search."/".$tableName."/".$ret."*<br />\n";
1025 SQL_FREERESULT($result);
1027 if (($lookFor == "counter") || ($lookFor == "id")) {
1037 * Dynamic referral system, can also send mails!
1039 * uid = Referral ID wich should receive...
1040 * points = ... xxx points
1041 * send_notify = shall I send the referral an email or not?
1042 * rid = inc/modules/guest/what-confirm.php need this
1043 * locked = Shall I pay it to normal (false) or locked (true) points ammount?
1044 * add_mode = Add points only to $uid or also refs? (WARNING! Changing "ref" to "direct"
1045 * will cause no referral will get points ever!!!)
1047 function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $locked=false, $add_mode="ref")
1049 global $DEPTH, $_CONFIG, $DATA;
1052 //DEBUG_LOG(__FUNCTION__.": uid={$uid},points={$points}");
1054 // When $uid = 0 add points to jackpot
1056 // Add points to jackpot
1057 ADD_JACKPOT($points);
1061 // Count up referral depth
1062 if (empty($DEPTH)) {
1063 // Initialialize referral system
1066 // Increase referral level
1070 // Percents and table
1071 $percents = "percents"; if (isset($_CONFIG['db_percents'])) $percents = $_CONFIG['db_percents'];
1072 $table = "refdepths"; if (isset($_CONFIG['db_table'])) $table = $_CONFIG['db_table'];
1074 // Which points, locked or normal?
1075 $data = "points"; if ($locked) $data = "locked_points";
1077 // Check user account
1078 $result_user = SQL_QUERY_ESC("SELECT refid, email FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
1079 array(bigintval($uid)), __FILE__, __LINE__);
1081 //* DEBUG */ echo "+".SQL_NUMROWS($result_user).":".$points."+<br />\n";
1082 if (SQL_NUMROWS($result_user) == 1) {
1083 // This is the user and his ref
1084 list ($ref, $email) = SQL_FETCHROW($result_user);
1087 //DEBUG_LOG(__FUNCTION__.": ref={$ref},email={$email},DEPTH={$DEPTH}");
1090 $result_lvl = SQL_QUERY_ESC("SELECT %s FROM "._MYSQL_PREFIX."_%s WHERE level='%s' LIMIT 1",
1091 array($percents, $table, bigintval($DEPTH)), __FILE__, __LINE__);
1092 //* DEBUG */ echo "DEPTH:".$DEPTH."<br />\n";
1093 if (SQL_NUMROWS($result_lvl) == 1) {
1095 list($per) = SQL_FETCHROW($result_lvl);
1097 // Calculate new points
1098 $ref_points = $points * $per / 100;
1101 //DEBUG_LOG(__FUNCTION__.": percent={$per},ref_points={$ref_points}");
1104 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points SET %s=%s+%s WHERE userid=%s AND ref_depth=%d LIMIT 1",
1105 array($data, $data, $ref_points, bigintval($uid), bigintval($DEPTH)), __FILE__, __LINE__);
1108 //DEBUG_LOG(__FUNCTION__.": affectedRows=".SQL_AFFECTEDROWS().",DEPTH={$DEPTH}");
1110 // No entry updated?
1111 if (SQL_AFFECTEDROWS() == 0) {
1112 // First ref in this level! :-)
1113 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_points (userid, ref_depth, %s) VALUES (%s, %d, %s)",
1114 array($data, bigintval($uid), bigintval($DEPTH), $ref_points), __FILE__, __LINE__);
1117 //DEBUG_LOG(__FUNCTION__.": insertedRows=".SQL_AFFECTEDROWS()."");
1120 // Update mediadata as well
1121 if (GET_EXT_VERSION("mediadata") >= "0.0.4") {
1123 MEDIA_UPDATE_ENTRY(array("total_points"), "add", $ref_points);
1126 // Points updated, maybe I shall send him an email?
1127 if (($send_notify) && ($ref > 0) && (!$locked)) {
1131 'level' => bigintval($DEPTH),
1132 'points' => $ref_points,
1133 'refid' => bigintval($ref)
1136 // Load email template
1137 $msg = LOAD_EMAIL_TEMPLATE("confirm-referral", $content, bigintval($uid));
1139 SEND_EMAIL($email, THANX_REFERRAL_ONE, $msg);
1140 } elseif (($send_notify) && ($ref == 0) && (!$locked) && ($add_mode == "direct") && (!defined('__POINTS_VALUE'))) {
1141 // Direct payment shall be notified about
1142 define('__POINTS_VALUE', $ref_points);
1145 $msg = LOAD_EMAIL_TEMPLATE("add-points", REASON_DIRECT_PAYMENT, $uid);
1148 SEND_EMAIL($email, SUBJECT_DIRECT_PAYMENT, $msg);
1149 if (!isset($_GET['mid'])) LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_POINTS_ADDED);
1152 // Maybe there's another ref?
1153 if (($ref > 0) && ($points > 0) && ($ref != $uid) && ($add_mode == "ref")) {
1154 // Then let's credit him here...
1155 ADD_POINTS_REFSYSTEM($ref, $points, $send_notify, $ref, $locked);
1160 SQL_FREERESULT($result_lvl);
1164 SQL_FREERESULT($result_user);
1167 function UPDATE_REF_COUNTER($uid)
1169 global $REF_LVL, $cacheInstance;
1171 // Make it sure referral level zero (member him-/herself) is at least selected
1172 if (empty($REF_LVL)) $REF_LVL = 0;
1175 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_refsystem SET counter=counter+1 WHERE userid=%s AND level='%s' LIMIT 1",
1176 array(bigintval($uid), $REF_LVL), __FILE__, __LINE__);
1178 // When no entry was updated then we have to create it here
1179 if (SQL_AFFECTEDROWS() == 0)
1182 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_refsystem (userid, level, counter) VALUES ('%s', '%s', '1')",
1183 array(bigintval($uid), $REF_LVL), __FILE__, __LINE__);
1186 // Check for his referral
1187 $result = SQL_QUERY_ESC("SELECT refid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
1188 array(bigintval($uid)), __FILE__, __LINE__);
1189 list($ref) = SQL_FETCHROW($result);
1192 SQL_FREERESULT($result);
1194 // When he has a referral...
1195 if (($ref > 0) && ($ref != $uid))
1197 // Move to next referral level and count his counter one up!
1198 $REF_LVL++; UPDATE_REF_COUNTER($ref);
1200 elseif ((($ref == $uid) || ($ref == 0)) && (GET_EXT_VERSION("cache") >= "0.1.2"))
1202 // Remove cache here
1203 if ($cacheInstance->cache_file("refsystem", true)) $cacheInstance->cache_destroy();
1206 // Updates/extends the online list
1207 function UPDATE_ONLINE_LIST($SID, $mod, $act, $wht) {
1210 // Do not update online list when extension is deactivated
1211 if (!EXT_IS_ACTIVE("online", true)) return;
1215 // This is invalid here!
1216 print "Invalid session. Backtrace:<pre>";
1217 debug_print_backtrace();
1221 // Initialize variables
1222 $uid = 0; $rid = 0; $MEM = "N"; $ADMIN = "N";
1225 if ((!empty($GLOBALS['userid'])) && ($GLOBALS['userid'] > 0) && (IS_MEMBER())) {
1227 $uid = bigintval($GLOBALS['userid']);
1236 if (isSessionVariableSet('refid')) {
1238 if (get_session('refid') > 0) $rid = bigintval($GLOBALS['refid']);
1241 // Now search for the user
1242 $result = SQL_QUERY_ESC("SELECT timestamp FROM "._MYSQL_PREFIX."_online
1243 WHERE sid='%s' LIMIT 1",
1244 array($SID), __FILE__, __LINE__);
1247 if (SQL_NUMROWS($result) == 1) {
1249 SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_online SET
1257 timestamp=UNIX_TIMESTAMP()
1258 WHERE sid='%s' LIMIT 1",
1259 array($mod, $act, $wht, $uid, $rid, $MEM, $ADMIN, $SID), __FILE__, __LINE__
1262 // No entry does exists so we simply add it!
1263 SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_online (module, action, what, userid, refid, is_member, is_admin, timestamp, sid, ip) VALUES ('%s', '%s', '%s', %s, %s, '%s', '%s', UNIX_TIMESTAMP(), '%s', '%s')",
1264 array($mod, $act, $wht, $uid, $rid, $MEM, $ADMIN, $SID, getenv('REMOTE_ADDR')), __FILE__, __LINE__
1269 SQL_FREERESULT($result);
1271 // Purge old entries
1272 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_online WHERE timestamp <= (UNIX_TIMESTAMP() - %s)",
1273 array($_CONFIG['online_timeout']), __FILE__, __LINE__);
1275 // OBSULETE: Sends out mail to all administrators
1276 function SEND_ADMIN_EMAILS($subj, $msg) {
1277 // Load all admin email addresses
1278 $result = SQL_QUERY("SELECT email FROM "._MYSQL_PREFIX."_admins ORDER BY id ASC", __FILE__, __LINE__);
1279 while (list($email) = SQL_FETCHROW($result)) {
1280 // Send the email out
1281 SEND_EMAIL($email, $subj, $msg);
1285 SQL_FREERESULT($result);
1287 // Really simple... ;-)
1289 // Get ID number from administrator's login name
1290 function GET_ADMIN_ID($login) {
1293 if (!empty($cacheArray['admins']['aid'][$login])) {
1295 $ret = $cacheArray['admins']['aid'][$login];
1296 if (empty($ret)) $ret = "-1";
1298 // Load from database
1299 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
1300 array($login), __FILE__, __LINE__);
1301 if (SQL_NUMROWS($result) == 1) {
1302 list($ret) = SQL_FETCHROW($result);
1306 SQL_FREERESULT($result);
1311 // Get password hash from administrator's login name
1312 function GET_ADMIN_HASH($login)
1316 if (!empty($cacheArray['admins']['password'][$login]))
1319 $ret = $cacheArray['admins']['password'][$login];
1320 if (empty($ret)) $ret = "-1";
1324 // Load from database
1325 $result = SQL_QUERY_ESC("SELECT password FROM "._MYSQL_PREFIX."_admins WHERE login='%s' LIMIT 1",
1326 array($login), __FILE__, __LINE__);
1327 if (SQL_NUMROWS($result) == 1)
1329 list($ret) = SQL_FETCHROW($result);
1330 SQL_FREERESULT($result);
1336 function GET_ADMIN_LOGIN ($aid) {
1339 if (!empty($cacheArray['admins']['login'])) {
1341 if (!empty($cacheArray['admins']['login'][$aid])) {
1342 $ret = $cacheArray['admins']['login'][$aid];
1344 if (empty($ret)) $ret = "***";
1346 // Load from database
1347 $result = SQL_QUERY_ESC("SELECT login FROM "._MYSQL_PREFIX."_admins WHERE id=%s LIMIT 1",
1348 array(bigintval($aid)), __FILE__, __LINE__);
1349 if (SQL_NUMROWS($result) == 1) {
1351 list($ret) = SQL_FETCHROW($result);
1354 $cacheArray['admins']['login'][$aid] = $ret;
1358 SQL_FREERESULT($result);
1363 function ADD_OPTION_LINES($table, $id, $name, $default="",$special="",$where="") {
1365 if ($table == "/ARRAY/") {
1366 // Selection from array
1367 if (is_array($id) && is_array($name) && sizeof($id) == sizeof($name)) {
1369 foreach ($id as $idx => $value) {
1370 $ret .= "<OPTION value=\"".$value."\"";
1371 if ($default == $value) $ret .= " selected checked";
1372 $ret .= ">".$name[$idx]."</OPTION>\n";
1376 // Data from database
1378 if (!empty($special)) $SPEC = ", ".$special;
1379 $ORDER = $name.$SPEC;
1380 if ($table == "country") $ORDER = $special;
1381 $result = SQL_QUERY_ESC("SELECT %s, %s".$SPEC." FROM "._MYSQL_PREFIX."_%s ".$where." ORDER BY %s",
1382 array($id, $ORDER, $table, $name), __FILE__, __LINE__);
1383 if (SQL_NUMROWS($result) > 0) {
1384 // Found data so add them as OPTION lines: $id is the value and $name is the "name" of the option
1385 while (list($value, $title, $add) = SQL_FETCHROW($result)) {
1386 if (empty($special)) $add = "";
1387 $ret .= "<OPTION value=\"".$value."\"";
1388 if ($default == $value) $ret .= " selected checked";
1389 if (!empty($add)) $add = " (".$add.")";
1390 $ret .= ">".$title.$add."</OPTION>\n";
1394 SQL_FREERESULT($result);
1397 $ret = "<OPTION value=\"x\">".SELECT_NONE."</OPTION>\n";
1401 // Return - hopefully - the requested data
1405 function activateExchange() {
1407 $result = SQL_QUERY("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE status='CONFIRMED' AND max_mails > 0", __FILE__, __LINE__);
1408 if (SQL_NUMROWS($result) >= $_CONFIG['activate_xchange'])
1411 SQL_FREERESULT($result);
1415 "UPDATE "._MYSQL_PREFIX."_mod_reg SET locked='N', hidden='N', mem_only='Y' WHERE module='order' LIMIT 1",
1416 "UPDATE "._MYSQL_PREFIX."_member_menu SET visible='Y', locked='N' WHERE what='order' OR what='unconfirmed' LIMIT 2",
1417 "UPDATE "._MYSQL_PREFIX."_config SET activate_xchange='0' WHERE config=0 LIMIT 1"
1421 foreach ($SQLs as $sql) {
1422 $result = SQL_QUERY($sql, __FILE__, __LINE__);
1425 // @TODO Destroy cache
1429 function DELETE_USER_ACCOUNT($uid, $reason)
1432 $result = SQL_QUERY_ESC("SELECT (SUM(p.points) - d.used_points) AS points
1433 FROM "._MYSQL_PREFIX."_user_points AS p
1434 LEFT JOIN "._MYSQL_PREFIX."_user_data AS d
1435 ON p.userid=d.userid
1436 WHERE p.userid=%s", array(bigintval($uid)), __FILE__, __LINE__);
1437 if (SQL_NUMROWS($result) == 1) {
1438 // Save his points to add them to the jackpot
1439 list($points) = SQL_FETCHROW($result);
1440 SQL_FREERESULT($result);
1442 // Delete points entries as well
1443 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_points WHERE userid=%s", array(bigintval($uid)), __FILE__, __LINE__);
1445 // Update mediadata as well
1446 if (GET_EXT_VERSION("mediadata") >= "0.0.4") {
1448 MEDIA_UPDATE_ENTRY(array("total_points"), "sub", $points);
1451 // Now, when we have all his points adds them do the jackpot!
1452 ADD_JACKPOT($points);
1455 // Delete category selections as well...
1456 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_cats WHERE userid=%s",
1457 array(bigintval($uid)), __FILE__, __LINE__);
1459 // Remove from rallye if found
1460 if (EXT_IS_ACTIVE("rallye")) {
1461 $result = SQL_QUERY("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_rallye_users WHERE userid=%s",
1462 array(bigintval($uid)), __FILE__, __LINE__);
1465 // Now a mail to the user and that's all...
1466 $msg = LOAD_EMAIL_TEMPLATE("del-user", $reason, $uid);
1467 SEND_EMAIL($uid, ADMIN_DEL_ACCOUNT, $msg);
1469 // Ok, delete the account!
1470 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1", array(bigintval($uid)), __FILE__, __LINE__);
1473 function META_DESCRIPTION($mod, $wht)
1475 global $_CONFIG, $DEPTH;
1476 if (($mod != "admin") && ($mod != "login"))
1478 // Exclude admin and member's area
1479 $DESCR = MAIN_TITLE." ".trim($_CONFIG['title_middle'])." ".ADD_DESCR("guest", "what-".$wht, true);
1481 OUTPUT_HTML("<META name=\"description\" content=\"".$DESCR."\">");
1485 function ADD_JACKPOT($points)
1487 $result = SQL_QUERY("SELECT points FROM "._MYSQL_PREFIX."_jackpot WHERE ok='ok' LIMIT 1", __FILE__, __LINE__);
1488 if (SQL_NUMROWS($result) == 0)
1491 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_jackpot (ok, points) VALUES ('ok', '%s')", array($points), __FILE__, __LINE__);
1496 SQL_FREERESULT($result);
1499 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_jackpot SET points=points+%s WHERE ok='ok' LIMIT 1",
1500 array($points), __FILE__, __LINE__);
1504 function SUB_JACKPOT($points)
1509 // Get current points
1510 $result = SQL_QUERY("SELECT points FROM "._MYSQL_PREFIX."_jackpot WHERE ok='ok' LIMIT 1", __FILE__, __LINE__);
1511 if (SQL_NUMROWS($result) == 0)
1514 $result = SQL_QUERY("INSERT INTO "._MYSQL_PREFIX."_jackpot (ok, points) VALUES ('ok', 0.00000)", __FILE__, __LINE__);
1519 SQL_FREERESULT($result);
1522 list($jackpot) = SQL_FETCHROW($result);
1523 if ($jackpot >= $points)
1525 // Update points when there are enougth points in jackpot
1526 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_jackpot SET points=points-%s WHERE ok='ok' LIMIT 1",
1527 array($points), __FILE__, __LINE__);
1528 $ret = $jackpot - $points;
1533 function IS_DEMO() {
1534 return ((EXT_IS_ACTIVE("demo")) && (get_session('admin_login') == "demo"));
1537 function LOAD_CONFIG($no="0") {
1539 $CFG_DUMMY = array();
1541 // Check for cache extension, cache-array and if the requested configuration is in cache
1542 if ((is_array($cacheArray)) && (isset($cacheArray['config'][$no])) && (is_array($cacheArray['config'][$no]))) {
1543 // Load config from cache
1544 //* DEBUG: */ echo gettype($cacheArray['config'][$no])."<br />\n";
1545 foreach ($cacheArray['config'][$no] as $key => $value) {
1546 $CFG_DUMMY[$key] = $value;
1549 // Count cache hits if exists
1550 if ((isset($CFG_DUMMY['cache_hits'])) && (EXT_IS_ACTIVE("cache"))) {
1551 $CFG_DUMMY['cache_hits']++;
1554 // Load config from DB
1555 $result_config = SQL_QUERY_ESC("SELECT * FROM "._MYSQL_PREFIX."_config WHERE config=%d LIMIT 1",
1556 array(bigintval($no)), __FILE__, __LINE__);
1558 // Get config from database
1559 $CFG_DUMMY = SQL_FETCHARRAY($result_config);
1562 SQL_FREERESULT($result_config);
1564 // Remember this config in the array
1565 $cacheArray['config'][$no] = $CFG_DUMMY;
1568 // Return config array
1571 // Gets the matching what name from module
1572 function GET_WHAT($MOD_CHECK) {
1574 //* DEBUG: */ echo __LINE__."!".$MOD_CHECK."!<br />\n";
1591 // Return what value
1595 function MODULE_HAS_MENU($mod, $forceDb = false)
1597 global $cacheArray, $_CONFIG;
1599 // All is false by default
1601 //* DEBUG: */ echo __FUNCTION__.":mod={$mod},cache=".GET_EXT_VERSION("cache")."<br />\n";
1602 if (GET_EXT_VERSION("cache") >= "0.1.2") {
1603 // Cache version is okay, so let's check the cache!
1604 if (isset($cacheArray['modules']['has_menu'][$mod])) {
1605 // Check module cache and count hit
1606 $ret = ($cacheArray['modules']['has_menu'][$mod] == "Y");
1607 $_CONFIG['cache_hits']++;
1608 } elseif (isset($cacheArray['extensions']['ext_menu'][$mod])) {
1609 // Check cache and count hit
1610 $ret = ($cacheArray['extensions']['ext_menu'][$mod] == "Y");
1611 $_CONFIG['cache_hits']++;
1613 } elseif ((GET_EXT_VERSION("sql_patches") >= "0.3.6") && ((!EXT_IS_ACTIVE("cache")) || ($forceDb === true))) {
1614 // Check database for entry
1615 $result = SQL_QUERY_ESC("SELECT has_menu FROM "._MYSQL_PREFIX."_mod_reg WHERE module='%s' LIMIT 1",
1616 array($mod), __FILE__, __LINE__);
1617 if (SQL_NUMROWS($result) == 1) {
1618 list($has_menu) = SQL_FETCHROW($result);
1620 // Fake cache... ;-)
1621 $cacheArray['extensions']['ext_menu'][$mod] = $has_menu;
1623 // Does it have a menu?
1624 $ret = ($has_menu == "Y");
1628 SQL_FREERESULT($result);
1629 } elseif (GET_EXT_VERSION("sql_patches") == "") {
1630 // No sql_patches installed, so maybe in admin area?
1631 $ret = ((IS_ADMIN()) && ($mod == "admin")); // Then there is a menu!
1637 // Subtract points from database and mediadata cache
1638 function SUB_POINTS ($uid, $points) {
1639 // Add points to used points
1640 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET `used_points`=`used_points`+%s WHERE userid=%s LIMIT 1",
1641 array($points, bigintval($uid)), __FILE__, __LINE__);
1643 // Update mediadata as well
1644 if (GET_EXT_VERSION("mediadata") >= "0.0.4") {
1646 MEDIA_UPDATE_ENTRY(array("total_points"), "sub", $points);
1649 // Update config entries
1650 function UPDATE_CONFIG ($entries, $values, $updateMode="") {
1651 // Do we have multiple entries?
1652 if (is_array($entries)) {
1655 foreach ($entries as $idx => $entry) {
1657 if (!empty($updateMode)) {
1659 $all .= sprintf("%s=%s%s%s,", $entry, $entry, $updateMode, (float)$values[$idx]);
1661 // Check if string or number
1662 if (($values[$idx] + 0) === $values[$idx]) {
1664 $all .= sprintf("%s=%s,", $entry, (float)$values[$idx]);
1667 $all .= sprintf("%s='%s',", $entry, SQL_ESCAPE($values[$idx]));
1672 // Remove last comma
1673 $entries = substr($all, 0, -1);
1674 } elseif (!empty($updateMode)) {
1676 $entries .= sprintf("=%s%s%s", $entries, $updateMode, (float)$value);
1678 // Regular entry to update
1679 $entries .= sprintf("='%s'", SQL_ESCAPE($values));
1682 // Run database update
1683 //DEBUG_LOG(__FUNCTION__.":entries={$entries}");
1684 SQL_QUERY("UPDATE "._MYSQL_PREFIX."_config SET ".$entries." WHERE config=0 LIMIT 1", __FILE__, __LINE__);
1686 // Get affected rows
1687 $affectedRows = SQL_AFFECTEDROWS();
1688 //* DEBUG: */ echo __FUNCTION__.":entries={$entries},affectedRows={$affectedRows}<br />\n";
1691 if ((GET_EXT_VERSION("cache") >= "0.1.2") && ($affectedRows == 1)) {
1692 global $cacheInstance, $_CONFIG, $CSS;
1693 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
1696 require(PATH."inc/load_cache-config.php");
1699 // Creates a new task for updated extension
1700 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $subject, $notes) {
1701 // Check if task is not there
1702 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE subject='%s' LIMIT 1",
1703 array($subject), __FILE__, __LINE__);
1704 if (SQL_NUMROWS($result) == 0) {
1705 // Task not created so it's a brand-new extension which we need to register and create a task for!
1706 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created) VALUES ('%s', '0', 'NEW', 'EXTENSION_UPDATE', '%s', '%s', UNIX_TIMESTAMP())",
1707 array($admin_id, $subject, $notes), __FILE__, __LINE__);
1711 SQL_FREERESULT($result);
1713 // Creates a new task for newly installed extension
1714 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
1715 // Not installed and do we have created a task for the admin?
1716 $result = SQL_QUERY_ESC("SELECT `id` FROM `"._MYSQL_PREFIX."_task_system` WHERE `subject` LIKE '%s%%' LIMIT 1",
1717 array($subject), __FILE__, __LINE__);
1718 if ((SQL_NUMROWS($result) == 0) && (GET_EXT_VERSION($ext) == "")) {
1720 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
1726 // Load text for task
1727 if (FILE_READABLE($tpl)) {
1728 // Load extension's own text template (HTML!)
1729 $msg = LOAD_TEMPLATE("ext_".$ext, true);
1731 // Load default message
1732 $msg = LOAD_EMAIL_TEMPLATE("admin_new_ext","", 0);
1735 // Task not created so it's a brand-new extension which we need to register and create a task for!
1736 $result_insert = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created)
1737 VALUES (%s, 0, 'NEW', 'EXTENSION', '%s', '%s', UNIX_TIMESTAMP())",
1742 ), __FILE__, __LINE__, true, false
1747 SQL_FREERESULT($result);