<commit_before><?php

    require_once(_XE_PATH_.'modules/mailing/mailing.lib.php');

    class mailingController extends mailing {

        function init(){
        }

        function procMailingToggleList()
        {
            $oMemberModel =& getModel('member');

            $args->module_srl = Context::get('module_srl');
            if(!$args->module_srl) return new Object(-1,'msg_invalid_request');

            if(!Context::get('is_logged')) return new Object(-1,'msg_not_permitted');

            $logged_info = Context::get('logged_info');
            $args->member_srl = $logged_info->member_srl;

            $output = executeQuery('mailing.getMemberJoined', $args);
            if($output->data) {
                return executeQuery("mailing.deleteMember", $args);
            } else {
                $member_info = $oMemberModel->getMemberInfoByMemberSrl($args->member_srl);
                $args->email_address = $member_info->email_address;
                return executeQuery("mailing.insertMember", $args);
            }
        }

        function removeOrgMsg($body) {
            preg_match("/<div[^>]*>(.+)<div style=\'border/siU", $body, $match);
            if($match) return $match[1];
            return $body;
        }

        function replaceCid($body, $reverse = false) {
            $cid_array = Context::get('cid_array');
            if(!$cid_array) return $body;
            foreach($cid_array as $cid => $uploaded_filename)
            {
                if($reverse)
                {
                    $body = str_replace($uploaded_filename, "cid:".$cid, $body);
                }
                else
                {
                    $body = str_replace("cid:".$cid, $uploaded_filename, $body);
                }
            }
            return $body;
        }

        function writeComment($comment_srl, &$mailMessage, &$member_info, $reference_srl, $module_srl)
        {
            $obj = null;
            $oCommentModel =& getModel('comment');
            $oComment = $oCommentModel->getComment($reference_srl);
            if($oComment->isExists())
            {
                $obj->document_srl =  $oComment->get('document_srl');
                $obj->parent_srl = $reference_srl;
            }
            else
            {
                $obj->document_srl = $reference_srl;
            }

            $obj->comment_srl = $comment_srl;
            $obj->member_srl = $member_info->member_srl;
            $obj->user_id = $member_info->user_id;
            $obj->user_name = $member_info->user_name;
            $obj->nick_name = $member_info->nick_name;
            $obj->email_address = $member_info->email_address;
            $obj->homepage = $member_info->homepage;
            $obj->content = $this->removeOrgMsg($this->replaceCid($mailMessage->getBody()));
            $obj->module_srl = $module_srl;
            $obj->title = $mailMessage->subject;
            $oCommentController = &getController('comment');
            $output = $oCommentController->insertComment($obj, false);
            return $obj;
        }

        function procMailingInsertMail()
        {
            $message = Context::get('message');
            if(!$message) return;
            $mailMessage = new mailMessage($message['tmp_name']);
            $mailMessage->parse();
            if($mailMessage->messageId)
            {
                preg_match("/@(.+)>$/i", $mailMessage->messageId, $matches);
                if($matches[1] == $_SERVER["SERVER_NAME"])
                {
                    return;
                }
            }

            $fromAddress = mailparse_rfc822_parse_addresses($mailMessage->from);
            $fromAddress = array_shift($fromAddress);
            $fromAddress = $fromAddress["address"];

            $memberModel =& getModel('member');
            $member_srl = $memberModel->getMemberSrlByEmailAddress($fromAddress);
            if(!$member_srl) 
            {
                $mailMessage->close();
                return;
            }
            $member_info = $memberModel->getMemberInfoByMemberSrl($member_srl);

            $toAddress = mailparse_rfc822_parse_addresses($mailMessage->to);
            $obj = array_shift($toAddress);
            $obj = $obj["address"];
            $mailingAddress = $obj;
            $mid = explode("@", $obj);
            $mid = array_shift($mid);
            $mid = explode(".", $mid);
            $site_srl = 0;
            $moduleModel =& getModel('module');
            if(count($mid) > 1)
            {
                $vid = $mid[0];
                $mid = $mid[1];
                $site_info = $moduleModel->getSiteInfoByDomain($vid);
                $site_srl = $site_info->site_srl;
            }
            else
            {
                $mid = $mid[0];
            }

            $targetModule = $moduleModel->getModuleInfoByMid($mid, $site_srl);
            if(!$targetModule)
            {
                $mailMessage->close();
                return;
            }
            $oModel =& getModel('mailing');
            if(!$oModel->isMember($targetModule->module_srl, $fromAddress, $member_info->member_srl))
            {
                $mailMessage->close();
                return;
            }
            $oMemberController = &getController('member');
            $oMemberController->doLogin($member_info->user_id);

            $reference_srl = null;
            if($mailMessage->inreplyto)
            {
                $reference_srl = $mailMessage->inreplyto;  
            }
            else if($mailMessage->references)
            {
                $reference_srl = $mailMessage->references;
            }

            if($reference_srl)
            {
                $reference_srl = explode("@", $reference_srl); <commit_msg>#18847532 : insert mail even if mailing address is set as second(or later) address<commit_after><?php

    require_once(_XE_PATH_.'modules/mailing/mailing.lib.php');

    class mailingController extends mailing {

        function init(){
        }

        function procMailingToggleList()
        {
            $oMemberModel =& getModel('member');

            $args->module_srl = Context::get('module_srl');
            if(!$args->module_srl) return new Object(-1,'msg_invalid_request');

            if(!Context::get('is_logged')) return new Object(-1,'msg_not_permitted');

            $logged_info = Context::get('logged_info');
            $args->member_srl = $logged_info->member_srl;

            $output = executeQuery('mailing.getMemberJoined', $args);
            if($output->data) {
                return executeQuery("mailing.deleteMember", $args);
            } else {
                $member_info = $oMemberModel->getMemberInfoByMemberSrl($args->member_srl);
                $args->email_address = $member_info->email_address;
                return executeQuery("mailing.insertMember", $args);
            }
        }

        function removeOrgMsg($body) {
            preg_match("/<div[^>]*>(.+)<div style=\'border/siU", $body, $match);
            if($match) return $match[1];
            return $body;
        }

        function replaceCid($body, $reverse = false) {
            $cid_array = Context::get('cid_array');
            if(!$cid_array) return $body;
            foreach($cid_array as $cid => $uploaded_filename)
            {
                if($reverse)
                {
                    $body = str_replace($uploaded_filename, "cid:".$cid, $body);
                }
                else
                {
                    $body = str_replace("cid:".$cid, $uploaded_filename, $body);
                }
            }
            return $body;
        }

        function writeComment($comment_srl, &$mailMessage, &$member_info, $reference_srl, $module_srl)
        {
            $obj = null;
            $oCommentModel =& getModel('comment');
            $oComment = $oCommentModel->getComment($reference_srl);
            if($oComment->isExists())
            {
                $obj->document_srl =  $oComment->get('document_srl');
                $obj->parent_srl = $reference_srl;
            }
            else
            {
                $obj->document_srl = $reference_srl;
            }

            $obj->comment_srl = $comment_srl;
            $obj->member_srl = $member_info->member_srl;
            $obj->user_id = $member_info->user_id;
            $obj->user_name = $member_info->user_name;
            $obj->nick_name = $member_info->nick_name;
            $obj->email_address = $member_info->email_address;
            $obj->homepage = $member_info->homepage;
            $obj->content = $this->removeOrgMsg($this->replaceCid($mailMessage->getBody()));
            $obj->module_srl = $module_srl;
            $obj->title = $mailMessage->subject;
            $oCommentController = &getController('comment');
            $output = $oCommentController->insertComment($obj, false);
            return $obj;
        }

        function procMailingInsertMail()
        {
            $message = Context::get('message');
            if(!$message) return;
            $mailMessage = new mailMessage($message['tmp_name']);
            $mailMessage->parse();
            if($mailMessage->messageId)
            {
                preg_match("/@(.+)>$/i", $mailMessage->messageId, $matches);
                if($matches[1] == $_SERVER["SERVER_NAME"])
                {
                    return;
                }
            }

            $fromAddress = mailparse_rfc822_parse_addresses($mailMessage->from);
            $fromAddress = array_shift($fromAddress);
            $fromAddress = $fromAddress["address"];

            $memberModel =& getModel('member');
            $member_srl = $memberModel->getMemberSrlByEmailAddress($fromAddress);
            if(!$member_srl) 
            {
                $mailMessage->close();
                return;
            }
            $member_info = $memberModel->getMemberInfoByMemberSrl($member_srl);

			$oModuleModel =& getModel('module');
            $mailingConfig = $oModuleModel->getModuleConfig('mailing');
            $maildomain = $mailingConfig->maildomain;
            if(!$maildomain)
            {
                $maildomain = $_SERVER["SERVER_NAME"];
            }

            $toAddress = mailparse_rfc822_parse_addresses($mailMessage->to);
			foreach($toAddress as $objAddress)
			{
				$obj = $objAddress["address"];
				$mailingAddress = $obj;
				$mid = explode("@", $obj);
				if($mid[1] != $maildomain) continue;
				$mid = array_shift($mid);
				$mid = explode(".", $mid);
				$site_srl = 0;
				$moduleModel =& getModel('module');
				if(count($mid) > 1)
				{
					$vid = $mid[0];
					$mid = $mid[1];
					$site_info = $moduleModel->getSiteInfoByDomain($vid);
					$site_srl = $site_info->site_srl;
				}
				else
				{
					$mid = $mid[0];
				}

				$targetModule = $moduleModel->getModuleInfoByMid($mid, $site_srl);
				if($targetModule) break;
			}

            if(!$targetModule)
            {
                $mailMessage->close();
                return;
            }
            $oModel =& getModel('mailing');
            if(!$oModel->isMember($targetModule->module_srl, $fromAddress, $member_info->member_srl))
            {
                $mailMessage->close();
                return;
            }
            $oMemberController = &getController('member');
            $oMemberController->doLogin($member_info->user_id);

            $reference_srl = null;
            if($mailMessage->inreplyto)
            {
                $reference_srl = $mailMessage->inreplyto;  
            }
            else if($mailMessage->references)
            {
                $reference_srl = $mailMessage->references;
            }

            if($reference_srl)
            {
                $reference_srl = explode("@", $reference_srl); 
{'fraction_non_alphanumeric': 0.13784672455948066, 'fraction_numerical': 0.003288087007840823, 'mean_word_length': 1.7319207738369415, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '7520483', 'n_tokens_mistral': 3510, 'n_tokens_neox': 3305, 'n_words': 463}