Merge pull request #16838 from mlutfy/core1280
[civicrm-core.git] / CRM / Core / Page / Redirect.php
CommitLineData
5c2885f8 1<?php
2
3/**
4 * Placeholder page which generates a redirect
5 *
0b882a86 6 * ```
5c2885f8 7 * <item>
8 * <path>civicrm/admin/options/case_type</path>
9 * <page_callback>CRM_Core_Page_Redirect</page_callback>
10 * <page_arguments>url=civicrm/foo/bar?whiz=bang&amp;passthru=%%passthru%%</page_arguments>
11 * </item>
0b882a86 12 * ```
5c2885f8 13 */
14class CRM_Core_Page_Redirect extends CRM_Core_Page {
4b62bc4f
EM
15
16 /**
1cd3ffa9 17 * Run page.
4b62bc4f
EM
18 *
19 * @param string $path
20 * @param array $pageArgs
4b62bc4f 21 */
be2fb01f 22 public function run($path = NULL, $pageArgs = []) {
5c2885f8 23 $url = self::createUrl($path, $_REQUEST, $pageArgs, TRUE);
5c2885f8 24 CRM_Utils_System::redirect($url);
25 }
26
27 /**
6a0b768e
TO
28 * @param array $requestPath
29 * The parts of the path in the current page request.
30 * @param array $requestArgs
31 * Any GET arguments.
32 * @param array $pageArgs
33 * The page_arguments registered in the router.
6a0b768e
TO
34 * @param bool $absolute
35 * Whether to return an absolute URL.
a6c01b45
CW
36 * @return string
37 * URL
5c2885f8 38 */
39 public static function createUrl($requestPath, $requestArgs, $pageArgs, $absolute) {
40 if (empty($pageArgs['url'])) {
79e11805 41 CRM_Core_Error::statusBounce('This page is configured as a redirect, but it does not have a target.');
5c2885f8 42 }
43
be2fb01f 44 $vars = [];
5c2885f8 45 // note: %% isn't legal in a well-formed URL, so it's not a bad variable-delimiter
46 foreach ($requestPath as $pathPos => $pathPart) {
47 $vars["%%{$pathPos}%%"] = urlencode($pathPart);
48 }
49 foreach ($requestArgs as $var => $value) {
50 $vars["%%{$var}%%"] = urlencode($value);
51 }
52 $urlString = strtr($pageArgs['url'], $vars);
53 $urlString = preg_replace('/%%[a-zA-Z0-9]+%%/', '', $urlString);
54
55 $urlParts = parse_url($urlString);
56 $url = CRM_Utils_System::url(
57 $urlParts['path'],
58 CRM_Utils_Array::value('query', $urlParts, NULL),
59 $absolute,
60 CRM_Utils_Array::value('fragment', $urlParts, NULL)
61 );
62
63 return $url;
64 }
96025800 65
5c2885f8 66}