PluginProbe
User Access Manager / 2.1.9
User Access Manager v2.1.9
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Controller / Frontend / RedirectController.php

RedirectController.php in User Access Manager 2.1.9, at src/Controller/Frontend/RedirectController.php

434 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * FrontendRedirectController.php
4 *
5 * The FrontendRedirectController class file.
6 *
7 * PHP versions 5
8 *
9 * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 * @copyright 2008-2017 Alexander Schneider
11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 * @version SVN: $id$
13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 */
15 namespace UserAccessManager\Controller\Frontend;
16
17 use UserAccessManager\Access\AccessHandler;
18 use UserAccessManager\Cache\Cache;
19 use UserAccessManager\Config\MainConfig;
20 use UserAccessManager\Config\WordpressConfig;
21 use UserAccessManager\Controller\Controller;
22 use UserAccessManager\Database\Database;
23 use UserAccessManager\File\FileHandler;
24 use UserAccessManager\File\FileObject;
25 use UserAccessManager\File\FileObjectFactory;
26 use UserAccessManager\Object\ObjectHandler;
27 use UserAccessManager\Util\Util;
28 use UserAccessManager\Wrapper\Php;
29 use UserAccessManager\Wrapper\Wordpress;
30
31 /**
32 * Class FrontendRedirectController
33 *
34 * @package UserAccessManager\Controller
35 */
36 class RedirectController extends Controller
37 {
38 use LoginControllerTrait;
39
40 const POST_URL_CACHE_KEY = 'PostUrls';
41
42 /**
43 * @var MainConfig
44 */
45 private $mainConfig;
46
47 /**
48 * @var Database
49 */
50 private $database;
51
52 /**
53 * @var Cache
54 */
55 private $cache;
56
57 /**
58 * @var Util
59 */
60 private $util;
61
62 /**
63 * @var ObjectHandler
64 */
65 private $objectHandler;
66
67 /**
68 * @var AccessHandler
69 */
70 private $accessHandler;
71
72 /**
73 * @var FileHandler
74 */
75 private $fileHandler;
76
77 /**
78 * @var FileObjectFactory
79 */
80 private $fileObjectFactory;
81
82 /**
83 * RedirectController constructor.
84 *
85 * @param Php $php
86 * @param Wordpress $wordpress
87 * @param WordpressConfig $wordpressConfig
88 * @param MainConfig $mainConfig
89 * @param Database $database
90 * @param Util $util
91 * @param Cache $cache
92 * @param ObjectHandler $objectHandler
93 * @param AccessHandler $accessHandler
94 * @param FileHandler $fileHandler
95 * @param FileObjectFactory $fileObjectFactory
96 */
97 public function __construct(
98 Php $php,
99 Wordpress $wordpress,
100 WordpressConfig $wordpressConfig,
101 MainConfig $mainConfig,
102 Database $database,
103 Util $util,
104 Cache $cache,
105 ObjectHandler $objectHandler,
106 AccessHandler $accessHandler,
107 FileHandler $fileHandler,
108 FileObjectFactory $fileObjectFactory
109 ) {
110 parent::__construct($php, $wordpress, $wordpressConfig);
111 $this->mainConfig = $mainConfig;
112 $this->database = $database;
113 $this->util = $util;
114 $this->cache = $cache;
115 $this->objectHandler = $objectHandler;
116 $this->accessHandler = $accessHandler;
117 $this->fileHandler = $fileHandler;
118 $this->fileObjectFactory = $fileObjectFactory;
119 }
120
121 /**
122 * @return Wordpress
123 */
124 protected function getWordpress()
125 {
126 return $this->wordpress;
127 }
128
129 /**
130 * Returns the post by the given url.
131 *
132 * @param string $url The url of the post(attachment).
133 *
134 * @return int
135 */
136 public function getPostIdByUrl($url)
137 {
138 $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
139
140 if (isset($postUrls[$url]) === true) {
141 return $postUrls[$url];
142 }
143
144 //Filter size
145 $newUrlPieces = preg_split('/-[0-9]{1,}x[0-9]{1,}(_[a-z])?/', $url);
146 $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0].$newUrlPieces[1] : $newUrlPieces[0];
147 $newUrl = preg_replace('/\-pdf\.jpg$/', '.pdf', $newUrl);
148
149 $postUrls[$url] = $this->wordpress->attachmentUrlToPostId($newUrl);
150 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
151
152 return $postUrls[$url];
153 }
154
155 /**
156 * Returns the file object by the given type and url.
157 *
158 * @param string $objectType The type of the requested file.
159 * @param string $objectUrl The file url.
160 *
161 * @return null|FileObject
162 */
163 private function getFileSettingsByType($objectType, $objectUrl)
164 {
165 $fileObject = null;
166
167 if ($objectType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
168 $uploadDirs = $this->wordpress->getUploadDir();
169 $uploadDir = str_replace(ABSPATH, '/', $uploadDirs['basedir']);
170 $regex = '/.*'.str_replace('/', '\/', $uploadDir).'\//i';
171 $cleanObjectUrl = preg_replace($regex, '', $objectUrl);
172 $uploadUrl = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
173 $objectUrl = rtrim($uploadUrl, '/').'/'.ltrim($cleanObjectUrl, '/');
174
175 $post = $this->objectHandler->getPost($this->getPostIdByUrl($objectUrl));
176
177 if ($post !== false
178 && $post->post_type === ObjectHandler::ATTACHMENT_OBJECT_TYPE
179 ) {
180 $multiPath = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
181
182 $fileObject = $this->fileObjectFactory->createFileObject(
183 $post->ID,
184 $objectType,
185 $uploadDirs['basedir'].str_replace($multiPath, '', $objectUrl),
186 $this->wordpress->attachmentIsImage($post->ID)
187 );
188 }
189 } else {
190 $extraParameter = $this->getRequestParameter('uamextra');
191
192 $fileObject = $this->wordpress->applyFilters(
193 'uam_get_file_settings_by_type',
194 $fileObject,
195 $objectType,
196 $objectUrl,
197 $extraParameter
198 );
199 }
200
201 return $fileObject;
202 }
203
204 /**
205 * Delivers the content of the requested file.
206 *
207 * @param string $objectType The type of the requested file.
208 * @param string $objectUrl The file url.
209 */
210 public function getFile($objectType, $objectUrl)
211 {
212 $fileObject = $this->getFileSettingsByType($objectType, $objectUrl);
213
214 if ($fileObject === null) {
215 return;
216 }
217
218 if ($this->accessHandler->checkObjectAccess($fileObject->getType(), $fileObject->getId()) === true) {
219 $file = $fileObject->getFile();
220 } elseif ($fileObject->isImage() === true) {
221 if ($this->mainConfig->getNoAccessImageType() === 'custom') {
222 $file = $this->mainConfig->getCustomNoAccessImage();
223 } else {
224 $realPath = $this->wordpressConfig->getRealPath();
225 $file = $realPath.'assets'.DIRECTORY_SEPARATOR.'gfx'.DIRECTORY_SEPARATOR.'noAccessPic.png';
226 }
227 } else {
228 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
229 return;
230 }
231
232 $this->fileHandler->getFile($file, $fileObject->isImage());
233 }
234
235 /**
236 * Returns the redirect url and the permalink of the post if exists.
237 *
238 * @param null|string $permalink
239 *
240 * @return null|string
241 */
242 private function getRedirectUrlAndPermalink(&$permalink)
243 {
244 $permalink = null;
245 $redirect = $this->mainConfig->getRedirect();
246
247 if ($redirect === 'custom_page') {
248 $redirectCustomPage = $this->mainConfig->getRedirectCustomPage();
249 $post = $this->objectHandler->getPost($redirectCustomPage);
250 $url = null;
251
252 if ($post !== false) {
253 $url = $post->guid;
254 $permalink = $this->wordpress->getPageLink($post);
255 }
256 } elseif ($redirect === 'custom_url') {
257 $url = $this->mainConfig->getRedirectCustomUrl();
258 } else {
259 $url = $this->wordpress->getHomeUrl('/');
260 }
261
262 return $url;
263 }
264
265 /**
266 * Redirects the user to his destination.
267 *
268 * @param bool $checkPosts
269 */
270 public function redirectUser($checkPosts = true)
271 {
272 if ($checkPosts === true) {
273 $posts = (array)$this->wordpress->getWpQuery()->get_posts();
274
275 foreach ($posts as $post) {
276 if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID)) {
277 return;
278 }
279 }
280 }
281
282 $url = $this->getRedirectUrlAndPermalink($permalink);
283 $currentUrl = $this->util->getCurrentUrl();
284
285 if ($url !== null && $url !== $currentUrl && $permalink !== $currentUrl) {
286 $this->wordpress->wpRedirect($url);
287 $this->php->callExit();
288 }
289 }
290
291 /**
292 * Returns the post id by the post name.
293 *
294 * @param string $name
295 *
296 * @return int
297 */
298 private function getPostIdByName($name)
299 {
300 $postableTypes = implode('\',\'', $this->objectHandler->getPostTypes());
301
302 $query = $this->database->prepare(
303 "SELECT ID
304 FROM {$this->database->getPostsTable()}
305 WHERE post_name = %s
306 AND post_type IN ('{$postableTypes}')",
307 $name
308 );
309
310 return (int)$this->database->getVariable($query);
311 }
312
313 /**
314 * Extracts the object type and id.
315 *
316 * @param object $pageParams
317 * @param null|string $objectType
318 * @param null|string $objectId
319 */
320 private function extractObjectTypeAndId($pageParams, &$objectType, &$objectId)
321 {
322 $objectType = null;
323 $objectId = null;
324
325 $simpleTypes = [
326 'p' => ObjectHandler::GENERAL_POST_OBJECT_TYPE,
327 'page_id' => ObjectHandler::GENERAL_POST_OBJECT_TYPE,
328 'cat_id' => ObjectHandler::GENERAL_TERM_OBJECT_TYPE
329 ];
330
331 foreach ($simpleTypes as $queryVar => $newObjectType) {
332 if (isset($pageParams->query_vars[$queryVar]) === true) {
333 $objectType = $newObjectType;
334 $objectId = $pageParams->query_vars[$queryVar];
335 }
336 }
337
338 if (isset($pageParams->query_vars['name']) === true) {
339 $objectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE;
340 $objectId = $this->getPostIdByName($pageParams->query_vars['name']);
341 } elseif (isset($pageParams->query_vars['pagename']) === true) {
342 $object = $this->wordpress->getPageByPath($pageParams->query_vars['pagename']);
343
344 if ($object !== null) {
345 $objectType = $object->post_type;
346 $objectId = $object->ID;
347 }
348 }
349 }
350
351 /**
352 * Redirects to a page or to content.
353 *
354 * @param string $headers The headers which are given from wordpress.
355 * @param object $pageParams The params of the current page.
356 *
357 * @return string
358 */
359 public function redirect($headers, $pageParams)
360 {
361 $fileUrl = $this->getRequestParameter('uamgetfile');
362 $fileType = $this->getRequestParameter('uamfiletype');
363
364 if ($fileUrl !== null && $fileType !== null) {
365 $this->getFile($fileType, $fileUrl);
366 } elseif ($this->wordpressConfig->atAdminPanel() === false
367 && $this->mainConfig->getRedirect() !== 'false'
368 ) {
369 $this->extractObjectTypeAndId($pageParams, $objectType, $objectId);
370
371 if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
372 $this->redirectUser(false);
373 }
374 }
375
376 return $headers;
377 }
378
379 /**
380 * Returns the url for a locked file.
381 *
382 * @param string $url The base url.
383 * @param integer $id The _iId of the file.
384 *
385 * @return string
386 */
387 public function getFileUrl($url, $id)
388 {
389 if ($this->mainConfig->lockFile() === true
390 && ($this->wordpress->isNginx() === false || $this->wordpress->gotModRewrite() === false)
391 ) {
392 $post = $this->objectHandler->getPost($id);
393
394 if ($post !== null) {
395 $type = explode('/', $post->post_mime_type);
396 $type = (isset($type[1]) === true) ? $type[1] : $type[0];
397
398 $lockedFileTypes = $this->mainConfig->getLockedFiles();
399 $fileTypes = explode(',', $lockedFileTypes);
400
401 if ($lockedFileTypes === 'all' || in_array($type, $fileTypes) === true) {
402 $url = $this->wordpress->getHomeUrl('/').'?uamfiletype=attachment&uamgetfile='.$url;
403 }
404 }
405 }
406
407 return $url;
408 }
409
410 /**
411 * Caches the urls for the post for a later lookup.
412 *
413 * @param string $url The url of the post.
414 * @param object $post The post object.
415 *
416 * @return string
417 */
418 public function cachePostLinks($url, $post)
419 {
420 $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
421 $postUrls[$url] = $post->ID;
422 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
423 return $url;
424 }
425
426 /**
427 * Tries to load the file via x send file
428 */
429 public function testXSendFile()
430 {
431 $this->fileHandler->deliverXSendFileTestFile();
432 }
433 }
434