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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Referrers;
use Piwik\Cache;
use Piwik\Common;
use Piwik\Config;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\SettingsPiwik;
use Piwik\Singleton;
/**
* Contains methods to access search engine definition data.
*/
class Social extends Singleton
{
const OPTION_STORAGE_NAME = 'SocialDefinitions';
/** @var string location of definition file (relative to PIWIK_INCLUDE_PATH) */
const DEFINITION_FILE = '/vendor/matomo/searchengine-and-social-list/Socials.yml';
protected $definitionList = null;
/**
* Returns list of search engines by URL
*
* @return array Array of ( URL => array( searchEngineName, keywordParameter, path, charset ) )
*/
public function getDefinitions()
{
$cache = Cache::getEagerCache();
$cacheId = 'Social-' . self::OPTION_STORAGE_NAME;
if ($cache->contains($cacheId)) {
$list = $cache->fetch($cacheId);
} else {
$list = $this->loadDefinitions();
$cache->save($cacheId, $list);
}
return $list;
}
private function loadDefinitions()
{
if ($this->definitionList === null) {
$referrerDefinitionSyncOpt = Config::getInstance()->General['enable_referrer_definition_syncs'];
if( $referrerDefinitionSyncOpt == 1) {
$this->loadRemoteDefinitions();
} else {
$this->loadLocalYmlData();
}
}
Piwik::postEvent('Referrer.addSocialUrls', array(&$this->definitionList));
return $this->definitionList;
}
/**
* Loads definitions sourced from remote yaml with a local fallback
*/
private function loadRemoteDefinitions()
{
// Read first from the auto-updated list in database
$list = Option::get(self::OPTION_STORAGE_NAME);
if ($list && SettingsPiwik::isInternetEnabled()) {
$this->definitionList = Common::safe_unserialize(base64_decode($list));
} else {
// Fallback to reading the bundled list
$this->loadLocalYmlData();
Option::set(self::OPTION_STORAGE_NAME, base64_encode(serialize($this->definitionList)));
}
}
/**
* Loads the definition data from the local definitions file
*/
private function loadLocalYmlData()
{
$yml = file_get_contents(PIWIK_INCLUDE_PATH . self::DEFINITION_FILE);
$this->definitionList = $this->loadYmlData($yml);
}
/**
* Parses the given YML string and caches the resulting definitions
*
* @param string $yml
* @return array
*/
public function loadYmlData($yml)
{
$searchEngines = \Spyc::YAMLLoadString($yml);
$this->definitionList = $this->transformData($searchEngines);
return $this->definitionList;
}
protected function transformData($socials)
{
$urlToName = array();
foreach ($socials as $name => $urls) {
if (empty($urls) || !is_array($urls)) {
continue;
}
foreach ($urls as $url) {
$urlToName[$url] = $name;
}
}
return $urlToName;
}
/**
* Returns true if a URL belongs to a social network, false if otherwise.
*
* @param string $url The URL to check.
* @param string|bool $socialName The social network's name to check for, or false to check
* for any.
* @return bool
*/
public function isSocialUrl($url, $socialName = false)
{
foreach ($this->getDefinitions() as $domain => $name) {
if (preg_match('/(^|[\.\/])'.$domain.'([\.\/]|$)/', $url) && ($socialName === false || $name == $socialName)) {
return true;
}
}
return false;
}
/**
* Gets social network name from URL.
*
* @param string $url
* @return string
*/
public function getSocialNetworkFromDomain($url)
{
foreach ($this->getDefinitions() as $domain => $name) {
if (preg_match('/(^|[\.\/])'.$domain.'([\.\/]|$)/', $url)) {
return $name;
}
}
return Piwik::translate('General_Unknown');
}
/**
* Returns the main url of the social network the given url matches
*
* @param string $url
*
* @return string
*/
public function getMainUrl($url)
{
$social = $this->getSocialNetworkFromDomain($url);
foreach ($this->getDefinitions() as $domain => $name) {
if ($name == $social) {
return $domain;
}
}
return $url;
}
/**
* Returns the main url of the given social network
*
* @param string $social
*
* @return string
*/
public function getMainUrlFromName($social)
{
foreach ($this->getDefinitions() as $domain => $name) {
if ($name == $social) {
return $domain;
}
}
return null;
}
/**
* Return social network logo path by URL
*
* @param string $domain
* @return string path
* @see plugins/Morpheus/icons/dist/socials/
*/
public function getLogoFromUrl($domain)
{
$social = $this->getSocialNetworkFromDomain($domain);
$socialNetworks = $this->getDefinitions();
$filePattern = 'plugins/Morpheus/icons/dist/socials/%s.png';
$socialDomains = array_keys($socialNetworks, $social);
foreach ($socialDomains as $domain) {
if (file_exists(PIWIK_INCLUDE_PATH . '/' . sprintf($filePattern, $domain))) {
return sprintf($filePattern, $domain);
}
}
return sprintf($filePattern, 'xx');
}
}
|