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.files', function () {
38 return \CRM_Core_Config
::singleton()->userSystem
->getDefaultFileStorage();
40 ->register('cms', function () {
42 'path' => \CRM_Core_Config
::singleton()->userSystem
->cmsRootPath(),
43 'url' => \CRM_Utils_System
::baseCMSURL(),
46 ->register('cms.root', function () {
48 'path' => \CRM_Core_Config
::singleton()->userSystem
->cmsRootPath(),
49 // Misleading: this *removes* the language part of the URL, producing a pristine base URL.
50 'url' => \CRM_Utils_System
::languageNegotiationURL(\CRM_Utils_System
::baseCMSURL(), FALSE, TRUE),
56 * Register a new URL/file path mapping.
59 * The name of the variable.
60 * @param callable $factory
61 * Function which returns an array with keys:
66 public function register($name, $factory) {
67 $this->variableFactory
[$name] = $factory;
78 public function getVariable($name, $attr) {
79 if (!isset($this->variables
[$name])) {
80 $this->variables
[$name] = call_user_func($this->variableFactory
[$name]);
82 if (!isset($this->variables
[$name][$attr])) {
83 throw new \
RuntimeException("Cannot resolve path using \"$name.$attr\"");
85 return $this->variables
[$name][$attr];
89 * Does the variable exist.
95 public function hasVariable($name) {
96 return isset($this->variableFactory
[$name]);
100 * Determine the absolute path to a file, given that the file is most likely
101 * in a given particular variable.
103 * @param string $value
105 * Use "." to reference to default file root.
106 * Values may begin with a variable, e.g. "[civicrm.files]/upload".
107 * @return mixed|string
109 public function getPath($value) {
110 $defaultContainer = self
::DEFAULT_PATH
;
111 if ($value && $value{0} == '[' && preg_match(';^\[([a-zA-Z0-9\._]+)\]/(.*);', $value, $matches)) {
112 $defaultContainer = $matches[1];
113 $value = $matches[2];
118 if ($value === '.') {
121 return \CRM_Utils_File
::absoluteDirectory($value, $this->getVariable($defaultContainer, 'path'));
125 * Determine the URL to a file.
127 * @param string $value
128 * The file path. The path may begin with a variable, e.g. "[civicrm.files]/upload".
129 * @param string $preferFormat
130 * The preferred format ('absolute', 'relative').
131 * The result data may not meet the preference -- if the setting
132 * refers to an external domain, then the result will be
133 * absolute (regardless of preference).
134 * @param bool|NULL $ssl
135 * NULL to autodetect. TRUE to force to SSL.
136 * @return mixed|string
138 public function getUrl($value, $preferFormat = 'relative', $ssl = NULL) {
139 $defaultContainer = self
::DEFAULT_URL
;
140 if ($value && $value{0} == '[' && preg_match(';^\[([a-zA-Z0-9\._]+)\](/(.*))$;', $value, $matches)) {
141 $defaultContainer = $matches[1];
142 $value = empty($matches[3]) ?
'.' : $matches[3];
148 if ($value === '.') {
151 if (substr($value, 0, 4) == 'http') {
155 $value = $this->getVariable($defaultContainer, 'url') . $value;
157 if ($preferFormat === 'relative') {
158 $parsed = parse_url($value);
159 if (isset($_SERVER['HTTP_HOST']) && isset($parsed['host']) && $_SERVER['HTTP_HOST'] == $parsed['host']) {
160 $value = $parsed['path'];
164 if ($ssl ||
($ssl === NULL && \CRM_Utils_System
::isSSL())) {
165 $value = str_replace('http://', 'https://', $value);