<?php

/**
 * @name          : Joomla Social Pinboard
 * * @version	  : 2.0
 * @package       : apptha
 * @since         : Joomla 1.5
 * @author        : Apptha - http://www.apptha.com
 * @copyright     : Copyright (C) 2011 Powered by Apptha
 * @license       : http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
 * @abstract      : Social Pinboard Component contact model
 * @Creation Date : March 2013
 * @Modified Date : March 2013
 * */
// No direct Access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');

class wishlogueModelcontact extends WishlogueModel {

	function send_feedback() {

		$sender_message = JRequest::getVar('sender_message');
		$sender_subject = JREQUEST::getVar('sender_subject');
		$sender_email = JREQUEST::getVar('sender_email');
		$sender_name = JREQUEST::getVar('sender_name');

		if ($sender_message != '' && $sender_subject != '' && $sender_email != '' && $sender_name != '') {

			$conf = JFactory::getConfig();
			$siteName = $conf->get('sitename');
			$adminMail = $conf->get('mailfrom');
			$userName = $conf->get('fromname');

			$mailer = JFactory::getMailer();
			$app = JFactory::getApplication();
			$baseurl = JURI::root();

			$templateparams = $app->getTemplate(true)->params; // get the tempalte parameters
			$logo = $templateparams->get('logo'); //get the logo
			if ($logo != null) {
				$image_source = JURI::base() . '/' . htmlspecialchars($logo);
			} else {
				$image_source = JURI::base() . '/templates/wishlogue/images/logo-large.png';
			}

			$mailer->setSender($sender_email);
			$mailer->addRecipient($adminMail);

			$content = file_get_contents(JURI::base() . '/templates/wishlogue/emailtemplate/send_feedback.html');
			$content = str_replace("{baseurl}", $baseurl, $content);
			$content = str_replace("{site_logo}", $image_source, $content);
			$content = str_replace("{site_name}", $siteName, $content);
			$content = str_replace("{username}", $userName, $content);
			$content = str_replace("{from}", $sender_name . ' ( ' . $sender_email . ' ) ', $content);
			$content = str_replace("{date}", date('d-M-Y h:i:s A'), $content);
			$content = str_replace("{subject}", $sender_subject, $content);
			$content = str_replace("{message}", $sender_message, $content);

			$mailer->isHTML(true);
			$mailer->setSubject($sender_subject);
			$mailer->Encoding = 'base64';
			$mailer->setBody($content);

			$send = $mailer->Send();
			if ($send) {
				return 'success';
			}
			return 'failed';
		}
	}

	function captcha_code() {
		session_start();

		$image_width = 120;
		$image_height = 40;
		$characters_on_image = 6;

		$font = JPATH_COMPONENT . '/helpers/monofont.ttf';

		$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
		$random_dots = 10;
		$random_lines = 30;
		$captcha_text_color = "0x142864";
		$captcha_noice_color = "0x142864";

		$code = '';

		$i = 0;
		while ($i < $characters_on_image) {
			$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters) - 1), 1);
			$i++;
		}

		$_SESSION['6_letters_code'] = $code;

		$font_size = $image_height * 0.75;
		$image = @imagecreate($image_width, $image_height);

		$background_color = imagecolorallocate($image, 255, 255, 255);

		$arr_text_color = wishlogueModelcontact::hexrgb($captcha_text_color);

		$text_color = imagecolorallocate($image, $arr_text_color['red'], $arr_text_color['green'], $arr_text_color['blue']);

		$arr_noice_color = wishlogueModelcontact::hexrgb($captcha_noice_color);
		$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], $arr_noice_color['green'], $arr_noice_color['blue']);

		for ($i = 0; $i < $random_dots; $i++) {
			imagefilledellipse($image, mt_rand(0, $image_width), mt_rand(0, $image_height), 2, 3, $image_noise_color);
		}

		for ($i = 0; $i < $random_lines; $i++) {
			imageline($image, mt_rand(0, $image_width), mt_rand(0, $image_height), mt_rand(0, $image_width), mt_rand(0, $image_height), $image_noise_color);
		}

		$textbox = imagettfbbox($font_size, 0, $font, $code);
		$x = ($image_width - $textbox[4]) / 2;
		$y = ($image_height - $textbox[5]) / 2;
		imagettftext($image, $font_size, 0, $x, $y, $text_color, $font, $code);

		ob_end_clean();
		JResponse::clearHeaders();
		JResponse::setHeader('Content-Type', 'image/jpeg', true);
		JResponse::sendHeaders();

		imagejpeg($image); //showing the image

		imagedestroy($image); //destroying the image instance
	}

	function hexrgb($hexstr) {
		$int = hexdec($hexstr);
		return array("red" => 0xFF & ($int >> 0x10),
			"green" => 0xFF & ($int >> 0x8),
			"blue" => 0xFF & $int);
	}

	function captcha_session() {
		ob_end_clean();
		session_start();
		return $_SESSION['6_letters_code'];
	}
}

?>