PluginProbe
User Access Manager / 2.1.5
User Access Manager v2.1.5
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.5, at src/Controller/Frontend/RedirectController.php

424 lines 12.7 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 edit string
145 $newUrlPieces = preg_split('/-e[0-9]{1,}/', $url);
146 $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0].$newUrlPieces[1] : $newUrlPieces[0];
147
148 //Filter size
149 $newUrlPieces = preg_split('/-[0-9]{1,}x[0-9]{1,}(_[a-z])?/', $newUrl);
150 $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0].$newUrlPieces[1] : $newUrlPieces[0];
151 $newUrl = preg_replace('/\-pdf\.jpg$/', '.pdf', $newUrl);
152
153 $postUrls[$url] = $this->wordpress->attachmentUrlToPostId($newUrl);
154 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
155
156 return $postUrls[$url];
157 }
158
159 /**
160 * Returns the file object by the given type and url.
161 *
162 * @param string $objectType The type of the requested file.
163 * @param string $objectUrl The file url.
164 *
165 * @return null|FileObject
166 */
167 private function getFileSettingsByType($objectType, $objectUrl)
168 {
169 $fileObject = null;
170
171 if ($objectType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
172 $uploadDirs = $this->wordpress->getUploadDir();
173 $uploadDir = str_replace(ABSPATH, '/', $uploadDirs['basedir']);
174 $regex = '/.*'.str_replace('/', '\/', $uploadDir).'\//i';
175 $cleanObjectUrl = preg_replace($regex, '', $objectUrl);
176 $uploadUrl = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
177 $objectUrl = rtrim($uploadUrl, '/').'/'.ltrim($cleanObjectUrl, '/');
178
179 $post = $this->objectHandler->getPost($this->getPostIdByUrl($objectUrl));
180
181 if ($post !== false
182 && $post->post_type === ObjectHandler::ATTACHMENT_OBJECT_TYPE
183 ) {
184 $multiPath = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
185
186 $fileObject = $this->fileObjectFactory->createFileObject(
187 $post->ID,
188 $objectType,
189 $uploadDirs['basedir'].str_replace($multiPath, '', $objectUrl),
190 $this->wordpress->attachmentIsImage($post->ID)
191 );
192 }
193 } else {
194 $extraParameter = $this->getRequestParameter('uamextra');
195
196 $fileObject = $this->wordpress->applyFilters(
197 'uam_get_file_settings_by_type',
198 $fileObject,
199 $objectType,
200 $objectUrl,
201 $extraParameter
202 );
203 }
204
205 return $fileObject;
206 }
207
208 /**
209 * Delivers the content of the requested file.
210 *
211 * @param string $objectType The type of the requested file.
212 * @param string $objectUrl The file url.
213 */
214 public function getFile($objectType, $objectUrl)
215 {
216 $fileObject = $this->getFileSettingsByType($objectType, $objectUrl);
217
218 if ($fileObject === null) {
219 return;
220 }
221
222 if ($this->accessHandler->checkObjectAccess($fileObject->getType(), $fileObject->getId()) === true) {
223 $file = $fileObject->getFile();
224 } elseif ($fileObject->isImage() === true) {
225 $realPath = $this->wordpressConfig->getRealPath();
226 $file = $realPath.'assets/gfx/noAccessPic.png';
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->wordpressConfig->isPermalinksActive() === false && $this->mainConfig->lockFile() === true) {
390 $post = $this->objectHandler->getPost($id);
391
392 if ($post !== null) {
393 $type = explode('/', $post->post_mime_type);
394 $type = (isset($type[1]) === true) ? $type[1] : $type[0];
395
396 $lockedFileTypes = $this->mainConfig->getLockedFileTypes();
397 $fileTypes = explode(',', $lockedFileTypes);
398
399 if ($lockedFileTypes === 'all' || in_array($type, $fileTypes) === true) {
400 $url = $this->wordpress->getHomeUrl('/').'?uamfiletype=attachment&uamgetfile='.$url;
401 }
402 }
403 }
404
405 return $url;
406 }
407
408 /**
409 * Caches the urls for the post for a later lookup.
410 *
411 * @param string $url The url of the post.
412 * @param object $post The post object.
413 *
414 * @return string
415 */
416 public function cachePostLinks($url, $post)
417 {
418 $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
419 $postUrls[$url] = $post->ID;
420 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
421 return $url;
422 }
423 }
424