forked from vbuck/rootd-link
/
Node.php
348 lines (280 loc) · 9.64 KB
/
Node.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
<?php
/**
* Link node model.
*
* @package Rootd_Link
* @author Rick Buczynski <me@rickbuczynski.com>
* @copyright 2014 Rick Buczynski. All Rights Reserved.
*/
class Rootd_Link_Model_Node
extends Mage_Core_Model_Abstract
{
const EVENT_VIEW = 'rootd_link_node_view';
/* @var $_attachment Rootd_Link_Model_Node_Attachment */
protected $_attachment;
/**
* Initialize the model.
*
* @return void
*/
protected function _construct()
{
$this->_init('link/node');
}
/**
* Save the attachment if requested.
*
* @return Rootd_Link_Model_Node
*/
protected function _beforeSave()
{
if ($this->getSaveAttachmentFlag()) {
$this->_getResource()->setAttachment($this);
}
return $this;
}
/**
* Check if a path is an absolute, external URL.
*
* @param string $path The input path.
*
* @return boolean
*/
protected function _getIsExternal($path = '')
{
$part = substr($path, 0, 6);
return $part === 'http:/' || $part === 'https:';
}
/**
* Get the query string from request.
*
* @return string|boolean
*/
protected function _getQueryString()
{
if (!empty($_SERVER['QUERY_STRING'])) {
$queryParams = array();
parse_str($_SERVER['QUERY_STRING'], $queryParams);
$hasChanges = false;
foreach ($queryParams as $key => $value) {
if (substr($key, 0, 3) === '___') {
unset($queryParams[$key]);
$hasChanges = true;
}
}
if ($hasChanges) {
return http_build_query($queryParams);
} else {
return $_SERVER['QUERY_STRING'];
}
}
return false;
}
/**
* Redirect to given URL.
*
* @param string $url The target URL.
* @param boolean $isPermanent A flag to set the redirect headers (301/302).
*
* @return void
*/
protected function _sendRedirectHeaders($url, $isPermanent = false)
{
if ($isPermanent) {
header('HTTP/1.1 301 Moved Permanently');
}
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header('Location: ' . $url);
exit;
}
/**
* Dispatch registered events on link view.
*
* @return Rootd_Link_Model_Node
*/
protected function _triggerEvents()
{
Mage::dispatchEvent(self::EVENT_VIEW, array('object' => $this));
$otherEvents = explode(',', $this->getEventTriggers());
foreach ($otherEvents as $event) {
Mage::dispatchEvent($event, array('object' => $this));
}
return $this;
}
/**
* Get the attachment model.
*
* @return Rootd_Link_Model_Node_Attachment
*/
public function getAttachment()
{
if (!$this->_attachment) {
$attachment = Mage::getModel('link/node_attachment');
if ($this->getObjectId()) {
$attachment->load($this->getObjectId());
}
$this->_attachment = $attachment;
}
return $this->_attachment;
}
/**
* Get the gateway page URL.
*
* @return string
*/
public function getGatewayUrl()
{
return Mage::getUrl(Mage::helper('link')->getFrontName() . "/protect/{$this->getId()}");
}
/**
* Get the public link URL.
*
* @return string
*/
public function getPublicUrl()
{
if ($this->getObjectId()) {
$path = $this->getAttachment()->getRequestPath();
} else {
$path = $this->getRequestPath();
}
if (substr($path, 0, 1) != '/') {
$path = '/' . $path;
}
if ($this->_getIsExternal($path)) {
$base = '';
} else {
$base = rtrim(Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB), '/');
}
return $base . $path;
}
/**
* Get the store ID.
*
* @return integer|null
*/
public function getStoreId()
{
return $this->_getData('store_id');
}
/**
* Check for an option on the link.
*
* @param string $key The option key.
*
* @return boolean
*/
public function hasOption($key)
{
$options = explode(',', $this->getOptions());
return array_search($key, $options) !== false;
}
/**
* Load link information for request.
*
* @param mixed $path
*
* @return Rootd_Link_Model_Node
*/
public function loadByRequestPath($path)
{
$this->setId(null);
$this->_getResource()->loadByRequestPath($this, $path);
$this->_afterLoad();
$this->setOrigData();
$this->_hasDataChanges = false;
return $this;
}
/**
* Implement logic of link rewrites.
*
* @param Zend_Controller_Request_Http $request The request object.
* @param Zend_Controller_Response_Http $response The response object.
*
* @return boolean
*/
public function rewrite(Zend_Controller_Request_Http $request = null, Zend_Controller_Response_Http $response = null)
{
if (!Mage::isInstalled()) {
return false;
}
if (is_null($request)) {
$request = Mage::app()->getFrontController()->getRequest();
}
if (is_null($response)) {
$response = Mage::app()->getFrontController()->getResponse();
}
if (is_null($this->getStoreId()) || false===$this->getStoreId()) {
$this->setStoreId(Mage::app()->getStore()->getId());
}
/**
* We have two cases of incoming paths - with and without slashes at the end ("/somepath/" and "/somepath").
* Each of them matches two url rewrite request paths - with and without slashes at the end ("/somepath/" and "/somepath").
* Choose any matched rewrite, but in priority order that depends on same presence of slash and query params.
*/
$requestCases = array();
$pathInfo = $request->getPathInfo();
$origSlash = (substr($pathInfo, -1) == '/') ? '/' : '';
$requestPath = trim($pathInfo, '/');
// If there were final slash - add nothing to less priority paths. And vice versa.
$altSlash = $origSlash ? '' : '/';
$queryString = $this->_getQueryString(); // Query params in request, matching "path + query" has more priority
if ($queryString) {
$requestCases[] = $requestPath . $origSlash . '?' . $queryString;
$requestCases[] = $requestPath . $altSlash . '?' . $queryString;
}
$requestCases[] = $requestPath . $origSlash;
$requestCases[] = $requestPath . $altSlash;
$this->loadByRequestPath($requestCases);
if (!$this->getId()) {
return false;
}
if (
$this->getPassword() &&
(Mage::helper('core')->encrypt($request->getParam('link_password')) != $this->getPassword())
)
{
// Add message if password was specified
if (!is_null($request->getParam('link_password'))) {
Mage::getSingleton('core/session')->addError(Mage::helper('link')->__('The password provided for this link is invalid.'));
}
$this->_sendRedirectHeaders($this->getGatewayUrl(), true);
exit;
}
$request->setAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS, $this->getRequestPath());
$isPermanentRedirectOption = $this->hasOption('RP');
if ($this->_getIsExternal($this->getTargetPath())) {
$destinationStoreCode = Mage::app()->getStore($this->getStoreId())->getCode();
Mage::app()->getCookie()->set(Mage_Core_Model_Store::COOKIE_NAME, $destinationStoreCode, true);
$this->_triggerEvents();
$this->_sendRedirectHeaders($this->getTargetPath(), $isPermanentRedirectOption);
} else {
$targetUrl = $request->getBaseUrl(). '/' . $this->getTargetPath();
}
$isRedirectOption = $this->hasOption('R');
if ($isRedirectOption || $isPermanentRedirectOption) {
if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()->getCode()) {
$targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getTargetPath();
}
$this->_triggerEvents();
$this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption);
}
if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()->getCode()) {
$targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getTargetPath();
}
$queryString = $this->_getQueryString();
if ($queryString) {
$targetUrl .= '?'.$queryString;
}
$request->setRequestUri($targetUrl);
$request->setPathInfo($this->getTargetPath());
$this->_triggerEvents();
return true;
}
public function setAttachment(Rootd_Link_Model_Node_Attachment $attachment)
{
$this->_attachment = $attachment;
return $this;
}
}