Repository navigation
Expand file tree
/
Copy pathfunctions.inc
More file actions
296 lines (244 loc) · 9.56 KB
/
Copy pathfunctions.inc
File metadata and controls
296 lines (244 loc) · 9.56 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | analytics plugin 1.1.3 |
// +---------------------------------------------------------------------------+
// | functions.inc |
// | |
// | This file does two things: 1) it implements the necessary Geeklog Plugin |
// | API method and 2) implements all the common code needed by the analytics |
// | plugin' PHP files. |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2000-2016 by the following authors: |
// | |
// | Authors: Tony Bibbs - tony AT tonybibbs DOT com |
// | Tom Willett - twillett AT users DOT sourceforge DOT net |
// | Blaine Lang - langmail AT sympatico DOT ca |
// | Dirk Haun - dirk AT haun-online DOT de |
// | Ben - hostellerie.org AT gmail DOT com |
// +---------------------------------------------------------------------------+
// | |
// | This program is free software; you can redistribute it and/or |
// | modify it under the terms of the GNU General Public License |
// | as published by the Free Software Foundation; either version 2 |
// | of the License, or (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software Foundation, |
// | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
// | |
// +---------------------------------------------------------------------------+
//
if (strpos(strtolower($_SERVER['PHP_SELF']), 'functions.inc') !== false) {
die('This file can not be used on its own.');
}
$pi_name = 'analytics';
$_AN_CONF['version'] = '1.1.3';
$plugin_path = $_CONF['path'] . 'plugins/analytics/';
$langfile = $plugin_path . 'language/' . $_CONF['language'] . '.php';
if (file_exists($langfile)) {
require_once $langfile;
} else {
require_once $plugin_path . 'language/english.php';
}
/**
* Return a trimmed scalar configuration value.
*/
function analytics_trimConfigValue($value)
{
if (is_string($value) || is_numeric($value)) {
return trim((string) $value);
}
return '';
}
/**
* Normalize a GA4 Measurement ID. Returns an empty string when invalid.
*/
function analytics_normalizeMeasurementId($value)
{
$value = strtoupper(analytics_trimConfigValue($value));
if ($value === '' || !preg_match('/^G-[A-Z0-9]+$/', $value)) {
return '';
}
return $value;
}
/**
* Normalize a GA4 Property ID to the numeric identifier only.
*/
function analytics_normalizePropertyId($value)
{
$value = analytics_trimConfigValue($value);
if (stripos($value, 'properties/') === 0) {
$value = substr($value, strlen('properties/'));
}
if ($value === '' || !ctype_digit($value)) {
return '';
}
return $value;
}
/**
* Normalize a Google OAuth web Client ID. Returns empty when invalid.
*/
function analytics_normalizeClientId($value)
{
$value = analytics_trimConfigValue($value);
if ($value === '' || !preg_match('/^[A-Za-z0-9._-]+\.apps\.googleusercontent\.com$/', $value)) {
return '';
}
return $value;
}
/**
* Normalize a hostname. URLs are accepted for convenience.
*/
function analytics_normalizeHostname($value)
{
$value = analytics_trimConfigValue($value);
if ($value === '') {
return '';
}
if ($value === '*') {
return '*';
}
if (strpos($value, '://') !== false) {
$host = parse_url($value, PHP_URL_HOST);
} else {
$host = parse_url('//' . $value, PHP_URL_HOST);
}
if ($host === false || $host === null || $host === '') {
return '';
}
$host = strtolower(rtrim(trim($host), '.'));
if (!preg_match('/^[a-z0-9.-]+$/', $host)) {
return '';
}
return $host;
}
/**
* Read and normalize all runtime configuration in one place.
*/
function analytics_getConfig()
{
global $_CONF;
$raw = array();
if (class_exists('config')) {
$c = config::get_instance();
$config = $c->get_config('analytics');
if (is_array($config)) {
$raw = $config;
}
}
$gaCode = isset($raw['ga_code']) ? analytics_normalizeMeasurementId($raw['ga_code']) : '';
$propertyId = isset($raw['property_id']) ? analytics_normalizePropertyId($raw['property_id']) : '';
$clientId = isset($raw['client_id']) ? analytics_normalizeClientId($raw['client_id']) : '';
$configuredHostname = isset($raw['hostname']) ? analytics_normalizeHostname($raw['hostname']) : '';
if ($configuredHostname === '') {
$siteUrl = isset($_CONF['site_url']) ? $_CONF['site_url'] : '';
$configuredHostname = analytics_normalizeHostname($siteUrl);
}
$hostnameFilterEnabled = ($configuredHostname !== '' && $configuredHostname !== '*');
$hostname = $hostnameFilterEnabled ? $configuredHostname : '';
return array(
'ga_code' => $gaCode,
'property_id' => $propertyId,
'client_id' => $clientId,
'hostname' => $hostname,
'hostname_filter_enabled' => $hostnameFilterEnabled
);
}
/**
* Encode a PHP value for safe insertion in JavaScript source.
*/
function analytics_jsonEncode($value)
{
return json_encode($value, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);
}
function plugin_chkVersion_analytics()
{
global $_AN_CONF;
return $_AN_CONF['version'];
}
function plugin_autouninstall_analytics()
{
return array(
'tables' => array(),
'groups' => array('analytics Admin'),
'features' => array('analytics.edit'),
// Keep the legacy block name here so old installations are cleaned up.
'php_blocks' => array('phpblock_analytics'),
'vars' => array()
);
}
function plugin_getheadercode_analytics()
{
$analyticsConfig = analytics_getConfig();
$measurementId = $analyticsConfig['ga_code'];
if ($measurementId === '') {
return '';
}
$measurementIdHtml = htmlspecialchars($measurementId, ENT_QUOTES, 'UTF-8');
$measurementIdJs = analytics_jsonEncode($measurementId);
return LB . '<!-- Global site tag (gtag.js) - Google Analytics -->' . LB
. '<script async src="https://www.googletagmanager.com/gtag/js?id=' . $measurementIdHtml . '"></script>' . LB
. '<script>' . LB
. ' window.dataLayer = window.dataLayer || [];' . LB
. ' function gtag(){dataLayer.push(arguments);}' . LB
. ' gtag(\'js\', new Date());' . LB
. ' gtag(\'config\', ' . $measurementIdJs . ');' . LB
. '</script>' . LB;
}
function plugin_getadminoption_analytics()
{
global $_CONF, $LANG_analytics01;
if (SEC_hasRights('analytics.edit')) {
return array($LANG_analytics01['plugin_name'], $_CONF['site_admin_url'] . '/plugins/analytics/index.php');
}
return false;
}
function plugin_cclabel_analytics()
{
global $_CONF, $LANG_analytics01;
if (SEC_hasRights('analytics.edit')) {
return array(
$LANG_analytics01['plugin_name'],
$_CONF['site_admin_url'] . '/plugins/analytics/index.php',
plugin_geticon_analytics()
);
}
return false;
}
function plugin_geticon_analytics()
{
global $_CONF;
return $_CONF['site_admin_url'] . '/plugins/analytics/images/logo.png';
}
function plugin_upgrade_analytics()
{
global $_TABLES, $_CONF, $_AN_CONF, $_DB_table_prefix;
$currentVersion = DB_getItem($_TABLES['plugins'], 'pi_version', "pi_name='analytics'");
if ($currentVersion == $_AN_CONF['version']) {
return true;
}
require_once $_CONF['path'] . 'plugins/analytics/install_updates.php';
analytics_update_ConfValues_1_1_3();
// Migrate legacy tracking code if the historical table still exists.
$oldTable = $_DB_table_prefix . 'analytics';
$tableExists = DB_query("SHOW TABLES LIKE '{$oldTable}'");
if (DB_numRows($tableExists) > 0) {
$code = analytics_trimConfigValue(DB_getItem($oldTable, 'code'));
if ($code !== '' && class_exists('config')) {
$c = config::get_instance();
if ($c->group_exists('analytics')) {
$c->set('ga_code', $code, 'analytics');
}
}
DB_query("DROP TABLE {$oldTable}");
}
DB_query("UPDATE {$_TABLES['plugins']} SET pi_version='{$_AN_CONF['version']}',pi_homepage='https://github.com/Geeklog-Plugins/analytics',pi_gl_version='2.1.1' WHERE pi_name='analytics' LIMIT 1");
return true;
}
?>