<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  Sylvain FATOME
 * 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 2 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
 * (in the directory docs);
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Contact : galawa@free.fr
 *
 * Web : http://smi.no-ip.org
 ****************************************************************************************/


// Vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

/*****************************************************/
/* Fonctions d'extension de la classe PHPMailer      */
/*****************************************************/

/**
 * Class SMIMailer
 */
class SMIMailer extends PHPMailer
{

    public $tab_dest = [];
    public $result_mail = true;
    public $copieadmin = true;
    public $attach = '';

    // Initialisation des paramètres
    function init_PHPM()
    {
        global
        $admin_name,
        $admin_mail,
        $smtp_nom,
        $smtp_secure,
        $smtp_port,
        $smtp_login,
        $smtp_mdp;

        // $this->CharSet='iso-8859-15';
        $this->CharSet = 'UTF-8';
        // Si SMTP renseigné, on passe en mode "smtp" et on injecte les bonnes variables
        if (trim($smtp_nom) <> "") {
            $this->Mailer = "smtp";
            $this->Host = $smtp_nom;
            $this->Port = $smtp_port;
            if (trim($smtp_login) <> "") {
                $this->Username = $smtp_login;
                $this->Password = $smtp_mdp;
                $this->SMTPAuth = true;
                if (trim($smtp_secure) <> "") {
                    $this->SMTPSecure = $smtp_secure;
                }
            }
        } else // Si rien renseigné, on passe en mode "mail"
            $this->Mailer = "mail";

        $this->IsHTML(true);
        // $this->SMTPDebug = true; // Pour déboggage
        $this->WordWrap = 75;
        $this->AltBody = _("Pour visualiser ce message, merci d'utiliser un gestionnaire de mails compatible HTML");

        // Entête de l'email
        $this->Priority = 3;

        //Expédié par
        $this->From = $admin_mail;
        $this->FromName = $admin_name;
        $this->Sender = $admin_mail;
        $this->tab_dest = [];
        $this->result_mail = true;
        $this->copieadmin = true;
        $this->attach = "";
    }

    // Envoi des messages
    function send_PHPM()
    {
        global
        $smtp_pausem,
        $admin_mail,
        $admin_name;

        if (!ini_get('safe_mode'))
            set_time_limit(0); // On demande à PHP de ne pas interrrompre le script pendant le traitement

        $mode = $this->Mailer;
        $tab_dest = $this->tab_dest;
        $sujet = $this->Subject;
        $copieadmin = $this->copieadmin;

        $sent = TRUE;
        switch ($mode) {
            // Le mode mail interdit les CC et les BCC
            case "mail" :
                foreach ($tab_dest as $key => $value) {
                    if ($sent == TRUE) {
                        $this->ClearAddresses();
                        $this->AddAddress($key, $value);
                        $sent = $this->Send();
                        sleep($smtp_pausem);
                    } else
                        break;
                }

                // Envoi à l'administrateur si demandé
                if ($sent == TRUE) {
                    if (($copieadmin == TRUE) AND ($admin_mail <> "")) {
                        $this->ClearAllRecipients();
                        // $this->ClearAddresses();
                        $this->AddAddress($admin_mail, $admin_name);
                        $this->Subject = $sujet . ' (' . _("Copie administrateur") . ')';
                        $sent = $this->Send();
                    }
                }
                break;

            default :
                $i = 0;
                foreach ($tab_dest as $key => $value) {
                    if ($i == 0)
                        $this->AddAddress($key, $value);
                    else
                        $this->AddBCC($key, $value);
                    $i++;
                }

                // Envoi à l'administrateur si demandé
                if (($copieadmin == TRUE) AND ($admin_mail <> ""))
                    $this->AddBCC($admin_mail, $admin_name);
                $sent = $this->Send();
                break;
        }
        $this->result_mail = $sent;
    }

    // Ajout des pièces jointes
    function attach_PHPM()
    {
        global
            $url_gfc;

        $attach = $this->attach;

        // Il y a une pièce attachée ?
        if (trim($attach) <> "") {
            $tab_attach = explode(".", $attach);
            $type_fic = $tab_attach[count($tab_attach) - 1];
            switch (strtolower($type_fic)) {
                case "7z":
                    $type_mime = "application/x-compress";
                    break;
                case "ai":
                    $type_mime = "application/postscript";
                    break;
                case "asf":
                case "asx":
                    $type_mime = "video/x-ms-asf";
                    break;
                case "aif":
                case "aiff":
                    $type_mime = "audio/aiff";
                    break;
                case "avi":
                    $type_mime = "video/x-msvideo";
                    break;
                case "bin":
                    $type_mime = "application/octet-stream";
                    break;
                case "bmp":
                    $type_mime = "image/bmp";
                    break;
                case "bz2":
                    $type_mime = "application/x-bzip";
                    break;
                case "cgi":
                    $type_mime = "magnus-internal/cgi";
                    break;
                case "css":
                    $type_mime = "text/css";
                    break;
                case "dcr":
                    $type_mime = "application/x-director";
                    break;
                case "dxr":
                    $type_mime = "application/x-director";
                    break;
                case "dll":
                    $type_mime = "application/octet-stream";
                    break;
                case "doc":
                    $type_mime = "application/msword";
                    break;
                case "eps":
                    $type_mime = "application/postscript";
                    break;
                case "exe":
                    $type_mime = "application/octet-stream";
                    break;
                case "gif":
                    $type_mime = "image/gif";
                    break;
                case "gtar":
                    $type_mime = "application/x-gtar";
                    break;
                case "gz":
                    $type_mime = "application/gzip";
                    break;
                case "htm":
                    $type_mime = "text/html";
                    break;
                case "html":
                    $type_mime = "text/html";
                    break;
                case "iso":
                    $type_mime = "application/octet-stream";
                    break;
                case "jar":
                    $type_mime = "application/java-archive";
                    break;
                case "java":
                    $type_mime = "text/x-java-source";
                    break;
                case "jnlp":
                    $type_mime = "application/x-java-jnlp-file";
                    break;
                case "js":
                    $type_mime = "application/x-javascript";
                    break;
                case "jpg":
                    $type_mime = "image/jpg";
                    break;
                case "jpe":
                    $type_mime = "image/jpg";
                    break;
                case "jpeg":
                    $type_mime = "image/jpg";
                    break;
                case "kdc":
                    $type_mime = "image/x-kdc";
                    break;
                case "lzh":
                    $type_mime = "application/octet-stream";
                    break;
                case "mdb":
                    $type_mime = "application/mdb";
                    break;
                case "mid":
                case "midi":
                    $type_mime = "audio/x-midi";
                    break;
                case "mov":
                    $type_mime = "video/quicktime";
                    break;
                case "mp2":
                    $type_mime = "audio/x-mpeg";
                    break;
                case "mp3":
                    $type_mime = "audio/mpeg";
                    break;
                case "mpg":
                case "mpe":
                case "mpeg":
                    $type_mime = "video/mpeg";
                    break;
                case "mng":
                    $type_mime = "video/x-mng";
                    break;
                case "ogg":
                    $type_mime = "audio/ogg";
                    break;
                case "pcd":
                    $type_mime = "image/x-pcd";
                    break;
                case "pdf":
                    $type_mime = "application/pdf";
                    break;
                case "php":
                    $type_mime = "application/x-httpd-php";
                    break;
                case "php3":
                    $type_mime = "application/x-httpd-php3";
                    break;
                case "php4":
                    $type_mime = "application/x-httpd-php";
                    break;
                case "png":
                    $type_mime = "image/png";
                    break;
                case "pps":
                case "ppt":
                    $type_mime = "application/mspowerpoint";
                    break;
                case "psd":
                    $type_mime = "image/psd";
                    break;
                case "qt":
                    $type_mime = "video/quicktime";
                    break;
                case "qti":
                    $type_mime = "image/x-quicktime";
                    break;
                case "rar":
                    $type_mime = "encoding/x-compress";
                    break;
                case "ra":
                case "rm":
                case "ram":
                    $type_mime = "audio/x-pn-realaudio";
                    break;
                case "rtf":
                    $type_mime = "application/rtf";
                    break;
                case "rv":
                    $type_mime = "video/vnd.rn-realvideo";
                    break;
                case "swa":
                    $type_mime = "application/x-director";
                    break;
                case "swf":
                    $type_mime = "application/x-shockwave-flash";
                    break;
                case "tar":
                    $type_mime = "application/x-tar";
                    break;
                case "tar.gz":
                    $type_mime = "application/x-gtar";
                    break;
                case "tgz":
                    $type_mime = "application/gzip";
                    break;
                case "tif":
                    $type_mime = "image/tiff";
                    break;
                case "tiff":
                    $type_mime = "image/tiff";
                    break;
                case "torrent":
                    $type_mime = "application/x-bittorrent";
                    break;
                case "txt":
                    $type_mime = "text/plain";
                    break;
                case "wav":
                    $type_mime = "audio/wav";
                    break;
                case "wbmp":
                    $type_mime = "image/vnd.wap.wbmp";
                    break;
                case "wma":
                    $type_mime = "audio/x-ms-wma";
                    break;
                case "wmv":
                    $type_mime = "video/x-ms-wmv";
                    break;
                case "wmx":
                    $type_mime = "video/x-ms-wmx";
                    break;
                case "xls":
                    $type_mime = "application/xls";
                    break;
                case "xml":
                    $type_mime = "application/xml";
                    break;
                case "xsl":
                    $type_mime = "application/xsl";
                    break;
                case "zip":
                    $type_mime = "application/x-zip-compressed";
                    break;
                default:
                    $type_mime = "application/octet-stream";
                    break;
            }
            $url_svg = $url_gfc . '/pj/';
            $nom_fic = str_replace($url_svg, "", $tab_attach[count($tab_attach) - 2]);
            $nom_complet = $nom_fic . $type_fic;
            $this->AddAttachment($attach, $nom_complet, 'base64', $type_mime);
        }
    }
}

?>