* @copyright 2018 Dhanashree Inc * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL License * @version SVN: 1.0.0 * @link http://www.dhanashree.com/ * @since File available since Release 1.0.0 */ if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } if (!class_exists('Accu_Auto_Backup_Cron_Manager')) { /** * Cron Class * * PHP version 5 * * This class concern with all cron/scheduler feature all over the plug-in. * * LICENSE: GPL-2.0+ * * @category Module * @package Accu_Auto_Backup * @subpackage Accu_Auto_Backup/includes * @author Dhanashree Inc * @copyright 2018 Dhanashree Inc * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL License * @version Release: 1.0.0 * @link http://www.dhanashree.com/ * @since File available since Release 1.0.0 */ class Accu_Auto_Backup_Cron_Manager { /** * The single instance of the class. * * @var Accu_Auto_Backup_Cron_Manager */ protected static $instance = null; private $_cron_status = null; private $_accu_tbl = null; private $_accu_cron_url = null; private $_accu_cron_get_url = null; private $_accu_cron_path = null; private $_accu_cron_exec_path = null; private $_accu_existing_cron_job_list = array(); /** * Constructer of Cron Job * * @return null */ public function __construct() { global $wpdb; if (!isset($this->_cron_status) || null === $this->_cron_status) { $this->_cron_status = $this->validateCron(); } $this->_accu_tbl = $wpdb->prefix . 'accubackup'; $this->_accu_cron_url = ACCU_DIR_URL . 'accu-auto-backup-cron.php'; $this->_accu_cron_get_url = home_url('index.php?accu_auto_backup_cron=yes'); $this->_accu_cron_path = ACCU_DIR_PATH . 'accu-auto-backup-cron.php'; $this->_accu_cron_exec_path = ACCU_DIR_PATH . 'log/accu-cron.txt'; // } /** * Main Accu_auto_backup_Cron_manager Instance. * * Ensures only one instance of Accu_auto_backup_Cron_manager is loaded or can be loaded. * * @since 1.0.0 * @static * * @see Accu_cron_mgr() * * @return Accu_cron_mgr - Main instance. */ public static function instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } /** * Will append response data to accu_cron.txt file. * * @param type $response Response of called function. * * @return null */ public function accuLog($response) { $log_file = ACCU_LOG_FILE; if ($response) { $response_txt = '-----' . current_time('Y-m-d h:i:s') . "-----\r\n" . $response . "\r\n\r\n"; @file_put_contents($log_file, $response_txt, FILE_APPEND); //-- remoe file if it exceeds from 5 MB if (file_exists($log_file)) { $filesize = filesize($log_file); // bytes $filesize = round($filesize / 1024, 2); // KB with two digits if ($filesize > 5000) { @file_put_contents(ACCU_LOG_FILE, ''); } } } } /** * Will run cron using front website query string. * * @return null */ public function runFrontCron() { if (!is_admin()) { $inputs_value = filter_input(INPUT_GET, 'accu_auto_backup_cron'); if (isset($inputs_value) && null !== $inputs_value && '' !== $inputs_value && 'yes' === $inputs_value) { /** * Check first cron has been enabled or not. */ $validate_cron_run = $this->validateCron(); if (true === $validate_cron_run) { $response = $this->runCron(); } else { $accu_errors = $validate_cron_run; $this->accuLog($accu_errors); } } return null; } } /** * Run Cron file. * * @return type */ public function runCron() { $response = null; $get_current_tmp = current_time('timestamp', 1); $get_next_tmp = $this->getBackupDetails('next'); if ((float) $get_current_tmp >= (float) $get_next_tmp) { $response = apply_filters('accu_backup_event', $response); $this->accuLog($response); } return $response; } /** * Validate the Current cron Job. * * @return type */ public function validateCron() { $check_cron_enable = get_option('accu_auto_backup_sc_auto_enabled'); $get_cron_method = get_option('accu_auto_backup_backup_method'); $output = true; if (!isset($check_cron_enable) || null === $check_cron_enable || 'no' == $check_cron_enable) { $output = __('Accu Auto Backup Cron has been disabled.', 'accu-auto-backup'); } return $output; } /** * Get Cron Information in General Tab. * * @return html cron html */ public function getCronInfo() { $check_cron_enable = get_option('accu_auto_backup_sc_auto_enabled'); if (false !== $check_cron_enable && 'yes' === $check_cron_enable) { //echo get_date_from_gmt( date( 'Y-m-d H:i:s', $my_unix_timestamp ), 'F j, Y H:i:s' ) $c_time = get_date_from_gmt(date('Y-m-d H:i:s'), 'D, F j, Y H:i'); //$next_time = date('D, F j, Y H:i', $this->get_backup_details('next')); //$last_time = date('D, F j, Y H:i', $this->get_backup_details('last')); $next_time = get_date_from_gmt(date('Y-m-d H:i:s', $this->getBackupDetails('next')), 'D, F j, Y H:i'); $last_time = get_date_from_gmt(date('Y-m-d H:i:s', $this->getBackupDetails('last')), 'D, F j, Y H:i'); ?>
General ", 'accu-auto-backup'); ?>
:
:

calculateNext($this->getBackupDetails('last'), get_option('accu_auto_backup_sc_auto_interval')); } else { $return = __('Please select backup interval from settings.', 'accu-auto-backup'); } break; default: break; } } return $return; } /** * Output HTML for Manual Cron for General Tab. * * @return null */ public function getManualCronHtml() { $cron_url = $this->_accu_cron_url; $cron_path = $this->_accu_cron_path; ?>
isFuncEnabled('shell_exec')) { //set the cron //$this->accuAppendCronjob('* * * * * php ' . $this->_accu_cron_path); $this->accuAppendCronjob('* * * * * GET "' . $this->_accu_cron_get_url . '"'); //$response = $this->accu_remove_cronjob('* * * * * php ' . $this->accu_cron_path); } else { $return = __('Permission denie! You are not able to run shell commands. please contact for hosting provider to enable.', 'accu-auto-backup'); } } elseif (false !== $get_cron_status && $get_cron_status == 'yes' && false !== $get_cron_method && 'manual' == $get_cron_method) { $this->removeAccuAutoCronJobLine(); } return $return; } /** * Will Remove the cron line from linux crontab. * * @return null */ public function removeAccuAutoCronJobLine() { if (true === $this->isFuncEnabled('shell_exec')) { //$this->accuRemoveCronjob('* * * * * php ' . $this->_accu_cron_path); $this->accuRemoveCronjob('* * * * * GET "' . $this->_accu_cron_get_url . '"'); } } /** * Check extension or permission for shell_exec or exec command to run for user. * * @param type $func name of function to check. * * @return type */ public function isFuncEnabled($func) { return is_callable($func) && false === stripos(ini_get('disable_functions'), $func); } /** * Check new/existing line available in crontab of linux system or not. * * @param type $command command to check. * * @return boolean */ public function accuCronjobExists($command) { $cronjob_exists = false; //$current_user = trim(shell_exec('whoami')); //exec('crontab -l | grep -i '.$command, $crontab); exec('crontab -l', $crontab); $filename = $this->_accu_cron_exec_path; @file_put_contents($filename, $crontab); if (isset($crontab) && is_array($crontab)) { $this->_accu_existing_cron_job_list = $crontab; foreach ($crontab as $key => $single_value) { if ($single_value == $command) { $cronjob_exists = true; break; } } } return $cronjob_exists; } /** * Get current user IP to track the reference for log. * * @return type */ public function accuGetTheUserIp() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } /** * Will Append the cronjob line to linux system crontab. * * @param type $command command to add into crontab. * * @return type */ public function accuAppendCronjob($command) { $output = null; if (is_string($command) && !empty($command) && $this->accuCronjobExists($command) === false) { if (file_exists($this->_accu_cron_exec_path) && is_writable($this->_accu_cron_exec_path)) { $this->_accu_existing_cron_job_list[] = PHP_EOL . $command . PHP_EOL; $new_list = implode("\n", $this->_accu_existing_cron_job_list); @file_put_contents($this->_accu_cron_exec_path, $new_list); shell_exec('crontab -i ' . $this->_accu_cron_exec_path); //$output = shell_exec('crontab -l'); //file_put_contents($this->accu_cron_exec_path, $output . $command . PHP_EOL); $get_current_time = current_time('Y-m-d H:i:s'); $get_current_ip = $this->accuGetTheUserIp(); $response = sprintf(__('Accu backup auto cron has been setup on : %1$s from IP Address : %2$s', 'accu-auto-backup'), $get_current_time, $get_current_ip); $this->accuLog($response); } else { $this->accuLog(__('Accu backup could not able to create cron text file. please check with write permission', 'accu-auto-backup')); } } return $output; } /** * Remove the line or command from crontab of linux system. * * @param type $command command to remove from crontab. * * @return null */ public function accuRemoveCronjob($command) { $output = null; if (is_string($command) && !empty($command) && $this->accuCronjobExists($command) === true) { if (null !== $this->_accu_existing_cron_job_list) { $get_key = array_search($command, $this->_accu_existing_cron_job_list); $get_current_time = current_time('Y-m-d H:i:s'); $get_current_ip = $this->accuGetTheUserIp(); unset($this->_accu_existing_cron_job_list[$get_key]); $new_list = implode("\n", $this->_accu_existing_cron_job_list); @file_put_contents($this->_accu_cron_exec_path, $new_list); $removed_output = null; exec('crontab -i ' . $this->_accu_cron_exec_path, $removed_output); $response = sprintf(__('Accu backup auto cron command : %1$s has been removed on : %2$s from IP Address : %3$s', 'accu-auto-backup'), $command, $get_current_time, $get_current_ip); $this->accuLog($response); } } } } /** * Main instance of Accu Cron Job * * Returns the main instance of Accu_Cron to prevent the need to use globals. * * @since 1.0.0 * @return Accu_Auto_Backup_Cron_Manager instance * */ function accuCronManager() { return Accu_Auto_Backup_Cron_Manager::instance(); } // Global for backwards compatibility. $GLOBALS['accu_cron_mgr'] = accuCronManager(); }