PluginProbe
User Access Manager / 2.1.3
User Access Manager v2.1.3
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.3, at src/Controller/Frontend/RedirectController.php

416 lines 12.6 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 * Returns the post by the given url.
123 *
124 * @param string $url The url of the post(attachment).
125 *
126 * @return int
127 */
128 public function getPostIdByUrl($url)
129 {
130 $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
131
132 if (isset($postUrls[$url]) === true) {
133 return $postUrls[$url];
134 }
135
136 //Filter edit string
137 $newUrlPieces = preg_split('/-e[0-9]{1,}/', $url);
138 $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0].$newUrlPieces[1] : $newUrlPieces[0];
139
140 //Filter size
141 $newUrlPieces = preg_split('/-[0-9]{1,}x[0-9]{1,}(_[a-z])?/', $newUrl);
142 $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0].$newUrlPieces[1] : $newUrlPieces[0];
143 $newUrl = preg_replace('/\-pdf\.jpg$/', '.pdf', $newUrl);
144
145 $postUrls[$url] = $this->wordpress->attachmentUrlToPostId($newUrl);
146 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
147
148 return $postUrls[$url];
149 }
150
151 /**
152 * Returns the file object by the given type and url.
153 *
154 * @param string $objectType The type of the requested file.
155 * @param string $objectUrl The file url.
156 *
157 * @return null|FileObject
158 */
159 private function getFileSettingsByType($objectType, $objectUrl)
160 {
161 $fileObject = null;
162
163 if ($objectType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
164 $uploadDirs = $this->wordpress->getUploadDir();
165 $uploadDir = str_replace(ABSPATH, '/', $uploadDirs['basedir']);
166 $regex = '/.*'.str_replace('/', '\/', $uploadDir).'\//i';
167 $cleanObjectUrl = preg_replace($regex, '', $objectUrl);
168 $uploadUrl = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
169 $objectUrl = rtrim($uploadUrl, '/').'/'.ltrim($cleanObjectUrl, '/');
170
171 $post = $this->objectHandler->getPost($this->getPostIdByUrl($objectUrl));
172
173 if ($post !== false
174 && $post->post_type === ObjectHandler::ATTACHMENT_OBJECT_TYPE
175 ) {
176 $multiPath = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
177
178 $fileObject = $this->fileObjectFactory->createFileObject(
179 $post->ID,
180 $objectType,
181 $uploadDirs['basedir'].str_replace($multiPath, '', $objectUrl),
182 $this->wordpress->attachmentIsImage($post->ID)
183 );
184 }
185 } else {
186 $extraParameter = $this->getRequestParameter('uamextra');
187
188 $fileObject = $this->wordpress->applyFilters(
189 'uam_get_file_settings_by_type',
190 $fileObject,
191 $objectType,
192 $objectUrl,
193 $extraParameter
194 );
195 }
196
197 return $fileObject;
198 }
199
200 /**
201 * Delivers the content of the requested file.
202 *
203 * @param string $objectType The type of the requested file.
204 * @param string $objectUrl The file url.
205 */
206 public function getFile($objectType, $objectUrl)
207 {
208 $fileObject = $this->getFileSettingsByType($objectType, $objectUrl);
209
210 if ($fileObject === null) {
211 return;
212 }
213
214 if ($this->accessHandler->checkObjectAccess($fileObject->getType(), $fileObject->getId()) === true) {
215 $file = $fileObject->getFile();
216 } elseif ($fileObject->isImage() === true) {
217 $realPath = $this->wordpressConfig->getRealPath();
218 $file = $realPath.'assets/gfx/noAccessPic.png';
219 } else {
220 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
221 return;
222 }
223
224 $this->fileHandler->getFile($file, $fileObject->isImage());
225 }
226
227 /**
228 * Returns the redirect url and the permalink of the post if exists.
229 *
230 * @param null|string $permalink
231 *
232 * @return null|string
233 */
234 private function getRedirectUrlAndPermalink(&$permalink)
235 {
236 $permalink = null;
237 $redirect = $this->mainConfig->getRedirect();
238
239 if ($redirect === 'custom_page') {
240 $redirectCustomPage = $this->mainConfig->getRedirectCustomPage();
241 $post = $this->objectHandler->getPost($redirectCustomPage);
242 $url = null;
243
244 if ($post !== false) {
245 $url = $post->guid;
246 $permalink = $this->wordpress->getPageLink($post);
247 }
248 } elseif ($redirect === 'custom_url') {
249 $url = $this->mainConfig->getRedirectCustomUrl();
250 } else {
251 $url = $this->wordpress->getHomeUrl('/');
252 }
253
254 return $url;
255 }
256
257 /**
258 * Redirects the user to his destination.
259 *
260 * @param bool $checkPosts
261 */
262 public function redirectUser($checkPosts = true)
263 {
264 if ($checkPosts === true) {
265 $posts = (array)$this->wordpress->getWpQuery()->get_posts();
266
267 foreach ($posts as $post) {
268 if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID)) {
269 return;
270 }
271 }
272 }
273
274 $url = $this->getRedirectUrlAndPermalink($permalink);
275 $currentUrl = $this->util->getCurrentUrl();
276
277 if ($url !== null && $url !== $currentUrl && $permalink !== $currentUrl) {
278 $this->wordpress->wpRedirect($url);
279 $this->php->callExit();
280 }
281 }
282
283 /**
284 * Returns the post id by the post name.
285 *
286 * @param string $name
287 *
288 * @return int
289 */
290 private function getPostIdByName($name)
291 {
292 $postableTypes = implode('\',\'', $this->objectHandler->getPostTypes());
293
294 $query = $this->database->prepare(
295 "SELECT ID
296 FROM {$this->database->getPostsTable()}
297 WHERE post_name = %s
298 AND post_type IN ('{$postableTypes}')",
299 $name
300 );
301
302 return (int)$this->database->getVariable($query);
303 }
304
305 /**
306 * Extracts the object type and id.
307 *
308 * @param object $pageParams
309 * @param null|string $objectType
310 * @param null|string $objectId
311 */
312 private function extractObjectTypeAndId($pageParams, &$objectType, &$objectId)
313 {
314 $objectType = null;
315 $objectId = null;
316
317 $simpleTypes = [
318 'p' => ObjectHandler::GENERAL_POST_OBJECT_TYPE,
319 'page_id' => ObjectHandler::GENERAL_POST_OBJECT_TYPE,
320 'cat_id' => ObjectHandler::GENERAL_TERM_OBJECT_TYPE
321 ];
322
323 foreach ($simpleTypes as $queryVar => $newObjectType) {
324 if (isset($pageParams->query_vars[$queryVar]) === true) {
325 $objectType = $newObjectType;
326 $objectId = $pageParams->query_vars[$queryVar];
327 }
328 }
329
330 if (isset($pageParams->query_vars['name']) === true) {
331 $objectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE;
332 $objectId = $this->getPostIdByName($pageParams->query_vars['name']);
333 } elseif (isset($pageParams->query_vars['pagename']) === true) {
334 $object = $this->wordpress->getPageByPath($pageParams->query_vars['pagename']);
335
336 if ($object !== null) {
337 $objectType = $object->post_type;
338 $objectId = $object->ID;
339 }
340 }
341 }
342
343 /**
344 * Redirects to a page or to content.
345 *
346 * @param string $headers The headers which are given from wordpress.
347 * @param object $pageParams The params of the current page.
348 *
349 * @return string
350 */
351 public function redirect($headers, $pageParams)
352 {
353 $fileUrl = $this->getRequestParameter('uamgetfile');
354 $fileType = $this->getRequestParameter('uamfiletype');
355
356 if ($fileUrl !== null && $fileType !== null) {
357 $this->getFile($fileType, $fileUrl);
358 } elseif ($this->wordpressConfig->atAdminPanel() === false
359 && $this->mainConfig->getRedirect() !== 'false'
360 ) {
361 $this->extractObjectTypeAndId($pageParams, $objectType, $objectId);
362
363 if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
364 $this->redirectUser(false);
365 }
366 }
367
368 return $headers;
369 }
370
371 /**
372 * Returns the url for a locked file.
373 *
374 * @param string $url The base url.
375 * @param integer $id The _iId of the file.
376 *
377 * @return string
378 */
379 public function getFileUrl($url, $id)
380 {
381 if ($this->wordpressConfig->isPermalinksActive() === false && $this->mainConfig->lockFile() === true) {
382 $post = $this->objectHandler->getPost($id);
383
384 if ($post !== null) {
385 $type = explode('/', $post->post_mime_type);
386 $type = (isset($type[1]) === true) ? $type[1] : $type[0];
387
388 $lockedFileTypes = $this->mainConfig->getLockedFileTypes();
389 $fileTypes = explode(',', $lockedFileTypes);
390
391 if ($lockedFileTypes === 'all' || in_array($type, $fileTypes) === true) {
392 $url = $this->wordpress->getHomeUrl('/').'?uamfiletype=attachment&uamgetfile='.$url;
393 }
394 }
395 }
396
397 return $url;
398 }
399
400 /**
401 * Caches the urls for the post for a later lookup.
402 *
403 * @param string $url The url of the post.
404 * @param object $post The post object.
405 *
406 * @return string
407 */
408 public function cachePostLinks($url, $post)
409 {
410 $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
411 $postUrls[$url] = $post->ID;
412 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
413 return $url;
414 }
415 }
416