Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-10-28-14-52-15
[civicrm-core.git] / CRM / Core / Config.php
index 3356f009743c654929c3ef5e4671ce633b8f552d..377cec0afeb013ce2ae0dc7d1b2cca129dccdf78 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -113,7 +113,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
    * the handle on the mail handler that we are using
    * @var object
    */
-  private static $_mail = NULL;
+  public static $_mail = NULL;
 
   /**
    * We only need one instance of this object. So we use the singleton
@@ -193,10 +193,16 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
         //initialize variables. for gencode we cannot load from the
         //db since the db might not be initialized
         if ($loadFromDB) {
+          // initialize stuff from the settings file
+          self::$_singleton->setCoreVariables();
+
           self::$_singleton->_initVariables();
 
-          // retrieve and overwrite stuff from the settings file
-          self::$_singleton->setCoreVariables();
+          // I dont think we need to do this twice
+          // however just keeping this commented for now in 4.4
+          // in case we hit any issues - CRM-13064
+          // We can safely delete this once we release 4.4.4
+          // self::$_singleton->setCoreVariables();
         }
         $cache->set('CRM_Core_Config' . CRM_Core_Config::domainID(), self::$_singleton);
       }
@@ -236,6 +242,9 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
           array(1 => array($userID, 'Integer'))
         );
       }
+
+      // initialize authentication source
+      self::$_singleton->initAuthSrc();
     }
     return self::$_singleton;
   }
@@ -497,12 +506,10 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
   }
 
   /**
-   * retrieve a mailer to send any mail from the applciation
+   * Retrieve a mailer to send any mail from the application
    *
    * @param boolean $persist open a persistent smtp connection, should speed up mailings
-   *
    * @access private
-   *
    * @return object
    */
   static function &getMailer($persist = FALSE) {
@@ -514,11 +521,12 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
       if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB ||
         (defined('CIVICRM_MAILER_SPOOL') && CIVICRM_MAILER_SPOOL)
       ) {
-        self::$_mail = new CRM_Mailing_BAO_Spool();
+        self::$_mail = self::_createMailer('CRM_Mailing_BAO_Spool', array());
       }
       elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) {
         if ($mailingInfo['smtpServer'] == '' || !$mailingInfo['smtpServer']) {
-          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'))));
+          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'))));
+          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'))));
         }
 
         $params['host'] = $mailingInfo['smtpServer'] ? $mailingInfo['smtpServer'] : 'localhost';
@@ -543,33 +551,57 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
         // CRM-9349
         $params['persist'] = $persist;
 
-        self::$_mail = Mail::factory('smtp', $params);
+        self::$_mail = self::_createMailer('smtp', $params);
       }
       elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) {
         if ($mailingInfo['sendmail_path'] == '' ||
           !$mailingInfo['sendmail_path']
         ) {
-          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'))));
+          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'))));
+          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'))));
         }
         $params['sendmail_path'] = $mailingInfo['sendmail_path'];
         $params['sendmail_args'] = $mailingInfo['sendmail_args'];
 
-        self::$_mail = Mail::factory('sendmail', $params);
+        self::$_mail = self::_createMailer('sendmail', $params);
       }
       elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) {
-        $params = array();
-        self::$_mail = Mail::factory('mail', $params);
+        self::$_mail = self::_createMailer('mail', array());
       }
       elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) {
-        self::$_mail = Mail::factory('mock', $params);
+        self::$_mail = self::_createMailer('mock', array());
+      }
+      elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) {
+        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'))));
+        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'))));
       }
       else {
-        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');
+        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'))));
+        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'))));
+        CRM_Core_Error::debug_var('mailing_info', $mailingInfo);
       }
     }
     return self::$_mail;
   }
 
+  /**
+   * Create a new instance of a PEAR Mail driver
+   *
+   * @param string $driver 'CRM_Mailing_BAO_Spool' or a name suitable for Mail::factory()
+   * @param array $params
+   * @return Mail (More specifically, a class which implements the "send()" function)
+   */
+  public static function _createMailer($driver, $params) {
+    if ($driver == 'CRM_Mailing_BAO_Spool') {
+      $mailer = new CRM_Mailing_BAO_Spool($params);
+    }
+    else {
+      $mailer = Mail::factory($driver, $params);
+    }
+    CRM_Utils_Hook::alterMail($mailer, $driver, $params);
+    return $mailer;
+  }
+
   /**
    * delete the web server writable directories
    *
@@ -623,6 +655,17 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
     CRM_Core_DAO::executeQuery($query);
   }
 
+  // This method should initialize auth sources
+  function initAuthSrc() {
+    $session = CRM_Core_Session::singleton();
+    if ($session->get('userID') && !$session->get('authSrc')) {
+      $session->set('authSrc', CRM_Core_Permission::AUTH_SRC_LOGIN);
+    }
+
+    // checksum source
+    CRM_Contact_BAO_Contact_Permission::initChecksumAuthSrc();
+  }
+
   /**
    * one function to get domain ID
    */
@@ -728,9 +771,12 @@ class CRM_Core_Config extends CRM_Core_Config_Variables {
 SELECT TABLE_NAME as tableName
 FROM   INFORMATION_SCHEMA.TABLES
 WHERE  TABLE_SCHEMA = %1
-AND    ( TABLE_NAME LIKE 'civicrm_import_job_%'
-OR       TABLE_NAME LIKE 'civicrm_export_temp%'
-OR       TABLE_NAME LIKE 'civicrm_task_action_temp%' )
+AND
+  ( TABLE_NAME LIKE 'civicrm_import_job_%'
+  OR       TABLE_NAME LIKE 'civicrm_export_temp%'
+  OR       TABLE_NAME LIKE 'civicrm_task_action_temp%'
+  OR       TABLE_NAME LIKE 'civicrm_report_temp%'
+  )
 ";
 
     $params = array(1 => array($dao->database(), 'String'));