| @@ -1,123 +1,387 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -include_once dirname( __FILE__ ) . '/api/api-group.php'; | |
| 4 | -include_once dirname( __FILE__ ) . '/api/api-redirect.php'; | |
| 5 | -include_once dirname( __FILE__ ) . '/api/api-log.php'; | |
| 6 | -include_once dirname( __FILE__ ) . '/api/api-404.php'; | |
| 7 | -include_once dirname( __FILE__ ) . '/api/api-settings.php'; | |
| 8 | -include_once dirname( __FILE__ ) . '/api/api-plugin.php'; | |
| 9 | -include_once dirname( __FILE__ ) . '/api/api-import.php'; | |
| 10 | -include_once dirname( __FILE__ ) . '/api/api-export.php'; | |
| 3 | +// Todo: use the JSON API | |
| 4 | +class Redirection_Api { | |
| 5 | + public $endpoints = array( | |
| 6 | + 'load_settings', | |
| 7 | + 'save_settings', | |
| 8 | + 'get_logs', | |
| 9 | + 'log_action', | |
| 10 | + 'delete_all', | |
| 11 | + 'delete_plugin', | |
| 12 | + 'get_redirect', | |
| 13 | + 'set_redirect', | |
| 14 | + 'redirect_action', | |
| 15 | + 'get_group', | |
| 16 | + 'set_group', | |
| 17 | + 'group_action', | |
| 18 | + 'import_data', | |
| 19 | + 'export_data', | |
| 20 | + ); | |
| 11 | 21 | |
| 12 | -define( 'REDIRECTION_API_NAMESPACE', 'redirection/v1' ); | |
| 22 | + public function __construct() { | |
| 23 | + foreach ( $this->endpoints as $point ) { | |
| 24 | + add_action( 'wp_ajax_red_'.$point, array( $this, 'check_auth' ), 9 ); | |
| 25 | + add_action( 'wp_ajax_red_'.$point, array( $this, 'ajax_'.$point ), 10 ); | |
| 26 | + } | |
| 27 | + } | |
| 13 | 28 | |
| 14 | -class Redirection_Api_Route { | |
| 15 | - protected function add_error_details( WP_Error $error, $line, $code = 400 ) { | |
| 16 | - global $wpdb; | |
| 29 | + public function check_auth( $params ) { | |
| 30 | + if ( check_ajax_referer( 'wp_rest', false, false ) === false ) { | |
| 31 | + wp_die( $this->output_ajax_response( array( 'error' => __( 'Unable to perform action' ).' - bad nonce "wp_rest" ('.__LINE__.')' ) ) ); | |
| 32 | + } | |
| 17 | 33 | |
| 18 | - $data = array( | |
| 19 | - 'status' => $code, | |
| 20 | - 'error_code' => $line, | |
| 21 | - ); | |
| 34 | + if ( $this->user_has_access() === false ) { | |
| 35 | + wp_die( $this->output_ajax_response( array( 'error' => __( 'No permissions to perform action ('.__LINE__.')' ) ) ) ); | |
| 36 | + } | |
| 37 | + } | |
| 22 | 38 | |
| 23 | - if ( isset( $wpdb->last_error ) && $wpdb->last_error ) { | |
| 24 | - $data['wpdb'] = $wpdb->last_error; | |
| 39 | + private function get_params( $params ) { | |
| 40 | + if ( empty( $params ) ) { | |
| 41 | + $params = $_POST; | |
| 25 | 42 | } |
| 26 | 43 | |
| 27 | - $error->add_data( $data ); | |
| 28 | - return $error; | |
| 44 | + return $params; | |
| 29 | 45 | } |
| 30 | 46 | |
| 31 | - public function permission_callback( WP_REST_Request $request ) { | |
| 32 | - return current_user_can( apply_filters( 'redirection_role', 'manage_options' ) ); | |
| 47 | + public function ajax_import_data( $params ) { | |
| 48 | + $params = $this->get_params( $params ); | |
| 49 | + $upload = isset( $_FILES[ 'file' ] ) ? $_FILES[ 'file' ] : false; | |
| 50 | + $group_id = isset( $params['group'] ) ? intval( $params['group'], 10 ) : 0; | |
| 51 | + | |
| 52 | + $result = array( 'error' => 'Invalid file ('.__LINE__.')' ); | |
| 53 | + if ( $upload && is_uploaded_file( $upload['tmp_name'] ) ) { | |
| 54 | + $result = array( 'error' => 'Invalid group ('.__LINE__.')' ); | |
| 55 | + | |
| 56 | + $count = Red_FileIO::import( $group_id, $upload ); | |
| 57 | + if ( $count !== false ) { | |
| 58 | + $result = array( | |
| 59 | + 'imported' => $count, | |
| 60 | + ); | |
| 61 | + } | |
| 62 | + } | |
| 63 | + | |
| 64 | + return $this->output_ajax_response( $result ); | |
| 33 | 65 | } |
| 34 | 66 | |
| 35 | - public function get_route( $method, $callback ) { | |
| 36 | - return array( | |
| 37 | - 'methods' => $method, | |
| 38 | - 'callback' => array( $this, $callback ), | |
| 39 | - 'permission_callback' => array( $this, 'permission_callback' ), | |
| 40 | - ); | |
| 67 | + public function ajax_export_data( $params ) { | |
| 68 | + $params = $this->get_params( $params ); | |
| 69 | + $moduleId = isset( $params['module'] ) ? intval( $params['module'], 10 ) : false; | |
| 70 | + $format = 'json'; | |
| 71 | + | |
| 72 | + if ( isset( $params['format'] ) && in_array( $params['format'], array( 'csv', 'apache', 'nginx', 'json' ) ) ) { | |
| 73 | + $format = $params['format']; | |
| 74 | + } | |
| 75 | + | |
| 76 | + $result = array( 'error' => 'Invalid module ('.__LINE__.')' ); | |
| 77 | + | |
| 78 | + $export = Red_FileIO::export( $moduleId, $format ); | |
| 79 | + if ( $export !== false ) { | |
| 80 | + $result = array( | |
| 81 | + 'data' => $export['data'], | |
| 82 | + 'total' => $export['total'], | |
| 83 | + ); | |
| 84 | + } | |
| 85 | + | |
| 86 | + return $this->output_ajax_response( $result ); | |
| 41 | 87 | } |
| 42 | -} | |
| 43 | 88 | |
| 44 | -class Redirection_Api_Filter_Route extends Redirection_Api_Route { | |
| 45 | - protected function get_filter_args( $filter_fields, $order_fields ) { | |
| 46 | - return array( | |
| 47 | - 'filterBy' => array( | |
| 48 | - 'description' => 'Field to filter by', | |
| 49 | - 'type' => 'enum', | |
| 50 | - 'enum' => $filter_fields, | |
| 51 | - ), | |
| 52 | - 'filter' => array( | |
| 53 | - 'description' => 'Value to filter by', | |
| 54 | - 'type' => 'string', | |
| 55 | - ), | |
| 56 | - 'orderby' => array( | |
| 57 | - 'description' => 'Field to order results by', | |
| 58 | - 'type' => 'enum', | |
| 59 | - 'enum' => $order_fields, | |
| 60 | - ), | |
| 61 | - 'direction' => array( | |
| 62 | - 'description' => 'Direction of ordered results', | |
| 63 | - 'type' => 'enum', | |
| 64 | - 'default' => 'desc', | |
| 65 | - 'enum' => array( 'asc', 'desc' ), | |
| 66 | - ), | |
| 67 | - 'per_page' => array( | |
| 68 | - 'description' => 'Number of results per page', | |
| 69 | - 'type' => 'integer', | |
| 70 | - 'default' => 25, | |
| 71 | - 'minimum' => 5, | |
| 72 | - 'maximum' => RED_MAX_PER_PAGE, | |
| 73 | - ), | |
| 74 | - 'page' => array( | |
| 75 | - 'description' => 'Page offset', | |
| 76 | - 'type' => 'integer', | |
| 77 | - 'minimum' => 0, | |
| 78 | - 'default' => 0, | |
| 79 | - ), | |
| 80 | - ); | |
| 89 | + public function ajax_group_action( $params ) { | |
| 90 | + $params = $this->get_params( $params ); | |
| 91 | + | |
| 92 | + $action = false; | |
| 93 | + $items = array(); | |
| 94 | + if ( isset( $params['items'] ) ) { | |
| 95 | + $items = array_map( 'intval', explode( ',', $params['items'] ) ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + if ( isset( $params['bulk'] ) && in_array( $params['bulk'], array( 'delete', 'enable', 'disable' ) ) ) { | |
| 99 | + $action = $params['bulk']; | |
| 100 | + | |
| 101 | + foreach ( $items as $item ) { | |
| 102 | + $group = Red_Group::get( intval( $item, 10 ) ); | |
| 103 | + | |
| 104 | + if ( $group ) { | |
| 105 | + if ( $action === 'delete' ) { | |
| 106 | + $group->delete(); | |
| 107 | + } else if ( $action === 'disable' ) { | |
| 108 | + $group->disable(); | |
| 109 | + } else if ( $action === 'enable' ) { | |
| 110 | + $group->enable(); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + } | |
| 114 | + } | |
| 115 | + | |
| 116 | + return $this->output_ajax_response( Red_Group::get_filtered( $params ) ); | |
| 81 | 117 | } |
| 82 | 118 | |
| 83 | - public function register_bulk( $namespace, $route, $filters, $orders, $callback ) { | |
| 84 | - register_rest_route( $namespace, $route, array( | |
| 85 | - $this->get_route( WP_REST_Server::EDITABLE, $callback ), | |
| 86 | - 'args' => array_merge( $this->get_filter_args( $filters, $orders ), array( | |
| 87 | - 'items' => array( | |
| 88 | - 'description' => 'Comma separated list of item IDs to perform action on', | |
| 89 | - 'type' => 'string|integer', | |
| 90 | - 'required' => true, | |
| 91 | - ), | |
| 92 | - ) ), | |
| 119 | + public function ajax_get_group( $params ) { | |
| 120 | + $params = $this->get_params( $params ); | |
| 121 | + | |
| 122 | + return $this->output_ajax_response( Red_Group::get_filtered( $params ) ); | |
| 123 | + } | |
| 124 | + | |
| 125 | + public function ajax_set_group( $params ) { | |
| 126 | + $params = $this->get_params( $params ); | |
| 127 | + | |
| 128 | + $groupId = 0; | |
| 129 | + $name = ''; | |
| 130 | + $moduleId = 0; | |
| 131 | + if ( isset( $params['id'] ) ) { | |
| 132 | + $groupId = intval( $params['id'], 10 ); | |
| 133 | + } | |
| 134 | + | |
| 135 | + $result = array( 'error' => 'Invalid group or parameters ('.__LINE__.')' ); | |
| 136 | + if ( $groupId > 0 ) { | |
| 137 | + $group = Red_Group::get( $groupId ); | |
| 138 | + | |
| 139 | + if ( $group && isset( $params['name'] ) && isset( $params['moduleId'] ) ) { | |
| 140 | + if ( $group->update( $params ) ) { | |
| 141 | + $result = array( 'item' => $group->to_json() ); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + } else { | |
| 145 | + $group = Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 ); | |
| 146 | + | |
| 147 | + if ( $group ) { | |
| 148 | + $result = Red_Group::get_filtered( $params ); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + return $this->output_ajax_response( $result ); | |
| 153 | + } | |
| 154 | + | |
| 155 | + public function ajax_get_redirect( $params ) { | |
| 156 | + $params = $this->get_params( $params ); | |
| 157 | + | |
| 158 | + return $this->output_ajax_response( Red_Item::get_filtered( $params ) ); | |
| 159 | + } | |
| 160 | + | |
| 161 | + public function ajax_set_redirect( $params ) { | |
| 162 | + $params = $this->get_params( $params ); | |
| 163 | + | |
| 164 | + $redirectId = 0; | |
| 165 | + if ( isset( $params['id'] ) ) { | |
| 166 | + $redirectId = intval( $params['id'], 10 ); | |
| 167 | + } | |
| 168 | + | |
| 169 | + $result = array( 'error' => 'Invalid redirect details ('.__LINE__.')' ); | |
| 170 | + if ( $redirectId === 0 ) { | |
| 171 | + $redirect = Red_Item::create( $params ); | |
| 172 | + | |
| 173 | + if ( is_wp_error( $redirect ) ) { | |
| 174 | + $result = array( 'error' => $redirect->get_error_message() ); | |
| 175 | + } else { | |
| 176 | + $result = Red_Item::get_filtered( $params ); | |
| 177 | + } | |
| 178 | + } else { | |
| 179 | + $redirect = Red_Item::get_by_id( $redirectId ); | |
| 180 | + | |
| 181 | + if ( $redirect ) { | |
| 182 | + $result = $redirect->update( $params ); | |
| 183 | + | |
| 184 | + if ( is_wp_error( $result ) ) { | |
| 185 | + $result = array( 'error' => $redirect->get_error_message() ); | |
| 186 | + } else { | |
| 187 | + $result = array( 'item' => $redirect->to_json() ); | |
| 188 | + } | |
| 189 | + } | |
| 190 | + } | |
| 191 | + | |
| 192 | + return $this->output_ajax_response( $result ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + public function ajax_redirect_action( $params ) { | |
| 196 | + $params = $this->get_params( $params ); | |
| 197 | + | |
| 198 | + $action = false; | |
| 199 | + $items = array(); | |
| 200 | + if ( isset( $params['items'] ) ) { | |
| 201 | + $items = array_map( 'intval', explode( ',', $params['items'] ) ); | |
| 202 | + } | |
| 203 | + if ( isset( $params['bulk'] ) && in_array( $params['bulk'], array( 'delete', 'enable', 'disable', 'reset' ) ) ) { | |
| 204 | + $action = $params['bulk']; | |
| 205 | + | |
| 206 | + foreach ( $items as $item ) { | |
| 207 | + $redirect = Red_Item::get_by_id( intval( $item, 10 ) ); | |
| 208 | + | |
| 209 | + if ( $redirect ) { | |
| 210 | + if ( $action === 'delete' ) { | |
| 211 | + $redirect->delete(); | |
| 212 | + } else if ( $action === 'disable' ) { | |
| 213 | + $redirect->disable(); | |
| 214 | + } else if ( $action === 'enable' ) { | |
| 215 | + $redirect->enable(); | |
| 216 | + } else if ( $action === 'reset' ) { | |
| 217 | + $redirect->reset(); | |
| 218 | + } | |
| 219 | + } | |
| 220 | + } | |
| 221 | + } | |
| 222 | + | |
| 223 | + return $this->output_ajax_response( Red_Item::get_filtered( $params ) ); | |
| 224 | + } | |
| 225 | + | |
| 226 | + public function ajax_delete_plugin() { | |
| 227 | + $plugin = Redirection_Admin::init(); | |
| 228 | + $plugin->plugin_uninstall(); | |
| 229 | + | |
| 230 | + $current = get_option( 'active_plugins' ); | |
| 231 | + array_splice( $current, array_search( basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), $current ), 1 ); | |
| 232 | + update_option( 'active_plugins', $current ); | |
| 233 | + | |
| 234 | + return $this->output_ajax_response( array( 'location' => admin_url().'plugins.php' ) ); | |
| 235 | + } | |
| 236 | + | |
| 237 | + public function ajax_load_settings() { | |
| 238 | + return $this->output_ajax_response( array( | |
| 239 | + 'settings' => red_get_options(), | |
| 240 | + 'groups' => $this->groups_to_json( Red_Group::get_for_select() ), | |
| 241 | + 'installed' => get_home_path(), | |
| 93 | 242 | ) ); |
| 94 | 243 | } |
| 95 | -} | |
| 96 | 244 | |
| 97 | -class Redirection_Api { | |
| 98 | - private static $instance = null; | |
| 99 | - private $routes = array(); | |
| 245 | + public function ajax_save_settings( $settings = array() ) { | |
| 246 | + $settings = $this->get_params( $settings ); | |
| 247 | + $options = red_get_options(); | |
| 100 | 248 | |
| 101 | - static function init() { | |
| 102 | - if ( is_null( self::$instance ) ) { | |
| 103 | - self::$instance = new Redirection_Api(); | |
| 249 | + if ( isset( $settings['monitor_post'] ) ) { | |
| 250 | + $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) ); | |
| 104 | 251 | } |
| 105 | 252 | |
| 106 | - return self::$instance; | |
| 253 | + if ( isset( $settings['auto_target'] ) ) { | |
| 254 | + $options['auto_target'] = stripslashes( $settings['auto_target'] ); | |
| 255 | + } | |
| 256 | + | |
| 257 | + if ( isset( $settings['support'] ) ) { | |
| 258 | + $options['support'] = $settings['support'] === 'true' ? true : false; | |
| 259 | + } | |
| 260 | + | |
| 261 | + if ( isset( $settings['token'] ) ) { | |
| 262 | + $options['token'] = stripslashes( $settings['token'] ); | |
| 263 | + } | |
| 264 | + | |
| 265 | + if ( !isset( $settings['token'] ) || trim( $options['token'] ) === '' ) { | |
| 266 | + $options['token'] = md5( uniqid() ); | |
| 267 | + } | |
| 268 | + | |
| 269 | + if ( isset( $settings['newsletter'] ) ) { | |
| 270 | + $options['newsletter'] = $settings['newsletter'] === 'true' ? true : false; | |
| 271 | + } | |
| 272 | + | |
| 273 | + if ( isset( $settings['expire_redirect'] ) ) { | |
| 274 | + $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) ); | |
| 275 | + } | |
| 276 | + | |
| 277 | + if ( isset( $settings['expire_404'] ) ) { | |
| 278 | + $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) ); | |
| 279 | + } | |
| 280 | + | |
| 281 | + $module = Red_Module::get( 2 ); | |
| 282 | + $options['modules'][2] = $module->update( $settings ); | |
| 283 | + | |
| 284 | + update_option( 'redirection_options', $options ); | |
| 285 | + do_action( 'redirection_save_options', $options ); | |
| 286 | + | |
| 287 | + return $this->ajax_load_settings(); | |
| 107 | 288 | } |
| 108 | 289 | |
| 109 | - public function __construct() { | |
| 110 | - global $wpdb; | |
| 290 | + public function ajax_get_logs( $params ) { | |
| 291 | + $params = $this->get_params( $params ); | |
| 292 | + $result = $this->get_logs( $params ); | |
| 111 | 293 | |
| 112 | - $wpdb->hide_errors(); | |
| 294 | + return $this->output_ajax_response( $result ); | |
| 295 | + } | |
| 113 | 296 | |
| 114 | - $this->routes[] = new Redirection_Api_Redirect( REDIRECTION_API_NAMESPACE ); | |
| 115 | - $this->routes[] = new Redirection_Api_Group( REDIRECTION_API_NAMESPACE ); | |
| 116 | - $this->routes[] = new Redirection_Api_Log( REDIRECTION_API_NAMESPACE ); | |
| 117 | - $this->routes[] = new Redirection_Api_404( REDIRECTION_API_NAMESPACE ); | |
| 118 | - $this->routes[] = new Redirection_Api_Settings( REDIRECTION_API_NAMESPACE ); | |
| 119 | - $this->routes[] = new Redirection_Api_Plugin( REDIRECTION_API_NAMESPACE ); | |
| 120 | - $this->routes[] = new Redirection_Api_Import( REDIRECTION_API_NAMESPACE ); | |
| 121 | - $this->routes[] = new Redirection_Api_Export( REDIRECTION_API_NAMESPACE ); | |
| 297 | + public function ajax_log_action( $params ) { | |
| 298 | + $params = $this->get_params( $params ); | |
| 299 | + | |
| 300 | + // Do the action | |
| 301 | + if ( isset( $params['bulk'] ) && isset( $params['items'] ) && $params['bulk'] === 'delete' ) { | |
| 302 | + $items = explode( ',', $params['items'] ); | |
| 303 | + | |
| 304 | + if ( $this->get_log_type( $params ) === 'log' ) { | |
| 305 | + array_map( array( 'RE_Log', 'delete' ), $items ); | |
| 306 | + } else { | |
| 307 | + array_map( array( 'RE_404', 'delete' ), $items ); | |
| 308 | + } | |
| 309 | + } | |
| 310 | + | |
| 311 | + $result = $this->get_logs( $params ); | |
| 312 | + | |
| 313 | + return $this->output_ajax_response( $result ); | |
| 314 | + } | |
| 315 | + | |
| 316 | + public function ajax_delete_all( $params ) { | |
| 317 | + $params = $this->get_params( $params ); | |
| 318 | + | |
| 319 | + if ( isset( $params['logType'] ) ) { | |
| 320 | + if ( $params['logType'] === 'log' ) { | |
| 321 | + RE_Log::delete_all(); | |
| 322 | + } else { | |
| 323 | + RE_404::delete_all(); | |
| 324 | + } | |
| 325 | + } | |
| 326 | + | |
| 327 | + $result = $this->get_logs( $params ); | |
| 328 | + | |
| 329 | + return $this->output_ajax_response( $result ); | |
| 330 | + } | |
| 331 | + | |
| 332 | + private function get_log_type( $params ) { | |
| 333 | + $type = 'log'; | |
| 334 | + | |
| 335 | + if ( isset( $params['logType'] ) && in_array( $params['logType'], array( 'log', '404' ), true ) ) { | |
| 336 | + $type = $params['logType']; | |
| 337 | + } | |
| 338 | + | |
| 339 | + return $type; | |
| 340 | + } | |
| 341 | + | |
| 342 | + private function get_logs( array $params ) { | |
| 343 | + $type = $this->get_log_type( $params ); | |
| 344 | + | |
| 345 | + if ( $type === 'log' ) { | |
| 346 | + return RE_Filter_Log::get( 'redirection_logs', 'RE_Log', $params ); | |
| 347 | + } else if ( $type === '404' ) { | |
| 348 | + if ( isset( $params['filterBy'] ) && isset( $params['filter'] ) && $params['filterBy'] === 'ip' ) { | |
| 349 | + $params['filter'] = ip2long( $params['filter'] ); | |
| 350 | + } | |
| 351 | + | |
| 352 | + return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params ); | |
| 353 | + } | |
| 354 | + | |
| 355 | + return array( 'items' => array(), 'total' => 0 ); | |
| 356 | + } | |
| 357 | + | |
| 358 | + private function groups_to_json( $groups, $depth = 0 ) { | |
| 359 | + $items = array(); | |
| 360 | + | |
| 361 | + foreach ( $groups as $text => $value ) { | |
| 362 | + if ( is_array( $value ) && $depth === 0 ) { | |
| 363 | + $items[] = (object)array( 'text' => $text, 'value' => $this->groups_to_json( $value, 1 ) ); | |
| 364 | + } else { | |
| 365 | + $items[] = (object)array( 'text' => $value, 'value' => $text ); | |
| 366 | + } | |
| 367 | + } | |
| 368 | + | |
| 369 | + return $items; | |
| 370 | + } | |
| 371 | + | |
| 372 | + private function output_ajax_response( $data ) { | |
| 373 | + $result = wp_json_encode( $data ); | |
| 374 | + | |
| 375 | + if ( defined( 'DOING_AJAX' ) ) { | |
| 376 | + header( 'Content-Type: application/json' ); | |
| 377 | + echo $result; | |
| 378 | + wp_die(); | |
| 379 | + } | |
| 380 | + | |
| 381 | + return $result; | |
| 382 | + } | |
| 383 | + | |
| 384 | + private function user_has_access() { | |
| 385 | + return current_user_can( apply_filters( 'redirection_role', 'administrator' ) ); | |
| 122 | 386 | } |
| 123 | 387 | } |