<?php
// +---------------------------------------------+
// |     Copyright  2008-2018 weenCompany        |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

if(!defined('IN_WEENCOMPANY'))
{
  die("File not found!");
}



function m11_UpdateUser()
{
  global $DB;

  $language = GetLanguage(11);

  $userfullname        = trim($_POST['userfullname']);
  $usercompany        = trim($_POST['usercompany']);
  $useradd        = trim($_POST['useradd']);
  $userpostcode        = trim($_POST['userpostcode']);
  $usertel        = trim($_POST['usertel']);
  $userfax        = trim($_POST['userfax']);
  $useronline        = trim($_POST['useronline']);
  $userwebsite        = trim($_POST['userwebsite']);

  if(strlen($_POST['newpassword']) > 0)
  {
    if(!isPass($_POST['newpassword']))
    {
      $error[] = $language['enter_valid_password'];
    }elseif($_POST['newpassword'] != $_POST['newpasswordconfirm'])
    {
      $error[] = $language['password_unmatched'];
    }
  }

  if(strlen($_POST['email']) > 0)
  {
    if(!isEmail($_POST['email']))
    {
      $error[] = $language['enter_valid_email'];
    }elseif($DB->query_first("SELECT email FROM " . TABLE_PREFIX . "users WHERE email = '$_POST[email]' and  userid != '".$_POST['m11_userid']."'"))
    {
      $error[] = $language['email_already_exists'];
    }
  }

  if(!$error)
  {
    // change email?
    if(strlen($_POST['email']) > 0)
    {
      $DB->query("UPDATE " . TABLE_PREFIX . "users SET email = '".$_POST['email']."' WHERE userid = '".$_POST['m11_userid']."'");
    }

    // change password?
    if(strlen($_POST['newpassword']) > 0)
    {
      $DB->query("UPDATE " . TABLE_PREFIX . "users SET password = '".md5($_POST['newpassword'])."' WHERE userid = '".$_POST['m11_userid']."'");
    }
	$DB->query("UPDATE " . TABLE_PREFIX . "users SET userfullname       = '$userfullname',
                                                     usercompany       = '$usercompany',
                                                     useradd       = '$useradd',
                                                     userpostcode       = '$userpostcode',
                                                     usertel       = '$usertel',
                                                     userfax       = '$userfax',
                                                     useronline       = '$useronline',
                                                     userwebsite       = '$userwebsite'												 
                                               WHERE userid      = '".$_POST['m11_userid']."'");

    echo $language['profile_updated'];
  }
  else
  {
    m11_UserCp($error);
  }
}


function m11_UserCp($error)
{
  global $DB, $categoryid, $userinfo, $inputsize;

  $language = GetLanguage(11);

  $getsettings = $DB->query("SELECT title, value FROM " . TABLE_PREFIX . "modulesettings WHERE moduleid = '12'");
  while($setting = $DB->fetch_array($getsettings))
  {
			$settings[$setting['title']] = $setting['value'];
  }

  $maxusername = $settings['用戶名長度'];
  $maxpassword = $settings['密碼長度'];

  if($error)
  {
    foreach($error as $key => $value)
    {
      echo $value.'<br />';
    }
  }

  $user = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "users WHERE username = '".addslashes($userinfo['username'])."'");

  echo '<form name="form" method="post" action="' . RewriteLink('index.php?categoryid=' . $categoryid) . '">

        <input type="hidden" name="m11_userid" value="'.$user['userid'].'" />

        <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="100">&nbsp;</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><b>' . $user['username'] . '</b></td>
        </tr>
        <tr>
          <td width="100">' . $language['new_password'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="password" name="newpassword" maxlength="'.$maxpassword.'" size="'.$inputsize.'" /></td>
        </tr>
        <tr>
          <td nowrap="nowrap">' . $language['confirm_password'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="password" name="newpasswordconfirm" maxlength="'.$maxpassword.'" size="'.$inputsize.'" /></td>
        </tr>
        <tr>
          <td width="100">' . $language['new_email'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="text" name="email" maxlength="58" size="'.$inputsize.'"  value="'.$user['email'].'"/></td>
        </tr>';
		
	if($settings['顯示姓名']){
	echo '
        <tr>
          <td width="100">' . $language['user_fullname'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="text" name="userfullname" maxlength="58" size="'.$inputsize.'"  value="'.$user['userfullname'].'"/></td>
        </tr>';
	}
	if($settings['顯示單位']){
	echo '
        <tr>
          <td width="100">' . $language['user_company'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="text" name="usercompany" maxlength="58" size="'.$inputsize.'"  value="'.$user['usercompany'].'"/></td>
        </tr>';
	}
	if($settings['顯示地址']){
	echo '
        <tr>
          <td width="100">' . $language['user_add'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="text" name="useradd" maxlength="58" size="'.$inputsize.'"  value="'.$user['useradd'].'"/></td>
        </tr>';
	}
	if($settings['顯示郵編']){
	echo '
        <tr>
          <td width="100">' . $language['user_postcode'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="text" name="userpostcode" maxlength="58" size="'.$inputsize.'"  value="'.$user['userpostcode'].'"/></td>
        </tr>';
	}
	if($settings['顯示電話']){
	echo '
        <tr>
          <td width="100">' . $language['user_tel'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="text" name="usertel" maxlength="58" size="'.$inputsize.'"  value="'.$user['usertel'].'"/></td>
        </tr>';
	}
	if($settings['顯示傳真']){
	echo '
        <tr>
          <td width="100">' . $language['user_fax'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="text" name="userfax" maxlength="58" size="'.$inputsize.'"  value="'.$user['userfax'].'"/></td>
        </tr>';
	}
	if($settings['顯示在線聯繫']){
	echo '
        <tr>
          <td width="100">' . $language['user_online'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="text" name="useronline" maxlength="58" size="'.$inputsize.'"  value="'.$user['useronline'].'"/></td>
        </tr>';
	}
	if($settings['顯示網址']){
	echo '
        <tr>
          <td width="100">' . $language['user_website'] . '</td>
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="text" name="userwebsite" maxlength="58" size="'.$inputsize.'"  value="'.$user['userwebsite'].'"/></td>
        </tr>';
	}
		
		echo '
        <tr>
          <td width="100">
          <td style="padding-left: 10px; padding-bottom: 10px;"><input type="submit" name="m11_updateuser" value="' . strip_tags($language['update_profile']) . '" /></td>
        </tr>
        </table></form>';
}


$m11_language = GetLanguage(11);

$m11_usersystem = $DB->query_first("SELECT name FROM " . TABLE_PREFIX . "usersystems WHERE activated = '1'");

if($userinfo['loggedin'])
{
  if($m11_usersystem['name'] == 'weenCompany')
  {
    if(isset($_POST['m11_updateuser']))
    {
    	m11_UpdateUser();
    }
    else
    {
    	m11_UserCp(NULL);
    }
  }
  else
  {
    // $cppath already has been through RewriteLink in the integration file (or $weenurl added)
    $cppath = ForumLink(2);
    echo '<a href="' . $cppath . '">' . $m11_language['visit_cp'] . '</a>';
  }
}
else
{
  echo $sdlanguage['no_view_access'];
}

unset($m11_language, $m11_usersystem);


?>
