<commit_before><?php
/**
 * class.spool.php
 *
 * @package workflow.engine.classes
 *
 * ProcessMaker Open Source Edition
 * Copyright (C) 2004 - 2011 Colosa Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
 * Coral Gables, FL, 33134, USA, or email <EMAIL>.
 *
 */

/**
 * spoolRun - brief send email from the spool database, and see if we have all the addresses we send to.
 *
 * @author <NAME> <<EMAIL>@[REMOVE_THESE_CAPITALS]<EMAIL>>
 * @copyright Copyright (c) 2007, <NAME>
 * @license http://www.opensource.org/licenses/gpl-3.0.html GNU Public License
 * @link http://www.openmail.cc
 */

/**
 * LOG FIXES
 * =========
 *
 * 24-03-2010 <NAME>. <<EMAIL>>
 * class: the $ExceptionCode and $aWarnings class attributes were added
 * function handleFrom(): Validations for invalid data for {$this->fileData['from_email']} were added
 * function resendEmails(): handler for warnings was added and fixes
 * function getWarnings(): added
 * function sendMail(): now is handling the exception
 */

require_once ('classes/model/AppMessage.php');

/**
 *
 * @package workflow.engine.ProcessMaker
 */

class spoolRun
{
    public $config;
    private $fileData;
    private $spool_id;
    public $status;
    public $error;

    private $ExceptionCode = Array (); //Array to define the Expetion codes
    private $aWarnings = Array (); //Array to store the warning that were throws by the class


    private $longMailEreg;
    private $mailEreg;

    /**
     * Class constructor - iniatilize default values
     *
     * @param none
     * @return none
     */
    public function __construct ()
    {
        $this->config = array ();
        $this->fileData = array ();
        $this->spool_id = '';
        $this->status = 'pending';
        $this->error = '';

        $this->ExceptionCode['FATAL'] = 1;
        $this->ExceptionCode['WARNING'] = 2;
        $this->ExceptionCode['NOTICE'] = 3;

        $this->longMailEreg = '/(.*)(<([\w\-\.]+@[\w\-_\.]+\.\w{2,3})+>)/';
        $this->mailEreg = '/^([\w\-_\.]+@[\w\-_\.]+\.\w{2,3}+)$/';
    }

    /**
     * get all files into spool in a list
     *
     * @param none
     * @return none
     */
    public function getSpoolFilesList ()
    {
        $sql = "SELECT * FROM APP_MESSAGE WHERE APP_MSG_STATUS ='pending'";

        $con = Propel::getConnection( "workflow" );
        $stmt = $con->prepareStatement( $sql );
        $rs = $stmt->executeQuery();

        while ($rs->next()) {
            $this->spool_id = $rs->getString( 'APP_MSG_UID' );
            $this->fileData['subject'] = $rs->getString( 'APP_MSG_SUBJECT' );
            $this->fileData['from'] = $rs->getString( 'APP_MSG_FROM' );
            $this->fileData['to'] = $rs->getString( 'APP_MSG_TO' );
            $this->fileData['body'] = $rs->getString( 'APP_MSG_BODY' );
            $this->fileData['date'] = $rs->getString( 'APP_MSG_DATE' );
            $this->fileData['cc'] = $rs->getString( 'APP_MSG_CC' );
            $this->fileData['bcc'] = $rs->getString( 'APP_MSG_BCC' );
            $this->fileData['template'] = $rs->getString( 'APP_MSG_TEMPLATE' );
            $this->fileData['attachments'] = array (); //$rs->getString('APP_MSG_ATTACH');
            if ($this->config['MESS_ENGINE'] == 'OPENMAIL') {
                if ($this->config['MESS_SERVER'] != '') {
                    if (($sAux = @gethostbyaddr( $this->config['MESS_SERVER'] ))) {
                        $this->fileData['domain'] = $sAux;
                    } else {
                        $this->fileData['domain'] = $this->config['MESS_SERVER'];
                    }
                } else {
                    $this->fileData['domain'] = gethostbyaddr( '127.0.0.1' );
                }
            }
            $this->sendMail();
        }
    }

    /**
     * create a msg record for spool
     *
     * @param array $aData
     * @return none
     */
    public function create ($aData)
    {
        $aData['app_msg_show_message'] = (isset($aData['app_msg_show_message'])) ? $aData['app_msg_show_message'] : 1;
        $sUID = $this->db_insert( $aData );

        $aData['app_msg_date'] = isset( $aData['app_msg_date'] ) ? $aData['app_msg_date'] : '';

        if (isset( $aData['app_msg_status'] )) {
            $this->status = strtolower( $aData['app_msg_status'] );
        }

        $this->setData( $sUID, $aData['app_msg_subject'], $aData['app_msg_from'], $aData['app_msg_to'], $aData['app_msg_body'], $aData['app_msg_date'], $aData['app_msg_cc'], $aData['app_msg_bcc'], $aData['app_msg_template'], $aData['app_msg_attach'] );
    }

    /**
     * set configuration
     *
     * @param array $aConfig
     * @return none
     */
    public function setConfig ($aConfig)
    {
        $this->config = $aConfig;
    }

    /**
     * set email parameters
     *
     * @param string $sAppMsgUid, $sSubject, $sFrom, $sTo, $sBody, $sDate, $sCC, $sBCC, $sTemplate
     * @return none
     */
    public function setData ($sAppMsgUid, $sSubject, $sFrom, $sTo, $sBody, $sDate = '', $sCC = '', $sBCC = '', $sTemplate = '', $aAttachment = array())
    {
        $this->spool_id = $sAppMsgUid;
        $this->fileData['subject'] = $sSubject;
        $this->fileData['from'] = $sFrom;
        $this->fileData['to'] = $sTo;
        $this->fileData['body'] = $sBody;
        $this->fileData['date'] = ($sDate != '' ? $sDate : date( 'Y-m-d H:i:s' ));
        $this->fileData['cc'] = $sCC;
        $this->fileData['bcc'] = $sBCC;
        $this->fileData['template'] = $sTemplate;
        $this->fileData['attachments'] = is_array( $aAttachment ) ? $aAttachment : ($aAttachment != '' ? explode( ',', $aAttachment ) : array ());
        $this->fileData['envelope_to'] = array ();

        if ($this->config['MESS_ENGINE'] == 'OPENMAIL') {
            if ($this->config['MESS_SERVER'] != '') {
                if (($sAux = @gethostbyaddr( $this->config['MESS_SERVER'] ))) {
                    $this->fileData['domain'] = $sAux;
                } else {
                    $this->fileData['domain'] = $this->config['MESS_SERVER'];
                }
            } else {
                $this->fileData['domain'] = gethostbyaddr( '127.0.0.1' );
            }
        }
    }

    /**
     * send mail
     *
     * @param none
     * @return boolean true or exception
     */
    public function sendMail ()
    {
        try {
            $this->handleFrom();
            $this->handleEnvelopeTo();
            $this->handleMail();
            $this->updateSpoolStatus();
            return true;
        } catch (Exception $e) {
            throw $e;
        }
    }

    /**
     * update the status to spool
     *
     * @param none
     * @return none
     */
    private function updateSpoolStatus ()
    {
        $oAppMessage = AppMessagePeer::retrieveByPK( $this->spool_id );
        if (is_array( $this->fileData['attachments'] )) {
            $attachment = implode( ",", $this->fileData['attachments'] );
            $oAppMessage->setappMsgAttach( $attachment );
        }
        $oAppMessage->setappMsgstatus( $this->status );
        $oAppMessage->setappMsgsenddate( date( 'Y-m-d H:i:s' ) );
        $oAppMessage->save();
    }

    /**
     * handle the email that was set in "TO" parameter
     *
     * @param none
     * @return boolean true or exception
     */
    private function handleFrom ()
    {
        if (strpos( $this->fileData['from'], '<' ) !== false) {
            //to validate complex email address i.e. <NAME> <<EMAIL>>
            preg_match( $this->longMailEreg, $this->fileData['from'], $matches );
            if (isset( $matches[1] ) && $matches[1] != '') {
                //drop the " characters if they exist
                $this->fileData['from_name'] = trim( str_replace( '"', '', $matches[1] ) );
            } else {
                //if the from name was not set
                $this->fileData['from_name'] = '';
            }

            if (! isset( $matches[3] )) {
                throw new Exception( 'Invalid email address in FROM parameter (' . $this->fileData['from'] . ')', $this->ExceptionCode['WARNING'] );
            }

            $this->fileData['from_email'] = trim( $matches[3] );
        } else {
            //to validate simple email address i.e. <EMAIL>
            preg_match( $this->mailEreg, $this->fileData['from'], $matches );

            if (! isset( $matches[0] )) {
                throw new Exception( 'Invalid email address in FROM parameter (' . $this->fileData['from'] . ')', $this->ExceptionCode['WARNING'] );
            }

            $this->fileData['from_name'] = '';
            $this->fileData['from_email'] = $matches[0];
        }

    }

    /**
     * handle all recipients to compose the mail
     *
     * @param none
     * @return boolean true or exception
     */
    private function handleEnvelopeTo ()
    {
        $hold = array ();
        $holdcc = array ();
        $holdbcc = array ();
        $text = trim( $this->fileData['to'] );

        $textcc = '';
        $textbcc = '';
        if (isset( $this->fileData['cc'] ) && trim( $this->fileData['cc'] ) != '') {
            $textcc = trim( $this->fileData['cc'] );
        }

        if (isset( $this->fileData['bcc'] ) && trim( $this->fileData['bcc'] ) != '') {
            $textbcc = trim( $this->fileData['bcc'] );
        }

        if (false !== (strpos( $text, ',' ))) {
            $hold = explode( ',', $text );

            foreach ($hold as $val) {
                if (strlen( $val ) > 0) {
                    $this->fileData['envelope_to'][] = "$val";
                }
            }
        } else if ($text != '') {
            $this->fileData['envelope_to'][] = "$text";
        } else {
            $this->fileData['envelope_to'] = Array ();
        }

        //CC
        if (false !== (strpos( $textcc, ',' ))) {
            $holdcc = explode( ',', $textcc );

            foreach ($holdcc as $valcc) {
                if (strlen( $valcc ) > 0) {
                    $this->fileData['envelope_cc'][] = "$valcc";
                }
            }
        } else if ($textcc != '') {
            $this->fileData['envelope_cc'][] = "$textcc";
        } else {
            $this->fileData['envelope_cc'] = Array ();
        }

        //BCC
        if (false !== (strpos( $textbcc, ',' ))) {
            $holdbcc = explode( ',', $textbcc );

            foreach ($holdbcc as $valbcc) {
                if (strlen( $valbcc ) > 0) {
                    $this->fileData['envelope_bcc'][] = "$valbcc";
                }
            }
        } else if ($textbcc != '') {
            $this->fileData['envelope_bcc'][] = "$textbcc";
        } else {
            $this->fileData['envelope_bcc'] = Array ();
        }

    }

    /**
     * handle and compose the email content and parameters
     *
     * @param none
     * @return none
     */
    private function handleMail ()
    {
        if (count( $this->fileData['envelope_to'] ) > 0) {<commit_msg>BUG 10103 PMFSendMessage with attachments SOLVED<commit_after><?php
/**
 * class.spool.php
 *
 * @package workflow.engine.classes
 *
 * ProcessMaker Open Source Edition
 * Copyright (C) 2004 - 2011 Colosa Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
 * Coral Gables, FL, 33134, USA, or email <EMAIL>.
 *
 */

/**
 * spoolRun - brief send email from the spool database, and see if we have all the addresses we send to.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2007, <NAME>
 * @license http://www.opensource.org/licenses/gpl-3.0.html GNU Public License
 * @link http://www.openmail.cc
 */

/**
 * LOG FIXES
 * =========
 *
 * 24-03-2010 <NAME>. <<EMAIL>>
 * class: the $ExceptionCode and $aWarnings class attributes were added
 * function handleFrom(): Validations for invalid data for {$this->fileData['from_email']} were added
 * function resendEmails(): handler for warnings was added and fixes
 * function getWarnings(): added
 * function sendMail(): now is handling the exception
 */

require_once ('classes/model/AppMessage.php');

/**
 *
 * @package workflow.engine.ProcessMaker
 */

class spoolRun
{
    public $config;
    private $fileData;
    private $spool_id;
    public $status;
    public $error;

    private $ExceptionCode = Array (); //Array to define the Expetion codes
    private $aWarnings = Array (); //Array to store the warning that were throws by the class


    private $longMailEreg;
    private $mailEreg;

    /**
     * Class constructor - iniatilize default values
     *
     * @param none
     * @return none
     */
    public function __construct ()
    {
        $this->config = array ();
        $this->fileData = array ();
        $this->spool_id = '';
        $this->status = 'pending';
        $this->error = '';

        $this->ExceptionCode['FATAL'] = 1;
        $this->ExceptionCode['WARNING'] = 2;
        $this->ExceptionCode['NOTICE'] = 3;

        $this->longMailEreg = '/(.*)(<([\w\-\.]+@[\w\-_\.]+\.\w{2,3})+>)/';
        $this->mailEreg = '/^([\w\-_\.]+@[\w\-_\.]+\.\w{2,3}+)$/';
    }

    /**
     * get all files into spool in a list
     *
     * @param none
     * @return none
     */
    public function getSpoolFilesList ()
    {
        $sql = "SELECT * FROM APP_MESSAGE WHERE APP_MSG_STATUS ='pending'";

        $con = Propel::getConnection( "workflow" );
        $stmt = $con->prepareStatement( $sql );
        $rs = $stmt->executeQuery();

        while ($rs->next()) {
            $this->spool_id = $rs->getString( 'APP_MSG_UID' );
            $this->fileData['subject'] = $rs->getString( 'APP_MSG_SUBJECT' );
            $this->fileData['from'] = $rs->getString( 'APP_MSG_FROM' );
            $this->fileData['to'] = $rs->getString( 'APP_MSG_TO' );
            $this->fileData['body'] = $rs->getString( 'APP_MSG_BODY' );
            $this->fileData['date'] = $rs->getString( 'APP_MSG_DATE' );
            $this->fileData['cc'] = $rs->getString( 'APP_MSG_CC' );
            $this->fileData['bcc'] = $rs->getString( 'APP_MSG_BCC' );
            $this->fileData['template'] = $rs->getString( 'APP_MSG_TEMPLATE' );
            $this->fileData['attachments'] = array (); //$rs->getString('APP_MSG_ATTACH');
            if ($this->config['MESS_ENGINE'] == 'OPENMAIL') {
                if ($this->config['MESS_SERVER'] != '') {
                    if (($sAux = @gethostbyaddr( $this->config['MESS_SERVER'] ))) {
                        $this->fileData['domain'] = $sAux;
                    } else {
                        $this->fileData['domain'] = $this->config['MESS_SERVER'];
                    }
                } else {
                    $this->fileData['domain'] = gethostbyaddr( '127.0.0.1' );
                }
            }
            $this->sendMail();
        }
    }

    /**
     * create a msg record for spool
     *
     * @param array $aData
     * @return none
     */
    public function create ($aData)
    {
        if (is_array($aData['app_msg_attach'])) {
            $attachment = implode(",", $aData['app_msg_attach']);
            $aData['app_msg_attach'] = $attachment;
        }
        $aData['app_msg_show_message'] = (isset($aData['app_msg_show_message'])) ? $aData['app_msg_show_message'] : 1;
        $sUID = $this->db_insert( $aData );

        $aData['app_msg_date'] = isset( $aData['app_msg_date'] ) ? $aData['app_msg_date'] : '';

        if (isset( $aData['app_msg_status'] )) {
            $this->status = strtolower( $aData['app_msg_status'] );
        }

        $this->setData( $sUID, $aData['app_msg_subject'], $aData['app_msg_from'], $aData['app_msg_to'], $aData['app_msg_body'], $aData['app_msg_date'], $aData['app_msg_cc'], $aData['app_msg_bcc'], $aData['app_msg_template'], $aData['app_msg_attach'] );
    }

    /**
     * set configuration
     *
     * @param array $aConfig
     * @return none
     */
    public function setConfig ($aConfig)
    {
        $this->config = $aConfig;
    }

    /**
     * set email parameters
     *
     * @param string $sAppMsgUid, $sSubject, $sFrom, $sTo, $sBody, $sDate, $sCC, $sBCC, $sTemplate
     * @return none
     */
    public function setData ($sAppMsgUid, $sSubject, $sFrom, $sTo, $sBody, $sDate = '', $sCC = '', $sBCC = '', $sTemplate = '', $aAttachment = array())
    {
        $this->spool_id = $sAppMsgUid;
        $this->fileData['subject'] = $sSubject;
        $this->fileData['from'] = $sFrom;
        $this->fileData['to'] = $sTo;
        $this->fileData['body'] = $sBody;
        $this->fileData['date'] = ($sDate != '' ? $sDate : date( 'Y-m-d H:i:s' ));
        $this->fileData['cc'] = $sCC;
        $this->fileData['bcc'] = $sBCC;
        $this->fileData['template'] = $sTemplate;
        $this->fileData['attachments'] = is_array( $aAttachment ) ? $aAttachment : ($aAttachment != '' ? explode( ',', $aAttachment ) : array ());
        $this->fileData['envelope_to'] = array ();

        if ($this->config['MESS_ENGINE'] == 'OPENMAIL') {
            if ($this->config['MESS_SERVER'] != '') {
                if (($sAux = @gethostbyaddr( $this->config['MESS_SERVER'] ))) {
                    $this->fileData['domain'] = $sAux;
                } else {
                    $this->fileData['domain'] = $this->config['MESS_SERVER'];
                }
            } else {
                $this->fileData['domain'] = gethostbyaddr( '127.0.0.1' );
            }
        }
    }

    /**
     * send mail
     *
     * @param none
     * @return boolean true or exception
     */
    public function sendMail ()
    {
        try {
            $this->handleFrom();
            $this->handleEnvelopeTo();
            $this->handleMail();
            $this->updateSpoolStatus();
            return true;
        } catch (Exception $e) {
            throw $e;
        }
    }

    /**
     * update the status to spool
     *
     * @param none
     * @return none
     */
    private function updateSpoolStatus ()
    {
        $oAppMessage = AppMessagePeer::retrieveByPK( $this->spool_id );
        if (is_array( $this->fileData['attachments'] )) {
            $attachment = implode( ",", $this->fileData['attachments'] );
            $oAppMessage->setappMsgAttach( $attachment );
        }
        $oAppMessage->setappMsgstatus( $this->status );
        $oAppMessage->setappMsgsenddate( date( 'Y-m-d H:i:s' ) );
        $oAppMessage->save();
    }

    /**
     * handle the email that was set in "TO" parameter
     *
     * @param none
     * @return boolean true or exception
     */
    private function handleFrom ()
    {
        if (strpos( $this->fileData['from'], '<' ) !== false) {
            //to validate complex email address i.e. <NAME> <<EMAIL>>
            preg_match( $this->longMailEreg, $this->fileData['from'], $matches );
            if (isset( $matches[1] ) && $matches[1] != '') {
                //drop the " characters if they exist
                $this->fileData['from_name'] = trim( str_replace( '"', '', $matches[1] ) );
            } else {
                //if the from name was not set
                $this->fileData['from_name'] = '';
            }

            if (! isset( $matches[3] )) {
                throw new Exception( 'Invalid email address in FROM parameter (' . $this->fileData['from'] . ')', $this->ExceptionCode['WARNING'] );
            }

            $this->fileData['from_email'] = trim( $matches[3] );
        } else {
            //to validate simple email address i.e. <EMAIL>
            preg_match( $this->mailEreg, $this->fileData['from'], $matches );

            if (! isset( $matches[0] )) {
                throw new Exception( 'Invalid email address in FROM parameter (' . $this->fileData['from'] . ')', $this->ExceptionCode['WARNING'] );
            }

            $this->fileData['from_name'] = '';
            $this->fileData['from_email'] = $matches[0];
        }

    }

    /**
     * handle all recipients to compose the mail
     *
     * @param none
     * @return boolean true or exception
     */
    private function handleEnvelopeTo ()
    {
        $hold = array ();
        $holdcc = array ();
        $holdbcc = array ();
        $text = trim( $this->fileData['to'] );

        $textcc = '';
        $textbcc = '';
        if (isset( $this->fileData['cc'] ) && trim( $this->fileData['cc'] ) != '') {
            $textcc = trim( $this->fileData['cc'] );
        }

        if (isset( $this->fileData['bcc'] ) && trim( $this->fileData['bcc'] ) != '') {
            $textbcc = trim( $this->fileData['bcc'] );
        }

        if (false !== (strpos( $text, ',' ))) {
            $hold = explode( ',', $text );

            foreach ($hold as $val) {
                if (strlen( $val ) > 0) {
                    $this->fileData['envelope_to'][] = "$val";
                }
            }
        } elseif ($text != '') {
            $this->fileData['envelope_to'][] = "$text";
        } else {
            $this->fileData['envelope_to'] = Array ();
        }

        //CC
        if (false !== (strpos( $textcc, ',' ))) {
            $holdcc = explode( ',', $textcc );

            foreach ($holdcc as $valcc) {
                if (strlen( $valcc ) > 0) {
                    $this->fileData['envelope_cc'][] = "$valcc";
                }
            }
        } elseif ($textcc != '') {
            $this->fileData['envelope_cc'][] = "$textcc";
        } else {
            $this->fileData['envelope_cc'] = Array ();
        }

        //BCC
        if (false !== (strpos( $textbcc, ',' ))) {
            $holdbcc = explode( ',', $textbcc );

            foreach ($holdbcc as $valbcc) {
                if (strlen( $valbcc ) > 0) {
                    $this->fileData['envelope_bcc'][] = "$valbcc";
                }
            }
        } elseif ($textbcc != '') {
            $this->fileData['envelope_bcc'][] = "$textbcc";
        } else {
            $this->fileData['envelope_bcc'] = Array ();
        }

    }

    /**
     * handle and compose the email content and parameters
     *
     * @param none
     * @return none
     */
    private function handleMail ()
    {
        if (count( $this->fileData['envelope_to'] ) > 0) {
{'fraction_non_alphanumeric': 0.17364937006192613, 'fraction_numerical': 0.00559470424941277, 'mean_word_length': 2.056121117201775, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 254, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3818673', 'n_tokens_mistral': 7437, 'n_tokens_neox': 7243, 'n_words': 1607}