boost_stats.php 5.22 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Place in webroot, for faster stats if needed.
 */

8 9 10
// Exit script if nothing was passed to it.
if (   !isset($_GET['nid'])
    && !isset($_GET['title'])
11
    && !isset($_GET['qq'])
12 13
    && !isset($_GET['referer'])
    ) {
14 15
  boost_stats_full_boot();
  drupal_not_found();
16 17 18
  exit;
}

19 20 21 22 23
if (!isset($_GET['js'])) {
  // stats not called via JS, send image out & close connection.
  boost_stats_async_image();
}

24 25
// connect to db & set variables.
boost_stats_init();
26

27
// Set node counter.
28
if (boost_stats_variable_get('statistics_count_content_views')) {
29
  boost_stats_update_node_counter();
30 31
}

32
// Set access log.
33
if (boost_stats_variable_get('statistics_enable_access_log')) {
34 35 36
  boost_stats_add_access_log();
}

mikeytown2's avatar
mikeytown2 committed
37 38 39 40 41 42 43 44
if (isset($_GET['js'])) {
  if ($_GET['js'] == 1) {
    $json = array();
    // Get stats block html.
    $json = array_merge($json, boost_stats_output_stats_block());

    // Send JSON Back
    if (!empty($json)) {
45
      drupal_json($json);
mikeytown2's avatar
mikeytown2 committed
46 47 48 49 50 51
    }
  }
  // Send HTML back
  elseif ($_GET['js'] == 2) {
    echo array_pop(boost_stats_output_stats_block());
  }
52 53 54 55 56
}

// end of script, exit.
exit;

57 58 59


function boost_stats_async_image() {
60
  // Script should take under 1MB of memory to work.
61
  // Prime php for background operations
62 63 64
  while (ob_get_level()) {
    ob_end_clean();
  }
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  header("Connection: close");
  ignore_user_abort();

  // Output of 1 pixel transparent gif
  ob_start();
  header("Content-type: image/gif");
  header("Expires: Wed, 11 Nov 1998 11:11:11 GMT");
  header("Cache-Control: no-cache");
  header("Cache-Control: must-revalidate");
  header("Content-Length: 43");
  header("Connection: close");
  printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",71,73,70,56,57,97,1,0,1,0,128,255,0,192,192,192,0,0,0,33,249,4,1,0,0,0,0,44,0,0,0,0,1,0,1,0,0,2,2,68,1,0,59);
  ob_end_flush();
  flush();

  // Image returned and connection closed.
  // Do background processing. Time taken below should not effect page load times.
}

84 85
function boost_stats_init() {
  Global $nid, $title, $q, $referer, $session_id, $uid, $count_views, $access_log, $variables;
86 87 88

  // Connect to DB.
  include_once './includes/bootstrap.inc';
89
  include_once './includes/common.inc';
90 91 92
  drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

  // Set variables passed via GET.
mikeytown2's avatar
mikeytown2 committed
93 94
  $nid = (isset($_GET['nid']) && is_numeric($_GET['nid'])) ? $_GET['nid'] : NULL;
  $title = (isset($_GET['title']) && $_GET['title'] != 'NULL') ? urldecode($_GET['title']) : NULL;
95
  $q = (isset($_GET['qq']) && $_GET['qq'] != 'NULL') ? $_GET['qq'] : NULL;
96
  //$q = (isset($_GET['q']) && $_GET['q'] != 'NULL') ? $_GET['q'] : NULL;
97
  $referer = isset($_GET['referer']) ? $_GET['referer'] : NULL;
98 99

  // $session_id only goes in the access log; only used for stats, not creds.
100 101
  $session_id = session_id();
  if (empty($session_id)) {
102
    if (empty($_COOKIE[session_name()])) {
103
      if (empty($_SERVER['HTTP_USER_AGENT'])) {
104 105 106 107 108
        $session_id = md5(ip_address());
      }
      else {
        $session_id = md5($_SERVER['HTTP_USER_AGENT'] . ip_address());
      }
109
    }
110 111 112
    else {
      $session_id = $_COOKIE[session_name()];
    }
113
  }
114 115

  // Anonymous users always get a User ID of 0.
116
  $uid = 0;
117

118
  // load all boost, statistics & throttle variables; 1 transaction instead of multiple
119 120 121 122 123 124 125 126
  $result = db_query("
SELECT * FROM {variable}
WHERE name LIKE 'boost_%'
OR name LIKE 'statistics_%'
OR name LIKE 'throttle_%'
");
  while ($variable = db_fetch_object($result)) {
    $variables[$variable->name] = unserialize($variable->value);
127 128 129
  }
}

130
function boost_stats_update_node_counter() {
131 132 133 134 135 136 137 138 139 140 141
  Global $nid;

  // A node has been viewed, so update the node's counters.
  db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), $nid);
  // If we affected 0 rows, this is the first time viewing the node.
  if (!db_affected_rows()) {
    // We must create a new row to store counters for the new node.
    db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES (%d, 1, 1, %d)', $nid, time());
  }
}

142
function boost_stats_add_access_log() {
143 144
  Global $title, $q, $referer, $session_id, $uid;

145
  db_query("INSERT INTO {accesslog} (title, path, url, hostname, uid, sid, timer, timestamp) values('%s', '%s', '%s', '%s', %d, '%s', %d, %d)", $title, $q, $referer, ip_address(), $uid, $session_id, timer_read('page'), time());
146
}
147

148
function boost_stats_output_stats_block() {
mikeytown2's avatar
mikeytown2 committed
149 150 151 152 153 154 155 156 157
  if (boost_stats_variable_get('boost_block_show_stats')) {
    if (!boost_stats_variable_get('boost_block_cache_stats_block') && !boost_stats_variable_get('throttle_level')) {
      boost_stats_full_boot();
      $block = module_invoke('statistics', 'block', 'view', 0);
      $block = $block['content'];
    }
    else {
      $block = boost_stats_variable_get('boost_statistics_html');
    }
158 159
  }
  else {
mikeytown2's avatar
mikeytown2 committed
160
    $block = 'NULL';
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  }

  return array('#boost-stats' => $block);
}

function boost_stats_full_boot() {
  Global $full_boot;
  if (!isset($full_boot)) {
    $full_boot = FALSE;
  }
  if (!$full_boot) {
    include_once './includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  }
  $full_boot = TRUE;
176
}
177 178 179 180

function boost_stats_variable_get($name) {
  Global $variables;
  return isset($variables[$name]) ? $variables[$name] : FALSE;
181
}