Beispiel #1
0
function wbz404_adminCapturedPage()
{
    $sub = "captured";
    $options = wbz404_getOptions();
    $tableOptions = wbz404_getTableOptions();
    wbz404_drawFilters($sub, $tableOptions);
    $columns['url']['title'] = "URL";
    $columns['url']['orderby'] = "url";
    $columns['url']['width'] = "50%";
    $columns['hits']['title'] = "Hits";
    $columns['hits']['orderby'] = "hits";
    $columns['hits']['width'] = "10%";
    $columns['timestamp']['title'] = "Created";
    $columns['timestamp']['orderby'] = "timestamp";
    $columns['timestamp']['width'] = "20%";
    $columns['last_used']['title'] = "Last Used";
    $columns['last_used']['orderby'] = "";
    $columns['last_used']['width'] = "20%";
    date_default_timezone_set(get_option('timezone_string'));
    echo "<div class=\"tablenav\">";
    wbz404_drawPaginationLinks($sub, $tableOptions);
    if ($tableOptions['filter'] == '-1') {
        echo "<div class=\"alignleft actions\">";
        $eturl = "?page=wbz404_redirected&subpage=wbz404_captured&filter=-1";
        $trashaction = "wbz404_emptyCapturedTrash";
        $eturl = wp_nonce_url($eturl, $trashaction);
        echo "<form method=\"POST\" action=\"" . $eturl . "\">";
        echo "<input type=\"hidden\" name=\"action\" value=\"emptyCapturedTrash\">";
        echo "<input type=\"submit\" class=\"button-secondary\" value=\"" . wbz404_trans('Empty Trash') . "\">";
        echo "</form>";
        echo "</div>";
    } else {
        echo "<div class=\"alignleft actions\">";
        $url = "?page=wbz404_redirected&subpage=wbz404_captured";
        if ($tableOptions['filter'] != 0) {
            $url .= "&filter=" . $tableOptions['filter'];
        }
        if (!($tableOptions['orderby'] == "url" && $tableOptions['order'] == "ASC")) {
            $url .= "&orderby=" . $tableOptions['orderby'] . "&order=" . $tableOptions['order'];
        }
        $bulkaction = "wbz404_capturedBulkAction";
        $url = wp_nonce_url($url, $bulkaction);
        echo "<form method=\"POST\" action=\"" . $url . "\">";
        echo "<select name=\"action\">";
        if ($tableOptions['filter'] != WBZ404_IGNORED) {
            echo "<option value=\"bulkignore\">" . wbz404_trans('Mark as ignored') . "</option>";
        } else {
            echo "<option value=\"bulkcaptured\">" . wbz404_trans('Mark as captured') . "</option>";
        }
        echo "<option value=\"bulktrash\">" . wbz404_trans('Trash') . "</option>";
        echo "</select>";
        echo "<input type=\"submit\" class=\"button-secondary\" value=\"" . wbz404_trans('Apply') . "\">";
        echo "</div>";
    }
    echo "</div>";
    echo "<table class=\"wp-list-table widefat fixed\">";
    echo "<thead>";
    wbz404_buildTableColumns($sub, $tableOptions, $columns);
    echo "</thead>";
    echo "<tfoot>";
    wbz404_buildTableColumns($sub, $tableOptions, $columns);
    echo "</tfoot>";
    echo "<tbody id=\"the-list\">";
    $rows = wbz404_getRecords($sub, $tableOptions);
    $displayed = 0;
    $y = 1;
    foreach ($rows as $row) {
        $displayed++;
        $hits = $row['hits'];
        $last_used = wbz404_getRedirectLastUsed($row['id']);
        if ($last_used != 0) {
            $last = date("Y/m/d h:i:s A", $last_used);
        } else {
            $last = wbz404_trans('Never Used');
        }
        $editlink = "?page=wbz404_redirected&subpage=wbz404_edit&id=" . $row['id'];
        $logslink = "?page=wbz404_redirected&subpage=wbz404_logs&id=" . $row['id'];
        $trashlink = "?page=wbz404_redirected&&subpage=wbz404_captured&id=" . $row['id'];
        $ignorelink = "?page=wbz404_redirected&&subpage=wbz404_captured&id=" . $row['id'];
        $deletelink = "?page=wbz404_redirected&subpage=wbz404_captured&remove=1&id=" . $row['id'];
        if ($tableOptions['filter'] == -1) {
            $trashlink .= "&trash=0";
            $trashtitle = wbz404_trans('Restore');
        } else {
            $trashlink .= "&trash=1";
            $trashtitle = wbz404_trans('Trash');
        }
        if ($tableOptions['filter'] == WBZ404_IGNORED) {
            $ignorelink .= "&ignore=0";
            $ignoretitle = wbz404_trans('Remove Ignore Status');
        } else {
            $ignorelink .= "&ignore=1";
            $ignoretitle = wbz404_trans('Ignore 404 Error');
        }
        if (!($tableOptions['orderby'] == "url" && $tableOptions['order'] == "ASC")) {
            $trashlink .= "&orderby=" . $tableOptions['orderby'] . "&order=" . $tableOptions['order'];
            $ignorelink .= "&orderby=" . $tableOptions['orderby'] . "&order=" . $tableOptions['order'];
            $deletelink .= "&orderby=" . $tableOptions['orderby'] . "&order=" . $tableOptions['order'];
        }
        if ($tableOptions['filter'] != 0) {
            $trashlink .= "&filter=" . $tableOptions['filter'];
            $ignorelink .= "&filter=" . $tableOptions['filter'];
            $deletelink .= "&filter=" . $tableOptions['filter'];
        }
        $trashaction = "wbz404_trashRedirect";
        $trashlink = wp_nonce_url($trashlink, $trashaction);
        if ($tableOptions['filter'] == -1) {
            $deleteaction = "wbz404_removeRedirect";
            $deletelink = wp_nonce_url($deletelink, $deleteaction);
        }
        $ignoreaction = "wbz404_ignore404";
        $ignorelink = wp_nonce_url($ignorelink, $ignoreaction);
        $class = "";
        if ($y == 0) {
            $class = " class=\"alternate\"";
            $y++;
        } else {
            $y = 0;
        }
        echo "<tr id=\"post-" . $row['id'] . "\"" . $class . ">";
        echo "<th class=\"check-column\">";
        if ($tableOptions['filter'] != '-1') {
            echo "<input type=\"checkbox\" name=\"idnum[]\" value=\"" . $row['id'] . "\">";
        }
        echo "</th>";
        echo "<td>";
        echo "<strong><a href=\"" . $editlink . "\" title=\"" . wbz404_trans('Edit Redirect Details') . "\">" . $row['url'] . "</a></strong>";
        echo "<div class=\"row-actions\">";
        if ($tableOptions['filter'] != -1) {
            echo "<span class=\"edit\"><a href=\"" . $editlink . "\" title=\"" . wbz404_trans('Edit Redirect Details') . "\">" . wbz404_trans('Edit') . "</a></span>";
            echo " | ";
        }
        echo "<span class=\"trash\"><a href=\"" . $trashlink . "\" title=\"" . wbz404_trans('Trash Redirected URL') . "\">" . $trashtitle . "</a></span>";
        echo " | ";
        echo "<span class=\"view\"><a href=\"" . $logslink . "\" title=\"" . wbz404_trans('View Redirect Logs') . "\">" . wbz404_trans('View Logs') . "</a></span>";
        if ($tableOptions['filter'] != -1) {
            echo " | ";
            echo "<span class=\"ignore\"><a href=\"" . $ignorelink . "\" title=\"" . $ignoretitle . "\">" . $ignoretitle . "</a></span>";
        } else {
            echo " | ";
            echo "<span class=\"delete\"><a href=\"" . $deletelink . "\" title=\"" . wbz404_trans('Delete Redirect Permanently') . "\">" . wbz404_trans('Delete Permanently') . "</a></span>";
        }
        echo "</div>";
        echo "</td>";
        echo "<td>" . $hits . "</td>";
        echo "<td>" . date("Y/m/d h:i:s A", $row['timestamp']) . "</td>";
        echo "<td>" . $last . "</td>";
        echo "<td></td>";
        echo "</tr>";
    }
    if ($displayed == 0) {
        echo "<tr>";
        echo "<td></td>";
        echo "<td colspan=\"8\" style=\"text-align: center; font-weight: bold;\">" . wbz404_trans('No Records To Display') . "</td>";
        echo "<td></td>";
        echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";
    echo "<div class=\"tablenav\">";
    if ($tableOptions['filter'] != '-1') {
        echo "</form>";
    }
    wbz404_drawPaginationLinks($sub, $tableOptions);
    echo "</div>";
}
Beispiel #2
0
function wbz404_redirectCanonical($redirect, $request)
{
    if (is_single() || is_page()) {
        if (!is_feed() && !is_trackback() && !is_preview()) {
            $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
            $data = wbz404_loadRedirectData($requestedURL);
            if ($data['id'] != '0') {
                wbz404_ProcessRedirect($data);
            } else {
                if ($options['auto_redirects'] == '1' && $options['force_permalinks'] == '1') {
                    $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);
                    if ($requestedURL != $perma_link) {
                        $redirect_id = wbz404_setupRedirect($requestedURL, WBZ404_AUTO, WBZ404_POST, $theID, $options['default_redirect'], 0);
                        wbz404_logRedirectHit($redirect_id, $perma_link);
                        wp_redirect($perma_link, $options['default_redirect']);
                        exit;
                    }
                }
            }
        }
    }
    if (is_404()) {
        return false;
    }
    return $redirect;
}
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']);
                        }
                    }
                }
            }
        }
    }
}