<?php
    lt_include( PLOG_CLASS_PATH."class/action/blogaction.class.php" );
    lt_include( PLOG_CLASS_PATH."class/view/plugintemplatedview.class.php" );
    lt_include( PLOG_CLASS_PATH."class/view/errorview.class.php" );
    lt_include( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
    lt_include( PLOG_CLASS_PATH."class/data/validator/emailvalidator.class.php" );
	lt_include( PLOG_CLASS_PATH."class/mail/emailservice.class.php" );
    lt_include( PLOG_CLASS_PATH."class/data/filter/allowedhtmlfilter.class.php" );
    lt_include( PLOG_CLASS_PATH."class/data/filter/javascriptfilter.class.php" );
    
    /**
     * Checks the data and sends an email if everything validates
     */
    class ContactPluginSendAction extends BlogAction
    {
        function ContactPluginSendAction($actionInfo, $request){
            $this->BlogAction($actionInfo, $request);

        	$this->registerFieldValidator("userName", new StringValidator());
        	$this->registerFieldValidator("userEmail", new EmailValidator());
        	$this->registerFieldValidator("commentTopic", new StringValidator());
        	$this->registerFieldValidator("commentText", new StringValidator( true ));

			$f = new AllowedHtmlFilter();
			$f->addFilter( new JavascriptFilter());
			$this->_request->registerFilter( "commentText", $f );			
            
			$view = new ErrorView($this->_blogInfo);
        	$view->setErrorMessage($this->_locale->tr("plugin_contact_error_data"));
        	$this->setValidationErrorView($view);
        }


        function validate(){
            if(!parent::validate())
                return false;

            $blogSettings = $this->_blogInfo->getSettings();

                // get words to match
			$bannedKeywords = $blogSettings->getValue("plugin_contact_banned_keywords");
                // no banned words, exit out
            if(!$bannedKeywords)
                return true;

                // we use comma delimited in the database and for the users, but we need
                // a 'pipe' symbol for the regexp to work
            $bannedKeywords = str_replace(",", "|", $bannedKeywords);

            $regexp = "/($bannedKeywords)/";
            
            $text = $this->_request->getValue("commentText");
            $subject = $this->_request->getValue("commentTopic");

            if(preg_match($regexp, $text) || preg_match($regexp, $subject)){
            	$this->_view = new ErrorView( $this->_blogInfo, $this->_locale->tr("plugin_contact_error_banned_keywords"));
                $this->setCommonData();
                return false;
            }

            return true;
        }
        
        function perform(){
            $username = $this->_request->getValue("userName");
            $emailAddress = $this->_request->getValue("userEmail");
            $text = $this->_request->getValue("commentText");
            $subject = $this->_request->getValue("commentTopic");

            if($this->_checkSpam($username, $emailAddress, $text, $subject)){
                $this->_view = new ErrorView($this->_blogInfo);
                $this->_view->setErrorMessage($this->_locale->tr("plugin_contact_error_spam"));
                $this->setCommonData();
                return false;
            }
            
            $postText = $this->_locale->pr("plugin_contact_contact_from",
                                           "$username <$emailAddress>");
            $postText .= $text;

            $config =& Config::getConfig();

            $message = new EmailMessage();
            $message->setFrom($config->getValue( "post_notification_source_address"));
            $message->setSubject($subject);
            $message->setCharset($this->_locale->getCharset());
            $message->setBody($postText);

            $user =& $this->_blogInfo->getOwnerInfo();
            $message->setTo($user->getEmail());
            
            $service = new EmailService();
            $service->sendMessage($message);
        
            $this->_view = new PluginTemplatedView($this->_blogInfo, "contact", "accepted");
            $this->setCommonData();
            return true;
        }

            // run the inputs through the bayesian filter to see if it thinks it is spam
            // copied from bayesianfilter::getSpamProbability since that function is marked
            // as private
        function _checkSpam($userName, $userEmail, $text, $topic){
                // make a copy of the request so we can fake the filters into running
                // during our action
            $request = new Request($this->_request->getAsArray());
            $request->setValue("op", "AddComment");
                
            $pipeline = new Pipeline($request, $this->_blogInfo);
            $result = $pipeline->process();

                // if the pipeline blocked the request, then we have
                // to let the user know
            if( !$result->isValid())
                return true;

            return false;
        }
        
    }
