<?php 
/*#################################################################*\
|# Licence Number 0DT7-0122-0NW1-0210
|# -------------------------------------------------------------   #|
|# Copyright (c)2010 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	
/**
# ################################################################################
# Project:   PHP Link Directory
#
# **********************************************************************
# Copyright (C) 2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
#
# This software is for use only to those who have purchased a license.
# A license must be purchased for EACH installation of the software.
#
# By using the software you agree to the terms:
#
#    - You may not redistribute, sell or otherwise share this software
#      in whole or in part without the consent of the the ownership
#      of PHP Link Directory. Please contact david@david-duval.com
#      if you need more information.
#
#    - You agree to retain a link back to http://www.phplinkdirectory.com/
#      on all pages of your directory in you purchased the $25 version
#      of the software.
#
# License holders are entitled to upgrades to the 3.4 branch of the software
# as they are made available at http://www.phplinkdirectory.com/
#
# In some cases, license holders may be required to agree to changes
# in the software license before receiving updates to the software.
# **********************************************************************
#
# For questions, help, comments, discussion, etc., please join the
# PHP Link Directory Forum http://www.phplinkdirectory.com/forum/
#
# @link           http://www.phplinkdirectory.com/
# @copyright      2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
# @projectManager David DuVal <david@david-duval.com>
# @package        PHPLinkDirectory
# @version        4.0.0
# ################################################################################
*/

require_once 'init.php';

if (empty ($_REQUEST['submit']))
{
   $sql = "SELECT * FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($_SESSION['phpld']['adminpanel']['id']);
   $row = $db->GetRow($sql);
   $tpl->assign($row);
   SmartyValidate :: connect($tpl);
   SmartyValidate :: register_form('conf_profile', true);
   SmartyValidate :: register_criteria('isValueUnique', 'validate_unique'   , 'conf_profile');
   SmartyValidate :: register_criteria('isNotEqual'   , 'validate_not_equal', 'conf_profile');

   SmartyValidate :: register_validator('v_NAME'       , 'NAME'              , 'notEmpty', false, false, 'trim', 'conf_profile');
   SmartyValidate :: register_validator('v_NAME_LENGTH', 'NAME:'.USER_NAME_MIN_LENGTH.':'.USER_NAME_MAX_LENGTH, 'isLength', false, false, 'trim', 'conf_profile');
   SmartyValidate :: register_validator('v_LANGUAGE'   , 'LANGUAGE'          , 'notEmpty', false, false, 'trim', 'conf_profile');
   SmartyValidate :: register_validator('v_PASSWORD'   , 'PASSWORD'          , 'notEmpty', true , false, 'trim', 'conf_profile');
   SmartyValidate :: register_validator('v_PASSWORD_LENGTH', 'PASSWORD:'.USER_PASSWORD_MIN_LENGTH.':'.USER_PASSWORD_MAX_LENGTH, 'isLength', true, false, 'trim', 'conf_profile');
   SmartyValidate :: register_validator('v_PASSWORDC'  , 'PASSWORD:PASSWORDC', 'isEqual' , true , false, 'trim', 'conf_profile');
   SmartyValidate :: register_validator('v_EMAIL'      , 'EMAIL'             , 'isEmail' , false, false, 'trim', 'conf_profile');
   SmartyValidate :: register_validator('v_EMAIL_U'    , 'EMAIL:user:'.$_SESSION['phpld']['adminpanel']['id'], 'isValueUnique', false, false, 'trim', 'conf_profile');
}
else
{
   SmartyValidate :: connect($tpl);
   $data = get_table_data('user');

   $error = 0;
   $errorMsg = '';

   if (!isset ($data['SUBMIT_NOTIF']))
      $data['SUBMIT_NOTIF'] = 0;

   if (!isset ($data['PAYMENT_NOTIF']))
      $data['PAYMENT_NOTIF'] = 0;

   $data['ID']        = $_SESSION['phpld']['adminpanel']['id'];
   $data['PASSWORDC'] = $_REQUEST['PASSWORDC'];

   if (SmartyValidate :: is_valid($data, 'conf_profile'))
   {
      unset ($data['PASSWORDC']);

      $unhashedPassword = (!empty ($data['PASSWORD']) ? $data['PASSWORD'] : '');

      if (empty ($data['PASSWORD']))
         $data['PASSWORD'] = $db->GetOne("SELECT `PASSWORD` FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($data['ID']));
      else
         $data['PASSWORD'] = encrypt_password($data['PASSWORD']);

      $mode = "UPDATE";
      $where = " `ID` = ".$db->qstr($data['ID']);

      if (!DEMO && $db->AutoExecute($tables['user']['name'], $data, $mode, $where) > 0)
      {
         //Send user settings confirmation email

         //Get email template
         $emailTpl = get_email_template('NTF_USER_DETAILS_TPL');
/*
         $tplSql   = "SELECT `SUBJECT`, `BODY` FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr(NTF_USER_DETAILS_TPL);
         $emailTpl = $db->GetRow($tplSql);
*/

         //if password changed on edit and an email tpl is available
         if (!empty ($unhashedPassword) && is_array ($emailTpl) && !empty ($emailTpl))
         {
            //Get user data ($data is not complete)
            $pdata = $db->GetRow("SELECT * FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($data['ID']));

            //Send unhashed password
            $pdata['PASSWORD'] = $unhashedPassword;

            $mail = get_emailer_admin();

            //Add email subject
            $emailSubject = $emailTpl['SUBJECT'];
            $emailSubject = replace_email_vars($emailSubject, $pdata, 5);
            $mail->Subject = trim ($emailSubject);

            //Add owner email address
            $mail->AddAddress($data['EMAIL'], $pdata['NAME']);

            //Add email body
            $emailBody = $emailTpl['BODY'];
            $emailBody = replace_email_vars($emailBody, $pdata, 5);
            $mail->Body = trim ($emailBody);

            //Send email
            if (!$mail->Send())
            {
               $error++;
               $tpl->assign('error', $error);
               $errorMsg = $mail->ErrorInfo;
               $tpl->assign('errorMsg', $errorMsg);
            }

            //Clear all addresses (and attachments) for next loop
            $mail->ClearAddresses();
            $mail->ClearAttachments();

            //Free memory
            unset ($pdata, $unhashedPassword, $mail, $emailBody, $emailSubject);
         }

         $_SESSION['user_language'] = $data['LANGUAGE'];

         $tpl->assign('posted', true);
         update_link_owner($data['ID']);

         //Free memory
         unset ($emailTpl);
      }

      //Remove unhashed password (if still available)
      unset ($unhashedPassword);

   }
}

$tpl->assign('languages', select_lang('../lang/'));
$tpl->assign($data);

$content = $tpl->fetch(ADMIN_TEMPLATE.'/conf_profile.tpl');
$tpl->assign('content', $content);

//Clean whitespace
$tpl->load_filter('output', 'trimwhitespace');

//Make output
echo $tpl->fetch(ADMIN_TEMPLATE.'/main.tpl');
?>