PluginProbe
Redirection / 5.0
Redirection v5.0
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / api / api-log.php

api-log.php in Redirection 5.0, at api/api-log.php

203 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @api {get} /redirection/v1/log Get logs
5 * @apiName GetLogs
6 * @apiDescription Get a paged list of redirect logs after applying a set of filters and result ordering.
7 * @apiGroup Log
8 *
9 * @apiUse LogQueryParams
10 *
11 * @apiUse LogList
12 * @apiUse 401Error
13 * @apiUse 404Error
14 */
15
16 /**
17 * @api {post} /redirection/v1/log Delete logs
18 * @apiName DeleteLogs
19 * @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.
20 * performing the action, based on the supplied query parameters. This information can be used to refresh a list displayed to the client.
21 * @apiGroup Log
22 *
23 * @apiParam (Query Parameter) {String} filterBy[ip] Filter the results by the supplied IP
24 * @apiParam (Query Parameter) {String} filterBy[url] Filter the results by the supplied URL
25 * @apiParam (Query Parameter) {String} filterBy[url-exact] Filter the results by the exact URL (not a substring match, as per `url`)
26 * @apiParam (Query Parameter) {String} filterBy[referrer] Filter the results by the supplied referrer
27 * @apiParam (Query Parameter) {String} filterBy[agent] Filter the results by the supplied user agent
28 * @apiParam (Query Parameter) {String} filterBy[target] Filter the results by the supplied redirect target
29 *
30 * @apiUse LogList
31 * @apiUse 401Error
32 * @apiUse 404Error
33 */
34
35 /**
36 * @api {post} /redirection/v1/bulk/log/:type Bulk action
37 * @apiName BulkAction
38 * @apiDescription Delete logs by ID
39 * @apiGroup Log
40 *
41 * @apiParam (URL) {String="delete"} :type Type of bulk action that is applied to every log ID.
42 * @apiParam (Query Parameter) {String[]} [items] Array of group IDs to perform the action on
43 * @apiParam (Query Parameter) {Boolean=false} [global] Perform action globally using the filter parameters
44 * @apiUse LogQueryParams
45 *
46 * @apiUse LogList
47 * @apiUse 401Error
48 * @apiUse 404Error
49 * @apiUse 400MissingError
50 */
51
52 /**
53 * @apiDefine LogQueryParams Log query parameters
54 *
55 * @apiParam (Query Parameter) {String} [filterBy[ip]] Filter the results by the supplied IP
56 * @apiParam (Query Parameter) {String} [filterBy[url]] Filter the results by the supplied URL
57 * @apiParam (Query Parameter) {String} [filterBy[url-]exact] Filter the results by the exact URL (not a substring match, as per `url`)
58 * @apiParam (Query Parameter) {String} [filterBy[referrer]] Filter the results by the supplied referrer
59 * @apiParam (Query Parameter) {String} [filterBy[agent]] Filter the results by the supplied user agent
60 * @apiParam (Query Parameter) {String} [filterBy[target]] Filter the results by the supplied redirect target
61 * @apiParam (Query Parameter) {String} [filterBy[domain]] Filter the results by the supplied domain name
62 * @apiParam (Query Parameter) {String} [filterBy[redirect_by]] Filter the results by the redirect agent
63 * @apiParam (Query Parameter) {String="head","get","post"} [filterBy[method]] Filter the results by the supplied HTTP request method
64 * @apiParam (Query Parameter) {String="ip","url"} [orderby] Order by IP or URL
65 * @apiParam (Query Parameter) {String="asc","desc"} [direction=desc] Direction to order the results by (ascending or descending)
66 * @apiParam (Query Parameter) {Integer{1...200}} [per_page=25] Number of results per request
67 * @apiParam (Query Parameter) {Integer} [page=0] Current page of results
68 * @apiParam (Query Parameter) {String="ip","url"} [groupBy] Group by IP or URL
69 */
70
71 /**
72 * @apiDefine LogList
73 *
74 * @apiSuccess {Object[]} items Array of log objects
75 * @apiSuccess {Integer} items.id ID of log entry
76 * @apiSuccess {String} items.created Date the log entry was recorded
77 * @apiSuccess {Integer} items.created_time Unix time value for `created`
78 * @apiSuccess {Integer} items.url The requested URL that caused the log entry
79 * @apiSuccess {String} items.agent User agent of the client initiating the request
80 * @apiSuccess {Integer} items.referrer Referrer of the client initiating the request
81 * @apiSuccess {Integer} total Number of items
82 *
83 * @apiSuccessExample {json} Success 200:
84 * HTTP/1.1 200 OK
85 * {
86 * "items": [
87 * {
88 * "id": 3,
89 * "created": "2019-01-01 12:12:00,
90 * "created_time": "12345678",
91 * "url": "/the-url",
92 * "agent": "FancyBrowser",
93 * "referrer": "http://site.com/previous/,
94 * }
95 * ],
96 * "total": 1
97 * }
98 */
99
100 /**
101 * Log API endpoint
102 */
103 class Redirection_Api_Log extends Redirection_Api_Filter_Route {
104 /**
105 * Log API endpoint constructor
106 *
107 * @param String $namespace Namespace.
108 */
109 public function __construct( $namespace ) {
110 $orders = [ 'url', 'ip', 'total', 'count', '' ];
111 $filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url', 'target', 'domain', 'method', 'http', 'redirect_by' ];
112
113 register_rest_route( $namespace, '/log', array(
114 'args' => $this->get_filter_args( $orders, $filters ),
115 $this->get_route( WP_REST_Server::READABLE, 'route_log', [ $this, 'permission_callback_manage' ] ),
116 ) );
117
118 register_rest_route( $namespace, '/bulk/log/(?P<bulk>delete)', [
119 $this->get_route( WP_REST_Server::EDITABLE, 'route_bulk', [ $this, 'permission_callback_delete' ] ),
120 'args' => array_merge( $this->get_filter_args( $orders, $filters ), [
121 'items' => [
122 'description' => 'Comma separated list of item IDs to perform action on',
123 'type' => 'array',
124 'items' => [
125 'description' => 'Item ID',
126 'type' => [ 'string', 'number' ],
127 ],
128 ],
129 ] ),
130 ] );
131 }
132
133 /**
134 * Checks a manage capability
135 *
136 * @param WP_REST_Request $request Request.
137 * @return Bool
138 */
139 public function permission_callback_manage( WP_REST_Request $request ) {
140 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_LOG_MANAGE );
141 }
142
143 /**
144 * Checks a delete capability
145 *
146 * @param WP_REST_Request $request Request.
147 * @return Bool
148 */
149 public function permission_callback_delete( WP_REST_Request $request ) {
150 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_LOG_DELETE );
151 }
152
153 /**
154 * Get log list
155 *
156 * @param WP_REST_Request $request The request.
157 * @return WP_Error|array Return an array of results, or a WP_Error
158 */
159 public function route_log( WP_REST_Request $request ) {
160 return $this->get_logs( $request->get_params() );
161 }
162
163 /**
164 * Perform bulk action on logs
165 *
166 * @param WP_REST_Request $request The request.
167 * @return WP_Error|array Return an array of results, or a WP_Error
168 */
169 public function route_bulk( WP_REST_Request $request ) {
170 $params = $request->get_params();
171
172 if ( isset( $params['items'] ) && is_array( $params['items'] ) ) {
173 $items = $params['items'];
174
175 foreach ( $items as $item ) {
176 if ( is_numeric( $item ) ) {
177 Red_Redirect_Log::delete( intval( $item, 10 ) );
178 } elseif ( isset( $params['groupBy'] ) ) {
179 $delete_by = 'url-exact';
180
181 if ( in_array( $params['groupBy'], [ 'ip', 'agent' ], true ) ) {
182 $delete_by = $params['groupBy'];
183 }
184
185 Red_Redirect_Log::delete_all( [ 'filterBy' => [ $delete_by => $item ] ] );
186 }
187 }
188 } elseif ( isset( $params['global'] ) && $params['global'] ) {
189 Red_Redirect_Log::delete_all( $params );
190 }
191
192 return $this->route_log( $request );
193 }
194
195 private function get_logs( array $params ) {
196 if ( isset( $params['groupBy'] ) && in_array( $params['groupBy'], [ 'ip', 'url', 'agent' ], true ) ) {
197 return Red_Redirect_Log::get_grouped( $params['groupBy'], $params );
198 }
199
200 return Red_Redirect_Log::get_filtered( $params );
201 }
202 }
203