+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',count=' . $count . ',SURFBAR_GET_SURF_LOCK()=' . SURFBAR_GET_SURF_LOCK() . ' - EXIT!');
+ return ($count == 1);
+}
+
+// Determine which user hash no more points left
+function SURFBAR_DETERMINE_DEPLETED_USERIDS ($limit=0) {
+ // Init array
+ $userids = array(
+ 'url_userid' => array(),
+ 'points' => array(),
+ 'notified' => array(),
+ );
+
+ // Do we have a current user id?
+ if ((isMember()) && ($limit == '0')) {
+ // Then add this as well
+ $userids['url_userid'][getMemberId()] = getMemberId();
+ $userids['points'][getMemberId()] = getTotalPoints(getMemberId());
+ $userids['notified'][getMemberId()] = '0';
+
+ // Get all userid except logged in one
+ $result = SQL_QUERY_ESC("SELECT
+ u.url_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.`url_userid`=d.`userid`
+WHERE
+ u.`url_userid` NOT IN (%s,0) AND
+ u.`url_status`='ACTIVE'
+GROUP BY
+ u.`url_userid`
+ORDER BY
+ u.`url_userid` ASC",
+ array(getMemberId()), __FUNCTION__, __LINE__);
+ } else {
+ // Get all userid
+ $result = SQL_QUERY("SELECT
+ u.url_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.`url_userid`=d.`userid`
+WHERE
+ u.`url_userid` > 0 AND
+ u.`url_status`='ACTIVE'
+GROUP BY
+ u.`url_userid`
+ORDER BY
+ u.`url_userid` ASC", __FUNCTION__, __LINE__);
+ }
+
+ // Load all userid
+ while ($content = SQL_FETCHARRAY($result)) {
+ // Get total points
+ $points = getTotalPoints($content['url_userid']);
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $content['url_userid'] . ',points=' . $points);
+
+ // Shall we add this to ignore?
+ if ($points <= $limit) {
+ // Ignore this one!
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $content['url_userid'] . ' has depleted points amount!');
+ $userids['url_userid'][$content['url_userid']] = $content['url_userid'];
+ $userids['points'][$content['url_userid']] = $points;
+ $userids['notified'][$content['url_userid']] = $content['notified'];
+ } // END - if
+ } // END - while
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Debug message
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'UIDs::count=' . count($userids) . ' (with own userid=' . getMemberId() . ')');
+
+ // Return result
+ return $userids;
+}
+
+// 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
+ `stats_id`
+FROM
+ `{?_MYSQL_PREFIX?}_surfbar_stats`
+WHERE
+ (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`stats_last_surfed`)) <= {?online_timeout?}
+GROUP BY
+ `stats_userid` ASC', __FUNCTION__, __LINE__);
+
+ // Fetch count
+ $count = SQL_NUMROWS($result);
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return result
+ return $count;
+}
+
+// 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 (getSurfbarPaymentModel() == '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()) {
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',prevStatus=' . $prevStatus . ',newStatus=' . $newStatus . ' - ENTERED!');
+ // 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
+
+ // Prepare array
+ $filterData = array(
+ 'url_id' => $urlId,
+ 'prev_status' => $prevStatus,
+ 'new_status' => $newStatus,
+ 'data' => $data,
+ 'abort' => null
+ );
+
+ // Run pre filter chain
+ $filterData = runFilterChain('pre_change_surfbar_url_status', $filterData);
+
+ // Abort here?
+ if (!is_null($filterData['abort'])) {
+ // Abort here
+ return $filterData['abort'];
+ }
+
+ // Update the status now
+ // ---------- Comment out for debugging/developing member actions! ---------
+ SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_surfbar_urls` SET `url_status`='%s' WHERE `url_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! ---------
+
+ // Run post filter chain
+ $filterData = runFilterChain('post_change_surfbar_url_status', $filterData);
+
+ // Extract data from it again
+ $data = $filterData['data'];
+
+ // All done!
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',prevStatus=' . $prevStatus . ',newStatus=' . $newStatus . ' - EXIT!');
+ 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 = getTotalConfirmedUser();
+
+ // 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 = getTotalConfirmedUser();
+
+ // 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
+ `locks_id`,
+ `locks_url_id`,
+ UNIX_TIMESTAMP(`locks_last_surfed`) AS `last_surfed`
+FROM
+ `{?_MYSQL_PREFIX?}_surfbar_locks`
+WHERE
+ `locks_userid`=%s
+ORDER BY
+ `locks_id` ASC", array(getMemberId()),
+ __FUNCTION__, __LINE__);
+
+ // Load all entries
+ while ($content = SQL_FETCHARRAY($result)) {
+ // Debug message
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'next - lid='.$content['locks_id'].',url='.$content['locks_url_id'].',rest='.(time() - $content['last_surfed']).'/'.SURFBAR_GET_SURF_LOCK());
+
+ // Skip entries that are too old
+ if (($content['last_surfed'] > (time() - SURFBAR_GET_SURF_LOCK())) && (!in_array($content['locks_url_id'], $ignored))) {
+ // Debug message
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'okay - lid='.$content['locks_id'].',url='.$content['locks_url_id'].',last='.$content['last_surfed']);
+
+ // Add only if missing or bigger
+ if ((!isset($IDs[$content['locks_url_id']])) || ($IDs[$content['locks_url_id']] > $content['last_surfed'])) {
+ // Debug message
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ADD - lid='.$content['locks_id'].',url='.$content['locks_url_id'].',last='.$content['last_surfed']);
+
+ // Add this id
+ $IDs[$content['locks_url_id']] = $content['last_surfed'];
+ $USE[$content['locks_url_id']] = $content['locks_id'];
+ } // END - if
+ } else {
+ // Debug message
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ignore - lid='.$content['locks_id'].',url='.$content['locks_url_id'].',last='.$content['last_surfed']);
+
+ // Ignore these old entries!
+ $ignored[] = $content['locks_url_id'];
+ unset($IDs[$content['locks_url_id']]);
+ unset($USE[$content['locks_url_id']]);
+ }
+ } // END - while
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return array
+ return $USE;
+}
+
+// "Getter" for maximum random number
+function SURFBAR_GET_MAX_RANDOM ($userids, $add) {
+ // Count max availabe entries
+ $result = SQL_QUERY("SELECT
+ sbu.url_id AS cnt
+FROM
+ `{?_MYSQL_PREFIX?}_surfbar_urls` AS sbu
+LEFT JOIN
+ `{?_MYSQL_PREFIX?}_surfbar_salts` AS sbs
+ON
+ sbu.url_id=sbs.salts_url_id
+LEFT JOIN
+ `{?_MYSQL_PREFIX?}_surfbar_locks` AS l
+ON
+ sbu.url_id=l.locks_url_id
+WHERE
+ sbu.url_userid NOT IN (" . implode(',', $userids) . ") AND
+ (sbu.url_views_allowed=0 OR (sbu.url_views_allowed > 0 AND sbu.url_views_max > 0)) AND
+ sbu.url_status='ACTIVE'
+ " . $add . "
+GROUP BY
+ sbu.url_id ASC", __FUNCTION__, __LINE__);
+
+ // Log last query
+ /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'lastQuery='.getConfig('db_last_query').'|numRows='.SQL_NUMROWS($result).'|Affected='.SQL_AFFECTEDROWS());
+
+ // 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
+ $urlArray = array();
+
+ // Begin the query
+ $result = SQL_QUERY_ESC("SELECT
+ u.url_id,
+ u.url_userid,
+ u.url,
+ u.url_status,
+ u.url_views_total,
+ u.url_views_max,
+ u.url_views_allowed,
+ UNIX_TIMESTAMP(u.url_registered) AS `url_registered`,
+ UNIX_TIMESTAMP(u.`url_last_locked`) AS `url_last_locked`,
+ u.`url_lock_reason`
+FROM
+ `{?_MYSQL_PREFIX?}_surfbar_urls` AS u
+WHERE
+ u.`url_userid`=%s AND
+ u.`url_status` != 'DELETED'
+ORDER BY
+ u.url_id ASC",
+ array(getMemberId()), __FUNCTION__, __LINE__);
+
+ // Are there entries?
+ if (!SQL_HASZERONUMS($result)) {
+ // Load all rows
+ while ($row = SQL_FETCHARRAY($result)) {
+ // Add the row
+ $urlArray[$row['url_id']] = $row;
+ } // END - while
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return the array
+ return $urlArray;
+}
+
+// "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 `actions_action` FROM `{?_MYSQL_PREFIX?}_surfbar_actions` WHERE `actions_status`='%s' ORDER BY `actions_id` ASC",
+ array($status), __FUNCTION__, __LINE__);
+
+ // Some entries there?
+ if (!SQL_HASZERONUMS($result)) {
+ // Load all actions
+ // @TODO This can be somehow rewritten
+ while ($content = SQL_FETCHARRAY($result)) {
+ $returnArray[] = $content['actions_action'];
+ } // END - if
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return result
+ return $returnArray;