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