Problem with one URL not in reload lock fixed
[mailer.git] / inc / libs / surfbar_functions.php
index 8d1df27..1c67607 100644 (file)
@@ -416,8 +416,45 @@ function SURFBAR_GET_NEXT_ID ($id = 0) {
                        $randNum = mt_rand(0, $maxRand);
                } // END - if
 
+               // Get all id from locks within the timestamp
+               $result = SQL_QUERY_ESC("SELECT id, url_id, UNIX_TIMESTAMP(last_surfed)
+FROM
+       "._MYSQL_PREFIX."_surfbar_locks
+WHERE
+       userid=%s
+ORDER BY
+       id ASC", array($GLOBALS['userid']),
+                       __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";
+                       // 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";
+                               // 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";
+                                       $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";
+                               $ignored[] = $url;
+                               unset($IDs[$url]);
+                               unset($USE[$url]);
+                       }
+               } // END - if
+
+               // Free result
+               SQL_FREERESULT($result);
+
                // And query the database
-               //* DEBUG: */ echo __FUNCTION__.":randNum={$randNum},maxRand={$maxRand}<br />\n";
+               //* DEBUG: */ echo __FUNCTION__.":randNum={$randNum},maxRand={$maxRand},surfLock=".SURFBAR_GET_DATA('surf_lock')."<br />\n";
                $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
@@ -426,11 +463,18 @@ 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 != %s AND sbu.status='CONFIRMED' AND (l.last_surfed IS NULL OR (UNIX_TIMESTAMP() - ".SURFBAR_GET_DATA('surf_lock').") >= UNIX_TIMESTAMP(l.last_surfed))
+WHERE sbu.userid != %s AND sbu.status='CONFIRMED' AND l.id IN (".implode(",", $USE).")
+GROUP BY sbu.id
 ORDER BY l.last_surfed ASC, sbu.id ASC
 LIMIT %s,1",
                        array($GLOBALS['userid'], $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
@@ -450,9 +494,9 @@ LIMIT 1",
        // Is there an id number?
        if (SQL_NUMROWS($result) == 1) {
                // Load/cache data
-               //* DEBUG: */ echo __FUNCTION__.":".count($SURFBAR_CACHE)."*<br />\n";
+               //* DEBUG: */ echo __FUNCTION__.":count(".count($SURFBAR_CACHE).") - BEFORE<br />\n";
                $SURFBAR_CACHE = merge_array($SURFBAR_CACHE, SQL_FETCHARRAY($result));
-               //* DEBUG: */ echo __FUNCTION__.":".count($SURFBAR_CACHE)."*<br />\n";
+               //* DEBUG: */ echo __FUNCTION__.":count(".count($SURFBAR_CACHE).") - AFTER<br />\n";
 
                // Is the time there?
                if (is_null($SURFBAR_CACHE['time'])) {