/**
  * Runs the selected task
  *
  * @access	public
  * @return	void
  */
 public function taskManagerRunTask()
 {
     /* INIT */
     $task_id = intval($this->request['task_id']);
     /* Check ID */
     if (!$task_id) {
         $this->registry->output->global_message = $this->lang->words['t_noid'];
         $this->taskManagerOverview();
         return;
     }
     /* Query the task */
     $this_task = $this->DB->buildAndFetch(array('select' => '*', 'from' => 'task_manager', 'where' => 'task_id=' . $task_id));
     /* NO task found */
     if (!$this_task['task_id']) {
         $this->registry->output->global_message = $this->lang->words['t_notask'];
         $this->taskManagerOverview();
         return;
     }
     /* Disabled? */
     if (!$this_task['task_enabled']) {
         $this->registry->output->global_message = $this->lang->words['t_disabled'];
         $this->taskManagerOverview();
         return;
     }
     /* Locked */
     if ($this_task['task_locked'] > 0 && !IN_DEV) {
         $this->registry->output->global_message = sprintf($this->lang->words['t_locked'], gmstrftime('%c', $this_task['task_locked']));
         $this->taskManagerOverview();
         return;
     }
     /* Get the next rund ate and then update the task */
     $newdate = $this->func_taskmanager->generateNextRun($this_task);
     $this->DB->update('task_manager', array('task_next_run' => $newdate, 'task_locked' => time()), "task_id=" . $task_id);
     $this->func_taskmanager->saveNextRunStamp();
     /* Run the task file */
     if (file_exists(IPSLib::getAppDir($this_task['task_application']) . '/tasks/' . $this_task['task_file'])) {
         require_once IPSLib::getAppDir($this_task['task_application']) . '/tasks/' . $this_task['task_file'];
         $myobj = new task_item($this->registry, $this->func_taskmanager, $this_task);
         $myobj->runTask();
         /* Bounce */
         $this->registry->output->global_message = $this->lang->words['t_ran'];
         $this->taskManagerOverview();
         return;
     } else {
         /* Bounce */
         $this->registry->output->global_message = sprintf($this->lang->words['t_nolocate'], IPSLib::getAppDir($this_task['task_application']), $this_task['task_file']);
         $this->taskManagerOverview();
         return;
     }
 }
 /**
  * Run a task
  *
  * @access	public
  * @return	void
  */
 public function runTask()
 {
     if (ipsRegistry::$request['ck'] and ipsRegistry::$request['ck']) {
         $this->type = 'cron';
         $this->cron_key = substr(trim(stripslashes(IPSText::alphanumericalClean(ipsRegistry::$request['ck']))), 0, 32);
     }
     if ($this->type == 'internal') {
         //-----------------------------------------
         // Loaded by our image...
         // ... get next job
         //-----------------------------------------
         $this_task = $this->DB->buildAndFetch(array('select' => '*', 'from' => 'task_manager', 'where' => 'task_enabled = 1 AND task_next_run <= ' . $this->time_now, 'order' => 'task_next_run ASC', 'limit' => array(0, 1)));
     } else {
         //-----------------------------------------
         // Cron.. load from cron key
         //-----------------------------------------
         $this_task = $this->DB->buildAndFetch(array('select' => '*', 'from' => 'task_manager', 'where' => "task_cronkey='" . $this->cron_key . "'"));
     }
     if ($this_task['task_id']) {
         //-----------------------------------------
         // Locked?
         //-----------------------------------------
         if ($this_task['task_locked'] > 0) {
             # Yes - now, how long has it been locked for?
             # If longer than 30 mins, unlock as something
             # has gone wrong.
             if ($this_task['task_locked'] < time() - 1800) {
                 $newdate = $this->generateNextRun($this_task);
                 $this->DB->update('task_manager', array('task_next_run' => $newdate, 'task_locked' => 0), "task_id=" . $this_task['task_id']);
                 $this->saveNextRunStamp();
             }
             # Cancel and return
             return;
         }
         //-----------------------------------------
         // Got it, now update row, lock and run..
         //-----------------------------------------
         $newdate = $this->generateNextRun($this_task);
         $this->DB->update('task_manager', array('task_next_run' => $newdate, 'task_locked' => time()), "task_id=" . $this_task['task_id']);
         $this->saveNextRunStamp();
         if (file_exists(IPSLib::getAppDir($this_task['task_application']) . '/tasks/' . $this_task['task_file'])) {
             require_once IPSLib::getAppDir($this_task['task_application']) . '/tasks/' . $this_task['task_file'];
             $myobj = new task_item($this->registry, $this, $this_task);
             $myobj->runTask();
             //-----------------------------------------
             // Any shutdown queries
             //-----------------------------------------
             $this->DB->return_die = 0;
             if (count($this->DB->obj['shutdown_queries'])) {
                 foreach ($this->DB->obj['shutdown_queries'] as $q) {
                     $this->DB->query($q);
                 }
             }
             $this->DB->return_die = 1;
             $this->DB->obj['shutdown_queries'] = array();
         }
     }
 }