<?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']) && !empty ($_SERVER['HTTP_REFERER']))
   $_SESSION['return'] = $_SERVER['HTTP_REFERER'];

$tpl->assign('ENABLE_REWRITE', ENABLE_REWRITE);

if ($_REQUEST['action'])
{
   list ($action, $id) = split(':', $_REQUEST['action']);

   $action = strtoupper (trim ($action));
   $id     = ($id < 0  ? 0 : intval ($id));

   $tpl->assign('action', strtoupper ($action));
}

if($_SESSION['phpld']['adminpanel']['is_admin'])
   $tpl->assign('admin_user', array (0 => ('Regular User'), 1 => _L('Administrator'), 2 => _L('Editor'), 3 => _L('Super Editor')));
else
   $tpl->assign('admin_user', array (0 => ('Regular User'), 2 => _L('Editor')));

$tpl->assign('yes_no', array (0 => _L('No'), 1 => _L('Yes')));

switch($action)
{
   case 'C' :
      if (isset ($_SESSION['return']))
         http_custom_redirect($_SESSION['return']);
      break;
   case 'A' :
      if ($db->Execute("UPDATE `{$tables['user']['name']}` SET `ACTIVE` = 1 WHERE `ID` = ".$db->qstr($id)))
      {
         if(isset($_SESSION['return']))
            http_custom_redirect($_SESSION['return']);
      }
      else
         $tpl->assign('sql_error', $db->ErrorMsg());
      break;
   case 'D' :
      if ($db->Execute("DELETE FROM `{$tables['user']['name']}` WHERE `ID`= ".$db->qstr($id)))
      {
         if ($db->Execute("DELETE FROM `{$tables['user_permission']['name']}` WHERE `USER_ID` = ".$db->qstr($id)))
         {
            if (isset ($_SESSION['return']))
               http_custom_redirect($_SESSION['return']);
         }
         else
            $tpl->assign('sql_error', $db->ErrorMsg());
      }
      else
         $tpl->assign('sql_error', $db->ErrorMsg());
      break;
   case 'E' :
      if (empty ($_POST['submit']))
         $data = $db->GetRow("SELECT * FROM `{$tables['user']['name']}` WHERE `ID` = ".$db->qstr($id));
   case 'N' :
   default :
      if(empty($_POST['submit']))
      {
         if ($action == 'N')
            $data = array ();

         SmartyValidate :: connect($tpl);
         SmartyValidate :: register_form('conf_users_edit', true);
         SmartyValidate :: register_criteria('isValueUnique', 'validate_unique'   , 'conf_users_edit');
         SmartyValidate :: register_criteria('isNotEqual'   , 'validate_not_equal', 'conf_users_edit');

         SmartyValidate :: register_validator('v_LOGIN', 'LOGIN', 'notEmpty', false, false, 'trim', 'conf_users_edit');
         SmartyValidate :: register_validator('v_LOGIN_LENGTH', 'LOGIN:'.USER_LOGIN_MIN_LENGTH.':'.USER_LOGIN_MAX_LENGTH, 'isLength', false, false, 'trim', 'conf_users_edit');
         SmartyValidate :: register_validator('v_LOGIN_U', 'LOGIN:user:'.$id, 'isValueUnique', false, false, 'trim', 'conf_users_edit');

         SmartyValidate :: register_validator('v_NAME'       , 'NAME'              , 'notEmpty', false, false, 'trim', 'conf_users_edit');
         SmartyValidate :: register_validator('v_NAME_LENGTH', 'NAME:'.USER_NAME_MIN_LENGTH.':'.USER_NAME_MAX_LENGTH, 'isLength', false, false, 'trim', 'conf_users_edit');

         SmartyValidate :: register_validator('v_LANGUAGE'   , 'LANGUAGE'          , 'notEmpty', false, false, 'trim', 'conf_users_edit');

         SmartyValidate :: register_validator('v_PASSWORD'   , 'PASSWORD'          , 'notEmpty', ($action == 'E' ? true : false), false, 'trim', 'conf_users_edit');
         SmartyValidate :: register_validator('v_PASSWORD_LENGTH', 'PASSWORD:'.USER_PASSWORD_MIN_LENGTH.':'.USER_PASSWORD_MAX_LENGTH, 'isLength', ($action == 'E' ? true : false), false, 'trim', 'conf_users_edit');

         SmartyValidate :: register_validator('v_PASSWORDC' , 'PASSWORD:PASSWORDC', 'isEqual'      , true , false, 'trim', 'conf_users_edit');

         SmartyValidate :: register_validator('v_EMAIL', 'EMAIL', 'isEmail', false, false, 'trim', 'conf_users_edit');
         SmartyValidate :: register_validator('v_EMAIL_U', 'EMAIL:user:'.$id, 'isValueUnique', false, false, 'trim', 'conf_users_edit');
      }
      else
      {
         SmartyValidate :: connect($tpl);
         $data = get_table_data('user');

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

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

         $data['ACTIVE'] = 1;
         if (isset($_REQUEST['cemail'])){
			$data['EMAIL_CONFIRMED'] = 1;
			}
         $data['PASSWORDC'] = $_REQUEST['PASSWORDC'];
         if (SmartyValidate :: is_valid($data, 'conf_users_edit'))
         {
            unset ($data['PASSWORDC']);
            if (empty ($id))
               $id = $db->GenID($tables['user']['name'].'_SEQ');

            $data['ID'] = $id;

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

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

            if ($db->Replace($tables['user']['name'], $data, 'ID', true) > 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);
               }

               update_link_owner($id);
               $tpl->assign('posted', true);

               //Free memory
               unset ($emailTpl);
               
               if ($data['LEVEL'] == 2)
                  http_custom_redirect('conf_user_permissions.php?action=N:0&u='.$id);
               else
               {
                  if ($action == 'N')
                     $data = array ();
                  else
                     if (isset ($_SESSION['return']))
                        http_custom_redirect($_SESSION['return']);
               }
            }
            else
            {
               $tpl->assign('sql_error', $db->ErrorMsg());
            }

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

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

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

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

//Compress output for faster loading
if (COMPRESS_OUTPUT == 1)
   $tpl->load_filter('output', 'CompressOutput');

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