| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Allows configuration of alerts for traffic change events.
|
| 6 |
*/
|
| 7 |
|
| 8 |
function incoming_menu() {
|
| 9 |
$items['admin/settings/incoming'] = array(
|
| 10 |
'title' => 'Incoming',
|
| 11 |
'description' => 'Control alerts you receive when the incoming traffic level on your site changes.',
|
| 12 |
'page callback' => 'drupal_get_form',
|
| 13 |
'page arguments' => array('incoming_admin_settings'),
|
| 14 |
'access arguments' => array('administer site configuration'),
|
| 15 |
'file' => 'incoming.admin.inc',
|
| 16 |
);
|
| 17 |
return $items;
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_exit().
|
| 22 |
*
|
| 23 |
*
|
| 24 |
*/
|
| 25 |
function incoming_exit() {
|
| 26 |
// If we've recently sent an alert, we don't need to check anything until the
|
| 27 |
// amount of time for a second alert set in the admin area has passed.
|
| 28 |
|
| 29 |
if (variable_get('incoming_alert_delay', 0) < time()) {
|
| 30 |
// The following logic determines what the current incoming level should
|
| 31 |
// be, and can be disabled by the admin. If enabled, the mt_rand() function
|
| 32 |
// returns a number between 0 and N, N being specified by the admin. If
|
| 33 |
// 0 is returned, the incoming logic is run, adding two additional database
|
| 34 |
// queries. Otherwise, the following logic is skipped. This mechanism is
|
| 35 |
// referred to in the admin page as the 'probability limiter', roughly
|
| 36 |
// limiting incoming related database calls to 1 in N. (Based on throttle_exit();)
|
| 37 |
if (!mt_rand(0, variable_get('incoming_probability_limiter', 9))) {
|
| 38 |
|
| 39 |
// The time period to check against in seconds.
|
| 40 |
$time_period = variable_get('incoming_time_period', 600);
|
| 41 |
|
| 42 |
// Get the number of anonymous users currently online, and the number of anonymous
|
| 43 |
// users that were online during the preceding equal amount of time.
|
| 44 |
$current_online = sess_count(time() - $time_period, TRUE);
|
| 45 |
$previous_online = (sess_count(time() - ($time_period*2), TRUE)) - $current_online;
|
| 46 |
|
| 47 |
// We'll only need to figure out how big the change is (and possibly send an alert)
|
| 48 |
// if there are more users online now than there were before.
|
| 49 |
if ($current_online > $previous_online) {
|
| 50 |
// How many new online users is takes for an alert to be sent.
|
| 51 |
$trigger = variable_get('incoming_alert_threshold', 10);
|
| 52 |
$difference = $current_online - $previous_online;
|
| 53 |
if ($difference > $trigger) {
|
| 54 |
incoming_alert($difference);
|
| 55 |
// Set the time this alert should be delayed until.
|
| 56 |
$delay_until = time() + variable_get('incoming_alert_suppression', 7200);
|
| 57 |
variable_set('incoming_alert_delay', $delay_until);
|
| 58 |
}
|
| 59 |
}
|
| 60 |
}
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Send an alert if the online user threshold is crossed.
|
| 66 |
*/
|
| 67 |
function incoming_alert($difference) {
|
| 68 |
$to = variable_get('incoming_alert_mail', NULL);
|
| 69 |
$params['difference'] = $difference;
|
| 70 |
|
| 71 |
// Only send if we have an address to send to.
|
| 72 |
if ($to) {
|
| 73 |
drupal_mail('incoming', 'alert', $to, $language, $params);
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
function incoming_mail($key, &$message, $params) {
|
| 78 |
switch($key) {
|
| 79 |
case 'alert':
|
| 80 |
$message['subject'] = t('Increase in incoming traffic: @site', array(
|
| 81 |
'@site' => variable_get('site_name', 'Drupal')));
|
| 82 |
$message['body'] = t("There has been a significant increase in incoming traffic to your site '@site'. @difference new visitors were detected in the last @since. @referrers", array(
|
| 83 |
'@site' => variable_get('site_name', 'Drupal'),
|
| 84 |
'@difference' => $params['difference'],
|
| 85 |
'@since' => format_interval(variable_get('incoming_time_period', 600)),
|
| 86 |
'@referrers' => incoming_likely_referrers(),
|
| 87 |
));
|
| 88 |
break;
|
| 89 |
}
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Format a list of incoming traffic sources.
|
| 94 |
*/
|
| 95 |
function incoming_likely_referrers() {
|
| 96 |
// Make sure the access log is enabled
|
| 97 |
if (variable_get('statistics_enable_access_log', 0) == 1){
|
| 98 |
$timespan = time() - variable_get('incoming_time_period', 600);
|
| 99 |
$query = "SELECT url, timestamp, COUNT(url) AS hits FROM {accesslog} WHERE url NOT LIKE '%%%s%%' AND url <> '' AND timestamp > %d GROUP BY url ORDER BY hits DESC";
|
| 100 |
$result = db_query($query, $_SERVER['HTTP_HOST'], $timespan);
|
| 101 |
|
| 102 |
$output = t("\n\n Recent Incoming Traffic: \n");
|
| 103 |
while ($referrer = db_fetch_object($result)) {
|
| 104 |
$hits = format_plural($referrer->hits, '1 hit', '@count hits');
|
| 105 |
$output .= t("!hits from !url \n", array('!hits' => $hits, '!url' => $referrer->url));
|
| 106 |
}
|
| 107 |
}
|
| 108 |
else {
|
| 109 |
$output = t("\n\n\n Enable access logging on your site to see referring url information in these alerts.");
|
| 110 |
}
|
| 111 |
return $output;
|
| 112 |
}
|