1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 
<?php
/* --------------------------------------------------------------
   EmailConfigurationProvider.inc.php 2018-03-05
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2018 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/**
 * Class EmailConfigurationProvider
 */
class EmailConfigurationProvider
{
    /**
     * @var \CI_DB_query_builder
     */
    protected $db;
    
    /**
     * @var string
     */
    protected $table = 'configuration';
    
    /**
     * @var string
     */
    protected $keyField = 'configuration_key';
    
    /**
     * @var string
     */
    protected $valueField = 'configuration_value';
    
    
    /**
     * EmailConfigurationProvider constructor.
     *
     * @param \CI_DB_query_builder $db
     */
    public function __construct(CI_DB_query_builder $db)
    {
        $this->db = $db;
    }
    
    
    /**
     * Returns the email transport configuration value.
     *
     * @return string Configuration value of email transport.
     */
    public function emailTransport()
    {
        return $this->_getEmailConfig('EMAIL_TRANSPORT');
    }
    
    
    /**
     * Returns the smtp auth configuration value.
     *
     * @return string Configuration value of emails smtp auth.
     */
    public function auth()
    {
        return $this->_getEmailConfig('SMTP_AUTH');
    }
    
    
    /**
     * Returns the smtp backup server configuration value.
     *
     * @return string Configuration value of emails smtp backup server.
     */
    public function backupServer()
    {
        return $this->_getEmailConfig('SMTP_Backup_Server');
    }
    
    
    /**
     * Returns the smtp encryption configuration value.
     *
     * @return string Configuration value of emails smtp encryption.
     */
    public function encryption()
    {
        return $this->_getEmailConfig('SMTP_ENCRYPTION');
    }
    
    
    /**
     * Returns the smtp main server configuration value.
     *
     * @return string Configuration value of emails smtp main server.
     */
    public function mainServer()
    {
        return $this->_getEmailConfig('SMTP_MAIN_SERVER');
    }
    
    
    /**
     * Returns the smtp password configuration value.
     *
     * @return string Configuration value of emails smtp password.
     */
    public function password()
    {
        return $this->_getEmailConfig('SMTP_PASSWORD');
    }
    
    
    /**
     * Returns the smtp port configuration value.
     *
     * @return string Configuration value of emails smtp port.
     */
    public function port()
    {
        return $this->_getEmailConfig('SMTP_PORT');
    }
    
    
    /**
     * Returns the smtp user configuration value.
     *
     * @return string Configuration value of emails smtp username.
     */
    public function user()
    {
        return $this->_getEmailConfig('SMTP_USERNAME');
    }
    
    
    /**
     * Returns the send mail path configuration value.
     *
     * @return string Configuration value of send mail path.
     */
    public function sendMailPath()
    {
        return $this->_getEmailConfig('SENDMAIL_PATH');
    }
    
    
    /**
     * Returns the charset configuration value for emails.
     * $_SESSION['language_charset'] will be used if it is set.
     * If not, the charset of the default shop language is taken.
     *
     * @return string
     */
    public function charset()
    {
        if(array_key_exists('language_charset', $_SESSION))
        {
            return $_SESSION['language_charset'];
        }
        $field           = 'language_charset';
        $defaultLanguage = $this->defaultLanguage();
        
        $result = $this->db->select($field)->from('languages')->where('code', $defaultLanguage)->get()->row_array();
        
        return array_key_exists($field, $result) ? $result[$field] : 'utf-8';
    }
    
    
    /**
     * Returns the language code configuration value.
     * The code is 'de', if $_SESSION['language'] is equal to 'german'.
     * Any other case will return en.
     *
     * @return string
     */
    public function languageCode()
    {
        return array_key_exists('language', $_SESSION) && $_SESSION['language'] === 'german' ? 'de' : 'en';
    }
    
    
    /**
     * Returns true if html emails should be used.
     * The configuration value will be read from configuration table.
     *
     * @return bool
     */
    public function useHtmlMail()
    {
        return $this->_getEmailConfig('EMAIL_USE_HTML') === 'true';
    }
    
    
    /**
     * Returns the default language configuration value.
     *
     * @return string Configuration value of default language.
     */
    private function defaultLanguage()
    {
        return $this->_getEmailConfig('DEFAULT_LANGUAGE');
    }
    
    
    /**
     * Returns the email configuration value for the given key.
     * The configuration value will be read from the configurations table.
     *
     * @param string $key Key of expected configuration value.
     *
     * @return string Configuration value of given key.
     */
    private function _getEmailConfig($key)
    {
        $result = $this->db->select($this->valueField)
                           ->from($this->table)
                           ->where($this->keyField, $key)
                           ->get()
                           ->row_array();
        
        return array_key_exists($this->valueField, $result) ? $result[$this->valueField] : '';
    }
}