-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext.cloudflare.php
More file actions
85 lines (71 loc) · 2.44 KB
/
ext.cloudflare.php
File metadata and controls
85 lines (71 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
class Cloudflare_ext
{
public $name = 'Cloudflare';
public $version = '1.0.0-rc2';
public $description = 'Purge Cloudflare cache';
public $settings_exist = 'y';
public $docs_url = '';
public $settings = [];
protected $debug = false;
public function __construct($settings = '')
{
$this->settings = $settings;
}
public function activate_extension()
{
$this->settings = [
'zone_id' => '',
'token' => '',
];
// https://docs.expressionengine.com/latest/development/extension-hooks/model/channel-entry.html
ee()->db->insert('extensions', [
'class' => __CLASS__,
'method' => 'purge',
'hook' => 'after_channel_entry_save',
'settings' => serialize($this->settings),
'priority' => 10,
'version' => $this->version,
'enabled' => 'y',
]);
// https://docs.expressionengine.com/latest/development/extension-hooks/model/template.html
ee()->db->insert('extensions', [
'class' => __CLASS__,
'method' => 'purge',
'hook' => 'after_template_save',
'settings' => serialize($this->settings),
'priority' => 10,
'version' => $this->version,
'enabled' => 'y',
]);
}
public function disable_extension()
{
ee()->db->where('class', __CLASS__);
ee()->db->delete('extensions');
}
public function settings()
{
$settings = [];
$settings['zone_id'] = ['i', '', ''];
$settings['token'] = ['i', '', ''];
return $settings;
}
public function purge()
{
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/'.$this->settings['zone_id'].'/purge_cache');
curl_setopt($handler, CURLOPT_POST, 1);
curl_setopt($handler, CURLOPT_POSTFIELDS, '{"purge_everything":true}');
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handler, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer '.$this->settings['token'],
]);
$response = curl_exec($handler);
curl_close($handler);
if ($this->debug) {
file_put_contents('log.txt', $response."\n", FILE_APPEND);
}
}
}