8804f524 by Jeff Balicki

dd

1 parent 80f12c8d
Showing 171 changed files with 3562 additions and 0 deletions
# Redirection
[![Build Status](https://travis-ci.org/johngodley/redirection.svg?branch=master)](https://travis-ci.org/johngodley/redirection)
Redirection is a WordPress plugin to manage 301 redirections, keep track of 404 errors, and generally tidy up any loose ends your site may have. This is particularly useful if you are migrating pages from an old website, or are changing the directory of your WordPress installation.
Note: this is the current 'trunk' version of Redirection. It may be newer than what is in the WordPress.org plugin repository, and should be considered experimental.
## Installation
Redirection can be installed by visiting the WordPress.org plugin page:
https://wordpress.org/plugins/redirection/
## Customisation
### Request Information
The following WordPress filters are available for customisation of a server requests:
- `redirection_request_url` - The request URL
- `redirection_request_agent` - The request user agent
- `redirection_request_referrer` - The request referrer
- `redirection_request_ip` - The request IP address
### Logging
The following WordPress filters are available for customisation of logged data:
- `redirection_404_data` - Data to be inserted into the 404 table
- `redirection_log_data` - Data to be inserted into the redirect log table
### Redirect source and target
- `redirection_url_source` - The original URL used before matching a request. Return false to stop any redirection
- `redirection_url_target` - The target URL after a request has been matched (and after any regular expression captures have been replaced). Return false to stop any redirection
### Dynamic URL data
The following special words can be inserted into a target URL:
- `%userid%` - Insert user's ID
- `%userlogin%` - Insert user's login name
- `%userurl%` - Insert user's custom URL
### Management
- `redirection_permalink_changed` - return boolean if a post's permalink has changed
- `redirection_remove_existing` - fired when a post changes permalink and we need to clear existing redirects that might affect it
Additionally, if the target URL is a number without any slashes then Redirection will treat it as a post ID and redirect to the full URL for that post.
## Support
Please raise any bug reports or enhancement requests here. Pull requests are always welcome.
You can find a more detailed description of the plugin on the [Redirection home page](http://urbangiraffe.com/plugins/redirection/)
Translations can be added here:
https://translate.wordpress.org/projects/wp-plugins/redirection
<?php
/**
* Return an error to the client, and trigger the WordPress error page
*/
class Error_Action extends Red_Action {
/**
* Set WordPress to show the error page
*
* @return void
*/
public function run() {
wp_reset_query();
// Set the query to be a 404
set_query_var( 'is_404', true );
// Return the 404 page
add_filter( 'template_include', [ $this, 'template_include' ] );
// Clear any posts if this is actually a valid URL
add_filter( 'pre_handle_404', [ $this, 'pre_handle_404' ] );
// Ensure the appropriate http code is returned
add_action( 'wp', [ $this, 'wp' ] );
}
/**
* Output selected HTTP code, as well as redirection header
*
* @return void
*/
public function wp() {
status_header( $this->code );
nocache_headers();
global $wp_version;
if ( version_compare( $wp_version, '5.1', '<' ) ) {
header( 'X-Redirect-Agent: redirection' );
} else {
header( 'X-Redirect-By: redirection' );
}
}
public function pre_handle_404() {
global $wp_query;
// Page comments plugin interferes with this
$wp_query->posts = [];
return false;
}
public function template_include() {
return get_404_template();
}
public function name() {
return __( 'Error (404)', 'redirection' );
}
}
<?php
/**
* The 'do nothing' action. This really does nothing, and is used to short-circuit Redirection so that it doesn't trigger other redirects.
*/
class Nothing_Action extends Red_Action {
/**
* Issue an action when nothing happens. This stops further processing.
*
* @return void
*/
public function run() {
do_action( 'redirection_do_nothing', $this->get_target() );
}
public function name() {
return __( 'Do nothing (ignore)', 'redirection' );
}
}
<?php
require_once dirname( __FILE__ ) . '/url.php';
/**
* A 'pass through' action. Matches a rewrite rather than a redirect, and uses PHP to fetch data from a remote URL.
*/
class Pass_Action extends Url_Action {
/**
* Process an external passthrough - a URL that lives external to this server.
*
* @param String $url Target URL.
* @return void
*/
public function process_external( $url ) {
// This is entirely at the user's risk. The $url is set by the user
// phpcs:ignore
echo wp_remote_fopen( $url );
}
/**
* Process an internal passthrough - a URL that lives on the same server. Here we change the request URI and continue without making a remote request.
*
* @param String $target Target URL.
* @return void
*/
public function process_internal( $target ) {
// Another URL on the server
$pos = strpos( $target, '?' );
$_SERVER['REQUEST_URI'] = $target;
$_SERVER['PATH_INFO'] = $target;
if ( $pos ) {
$_SERVER['QUERY_STRING'] = substr( $target, $pos + 1 );
$_SERVER['PATH_INFO'] = $target;
parse_str( $_SERVER['QUERY_STRING'], $_GET );
}
}
/**
* Is a URL external?
*
* @param String $target URL to test.
* @return boolean
*/
public function is_external( $target ) {
return substr( $target, 0, 7 ) === 'http://' || substr( $target, 0, 8 ) === 'https://';
}
/**
* Pass the data from the target
*
* @return void
*/
public function run() {
// External target
$target = $this->get_target();
if ( $target === null ) {
return;
}
if ( $this->is_external( $target ) ) {
// Pass on to an external request, echo the results, and then stop
$this->process_external( $target );
exit();
}
// Change the request and carry on
$this->process_internal( $target );
}
public function name() {
return __( 'Pass-through', 'redirection' );
}
}
<?php
require_once dirname( __FILE__ ) . '/url.php';
/**
* URL action - redirect to a URL
*/
class Random_Action extends Url_Action {
/**
* Get a random URL
*
* @return string|null
*/
private function get_random_url() {
// Pick a random WordPress page
global $wpdb;
$id = $wpdb->get_var( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_status='publish' AND post_password='' AND post_type='post' ORDER BY RAND() LIMIT 0,1" );
if ( $id ) {
$url = get_permalink( $id );
if ( $url ) {
return $url;
}
}
return null;
}
/**
* Run this action. May not return from this function.
*
* @return void
*/
public function run() {
$target = $this->get_random_url();
if ( $target ) {
$this->redirect_to( $target );
}
}
public function needs_target() {
return false;
}
public function name() {
return __( 'Redirect to random post', 'redirection' );
}
}
<?php
/**
* URL action - redirect to a URL
*/
class Url_Action extends Red_Action {
/**
* Redirect to a URL
*
* @param string $target Target URL.
* @return void
*/
protected function redirect_to( $target ) {
// This is a known redirect, possibly extenal
// phpcs:ignore
$redirect = wp_redirect( $target, $this->get_code(), 'redirection' );
if ( $redirect ) {
/** @psalm-suppress InvalidGlobal */
global $wp_version;
if ( version_compare( $wp_version, '5.1', '<' ) ) {
header( 'X-Redirect-Agent: redirection' );
}
die();
}
}
/**
* Run this action. May not return from this function.
*
* @return void
*/
public function run() {
$target = $this->get_target();
if ( $target !== null ) {
$this->redirect_to( $target );
}
}
/**
* Does this action need a target?
*
* @return boolean
*/
public function needs_target() {
return true;
}
public function name() {
return __( 'Redirect to URL', 'redirection' );
}
}
<?php
/**
* @api {get} /redirection/v1/404 Get 404 logs
* @apiName GetLogs
* @apiDescription Get a paged list of 404 logs after applying a set of filters and result ordering.
* @apiGroup 404
*
* @apiUse 404QueryParams
*
* @apiUse 404List
* @apiUse 401Error
* @apiUse 404Error
*/
/**
* @api {post} /redirection/v1/bulk/404/:type Bulk action
* @apiName BulkAction
* @apiDescription Delete 404 logs by ID
* @apiGroup 404
*
* @apiParam (URL) {String="delete"} :type Type of bulk action that is applied to every log ID.
*
* @apiParam (Query Parameter) {String[]} [items] Array of group IDs to perform the action on
* @apiParam (Query Parameter) {Boolean=false} [global] Perform action globally using the filter parameters
* @apiUse 404QueryParams
*
* @apiUse 404List
* @apiUse 401Error
* @apiUse 404Error
* @apiUse 400MissingError
*/
/**
* @apiDefine 404QueryParams 404 log query parameters
*
* @apiParam (Query Parameter) {String} [filterBy[ip]] Filter the results by the supplied IP
* @apiParam (Query Parameter) {String} [filterBy[url]] Filter the results by the supplied URL
* @apiParam (Query Parameter) {String} [filterBy[url-]exact] Filter the results by the exact URL (not a substring match, as per `url`)
* @apiParam (Query Parameter) {String} [filterBy[referrer]] Filter the results by the supplied referrer
* @apiParam (Query Parameter) {String} [filterBy[agent]] Filter the results by the supplied user agent
* @apiParam (Query Parameter) {String} [filterBy[target]] Filter the results by the supplied redirect target
* @apiParam (Query Parameter) {String} [filterBy[domain]] Filter the results by the supplied domain name
* @apiParam (Query Parameter) {String="head","get","post"} [filterBy[method]] Filter the results by the supplied HTTP request method
* @apiParam (Query Parameter) {Integer} [filterBy[http]] Filter the results by the supplied redirect HTTP code
* @apiParam (Query Parameter) {string="ip","url"} [orderby] Order by IP or URL
* @apiParam (Query Parameter) {String="asc","desc"} [direction] Direction to order the results by (ascending or descending)
* @apiParam (Query Parameter) {Integer{1...200}} [per_page=25] Number of results per request
* @apiParam (Query Parameter) {Integer} [page=0] Current page of results
* @apiParam (Query Parameter) {String="ip","url"} [groupBy] Group by IP or URL
*/
/**
* @apiDefine 404List
*
* @apiSuccess {Object[]} items Array of 404 log objects
* @apiSuccess {Integer} items.id ID of 404 log entry
* @apiSuccess {String} items.created Date the 404 log entry was recorded
* @apiSuccess {Integer} items.created_time Unix time value for `created`
* @apiSuccess {Integer} items.url The requested URL that caused the 404 log entry
* @apiSuccess {String} items.agent User agent of the client initiating the request
* @apiSuccess {Integer} items.referrer Referrer of the client initiating the request
* @apiSuccess {Integer} total Number of items
*
* @apiSuccessExample {json} Success 200:
* HTTP/1.1 200 OK
* {
* "items": [
* {
* "id": 3,
* "created": "2019-01-01 12:12:00,
* "created_time": "12345678",
* "url": "/the-url",
* "agent": "FancyBrowser",
* "referrer": "http://site.com/previous/,
* }
* ],
* "total": 1
* }
*/
/**
* 404 API endpoint
*/
class Redirection_Api_404 extends Redirection_Api_Filter_Route {
/**
* 404 API endpoint constructor
*
* @param String $namespace Namespace.
*/
public function __construct( $namespace ) {
$orders = [ 'url', 'ip', 'total', 'count', '' ];
$filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url', 'domain', 'method', 'http' ];
register_rest_route( $namespace, '/404', array(
'args' => $this->get_filter_args( $orders, $filters ),
$this->get_route( WP_REST_Server::READABLE, 'route_404', [ $this, 'permission_callback_manage' ] ),
) );
register_rest_route( $namespace, '/bulk/404/(?P<bulk>delete)', array(
$this->get_route( WP_REST_Server::EDITABLE, 'route_bulk', [ $this, 'permission_callback_delete' ] ),
'args' => array_merge( $this->get_filter_args( $orders, $filters ), [
'items' => [
'description' => 'Comma separated list of item IDs to perform action on',
'type' => 'array',
'items' => [
'description' => 'Item ID',
'type' => [ 'string', 'number' ],
],
],
] ),
) );
}
/**
* Checks a manage capability
*
* @param WP_REST_Request $request Request.
* @return Bool
*/
public function permission_callback_manage( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_404_MANAGE );
}
/**
* Checks a delete capability
*
* @param WP_REST_Request $request Request.
* @return Bool
*/
public function permission_callback_delete( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_404_DELETE );
}
/**
* Get 404 log
*
* @param WP_REST_Request $request The request.
* @return WP_Error|array Return an array of results, or a WP_Error
*/
public function route_404( WP_REST_Request $request ) {
return $this->get_404( $request->get_params() );
}
/**
* Perform action on 404s
*
* @param WP_REST_Request $request The request.
* @return WP_Error|array Return an array of results, or a WP_Error
*/
public function route_bulk( WP_REST_Request $request ) {
$params = $request->get_params();
if ( isset( $params['items'] ) && is_array( $params['items'] ) ) {
$items = $params['items'];
foreach ( $items as $item ) {
if ( is_numeric( $item ) ) {
Red_404_Log::delete( intval( $item, 10 ) );
} elseif ( isset( $params['groupBy'] ) ) {
$delete_by = 'url-exact';
if ( in_array( $params['groupBy'], [ 'ip', 'agent' ], true ) ) {
$delete_by = $params['groupBy'];
}
Red_404_Log::delete_all( [ 'filterBy' => [ $delete_by => $item ] ] );
}
}
if ( isset( $params['groupBy'] ) && $params['groupBy'] === 'url-exact' ) {
unset( $params['groupBy'] );
}
} elseif ( isset( $params['global'] ) && $params['global'] ) {
Red_404_Log::delete_all( $params );
}
return $this->get_404( $params );
}
/**
* Get 404 log
*
* @param array $params The request.
* @return WP_Error|array Return an array of results, or a WP_Error
*/
private function get_404( array $params ) {
if ( isset( $params['groupBy'] ) && in_array( $params['groupBy'], [ 'ip', 'url', 'agent', 'url-exact' ], true ) ) {
$group_by = $params['groupBy'];
if ( $group_by === 'url-exact' ) {
$group_by = 'url';
}
return Red_404_Log::get_grouped( $group_by, $params );
}
return Red_404_Log::get_filtered( $params );
}
}
<?php
/**
* @api {get} /redirection/v1/export/:module/:format Export redirects
* @apiName Export
* @apiDescription Export redirects for a module to Apache, CSV, Nginx, or JSON format
* @apiGroup Import/Export
*
* @apiParam (URL) {String="1","2","3","all"} :module The module to export, with 1 being WordPress, 2 is Apache, and 3 is Nginx
* @apiParam (URL) {String="csv","apache","nginx","json"} :format The format of the export
*
* @apiSuccess {String} data Exported data
* @apiSuccess {Integer} total Number of items exported
*
* @apiUse 401Error
* @apiUse 404Error
* @apiError (Error 400) redirect_export_invalid_module Invalid module
* @apiErrorExample {json} 404 Error Response:
* HTTP/1.1 400 Bad Request
* {
* "code": "redirect_export_invalid_module",
* "message": "Invalid module"
* }
*/
class Redirection_Api_Export extends Redirection_Api_Route {
public function __construct( $namespace ) {
register_rest_route( $namespace, '/export/(?P<module>1|2|3|all)/(?P<format>csv|apache|nginx|json)', array(
$this->get_route( WP_REST_Server::READABLE, 'route_export', [ $this, 'permission_callback_manage' ] ),
) );
}
public function permission_callback_manage( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_IO_MANAGE );
}
public function route_export( WP_REST_Request $request ) {
$module = $request['module'];
$format = 'json';
if ( in_array( $request['format'], [ 'csv', 'apache', 'nginx', 'json' ], true ) ) {
$format = $request['format'];
}
$export = Red_FileIO::export( $module, $format );
if ( $export === false ) {
return $this->add_error_details( new WP_Error( 'redirect_export_invalid_module', 'Invalid module' ), __LINE__ );
}
return array(
'data' => $export['data'],
'total' => $export['total'],
);
}
}
<?php
/**
* @api {get} /redirection/v1/group Get groups
* @apiName GetGroups
* @apiDescription Get a paged list of groups based after applying a set of filters and result ordering.
* @apiGroup Group
*
* @apiUse GroupQueryParams
*
* @apiUse GroupList
* @apiUse 401Error
* @apiUse 404Error
*/
/**
* @api {post} /redirection/v1/group Create group
* @apiName CreateGroup
* @apiDescription Create a new group, and return a paged list of groups.
* @apiGroup Group
*
* @apiUse GroupItem
* @apiUse GroupQueryParams
*
* @apiUse GroupList
* @apiUse 401Error
* @apiUse 404Error
* @apiError (Error 400) redirect_group_invalid Invalid group or parameters
* @apiErrorExample {json} 404 Error Response:
* HTTP/1.1 400 Bad Request
* {
* "code": "redirect_group_invalid",
* "message": "Invalid group or parameters"
* }
*/
/**
* @api {post} /redirection/v1/group/:id Update group
* @apiName UpdateGroup
* @apiDescription Update an existing group.
* @apiGroup Group
*
* @apiParam (URL) {Integer} :id Group ID to update
* @apiUse GroupList
*
* @apiSuccess {String} item The updated group
* @apiSuccess {Integer} item.id ID of group
* @apiSuccess {String} item.name Name of this group
* @apiSuccess {Boolean} item.enabled `true` if group (and redirects) are enabled, `false` otherwise
* @apiSuccess {Integer} item.redirects Number of redirects in this group
* @apiSuccess {String} item.moduleName Name of the module this group belongs to
* @apiSuccess {Integer} item.module_id ID of the module this group belongs to
*
* @apiUse 401Error
* @apiUse 404Error
*
* @apiError (Error 400) redirect_group_invalid Invalid group or parameters
* @apiErrorExample {json} 404 Error Response:
* HTTP/1.1 400 Bad Request
* {
* "code": "redirect_group_invalid",
* "message": "Invalid group or parameters"
* }
*/
/**
* @api {post} /redirection/v1/bulk/group/:type Bulk action
* @apiName BulkAction
* @apiDescription Enable, disable, and delete a set of groups. The endpoint will return the next page of results after.
* performing the action, based on the supplied query parameters. This information can be used to refresh a list displayed to the client.
* @apiGroup Group
*
* @apiParam (URL) {String="delete","enable","disable"} :type Type of bulk action that is applied to every group ID.
* Enabling or disabling a group will also enable or disable all redirects in that group
*
* @apiParam (Query Parameter) {String[]} [items] Array of group IDs to perform the action on
* @apiParam (Query Parameter) {Boolean=false} [global] Perform action globally using the filter parameters
* @apiUse GroupQueryParams
*
* @apiUse GroupList
* @apiUse 401Error
* @apiUse 404Error
* @apiUse 400MissingError
*/
/**
* @apiDefine GroupQueryParams
*
* @apiParam (Query Parameter) {String} [filterBy[name]] Filter the results by the supplied name
* @apiParam (Query Parameter) {String="enabled","disabled"} [filterBy[status]] Filter the results by the supplied status
* @apiParam (Query Parameter) {Integer="1","2","3"} [filterBy[module]] Filter the results by the supplied module ID
* @apiParam (Query Parameter) {String="name"} [orderby] Order in which results are returned
* @apiParam (Query Parameter) {String="asc","desc"} [direction=desc] Direction to order the results by (ascending or descending)
* @apiParam (Query Parameter) {Integer{1...200}} [per_page=25] Number of results per request
* @apiParam (Query Parameter) {Integer} [page=0] Current page of results
*/
/**
* @apiDefine GroupItem
*
* @apiParam (JSON Body) {String} name Name of the group
* @apiParam (JSON Body) {Integer="1","2","3"} moduleID Module ID of the group, with 1 being WordPress, 2 is Apache, and 3 is Nginx
*/
/**
* @apiDefine GroupList
*
* @apiSuccess {Object[]} items Array of group objects
* @apiSuccess {Integer} items.id ID of group
* @apiSuccess {String} items.name Name of this group
* @apiSuccess {Boolean} items.enabled `true` if group (and redirects) are enabled, `false` otherwise
* @apiSuccess {Integer} items.redirects Number of redirects in this group
* @apiSuccess {String} items.moduleName Name of the module this group belongs to
* @apiSuccess {Integer} items.module_id ID of the module this group belongs to
* @apiSuccess {Integer} total Number of items
*
* @apiSuccessExample {json} Success 200:
* HTTP/1.1 200 OK
* {
* "items": [
* {
* "id": 3,
* "enabled": true,
* "moduleName": "WordPress",
* "module_id": 1,
* "name": "Redirections",
* "redirects": 0,
* }
* ],
* "total": 1
* }
*/
/**
* Group API endpoint
*/
class Redirection_Api_Group extends Redirection_Api_Filter_Route {
/**
* 404 API endpoint constructor
*
* @param String $namespace Namespace.
*/
public function __construct( $namespace ) {
$orders = [ 'name', 'id', '' ];
$filters = [ 'status', 'module', 'name' ];
register_rest_route( $namespace, '/group', array(
'args' => $this->get_filter_args( $orders, $filters ),
$this->get_route( WP_REST_Server::READABLE, 'route_list', [ $this, 'permission_callback_manage' ] ),
array_merge(
$this->get_route( WP_REST_Server::EDITABLE, 'route_create', [ $this, 'permission_callback_add' ] ),
array( 'args' => $this->get_group_args() )
),
) );
register_rest_route( $namespace, '/group/(?P<id>[\d]+)', array(
'args' => $this->get_group_args(),
$this->get_route( WP_REST_Server::EDITABLE, 'route_update', [ $this, 'permission_callback_add' ] ),
) );
register_rest_route( $namespace, '/bulk/group/(?P<bulk>delete|enable|disable)', array(
$this->get_route( WP_REST_Server::EDITABLE, 'route_bulk', [ $this, 'permission_callback_bulk' ] ),
'args' => array_merge( $this->get_filter_args( $orders, $filters ), [
'items' => [
'description' => 'Comma separated list of item IDs to perform action on',
'type' => 'array',
'items' => [
'description' => 'Item ID',
'type' => [ 'string', 'number' ],
],
],
] ),
) );
}
/**
* Checks a manage capability
*
* Access to group data is required by the CAP_GROUP_MANAGE and CAP_REDIRECT_MANAGE caps
*
* @param WP_REST_Request $request Request.
* @return Bool
*/
public function permission_callback_manage( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_GROUP_MANAGE ) || Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_REDIRECT_MANAGE );
}
/**
* Checks a bulk capability
*
* @param WP_REST_Request $request Request.
* @return Bool
*/
public function permission_callback_bulk( WP_REST_Request $request ) {
if ( $request['bulk'] === 'delete' ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_GROUP_DELETE );
}
return $this->permission_callback_add( $request );
}
/**
* Checks a create capability
*
* @param WP_REST_Request $request Request.
* @return Bool
*/
public function permission_callback_add( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_GROUP_ADD );
}
private function get_group_args() {
return array(
'moduleId' => array(
'description' => 'Module ID',
'type' => 'integer',
'minimum' => 0,
'maximum' => 3,
'required' => true,
),
'name' => array(
'description' => 'Group name',
'type' => 'string',
'required' => true,
),
'status' => [
'description' => 'Status of the group',
],
);
}
/**
* Get group list
*
* @param WP_REST_Request $request The request.
* @return WP_Error|array Return an array of results, or a WP_Error
*/
public function route_list( WP_REST_Request $request ) {
return Red_Group::get_filtered( $request->get_params() );
}
/**
* Create a group
*
* @param WP_REST_Request $request The request.
* @return WP_Error|array Return an array of results, or a WP_Error
*/
public function route_create( WP_REST_Request $request ) {
$params = $request->get_params( $request );
$group = Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 );
if ( $group ) {
return Red_Group::get_filtered( $params );
}
return $this->add_error_details( new WP_Error( 'redirect_group_invalid', 'Invalid group or parameters' ), __LINE__ );
}
/**
* Update a 404
*
* @param WP_REST_Request $request The request.
* @return WP_Error|array Return an array of results, or a WP_Error
*/
public function route_update( WP_REST_Request $request ) {
$params = $request->get_params( $request );
$group = Red_Group::get( intval( $request['id'], 10 ) );
if ( $group ) {
$result = $group->update( $params );
if ( $result ) {
return array( 'item' => $group->to_json() );
}
}
return $this->add_error_details( new WP_Error( 'redirect_group_invalid', 'Invalid group details' ), __LINE__ );
}
/**
* Perform action on groups
*
* @param WP_REST_Request $request The request.
* @return WP_Error|array Return an array of results, or a WP_Error
*/
public function route_bulk( WP_REST_Request $request ) {
$params = $request->get_params();
$action = $request['bulk'];
$items = [];
if ( isset( $params['items'] ) && is_array( $params['items'] ) ) {
$items = $params['items'];
} elseif ( isset( $params['global'] ) && $params['global'] ) {
// Groups have additional actions that fire and so we need to action them individually
$groups = Red_Group::get_all( $params );
$items = array_column( $groups, 'id' );
}
foreach ( $items as $item ) {
$group = Red_Group::get( intval( $item, 10 ) );
if ( is_object( $group ) ) {
if ( $action === 'delete' ) {
$group->delete();
} elseif ( $action === 'disable' ) {
$group->disable();
} elseif ( $action === 'enable' ) {
$group->enable();
}
}
}
return $this->route_list( $request );
}
}
<?php
/**
* @api {get} /redirection/v1/import/file/:group_id Import redirects
* @apiName Import
* @apiDescription Import redirects from CSV, JSON, or Apache .htaccess
* @apiGroup Import/Export
*
* @apiParam (URL) {Integer} :group_id The group ID to import into
* @apiParam (File) {File} file The multipart form upload containing the file to import
*
* @apiSuccess {Integer} imported Number of items imported
*
* @apiUse 401Error
* @apiUse 404Error
* @apiError (Error 400) redirect_import_invalid_group Invalid group
* @apiErrorExample {json} 404 Error Response:
* HTTP/1.1 400 Bad Request
* {
* "code": "redirect_import_invalid_group",
* "message": "Invalid group"
* }
* @apiError (Error 400) redirect_import_invalid_file Invalid file upload
* @apiErrorExample {json} 404 Error Response:
* HTTP/1.1 400 Bad Request
* {
* "code": "redirect_import_invalid_file",
* "message": "Invalid file upload"
* }
*/
class Redirection_Api_Import extends Redirection_Api_Route {
public function __construct( $namespace ) {
register_rest_route( $namespace, '/import/file/(?P<group_id>\d+)', array(
$this->get_route( WP_REST_Server::EDITABLE, 'route_import_file', [ $this, 'permission_callback_manage' ] ),
) );
register_rest_route( $namespace, '/import/plugin', array(
$this->get_route( WP_REST_Server::READABLE, 'route_plugin_import_list', [ $this, 'permission_callback_manage' ] ),
) );
register_rest_route( $namespace, '/import/plugin', array(
$this->get_route( WP_REST_Server::EDITABLE, 'route_plugin_import', [ $this, 'permission_callback_manage' ] ),
) );
}
public function permission_callback_manage( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_IO_MANAGE );
}
public function route_plugin_import_list( WP_REST_Request $request ) {
include_once dirname( __DIR__ ) . '/models/importer.php';
return array( 'importers' => Red_Plugin_Importer::get_plugins() );
}
public function route_plugin_import( WP_REST_Request $request ) {
include_once dirname( __DIR__ ) . '/models/importer.php';
$params = $request->get_params( $request );
$groups = Red_Group::get_all();
$plugins = is_array( $request['plugin'] ) ? $request['plugin'] : [ $request['plugin'] ];
$total = 0;
foreach ( $plugins as $plugin ) {
$total += Red_Plugin_Importer::import( $plugin, $groups[0]['id'] );
}
return [ 'imported' => $total ];
}
public function route_import_file( WP_REST_Request $request ) {
$upload = $request->get_file_params();
$upload = isset( $upload['file'] ) ? $upload['file'] : false;
$group_id = $request['group_id'];
if ( $upload && is_uploaded_file( $upload['tmp_name'] ) ) {
$count = Red_FileIO::import( $group_id, $upload );
if ( $count !== false ) {
return array(
'imported' => $count,
);
}
return $this->add_error_details( new WP_Error( 'redirect_import_invalid_group', 'Invalid group' ), __LINE__ );
}
return $this->add_error_details( new WP_Error( 'redirect_import_invalid_file', 'Invalid file' ), __LINE__ );
}
}
<?php
/**
* @api {get} /redirection/v1/log Get logs
* @apiName GetLogs
* @apiDescription Get a paged list of redirect logs after applying a set of filters and result ordering.
* @apiGroup Log
*
* @apiUse LogQueryParams
*
* @apiUse LogList
* @apiUse 401Error
* @apiUse 404Error
*/
/**
* @api {post} /redirection/v1/log Delete logs
* @apiName DeleteLogs
* @apiDescription Delete logs by filter. If no filter is supplied then all entries will be deleted. The endpoint will return the next page of results after.
* performing the action, based on the supplied query parameters. This information can be used to refresh a list displayed to the client.
* @apiGroup Log
*
* @apiParam (Query Parameter) {String} filterBy[ip] Filter the results by the supplied IP
* @apiParam (Query Parameter) {String} filterBy[url] Filter the results by the supplied URL
* @apiParam (Query Parameter) {String} filterBy[url-exact] Filter the results by the exact URL (not a substring match, as per `url`)
* @apiParam (Query Parameter) {String} filterBy[referrer] Filter the results by the supplied referrer
* @apiParam (Query Parameter) {String} filterBy[agent] Filter the results by the supplied user agent
* @apiParam (Query Parameter) {String} filterBy[target] Filter the results by the supplied redirect target
*
* @apiUse LogList
* @apiUse 401Error
* @apiUse 404Error
*/
/**
* @api {post} /redirection/v1/bulk/log/:type Bulk action
* @apiName BulkAction
* @apiDescription Delete logs by ID
* @apiGroup Log
*
* @apiParam (URL) {String="delete"} :type Type of bulk action that is applied to every log ID.
* @apiParam (Query Parameter) {String[]} [items] Array of group IDs to perform the action on
* @apiParam (Query Parameter) {Boolean=false} [global] Perform action globally using the filter parameters
* @apiUse LogQueryParams
*
* @apiUse LogList
* @apiUse 401Error
* @apiUse 404Error
* @apiUse 400MissingError
*/
/**
* @apiDefine LogQueryParams Log query parameters
*
* @apiParam (Query Parameter) {String} [filterBy[ip]] Filter the results by the supplied IP
* @apiParam (Query Parameter) {String} [filterBy[url]] Filter the results by the supplied URL
* @apiParam (Query Parameter) {String} [filterBy[url-]exact] Filter the results by the exact URL (not a substring match, as per `url`)
* @apiParam (Query Parameter) {String} [filterBy[referrer]] Filter the results by the supplied referrer
* @apiParam (Query Parameter) {String} [filterBy[agent]] Filter the results by the supplied user agent
* @apiParam (Query Parameter) {String} [filterBy[target]] Filter the results by the supplied redirect target
* @apiParam (Query Parameter) {String} [filterBy[domain]] Filter the results by the supplied domain name
* @apiParam (Query Parameter) {String} [filterBy[redirect_by]] Filter the results by the redirect agent
* @apiParam (Query Parameter) {String="head","get","post"} [filterBy[method]] Filter the results by the supplied HTTP request method
* @apiParam (Query Parameter) {String="ip","url"} [orderby] Order by IP or URL
* @apiParam (Query Parameter) {String="asc","desc"} [direction=desc] Direction to order the results by (ascending or descending)
* @apiParam (Query Parameter) {Integer{1...200}} [per_page=25] Number of results per request
* @apiParam (Query Parameter) {Integer} [page=0] Current page of results
* @apiParam (Query Parameter) {String="ip","url"} [groupBy] Group by IP or URL
*/
/**
* @apiDefine LogList
*
* @apiSuccess {Object[]} items Array of log objects
* @apiSuccess {Integer} items.id ID of log entry
* @apiSuccess {String} items.created Date the log entry was recorded
* @apiSuccess {Integer} items.created_time Unix time value for `created`
* @apiSuccess {Integer} items.url The requested URL that caused the log entry
* @apiSuccess {String} items.agent User agent of the client initiating the request
* @apiSuccess {Integer} items.referrer Referrer of the client initiating the request
* @apiSuccess {Integer} total Number of items
*
* @apiSuccessExample {json} Success 200:
* HTTP/1.1 200 OK
* {
* "items": [
* {
* "id": 3,
* "created": "2019-01-01 12:12:00,
* "created_time": "12345678",
* "url": "/the-url",
* "agent": "FancyBrowser",
* "referrer": "http://site.com/previous/,
* }
* ],
* "total": 1
* }
*/
/**
* Log API endpoint
*/
class Redirection_Api_Log extends Redirection_Api_Filter_Route {
/**
* Log API endpoint constructor
*
* @param String $namespace Namespace.
*/
public function __construct( $namespace ) {
$orders = [ 'url', 'ip', 'total', 'count', '' ];
$filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url', 'target', 'domain', 'method', 'http', 'redirect_by' ];
register_rest_route( $namespace, '/log', array(
'args' => $this->get_filter_args( $orders, $filters ),
$this->get_route( WP_REST_Server::READABLE, 'route_log', [ $this, 'permission_callback_manage' ] ),
) );
register_rest_route( $namespace, '/bulk/log/(?P<bulk>delete)', [
$this->get_route( WP_REST_Server::EDITABLE, 'route_bulk', [ $this, 'permission_callback_delete' ] ),
'args' => array_merge( $this->get_filter_args( $orders, $filters ), [
'items' => [
'description' => 'Comma separated list of item IDs to perform action on',
'type' => 'array',
'items' => [
'description' => 'Item ID',
'type' => [ 'string', 'number' ],
],
],
] ),
] );
}
/**
* Checks a manage capability
*
* @param WP_REST_Request $request Request.
* @return Bool
*/
public function permission_callback_manage( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_LOG_MANAGE );
}
/**
* Checks a delete capability
*
* @param WP_REST_Request $request Request.
* @return Bool
*/
public function permission_callback_delete( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_LOG_DELETE );
}
/**
* Get log list
*
* @param WP_REST_Request $request The request.
* @return WP_Error|array Return an array of results, or a WP_Error
*/
public function route_log( WP_REST_Request $request ) {
return $this->get_logs( $request->get_params() );
}
/**
* Perform bulk action on logs
*
* @param WP_REST_Request $request The request.
* @return WP_Error|array Return an array of results, or a WP_Error
*/
public function route_bulk( WP_REST_Request $request ) {
$params = $request->get_params();
if ( isset( $params['items'] ) && is_array( $params['items'] ) ) {
$items = $params['items'];
foreach ( $items as $item ) {
if ( is_numeric( $item ) ) {
Red_Redirect_Log::delete( intval( $item, 10 ) );
} elseif ( isset( $params['groupBy'] ) ) {
$delete_by = 'url-exact';
if ( in_array( $params['groupBy'], [ 'ip', 'agent' ], true ) ) {
$delete_by = $params['groupBy'];
}
Red_Redirect_Log::delete_all( [ 'filterBy' => [ $delete_by => $item ] ] );
}
}
} elseif ( isset( $params['global'] ) && $params['global'] ) {
Red_Redirect_Log::delete_all( $params );
}
return $this->route_log( $request );
}
private function get_logs( array $params ) {
if ( isset( $params['groupBy'] ) && in_array( $params['groupBy'], [ 'ip', 'url', 'agent' ], true ) ) {
return Red_Redirect_Log::get_grouped( $params['groupBy'], $params );
}
return Red_Redirect_Log::get_filtered( $params );
}
}
<?php
/**
* 'Plugin' functions for Redirection
*/
class Redirection_Api_Plugin extends Redirection_Api_Route {
public function __construct( $namespace ) {
register_rest_route( $namespace, '/plugin', array(
$this->get_route( WP_REST_Server::READABLE, 'route_status', [ $this, 'permission_callback_manage' ] ),
) );
register_rest_route( $namespace, '/plugin', array(
$this->get_route( WP_REST_Server::EDITABLE, 'route_fixit', [ $this, 'permission_callback_manage' ] ),
'args' => [
'name' => array(
'description' => 'Name',
'type' => 'string',
),
'value' => array(
'description' => 'Value',
'type' => 'string',
),
],
) );
register_rest_route( $namespace, '/plugin/delete', array(
$this->get_route( WP_REST_Server::EDITABLE, 'route_delete', [ $this, 'permission_callback_manage' ] ),
) );
register_rest_route( $namespace, '/plugin/test', array(
$this->get_route( WP_REST_Server::ALLMETHODS, 'route_test', [ $this, 'permission_callback_manage' ] ),
) );
register_rest_route( $namespace, '/plugin/data', array(
$this->get_route( WP_REST_Server::EDITABLE, 'route_database', [ $this, 'permission_callback_manage' ] ),
'args' => [
'upgrade' => [
'description' => 'Upgrade parameter',
'type' => 'string',
'enum' => array(
'stop',
'skip',
),
],
],
) );
}
public function permission_callback_manage( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_SUPPORT_MANAGE );
}
public function route_status( WP_REST_Request $request ) {
include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php';
$fixer = new Red_Fixer();
return $fixer->get_json();
}
public function route_fixit( WP_REST_Request $request ) {
include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php';
$params = $request->get_params();
$fixer = new Red_Fixer();
if ( isset( $params['name'] ) && isset( $params['value'] ) ) {
global $wpdb;
$fixer->save_debug( $params['name'], $params['value'] );
$groups = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ), 10 );
if ( $groups === 0 ) {
Red_Group::create( 'new group', 1 );
}
} else {
$fixer->fix( $fixer->get_status() );
}
return $fixer->get_json();
}
public function route_delete() {
if ( is_multisite() ) {
return new WP_Error( 'redirect_delete_multi', 'Multisite installations must delete the plugin from the network admin' );
}
$plugin = Redirection_Admin::init();
$plugin->plugin_uninstall();
$current = get_option( 'active_plugins' );
$plugin_position = array_search( basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ), $current );
if ( $plugin_position !== false ) {
array_splice( $current, $plugin_position, 1 );
update_option( 'active_plugins', $current );
}
return array( 'location' => admin_url() . 'plugins.php' );
}
public function route_test( WP_REST_Request $request ) {
return array(
'success' => true,
);
}
public function route_database( WP_REST_Request $request ) {
$params = $request->get_params();
$status = new Red_Database_Status();
$upgrade = false;
if ( isset( $params['upgrade'] ) && in_array( $params['upgrade'], [ 'stop', 'skip' ], true ) ) {
$upgrade = $params['upgrade'];
}
// Check upgrade
if ( ! $status->needs_updating() && ! $status->needs_installing() ) {
/* translators: version number */
$status->set_error( sprintf( __( 'Your database does not need updating to %s.', 'redirection' ), REDIRECTION_DB_VERSION ) );
return $status->get_json();
}
if ( $upgrade === 'stop' ) {
$status->stop_update();
} elseif ( $upgrade === 'skip' ) {
$status->set_next_stage();
}
if ( $upgrade === false || $status->get_current_stage() ) {
$database = new Red_Database();
$database->apply_upgrade( $status );
}
return $status->get_json();
}
}
<?php
/**
* @api {get} /redirection/v1/setting Get settings
* @apiName GetSettings
* @apiDescription Get all settings for Redirection. This includes user-configurable settings, as well as necessary WordPress settings.
* @apiGroup Settings
*
* @apiUse SettingItem
* @apiUse 401Error
* @apiUse 404Error
*/
/**
* @api {post} /redirection/v1/setting Update settings
* @apiName UpdateSettings
* @apiDescription Update Redirection settings. Note you can do partial updates, and only the values specified will be changed.
* @apiGroup Settings
*
* @apiParam {Object} settings An object containing all the settings to update
* @apiParamExample {json} settings:
* {
* "expire_redirect": 14,
* "https": false
* }
*
* @apiUse SettingItem
* @apiUse 401Error
* @apiUse 404Error
*/
/**
* @apiDefine SettingItem Settings
* Redirection settings
*
* @apiSuccess {Object[]} settings An object containing all settings
* @apiSuccess {String} settings.expire_redirect
* @apiSuccess {String} settings.token
* @apiSuccess {String} settings.monitor_post
* @apiSuccess {String} settings.monitor_types
* @apiSuccess {String} settings.associated_redirect
* @apiSuccess {String} settings.auto_target
* @apiSuccess {String} settings.expire_redirect
* @apiSuccess {String} settings.expire_404
* @apiSuccess {String} settings.modules
* @apiSuccess {String} settings.newsletter
* @apiSuccess {String} settings.redirect_cache
* @apiSuccess {String} settings.ip_logging
* @apiSuccess {String} settings.last_group_id
* @apiSuccess {String} settings.rest_api
* @apiSuccess {String} settings.https
* @apiSuccess {String} settings.headers
* @apiSuccess {String} settings.database
* @apiSuccess {String} settings.relcoate Relocate this site to the specified domain (and path)
* @apiSuccess {String="www","nowww",""} settings.preferred_domain Preferred canonical domain
* @apiSuccess {String[]} settings.aliases Array of domains that will be redirected to the current WordPress site
* @apiSuccess {Object[]} groups An array of groups
* @apiSuccess {String} groups.label Name of the group
* @apiSuccess {Integer} groups.value Group ID
* @apiSuccess {String} installed The path that WordPress is installed in
* @apiSuccess {Boolean} canDelete True if Redirection can be deleted, false otherwise (on multisite, for example)
* @apiSuccess {String[]} post_types Array of WordPress post types
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "settings": {
* "expire_redirect": 7,
* "https": true
* },
* "groups": [
* { label: 'My group', value: 5 }
* ],
* "installed": "/var/html/wordpress",
* "canDelete": true,
* "post_types": [
* "post",
* "page"
* ]
* }
*/
class Redirection_Api_Settings extends Redirection_Api_Route {
public function __construct( $namespace ) {
register_rest_route( $namespace, '/setting', array(
$this->get_route( WP_REST_Server::READABLE, 'route_settings', [ $this, 'permission_callback_manage' ] ),
$this->get_route( WP_REST_Server::EDITABLE, 'route_save_settings', [ $this, 'permission_callback_manage' ] ),
) );
}
public function route_settings( WP_REST_Request $request ) {
if ( ! function_exists( 'get_home_path' ) ) {
include_once ABSPATH . '/wp-admin/includes/file.php';
}
return [
'settings' => red_get_options(),
'groups' => $this->groups_to_json( Red_Group::get_for_select() ),
'installed' => get_home_path(),
'canDelete' => ! is_multisite(),
'post_types' => red_get_post_types(),
];
}
public function permission_callback_manage( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_OPTION_MANAGE ) || Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_SITE_MANAGE );
}
public function route_save_settings( WP_REST_Request $request ) {
$params = $request->get_params();
$result = true;
if ( isset( $params['location'] ) && strlen( $params['location'] ) > 0 ) {
$module = Red_Module::get( 2 );
$result = $module->can_save( $params['location'] );
}
red_set_options( $params );
$settings = $this->route_settings( $request );
if ( is_wp_error( $result ) ) {
$settings['warning'] = $result->get_error_message();
}
return $settings;
}
private function groups_to_json( $groups, $depth = 0 ) {
$items = array();
foreach ( $groups as $text => $value ) {
if ( is_array( $value ) && $depth === 0 ) {
$items[] = (object) array(
'label' => $text,
'value' => $this->groups_to_json( $value, 1 ),
);
} else {
$items[] = (object) array(
'label' => $value,
'value' => $text,
);
}
}
return $items;
}
}
<?php
require_once __DIR__ . '/api-group.php';
require_once __DIR__ . '/api-redirect.php';
require_once __DIR__ . '/api-log.php';
require_once __DIR__ . '/api-404.php';
require_once __DIR__ . '/api-settings.php';
require_once __DIR__ . '/api-plugin.php';
require_once __DIR__ . '/api-import.php';
require_once __DIR__ . '/api-export.php';
define( 'REDIRECTION_API_NAMESPACE', 'redirection/v1' );
/**
* @apiDefine 401Error
*
* @apiError (Error 401) rest_forbidden You are not authorized to access this API endpoint
* @apiErrorExample {json} 401 Error Response:
* HTTP/1.1 401 Bad Request
* {
* "code": "rest_forbidden",
* "message": "Sorry, you are not allowed to do that."
* }
*/
/**
* @apiDefine 404Error
*
* @apiError (Error 404) rest_no_route Endpoint not found
* @apiErrorExample {json} 404 Error Response:
* HTTP/1.1 404 Not Found
* {
* "code": "rest_no_route",
* "message": "No route was found matching the URL and request method"
* }
*/
/**
* @apiDefine 400Error
*
* @apiError rest_forbidden You are not authorized to access this API endpoint
* @apiErrorExample {json} 400 Error Response:
* HTTP/1.1 400 Bad Request
* {
* "error": "invalid",
* "message": "Invalid request"
* }
*/
/**
* @apiDefine 400MissingError
* @apiError (Error 400) rest_missing_callback_param Some required parameters are not present or not in the correct format
* @apiErrorExample {json} 400 Error Response:
* HTTP/1.1 400 Bad Request
* {
* "code": "rest_missing_callback_param",
* "message": "Missing parameter(s): PARAM"
* }
*/
class Redirection_Api_Route {
protected function add_error_details( WP_Error $error, $line, $code = 400 ) {
global $wpdb;
$data = array(
'status' => $code,
'error_code' => $line,
);
if ( isset( $wpdb->last_error ) && $wpdb->last_error ) {
$data['wpdb'] = $wpdb->last_error;
}
$error->add_data( $data );
return $error;
}
public function permission_callback( WP_REST_Request $request ) {
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_PLUGIN );
}
public function get_route( $method, $callback, $permissions = false ) {
return [
'methods' => $method,
'callback' => [ $this, $callback ],
'permission_callback' => $permissions ? $permissions : [ $this, 'permission_callback' ],
];
}
}
class Redirection_Api_Filter_Route extends Redirection_Api_Route {
public function validate_filter( $value, $request, $param ) {
$fields = $request->get_attributes()['args']['filterBy']['filter_fields'];
if ( ! is_array( $value ) ) {
return new WP_Error( 'rest_invalid_param', 'Filter is not an array', array( 'status' => 400 ) );
}
if ( ! empty( $fields ) ) {
foreach ( array_keys( $value ) as $key ) {
if ( ! in_array( $key, $fields, true ) ) {
return new WP_Error( 'rest_invalid_param', 'Filter type is not supported: ' . $key, array( 'status' => 400 ) );
}
}
}
return true;
}
protected function get_filter_args( $order_fields, $filters = [] ) {
return [
'filterBy' => [
'description' => 'Field to filter by',
'validate_callback' => [ $this, 'validate_filter' ],
'filter_fields' => $filters,
],
'orderby' => [
'description' => 'Field to order results by',
'type' => 'string',
'enum' => $order_fields,
],
'direction' => [
'description' => 'Direction of ordered results',
'type' => 'string',
'default' => 'desc',
'enum' => [ 'asc', 'desc' ],
],
'per_page' => [
'description' => 'Number of results per page',
'type' => 'integer',
'default' => 25,
'minimum' => 5,
'maximum' => RED_MAX_PER_PAGE,
],
'page' => [
'description' => 'Page offset',
'type' => 'integer',
'minimum' => 0,
'default' => 0,
],
];
}
/**
* Register a bulk action route
*
* @param String $namespace Namespace.
* @param String $route Route.
* @param Array $orders
* @param Array $filters
* @param Object $callback
* @param boolean $permissions
* @return void
*/
public function register_bulk( $namespace, $route, $orders, $filters, $callback, $permissions = false ) {
register_rest_route( $namespace, $route, array(
$this->get_route( WP_REST_Server::EDITABLE, $callback, $permissions ),
'args' => array_merge( $this->get_filter_args( $orders, $filters ), [
'items' => [
'description' => 'Comma separated list of item IDs to perform action on',
'type' => 'array',
'items' => [
'type' => 'string',
],
],
] ),
) );
}
}
class Redirection_Api {
private static $instance = null;
private $routes = array();
public static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new Redirection_Api();
}
return self::$instance;
}
public function __construct() {
global $wpdb;
$wpdb->hide_errors();
$this->routes[] = new Redirection_Api_Redirect( REDIRECTION_API_NAMESPACE );
$this->routes[] = new Redirection_Api_Group( REDIRECTION_API_NAMESPACE );
$this->routes[] = new Redirection_Api_Log( REDIRECTION_API_NAMESPACE );
$this->routes[] = new Redirection_Api_404( REDIRECTION_API_NAMESPACE );
$this->routes[] = new Redirection_Api_Settings( REDIRECTION_API_NAMESPACE );
$this->routes[] = new Redirection_Api_Plugin( REDIRECTION_API_NAMESPACE );
$this->routes[] = new Redirection_Api_Import( REDIRECTION_API_NAMESPACE );
$this->routes[] = new Redirection_Api_Export( REDIRECTION_API_NAMESPACE );
}
}
<?php
class Red_Database_Status {
// Used in < 3.7 versions of Redirection, but since migrated to general settings
const OLD_DB_VERSION = 'redirection_version';
const DB_UPGRADE_STAGE = 'database_stage';
const RESULT_OK = 'ok';
const RESULT_ERROR = 'error';
const STATUS_OK = 'ok';
const STATUS_NEED_INSTALL = 'need-install';
const STATUS_NEED_UPDATING = 'need-update';
const STATUS_FINISHED_INSTALL = 'finish-install';
const STATUS_FINISHED_UPDATING = 'finish-update';
private $stage = false;
private $stages = [];
private $status = false;
private $result = false;
private $reason = false;
private $debug = [];
public function __construct() {
$this->status = self::STATUS_OK;
if ( $this->needs_installing() ) {
$this->status = self::STATUS_NEED_INSTALL;
}
$this->load_stage();
if ( $this->needs_updating() ) {
$this->status = self::STATUS_NEED_UPDATING;
}
}
public function load_stage() {
$settings = red_get_options();
if ( isset( $settings[ self::DB_UPGRADE_STAGE ] ) ) {
$this->stage = isset( $settings[ self::DB_UPGRADE_STAGE ]['stage'] ) ? $settings[ self::DB_UPGRADE_STAGE ]['stage'] : false;
$this->stages = isset( $settings[ self::DB_UPGRADE_STAGE ]['stages'] ) ? $settings[ self::DB_UPGRADE_STAGE ]['stages'] : [];
$this->status = isset( $settings[ self::DB_UPGRADE_STAGE ]['status'] ) ? $settings[ self::DB_UPGRADE_STAGE ]['status'] : false;
}
}
/**
* Does the database need install
*
* @return bool true if needs installing, false otherwise
*/
public function needs_installing() {
$settings = red_get_options();
if ( $settings['database'] === '' && $this->get_old_version() === false ) {
return true;
}
return false;
}
/**
* Does the current database need updating to the target
*
* @return bool true if needs updating, false otherwise
*/
public function needs_updating() {
// We need updating if we don't need to install, and the current version is less than target version
if ( $this->needs_installing() === false && version_compare( $this->get_current_version(), REDIRECTION_DB_VERSION, '<' ) ) {
return true;
}
// Also if we're still in the process of upgrading
if ( $this->get_current_stage() ) {
return true;
}
return false;
}
/**
* Get current database version
*
* @return string Current database version
*/
public function get_current_version() {
$settings = red_get_options();
if ( $settings['database'] !== '' ) {
return $settings['database'];
} elseif ( $this->get_old_version() !== false ) {
$version = $this->get_old_version();
// Upgrade the old value
red_set_options( array( 'database' => $version ) );
delete_option( self::OLD_DB_VERSION );
$this->clear_cache();
return $version;
}
return '';
}
private function get_old_version() {
return get_option( self::OLD_DB_VERSION );
}
public function check_tables_exist() {
$latest = Red_Database::get_latest_database();
$missing = $latest->get_missing_tables();
// No tables installed - do a fresh install
if ( count( $missing ) === count( $latest->get_all_tables() ) ) {
delete_option( Red_Database_Status::OLD_DB_VERSION );
red_set_options( [ 'database' => '' ] );
$this->clear_cache();
$this->status = self::STATUS_NEED_INSTALL;
$this->stop_update();
} elseif ( count( $missing ) > 0 && version_compare( $this->get_current_version(), '2.3.3', 'ge' ) ) {
// Some tables are missing - try and fill them in
$latest->install();
}
}
/**
* Does the current database support a particular version
*
* @param string $version Target version
* @return bool true if supported, false otherwise
*/
public function does_support( $version ) {
return version_compare( $this->get_current_version(), $version, 'ge' );
}
public function is_error() {
return $this->result === self::RESULT_ERROR;
}
public function set_error( $error ) {
global $wpdb;
$this->result = self::RESULT_ERROR;
$this->reason = str_replace( "\t", ' ', $error );
if ( $wpdb->last_error ) {
$this->debug[] = $wpdb->last_error;
if ( strpos( $wpdb->last_error, 'command denied to user' ) !== false ) {
$this->reason .= ' - ' . __( 'Insufficient database permissions detected. Please give your database user appropriate permissions.', 'redirection' );
}
}
$latest = Red_Database::get_latest_database();
$this->debug = array_merge( $this->debug, $latest->get_table_schema() );
$this->debug[] = 'Stage: ' . $this->get_current_stage();
}
public function set_ok( $reason ) {
$this->reason = $reason;
$this->result = self::RESULT_OK;
$this->debug = [];
}
/**
* Stop current upgrade
*/
public function stop_update() {
$this->stage = false;
$this->stages = [];
$this->debug = [];
red_set_options( [ self::DB_UPGRADE_STAGE => false ] );
$this->clear_cache();
}
public function finish() {
$this->stop_update();
if ( $this->status === self::STATUS_NEED_INSTALL ) {
$this->status = self::STATUS_FINISHED_INSTALL;
} elseif ( $this->status === self::STATUS_NEED_UPDATING ) {
$this->status = self::STATUS_FINISHED_UPDATING;
}
}
/**
* Get current upgrade stage
* @return string|bool Current stage name, or false if not upgrading
*/
public function get_current_stage() {
return $this->stage;
}
/**
* Move current stage on to the next
*/
public function set_next_stage() {
$this->debug = [];
$stage = $this->get_current_stage();
if ( $stage ) {
$stage = $this->get_next_stage( $stage );
// Save next position
if ( $stage ) {
$this->set_stage( $stage );
} else {
$this->finish();
}
}
}
/**
* Get current upgrade status
*
* @return array Database status array
*/
public function get_json() {
// Base information
$result = [
'status' => $this->status,
'inProgress' => $this->stage !== false,
];
// Add on version status
if ( $this->status === self::STATUS_NEED_INSTALL || $this->status === self::STATUS_NEED_UPDATING ) {
$result = array_merge(
$result,
$this->get_version_upgrade(),
[ 'manual' => $this->get_manual_upgrade() ]
);
}
// Add on upgrade status
if ( $this->is_error() ) {
$result = array_merge( $result, $this->get_version_upgrade(), $this->get_progress_status(), $this->get_error_status() );
} elseif ( $result['inProgress'] ) {
$result = array_merge( $result, $this->get_progress_status() );
} elseif ( $this->status === self::STATUS_FINISHED_INSTALL || $this->status === self::STATUS_FINISHED_UPDATING ) {
$result['complete'] = 100;
$result['reason'] = $this->reason;
}
return $result;
}
private function get_error_status() {
return [
'reason' => $this->reason,
'result' => self::RESULT_ERROR,
'debug' => $this->debug,
];
}
private function get_progress_status() {
$complete = 0;
if ( $this->stage ) {
$complete = round( ( array_search( $this->stage, $this->stages, true ) / count( $this->stages ) ) * 100, 1 );
}
return [
'complete' => $complete,
'result' => self::RESULT_OK,
'reason' => $this->reason,
];
}
private function get_version_upgrade() {
return [
'current' => $this->get_current_version() ? $this->get_current_version() : '-',
'next' => REDIRECTION_DB_VERSION,
'time' => microtime( true ),
];
}
/**
* Set the status information for a database upgrade
*/
public function start_install( array $upgrades ) {
$this->set_stages( $upgrades );
$this->status = self::STATUS_NEED_INSTALL;
}
public function start_upgrade( array $upgrades ) {
$this->set_stages( $upgrades );
$this->status = self::STATUS_NEED_UPDATING;
}
private function set_stages( array $upgrades ) {
$this->stages = [];
foreach ( $upgrades as $upgrade ) {
$upgrader = Red_Database_Upgrader::get( $upgrade );
$this->stages = array_merge( $this->stages, array_keys( $upgrader->get_stages() ) );
}
if ( count( $this->stages ) > 0 ) {
$this->set_stage( $this->stages[0] );
}
}
public function set_stage( $stage ) {
$this->stage = $stage;
$this->save_details();
}
private function save_details() {
$stages = [
self::DB_UPGRADE_STAGE => [
'stage' => $this->stage,
'stages' => $this->stages,
'status' => $this->status,
],
];
red_set_options( $stages );
$this->clear_cache();
}
private function get_manual_upgrade() {
$queries = [];
$database = new Red_Database();
$upgraders = $database->get_upgrades_for_version( $this->get_current_version(), false );
foreach ( $upgraders as $upgrade ) {
$upgrade = Red_Database_Upgrader::get( $upgrade );
$stages = $upgrade->get_stages();
foreach ( array_keys( $stages ) as $stage ) {
$queries = array_merge( $queries, $upgrade->get_queries_for_stage( $stage ) );
}
}
return $queries;
}
private function get_next_stage( $stage ) {
$database = new Red_Database();
$upgraders = $database->get_upgrades_for_version( $this->get_current_version(), $this->get_current_stage() );
if ( count( $upgraders ) === 0 ) {
$upgraders = $database->get_upgrades_for_version( $this->get_current_version(), false );
}
$upgrader = Red_Database_Upgrader::get( $upgraders[0] );
// Where are we in this?
$pos = array_search( $this->stage, $this->stages, true );
if ( $pos === count( $this->stages ) - 1 ) {
$this->save_db_version( REDIRECTION_DB_VERSION );
return false;
}
// Set current DB version
$current_stages = array_keys( $upgrader->get_stages() );
if ( array_search( $this->stage, $current_stages, true ) === count( $current_stages ) - 1 ) {
$this->save_db_version( $upgraders[1]['version'] );
}
// Move on to next in current version
return $this->stages[ $pos + 1 ];
}
public function save_db_version( $version ) {
red_set_options( array( 'database' => $version ) );
delete_option( self::OLD_DB_VERSION );
$this->clear_cache();
}
private function clear_cache() {
if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) && function_exists( 'wp_cache_flush' ) ) {
wp_cache_flush();
}
}
}
<?php
abstract class Red_Database_Upgrader {
private $queries = [];
private $live = true;
/**
* Return an array of all the stages for an upgrade
*
* @return array stage name => reason
*/
abstract public function get_stages();
public function get_reason( $stage ) {
$stages = $this->get_stages();
if ( isset( $stages[ $stage ] ) ) {
return $stages[ $stage ];
}
return 'Unknown';
}
/**
* Run a particular stage on the current upgrader
*
* @return Red_Database_Status
*/
public function perform_stage( Red_Database_Status $status ) {
global $wpdb;
$stage = $status->get_current_stage();
if ( $this->has_stage( $stage ) && method_exists( $this, $stage ) ) {
try {
$this->$stage( $wpdb );
$status->set_ok( $this->get_reason( $stage ) );
} catch ( Exception $e ) {
$status->set_error( $e->getMessage() );
}
} else {
$status->set_error( 'No stage found for upgrade ' . $stage );
}
}
public function get_queries_for_stage( $stage ) {
global $wpdb;
$this->queries = [];
$this->live = false;
$this->$stage( $wpdb, false );
$this->live = true;
return $this->queries;
}
/**
* Returns the current database charset
*
* @return string Database charset
*/
public function get_charset() {
global $wpdb;
$charset_collate = '';
if ( ! empty( $wpdb->charset ) ) {
// Fix some common invalid charset values
$fixes = [
'utf-8',
'utf',
];
$charset = $wpdb->charset;
if ( in_array( strtolower( $charset ), $fixes, true ) ) {
$charset = 'utf8';
}
$charset_collate = "DEFAULT CHARACTER SET $charset";
}
if ( ! empty( $wpdb->collate ) ) {
$charset_collate .= " COLLATE=$wpdb->collate";
}
return $charset_collate;
}
/**
* Performs a $wpdb->query, and throws an exception if an error occurs
*
* @return bool true if query is performed ok, otherwise an exception is thrown
*/
protected function do_query( $wpdb, $sql ) {
if ( ! $this->live ) {
$this->queries[] = $sql;
return true;
}
// These are known queries without user input
// phpcs:ignore
$result = $wpdb->query( $sql );
if ( $result === false ) {
/* translators: 1: SQL string */
throw new Exception( sprintf( 'Failed to perform query "%s"', $sql ) );
}
return true;
}
/**
* Load a database upgrader class
*
* @return object Database upgrader
*/
public static function get( $version ) {
include_once dirname( __FILE__ ) . '/schema/' . str_replace( [ '..', '/' ], '', $version['file'] );
return new $version['class'];
}
private function has_stage( $stage ) {
return in_array( $stage, array_keys( $this->get_stages() ), true );
}
}
<?php
require_once __DIR__ . '/database-status.php';
require_once __DIR__ . '/database-upgrader.php';
class Red_Database {
/**
* Get all upgrades for a database version
*
* @return array Array of versions from self::get_upgrades()
*/
public function get_upgrades_for_version( $current_version, $current_stage ) {
if ( empty( $current_version ) ) {
return [
[
'version' => REDIRECTION_DB_VERSION,
'file' => 'latest.php',
'class' => 'Red_Latest_Database',
],
];
}
$upgraders = [];
$found = false;
foreach ( $this->get_upgrades() as $upgrade ) {
if ( ! $found ) {
$upgrader = Red_Database_Upgrader::get( $upgrade );
$stage_present = in_array( $current_stage, array_keys( $upgrader->get_stages() ), true );
$same_version = $current_stage === false && version_compare( $upgrade['version'], $current_version, 'gt' );
if ( $stage_present || $same_version ) {
$found = true;
}
}
if ( $found ) {
$upgraders[] = $upgrade;
}
}
return $upgraders;
}
/**
* Apply a particular upgrade stage
*
* @return mixed Result for upgrade
*/
public function apply_upgrade( Red_Database_Status $status ) {
$upgraders = $this->get_upgrades_for_version( $status->get_current_version(), $status->get_current_stage() );
if ( count( $upgraders ) === 0 ) {
$status->set_error( 'No upgrades found for version ' . $status->get_current_version() );
return;
}
if ( $status->get_current_stage() === false ) {
if ( $status->needs_installing() ) {
$status->start_install( $upgraders );
} else {
$status->start_upgrade( $upgraders );
}
}
// Look at first upgrade
$upgrader = Red_Database_Upgrader::get( $upgraders[0] );
// Perform the upgrade
$upgrader->perform_stage( $status );
if ( ! $status->is_error() ) {
$status->set_next_stage();
}
}
public static function apply_to_sites( $callback ) {
if ( is_multisite() && ( is_network_admin() || defined( 'WP_CLI' ) && WP_CLI ) ) {
$total = get_sites( [ 'count' => true ] );
$per_page = 100;
// Paginate through all sites and apply the callback
for ( $offset = 0; $offset < $total; $offset += $per_page ) {
array_map( function( $site ) use ( $callback ) {
switch_to_blog( $site->blog_id );
$callback();
restore_current_blog();
}, get_sites( [ 'number' => $per_page, 'offset' => $offset ] ) );
}
return;
}
$callback();
}
/**
* Get latest database installer
*
* @return object Red_Latest_Database
*/
public static function get_latest_database() {
include_once dirname( __FILE__ ) . '/schema/latest.php';
return new Red_Latest_Database();
}
/**
* List of all upgrades and their associated file
*
* @return array Database upgrade array
*/
public function get_upgrades() {
return [
[
'version' => '2.0.1',
'file' => '201.php',
'class' => 'Red_Database_201',
],
[
'version' => '2.1.16',
'file' => '216.php',
'class' => 'Red_Database_216',
],
[
'version' => '2.2',
'file' => '220.php',
'class' => 'Red_Database_220',
],
[
'version' => '2.3.1',
'file' => '231.php',
'class' => 'Red_Database_231',
],
[
'version' => '2.3.2',
'file' => '232.php',
'class' => 'Red_Database_232',
],
[
'version' => '2.3.3',
'file' => '233.php',
'class' => 'Red_Database_233',
],
[
'version' => '2.4',
'file' => '240.php',
'class' => 'Red_Database_240',
],
[
'version' => '4.0',
'file' => '400.php',
'class' => 'Red_Database_400',
],
[
'version' => '4.1',
'file' => '410.php',
'class' => 'Red_Database_410',
],
[
'version' => '4.2',
'file' => '420.php',
'class' => 'Red_Database_420',
],
];
}
}
<?php
// Note: not localised as the messages aren't important enough
class Red_Database_201 extends Red_Database_Upgrader {
public function get_stages() {
return [
'add_title_201' => 'Add titles to redirects',
];
}
protected function add_title_201( $wpdb ) {
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `title` varchar(50) NULL" );
}
}
<?php
// Note: not localised as the messages aren't important enough
class Red_Database_216 extends Red_Database_Upgrader {
public function get_stages() {
return [
'add_group_indices_216' => 'Add indices to groups',
'add_redirect_indices_216' => 'Add indices to redirects',
];
}
protected function add_group_indices_216( $wpdb ) {
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_groups` ADD INDEX(module_id)" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_groups` ADD INDEX(status)" );
return true;
}
protected function add_redirect_indices_216( $wpdb ) {
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX(url(191))" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX(status)" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX(regex)" );
return true;
}
}
<?php
// Note: not localised as the messages aren't important enough
class Red_Database_220 extends Red_Database_Upgrader {
public function get_stages() {
return [
'add_group_indices_220' => 'Add group indices to redirects',
'add_log_indices_220' => 'Add indices to logs',
];
}
protected function add_group_indices_220( $wpdb ) {
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX `group_idpos` (`group_id`,`position`)" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX `group` (`group_id`)" );
return true;
}
protected function add_log_indices_220( $wpdb ) {
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `created` (`created`)" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `redirection_id` (`redirection_id`)" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `ip` (`ip`)" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `group_id` (`group_id`)" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD INDEX `module_id` (`module_id`)" );
return true;
}
}
<?php
// Note: not localised as the messages aren't important enough
class Red_Database_231 extends Red_Database_Upgrader {
public function get_stages() {
return [
'remove_404_module_231' => 'Remove 404 module',
'create_404_table_231' => 'Create 404 table',
];
}
protected function remove_404_module_231( $wpdb ) {
return $this->do_query( $wpdb, "UPDATE {$wpdb->prefix}redirection_groups SET module_id=1 WHERE module_id=3" );
}
protected function create_404_table_231( $wpdb ) {
$this->do_query( $wpdb, $this->get_404_table( $wpdb ) );
}
private function get_404_table( $wpdb ) {
$charset_collate = $this->get_charset();
return "CREATE TABLE `{$wpdb->prefix}redirection_404` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
`url` varchar(255) NOT NULL DEFAULT '',
`agent` varchar(255) DEFAULT NULL,
`referrer` varchar(255) DEFAULT NULL,
`ip` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `created` (`created`),
KEY `url` (`url`(191)),
KEY `ip` (`ip`),
KEY `referrer` (`referrer`(191))
) $charset_collate";
}
}
<?php
// Note: not localised as the messages aren't important enough
class Red_Database_232 extends Red_Database_Upgrader {
public function get_stages() {
return [
'remove_modules_232' => 'Remove module table',
];
}
protected function remove_modules_232( $wpdb ) {
$this->do_query( $wpdb, "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_modules" );
return true;
}
}
<?php
// Note: not localised as the messages aren't important enough
class Red_Database_233 extends Red_Database_Upgrader {
public function get_stages() {
return [
'fix_invalid_groups_233' => 'Migrate any groups with invalid module ID',
];
}
protected function fix_invalid_groups_233( $wpdb ) {
$this->do_query( $wpdb, "UPDATE {$wpdb->prefix}redirection_groups SET module_id=1 WHERE module_id > 2" );
$latest = Red_Database::get_latest_database();
return $latest->create_groups( $wpdb );
}
}
<?php
/**
* There are several problems with 2.3.3 => 2.4 that this attempts to cope with:
* - some sites have a misconfigured IP column
* - some sites don't have any IP column
*/
class Red_Database_240 extends Red_Database_Upgrader {
public function get_stages() {
return [
'convert_int_ip_to_varchar_240' => 'Convert integer IP values to support IPv6',
'expand_log_ip_column_240' => 'Expand IP size in logs to support IPv6',
'convert_title_to_text_240' => 'Expand size of redirect titles',
'add_missing_index_240' => 'Add missing IP index to 404 logs',
];
}
private function has_ip_index( $wpdb ) {
$wpdb->hide_errors();
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
$wpdb->show_errors();
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'key `ip` (' ) !== false ) {
return true;
}
return false;
}
protected function has_varchar_ip( $wpdb ) {
$wpdb->hide_errors();
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
$wpdb->show_errors();
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), '`ip` varchar(45)' ) !== false ) {
return true;
}
return false;
}
protected function has_int_ip( $wpdb ) {
$wpdb->hide_errors();
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
$wpdb->show_errors();
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), '`ip` int' ) !== false ) {
return true;
}
return false;
}
protected function convert_int_ip_to_varchar_240( $wpdb ) {
if ( $this->has_int_ip( $wpdb ) ) {
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `ipaddress` VARCHAR(45) DEFAULT NULL AFTER `ip`" );
$this->do_query( $wpdb, "UPDATE {$wpdb->prefix}redirection_404 SET ipaddress=INET_NTOA(ip)" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP `ip`" );
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` CHANGE `ipaddress` `ip` VARCHAR(45) DEFAULT NULL" );
}
return true;
}
protected function expand_log_ip_column_240( $wpdb ) {
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` CHANGE `ip` `ip` VARCHAR(45) DEFAULT NULL" );
}
protected function add_missing_index_240( $wpdb ) {
if ( $this->has_ip_index( $wpdb ) ) {
// Remove index
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP INDEX ip" );
}
// Ensure we have an IP column
$this->convert_int_ip_to_varchar_240( $wpdb );
if ( ! $this->has_varchar_ip( $wpdb ) ) {
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `ip` VARCHAR(45) DEFAULT NULL" );
}
// Finally add the index
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD INDEX `ip` (`ip`)" );
}
protected function convert_title_to_text_240( $wpdb ) {
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` CHANGE `title` `title` text" );
}
}
<?php
class Red_Database_400 extends Red_Database_Upgrader {
public function get_stages() {
return [
'add_match_url_400' => 'Add a matched URL column',
'add_match_url_index' => 'Add match URL index',
'add_redirect_data_400' => 'Add column to store new flags',
'convert_existing_urls_400' => 'Convert existing URLs to new format',
];
}
private function has_column( $wpdb, $column ) {
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_items`", ARRAY_N );
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), strtolower( $column ) ) !== false ) {
return true;
}
return false;
}
private function has_match_index( $wpdb ) {
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_items`", ARRAY_N );
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'key `match_url' ) !== false ) {
return true;
}
return false;
}
protected function add_match_url_400( $wpdb ) {
if ( ! $this->has_column( $wpdb, '`match_url` varchar(2000)' ) ) {
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `match_url` VARCHAR(2000) NULL DEFAULT NULL AFTER `url`" );
}
return true;
}
protected function add_match_url_index( $wpdb ) {
if ( ! $this->has_match_index( $wpdb ) ) {
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD INDEX `match_url` (`match_url`(191))" );
}
}
protected function add_redirect_data_400( $wpdb ) {
if ( ! $this->has_column( $wpdb, '`match_data` TEXT' ) ) {
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_items` ADD `match_data` TEXT NULL DEFAULT NULL AFTER `match_url`" );
}
return true;
}
protected function convert_existing_urls_400( $wpdb ) {
// All regex get match_url=regex
$this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url='regex' WHERE regex=1" );
// Remove query part from all URLs and lowercase
$this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url=LOWER(url) WHERE regex=0" );
// Set exact match if query param present
$this->do_query( $wpdb, $wpdb->prepare( "UPDATE `{$wpdb->prefix}redirection_items` SET match_data=%s WHERE regex=0 AND match_url LIKE '%?%'", '{"source":{"flag_query":"exactorder"}}' ) );
// Trim the last / from a URL
$this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url=LEFT(match_url,LENGTH(match_url)-1) WHERE regex=0 AND match_url != '/' AND RIGHT(match_url, 1) = '/'" );
$this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url=REPLACE(match_url, '/?', '?') WHERE regex=0" );
// Any URL that is now empty becomes /
return $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url='/' WHERE match_url=''" );
}
}
<?php
class Red_Database_410 extends Red_Database_Upgrader {
public function get_stages() {
return [
'handle_double_slash' => 'Support double-slash URLs',
];
}
protected function handle_double_slash( $wpdb ) {
// Update any URL with a double slash at the end
$this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url=LOWER(LEFT(SUBSTRING_INDEX(url, '?', 1),LENGTH(SUBSTRING_INDEX(url, '?', 1)) - 1)) WHERE RIGHT(SUBSTRING_INDEX(url, '?', 1), 2) = '//' AND regex=0" );
// Any URL that is now empty becomes /
return $this->do_query( $wpdb, "UPDATE `{$wpdb->prefix}redirection_items` SET match_url='/' WHERE match_url=''" );
}
}
<?php
class Red_Database_420 extends Red_Database_Upgrader {
public function get_stages() {
return [
'add_extra_logging' => 'Add extra logging support',
'remove_module_id' => 'Remove module ID from logs',
'remove_group_id' => 'Remove group ID from logs',
'add_extra_404' => 'Add extra 404 logging support',
];
}
protected function remove_module_id( $wpdb ) {
if ( ! $this->has_module_id( $wpdb ) ) {
return true;
}
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` DROP `module_id`" );
}
protected function remove_group_id( $wpdb ) {
if ( ! $this->has_group_id( $wpdb ) ) {
return true;
}
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` DROP `group_id`" );
}
private function has_module_id( $wpdb ) {
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_logs`", ARRAY_N );
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'module_id' ) !== false ) {
return true;
}
return false;
}
private function has_group_id( $wpdb ) {
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_logs`", ARRAY_N );
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'group_id' ) !== false ) {
return true;
}
return false;
}
private function has_log_domain( $wpdb ) {
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_logs`", ARRAY_N );
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'domain` varchar' ) !== false ) {
return true;
}
return false;
}
private function has_404_domain( $wpdb ) {
$existing = $wpdb->get_row( "SHOW CREATE TABLE `{$wpdb->prefix}redirection_404`", ARRAY_N );
if ( isset( $existing[1] ) && strpos( strtolower( $existing[1] ), 'domain` varchar' ) !== false ) {
return true;
}
return false;
}
protected function add_extra_logging( $wpdb ) {
if ( $this->has_log_domain( $wpdb ) ) {
return true;
}
// Update any URL with a double slash at the end
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD `domain` VARCHAR(255) NULL DEFAULT NULL AFTER `url`" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD `http_code` INT(11) unsigned NOT NULL DEFAULT 0 AFTER `referrer`" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD `request_method` VARCHAR(10) NULL DEFAULT NULL AFTER `http_code`" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD `redirect_by` VARCHAR(50) NULL DEFAULT NULL AFTER `request_method`" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` ADD `request_data` MEDIUMTEXT NULL DEFAULT NULL AFTER `request_method`" );
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_logs` CHANGE COLUMN `agent` `agent` MEDIUMTEXT NULL" );
}
protected function add_extra_404( $wpdb ) {
if ( $this->has_404_domain( $wpdb ) ) {
return true;
}
// Update any URL with a double slash at the end
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `domain` VARCHAR(255) NULL DEFAULT NULL AFTER `url`" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `http_code` INT(11) unsigned NOT NULL DEFAULT 0 AFTER `referrer`" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `request_method` VARCHAR(10) NULL DEFAULT NULL AFTER `http_code`" );
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` ADD `request_data` MEDIUMTEXT NULL DEFAULT NULL AFTER `request_method`" );
// Same as log table
$this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` DROP INDEX `url`" );
return $this->do_query( $wpdb, "ALTER TABLE `{$wpdb->prefix}redirection_404` CHANGE COLUMN `url` `url` MEDIUMTEXT NOT NULL" );
}
}
<?php
/**
* Latest database schema
*/
class Red_Latest_Database extends Red_Database_Upgrader {
public function get_stages() {
return [
/* translators: displayed when installing the plugin */
'create_tables' => __( 'Install Redirection tables', 'redirection' ),
/* translators: displayed when installing the plugin */
'create_groups' => __( 'Create basic data', 'redirection' ),
];
}
/**
* Install the latest database
*
* @return bool|WP_Error true if installed, WP_Error otherwise
*/
public function install() {
global $wpdb;
foreach ( $this->get_stages() as $stage => $info ) {
$result = $this->$stage( $wpdb );
if ( is_wp_error( $result ) ) {
if ( $wpdb->last_error ) {
$result->add_data( $wpdb->last_error );
}
return $result;
}
}
red_set_options( array( 'database' => REDIRECTION_DB_VERSION ) );
return true;
}
/**
* Remove the database and any options (including unused ones)
*/
public function remove() {
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_items" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_logs" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_groups" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_modules" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_404" );
delete_option( 'redirection_lookup' );
delete_option( 'redirection_post' );
delete_option( 'redirection_root' );
delete_option( 'redirection_index' );
delete_option( 'redirection_options' );
delete_option( Red_Database_Status::OLD_DB_VERSION );
}
/**
* Return any tables that are missing from the database
*
* @return array Array of missing table names
*/
public function get_missing_tables() {
global $wpdb;
$tables = array_keys( $this->get_all_tables() );
$missing = [];
foreach ( $tables as $table ) {
$result = $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) );
if ( intval( $result, 10 ) !== 1 ) {
$missing[] = $table;
}
}
return $missing;
}
/**
* Get table schema for latest database tables
*
* @return array Database schema array
*/
public function get_table_schema() {
global $wpdb;
$tables = array_keys( $this->get_all_tables() );
$show = array();
foreach ( $tables as $table ) {
// These are known queries without user input
// phpcs:ignore
$row = $wpdb->get_row( 'SHOW CREATE TABLE ' . $table, ARRAY_N );
if ( $row ) {
$show = array_merge( $show, explode( "\n", $row[1] ) );
$show[] = '';
} else {
/* translators: 1: table name */
$show[] = sprintf( __( 'Table "%s" is missing', 'redirection' ), $table );
}
}
return $show;
}
/**
* Return array of table names and table schema
*
* @return array
*/
public function get_all_tables() {
global $wpdb;
$charset_collate = $this->get_charset();
return array(
"{$wpdb->prefix}redirection_items" => $this->create_items_sql( $wpdb->prefix, $charset_collate ),
"{$wpdb->prefix}redirection_groups" => $this->create_groups_sql( $wpdb->prefix, $charset_collate ),
"{$wpdb->prefix}redirection_logs" => $this->create_log_sql( $wpdb->prefix, $charset_collate ),
"{$wpdb->prefix}redirection_404" => $this->create_404_sql( $wpdb->prefix, $charset_collate ),
);
}
/**
* Creates default group information
*/
public function create_groups( $wpdb, $is_live = true ) {
if ( ! $is_live ) {
return true;
}
$defaults = [
[
'name' => __( 'Redirections', 'redirection' ),
'module_id' => 1,
'position' => 0,
],
[
'name' => __( 'Modified Posts', 'redirection' ),
'module_id' => 1,
'position' => 1,
],
];
$existing_groups = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" );
// Default groups
if ( intval( $existing_groups, 10 ) === 0 ) {
$wpdb->insert( $wpdb->prefix . 'redirection_groups', $defaults[0] );
$wpdb->insert( $wpdb->prefix . 'redirection_groups', $defaults[1] );
}
$group = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}redirection_groups LIMIT 1" );
if ( $group ) {
red_set_options( array( 'last_group_id' => $group->id ) );
}
return true;
}
/**
* Creates all the tables
*/
public function create_tables( $wpdb ) {
global $wpdb;
foreach ( $this->get_all_tables() as $table => $sql ) {
$sql = preg_replace( '/[ \t]{2,}/', '', $sql );
$this->do_query( $wpdb, $sql );
}
return true;
}
private function create_items_sql( $prefix, $charset_collate ) {
return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_items` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`url` mediumtext NOT NULL,
`match_url` VARCHAR(2000) DEFAULT NULL,
`match_data` TEXT,
`regex` INT(11) unsigned NOT NULL DEFAULT '0',
`position` INT(11) unsigned NOT NULL DEFAULT '0',
`last_count` INT(10) unsigned NOT NULL DEFAULT '0',
`last_access` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',
`group_id` INT(11) NOT NULL DEFAULT '0',
`status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
`action_type` VARCHAR(20) NOT NULL,
`action_code` INT(11) unsigned NOT NULL,
`action_data` MEDIUMTEXT,
`match_type` VARCHAR(20) NOT NULL,
`title` TEXT,
PRIMARY KEY (`id`),
KEY `url` (`url`(191)),
KEY `status` (`status`),
KEY `regex` (`regex`),
KEY `group_idpos` (`group_id`,`position`),
KEY `group` (`group_id`),
KEY `match_url` (`match_url`(191))
) $charset_collate";
}
private function create_groups_sql( $prefix, $charset_collate ) {
return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_groups` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`tracking` INT(11) NOT NULL DEFAULT '1',
`module_id` INT(11) unsigned NOT NULL DEFAULT '0',
`status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
`position` INT(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `module_id` (`module_id`),
KEY `status` (`status`)
) $charset_collate";
}
private function create_log_sql( $prefix, $charset_collate ) {
return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_logs` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
`url` MEDIUMTEXT NOT NULL,
`domain` VARCHAR(255) DEFAULT NULL,
`sent_to` MEDIUMTEXT,
`agent` MEDIUMTEXT,
`referrer` MEDIUMTEXT,
`http_code` INT(11) unsigned NOT NULL DEFAULT '0',
`request_method` VARCHAR(10) DEFAULT NULL,
`request_data` MEDIUMTEXT,
`redirect_by` VARCHAR(50) DEFAULT NULL,
`redirection_id` INT(11) unsigned DEFAULT NULL,
`ip` VARCHAR(45) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `created` (`created`),
KEY `redirection_id` (`redirection_id`),
KEY `ip` (`ip`)
) $charset_collate";
}
private function create_404_sql( $prefix, $charset_collate ) {
return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_404` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
`url` MEDIUMTEXT NOT NULL,
`domain` VARCHAR(255) DEFAULT NULL,
`agent` VARCHAR(255) DEFAULT NULL,
`referrer` VARCHAR(255) DEFAULT NULL,
`http_code` INT(11) unsigned NOT NULL DEFAULT '0',
`request_method` VARCHAR(10) DEFAULT NULL,
`request_data` MEDIUMTEXT,
`ip` VARCHAR(45) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `created` (`created`),
KEY `referrer` (`referrer`(191)),
KEY `ip` (`ip`)
) $charset_collate";
}
}
<?php
class Red_Apache_File extends Red_FileIO {
public function force_download() {
parent::force_download();
header( 'Content-Type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename="' . $this->export_filename( 'htaccess' ) . '"' );
}
public function get_data( array $items, array $groups ) {
include_once dirname( dirname( __FILE__ ) ) . '/models/htaccess.php';
$htaccess = new Red_Htaccess();
foreach ( $items as $item ) {
$htaccess->add( $item );
}
return $htaccess->get() . PHP_EOL;
}
public function load( $group, $filename, $data ) {
// Remove any comments
$data = str_replace( "\n", "\r", $data );
// Split it into lines
$lines = array_filter( explode( "\r", $data ) );
$count = 0;
foreach ( (array) $lines as $line ) {
$item = $this->get_as_item( $line );
if ( $item ) {
$item['group_id'] = $group;
$redirect = Red_Item::create( $item );
if ( ! is_wp_error( $redirect ) ) {
$count++;
}
}
}
return $count;
}
public function get_as_item( $line ) {
$item = false;
if ( preg_match( '@rewriterule\s+(.*?)\s+(.*?)\s+(\[.*\])*@i', $line, $matches ) > 0 ) {
$item = array(
'url' => $this->regex_url( $matches[1] ),
'match_type' => 'url',
'action_type' => 'url',
'action_data' => array( 'url' => $this->decode_url( $matches[2] ) ),
'action_code' => $this->get_code( $matches[3] ),
'regex' => $this->is_regex( $matches[1] ),
);
} elseif ( preg_match( '@Redirect\s+(.*?)\s+"(.*?)"\s+(.*)@i', $line, $matches ) > 0 || preg_match( '@Redirect\s+(.*?)\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) {
$item = array(
'url' => $this->decode_url( $matches[2] ),
'match_type' => 'url',
'action_type' => 'url',
'action_data' => array( 'url' => $this->decode_url( $matches[3] ) ),
'action_code' => $this->get_code( $matches[1] ),
);
} elseif ( preg_match( '@Redirect\s+"(.*?)"\s+(.*)@i', $line, $matches ) > 0 || preg_match( '@Redirect\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) {
$item = array(
'url' => $this->decode_url( $matches[1] ),
'match_type' => 'url',
'action_type' => 'url',
'action_data' => array( 'url' => $this->decode_url( $matches[2] ) ),
'action_code' => 302,
);
} elseif ( preg_match( '@Redirectmatch\s+(.*?)\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) {
$item = array(
'url' => $this->decode_url( $matches[2] ),
'match_type' => 'url',
'action_type' => 'url',
'action_data' => array( 'url' => $this->decode_url( $matches[3] ) ),
'action_code' => $this->get_code( $matches[1] ),
'regex' => true,
);
} elseif ( preg_match( '@Redirectmatch\s+(.*?)\s+(.*)@i', $line, $matches ) > 0 ) {
$item = array(
'url' => $this->decode_url( $matches[1] ),
'match_type' => 'url',
'action_type' => 'url',
'action_data' => array( 'url' => $this->decode_url( $matches[2] ) ),
'action_code' => 302,
'regex' => true,
);
}
if ( $item ) {
$item['action_type'] = 'url';
$item['match_type'] = 'url';
if ( $item['action_code'] === 0 ) {
$item['action_type'] = 'pass';
}
return $item;
}
return false;
}
private function decode_url( $url ) {
$url = rawurldecode( $url );
// Replace quoted slashes
$url = preg_replace( '@\\\/@', '/', $url );
// Ensure escaped '.' is still escaped
$url = preg_replace( '@\\\\.@', '\\\\.', $url );
return $url;
}
private function is_str_regex( $url ) {
$regex = '()[]$^?+.';
$escape = false;
$len = strlen( $url );
for ( $x = 0; $x < $len; $x++ ) {
$escape = false;
$char = substr( $url, $x, 1 );
if ( $char === '\\' ) {
$escape = true;
} elseif ( strpos( $regex, $char ) !== false && ! $escape ) {
return true;
}
}
return false;
}
private function is_regex( $url ) {
if ( $this->is_str_regex( $url ) ) {
$tmp = ltrim( $url, '^' );
$tmp = rtrim( $tmp, '$' );
if ( $this->is_str_regex( $tmp ) ) {
return true;
}
}
return false;
}
private function regex_url( $url ) {
$url = $this->decode_url( $url );
if ( $this->is_str_regex( $url ) ) {
$tmp = ltrim( $url, '^' );
$tmp = rtrim( $tmp, '$' );
if ( $this->is_str_regex( $tmp ) ) {
return '^/' . ltrim( $tmp, '/' );
}
return '/' . ltrim( $tmp, '/' );
}
return $this->decode_url( $url );
}
private function get_code( $code ) {
if ( strpos( $code, '301' ) !== false || stripos( $code, 'permanent' ) !== false ) {
return 301;
} elseif ( strpos( $code, '302' ) !== false ) {
return 302;
} elseif ( strpos( $code, '307' ) !== false || stripos( $code, 'seeother' ) !== false ) {
return 307;
} elseif ( strpos( $code, '404' ) !== false || stripos( $code, 'forbidden' ) !== false || strpos( $code, 'F' ) !== false ) {
return 404;
} elseif ( strpos( $code, '410' ) !== false || stripos( $code, 'gone' ) !== false || strpos( $code, 'G' ) !== false ) {
return 410;
}
return 302;
}
}
<?php
class Red_Csv_File extends Red_FileIO {
const CSV_SOURCE = 0;
const CSV_TARGET = 1;
const CSV_REGEX = 2;
const CSV_CODE = 3;
public function force_download() {
parent::force_download();
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment; filename="' . $this->export_filename( 'csv' ) . '"' );
}
public function get_data( array $items, array $groups ) {
$lines = [ implode( ',', array( 'source', 'target', 'regex', 'type', 'code', 'match', 'hits', 'title' ) ) ];
foreach ( $items as $line ) {
$lines[] = $this->item_as_csv( $line );
}
return implode( PHP_EOL, $lines ) . PHP_EOL;
}
public function item_as_csv( $item ) {
$data = $item->match->get_data();
$data = isset( $data['url'] ) ? $data = $data['url'] : '*';
$csv = array(
$item->get_url(),
$data,
$item->is_regex() ? 1 : 0,
$item->get_action_code(),
$item->get_action_type(),
$item->get_hits(),
$item->get_title(),
);
$csv = array_map( array( $this, 'escape_csv' ), $csv );
return implode( ',', $csv );
}
public function escape_csv( $item ) {
return '"' . str_replace( '"', '""', $item ) . '"';
}
public function load( $group, $filename, $data ) {
ini_set( 'auto_detect_line_endings', true );
$file = fopen( $filename, 'r' );
ini_set( 'auto_detect_line_endings', false );
if ( $file ) {
$separators = [
',',
';',
'|',
];
foreach ( $separators as $separator ) {
fseek( $file, 0 );
$count = $this->load_from_file( $group, $file, $separator );
if ( $count > 0 ) {
return $count;
}
}
}
return 0;
}
public function load_from_file( $group_id, $file, $separator ) {
$count = 0;
while ( ( $csv = fgetcsv( $file, 5000, $separator ) ) ) {
$item = $this->csv_as_item( $csv, $group_id );
if ( $item ) {
$created = Red_Item::create( $item );
if ( ! is_wp_error( $created ) ) {
$count++;
}
}
}
return $count;
}
private function get_valid_code( $code ) {
if ( get_status_header_desc( $code ) !== '' ) {
return intval( $code, 10 );
}
return 301;
}
public function csv_as_item( $csv, $group ) {
if ( count( $csv ) > 1 && $csv[ self::CSV_SOURCE ] !== 'source' && $csv[ self::CSV_TARGET ] !== 'target' ) {
return array(
'url' => trim( $csv[ self::CSV_SOURCE ] ),
'action_data' => array( 'url' => trim( $csv[ self::CSV_TARGET ] ) ),
'regex' => isset( $csv[ self::CSV_REGEX ] ) ? $this->parse_regex( $csv[ self::CSV_REGEX ] ) : $this->is_regex( $csv[ self::CSV_SOURCE ] ),
'group_id' => $group,
'match_type' => 'url',
'action_type' => 'url',
'action_code' => isset( $csv[ self::CSV_CODE ] ) ? $this->get_valid_code( $csv[ self::CSV_CODE ] ) : 301,
);
}
return false;
}
private function parse_regex( $value ) {
return intval( $value, 10 ) === 1 ? true : false;
}
private function is_regex( $url ) {
$regex = '()[]$^*';
if ( strpbrk( $url, $regex ) === false ) {
return false;
}
return true;
}
}
<?php
class Red_Json_File extends Red_FileIO {
public function force_download() {
parent::force_download();
header( 'Content-Type: application/json' );
header( 'Content-Disposition: attachment; filename="' . $this->export_filename( 'json' ) . '"' );
}
public function get_data( array $items, array $groups ) {
$version = red_get_plugin_data( dirname( dirname( __FILE__ ) ) . '/redirection.php' );
$items = array(
'plugin' => array(
'version' => trim( $version['Version'] ),
'date' => date( 'r' ),
),
'groups' => $groups,
'redirects' => array_map( function( $item ) {
return $item->to_json();
}, $items ),
);
return wp_json_encode( $items, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) . PHP_EOL;
}
public function load( $group, $filename, $data ) {
global $wpdb;
$count = 0;
$json = @json_decode( $data, true );
if ( $json === false ) {
return 0;
}
// Import groups
$groups = array();
$group_map = array();
if ( isset( $json['groups'] ) ) {
foreach ( $json['groups'] as $group ) {
$old_group_id = $group['id'];
unset( $group['id'] );
$group = Red_Group::create( $group['name'], $group['module_id'], $group['enabled'] ? true : false );
if ( $group ) {
$group_map[ $old_group_id ] = $group->get_id();
}
}
}
unset( $json['groups'] );
// Import redirects
if ( isset( $json['redirects'] ) ) {
foreach ( $json['redirects'] as $pos => $redirect ) {
unset( $redirect['id'] );
if ( ! isset( $group_map[ $redirect['group_id'] ] ) ) {
$new_group = Red_Group::create( 'Group', 1 );
$group_map[ $redirect['group_id'] ] = $new_group->get_id();
}
if ( $redirect['match_type'] === 'url' && isset( $redirect['action_data'] ) && ! is_array( $redirect['action_data'] ) ) {
$redirect['action_data'] = array( 'url' => $redirect['action_data'] );
}
$redirect['group_id'] = $group_map[ $redirect['group_id'] ];
Red_Item::create( $redirect );
$count++;
// Helps reduce memory usage
unset( $json['redirects'][ $pos ] );
$wpdb->queries = array();
$wpdb->num_queries = 0;
}
}
return $count;
}
}
<?php
class Red_Nginx_File extends Red_FileIO {
public function force_download() {
parent::force_download();
header( 'Content-Type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename="' . $this->export_filename( 'nginx' ) . '"' );
}
public function get_data( array $items, array $groups ) {
$lines = array();
$version = red_get_plugin_data( dirname( dirname( __FILE__ ) ) . '/redirection.php' );
$lines[] = '# Created by Redirection';
$lines[] = '# ' . date( 'r' );
$lines[] = '# Redirection ' . trim( $version['Version'] ) . ' - https://redirection.me';
$lines[] = '';
$lines[] = 'server {';
$parts = array();
foreach ( $items as $item ) {
if ( $item->is_enabled() ) {
$parts[] = $this->get_nginx_item( $item );
}
}
$lines = array_merge( $lines, array_filter( $parts ) );
$lines[] = '}';
$lines[] = '';
$lines[] = '# End of Redirection';
return implode( PHP_EOL, $lines ) . PHP_EOL;
}
private function get_redirect_code( Red_Item $item ) {
if ( $item->get_action_code() === 301 ) {
return 'permanent';
}
return 'redirect';
}
function load( $group, $data, $filename = '' ) {
return 0;
}
private function get_nginx_item( Red_Item $item ) {
$target = 'add_' . $item->get_match_type();
if ( method_exists( $this, $target ) ) {
return ' ' . $this->$target( $item, $item->get_match_data() );
}
return false;
}
private function add_url( Red_Item $item, array $match_data ) {
return $this->get_redirect( $item->get_url(), $item->get_action_data(), $this->get_redirect_code( $item ), $match_data['source'] );
}
private function add_agent( Red_Item $item, array $match_data ) {
if ( $item->match->url_from ) {
$lines[] = 'if ( $http_user_agent ~* ^' . $item->match->user_agent . '$ ) {';
$lines[] = ' ' . $this->get_redirect( $item->get_url(), $item->match->url_from, $this->get_redirect_code( $item ), $match_data['source'] );
$lines[] = ' }';
}
if ( $item->match->url_notfrom ) {
$lines[] = 'if ( $http_user_agent !~* ^' . $item->match->user_agent . '$ ) {';
$lines[] = ' ' . $this->get_redirect( $item->get_url(), $item->match->url_notfrom, $this->get_redirect_code( $item ), $match_data['source'] );
$lines[] = ' }';
}
return implode( "\n", $lines );
}
private function add_referrer( Red_Item $item, array $match_data ) {
if ( $item->match->url_from ) {
$lines[] = 'if ( $http_referer ~* ^' . $item->match->referrer . '$ ) {';
$lines[] = ' ' . $this->get_redirect( $item->get_url(), $item->match->url_from, $this->get_redirect_code( $item ), $match_data['source'] );
$lines[] = ' }';
}
if ( $item->match->url_notfrom ) {
$lines[] = 'if ( $http_referer !~* ^' . $item->match->referrer . '$ ) {';
$lines[] = ' ' . $this->get_redirect( $item->get_url(), $item->match->url_notfrom, $this->get_redirect_code( $item ), $match_data['source'] );
$lines[] = ' }';
}
return implode( "\n", $lines );
}
private function get_redirect( $line, $target, $code, $source ) {
// Remove any existing start/end from a regex
$line = ltrim( $line, '^' );
$line = rtrim( $line, '$' );
if ( isset( $source['flag_case'] ) && $source['flag_case'] ) {
$line = '(?i)^' . $line;
} else {
$line = '^' . $line;
}
$line = preg_replace( "/[\r\n\t].*?$/s", '', $line );
$line = preg_replace( '/[^\PC\s]/u', '', $line );
$target = preg_replace( "/[\r\n\t].*?$/s", '', $target );
$target = preg_replace( '/[^\PC\s]/u', '', $target );
return 'rewrite ' . $line . '$ ' . $target . ' ' . $code . ';';
}
}
<?php
class Red_Rss_File extends Red_FileIO {
public function force_download() {
header( 'Content-type: text/xml; charset=' . get_option( 'blog_charset' ), true );
}
public function get_data( array $items, array $groups ) {
$xml = '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . ">\r\n";
ob_start();
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Redirection - <?php bloginfo_rss( 'name' ); ?></title>
<link><?php esc_url( bloginfo_rss( 'url' ) ); ?></link>
<description><?php esc_html( bloginfo_rss( 'description' ) ); ?></description>
<pubDate><?php echo esc_html( mysql2date( 'D, d M Y H:i:s +0000', get_lastpostmodified( 'GMT' ), false ) ); ?></pubDate>
<generator>
<?php echo esc_html( 'http://wordpress.org/?v=' ); ?>
<?php bloginfo_rss( 'version' ); ?>
</generator>
<language><?php echo esc_html( get_option( 'rss_language' ) ); ?></language>
<?php foreach ( $items as $log ) : ?>
<item>
<title><?php echo esc_html( $log->get_url() ); ?></title>
<link><![CDATA[<?php echo esc_url( home_url() ) . esc_url( $log->get_url() ); ?>]]></link>
<pubDate><?php echo esc_html( date( 'D, d M Y H:i:s +0000', intval( $log->get_last_hit(), 10 ) ) ); ?></pubDate>
<guid isPermaLink="false"><?php echo esc_html( $log->get_id() ); ?></guid>
<description><?php echo esc_html( $log->get_url() ); ?></description>
</item>
<?php endforeach; ?>
</channel>
</rss>
<?php
$xml .= ob_get_contents();
ob_end_clean();
return $xml;
}
function load( $group, $data, $filename = '' ) {
}
}
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.