You are here

civicrm_cron.module in CiviCRM Cron 6

Same filename and directory in other branches
  1. 8 civicrm_cron.module
  2. 7.2 civicrm_cron.module
  3. 7 civicrm_cron.module

Civicrm Cron Module Uses _cron for drupals cron job to trigger civicrm's new Consolidated Cron Jobs used by Visions Service Adventures website

File

civicrm_cron.module
View source
<?php

/* For drupal 6.x
 *
 *
 */

/**
 * @file
 * Civicrm Cron Module
 *   Uses _cron for drupals cron job to trigger civicrm's new Consolidated Cron Jobs 
 *   used by Visions Service Adventures website
 */

/*
//  Implementation of hook_init()
//
*/
function civicrm_cron_init() {
  if ((arg(0) == 'admin' || arg(0) == 'civicrm') && user_access('administer CiviCRM')) {
    if (!($mail_process_user = user_load(array(
      'name' => variable_get('civicrm_cron_username', NULL),
    )))) {
      return;
    }
    if ($mail_process_user->status) {
      drupal_set_message("CiviCRM Cron Module Problem!! The mail user you have set up, \n     <a href = 'user/{$mail_process_user->uid}'>{$mail_process_user->name}</a>, is set as an <em>active</em> user. \nThe password for this user is stored in the clear in the database.  <p>\nTo greatly improve security, you should make this user blocked: " . l('Edit this user', 'user/' . $mail_process_user->uid . '/edit'), error);
    }
  }

  // end "if an administrator on an admin or civi page" ...
  return;
}

/*
//  Implementation of hook_cron()
//
*/
function civicrm_cron_cron() {
  $cron_values['name'] = variable_get('civicrm_cron_username', NULL);
  $cron_values['pass'] = variable_get('civicrm_cron_password', NULL);
  $cron_values['key'] = variable_get('civicrm_cron_sitekey', CIVICRM_SITE_KEY);
  $url = civicrm_cron_get_url($cron_values);

  //  if ( !$civi_cron_user || !$civi_cron_pass || !$civi_cron_key) return;

  //print_r($url);
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $output = curl_exec($ch);
  curl_close($ch);
}

/*
//  Implementation of hook_menu
//
//
*/
function civicrm_cron_menu() {
  $items = array();
  $items['admin/settings/civicrm_cron'] = array(
    'title' => "CiviCRM Cron Configuration",
    'description' => 'Configure how drupal cron will trigger CiviCRM Consolidated Cron Jobs',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'civicrm_cron_admin_settings',
    ),
    'access arguments' => array(
      'access administration pages',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Builds the civicrm_cron admininstration settings form.
 */
function civicrm_cron_admin_settings($form, &$form_state) {
  $form = array();
  if (!civicrm_initialize()) {
    drupal_set_message(t('Failed to initialize CiviCRM'));
    return;
  }
  $cron_values['name'] = variable_get('civicrm_cron_username', NULL);
  $cron_values['pass'] = variable_get('civicrm_cron_password', NULL);
  $cron_values['key'] = variable_get('civicrm_cron_sitekey', CIVICRM_SITE_KEY);
  $url = civicrm_cron_get_url($cron_values);
  if ($url) {
    $result = drupal_http_request($url);

    //look for the CiviCRM error in response... successful processing returns nothing
    if ($result->data) {
      drupal_set_message('Attempted to use the following URL to process CiviCRM\'s cron: ' . $url . ' <br /> CiviCRM Error: ' . $result->data, 'warning');
    }
    else {
      drupal_set_message(t('CiviCRM Cron Successfully Run'));
    }
  }
  $form['instructions'] = array(
    '#value' => '<div>' . t('Use this page to configure authentication settings for Drupal cron to automatically trigger CiviCRM\'s Consolidated Cron Jobs: ' . $base_url) . '</div>',
  );
  $form['civicrm_cron_username'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#description' => t('Name the drupal user under whose name the CiviCRM cronjobs should operate. <a href ="/admin/user/create" target ="_blank" >create new user</a>?'),
    '#default_value' => $cron_values['name'],
    '#size' => '20',
    '#maxlength' => '78',
  );
  $form['civicrm_cron_password'] = array(
    '#type' => 'textfield',
    '#title' => t('Cronjob Users Password'),
    '#description' => t('Enter the password for this drupal user account'),
    '#default_value' => $cron_values['pass'],
    '#size' => '20',
    '#maxlength' => '78',
  );
  $form['civicrm_cron_sitekey'] = array(
    '#type' => 'textfield',
    '#title' => t('Site Key'),
    '#description' => t('The site key can be found by looking in civicrm.settings.php, usually found in sites/default/civicrm.settings.php'),
    '#default_value' => $cron_values['key'],
  );
  return system_settings_form($form);
}
function civicrm_cron_get_url($cron_values) {
  if ($cron_values['name'] && $cron_values['pass'] && $cron_values['key']) {
    global $base_url;
    $civicrm_path = drupal_get_path('module', 'civicrm');

    //remove the last occurrence of 'drupal' from path
    $pos = strrpos($civicrm_path, 'drupal');
    if ($pos !== false) {
      $civicrm_path = substr_replace($civicrm_path, '', $pos, strlen($civicrm_path));
    }
    $url = $civicrm_path . 'bin/cron.php';
    return $base_url . url($url, array(
      'query' => $cron_values,
    ));
  }
  return FALSE;
}

Functions

Namesort descending Description
civicrm_cron_admin_settings Builds the civicrm_cron admininstration settings form.
civicrm_cron_cron
civicrm_cron_get_url
civicrm_cron_init
civicrm_cron_menu