Пример #1
0
 /**
  * Initialise some settings
  */
 public function runCron()
 {
     // Buffer all output to prevent conflicts with external software
     ob_start();
     // Start the clock
     $starttime = time();
     $db = JFactory::getDbo();
     // First check if we deal with a valid user
     if ($this->Login()) {
         // Set some global values
         $jinput = JFactory::getApplication()->input;
         $jfilter = new JFilterInput();
         // Get the parameters
         require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/settings.php';
         $settings = new CsviSettings();
         // Check if we are running cron mode and set some necessary variables
         $_SERVER['SERVER_ADDR'] = $_SERVER['HTTP_HOST'] = $settings->get('site.hostname');
         $_SERVER['SCRIPT_NAME'] = '/index.php';
         $_SERVER['REQUEST_URI'] = '/';
         $_SERVER['PHP_SELF'] = '/index.php';
         // Get the task to do
         if (isset($this->_variables['task'])) {
             $task = $jfilter->clean($this->_variables['task']);
         } else {
             $task = '';
         }
         // Perform the requested task
         switch ($task) {
             case 'maintenance':
                 $jinput->set('task', 'maintenance.' . $this->_variables['operation']);
                 // Fire CSVI VirtueMart
                 $this->ExecuteJob();
                 break;
             default:
                 // Second check if any template is set to process
                 if (array_key_exists('template_id', $this->_variables)) {
                     $template_id = $jfilter->clean($this->_variables['template_id'], 'int');
                 } else {
                     $template_id = false;
                 }
                 if (array_key_exists('template_name', $this->_variables)) {
                     $template_name = $jfilter->clean($this->_variables['template_name']);
                 } else {
                     $template_name = false;
                 }
                 if ($template_id || $template_name) {
                     // There is a template_id or template name, get some details to streamline processing
                     $where = empty($template_id) ? 'name=' . $db->Quote($template_name) : 'id=' . $template_id;
                     // There is a template name, get some details to streamline processing
                     $q = "SELECT id AS template_id, name AS template_name, settings\r\n\t\t\t\t\t\t\tFROM #__csvi_template_settings\r\n\t\t\t\t\t\t\tWHERE " . $where;
                     $db->setQuery($q);
                     $row = $db->loadObject();
                     if (is_object($row)) {
                         echo JText::sprintf('COM_CSVI_PROCESSING_STARTED', date('jS F Y, g:i a')) . "\n";
                         echo JText::sprintf('COM_CSVI_TEMPLATE', $row->template_name) . "\n";
                         // Set the template ID
                         $jinput->set('select_template', $row->template_id);
                         $jinput->set('template_name', $row->template_name);
                         // Set the settings
                         if (array_key_exists('jform', $this->_variables)) {
                             $settings = CsviHelper::arrayExtend(json_decode($row->settings, true), $this->_variables['jform']);
                         } else {
                             $settings = json_decode($row->settings, true);
                         }
                         // Set some export settings
                         if ($settings['options']['action'] == 'export') {
                             // Export settings
                             $jinput->set('task', 'exportfile.process');
                             // Set export to
                             if ($settings['general']['exportto'] == 'todownload') {
                                 $settings['general']['exportto'] = 'tofile';
                             }
                         } else {
                             if ($settings['options']['action'] == 'import') {
                                 // Import settings
                                 $jinput->set('task', 'importfile.doimport');
                                 // Turn off preview
                                 $settings['general']['show_preview'] = 0;
                             }
                         }
                         // Post the settings
                         $jinput->set('jform', $settings, 'post');
                         // Fire CSVI
                         $this->ExecuteJob();
                     } else {
                         if ($template_name) {
                             echo JText::sprintf('COM_CSVI_NO_TEMPLATE_FOUND', $template_name) . "\n";
                         } else {
                             if ($template_id) {
                                 echo JText::sprintf('COM_CSVI_NO_TEMPLATE_FOUND', $template_id) . "\n";
                             }
                         }
                     }
                 } else {
                     echo JText::_('COM_CSVI_NO_TEMPLATE_SPECIFIED') . "\n";
                 }
                 break;
         }
     } else {
         $error = JError::getError();
         echo $error->message . "\n";
     }
     echo sprintf(JText::_('COM_CSVI_PROCESSING_FINISHED'), date('jS F Y, g:i a')) . "\n";
     $duration = time() - $starttime;
     if ($duration < 60) {
         echo sprintf(JText::_('COM_CSVI_PROCESSING_SECONDS'), $duration) . "\n";
     } else {
         echo sprintf(JText::_('COM_CSVI_PROCESSING_MINUTES'), number_format($duration / 60, 2)) . "\n";
     }
     // Done, lets log the user out
     $this->UserLogout();
     // Display any generated messages
     $messages = ob_get_contents();
     @ob_end_clean();
     echo $messages;
 }