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 = array();
27 private $variableFactory = array();
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/'),
43 ->register('civicrm.vendor', function () {
45 'path' => \Civi
::paths()->getPath('[civicrm.root]/vendor/'),
46 'url' => \Civi
::paths()->getUrl('[civicrm.root]/vendor/'),
49 ->register('civicrm.bower', function () {
51 'path' => \Civi
::paths()->getPath('[civicrm.root]/bower_components/'),
52 'url' => \Civi
::paths()->getUrl('[civicrm.root]/bower_components/'),
55 ->register('civicrm.files', function () {
56 return \CRM_Core_Config
::singleton()->userSystem
->getDefaultFileStorage();
58 ->register('cms', function () {
60 'path' => \CRM_Core_Config
::singleton()->userSystem
->cmsRootPath(),
61 'url' => \CRM_Utils_System
::baseCMSURL(),
64 ->register('cms.root', function () {
66 'path' => \CRM_Core_Config
::singleton()->userSystem
->cmsRootPath(),
67 // Misleading: this *removes* the language part of the URL, producing a pristine base URL.
68 'url' => \CRM_Utils_System
::languageNegotiationURL(\CRM_Utils_System
::baseCMSURL(), FALSE, TRUE),
74 * Register a new URL/file path mapping.
77 * The name of the variable.
78 * @param callable $factory
79 * Function which returns an array with keys:
84 public function register($name, $factory) {
85 $this->variableFactory
[$name] = $factory;
96 public function getVariable($name, $attr) {
97 if (!isset($this->variables
[$name])) {
98 $this->variables
[$name] = call_user_func($this->variableFactory
[$name]);
99 if (isset($GLOBALS['civicrm_paths'][$name])) {
100 $this->variables
[$name] = array_merge($this->variables
[$name], $GLOBALS['civicrm_paths'][$name]);
103 if (!isset($this->variables
[$name][$attr])) {
104 throw new \
RuntimeException("Cannot resolve path using \"$name.$attr\"");
106 return $this->variables
[$name][$attr];
110 * Does the variable exist.
112 * @param string $name
116 public function hasVariable($name) {
117 return isset($this->variableFactory
[$name]);
121 * Determine the absolute path to a file, given that the file is most likely
122 * in a given particular variable.
124 * @param string $value
126 * Use "." to reference to default file root.
127 * Values may begin with a variable, e.g. "[civicrm.files]/upload".
128 * @return mixed|string
130 public function getPath($value) {
131 $defaultContainer = self
::DEFAULT_PATH
;
132 if ($value && $value{0} == '[' && preg_match(';^\[([a-zA-Z0-9\._]+)\]/(.*);', $value, $matches)) {
133 $defaultContainer = $matches[1];
134 $value = $matches[2];
139 if ($value === '.') {
142 return \CRM_Utils_File
::absoluteDirectory($value, $this->getVariable($defaultContainer, 'path'));
146 * Determine the URL to a file.
148 * @param string $value
149 * The file path. The path may begin with a variable, e.g. "[civicrm.files]/upload".
150 * @param string $preferFormat
151 * The preferred format ('absolute', 'relative').
152 * The result data may not meet the preference -- if the setting
153 * refers to an external domain, then the result will be
154 * absolute (regardless of preference).
155 * @param bool|NULL $ssl
156 * NULL to autodetect. TRUE to force to SSL.
157 * @return mixed|string
159 public function getUrl($value, $preferFormat = 'relative', $ssl = NULL) {
160 $defaultContainer = self
::DEFAULT_URL
;
161 if ($value && $value{0} == '[' && preg_match(';^\[([a-zA-Z0-9\._]+)\](/(.*))$;', $value, $matches)) {
162 $defaultContainer = $matches[1];
163 $value = empty($matches[3]) ?
'.' : $matches[3];
169 if ($value === '.') {
172 if (substr($value, 0, 4) == 'http') {
176 $value = $this->getVariable($defaultContainer, 'url') . $value;
178 if ($preferFormat === 'relative') {
179 $parsed = parse_url($value);
180 if (isset($_SERVER['HTTP_HOST']) && isset($parsed['host']) && $_SERVER['HTTP_HOST'] == $parsed['host']) {
181 $value = $parsed['path'];
185 if ($ssl ||
($ssl === NULL && \CRM_Utils_System
::isSSL())) {
186 $value = str_replace('http://', 'https://', $value);