-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualmin-google-analytics-lib.pl
executable file
·227 lines (206 loc) · 5.81 KB
/
virtualmin-google-analytics-lib.pl
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use strict;
use warnings;
our %access;
our $module_config_directory;
our $module_root_directory;
our @tracking_services;
our $apachemod_lib_cmd;
BEGIN { push(@INC, ".."); };
eval "use WebminCore;";
&init_config();
&foreign_require("virtual-server", "virtual-server-lib.pl");
%access = &get_module_acl();
$apachemod_lib_cmd = "$module_config_directory/apachemod.pl";
@tracking_services = (
[ 'account', \&get_analytics_account, \&save_analytics_account,
'[A-Za-z0-9\\-]+' ],
[ 'quantcast', \&get_quantcast_account, \&save_quantcast_account,
'[A-Za-z0-9\\-]+' ],
[ 'clicky', \&get_clicky_account, \&save_clicky_account,
'[0-9]+' ],
[ 'woopra', \&get_woopra_account, \&save_woopra_account,
'[A-Za-z0-9\\-]+' ],
[ 'piwik', \&get_piwik_account, \&save_piwik_account,
'[0-9]+' ],
);
# get_analytics_account(&domain)
# Returns the Google Analytics account ID for a virtual server, by looking
# at the server's PerlSetVar directive.
sub get_analytics_account
{
my ($d) = @_;
return &get_perlsetvar($d, "AnalyticsID");
}
# get_quantcast_account(&domain)
# Returns the Quantcast account ID for a virtual server, by looking
# at the server's PerlSetVar directive.
sub get_quantcast_account
{
my ($d) = @_;
return &get_perlsetvar($d, "QuantcastID");
}
# get_clicky_account(&domain)
# Returns the Clicky account ID for a virtual server, by looking
# at the server's PerlSetVar directive.
sub get_clicky_account
{
my ($d) = @_;
return &get_perlsetvar($d, "ClickyID");
}
# get_woopra_account(&domain)
# Returns the Woopra account name for a virtual server, by looking
# at the server's PerlSetVar directive. Not actually used in the JS though.
sub get_woopra_account
{
my ($d) = @_;
return &get_perlsetvar($d, "WoopraID");
}
# get_piwik_account(&domain)
# Returns the Piwik site ID for a virtual server, by looking
# at the server's PerlSetVar directive.
sub get_piwik_account
{
my ($d) = @_;
return &get_perlsetvar($d, "PiwikID");
}
# get_perlsetvar(&domain, name)
sub get_perlsetvar
{
my ($d, $name) = @_;
my ($virt, $vconf) = &virtual_server::get_apache_virtual($d->{'dom'},
$d->{'web_port'});
if ($virt) {
my @psv = &apache::find_directive("PerlSetVar", $vconf);
foreach my $psv (@psv) {
if ($psv =~ /^\Q$name\E\s+(\S+)/) {
return $1;
}
}
}
return undef;
}
# save_analytics_account(&domain, account)
# Add or set the PerlSetVar directive use by the Perl filter to generate
# the correct <script> section for Google Analytics.
sub save_analytics_account
{
my ($d, $account) = @_;
return &save_perlsetvar($d, $account, "AnalyticsID");
}
# save_quantcast_account(&domain, account)
# Adds directives for the MyBlogLog account ID
sub save_quantcast_account
{
my ($d, $account) = @_;
return &save_perlsetvar($d, $account, "QuantcastID");
}
# save_clicky_account(&domain, account)
# Adds directives for the Clicky account ID
sub save_clicky_account
{
my ($d, $account) = @_;
return &save_perlsetvar($d, $account, "ClickyID");
}
# save_woopra_account(&domain, account)
# Adds directives for the Woopra account name
sub save_woopra_account
{
my ($d, $account) = @_;
return &save_perlsetvar($d, $account, "WoopraID");
}
# save_piwik_account(&domain, account)
# Adds directives for the Piwik site ID
sub save_piwik_account
{
my ($d, $account) = @_;
return &save_perlsetvar($d, $account, "PiwikID");
}
# save_perlsetvar(&domain, account, name)
sub save_perlsetvar
{
my ($d, $account, $name) = @_;
&virtual_server::obtain_lock_web($d)
if (defined(&virtual_server::obtain_lock_web));
&virtual_server::require_apache();
my $conf = &apache::get_config();
my @ports = ( $d->{'web_port'} );
push(@ports, $d->{'web_sslport'}) if ($d->{'ssl'});
my $done = 0;
foreach my $p (@ports) {
my ($virt, $vconf) =
&virtual_server::get_apache_virtual($d->{'dom'}, $p);
next if (!$virt);
my @psv = &apache::find_directive("PerlSetVar", $vconf);
my @oldpsv = @psv;
@psv = grep { !/^\Q$name\E/ } @psv;
if ($account) {
push(@psv, "$name $account");
}
if (scalar(@psv) || scalar(@oldpsv)) {
&apache::save_directive("PerlSetVar", \@psv, $vconf, $conf);
&flush_file_lines($virt->{'file'});
}
$done++;
}
&virtual_server::release_lock_web($d)
if (defined(&virtual_server::release_lock_web));
if ($done) {
&virtual_server::register_post_action(\&virtual_server::restart_apache);
}
return $done;
}
# has_analytics_directives(&domain)
# Checks if some domain has the mod_perl directives needed for analytics
# integration (PerlOutputFilterHandler and PerlRequire)
sub has_analytics_directives
{
my ($d) = @_;
my ($virt, $vconf) = &virtual_server::get_apache_virtual($d->{'dom'},
$d->{'web_port'});
my @pof = &apache::find_directive("PerlOutputFilterHandler", $vconf);
@pof = grep { $_ eq "Virtualmin::GoogleAnalytics" } @pof;
return 0 if (!@pof);
my @prq = &apache::find_directive("PerlRequire", $vconf);
@prq = grep { $_ eq "$module_config_directory/apachemod.pl" } @prq;
return 0 if (!@prq);
return 1;
}
# get_piwik_default_url(&domain)
# Returns the default piwik URL. This is keyed off the top-level domain
sub get_piwik_default_url
{
my ($d) = @_;
my $parent = $d->{'parent'} ? &virtual_server::get_domain($d->{'parent'})
: $d;
return $parent->{'piwik_url'};
}
# save_piwik_default_url(&domain, url)
# Set the piwik default base URL
sub save_piwik_default_url
{
my ($d, $url) = @_;
my $parent = $d->{'parent'} ? &virtual_server::get_domain($d->{'parent'})
: $d;
$parent->{'piwik_url'} = $url;
&virtual_server::save_domain($parent);
}
# create_apachemod()
# Creates the apachemod.pl command used by Apache to find the Perl module that
# does the actual HTML modification
sub create_apachemod
{
my $perl_path = &get_perl_path();
no strict "subs";
&open_lock_tempfile(CMD, ">$apachemod_lib_cmd");
&print_tempfile(CMD, <<EOF
#!$perl_path
use lib '$module_root_directory';
1;
EOF
);
&close_tempfile(CMD);
use strict "subs";
chmod(0755, $apachemod_lib_cmd);
}
1;
1;