]> git.mxchange.org Git - mailer.git/blobdiff - inc/libs/surfbar_functions.php
Extension ext-network continued:
[mailer.git] / inc / libs / surfbar_functions.php
index 42ec4152d835073a01837cf842a0b3022fda5f39..f1b2cb882bc27ccf4c9bee4c62b269c3540857b1 100644 (file)
@@ -17,7 +17,7 @@
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
- * For more information visit: http://www.mxchange.org                  *
+ * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
@@ -49,27 +49,27 @@ function SURFBAR_ADMIN_ADD_URL ($url, $limit, $reload) {
        // Do some pre-checks
        if (!isAdmin()) {
                // Not an admin
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s : Not admin.", $url, $limit, $reload));
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s: Not admin.", $url, $limit, $reload));
                return false;
        } elseif (!isUrlValid($url)) {
                // URL invalid
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s : Invalid URL.", $url, $limit, $reload));
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s: Invalid URL.", $url, $limit, $reload));
                return false;
        } elseif (SURFBAR_LOOKUP_BY_URL($url, 0)) {
                // URL already found in surfbar!
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s : Already added.", $url, $limit, $reload));
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s: Already added.", $url, $limit, $reload));
                return false;
        } elseif (!SURFBAR_IF_USER_BOOK_MORE_URLS()) {
                // No more allowed!
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s : No more URLs allowed.", $url, $limit, $reload));
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s: No more URLs allowed.", $url, $limit, $reload));
                return false;
        } elseif ('' . ($limit + 0) . '' != '' . $limit . '') {
                // Invalid limit entered
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s : Invalid limit entered.", $url, $limit, $reload));
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s: Invalid limit entered.", $url, $limit, $reload));
                return false;
        } elseif ('' . ($reload + 0) . '' != '' . $reload . '') {
                // Invalid amount entered
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s : Invalid reload entered.", $url, $limit, $reload));
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Cannot add URL=%s,limit=%s,reload=%s: Invalid reload entered.", $url, $limit, $reload));
                return false;
        }
 
@@ -82,15 +82,19 @@ function SURFBAR_ADMIN_MIGRATE_URL ($url, $userid) {
        // Do some pre-checks
        if (!isAdmin()) {
                // Not an admin
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot migrate URL: Not admin, url=' . $url . ',userid=' . $userid);
                return false;
        } elseif (!isUrlValid($url)) {
                // URL invalid
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot migrate URL: Invalid URL, url=' . $url . ',userid=' . $userid);
                return false;
        } elseif (SURFBAR_LOOKUP_BY_URL($url, $userid)) {
                // URL already found in surfbar!
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot migrate URL: Already added, url=' . $url . ',userid=' . $userid);
                return false;
        } elseif (!SURFBAR_IF_USER_BOOK_MORE_URLS($userid)) {
                // No more allowed!
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot migrate URL: Maximum exceeded, url=' . $url . ',userid=' . $userid);
                return false;
        }
 
@@ -103,12 +107,15 @@ function SURFBAR_ADMIN_UNLOCK_URL_IDS ($IDs) {
        // Is this an admin or invalid array?
        if (!isAdmin()) {
                // Not admin or invalid ids array
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Not admin');
                return false;
        } elseif (!is_array($IDs)) {
                // No array
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: IDs type ' . gettype($IDs) . '!=array');
                return false;
        } elseif (count($IDs) == 0) {
                // Empty array
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: IDs is empty');
                return false;
        }
 
@@ -130,12 +137,15 @@ function SURFBAR_ADMIN_REJECT_URL_IDS ($IDs) {
        // Is this an admin or invalid array?
        if (!isAdmin()) {
                // Not admin or invalid ids array
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Not admin');
                return false;
        } elseif (!is_array($IDs)) {
                // No array
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: IDs type ' . gettype($IDs) . '!=array');
                return false;
        } elseif (count($IDs) == 0) {
                // Empty array
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: IDs is empty');
                return false;
        }
 
@@ -162,18 +172,23 @@ function SURFBAR_MEMBER_ADD_URL ($url, $limit) {
        // Do some pre-checks
        if (!isMember()) {
                // Not a member
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Not member');
                return false;
        } elseif ((!isUrlValid($url)) && (!isAdmin())) {
                // URL invalid
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Invalid, url=' . $url . ',limit=' . $limit);
                return false;
        } elseif (SURFBAR_LOOKUP_BY_URL($url, getMemberId())) {
                // URL already found in surfbar!
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Already found, url=' . $url . ',limit=' . $limit);
                return false;
        } elseif (!SURFBAR_IF_USER_BOOK_MORE_URLS(getMemberId())) {
                // No more allowed!
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Maximum exceeded, url=' . $url . ',limit=' . $limit);
                return false;
        } elseif (''.($limit + 0).'' != ''.$limit.'') {
                // Invalid amount entered
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Invalid limit, url=' . $url . ',limit=' . $limit);
                return false;
        }
 
@@ -196,7 +211,7 @@ function SURFBAR_MEMBER_ACTIONS ($urlId, $status) {
        // "Walk" through all actions and create forms
        foreach ($actionArray as $actionId => $action) {
                // Add form for this action
-               $OUT .= loadTemplate('member_surfbar_list_form', true, array(
+               $OUT .= loadTemplate('member_list_surfbar_form', true, array(
                        'width'  => $width,
                        'url_id' => bigintval($urlId),
                        'action' => strtolower($action)
@@ -219,15 +234,19 @@ function SURFBAR_MEMBER_DO_FORM ($formData, $urlArray) {
        // Is this a member?
        if (!isMember()) {
                // No member!
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Not member');
                return false;
        } elseif ((!isset($formData['id'])) || (!isset($formData['action']))) {
                // Important form elements are missing!
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Invalid form data, required field id/action not found');
                return false;
        } elseif (!isset($urlArray[$formData['id']])) {
-               // id not found in cache
+               // Id not found in cache
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Field id not found in cache');
                return false;
        } elseif (!SURFBAR_VALIDATE_MEMBER_ACTION_STATUS($formData['action'], $urlArray[$formData['id']]['url_status'])) {
                // Action not allowed for current URL status
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Action not allowed to perform. action=' . $formData['action'] . ',url_status=' . $urlArray[$formData['id']]['url_status'] . 'id=' . $formData['id']);
                return false;
        }
 
@@ -255,8 +274,7 @@ function SURFBAR_MEMBER_DO_FORM ($formData, $urlArray) {
                $performed = call_user_func_array($functionName, $urlData);
        } else {
                // Log invalid request
-               logDebugMessage(__FUNCTION__, __LINE__, "action={$formData['action']},id={$formData['id']},function={$functionName}");
-               addFatalMessage(__FUNCTION__, __LINE__, "Invalid member action! action=%s,id=%s,function=%s", array($formData['action'], $formData['id'], $functionName));
+               debug_report_bug(__FUNCTION__, __LINE__, 'Invalid member action! action=' . $formData['action'] . ',id=' . $formData['id'] . ',function=' . $functionName);
        }
 
        // Return status
@@ -408,7 +426,7 @@ function SURFBAR_MEMBER_EXECUTE_ACTION ($action, $urlData) {
                } // END - if
        } else {
                // Not found
-               addFatalMessage(__FUNCTION__, __LINE__, '{--MEMBER_SURFBAR_EXECUTE_ACTION_404--}', $functionName);
+               debug_report_bug(__FUNCTION__, __LINE__, 'Callback function ' . $functionName . ' not found.');
        }
 
        // Return status
@@ -635,7 +653,9 @@ function SURFBAR_INSERT_URL_BY_ARRAY ($urlData) {
        $userid = bigintval($urlData['url_userid']);
 
        // Is the id set?
-       if (empty($userid)) $userid = '0';
+       if (empty($userid)) {
+               $userid = 'NULL';
+       } // END - if
 
        // Just run the insert query for now
        SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_surfbar_urls` (`url_userid`,`url`,`url_status`,`url_views_max`,`url_views_allowed`,`url_fixed_reload`) VALUES (%s,'%s','%s',%s,%s,%s)",
@@ -841,10 +861,10 @@ function SURFBAR_DETERMINE_TEMPLATE_NAME() {
        $templateName = 'surfbar_frameset';
 
        // Any frame set? ;-)
-       if (isGetRequestParameterSet('frame')) {
+       if (isGetRequestElementSet('frame')) {
                // Use the frame as a template name part... ;-)
                $templateName = sprintf("surfbar_frame_%s",
-                       getRequestParameter('frame')
+                       getRequestElement('frame')
                );
        } // END - if
 
@@ -855,18 +875,20 @@ function SURFBAR_DETERMINE_TEMPLATE_NAME() {
 // Check if the "reload lock" of the current user is full, call this function
 // before you call SURFBAR_CHECK_RELOAD_LOCK().
 function SURFBAR_CHECK_RELOAD_FULL () {
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Fixed surf lock is ' . getConfig('surfbar_static_lock') . ' - ENTERED!');
        // Default is full!
        $isFull = true;
 
        // Cache static reload lock
        $GLOBALS['surfbar_cache']['surf_lock'] = getConfig('surfbar_static_lock');
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Fixed surf lock is '.getConfig('surfbar_static_lock') . '', false);
 
        // Do we have dynamic model?
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'surf_lock=' . $GLOBALS['surfbar_cache']['surf_lock'] . ' - BEFORE');
        if (getSurfbarPaymentModel() == 'DYNAMIC') {
                // "Calculate" dynamic lock
                $GLOBALS['surfbar_cache']['surf_lock'] += SURFBAR_CALCULATE_DYNAMIC_ADD();
        } // END - if
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'surf_lock=' . $GLOBALS['surfbar_cache']['surf_lock'] . ' - AFTER');
 
        // Ask the database
        $result = SQL_QUERY_ESC("SELECT
@@ -904,15 +926,15 @@ LIMIT 1",
        $total = SURFBAR_GET_TOTAL_URLS();
 
        // Do we have some URLs in lock? Admins can always surf on own URLs!
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "userLocks=".SURFBAR_GET_USER_LOCKS().",total={$total}", false);
        $isFull = ((SURFBAR_GET_USER_LOCKS() == $total) && ($total > 0));
 
        // Return result
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userLocks=' . SURFBAR_GET_USER_LOCKS() . ',total=' . $total . 'isFull=' . intval($isFull) . ' - EXIT!');
        return $isFull;
 }
 
 // Get total amount of URLs of given status for current user or of ACTIVE URLs by default
-function SURFBAR_GET_TOTAL_URLS ($status = 'ACTIVE', $excludeUserId = '0') {
+function SURFBAR_GET_TOTAL_URLS ($status = 'ACTIVE', $excludeUserId = NULL) {
        // Determine depleted user account
        $userids = SURFBAR_DETERMINE_DEPLETED_USERIDS();
 
@@ -947,25 +969,25 @@ LIMIT 1",
        SQL_FREERESULT($result);
 
        // Debug message
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'cnt=' . $count);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'cnt=' . $count);
 
        // Return result
        return $count;
 }
 
 // Check wether the user is allowed to book more URLs
-function SURFBAR_IF_USER_BOOK_MORE_URLS ($userid = '0') {
+function SURFBAR_IF_USER_BOOK_MORE_URLS ($userid = NULL) {
        // Is this admin and userid is zero or does the user has some URLs left to book?
-       return ((($userid == '0') && (isAdmin())) || (SURFBAR_GET_TOTAL_USER_URLS($userid, '', array('REJECTED')) < getSurfbarMaxOrder()));
+       return (((is_null($userid)) && (isAdmin())) || (SURFBAR_GET_TOTAL_USER_URLS($userid, '', array('REJECTED')) < getSurfbarMaxOrder()));
 }
 
 // Get total amount of URLs of given status for current user
-function SURFBAR_GET_TOTAL_USER_URLS ($userid = '0', $status = '', $exclude = '') {
+function SURFBAR_GET_TOTAL_USER_URLS ($userid = NULL, $status = '', $exclude = '') {
        // Is the user 0 and user is logged in?
-       if (($userid == '0') && (isMember())) {
+       if ((is_null($userid)) && (isMember())) {
                // Then use this userid
                $userid = getMemberId();
-       } elseif ($userid == '0') {
+       } elseif (is_null($userid)) {
                // Error!
                return (getSurfbarMaxOrder() + 1);
        }
@@ -976,16 +998,16 @@ function SURFBAR_GET_TOTAL_USER_URLS ($userid = '0', $status = '', $exclude = ''
        // Is the status set?
        if (is_array($status)) {
                // Only URLs with these status
-               $add = sprintf(" AND `url_status` IN('%s')", implode("','", $status));
+               $add = sprintf(" WHERE `url_status` IN('%s')", implode("','", $status));
        } elseif (!empty($status)) {
                // Only URLs with this status
-               $add = sprintf(" AND `url_status`='%s'", $status);
+               $add = sprintf(" WHERE `url_status`='%s'", $status);
        } elseif (is_array($exclude)) {
                // Exclude URLs with these status
-               $add = sprintf(" AND `url_status` NOT IN('%s')", implode("','", $exclude));
+               $add = sprintf(" WHERE `url_status` NOT IN('%s')", implode("','", $exclude));
        } elseif (!empty($exclude)) {
                // Exclude URLs with this status
-               $add = sprintf(" AND `url_status` != '%s'", $exclude);
+               $add = sprintf(" WHERE `url_status` != '%s'", $exclude);
        }
 
        // Get amount from database
@@ -997,53 +1019,32 @@ function SURFBAR_GET_TOTAL_USER_URLS ($userid = '0', $status = '', $exclude = ''
 
 // Generate a validation code for the given id number
 function SURFBAR_GENERATE_VALIDATION_CODE ($urlId, $salt = '') {
-       // @TODO Invalid salt should be refused
-       $length = '0';
-       $GLOBALS['surfbar_cache']['salt'] = 'INVALID';
-
-       // Is extension ext-other there?
-       if (isExtensionActive('other')) {
-               // Get code length from config
-               $length = getCodeLength();
-       } // END - if
-
-       // Fix length to 10
-       if ($length == '0') {
-               $length = 10;
-       } // END - if
-
-       // Generate a code until the length matches
-       $valCode = '';
-       while (strlen($valCode) != $length) {
-               // Is the salt set?
-               if (empty($salt)) {
-                       // Generate random hashed string
-                       $GLOBALS['surfbar_cache']['salt'] = sha1(generatePassword(mt_rand(200, 255)));
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'newSalt='.SURFBAR_GET_SALT().'', false);
-               } else {
-                       // Use this as salt!
-                       $GLOBALS['surfbar_cache']['salt'] = $salt;
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'oldSalt='.SURFBAR_GET_SALT().'', false);
-               }
-
-               // ... and now the validation code
-               $valCode = generateRandomCode($length, sha1(SURFBAR_GET_SALT() . getEncryptSeperator() . $urlId), getMemberId());
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'valCode='.valCode.'', false);
-       } // END - while
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',salt=' . $salt . ' - ENTERED!');
+       // Init hash with invalid value
+       if (empty($salt)) {
+                // Generate random hashed string
+               $GLOBALS['surfbar_cache']['salt'] = sha1(generatePassword(mt_rand(200, 255)));
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'newSalt='.SURFBAR_GET_SALT().'', false);
+       } else {
+               // Use this as salt!
+               $GLOBALS['surfbar_cache']['salt'] = $salt;
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'oldSalt='.SURFBAR_GET_SALT().'', false);
+       }
 
        // Hash it with md5() and salt it with the random string
-       $hashedCode = generateHash(md5($valCode), SURFBAR_GET_SALT());
+       $hashedCode = generateHash(md5($urlId . getEncryptSeperator() . getMemberId()), SURFBAR_GET_SALT());
 
        // Finally encrypt it PGP-like and return it
        $valHashedCode = encodeHashForCookie($hashedCode);
 
        // Return hashed value
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'finalValCode='.$valHashedCode.'', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',salt=' . $salt . ',urlId=' . $urlId . ',salt=' . $salt . ',valHashedCode=' . $valHashedCode . ' - EXIT!');
        return $valHashedCode;
 }
 
 // Check validation code
 function SURFBAR_CHECK_VALIDATION_CODE ($urlId, $check, $salt) {
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',check=' . $check . ',salt=' . $salt . ' - ENTERED!');
        // Secure id number
        $urlId = bigintval($urlId);
 
@@ -1051,38 +1052,52 @@ function SURFBAR_CHECK_VALIDATION_CODE ($urlId, $check, $salt) {
        $code = SURFBAR_GENERATE_VALIDATION_CODE($urlId, $salt);
 
        // Return result of checking hashes and salts
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '---'.$code.'|'.$check.'---', false);
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, '+++'.$salt.'|'.SURFBAR_GET_DATA('salts_last_salt').'+++', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',(code==check)=' . intval($code == $check) . ',(salt==salts_last_salt)=' . intval($salt == SURFBAR_GET_DATA('salts_last_salt')) . ' - EXIT!');
        return (($code == $check) && ($salt == SURFBAR_GET_DATA('salts_last_salt')));
 }
 
 // Lockdown the userid/id combination (reload lock)
 function SURFBAR_LOCKDOWN_ID ($urlId) {
-       //* DEBUG: */ debugOutput('LOCK!');
-       ///* DEBUG: */ return;
-       // Just add it to the database
-       SQL_QUERY_ESC('INSERT INTO `{?_MYSQL_PREFIX?}_surfbar_locks` (`locks_userid`, `locks_url_id`) VALUES (%s, %s)',
-               array(getMemberId(), bigintval($urlId)), __FUNCTION__, __LINE__);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',getMemberId()=' . getMemberId() . ' - ENTERED!');
+       // Search for an entry
+       $countLock = countSumTotalData(getMemberId(), 'surfbar_locks', 'locks_id', 'locks_userid', true, ' AND `locks_url_id`=' . bigintval($urlId));
+
+       // Do we have no record?
+       if ($countLock == 0) {
+               // Just add it to the database
+               SQL_QUERY_ESC('INSERT INTO `{?_MYSQL_PREFIX?}_surfbar_locks` (`locks_userid`,`locks_url_id`) VALUES (%s, %s)',
+                       array(
+                               getMemberId(),
+                               bigintval($urlId)
+                       ), __FUNCTION__, __LINE__);
+       } // END - if
 
        // Remove the salt from database
        SQL_QUERY_ESC('DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_surfbar_salts` WHERE `salts_url_id`=%s AND `salts_userid`=%s LIMIT 1',
-               array(bigintval($urlId), getMemberId()), __FUNCTION__, __LINE__);
+               array(
+                       bigintval($urlId),
+                       getMemberId()
+               ), __FUNCTION__, __LINE__);
+
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',getMemberId()=' . getMemberId() . ',SQL_AFFECTEDROWS()=' . SQL_AFFECTEDROWS() . ' - EXIT!');
 }
 
 // Pay points to the user and remove it from the sender if userid is given else it is a "sponsored surf"
 function SURFBAR_PAY_POINTS () {
        // Remove it from the URL owner
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.SURFBAR_GET_USERID().',costs='.SURFBAR_GET_COSTS().'', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.SURFBAR_GET_USERID().',costs='.SURFBAR_GET_COSTS() . ' - ENTERED!');
        if (isValidUserId(SURFBAR_GET_USERID())) {
+               // Subtract points and ignore return status
                subtractPoints(sprintf("surfbar_%s", getSurfbarPaymentModel()), SURFBAR_GET_USERID(), SURFBAR_GET_COSTS());
        } // END - if
 
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.getMemberId().',reward='.SURFBAR_GET_REWARD().'', false);
-       // @TODO Try to rewrite the following unset()
-       unset($GLOBALS['ref_level']);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.getMemberId().',reward='.SURFBAR_GET_REWARD());
+       // Init referal system here
+       initReferalSystem();
 
-       // Book it to the user
-       addPointsThroughReferalSystem(sprintf("surfbar_%s", getSurfbarPaymentModel()), getMemberId(), SURFBAR_GET_REWARD());
+       // Book it to the user and ignore return status
+       addPointsThroughReferalSystem(sprintf("surfbar:%s", getSurfbarPaymentModel()), getMemberId(), SURFBAR_GET_REWARD());
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.SURFBAR_GET_USERID().',costs='.SURFBAR_GET_COSTS() . ' - EXIT!');
 }
 
 // Updates the statistics of current URL/userid
@@ -1113,7 +1128,7 @@ function SURFBAR_UPDATE_INSERT_STATS_RECORD () {
        // Was that update okay?
        if (SQL_HASZEROAFFECTED()) {
                // No, then insert entry
-               SQL_QUERY_ESC('INSERT INTO `{?_MYSQL_PREFIX?}_surfbar_stats` (`stats_userid`, `stats_url_id`, `stats_count`) VALUES (%s,%s,1)',
+               SQL_QUERY_ESC('INSERT INTO `{?_MYSQL_PREFIX?}_surfbar_stats` (`stats_userid`,`stats_url_id`,`stats_count`) VALUES (%s,%s,1)',
                        array(
                                getMemberId(),
                                SURFBAR_GET_ID()
@@ -1132,12 +1147,18 @@ function SURFBAR_UPDATE_INSERT_STATS_RECORD () {
 
 // Update the salt for validation and statistics
 function SURFBAR_UPDATE_SALT_STATS () {
-       // Update statistics record
-       SURFBAR_UPDATE_INSERT_STATS_RECORD();
-
        // Update salt
        SURFBAR_GENERATE_VALIDATION_CODE(SURFBAR_GET_ID());
 
+       // Make sure only valid salts can pass
+       if (SURFBAR_GET_SALT() == 'INVALID') {
+               // Invalid provided
+               debug_report_bug(__FUNCTION__, __LINE__, 'Invalid salt provided, id=' . SURFBAR_GET_ID() . ',getMemberId()=' . getMemberId());
+       } // END - if
+
+       // 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 `salts_last_salt`='%s' WHERE `salts_url_id`=%s AND `salts_userid`=%s LIMIT 1",
                array(
@@ -1147,17 +1168,21 @@ function SURFBAR_UPDATE_SALT_STATS () {
                ), __FUNCTION__, __LINE__);
 
        // Debug message
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'salt='.SURFBAR_GET_SALT().',id='.SURFBAR_GET_ID().',userid='.getMemberId().'', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'salt=' . SURFBAR_GET_SALT() . ',id=' . SURFBAR_GET_ID() . ',userid=' . getMemberId() . ',SQL_AFFECTEDROWS()=' . SQL_AFFECTEDROWS() . ' - UPDATE!');
 
        // Was that okay?
        if (SQL_HASZEROAFFECTED()) {
                // Insert missing entry!
-               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_surfbar_salts` (`salts_url_id`, `salts_userid`, `salts_last_salt`) VALUES (%s, %s, '%s')",
-                       array(SURFBAR_GET_ID(), getMemberId(), SURFBAR_GET_SALT()), __FUNCTION__, __LINE__);
+               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_surfbar_salts` (`salts_url_id`,`salts_userid`,`salts_last_salt`) VALUES (%s, %s, '%s')",
+                       array(
+                               SURFBAR_GET_ID(),
+                               getMemberId(),
+                               SURFBAR_GET_SALT()
+                       ), __FUNCTION__, __LINE__);
        } // END - if
 
        // Debug message
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'affectedRows='.SQL_AFFECTEDROWS().'', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'affectedRows=' . SQL_AFFECTEDROWS() . ' - EXIT!');
 
        // Return if the update was okay
        return (!SQL_HASZEROAFFECTED());
@@ -1165,7 +1190,7 @@ function SURFBAR_UPDATE_SALT_STATS () {
 
 // Check if the reload lock is active for given id
 function SURFBAR_CHECK_RELOAD_LOCK ($urlId) {
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $urlId . '', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'id=' . $urlId . ' -  ENTERED!');
        // Ask the database
        $result = SQL_QUERY_ESC("SELECT COUNT(`locks_id`) AS cnt
 FROM
@@ -1187,7 +1212,7 @@ LIMIT 1",
        SQL_FREERESULT($result);
 
        // Return check
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'cnt=' . $count . ',' . SURFBAR_GET_SURF_LOCK() . '', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',count=' . $count . ',SURFBAR_GET_SURF_LOCK()=' . SURFBAR_GET_SURF_LOCK() . ' - EXIT!');
        return ($count == 1);
 }
 
@@ -1247,12 +1272,12 @@ ORDER BY
        while ($content = SQL_FETCHARRAY($result)) {
                // Get total points
                $points = getTotalPoints($content['url_userid']);
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "userid={$content['url_userid']},points={$points}", false);
+               /* 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!", false);
+                       /* 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'];
@@ -1263,7 +1288,7 @@ ORDER BY
        SQL_FREERESULT($result);
 
        // Debug message
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "UIDs::count=".count($userids)." (with own userid=".getMemberId().')', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'UIDs::count=' . count($userids) . ' (with own userid=' . getMemberId() . ')');
 
        // Return result
        return $userids;
@@ -1316,8 +1341,8 @@ function SURFBAR_DETERMINE_WAIT_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!');
+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);
 
@@ -1369,7 +1394,7 @@ function SURFBAR_CHANGE_STATUS ($urlId, $prevStatus, $newStatus, $data=array())
        $data = $filterData['data'];
 
        // All done!
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',prevStatus=' . $prevStatus . ',newStatus=' . $newStatus . ' - EXIT!');
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',prevStatus=' . $prevStatus . ',newStatus=' . $newStatus . ' - EXIT!');
        return true;
 }
 
@@ -1457,7 +1482,10 @@ function SURFBAR_GET_LOCK_IDS () {
        $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
+       $result = SQL_QUERY_ESC("SELECT
+       `locks_id`,
+       `locks_url_id`,
+       UNIX_TIMESTAMP(`locks_last_surfed`) AS `last_surfed`
 FROM
        `{?_MYSQL_PREFIX?}_surfbar_locks`
 WHERE
@@ -1469,30 +1497,30 @@ ORDER BY
        // 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);
+               /* 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['url_id'], $ignored))) {
+               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['id'].',url='.$content['url_id'].',last='.$content['last_surfed'].'', false);
+                       /* 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['url_id']])) || ($IDs[$content['url_id']] > $content['last_surfed'])) {
+                       if ((!isset($IDs[$content['locks_url_id']])) || ($IDs[$content['locks_url_id']] > $content['last_surfed'])) {
                                // Debug message
-                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ADD - lid='.$content['id'].',url='.$content['url_id'].',last='.$content['last_surfed'].'', false);
+                               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ADD - lid='.$content['locks_id'].',url='.$content['locks_url_id'].',last='.$content['last_surfed']);
 
                                // Add this id
-                               $IDs[$content['url_id']] = $content['last_surfed'];
-                               $USE[$content['url_id']] = $content['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['id'].',url='.$content['url_id'].',last='.$content['last_surfed'].'', false);
+                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ignore - lid='.$content['locks_id'].',url='.$content['locks_url_id'].',last='.$content['last_surfed']);
 
                        // Ignore these old entries!
-                       $ignored[] = $content['url_id'];
-                       unset($IDs[$content['url_id']]);
-                       unset($USE[$content['url_id']]);
+                       $ignored[] = $content['locks_url_id'];
+                       unset($IDs[$content['locks_url_id']]);
+                       unset($USE[$content['locks_url_id']]);
                }
        } // END - while
 
@@ -1527,7 +1555,7 @@ 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().'', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'lastQuery='.getConfig('db_last_query').'|numRows='.SQL_NUMROWS($result).'|Affected='.SQL_AFFECTEDROWS());
 
        // Fetch max rand
        $maxRand = SQL_NUMROWS($result);
@@ -1609,6 +1637,7 @@ function SURFBAR_GET_ARRAY_FROM_STATUS ($status) {
 // Reload to configured stop page
 function SURFBAR_RELOAD_TO_STOP_PAGE ($page = 'stop') {
        // Internal or external?
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'page=' . $page . ' - ENTERED!');
        if ((getConfig('surfbar_pause_mode') == 'INTERNAL') || (getConfig('surfbar_pause_url') == '')) {
                // Reload to internal page
                redirectToUrl('surfbar.php?frame=' . $page);
@@ -1620,13 +1649,14 @@ function SURFBAR_RELOAD_TO_STOP_PAGE ($page = 'stop') {
 
 // 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') {
+function SURFBAR_DETERMINE_NEXT_ID ($urlId = NULL) {
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ' - ENTERED!');
        // Default is no id and no random number
        $nextId = '0';
        $randNum = '0';
 
        // Is the id set?
-       if ($urlId == '0') {
+       if (is_null($urlId)) {
                // Get array with lock ids
                $USE = SURFBAR_GET_LOCK_IDS();
 
@@ -1657,7 +1687,7 @@ function SURFBAR_DETERMINE_NEXT_ID ($urlId = '0') {
                } // END - if
 
                // And query the database
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'randNum='.$randNum.',maxRand='.$maxRand.',surfLock='.SURFBAR_GET_SURF_LOCK().'', false);
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'randNum='.$randNum.',maxRand='.$maxRand.',surfLock='.SURFBAR_GET_SURF_LOCK());
                $result = SQL_QUERY_ESC("SELECT
        sbu.url_id,
        sbu.url_userid,
@@ -1724,12 +1754,12 @@ LIMIT 1",
        }
 
        // Is there an id number?
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'lastQuery='.getConfig('db_last_query').'|numRows='.SQL_NUMROWS($result).'|Affected='.SQL_AFFECTEDROWS().'', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'lastQuery='.getConfig('db_last_query').'|numRows='.SQL_NUMROWS($result).'|Affected='.SQL_AFFECTEDROWS());
        if (SQL_NUMROWS($result) == 1) {
                // Load/cache data
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count('.count($GLOBALS['surfbar_cache']).') - BEFORE', false);
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count('.count($GLOBALS['surfbar_cache']).') - BEFORE', false);
                $GLOBALS['surfbar_cache'] = merge_array($GLOBALS['surfbar_cache'], SQL_FETCHARRAY($result));
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count('.count($GLOBALS['surfbar_cache']).') - AFTER', false);
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'count('.count($GLOBALS['surfbar_cache']).') - AFTER', false);
 
                // Determine waiting time
                $GLOBALS['surfbar_cache']['time'] = SURFBAR_DETERMINE_WAIT_TIME();
@@ -1737,28 +1767,28 @@ LIMIT 1",
                // Is the last salt there?
                if (is_null($GLOBALS['surfbar_cache']['salts_last_salt'])) {
                        // Then repair it wit the static!
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'last_salt - FIXED!', false);
+                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'last_salt - FIXED!', false);
                        $GLOBALS['surfbar_cache']['salts_last_salt'] = '';
                } // END - if
 
                // Fix missing last_surfed
                if ((!isset($GLOBALS['surfbar_cache']['last_surfed'])) || (is_null($GLOBALS['surfbar_cache']['last_surfed']))) {
                        // Fix it here
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'last_surfed - FIXED!', false);
+                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'last_surfed - FIXED!', false);
                        $GLOBALS['surfbar_cache']['last_surfed'] = '0';
                } // END - if
 
                // Get base/fixed reward and costs
                $GLOBALS['surfbar_cache']['reward'] = SURFBAR_DETERMINE_REWARD();
                $GLOBALS['surfbar_cache']['costs']  = SURFBAR_DETERMINE_COSTS();
-               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'BASE/STATIC - reward='.SURFBAR_GET_REWARD().'|costs='.SURFBAR_GET_COSTS().'', false);
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'BASE/STATIC - reward='.SURFBAR_GET_REWARD().'|costs='.SURFBAR_GET_COSTS());
 
                // Only in dynamic model add the dynamic bonus!
                if (getSurfbarPaymentModel() == 'DYNAMIC') {
                        // Calculate dynamic reward/costs and add it
                        $GLOBALS['surfbar_cache']['reward'] += SURFBAR_CALCULATE_DYNAMIC_ADD();
                        $GLOBALS['surfbar_cache']['costs']  += SURFBAR_CALCULATE_DYNAMIC_ADD();
-                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'DYNAMIC+ - reward='.SURFBAR_GET_REWARD().'|costs='.SURFBAR_GET_COSTS().'', false);
+                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'DYNAMIC+ - reward='.SURFBAR_GET_REWARD().'|costs='.SURFBAR_GET_COSTS());
                } // END - if
 
                // Now get the id
@@ -1769,7 +1799,7 @@ LIMIT 1",
        SQL_FREERESULT($result);
 
        // Return result
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'nextId='.$nextId.'', false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'urlId=' . $urlId . ',nextId=' . $nextId . ' - EXIT!');
        return $nextId;
 }
 
@@ -1850,10 +1880,10 @@ function SURFBAR_INIT () {
 
 // Private getter for data elements
 function SURFBAR_GET_DATA ($element) {
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "element={$element}", false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ' - ENTERED!');
 
        // Default is null
-       $data = null;
+       $data = NULL;
 
        // Is the entry there?
        if (isset($GLOBALS['surfbar_cache'][$element])) {
@@ -1867,7 +1897,7 @@ function SURFBAR_GET_DATA ($element) {
        }
 
        // Return result
-       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . $element . ']=[' . gettype($data) . ']' . $data, false);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . $element . ']=[' . gettype($data) . ']' . $data . ' - EXIT!');
        return $data;
 }
 
@@ -1949,5 +1979,11 @@ function SURFBAR_GET_NEW_STATUS () {
        return SURFBAR_GET_DATA('new_status');
 }
 
+// Getter for last salt
+function SURFBAR_GET_LAST_SALT () {
+       // Get data element and return its contents
+       return SURFBAR_GET_DATA('salts_last_salt');
+}
+
 // [EOF]
 ?>