]> git.mxchange.org Git - mailer.git/commitdiff
More fixes for surfbar extension
authorRoland Häder <roland@mxchange.org>
Mon, 8 Sep 2008 10:47:15 +0000 (10:47 +0000)
committerRoland Häder <roland@mxchange.org>
Mon, 8 Sep 2008 10:47:15 +0000 (10:47 +0000)
inc/db/lib-mysql3.php
inc/functions.php
inc/libs/surfbar_functions.php

index 9bf9d9eaf98b8b0036243c2fd122bb6deeaf37b1..44bcbdcb920d1e665da12d7f74849fe665ebe8c4 100644 (file)
@@ -53,7 +53,10 @@ function SQL_QUERY($sql_string, $F, $L) {
 ".MYSQL_QUERY_STRING."<br />
 ".$sql_string);
 
-       // Starting time
+       // Save last successfull query
+       $_CONFIG['db_last_query'] = $sql_string;
+
+       // Ending time
        $querytimeAfter = array_sum(explode(' ', microtime()));
 
        // Calculate query time
@@ -70,24 +73,26 @@ function SQL_QUERY($sql_string, $F, $L) {
                //
                // Debugging stuff...
                //
-               $fp = @fopen(PATH."inc/cache/debug.log", 'a') or mxchange_die("Cannot write debug.log!");
+               $fp = @fopen(PATH."inc/cache/mysql.log", 'a') or mxchange_die("Cannot write mysql.log!");
                if (!isset($OK)) {
                        // Write first entry
                        fwrite($fp, "Module=".$GLOBALS['module']."\n");
                        $OK = true;
-               }
+               } // END - if
                fwrite($fp, $F."(LINE=".$L."|NUM=".SQL_NUMROWS($result)."|AFFECTED=".SQL_AFFECTEDROWS()."|QUERYTIME:".$queryTime."): ".str_replace('\r', "", str_replace('\n', " ", $sql_string))."\n");
                fclose($fp);
-       }
+       } // END - if
 
        // Count DB hits
        if (!isset($_CONFIG['db_hits'])) {
                // Count in dummy variable
-               $_CONFIG['db_hits'] = 0;
+               $_CONFIG['db_hits'] = 1;
        } else {
                // Count to config array
                $_CONFIG['db_hits']++;
        }
+
+       // Return the result
        return $result;
 }
 
index a75df208132ec11520e27bf1e709a328657d7f65..7db231cb3ac38a47e97865b47e499fbc986ff457 100644 (file)
@@ -2261,6 +2261,16 @@ function merge_array ($array1, $array2) {
        debug_print_backtrace();
        die("</pre>");
 }
+// Debug message logger
+function DEBUG_LOG ($message) {
+       // Is debug mode enabled?
+       if (isBooleanConstantAndTrue('DEBUG_MODE')) {
+               // Log this message away
+               $fp = fopen(PATH."inc/cache/debug.log", 'a') or mxchange_die("Cannot write logfile debug.log!");
+               fwrite($fp, date("d.m.Y|H:i:s", time())."|{$message}\n");
+               fclose($fp);
+       } // END - if
+}
 //
 //////////////////////////////////////////////////
 //                                              //
index efb49a9a0a72fe2b8dd139d0fc863493b26ee396..72b8c8bb17a204caef20b7605b70ad7a5adf4438 100644 (file)
@@ -234,6 +234,7 @@ function SURFBAR_CHECK_RELOAD_FULL() {
        if ($_CONFIG['surfbar_pay_model'] == "STATIC") {
                // Cache static reload lock
                $SURFBAR_CACHE['surf_lock'] = $_CONFIG['surfbar_static_lock'];
+               //DEBUG_LOG(__FUNCTION__.":Fixed surf lock is ".$_CONFIG['surfbar_static_lock']."");
 
                // Ask the database
                $result = SQL_QUERY_ESC("SELECT COUNT(id) AS cnt FROM "._MYSQL_PREFIX."_surfbar_locks
@@ -258,7 +259,7 @@ LIMIT 1",
                $total = SURFBAR_GET_TOTAL_URLS();
 
                // Do we have some URLs in lock? Admins can always surf on own URLs!
-               //* DEBUG: */ echo __FUNCTION__.":userLocks=".SURFBAR_GET_DATA('user_locks').",total={$total}<br />\n";
+               //DEBUG_LOG(__FUNCTION__.":userLocks=".SURFBAR_GET_DATA('user_locks').",total={$total}");
                $isFull = ((SURFBAR_GET_DATA('user_locks') == $total) && ($total > 0));
        } else {
                // Dynamic model...
@@ -270,11 +271,14 @@ LIMIT 1",
 }
 // Get total amount of URLs of given status for current user or of CONFIRMED URLs by default
 function SURFBAR_GET_TOTAL_URLS ($status="CONFIRMED") {
+       // Determine depleted user account
+       $UIDs = SURFBAR_DETERMINE_DEPLETED_USERIDS();
+
        // Get amount from database
        $result = SQL_QUERY_ESC("SELECT COUNT(id) AS cnt
 FROM "._MYSQL_PREFIX."_surfbar_urls
-WHERE userid != %d AND status='%s'",
-               array($GLOBALS['userid'], $status), __FILE__, __LINE__
+WHERE userid NOT IN (".implode(",", $UIDs).") AND status='%s'",
+               array($status), __FILE__, __LINE__
        );
 
        // Fetch row
@@ -297,22 +301,25 @@ function SURFBAR_GENERATE_VALIDATION_CODE ($id, $salt="") {
                if (empty($salt)) {
                        // Generate random hashed string
                        $SURFBAR_CACHE['salt'] = sha1(GEN_PASS(255));
+                       //DEBUG_LOG(__FUNCTION__.":newSalt=".SURFBAR_GET_SALT()."");
                } else {
                        // Use this as salt!
                        $SURFBAR_CACHE['salt'] = $salt;
+                       //DEBUG_LOG(__FUNCTION__.":oldSalt=".SURFBAR_GET_SALT()."");
                }
-               //* DEBUG: */ echo __FUNCTION__.":".SURFBAR_GET_SALT()."*<br />\n";
 
                // ... and now the validation code
                $valCode = GEN_RANDOM_CODE($_CONFIG['code_length'], sha1(SURFBAR_GET_SALT().":".$id), $GLOBALS['userid']);
-               //* DEBUG: */ echo __FUNCTION__.":valCode={$valCode}<br />\n";
+               //DEBUG_LOG(__FUNCTION__.":valCode={$valCode}");
        } // END - while
 
        // Hash it with md5() and salt it with the random string
        $hashedCode = generateHash(md5($valCode), SURFBAR_GET_SALT());
 
        // Finally encrypt it PGP-like and return it
-       return generatePassString($hashedCode);
+       $valHashedCode = generatePassString($hashedCode);
+       //DEBUG_LOG(__FUNCTION__.":finalValCode={$valHashedCode}");
+       return $valHashedCode;
 }
 // Check validation code
 function SURFBAR_CHECK_VALIDATION_CODE ($id, $check, $salt) {
@@ -325,14 +332,14 @@ function SURFBAR_CHECK_VALIDATION_CODE ($id, $check, $salt) {
        $code = SURFBAR_GENERATE_VALIDATION_CODE($id, $salt);
 
        // Return result of checking hashes and salts
-       //* DEBUG: */ echo __FUNCTION__.":---".$code."---<br />\n---".$check."---<br />\n";
-       //* DEBUG: */ echo __FUNCTION__.":+++".$salt."+++<br />\n+++".SURFBAR_GET_DATA('last_salt')."+++<br />\n";
+       //DEBUG_LOG(__FUNCTION__.":---".$code."|".$check."---");
+       //DEBUG_LOG(__FUNCTION__.":+++".$salt."|".SURFBAR_GET_DATA('last_salt')."+++");
        return (($code == $check) && ($salt == SURFBAR_GET_DATA('last_salt')));
 }
 // Lockdown the userid/id combination (reload lock)
 function SURFBAR_LOCKDOWN_ID ($id) {
-       //* DEBUG: */ print "LOCK!<br />\n";
-       //* DEBUG: */ return;
+       //* DEBUG: */ print "LOCK!");
+       ///* DEBUG: */ return;
        // Just add it to the database
        SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_surfbar_locks (userid, url_id) VALUES(%s, %s)",
                array($GLOBALS['userid'], bigintval($id)), __FILE__, __LINE__);
@@ -349,11 +356,13 @@ function SURFBAR_PAY_POINTS ($id) {
        $_CONFIG['db_percents'] = "percent";
        $_CONFIG['db_table']    = "surfbar_reflevels";
 
+       // Remove it from the URL owner
+       //DEBUG_LOG(__FUNCTION__.":uid=".SURFBAR_GET_USERID().",costs=".SURFBAR_GET_COSTS()."");
+       SUB_POINTS(SURFBAR_GET_USERID(), SURFBAR_GET_COSTS());
+
        // Book it to the user
+       //DEBUG_LOG(__FUNCTION__.":uid=".$GLOBALS['userid'].",reward=".SURFBAR_GET_REWARD()."");
        ADD_POINTS_REFSYSTEM($GLOBALS['userid'], SURFBAR_GET_DATA('reward'));
-
-       // Remove it from the URL owner
-       SUB_POINTS($SURFBAR_CACHE['userid'], SURFBAR_GET_DATA('costs'));
 }
 // Update the salt for validation
 function SURFBAR_UPDATE_SALT() {
@@ -365,6 +374,9 @@ function SURFBAR_UPDATE_SALT() {
        SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_surfbar_salts SET last_salt='%s' WHERE url_id=%s AND userid=%s LIMIT 1",
                array(SURFBAR_GET_SALT(), SURFBAR_GET_ID(), $GLOBALS['userid']), __FILE__, __LINE__);
 
+       // Debug message
+       //DEBUG_LOG(__FUNCTION__.":salt=".SURFBAR_GET_SALT().",id=".SURFBAR_GET_ID().",uid=".$GLOBALS['userid']."");
+
        // Was that okay?
        if (SQL_AFFECTEDROWS() == 0) {
                // Insert missing entry!
@@ -372,12 +384,15 @@ function SURFBAR_UPDATE_SALT() {
                        array(SURFBAR_GET_ID(), $GLOBALS['userid'], SURFBAR_GET_SALT()), __FILE__, __LINE__);
        } // END - if
 
+       // Debug message
+       //DEBUG_LOG(__FUNCTION__.":affectedRows=".SQL_AFFECTEDROWS()."");
+
        // Return if the update was okay
        return (SQL_AFFECTEDROWS() == 1);
 }
 // Check if the reload lock is active for given id
 function SURFBAR_CHECK_RELOAD_LOCK ($id) {
-       //* DEBUG: */ echo __FUNCTION__.":id={$id}<br />\n";
+       //DEBUG_LOG(__FUNCTION__.":id={$id}");
        // Ask the database
        $result = SQL_QUERY_ESC("SELECT COUNT(id) AS cnt
 FROM "._MYSQL_PREFIX."_surfbar_locks
@@ -394,9 +409,56 @@ LIMIT 1",
        SQL_FREERESULT($result);
 
        // Return check
-       //* DEBUG: */ echo __FUNCTION__.":cnt={$cnt},".SURFBAR_GET_DATA('surf_lock')."<br />\n";
+       //DEBUG_LOG(__FUNCTION__.":cnt={$cnt},".SURFBAR_GET_DATA('surf_lock')."");
        return ($cnt == 1);
 }
+// Determine which user hash no more points left
+function SURFBAR_DETERMINE_DEPLETED_USERIDS() {
+       // Init array
+       $UIDs = array();
+
+       // Do we have a current user id?
+       if (IS_LOGGED_IN()) {
+               // Then add this as well
+               $UIDs[] = $GLOBALS['userid'];
+
+               // Get all userid except logged in one
+               $result = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_surfbar_urls
+WHERE userid != %s AND status='CONFIRMED'
+GROUP BY userid
+ORDER BY userid ASC",
+                       array($GLOBALS['userid']), __FILE__, __LINE__);
+       } else {
+               // Get all userid
+               $result = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_surfbar_urls
+WHERE status='CONFIRMED'
+GROUP BY userid
+ORDER BY userid ASC", __FILE__, __LINE__);
+       }
+
+       // Load all userid
+       while (list($uid) = SQL_FETCHROW($result)) {
+               // Get total points
+               $points = GET_TOTAL_DATA($uid, "user_points", "points") - GET_TOTAL_DATA($uid, "user_data", "used_points");
+               //DEBUG_LOG(__FUNCTION__.":uid={$uid},points={$points}");
+
+               // Shall we add this to ignore?
+               if ($points <= 0) {
+                       // Ignore this one!
+                       $UIDs[] = $uid;
+                       //DEBUG_LOG(__FUNCTION__.":uid={$uid} has depleted points amount!");
+               } // END - if
+       } // END - while
+
+       // Free result
+       SQL_FREERESULT($result);
+
+       // Debug message
+       //DEBUG_LOG(__FUNCTION__.":UIDs::count=".count($UIDs)." (with own userid=".$GLOBALS['userid'].")");
+
+       // Return result
+       return $UIDs;
+}
 // Determine next id for surfbar view, always call this before you call other
 // getters below this function!!!
 function SURFBAR_GET_NEXT_ID ($id = 0) {
@@ -407,14 +469,10 @@ function SURFBAR_GET_NEXT_ID ($id = 0) {
 
        // Is the ID set?
        if ($id == 0) {
-               // Set max random factor to total URLs minus 1
-               $maxRand = SURFBAR_GET_TOTAL_URLS() - 1;
-
-               // If more than one URL can be called generate the random number!
-               if ($maxRand > 1) {
-                       // Generate random number
-                       $randNum = mt_rand(0, $maxRand);
-               } // END - if
+               // Prepare some arrays
+               $IDs = array();
+               $USE = array();
+               $ignored = array();
 
                // Get all id from locks within the timestamp
                $result = SQL_QUERY_ESC("SELECT id, url_id, UNIX_TIMESTAMP(last_surfed)
@@ -427,28 +485,26 @@ ORDER BY
                        __FILE__, __LINE__);
 
                // Load all entries
-               $IDs = array(); $USE = array();
-               $ignored = array();
                while (list($id, $url, $last) = SQL_FETCHROW($result)) {
-                       //* DEBUG: */ echo __FUNCTION__.": next - id={$id},url={$url},last={$last}<br />\n";
+                       //DEBUG_LOG(__FUNCTION__.":next - id={$id},url={$url},last={$last}");
                        // Skip entries that are too old
                        if (($last < (time() - SURFBAR_GET_DATA('surf_lock'))) && (!in_array($url, $ignored))) {
-                               //* DEBUG: */ echo __FUNCTION__.": okay - id={$id},url={$url},last={$last}<br />\n";
+                               //DEBUG_LOG(__FUNCTION__.":okay - id={$id},url={$url},last={$last}");
                                // Add only if missing or bigger
                                if ((!isset($IDs[$url])) || ($IDs[$url] <= $last)) {
                                        // Add this ID
-                                       //* DEBUG: */ echo __FUNCTION__.": ADD - id={$id},url={$url},last={$last}<br />\n";
+                                       //DEBUG_LOG(__FUNCTION__.":ADD - id={$id},url={$url},last={$last}");
                                        $IDs[$url] = $last;
                                        $USE[$url] = $id;
                                } // END - if
                        } else {
                                // Ignore these old entries!
-                               //* DEBUG: */ echo __FUNCTION__.": ignore - id={$id},url={$url},last={$last}<br />\n";
+                               //DEBUG_LOG(__FUNCTION__.":ignore - id={$id},url={$url},last={$last}");
                                $ignored[] = $url;
                                unset($IDs[$url]);
                                unset($USE[$url]);
                        }
-               } // END - if
+               } // END - while
 
                // Free result
                SQL_FREERESULT($result);
@@ -459,32 +515,38 @@ ORDER BY
                        $ADD = " AND l.id IN (".implode(",", $USE).")";
                } // END - if
 
-               // Get all userid
-               $result = SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_surfbar_urls
-WHERE userid != %s AND status='CONFIRMED'
-GROUP BY userid
-ORDER BY userid ASC",
-                       array($GLOBALS['userid']), __FILE__, __LINE__);
+               // Determine depleted user account
+               $UIDs = SURFBAR_DETERMINE_DEPLETED_USERIDS();
 
-               // Load all userid
-               $UIDs = array($GLOBALS['userid']);
-               while (list($uid) = SQL_FETCHROW($result)) {
-                       // Get total points
-                       $points = GET_TOTAL_DATA($uid, "user_points", "points") - GET_TOTAL_DATA($uid, "user_data", "used_points");
-                       //* DEBUG: */ echo __FUNCTION__.":uid={$uid},points={$points}<br />\n";
-
-                       // Shall we add this to ignore?
-                       if ($points <= 0) {
-                               // Ignore this one!
-                               $UIDs[] = $uid;
-                       } // END - if
-               } // END - while
+               // Count max availabe entries
+               $result = SQL_QUERY("SELECT sbu.id AS cnt
+FROM "._MYSQL_PREFIX."_surfbar_urls AS sbu
+LEFT JOIN "._MYSQL_PREFIX."_payments AS p
+ON sbu.payment_id=p.id
+LEFT JOIN "._MYSQL_PREFIX."_surfbar_salts AS sbs
+ON sbu.id=sbs.url_id
+LEFT JOIN "._MYSQL_PREFIX."_surfbar_locks AS l
+ON sbu.id=l.url_id
+WHERE sbu.userid NOT IN (".implode(",", $UIDs).") AND sbu.status='CONFIRMED'".$ADD."
+GROUP BY sbu.id", __FILE__, __LINE__);
+
+               // Log last query
+               //DEBUG_LOG(__FUNCTION__.":lastQuery=".$_CONFIG['db_last_query']."|numRows=".SQL_NUMROWS($result)."|Affected=".SQL_AFFECTEDROWS($result)."");
+
+               // Fetch max rand
+               $maxRand = SQL_NUMROWS($result);
 
                // Free result
                SQL_FREERESULT($result);
 
+               // If more than one URL can be called generate the random number!
+               if ($maxRand > 1) {
+                       // Generate random number
+                       $randNum = mt_rand(0, $maxRand);
+               } // END - if
+
                // And query the database
-               //* DEBUG: */ echo __FUNCTION__.":randNum={$randNum},maxRand={$maxRand},surfLock=".SURFBAR_GET_DATA('surf_lock')."<br />\n";
+               //DEBUG_LOG(__FUNCTION__.":randNum={$randNum},maxRand={$maxRand},surfLock=".SURFBAR_GET_DATA('surf_lock')."");
                $result = SQL_QUERY_ESC("SELECT sbu.id, sbu.userid, sbu.url, sbs.last_salt, sbu.reward, sbu.costs, sbu.views_total, p.time, UNIX_TIMESTAMP(l.last_surfed) AS last_surfed
 FROM "._MYSQL_PREFIX."_surfbar_urls AS sbu
 LEFT JOIN "._MYSQL_PREFIX."_payments AS p
@@ -499,12 +561,6 @@ ORDER BY l.last_surfed ASC, sbu.id ASC
 LIMIT %s,1",
                        array($randNum), __FILE__, __LINE__
                );
-               /*
-               while($content = SQL_FETCHARRAY($result)) {
-                       echo "<pre>".print_r($content, true)."</pre>";
-               }
-               die();
-               */
        } else {
                // Get data from specified id number
                $result = SQL_QUERY_ESC("SELECT sbu.id, sbu.userid, sbu.url, sbs.last_salt, sbu.reward, sbu.costs, sbu.views_total, p.time, UNIX_TIMESTAMP(l.last_surfed) AS last_surfed
@@ -522,30 +578,31 @@ LIMIT 1",
        }
 
        // Is there an id number?
+       //DEBUG_LOG(__FUNCTION__.":lastQuery=".$_CONFIG['db_last_query']."|numRows=".SQL_NUMROWS($result)."|Affected=".SQL_AFFECTEDROWS($result)."");
        if (SQL_NUMROWS($result) == 1) {
                // Load/cache data
-               //* DEBUG: */ echo __FUNCTION__.":count(".count($SURFBAR_CACHE).") - BEFORE<br />\n";
+               //DEBUG_LOG(__FUNCTION__.":count(".count($SURFBAR_CACHE).") - BEFORE");
                $SURFBAR_CACHE = merge_array($SURFBAR_CACHE, SQL_FETCHARRAY($result));
-               //* DEBUG: */ echo __FUNCTION__.":count(".count($SURFBAR_CACHE).") - AFTER<br />\n";
+               //DEBUG_LOG(__FUNCTION__.":count(".count($SURFBAR_CACHE).") - AFTER");
 
                // Is the time there?
                if (is_null($SURFBAR_CACHE['time'])) {
                        // Then repair it wit the static!
-                       //* DEBUG: */ echo __FUNCTION__.": time - STATIC!<br />\n";
+                       //DEBUG_LOG(__FUNCTION__.":time - STATIC!");
                        $SURFBAR_CACHE['time'] = $_CONFIG['surfbar_static_time'];
                } // END - if
 
                // Is the last salt there?
                if (is_null($SURFBAR_CACHE['last_salt'])) {
                        // Then repair it wit the static!
-                       //* DEBUG: */ echo __FUNCTION__.": last_salt - FIXED!<br />\n";
+                       //DEBUG_LOG(__FUNCTION__.":last_salt - FIXED!");
                        $SURFBAR_CACHE['last_salt'] = "";
                } // END - if
 
                // Fix missing last_surfed
                if ((!isset($SURFBAR_CACHE['last_surfed'])) || (is_null($SURFBAR_CACHE['last_surfed']))) {
                        // Fix it here
-                       //* DEBUG: */ echo __FUNCTION__.": last_surfed - FIXED!<br />\n";
+                       //DEBUG_LOG(__FUNCTION__.":last_surfed - FIXED!");
                        $SURFBAR_CACHE['last_surfed'] = "0";
                } // END - if
 
@@ -554,10 +611,12 @@ LIMIT 1",
                        // Then use static reward/costs!
                        $SURFBAR_CACHE['reward'] = $_CONFIG['surfbar_static_reward'];
                        $SURFBAR_CACHE['costs']  = $_CONFIG['surfbar_static_costs'];
+                       //DEBUG_LOG(__FUNCTION__.":FIXED - reward=".SURFBAR_GET_REWARD()."|costs=".SURFBAR_GET_COSTS()."");
                } else {
                        // Calculate dynamic reward/costs and add it
                        $SURFBAR_CACHE['reward'] += SURFBAR_CALCULATE_DYNAMIC_REWARD_ADD();
                        $SURFBAR_CACHE['costs']  += SURFBAR_CALCULATE_DYNAMIC_COSTS_ADD();
+                       //DEBUG_LOG(__FUNCTION__.":DYNAMIC - reward=".SURFBAR_GET_REWARD()."|costs=".SURFBAR_GET_COSTS()."");
                }
 
                // Now get the id
@@ -568,7 +627,7 @@ LIMIT 1",
        SQL_FREERESULT($result);
 
        // Return result
-       //* DEBUG: */ echo __FUNCTION__.":nextId={$nextId}<br />\n";
+       //DEBUG_LOG(__FUNCTION__.":nextId={$nextId}");
        return $nextId;
 }
 // ----------------------------------------------------------------------------
@@ -578,6 +637,7 @@ LIMIT 1",
 // Private getter for data elements
 function SURFBAR_GET_DATA ($element) {
        global $SURFBAR_CACHE;
+       //DEBUG_LOG(__FUNCTION__.":element={$element}");
 
        // Default is null
        $data = null;
@@ -594,6 +654,7 @@ function SURFBAR_GET_DATA ($element) {
        }
 
        // Return result
+       //DEBUG_LOG(__FUNCTION__.":element[$element]={$data}");
        return $data;
 }
 // Getter for reward from cache
@@ -621,6 +682,11 @@ function SURFBAR_GET_ID () {
        // Get data element and return its contents
        return SURFBAR_GET_DATA('id');
 }
+// Getter for userid from cache
+function SURFBAR_GET_USERID () {
+       // Get data element and return its contents
+       return SURFBAR_GET_DATA('userid');
+}
 // Getter for user reload locks
 function SURFBAR_GET_USER_RELOAD_LOCK () {
        // Get data element and return its contents