You are here

counter.module in Counter 7

Same filename and directory in other branches
  1. 8 counter.module
  2. 5 counter.module
  3. 6.2 counter.module
  4. 6 counter.module

File

counter.module
View source
<?php

/**
 * @file
 * The counter module used for displaying Site Counter.
 */
require_once 'counter.lib.inc';

/**
 * Implements hook_help().
 */
function counter_help($section) {
  switch ($section) {
    case 'admin/help#Counter':
      $output = "The counter module used for displaying Site Counter.";
      return $output;
    case 'admin/modules#description':
      return 'The counter module used for displaying Site Counter';
  }
}

/**
 * Implements hook_permission().
 */
function counter_permission() {
  return array(
    'administer counter' => array(
      'title' => t('Administer Counter'),
    ),
  );
}

/**
 * Menu callback. Prints a listing of active nodes on the site.
 */
function counter_menu() {
  $items['admin/config/counter'] = array(
    'title' => 'Counter',
    'page callback' => 'system_admin_menu_block_page',
    // 'page arguments'   => array('counter_admin_settings'),
    'access arguments' => array(
      'administer counter',
    ),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items['admin/config/counter/basic'] = array(
    'title' => 'Basic Counter',
    'description' => 'Show Site Counter, Client IP, and Unique Visitor.',
    'access arguments' => array(
      'administer counter',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'counter_admin_settings',
    ),
    'weight' => -10,
    'file' => 'counter.settings.inc',
  );
  $items['admin/config/counter/advanced'] = array(
    'title' => 'Advanced Counter',
    'access arguments' => array(
      'administer counter',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'counter_settings_advanced',
    ),
    'file' => 'counter.settings.inc',
  );
  $items['admin/config/counter/initial'] = array(
    'title' => 'Initial Value Counter',
    'access arguments' => array(
      'administer counter',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'counter_settings_initial',
    ),
    'file' => 'counter.settings.inc',
  );
  return $items;
}

/**
 * Implements hook_block_info().
 */
function counter_block_info() {
  $blocks = array();
  $blocks['counter'] = array(
    'info' => t('Counter'),
  );
  return $blocks;
}

/**
 * Implements hook_block().
 */
function counter_block_view($delta = '') {
  global $user, $node, $db_url;
  $counter_show_site_counter = variable_get('counter_show_site_counter', 1);
  $counter_show_unique_visitor = variable_get('counter_show_unique_visitor', 1);
  $counter_registered_user = variable_get('counter_registered_user', 1);
  $counter_unregistered_user = variable_get('counter_unregistered_user', 1);
  $counter_blocked_user = variable_get('counter_blocked_user', 1);
  $counter_published_node = variable_get('counter_published_node', 1);
  $counter_unpublished_node = variable_get('counter_unpublished_node', 1);
  $counter_show_server_ip = variable_get('counter_show_server_ip', 1);
  $counter_show_ip = variable_get('counter_show_ip', 1);
  $counter_show_counter_since = variable_get('counter_show_counter_since', 1);
  $counter_skip_admin = variable_get('counter_skip_admin', 0);
  $counter_refresh_delay = variable_get('counter_refresh_delay', 10);
  $counter_insert_delay = variable_get('counter_insert_delay', 1);
  $counter_initial_counter = variable_get('counter_initial_counter', 0);
  $counter_initial_unique_visitor = variable_get('counter_initial_unique_visitor', 0);
  $counter_initial_since = variable_get('counter_initial_since', 0);
  $counter_statistic_today = variable_get('counter_statistic_today', 1);
  $counter_statistic_week = variable_get('counter_statistic_week', 1);
  $counter_statistic_month = variable_get('counter_statistic_month', 1);
  $counter_statistic_year = variable_get('counter_statistic_year', 1);

  // Block display.
  $block['subject'] = 'Counter';
  $ip = ip_address();
  $counter_svr_ip = $_SERVER['SERVER_ADDR'];
  $created = time();
  $url = check_plain(request_uri());

  // counter_insert_delay
  $db_types = db_driver();
  switch ($db_types) {
    case 'mssql':
      $sql = " SELECT TOP 1 created FROM {counter} WHERE created<>0 ORDER BY created DESC";
      break;
    case 'oracle':
      $sql = " SELECT created FROM {counter} WHERE ROWNUM=1 AND created<>0 ORDER BY created DESC";
      break;

    // mysql, mysqli, pgsql.
    default:
      $sql = " SELECT created FROM {counter} WHERE created<>0 ORDER BY created DESC LIMIT 1";
  }
  $counter_lastdate = db_query($sql)
    ->fetchField();

  // Check if permited to insert data.
  $interval = time() - $counter_lastdate;
  $data_insert = $interval >= $counter_insert_delay ? 1 : 0;
  $data_update = $interval >= $counter_refresh_delay ? 1 : 0;

  // uid, nid, type, browser name, browser version, platform.
  $node = menu_get_object();
  $nid = 0;
  $type = '';
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = $node->nid;
    $type = $node->type;
  }
  $browser = counter_get_browser();
  $browser_name = $browser['browser_name'];
  $browser_version = $browser['browser_version'];
  $platform = $browser['platform'];
  $sql = " INSERT INTO {counter} (ip,created,url, uid, nid, type, browser_name, browser_version, platform)" . " VALUES (:ip, :created, :url, :uid, :nid, :type, :browser_name, :browser_version, :platform)";
  $counter_exec = FALSE;
  if ($data_insert && $user->uid != 1) {
    $counter_exec = TRUE;
  }
  else {
    if ($data_insert && $user->uid == 1 && !$counter_skip_admin) {
      $counter_exec = TRUE;
    }
  }
  if ($counter_exec) {
    $results = db_query($sql, array(
      ':ip' => $ip,
      ':created' => $created,
      ':url' => $url,
      ':uid' => $user->uid,
      ':nid' => $nid,
      ':type' => $type,
      ':browser_name' => $browser_name,
      ':browser_version' => $browser_version,
      ':platform' => $platform,
    ));
  }
  else {
    return;
  }
  $sql_site_counter = "SELECT counter_value FROM {counter_data} WHERE counter_name='site_counter'";
  $site_counter = db_query($sql_site_counter)
    ->fetchField();
  $new_site_counter = $site_counter + 1;
  $sql = " UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='site_counter'";
  $results = db_query($sql, array(
    ':counter_value' => $new_site_counter,
  ));

  // Read counter_data.
  $sql = " SELECT * FROM {counter_data} ORDER BY counter_name";
  $results = db_query($sql);
  $i = 0;
  foreach ($results as $data) {
    $i++;
    $counter_name[$i] = $data->counter_name;
    $counter_value[$i] = $data->counter_value;
  }

  // Write output.
  $output = '<div  id="counter">';
  $output .= '<ul>';
  if ($counter_show_site_counter) {
    $output .= '<li>' . t('Site Counter:') . '<strong>' . number_format($counter_initial_counter + $counter_value[4]) . '</strong></li>';
  }
  if ($counter_show_unique_visitor) {
    if ($data_update) {
      $sql = " SELECT count(*) as total " . " FROM (SELECT ip FROM {counter} GROUP BY ip) c";
      $counter_unique = db_query($sql)
        ->fetchField();
      $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='unique_visitor' ";
      $results = db_query($sql, array(
        ':counter_value' => $counter_unique,
      ));
    }
    else {
      $counter_unique = $counter_value[5];
    }
    $output .= '<li>' . t('Unique Visitor:') . '<strong>' . number_format($counter_initial_unique_visitor + $counter_unique) . '</strong></li>';
  }
  if ($counter_registered_user) {
    if ($data_update) {
      $sql = " SELECT count(*) as total FROM {users} WHERE access<>0 and uid<>0";
      $total = db_query($sql)
        ->fetchField();
      $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='registered_user' ";
      $results = db_query($sql, array(
        ':counter_value' => $total,
      ));
    }
    else {
      $total = $counter_value[3];
    }
    $output .= '<li>' . t('Registered Users:') . '<strong>' . number_format($total) . '</strong></li>';
  }
  if ($counter_unregistered_user) {
    if ($data_update) {
      $sql = " SELECT count(*) as total FROM {users} WHERE access=0 and uid<>0";
      $total = db_query($sql)
        ->fetchField();
      $sql = "UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='unregistered_user' ";
      $results = db_query($sql, array(
        ':counter_value' => $total,
      ));
    }
    else {
      $total = $counter_value[7];
    }
    $output .= '<li>' . t('Unregistered Users:') . '<strong>' . number_format($total) . '</strong></li>';
  }
  if ($counter_blocked_user) {
    if ($data_update) {
      $sql = " SELECT count(*) as total FROM {users} WHERE status=0 and uid<>0";
      $total = db_query($sql)
        ->fetchField();
      $sql = " UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='blocked_user' ";
      $results = db_query($sql, array(
        ':counter_value' => $total,
      ));
    }
    else {
      $total = $counter_value[1];
    }
    $output .= '<li>' . t('Blocked Users:') . '<strong>' . number_format($total) . '</strong></li>';
  }
  if ($counter_published_node) {
    if ($data_update) {
      $sql = " SELECT count(*) as total FROM {node} WHERE status=1";
      $total = db_query($sql)
        ->fetchField();
      $sql = " UPDATE {counter_data} SET counter_value= :counter_value WHERE counter_name='published_node' ";
      $results = db_query($sql, array(
        ':counter_value' => $total,
      ));
    }
    else {
      $total = $counter_value[2];
    }
    $output .= '<li>' . t('Published Nodes:') . '<strong>' . number_format($total) . '</strong></li>';
  }
  if ($counter_unpublished_node) {
    if ($data_update) {
      $sql = " SELECT count(*) as total FROM {node} WHERE status=0";
      $total = db_query($sql)
        ->fetchField();
      $sql = "UPDATE {counter_data} SET counter_value = :counter_value WHERE counter_name='unpublished_node' ";
      $results = db_query($sql, array(
        ':counter_value' => $total,
      ));
    }
    else {
      $total = $counter_value[6];
    }
    $output .= '<li>' . t('Unpublished Nodes:') . '<strong>' . number_format($total) . '</strong></li>';
  }
  if ($counter_show_server_ip) {
    $output .= '<li>' . t("Server IP:") . '<strong>' . $counter_svr_ip . '</strong></li>';
  }
  if ($counter_show_ip) {
    $output .= '<li>' . t("Your IP:") . '<strong>' . $ip . '</strong></li>';
  }
  if ($counter_show_counter_since) {
    switch ($db_types) {
      case 'mssql':
        $sql = " SELECT TOP 1 created FROM {counter} WHERE created>0 ORDER BY created ASC";
        break;
      case 'oracle':
        $sql = " SELECT created FROM {counter} WHERE ROWNUM=1 AND created>0 ORDER BY created ASC";
        break;
      default:
        $sql = " SELECT created FROM {counter} WHERE created>0 ORDER BY created ASC LIMIT 1";
    }
    $counter_since = db_query($sql)
      ->fetchField();
    if ($counter_initial_since != 0) {
      $counter_since = $counter_initial_since;
    }
    $output .= '<li>' . t("Since:") . '<strong>' . date('d M Y', $counter_since) . '</strong></li>';
  }
  if ($counter_statistic_today || $counter_statistic_week || $counter_statistic_month || $counter_statistic_year) {
    $output .= '<li>' . t("Visitors:") . '</li>';
  }
  $output .= '<ul>';
  if ($counter_statistic_today) {
    $date1 = strtotime(date('Y-m-d'));
    $sql = "SELECT count(*) AS total FROM {counter} WHERE created >= :counter_stat_today";
    $results = db_query($sql, array(
      ':counter_stat_today' => $date1,
    ));
    $statistic = $results
      ->fetchField();
    $output .= '<li>' . t("Today:") . '<strong>' . number_format($statistic) . "</strong></li>";
  }
  if ($counter_statistic_week) {
    $date1 = strtotime(date('Y-m-d')) - 7 * 24 * 60 * 60;
    $date2 = time();
    $sql = " SELECT count(*) AS total FROM {counter} WHERE created > :date1 AND created <= :date2";
    $results = db_query($sql, array(
      ':date1' => $date1,
      ':date2' => $date2,
    ));
    $statistic = $results
      ->fetchField();
    $output .= '<li>' . t("This week:") . '<strong>' . number_format($statistic) . "</strong></li>";
  }
  if ($counter_statistic_month) {
    $date1 = strtotime(date('Y-m-d')) - 30 * 24 * 60 * 60;
    $date2 = time();
    $sql = " SELECT count(*) AS total FROM {counter} WHERE created > :date1 AND created <= :date2";
    $results = db_query($sql, array(
      ':date1' => $date1,
      ':date2' => $date2,
    ));
    $statistic = $results
      ->fetchField();
    $output .= '<li>' . t("This month:") . '<strong>' . number_format($statistic) . "</strong></li>";
  }
  if ($counter_statistic_year) {
    $date1 = strtotime(date('Y-m-d')) - 365 * 24 * 60 * 60;
    $date2 = time();
    $sql = " SELECT count(*) AS total FROM {counter} WHERE created > :date1 AND created <= :date2";
    $results = db_query($sql, array(
      ':date1' => $date1,
      ':date2' => $date2,
    ));
    $statistic = $results
      ->fetchField();
    $output .= '<li>' . t("This year:") . '<strong>' . number_format($statistic) . '</strong></li>';
  }
  $output .= '</ul>';
  $output .= '</ul>';
  $output .= '</div>';
  $block['content'] = $output;
  return $block;
}

/**
 * Implements hook_views_api().
 */
function counter_views_api() {
  return array(
    'api' => 3,
  );
}

Functions

Namesort descending Description
counter_block_info Implements hook_block_info().
counter_block_view Implements hook_block().
counter_help Implements hook_help().
counter_menu Menu callback. Prints a listing of active nodes on the site.
counter_permission Implements hook_permission().
counter_views_api Implements hook_views_api().