<?php
require_once('recaptchalib.php');
class Feedback extends Controller {

	function Feedback()
	{
		parent::Controller();
		$this->load->helper(array('form', 'url'));
		$this->publickey = "6LdwCMMSAAAAAEy-2pp-BPy1eI-B8sm5OVJx8OcT";
		$this->privatekey = "6LdwCMMSAAAAABM8EBh2J_66WcZCBfVuROHfCT6S";
	}

	function index()
	{ 
		$this->load->Model('Main');
		$data['message']='';
		$data['capcha']='';
		$bUseCapcha=0;
		$bCapchaValid=1;

		$sql = mysql_query('SELECT have_feedback 
							FROM users 
							WHERE email = "'.$this->session->userdata('email').'"');
		$row = mysql_fetch_array($sql);
		 if(mysql_num_rows($sql))
			   {
				   	if($row['have_feedback']==1)
				   	{
						$bUseCapcha=1;
				   	}
			   }	   
		
		
		$data['whatlike'] = $this->input->post('whatlike');
		$data['notlike'] = $this->input->post('notlike');
		$data['addfeature'] = $this->input->post('addfeature');
		
		if($this->input->post('add'))
		{
			if($bUseCapcha)
			{
				if ($_POST["recaptcha_response_field"]) 
				{
		        	$resp = recaptcha_check_answer ($this->privatekey,
		                                        $_SERVER["REMOTE_ADDR"],
		                                        $_POST["recaptcha_challenge_field"],
		                                        $_POST["recaptcha_response_field"]);

			        if (!$resp->is_valid) 
			        {
			        	$bCapchaValid=0;
			        } 
				}
				else 
				{
					$bCapchaValid=0;
				}
			}
			
			if($bCapchaValid)
			{
				if(!empty($data['whatlike'])  && !empty($data['notlike']) && !empty($data['addfeature']))
				{
					$this->sendMail($data);
					
					$sql = mysql_query('UPDATE users 
										SET have_feedback =1 
										WHERE email = "'.$this->session->userdata('email').'"');
					
					$data['whatlike']='';
					$data['notlike']='';
					$data['addfeature']='';
					$data['message']='Your feedback has been submitted. Thank you!';
					$bUseCapcha=1;
				}
			}
			else 
			{
				$data['message']='Please enter a valid text from image';
			}
		}
		
		if($bUseCapcha)
		{
			$data['capcha']=recaptcha_get_html($this->publickey , '');
		}
		
		$this->load->view('redirect_login');
		$this->load->view('header');
		$this->load->view('feedbackhtml',$data);
		$this->load->view('footer');
	}

	private function sendMail($data)
	{
		$message = '
			<html>
			<body>
			  <table>
			    <tr>
			       <td>
			        <b>What do you like about Sounit?</b><br/>
			        '.$data['whatlike'].'
			        <br/>
			        <br/>
			        </td>
				</tr>
				<tr>
			        <td>
			        <b>What you do not like about Sounit?</b><br/>
			        '.$data['notlike'].'
			        <br/>
			        <br/>
			        </td>
				</tr>
				<tr>
				    <td>
				      <b>What features you would like to be included in our next releases?</b><br/>
				      '.$data['addfeature'].'
				    </td>
			    </tr>
			  </table>
			</body>
			</html>';
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		mail('admin@sounit.com', 'User add Feedback', $message, $headers);
	}
}