PluginProbe
Remote Website Management by Watchful / trunk
Remote Website Management by Watchful vtrunk
v2.0.17 v2.0.16 v2.0.15 v2.0.14 v2.0.13 v2.0.12 v2.0.11 trunk v1.2.11 v1.2.12 v1.2.13 v1.2.14 v1.2.17 v1.2.19 v1.2.20 v1.2.9 v1.3.0 v1.3.1 v1.3.2 v1.4.1 v1.4.10 v1.4.11 v1.4.12 v1.4.2 v1.4.3 All 72 releases
watchful / lib / Controller / Files.php

Files.php in Remote Website Management by Watchful trunk, at lib/Controller/Files.php

226 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Controller for managing WP files.
4 *
5 * @version 2016-12-20 11:41 UTC+01
6 * @package Watchful WP Client
7 * @author Watchful
8 * @authorUrl https://watchful.net
9 * @copyright Copyright (c) 2020 watchful.net
10 * @license GNU/GPL
11 */
12
13 namespace Watchful\Controller;
14
15 use Watchful\Audit\Files\Tools;
16 use Watchful\Exception;
17 use Watchful\Helpers\Authentification;
18 use WP_REST_Request;
19 use WP_REST_Server;
20
21
22 if (!defined('ABSPATH')) {
23 exit; // Exit if accessed directly.
24 }
25
26 /**
27 * Watchful files class.
28 */
29 class Files implements BaseControllerInterface
30 {
31
32 /**
33 * Register watchful routes for WP API v2.
34 *
35 * @since 1.2.0
36 */
37 public function register_routes()
38 {
39 register_rest_route(
40 'watchful/v1',
41 '/files/chmod',
42 array(
43 array(
44 'methods' => WP_REST_Server::CREATABLE,
45 'callback' => array($this, 'chmod'),
46 'permission_callback' => array('Watchful\Routes', 'authentification'),
47 'args' => array_merge(
48 Authentification::get_arguments(),
49 array(
50 'path' => array(
51 'default' => null,
52 ),
53 'permissions' => array(
54 'default' => null,
55 'sanitize_callback' => 'esc_attr',
56 'validate_callback' => function ($param) {
57 return is_numeric($param);
58 },
59 ),
60 )
61 ),
62 ),
63 )
64 );
65
66 register_rest_route(
67 'watchful/v1',
68 '/files/',
69 array(
70 array(
71 'methods' => WP_REST_Server::READABLE,
72 'callback' => array($this, 'read'),
73 'permission_callback' => array('Watchful\Routes', 'authentification'),
74 'args' => array_merge(
75 Authentification::get_arguments(),
76 array(
77 'path' => array(
78 'default' => null,
79 ),
80 )
81 ),
82 ),
83 )
84 );
85
86 register_rest_route(
87 'watchful/v1',
88 '/files/',
89 array(
90 array(
91 'methods' => WP_REST_Server::EDITABLE,
92 'callback' => array($this, 'write'),
93 'permission_callback' => array('Watchful\Routes', 'authentification'),
94 'args' => array_merge(
95 Authentification::get_arguments(),
96 array(
97 'path' => array(
98 'default' => null,
99 ),
100 'fileperms' => array(
101 'default' => '0644',
102 'sanitize_callback' => 'esc_attr',
103 'validate_callback' => function ($param, $request, $key) {
104 return is_numeric($param);
105 },
106 ),
107 'dirperms' => array(
108 'default' => '0775',
109 'sanitize_callback' => 'esc_attr',
110 'validate_callback' => function ($param, $request, $key) {
111 return is_numeric($param);
112 },
113 ),
114 )
115 ),
116 ),
117 )
118 );
119
120 register_rest_route(
121 'watchful/v1',
122 '/files/',
123 array(
124 array(
125 'methods' => WP_REST_Server::DELETABLE,
126 'callback' => array($this, 'delete'),
127 'permission_callback' => array('Watchful\Routes', 'authentification'),
128 'args' => array_merge(
129 Authentification::get_arguments(),
130 array(
131 'path' => array(
132 'default' => null,
133 ),
134 )
135 ),
136 ),
137 )
138 );
139 }
140
141
142 /**
143 * Set permission on a file or a directory.
144 *
145 * @param WP_REST_Request $request The request containing the chmod info.
146 *
147 * @return string
148 * @throws Exception If one or more required parameters are missing from request.
149 */
150 public function chmod(WP_REST_Request $request)
151 {
152 if (!$request->get_param('path')) {
153 throw new Exception('parameter is missing. path required', 400);
154 }
155
156 if (!$request->get_param('permissions')) {
157 throw new Exception('parameter is missing. permissions required', 400);
158 }
159
160 $tools = new Tools($request->get_param('path'));
161
162 return $tools->chmod($request->get_param('permissions'));
163 }
164
165 /**
166 * Read contents of a file.
167 *
168 * @param WP_REST_Request $request The request containing the file info.
169 *
170 * @return string
171 * @throws Exception If file info is not available in request.
172 */
173 public function read(WP_REST_Request $request)
174 {
175 if (!$request->get_param('path')) {
176 throw new Exception('parameter is missing. path required', 400);
177 }
178
179 $tools = new Tools($request->get_param('path'));
180
181 return $tools->read();
182 }
183
184 /**
185 * Write content to a file or create a file.
186 *
187 * @param WP_REST_Request $request The request with the file info.
188 *
189 * @return string
190 * @throws Exception If file info is not available in request.
191 */
192 public function write(WP_REST_Request $request)
193 {
194 if (!$request->get_param('path')) {
195 throw new Exception('parameter is missing. path required', 400);
196 }
197
198 if (!$request->get_body()) {
199 throw new Exception('body is empty', 400);
200 }
201
202 $tools = new Tools($request->get_param('path'), true);
203
204 return $tools->write($request->get_body(), $request->get_param('fileperms'), $request->get_param('dirperms'));
205 }
206
207 /**
208 * Delete a file or a directory.
209 *
210 * @param WP_REST_Request $request The request with the file info.
211 *
212 * @return string
213 * @throws Exception If file info is not available in request.
214 */
215 public function delete(WP_REST_Request $request)
216 {
217 if (!$request->get_param('path')) {
218 throw new Exception('parameter is missing. path required', 400);
219 }
220
221 $tools = new Tools($request->get_param('path'));
222
223 return $tools->delete();
224 }
225 }
226