-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatisticsAggregationPageHandler.inc.php
executable file
·153 lines (116 loc) · 5.24 KB
/
StatisticsAggregationPageHandler.inc.php
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
<?php
/**
* @file StatisticsAggregationPageHandler.inc.php
*
* Copyright (c) 2003-2010 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @package plugins.generic.statisticsAggregator
* @class StatisticsAggregationPageHandler
*
*/
class StatisticsAggregationPageHandler extends Handler {
function index($args) {
return true;
}
function viewstats($args) {
$journal =& Request::getJournal();
$journalId = $journal->getJournalId();
$statAggrPlugin =& PluginRegistry::getPlugin('generic', 'StatisticsAggregationPlugin');
$statisticsAggregationSiteId =& $statAggrPlugin->getSetting($journalId, 'statisticsAggregationSiteId');
if ($statisticsAggregationSiteId != '') {
Request::redirectUrl('http://warhammer.hil.unb.ca/stats/' . $statisticsAggregationSiteId . '/landing.php');
}
}
function lookup($args) {
$journal =& Request::getJournal();
$journalId = $journal->getJournalId();
$subscriptionDao =& DAORegistry::getDAO('SubscriptionDAO');
$journalStatisticsDao =& DAORegistry::getDAO('JournalStatisticsDAO');
$subscriptionStats = $journalStatisticsDao->getSubscriptionStatistics($journalId);
$registeredUsers = $journalStatisticsDao->getUserStatistics($journalId);
$totalReaders = 0;
if (isset($registeredUsers['reader'])) {
$totalReaders = $registeredUsers['reader'];
}
$ipXML = Request::getUserVar('ipXML');
if ($ipXML != '') {
// build an XML doc based on the IP document posted from the aggregation server
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($ipXML);
if ($xmlDoc) {
// grab the <h> node which contains the <v> visitor nodes
$hostNodeList = $xmlDoc->getElementsByTagName('h');
if ($hostNodeList->length == 1) { // there should just be one top level host node.
$node =& $hostNodeList->item(0);
if ($node->hasAttributes()) {
// the <h> node has a 'key' attribute containing the sha1 security hash sent along
$key =& $node->attributes->item(0)->value;
// key is a security code we included in the XML document when we sent it
if (preg_match("/^[\w\d]{40}$/", $key)) {
// load our plugin to get settings
$statAggrPlugin =& PluginRegistry::getPlugin('generic', 'StatisticsAggregationPlugin');
$storedHashCode =& $statAggrPlugin->getSetting($journalId, 'statisticsAggregationSiteId');
$storedEmailAddress =& $statAggrPlugin->getSetting($journalId, 'statisticsAggregationSiteEmail');
// this was a valid submission? the keys match?
if (sha1($storedHashCode . $storedEmailAddress) == $key) {
// grab <v> nodes.
$visitorNodes = $xmlDoc->getElementsByTagName('v');
$visitorNodeCount = $visitorNodes->length;
for ($i = 0 ; $i < $visitorNodeCount ; $i ++) {
$visitorNode =& $visitorNodes->item($i);
$hostToSearch =& $visitorNode->attributes->item(0)->value; // it's either a domain or an IP address
$subscriptionId = false;
$subscriptionName = '';
$domain = $ipAddress = null;
if (preg_match("{^\d+\.\d+\.\d+\.\d+$}", $hostToSearch)) { // it's an IP?
$ipAddress = $hostToSearch;
$domain = null;
} else {
$domain = $hostToSearch;
$ipAddress = null;
}
$subscriptionId = $subscriptionDao->isValidSubscription($domain, $ipAddress, null, $journalId);
if ($subscriptionId) {
$subscription =& $subscriptionDao->getSubscription($subscriptionId);
$userDao =& DAORegistry::getDAO('UserDAO');
$subScriptionUser =& $userDao->getUser($subscription->getUserId());
$subscriptionName = $subScriptionUser->getFullName();
}
// whether we found a subscription or not, set the 'sub' attribute on the <v> node so we can send it back.
$visitorNode->setAttribute('sub', $subscriptionName);
}
} else { // error out -- incorrect security key
return null;
}
}
}
}
// build the new document
$rootNode = $xmlDoc->getElementsByTagName('visitorDocument')->item(0);
$userInformationElement = $xmlDoc->createElement('userInfo');
$readerTotalElement = $xmlDoc->createElement('readers');
$readerTotalText = $xmlDoc->createTextNode($totalReaders);
$readerTotalElement->appendChild($readerTotalText);
$userInformationElement->appendChild($readerTotalElement);
$subscriptionsElement = $xmlDoc->createElement('subscriptions');
foreach ($subscriptionStats as $subscriptionType) {
$subscriptionElement = $xmlDoc->createElement('sub');
$subscriptionElement->setAttribute('name', $subscriptionType['name']);
$subscriptionElement->setAttribute('count', $subscriptionType['count']);
$subscriptionsElement->appendChild($subscriptionElement);
}
$userInformationElement->appendChild($subscriptionsElement);
// import our new user info into the original document, append it, and send it back.
$userInformationElement = $xmlDoc->importNode($userInformationElement, true);
$rootNode->appendChild($userInformationElement);
print $xmlDoc->saveXML(); // this is the respone to the CURL request from the aggregation server.
exit(0);
}
} else {
error_log("No XML document retrieved. Exiting.");
exit(0);
}
}
}
?>