<commit_before><?php

require_once 'Services/Akismet2.php';
require_once 'Swat/SwatUI.php';
require_once 'Swat/SwatString.php';
require_once 'Site/dataobjects/SiteContactMessage.php';
require_once 'Site/SiteMultipartMailMessage.php';
require_once 'Site/pages/SiteDBEditPage.php';

/**
 *
 * @package   Site
 * @copyright 2006-2010 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
class SiteContactPage extends SiteDBEditPage
{
	// {{{ protected function getUiXml()

	protected function getUiXml()
	{
		return 'Site/pages/contact.xml';
	}

	// }}}

	// process phase
	// {{{ protected function saveData()

	protected function saveData(SwatForm $form)
	{
		$class_name = SwatDBClassMap::get('SiteContactMessage');
		$contact_message = new $class_name();
		$contact_message->setDatabase($this->app->db);

		$this->processMessage($contact_message);

		$contact_message->spam = $this->isMessageSpam($contact_message);
		$contact_message->save();
	}

	// }}}
	// {{{ protected function processMessage()

	protected function processMessage(SiteContactMessage $message)
	{
		$message->email    = $this->ui->getWidget('email')->value;
		$message->subject  = $this->ui->getWidget('subject')->value;
		$message->message  = $this->ui->getWidget('message')->value;
		$message->instance = $this->app->getInstance();

		if (isset($_SERVER['REMOTE_ADDR'])) {
			$message->ip_address = substr($_SERVER['REMOTE_ADDR'], 0, 15);
		}

		if (isset($_SERVER['HTTP_USER_AGENT'])) {
			$message->user_agent = substr($_SERVER['HTTP_USER_AGENT'], 0, 255);
		}

		$message->createdate = new SwatDate();
		$message->createdate->toUTC();
	}

	// }}}
	// {{{ protected function isMessageSpam()

	protected function isMessageSpam(SiteContactMessage $message)
	{
		$is_spam = false;

		if ($this->app->config->comment->akismet_key !== null) {
			$uri = $this->app->getBaseHref();
			try {
				$akismet = new Services_Akismet2($uri,
					$this->app->config->comment->akismet_key);

				$is_spam = $akismet->isSpam(
					$this->getAkismetComment($message), true);

			} catch (Exception $e) {
			}
		}

		return $is_spam;
	}

	// }}}
	// {{{ protected function getAkismetComment()

	protected function getAkismetComment(SiteContactMessage $message)
	{
		return new Services_Akismet2_Comment(
			array(
				'comment_author_email' => $message->email,
				'comment_content'      => $message->message,
				'comment_type'         => 'comment',
				'permalink'            => $uri.$this->source,
				'user_ip'              => $message->ip_address,
				'user_agent'           => $message->user_agent,
			)
		);
	}

	// }}}
	// {{{ protected function getRollbackMessage()

	protected function getRollbackMessage(SwatForm $form)
	{
		$message = new SwatMessage(
			Site::_('An error has occurred. Your message was not sent.'),
			'system-error');

		$message->secondary_content = sprintf(Site::_(<commit_msg>Fix undefined variable 'uri'.<commit_after><?php

require_once 'Services/Akismet2.php';
require_once 'Swat/SwatUI.php';
require_once 'Swat/SwatString.php';
require_once 'Site/dataobjects/SiteContactMessage.php';
require_once 'Site/SiteMultipartMailMessage.php';
require_once 'Site/pages/SiteDBEditPage.php';

/**
 *
 * @package   Site
 * @copyright 2006-2010 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
class SiteContactPage extends SiteDBEditPage
{
	// {{{ protected function getUiXml()

	protected function getUiXml()
	{
		return 'Site/pages/contact.xml';
	}

	// }}}

	// process phase
	// {{{ protected function saveData()

	protected function saveData(SwatForm $form)
	{
		$class_name = SwatDBClassMap::get('SiteContactMessage');
		$contact_message = new $class_name();
		$contact_message->setDatabase($this->app->db);

		$this->processMessage($contact_message);

		$contact_message->spam = $this->isMessageSpam($contact_message);
		$contact_message->save();
	}

	// }}}
	// {{{ protected function processMessage()

	protected function processMessage(SiteContactMessage $message)
	{
		$message->email    = $this->ui->getWidget('email')->value;
		$message->subject  = $this->ui->getWidget('subject')->value;
		$message->message  = $this->ui->getWidget('message')->value;
		$message->instance = $this->app->getInstance();

		if (isset($_SERVER['REMOTE_ADDR'])) {
			$message->ip_address = substr($_SERVER['REMOTE_ADDR'], 0, 15);
		}

		if (isset($_SERVER['HTTP_USER_AGENT'])) {
			$message->user_agent = substr($_SERVER['HTTP_USER_AGENT'], 0, 255);
		}

		$message->createdate = new SwatDate();
		$message->createdate->toUTC();
	}

	// }}}
	// {{{ protected function isMessageSpam()

	protected function isMessageSpam(SiteContactMessage $message)
	{
		$is_spam = false;

		if ($this->app->config->comment->akismet_key !== null) {
			$uri = $this->app->getBaseHref();
			try {
				$akismet = new Services_Akismet2($uri,
					$this->app->config->comment->akismet_key);

				$is_spam = $akismet->isSpam(
					$this->getAkismetComment($message), true);

			} catch (Exception $e) {
			}
		}

		return $is_spam;
	}

	// }}}
	// {{{ protected function getAkismetComment()

	protected function getAkismetComment(SiteContactMessage $message)
	{
		$uri = $this->app->getBaseHref();

		return new Services_Akismet2_Comment(
			array(
				'comment_author_email' => $message->email,
				'comment_content'      => $message->message,
				'comment_type'         => 'comment',
				'permalink'            => $uri.$this->source,
				'user_ip'              => $message->ip_address,
				'user_agent'           => $message->user_agent,
			)
		);
	}

	// }}}
	// {{{ protected function getRollbackMessage()

	protected function getRollbackMessage(SwatForm $form)
	{
		$message = new SwatMessage(
			Site::_('An error has occurred. Your message was not sent.'),
			'system-error');

		$message->secondary_content = sprintf(Site::_(
{'fraction_non_alphanumeric': 0.1681748335325252, 'fraction_numerical': 0.006829434864264982, 'mean_word_length': 4.080659150043365, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '6175932', 'n_tokens_mistral': 2183, 'n_tokens_neox': 1934, 'n_words': 320}