Hi All,
I tried to add this function to my website.
As a PHP newbie, please tell me if the code has any mistake or can be improved.
Below is my workflow.
Visit Cloudflare -> Select my domain -> Get the zone ID -> Get the Global API KEY
Database chv_deletions,
Add 2 new columns
1. "deleted_content_addition" Type: TEXT Checked:NULL
2. "deleted_content_timestamp" Type: TIMESTAMP Default : CURRENT_TIMESTAMP Checked:NULL
In class.image.php (app/lib/classes)
Find:
Add below code above of this:
Find:
Add below code below the above code:
Create route.cron.php at app/routes/overrides:
Add below code:
Please do remember to replace the zone ID and the global API key.
Create Cron job:
Then the system will purge the cache of deleted images(the images which were deleted from now to 2 mins ago).
The Cron job runs every minute. The system won't connect to CF API when there is no deleted image at the nearest 2 mins.
I tried to add this function to my website.
As a PHP newbie, please tell me if the code has any mistake or can be improved.
Below is my workflow.
Visit Cloudflare -> Select my domain -> Get the zone ID -> Get the Global API KEY
Database chv_deletions,
Add 2 new columns
1. "deleted_content_addition" Type: TEXT Checked:NULL
2. "deleted_content_timestamp" Type: TIMESTAMP Default : CURRENT_TIMESTAMP Checked:NULL
In class.image.php (app/lib/classes)
Find:
Code:
// Log image deletion
PHP:
$imagelink[] = $image['file_resource']['chain']['image'];
if ($image['file_resource']['chain']['thumb'] !== NULL){
$imagelink[] = $image['file_resource']['chain']['thumb'];
};
if ($image['file_resource']['chain']['medium'] !== NULL) {
$imagelink[] = $image['file_resource']['chain']['medium'];
};
PHP:
'content_original_filename' => $image['original_filename'],
PHP:
'content_addition' => json_encode($imagelink),
Create route.cron.php at app/routes/overrides:
Add below code:
PHP:
<?php
/* Just an example of route extension */
$route = function ($handler) {
$db = CHV\DB::getInstance();
$db->query('SELECT deleted_content_addition FROM ' . CHV\DB::getTable('deletions') . ' WHERE deleted_content_addition IS NOT NULL AND deleted_content_timestamp > (NOW() - INTERVAL 2 MINUTE)');
$result = $db->fetchAll();
$new_data = array();
$filelist['files'] = array();
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($result));
foreach($it as $v) {
$v = json_decode($v);
$new_data[] = $v;
}
$it1 = new RecursiveIteratorIterator(new RecursiveArrayIterator($new_data));
foreach($it1 as $v1) {
$filelist['files'][] = $v1;;
}
$string = json_encode($filelist);
if (is_null($filelist['files'][0])){
die("Diu! No job do ar");
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/zones/****ZONE ID****/purge_cache');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
$headers = array();
$headers[] = 'X-Auth-Email: youecloudflareaccount@example.com';
$headers[] = 'X-Auth-Key: **You Account Global API Key**';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
die("Job Done");
};
Please do remember to replace the zone ID and the global API key.
Create Cron job:
Code:
* * * * * curl https://example.com/cron
Then the system will purge the cache of deleted images(the images which were deleted from now to 2 mins ago).
The Cron job runs every minute. The system won't connect to CF API when there is no deleted image at the nearest 2 mins.