1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* Common use: |
6
|
|
|
* ```php |
7
|
|
|
* CountryPrices_ChangeCountryController::changeto('XX'); |
8
|
|
|
* CountryPrices_ChangeCountryController::new_country_link('XX'); |
9
|
|
|
* ``` |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class CountryPrices_ChangeCountryController extends ContentController |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* make sure to match route... |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private static $url_segment = 'shoppingcart-countries'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* needs to be saved like this: |
24
|
|
|
* ZA => 'myshop.co.za' |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $off_site_url_redirects = array(); |
|
|
|
|
29
|
|
|
|
30
|
|
|
private static $allowed_actions = array( |
|
|
|
|
31
|
|
|
"changeto" => true, |
32
|
|
|
"confirmredirection" => true |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* only call this function if it is a NEW country we are dealing with! |
37
|
|
|
* |
38
|
|
|
* @param string $newCountryCode |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
|
|
public static function set_new_country($newCountryCode) |
42
|
|
|
{ |
43
|
|
|
$newCountryCode = strtoupper($newCountryCode); |
44
|
|
|
|
45
|
|
|
Session::set('MyCloudFlareCountry', $newCountryCode); |
46
|
|
|
$o = Shoppingcart::current_order(); |
47
|
|
|
if ($o && ($o->getCountry() == $newCountryCode)) { |
|
|
|
|
48
|
|
|
//.. |
49
|
|
|
} else { |
50
|
|
|
ShoppingCart::singleton()->clear(); |
51
|
|
|
} |
52
|
|
|
CountryPrice_OrderDOD::localise_order($newCountryCode, true); |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* link to change to a new country. |
58
|
|
|
* |
59
|
|
|
* @param string $newCountryCode |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
|
public static function new_country_link($newCountryCode) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$newCountryCode = strtoupper($newCountryCode); |
65
|
|
|
$redirectsArray = Config::inst()->get('CountryPrices_ChangeCountryController', 'off_site_url_redirects'); |
66
|
|
|
if (isset($redirectsArray[$newCountryCode])) { |
67
|
|
|
return $redirectsArray[$newCountryCode]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return Injector::inst()->get('CountryPrices_ChangeCountryController')->Link('changeto/'.strtolower($newCountryCode).'/'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* |
75
|
|
|
* @param SS_HTTPRequest $request |
76
|
|
|
* |
77
|
|
|
* @return SS_HTTPResponse |
|
|
|
|
78
|
|
|
*/ |
79
|
|
|
public function changeto($request) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
//check for offsite redirects??? |
|
|
|
|
82
|
|
|
$newCountryCode = strotoupper($request->param('ID')); |
83
|
|
|
self::set_new_country($newCountryCode); |
84
|
|
|
|
85
|
|
|
//redirect now |
86
|
|
|
$param = Config::inst()->get('CountryPrice_Translation', 'locale_get_parameter'); |
87
|
|
|
if(isset($_GET['force']) && $_GET['force']) { |
88
|
|
|
|
89
|
|
|
return $this->redirect(self::$url_segment . '/changeto/' .$newCountryCode . '/'. '?force-back-home'); |
90
|
|
|
} |
91
|
|
|
if(isset($_GET['force-back-home']) || $_GET['force-back-home']) { |
92
|
|
|
|
93
|
|
|
return $this->redirect('/'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->redirect($this->findNewURL($param, $newCountryCode)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function Link($action = null) |
100
|
|
|
{ |
101
|
|
|
return Controller::join_links(Config::inst()->get('CountryPrices_ChangeCountryController', 'url_segment'), $action); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Remove a query string parameter from an URL. |
106
|
|
|
* |
107
|
|
|
* @param string $varname |
108
|
|
|
* @param string $newCountryCode |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function findNewURL($varname = 'ecomlocale', $newCountryCode) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
|
115
|
|
|
//COPIED FROM DIRECTOR::redirectBack() |
116
|
|
|
// Don't cache the redirect back ever |
117
|
|
|
HTTP::set_cache_age(0); |
118
|
|
|
|
119
|
|
|
$url = null; |
120
|
|
|
|
121
|
|
|
// In edge-cases, this will be called outside of a handleRequest() context; in that case, |
122
|
|
|
// redirect to the homepage - don't break into the global state at this stage because we'll |
123
|
|
|
// be calling from a test context or something else where the global state is inappropraite |
124
|
|
|
if ($this->getRequest()) { |
125
|
|
|
if ($this->getRequest()->requestVar('BackURL')) { |
126
|
|
|
$url = $this->getRequest()->requestVar('BackURL'); |
127
|
|
|
} elseif ($this->getRequest()->isAjax() && $this->getRequest()->getHeader('X-Backurl')) { |
128
|
|
|
$url = $this->getRequest()->getHeader('X-Backurl'); |
129
|
|
|
} elseif ($this->getRequest()->getHeader('Referer')) { |
130
|
|
|
$url = $this->getRequest()->getHeader('Referer'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (!$url) { |
135
|
|
|
$url = Director::baseURL(); |
136
|
|
|
} |
137
|
|
|
// absolute redirection URLs not located on this site may cause phishing |
138
|
|
|
if (Director::is_site_url($url)) { |
139
|
|
|
$url = Director::absoluteURL($url, true); |
140
|
|
|
$parsedUrl = parse_url($url); |
141
|
|
|
$query = array(); |
142
|
|
|
|
143
|
|
|
if (isset($parsedUrl['query'])) { |
144
|
|
|
parse_str($parsedUrl['query'], $query); |
145
|
|
|
if ($query[$varname] !== $newCountryCode) { |
146
|
|
|
unset($query[$varname]); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$path = isset($parsedUrl['path']) ? $parsedUrl['path'] : ''; |
|
|
|
|
151
|
|
|
|
152
|
|
|
$query = !empty($query) ? '?'. http_build_query($query) : ''; |
153
|
|
|
|
154
|
|
|
$hasCountrySegment = CountryPrice_Translation::get_country_url_provider()->hasCountrySegment($url); |
155
|
|
|
if($hasCountrySegment){ |
156
|
|
|
$url = CountryPrice_Translation::get_country_url_provider()->replaceCountryCodeInUrl($newCountryCode, $url); |
157
|
|
|
} |
158
|
|
|
else { |
159
|
|
|
$url = CountryPrice_Translation::get_country_url_provider()->addCountryCodeToUrl($newCountryCode, $url); |
160
|
|
|
} |
161
|
|
|
return $url.$query; |
162
|
|
|
} |
163
|
|
|
return '/'; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function confirmredirection($request) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.