-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdc_pixelaccess.module
More file actions
168 lines (151 loc) · 4.46 KB
/
dc_pixelaccess.module
File metadata and controls
168 lines (151 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* @file
* Pixel access counter
*/
/**
* Implementation of hook_menu().
*/
function dc_pixelaccess_menu() {
$items = array();
$items['ajax/dc_pixelaccess_stats.php'] = array(
'page callback' => 'dc_pixelaccess_ajax_callback',
'type' => MENU_CALLBACK,
'access callback' => 1,
'access arguments' => array('access content'),
'file path' => drupal_get_path('module', 'dc_pixelaccess'),
'file' => 'dc_pixelaccess.ajax.inc'
);
return $items;
}
/**
* Implementation of hook_block().
*/
function dc_pixelaccess_block($op = 'list', $delta = 0, $edit = array()) {
global $user;
switch ($op) {
case 'list':
$blocks['dc-pixelaccess-count'] = array(
'info' => t('DrupalCONCEPT: AJAX core statistics'),
'weight' => 10,
'cache' => BLOCK_NO_CACHE,
);
return $blocks;
case 'view':
//dpm($_SERVER);
if (!( strpos($_SERVER['SCRIPT_FILENAME'], 'index.php') === FALSE
|| variable_get('site_offline', 0)
|| ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != 'HEAD')
|| $_SERVER['SERVER_SOFTWARE'] === 'PHP CLI'
|| isset($_GET['nocache'])
|| !empty($user->uid)
|| !module_exists('statistics')
)) {
$block = array();
$block['subject'] = t('DrupalCONCEPT Pixelaccess counter');
$block['content'] .= '<div id="dc_pixelaccess-stats"></div>' . dc_pixelaccess_stats_generate("ajax/dc_pixelaccess_stats.php");
}
else {
$block['content'] .= '<div id="dc_pixelaccess-stats"></div>';
drupal_add_js('$("#dc_pixelaccess-stats").parent().parent().hide();', 'inline', 'footer');
}
break;
}
return $block;
}
/**
* Generate js/html for DC pixelaccess stat counter.
*
* NOTE HTML code could be added to the $buffer directly. Would prevent 2x
* counts on first view. Would be hard to do though.
*
* @param $filename
* Name of pixelaccess's statistics php file.
*/
function dc_pixelaccess_stats_generate($filename) {
global $base_path;
// is node & node count enabled.
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '' && variable_get('statistics_count_content_views', 0)) {
$nid = arg(1);;
}
else {
$nid = 'NULL';
}
// access log enabled.
if ((variable_get('statistics_enable_access_log', 0)) && (module_invoke('throttle', 'status') == 0)) {
$title = drupal_urlencode(strip_tags(drupal_get_title()));
$q = $_GET['q'];
}
else {
$title = 'NULL';
$q = 'NULL';
}
$page_js = array(
'dc_pixelaccess' => array(
'nid' => $nid,
'q' => $q,
'title' => $title,
),
);
$site_js = <<<ETO
$.getJSON(Drupal.settings.basePath + "$filename", {nocache: "1", js: "1", nid: Drupal.settings.dc_pixelaccess.nid, qq: Drupal.settings.dc_pixelaccess.q, title: Drupal.settings.dc_pixelaccess.title, referer: document.referrer}, function(response) {
$.each(response, function(id, contents) {
if (contents == 'NULL') {
$(id).parent().parent().hide();
}
else {
$(id).html(contents);
}
});
});
ETO;
// page specific variables
drupal_add_js($page_js, 'setting', 'header');
// site-wide code
drupal_add_js($site_js, 'inline', 'footer');
// no script code
$page_ns = '<noscript><div style="display:inline;"><img src="' . $base_path . $filename . '?nocache=1' . '&nid='. $nid . '&title='. $title . '&q='. $q . '" alt="" /></div></noscript>';
return $page_ns;
}
/**
* Output text & set php in async mode.
*
* @param $output
* string - Text to output to open connection.
* @param $wait
* bool - Wait 1 second?
* @param $content_type
* string - Content type header.
*/
function dc_pixelaccess_async_opp($output, $wait = TRUE, $content_type = "text/html; charset=utf-8", $length = 0) {
// Calculate Content Lenght
if ($length == 0) {
$output .= "\n";
$length = (mb_strlen($output, '8bit')-1);
}
// Prime php for background operations
$loop = 0;
while (ob_get_level() && $loop < 25) {
ob_end_clean();
$loop++;
}
header("Connection: close");
ignore_user_abort();
// Output headers & data
ob_start();
header("Content-type: " . $content_type);
header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Cache-Control: must-revalidate");
header("Content-Length: " . $length);
header("Connection: close");
print($output);
ob_end_flush();
flush();
// wait for 5 milliseconds
if ($wait) {
usleep(5000);
}
// text returned and connection closed.
// Do background processing. Time taken after should not effect page load times.
}