forked from mdurys/piwikanalyticsjs-prestashop
/
PKHelper.php
638 lines (589 loc) · 27.3 KB
/
PKHelper.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
<?php
if (!defined('_PS_VERSION_'))
exit;
if (class_exists('PKHelper', FALSE))
return;
/*
* Copyright (C) 2014-2015 Christian Jensen
*
* This file is part of PiwikAnalyticsJS for prestashop.
*
* PiwikAnalyticsJS for prestashop 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 3 of the License, or
* (at your option) any later version.
*
* PiwikAnalyticsJS for prestashop 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 PiwikAnalyticsJS for prestashop. If not, see <http://www.gnu.org/licenses/>.
*
*
* @link http://cmjnisse.github.io/piwikanalyticsjs-prestashop
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
class PKHelper {
public static $acp = array(
'updatePiwikSite' => array(
'required' => array('idSite'),
'optional' => array('siteName', 'urls', 'ecommerce', 'siteSearch', 'searchKeywordParameters', 'searchCategoryParameters', 'excludedIps', 'excludedQueryParameters', 'timezone', 'currency', 'group', 'startDate', 'excludedUserAgents', 'keepURLFragments', 'type'),
'order' => array('idSite', 'siteName', 'urls', 'ecommerce', 'siteSearch', 'searchKeywordParameters', 'searchCategoryParameters', 'excludedIps', 'excludedQueryParameters', 'timezone', 'currency', 'group', 'startDate', 'excludedUserAgents', 'keepURLFragments', 'type'),
),
'getPiwikSite' => array('required' => array('idSite'), 'optional' => array(''), 'order' => array('idSite'),),
'getPiwikSite2' => array('required' => array('idSite'), 'optional' => array(''), 'order' => array('idSite'),),
'getSitesGroups' => array('required' => array(), 'optional' => array(), 'order' => array(),),
'getSitesWithViewAccess' => array('required' => array(), 'optional' => array(), 'order' => array(),),
'getSitesWithAdminAccess' => array('required' => array(), 'optional' => array('fetchAliasUrls','getBaseURLParams'), 'order' => array('fetchAliasUrls','getBaseURLParams'),),
'getTokenAuth' => array(
'required' => array('userLogin'),
'optional' => array('password', 'md5Password'),
'order' => array('userLogin', 'password', 'md5Password'),
),
);
/**
* all errors isset by class PKHelper
* @var string[]
*/
public static $errors = array();
/**
* last isset error by class PKHelper
* @var string
*/
public static $error = "";
protected static $_cachedResults = array();
/**
* for Prestashop 1.4 translation
* @var piwikanalyticsjs
*/
public static $_module = null;
/**
* prefix to use for configurations values
*/
const CPREFIX = "PIWIK_";
const FAKEUSERAGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0 (Fake Useragent from CLASS:PKHelper.php)";
public static $httpAuthUsername = "";
public static $httpAuthPassword = "";
public static $piwikHost = "";
/**
* create a log of all events if set to "1", usefull if tracking not working
* Log debug == 1
* DO NOT log == 0
* log will be saved to [PS ROOT]/log/YYYYMMDD_piwik.debug.log
*/
const DEBUGLOG = 1;
/** @var FileLogger */
private static $_debug_logger = NULL;
/** @var FileLogger */
private static $_error_logger = NULL;
/**
* logs message to [PS ROOT]/log/YYYYMMDD_piwik.error.log
* @param string $message
*/
public static function ErrorLogger($message) {
if (self::$_error_logger == NULL) {
self::$_error_logger = new FileLogger(FileLogger::ERROR);
self::$_error_logger->setFilename(_PS_ROOT_DIR_ . '/log/' . date('Ymd') . '_piwik.error.log');
}
self::$_error_logger->logError($message);
}
/**
* logs message to [PS ROOT]/log/YYYYMMDD_piwik.debug.log
* @param string $message
*/
public static function DebugLogger($message) {
if (PKHelper::DEBUGLOG != 1)
return;
if (self::$_debug_logger == NULL) {
self::$_debug_logger = new FileLogger(FileLogger::DEBUG);
self::$_debug_logger->setFilename(_PS_ROOT_DIR_ . '/log/' . date('Ymd') . '_piwik.debug.log');
}
self::$_debug_logger->logDebug($message);
}
/**
*
* @param type $idSite
* @param type $siteName
* @param array $urls
* @param type $ecommerce
* @param type $siteSearch
* @param type $searchKeywordParameters
* @param type $searchCategoryParameters
* @param type $excludedIps
* @param type $excludedQueryParameters
* @param type $timezone
* @param type $currency
* @param type $group
* @param type $startDate
* @param type $excludedUserAgents
* @param type $keepURLFragments
* @param type $type
* @return boolean
*/
public static function updatePiwikSite($idSite, $siteName = NULL, $urls = NULL, $ecommerce = NULL, $siteSearch = NULL, $searchKeywordParameters = NULL, $searchCategoryParameters = NULL, $excludedIps = NULL, $excludedQueryParameters = NULL, $timezone = NULL, $currency = NULL, $group = NULL, $startDate = NULL, $excludedUserAgents = NULL, $keepURLFragments = NULL, $type = NULL) {
if (!self::baseTest() || ($idSite <= 0))
return false;
$url = self::getBaseURL($idSite);
$url .= "&method=SitesManager.updateSite&format=JSON";
if ($siteName !== NULL)
$url .= "&siteName=" . urlencode($siteName);
if ($urls !== NULL) {
foreach (explode(',', $urls) as $value) {
$url .= "&urls[]=" . urlencode(trim($value));
}
}
if ($ecommerce !== NULL)
$url .= "&ecommerce=" . urlencode($ecommerce);
if ($siteSearch !== NULL)
$url .= "&siteSearch=" . urlencode($siteSearch);
if ($searchKeywordParameters !== NULL)
$url .= "&searchKeywordParameters=" . urlencode($searchKeywordParameters);
if ($searchCategoryParameters !== NULL)
$url .= "&searchCategoryParameters=" . urlencode($searchCategoryParameters);
if ($excludedIps !== NULL)
$url .= "&excludedIps=" . urlencode($excludedIps);
if ($excludedQueryParameters !== NULL)
$url .= "&excludedQueryParameters=" . urlencode($excludedQueryParameters);
if ($timezone !== NULL)
$url .= "&timezone=" . urlencode($timezone);
if ($currency !== NULL)
$url .= "¤cy=" . urlencode($currency);
if ($group !== NULL)
$url .= "&group=" . urlencode($group);
if ($startDate !== NULL)
$url .= "&startDate=" . urlencode($startDate);
if ($excludedUserAgents !== NULL)
$url .= "&excludedUserAgents=" . urlencode($excludedUserAgents);
if ($keepURLFragments !== NULL)
$url .= "&keepURLFragments=" . urlencode($keepURLFragments);
if ($type !== NULL)
$url .= "&type=" . urlencode($type);
if ($result = self::getAsJsonDecoded($url)) {
$url2 = self::getBaseURL($idSite) . "&method=SitesManager.getSiteFromId&format=JSON";
unset(self::$_cachedResults[md5($url2)]); // Clear cache for updated site
return ($result->result == 'success' && $result->message == 'ok' ? TRUE : ($result->result != 'success' ? $result->message : FALSE));
} else
return FALSE;
}
/**
* get all website groups
* @return array|boolean
*/
public static function getSitesGroups() {
if (!self::baseTest())
return FALSE;
$url = self::getBaseURL();
$url .= "&method=SitesManager.getSitesGroups&format=JSON";
if ($result = self::getAsJsonDecoded($url))
return $result;
else
return FALSE;
}
/**
* get users token auth from Piwik
* NOTE: password is required either an md5 encoded password or a normal string
* @param string $userLogin the user name
* @param string $password password in clear text
* @param string $md5Password md5 encoded password
* @return string|boolean
*/
public static function getTokenAuth($userLogin, $password = NULL, $md5Password = NULL) {
if ($password === null || empty($password)) {
$password = $md5Password;
if ($md5Password === NULL || empty($md5Password)) {
self::$error = self::l('A password is required for method PKHelper::getTokenAuth()!');
self::$errors[] = self::$error;
return FALSE;
}
} else
$password = md5($password);
$url = self::getBaseURL(0, NULL, NULL, 'API', NULL, '');
$url .= "&method=UsersManager.getTokenAuth&userLogin={$userLogin}&md5Password={$password}&format=JSON";
if ($result = self::getAsJsonDecoded($url)) {
if (isset($result->result)) {
self::$error = $result->message;
self::$errors[] = self::$error;
}
return isset($result->value) ? $result->value : FALSE;
} else
return FALSE;
}
/**
* get image tracking code for use with or without proxy script
* @return array
*/
public static function getPiwikImageTrackingCode() {
$ret = array(
'default' => self::l('I need Site ID and Auth Token before i can get your image tracking code'),
'proxy' => self::l('I need Site ID and Auth Token before i can get your image tracking code')
);
$idSite = (int) Configuration::get(PKHelper::CPREFIX . 'SITEID');
if (!self::baseTest() || ($idSite <= 0))
return $ret;
$url = self::getBaseURL();
$url .= "&method=SitesManager.getImageTrackingCode&format=JSON&actionName=NoJavaScript";
$url .= "&piwikUrl=" . urlencode(rtrim(Configuration::get(PKHelper::CPREFIX . 'HOST'), '/'));
$md5Url = md5($url);
if (!isset(self::$_cachedResults[$md5Url])) {
if ($result = self::getAsJsonDecoded($url))
self::$_cachedResults[$md5Url] = $result;
else
self::$_cachedResults[$md5Url] = false;
}
if (self::$_cachedResults[$md5Url] !== FALSE) {
$ret['default'] = htmlentities('<noscript>' . self::$_cachedResults[$md5Url]->value . '</noscript>');
if ((bool) Configuration::get('PS_REWRITING_SETTINGS'))
$ret['proxy'] = str_replace(Configuration::get(PKHelper::CPREFIX . 'HOST') . 'piwik.php', Configuration::get(PKHelper::CPREFIX . 'PROXY_SCRIPT'), $ret['default']);
else
$ret['proxy'] = str_replace(Configuration::get(PKHelper::CPREFIX . 'HOST') . 'piwik.php?', Configuration::get(PKHelper::CPREFIX . 'PROXY_SCRIPT') . '&', $ret['default']);
}
return $ret;
}
/**
* get Piwik site based on the current settings in the configuration
* @return stdClass[]
*/
public static function getPiwikSite($idSite = 0) {
if ($idSite == 0)
$idSite = (int) Configuration::get(PKHelper::CPREFIX . 'SITEID');
if (!self::baseTest() || ($idSite <= 0))
return false;
$url = self::getBaseURL($idSite);
$url .= "&method=SitesManager.getSiteFromId&format=JSON";
$md5Url = md5($url);
if (!isset(self::$_cachedResults[$md5Url])) {
if ($result = self::getAsJsonDecoded($url))
self::$_cachedResults[$md5Url] = $result;
else
self::$_cachedResults[$md5Url] = false;
}
if (self::$_cachedResults[$md5Url] !== FALSE) {
if (isset(self::$_cachedResults[$md5Url]->result) && self::$_cachedResults[$md5Url]->result == 'error') {
self::$error = self::$_cachedResults[$md5Url]->message;
self::$errors[] = self::$error;
return false;
}
if (!isset(self::$_cachedResults[$md5Url][0])) {
return false;
}
if (((bool) self::$_cachedResults[$md5Url][0]->ecommerce === false) || self::$_cachedResults[$md5Url][0]->ecommerce == 0) {
// if ((_PS_VERSION_ < '1.5'))
// self::$error = self::l('E-commerce is not active for your site in piwik!');
// else
self::$error = self::l('E-commerce is not active for your site in piwik!, you can enable it in the advanced settings on this page');
self::$errors[] = self::$error;
}
if (((bool) self::$_cachedResults[$md5Url][0]->sitesearch) === false || self::$_cachedResults[$md5Url][0]->sitesearch == 0) {
// if ((_PS_VERSION_ < '1.5'))
// self::$error = self::l('Site search is not active for your site in piwik!');
// else
self::$error = self::l('Site search is not active for your site in piwik!, you can enable it in the advanced settings on this page');
self::$errors[] = self::$error;
}
return self::$_cachedResults[$md5Url];
}
return false;
}
public static function getPiwikSite2($idSite = 0) {
if ($idSite == 0)
$idSite = (int) Configuration::get(PKHelper::CPREFIX . 'SITEID');
if ($result = self::getPiwikSite($idSite)) {
$url = self::getBaseURL($idSite);
$url .= "&method=SitesManager.getSiteUrlsFromId&format=JSON";
if ($resultUrls = self::getAsJsonDecoded($url)) {
$result[0]->main_url = implode(',', $resultUrls);
}
return $result;
}
return false;
}
/**
* get all supported time zones from piwik
* @return array
*/
public static function getTimezonesList() {
if (!self::baseTest())
return array();
$url = self::getBaseURL();
$url .= "&method=SitesManager.getTimezonesList&format=JSON";
$md5Url = md5($url);
if (!isset(self::$_cachedResults[$md5Url])) {
if ($result = self::getAsJsonDecoded($url))
self::$_cachedResults[$md5Url] = $result;
else
self::$_cachedResults[$md5Url] = array();
}
return self::$_cachedResults[$md5Url];
}
public static function getSitesWithViewAccess() {
if (!self::baseTest())
return array();
$url = self::getBaseURL();
$url .= "&method=SitesManager.getSitesWithViewAccess&format=JSON";
$md5Url = md5($url);
if (!isset(self::$_cachedResults[$md5Url])) {
if ($result = self::getAsJsonDecoded($url))
self::$_cachedResults[$md5Url] = $result;
else
self::$_cachedResults[$md5Url] = array();
}
return self::$_cachedResults[$md5Url];
}
/**
* Alias of PKHelper::getSitesWithAdminAccess()
* get all Piwik sites the current authentication token has admin access to
* @param boolean $fetchAliasUrls
* @return stdClass[]
*/
public static function getMyPiwikSites($fetchAliasUrls = false) {
return self::getSitesWithAdminAccess($fetchAliasUrls);
}
/**
* get all Piwik sites the current authentication token has admin access to
* @param boolean $fetchAliasUrls
* @return stdClass[]
*/
public static function getSitesWithAdminAccess($fetchAliasUrls = false, $getBaseURLParams = NULL) {
if (!self::baseTest())
return array();
if ($getBaseURLParams == NULL && !is_array($getBaseURLParams) || !(is_array($getBaseURLParams) && count($getBaseURLParams) == 6))
$url = self::getBaseURL();
else {
extract($getBaseURLParams, EXTR_OVERWRITE);
$url = self::getBaseURL($idSite, $pkHost, $https, $pkModule, $isoCode, $tokenAuth);
}
$url .= "&method=SitesManager.getSitesWithAdminAccess&format=JSON" . ($fetchAliasUrls ? '&fetchAliasUrls=1' : '');
$md5Url = md5($url);
if (!isset(self::$_cachedResults[$md5Url . "2"])) {
if ($result = self::getAsJsonDecoded($url))
self::$_cachedResults[$md5Url] = $result;
else
self::$_cachedResults[$md5Url] = array();
}
return self::$_cachedResults[$md5Url];
}
/**
* get all Piwik siteIDs the current authentication token has admin access to
* @return array
*/
public static function getMyPiwikSiteIds() {
if (!self::baseTest())
return array();
$url = self::getBaseURL();
$url .= "&method=SitesManager.getSitesIdWithAdminAccess&format=JSON";
$md5Url = md5($url);
if (!isset(self::$_cachedResults[$md5Url])) {
if ($result = self::getAsJsonDecoded($url))
self::$_cachedResults[$md5Url] = $result;
else
self::$_cachedResults[$md5Url] = array();
}
return self::$_cachedResults[$md5Url];
}
/**
* get the base url for all requests to Piwik
* @param integer $idSite
* @param string $pkHost
* @param boolean $https
* @param string $pkModule
* @param string $isoCode
* @param string $tokenAuth
* @return string
*/
protected static function getBaseURL($idSite = NULL, $pkHost = NULL, $https = NULL, $pkModule = 'API', $isoCode = NULL, $tokenAuth = NULL) {
if ($https === NULL)
$https = (bool) Configuration::get(PKHelper::CPREFIX . 'CRHTTPS');
if (self::$piwikHost == "" || self::$piwikHost === false)
self::$piwikHost = Configuration::get(PKHelper::CPREFIX . 'HOST');
if ($pkHost === NULL)
$pkHost = self::$piwikHost;
if ($isoCode === NULL)
$isoCode = strtolower((isset(Context::getContext()->language->iso_code) ? Context::getContext()->language->iso_code : 'en'));
if ($idSite === NULL)
$idSite = Configuration::get(PKHelper::CPREFIX . 'SITEID');
if ($tokenAuth === NULL)
$tokenAuth = Configuration::get(PKHelper::CPREFIX . 'TOKEN_AUTH');
return ($https ? 'https' : 'http') . "://{$pkHost}index.php?module={$pkModule}&language={$isoCode}&idSite={$idSite}&token_auth={$tokenAuth}";
}
/**
* check if the basics are there before we make any piwik requests
* @return boolean
*/
protected static function baseTest() {
static $_error1 = FALSE;
$pkToken = Configuration::get(PKHelper::CPREFIX . 'TOKEN_AUTH');
$pkHost = Configuration::get(PKHelper::CPREFIX . 'HOST');
if (empty($pkToken) || empty($pkHost)) {
if (!$_error1) {
self::$error = self::l('Piwik auth token and/or Piwik site id cannot be empty');
self::$errors[] = self::$error;
$_error1 = TRUE;
}
return false;
}
return true;
}
/**
* get output of api as json decoded object
* @param string $url the full http(s) url to use for fetching the api result
* @return boolean
*/
protected static function getAsJsonDecoded($url) {
static $_error2 = FALSE;
$use_cURL = (bool) Configuration::get(PKHelper::CPREFIX . 'USE_CURL');
$getF = self::get_http($url);
if ($getF !== FALSE) {
return Tools::jsonDecode($getF);
}
return FALSE;
}
public static function get_http($url, $headers = array()) {
static $_error2 = FALSE;
PKHelper::DebugLogger('START: PKHelper::get_http(' . $url . ',' . print_r($headers, true) . ')');
// class: Context is not loaded when using piwik.php proxy on prestashop 1.4
if (class_exists('Context', FALSE))
$lng = strtolower((isset(Context::getContext()->language->iso_code) ? Context::getContext()->language->iso_code : 'en'));
else
$lng = 'en';
$timeout = 5; // should go in module conf
if (self::$httpAuthUsername == "" || self::$httpAuthUsername === false)
self::$httpAuthUsername = Configuration::get(PKHelper::CPREFIX . 'PAUTHUSR');
if (self::$httpAuthPassword == "" || self::$httpAuthPassword === false)
self::$httpAuthPassword = Configuration::get(PKHelper::CPREFIX . 'PAUTHPWD');
$httpauth_usr = self::$httpAuthUsername;
$httpauth_pwd = self::$httpAuthPassword;
$use_cURL = (bool) Configuration::get(PKHelper::CPREFIX . 'USE_CURL');
if ($use_cURL === FALSE) {
PKHelper::DebugLogger('Using \'file_get_contents\' to fetch remote');
$httpauth = "";
if ((!empty($httpauth_usr) && !is_null($httpauth_usr) && $httpauth_usr !== false) && (!empty($httpauth_pwd) && !is_null($httpauth_pwd) && $httpauth_pwd !== false)) {
$httpauth = "Authorization: Basic " . base64_encode("$httpauth_usr:$httpauth_pwd") . "\r\n";
}
$options = array(
'http' => array(
'user_agent' => (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : PKHelper::FAKEUSERAGENT),
'method' => "GET",
'timeout' => $timeout,
'header' => (!empty($headers) ? implode('', $headers) : "Accept-language: {$lng}\r\n") . $httpauth
)
);
$context = stream_context_create($options);
PKHelper::DebugLogger('Calling: ' . $url . (!empty($httpauth) ? "\n\t- With Http auth" : ""));
$result = @file_get_contents($url, false, $context);
if ($result === FALSE) {
$http_response = "";
if (isset($http_response_header) && is_array($http_response_header)) {
foreach ($http_response_header as $value) {
if (preg_match("/^HTTP\/.*/i", $value)) {
$http_response = ':' . $value;
}
}
}
PKHelper::DebugLogger('request returned ERROR: http response: ' . $http_response);
if (isset($http_response_header))
PKHelper::DebugLogger('$http_response_header: ' . print_r($http_response_header, true));
if (!$_error2) {
self::$error = sprintf(self::l('Unable to connect to api%s'), " {$http_response}");
self::$errors[] = self::$error;
$_error2 = TRUE;
PKHelper::DebugLogger('Last error message: ' . self::$error);
}
} else {
PKHelper::DebugLogger('request returned OK');
}
PKHelper::DebugLogger('END: PKHelper::get_http(): OK');
return $result;
} else {
PKHelper::DebugLogger('Using \'cURL\' to fetch remote');
try {
$ch = curl_init();
PKHelper::DebugLogger("\t: \$ch = curl_init()");
curl_setopt($ch, CURLOPT_URL, $url);
PKHelper::DebugLogger("\t: curl_setopt(\$ch, CURLOPT_URL, $url)");
// @TODO make this work, but how to filter out the headers from returned result??
//curl_setopt($ch, CURLOPT_HEADER, 1);
(!empty($headers) ?
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers) :
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-language: {$lng}\r\n"))
);
PKHelper::DebugLogger("\t: curl_setopt(\$ch, CURLOPT_HTTPHEADER, array(...))");
curl_setopt($ch, CURLOPT_USERAGENT, (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : PKHelper::FAKEUSERAGENT));
if ((!empty($httpauth_usr) && !is_null($httpauth_usr) && $httpauth_usr !== false) && (!empty($httpauth_pwd) && !is_null($httpauth_pwd) && $httpauth_pwd !== false))
curl_setopt($ch, CURLOPT_USERPWD, $httpauth_usr . ":" . $httpauth_pwd);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPGET, 1); // just to be safe
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if (($return = curl_exec($ch)) === false) {
if (!$_error2) {
self::$error = curl_error($ch);
self::$errors[] = self::$error;
$_error2 = TRUE;
}
$return = false;
}
curl_close($ch);
PKHelper::DebugLogger('END: PKHelper::get_http(): OK');
return $return;
} catch (Exception $ex) {
self::$errors[] = $ex->getMessage();
PKHelper::DebugLogger('Exception: ' . $ex->getMessage());
PKHelper::DebugLogger('END: PKHelper::get_http(): ERROR');
return false;
}
}
}
/**
* @see Module::l
*/
private static function l($string, $specific = false) {
if (version_compare(_PS_VERSION_, '1.5.0.13', "<="))
return PKHelper::$_module->l($string, ($specific) ? $specific : 'pkhelper');
return Translate::getModuleTranslation('piwikanalyticsjs', $string, ($specific) ? $specific : 'pkhelper');
// the following lines are need for the translation to work properly
// $this->l('I need Site ID and Auth Token before i can get your image tracking code')
// $this->l('E-commerce is not active for your site in piwik!, you can enable it in the advanced settings on this page')
// $this->l('Site search is not active for your site in piwik!, you can enable it in the advanced settings on this page')
// $this->l('Unable to connect to api %s')
// $this->l('E-commerce is not active for your site in piwik!')
// $this->l('Site search is not active for your site in piwik!')
// $this->l('A password is required for method PKHelper::getTokenAuth()!')
}
/**
* get websites by group
* NOTE: Not tested not in use by this module but here for the future, and may be removed.!
* @param string $group
* @return array|boolean
*/
public static function getSitesFromGroup($group) {
if (!self::baseTest())
return FALSE;
$url = self::getBaseURL();
$url .= "&method=SitesManager.getSitesFromGroup&format=JSON&group=" . urlencode($group);
if ($result = self::getAsJsonDecoded($url))
return $result;
else
return FALSE;
}
/**
* rename websites group
* NOTE: Not tested not in use by this module but here for the future, and may be removed.!
* @param string $oldGroupName
* @param string $newGroupName
* @return array|boolean
*/
public static function renameGroup($oldGroupName, $newGroupName) {
if (!self::baseTest())
return FALSE;
$url = self::getBaseURL();
$url .= "&method=SitesManager.getSitesFromGroup&format=JSON"
. "&oldGroupName=" . urlencode($oldGroupName)
. "&newGroupName=" . urlencode($newGroupName);
if ($result = self::getAsJsonDecoded($url))
return $result;
else
return FALSE;
}
}