/
extension.driver.php
128 lines (104 loc) · 4.57 KB
/
extension.driver.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
<?php
Class extension_frontend_tracking extends Extension{
public static $xml;
public function getSubscribedDelegates(){
return array(
// Used for adding the nonce
array(
'page' => '/frontend/',
'delegate' => 'FrontendParamsResolve',
'callback' => 'addParams',
),
array(
'page' => '/frontend/',
'delegate' => 'EventPreSaveFilter',
'callback' => 'eventPreSaveFilter',
)
);
}
/**
* Add the custom parameters before entry save. This will ensure we can track without passing data with post
*/
public function eventPreSaveFilter($context){
include_once dirname(__FILE__) . '/lib/geoip/geoip.inc';
$gi = geoip_open(dirname(__FILE__) . '/lib/geoip/data/GeoIP.dat',GEOIP_STANDARD);
// Visitor's IP
$context['fields']['ip'] = $_SERVER['REMOTE_ADDR'];
// Visitor's Country
$context['fields']['country'] = strtolower(geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']));
geoip_close($gi);
//to replace with Symphony Cookie
$referer = new Cookie('referer', $validFor , __SYM_COOKIE_PATH__, NULL, false);
$context['fields']['referer'] = $referer->get('referer');
//check if a url is being passed
if (empty($context['fields']['url']))
$context['fields']['url'] = $_SERVER['HTTP_REFERER'];
//check if a url is being passed
if (empty($context['fields']['language']))
$context['fields']['language'] = FLang::getLang();
if (empty($context['fields']['website'])){
$page = Frontend::Page();
$context['fields']['website'] =$page->_param['root'];
}
}
/**
* Add custom params to pool
*/
public function addParams($context){
$hostname = exec('hostname');
include_once dirname(__FILE__) . '/lib/geoip/geoip.inc';
$gi = geoip_open(dirname(__FILE__) . '/lib/geoip/data/GeoIP.dat',GEOIP_STANDARD);
// Server Status
$context['params']['server-status'] = Symphony::Configuration()->get('status', 'server');
// Client's IP
$context['params']['client-ip'] = $_SERVER['REMOTE_ADDR'];
// $context['params']['client-ip'] = '195.158.105.159';
$context['params']['country-code'] = geoip_country_code_by_addr($gi, $context['params']['client-ip']);
if (empty($context['params']['country-code'])){
$context['params']['country-code'] ='MT'; //to be replaced with Default Country
}
// $context['params']['country-name'] = 'switzerland';
geoip_close($gi);
$phoneCodes = include dirname(__FILE__) . '/lib/country/phone_codes.php';
//country list taken from https://github.com/umpirsky/country-list/tree/master/country/cldr
$country = include dirname(__FILE__) . '/lib/country/'.FLang::getLang().'/country.php';
$key = array_search(strtolower($context['params']['country-name']),array_map('strtolower',$country));
$context['params']['country-name'] = $country[$context['params']['country-code']];
$context['params']['country-phone-code'] = $phoneCodes[$context['params']['country-code']];
$this->setReferer($context);
Symphony::Profiler()->sample( 'Frontend Params Complete on: ' . $hostname, PROFILE_LAP);
}
private function setReferer($context){
// Look for an provcode and save in session
// NOTE The empty string (or 0) will not match, resulting in the HTTP_REFERER being used - this is intentional!
$ref_url_param_name = 'ref';
$ref_url_param_value = $context['params']["url-{$ref_url_param_name}"];
$validFor = (30 * 60); // should use a config option to set the validity length
$referer = new Cookie('referer', $validFor , __SYM_COOKIE_PATH__, NULL, false);
$old_referer = $referer->get('referer');
if (!empty($ref_url_param_value)) {
// We have a code via URL param: ...?ref=12345
$referer->set('referer',"Ref Code: {$ref_url_param_value} | Referer: {$_SERVER['HTTP_REFERER']} | First URL: {$context['params']['current-url']}");
} elseif (empty($old_referer) && $_SERVER['HTTP_REFERER']) {
// echo('test');
//in here we should somewhat separate organic searches
// Set to HTTP_REFERER, assuming that in session is empty
$referer->set('referer',"Referer: {$_SERVER['HTTP_REFERER']} | First URL: {$context['params']['current-url']}");
} elseif (empty($old_referer)) {
//otherwise set as direct
$referer->set('referer',"Direct | First URL: {$context['params']['current-url']}");
}
// Always make it a param in case a template needs it
$context['params']['referer'] = $referer->get('referer');
}
public function enable(){
return $this->install();
}
public function disable(){
}
public function install(){
}
public function uninstall(){
}
}
?>