1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr7Middlewares\Middleware; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr7Middlewares\Utils; |
9
|
|
|
|
10
|
|
|
class Piwik |
11
|
|
|
{ |
12
|
|
|
use Utils\HtmlInjectorTrait; |
13
|
|
|
|
14
|
|
|
private $options = [ |
15
|
|
|
['trackPageView'], |
16
|
|
|
['enableLinkTracking'], |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int|null The site's ID |
21
|
|
|
*/ |
22
|
|
|
private $siteId = 1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|null The Piwik url |
26
|
|
|
*/ |
27
|
|
|
private $piwikUrl; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor.Set the Piwik's url. |
31
|
|
|
* |
32
|
|
|
* @param string $piwikUrl |
33
|
|
|
*/ |
34
|
|
|
public function __construct($piwikUrl = null) |
35
|
|
|
{ |
36
|
|
|
if ($piwikUrl !== null) { |
37
|
|
|
$this->piwikUrl($piwikUrl); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set the site's id. |
43
|
|
|
* |
44
|
|
|
* @param int $siteId |
45
|
|
|
* |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
|
|
public function siteId($siteId) |
49
|
|
|
{ |
50
|
|
|
$this->siteId = $siteId; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the piwik url. |
57
|
|
|
* |
58
|
|
|
* @param string $url |
59
|
|
|
* |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
|
|
public function piwikUrl($url) |
63
|
|
|
{ |
64
|
|
|
$this->piwikUrl = (string) $url; |
65
|
|
|
|
66
|
|
|
//ensure the url is ending by "/" |
67
|
|
|
if (substr($this->piwikUrl, -1) !== '/') { |
68
|
|
|
$this->piwikUrl .= '/'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add an option. |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
* ... |
79
|
|
|
* |
80
|
|
|
* @return self |
81
|
|
|
*/ |
82
|
|
|
public function addOption($name) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$this->options[] = func_get_args(); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Execute the middleware. |
91
|
|
|
* |
92
|
|
|
* @param ServerRequestInterface $request |
93
|
|
|
* @param ResponseInterface $response |
94
|
|
|
* @param callable $next |
95
|
|
|
* |
96
|
|
|
* @return ResponseInterface |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$response = $next($request, $response); |
101
|
|
|
|
102
|
|
|
if ($this->isInjectable($request)) { |
103
|
|
|
return $this->inject($response, $this->getCode()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $response; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns the piwik code. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
private function getCode() |
115
|
|
|
{ |
116
|
|
|
$_paq = ''; |
117
|
|
|
|
118
|
|
|
foreach ($this->options as $option) { |
119
|
|
|
$option[0] = "'".$option[0]."'"; |
120
|
|
|
$_paq .= sprintf('_paq.push([%s]);', implode(',', $option)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return <<<PWK |
124
|
|
|
<script> |
125
|
|
|
var _paq = _paq || []; |
126
|
|
|
{$_paq} |
127
|
|
|
(function() { |
128
|
|
|
var u="{$this->piwikUrl}"; |
129
|
|
|
_paq.push(['setTrackerUrl', u+'piwik.php']); |
130
|
|
|
_paq.push(['setSiteId', {$this->siteId}]); |
131
|
|
|
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; |
132
|
|
|
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); |
133
|
|
|
})(); |
134
|
|
|
</script> |
135
|
|
|
<noscript><p><img src="{$this->piwikUrl}piwik.php?idsite={$this->siteId}" style="border:0;" alt="" /></p></noscript> |
136
|
|
|
PWK; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.