+// Determine which user hash no more points left
+function SURFBAR_DETERMINE_DEPLETED_USERIDS ($limit=0) {
+ // Init array
+ $UIDs = array(
+ 'uid' => array(),
+ 'points' => array(),
+ 'notified' => array(),
+ );
+
+ // Do we have a current user id?
+ if ((IS_MEMBER()) && ($limit == 0)) {
+ // Then add this as well
+ $UIDs['uid'][$GLOBALS['userid']] = $GLOBALS['userid'];
+ $UIDs['points'][$GLOBALS['userid']] = GET_TOTAL_DATA($GLOBALS['userid'], "user_points", "points") - GET_TOTAL_DATA($GLOBALS['userid'], "user_data", "used_points");
+ $UIDs['notified'][$GLOBALS['userid']] = 0;
+
+ // Get all userid except logged in one
+ $result = SQL_QUERY_ESC("SELECT u.userid, UNIX_TIMESTAMP(d.surfbar_low_notified) AS notified
+FROM "._MYSQL_PREFIX."_surfbar_urls AS u
+INNER JOIN "._MYSQL_PREFIX."_user_data AS d
+ON u.userid=d.userid
+WHERE u.userid NOT IN (%s,0) AND u.status='ACTIVE'
+GROUP BY u.userid
+ORDER BY u.userid ASC",
+ array($GLOBALS['userid']), __FILE__, __LINE__);
+ } else {
+ // Get all userid
+ $result = SQL_QUERY("SELECT u.userid, UNIX_TIMESTAMP(d.surfbar_low_notified) AS notified
+FROM "._MYSQL_PREFIX."_surfbar_urls AS u
+INNER JOIN "._MYSQL_PREFIX."_user_data AS d
+ON u.userid=d.userid
+WHERE u.status='ACTIVE'
+GROUP BY u.userid
+ORDER BY u.userid ASC", __FILE__, __LINE__);
+ }
+
+ // Load all userid
+ while (list($uid, $notified) = 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__, __LINE__, "uid={$uid},points={$points}", false);
+
+ // Shall we add this to ignore?
+ if ($points <= $limit) {
+ // Ignore this one!
+ //DEBUG_LOG(__FUNCTION__, __LINE__, "uid={$uid} has depleted points amount!", false);
+ $UIDs['uid'][$uid] = $uid;
+ $UIDs['points'][$uid] = $points;
+ $UIDs['notified'][$uid] = $notified;
+ } // END - if
+ } // END - while
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Debug message
+ //DEBUG_LOG(__FUNCTION__, __LINE__, "UIDs::count=".count($UIDs)." (with own userid=".$GLOBALS['userid'].")", false);
+
+ // Return result
+ return $UIDs;
+}
+// Determine how many users are Online in surfbar
+function SURFBAR_DETERMINE_TOTAL_ONLINE () {
+ global $_CONFIG;
+
+ // Count all users in surfbar modue and return the value
+ $result = SQL_QUERY_ESC("SELECT id
+FROM "._MYSQL_PREFIX."_surfbar_stats
+WHERE (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(last_online)) <= %s
+GROUP BY userid",
+ array($_CONFIG['online_timeout']), __FILE__, __LINE__);
+
+ // Fetch count
+ $cnt = SQL_NUMROWS($result);
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return result
+ return $cnt;
+}
+// Determine waiting time for one URL
+function SURFBAR_DETERMINE_WAIT_TIME () {
+ global $_CONFIG;
+
+ // Static time is default
+ $time = $_CONFIG['surfbar_static_time'];
+
+ // Which payment model do we have?
+ if ($_CONFIG['surfbar_pay_model'] == "DYNAMIC") {
+ // "Calculate" dynamic time
+ $time += SURFBAR_CALCULATE_DYNAMIC_ADD();
+ } // END - if
+
+ // Return value
+ return $time;
+}
+// Changes the status of an URL from given to other
+function SURFBAR_CHANGE_STATUS ($urlId, $prevStatus, $newStatus, $data=array()) {
+ global $_CONFIG;
+
+ // Get URL data for status comparison if missing
+ if ((!is_array($data)) || (count($data) == 0)) {
+ // Fetch missing URL data
+ $data = SURFBAR_GET_URL_DATA($urlId);
+ } // END - if
+
+ // Is the status like prevStatus is saying?
+ if ($data[$urlId]['status'] != $prevStatus) {
+ // No, then abort here
+ return false;
+ } // END - if
+
+
+ // Update the status now
+ // ---------- Comment out for debugging/developing member actions! ---------
+ //SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_surfbar_urls SET status='%s' WHERE id=%s LIMIT 1",
+ // array($newStatus, bigintval($urlId)), __FILE__, __LINE__);
+ // ---------- Comment out for debugging/developing member actions! ---------
+
+ // Was that fine?
+ if (SQL_AFFECTEDROWS() != 1) {
+ // No, something went wrong
+ return false;
+ } // END - if
+
+ // Prepare content for notification routines
+ $data[$urlId]['uid'] = $data[$urlId]['userid'];
+ $data[$urlId]['frametester'] = FRAMETESTER($data[$urlId]['url']);
+ $data[$urlId]['reward'] = TRANSLATE_COMMA($_CONFIG['surfbar_static_reward']);
+ $data[$urlId]['costs'] = TRANSLATE_COMMA($_CONFIG['surfbar_static_costs']);
+ $data[$urlId]['status'] = SURFBAR_TRANSLATE_STATUS($newStatus);
+ $data[$urlId]['registered'] = MAKE_DATETIME($data[$urlId]['registered'], "2");
+ $newStatus = strtolower($newStatus);
+
+ // Send admin notification
+ SURFBAR_NOTIFY_ADMIN("url_{$newStatus}", $data[$urlId]);
+
+ // Send user notification
+ SURFBAR_NOTIFY_USER("url_{$newStatus}", $data[$urlId]);
+
+ // All done!
+ return true;
+}
+// Calculate minimum value for dynamic payment model
+function SURFBAR_CALCULATE_DYNAMIC_MIN_VALUE () {
+ global $_CONFIG;
+
+ // Addon is zero by default
+ $addon = 0;
+
+ // Percentage part
+ $percent = abs(log($_CONFIG['surfbar_dynamic_percent'] / 100 + 1));
+
+ // Get total users
+ $totalUsers = GET_TOTAL_DATA("CONFIRMED", "user_data", "userid", "status", true);
+
+ // Get online users
+ $onlineUsers = SURFBAR_DETERMINE_TOTAL_ONLINE();
+
+ // Calculate addon
+ $addon += abs(log($onlineUsers / $totalUsers + 1) * $percent * $totalUsers);
+
+ // Get total URLs
+ $totalUrls = SURFBAR_GET_TOTAL_URLS("ACTIVE", "0");
+
+ // Get user's total URLs
+ $userUrls = SURFBAR_GET_TOTAL_USER_URLS(0, "ACTIVE");
+
+ // Calculate addon
+ if ($totalUrls > 0) {
+ $addon += abs(log($userUrls / $totalUrls + 1) * $percent * $totalUrls);
+ } else {
+ $addon += abs(log($userUrls / 1 + 1) * $percent * $totalUrls);
+ }
+
+ // Return addon
+ return $addon;
+}
+// Calculate maximum value for dynamic payment model
+function SURFBAR_CALCULATE_DYNAMIC_MAX_VALUE () {
+ global $_CONFIG;
+
+ // Addon is zero by default
+ $addon = 0;
+
+ // Maximum value
+ $max = log(2);
+
+ // Percentage part
+ $percent = abs(log($_CONFIG['surfbar_dynamic_percent'] / 100 + 1));
+
+ // Get total users
+ $totalUsers = GET_TOTAL_DATA("CONFIRMED", "user_data", "userid", "status", true);
+
+ // Calculate addon
+ $addon += abs($max * $percent * $totalUsers);
+
+ // Get total URLs
+ $totalUrls = SURFBAR_GET_TOTAL_URLS("ACTIVE", "0");
+
+ // Calculate addon
+ $addon += abs($max * $percent * $totalUrls);
+
+ // Return addon
+ return $addon;
+}
+// Calculate dynamic lock
+function SURFBAR_CALCULATE_DYNAMIC_LOCK () {
+ global $_CONFIG;
+
+ // Default lock is 30 seconds
+ $addon = 30;
+
+ // Get online users
+ $onlineUsers = SURFBAR_DETERMINE_TOTAL_ONLINE();
+
+ // Calculate lock
+ $addon = abs(log($onlineUsers / $addon + 1));
+
+ // Return value
+ return $addon;
+}
+// "Getter" for lock ids array
+function SURFBAR_GET_LOCK_IDS () {
+ // 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) AS last
+FROM
+ "._MYSQL_PREFIX."_surfbar_locks
+WHERE
+ userid=%s
+ORDER BY
+ id ASC", array($GLOBALS['userid']),
+ __FILE__, __LINE__);
+
+ // Load all entries
+ while (list($lid, $url, $last) = SQL_FETCHROW($result)) {
+ // Debug message
+ //DEBUG_LOG(__FUNCTION__, __LINE__, "next - lid={$lid},url={$url},rest=".(time() - $last)."/".SURFBAR_GET_DATA('surf_lock')."", false);
+
+ // Skip entries that are too old
+ if (($last > (time() - SURFBAR_GET_DATA('surf_lock'))) && (!in_array($url, $ignored))) {
+ // Debug message
+ //DEBUG_LOG(__FUNCTION__, __LINE__, "okay - lid={$lid},url={$url},last={$last}", false);
+
+ // Add only if missing or bigger
+ if ((!isset($IDs[$url])) || ($IDs[$url] > $last)) {
+ // Debug message
+ //DEBUG_LOG(__FUNCTION__, __LINE__, "ADD - lid={$lid},url={$url},last={$last}", false);
+
+ // Add this ID
+ $IDs[$url] = $last;
+ $USE[$url] = $lid;
+ } // END - if
+ } else {
+ // Debug message
+ //DEBUG_LOG(__FUNCTION__, __LINE__, "ignore - lid={$lid},url={$url},last={$last}", false);
+
+ // Ignore these old entries!
+ $ignored[] = $url;
+ unset($IDs[$url]);
+ unset($USE[$url]);
+ }
+ } // END - while
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return array
+ return $USE;
+}
+// "Getter" for maximum random number
+function SURFBAR_GET_MAX_RANDOM ($UIDs, $ADD) {
+ global $_CONFIG;
+ // Count max availabe entries
+ $result = SQL_QUERY("SELECT sbu.id AS cnt
+FROM "._MYSQL_PREFIX."_surfbar_urls AS sbu
+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.views_allowed=0 OR (sbu.views_allowed > 0 AND sbu.views_max > 0)) AND sbu.status='ACTIVE'".$ADD."
+GROUP BY sbu.id", __FILE__, __LINE__);
+
+ // Log last query
+ //DEBUG_LOG(__FUNCTION__, __LINE__, "lastQuery=".$_CONFIG['db_last_query']."|numRows=".SQL_NUMROWS($result)."|Affected=".SQL_AFFECTEDROWS()."", false);
+
+ // Fetch max rand
+ $maxRand = SQL_NUMROWS($result);
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return value
+ return $maxRand;
+}
+// Load all URLs of the current user and return it as an array
+function SURFBAR_GET_USER_URLS () {
+ // Init array
+ $URLs = array();
+
+ // Begin the query
+ $result = SQL_QUERY_ESC("SELECT u.id, u.userid, u.url, u.views_total, u.status, UNIX_TIMESTAMP(u.registered) AS registered, UNIX_TIMESTAMP(u.last_locked) AS last_locked, u.lock_reason AS lock_reason
+FROM "._MYSQL_PREFIX."_surfbar_urls AS u
+WHERE u.userid=%s AND u.status != 'DELETED'
+ORDER BY u.id ASC",
+ array($GLOBALS['userid']), __FILE__, __LINE__);
+
+ // Are there entries?
+ if (SQL_NUMROWS($result) > 0) {
+ // Load all rows
+ while ($row = SQL_FETCHARRAY($result)) {
+ // Add the row
+ $URLs[$row['id']] = $row;
+ } // END - while
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return the array
+ return $URLs;
+}
+// "Getter" for member action array for given status
+function SURFBAR_GET_ACTION_ARRAY ($status) {
+ // Init array
+ $returnArray = array();
+
+ // Get all assigned actions
+ $result = SQL_QUERY_ESC("SELECT action FROM "._MYSQL_PREFIX."_surfbar_actions WHERE status='%s' ORDER BY id ASC",
+ array($status), __FILE__, __LINE__);
+
+ // Some entries there?
+ if (SQL_NUMROWS($result) > 0) {
+ // Load all actions
+ while (list($action) = SQL_FETCHROW($result)) {
+ $returnArray[] = $action;
+ } // END - if
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return result
+ return $returnArray;
+}
+// Reload to configured stop page
+function SURFBAR_RELOAD_TO_STOP_PAGE($page="stop") {
+ global $_CONFIG;
+
+ // Internal or external?
+ if (($_CONFIG['surfbar_pause_mode'] == "INTERNAL") || (empty($_CONFIG['surfbar_pause_url']))) {
+ // Reload to internal page
+ LOAD_URL("surfbar.php?frame=".$page);
+ } else {
+ // Reload to external page
+ LOAD_URL($_CONFIG['surfbar_pause_url']);
+ }
+}
+
+// Determine next id for surfbar or get data for given id, always call this before you call other