You are here

function anonymous_publishing_cron in Anonymous Publishing 7

Implements hook_cron().

File

./anonymous_publishing.module, line 53
Hooks for the Anonymous Publishing parent module.

Code

function anonymous_publishing_cron() {
  $cl_audodelhours = -1;
  $cl_period = $pet_period = -1;
  if (module_exists('anonymous_publishing_cl')) {
    $cl_audodelhours = variable_get('anonymous_publishing_cl_autodelhours', -1);
    $cl_period = variable_get('anonymous_publishing_cl_period', -1);
  }
  if (module_exists('anonymous_publishing_pet')) {
    $pet_period = variable_get('anonymous_publishing_pet_period', -1);
  }

  // Audeodelete not verified content.
  if ($cl_audodelhours >= 0) {
    $limit = REQUEST_TIME - $cl_audodelhours * 3600;

    // Fetch all nodes that has not been verified older than limit.
    $sql = "SELECT a.apid, n.nid, created FROM {anonymous_publishing} a JOIN {node} n ON a.nid = n.nid WHERE a.verified = 0 AND n.created < :limit  AND a.cid = 0 ORDER BY a.nid ASC";
    $result = db_query($sql, array(
      ':limit' => $limit,
    ));
    $stats = variable_get('anonymous_publishing_cl_stats', array(
      'start' => REQUEST_TIME,
      'smart' => 0,
      'stupid' => 0,
    ));
    $bots = 0;
    while ($row = $result
      ->fetchAssoc()) {
      $bots++;
      db_delete('anonymous_publishing')
        ->condition('apid', $row['apid'])
        ->execute();
      node_delete($row['nid']);
    }

    // Fetch all comments that has not been verified older than limit.
    $sql = "SELECT a.apid, c.cid, c.created FROM {anonymous_publishing} a JOIN {comment} c ON a.cid = c.cid WHERE a.verified = 0 AND c.created < :limit  AND a.cid <> 0 ORDER BY a.cid ASC";
    $result = db_query($sql, array(
      ':limit' => $limit,
    ));
    while ($row = $result
      ->fetchAssoc()) {
      $bots++;
      db_delete('anonymous_publishing')
        ->condition('apid', $row['apid'])
        ->execute();
      comment_delete($row['cid']);
    }
    if ($bots) {
      $stats['smart'] += $bots;
      variable_set('anonymous_publishing_cl_stats', $stats);
    }
  }

  // Redact e-mail and IP address.
  if ($cl_period > -1) {
    $rows = db_query("SELECT apid, nid, cid  FROM {anonymous_publishing}")
      ->fetchAll(PDO::FETCH_ASSOC);
    foreach ($rows as $row) {
      if ($row['cid']) {
        $created = db_query("SELECT created FROM {comment} WHERE :cid = cid", array(
          ':cid' => $row['cid'],
        ))
          ->fetchField();
      }
      else {
        $created = db_query("SELECT created FROM {node} WHERE :nid = nid", array(
          ':nid' => $row['nid'],
        ))
          ->fetchField();
      }
      $age = REQUEST_TIME - $created;
      if ($age > $cl_period) {
        db_delete('anonymous_publishing')
          ->condition('apid', $row['apid'])
          ->condition('verified', 1)
          ->execute();
        db_update('anonymous_publishing')
          ->condition('apid', $row['apid'])
          ->fields(array(
          'ip' => '',
        ))
          ->execute();
      }
    }
  }

  // Redact e-mail and real name.
  if ($pet_period > -1) {
    $rows = db_query("SELECT rnid, nid, cid  FROM {anonymous_publishing_realname}")
      ->fetchAll(PDO::FETCH_ASSOC);
    foreach ($rows as $row) {
      if ($row['cid']) {
        $created = db_query("SELECT created FROM {comment} WHERE :cid = cid", array(
          ':cid' => $row['cid'],
        ))
          ->fetchField();
      }
      else {
        $created = db_query("SELECT created FROM {node} WHERE :nid = nid", array(
          ':nid' => $row['nid'],
        ))
          ->fetchField();
      }
      $age = REQUEST_TIME - $created;
      if ($age > $pet_period) {
        db_delete('anonymous_publishing_realname')
          ->condition('rnid', $row['rnid'])
          ->execute();
      }
    }
  }
}