<?php
/***************************************************************************
 *                                presentation_form.php
 *                            -------------------
 *   begin                : Saturday, Sept 23, 2005
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : support@phpbb.com
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

define('IN_PHPBB', true);
define('IN_PRESENTATION_FORM', true);

$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
// Fonction Insert_Post de Netclectic
include($phpbb_root_path . 'includes/functions_insert_post.'.$phpEx);

$submit = ( isset($_POST['submit']) ) ? TRUE : FALSE; 
$pseudo = ( !empty($_POST['pseudo']) ) ? stripslashes(htmlspecialchars($_POST['pseudo'])) : "";
$presentation = ( !empty($_POST['presentation']) ) ? stripslashes(htmlspecialchars($_POST['presentation'])) : "";

$loisirs_1 = ( !empty($_POST['loisirs_1']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_1'])) : "";
$loisirs_2 = ( !empty($_POST['loisirs_2']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_2'])) : "";
$loisirs_3 = ( !empty($_POST['loisirs_3']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_3'])) : "";
$loisirs_4 = ( !empty($_POST['loisirs_4']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_4'])) : "";
$loisirs_5 = ( !empty($_POST['loisirs_5']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_5'])) : "";
$loisirs_6 = ( !empty($_POST['loisirs_6']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_6'])) : "";
$loisirs_7 = ( !empty($_POST['loisirs_7']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_7'])) : "";
$loisirs_8 = ( !empty($_POST['loisirs_8']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_8'])) : "";
$loisirs_9 = ( !empty($_POST['loisirs_9']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_9'])) : "";
$loisirs_10 = ( !empty($_POST['losirs_10']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_10'])) : "";
$loisirs_11 = ( !empty($_POST['loisirs_11']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_11'])) : "";
$loisirs_12 = ( !empty($_POST['loisirs_12']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_12'])) : "";
$loisirs_13 = ( !empty($_POST['loisirs_13']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_13'])) : "";
$loisirs_14 = ( !empty($_POST['loisirs_14']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_14'])) : "";
$loisirs_15 = ( !empty($_POST['loisirs_15']) ) ? stripslashes(htmlspecialchars($_POST['loisirs_15'])) : "";

$qualites = ( !empty($_POST['qualites']) ) ? stripslashes(htmlspecialchars($_POST['qualites'])) : "";
$defauts = ( !empty($_POST['defauts']) ) ? stripslashes(htmlspecialchars($_POST['defauts'])) : "";
$info = ( !empty($_POST['info']) ) ? stripslashes(htmlspecialchars($_POST['info'])) : "";
$adresse = ( !empty($_POST['adresse']) ) ? stripslashes(htmlspecialchars($_POST['adresse'])) : "";
$photo = ( !empty($_POST['photo']) ) ? stripslashes(htmlspecialchars($_POST['photo'])) : "";

//
// Début Attachment
//

// Tableau qui contiendra le nom des fichiers à uploader
$filenames = array();
$files = array();


// Pour uploader 3 fichiers (0, 1, 2)
for ( $i = 0; $i <= 2; $i++ )
{
	// Nom réel du fichier
	$filename = ($HTTP_POST_FILES['fileupload']['name'][$i] != 'none') ? trim( $HTTP_POST_FILES['fileupload']['name'][$i] ) : '';
	// Nom du fichier sur le server. Ex: c:\Windows\tmp\fdgdFDG.tmp
	$file = ($HTTP_POST_FILES['fileupload']['tmp_name'][$i] != 'none') ? trim( $HTTP_POST_FILES['fileupload']['tmp_name'][$i] ) : '';

	// Fichier sur le serveur
	if ( !empty($file) )
	{
		$filenames[] = $file;
	}

	// Vrai nom des fichiers
	if ( !empty($filename) )
	{
		$files[] = $filename;
	}
}

/*

// Pour uploader juste 1 fichier

// Nom réel du fichier
$filename = ($HTTP_POST_FILES['fileupload']['name'] != 'none') ? trim( $HTTP_POST_FILES['fileupload']['name'] ) : '';
// Nom du fichier sur le server. Ex: c:\Windows\tmp\fdgdFDG.tmp
$file = ($HTTP_POST_FILES['fileupload']['tmp_name'] != 'none') ? trim( $HTTP_POST_FILES['fileupload']['tmp_name'] ) : '';

// Fichier sur le serveur
$filenames[] = $file;
// Vrai nom des fichiers
$files[] = $filename;

*/

//
// Fin Attachment
//

// Forum où inserer le message
$forum_id = 3;

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_POSTING);
init_userprefs($userdata);
//
// End session management
//

// Si l'utilisateur n'est pas loggé
if ( !$userdata['session_logged_in'] )
{
	$url = append_sid($phpbb_root_path . 'login.' . $phpEx . '?redirect=presentation_form.' . $phpEx);

	$redirection_texte = sprintf('Désolé, vous devez être connecté au forum pour pouvoir visualiser cette page<br /><br />Cliquez %sici%s pour vous connecter', '<a href="' . $url . '">', '</a>');

	message_die(GENERAL_MESSAGE, $redirection_texte);

	$redirect = "mode=newtopic&" . POST_FORUM_URL . "=" . $forum_id;

	redirect(append_sid("login.$phpEx?redirect=presentation_form.$phpEx&" . $redirect, true));
}

//
// Dump out the page header and load viewforum template
//
define('SHOW_ONLINE', true);
$page_title = 'Poster votre présentation';
include($phpbb_root_path . 'includes/page_header.'.$phpEx); 

$upload = $phpbb_root_path . 'attach_mod/includes/functions_attach.'.$phpEx ;
   if ( file_exists($upload) )
    {
      $template->assign_block_vars('upload_enable', array());      
    }

$template->set_filenames(array(
	'body' => 'presentation_form_body.tpl')
);
make_jumpbox('viewforum.'.$phpEx);

$form_enctype = 'enctype="multipart/form-data"';

$template->assign_vars(array(
	'S_POST_ACTION' => append_sid(basename(__FILE__) . '?f=' . $forum_id),
	'S_FORM_ENCTYPE' => $form_enctype,

	'L_PRESENTATION_FORM_AUTHOR' => 'Presentation Form by <a href=mailto:morpheus@2037.org>Morpheus</a> and <a href=mailto:cybernord@hotmail.com>CyberNord</a>',
	'L_TITLE' => $page_title,

	'U_FORM' => basename(__FILE__),
	'MAX_FILE_SIZE_VALUE' => '100000',

	'PSEUDO' => $pseudo,
	'PRESENTATION' => $presentation,
	'LOISIRS_1' => $loisirs_1,
	'LOISIRS_2' => $loisirs_2,
	'LOISIRS_3' => $loisirs_3,
	'LOISIRS_4' => $loisirs_4,
	'LOISIRS_5' => $loisirs_5,
	'LOISIRS_6' => $loisirs_6,
	'LOISIRS_7' => $loisirs_7,
	'LOISIRS_8' => $loisirs_8,
	'LOISIRS_9' => $loisirs_9,
	'LOISIRS_10' => $loisirs_10,
	'LOISIRS_11' => $loisirs_11,
	'LOISIRS_12' => $loisirs_12,
	'LOISIRS_13' => $loisirs_13,
	'LOISIRS_14' => $loisirs_14,
	'LOISIRS_15' => $loisirs_15,
	'QUALITES' => $qualites, 
	'DEFAUTS' => $defauts,
	'INFO' => $info,
	'ADRESSE' => $adresse,
	'PHOTO' => $photo)
);

if ( $submit )
{
	$message_fin = '';

	// Message du post
	$subject = " " . $pseudo;

	$message = "[list]";
	$message .= "[b][align=center][size=18]" . $pseudo . "[/size][/align][/b]" . "\n";
	$message .= " " . "\n";
	$message .= "[b]" . $presentation . "[/b]" . "\n";
	$message .= " " . "\n";
	$message .= "[b]:arrow: Loisirs :[/b] " . $loisirs_1 . $loisirs_2 . $loisirs_3 . $loisirs_4 . $loisirs_5 . $loisirs_6 . $loisirs_7 . $loisirs_8 . "\n";
	$message .= "[list]" . $loisirs_9 . $loisirs_10 . $loisirs_11 . $loisirs_12 . $loisirs_13 . $loisirs_14 . $loisirs_15 . "[/list]" . "\n";   
	
	if ( $qualites != "" )
	{
		$message .= "[b]:arrow: Qualités :[/b] " . $qualites . "\n";
	}
	if ( $defauts != "" )
	{ 
	$message .= "[b]:arrow: Défauts :[/b] " . $defauts . "\n";
	}
	$message .= " " . "\n";
	if ( $info != "" )
	{
	$message .= "[b]:arrow: Comment j'ai connu le forum :[/b] " . $info . "\n";
	}
	if ( $adresse != "" )
	{
		$message .= "[b]:arrow: Adresse du forum ou du site :[/b] " . $adresse . "\n";
	} 
	if ( $photo != "" )
	{
		$photo .= " " . $photo . "\n";
	}
	$message .= "[/list]";

	// On trie le tableau
	$ary = implode(",", $filenames);

	// S'il n'y a pas de fichier à uploader
	if ( $ary == '' )
	{
		// On utilise la fonction d'insertion directe
		$post_details = insert_post($message, $subject, $forum_id, $userdata['user_id'], $userdata['username'], $userdata['user_attachsig']);
	}
	else // Sinon
	{
		// array_multisort($filenames, $files);

		// Si on ne peut pas uploader les fichiers sur le serveur
		if ( !move_files_on_server($filenames, $files) )
		{
			$message_fin .= 'Une erreur s\'est produite lors de l\'upload de vos fichiers.<br /><br />';

			// Debug
			// print_r($files);
			// print_r($filenames);

			// On poste le message sans les fichier joints
			$post_details = insert_post($message, $subject, $forum_id, $userdata['user_id'], $userdata['username'], $userdata['user_attachsig']);
		}
		else // Sinon
		{
			// On insère un post avec attachment
			$post_details = insert_post_with_attach($message, $subject, $forum_id, $userdata['user_id'], $userdata['username'], $userdata['user_attachsig'], $files);
		}
	}

	// On active la notification des messages
	$sql = "INSERT INTO " . TOPICS_WATCH_TABLE . " (topic_id, user_id, notify_status) 
			VALUES ( " . $post_details['topic_id'] . ", " . $userdata['user_id'] . ", " . TOPIC_WATCH_NOTIFIED . ")";
	if (!($result = $db->sql_query($sql)))
	{
		message_die(GENERAL_ERROR, 'La notification des messages n\'a pas été activée', '', __LINE__, __FILE__, $sql);
	}

    $url = append_sid($phpbb_root_path . 'viewtopic.' . $phpEx . '?' . POST_TOPIC_URL . '=' . $post_details['topic_id']);

	// Message Privé
	$script_name = preg_replace('/^\/?(.*?)\/?$/', "\\1", trim($board_config['script_path']));
	$script_name = ( $script_name != '' ) ? $script_name . '/viewtopic.'.$phpEx : 'viewtopic.'.$phpEx;
	$server_name = trim($board_config['server_name']);
	$server_protocol = ( $board_config['cookie_secure'] ) ? 'https://' : 'http://';
	$server_port = ( $board_config['server_port'] <> 80 ) ? ':' . trim($board_config['server_port']) . '/' : '/';

	$server_url = $server_protocol . $server_name . $server_port . $script_name;

	$url_msg = append_sid($server_url . '?' . POST_TOPIC_URL . '=' . $post_details['topic_id']);

	$pm_subject = "[Rappel] Présentation de " . $pseudo;

	$pm_message = "Votre présentation a bien été postée sur le forum.\n\nVous pouvez la visualiser en cliquant [url=" . $url_msg . "]ici[/url].";

	$pm_message = addslashes($pm_message);

	$to_id = $userdata['user_id'];
	$from_id = 2;

	// Envoi du MP
	// insert_pm($to_id, $pm_message, $pm_subject, $from_id);

	$template->assign_vars(array(
			'META' => '<meta http-equiv="refresh" content="3;url=' . $url . '">')
		);

	$message_fin .=  sprintf('Votre présentation a été postée avec succès sur le forum.<br /><br />Cliquez %sici%s pour voir votre présentation', '<a href="' . $url . '">', '</a>');
	message_die(GENERAL_MESSAGE, $message_fin);
}


//
// Parse the page and print
//
$template->pparse('body');

//
// Page footer
//
include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
?>