0) { $console_exists = db_get_row('tconsole', 'id_console', $config['id_console']); if ($console_exists === false) { db_process_sql_insert( 'tconsole', [ 'id_console' => $config['id_console'], 'description' => $config['console_description'], 'version' => $pandora_version, 'console_type' => ($config['reporting_console_node'] === true) ? 1 : 0, 'timezone' => $config['timezone'], 'public_url' => $config['public_url'], ] ); } else { db_process_sql_update( 'tconsole', [ 'description' => $config['console_description'], 'timezone' => $config['timezone'], 'public_url' => $config['public_url'], 'console_type' => (int) $config['reporting_console_node'], 'version' => $pandora_version, ], [ 'id_console' => $config['id_console'], ] ); } } // Maintenance task: schedule daily task to manage GoTTY processes if not defined yet. // Must do at every Cron execution. $gotty_ssh_enabled = (bool) $config['gotty_ssh_enabled']; $gotty_telnet_enabled = (bool) $config['gotty_telnet_enabled']; if ($gotty_ssh_enabled === true || $gotty_telnet_enabled === true) { // Create necessary data in task tables when some method of GoTTY is enabled in setup. if ((bool) $config['enterprise_installed'] === false) { $call_func_user_task_id = db_get_value_sql('SELECT id FROM `tuser_task` WHERE `function_name` = "cron_task_call_user_function"'); if ($call_func_user_task_id === false) { db_process_sql("INSERT INTO `tuser_task` (`function_name`, `parameters`, `name`) VALUES ('cron_task_call_user_function','a:1:{i:0;a:2:{s:11:\"description\";s:13:\"Function name\";s:4:\"type\";s:4:\"text\";}}','Call PHP function')"); } } $user_function_task_id = db_get_value_sql('SELECT id FROM `tuser_task_scheduled` WHERE `args` LIKE "%cron_task_start_gotty%"'); if ($user_function_task_id === false) { // Schedule task to manage GoTTY processes daily if it is not scheduled yet. $this->schedule( 'cron_task_call_user_function', [ 0 => 'cron_task_start_gotty', 'function_name' => 'cron_task_start_gotty', 'internal' => 1, ], 'daily', 0, 0, strtotime('tomorrow') ); } } // Maintenance task: check whether start GoTTY SSH and Telnet processes are running and start otherwise. // Must do at every Cron execution. cron_task_start_gotty(false); // Do not output anything until is completed. There're session // operations inside cron_task_run function. ob_start(); if (cron_task_lock() === false) { // Cannot continue. Locked. echo ob_get_clean(); exit; } $time = get_system_time(); $scheduled_tasks = db_get_all_rows_in_table('tuser_task_scheduled'); if (!$scheduled_tasks) { $scheduled_tasks = []; } /* Watch out! First_execution corresponds to next_execution the name of the bbdd is maintained to ensure integrity. */ foreach ($scheduled_tasks as $task) { $params = unserialize($task['args']); if ($this->shouldTaskRun($task) === false) { continue; } if ($task['scheduled'] == 'no') { if (($params['first_execution']) < $time) { echo date('Y/m/d H:i:s').' Execute once time cron task: '; echo $task['id']; echo "\n\n"; cron_task_run($task['id']); // The task was not scheduled and was executed once. db_process_sql_delete( 'tuser_task_scheduled', ['id' => $task['id']] ); } } else { if (($params['first_execution']) < $time) { echo date('Y/m/d H:i:s').' EXECUTED CRON TASK: '.$task['id']; echo "\n"; echo "\n"; cron_task_run($task['id']); } } } // Dump to output. echo ob_get_clean(); // Release the lock. cron_task_release_lock(); } /** * Schedules a discovery console task to be executed by cron. * * @param string $function_name Name of the function: * cron_task_generate_report * cron_task_generate_report_by_template * cron_task_save_report_to_disk * cron_task_do_backup * cron_task_execute_custom_script * cron_task_save_xml_report_to_disk * cron_task_feedback_send_mail * cron_task_generate_csv_log. * @param array $arguments Task execution arguments (if needed). * @param string $schedule Task schedule options: * 'no', * 'hourly', * 'daily', * 'weekly', * 'monthly', * 'yearly', * 'custom'. * @param integer $group_id Group id (0 => all). * @param string|null $id_user User id, if null, current user. * @param integer|null $time_start When to start, if null, now. * * @return boolean Sucessfully scheduled or not. */ public function schedule( string $function_name, array $arguments=[], string $schedule='no', int $group_id=0, ?string $id_user=null, ?int $time_start=null ) { global $config; if ($id_user === null) { $id_user = $config['id_user']; } $idUserTask = db_get_value( 'id', 'tuser_task', 'function_name', $function_name ); if ($idUserTask === false) { // Failed to identify function. return false; } if (in_array($schedule, self::SCHEDULES) === false) { // Failed to schedule. Not a valid schedule option. return false; } if ($time_start === null) { $time_start = strtotime('now'); } // Params for send mail with cron. $parameters = array_merge( $arguments, [ 'first_execution' => $time_start ] ); // Values insert task cron. $task = [ 'id_usuario' => $id_user, 'id_user_task' => $idUserTask, 'args' => serialize($parameters), 'scheduled' => $schedule, 'id_grupo' => $group_id, ]; $result = db_process_sql_insert( 'tuser_task_scheduled', $task ); return ($result !== false); } }