Browse Source

Controllers: Implement new HTTP class in development toggle

master
Riyyi 3 years ago
parent
commit
b244b625ab
  1. 31
      app/controllers/CacheController.php
  2. 2
      public/js/app.js
  3. 2
      route.php

31
app/controllers/CacheController.php

@ -3,6 +3,7 @@
namespace App\Controllers; namespace App\Controllers;
use App\Classes\Config; use App\Classes\Config;
use App\Classes\Http\Http;
use App\Model\ConfigModel; use App\Model\ConfigModel;
class CacheController extends PageController { class CacheController extends PageController {
@ -15,7 +16,7 @@ class CacheController extends PageController {
parent::view(); parent::view();
} }
public function developmentAction(): void public function toggleAction(): void
{ {
if (Config::c('CLOUDFLARE_ENABLED') != '1') { if (Config::c('CLOUDFLARE_ENABLED') != '1') {
return; return;
@ -25,34 +26,24 @@ class CacheController extends PageController {
$zone = Config::c('CLOUDFLARE_ZONE'); $zone = Config::c('CLOUDFLARE_ZONE');
$url = "https://api.cloudflare.com/client/v4/zones/$zone/settings/development_mode"; $url = "https://api.cloudflare.com/client/v4/zones/$zone/settings/development_mode";
$headers = [
"Authorization: Bearer $token",
"Content-Type: application/json"
];
$config = $this->getConfigValues(); $config = $this->getConfigValues();
$currentState = $config['CLOUDFLARE_DEVELOPMENT_MODE_ENABLED']; $currentState = $config['CLOUDFLARE_DEVELOPMENT_MODE_ENABLED'];
$newState = $currentState == '1' ? 'off' : 'on'; $response = (new Http)->withToken($token)
$data = '{"value": "' . $newState . '"}'; ->asJson()
->acceptJson()
->patch($url, [
'value' => $currentState == '1' ? 'off' : 'on',
]);
$this->saveConfigValues($response->body());
$curl = curl_init(); echo $response->body();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($curl, CURLINFO_HEADER_OUT, 1);
$response = curl_exec($curl);
// $info = curl_getinfo($curl, CURLINFO_HEADER_OUT);
curl_close($curl);
$this->saveConfigValues($response);
echo $response;
// echo $info;
} }
//-------------------------------------// //-------------------------------------//

2
public/js/app.js

@ -238,7 +238,7 @@ $(document).ready(function() {
return; return;
} }
$.get('/admin/toggle-development-mode').done(function(data) $.get('/admin/cache/toggle').done(function(data)
{ {
const response = JSON.parse(data); const response = JSON.parse(data);
if (response.success == false) { if (response.success == false) {

2
route.php

@ -23,7 +23,7 @@ return [
['/admin', 'AdminController', '', ''], ['/admin', 'AdminController', '', ''],
['/admin/cache', 'CacheController', 'cache', ''], ['/admin/cache', 'CacheController', 'cache', ''],
['/admin/toggle', 'AdminController', 'toggle', ''], ['/admin/toggle', 'AdminController', 'toggle', ''],
['/admin/toggle-development-mode', 'CacheController', 'development', ''], ['/admin/cache/toggle', 'CacheController', 'toggle', ''],
['/admin/syntax-highlighting', 'AdminController', 'syntax', ''], ['/admin/syntax-highlighting', 'AdminController', 'syntax', ''],
['/test', 'TestController', '', ''], ['/test', 'TestController', '', ''],
// ["", "", "", ""], // ["", "", "", ""],

Loading…
Cancel
Save