function wbz404_adminPage() { $sub = ""; $message = ""; //Handle Post Actions if (isset($_POST['action'])) { $action = $_POST['action']; } else { $action = ""; } if ($action == "updateOptions") { if (check_admin_referer('wbz404UpdateOptions') && is_admin()) { $sub = "wbz404_options"; $message = wbz404_updateOptions(); if ($message == "") { $message = wbz404_trans('Options Saved Successfully!'); } else { $message .= wbz404_trans('Some options were not saved successfully.'); } } } else { if ($action == "addRedirect") { if (check_admin_referer('wbz404addRedirect') && is_admin()) { $message = wbz404_addAdminRedirect(); if ($message == "") { $message = wbz404_trans('New Redirect Added Successfully!'); } else { $message .= wbz404_trans('Error: unable to add new redirect successfully.'); } } } else { if ($action == "emptyRedirectTrash") { if (check_admin_referer('wbz404_emptyRedirectTrash') && is_admin()) { wbz404_emptyTrash('redirects'); $message = wbz404_trans('All trashed URLs have been deleted!'); } } else { if ($action == "emptyCapturedTrash") { if (check_admin_referer('wbz404_emptyCapturedTrash') && is_admin()) { wbz404_emptyTrash('captured'); $message = wbz404_trans('All trashed URLs have been deleted!'); } } else { if ($action == "bulkignore" || $action == "bulkcaptured" || $action == "bulktrash") { if (check_admin_referer('wbz404_capturedBulkAction') && is_admin()) { $message = wbz404_bulkProcess($action, $_POST['idnum']); } } else { if ($action == "purgeRedirects") { if (check_admin_referer('wbz404_purgeRedirects') && is_admin()) { $message = wbz404_purgeRedirects(); } } } } } } } // Handle Trash Functionality if (isset($_GET['trash'])) { if (check_admin_referer('wbz404_trashRedirect') && is_admin()) { $trash = ""; if ($_GET['trash'] == 0) { $trash = 0; } else { if ($_GET['trash'] == 1) { $trash = 1; } } if ($trash == 0 || $trash == 1) { $message = wbz404_setTrash($_GET['id'], $trash); if ($message == "") { if ($trash == 1) { $message = wbz404_trans("Redirect moved to trash successfully!"); } else { $message = wbz404_trans("Redirect restored from trash successfully!"); } } else { if ($trash == 1) { $message = wbz404_trans("Error: Unable to move redirect to trash."); } else { $message = wbz404_trans("Error: Unable to move redirect from trash."); } } } } } //Handle Delete Functionality if (isset($_GET['remove']) && $_GET['remove'] == 1) { if (check_admin_referer('wbz404_removeRedirect') && is_admin()) { if (preg_match('/[0-9]+/', $_GET['id'])) { wbz404_cleanRedirect($_GET['id']); $message = wbz404_trans('Redirect Removed Successfully!'); } } } //Handle Ignore Functionality if (isset($_GET['ignore'])) { if (check_admin_referer('wbz404_ignore404') && is_admin()) { if ($_GET['ignore'] == 0 || $_GET['ignore'] == 1) { if (preg_match('/[0-9]+/', $_GET['id'])) { if ($_GET['ignore'] == 1) { $newstatus = WBZ404_IGNORED; } else { $newstatus = WBZ404_CAPTURED; } $message = wbz404_setIgnore($_GET['id'], $newstatus); if ($message == "") { if ($newstatus == WBZ404_CAPTURED) { $message = wbz404_trans('Removed 404 URL from ignored list successfully!'); } else { $message = wbz404_trans('404 URL marked as ignored successfully!'); } } else { if ($newstatus == WBZ404_CAPTURED) { $message = wbz404_trans('Error: unable to remove URL from ignored list'); } else { $message = wbz404_trans('Error: unable to mark URL as ignored'); } } } } } } //Handle edit posts if (isset($_POST['action']) && $_POST['action'] == "editRedirect") { if (isset($_POST['id']) && preg_match('/[0-9]+/', $_POST['id'])) { if (check_admin_referer('wbz404editRedirect') && is_admin()) { $message = wbz404_editRedirectData(); if ($message == "") { $message .= wbz404_trans("Redirect Information Updated Successfully!"); $sub = "redirects"; } else { $message .= wbz404_trans("Error: Unable to update redirect data."); } } } } // Deal With Page Tabs if ($sub == "") { if (isset($_GET['subpage'])) { $sub = strtolower($_GET['subpage']); } else { $sub = ""; } } if ($sub == "wbz404_options") { $sub = "options"; } else { if ($sub == "wbz404_captured") { $sub = "captured"; } else { if ($sub == "wbz404_logs") { $sub = "logs"; } else { if ($sub == "wbz404_edit") { $sub = "edit"; } else { if ($sub == "wbz404_stats") { $sub = "stats"; } else { if ($sub == "wbz404_tools") { $sub = "tools"; } else { $sub = "redirects"; } } } } } } wbz404_adminHeader($sub, $message); if ($sub == "redirects") { wbz404_adminRedirectsPage(); } else { if ($sub == "captured") { wbz404_adminCapturedPage(); } else { if ($sub == "options") { wbz404_adminOptionsPage(); } else { if ($sub == "logs") { wbz404_adminLogsPage(); } else { if ($sub == "edit") { wbz404_adminEditPage(); } else { if ($sub == "stats") { wbz404_adminStatsPage(); } else { if ($sub == "tools") { wbz404_adminToolsPage(); } else { echo wbz404_trans('Invalid Sub Page ID'); } } } } } } } wbz404_adminFooter(); }
function wbz404_process404() { $options = wbz404_getOptions(); $urlRequest = $_SERVER['REQUEST_URI']; $urlParts = parse_url($urlRequest); $requestedURL = $urlParts['path']; $requestedURL .= wbz404_SortQuery($urlParts); //Get URL data if it's already in our database $redirect = wbz404_loadRedirectData($requestedURL); if (is_404() && $requestedURL != "") { if ($redirect['id'] != '0') { //A redirect record exists. wbz404_ProcessRedirect($redirect); } else { //No redirect record. $found = 0; if ($options['auto_redirects'] == '1') { //Site owner wants automatic redirects $permalinks = wbz404_rankPermalinks($requestedURL, $options['auto_cats'], $options['auto_tags']); $minScore = $options['auto_score']; foreach ($permalinks as $k => $v) { $permalink = wbz404_permalinkInfo($k, $v); if ($permalink['score'] >= $minScore) { $found = 1; break; } else { //Score not high enough break; } } if ($found == 1) { //We found a permalink that will work! $type = 0; if ($permalink['type'] == "POST") { $type = WBZ404_POST; } else { if ($permalink['type'] == "CAT") { $type = WBZ404_CAT; } else { if ($permalink['type'] == "TAG") { $type = WBZ404_TAG; } } } if ($type != 0) { $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_AUTO, $type, $permalink['id'], $options['default_redirect'], 0); } } } if ($found == 1) { //Perform actual redirect wbz404_logRedirectHit($redirect_id, $permalink['link']); wp_redirect($permalink['link'], $options['default_redirect']); exit; } else { //Check for incoming 404 settings if ($options['capture_404'] == '1') { $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_CAPTURED, 0, 0, $options['default_redirect'], 0); wbz404_logRedirectHit($redirect_id, '404'); } } } } else { if (is_single() || is_page()) { if (!is_feed() && !is_trackback() && !is_preview()) { $theID = get_the_ID(); $permalink = wbz404_permalinkInfo($theID . "|POST", 0); $urlParts = parse_url($permalink['link']); $perma_link = $urlParts['path']; $paged = get_query_var('page') ? get_query_var('page') : FALSE; if (!$paged === FALSE) { if ($urlParts[query] == "") { if (substr($perma_link, -1) == "/") { $perma_link .= $paged . "/"; } else { $perma_link .= "/" . $paged; } } else { $urlParts['query'] .= "&page=" . $paged; } } $perma_link .= wbz404_SortQuery($urlParts); //Check for forced permalinks if ($options['force_permalinks'] == '1' && $options['auto_redirects'] == '1') { if ($requestedURL != $perma_link) { if ($redirect['id'] != '0') { wbz404_ProcessRedirect($redirect); } else { $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_AUTO, WBZ404_POST, $permalink['id'], $options['default_redirect'], 0); wbz404_logRedirectHit($redirect_id, $permalink['link']); wp_redirect($permalink['link'], $options['default_redirect']); exit; } } } if ($requestedURL == $perma_link) { //Not a 404 Link. Check for matches if ($options['remove_matches'] == '1') { if ($redirect['id'] != '0') { wbz404_cleanRedirect($redirect['id']); } } } } } } }
function wbz404_cleaningCron() { global $wpdb; $options = wbz404_getOptions(); $now = time(); //Remove Captured URLs if ($options['capture_deletion'] != '0') { $capture_time = $options['capture_deletion'] * 86400; $then = $now - $capture_time; //Clean up old logs $query = "delete from " . $wpdb->prefix . "wbz404_logs where "; $query .= "redirect_id in (select id from " . $wpdb->prefix . "wbz404_redirects where status = " . $wpdb->escape(WBZ404_CAPTURED) . " or status = " . $wpdb->escape(WBZ404_IGNORED) . ") "; $query .= "and timestamp < " . $wpdb->escape($then); $wpdb->query($query); //Find unused urls $query = "select id from " . $wpdb->prefix . "wbz404_redirects where (status = " . $wpdb->escape(WBZ404_CAPTURED) . " or status = " . $wpdb->escape(WBZ404_IGNORED) . ") and "; $query .= "timestamp <= " . $wpdb->escape($then) . " and id not in ("; $query .= "select redirect_id from " . $wpdb->prefix . "wbz404_logs"; $query .= ")"; $rows = $wpdb->get_results($query, ARRAY_A); foreach ($rows as $row) { //Remove Them wbz404_cleanRedirect($row['id']); } } //Remove Automatic Redirects if ($options['auto_deletion'] != '0') { $auto_time = $options['auto_deletion'] * 86400; $then = $now - $auto_time; //Clean up old logs $query = "delete from " . $wpdb->prefix . "wbz404_logs where "; $query .= "redirect_id in (select id from " . $wpdb->prefix . "wbz404_redirects where status = " . $wpdb->escape(WBZ404_AUTO) . ") "; $query .= "and timestamp < " . $wpdb->escape($then); $wpdb->query($query); //Find unused urls $query = "select id from " . $wpdb->prefix . "wbz404_redirects where status = " . $wpdb->escape(WBZ404_AUTO) . " and "; $query .= "timestamp <= " . $wpdb->escape($then) . " and id not in ("; $query .= "select redirect_id from " . $wpdb->prefix . "wbz404_logs"; $query .= ")"; $rows = $wpdb->get_results($query, ARRAY_A); foreach ($rows as $row) { //Remove Them wbz404_cleanRedirect($row['id']); } } //Remove Manual Redirects if ($options['manual_deletion'] != '0') { $manual_time = $options['manual_deletion'] * 86400; $then = $now - $manual_time; //Clean up old logs $query = "delete from " . $wpdb->prefix . "wbz404_logs where "; $query .= "redirect_id in (select id from " . $wpdb->prefix . "wbz404_redirects where status = " . $wpdb->escape(WBZ404_MANUAL) . ") "; $query .= "and timestamp < " . $wpdb->escape($then); $wpdb->query($query); //Find unused urls $query = "select id from " . $wpdb->prefix . "wbz404_redirects where status = " . $wpdb->escape(WBZ404_MANUAL) . " and "; $query .= "timestamp <= " . $wpdb->escape($then) . " and id not in ("; $query .= "select redirect_id from " . $wpdb->prefix . "wbz404_logs"; $query .= ")"; $rows = $wpdb->get_results($query, ARRAY_A); foreach ($rows as $row) { //Remove Them wbz404_cleanRedirect($row['id']); } } }
function wbz404_process404() { $options = wbz404_getOptions(); $urlRequest = $_SERVER['REQUEST_URI']; $urlParts = parse_url($urlRequest); $requestedURL = $urlParts['path']; $queryArgs = wbz404_SortQuery($urlParts); /* The only case where we want the query string to be considered part of the URL we are processing * is when it contains 'page_id'. In all other cases, we want to strip the query args -- they are typically * the utm labels in those other situations, which we want to ignore. */ if (strpos($queryArgs, 'page_id') !== false) { $requestedURL .= $queryArgs; } //Get URL data if it's already in our database $redirect = wbz404_loadRedirectData($requestedURL); if (is_404() && $requestedURL != "") { if ($redirect['id'] != '0') { //A redirect record exists. wbz404_ProcessRedirect($redirect); } else { //No redirect record. $found = 0; if ($options['auto_redirects'] == '1') { //Site owner wants automatic redirects $permalinks = wbz404_rankPermalinks($requestedURL, $options['auto_cats'], $options['auto_tags']); $minScore = $options['auto_score']; foreach ($permalinks as $k => $v) { $permalink = wbz404_permalinkInfo($k, $v); if ($permalink['score'] >= $minScore) { $found = 1; break; } else { //Score not high enough break; } } if ($found == 1) { //We found a permalink that will work! $type = 0; if ($permalink['type'] == "POST") { $type = WBZ404_POST; } else { if ($permalink['type'] == "CAT") { $type = WBZ404_CAT; } else { if ($permalink['type'] == "TAG") { $type = WBZ404_TAG; } } } if ($type != 0) { $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_AUTO, $type, $permalink['id'], $options['default_redirect'], 0); } } } if ($found == 1) { //Perform actual redirect wbz404_logRedirectHit($redirect_id, $permalink['link']); wp_redirect($permalink['link'], $options['default_redirect']); exit; } else { /* Workaround for the issue where bbPress user profile pages would be * marked as 404 by the function handle_404() in the Wordpress core. */ $forums_user_pattern = '/\\/forums\\/users\\//'; if (preg_match($forums_user_pattern, $requestedURL) && function_exists('bbpress')) { $bbp = bbpress(); if (!empty($bbp->displayed_user)) { return; } } /* Redirect requests to non-existent category pages to the first page URL. */ $paged_pattern = '/\\/page\\/\\d\\d?\\/?$/'; if (preg_match($paged_pattern, $requestedURL)) { $target_url = preg_replace($paged_pattern, '/', $requestedURL); error_log("Redirecting {$requestedURL} to {$target_url} in " . __FILE__); wp_redirect($target_url, 302); } //Check for incoming 404 settings if ($options['capture_404'] == '1') { $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_CAPTURED, 0, 0, $options['default_redirect'], 0); wbz404_logRedirectHit($redirect_id, '404'); } } } } else { if (is_single() || is_page()) { if (!is_feed() && !is_trackback() && !is_preview()) { $theID = get_the_ID(); $permalink = wbz404_permalinkInfo($theID . "|POST", 0); $urlParts = parse_url($permalink['link']); $perma_link = $urlParts['path']; $paged = get_query_var('page') ? get_query_var('page') : false; if (!$paged === false) { if ($urlParts[query] == "") { if (substr($perma_link, -1) == "/") { $perma_link .= $paged . "/"; } else { $perma_link .= "/" . $paged; } } else { $urlParts['query'] .= "&page=" . $paged; } } $perma_link .= wbz404_SortQuery($urlParts); //Check for forced permalinks if ($options['force_permalinks'] == '1' && $options['auto_redirects'] == '1') { if ($requestedURL != $perma_link) { if ($redirect['id'] != '0') { wbz404_ProcessRedirect($redirect); } else { $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_AUTO, WBZ404_POST, $permalink['id'], $options['default_redirect'], 0); wbz404_logRedirectHit($redirect_id, $permalink['link']); wp_redirect($permalink['link'], $options['default_redirect']); exit; } } } if ($requestedURL == $perma_link) { //Not a 404 Link. Check for matches if ($options['remove_matches'] == '1') { if ($redirect['id'] != '0') { wbz404_cleanRedirect($redirect['id']); } } } } } } }