<?php

/*
 General utility class from Swift Mailer.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

/**
 * General utility class in Swift Mailer, not to be instantiated.
 * 
 * @package Swift
 * 
 * @author Chris Corbyn
 */
abstract class Swift
{
  
  /** Swift Mailer Version number generated during dist release process */
  const VERSION = '4.0.3';
  
  /**
   * Internal autoloader for spl_autoload_register().
   * 
   * @param string $class
   */
  public static function autoload($class)
  {
    //Don't interfere with other autoloaders
    if (substr($class, 0, strlen(__CLASS__)) != __CLASS__)
    {
      return;
    }
    
    $path = SWIFT_CLASS_DIRECTORY . '/' . str_replace('_', '/', $class) . '.php';
    
    if (file_exists($path))
    {
      require_once $path;                                                        
    }
  }
  
  /**
   * Configure autoloading using Swift Mailer.
   * 
   * This is designed to play nicely with other autoloaders.
   */
  public static function registerAutoload()
  {
    if (!$callbacks = spl_autoload_functions())
    {
      $callbacks = array();
    }
    foreach ($callbacks as $callback)
    {
      spl_autoload_unregister($callback);
    }
    spl_autoload_register(array('Swift', 'autoload'));
    foreach ($callbacks as $callback)
    {
      spl_autoload_register($callback);
    }
  }
  
  /**
   * Initialize Swift dependencies.
   */
  public static function init()
  {
    static $initialized = false;
    
    if ($initialized)
    {
      return;
    }
    
    // Cache dependencies.
    Swift_DependencyContainer::getInstance()
      
      -> register('cache')
      -> asAliasOf('cache.array')
      
      -> register('tempdir')
      -> asValue('/tmp')
      
      -> register('cache.null')
      -> asSharedInstanceOf('Swift_KeyCache_NullKeyCache')
      
      -> register('cache.array')
      -> asSharedInstanceOf('Swift_KeyCache_ArrayKeyCache')
      -> withDependencies(array('cache.inputstream'))
      
      -> register('cache.disk')
      -> asSharedInstanceOf('Swift_KeyCache_DiskKeyCache')
      -> withDependencies(array('cache.inputstream', 'tempdir'))
      
      -> register('cache.inputstream')
      -> asNewInstanceOf('Swift_KeyCache_SimpleKeyCacheInputStream')
      
      ;
    
    // Mime dependencies.
    $swift_mime_types = array(
      'aif'  => 'audio/x-aiff',
      'aiff' => 'audio/x-aiff',
      'avi'  => 'video/avi',
      'bmp'  => 'image/bmp',
      'bz2'  => 'application/x-bz2',
      'csv'  => 'text/csv',
      'dmg'  => 'application/x-apple-diskimage',
      'doc'  => 'application/msword',
      'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
      'eml'  => 'message/rfc822',
      'aps'  => 'application/postscript',
      'exe'  => 'application/x-ms-dos-executable',
      'flv'  => 'video/x-flv',
      'gif'  => 'image/gif',
      'gz'   => 'application/x-gzip',
      'hqx'  => 'application/stuffit',
      'htm'  => 'text/html',
      'html' => 'text/html',
      'jar'  => 'application/x-java-archive',
      'jpeg' => 'image/jpeg',
      'jpg'  => 'image/jpeg',
      'm3u'  => 'audio/x-mpegurl',
      'm4a'  => 'audio/mp4',
      'mdb'  => 'application/x-msaccess',
      'mid'  => 'audio/midi',
      'midi' => 'audio/midi',
      'mov'  => 'video/quicktime',
      'mp3'  => 'audio/mpeg',
      'mp4'  => 'video/mp4',
      'mpeg' => 'video/mpeg',
      'mpg'  => 'video/mpeg',
      'odg'  => 'vnd.oasis.opendocument.graphics',
      'odp'  => 'vnd.oasis.opendocument.presentation',
      'odt'  => 'vnd.oasis.opendocument.text',
      'ods'  => 'vnd.oasis.opendocument.spreadsheet',
      'ogg'  => 'audio/ogg',
      'pdf'  => 'application/pdf',
      'png'  => 'image/png',
      'ppt'  => 'application/vnd.ms-powerpoint',
      'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
      'ps'   => 'application/postscript',
      'rar'  => 'application/x-rar-compressed',
      'rtf'  => 'application/rtf',
      'tar'  => 'application/x-tar',
      'sit'  => 'application/x-stuffit',
      'svg'  => 'image/svg+xml',
      'tif'  => 'image/tiff',
      'tiff' => 'image/tiff',
      'ttf'  => 'application/x-font-truetype',
      'txt'  => 'text/plain',
      'vcf'  => 'text/x-vcard',
      'wav'  => 'audio/wav',
      'wma'  => 'audio/x-ms-wma',
      'wmv'  => 'audio/x-ms-wmv',
      'xls'  => 'application/excel',
      'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'xml'  => 'application/xml',
      'zip'  => 'application/zip'
    );
    
    Swift_DependencyContainer::getInstance()
      
      -> register('properties.charset')
      -> asValue('utf-8')
      
      -> register('mime.message')
      -> asNewInstanceOf('Swift_Mime_SimpleMessage')
      -> withDependencies(array(
        'mime.headerset',
        'mime.qpcontentencoder',
        'cache',
        'properties.charset'
      ))
      
      -> register('mime.part')
      -> asNewInstanceOf('Swift_Mime_MimePart')
      -> withDependencies(array(
        'mime.headerset',
        'mime.qpcontentencoder',
        'cache',
        'properties.charset'
      ))
      
      -> register('mime.attachment')
      -> asNewInstanceOf('Swift_Mime_Attachment')
      -> withDependencies(array(
        'mime.headerset',
        'mime.base64contentencoder',
        'cache'
      ))
      -> addConstructorValue($swift_mime_types)
      
      -> register('mime.embeddedfile')
      -> asNewInstanceOf('Swift_Mime_EmbeddedFile')
      -> withDependencies(array(
        'mime.headerset',
        'mime.base64contentencoder',
        'cache'
      ))
      -> addConstructorValue($swift_mime_types)
      
      -> register('mime.headerfactory')
      -> asNewInstanceOf('Swift_Mime_SimpleHeaderFactory')
      -> withDependencies(array(
          'mime.qpheaderencoder',
          'mime.rfc2231encoder',
          'properties.charset'
        ))
      
      -> register('mime.headerset')
      -> asNewInstanceOf('Swift_Mime_SimpleHeaderSet')
      -> withDependencies(array('mime.headerfactory', 'properties.charset'))
      
      -> register('mime.qpheaderencoder')
      -> asNewInstanceOf('Swift_Mime_HeaderEncoder_QpHeaderEncoder')
      -> withDependencies(array('mime.charstream'))
      
      -> register('mime.charstream')
      -> asNewInstanceOf('Swift_CharacterStream_ArrayCharacterStream')
      -> withDependencies(array('mime.characterreaderfactory', 'properties.charset'))
      
      -> register('mime.bytecanonicalizer')
      -> asSharedInstanceOf('Swift_StreamFilters_ByteArrayReplacementFilter')
      -> addConstructorValue(array(array(0x0D, 0x0A), array(0x0D), array(0x0A)))
      -> addConstructorValue(array(array(0x0A), array(0x0A), array(0x0D, 0x0A)))
      
      -> register('mime.characterreaderfactory')
      -> asSharedInstanceOf('Swift_CharacterReaderFactory_SimpleCharacterReaderFactory')
      
      -> register('mime.qpcontentencoder')
      -> asNewInstanceOf('Swift_Mime_ContentEncoder_QpContentEncoder')
      -> withDependencies(array('mime.charstream', 'mime.bytecanonicalizer'))
      
      -> register('mime.7bitcontentencoder')
      -> asNewInstanceOf('Swift_Mime_ContentEncoder_PlainContentEncoder')
      -> addConstructorValue('7bit')
      -> addConstructorValue(true)
      
      -> register('mime.8bitcontentencoder')
      -> asNewInstanceOf('Swift_Mime_ContentEncoder_PlainContentEncoder')
      -> addConstructorValue('8bit')
      -> addConstructorValue(true)
      
      -> register('mime.base64contentencoder')
      -> asSharedInstanceOf('Swift_Mime_ContentEncoder_Base64ContentEncoder')
      
      -> register('mime.rfc2231encoder')
      -> asNewInstanceOf('Swift_Encoder_Rfc2231Encoder')
      -> withDependencies(array('mime.charstream'))
      
      ;
    
    // Transport dependencies.
    Swift_DependencyContainer::getInstance()
      
      -> register('transport.smtp')
      -> asNewInstanceOf('Swift_Transport_EsmtpTransport')
      -> withDependencies(array(
        'transport.buffer',
        array('transport.authhandler'),
        'transport.eventdispatcher'
      ))
      
      -> register('transport.sendmail')
      -> asNewInstanceOf('Swift_Transport_SendmailTransport')
      -> withDependencies(array(
        'transport.buffer',
        'transport.eventdispatcher'
      ))
      
      -> register('transport.mail')
      -> asNewInstanceOf('Swift_Transport_MailTransport')
      -> withDependencies(array('transport.mailinvoker', 'transport.eventdispatcher'))
      
      -> register('transport.loadbalanced')
      -> asNewInstanceOf('Swift_Transport_LoadBalancedTransport')
      
      -> register('transport.failover')
      -> asNewInstanceOf('Swift_Transport_FailoverTransport')
      
      -> register('transport.mailinvoker')
      -> asSharedInstanceOf('Swift_Transport_SimpleMailInvoker')
      
      -> register('transport.buffer')
      -> asNewInstanceOf('Swift_Transport_StreamBuffer')
      -> withDependencies(array('transport.replacementfactory'))
      
      -> register('transport.authhandler')
      -> asNewInstanceOf('Swift_Transport_Esmtp_AuthHandler')
      -> withDependencies(array(
        array(
          'transport.crammd5auth',
          'transport.loginauth',
          'transport.plainauth'
        )
      ))
      
      -> register('transport.crammd5auth')
      -> asNewInstanceOf('Swift_Transport_Esmtp_Auth_CramMd5Authenticator')
      
      -> register('transport.loginauth')
      -> asNewInstanceOf('Swift_Transport_Esmtp_Auth_LoginAuthenticator')
      
      -> register('transport.plainauth')
      -> asNewInstanceOf('Swift_Transport_Esmtp_Auth_PlainAuthenticator')
      
      -> register('transport.eventdispatcher')
      -> asNewInstanceOf('Swift_Events_SimpleEventDispatcher')
      
      -> register('transport.replacementfactory')
      -> asSharedInstanceOf('Swift_StreamFilters_StringReplacementFilterFactory')
      
      ;
    
    // Preferences
    Swift_Preferences::getInstance()->setCharset('utf-8');
    
    if (function_exists('sys_get_temp_dir') && is_writable(sys_get_temp_dir()))
    {
      Swift_Preferences::getInstance()
        -> setTempDir(sys_get_temp_dir())
        -> setCacheType('disk');
    }
    
    $initialized = true;
  }
  
}
