diff --git a/app/controllers/AdminController.php b/app/controllers/AdminController.php index f844fd1..2a4c0f7 100644 --- a/app/controllers/AdminController.php +++ b/app/controllers/AdminController.php @@ -2,7 +2,6 @@ namespace App\Controllers; -use App\Classes\Config; use App\Classes\User; class AdminController extends PageController { @@ -13,37 +12,6 @@ class AdminController extends PageController { parent::view('', 'Admin'); } - public function developmentAction(): void - { - if (Config::c('DEVELOPMENT_MODE') == 'cloudflare') { - $token = Config::c('DEVELOPMENT_MODE_TOKEN'); - $zone = Config::c('DEVELOPMENT_MODE_ZONE'); - - $url = "https://api.cloudflare.com/client/v4/zones/$zone/settings/development_mode"; - $headers = [ - "Authorization: Bearer $token", - "Content-Type: application/json" - ]; - $data = '{"value": "on"}'; - - $curl = curl_init(); - 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); - } - - echo $response; - // echo $info; - } - public function toggleAction(): void { User::toggle(); echo User::getToggle() ? '1' : '0'; diff --git a/app/controllers/CacheController.php b/app/controllers/CacheController.php new file mode 100644 index 0000000..736173b --- /dev/null +++ b/app/controllers/CacheController.php @@ -0,0 +1,128 @@ +getConfigValues(); + + $this->router->service()->config = $config; + parent::view(); + } + + public function developmentAction(): void + { + if (Config::c('CLOUDFLARE_ENABLED') != '1') { + return; + } + + $token = Config::c('CLOUDFLARE_TOKEN'); + $zone = Config::c('CLOUDFLARE_ZONE'); + + $url = "https://api.cloudflare.com/client/v4/zones/$zone/settings/development_mode"; + $headers = [ + "Authorization: Bearer $token", + "Content-Type: application/json" + ]; + + $config = $this->getConfigValues(); + $currentState = $config['CLOUDFLARE_DEVELOPMENT_MODE_ENABLED']; + + $newState = $currentState == '1' ? 'off' : 'on'; + $data = '{"value": "' . $newState . '"}'; + + $curl = curl_init(); + 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; + } + + //-------------------------------------// + + private static function getConfigValues(): array + { + $result = []; + + $config = [ + 'CLOUDFLARE_DEVELOPMENT_MODE_ENABLED' => '0', + 'CLOUDFLARE_DEVELOPMENT_MODE_UPDATED_AT' => '', + 'CLOUDFLARE_DEVELOPMENT_MODE_EXPIRES_IN' => '', + ]; + + foreach ($config as $key => $value) { + $result[$key] = ConfigModel::firstOrCreate( + ['key' => $key], + ['value' => $value] + )->value; + } + + if ($result['CLOUDFLARE_DEVELOPMENT_MODE_ENABLED']) { + $expiresIn = $result['CLOUDFLARE_DEVELOPMENT_MODE_EXPIRES_IN']; + $updatedAt = $result['CLOUDFLARE_DEVELOPMENT_MODE_UPDATED_AT']; + + $expiresAtObject = new \DateTime($updatedAt); + $expiresAt = $expiresAtObject + ->modify("+ $expiresIn seconds") + ->format('Y-m-d H:i:s'); + $nowObject = new \DateTime('now'); + $now = $nowObject->format('Y-m-d H:i:s'); + + if ($now >= $expiresAt) { + ConfigModel::updateOrCreate( + ['key' => 'CLOUDFLARE_DEVELOPMENT_MODE_ENABLED'], + ['value' => 0]); + $result['CLOUDFLARE_DEVELOPMENT_MODE_ENABLED'] = 0; + } + else { + $result['enabled-remaining'] = $expiresAtObject->modify( + '- ' . $nowObject->getTimestamp() . ' seconds' + )->format('H:i:s'); + } + } + + return $result; + } + + private static function saveConfigValues(string $response): void + { + $decodedResponse = json_decode($response, true); + if ($decodedResponse['success'] == true) { + $state = $decodedResponse['result']['value']; + $expiresIn = $decodedResponse['result']['time_remaining']; + $updatedAt = $decodedResponse['result']['modified_on']; + + $updatedAtFormatted = (new \DateTime($updatedAt))->format('Y-m-d H:i:s'); + + ConfigModel::updateOrCreate( + ['key' => 'CLOUDFLARE_DEVELOPMENT_MODE_ENABLED'], + ['value' => $state == 'on' ? 1 : 0]); + + ConfigModel::updateOrCreate( + ['key' => 'CLOUDFLARE_DEVELOPMENT_MODE_EXPIRES_IN'], + ['value' => $expiresIn]); + + ConfigModel::updateOrCreate( + ['key' => 'CLOUDFLARE_DEVELOPMENT_MODE_UPDATED_AT'], + ['value' => $updatedAtFormatted]); + } + } + +} diff --git a/app/model/ConfigModel.php b/app/model/ConfigModel.php new file mode 100644 index 0000000..0411647 --- /dev/null +++ b/app/model/ConfigModel.php @@ -0,0 +1,23 @@ + +
config.php
file: ++ +'CLOUDFLARE_ENABLED' => '1',
+ Cloudflare cache options: +
+Granuarly remove .css and .js files from Cloudflare's cache.
+Granuarly remove font and images files from Cloudflare's cache.
+Remove ALL files from Cloudflare's cache.
+
+ This will bypass Cloudflare's accelerated cache and slow down your site,
+ but is useful if you are making changes to cacheable content
+ (like images, CSS, or JavaScript) and would like to see those changes right away.
+ Once entered, development mode will last for 3 hours and then automatically toggle off.
+ config['CLOUDFLARE_DEVELOPMENT_MODE_ENABLED']; ?>
+
+ Enabled for another
+
+ = $state == '1' ? $this->config['enabled-remaining'] : ''; ?>
+
hours.
+
+