This repository was archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.useragent.plugin.php
More file actions
95 lines (83 loc) · 3.07 KB
/
class.useragent.plugin.php
File metadata and controls
95 lines (83 loc) · 3.07 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
<?php if (!defined('APPLICATION')) exit();
class UserAgentPlugin extends Gdn_Plugin {
public $Logos = array(
'Chrome' => 'chrome.png',
'Chromium' => 'chromium.png',
'Firefox' => 'firefox.png',
'IE' => 'ie.png',
'Maxthon' => 'maxthon.png',
'Mercury' => 'mercury.png',
'Opera' => 'opera.png',
'PaleMoon' => 'palemoon.png',
'Safari' => 'safari.png'
);
public function Base_Render_Before($Sender) {
$Sender->addCssFile('useragent.css', 'plugins/UserAgent');
}
// Comments display
public function DiscussionController_CommentInfo_Handler($Sender, $Args) {
$Comment = GetValue('Comment', $Args);
$Attributes = GetValue('Attributes', $Comment);
$this->AttachInfo($Sender, $Attributes);
}
// Comments, after saving an edit
public function PostController_CommentInfo_Handler($Sender, $Args) {
$this->DiscussionController_CommentInfo_Handler($Sender, $Args);
}
// Discussions display
public function DiscussionController_DiscussionInfo_Handler($Sender, $Args) {
$Discussion = GetValue('Discussion', $Args);
$Attributes = GetValue('Attributes', $Discussion);
$this->AttachInfo($Sender, $Attributes);
}
public function CommentModel_BeforeSaveComment_Handler($Sender, $Args) {
if ($Args['FormPostValues']['InsertUserID'] != Gdn::Session()->UserID)
return;
$this->SetAttributes($Sender, $Args);
}
public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender, $Args) {
if ($Args['FormPostValues']['InsertUserID'] != Gdn::Session()->UserID)
return;
$this->SetAttributes($Sender, $Args);
}
/**
* Collect user agent data and save in Attributes array.
*/
protected function SetAttributes($Sender, $Args) {
if (!isset($Args['FormPostValues']['Attributes'])) {
$Args['FormPostValues']['Attributes'] = array();
} else {
$Args['FormPostValues']['Attributes'] = unserialize($Args['FormPostValues']['Attributes']);
}
// Add user agent data to Attributes
$UserAgent = GetValue('HTTP_USER_AGENT', $_SERVER);
$Args['FormPostValues']['Attributes']['UserAgent'] = GetValue('HTTP_USER_AGENT', $_SERVER);
$BrowserData = @get_browser($UserAgent); // requires browsecap.ini
if ($BrowserData) {
$Args['FormPostValues']['Attributes']['Browser'] = $BrowserData->browser;
}
$Args['FormPostValues']['Attributes'] = serialize($Args['FormPostValues']['Attributes']);
}
/**
* Output user agent information.
*/
protected function AttachInfo($Sender, $Attributes) {
$Info = null;
$UserAgent = GetValue('UserAgent', $Attributes);
$Browser = GetValue('Browser', $Attributes);
if ($UserAgent) {
if ($Browser) {
$Logo = $this->Logos[$Browser];
if ($Logo) {
$Info = Img($this->GetResource('logos/'.$Logo, FALSE, FALSE), array('alt' => htmlspecialchars($Browser)));
} else {
$Info = htmlspecialchars($Browser);
}
}
If (!$Info) {
$Info = '[?]';
}
echo Wrap($Info, 'span', array('class' => 'MItem UserAgent', 'title' => htmlspecialchars($UserAgent)));
}
}
}