Files
resatennis_tyroltenn/IDrive/.config/c.Scheduler.php

604 lines
20 KiB
PHP
Executable File

<?php
class Scheduler {
public static $webDirPath;
public static $popupListPath;
public static $script_dir;
public static $machine;
public static $appVersion;
public static $releaseDate;
public static $user;
public static $user_name;
public static $user_dir;
public static $nasUser_dir;
public static $service_dir;
public static $restore_search_dir;
public static $backup_search_dir;
public static $delete_dir;
public static $delete_log_dir;
public static $backup_dir;
public static $restore_dir;
public static $restore_list_dir;
public static $localbackup_dir;
public static $localrestore_dir;
public static $expressDBPath;
public static $hour;
public static $minutes;
public static $day_of_month;
public static $meridian = array("am"=>"AM", "pm"=>"PM");
public static $weekdays = array("SUN"=>"Sunday", "MON"=>"Monday", "TUE"=>"Tuesday", "WED"=>"Wednesday", "THU"=>"Thursday", "FRI"=>"Friday", "SAT"=>"Saturday");
public static $backup_script = "Backup_Script_IDrive.php";
public static $restore_script = "Restore_Script_IDrive.php";
// public static $configtxt = CONFIG_FILE;
public static $poplisttxt = POP_UP_LIST_FILE;
public static $mpctxt = MPC_FILE;
public static $backup = "Backup";
public static $search = ".+searchList";
public static $restore = "Restore";
public static $restoreList = ".RestoreList";
public static $localBackup = "LocalBackup";
public static $localRestore = "LocalRestore";
public static $expressDBDir = "ExpressDB";
public static $delete = "Delete";
public static $delete_log = "LOGS";
public static $manual_dir = "+ManualTask";
public static $trash_manual_dir = "+TrashManualTask";
private static $log = true;
public static $isScheduled = "";
public static $temp_file;
public static $php_path = PHP_PATH_FOR_SYNOLOGY;
public static $php_dir;
public static $archive = ARCHIVE_JOB;
public static $archiveclean_dir; //Added by sajana for archive cleanup... $archive
public static $appUpdateLockDir = '';
public static $appUpdateLockFile = '';
/* public static function ReadConfigFile() {
echo self::$service_dir;exit;
$conf_file = dirname(__FILE__)."/".self::$configtxt;
if(file_exists($conf_file)){
$file = fopen($conf_file,"r");
while(!feof($file)){
$pair = fgets($file);
print_r(explode(" ",$pair));
$hash[array_shift($pair)] = array_shift($pair);
$line = fgets($file);
$arr = explode("=", $line);
$key = trim($arr[0]);
if($key == "user") {
$key = "nasUser";
}
$value = trim($arr[1]);
$_SESSION[$key] = $value;
}exit;
fclose($file);
}
}*/
public static function GetHost($username) {
$host = '';
$mpc = self::$nasUser_dir."/$username/".self::$mpctxt;
if(file_exists($mpc) && customFileSize($mpc)>0) {
$content = file_get_contents($mpc);
$host = trim($content);
} else if(!$_SESSION['dedup']){
$host = update_mpc();
}
return $host;
}
public static function SetSchedulerPath($webDir, $userName, $user, $machine) {
GLOBAL $appVersion;
self::$webDirPath = getWebDirPath();
self::$script_dir = self::$webDirPath."/".APPDIR."/".SCRIPT."/";
self::$php_dir = self::$webDirPath."/".APPDIR."/".CONFIG."/";
self::$machine = $machine;
self::$user = $user;
self::$user_name = $userName;
self::$service_dir = self::$webDirPath."/".SERVICE."/";
# commented by anil to hide NAS user concept.
self::$nasUser_dir = self::$service_dir.USER_PROFILE."/$user/";
//self::$nasUser_dir = self::$service_dir.USER_PROFILE."/";
self::$user_dir = self::$nasUser_dir."$userName/";
self::$backup_dir = self::$user_dir.self::$backup."/";
self::$restore_dir = self::$user_dir.self::$restore."/";
self::$localbackup_dir = self::$user_dir.self::$localBackup."/";
self::$localrestore_dir = self::$user_dir.self::$localRestore."/";
self::$expressDBPath = self::$user_dir . self::$expressDBDir . '/';
self::$backup_search_dir = self::$backup_dir.self::$search."/";
self::$restore_search_dir = self::$restore_dir.self::$search."/";
self::$delete_dir = self::$user_dir.self::$delete."/";
self::$delete_log_dir = self::$delete_dir.self::$delete_log."/";
self::$popupListPath = self::$user_dir.self::$poplisttxt;
self::$restore_list_dir = self::$user_dir.self::$restoreList;
self::$hour = range(1,12);
self::$minutes = range(0,59);
self::$day_of_month = range(1,31);
self::$temp_file = self::$user_dir.TEMP_OPERATION_FILE;
self::$archiveclean_dir = self::$user_dir.self::$archive."/";
self::$appUpdateLockDir = self::$service_dir.'Update';
self::$appUpdateLockFile = self::$service_dir.'Update/lock';
machineBasedContent($machine);
}
public static function AddJob($data, $job) {
$array_data = self::ReadJobs();
$array_data[$job] = array();
$array_data[$job] = $data;
$json_data = json_encode($array_data);
self::UpdateJob($json_data);
}
public static function ReadJobs($type = "web", $username = "") {
$poplist = "";
if($type == "web") {
$poplist = self::$popupListPath;
}else if($type == "cmd"){
$poplist = self::$nasUser_dir."/$username/".self::$poplisttxt;
}
$array_data = array();
if(file_exists($poplist)) {
$content = file_get_contents($poplist);
$array_data = json_decode($content, true);
}else {
$array_data = array();
}
return $array_data;
}
public static function UpdateJob($json_data) {
GLOBAL $permissionVal;
file_put_contents(self::$popupListPath, $json_data);
@chmod(self::$popupListPath,$permissionVal);
}
public static function AddCutOffCron($job, $data, $cront_time) {
try {
$HOUR = "*";
if(isset($data["cutoff_meridian"])) {
if($data["cutoff_meridian"] == "pm") {
if($data["cutoff_hour"]<12) {
$HOUR = 12 + $data["cutoff_hour"];
} else {
$HOUR = $data["cutoff_hour"];
}
} else {
if($data["cutoff_hour"]>=12) {
$HOUR = 12 - $data["cutoff_hour"];
} else {
$HOUR = $data["cutoff_hour"];
}
}
}
$MIN = $data["cutoff_minute"];
$DOM = "*";
$MON = "*";
$DOW = "*";
if(isset($data["scheduler_selection"]) && $data["scheduler_selection"] == "monthly" && $cront_time["DOM"] != "*") {
if($cront_time["HOUR"] > $HOUR) {
$DOM = $cront_time["DOM"] + 1;
if($DOM > 31){
$DOM = 1;
}
}else {
$DOM = $cront_time["DOM"];
}
}else if(!isset($data["scheduler_selection"]) && ($cront_time["DOW"] != "*" && $cront_time["DOW"] != '')){
if($cront_time["HOUR"] > $HOUR) {
$dow_arr = explode(",", $cront_time["DOW"]);
$dow_arr_shift = array();
foreach($dow_arr as $day) {
$val = $day + 1;
if($val == 7) {
$val = 0;
}
$dow_arr_shift[] = $val;
}
$DOW = implode(",", $dow_arr_shift);
}else {
$DOW = $cront_time["DOW"];
}
}
$content = getCronContent($MIN, $HOUR, $DOM, $MON, $DOW);
$command = self::CreateCutOffCommand($job, $data["job_type"]);
$content .= $command;
return $content;
} catch (Exception $e) {
print_r($e);exit;
}
}
public static function RemoveCutOffCron($job, $job_type) {
$content = self::CreateCutOffCommand($job, $job_type);
self::RemoveCronJob($content, "CutOff");
}
public static function CreateCutOffCommand($job, $job_type) {
$perlPath = getPerlPath();
$phpPath = getPhpPath();
if(strpos($job,$job_type.'_')!== false){
$job = substr($job,strlen($job_type.'_'));
}
//$command = "cd \"".self::$user_dir.$job_type."/".$job."\"; $perlPath ".self::$script_dir.JOBTERMINATE;
$command = "cd \"".self::$user_dir.$job_type."/".$job."\"; $phpPath ".self::$script_dir.JOBTERMINATE;
return $command;
}
public static function UpdateJobData($job, $data, $weekday_selection = array()) {
$jobs = self::ReadJobs();
$job_type = $jobs[$job]["job_type"];//added by avinash 09 May 2014.
$jobName = substr($job,(strlen($job_type)+1));//added by avinash 09 May 2014.
$jobs[$job]["scheduler"] = array();
$final_content ="";
//if($data['start_immediately']=='' && $data['start_minute']!=''){
if(!isset($data['start_immediately']) && isset($data['start_minute'])){
foreach($data as $k=>$schedule) {
$jobs[$job]["scheduler"][$k] = $schedule;
}
if(!empty($weekday_selection)) {
$jobs[$job]["scheduler"]["weekday_selection"] = json_encode($weekday_selection);
}else {
if(isset($jobs[$job]["scheduler"]["weekday_selection"])) {
unset($jobs[$job]["scheduler"]["weekday_selection"]);
}
}
$json_data = json_encode($jobs);
self::UpdateJob($json_data);
$cront_time = self::JobCronTime($job, $data, $weekday_selection);
if(( isset($data["scheduler_selection"]) || isset($data["weekday_selection"]) ) ) {
$final_content .= self::AddCronEntry($job, $data, $cront_time);
}
if(isset($data["cut_off_time"]) && $data["cut_off_time"] == "on") {
$final_content .= "\n";
$final_content .= self::AddCutOffCron($job, $data, $cront_time);
}
if($final_content != ""){
self::AddCronJob($job, $job_type, $final_content);
self::Log("", "content : ", $final_content);
}
} else if(!isset($data['start_immediately']) && !isset($data['start_minute'])){
unset($jobs[$job]["scheduler"]);
$json_data = json_encode($jobs);
self::UpdateJob($json_data);
self::RemoveCronEntry($jobName, $job_type, "");
} else {
if(isset($data['email_notify'])){
foreach($data as $k=>$schedule) {
if($k != "start_immediately" && !strstr($k,"cut_off") && !strstr($k,"cutoff")){
$jobs[$job]["scheduler"][$k] = $schedule;
}
}
} else {
unset($jobs[$job]["scheduler"]);
}
if(isset($data["cut_off_time"]) && $data["cut_off_time"] == "on") {
$final_content .= self::AddCutOffCron($job, $data, $cront_time);
}
self::AddCronJob($job, $job_type, $final_content);
$json_data = json_encode($jobs);
self::UpdateJob($json_data);
}
return $json_data;
}
public static function JobCronTime($job, $data, $weekday_selection) {
$MIN = $data["start_minute"];
if(isset($data["scheduler_selection"]) && $data["scheduler_selection"] == "hourly") {
$HOUR = "*";
}
else {
if($data["start_meridian"] == "pm") {
if($data["start_hour"]<12){
$start_hour = 12 + $data["start_hour"];
} else {
$start_hour = $data["start_hour"];
}
}
else {
if($data["start_hour"]>=12){
$start_hour = 12 - $data["start_hour"];
} else {
$start_hour = $data["start_hour"];
}
}
$HOUR = $start_hour;
}
if(isset($data["scheduler_selection"]) && $data["scheduler_selection"] == "monthly") {
$DOM = $data["day_of_month"];
}
else {
$DOM = "*";
}
$MON = "*";
if(empty($weekday_selection)) {
$DOW = "*";
}
else {
$val = array("SUN"=>0, "MON"=>1, "TUE"=>2, "WED"=>3, "THU"=>4, "FRI"=>5, "SAT"=>6);
$DOW = "";
if(count($weekday_selection) < 7) {
foreach($weekday_selection as $day) {
$DOW .= $val[$day].",";
}
$DOW = substr($DOW, 0, -1);
}
else {
$DOW = "*";
}
}
$cront_time = array("MIN"=>$MIN, "HOUR"=>$HOUR, "DOM"=>$DOM, "MON"=>$MON, "DOW"=>$DOW);
return $cront_time;
}
public static function StartJobImmediately($job, $job_type) {
$command = self::CreateJobCommand($job, $job_type, false);
$command .= " > /dev/null 2>/dev/null &";
$retval = shell_exec("$command");
return $retval;
}
public static function RemoveCronEntry($job, $job_type, $status) {
$content = '"'.self::$user_dir.$job_type."/".$job.'"';
self::RemoveCronJob($content, $status);
}
public static function RemoveCronJob($content, $getStatus) {
GLOBAL $permissionVal;
file_put_contents(self::$temp_file,REMOVECRON);
file_put_contents(self::$temp_file, "\n".$content."\n", FILE_APPEND);
file_put_contents(self::$temp_file, $getStatus, FILE_APPEND);
@chmod(self::$temp_file, $permissionVal);
#self::executeScript(OPERATIONSFILE." ".REMOVECRON, true, "", "");
self::executeScript(OPERATIONSFILE, true, "", "");
}
public static function executeScript($script, $job_dir, $job, $job_type) {
GLOBAL $machine;
$currPath = "";
if($job_dir == true) {
if($job == '') {
$currPath = self::$user_dir . $job_type;
} else {
$currPath = self::$user_dir . $job_type . "/{$job}";
}
}
$retVal = '';
$retVal = phpScriptExecution($script, $currPath, 1, TRUE);
return $retVal;
}
public static function AddCronEntry($job, $data, $cront_time) {
$MIN = $cront_time["MIN"];
$HOUR = $cront_time["HOUR"];
$DOM = $cront_time["DOM"];
$MON = $cront_time["MON"];
$DOW = $cront_time["DOW"];
$content = getCronContent($MIN, $HOUR, $DOM, $MON, $DOW);
$command = self::CreateJobCommand($job, $data["job_type"]);
$content .= $command;
return $content;
}
public static function AddCronJob($job, $job_type, $content) {
GLOBAL $permissionVal;
if(strpos($job,$job_type.'_')!== false) {
$job = substr($job,strlen($job_type.'_'));
}
$job_path = '"'.self::$user_dir.$job_type."/".$job.'"';
file_put_contents(self::$temp_file,UPDATECRON);
file_put_contents(self::$temp_file,"\n".$job_path, FILE_APPEND);
file_put_contents(self::$temp_file, "\n".$content, FILE_APPEND);
@chmod(self::$temp_file, $permissionVal);
self::executeScript(OPERATIONSFILE, true, "", "");
}
public static function CdJobDirCommand($job, $job_type) {
$command = "cd \"".self::$user_dir.$job_type."/".$job."\"; ";
return $command;
}
public static function CreateJobCommand($job, $job_type, $cron = true) {
if($job_type == "Backup") {
$script = self::$backup_script;
} else if($job_type == "Restore") {
$script = self::$restore_script;
}
if(strpos($job,$job_type.'_')!== false)
$job = substr($job,strlen($job_type.'_'));
$perlPath = getPerlPath();
$phpPath = getPhpPath();
$currPath = self::$user_dir.$job_type."/$job";
if(!$cron){
//$command = perlScriptExecution($script, $currPath, 0);
$command = phpScriptExecution($script, $currPath, 0);
} else {
if($job_type == ARCHIVE_JOB){
$command = "cd \"$currPath\"; $phpPath ".self::$php_dir.PERIODIC_CRON;
}else{
$command = "cd \"$currPath\"; $phpPath ".self::$script_dir.$script;
}
}
return $command;
}
public static function DeleteJob($job) {
if (file_exists(self::$temp_file)) {
$jobs_array = self::ReadJobs();
$json_data = json_encode($jobs_array);
return $json_data;
}
else {
$jobs_array = self::ReadJobs();
$job_type = $jobs_array[$job]["job_type"];
if(isset($jobs_array[$job])) {
unset($jobs_array[$job]);
}
$jobName = substr($job,(strlen($job_type)+1)); // Added by Senthil on May08,2014
$json_data = json_encode($jobs_array);
self::UpdateJob($json_data);
$pid_file = self::$user_dir.$job_type."/".$jobName."/".PID_FILE;
if(file_exists($pid_file)){
self::CancelJob($job);
}
self::RemoveCronEntry($jobName, $job_type, "Delete");
self::RemoveCronEntry($jobName, ARCHIVE_JOB, "Delete");
return $json_data;
}
}
public static function JobInProgress($job, $job_type) {
$pid_files = glob( self::$user_dir.$job_type."/".$job."/".PID_FILE );
return $pid_files;
}
public static function CancelJob($job) {
GLOBAL $permissionVal;
$arr = explode('_', $job, 2);
$job_type = $arr[0];
$job = $arr[1];
$userCancelFile = self::$user_dir.$job_type."/".$job."/".CANCEL_PATH;
if(!file_exists($userCancelFile)){
file_put_contents($userCancelFile, '');
@chmod($userCancelFile, $permissionVal);
}
self::executeScript(JOBTERMINATE, true, $job, $job_type);
}
public static function DirProperties($fullPath) {
//Added to enumerate folder which is having single quote(') in name
$fullPath = str_replace("'","'\''",$fullPath);
$res = getListForDirectory($fullPath);
$res1 = explode("\n", $res);
$res1 = array_filter($res1);
$dir_list = array();
foreach($res1 as $res2) {
$split = preg_split('/\s+/', $res2,5);
$split1 = preg_split('/\s+/', $split[4],2);
$size = trim($split1[0]);
$date = trim(substr($split1[1],0,24));
$dirname = substr($split1[1],25);
if(trim($dirname)!='.' && trim($dirname)!='..'){
$type = substr($split[0],0,1);
$type = $type=='d'?"D":"F";
$dir_list[$dirname]["size"] = humanReadableSize(trim($size));
$parts = preg_split('/\s+/', $date);
$dir_list[$dirname]["datetime"] = date("Y/m/d g:i:s A", strtotime($parts[2]." ".$parts[1]." ".$parts[4]." ".$parts[3]));
$dir_list[$dirname]["type"] = $type;
}
}
ksort($dir_list);
return $dir_list;
}
/*********************************************************************************************************
* Function Name :
* Objective : This method records the activity in log file. This log is used for send error functionality.
* Added By : Sabin Cheruvattil - 2015-05-06
**********************************************************************************************************/
public static function Log($data = "", $trace = array(), $access = "") {
GLOBAL $permissionVal, $mode, $machine, $appVersion, $releaseDate;
$currVersion ='';
$user = "";
if(!empty($appVersion))
$currVersion = trim(str_replace("Version","",$appVersion));
$trace_dir_name = '.trace';
if($data == '') {
return FALSE;
}
if($mode === 'debug' && $access != "SYNXKH#HG") {
return;
}
try {
if(self::$log) {
if(!is_array($trace) || empty($trace)) {
$trace = debug_backtrace();
}
if(($machine == ASUSTOR || $machine == SYNOLOGY_LOCAL ) && self::$user == "root")
$user = "admin";
else
$user = self::$user;
$date = date("Y/m/d h:i:s");
$content = "[{$date}][". basename($trace[0]['file']) . "][Line: {$trace[0]['line']}] " . trim($data) . "\n";
$userDir = self::$user_dir;
$traceDir = $userDir . "/{$trace_dir_name}";
if(!is_dir($userDir)) {
mkdir($userDir, $permissionVal);
@chmod($userDir, $permissionVal);
}
if(!is_dir($traceDir)){
mkdir($traceDir, $permissionVal);
@chmod($traceDir, 0777);
}
$log_file = $traceDir."/log_$currVersion.txt";
$log_file_back = $log_file."_".time();
$lock_back_pattern = "/log_*";
if(file_exists($log_file)) {
$fileSize = customFileSize($log_file);
if($fileSize >= MAX_LOG_FILE_SIZE * 1024 * 1024) {
rename($log_file, $log_file_back);
# The following code only needs to run when the log is getting renamed
# as there will be un necesssary checks for the number of log files whenever log method is called
$logs = glob($traceDir.$lock_back_pattern);
if(count($logs) > MAX_LOG_FILE_COUNT) {
while(count($logs) > MAX_LOG_FILE_COUNT) {
unlink(array_shift($logs));
}
}
file_put_contents($log_file, "NAS Profile: ".$user."\nIDrive User name: ".self::$user_name."\n$appVersion\nRelease Date: $releaseDate\n", FILE_APPEND);
}
} else {
# The following code only needs to run when the log is getting renamed
# as there will be un necesssary checks for the number of log files whenever log method is called
$logs = glob($traceDir.$lock_back_pattern);
if(count($logs) > MAX_LOG_FILE_COUNT) {
while(count($logs) > MAX_LOG_FILE_COUNT) {
unlink(array_shift($logs));
}
}
file_put_contents($log_file, "NAS Profile: ".$user."\nIDrive User name: ".self::$user_name."\n$appVersion\nRelease Date: $releaseDate\n", FILE_APPEND);
}
file_put_contents($log_file, $content, FILE_APPEND);
@chmod($log_file, $permissionVal);
}
} catch(Exception $e) {}
}
public static function debug_Log($data = "") {
GLOBAL $mode;
try {
if($mode != 'debug') {
return;
}
$trace = debug_backtrace();
self::Log($data, $trace, "SYNXKH#HG");
} catch (Exception $e) {}
}
}
?>