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
$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...
}
// 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
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) {
$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__);
$_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() {
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!
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
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) {
// 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)
__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);
$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
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
}
// 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
// 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
SQL_FREERESULT($result);
// Return result
- //* DEBUG: */ echo __FUNCTION__.":nextId={$nextId}<br />\n";
+ //DEBUG_LOG(__FUNCTION__.":nextId={$nextId}");
return $nextId;
}
// ----------------------------------------------------------------------------
// Private getter for data elements
function SURFBAR_GET_DATA ($element) {
global $SURFBAR_CACHE;
+ //DEBUG_LOG(__FUNCTION__.":element={$element}");
// Default is null
$data = null;
}
// Return result
+ //DEBUG_LOG(__FUNCTION__.":element[$element]={$data}");
return $data;
}
// Getter for reward from cache
// 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