+
+// Do the member form request
+function SURFBAR_MEMBER_DO_FORM ($formData, $URLs) {
+ // By default no action is performed
+ $performed = false;
+
+ // Is this a member?
+ if (!isMember()) {
+ // No member!
+ return false;
+ } elseif ((!isset($formData['id'])) || (!isset($formData['action']))) {
+ // Important form elements are missing!
+ return false;
+ } elseif (!isset($URLs[$formData['id']])) {
+ // id not found in cache
+ return false;
+ } elseif (!SURFBAR_VALIDATE_MEMBER_ACTION_STATUS($formData['action'], $URLs[$formData['id']]['status'])) {
+ // Action not allowed for current URL status
+ return false;
+ }
+
+ // Secure action
+ $action = secureString($formData['action']);
+
+ // Has it changed?
+ if ($action != $formData['action']) {
+ // Invalid data in action found
+ return false;
+ } // END - if
+
+ // Create the function name for selected action
+ $functionName = sprintf("SURFBAR_MEMBER_%s_ACTION", strtoupper($action));
+
+ // Is the function there?
+ if (function_exists($functionName)) {
+ // Add new status
+ $URLs[$formData['id']]['new_status'] = SURFBAR_GET_NEW_STATUS('new_status');
+
+ // Extract URL data for call-back
+ $urlData = array(merge_array($URLs[$formData['id']], array($action => $formData)));
+
+ // Action found so execute it
+ $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));
+ }
+
+ // Return status
+ return $performed;
+}
+
+// Validate if the requested action can be performed on current URL status
+function SURFBAR_VALIDATE_MEMBER_ACTION_STATUS ($action, $status) {
+ // Search for the requested action/status combination in database
+ $result = SQL_QUERY_ESC("SELECT new_status FROM `{?_MYSQL_PREFIX?}_surfbar_actions` WHERE `action`='%s' AND `status`='%s' LIMIT 1",
+ array($action, $status), __FUNCTION__, __LINE__);
+
+ // Is the entry there?
+ $isValid = (SQL_NUMROWS($result) == 1);
+
+ // Fetch the new status if found
+ if ($isValid) {
+ // Load new status
+ list($GLOBALS['surfbar_cache']['new_status']) = SQL_FETCHROW($result);
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return status
+ return $isValid;
+}
+
+//
+// -----------------------------------------------------------------------------
+// Member actions
+// -----------------------------------------------------------------------------
+//
+// Retreat a booked URL
+function SURFBAR_MEMBER_RETREAT_ACTION ($urlData) {
+ // Create the data array for next function call
+ $data = array(
+ $urlData['id'] => $urlData
+ );
+
+ // Simply change the status here
+ return SURFBAR_CHANGE_STATUS ($urlData['id'], $urlData['status'], $urlData['new_status'], $data);
+}
+
+// Book an URL now (from migration)
+function SURFBAR_MEMBER_BOOKNOW_ACTION ($urlData) {
+ // Create the data array for next function call
+ $data = array(
+ $urlData['id'] => $urlData
+ );
+
+ // Simply change the status here
+ return SURFBAR_CHANGE_STATUS ($urlData['id'], $urlData['status'], $urlData['new_status'], $data);
+}
+
+// Show edit form or do the changes
+function SURFBAR_MEMBER_EDIT_ACTION ($urlData) {
+ // Is the "execute" flag there?
+ if (isset($urlData['edit']['execute'])) {
+ // Execute the changes
+ return SURFBAR_MEMBER_EXECUTE_ACTION('edit', $urlData);
+ } // END - if
+
+ // Display form
+ return SURFBAR_MEMBER_DISPLAY_ACTION_FORM('edit', $urlData);
+}
+
+// Show delete form or do the changes
+function SURFBAR_MEMBER_DELETE_ACTION ($urlData) {
+ // Is the "execute" flag there?
+ if (isset($urlData['delete']['execute'])) {
+ // Execute the changes
+ return SURFBAR_MEMBER_EXECUTE_ACTION('delete', $urlData);
+ } // END - if
+
+ // Display form
+ return SURFBAR_MEMBER_DISPLAY_ACTION_FORM('delete', $urlData);
+}
+
+// Pause active banner
+function SURFBAR_MEMBER_PAUSE_ACTION ($urlData) {
+ return SURFBAR_CHANGE_STATUS($urlData['id'], $urlData['status'], $urlData['new_status'], array($urlData['id'] => $urlData));
+}
+
+// Unpause stopped banner
+function SURFBAR_MEMBER_UNPAUSE_ACTION ($urlData) {
+ // Fix missing entry for template
+ $urlData['edit'] = $urlData['unpause'];
+ $urlData['edit']['url'] = $urlData['url'];
+ $urlData['edit']['limit'] = $urlData['views_max'];
+
+ // Return status change
+ return SURFBAR_CHANGE_STATUS($urlData['id'], $urlData['status'], $urlData['new_status'], array($urlData['id'] => $urlData));
+}
+
+// Resubmit locked URL
+function SURFBAR_MEMBER_RESUBMIT_ACTION ($urlData) {
+ return SURFBAR_CHANGE_STATUS($urlData['id'], $urlData['status'], $urlData['new_status'], array($urlData['id'] => $urlData));
+}
+
+// Display selected "action form"
+function SURFBAR_MEMBER_DISPLAY_ACTION_FORM ($action, $urlData) {
+ // Translate some data
+ $urlData['registered'] = generateDateTime($urlData['registered'], 2);
+ $urlData['views_total'] = translateComma($urlData['views_total']);
+ $urlData['views_max'] = translateComma($urlData['views_max']);
+ $urlData['views_allowed'] = translateComma($urlData['views_allowed']);
+ $urlData['last_locked'] = generateDateTime($urlData['last_locked'], 2);
+
+ // Is the lock reason empty?
+ if (empty($urlData['lock_reason'])) {
+ // Fix it to three dashes
+ $urlData['lock_reason'] = '---';
+ } // END - if
+
+ // Include fields only for action 'edit'
+ if ($action == 'edit') {
+ // Default is not limited
+ $urlData['limited_y'] = '';
+ $urlData['limited_n'] = ' checked="checked"';
+ $urlData['limited'] = 'false';
+
+ // Is this URL limited?
+ if ($urlData['views_max'] > 0) {
+ // Then rewrite form data
+ $urlData['limited_y'] = ' checked="checked"';
+ $urlData['limited_n'] = '';
+ $urlData['limited'] = 'true';
+ } // END - if
+ } // END - if
+
+ // Load the form and display it
+ loadTemplate(sprintf("member_surfbar_%s_action_form", $action), false, $urlData);
+
+ // All fine by default ... ;-)
+ return true;
+}
+
+// Execute choosen action
+function SURFBAR_MEMBER_EXECUTE_ACTION ($action, $urlData) {
+ // By default nothing is executed
+ $executed = false;
+
+ // Is limitation "no" and "limit" is > 0?
+ if ((isset($urlData[$action]['limited'])) && ($urlData[$action]['limited'] != 'Y') && ((isset($urlData[$action]['limit'])) && ($urlData[$action]['limit'] > 0)) || (!isset($urlData[$action]['limit']))) {
+ // Set it to unlimited
+ $urlData[$action]['limit'] = '0';
+ } // END - if
+
+ // Construct function name
+ $functionName = sprintf("SURFBAR_MEMBER_EXECUTE_%s_ACTION", strtoupper($action));
+
+ // Is 'userid' set and not 'userid' ?
+ if ((!isset($urlData['userid'])) && (isset($urlData['userid']))) {
+ // Auto-fix this
+ $urlData['userid'] = $urlData['userid'];
+ } // END - if
+
+ // Is that function there?
+ if (function_exists($functionName)) {
+ // Execute the function
+ if (call_user_func_array($functionName, array($urlData)) == true) {
+ // Update status as well
+ $executed = SURFBAR_CHANGE_STATUS($urlData['id'], $urlData['status'], $urlData['new_status'], array($urlData['id'] => $urlData));
+ } // END - if
+ } else {
+ // Not found!
+ addFatalMessage(__FUNCTION__, __LINE__, getMessage('MEMBER_SURFBAR_EXECUTE_ACTION_404'), $functionName);
+ }
+
+ // Return status
+ return $executed;
+}
+// "Execute edit" function: Update changed data
+function SURFBAR_MEMBER_EXECUTE_EDIT_ACTION ($urlData) {
+ // Default is nothing done
+ $status = false;
+
+ // Has the URL or limit changed?
+ if (true) {
+ //if (($urlData['views_allowed'] != $urlData['edit']['limit']) || ($url1 != $url2)) {
+ // Run the query
+ SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_surfbar_urls` SET url='%s', views_allowed=%s, views_max=%s WHERE `id`=%s AND `status`='%s' LIMIT 1",
+ array($urlData['url'], $urlData['edit']['limit'], $urlData['edit']['limit'], $urlData['id'], $urlData['status']), __FUNCTION__, __LINE__);
+
+ // All fine
+ $status = true;
+ } // END - if
+
+ // Return status
+ return $status;
+}
+// "Execute delete" function: Does nothing...
+function SURFBAR_MEMBER_EXECUTE_DELETE_ACTION ($urlData) {
+ // Nothing special to do (see above function for such "special actions" to perform)
+ return true;
+}
+//
+// -----------------------------------------------------------------------------
+// Self-maintenance functions
+// -----------------------------------------------------------------------------
+//
+// Main function
+function SURFBAR_HANDLE_SELF_MAINTENANCE () {
+ // Handle URLs which limit has depleted so we can stop them
+ SURFBAR_HANDLE_DEPLETED_VIEWS();
+
+ // Handle low-points amounts
+ SURFBAR_HANDLE_LOW_POINTS();
+}
+// Handle URLs which limit has depleted
+function SURFBAR_HANDLE_DEPLETED_VIEWS () {
+ // Get all URLs
+ $urlArray = SURFBAR_GET_URL_DATA(0, 'views_max', 'id', 'ASC', 'id', " AND views_allowed>0 AND `status`='ACTIVE'");
+
+ // Do we have some entries?
+ if (count($urlArray) > 0) {
+ // Then handle all!
+ foreach ($urlArray as $id => $urlData) {
+ // Backup data
+ $data = $urlData;
+
+ // Rewrite array for next call
+ $urlData[$id] = $data;
+
+ // Handle the status
+ SURFBAR_CHANGE_STATUS($id, 'ACTIVE', 'DEPLETED', $urlData);
+ } // END - foreach
+ } // END - if
+}
+
+// Alert users which have URLs booked and are low on points amount
+function SURFBAR_HANDLE_LOW_POINTS () {
+ // Get all userids
+ $userids = SURFBAR_DETERMINE_DEPLETED_USERIDS(getConfig('surfbar_warn_low_points'));
+
+ // "Walk" through all URLs
+ foreach ($userids['userid'] as $userid => $dummy) {
+ // Is the last notification far enougth away to notify again?
+ if ((time() - $userids['notified'][$userid]) >= getConfig('surfbar_low_interval')) {
+ // Prepare content
+ $content = array(
+ 'userid' => $userid,
+ 'low' => translateComma(getConfig('surfbar_warn_low_points')),
+ 'points' => translateComma($userids['points'][$userid]),
+ 'notified' => generateDateTime($userids['notified'][$userid]),
+ 'interval' => createFancyTime(getConfig('surfbar_low_interval'))
+ );
+
+ // Notify this user
+ SURFBAR_NOTIFY_USER('low_points', $content);
+
+ // Update last notified
+ SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET surfbar_low_notified=NOW() WHERE `userid`=%s LIMIT 1",
+ array($userid), __FUNCTION__, __LINE__);
+ } // END - if
+ } // END - foreach
+}
+
+//