Commit | Line | Data |
---|---|---|
e3d28c74 TO |
1 | <?php |
2 | namespace Civi\Core; | |
3 | ||
4 | /** | |
5 | * Class Paths | |
6 | * @package Civi\Core | |
7 | * | |
8 | * This paths class translates path-expressions into local file paths and | |
9 | * URLs. Path-expressions may take a few forms: | |
10 | * | |
11 | * - Paths and URLs may use a variable prefix. For example, '[civicrm.files]/upload' | |
12 | * - Paths and URLS may be absolute. | |
13 | * - Paths may be relative (base dir: [civicrm.files]). | |
14 | * - URLs may be relative (base dir: [cms.root]). | |
15 | */ | |
16 | class Paths { | |
17 | ||
18 | const DEFAULT_URL = 'cms.root'; | |
19 | const DEFAULT_PATH = 'civicrm.files'; | |
20 | ||
21 | /** | |
22 | * @var array | |
23 | * Array(string $name => array(url => $, path => $)). | |
24 | */ | |
b698e2d5 | 25 | private $variables = array(); |
e3d28c74 | 26 | |
b698e2d5 | 27 | private $variableFactory = array(); |
e3d28c74 | 28 | |
e8e8f3ad | 29 | /** |
30 | * Class constructor. | |
31 | */ | |
e3d28c74 | 32 | public function __construct() { |
f553d1ea | 33 | $paths = $this; |
e3d28c74 | 34 | $this |
e3d28c74 TO |
35 | ->register('civicrm.root', function () { |
36 | return \CRM_Core_Config::singleton()->userSystem->getCiviSourceStorage(); | |
37 | }) | |
2f4b426c TO |
38 | ->register('civicrm.packages', function () { |
39 | return array( | |
40 | 'path' => \Civi::paths()->getPath('[civicrm.root]/packages/'), | |
41 | 'url' => \Civi::paths()->getUrl('[civicrm.root]/packages/'), | |
42 | ); | |
43 | }) | |
44 | ->register('civicrm.vendor', function () { | |
45 | return array( | |
46 | 'path' => \Civi::paths()->getPath('[civicrm.root]/vendor/'), | |
47 | 'url' => \Civi::paths()->getUrl('[civicrm.root]/vendor/'), | |
48 | ); | |
49 | }) | |
50 | ->register('civicrm.bower', function () { | |
51 | return array( | |
52 | 'path' => \Civi::paths()->getPath('[civicrm.root]/bower_components/'), | |
53 | 'url' => \Civi::paths()->getUrl('[civicrm.root]/bower_components/'), | |
54 | ); | |
55 | }) | |
e3d28c74 TO |
56 | ->register('civicrm.files', function () { |
57 | return \CRM_Core_Config::singleton()->userSystem->getDefaultFileStorage(); | |
58 | }) | |
f553d1ea | 59 | ->register('wp.frontend.base', function () { |
f5dd9d4a | 60 | return array('url' => rtrim(CIVICRM_UF_BASEURL, '/') . '/'); |
f553d1ea KC |
61 | }) |
62 | ->register('wp.frontend', function () use ($paths) { | |
63 | $config = \CRM_Core_Config::singleton(); | |
64 | $suffix = defined('CIVICRM_UF_WP_BASEPAGE') ? CIVICRM_UF_WP_BASEPAGE : $config->wpBasePage; | |
65 | return array( | |
66 | 'url' => $paths->getVariable('wp.frontend.base', 'url') . $suffix, | |
67 | ); | |
68 | }) | |
69 | ->register('wp.backend.base', function () { | |
f5dd9d4a | 70 | return array('url' => rtrim(CIVICRM_UF_BASEURL, '/') . '/wp-admin/'); |
f553d1ea KC |
71 | }) |
72 | ->register('wp.backend', function () use ($paths) { | |
73 | return array( | |
74 | 'url' => $paths->getVariable('wp.backend.base', 'url') . 'admin.php', | |
75 | ); | |
76 | }) | |
e3d28c74 TO |
77 | ->register('cms', function () { |
78 | return array( | |
79 | 'path' => \CRM_Core_Config::singleton()->userSystem->cmsRootPath(), | |
80 | 'url' => \CRM_Utils_System::baseCMSURL(), | |
81 | ); | |
82 | }) | |
83 | ->register('cms.root', function () { | |
84 | return array( | |
85 | 'path' => \CRM_Core_Config::singleton()->userSystem->cmsRootPath(), | |
86 | // Misleading: this *removes* the language part of the URL, producing a pristine base URL. | |
87 | 'url' => \CRM_Utils_System::languageNegotiationURL(\CRM_Utils_System::baseCMSURL(), FALSE, TRUE), | |
88 | ); | |
89 | }); | |
90 | } | |
91 | ||
92 | /** | |
93 | * Register a new URL/file path mapping. | |
94 | * | |
95 | * @param string $name | |
b698e2d5 | 96 | * The name of the variable. |
e3d28c74 TO |
97 | * @param callable $factory |
98 | * Function which returns an array with keys: | |
99 | * - path: string. | |
100 | * - url: string. | |
4b350175 | 101 | * @return Paths |
e3d28c74 TO |
102 | */ |
103 | public function register($name, $factory) { | |
b698e2d5 | 104 | $this->variableFactory[$name] = $factory; |
e3d28c74 TO |
105 | return $this; |
106 | } | |
107 | ||
b698e2d5 TO |
108 | /** |
109 | * @param string $name | |
110 | * Ex: 'civicrm.root'. | |
111 | * @param string $attr | |
112 | * Ex: 'url', 'path'. | |
113 | * @return mixed | |
114 | */ | |
115 | public function getVariable($name, $attr) { | |
116 | if (!isset($this->variables[$name])) { | |
117 | $this->variables[$name] = call_user_func($this->variableFactory[$name]); | |
313a57a0 TO |
118 | if (isset($GLOBALS['civicrm_paths'][$name])) { |
119 | $this->variables[$name] = array_merge($this->variables[$name], $GLOBALS['civicrm_paths'][$name]); | |
120 | } | |
e3d28c74 | 121 | } |
b698e2d5 | 122 | if (!isset($this->variables[$name][$attr])) { |
e3d28c74 TO |
123 | throw new \RuntimeException("Cannot resolve path using \"$name.$attr\""); |
124 | } | |
b698e2d5 TO |
125 | return $this->variables[$name][$attr]; |
126 | } | |
127 | ||
e8e8f3ad | 128 | /** |
129 | * Does the variable exist. | |
130 | * | |
131 | * @param string $name | |
132 | * | |
133 | * @return bool | |
134 | */ | |
b698e2d5 TO |
135 | public function hasVariable($name) { |
136 | return isset($this->variableFactory[$name]); | |
e3d28c74 TO |
137 | } |
138 | ||
139 | /** | |
140 | * Determine the absolute path to a file, given that the file is most likely | |
b698e2d5 | 141 | * in a given particular variable. |
e3d28c74 TO |
142 | * |
143 | * @param string $value | |
b698e2d5 TO |
144 | * The file path. |
145 | * Use "." to reference to default file root. | |
146 | * Values may begin with a variable, e.g. "[civicrm.files]/upload". | |
e3d28c74 TO |
147 | * @return mixed|string |
148 | */ | |
149 | public function getPath($value) { | |
150 | $defaultContainer = self::DEFAULT_PATH; | |
151 | if ($value && $value{0} == '[' && preg_match(';^\[([a-zA-Z0-9\._]+)\]/(.*);', $value, $matches)) { | |
152 | $defaultContainer = $matches[1]; | |
153 | $value = $matches[2]; | |
154 | } | |
155 | if (empty($value)) { | |
156 | return FALSE; | |
157 | } | |
158 | if ($value === '.') { | |
159 | $value = ''; | |
160 | } | |
b698e2d5 | 161 | return \CRM_Utils_File::absoluteDirectory($value, $this->getVariable($defaultContainer, 'path')); |
e3d28c74 TO |
162 | } |
163 | ||
164 | /** | |
b698e2d5 | 165 | * Determine the URL to a file. |
e3d28c74 TO |
166 | * |
167 | * @param string $value | |
b698e2d5 | 168 | * The file path. The path may begin with a variable, e.g. "[civicrm.files]/upload". |
e3d28c74 TO |
169 | * @param string $preferFormat |
170 | * The preferred format ('absolute', 'relative'). | |
171 | * The result data may not meet the preference -- if the setting | |
172 | * refers to an external domain, then the result will be | |
173 | * absolute (regardless of preference). | |
174 | * @param bool|NULL $ssl | |
175 | * NULL to autodetect. TRUE to force to SSL. | |
176 | * @return mixed|string | |
177 | */ | |
178 | public function getUrl($value, $preferFormat = 'relative', $ssl = NULL) { | |
179 | $defaultContainer = self::DEFAULT_URL; | |
ac47f7ca | 180 | if ($value && $value{0} == '[' && preg_match(';^\[([a-zA-Z0-9\._]+)\](/(.*))$;', $value, $matches)) { |
e3d28c74 | 181 | $defaultContainer = $matches[1]; |
ac47f7ca | 182 | $value = empty($matches[3]) ? '.' : $matches[3]; |
e3d28c74 TO |
183 | } |
184 | ||
185 | if (empty($value)) { | |
186 | return FALSE; | |
187 | } | |
188 | if ($value === '.') { | |
189 | $value = ''; | |
190 | } | |
191 | if (substr($value, 0, 4) == 'http') { | |
192 | return $value; | |
193 | } | |
194 | ||
b698e2d5 | 195 | $value = $this->getVariable($defaultContainer, 'url') . $value; |
e3d28c74 TO |
196 | |
197 | if ($preferFormat === 'relative') { | |
198 | $parsed = parse_url($value); | |
199 | if (isset($_SERVER['HTTP_HOST']) && isset($parsed['host']) && $_SERVER['HTTP_HOST'] == $parsed['host']) { | |
200 | $value = $parsed['path']; | |
201 | } | |
202 | } | |
203 | ||
204 | if ($ssl || ($ssl === NULL && \CRM_Utils_System::isSSL())) { | |
205 | $value = str_replace('http://', 'https://', $value); | |
206 | } | |
207 | ||
208 | return $value; | |
209 | } | |
210 | ||
211 | } |