8 * This paths class translates path-expressions into local file paths and
9 * URLs. Path-expressions may take a few forms:
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]).
18 const DEFAULT_URL
= 'cms.root';
19 const DEFAULT_PATH
= 'civicrm.files';
23 * Array(string $name => array(url => $, path => $)).
25 private $variables = [];
27 private $variableFactory = [];
32 public function __construct() {
34 ->register('civicrm.root', function () {
35 return \CRM_Core_Config
::singleton()->userSystem
->getCiviSourceStorage();
37 ->register('civicrm.packages', function () {
39 'path' => \Civi
::paths()->getPath('[civicrm.root]/packages/'),
40 'url' => \Civi
::paths()->getUrl('[civicrm.root]/packages/', 'absolute'),
43 ->register('civicrm.vendor', function () {
45 'path' => \Civi
::paths()->getPath('[civicrm.root]/vendor/'),
46 'url' => \Civi
::paths()->getUrl('[civicrm.root]/vendor/', 'absolute'),
49 ->register('civicrm.bower', function () {
51 'path' => \Civi
::paths()->getPath('[civicrm.root]/bower_components/'),
52 'url' => \Civi
::paths()->getUrl('[civicrm.root]/bower_components/', 'absolute'),
55 ->register('civicrm.files', function () {
56 return \CRM_Core_Config
::singleton()->userSystem
->getDefaultFileStorage();
58 ->register('civicrm.private', function () {
60 // For backward compatibility with existing deployments, this
61 // effectively returns `dirname(CIVICRM_TEMPLATE_COMPILEDIR)`.
62 // That's confusing. Future installers should probably set `civicrm.private`
63 // explicitly instead of setting `CIVICRM_TEMPLATE_COMPILEDIR`.
64 'path' => \CRM_Utils_File
::baseFilePath(),
67 ->register('civicrm.log', function () {
69 'path' => \Civi
::paths()->getPath('[civicrm.private]/ConfigAndLog'),
72 ->register('civicrm.compile', function () {
74 // These two formulations are equivalent in typical deployments; however,
75 // for existing systems which previously customized CIVICRM_TEMPLATE_COMPILEDIR,
76 // using the constant should be more backward-compatibility.
77 'path' => defined('CIVICRM_TEMPLATE_COMPILEDIR') ? CIVICRM_TEMPLATE_COMPILEDIR
: \Civi
::paths()->getPath('[civicrm.private]/templates_c'),
80 ->register('civicrm.l10n', function () {
81 $dir = defined('CIVICRM_L10N_BASEDIR') ? CIVICRM_L10N_BASEDIR
: \Civi
::paths()->getPath('[civicrm.private]/l10n');
83 'path' => is_dir($dir) ?
$dir : \Civi
::paths()->getPath('[civicrm.root]/l10n'),
86 ->register('cms', function () {
88 'path' => \CRM_Core_Config
::singleton()->userSystem
->cmsRootPath(),
89 'url' => \CRM_Utils_System
::baseCMSURL(),
92 ->register('cms.root', function () {
94 'path' => \CRM_Core_Config
::singleton()->userSystem
->cmsRootPath(),
95 // Misleading: this *removes* the language part of the URL, producing a pristine base URL.
96 'url' => \CRM_Utils_System
::languageNegotiationURL(\CRM_Utils_System
::baseCMSURL(), FALSE, TRUE),
102 * Register a new URL/file path mapping.
104 * @param string $name
105 * The name of the variable.
106 * @param callable $factory
107 * Function which returns an array with keys:
112 public function register($name, $factory) {
113 $this->variableFactory
[$name] = $factory;
118 * @param string $name
119 * Ex: 'civicrm.root'.
120 * @param string $attr
124 public function getVariable($name, $attr) {
125 if (!isset($this->variables
[$name])) {
126 $this->variables
[$name] = call_user_func($this->variableFactory
[$name]);
127 if (isset($GLOBALS['civicrm_paths'][$name])) {
128 $this->variables
[$name] = array_merge($this->variables
[$name], $GLOBALS['civicrm_paths'][$name]);
130 if (isset($this->variables
[$name]['url'])) {
131 // Typical behavior is to return an absolute URL. If an admin has put an override that's site-relative, then convert.
132 $this->variables
[$name]['url'] = $this->toAbsoluteUrl($this->variables
[$name]['url'], $name);
135 if (!isset($this->variables
[$name][$attr])) {
136 throw new \
RuntimeException("Cannot resolve path using \"$name.$attr\"");
138 return $this->variables
[$name][$attr];
143 * Ex: 'https://example.com:8000/foobar' or '/foobar'
145 * Ex: 'civicrm.root' or 'civicrm.packages'
148 private function toAbsoluteUrl($url, $for) {
152 elseif ($url[0] === '/') {
153 // Relative URL interpretation
154 if ($for === 'cms.root') {
155 throw new \
RuntimeException('Invalid configuration: the [cms.root] path must be an absolute URL');
157 $cmsUrl = rtrim($this->getVariable('cms.root', 'url'), '/');
158 // The norms for relative URLs dictate:
159 // Single-slash: "/sub/dir" or "/" (domain-relative)
160 // Double-slash: "//example.com/sub/dir" (same-scheme)
161 $prefix = ($url === '/' ||
$url[1] !== '/')
163 : (parse_url($cmsUrl, PHP_URL_SCHEME
) . ':');
164 return $prefix . $url;
167 // Assume this is an absolute URL, as in the past ('_h_ttp://').
173 * Does the variable exist.
175 * @param string $name
179 public function hasVariable($name) {
180 return isset($this->variableFactory
[$name]);
184 * Determine the absolute path to a file, given that the file is most likely
185 * in a given particular variable.
187 * @param string $value
189 * Use "." to reference to default file root.
190 * Values may begin with a variable, e.g. "[civicrm.files]/upload".
191 * @return mixed|string
193 public function getPath($value) {
194 if ($value === NULL ||
$value === FALSE ||
$value === '') {
198 $defaultContainer = self
::DEFAULT_PATH
;
199 if ($value && $value{0} == '[' && preg_match(';^\[([a-zA-Z0-9\._]+)\]/(.*);', $value, $matches)) {
200 $defaultContainer = $matches[1];
201 $value = $matches[2];
204 $isDot = $value === '.';
209 $result = \CRM_Utils_File
::absoluteDirectory($value, $this->getVariable($defaultContainer, 'path'));
210 return $isDot ?
rtrim($result, '/' . DIRECTORY_SEPARATOR
) : $result;
214 * Determine the URL to a file.
216 * @param string $value
217 * The file path. The path may begin with a variable, e.g. "[civicrm.files]/upload".
219 * This function was designed for locating files under a given tree, and the
220 * the result for a straight variable expressions ("[foo.bar]") was not
221 * originally defined. You may wish to use one of these:
223 * - getVariable('foo.bar', 'url') => Lookup variable by itself
224 * - getUrl('[foo.bar]/') => Get the variable (normalized with a trailing "/").
225 * - getUrl('[foo.bar]/.') => Get the variable (normalized without a trailing "/").
226 * @param string $preferFormat
227 * The preferred format ('absolute', 'relative').
228 * The result data may not meet the preference -- if the setting
229 * refers to an external domain, then the result will be
230 * absolute (regardless of preference).
231 * @param bool|NULL $ssl
232 * NULL to autodetect. TRUE to force to SSL.
233 * @return FALSE|string
234 * The URL for $value (string), or FALSE if the $value is not specified.
236 public function getUrl($value, $preferFormat = 'relative', $ssl = NULL) {
237 if ($value === NULL ||
$value === FALSE ||
$value === '') {
241 $defaultContainer = self
::DEFAULT_URL
;
242 if ($value && $value{0} == '[' && preg_match(';^\[([a-zA-Z0-9\._]+)\](/(.*))$;', $value, $matches)) {
243 $defaultContainer = $matches[1];
244 $value = $matches[3];
247 $isDot = $value === '.';
248 if (substr($value, 0, 5) === 'http:' ||
substr($value, 0, 6) === 'https:') {
252 $value = rtrim($this->getVariable($defaultContainer, 'url'), '/') . ($isDot ?
'' : "/$value");
254 if ($preferFormat === 'relative') {
255 $parsed = parse_url($value);
256 if (isset($_SERVER['HTTP_HOST']) && isset($parsed['host']) && $_SERVER['HTTP_HOST'] == $parsed['host']) {
257 $value = $parsed['path'];
261 if ($ssl ||
($ssl === NULL && \CRM_Utils_System
::isSSL())) {
262 $value = str_replace('http://', 'https://', $value);