PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / api / class-loggerroute.php

class-loggerroute.php in DecaLog 4.4.0, at includes/api/class-loggerroute.php

195 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * DecaLog logger read handler
4 *
5 * Handles all logger reads.
6 *
7 * @package API
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 2.0.0
10 */
11
12 namespace Decalog;
13
14 use Decalog\Plugin\Feature\Log;
15 use Decalog\System\Role;
16 use Decalog\Plugin\Feature\DLogger;
17 use Decalog\Plugin\Feature\Wpcli;
18 use Decalog\Handler\SharedMemoryHandler;
19
20 /**
21 * Define the item operations functionality.
22 *
23 * Handles all item operations.
24 *
25 * @package API
26 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
27 * @since 2.0.0
28 */
29 class LoggerRoute extends \WP_REST_Controller {
30
31 /**
32 * The internal logger.
33 *
34 * @since 2.0.0
35 * @var DLogger $logger The plugin rest logger.
36 */
37 protected $logger;
38
39 /**
40 * The acceptable levels.
41 *
42 * @since 2.0.0
43 * @var array $levels The acceptable levels.
44 */
45 protected $levels = [ 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency' ];
46
47 /**
48 * The acceptable modes.
49 *
50 * @since 2.0.0
51 * @var array $modes The acceptable modes.
52 */
53 protected $modes = [ 'wp', 'http', 'php' ];
54
55 /**
56 * Register the routes for the objects of the controller.
57 *
58 * @since 2.0.0
59 */
60 public function register_routes() {
61 $this->logger = Log::bootstrap( 'plugin', DECALOG_PRODUCT_SHORTNAME, DECALOG_VERSION );
62 $this->register_route_livelog();
63 }
64
65 /**
66 * Register the routes for livelog.
67 *
68 * @since 2.0.0
69 */
70 public function register_route_livelog() {
71 register_rest_route(
72 DECALOG_REST_NAMESPACE,
73 'livelog',
74 [
75 [
76 'methods' => 'GET',
77 'callback' => [ $this, 'get_livelog' ],
78 'permission_callback' => [ $this, 'get_livelog_permissions_check' ],
79 'args' => array_merge( $this->arg_schema_livelog() ),
80 'schema' => [ $this, 'get_schema' ],
81 ],
82 ]
83 );
84 }
85
86 /**
87 * Get the query params for livelog.
88 *
89 * @return array The schema fragment.
90 * @since 2.0.0
91 */
92 public function arg_schema_livelog() {
93 return [
94 'index' => [
95 'description' => 'The index to start from.',
96 'type' => 'string',
97 'required' => false,
98 'default' => '0',
99 'sanitize_callback' => 'sanitize_text_field',
100 ],
101 'level' => [
102 'description' => 'The minimum level to get.',
103 'type' => 'string',
104 'enum' => $this->levels,
105 'required' => false,
106 'default' => 'info',
107 'sanitize_callback' => [ $this, 'sanitize_level' ],
108 ],
109 'mode' => [
110 'description' => 'The details shown.',
111 'type' => 'string',
112 'enum' => $this->modes,
113 'required' => false,
114 'default' => 'wp',
115 'sanitize_callback' => [ $this, 'sanitize_mode' ],
116 ],
117 ];
118 }
119
120 /**
121 * Check if a given request has access to get livelogs
122 *
123 * @param \WP_REST_Request $request Full data about the request.
124 * @return \WP_Error|bool
125 */
126 public function get_livelog_permissions_check( $request ) {
127 if ( ! is_user_logged_in() ) {
128 $this->logger->warning( 'Unauthenticated API call.', 401 );
129 return new \WP_Error( 'rest_not_logged_in', 'You must be logged in to access live logs.', [ 'status' => 401 ] );
130 }
131 return Role::override_privileges() || Role::SUPER_ADMIN === Role::admin_type() || Role::SINGLE_ADMIN === Role::admin_type();
132 }
133
134 /**
135 * Sanitization callback for level.
136 *
137 * @param mixed $value Value of the arg.
138 * @param \WP_REST_Request $request Current request object.
139 * @param string $param Name of the arg.
140 * @return string The level sanitized.
141 * @since 2.0.0
142 */
143 public function sanitize_level( $value, $request = null, $param = null ) {
144 $result = 'info';
145 if ( in_array( (string) $value, $this->levels, true ) ) {
146 $result = (string) $value;
147 }
148 return $result;
149 }
150
151 /**
152 * Sanitization callback for mode.
153 *
154 * @param mixed $value Value of the arg.
155 * @param \WP_REST_Request $request Current request object.
156 * @param string $param Name of the arg.
157 * @return string The mode sanitized.
158 * @since 2.0.0
159 */
160 public function sanitize_mode( $value, $request = null, $param = null ) {
161 $result = 'http';
162 if ( in_array( (string) $value, $this->modes, true ) ) {
163 $result = (string) $value;
164 }
165 return $result;
166 }
167
168 /**
169 * Get a collection of items
170 *
171 * @param \WP_REST_Request $request Full data about the request.
172 * @return \WP_REST_Response
173 */
174 public function get_livelog( $request ) {
175 if ( '0' === $request['index'] ) {
176 $index = array_key_last( SharedMemoryHandler::read() );
177 if ( ! isset( $index ) ) {
178 $index = '0';
179 }
180 $records = [];
181 $this->logger->notice( 'Live console launched.' );
182 } else {
183 $records = Wpcli::records_format( Wpcli::records_filter( SharedMemoryHandler::read(), [ 'level' => $request['level'] ], $request['index'] ), $request['mode'], 320 );
184 $index = array_key_last( $records );
185 if ( ! isset( $index ) ) {
186 $index = $request['index'];
187 }
188 }
189 $result = [];
190 $result['index'] = $index;
191 $result['items'] = $records;
192 return new \WP_REST_Response( $result, 200 );
193 }
194
195 }