- Daily/weekly/monthly reset completely rewritten
[mailer.git] / inc / libs / surfbar_functions.php
index 72b8c8bb17a204caef20b7605b70ad7a5adf4438..4c931566ad8da8f87688334143e513372de0896a 100644 (file)
@@ -39,21 +39,50 @@ if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
 
 // Admin has added an URL with given user id
 function SURFBAR_ADMIN_ADD_URL ($url, $uid, $reward, $costs, $paymentId) {
-       // Is this really an admin?
+       // Do some pre-checks
        if (!IS_ADMIN()) {
-               // Then leave here
+               // Not an admin
                return false;
-       } // END - if
-
-       // Check if that URL does not exist
-       if (SURFBAR_LOOKUP_BY_URL($url, $uid)) {
-               // Already found!
+       } elseif (!VALIDATE_URL($url)) {
+               // URL invalid
                return false;
-       } // END - if
+       } elseif (SURFBAR_LOOKUP_BY_URL($url, $uid)) {
+               // URL already found in surfbar!
+               return false;
+       } elseif (!SURFBAR_IF_USER_BOOK_MORE_URLS($uid)) {
+               // No more allowed!
+               return false;
+       }
 
        // Register the new URL
        return SURFBAR_REGISTER_URL($url, $uid, $reward, $costs, $paymentId, "CONFIRMED", "unlock");
 }
+// Member has added an URL
+function SURFBAR_MEMBER_ADD_URL ($url) {
+       global $_CONFIG;
+
+       // Do some pre-checks
+       if (!IS_LOGGED_IN()) {
+               // Not a member
+               return false;
+       } elseif (!VALIDATE_URL($url)) {
+               // URL invalid
+               return false;
+       } elseif (SURFBAR_LOOKUP_BY_URL($url, $GLOBALS['userid'])) {
+               // URL already found in surfbar!
+               return false;
+       } elseif (!SURFBAR_IF_USER_BOOK_MORE_URLS()) {
+               // No more allowed!
+               return false;
+       }
+
+       // Do we have fixed or dynamic payment model?
+       $reward = SURFBAR_DETERMINE_REWARD();
+       $costs  = SURFBAR_DETERMINE_COSTS();
+
+       // Register the new URL
+       return SURFBAR_REGISTER_URL($url, $GLOBALS['userid'], $reward, $costs);
+}
 // Looks up by an URL
 function SURFBAR_LOOKUP_BY_URL ($url) {
        // Now lookup that given URL by itself
@@ -104,7 +133,7 @@ ORDER BY %s %s",
        return $lastUrlData;
 }
 // Registers an URL with the surfbar. You should have called SURFBAR_LOOKUP_BY_URL() first!
-function SURFBAR_REGISTER_URL ($url, $uid, $reward, $paymentId, $costs, $status="PENDING", $addMode="reg") {
+function SURFBAR_REGISTER_URL ($url, $uid, $reward, $costs, $paymentId=0, $status="PENDING", $addMode="reg") {
        global $_CONFIG;
 
        // Make sure by the user registered URLs are always pending
@@ -144,7 +173,7 @@ function SURFBAR_REGISTER_URL ($url, $uid, $reward, $paymentId, $costs, $status=
 // Inserts an url by given data array and return the insert id
 function SURFBAR_INSERT_URL_BY_ARRAY ($urlData) {
        // Just run the insert query for now
-       SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_surfbar_urls (userid, url, reward, costs, payment_id, status) VALUES(%s, '%s', %s, %s, %s, '%s')",
+       SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_surfbar_urls (userid, url, reward, costs, payment_id, status) VALUES(%s, '%s', %s, %s, %d, '%s')",
                array(
                        bigintval($urlData['uid']),
                        $urlData['url'],
@@ -206,6 +235,37 @@ function SURFBAR_TRANSLATE_STATUS ($status) {
        // Return result
        return $statusTranslated;
 }
+// Determine reward
+function SURFBAR_DETERMINE_REWARD () {
+       global $_CONFIG;
+
+       // Do we have static or dynamic?
+       if ($_CONFIG['surfbar_pay_model'] == "STATIC") {
+               // Static model, so choose static values
+               $reward = $_CONFIG['surfbar_static_reward'];
+       } else {
+               // Dynamic model, so calculate values
+               die("DYNAMIC payment model not yet supported!");
+       }
+
+       // Return reward
+       return $reward;
+}
+// Determine costs
+function SURFBAR_DETERMINE_COSTS () {
+       global $_CONFIG;
+
+       // Do we have static or dynamic?
+       if ($_CONFIG['surfbar_pay_model'] == "STATIC") {
+               $costs  = $_CONFIG['surfbar_static_costs'];
+       } else {
+               // Dynamic model, so calculate values
+               die("DYNAMIC payment model not yet supported!");
+       }
+
+       // Return costs
+       return $costs;
+}
 // Determine right template name
 function SURFBAR_DETERMINE_TEMPLATE_NAME() {
        // Default is the frameset
@@ -290,6 +350,43 @@ WHERE userid NOT IN (".implode(",", $UIDs).") AND status='%s'",
        // Return result
        return $cnt;
 }
+// Check wether the user is allowed to book more URLs
+function SURFBAR_IF_USER_BOOK_MORE_URLS ($uid=0) {
+       global $_CONFIG;
+
+       // Simply check it out
+       return (SURFBAR_GET_TOTAL_USER_URLS($uid) < $_CONFIG['surfbar_max_order']);
+}
+// Get total amount of URLs of given status for current user
+function SURFBAR_GET_TOTAL_USER_URLS ($uid=0) {
+       global $_CONFIG;
+
+       // Is the user 0 and user is logged in?
+       if (($uid == 0) && (IS_LOGGED_IN())) {
+               // Then use this userid
+               $uid = $GLOBALS['userid'];
+       } elseif ($uid == 0) {
+               // Error!
+               return ($_CONFIG['surfbar_max_order'] + 1);
+       }
+
+       // Get amount from database
+       $result = SQL_QUERY_ESC("SELECT COUNT(id) AS cnt
+FROM "._MYSQL_PREFIX."_surfbar_urls
+WHERE userid=%s
+LIMIT %s",
+               array($uid, $_CONFIG['surfbar_max_order']), __FILE__, __LINE__
+       );
+
+       // Fetch row
+       list($cnt) = SQL_FETCHROW($result);
+
+       // Free result
+       SQL_FREERESULT($result);
+
+       // Return result
+       return $cnt;
+}
 // Generate a validation code for the given id number
 function SURFBAR_GENERATE_VALIDATION_CODE ($id, $salt="") {
        global $_CONFIG, $SURFBAR_CACHE;
@@ -364,12 +461,39 @@ function SURFBAR_PAY_POINTS ($id) {
        //DEBUG_LOG(__FUNCTION__.":uid=".$GLOBALS['userid'].",reward=".SURFBAR_GET_REWARD()."");
        ADD_POINTS_REFSYSTEM($GLOBALS['userid'], SURFBAR_GET_DATA('reward'));
 }
-// Update the salt for validation
-function SURFBAR_UPDATE_SALT() {
+// Updates the statistics of current URL/userid
+function SURFBAR_UPDATE_INSERT_STATS_RECORD () {
+       global $_CONFIG;
+
        // Update views_total
        SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_surfbar_urls SET views_total=views_total+1 WHERE id=%s LIMIT 1",
                array(SURFBAR_GET_ID()), __FILE__, __LINE__);
 
+       // Update the stats entry
+       SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_surfbar_stats SET count=count+1 WHERE userid=%s AND url_id=%s LIMIT 1",
+               array($GLOBALS['userid'], SURFBAR_GET_ID()), __FILE__, __LINE__);
+
+       // Was that update okay?
+       if (SQL_AFFECTEDROWS() == 0) {
+               // No, then insert entry
+               SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_surfbar_stats (userid,url_id,count) VALUES(%s,%s,1)",
+                       array($GLOBALS['userid'], SURFBAR_GET_ID()), __FILE__, __LINE__);
+       } // END - if
+
+       // Update total/daily/weekly/monthly counter
+       $_CONFIG['surfbar_total_counter']++;
+       $_CONFIG['surfbar_daily_counter']++;
+       $_CONFIG['surfbar_weekly_counter']++;
+       $_CONFIG['surfbar_monthly_counter']++;
+
+       // Update config as well
+       UPDATE_CONFIG(array("surfbar_total_counter", "surfbar_daily_counter", "surfbar_weekly_counter", "surfbar_monthly_counter"), array(1,1,1,1), "+");
+}
+// Update the salt for validation and statistics
+function SURFBAR_UPDATE_SALT_STATS () {
+       // Update statistics record
+       SURFBAR_UPDATE_INSERT_STATS_RECORD();
+
        // Simply store the salt from cache away in database...
        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__);
@@ -445,8 +569,8 @@ ORDER BY userid ASC", __FILE__, __LINE__);
                // Shall we add this to ignore?
                if ($points <= 0) {
                        // Ignore this one!
-                       $UIDs[] = $uid;
                        //DEBUG_LOG(__FUNCTION__.":uid={$uid} has depleted points amount!");
+                       $UIDs[] = $uid;
                } // END - if
        } // END - while
 
@@ -459,6 +583,23 @@ ORDER BY userid ASC", __FILE__, __LINE__);
        // 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 COUNT(id) FROM "._MYSQL_PREFIX."_surfbar_stats WHERE (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(last_online)) >= %s",
+               array($_CONFIG['online_timeout']), __FILE__, __LINE__);
+
+       // Fetch count
+       list($cnt) = SQL_FETCHROW($result);
+
+       // Free result
+       SQL_FREERESULT($result);
+
+       // Return result
+       return $cnt;
+}
 // Determine next id for surfbar view, always call this before you call other
 // getters below this function!!!
 function SURFBAR_GET_NEXT_ID ($id = 0) {
@@ -606,18 +747,18 @@ LIMIT 1",
                        $SURFBAR_CACHE['last_surfed'] = "0";
                } // END - if
 
-               // Are we in static mode?
-               if ($_CONFIG['surfbar_pay_model'] == "STATIC") {
-                       // 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 {
+               // Get base/fixed reward and costs
+               $SURFBAR_CACHE['reward'] = SURFBAR_DETERMINE_REWARD();
+               $SURFBAR_CACHE['costs']  = SURFBAR_DETERMINE_COSTS();
+               //DEBUG_LOG(__FUNCTION__.":BASE/STATIC - reward=".SURFBAR_GET_REWARD()."|costs=".SURFBAR_GET_COSTS()."");
+
+               // Only in dynamic model add the dynamic bonus!
+               if ($_CONFIG['surfbar_pay_model'] == "DYNAMIC") {
                        // 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()."");
-               }
+                       //DEBUG_LOG(__FUNCTION__.":DYNAMIC+ - reward=".SURFBAR_GET_REWARD()."|costs=".SURFBAR_GET_COSTS()."");
+               } // END - if
 
                // Now get the id
                $nextId = SURFBAR_GET_ID();