/** * Cache Cleaner addon for CS-Cart * * @author Internet Code */ if (!defined('BOOTSTRAP')) { die('Access denied'); } // Main addon functions function fn_a_code_casch_install() { // Create scheduled task for cache cleaning db_query("INSERT INTO ?:cron_jobs (script, params, description, date_created, last_start, status) VALUES ('addons/a_code_casch/cron_clean_cache.php', '', 'Cache Cleaner addon task', ?i, 0, 'A')", TIME); } function fn_a_code_casch_uninstall() { // Remove scheduled task db_query("DELETE FROM ?:cron_jobs WHERE script = ?s", 'addons/a_code_casch/cron_clean_cache.php'); } function fn_a_code_casch_delete_cache() { $cache_dir = fn_get_cache_path(); $deleted_files = 0; try { // Recursive function to delete files in directory $deleted_files = fn_a_code_casch_clean_directory($cache_dir); fn_set_notification('N', __('notice'), __('a_code_casch.cache_cleaned_successfully', array('[count]' => $deleted_files))); } catch (Exception $e) { fn_set_notification('E', __('error'), $e->getMessage()); } return $deleted_files; } function fn_a_code_casch_clean_directory($dir) { if (!is_dir($dir)) { return 0; } $count = 0; $files = scandir($dir); foreach ($files as $file) { if ($file == '.' || $file == '..') { continue; } $path = $dir . '/' . $file; if (is_dir($path)) { // If directory, recursively clean it $count += fn_a_code_casch_clean_directory($path); // Try to remove the directory if it's empty @rmdir($path); } else { // Remove file if (@unlink($path)) { $count++; } } } return $count; } // Hook for adding button to admin panel function fn_a_code_casch_buttons_data_post(&$buttons) { $buttons['a_code_casch'] = array( 'href' => 'a_code_casch.clean', 'position' => 50, 'title' => __('a_code_casch.clean_cache') ); } // Hook for settings page function fn_a_code_casch_get_route_runtime(&$req, $controller, $mode, $action) { if ($controller == 'a_code_casch' && $mode == 'clean') { if (!empty($_REQUEST['clean_cache']) && $_REQUEST['clean_cache'] == 'Y') { fn_a_code_casch_delete_cache(); } return array(CONTROLLER_STATUS_REDIRECT, 'index.index'); } } // Get scheduled cleaning period function fn_a_code_casch_get_schedule_period() { return Registry::get('addons.a_code_casch.cleaning_period'); }