Merge pull request #14128 from agileware/CIVICRM-1143
[civicrm-core.git] / CRM / Core / Config / Runtime.php
CommitLineData
c0a1f187
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
c0a1f187 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
c0a1f187
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 * Class CRM_Core_Config_Runtime
30 *
31 * The runtime describes the environment in which CiviCRM executes -- ie
32 * the DSN, CMS type, CMS URL, etc. Generally, runtime properties must be
33 * determined externally (before loading CiviCRM).
34 */
1b81ed50 35class CRM_Core_Config_Runtime extends CRM_Core_Config_MagicMerge {
c0a1f187
TO
36
37 public $dsn;
38
39 /**
40 * The name of user framework
41 *
42 * @var string
43 */
44 public $userFramework;
45
46 public $userFrameworkBaseURL;
47
48 public $userFrameworkClass;
49
50 /**
51 * The dsn of the database connection for user framework
52 *
53 * @var string
54 */
55 public $userFrameworkDSN;
56
57 /**
58 * The name of user framework url variable name
59 *
60 * @var string
61 */
62 public $userFrameworkURLVar = 'q';
63
606999ec
TO
64 public $userFrameworkVersion;
65
c0a1f187
TO
66 public $useFrameworkRelativeBase;
67
68 public $userHookClass;
69
c0a1f187
TO
70 /**
71 * Are we generating clean url's and using mod_rewrite
72 * @var string
73 */
74 public $cleanURL;
75
76 /**
77 * @var string
78 */
79 public $configAndLogDir;
80
81 public $templateCompileDir;
82
83 /**
84 * The root directory of our template tree.
85 * @var string
86 */
87 public $templateDir;
88
c0a1f187
TO
89 /**
90 * @param bool $loadFromDB
91 */
92 public function initialize($loadFromDB = TRUE) {
93 if (!defined('CIVICRM_DSN') && $loadFromDB) {
94 $this->fatal('You need to define CIVICRM_DSN in civicrm.settings.php');
95 }
96 $this->dsn = defined('CIVICRM_DSN') ? CIVICRM_DSN : NULL;
97
98 if (!defined('CIVICRM_TEMPLATE_COMPILEDIR') && $loadFromDB) {
99 $this->fatal('You need to define CIVICRM_TEMPLATE_COMPILEDIR in civicrm.settings.php');
100 }
101
102 if (defined('CIVICRM_TEMPLATE_COMPILEDIR')) {
103 $this->configAndLogDir = CRM_Utils_File::baseFilePath() . 'ConfigAndLog' . DIRECTORY_SEPARATOR;
104 CRM_Utils_File::createDir($this->configAndLogDir);
105 CRM_Utils_File::restrictAccess($this->configAndLogDir);
106
107 $this->templateCompileDir = defined('CIVICRM_TEMPLATE_COMPILEDIR') ? CRM_Utils_File::addTrailingSlash(CIVICRM_TEMPLATE_COMPILEDIR) : NULL;
108 CRM_Utils_File::createDir($this->templateCompileDir);
109 CRM_Utils_File::restrictAccess($this->templateCompileDir);
110 }
111
c0a1f187
TO
112 if (!defined('CIVICRM_UF')) {
113 $this->fatal('You need to define CIVICRM_UF in civicrm.settings.php');
114 }
c0a1f187 115
d4330c62
TO
116 $this->userFramework = CIVICRM_UF;
117 $this->userFrameworkClass = 'CRM_Utils_System_' . CIVICRM_UF;
118 $this->userHookClass = 'CRM_Utils_Hook_' . CIVICRM_UF;
c0a1f187 119
d4330c62 120 if (CIVICRM_UF == 'Joomla') {
c0a1f187
TO
121 $this->userFrameworkURLVar = 'task';
122 }
123
c0a1f187
TO
124 if (defined('CIVICRM_UF_DSN')) {
125 $this->userFrameworkDSN = CIVICRM_UF_DSN;
126 }
127
128 // this is dynamically figured out in the civicrm.settings.php file
129 if (defined('CIVICRM_CLEANURL')) {
130 $this->cleanURL = CIVICRM_CLEANURL;
131 }
132 else {
133 $this->cleanURL = 0;
134 }
d4330c62 135
be2fb01f 136 $this->templateDir = [dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR];
d4330c62 137
d4330c62 138 $this->initialized = 1;
c0a1f187
TO
139 }
140
f2ac86d1 141 /**
142 * Exit processing after a fatal event, outputting the message.
143 *
144 * @param string $message
145 */
c0a1f187
TO
146 private function fatal($message) {
147 echo $message;
148 exit();
149 }
150
1b81ed50 151 /**
152 * Include custom PHP and template paths
153 */
154 public function includeCustomPath() {
be2fb01f 155 $customProprtyName = ['customPHPPathDir', 'customTemplateDir'];
1b81ed50 156 foreach ($customProprtyName as $property) {
157 $value = $this->getSettings()->get($property);
158 if (!empty($value)) {
159 $customPath = Civi::paths()->getPath($value);
160 set_include_path($customPath . PATH_SEPARATOR . get_include_path());
161 }
162 }
163 }
164
83617886
TO
165 /**
166 * Create a unique identification code for this runtime.
167 *
168 * If two requests involve a different hostname, different
169 * port, different DSN, etc., then they should also have a
170 * different runtime ID.
171 *
172 * @return mixed
173 */
174 public static function getId() {
175 if (!isset(Civi::$statics[__CLASS__]['id'])) {
be2fb01f 176 Civi::$statics[__CLASS__]['id'] = md5(implode(\CRM_Core_DAO::VALUE_SEPARATOR, [
518fa0ee
SL
177 // e.g. one database, multi URL
178 defined('CIVICRM_DOMAIN_ID') ? CIVICRM_DOMAIN_ID : 1,
179 // e.g. one codebase, multi database
180 parse_url(CIVICRM_DSN, PHP_URL_PATH),
181 // e.g. CMS vs extern vs installer
182 \CRM_Utils_Array::value('SCRIPT_FILENAME', $_SERVER, ''),
183 // e.g. name-based vhosts
184 \CRM_Utils_Array::value('HTTP_HOST', $_SERVER, ''),
185 // e.g. port-based vhosts
186 \CRM_Utils_Array::value('SERVER_PORT', $_SERVER, ''),
83617886 187 // Depending on deployment arch, these signals *could* be redundant, but who cares?
be2fb01f 188 ]));
83617886
TO
189 }
190 return Civi::$statics[__CLASS__]['id'];
191 }
192
c0a1f187 193}