<?php
#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: MAMS (c) 2020-2021 by CMS Made Simple Foundation
#  An add-on module for CMS Made Simple to provide useful functions
#  and commonly used gui capabilities to other modules.
#-------------------------------------------------------------------------
# A fork of:
#
# Module: FrontEndUsers (c) 2008-2014 by Robert Campbell
#         (calguy1000@cmsmadesimple.org)
#
#-------------------------------------------------------------------------
#
# CMSMS - CMS Made Simple is (c) 2006 - 2021 by CMS Made Simple Foundation
# CMSMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# Visit the CMSMS Homepage at: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# 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.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin
# section that the site was built with CMS Made simple.
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE

namespace MAMS;

class email_verify_assistant extends verification_assistant
{
    private $_template;
    private $_subject;
    private $_title;
    private $_msg;
    private $_code_prompt;
    private $_link_prompt;

    private function get_default_template()
    {
        $typename = 'MAMS::forgot password email';
        $tpl = \CmsLayoutTemplate::load_dflt_by_type($typename);
        if( !$tpl ) {
            audit('','MAMS','No default template of type '.$typename.' found');
            throw new \RuntimeException('No default template of type '.$typename);
        }
        return $tpl->get_name();
    }

    public function __construct( \MAMS $mams )
    {
        parent::__construct($mams);

        $this->_template = $this->get_default_template();
        $this->_subject = 'Please verify your account for {sitename}';
        $this->_title = 'Please verify your account';
        $this->_msg = '<p>For security purposes we need to validate your identity.  This is done by verifying that you received this email correctly, and can follow these instructions.</p>
<p>Below, We have provided a unique security code to use to validate your credentials. Please click on the link provided and enter the security code. <em>It may already be filled in on the form</em>.  This will complete the operation and allow you to login to the service.</p>';
        $this->_code_prompt = 'Your security code is:';
        $this->_link_prompt = 'Please click on this link';
    }

    public function SetTemplate($str)
    {
        if( $str ) $this->_template = $str;
    }

    public function SetTitle($str)
    {
        if( $str ) $this->_title = $str;
    }

    public function SetSubject($str)
    {
        if( $str ) $this->_subject = $str;
    }

    public function SetMessage($str)
    {
        if( $str ) $this->_msg = $str;
    }

    public function SetCodePrompt($str)
    {
        if( $str ) $this->_code_prompt = $str;
    }

    public function SetLinkPrompt($str)
    {
        if( $str ) $this->_link_prompt = $str;
    }

    public function send( $uid, $code )
    {
        $uid = (int) $uid;
        $code = trim($code);
        $destpage = $this->get_destpage();
        if( $uid < 1 || empty($code) ) throw new \InvalidArgumentException('Bad data passed to '.__METHOD__);
        $email = $this->mams->GetEmail($uid);
        if( !$email ) throw new \LogicException('Attempt to create a '.__CLASS__.' for a user without a valid email address');

        // code will be sent in the URL
        $url = $this->get_verify_form_url( $uid, $code );

        $tpl = $this->mams->CreateSmartyTemplate($this->_template);
        $tpl->assign('title',$this->_title);
        $tpl->assign('message_forgotpwmail',$this->_msg);
        $tpl->assign('message_forgotpwemail',$this->_msg);
        $tpl->assign('message',$this->_msg);
        $tpl->assign('prompt_code',$this->_code_prompt);
        $tpl->assign('prompt_link',$this->_link_prompt);
        $tpl->assign('destpage',$destpage);
        $tpl->assign('data_code',$code);
        $tpl->assign('subject',$this->_subject);
        $tpl->assign('data_url',$url);
        $body = $tpl->fetch();

        // duplicated code... I know, I know... shaddap
        $smarty = $this->mams->cms->GetSmarty();
        $tpl = $smarty->CreateTemplate('string:'.$this->_subject);
        $tpl->assign('title',$this->_title);
        $tpl->assign('message_forgotpwmail',$this->_msg);
        $tpl->assign('message_forgotpwemail',$this->_msg);
        $tpl->assign('message',$this->_msg);
        $tpl->assign('prompt_code',$this->_code_prompt);
        $tpl->assign('prompt_link',$this->_link_prompt);
        $tpl->assign('destpage',$destpage);
        $tpl->assign('data_code',$code);
        $tpl->assign('subject',$this->_subject);
        $tpl->assign('data_url',$url);
        $subject = $tpl->fetch();

        $mailer = new \cms_mailer();
        $mailer->AddAddress($email);
        $mailer->SetBody($body);
        $mailer->SetSubject($subject);
        $mailer->IsHTML(TRUE);
        $mailer->SetPriority(1);
        $mailer->Send();

        audit($uid,$this->mams->GetName(),'Verification email sent');
        $this->mams->add_history($uid,'init_verify');
    }
}
