>"); /* * Retrieve all user and system tasks or execute one task when passed as argument */ if ($argc > 1) { // Second argument required? if ((!in_array($argv[1], array("lance_report","project_report"))) || ($argc > 2)) { $tasks[0]['param'] = serialize(array(task_id => $argv[1])); // Get task id for this task $all_tasks = db_fetch_tasks(); if (is_array($all_tasks)) { foreach($all_tasks as $task) { // Unserialize and parse parameters $unserialized_params = unserialize($task['param']); // Check task id if (strtolower($unserialized_params['task_id']) == strtolower($argv[1])) { // More arguments passed? Check description if ($argc > 2) { if (strtolower($unserialized_params['omschrijving']) == strtolower($argv[2])) { // Define id $tasks[0] = $task; } } else { // Define id $tasks[0] = $task; } } } } } } else { $tasks = db_fetch_tasks(); // Add some extra task which are called every time the task manager runs if (!is_array($tasks)) { $tasks = array(); } array_push($tasks, array(gebruiker => 0, omschrijving => "twitter_cache", update => "2012-01-22 11:00:00", param => "a:1:{s:7:\"task_id\";s:13:\"twitter_cache\";}")); array_push($tasks, array(gebruiker => 0, omschrijving => "connection_statistics", update => "2013-02-14 00:00:00", param => "a:2:{s:7:\"task_id\";s:21:\"connection_statistics\";s:10:\"time_param\";a:6:{s:7:\"rec_pat\";s:5:\"daily\";s:14:\"rec_pat_param1\";s:3:\"day\";s:13:\"rec_pat_start\";s:10:\"2013-02-14\";s:11:\"rec_pat_end\";s:0:\"\";s:12:\"rec_pat_time\";s:5:\"22:00\";s:10:\"rec_pat_cc\";s:0:\"\";}}\")")); } $pid_array = array(); // Handle tasks parallel if (is_array($tasks)) { foreach($tasks as $task) { // Initial values $valid = 0; $params = ""; $handled = FALSE; $update_state = FALSE; $syslog = array(); // Define base path $_PAGE_INFO['base_path'] = dirname(__FILE__) . "/../../"; // Set timezone when user exists $user = db_fetch_user($task['gebruiker'], "", 1); if (is_array($user)) { putenv("TZ=" . $user['tz']); } // Get current time (and the month/day/hour before) $now = date('Y-m-d H:i:s'); $now_hour_before = convert_datetime(time() - (60 * 60), TRUE); $now_day_before = convert_datetime(time() - (24 * 60 * 60), TRUE); $now_month_before = convert_datetime(time() - (30 * 24 * 60 * 60), TRUE); // Unserialize and parse parameters $unserialized_params = unserialize($task['param']); if (is_array($unserialized_params)) { foreach($unserialized_params as $key1 => $value1) { // Handle time_param array if (is_array($value1)) { foreach($value1 as $key2 => $value2) { $params[$key1][$key2] = $value2; } } // Handle parameters else { $params[$key1] = $value1; } } } // Active task or overruled by argument? if ((!array_key_exists($params['task_id'], $_ACTIVE_TASKS)) || ($argc > 1) || ($_ACTIVE_TASKS[$params['task_id']])) { // Time/date check, valid or no time parameters => always execute task if (is_array($params['time_param'])) { // Valid period if (is_array(convert_timeperiod($now, "", $params['time_param']['rec_pat_start'], ($params['time_param']['rec_pat_end'] == "end_date") ? $params['time_param']['rec_pat_end_param1'] : ""))) { // Valid occurences if (($params['time_param']['rec_pat_end'] != "occurences") || ($params['time_param']['rec_pat_end_param1'])) { // Get start/end times $start_time = (convert_datetime(date('Y-m-d') . " " . $params['time_param']['rec_pat_time'])); $end_time = $start_time + (15 * 60); // Valid time (within the range of 15 minutes!!!) if (($start_time <= (convert_datetime($now))) && ((convert_datetime($now)) < $end_time)) { // Handle recurrence pattern switch($params['time_param']['rec_pat']) { case "daily": // Check parameters switch($params['time_param']['rec_pat_param1']) { // Every day (always valid) case "day": // Log $syslog['recurrence'] = "every day"; // Valid task $valid = 1; $start_date = convert_datetime((convert_datetime($now) - (24*60*60)),1); break; // Every workday (monday - friday valid) case "workday": // Log $syslog['recurrence'] = "every workday"; $workdays = array("monday","tuesday","wednesday","thursday","friday"); if (in_array(strtolower(date('l')), $workdays)) { // Valid task $valid = 1; if (date('l') != "monday") { $start_date = convert_datetime((convert_datetime($now) - (24*60*60)),1); } else { $start_date = convert_datetime((convert_datetime($now) - (3*24*60*60)),1); } } break; default: break; } break; case "weekly": // Log $syslog['recurrence'] = strtolower($params['time_param']['rec_pat_param1']) . " every week"; // Valid day? if (strtolower(date('l')) == strtolower($params['time_param']['rec_pat_param1'])) { // Valid task $valid = 1; $start_date = convert_datetime((convert_datetime($now) - (7*24*60*60)),1); } break; case "monthly": // Log $syslog['recurrence'] = $params['time_param']['rec_pat_param1'] . " " . $params['time_param']['rec_pat_param2'] . " of the month"; // Handle parameter 1 if (in_array($params['time_param']['rec_pat_param1'], array("first","second","third","fourth"))) { $param1 = array_search($params['time_param']['rec_pat_param1'], array("first","second","third","fourth")) + 1; } else { // Get last day of the month if (in_array(date('n'), array(1,3,5,7,8,10,12))) { $param1 = 31; } else if (in_array(date('n'), array(4,6,9,11))) { $param1 = 30; } else { $param1 = ((date('Y') % 4) == 0) ? 29 : 28; } } // Handle parameter 2 if (strtolower($params['time_param']['rec_pat_param2']) == "day") { if (date('j') == $param1) { // Valid task $valid = 1; } } else if (strtolower(date('l')) == strtolower($params['time_param']['rec_pat_param2'])) { // Check first/second/third/fourth if ($param1 < 5) { if (ceil(date('j')/7) == $param1) { // Valid task $valid = 1; } } // Check last else if ((date('j') + 7) > $param1) { // Valid task $valid = 1; } } // Define month before if ($valid) { $start_date = $now_month_before; } break; default: break; } } else { // Log $syslog['info'] = "No valid start time"; } } else { // Log $syslog['info'] = "No more occurences"; } } else { // Log $syslog['info'] = "No valid timestamp"; } } else { // Log $syslog['recurrence'] = "always"; // Valid task $valid = 1; } // Extra check on CLI command if (!$valid && ($argc > 1)) { $valid = 1; } // Write log $syslog_notice = "Task manager, Valid: " . $valid . ", Description: " . $task['omschrijving'] . ", Task: " . $params['task_id']; if ($valid) { $syslog_notice .= ", Recurrence: " . $syslog['recurrence']; } if (strlen($syslog['info'])) { $syslog_notice .= ", Info: " . $syslog['info']; } $syslog_notice .= ", Now: " . $now . " (" . __FILE__ . ")"; DBG($syslog_notice); } else { DBG("skip task: " . $params['task_id']); } // Valid task if ($valid) { // Create child $pid = pcntl_fork(); // Invalid pid => exit if ($pid == -1) { exit(0); } // We are the child handle task else if ($pid == 0) { // Reopen db with new indentifier db_connect(); // Set language when user exists if (is_array($user)) { i18n_settext_language($user['i18n'], $_PAGE_INFO['base_path'] . "locale/"); } // Mail send to user/last status? $error = array(); $equipment_error = db_fetch("zkl_logerror"); if (is_array($equipment_error)) { foreach($equipment_error as $item) { if ($task['id'] == $item['task_id']) { array_push($error, $item['zkl']); } } } // Set time time (max 13 minutes, because the system task runs every 15 minutes) and memory limit (same as http deamon) // This to prevent never-ending tasks set_time_limit(780); ini_set('memory_limit', '2048M'); // Run the task if( $_SCRIPT_ALIAS[$params['task_id']] ) { $taskname = $_SCRIPT_ALIAS[$params['task_id']]; } else { $taskname = $params['task_id']; } if (file_exists("./tasks/" . $taskname . ".php")) { include("./tasks/" . $taskname . ".php"); } // Occurences (only when task handled) if (($handled) && $params['time_param']['rec_pat_end'] == "occurences") { // Decrement occurences --$unserialized_params['time_param']['rec_pat_end_param1']; $update_state = TRUE; } if( $handled && $update_state ) { // Update task db_update_task(array('gebruiker' => $task['gebruiker'], 'omschrijving' => $task['omschrijving'], 'param' => serialize($unserialized_params), 'id' => $task['id'])); } // Default child exit exit(0); } // We are the parent else { // Reopen db with new indentifier db_connect(); // Collect child ids array_push($pid_array, $pid); } } } } // Wait until all childs are finished if (is_array($pid_array)) { foreach ($pid_array as $pid) { pcntl_waitpid($pid, $status); } } // Debug info DBG("<<== Taskmanager end ==>>"); } else { // Debug info DBG("<<== Taskmanager: Inactive branch ==>>"); } } ?>