Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
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 | */ | |
116 | private static $_mail = NULL; | |
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(); | |
187 | self::$_singleton = $cache->get('CRM_Core_Config'); | |
188 | ||
189 | // if not in cache, fire off config construction | |
190 | if (!self::$_singleton) { | |
191 | self::$_singleton = new CRM_Core_Config; | |
192 | self::$_singleton->_initialize($loadFromDB); | |
193 | ||
194 | //initialize variables. for gencode we cannot load from the | |
195 | //db since the db might not be initialized | |
196 | if ($loadFromDB) { | |
197 | self::$_singleton->_initVariables(); | |
198 | ||
199 | // retrieve and overwrite stuff from the settings file | |
200 | self::$_singleton->setCoreVariables(); | |
201 | } | |
202 | $cache->set('CRM_Core_Config', self::$_singleton); | |
203 | } | |
204 | else { | |
205 | // we retrieve the object from memcache, so we now initialize the objects | |
206 | self::$_singleton->_initialize($loadFromDB); | |
207 | ||
208 | // CRM-9803, NYSS-4822 | |
209 | // this causes various settings to be reset and hence we should | |
210 | // only use the config object that we retrived from memcache | |
211 | } | |
212 | ||
213 | self::$_singleton->initialized = 1; | |
214 | ||
215 | if (isset(self::$_singleton->customPHPPathDir) && | |
216 | self::$_singleton->customPHPPathDir | |
217 | ) { | |
218 | $include_path = self::$_singleton->customPHPPathDir . PATH_SEPARATOR . get_include_path(); | |
219 | set_include_path($include_path); | |
220 | } | |
221 | ||
222 | // set the callback at the very very end, to avoid an infinite loop | |
223 | // set the error callback | |
224 | CRM_Core_Error::setCallback(); | |
225 | ||
226 | // call the hook so other modules can add to the config | |
227 | // again doing this at the very very end | |
228 | CRM_Utils_Hook::config(self::$_singleton); | |
229 | ||
230 | // make sure session is always initialised | |
231 | $session = CRM_Core_Session::singleton(); | |
232 | ||
233 | // for logging purposes, pass the userID to the db | |
234 | $userID = $session->get('userID'); | |
235 | if ($userID) { | |
236 | CRM_Core_DAO::executeQuery('SET @civicrm_user_id = %1', | |
237 | array(1 => array($userID, 'Integer')) | |
238 | ); | |
239 | } | |
240 | } | |
241 | return self::$_singleton; | |
242 | } | |
243 | ||
244 | ||
245 | private function _setUserFrameworkConfig($userFramework) { | |
246 | ||
247 | $this->userFrameworkClass = 'CRM_Utils_System_' . $userFramework; | |
248 | $this->userHookClass = 'CRM_Utils_Hook_' . $userFramework; | |
249 | $userPermissionClass = 'CRM_Core_Permission_' . $userFramework; | |
250 | $this->userPermissionClass = new $userPermissionClass(); | |
251 | ||
252 | $class = $this->userFrameworkClass; | |
253 | // redundant with _initVariables | |
254 | $userSystem = $this->userSystem = new $class(); | |
255 | ||
256 | if ($userFramework == 'Joomla') { | |
257 | $this->userFrameworkURLVar = 'task'; | |
258 | } | |
259 | ||
260 | if (defined('CIVICRM_UF_BASEURL')) { | |
261 | $this->userFrameworkBaseURL = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/'); | |
262 | ||
263 | //format url for language negotiation, CRM-7803 | |
264 | $this->userFrameworkBaseURL = CRM_Utils_System::languageNegotiationURL($this->userFrameworkBaseURL); | |
265 | ||
266 | if (CRM_Utils_System::isSSL()) { | |
267 | $this->userFrameworkBaseURL = str_replace('http://', 'https://', | |
268 | $this->userFrameworkBaseURL | |
269 | ); | |
270 | } | |
271 | } | |
272 | ||
273 | if (defined('CIVICRM_UF_DSN')) { | |
274 | $this->userFrameworkDSN = CIVICRM_UF_DSN; | |
275 | } | |
276 | ||
277 | // this is dynamically figured out in the civicrm.settings.php file | |
278 | if (defined('CIVICRM_CLEANURL')) { | |
279 | $this->cleanURL = CIVICRM_CLEANURL; | |
280 | } | |
281 | else { | |
282 | $this->cleanURL = 0; | |
283 | } | |
284 | ||
285 | $this->userFrameworkVersion = $userSystem->getVersion(); | |
286 | ||
287 | if ($userFramework == 'Joomla') { | |
288 | global $mainframe; | |
289 | $dbprefix = $mainframe ? $mainframe->getCfg('dbprefix') : 'jos_'; | |
290 | $this->userFrameworkUsersTableName = $dbprefix . 'users'; | |
291 | } | |
292 | elseif ($userFramework == 'WordPress') { | |
293 | global $wpdb; | |
294 | $dbprefix = $wpdb ? $wpdb->prefix : ''; | |
295 | $this->userFrameworkUsersTableName = $dbprefix . 'users'; | |
296 | } | |
297 | } | |
298 | ||
299 | /** | |
300 | * Initializes the entire application. | |
301 | * Reads constants defined in civicrm.settings.php and | |
302 | * stores them in config properties. | |
303 | * | |
304 | * @return void | |
305 | * @access public | |
306 | */ | |
307 | private function _initialize($loadFromDB = TRUE) { | |
308 | ||
309 | // following variables should be set in CiviCRM settings and | |
310 | // as crucial ones, are defined upon initialisation | |
311 | // instead of in CRM_Core_Config_Defaults | |
312 | if (defined('CIVICRM_DSN')) { | |
313 | $this->dsn = CIVICRM_DSN; | |
314 | } | |
315 | elseif ($loadFromDB) { | |
316 | // bypass when calling from gencode | |
317 | echo 'You need to define CIVICRM_DSN in civicrm.settings.php'; | |
318 | exit(); | |
319 | } | |
320 | ||
321 | if (defined('CIVICRM_TEMPLATE_COMPILEDIR')) { | |
322 | $this->templateCompileDir = CRM_Utils_File::addTrailingSlash(CIVICRM_TEMPLATE_COMPILEDIR); | |
323 | ||
324 | // also make sure we create the config directory within this directory | |
325 | // the below statement will create both the templates directory and the config and log directory | |
326 | $this->configAndLogDir = CRM_Utils_File::baseFilePath($this->templateCompileDir) . 'ConfigAndLog' . DIRECTORY_SEPARATOR; | |
327 | CRM_Utils_File::createDir($this->configAndLogDir); | |
328 | ||
329 | // we're automatically prefixing compiled templates directories with country/language code | |
330 | global $tsLocale; | |
331 | if (!empty($tsLocale)) { | |
332 | $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($tsLocale); | |
333 | } | |
334 | elseif (!empty($this->lcMessages)) { | |
335 | $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($this->lcMessages); | |
336 | } | |
337 | ||
338 | CRM_Utils_File::createDir($this->templateCompileDir); | |
339 | } | |
340 | elseif ($loadFromDB) { | |
341 | echo 'You need to define CIVICRM_TEMPLATE_COMPILEDIR in civicrm.settings.php'; | |
342 | exit(); | |
343 | } | |
344 | ||
345 | $this->_initDAO(); | |
346 | ||
347 | if (defined('CIVICRM_UF')) { | |
348 | $this->userFramework = CIVICRM_UF; | |
349 | $this->_setUserFrameworkConfig($this->userFramework); | |
350 | } | |
351 | else { | |
352 | echo 'You need to define CIVICRM_UF in civicrm.settings.php'; | |
353 | exit(); | |
354 | } | |
355 | ||
356 | // also initialize the logger | |
357 | self::$_log = Log::singleton('display'); | |
358 | ||
359 | // initialize component registry early to avoid "race" | |
360 | // between CRM_Core_Config and CRM_Core_Component (they | |
361 | // are co-dependant) | |
362 | $this->componentRegistry = new CRM_Core_Component(); | |
363 | } | |
364 | ||
365 | /** | |
366 | * initialize the DataObject framework | |
367 | * | |
368 | * @return void | |
369 | * @access private | |
370 | */ | |
371 | private function _initDAO() { | |
372 | CRM_Core_DAO::init($this->dsn); | |
373 | ||
374 | $factoryClass = $this->DAOFactoryClass; | |
375 | require_once str_replace('_', DIRECTORY_SEPARATOR, $factoryClass) . '.php'; | |
376 | CRM_Core_DAO::setFactory(new $factoryClass()); | |
377 | if (CRM_Utils_Constant::value('CIVICRM_MYSQL_STRICT', CRM_Utils_System::isDevelopment())) { | |
378 | CRM_Core_DAO::executeQuery('SET SESSION sql_mode = STRICT_TRANS_TABLES'); | |
379 | } | |
380 | } | |
381 | ||
382 | /** | |
383 | * returns the singleton logger for the application | |
384 | * | |
385 | * @param | |
386 | * @access private | |
387 | * | |
388 | * @return object | |
389 | */ | |
390 | static public function &getLog() { | |
391 | if (!isset(self::$_log)) { | |
392 | self::$_log = Log::singleton('display'); | |
393 | } | |
394 | ||
395 | return self::$_log; | |
396 | } | |
397 | ||
398 | /** | |
399 | * initialize the config variables | |
400 | * | |
401 | * @return void | |
402 | * @access private | |
403 | */ | |
404 | private function _initVariables() { | |
405 | // retrieve serialised settings | |
406 | $variables = array(); | |
407 | CRM_Core_BAO_ConfigSetting::retrieve($variables); | |
408 | ||
409 | // if settings are not available, go down the full path | |
410 | if (empty($variables)) { | |
411 | // Step 1. get system variables with their hardcoded defaults | |
412 | $variables = get_object_vars($this); | |
413 | ||
414 | // Step 2. get default values (with settings file overrides if | |
415 | // available - handled in CRM_Core_Config_Defaults) | |
416 | CRM_Core_Config_Defaults::setValues($variables); | |
417 | ||
418 | // retrieve directory and url preferences also | |
419 | CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($variables); | |
420 | ||
421 | // add component specific settings | |
422 | $this->componentRegistry->addConfig($this); | |
423 | ||
424 | // serialise settings | |
425 | $settings = $variables; | |
426 | CRM_Core_BAO_ConfigSetting::add($settings); | |
427 | } | |
428 | ||
429 | $urlArray = array('userFrameworkResourceURL', 'imageUploadURL'); | |
430 | $dirArray = array('uploadDir', 'customFileUploadDir'); | |
431 | ||
432 | foreach ($variables as $key => $value) { | |
433 | if (in_array($key, $urlArray)) { | |
434 | $value = CRM_Utils_File::addTrailingSlash($value, '/'); | |
435 | } | |
436 | elseif (in_array($key, $dirArray)) { | |
437 | if ($value) { | |
438 | $value = CRM_Utils_File::addTrailingSlash($value); | |
439 | } | |
440 | if (empty($value) || (CRM_Utils_File::createDir($value, FALSE) === FALSE)) { | |
441 | // seems like we could not create the directories | |
442 | // settings might have changed, lets suppress a message for now | |
443 | // so we can make some more progress and let the user fix their settings | |
444 | // for now we assign it to a know value | |
445 | // CRM-4949 | |
446 | $value = $this->templateCompileDir; | |
447 | $url = CRM_Utils_System::url('civicrm/admin/setting/path', 'reset=1'); | |
448 | 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( | |
449 | 1 => $key, | |
450 | 2 => $url | |
451 | )), ts('Check Settings'), 'alert'); | |
452 | } | |
453 | } | |
454 | elseif ($key == 'lcMessages') { | |
455 | // reset the templateCompileDir to locale-specific and make sure it exists | |
456 | if (substr($this->templateCompileDir, -1 * strlen($value) - 1, -1) != $value) { | |
457 | $this->templateCompileDir .= CRM_Utils_File::addTrailingSlash($value); | |
458 | CRM_Utils_File::createDir($this->templateCompileDir); | |
459 | } | |
460 | } | |
461 | ||
462 | $this->$key = $value; | |
463 | } | |
464 | ||
465 | if ($this->userFrameworkResourceURL) { | |
466 | // we need to do this here so all blocks also load from an ssl server | |
467 | if (CRM_Utils_System::isSSL()) { | |
468 | CRM_Utils_System::mapConfigToSSL(); | |
469 | } | |
470 | $rrb = parse_url($this->userFrameworkResourceURL); | |
471 | // dont use absolute path if resources are stored on a different server | |
472 | // CRM-4642 | |
473 | $this->resourceBase = $this->userFrameworkResourceURL; | |
474 | if (isset($_SERVER['HTTP_HOST']) && | |
475 | isset($rrb['host']) | |
476 | ) { | |
477 | $this->resourceBase = ($rrb['host'] == $_SERVER['HTTP_HOST']) ? $rrb['path'] : $this->userFrameworkResourceURL; | |
478 | } | |
479 | } | |
480 | ||
481 | if (!$this->customFileUploadDir) { | |
482 | $this->customFileUploadDir = $this->uploadDir; | |
483 | } | |
484 | ||
485 | if ($this->geoProvider) { | |
486 | $this->geocodeMethod = 'CRM_Utils_Geocode_' . $this->geoProvider; | |
487 | } | |
488 | elseif ($this->mapProvider) { | |
489 | $this->geocodeMethod = 'CRM_Utils_Geocode_' . $this->mapProvider; | |
490 | } | |
491 | ||
492 | require_once (str_replace('_', DIRECTORY_SEPARATOR, $this->userFrameworkClass) . '.php'); | |
493 | $class = $this->userFrameworkClass; | |
494 | // redundant with _setUserFrameworkConfig | |
495 | $this->userSystem = new $class(); | |
496 | } | |
497 | ||
498 | /** | |
499 | * retrieve a mailer to send any mail from the applciation | |
500 | * | |
501 | * @param boolean $persist open a persistent smtp connection, should speed up mailings | |
502 | * | |
503 | * @access private | |
504 | * | |
505 | * @return object | |
506 | */ | |
507 | static function &getMailer($persist = FALSE) { | |
508 | if (!isset(self::$_mail)) { | |
509 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, | |
510 | 'mailing_backend' | |
511 | ); | |
512 | ||
513 | if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB || | |
514 | (defined('CIVICRM_MAILER_SPOOL') && CIVICRM_MAILER_SPOOL) | |
515 | ) { | |
516 | self::$_mail = new CRM_Mailing_BAO_Spool(); | |
517 | } | |
518 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { | |
519 | if ($mailingInfo['smtpServer'] == '' || !$mailingInfo['smtpServer']) { | |
520 | CRM_Core_Error::fatal(ts('There is no valid smtp server setting. Click <a href=\'%1\'>Administer CiviCRM >> Global Settings</a> to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting', 'reset=1')))); | |
521 | } | |
522 | ||
523 | $params['host'] = $mailingInfo['smtpServer'] ? $mailingInfo['smtpServer'] : 'localhost'; | |
524 | $params['port'] = $mailingInfo['smtpPort'] ? $mailingInfo['smtpPort'] : 25; | |
525 | ||
526 | if ($mailingInfo['smtpAuth']) { | |
527 | $params['username'] = $mailingInfo['smtpUsername']; | |
528 | $params['password'] = CRM_Utils_Crypt::decrypt($mailingInfo['smtpPassword']); | |
529 | $params['auth'] = TRUE; | |
530 | } | |
531 | else { | |
532 | $params['auth'] = FALSE; | |
533 | } | |
534 | ||
535 | // set the localhost value, CRM-3153 | |
536 | $params['localhost'] = CRM_Utils_Array::value('SERVER_NAME', $_SERVER, 'localhost'); | |
537 | ||
538 | // also set the timeout value, lets set it to 30 seconds | |
539 | // CRM-7510 | |
540 | $params['timeout'] = 30; | |
541 | ||
542 | // CRM-9349 | |
543 | $params['persist'] = $persist; | |
544 | ||
545 | self::$_mail = Mail::factory('smtp', $params); | |
546 | } | |
547 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { | |
548 | if ($mailingInfo['sendmail_path'] == '' || | |
549 | !$mailingInfo['sendmail_path'] | |
550 | ) { | |
551 | CRM_Core_Error::fatal(ts('There is no valid sendmail path setting. Click <a href=\'%1\'>Administer CiviCRM >> Global Settings</a> to set the Sendmail Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting', 'reset=1')))); | |
552 | } | |
553 | $params['sendmail_path'] = $mailingInfo['sendmail_path']; | |
554 | $params['sendmail_args'] = $mailingInfo['sendmail_args']; | |
555 | ||
556 | self::$_mail = Mail::factory('sendmail', $params); | |
557 | } | |
558 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { | |
559 | $params = array(); | |
560 | self::$_mail = Mail::factory('mail', $params); | |
561 | } | |
562 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) { | |
563 | self::$_mail = Mail::factory('mock', $params); | |
564 | } | |
565 | else { | |
566 | CRM_Core_Session::setStatus(ts('There is no valid SMTP server Setting Or SendMail path setting. Click <a href=\'%1\'>Administer CiviCRM >> Global Settings</a> to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting', 'reset=1'))), ts('Check Settings'), 'alert'); | |
567 | } | |
568 | } | |
569 | return self::$_mail; | |
570 | } | |
571 | ||
572 | /** | |
573 | * delete the web server writable directories | |
574 | * | |
575 | * @param int $value 1 - clean templates_c, 2 - clean upload, 3 - clean both | |
576 | * | |
577 | * @access public | |
578 | * | |
579 | * @return void | |
580 | */ | |
581 | public function cleanup($value, $rmdir = TRUE) { | |
582 | $value = (int ) $value; | |
583 | ||
584 | if ($value & 1) { | |
585 | // clean templates_c | |
586 | CRM_Utils_File::cleanDir($this->templateCompileDir, $rmdir); | |
587 | CRM_Utils_File::createDir($this->templateCompileDir); | |
588 | } | |
589 | if ($value & 2) { | |
590 | // clean upload dir | |
591 | CRM_Utils_File::cleanDir($this->uploadDir); | |
592 | CRM_Utils_File::createDir($this->uploadDir); | |
593 | CRM_Utils_File::restrictAccess($this->uploadDir); | |
594 | } | |
595 | } | |
596 | ||
597 | /** | |
598 | * verify that the needed parameters are not null in the config | |
599 | * | |
600 | * @param CRM_Core_Config (reference ) the system config object | |
601 | * @param array (reference ) the parameters that need a value | |
602 | * | |
603 | * @return boolean | |
604 | * @static | |
605 | * @access public | |
606 | */ | |
607 | static function check(&$config, &$required) { | |
608 | foreach ($required as $name) { | |
609 | if (CRM_Utils_System::isNull($config->$name)) { | |
610 | return FALSE; | |
611 | } | |
612 | } | |
613 | return TRUE; | |
614 | } | |
615 | ||
616 | /** | |
617 | * reset the serialized array and recompute | |
618 | * use with care | |
619 | */ | |
620 | function reset() { | |
621 | $query = "UPDATE civicrm_domain SET config_backend = null"; | |
622 | CRM_Core_DAO::executeQuery($query); | |
623 | } | |
624 | ||
625 | /** | |
626 | * one function to get domain ID | |
627 | */ | |
628 | static function domainID($domainID = NULL, $reset = FALSE) { | |
629 | static $domain; | |
630 | if ($domainID) { | |
631 | $domain = $domainID; | |
632 | } | |
633 | if ($reset || empty($domain)) { | |
634 | $domain = defined('CIVICRM_DOMAIN_ID') ? CIVICRM_DOMAIN_ID : 1; | |
635 | } | |
636 | ||
637 | return $domain; | |
638 | } | |
639 | ||
640 | /** | |
641 | * do general cleanup of caches, temp directories and temp tables | |
642 | * CRM-8739 | |
643 | */ | |
644 | function cleanupCaches($sessionReset = TRUE) { | |
645 | // cleanup templates_c directory | |
646 | $this->cleanup(1, FALSE); | |
647 | ||
648 | // clear db caching | |
649 | self::clearDBCache(); | |
650 | ||
651 | if ($sessionReset) { | |
652 | $session = CRM_Core_Session::singleton(); | |
653 | $session->reset(2); | |
654 | } | |
655 | } | |
656 | ||
657 | /** | |
658 | * Do general cleanup of module permissions. | |
659 | */ | |
660 | function cleanupPermissions() { | |
661 | $module_files = CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(); | |
662 | foreach ($module_files as $module_file) { | |
663 | // Clean up old module permissions that have been removed in the current | |
664 | // module version. | |
665 | $this->userPermissionClass->upgradePermissions($module_file['prefix']); | |
666 | } | |
667 | } | |
668 | ||
669 | /** | |
670 | * Flush information about loaded modules | |
671 | */ | |
672 | function clearModuleList() { | |
673 | CRM_Extension_System::singleton()->getCache()->flush(); | |
674 | CRM_Utils_Hook::singleton(TRUE); | |
675 | CRM_Core_PseudoConstant::getModuleExtensions(TRUE); | |
676 | CRM_Core_Module::getAll(TRUE); | |
677 | } | |
678 | ||
679 | /** | |
680 | * clear db cache | |
681 | */ | |
682 | public static function clearDBCache() { | |
683 | $queries = array( | |
684 | 'TRUNCATE TABLE civicrm_acl_cache', | |
685 | 'TRUNCATE TABLE civicrm_acl_contact_cache', | |
686 | 'TRUNCATE TABLE civicrm_cache', | |
687 | 'TRUNCATE TABLE civicrm_prevnext_cache', | |
688 | 'UPDATE civicrm_group SET cache_date = NULL', | |
689 | 'TRUNCATE TABLE civicrm_group_contact_cache', | |
690 | 'TRUNCATE TABLE civicrm_menu', | |
691 | 'UPDATE civicrm_setting SET value = NULL WHERE name="navigation" AND contact_id IS NOT NULL', | |
692 | 'DELETE FROM civicrm_setting WHERE name="modulePaths"', // CRM-10543 | |
693 | ); | |
694 | ||
695 | foreach ($queries as $query) { | |
696 | CRM_Core_DAO::executeQuery($query); | |
697 | } | |
698 | ||
699 | // also delete all the import and export temp tables | |
700 | self::clearTempTables(); | |
701 | } | |
702 | ||
703 | /** | |
704 | * clear leftover temporary tables | |
705 | */ | |
706 | public static function clearTempTables() { | |
707 | // CRM-5645 | |
708 | $dao = CRM_Core_DAO::executeQuery("SELECT DATABASE();"); | |
709 | $query = " | |
710 | SELECT TABLE_NAME as tableName | |
711 | FROM INFORMATION_SCHEMA.TABLES | |
712 | WHERE TABLE_SCHEMA = %1 | |
713 | AND ( TABLE_NAME LIKE 'civicrm_import_job_%' | |
714 | OR TABLE_NAME LIKE 'civicrm_export_temp%' | |
715 | OR TABLE_NAME LIKE 'civicrm_task_action_temp%' ) | |
716 | "; | |
717 | ||
718 | $params = array(1 => array($dao->database(), 'String')); | |
719 | $tableDAO = CRM_Core_DAO::executeQuery($query, $params); | |
720 | $tables = array(); | |
721 | while ($tableDAO->fetch()) { | |
722 | $tables[] = $tableDAO->tableName; | |
723 | } | |
724 | if (!empty($tables)) { | |
725 | $table = implode(',', $tables); | |
726 | // drop leftover temporary tables | |
727 | CRM_Core_DAO::executeQuery("DROP TABLE $table"); | |
728 | } | |
729 | } | |
730 | ||
731 | /** | |
732 | * function to check if running in upgrade mode | |
733 | */ | |
734 | static function isUpgradeMode($path = NULL) { | |
735 | if (defined('CIVICRM_UPGRADE_ACTIVE')) { | |
736 | return TRUE; | |
737 | } | |
738 | ||
739 | if (!$path) { | |
740 | // note: do not re-initialize config here, since this function is part of | |
741 | // config initialization itself | |
742 | $urlVar = 'q'; | |
743 | if (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') { | |
744 | $urlVar = 'task'; | |
745 | } | |
746 | ||
747 | $path = CRM_Utils_Array::value($urlVar, $_GET); | |
748 | } | |
749 | ||
750 | if ($path && preg_match('/^civicrm\/upgrade(\/.*)?$/', $path)) { | |
751 | return TRUE; | |
752 | } | |
753 | ||
754 | return FALSE; | |
755 | } | |
756 | ||
757 | /** | |
758 | * Wrapper function to allow unit tests to switch user framework on the fly | |
759 | */ | |
760 | public function setUserFramework($userFramework = NULL) { | |
761 | $this->userFramework = $userFramework; | |
762 | $this->_setUserFrameworkConfig($userFramework); | |
763 | } | |
764 | } | |
765 | // end CRM_Core_Config | |
766 |