Merge branch 'master' into 5.28
[civicrm-core.git] / CRM / Core / Page / Redirect.php
1 <?php
2
3 /**
4 * Placeholder page which generates a redirect
5 *
6 * ```
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>
12 * ```
13 */
14 class CRM_Core_Page_Redirect extends CRM_Core_Page {
15
16 /**
17 * Run page.
18 *
19 * @param string $path
20 * @param array $pageArgs
21 */
22 public function run($path = NULL, $pageArgs = []) {
23 $url = self::createUrl($path, $_REQUEST, $pageArgs, TRUE);
24 CRM_Utils_System::redirect($url);
25 }
26
27 /**
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.
34 * @param bool $absolute
35 * Whether to return an absolute URL.
36 * @return string
37 * URL
38 */
39 public static function createUrl($requestPath, $requestArgs, $pageArgs, $absolute) {
40 if (empty($pageArgs['url'])) {
41 CRM_Core_Error::statusBounce('This page is configured as a redirect, but it does not have a target.');
42 }
43
44 $vars = [];
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 }
65
66 }