Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 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 | * Config handles all the run time configuration changes that the system needs to deal with. | |
30 | * Typically we'll have different values for a user's sandbox, a qa sandbox and a production area. | |
31 | * The default values in general, should reflect production values (minimizes chances of screwing up) | |
32 | * | |
33 | * @package CRM | |
06b69b18 | 34 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
35 | * $Id$ |
36 | * | |
37 | */ | |
38 | ||
39 | require_once 'Log.php'; | |
40 | require_once 'Mail.php'; | |
41 | ||
42 | require_once 'api/api.php'; | |
72536736 | 43 | |
28518c90 EM |
44 | /** |
45 | * Class CRM_Core_Config | |
46 | */ | |
6a488035 TO |
47 | class CRM_Core_Config extends CRM_Core_Config_Variables { |
48 | /// | |
49 | /// BASE SYSTEM PROPERTIES (CIVICRM.SETTINGS.PHP) | |
50 | /// | |
51 | ||
52 | /** | |
100fef9d | 53 | * The dsn of the database connection |
72536736 | 54 | * |
6a488035 TO |
55 | * @var string |
56 | */ | |
57 | public $dsn; | |
58 | ||
59 | /** | |
100fef9d | 60 | * The name of user framework |
72536736 | 61 | * |
6a488035 TO |
62 | * @var string |
63 | */ | |
64 | public $userFramework = 'Drupal'; | |
65 | ||
66 | /** | |
100fef9d | 67 | * The name of user framework url variable name |
72536736 | 68 | * |
6a488035 TO |
69 | * @var string |
70 | */ | |
71 | public $userFrameworkURLVar = 'q'; | |
72 | ||
73 | /** | |
100fef9d | 74 | * The dsn of the database connection for user framework |
72536736 | 75 | * |
6a488035 TO |
76 | * @var string |
77 | */ | |
78 | public $userFrameworkDSN = NULL; | |
79 | ||
80 | /** | |
81 | * The connector module for the CMS/UF | |
a6111172 | 82 | * @todo Introduce an interface. |
6a488035 | 83 | * |
a6111172 | 84 | * @var CRM_Utils_System_Base |
6a488035 TO |
85 | */ |
86 | public $userSystem = NULL; | |
87 | ||
dcbec360 EM |
88 | /** |
89 | * @var CRM_Core_Permission_Base | |
90 | */ | |
91 | public $userPermissionClass; | |
92 | ||
6a488035 | 93 | /** |
72536736 AH |
94 | * The root directory where Smarty should store compiled files |
95 | * | |
6a488035 TO |
96 | * @var string |
97 | */ | |
98 | public $templateCompileDir = './templates_c/en_US/'; | |
99 | ||
72536736 AH |
100 | /** |
101 | * @var string | |
102 | */ | |
6a488035 TO |
103 | public $configAndLogDir = NULL; |
104 | ||
105 | // END: BASE SYSTEM PROPERTIES (CIVICRM.SETTINGS.PHP) | |
106 | ||
107 | /// | |
108 | /// BEGIN HELPER CLASS PROPERTIES | |
109 | /// | |
110 | ||
111 | /** | |
100fef9d | 112 | * Are we initialized and in a proper state |
72536736 | 113 | * |
6a488035 TO |
114 | * @var string |
115 | */ | |
116 | public $initialized = 0; | |
117 | ||
4d66768d AH |
118 | /** |
119 | * @var string | |
120 | */ | |
121 | public $customPHPPathDir; | |
122 | ||
6a488035 | 123 | /** |
100fef9d | 124 | * The factory class used to instantiate our DB objects |
72536736 | 125 | * |
6a488035 TO |
126 | * @var string |
127 | */ | |
128 | private $DAOFactoryClass = 'CRM_Contact_DAO_Factory'; | |
129 | ||
130 | /** | |
131 | * The handle to the log that we are using | |
132 | * @var object | |
133 | */ | |
134 | private static $_log = NULL; | |
135 | ||
136 | /** | |
100fef9d | 137 | * The handle on the mail handler that we are using |
72536736 | 138 | * |
6a488035 TO |
139 | * @var object |
140 | */ | |
72ad6c1b | 141 | public static $_mail = NULL; |
6a488035 TO |
142 | |
143 | /** | |
144 | * We only need one instance of this object. So we use the singleton | |
145 | * pattern and cache the instance in this variable | |
72536736 AH |
146 | * |
147 | * @var CRM_Core_Config | |
6a488035 TO |
148 | */ |
149 | private static $_singleton = NULL; | |
150 | ||
151 | /** | |
72536736 | 152 | * @var CRM_Core_Component |
6a488035 TO |
153 | */ |
154 | public $componentRegistry = NULL; | |
155 | ||
156 | /// | |
157 | /// END HELPER CLASS PROPERTIES | |
158 | /// | |
159 | ||
160 | /// | |
161 | /// RUNTIME SET CLASS PROPERTIES | |
162 | /// | |
163 | ||
164 | /** | |
72536736 AH |
165 | * @var bool |
166 | * TRUE, if the call is CiviCRM. | |
167 | * FALSE, if the call is from the CMS. | |
6a488035 TO |
168 | */ |
169 | public $inCiviCRM = FALSE; | |
170 | ||
171 | /// | |
172 | /// END: RUNTIME SET CLASS PROPERTIES | |
173 | /// | |
174 | ||
175 | /** | |
72536736 | 176 | * @var string |
6a488035 | 177 | */ |
6a488035 TO |
178 | public $recaptchaPublicKey; |
179 | ||
180 | /** | |
181 | * The constructor. Sets domain id if defined, otherwise assumes | |
182 | * single instance installation. | |
6a488035 TO |
183 | */ |
184 | private function __construct() { | |
185 | } | |
186 | ||
187 | /** | |
188 | * Singleton function used to manage this object. | |
189 | * | |
190 | * @param $loadFromDB boolean whether to load from the database | |
191 | * @param $force boolean whether to force a reconstruction | |
192 | * | |
5af8c999 | 193 | * @return CRM_Core_Config |
6a488035 TO |
194 | * @static |
195 | */ | |
00be9182 | 196 | public static function &singleton($loadFromDB = TRUE, $force = FALSE) { |
6a488035 TO |
197 | if (self::$_singleton === NULL || $force) { |
198 | // goto a simple error handler | |
ca32aecc TO |
199 | $GLOBALS['civicrm_default_error_scope'] = CRM_Core_TemporaryErrorScope::create(array('CRM_Core_Error', 'handle')); |
200 | $errorScope = CRM_Core_TemporaryErrorScope::create(array('CRM_Core_Error', 'simpleHandler')); | |
6a488035 TO |
201 | |
202 | // lets ensure we set E_DEPRECATED to minimize errors | |
203 | // CRM-6327 | |
204 | if (defined('E_DEPRECATED')) { | |
205 | error_reporting(error_reporting() & ~E_DEPRECATED); | |
206 | } | |
207 | ||
208 | // first, attempt to get configuration object from cache | |
209 | $cache = CRM_Utils_Cache::singleton(); | |
0e04f44e | 210 | self::$_singleton = $cache->get('CRM_Core_Config' . CRM_Core_Config::domainID()); |
6a488035 TO |
211 | // if not in cache, fire off config construction |
212 | if (!self::$_singleton) { | |
213 | self::$_singleton = new CRM_Core_Config; | |
214 | self::$_singleton->_initialize($loadFromDB); | |
215 | ||
216 | //initialize variables. for gencode we cannot load from the | |
217 | //db since the db might not be initialized | |
218 | if ($loadFromDB) { | |
2ba175b6 DL |
219 | // initialize stuff from the settings file |
220 | self::$_singleton->setCoreVariables(); | |
221 | ||
6a488035 TO |
222 | self::$_singleton->_initVariables(); |
223 | ||
b2560503 | 224 | // I don't think we need to do this twice |
2ba175b6 DL |
225 | // however just keeping this commented for now in 4.4 |
226 | // in case we hit any issues - CRM-13064 | |
227 | // We can safely delete this once we release 4.4.4 | |
228 | // self::$_singleton->setCoreVariables(); | |
6a488035 | 229 | } |
0e04f44e | 230 | $cache->set('CRM_Core_Config' . CRM_Core_Config::domainID(), self::$_singleton); |
6a488035 TO |
231 | } |
232 | else { | |
233 | // we retrieve the object from memcache, so we now initialize the objects | |
234 | self::$_singleton->_initialize($loadFromDB); | |
235 | ||
236 | // CRM-9803, NYSS-4822 | |
237 | // this causes various settings to be reset and hence we should | |
b2560503 | 238 | // only use the config object that we retrieved from memcache |
6a488035 TO |
239 | } |
240 | ||
241 | self::$_singleton->initialized = 1; | |
242 | ||
243 | if (isset(self::$_singleton->customPHPPathDir) && | |
244 | self::$_singleton->customPHPPathDir | |
245 | ) { | |
246 | $include_path = self::$_singleton->customPHPPathDir . PATH_SEPARATOR . get_include_path(); | |
247 | set_include_path($include_path); | |
248 | } | |
249 | ||
250 | // set the callback at the very very end, to avoid an infinite loop | |
251 | // set the error callback | |
ca32aecc | 252 | unset($errorScope); |
6a488035 TO |
253 | |
254 | // call the hook so other modules can add to the config | |
255 | // again doing this at the very very end | |
256 | CRM_Utils_Hook::config(self::$_singleton); | |
257 | ||
258 | // make sure session is always initialised | |
259 | $session = CRM_Core_Session::singleton(); | |
260 | ||
261 | // for logging purposes, pass the userID to the db | |
262 | $userID = $session->get('userID'); | |
263 | if ($userID) { | |
264 | CRM_Core_DAO::executeQuery('SET @civicrm_user_id = %1', | |
265 | array(1 => array($userID, 'Integer')) | |
266 | ); | |
267 | } | |
e8f14831 DS |
268 | |
269 | // initialize authentication source | |
270 | self::$_singleton->initAuthSrc(); | |
6a488035 TO |
271 | } |
272 | return self::$_singleton; | |
273 | } | |
274 | ||
72536736 AH |
275 | /** |
276 | * @param string $userFramework | |
277 | * One of 'Drupal', 'Joomla', etc. | |
278 | */ | |
6a488035 TO |
279 | private function _setUserFrameworkConfig($userFramework) { |
280 | ||
281 | $this->userFrameworkClass = 'CRM_Utils_System_' . $userFramework; | |
282 | $this->userHookClass = 'CRM_Utils_Hook_' . $userFramework; | |
283 | $userPermissionClass = 'CRM_Core_Permission_' . $userFramework; | |
284 | $this->userPermissionClass = new $userPermissionClass(); | |
285 | ||
286 | $class = $this->userFrameworkClass; | |
287 | // redundant with _initVariables | |
c85aab98 | 288 | $this->userSystem = new $class(); |
6a488035 TO |
289 | |
290 | if ($userFramework == 'Joomla') { | |
291 | $this->userFrameworkURLVar = 'task'; | |
292 | } | |
293 | ||
294 | if (defined('CIVICRM_UF_BASEURL')) { | |
295 | $this->userFrameworkBaseURL = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/'); | |
296 | ||
297 | //format url for language negotiation, CRM-7803 | |
298 | $this->userFrameworkBaseURL = CRM_Utils_System::languageNegotiationURL($this->userFrameworkBaseURL); | |
299 | ||
300 | if (CRM_Utils_System::isSSL()) { | |
301 | $this->userFrameworkBaseURL = str_replace('http://', 'https://', | |
302 | $this->userFrameworkBaseURL | |
303 | ); | |
304 | } | |
305 | } | |
306 | ||
307 | if (defined('CIVICRM_UF_DSN')) { | |
308 | $this->userFrameworkDSN = CIVICRM_UF_DSN; | |
309 | } | |
310 | ||
311 | // this is dynamically figured out in the civicrm.settings.php file | |
312 | if (defined('CIVICRM_CLEANURL')) { | |
313 | $this->cleanURL = CIVICRM_CLEANURL; | |
314 | } | |
315 | else { | |
316 | $this->cleanURL = 0; | |
317 | } | |
318 | ||
c85aab98 | 319 | $this->userFrameworkVersion = $this->userSystem->getVersion(); |
6a488035 TO |
320 | |
321 | if ($userFramework == 'Joomla') { | |
72536736 | 322 | /** @var object|null $mainframe */ |
6a488035 TO |
323 | global $mainframe; |
324 | $dbprefix = $mainframe ? $mainframe->getCfg('dbprefix') : 'jos_'; | |
325 | $this->userFrameworkUsersTableName = $dbprefix . 'users'; | |
326 | } | |
327 | elseif ($userFramework == 'WordPress') { | |
328 | global $wpdb; | |
329 | $dbprefix = $wpdb ? $wpdb->prefix : ''; | |
330 | $this->userFrameworkUsersTableName = $dbprefix . 'users'; | |
331 | } | |
332 | } | |
333 | ||
334 | /** | |
335 | * Initializes the entire application. | |
336 | * Reads constants defined in civicrm.settings.php and | |
337 | * stores them in config properties. | |
338 | * | |
72536736 | 339 | * @param bool $loadFromDB |
6a488035 TO |
340 | */ |
341 | private function _initialize($loadFromDB = TRUE) { | |
342 | ||
343 | // following variables should be set in CiviCRM settings and | |
344 | // as crucial ones, are defined upon initialisation | |
345 | // instead of in CRM_Core_Config_Defaults | |
346 | if (defined('CIVICRM_DSN')) { | |
347 | $this->dsn = CIVICRM_DSN; | |
348 | } | |
349 | elseif ($loadFromDB) { | |
350 | // bypass when calling from gencode | |
351 | echo 'You need to define CIVICRM_DSN in civicrm.settings.php'; | |
352 | exit(); | |
353 | } | |
354 | ||
355 | if (defined('CIVICRM_TEMPLATE_COMPILEDIR')) { | |
356 | $this->templateCompileDir = CRM_Utils_File::addTrailingSlash(CIVICRM_TEMPLATE_COMPILEDIR); | |
357 | ||
358 | // also make sure we create the config directory within this directory | |
359 | // the below statement will create both the templates directory and the config and log directory | |
b597d0b1 DL |
360 | $this->configAndLogDir = |
361 | CRM_Utils_File::baseFilePath($this->templateCompileDir) . | |
362 | 'ConfigAndLog' . DIRECTORY_SEPARATOR; | |
6a488035 | 363 | CRM_Utils_File::createDir($this->configAndLogDir); |
fea6131e | 364 | CRM_Utils_File::restrictAccess($this->configAndLogDir); |
6a488035 TO |
365 | |
366 | // we're automatically prefixing compiled templates directories with country/language code | |
367 | global $tsLocale; | |
368 | if (!empty($tsLocale)) { | |
369 | $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($tsLocale); | |
370 | } | |
371 | elseif (!empty($this->lcMessages)) { | |
372 | $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($this->lcMessages); | |
373 | } | |
374 | ||
375 | CRM_Utils_File::createDir($this->templateCompileDir); | |
fea6131e | 376 | CRM_Utils_File::restrictAccess($this->templateCompileDir); |
6a488035 TO |
377 | } |
378 | elseif ($loadFromDB) { | |
379 | echo 'You need to define CIVICRM_TEMPLATE_COMPILEDIR in civicrm.settings.php'; | |
380 | exit(); | |
381 | } | |
382 | ||
383 | $this->_initDAO(); | |
384 | ||
385 | if (defined('CIVICRM_UF')) { | |
386 | $this->userFramework = CIVICRM_UF; | |
387 | $this->_setUserFrameworkConfig($this->userFramework); | |
388 | } | |
389 | else { | |
390 | echo 'You need to define CIVICRM_UF in civicrm.settings.php'; | |
391 | exit(); | |
392 | } | |
393 | ||
394 | // also initialize the logger | |
395 | self::$_log = Log::singleton('display'); | |
396 | ||
397 | // initialize component registry early to avoid "race" | |
398 | // between CRM_Core_Config and CRM_Core_Component (they | |
399 | // are co-dependant) | |
400 | $this->componentRegistry = new CRM_Core_Component(); | |
401 | } | |
402 | ||
403 | /** | |
100fef9d | 404 | * Initialize the DataObject framework |
6a488035 TO |
405 | * |
406 | * @return void | |
6a488035 TO |
407 | */ |
408 | private function _initDAO() { | |
409 | CRM_Core_DAO::init($this->dsn); | |
410 | ||
411 | $factoryClass = $this->DAOFactoryClass; | |
412 | require_once str_replace('_', DIRECTORY_SEPARATOR, $factoryClass) . '.php'; | |
413 | CRM_Core_DAO::setFactory(new $factoryClass()); | |
414 | if (CRM_Utils_Constant::value('CIVICRM_MYSQL_STRICT', CRM_Utils_System::isDevelopment())) { | |
415 | CRM_Core_DAO::executeQuery('SET SESSION sql_mode = STRICT_TRANS_TABLES'); | |
416 | } | |
417 | } | |
418 | ||
419 | /** | |
100fef9d | 420 | * Returns the singleton logger for the application |
6a488035 TO |
421 | * |
422 | * @param | |
6a488035 TO |
423 | * |
424 | * @return object | |
425 | */ | |
426 | static public function &getLog() { | |
427 | if (!isset(self::$_log)) { | |
428 | self::$_log = Log::singleton('display'); | |
429 | } | |
430 | ||
431 | return self::$_log; | |
432 | } | |
433 | ||
434 | /** | |
100fef9d | 435 | * Initialize the config variables |
6a488035 TO |
436 | * |
437 | * @return void | |
6a488035 TO |
438 | */ |
439 | private function _initVariables() { | |
440 | // retrieve serialised settings | |
441 | $variables = array(); | |
442 | CRM_Core_BAO_ConfigSetting::retrieve($variables); | |
443 | ||
444 | // if settings are not available, go down the full path | |
445 | if (empty($variables)) { | |
446 | // Step 1. get system variables with their hardcoded defaults | |
447 | $variables = get_object_vars($this); | |
448 | ||
449 | // Step 2. get default values (with settings file overrides if | |
450 | // available - handled in CRM_Core_Config_Defaults) | |
451 | CRM_Core_Config_Defaults::setValues($variables); | |
452 | ||
453 | // retrieve directory and url preferences also | |
454 | CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($variables); | |
455 | ||
456 | // add component specific settings | |
457 | $this->componentRegistry->addConfig($this); | |
458 | ||
459 | // serialise settings | |
460 | $settings = $variables; | |
461 | CRM_Core_BAO_ConfigSetting::add($settings); | |
462 | } | |
463 | ||
464 | $urlArray = array('userFrameworkResourceURL', 'imageUploadURL'); | |
465 | $dirArray = array('uploadDir', 'customFileUploadDir'); | |
466 | ||
467 | foreach ($variables as $key => $value) { | |
468 | if (in_array($key, $urlArray)) { | |
469 | $value = CRM_Utils_File::addTrailingSlash($value, '/'); | |
470 | } | |
471 | elseif (in_array($key, $dirArray)) { | |
472 | if ($value) { | |
473 | $value = CRM_Utils_File::addTrailingSlash($value); | |
474 | } | |
475 | if (empty($value) || (CRM_Utils_File::createDir($value, FALSE) === FALSE)) { | |
476 | // seems like we could not create the directories | |
477 | // settings might have changed, lets suppress a message for now | |
478 | // so we can make some more progress and let the user fix their settings | |
479 | // for now we assign it to a know value | |
480 | // CRM-4949 | |
481 | $value = $this->templateCompileDir; | |
482 | $url = CRM_Utils_System::url('civicrm/admin/setting/path', 'reset=1'); | |
483 | CRM_Core_Session::setStatus(ts('%1 has an incorrect directory path. Please go to the <a href="%2">path setting page</a> and correct it.', array( | |
484 | 1 => $key, | |
485 | 2 => $url | |
486 | )), ts('Check Settings'), 'alert'); | |
487 | } | |
488 | } | |
489 | elseif ($key == 'lcMessages') { | |
490 | // reset the templateCompileDir to locale-specific and make sure it exists | |
491 | if (substr($this->templateCompileDir, -1 * strlen($value) - 1, -1) != $value) { | |
492 | $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($value); | |
493 | CRM_Utils_File::createDir($this->templateCompileDir); | |
fea6131e | 494 | CRM_Utils_File::restrictAccess($this->templateCompileDir); |
6a488035 TO |
495 | } |
496 | } | |
497 | ||
498 | $this->$key = $value; | |
499 | } | |
500 | ||
501 | if ($this->userFrameworkResourceURL) { | |
502 | // we need to do this here so all blocks also load from an ssl server | |
503 | if (CRM_Utils_System::isSSL()) { | |
504 | CRM_Utils_System::mapConfigToSSL(); | |
505 | } | |
506 | $rrb = parse_url($this->userFrameworkResourceURL); | |
b2560503 | 507 | // don't use absolute path if resources are stored on a different server |
6a488035 TO |
508 | // CRM-4642 |
509 | $this->resourceBase = $this->userFrameworkResourceURL; | |
510 | if (isset($_SERVER['HTTP_HOST']) && | |
511 | isset($rrb['host']) | |
512 | ) { | |
513 | $this->resourceBase = ($rrb['host'] == $_SERVER['HTTP_HOST']) ? $rrb['path'] : $this->userFrameworkResourceURL; | |
514 | } | |
515 | } | |
516 | ||
517 | if (!$this->customFileUploadDir) { | |
518 | $this->customFileUploadDir = $this->uploadDir; | |
519 | } | |
520 | ||
521 | if ($this->geoProvider) { | |
522 | $this->geocodeMethod = 'CRM_Utils_Geocode_' . $this->geoProvider; | |
523 | } | |
524 | elseif ($this->mapProvider) { | |
525 | $this->geocodeMethod = 'CRM_Utils_Geocode_' . $this->mapProvider; | |
526 | } | |
527 | ||
528 | require_once (str_replace('_', DIRECTORY_SEPARATOR, $this->userFrameworkClass) . '.php'); | |
529 | $class = $this->userFrameworkClass; | |
530 | // redundant with _setUserFrameworkConfig | |
531 | $this->userSystem = new $class(); | |
532 | } | |
533 | ||
534 | /** | |
3713d69c | 535 | * Retrieve a mailer to send any mail from the application |
6a488035 TO |
536 | * |
537 | * @param boolean $persist open a persistent smtp connection, should speed up mailings | |
6a488035 TO |
538 | * @return object |
539 | */ | |
00be9182 | 540 | public static function &getMailer($persist = FALSE) { |
6a488035 TO |
541 | if (!isset(self::$_mail)) { |
542 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, | |
543 | 'mailing_backend' | |
544 | ); | |
545 | ||
546 | if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB || | |
547 | (defined('CIVICRM_MAILER_SPOOL') && CIVICRM_MAILER_SPOOL) | |
548 | ) { | |
72ad6c1b | 549 | self::$_mail = self::_createMailer('CRM_Mailing_BAO_Spool', array()); |
6a488035 TO |
550 | } |
551 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { | |
552 | if ($mailingInfo['smtpServer'] == '' || !$mailingInfo['smtpServer']) { | |
9327d839 DL |
553 | CRM_Core_Error::debug_log_message(ts('There is no valid smtp server setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); |
554 | CRM_Core_Error::fatal(ts('There is no valid smtp server setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
6a488035 TO |
555 | } |
556 | ||
557 | $params['host'] = $mailingInfo['smtpServer'] ? $mailingInfo['smtpServer'] : 'localhost'; | |
558 | $params['port'] = $mailingInfo['smtpPort'] ? $mailingInfo['smtpPort'] : 25; | |
559 | ||
560 | if ($mailingInfo['smtpAuth']) { | |
561 | $params['username'] = $mailingInfo['smtpUsername']; | |
562 | $params['password'] = CRM_Utils_Crypt::decrypt($mailingInfo['smtpPassword']); | |
563 | $params['auth'] = TRUE; | |
564 | } | |
565 | else { | |
566 | $params['auth'] = FALSE; | |
567 | } | |
568 | ||
569 | // set the localhost value, CRM-3153 | |
570 | $params['localhost'] = CRM_Utils_Array::value('SERVER_NAME', $_SERVER, 'localhost'); | |
571 | ||
572 | // also set the timeout value, lets set it to 30 seconds | |
573 | // CRM-7510 | |
574 | $params['timeout'] = 30; | |
575 | ||
576 | // CRM-9349 | |
577 | $params['persist'] = $persist; | |
578 | ||
72ad6c1b | 579 | self::$_mail = self::_createMailer('smtp', $params); |
6a488035 TO |
580 | } |
581 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { | |
582 | if ($mailingInfo['sendmail_path'] == '' || | |
583 | !$mailingInfo['sendmail_path'] | |
584 | ) { | |
9327d839 DL |
585 | CRM_Core_Error::debug_log_message(ts('There is no valid sendmail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the sendmail server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); |
586 | CRM_Core_Error::fatal(ts('There is no valid sendmail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the sendmail server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
6a488035 TO |
587 | } |
588 | $params['sendmail_path'] = $mailingInfo['sendmail_path']; | |
589 | $params['sendmail_args'] = $mailingInfo['sendmail_args']; | |
590 | ||
72ad6c1b | 591 | self::$_mail = self::_createMailer('sendmail', $params); |
6a488035 TO |
592 | } |
593 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { | |
72ad6c1b | 594 | self::$_mail = self::_createMailer('mail', array()); |
6a488035 TO |
595 | } |
596 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) { | |
72ad6c1b | 597 | self::$_mail = self::_createMailer('mock', array()); |
6a488035 | 598 | } |
72ad6c1b | 599 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) { |
9327d839 DL |
600 | CRM_Core_Error::debug_log_message(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); |
601 | CRM_Core_Session::setStatus(ts('Outbound mail has been disabled. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
4b7d50ce | 602 | } |
c8e4bea0 | 603 | else { |
9327d839 DL |
604 | CRM_Core_Error::debug_log_message(ts('There is no valid SMTP server Setting Or SendMail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); |
605 | CRM_Core_Session::setStatus(ts('There is no valid SMTP server Setting Or sendMail path setting. Click <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); | |
4b7d50ce | 606 | CRM_Core_Error::debug_var('mailing_info', $mailingInfo); |
6a488035 TO |
607 | } |
608 | } | |
609 | return self::$_mail; | |
610 | } | |
611 | ||
72ad6c1b | 612 | /** |
c8e4bea0 TO |
613 | * Create a new instance of a PEAR Mail driver |
614 | * | |
72ad6c1b TO |
615 | * @param string $driver 'CRM_Mailing_BAO_Spool' or a name suitable for Mail::factory() |
616 | * @param array $params | |
c8e4bea0 | 617 | * @return Mail (More specifically, a class which implements the "send()" function) |
72ad6c1b TO |
618 | */ |
619 | public static function _createMailer($driver, $params) { | |
620 | if ($driver == 'CRM_Mailing_BAO_Spool') { | |
621 | $mailer = new CRM_Mailing_BAO_Spool($params); | |
c8e4bea0 TO |
622 | } |
623 | else { | |
72ad6c1b TO |
624 | $mailer = Mail::factory($driver, $params); |
625 | } | |
91bff2e5 | 626 | CRM_Utils_Hook::alterMail($mailer, $driver, $params); |
72ad6c1b TO |
627 | return $mailer; |
628 | } | |
629 | ||
6a488035 | 630 | /** |
72536736 | 631 | * Deletes the web server writable directories |
6a488035 | 632 | * |
72536736 AH |
633 | * @param int $value |
634 | * 1: clean templates_c, 2: clean upload, 3: clean both | |
635 | * @param bool $rmdir | |
6a488035 TO |
636 | */ |
637 | public function cleanup($value, $rmdir = TRUE) { | |
638 | $value = (int ) $value; | |
639 | ||
640 | if ($value & 1) { | |
641 | // clean templates_c | |
642 | CRM_Utils_File::cleanDir($this->templateCompileDir, $rmdir); | |
643 | CRM_Utils_File::createDir($this->templateCompileDir); | |
644 | } | |
645 | if ($value & 2) { | |
646 | // clean upload dir | |
647 | CRM_Utils_File::cleanDir($this->uploadDir); | |
648 | CRM_Utils_File::createDir($this->uploadDir); | |
fea6131e TO |
649 | } |
650 | ||
651 | // Whether we delete/create or simply preserve directories, we should | |
652 | // certainly make sure the restrictions are enforced. | |
a738c74c | 653 | foreach (array($this->templateCompileDir, $this->uploadDir, $this->configAndLogDir, $this->customFileUploadDir) as $dir) { |
fea6131e TO |
654 | if ($dir && is_dir($dir)) { |
655 | CRM_Utils_File::restrictAccess($dir); | |
656 | } | |
6a488035 TO |
657 | } |
658 | } | |
659 | ||
660 | /** | |
100fef9d | 661 | * Verify that the needed parameters are not null in the config |
6a488035 TO |
662 | * |
663 | * @param CRM_Core_Config (reference ) the system config object | |
664 | * @param array (reference ) the parameters that need a value | |
665 | * | |
666 | * @return boolean | |
667 | * @static | |
6a488035 | 668 | */ |
00be9182 | 669 | public static function check(&$config, &$required) { |
6a488035 TO |
670 | foreach ($required as $name) { |
671 | if (CRM_Utils_System::isNull($config->$name)) { | |
672 | return FALSE; | |
673 | } | |
674 | } | |
675 | return TRUE; | |
676 | } | |
677 | ||
678 | /** | |
100fef9d | 679 | * Reset the serialized array and recompute |
6a488035 TO |
680 | * use with care |
681 | */ | |
00be9182 | 682 | public function reset() { |
6a488035 TO |
683 | $query = "UPDATE civicrm_domain SET config_backend = null"; |
684 | CRM_Core_DAO::executeQuery($query); | |
685 | } | |
686 | ||
e8f14831 | 687 | // This method should initialize auth sources |
00be9182 | 688 | public function initAuthSrc() { |
e8f14831 DS |
689 | $session = CRM_Core_Session::singleton(); |
690 | if ($session->get('userID') && !$session->get('authSrc')) { | |
691 | $session->set('authSrc', CRM_Core_Permission::AUTH_SRC_LOGIN); | |
692 | } | |
693 | ||
694 | // checksum source | |
695 | CRM_Contact_BAO_Contact_Permission::initChecksumAuthSrc(); | |
696 | } | |
697 | ||
6a488035 | 698 | /** |
100fef9d | 699 | * One function to get domain ID |
6a488035 | 700 | */ |
00be9182 | 701 | public static function domainID($domainID = NULL, $reset = FALSE) { |
6a488035 TO |
702 | static $domain; |
703 | if ($domainID) { | |
704 | $domain = $domainID; | |
705 | } | |
706 | if ($reset || empty($domain)) { | |
707 | $domain = defined('CIVICRM_DOMAIN_ID') ? CIVICRM_DOMAIN_ID : 1; | |
708 | } | |
709 | ||
710 | return $domain; | |
711 | } | |
712 | ||
713 | /** | |
100fef9d | 714 | * Do general cleanup of caches, temp directories and temp tables |
6a488035 TO |
715 | * CRM-8739 |
716 | */ | |
00be9182 | 717 | public function cleanupCaches($sessionReset = TRUE) { |
6a488035 TO |
718 | // cleanup templates_c directory |
719 | $this->cleanup(1, FALSE); | |
720 | ||
721 | // clear db caching | |
722 | self::clearDBCache(); | |
723 | ||
724 | if ($sessionReset) { | |
725 | $session = CRM_Core_Session::singleton(); | |
726 | $session->reset(2); | |
727 | } | |
728 | } | |
729 | ||
730 | /** | |
731 | * Do general cleanup of module permissions. | |
732 | */ | |
00be9182 | 733 | public function cleanupPermissions() { |
6a488035 | 734 | $module_files = CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(); |
7fccad46 TO |
735 | if ($this->userPermissionClass->isModulePermissionSupported()) { |
736 | // Can store permissions -- so do it! | |
0d8fc497 TO |
737 | $this->userPermissionClass->upgradePermissions( |
738 | CRM_Core_Permission::basicPermissions() | |
739 | ); | |
7fccad46 TO |
740 | } else { |
741 | // Cannot store permissions -- warn if any modules require them | |
742 | $modules_with_perms = array(); | |
743 | foreach ($module_files as $module_file) { | |
744 | $perms = $this->userPermissionClass->getModulePermissions($module_file['prefix']); | |
745 | if (!empty($perms)) { | |
746 | $modules_with_perms[] = $module_file['prefix']; | |
747 | } | |
748 | } | |
749 | if (!empty($modules_with_perms)) { | |
750 | CRM_Core_Session::setStatus( | |
10a5be27 | 751 | ts('Some modules define permissions, but the CMS cannot store them: %1', array(1 => implode(', ', $modules_with_perms))), |
7fccad46 TO |
752 | ts('Permission Error'), |
753 | 'error' | |
754 | ); | |
755 | } | |
6a488035 TO |
756 | } |
757 | } | |
758 | ||
759 | /** | |
760 | * Flush information about loaded modules | |
761 | */ | |
00be9182 | 762 | public function clearModuleList() { |
6a488035 TO |
763 | CRM_Extension_System::singleton()->getCache()->flush(); |
764 | CRM_Utils_Hook::singleton(TRUE); | |
765 | CRM_Core_PseudoConstant::getModuleExtensions(TRUE); | |
766 | CRM_Core_Module::getAll(TRUE); | |
767 | } | |
768 | ||
769 | /** | |
100fef9d | 770 | * Clear db cache |
6a488035 TO |
771 | */ |
772 | public static function clearDBCache() { | |
773 | $queries = array( | |
774 | 'TRUNCATE TABLE civicrm_acl_cache', | |
775 | 'TRUNCATE TABLE civicrm_acl_contact_cache', | |
776 | 'TRUNCATE TABLE civicrm_cache', | |
777 | 'TRUNCATE TABLE civicrm_prevnext_cache', | |
778 | 'UPDATE civicrm_group SET cache_date = NULL', | |
779 | 'TRUNCATE TABLE civicrm_group_contact_cache', | |
780 | 'TRUNCATE TABLE civicrm_menu', | |
781 | 'UPDATE civicrm_setting SET value = NULL WHERE name="navigation" AND contact_id IS NOT NULL', | |
782 | 'DELETE FROM civicrm_setting WHERE name="modulePaths"', // CRM-10543 | |
783 | ); | |
784 | ||
785 | foreach ($queries as $query) { | |
786 | CRM_Core_DAO::executeQuery($query); | |
787 | } | |
788 | ||
789 | // also delete all the import and export temp tables | |
790 | self::clearTempTables(); | |
791 | } | |
792 | ||
793 | /** | |
100fef9d | 794 | * Clear leftover temporary tables |
6a488035 TO |
795 | */ |
796 | public static function clearTempTables() { | |
797 | // CRM-5645 | |
798 | $dao = CRM_Core_DAO::executeQuery("SELECT DATABASE();"); | |
799 | $query = " | |
800 | SELECT TABLE_NAME as tableName | |
801 | FROM INFORMATION_SCHEMA.TABLES | |
802 | WHERE TABLE_SCHEMA = %1 | |
a3243ca9 | 803 | AND |
804 | ( TABLE_NAME LIKE 'civicrm_import_job_%' | |
805 | OR TABLE_NAME LIKE 'civicrm_export_temp%' | |
806 | OR TABLE_NAME LIKE 'civicrm_task_action_temp%' | |
807 | OR TABLE_NAME LIKE 'civicrm_report_temp%' | |
808 | ) | |
6a488035 TO |
809 | "; |
810 | ||
811 | $params = array(1 => array($dao->database(), 'String')); | |
812 | $tableDAO = CRM_Core_DAO::executeQuery($query, $params); | |
813 | $tables = array(); | |
814 | while ($tableDAO->fetch()) { | |
815 | $tables[] = $tableDAO->tableName; | |
816 | } | |
817 | if (!empty($tables)) { | |
818 | $table = implode(',', $tables); | |
819 | // drop leftover temporary tables | |
820 | CRM_Core_DAO::executeQuery("DROP TABLE $table"); | |
821 | } | |
822 | } | |
823 | ||
824 | /** | |
100fef9d | 825 | * Check if running in upgrade mode |
6a488035 | 826 | */ |
00be9182 | 827 | public static function isUpgradeMode($path = NULL) { |
6a488035 TO |
828 | if (defined('CIVICRM_UPGRADE_ACTIVE')) { |
829 | return TRUE; | |
830 | } | |
831 | ||
832 | if (!$path) { | |
833 | // note: do not re-initialize config here, since this function is part of | |
834 | // config initialization itself | |
835 | $urlVar = 'q'; | |
836 | if (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') { | |
837 | $urlVar = 'task'; | |
838 | } | |
839 | ||
840 | $path = CRM_Utils_Array::value($urlVar, $_GET); | |
841 | } | |
842 | ||
843 | if ($path && preg_match('/^civicrm\/upgrade(\/.*)?$/', $path)) { | |
844 | return TRUE; | |
845 | } | |
846 | ||
847 | return FALSE; | |
848 | } | |
849 | ||
850 | /** | |
851 | * Wrapper function to allow unit tests to switch user framework on the fly | |
852 | */ | |
853 | public function setUserFramework($userFramework = NULL) { | |
854 | $this->userFramework = $userFramework; | |
855 | $this->_setUserFrameworkConfig($userFramework); | |
856 | } | |
9be1374d EM |
857 | |
858 | /** | |
859 | * Is back office credit card processing enabled for this site - ie are there any installed processors that support | |
fbcb6fba | 860 | * it? |
52767de0 EM |
861 | * This function is used for determining whether to show the submit credit card link, not for determining which processors to show, hence |
862 | * it is a config var | |
9be1374d EM |
863 | * @return bool |
864 | */ | |
00be9182 | 865 | public static function isEnabledBackOfficeCreditCardPayments() { |
44b6505d | 866 | return CRM_Financial_BAO_PaymentProcessor::hasPaymentProcessorSupporting(array('BackOffice')); |
9be1374d | 867 | } |
6a488035 | 868 | } |