tenweb-speed-optimizer
/
vendor
/
10web-utils
/
tenweb-image-optimizer
/
src
/
TenWebIO
Last commit date
Exceptions
4 years ago
Queue
2 years ago
Views
2 years ago
Api.php
2 years ago
Attachments.php
2 years ago
Backup.php
2 years ago
CLI.php
3 years ago
Compress.php
2 years ago
CompressDataService.php
3 years ago
CompressService.php
3 years ago
Config.php
2 years ago
Init.php
2 years ago
LastCompress.php
3 years ago
Logs.php
2 years ago
PreInit.php
3 years ago
Rest.php
2 years ago
Settings.php
3 years ago
Utils.php
1 year ago
Rest.php
465 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TenWebIO; |
| 4 | |
| 5 | use Tenweb_Authorization\Helper; |
| 6 | |
| 7 | class Rest extends \WP_REST_Controller |
| 8 | { |
| 9 | private $version = '2'; |
| 10 | private $route = 'tenwebio'; |
| 11 | private $status = 404; |
| 12 | private $response = array(); |
| 13 | |
| 14 | private $bases = array( |
| 15 | 'compress-custom' => array('route' => '/compress-custom/', |
| 16 | 'methods' => \WP_REST_Server::CREATABLE, |
| 17 | 'args' => array( |
| 18 | 'restart' => array( |
| 19 | 'required' => false, |
| 20 | 'type' => 'int', |
| 21 | ), |
| 22 | 'image_urls' => array( |
| 23 | 'required' => false, |
| 24 | 'type' => 'array', |
| 25 | ), |
| 26 | 'page_id' => array( |
| 27 | 'required' => false, |
| 28 | 'type' => 'string', |
| 29 | ), |
| 30 | 'only_convert_webp' => array( |
| 31 | 'required' => false, |
| 32 | 'type' => 'number', |
| 33 | ), |
| 34 | 'origin' => array( |
| 35 | 'required' => false, |
| 36 | 'type' => 'string', |
| 37 | ), |
| 38 | )), |
| 39 | 'compress-one' => array('route' => '/compress-one/', |
| 40 | 'methods' => \WP_REST_Server::CREATABLE, |
| 41 | 'args' => array( |
| 42 | 'id' => array( |
| 43 | 'required' => true, |
| 44 | 'type' => 'int', |
| 45 | ), |
| 46 | )), |
| 47 | 'compress' => array('route' => '/compress/', |
| 48 | 'methods' => \WP_REST_Server::CREATABLE, |
| 49 | 'args' => array( |
| 50 | 'restart' => array( |
| 51 | 'required' => false, |
| 52 | 'type' => 'int', |
| 53 | ), |
| 54 | )), |
| 55 | 'logs' => array('route' => '/logs/', |
| 56 | 'methods' => \WP_REST_Server::READABLE, |
| 57 | 'args' => array()), |
| 58 | 'ping' => array('route' => '/ping/', |
| 59 | 'methods' => \WP_REST_Server::READABLE, |
| 60 | 'args' => array()), |
| 61 | 'clear-cache' => array('route' => '/clear-cache/', |
| 62 | 'methods' => \WP_REST_Server::READABLE, |
| 63 | 'args' => array()), |
| 64 | 'restore-backup' => array('route' => '/restore-backup/', |
| 65 | 'methods' => \WP_REST_Server::CREATABLE, |
| 66 | 'args' => array( |
| 67 | 'type' => array( |
| 68 | 'required' => false, |
| 69 | 'type' => 'string', |
| 70 | ), |
| 71 | )), |
| 72 | 'delete-backup' => array('route' => '/delete-backup/', |
| 73 | 'methods' => \WP_REST_Server::CREATABLE, |
| 74 | 'args' => array( |
| 75 | 'type' => array( |
| 76 | 'required' => false, |
| 77 | 'type' => 'string', |
| 78 | ), |
| 79 | )), |
| 80 | |
| 81 | ); |
| 82 | |
| 83 | |
| 84 | public function registerRoutes() |
| 85 | { |
| 86 | $namespace = $this->route . '/v' . $this->version; |
| 87 | foreach ($this->bases as $base => $route_data) { |
| 88 | register_rest_route($namespace, $route_data['route'], array( |
| 89 | array( |
| 90 | 'methods' => $route_data['methods'], |
| 91 | 'callback' => array($this, 'callback'), |
| 92 | 'permission_callback' => array($this, 'checkAuthorization'), |
| 93 | 'args' => $route_data['args'] |
| 94 | ) |
| 95 | )); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public function checkAuthorization($request) |
| 100 | { |
| 101 | $route = $request->get_route(); |
| 102 | $endpoint = $this->parseEndpoint($route); |
| 103 | if ($endpoint == 'ping') { |
| 104 | return true; |
| 105 | } |
| 106 | $unauthorized = false; |
| 107 | $unauthorized_response = array( |
| 108 | "code" => "unauthorized", |
| 109 | "message" => "", |
| 110 | "data" => array( |
| 111 | "status" => 401 |
| 112 | ) |
| 113 | ); |
| 114 | |
| 115 | if (Utils::ifWorkspaceOrDomainEmpty()) { |
| 116 | $unauthorized = true; |
| 117 | $unauthorized_response = array( |
| 118 | "code" => "empty_workspace", |
| 119 | "message" => "Empty workspace or domain.", |
| 120 | "data" => array( |
| 121 | "status" => 401 |
| 122 | ) |
| 123 | ); |
| 124 | } else { |
| 125 | if (!empty($request->get_param('tenwebio_nonce'))) { |
| 126 | if (!check_ajax_referer('tenwebio_rest', 'tenwebio_nonce', false)) { |
| 127 | $unauthorized = true; |
| 128 | $unauthorized_response = array( |
| 129 | "code" => "wrong_nonce", |
| 130 | "message" => "Wrong nonce.", |
| 131 | "data" => array( |
| 132 | "status" => 401 |
| 133 | ) |
| 134 | ); |
| 135 | } |
| 136 | } else { |
| 137 | $auth = Helper::get_instance(); |
| 138 | $token = $request->get_header('tenweb-authorization'); |
| 139 | if (empty($token) || $auth->check_single_token($token) === false) { |
| 140 | $unauthorized = true; |
| 141 | $unauthorized_response = array( |
| 142 | "code" => "unauthorized", |
| 143 | "message" => "unauthorized, please login", |
| 144 | "data" => array( |
| 145 | "status" => 401 |
| 146 | ) |
| 147 | ); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if ($unauthorized) { |
| 153 | Logs::setLog("rest:unauthorized", $unauthorized_response, 'error'); |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @param $request |
| 163 | * |
| 164 | * @return \WP_REST_Response |
| 165 | */ |
| 166 | public function callback($request) |
| 167 | { |
| 168 | $route = $request->get_route(); |
| 169 | $endpoint = $this->parseEndpoint($route); |
| 170 | try { |
| 171 | switch ($endpoint) { |
| 172 | case 'compress': |
| 173 | $this->compress($request); |
| 174 | break; |
| 175 | case 'compress-one': |
| 176 | $this->compressOne($request); |
| 177 | break; |
| 178 | case 'compress-custom' : |
| 179 | $this->compressCustom($request); |
| 180 | break; |
| 181 | case 'logs': |
| 182 | $this->logs($request); |
| 183 | break; |
| 184 | case 'ping': |
| 185 | $this->ping($request); |
| 186 | break; |
| 187 | case 'clear-cache': |
| 188 | $this->clearCache($request); |
| 189 | break; |
| 190 | case 'restore-backup': |
| 191 | $this->restoreBackup($request); |
| 192 | break; |
| 193 | case 'delete-backup': |
| 194 | $this->deleteBackup($request); |
| 195 | break; |
| 196 | default: |
| 197 | $this->status = 404; |
| 198 | $this->response = array( |
| 199 | "code" => "rest_no_route", |
| 200 | "message" => "No route was found matching the URL and request method.", |
| 201 | "data" => array( |
| 202 | "status" => 404 |
| 203 | ) |
| 204 | ); |
| 205 | break; |
| 206 | } |
| 207 | } catch (\Exception $e) { |
| 208 | $this->status = 500; |
| 209 | $this->response = array( |
| 210 | "code" => "error", |
| 211 | "message" => $e->getMessage(), |
| 212 | "data" => array( |
| 213 | "status" => 500 |
| 214 | ) |
| 215 | ); |
| 216 | } |
| 217 | Logs::setLog("rest:" . $endpoint . ":log", array( |
| 218 | 'action' => $endpoint, |
| 219 | 'status' => $this->status, |
| 220 | 'response' => $this->response, |
| 221 | )); |
| 222 | |
| 223 | return new \WP_REST_Response($this->response, $this->status); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /** |
| 228 | * @param $request |
| 229 | * |
| 230 | * @return void |
| 231 | * @throws \TenWebQueue\Exceptions\QueueException |
| 232 | */ |
| 233 | private function compress($request) |
| 234 | { |
| 235 | $restart = false; |
| 236 | $force = false; |
| 237 | if ($request->get_param('restart')) { |
| 238 | $restart = true; |
| 239 | } |
| 240 | if ($request->get_param('force')) { |
| 241 | $force = true; |
| 242 | } |
| 243 | try { |
| 244 | $compress = new CompressService($restart); |
| 245 | if ($compress->compressBulk($force)) { |
| 246 | $this->status = 201; |
| 247 | $this->response = array( |
| 248 | "status" => "201", |
| 249 | "message" => "ok", |
| 250 | ); |
| 251 | } else { |
| 252 | $this->status = 400; |
| 253 | } |
| 254 | |
| 255 | } catch (\Exception $e) { |
| 256 | $this->status = 500; |
| 257 | $this->response = array( |
| 258 | "status" => 500, |
| 259 | "message" => $e->getMessage(), |
| 260 | ); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @param $request |
| 266 | * |
| 267 | * @return void |
| 268 | * @throws \TenWebQueue\Exceptions\QueueException |
| 269 | */ |
| 270 | private function compressOne($request) |
| 271 | { |
| 272 | $restart = false; |
| 273 | if ($request->get_param('restart')) { |
| 274 | $restart = true; |
| 275 | } |
| 276 | try { |
| 277 | $compress = new CompressService($restart); |
| 278 | $compress->compressOne($request->get_param('id')); |
| 279 | $this->status = 201; |
| 280 | $this->response = array( |
| 281 | "status" => "201", |
| 282 | "message" => "ok", |
| 283 | ); |
| 284 | } catch (\Exception $e) { |
| 285 | $this->status = 500; |
| 286 | $this->response = array( |
| 287 | "status" => 500, |
| 288 | "message" => $e->getMessage(), |
| 289 | ); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @param $request |
| 295 | * |
| 296 | * @return void |
| 297 | */ |
| 298 | private function compressCustom($request) |
| 299 | { |
| 300 | $restart = false; |
| 301 | if ($request->get_param('restart')) { |
| 302 | $restart = true; |
| 303 | } |
| 304 | $image_urls = $request->get_param('image_urls'); |
| 305 | $page_id = $request->get_param('page_id'); |
| 306 | $only_convert_webp = (int)$request->get_param('only_convert_webp'); |
| 307 | $origin = $request->get_param('origin'); |
| 308 | try { |
| 309 | $compress = new CompressService($restart); |
| 310 | $compress->compressCustom($image_urls, $page_id, $only_convert_webp, $origin); |
| 311 | $this->status = 201; |
| 312 | $this->response = array( |
| 313 | "status" => "201", |
| 314 | "message" => "ok", |
| 315 | ); |
| 316 | } catch (\Exception $e) { |
| 317 | $this->status = 500; |
| 318 | $this->response = array( |
| 319 | "status" => 500, |
| 320 | "message" => $e->getMessage(), |
| 321 | ); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * @param $request |
| 327 | * |
| 328 | * @return void |
| 329 | */ |
| 330 | private function logs($request) |
| 331 | { |
| 332 | try { |
| 333 | $compress_data = new CompressDataService(); |
| 334 | $this->status = 200; |
| 335 | $this->response = array( |
| 336 | "status" => 200, |
| 337 | "message" => "ok", |
| 338 | "data" => $compress_data->getNotCompressedNumbers() |
| 339 | ); |
| 340 | } catch (\Exception $e) { |
| 341 | $this->status = 500; |
| 342 | $this->response = array( |
| 343 | "status" => 500, |
| 344 | "message" => $e->getMessage(), |
| 345 | ); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * @param $request |
| 351 | * |
| 352 | * @return void |
| 353 | */ |
| 354 | private function ping($request) |
| 355 | { |
| 356 | try { |
| 357 | $this->status = 200; |
| 358 | $this->response = array( |
| 359 | "status" => 200, |
| 360 | "message" => "ok" |
| 361 | ); |
| 362 | } catch (\Exception $e) { |
| 363 | $this->status = 500; |
| 364 | $this->response = array( |
| 365 | "status" => 500, |
| 366 | "message" => $e->getMessage(), |
| 367 | ); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * @param $request |
| 373 | * |
| 374 | * @return void |
| 375 | */ |
| 376 | private function clearCache($request) |
| 377 | { |
| 378 | try { |
| 379 | Settings::purgeCompressSettings(); |
| 380 | $compress_settings = new Settings(); |
| 381 | $compress_settings->getSettings(true, 1, 1); |
| 382 | $this->status = 200; |
| 383 | $this->response = array( |
| 384 | "status" => 200, |
| 385 | "message" => "ok" |
| 386 | ); |
| 387 | } catch (\Exception $e) { |
| 388 | $this->status = 500; |
| 389 | $this->response = array( |
| 390 | "status" => 500, |
| 391 | "message" => $e->getMessage(), |
| 392 | ); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * @param $request |
| 398 | * |
| 399 | * @return void |
| 400 | */ |
| 401 | private function restoreBackup($request) |
| 402 | { |
| 403 | try { |
| 404 | $type = $request->get_param('type') ? $request->get_param('type') : 'bulk'; |
| 405 | $backup = new Backup($type); |
| 406 | $backup->restoreOriginalImages(); |
| 407 | $this->status = 200; |
| 408 | $this->response = array( |
| 409 | "status" => 200, |
| 410 | "message" => "ok" |
| 411 | ); |
| 412 | } catch (\Exception $e) { |
| 413 | $this->status = 500; |
| 414 | $this->response = array( |
| 415 | "status" => 500, |
| 416 | "message" => $e->getMessage(), |
| 417 | ); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * @param $request |
| 423 | * |
| 424 | * @return void |
| 425 | */ |
| 426 | private function deleteBackup($request) |
| 427 | { |
| 428 | try { |
| 429 | $type = $request->get_param('type') ? $request->get_param('type') : 'bulk'; |
| 430 | $backup = new Backup($type); |
| 431 | $backup->deleteBackup(); |
| 432 | $this->status = 200; |
| 433 | $this->response = array( |
| 434 | "status" => 200, |
| 435 | "message" => "ok" |
| 436 | ); |
| 437 | } catch (\Exception $e) { |
| 438 | $this->status = 500; |
| 439 | $this->response = array( |
| 440 | "status" => 500, |
| 441 | "message" => $e->getMessage(), |
| 442 | ); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * @param $route |
| 448 | * |
| 449 | * @return int|null|string |
| 450 | */ |
| 451 | private function parseEndpoint($route) |
| 452 | { |
| 453 | $route_url = substr($route, 9); |
| 454 | foreach ($this->bases as $key => $value) { |
| 455 | $route_regex = '/' . $key . '/'; |
| 456 | if (preg_match($route_regex, $route_url)) { |
| 457 | return $key; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return null; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 |