<?php
/**---------------------------------------------
*
* Rooty, 2018 <rooty@rooty.me>
*
*
* This software is protected by copyright, please
* read the file COPYRIGHT.
* This program is distributed in the hope that it
* will be useful, but without any warranty; without
* even the implied warranty of merchantability or
* fitness for a particular purpose. Please
* read the file LICENCE.
*
*Ce fichier contient la classe UserModif
*<BR>
* @author Rooty <www.rooty.me|rooty@rooty.me>
* @since 2004/09/28
* @version 0.3
* @link www.rooty.me
* @package object_coordination
* @subpackage UserModif
* @copyright Copyright &copy; 2009-2018, Rooty
*/
if ( !defined('SYSTEM_IN') )
{
 die("Hacking attempt");
}
/**
* Classe UserModif
*
* classe de gestion du compte courant
*
* @package object_coordination
* @subpackage UserModif
*/
Class UserModif extends CoordinationObj
{

/**
* Function permettant d'afficher le formulaire de pré-inscription pour enregistrement dans la bd
* @access public
* @return chaine chaine vide
*/
    function actionModifier()
    {
		global $SERVER_NAME, $SCRIPT_NAME, $CONF_PERSISTANT_SECURE,$CONF_DATA_PATH;

		/* Récupération des messages utilisateurs */
		addMessageFile("user.ini");
		
		mxPageTitle("titlAdminSubscriptions");
	
        $arrayCallCss = array();
        $arrayCallCss[] = "data/css/bootstrap-datepicker/1.9.0/bootstrap-datepicker3.min.css";
        mxCallCss($arrayCallCss);

        $arrayCallJs = array();
        $arrayCallJs[] = "data/javascript/bootstrap-datepicker/1.9.0/bootstrap-datepicker.min.js";
        $arrayCallJs[] = "data/javascript/bootstrap-datepicker/1.9.0/locales/bootstrap-datepicker.fr.min.js";
        mxCallJs($arrayCallJs);
        
        $arrayRawJs[] = "\$(document).ready(function(){ $('.datepicker').datepicker(); });";
        mxRawJs($arrayRawJs);
		
		/* On inclus la template (voir package.template.php) */
		WithBlocPath("UserModif", "actionModifier");

		/* gestion du lib en fonction de la langue courante */
		$currentCode=$GLOBALS["SYSTEM_USER_SESSION"]->langue->getCode();
		$defaultCode=$GLOBALS["SYSTEM_DEFAULT_LANGUAGE_CODE"];
		/* fin récupération des codes par defaut */
		
// 		includeBO("nusoap/nusoap.php");
// 		$trustedApp = 'foo_proxy';
// 		$userEmail = 'listmaster@listes.fab-l3.org';
// 		$soapServer = "https://www.fab-l3.org/sympasoap";
// 		$soapclient = new nusoap_client($soapServer, true);
// 		$soapclient->debug_flag=true;
// 		$md5 = $soapclient->call('login', array($trustedApp, "GalEuhyk87"));
// 		
// 		if (gettype($md5) == "string") {
// // 			echo "<P ALIGN=\"center\"><FONT COLOR=\"#00FF00\">Authentication Succeeded</FONT></P>\n";
// 			$auth = "Auth ok";
// 		}else {
// // 			echo "<P ALIGN=\"center\"><FONT COLOR=\"#ff0000\">Authentication failed</FONT></P>\n";
// 			$auth = "Auth failed";
// 		}
// 		var_dump($auth);
// 		var_dump($md5);

		if ($GLOBALS["SYSTEM_USER_SESSION"]->user->getId()!=$GLOBALS["SYSTEM_DEFAULT_USER_GUEST"])
		{
			$titre = array(getMessage("mr"),getMessage("mrs"), getMessage("miss"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxSelect("user_title", "user_title", $GLOBALS["SYSTEM_USER_SESSION"]->user->getTitle(), $titre, "", "", "", "class=\"form\" title=\"".htmlspecialchars(getMessage("tipSelectTitle"), ENT_QUOTES)."\"");
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_first_name", "text", "user_first_name", $GLOBALS["SYSTEM_USER_SESSION"]->user->getFirstName(), "class=\"form\" required=\"true\" title=\"".htmlspecialchars(getMessage("tipFirstName"), ENT_QUOTES)."\"", getMessage("user_first_name"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_last_name", "text", "user_last_name", $GLOBALS["SYSTEM_USER_SESSION"]->user->getLastName(), "class=\"form\" required=\"true\" title=\"".htmlspecialchars(getMessage("tipLastName"), ENT_QUOTES)."\"", getMessage("user_last_name"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_login", "text", "user_login", $GLOBALS["SYSTEM_USER_SESSION"]->user->getLogin(), "class=\"form\" disabled", getMessage("user_login"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_password", "password", "user_password", $GLOBALS["SYSTEM_USER_SESSION"]->user->getPassword(), "class=\"form\" required=\"true\" title=\"".htmlspecialchars(getMessage("tipPassword"), ENT_QUOTES)."\"", getMessage("user_password"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("re_user_password", "password", "re_user_password", $GLOBALS["SYSTEM_USER_SESSION"]->user->getPassword(), "class=\"form\" required=\"true\" title=\"".htmlspecialchars(getMessage("tipPassword2"), ENT_QUOTES)."\"", getMessage("user_password"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_mail", "text", "user_mail", $GLOBALS["SYSTEM_USER_SESSION"]->user->getMail(), "class=\"form\" required=\"true\" title=\"".htmlspecialchars(getMessage("tipCourriel"), ENT_QUOTES)."\"", getMessage("user_mail"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_birthdate", "text", "user_birthdate", $GLOBALS["SYSTEM_USER_SESSION"]->user->getBirthDate(), "class=\"form\" required=\"true\" title=\"".htmlspecialchars(getMessage("tipBirthDate"), ENT_QUOTES)."\"", getMessage("user_birthdate"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_postal_code", "text", "user_postal_code", $GLOBALS["SYSTEM_USER_SESSION"]->user->getPostalCode(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipPostalCode"), ENT_QUOTES)."\"", getMessage("user_postal_code"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_postal_address", "text", "user_postal_address", $GLOBALS["SYSTEM_USER_SESSION"]->user->getPostalAddress(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipPostalAddress"), ENT_QUOTES)."\"", getMessage("user_postal_address"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_organisation", "text", "user_organisation", $GLOBALS["SYSTEM_USER_SESSION"]->user->getOrganisation(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipOrganisation"), ENT_QUOTES)."\"", getMessage("user_ogarnisation"));
			$tabcom = $GLOBALS["SYSTEM_USER_SESSION"]->user->getComment();
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_comment", "text", "user_comment", (isset($tabcom[0])?$tabcom[0]:""), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipComment"), ENT_QUOTES)."\"", getMessage("user_comment"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_city", "text", "user_city", $GLOBALS["SYSTEM_USER_SESSION"]->user->getCity(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipCity"), ENT_QUOTES)."\"", getMessage("user_city"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_state", "text", "user_state", $GLOBALS["SYSTEM_USER_SESSION"]->user->getState(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipState"), ENT_QUOTES)."\"", getMessage("user_state"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_country", "text", "user_country", $GLOBALS["SYSTEM_USER_SESSION"]->user->getCountry(), "class=\"form\" required=\"true\" title=\"".htmlspecialchars(getMessage("tipCountry"), ENT_QUOTES)."\"", getMessage("user_country"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_tel", "text", "user_tel", $GLOBALS["SYSTEM_USER_SESSION"]->user->getTel(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipTel"), ENT_QUOTES)."\"", getMessage("user_tel"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_fax", "text", "user_fax", $GLOBALS["SYSTEM_USER_SESSION"]->user->getFax(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipFax"), ENT_QUOTES)."\"", getMessage("user_fax"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_website", "text", "user_website", $GLOBALS["SYSTEM_USER_SESSION"]->user->getWebSite(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipWebsite"), ENT_QUOTES)."\"", getMessage("user_website"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_msn", "text", "user_msn", $GLOBALS["SYSTEM_USER_SESSION"]->user->getMsn(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipMsn"), ENT_QUOTES)."\"", getMessage("user_msn"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_jabber", "text", "user_jabber", $GLOBALS["SYSTEM_USER_SESSION"]->user->getJabber(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipJabber"), ENT_QUOTES)."\"", getMessage("user_jabber"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_facebook", "text", "user_facebook", $GLOBALS["SYSTEM_USER_SESSION"]->user->getFacebook(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipFacebook"), ENT_QUOTES)."\"", getMessage("user_facebook"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxFormField("user_mastodon", "text", "user_mastodon", $GLOBALS["SYSTEM_USER_SESSION"]->user->getMastodon(), "class=\"form\" title=\"".htmlspecialchars(getMessage("tipMastodon"), ENT_QUOTES)."\"", getMessage("user_mastodon"));
			$GLOBALS["SYSTEM_TEMPLATE"]->MxText("user_id", $GLOBALS["SYSTEM_USER_SESSION"]->user->getId());
		}

	# Récupération des étiquette du formulaire
	$GLOBALS["SYSTEM_TEMPLATE"]->MxAttribut("urlAction", formatUrl($this->getId(),"UserModif","execModifier"));

	ExitBloc();
	return "";
    }

/**
* Function permettant de controler les doublons, d'inscrire dans la bd et redirection avec message
* @access public
* @return chaine chaine vide
*/
    function execModifier()
    {
		global $SERVER_NAME, $SCRIPT_NAME, $CONF_PERSISTANT_SECURE,$CONF_DATA_PATH;

		/* gestion du lib en fonction de la langue courante */
		$currentCode=$GLOBALS["SYSTEM_USER_SESSION"]->langue->getCode();
		$defaultCode=$GLOBALS["SYSTEM_DEFAULT_LANGUAGE_CODE"];
		/* fin récupération des codes par defaut */

		/*Récupération des messages utilisateurs*/
		addMessageFile("user.ini");
		//On vide la chaine de vérification des champs obligatoires
		$chaine = "";
		
		if($_POST)
		{
			foreach($_POST as $cle=>$val)
			{
				if($val=="" && ($cle !="user_fax" && $cle !="user_country" && $cle !="user_id" && $cle !="user_website" && $cle !="user_msn" && $cle !="user_jabber" && $cle !="user_facebook" && $cle !="user_mastodon" && $cle !="user_organisation" && $cle !="user_comment" && $cle !="user_birthdate"))
				{
					$chaine .= getMessage($cle).", ";
				}
			}
			if($chaine!="")
			{
			$chaine .= getMessage('obligatory');
			}
		}

		//Si certains champs sont manquants pour l'inscription
		if ($chaine != "")
		{
			$GLOBALS["SYSTEM_CONTROL"]->messageSystem = $chaine;
			/* On inclus la template (voir package.template.php) */
			$this->actionModifier();
			return "";
		}

		# Teste si il y a des enregistrements sur la table des comptes
		$result=$GLOBALS['SYSTEM_MANAGER']['USER']->checkValid($_POST["user_login"], $_POST["user_password"]);
		if (($result != 0) && ($result !=$GLOBALS["SYSTEM_USER_SESSION"]->user->getId()) )
		{
			$GLOBALS["SYSTEM_CONTROL"]->messageSystem = "user_exist";
			/* On inclus la template (voir package.template.php) */
			$this->actionModifier();
			systemRedirect(formatUrl($this->getId(),"UserModif","actionModifier","user_exist"));
		}
		else
		{
			if ($_POST['user_password']!=$_POST['re_user_password'])
			{
				systemRedirect(formatUrl($this->getId(),"UserModif","actionModifier","msgWrongPassValidation"));
				return "";
			}

			//On enregistre les modifications de l'objet User ???????????????????????????????????????
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setTitle($_POST['user_title']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setFirstName($_POST['user_first_name']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setLastName($_POST['user_last_name']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setLogin($_POST['user_login']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setPassword($_POST['user_password']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setMail($_POST['user_mail']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setBirthDate($_POST['user_birthdate']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setWebSite($_POST['user_website']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setMsn($_POST['user_msn']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setJabber($_POST['user_jabber']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setFacebook($_POST['user_facebook']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setMastodon($_POST['user_mastodon']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setPostalAddress($_POST['user_postal_address']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setPostalCode($_POST['user_postal_code']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setCity($_POST['user_city']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setState($_POST['user_state']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setCountry($_POST['user_country']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setOrganisation( (($_POST['user_organisation']!="")?$_POST['user_organisation']:"Aucune organisation") );
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setTel($_POST['user_tel']);
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setFax($_POST['user_fax']);
// 			$GLOBALS["SYSTEM_USER_SESSION"]->user->setComment( $_POST['user_comment'] );
			$GLOBALS["SYSTEM_USER_SESSION"]->user->setComment( (($_POST['user_comment']!="")?$_POST['user_comment']:"Aucun commentaire") );
			//On enregistre les paramètres
			$ObjUsersave = $GLOBALS['SYSTEM_MANAGER']['USER']->saveInstance($GLOBALS["SYSTEM_USER_SESSION"]->user);

			# Redirection vers le formulaire
		        systemRedirect(formatUrl($this->getId(),"UserModif","actionModifier","userdatamodified"));
		}
		return "";
    }
}
?>
