<?php if(!defined('master_script')) die();

/**
 * Default settings
 *
 * @category   Module (comments)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-07-28
 */
	
	global $modules, $system_data, $user_data, $page_info, $comments, $id;
	
	if(!isset($module_settings["comments"]["embedded"])) $module_settings["comments"]["embedded"] = 0;
	if(!isset($module_settings["comments"]["email notification"])) $module_settings["comments"]["email notification"] = 0;
	if(!isset($module_settings["comments"]["closed"])) $module_settings["comments"]["closed"] = 0;

/**
 * Languages
 *
 * @category   Module (comments)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-07-28
 */

	$modules['comments']['key'] = 'comments';
	include_txt($modules['comments']['key'], $system_data['folder']['modules'] . $modules['comments']['key'] . '/languages/');
	include_txt($modules['comments']['key'], $system_data['folder']['settings'] . 'modules/' . $modules['comments']['key'] . '/languages/');

/**
 * Get comments for file or page
 *
 * @category   Module (comments)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-05-14
 */
	
	// Settings
	$module_settings["comments"]["allowed tags"] = array('a', 'abbr', 'acronym', 'b', 'blockquote', 'cite', 'code', 'del', 'em', 'i', 'q', 'strike', 'strong');
	
	// Default values
	$module_settings["comments"]["comment name"] = '';
	$module_settings["comments"]["comment email"] = '';
	$module_settings["comments"]["comment website"] = '';
	$module_settings["comments"]["comment text"] = '';
	
	// Module ID
	$id = isset($id) ? $id++ : 1;
	
	// Paths and files
	$page_storage_path = $system_data['folder']['pages'] . $page_info->alias . '/storage/';
	$comments_file = $page_storage_path . $page_info->alias . '.xml';
	if(isset($_POST['comments_xml_file']) && strrchr($_POST['comments_xml_file'], '.') == '.xml') $comments_file = $_POST['comments_xml_file'];
	
	// XML template for comment file
	$xml_content = is_file($comments_file) ? simplexml_load_file($comments_file) : simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><file></file>');
	
	// Default values
	$number_of_comments = isset($xml_content->comments) ? count($xml_content->comments[0]->children()) : 0;
	$comments = array();
	
	// Create path for comment files if not exists
	if(!file_exists($page_storage_path)) mkdir($page_storage_path);
	
	// Load comments
	if($module_settings["comments"]["embedded"] == 0) {
		for($i = 0; $i < $number_of_comments; $i++) {
			$comments[$i]['name'] = $xml_content->comments[0]->comment[$i]->name;
			$comments[$i]['email'] = $xml_content->comments[0]->comment[$i]->email;
			$comments[$i]['website'] = $xml_content->comments[0]->comment[$i]->website;
			$comments[$i]['timestamp'] = date($user_data["date format print"], intval($xml_content->comments[0]->comment[$i]->timestamp));
			$comments[$i]['text'] = $xml_content->comments[0]->comment[$i]->text;
		}
	}

/**
 * Save comment for file or page
 *
 * @category   Module (comments)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2010-07-26
 */

	$go = 1;
	
	// Validate form
	if(!isset($_POST['submit_comment'])) $go = 0;
	if($module_settings["comments"]["closed"] == 1) $go = 0;
	if(isset($_POST['current_url']) && substr($_POST['current_url'], 0, strlen('http://' . $_SERVER['SERVER_NAME'])) != 'http://' . $_SERVER['SERVER_NAME']) $go = 0;
	
	if(isset($_POST['required']) && time() - intval($_POST['required']) < 5) {
		$module_settings["comments"]["message"] = txt('comments', 'Bitte warten Sie mindestens 5 Sekunden, bevor Sie das Formular abschicken.');
		$go = 0;
	}

	// On submit
	if($go == 1) {
		// Validate user data
		$_POST['name'] = stripslashes(strip_tags(trim($_POST['name'])));
		$_POST['email'] = stripslashes(strip_tags(trim($_POST['email'])));
		$_POST['website'] = stripslashes(strip_tags(trim($_POST['website'])));
		$_POST['text'] = trim(nl2br(stripslashes(strip_tags($_POST['text'], '<' . implode('><', $module_settings["comments"]["allowed tags"]) . '>'))));
		
		if($_POST['name'] == '' || $_POST['text'] == '' || !is_email($_POST['email'])) {
			// Error handling
			$module_settings["comments"]["comment name"] = $_POST['name'];
			$module_settings["comments"]["comment email"] = $_POST['email'];
			$module_settings["comments"]["comment website"] = $_POST['website'];
			$module_settings["comments"]["comment text"] = $_POST['text'];
			
			$module_settings["comments"]["message"] = txt('comments', 'Bitte füllen Sie alle markierten Felder aus.');
		} else {
			// Save comment
			if($_POST['website'] != '' && substr($_POST['website'], 0, 7) != 'http://') $_POST['website'] = 'http://' . $_POST['website'];
			
			$timestamp = time();
			
			if($module_settings["comments"]["embedded"] == 0) {
				$i = count($comments);
				$comments[$i]['name'] = $_POST['name'];
				$comments[$i]['email'] = $_POST['email'];
				$comments[$i]['website'] = $_POST['website'];
				$comments[$i]['timestamp'] = date($user_data["date format print"], intval($timestamp));
				$comments[$i]['text'] = $_POST['text'];
			}
			
			connector('comments frontend save comment');
			
    		$xml_content->comments[0]->comment[$number_of_comments]->name = xml_string($_POST['name']);
    		$xml_content->comments[0]->comment[$number_of_comments]->email = xml_string($_POST['email']);
    		$xml_content->comments[0]->comment[$number_of_comments]->website = xml_string($_POST['website']);
    		$xml_content->comments[0]->comment[$number_of_comments]->timestamp = xml_string($timestamp);
    		$xml_content->comments[0]->comment[$number_of_comments]->text = xml_string($_POST['text']);
    		
    		$xml_content->saveXML($comments_file);
    		
    		$module_settings["comments"]["comment submitted"] = 1;
    		
    		// Send email notification
    		if($module_settings["comments"]["email notification"] == 1 && $user_data["email"] != '') {
				$mail_header[] = 'From: ' . $user_data['email'];
				$mail_header[] = 'Reply-To: ' . $_POST['email'];
				$mail_header[] = 'Return-Path: ' . $user_data['email'];
				$mail_header[] = 'X-Mailer: PHP/' . phpversion();
				$mail_header[] = 'X-Sender-IP: ' . $_SERVER['REMOTE_ADDR'];
				$mail_header[] = 'Content-type: text/plain; charset="UTF-8"';

				$content[] = txt('comments', 'Auf Ihrer Website wurde ein neuer Kommentar geschrieben.');
				$content[] = txt('comments', 'Von') . ' ' . $_POST['name'] . ($_POST['website'] != '' ? ' (' . $_POST['website'] . ')' : ' ') . ', ' . date("d.m.Y, H:i", $timestamp);
				$content[] = strip_tags($_POST['text']);
				$content[] = txt('comments', 'Kommentar anzeigen') . ': ' . $_POST['current_url'];
				
				connector('comments frontend send notification');
				
    			mail($user_data["email"], txt('comments', 'Neuer Kommentar'), implode("\n\n", $content), implode("\n", $mail_header), "-f " . $user_data['email']);
    		}
		}
	}

/**
 * Include module frontend template
 *
 * @category   Module (comments)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   1.0
 */

	include_template('comments');

?>