function system_cron

7.x system.module system_cron()
6.x system.module system_cron()
5.x system.module system_cron()

Implementation of hook_cron

We will perform operations necessary for keep FlightPath's tables in shape.

File

modules/system/system.module, line 2931

Code

function system_cron() {

  // Should we trim the watchdog table of extra entries?  Only once every so often, not every cron run.
  $last_run = intval(variable_get("system_last_run_trim_watchdog", 0));
  $check_against = strtotime("NOW - 7 DAYS"); // don't run any more often than once every 7 days
  $c = 0;
  if ($check_against > $last_run) {

    // Should we "trim" the watchdog table of old entries?
    $max_age_days = variable_get("max_watchdog_age", "1095");
    if ($max_age_days != "never" && ($max_age_days * 1) > 0) {

      // Okay, let's trim the table.
      $max_timestamp = strtotime("$max_age_days DAYS AGO");

      $result = db_query("DELETE FROM watchdog WHERE `timestamp` < ? ", $max_timestamp);
      $rows = db_affected_rows($result);
      if ($rows > 0) {
        watchdog("system", t("@rows old rows (older than @max days) trimmed from watchdog table on system cron run."), array("@rows" => $rows, "@max" => $max_age_days), WATCHDOG_DEBUG);
      }

    }

    // Should we trim the watchdog table of DEBUG records?
    $max_age_days = intval(variable_get("max_watchdog_debug_age", "30"));
    if ($max_age_days != "never" && ($max_age_days) > 0) {

      // Okay, let's trim the table.
      $max_timestamp = strtotime("$max_age_days DAYS AGO");

      $result = db_query("DELETE FROM watchdog WHERE `timestamp` < ? AND severity = ? ", $max_timestamp, WATCHDOG_DEBUG);
      $rows = db_affected_rows($result);
      if ($rows > 0) {
        watchdog("system", t("@rows old 'debug' rows (older than @max days) trimmed from watchdog table on system cron run."), array("@rows" => $rows, "@max" => $max_age_days), WATCHDOG_DEBUG);
      }

    }


    // Should we trim the watchdog table of ERROR records?
    $max_age_days = intval(variable_get("max_watchdog_error_age", "30"));
    if ($max_age_days != "never" && ($max_age_days) > 0) {

      // Okay, let's trim the table.
      $max_timestamp = strtotime("$max_age_days DAYS AGO");

      $result = db_query("DELETE FROM watchdog WHERE `timestamp` < ? AND severity = ? ", $max_timestamp, WATCHDOG_ERROR);
      $rows = db_affected_rows($result);
      if ($rows > 0) {
        watchdog("system", t("@rows old 'error' rows (older than @max days) trimmed from watchdog table on system cron run."), array("@rows" => $rows, "@max" => $max_age_days), WATCHDOG_DEBUG);
      }

    }



    variable_set("system_last_run_trim_watchdog", time());

  } // check against > last_run, so we should do it.





  // Should we delete from user_attributes any mda_validation_codes which are older than X hours?
  $max_age_hours = 1;
  // Okay, let's trim the table.
  $max_timestamp = strtotime("NOW - $max_age_hours HOURS");

  $result = db_query("DELETE FROM user_attributes WHERE `name` = 'mfa_validation_code' AND `updated` < ? ", $max_timestamp);
  $rows = db_affected_rows($result);
  if ($rows > 0) {
    watchdog("system", t("@rows old 'mfa_validation_code' rows (older than @max hours) trimmed from user_attributes table on system cron run."), array("@rows" => $rows, "@max" => $max_age_hours), WATCHDOG_DEBUG);
  }


  // Should we email someone of php errors?
  $tomail = trim(variable_get("notify_php_error_email_address", ""));
  if ($tomail != "") {

    // We should add this to our "to email" file, which will get emailed to this mail address later on.
    $error_files_dir = fp_get_files_path() . "/private";

    // Make sure our log file exists
    if (file_exists($error_files_dir . '/fp_php_errors_to_email_log.txt')) {

      // is there anything for us to email?
      $emsg = file_get_contents($error_files_dir . '/fp_php_errors_to_email_log.txt');
      if ($emsg && fp_trim($emsg) != "") {
        $hostname = php_uname('n');
        fp_mail($tomail, "PHP Error(s) in FlightPath ($hostname)", $emsg);
        // Make the file blank.
        file_put_contents($error_files_dir . '/fp_php_errors_to_email_log.txt', '');
      }

    }
  }

  ////////////
  // Remove expired banned ips, if any exist.
  $ban_expire = variable_get('system_ban_ip_expire', '2 WEEKS');
  if ($ban_expire != 'never') {
    $ban_ip_file_path = fp_get_files_path() . '/private/banned_ips.txt';
    if (file_exists($ban_ip_file_path)) {
      $max_timestamp = strtotime("NOW - $ban_expire");
      $res = db_query("SELECT * FROM banned_ips WHERE posted_ts < ?", array($max_timestamp));
      while ($cur = db_fetch_object($res)) {

        $ip = $cur->ip_address;
        // Erase this ip address from our ban file.
        $c = file_get_contents($ban_ip_file_path);
        $c = str_replace("$ip", '', $c);
        $c = str_replace("  ", '', $c);
        $c = str_replace(" \n", "", $c);
        $c = str_replace("\n\n", "", $c);
        $c = trim($c);

        // Write back into our file
        $respc = file_put_contents($ban_ip_file_path, $c);
        if ($respc === FALSE) {
          watchdog("system", "Unable to write to the ban ip file $ban_ip_file_path. Trying to remove $ip. File permission issue?", array(), WATCHDOG_ERROR);
        }

        // remove from table
        db_query("DELETE FROM banned_ips WHERE ip_address = ?", array($ip));

      } // while cur
    } // if file exists
  } // ban_expire != 'never'




}