b2evolution PHP Cross Reference Blogging Systems

Source: /inc/cron/cronjobs.ctrl.php - 380 lines - 12566 bytes - Text - Print

Description: This file implements the UI controller for Cron table.

   1  <?php
   2  /**
   3   * This file implements the UI controller for Cron table.
   4   *
   5   * This file is part of the evoCore framework - {@link http://evocore.net/}
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   9   *
  10   * {@internal License choice
  11   * - If you have received this file as part of a package, please find the license.txt file in
  12   *   the same folder or the closest folder above for complete license terms.
  13   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  14   *   then you must choose one of the following licenses before using the file:
  15   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  16   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  17   * }}
  18   *
  19   * {@internal Open Source relicensing agreement:
  20   * }}
  21   *
  22   * @package admin
  23   *
  24   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  25   * @author fplanque: Francois PLANQUE.
  26   *
  27   * @version $Id: cronjobs.ctrl.php 6136 2014-03-08 07:59:48Z manuel $
  28   */
  29  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  30  
  31  load_funcs( 'cron/_cron.funcs.php' );
  32  
  33  // Check minimum permission:
  34  $current_User->check_perm( 'options', 'view', true );
  35  
  36  $AdminUI->set_path( 'options', 'cron' );
  37  
  38  param( 'action', 'string', 'list' );
  39  
  40  // We want to remember these params from page to page:
  41  param( 'ctst_pending', 'integer', 0, true );
  42  param( 'ctst_started', 'integer', 0, true );
  43  param( 'ctst_timeout', 'integer', 0, true );
  44  param( 'ctst_error', 'integer', 0, true );
  45  param( 'ctst_finished', 'integer', 0, true );
  46  param( 'results_crontab_order', 'string', '-D', true );
  47  param( 'results_crontab_page', 'integer', 1, true );
  48  
  49  
  50  if( param( 'ctsk_ID', 'integer', '', true) )
  51  {// Load cronjob from cache:
  52      $CronjobCache = & get_CronjobCache();
  53      if( ( $edited_Cronjob = & $CronjobCache->get_by_ID( $ctsk_ID, false ) ) === false )
  54      {
  55          unset( $edited_Cronjob );
  56          forget_param( 'ctsk_ID' );
  57          $Messages->add( sprintf( T_('Requested &laquo;%s&raquo; object does not exist any longer.'), T_('Scheduled job') ), 'error' );
  58          $action = 'list';
  59      }
  60  }
  61  
  62  // fp> The if  below was the point where THE LINE WAS CROSSED!
  63  // This is bloated here. This has to go into the action handling block (and maybe a function)
  64  // THIS IS NO LONGER CONTROLLER INITIALIZATION. THIS IS ACTION EXECUTION!
  65  // dh> ok. Moved the other param inits above. Ok? I don't think it should be an extra function..
  66  
  67  // Init names and params for "static" available jobs and ask Plugins about their jobs:
  68  if( in_array( $action, array( 'new', 'create', 'edit', 'update', 'copy' ) ) )
  69  {
  70      // NOTE: keys starting with "plugin_" are reserved for jobs provided by Plugins
  71      $cron_job_names = array(
  72              'test' => T_('Basic test job'),
  73              'error' => T_('Error test job'),
  74              'anstispam_poll' => T_('Poll the antispam blacklist'),
  75              'prune_hits_sessions' => T_('Prune old hits & sessions (includes OPTIMIZE)'),
  76              'prune_page_cache' => T_('Prune old files from page cache'),
  77              'post_by_email' => T_('Create posts by email'),
  78              'process_hitlog' => T_('Extract info from hit log'),
  79              'unread_message_reminder' => T_( 'Send reminders about unread messages' ),
  80              'activate_account_reminder' => T_( 'Send reminders about non-activated accounts' ),
  81              'comment_moderation_reminder' => T_( 'Send reminders about comments awaiting moderation' ),
  82              'return_path' => T_('Process the return path inbox'),
  83              'light_db_maintenance' => T_('Light DB maintenance (ANALYZE)'),
  84              'heavy_db_maintenance' => T_('Heavy DB maintenance (CHECK & OPTIMIZE)'),
  85              // post notifications, not user schedulable
  86              // comment notifications, not user schedulable
  87          );
  88      $cron_job_params = array(
  89              'test' => array(
  90                  'ctrl' => 'cron/jobs/_test.job.php',
  91                  'params' => NULL ),
  92              'error' => array(
  93                  'ctrl' => 'cron/jobs/_error_test.job.php',
  94                  'params' => NULL ),
  95              'anstispam_poll' => array(
  96                  'ctrl' => 'cron/jobs/_antispam_poll.job.php',
  97                  'params' => NULL ),
  98              'prune_hits_sessions' => array(
  99                  'ctrl' => 'cron/jobs/_prune_hits_sessions.job.php',
 100                  'params' => NULL ),
 101              'prune_page_cache' => array(
 102                  'ctrl' => 'cron/jobs/_prune_page_cache.job.php',
 103                  'params' => NULL ),
 104              'post_by_email' => array(
 105                  'ctrl' => 'cron/jobs/_post_by_email.job.php',
 106                  'params' => NULL ),
 107              'process_hitlog' => array(
 108                  'ctrl' => 'cron/jobs/_process_hitlog.job.php',
 109                  'params' => NULL ),
 110              'unread_message_reminder' => array(
 111                  'ctrl' => 'cron/jobs/_unread_message_reminder.job.php',
 112                  'params' => NULL ),
 113              'activate_account_reminder' => array(
 114                  'ctrl' => 'cron/jobs/_activate_account_reminder.job.php',
 115                  'params' => NULL ),
 116              'comment_moderation_reminder' => array(
 117                  'ctrl' => 'cron/jobs/_comment_moderation_reminder.job.php',
 118                  'params' => NULL ),
 119              'return_path' => array(
 120                  'ctrl' => 'cron/jobs/_decode_returned_emails.job.php',
 121                  'params' => NULL ),
 122              'light_db_maintenance' => array(
 123                  'ctrl' => 'cron/jobs/_light_db_maintenance.job.php',
 124                  'params' => NULL ),
 125              'heavy_db_maintenance' => array(
 126                  'ctrl' => 'cron/jobs/_heavy_db_maintenance.job.php',
 127                  'params' => NULL ),
 128              // post notifications, not user schedulable
 129          );
 130  
 131      // Get additional jobs from Plugins:
 132      foreach( $Plugins->trigger_collect( 'GetCronJobs' ) as $plug_ID => $jobs )
 133      {
 134          if( ! is_array($jobs) )
 135          {
 136              $Debuglog->add( sprintf('GetCronJobs() for plugin #%d did not return array. Ignoring its jobs.', $plug_ID), array('plugins', 'error') );
 137              continue;
 138          }
 139          foreach( $jobs as $job )
 140          {
 141              // Validate params from plugin:
 142              if( ! isset($job['params']) )
 143              {
 144                  $job['params'] = NULL;
 145              }
 146              if( ! is_array($job) || ! isset($job['ctrl'], $job['name']) )
 147              {
 148                  $Debuglog->add( sprintf('GetCronJobs() for plugin #%d did return invalid job. Ignoring.', $plug_ID), array('plugins', 'error') );
 149                  continue;
 150              }
 151              if( isset($job['params']) && ! is_array($job['params']) )
 152              {
 153                  $Debuglog->add( sprintf('GetCronJobs() for plugin #%d did return invalid job params (not an array). Ignoring.', $plug_ID), array('plugins', 'error') );
 154                  continue;
 155              }
 156              $ctrl_id = 'plugin_'.$plug_ID.'_'.$job['ctrl'];
 157  
 158              $cron_job_names[$ctrl_id] = $job['name'];
 159              $cron_job_params[$ctrl_id] = array(
 160                      'ctrl' => $ctrl_id,
 161                      'params' => $job['params'],
 162                  );
 163          }
 164      }
 165  }
 166  
 167  
 168  switch( $action )
 169  {
 170      case 'new':
 171          // Check that we have permission to edit options:
 172          $current_User->check_perm( 'options', 'edit', true, NULL );
 173  
 174          load_class( 'cron/model/_cronjob.class.php', 'Cronjob' );
 175          $edited_Cronjob = new Cronjob();
 176          break;
 177  
 178      case 'edit':
 179      case 'copy':
 180          // Check that we have permission to edit options:
 181          $current_User->check_perm( 'options', 'edit', true, NULL );
 182  
 183          if( ( $action == 'edit' && $edited_Cronjob->get_status() != 'pending' ) ||
 184              ( $action == 'copy' && $edited_Cronjob->get_status() != 'error' ) )
 185          {    // Don't edit cron jobs with not "pending" status
 186              header_redirect( '?ctrl=crontab', 303 ); // Will EXIT
 187              // We have EXITed already at this point!!
 188          }
 189  
 190          if( $action == 'copy' )
 191          {    // Reset time to now for copied cron job
 192              global $localtimenow;
 193              $edited_Cronjob->start_timestamp = $localtimenow;
 194          }
 195  
 196          break;
 197  
 198      case 'create':
 199          // Check that this action request is not a CSRF hacked request:
 200          $Session->assert_received_crumb( 'crontask' );
 201  
 202          // Check that we have permission to edit options:
 203          $current_User->check_perm( 'options', 'edit', true, NULL );
 204  
 205          if( !empty( $edited_Cronjob ) )
 206          {    // It is a copy action, we should save the fields "controller" & "params"
 207              $ctsk_controller = $edited_Cronjob->get( 'controller' );
 208              $ctsk_params = $edited_Cronjob->get( 'params' );
 209          }
 210  
 211          // CREATE OBJECT:
 212          load_class( '/cron/model/_cronjob.class.php', 'Cronjob' );
 213          $edited_Cronjob = new Cronjob();
 214  
 215          if( $edited_Cronjob->load_from_Request( $cron_job_names, $cron_job_params ) )
 216          {    // We could load data from form without errors:
 217  
 218              if( !empty( $ctsk_controller ) )
 219              {    // Save controller field from copied object
 220                  $edited_Cronjob->set( 'controller', $ctsk_controller );
 221              }
 222  
 223              if( !empty( $ctsk_params ) )
 224              {    // Save params field from copied object
 225                  $edited_Cronjob->set( 'params', $ctsk_params );
 226              }
 227  
 228              // Save to DB:
 229              $edited_Cronjob->dbinsert();
 230  
 231              $Messages->add( T_('New job has been scheduled.'), 'success' );
 232  
 233              // Redirect so that a reload doesn't write to the DB twice:
 234              header_redirect( '?ctrl=crontab', 303 ); // Will EXIT
 235              // We have EXITed already at this point!!
 236          }
 237          break;
 238  
 239      case 'update':
 240          // Check that this action request is not a CSRF hacked request:
 241          $Session->assert_received_crumb( 'crontask' );
 242  
 243          // Check that we have permission to edit options:
 244          $current_User->check_perm( 'options', 'edit', true, NULL );
 245  
 246          if( $edited_Cronjob->load_from_Request( $cron_job_names, $cron_job_params ) )
 247          {    // We could load data from form without errors:
 248  
 249              if( $edited_Cronjob->dbupdate() )
 250              {    // The job was updated successfully
 251                  $Messages->add( T_('The scheduled job has been updated.'), 'success' );
 252              }
 253              else
 254              {    // Errors on updating, probably this job has not "pending" status
 255                  $Messages->add( T_('This scheduled job can not be updated.'), 'error' );
 256              }
 257  
 258              // Redirect so that a reload doesn't write to the DB twice:
 259              header_redirect( '?ctrl=crontab', 303 ); // Will EXIT
 260              // We have EXITed already at this point!!
 261          }
 262          break;
 263  
 264  
 265      case 'delete':
 266          // Check that this action request is not a CSRF hacked request:
 267          $Session->assert_received_crumb( 'crontask' );
 268  
 269          // Make sure we got an ord_ID:
 270          param( 'ctsk_ID', 'integer', true );
 271  
 272          // Check that we have permission to edit options:
 273          $current_User->check_perm( 'options', 'edit', true, NULL );
 274  
 275          // TODO: prevent deletion of running tasks.
 276          $DB->begin();
 277  
 278          $tsk_status =    $DB->get_var(
 279              'SELECT clog_status
 280                   FROM T_cron__log
 281                  WHERE clog_ctsk_ID= '.$ctsk_ID,
 282              0, 0, 'Check that task is not running' );
 283  
 284          if( $tsk_status == 'started' )
 285          {
 286              $DB->rollback();
 287  
 288              $Messages->add(  sprintf( T_('Job #%d is currently running. It cannot be deleted.'), $ctsk_ID ), 'error' );
 289          }
 290          else
 291          {
 292              // Delete task:
 293              $DB->query( 'DELETE FROM T_cron__task
 294                                          WHERE ctsk_ID = '.$ctsk_ID );
 295  
 296              // Delete log (if exists):
 297              $DB->query( 'DELETE FROM T_cron__log
 298                                          WHERE clog_ctsk_ID = '.$ctsk_ID );
 299  
 300              $DB->commit();
 301  
 302              $Messages->add(  sprintf( T_('Scheduled job #%d deleted.'), $ctsk_ID ), 'success' );
 303          }
 304  
 305          //forget_param( 'ctsk_ID' );
 306          //$action = 'list';
 307          // Redirect so that a reload doesn't write to the DB twice:
 308          header_redirect( regenerate_url( 'action,ctsk_ID', '', '', '&' ), 303 ); // Will EXIT
 309          // We have EXITed already at this point!!
 310          break;
 311  
 312  
 313      case 'view':
 314          $cjob_ID = param( 'cjob_ID', 'integer', true );
 315  
 316          $sql =  'SELECT *
 317                               FROM T_cron__task LEFT JOIN T_cron__log ON ctsk_ID = clog_ctsk_ID
 318                              WHERE ctsk_ID = '.$cjob_ID;
 319          $cjob_row = $DB->get_row( $sql, OBJECT, 0, 'Get cron job and log' );
 320          if( empty( $cjob_row ) )
 321          {
 322              $Messages->add( sprintf( T_('Job #%d does not exist any longer.'), $cjob_ID ), 'error' );
 323              $action = 'list';
 324          }
 325          break;
 326  
 327      case 'list':
 328          // Detect timed out tasks:
 329          detect_timeout_cron_jobs();
 330  
 331          break;
 332  }
 333  
 334  
 335  $AdminUI->breadcrumbpath_init( false );  // fp> I'm playing with the idea of keeping the current blog in the path here...
 336  $AdminUI->breadcrumbpath_add( T_('System'), '?ctrl=system' );
 337  $AdminUI->breadcrumbpath_add( T_('Scheduler'), '?ctrl=crontab' );
 338  
 339  if( in_array( $action, array( 'new', 'create', 'edit', 'update', 'copy' ) ) )
 340  { // load date picker style for cronjob.form.php
 341      require_css( 'ui.datepicker.css' );
 342  }
 343  
 344  // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)
 345  $AdminUI->disp_html_head();
 346  
 347  // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)
 348  $AdminUI->disp_body_top();
 349  
 350  // Begin payload block:
 351  $AdminUI->disp_payload_begin();
 352  
 353  switch( $action )
 354  {
 355      case 'new':
 356      case 'create':
 357      case 'edit':
 358      case 'update':
 359      case 'copy':
 360          // Display VIEW:
 361          $AdminUI->disp_view( 'cron/views/_cronjob.form.php' );
 362          break;
 363  
 364      case 'view':
 365          // Display VIEW:
 366          $AdminUI->disp_view( 'cron/views/_cronjob.view.php' ); // uses $cjob_row
 367          break;
 368  
 369      default:
 370          // Display VIEW:
 371          $AdminUI->disp_view( 'cron/views/_cronjob_list.view.php' );
 372  }
 373  
 374  // End payload block:
 375  $AdminUI->disp_payload_end();
 376  
 377  // Display body bottom, debug info and close </html>:
 378  $AdminUI->disp_global_footer();
 379  
 380  ?>

title

Description

title

Description

title

Description

title

title

Body