+// Determine which user hash no more points left
+function SURFBAR_DETERMINE_DEPLETED_USERIDS ($limit=0) {
+ // Init array
+ $UIDs = array(
+ 'userid' => array(),
+ 'points' => array(),
+ 'notified' => array(),
+ );
+
+ // Do we have a current user id?
+ if ((isMember()) && ($limit == 0)) {
+ // Then add this as well
+ $UIDs['userid'][getUserId()] = getUserId();
+ $UIDs['points'][getUserId()] = countSumTotalData(getUserId(), 'user_points', 'points') - countSumTotalData(getUserId(), 'user_data', 'used_points');
+ $UIDs['notified'][getUserId()] = 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(getUserId()), __FUNCTION__, __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", __FUNCTION__, __LINE__);
+ }
+
+ // Load all userid
+ while ($content = SQL_FETCHARRAY($result)) {
+ // Get total points
+ $points = countSumTotalData($content['userid'], 'user_points', 'points') - countSumTotalData($content['userid'], 'user_data', 'used_points');
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "userid={$content['userid']},points={$points}", false);
+
+ // Shall we add this to ignore?
+ if ($points <= $limit) {
+ // Ignore this one!
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "userid={$content['userid']} has depleted points amount!", false);
+ $UIDs['userid'][$content['userid']] = $content['userid'];
+ $UIDs['points'][$content['userid']] = $points;
+ $UIDs['notified'][$content['userid']] = $content['notified'];
+ } // END - if
+ } // END - while
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "UIDs::count=".count($UIDs)." (with own userid=".getUserId().')', false);
+
+ // Return result
+ return $UIDs;
+}
+
+// Determine how many users are Online in surfbar
+function SURFBAR_DETERMINE_TOTAL_ONLINE () {
+ // Count all users in surfbar modue and return the value
+ $result = SQL_QUERY("SELECT
+ `id`
+FROM
+ `{?_MYSQL_PREFIX?}_surfbar_stats`
+WHERE
+ (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`last_surfed`)) <= {?online_timeout?}
+GROUP BY
+ `userid` ASC", __FUNCTION__, __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 () {
+ // Get fixed reload lock
+ $fixed = SURFBAR_GET_FIXED_RELOAD();
+
+ // Is the fixed reload time set?
+ if ($fixed > 0) {
+ // Return it
+ return $fixed;
+ } // END - if
+
+ // Static time is default
+ $time = getConfig('surfbar_static_time');
+
+ // Which payment model do we have?
+ if (getConfig('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()) {
+ // Make new status always lower-case
+ $newStatus = strtolower($newStatus);
+
+ // 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 new status set?
+ if ((!is_string($newStatus)) || (empty($newStatus))) {
+ // Abort here, but fine!
+ return true;
+ } // 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)), __FUNCTION__, __LINE__);
+ // ---------- Comment out for debugging/developing member actions! ---------
+
+ // Was that fine?
+ // ---------- Comment out for debugging/developing member actions! ---------
+ //if (SQL_AFFECTEDROWS() != 1) {
+ // // No, something went wrong
+ // return false;
+ //} // END - if
+ // ---------- Comment out for debugging/developing member actions! ---------
+
+ // Prepare content for notification routines
+ $data[$urlId]['userid'] = $data[$urlId]['userid'];
+ $data[$urlId]['frametester'] = generateFrametesterUrl($data[$urlId]['url']);
+ $data[$urlId]['reward'] = translateComma(getConfig('surfbar_static_reward'));
+ $data[$urlId]['costs'] = translateComma(getConfig('surfbar_static_costs'));
+
+ // Do some dirty fixing here:
+ if (($data[$urlId]['status'] == 'STOPPED') && ($newStatus == 'pending')) {
+ // Fix for template change
+ $newStatus = 'continued';
+ } // END - if
+
+ // Send admin notification
+ SURFBAR_NOTIFY_ADMIN("url_{$data[$urlId]['status']}_{$newStatus}", $data[$urlId]);
+
+ // Send user notification
+ SURFBAR_NOTIFY_USER("url_{$data[$urlId]['status']}_{$newStatus}", $data[$urlId]);
+
+ // All done!
+ return true;
+}
+
+// Calculate minimum value for dynamic payment model
+function SURFBAR_CALCULATE_DYNAMIC_MIN_VALUE () {
+ // Addon is zero by default
+ $addon = 0;
+
+ // Percentage part
+ $percent = abs(log(getConfig('surfbar_dynamic_percent') / 100 + 1));
+
+ // Get total users
+ $totalUsers = countSumTotalData('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 () {
+ // Addon is zero by default
+ $addon = 0;
+
+ // Maximum value
+ $max = log(2);
+
+ // Percentage part
+ $percent = abs(log(getConfig('surfbar_dynamic_percent') / 100 + 1));
+
+ // Get total users
+ $totalUsers = countSumTotalData('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 () {
+ // 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_surfed
+FROM
+ `{?_MYSQL_PREFIX?}_surfbar_locks`
+WHERE
+ `userid`=%s
+ORDER BY
+ `id` ASC", array(getUserId()),
+ __FUNCTION__, __LINE__);
+
+ // Load all entries
+ while ($content = SQL_FETCHARRAY($result)) {
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'next - lid='.$content['id'].',url='.$content['url_id'].',rest='.(time() - $content['last_surfed']).'/'.SURFBAR_GET_SURF_LOCK().'', false);
+
+ // Skip entries that are too old
+ if (($content['last_surfed'] > (time() - SURFBAR_GET_SURF_LOCK())) && (!in_array($content['url_id'], $ignored))) {
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'okay - lid='.$content['id'].',url='.$content['url_id'].',last='.$content['last_surfed'].'', false);
+
+ // Add only if missing or bigger
+ if ((!isset($IDs[$content['url_id']])) || ($IDs[$content['url_id']] > $content['last_surfed'])) {
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ADD - lid='.$content['id'].',url='.$content['url_id'].',last='.$content['last_surfed'].'', false);
+
+ // Add this ID
+ $IDs[$content['url_id']] = $content['last_surfed'];
+ $USE[$content['url_id']] = $content['id'];
+ } // END - if
+ } else {
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ignore - lid='.$content['id'].',url='.$content['url_id'].',last='.$content['last_surfed'].'', false);
+
+ // Ignore these old entries!
+ $ignored[] = $content['url_id'];
+ unset($IDs[$content['url_id']]);
+ unset($USE[$content['url_id']]);
+ }
+ } // END - while
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return array
+ return $USE;
+}
+
+// "Getter" for maximum random number
+function SURFBAR_GET_MAX_RANDOM ($UIDs, $add) {
+ // 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", __FUNCTION__, __LINE__);
+
+ // Log last query
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'lastQuery='.getConfig('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.views_max, u.views_allowed, 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(getUserId()), __FUNCTION__, __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_ARRAY_FROM_STATUS ($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), __FUNCTION__, __LINE__);
+
+ // Some entries there?
+ if (SQL_NUMROWS($result) > 0) {
+ // Load all actions
+ // @TODO This can be somehow rewritten
+ while ($content = SQL_FETCHARRAY($result)) {
+ $returnArray[] = $content['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") {
+ // Internal or external?
+ if ((getConfig('surfbar_pause_mode') == 'INTERNAL') || (getConfig('surfbar_pause_url') == '')) {
+ // Reload to internal page
+ redirectToUrl('surfbar.php?frame=' . $page);
+ } else {
+ // Reload to external page
+ redirectToConfiguredUrl('surfbar_pause_url');
+ }
+}
+
+// Determine next id for surfbar or get data for given id, always call this before you call other
+// getters below this function!!!
+function SURFBAR_DETERMINE_NEXT_ID ($urlId = 0) {
+ // Default is no id and no random number
+ $nextId = 0;
+ $randNum = 0;