cf-cache-purge - Version 1.0.1

Version Notes

The first stable version, works with the current Cloudflare API

Download this release

Release Info

Developer Agli Panci
Extension cf-cache-purge
Version 1.0.1
Comparing to
See all releases


Version 1.0.1

app/code/community/DM/Cloudflare/Model/Observer.php ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /*
4
+ * The "observer" model.
5
+ *
6
+ * @author Agli Panci <agli.panci@gmail.com>
7
+ * @link https://
8
+ */
9
+
10
+ include 'cloudflare.php';
11
+
12
+ class DM_Cloudflare_Model_Observer {
13
+
14
+ private $cf_apikey;
15
+ private $cf_email;
16
+ private $cf_domain;
17
+
18
+
19
+ public $cf;
20
+
21
+ public function __construct()
22
+ {
23
+
24
+ $this->cf_apikey = Mage::getStoreConfig('general/cf_group/cf_api');
25
+ $this->cf_email = Mage::getStoreConfig('general/cf_group/cf_email');
26
+ $this->cf_domain = $this->clearUrl();
27
+ $this->cf= new cloudflare_api($this->cf_email, $this->cf_apikey);
28
+ }
29
+
30
+ public function purgeCache(){
31
+ $obj = $this->cf->fpurge_ts($this->cf_domain);
32
+ return $obj->result;
33
+ }
34
+
35
+ public function injectHtml(Varien_Event_Observer $observer) {
36
+ $block = $observer->getBlock();
37
+
38
+ if($block instanceof Mage_Adminhtml_Block_Cache_Additional) {
39
+ $transport = $observer->getTransport();
40
+
41
+ $insert =
42
+ '<tr>
43
+ <td class="scope-label">
44
+ <button onclick="setLocation(\'' . Mage::helper('adminhtml')->getUrl('adminhtml/clearapc/index') . '\')" type="button" class="scalable">
45
+ <span>' . Mage::helper('adminhtml')->__('Purge CloudFlare Cache') . '</span>
46
+ </button>
47
+ </td>
48
+ <td class="scope-label">' . Mage::helper('adminhtml')->__('Immediately purge cached resources for your website.') . '</td>
49
+ </tr>';
50
+
51
+ $dom = new DOMDocument();
52
+
53
+ $dom->loadHTML($transport->getHtml());
54
+
55
+ $td = $dom->createDocumentFragment();
56
+ $td->appendXML($insert);
57
+
58
+ $dom->getElementsByTagName('table')->item(1)->insertBefore($td, $dom->getElementsByTagName('table')->item(1)->firstChild);
59
+
60
+ $transport->setHtml($dom->saveHTML());
61
+ }
62
+ }
63
+
64
+ public function clearUrl(){
65
+
66
+ $input = trim(Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB), '/');
67
+
68
+ if (!preg_match('#^http(s)?://#', $input)) {
69
+ $input = 'http://' . $input;
70
+ }
71
+
72
+ $urlParts = parse_url($input);
73
+
74
+ $domain = preg_replace('/^www\./', '', $urlParts['host']);
75
+
76
+ return $domain;
77
+ }
78
+ }
app/code/community/DM/Cloudflare/Model/cloudflare.php ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * CloudFlare API
5
+ *
6
+ *
7
+ * @author AzzA <azza@broadcasthe.net>
8
+ * @copyright omgwtfhax inc. 2013
9
+ * @version 1.1
10
+ */
11
+ class cloudflare_api
12
+ {
13
+ //The URL of the API
14
+ private static $URL = 'https://www.cloudflare.com/api_json.html';
15
+
16
+
17
+ //Timeout for the API requests in seconds
18
+ const TIMEOUT = 5;
19
+
20
+ //Interval values for Stats
21
+ const INTERVAL_365_DAYS = 10;
22
+ const INTERVAL_30_DAYS = 20;
23
+ const INTERVAL_7_DAYS = 30;
24
+ const INTERVAL_DAY = 40;
25
+ const INTERVAL_24_HOURS = 100;
26
+ const INTERVAL_12_HOURS = 110;
27
+ const INTERVAL_6_HOURS = 120;
28
+
29
+ //Stores the api key
30
+ private $token_key;
31
+
32
+ //Stores the email login
33
+ private $email;
34
+
35
+ /**
36
+ * Make a new instance of the API client
37
+ */
38
+ public function __construct()
39
+ {
40
+ $parameters = func_get_args();
41
+
42
+ $this->email = $parameters[0];
43
+ $this->token_key = $parameters[1];
44
+
45
+ }
46
+
47
+ public function fpurge_ts($domain)
48
+ {
49
+ $data = array(
50
+ 'a' => 'fpurge_ts',
51
+ 'z' => $domain,
52
+ 'v' => 1
53
+ );
54
+ return $this->http_post($data);
55
+ }
56
+
57
+ private function http_post($data)
58
+ {
59
+
60
+ $data['u'] = $this->email;
61
+ $data['tkn'] = $this->token_key;
62
+
63
+ $ch = curl_init();
64
+ curl_setopt($ch, CURLOPT_VERBOSE, 0);
65
+ curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
66
+ curl_setopt($ch, CURLOPT_URL, self::$URL);
67
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
68
+ curl_setopt($ch, CURLOPT_POST, 1);
69
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
70
+ curl_setopt($ch, CURLOPT_TIMEOUT, self::TIMEOUT);
71
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
72
+ $http_result = curl_exec($ch);
73
+ $error = curl_error($ch);
74
+ $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
75
+ curl_close($ch);
76
+ if ($http_code != 200) {
77
+ return array(
78
+ 'error' => $error
79
+ );
80
+ } else {
81
+ return json_decode($http_result);
82
+ }
83
+ }
84
+ }
app/code/community/DM/Cloudflare/controllers/Adminhtml/ClearapcController.php ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /*
4
+ * The "clear" model.
5
+ *
6
+ * @author Agli Panci <agli.panci@gmail.com>
7
+ * @link https://
8
+ */
9
+
10
+ class DM_Cloudflare_Adminhtml_ClearapcController extends Mage_Adminhtml_Controller_Action
11
+ {
12
+ public function indexAction()
13
+ {
14
+
15
+ if(Mage::getModel('dm_cloudflare/observer')->purgeCache() == 'success')
16
+ {
17
+ Mage::getSingleton('adminhtml/session')->addSuccess('CloudFlare cache purged successfully.');
18
+ } else
19
+ {
20
+ Mage::getSingleton('adminhtml/session')->addError("There was an error purging CloudFlare cache");
21
+
22
+ }
23
+
24
+ $this->_redirect('adminhtml/cache/index');
25
+
26
+ }
27
+ }
app/code/community/DM/Cloudflare/etc/config.xml ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <DM_Cloudflare>
5
+ <version>1.0.0</version>
6
+ </DM_Cloudflare>
7
+ </modules>
8
+ <admin>
9
+ <routers>
10
+ <adminhtml>
11
+ <args>
12
+ <modules>
13
+ <DM_Cloudflare before="Mage_Adminhtml">DM_Cloudflare_Adminhtml</DM_Cloudflare>
14
+ </modules>
15
+ </args>
16
+ </adminhtml>
17
+ </routers>
18
+ </admin>
19
+ <global>
20
+ <models>
21
+ <dm_cloudflare>
22
+ <class>DM_Cloudflare_Model</class>
23
+ </dm_cloudflare>
24
+ </models>
25
+ </global>
26
+ <adminhtml>
27
+ <events>
28
+ <controller_action_postdispatch_adminhtml_cache_flushAll>
29
+ <observers>
30
+ <dm_cloudflare>
31
+ <type>singleton</type>
32
+ <class>dm_cloudflare/observer</class>
33
+ <method>purgeCache</method>
34
+ </dm_cloudflare>
35
+ </observers>
36
+ </controller_action_postdispatch_adminhtml_cache_flushAll>
37
+ <controller_action_postdispatch_adminhtml_cache_flushSystem>
38
+ <observers>
39
+ <dm_cloudflare>
40
+ <type>singleton</type>
41
+ <class>dm_cloudflare/observer</class>
42
+ <method>purgeCache</method>
43
+ </dm_cloudflare>
44
+ </observers>
45
+ </controller_action_postdispatch_adminhtml_cache_flushSystem>
46
+ <core_block_abstract_to_html_after>
47
+ <observers>
48
+ <dm_cloudflare>
49
+ <type>singleton</type>
50
+ <class>dm_cloudflare/observer</class>
51
+ <method>injectHtml</method>
52
+ </dm_cloudflare>
53
+ </observers>
54
+ </core_block_abstract_to_html_after>
55
+ </events>
56
+ </adminhtml>
57
+ </config>
app/code/community/DM/Cloudflare/etc/system.xml ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <tabs>
4
+ <general translate="label" module="cloudflare">
5
+ <label>General</label>
6
+ <sort_order>0</sort_order>
7
+ </general>
8
+ </tabs>
9
+ <sections>
10
+ <general translate="label" module="cloudflare">
11
+ <groups>
12
+ <cf_group translate="label">
13
+ <label>CloufFlare</label>
14
+ <frontend_type>text</frontend_type>
15
+ <sort_order>0</sort_order>
16
+ <show_in_default>1</show_in_default>
17
+ <show_in_website>1</show_in_website>
18
+ <show_in_store>1</show_in_store>
19
+ <fields>
20
+ <cf_api translate="label">
21
+ <label>Cloud Flare API</label>
22
+ <frontend_type>text</frontend_type>
23
+ <sort_order>0</sort_order>
24
+ <show_in_default>1</show_in_default>
25
+ <show_in_website>1</show_in_website>
26
+ <show_in_store>1</show_in_store>
27
+ </cf_api>
28
+ <cf_email translate="label">
29
+ <label>Cloud Flare Email</label>
30
+ <frontend_type>text</frontend_type>
31
+ <sort_order>0</sort_order>
32
+ <show_in_default>1</show_in_default>
33
+ <show_in_website>1</show_in_website>
34
+ <show_in_store>1</show_in_store>
35
+ </cf_email>
36
+ </fields>
37
+ </cf_group>
38
+ </groups>
39
+ </general>
40
+ </sections>
41
+ </config>
app/etc/modules/DM_Cloudflare.xml ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <DM_Cloudflare>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ <depends>
8
+ <Mage_Core />
9
+ </depends>
10
+ </DM_Cloudflare>
11
+ </modules>
12
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>cf-cache-purge</name>
4
+ <version>1.0.1</version>
5
+ <stability>stable</stability>
6
+ <license>GNU General Public License</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Integrate CloudFlare with Magento. Purge Cloudflare cache directly from Mangeto Cache Management.</summary>
10
+ <description>You don't need anymore to go to your Cloudflare account every time you needed to purge your cache because you made some changes on your Magento site. By using this extension you can Purge Cloudflare cache directly from Mangeto Cache Management.</description>
11
+ <notes>The first stable version, works with the current Cloudflare API</notes>
12
+ <authors><author><name>Agli Panci</name><user>aglipanci</user><email>agli.panci@gmail.com</email></author></authors>
13
+ <date>2014-03-23</date>
14
+ <time>19:41:13</time>
15
+ <contents><target name="magecommunity"><dir name="DM"><dir name="Cloudflare"><dir name="Model"><file name="Observer.php" hash="e9afbdeffd398c585cbfad39a2561c07"/><file name="cloudflare.php" hash="020a9a8f92d0b5cd31f6a548f7a96750"/></dir><dir name="controllers"><dir name="Adminhtml"><file name="ClearapcController.php" hash="3785c9e3df99cbaf14ae012de534baa7"/></dir></dir><dir name="etc"><file name="config.xml" hash="09ca62e9eec5711211ab3882d4357cd5"/><file name="system.xml" hash="f323ea4ab12aebc3c889df7d70054ce5"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="DM_Cloudflare.xml" hash="8702fd1338e94aabb3b091144a96bc0d"/></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.3.0</min><max>5.5.0</max></php><package><name>Mage_Core_Modukes</name><channel>community</channel><min>1.6.0</min><max>1.8.1</max></package><extension><name>curl</name><min/><max/></extension></required></dependencies>
18
+ </package>