<?php
require ('authentication_member.php');
?>
<?php

/* 注册提交 */
/* 162100源码 - 162100.com */

/*
if($_SERVER['HTTP_VIA']!='')
{
  err('本站禁止使用代理服务器注册！');
}
*/

require ('inc/function/cutstr.php');
require ('inc/function/filter.php');

$_POST = array_map('tirm_all', $_POST);
if (!$_GET['location'] = $_GET['location']) {
  $_GET['location'] = $_POST['location'];
}

//$loc = preg_replace('/^'.preg_quote($web['path'], '/').'/i', '', $_GET['location']);
//$loc = file_exists(preg_replace('/(\?|#).*$/', '', $loc)) ? $loc : 'index.php';
$loc = $_GET['location'] ? $_GET['location'] : './';
$date = gmdate("Y-m-d H:i:s", time() + (floatval($web["time_pos"]) * 3600));
$cookie_time = time() + floatval($web['time_pos']) * 3600;
//$GMT = gmdate('D, d M Y H:i:s T', $cookie_time);



if ($_POST['act'] == 'reg') {

  if ($web['stop_reg'] == 1) {
    err('当前系统设置为：禁止新用户注册。');
  } elseif ($web['stop_reg'] == 2) {
    $check1 = ',`check_reg`';
    $check2 = ',"1"';
    $check3 = '（此次注册需要审批，请耐心等待）。';
  } else {
    $check3 = '请保持持续热情支持['.$web['sitename'].']，并欢迎积极发表、反馈问题。';
  }

  if (!empty($session[0])) {
    err('您已经以 用户名：'.$session[0].' 登陆过了！要想更换用户名注册，请先<a'.$target.' href="member.php?post=login&act=logout&location='.urlencode($loc).'">退出</a>。');
  }

  if (empty($_POST['username'])) {
    err('提交被拒绝！用户名不能空！');
  }
  if (strlen($_POST['username']) < 3 || strlen($_POST['username']) > 45) {
    err('提交被拒绝！用户名长度范围为3-45字符！');
  }
  if (preg_match('/admin|manager|管理员|版主|斑竹|访客|系统欢迎信|162100|furuijinzhao|操|temp|info|mess|mail|请输入用户名/i', $_POST['username'], $matches)) {
    err('提交被拒绝！用户名中含'.$matches[0].'，未获通过。');
  }

  $_POST['username'] = str_replace('.', '&#46;', $_POST['username']);

  if (empty($_POST['password']) || !preg_match('/^[^\s\\\]{3,30}$/', $_POST['password'])) {
    err('提交被拒绝！密码——长度请控制在3-30个字符之内。不能有空格和 \ 。');
  }
  if ($_POST['password_again'] == '') {
    err('密码重输不能留空！');
  }
  if ($_POST['password'] != $_POST['password_again']) {
    err('两次密码输得不一样！');
  }
  fil();
  
  if (!isset($sql['db_err'])) {
    db_conn();
  }
  if ($sql['db_err'] != '') {
    err($sql['db_err']);
  }
  
  if (@mysql_num_rows(@mysql_query('SELECT id FROM `'.$sql['pref'].''.$sql['data']['承载成员档案的表名'].'` WHERE username="'.$_POST['username'].'" OR email="'.$_POST['username'].'"', $db)) > 0) {
    err('此用户名已被注册！');
  }
  if (@mysql_num_rows(@mysql_query('SELECT id FROM `'.$sql['pref'].''.$sql['data']['承载成员档案的表名'].'` WHERE username="'.$_POST['email'].'" OR email="'.$_POST['email'].'"', $db)) > 0) {
    err('此邮箱已有人使用！');
  }

  /*
  发送邮件-------------------
  */
  if ($web['mail_send'] == 1) {
    require_once ('inc/function/mail_class.php');
    $subject = "欢迎注册[".$web["sitename"]."]";
    $subject = "=?UTF-8?B?".base64_encode($subject)."?="; //此行解决utf-8编码邮件标题乱码
    $body = '　　欢迎加入['.$web["sitename"].']！<br />　　'.$check3.'<br />　　我们的站址为：<a href="'.$web['path'].'" target="_blank">'.$web['path'].'</a>，欢迎光临。<br /><br /><br /><br /><font color=#999999>如果不是您本人使用此邮箱进行的注册，您可至<a href="'.$web['path'].'" target="_blank">'.$web['path'].'</a>与管理员取得联系，我们将对冒用您邮箱的用户进行删除。</font>';
    $to = $_POST['email']; //收件人 
    $send = $smtp -> sendmail($to, $web['sender'], $subject, $body, $web['mailtype']);
    if ($send != 1) { 
      err('系统尝试连接您的邮箱['.$to.']并发送失败！原因：可能是您所填写的邮箱无效。请仔细填写并检查，或返回重试。如有疑问可<a href="list.php?area_id=96" target="_blank">站内留言</a>。');
    }
  }
  $messline = "".preg_replace("/[^\d]+/", "", $date)."|".$web['manager']."|欢迎加入".$web["sitename"]."！|欢迎加入！请保持持续热情支持".$web["sitename"]."，并欢迎积极发表、反馈问题。||| \n";
  $session_key = $date.'|'.s_rand(16);

  @mysql_query('ALTER TABLE `'.$sql['pref'].''.$sql['data']['承载成员档案的表名'].'` ADD COLUMN `session_key` VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT ""');

  @mysql_query('INSERT INTO `'.$sql['pref'].''.$sql['data']['承载成员档案的表名'].'` (`username`,`password`,`email`,`password_question`,`password_answer`,`regdate`,`thisdate`,`company_description`,`mess`,`mess_num`,`session_key`'.$check1.') values ("'.$_POST['username'].'","'.md162100($_POST['password']).'","'.$_POST['email'].'","'.$_POST['password_question'].'","'.$_POST['password_answer'].'","'.$date.'","'.$date.'","","'.addslashes($messline).'","1/1","'.$session_key.'"'.$check2.')');
  if ($web['regadd'] > 0) {
    @mysql_query('INSERT INTO `'.$sql['pref'].''.$sql['data']['承载财务数据的表名'].'` (`username`,`type`,`description`,`money`,`date`) VALUES ("'.$_POST['username'].'","0","用户注册","'.$web['regadd'].'","'.$date.'")', $db);
  }

  if (@mysql_affected_rows() > 0) {
    if ($web['stop_reg'] != 2) {
      $you = array(
        'name'  => $_POST['username'],
        'key'   => $session_key,
        'point' => $web['loginadd'],
        'power' => '',
      );

      setcookie('usercookie', ''.@implode('|', $you).'|'.$cookie_time.'||'.$web['path'], $cookie_time, '/', '.'.$web['root']);
      setcookie('usercookie', ''.@implode('|', $you).'|'.$cookie_time.'||'.$web['path'], $cookie_time, '/');
    }
    $yougourl = '<table width="100%" border="0" cellspacing="10" cellpadding="0" class="wherego"><tr>';
    $yougourl .= '<td class="whereyou">您可以选择去向：</td><td align="left"><ul><li><a'.$target.' href="./">首页</a></li><li><a'.$target.' href="member.php">用户中心</a></li><li><a'.$target.' href="write.php">发布文章</a></li><li><a'.$target.' href="'.$loc.'">'.$loc.'</a></li></ul></td>';
    $yougourl .= '</tr>
</table>';
    if ($_POST['m']==1) {
      err('
<script>
  if (parent.$("submit_safe_in")!=null) parent.$("submit_safe_in").innerHTML="注册成功！";
  parent.document.regform.reset();
  parent.location.href="./";
</script>注册成功！欢迎您 '.$you['name'].'。'.$check3.$yougourl.'', 'ok');

    }

    alert('注册成功！欢迎您 '.$you['name'].'。'.$check3.$yougourl.'', $loc);
  } else {
    err('注册不成功！['.mysql_errno().':'.mysql_error().']');
  }
  @mysql_close();


//修改注册信息
} elseif ($_POST['act'] == 'regfilemodify') {

  if (POWER == 0) {
    err('系统检测你未登陆，没有权限。');
  }

  if (!isset($sql['db_err'])) {
    db_conn();
  }
  if ($sql['db_err'] != '') {
    err($sql['db_err']);
  }

  if ($_POST['password_new'] != '') {
    if ($_POST['password'] == '') {
      err('请输入原密码！');
	}
    $result = @mysql_query('SELECT password FROM `'.$sql['pref'].''.$sql['data']['承载成员档案的表名'].'` WHERE username="'.$session[0].'" LIMIT 1', $db);
	if ($row = @mysql_fetch_assoc($result)) {
      if (md162100($_POST['password']) != $row['password']) {
        err('原密码输入错误！与档案不符。');
	  }
      unset($row);
	} else {
	  $err .= '<br />密码修改失败（数据库读取失败）';
	}
	@mysql_free_result($result);
    if (!preg_match('/^[^\s\\\]{3,30}$/', $_POST['password_new'])) {
      err('提交被拒绝！密码——长度请控制在3-30个字符之内。不能有空格和 \ 。');
    }
    if ($_POST['password_again'] != $_POST['password_new']) {
      err('提交被拒绝！确认密码，请确保两次密码都输入且一样。');
	}
    $pppp = 'password="'.md162100($_POST['password_new']).'",';
  }
  fil();

  if (@mysql_num_rows(@mysql_query('SELECT id FROM `'.$sql['pref'].''.$sql['data']['承载成员档案的表名'].'` WHERE email="'.$_POST['email'].'" AND username<>"'.$session[0].'"', $db)) > 0) {
    err('此邮箱已有人使用！');
  }

  if ($_POST['handtel'] != '' && !preg_match('/^\d{11}$/', $_POST['handtel'])) {
    err('移动电话请添11位数字！');
  }
  if ($_POST['birthday'] != '' && !preg_match('/^\d{4}\/\d{1,2}\/\d{1,2}$/', $_POST['birthday'])) {
    err('出生日期请添“0000/00/00”格式！');
  }
  if ($_POST['hometel'] != '' && !preg_match('/^\d{3,5}\-\d{5,8}(\-\d{1,5})?$/', $_POST['hometel'])) {
    err('办公电话请添“0000-00000000-0000 区号-数字（-分机可选）”格式。');
  }
  if ($_POST['zip'] != '' && !preg_match('/^\d{6}$/', $_POST['zip'])) {
    err('邮编请添6位数字！');
  }
  if ($_POST['qq'] != '' && !preg_match('/^\d{3,16}$/', $_POST['qq'])) {
    err('QQ请添16位以内数字！');
  }
  $_POST['realname'] = cutstr($_POST['realname'], 255);
  $_POST['company'] = cutstr($_POST['company'], 255);
  $_POST['address'] = cutstr($_POST['address'], 255);
  $_POST['sign'] = cutstr($_POST['sign'], 255);
  $_POST['company_description'] = cutstr($_POST['company_description'], 5000);
  if (!empty($_POST['sex'])) { 
    $_POST['sex'] = $_POST['sex'] == '先生' ? '先生' :'女士';
  }

  if ($web['mail_send'] == 1) {
    require_once ('inc/function/mail_class.php');
    $subject = "个人信息修改成功[".$web["sitename"]."]";
    $subject = "=?UTF-8?B?".base64_encode($subject)."?="; //此行解决utf-8编码邮件标题乱码
    $body = '　　您已成功修改了个人信息['.$web["sitename"].']！<br />　　请保持持续热情支持['.$web['sitename'].']，并欢迎积极发表、反馈问题。<br />　　我们的站址为：<a href="'.$web['path'].'" target="_blank">'.$web['path'].'</a>，欢迎光临。<br /><br /><br /><br /><font color=#999999>如果不是您本人使用此邮箱进行的注册，您可至<a href="'.$web['path'].'" target="_blank">'.$web['path'].'</a>与管理员取得联系，我们将对冒用您邮箱的用户进行删除。</font>';
    $to = $_POST['email']; //收件人
    $send = $smtp -> sendmail($to, $web['sender'], $subject, $body, $web['mailtype']);
    if ($send != 1) {
      err('系统尝试连接您的邮箱['.$to.']并发送失败！原因：可能是您所填写的邮箱无效。请仔细填写并检查，或返回重试。如有疑问可<a href="list.php?area_id=96" target="_blank">站内留言</a>。');
    }
  }
  @mysql_query('UPDATE `'.$sql['pref'].''.$sql['data']['承载成员档案的表名'].'` SET '.$pppp.'
  email="'.$_POST['email'].'",
  password_question="'.$_POST['password_question'].'",
  password_answer="'.$_POST['password_answer'].'",
  realname="'.$_POST['realname'].'",
  sex="'.$_POST['sex'].'",
  birthday="'.$_POST['birthday'].'",
  handtel="'.$_POST['handtel'].'",
  hometel="'.$_POST['hometel'].'",
  company="'.$_POST['company'].'",
  address="'.$_POST['address'].'",
  zip="'.$_POST['zip'].'",
  qq="'.$_POST['qq'].'",
  sign="'.$_POST['sign'].'",
  company_description="'.$_POST['company_description'].'"
  WHERE username="'.$session[0].'"', $db);


  if (@mysql_affected_rows() > 0) {
    if (!empty($_POST['company'])) {
      require ('inc/function/write_file.php');
      require ('inc/function/company_update.php');
    }
    if ($_POST['m']==1) {
      err('
<script>
  if (parent.$("submit_safe_in")!=null) parent.$("submit_safe_in").innerHTML="修改成功！";
  //parent.document.regform.reset();
</script>修改成功！'.$err.'', 'ok');

    }



    alert('修改成功！'.$err.'', 'member.php?get=file');
  } else {
    err('修改不成功！可能你未做改动['.mysql_errno().':'.mysql_error().']'.$err.'。');
  }
  @mysql_close();




} else {
  err('参数出错！');
}




function fil() {
  if (empty($_POST['password_question']) || !is_numeric($_POST['password_question'])) {
    err('密码问题不能留空！');
  }
  $_POST['password_answer'] = cutstr(filter1($_POST['password_answer']), 255);
  if (empty($_POST['password_answer'])) {
    err('密码答案不能留空！');
  }
  if (empty($_POST['email']) || !preg_match('/^[\w\.\-]+@[\w\-]+\.[\w\.]+$/', $_POST['email']) || strlen($_POST['email']) > 255) {
    err('email必填！格式：xxx@xxx.xxx(.xx) 。');
  }
  if (empty($_COOKIE['regimcode'])) {
    err('不能没有验证码！请检测你的浏览器，确保cookie未禁用。<span class="grayword">（如看不到验证码，请返回刷新页面）</span>');
  }
  if (empty($_POST['imcode']) || !is_numeric($_POST['imcode']))
    err('验证码非数字！<span class="grayword">（如看不到验证码，请返回刷新页面）</span>');
  if ($_POST['imcode'] != $_COOKIE['regimcode'])
    err('验证码不符！<span class="grayword">（如看不到验证码，请返回刷新页面）</span>');
}

function s_rand($l) {
  $rand = '';
  $c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ-abcdefghijklmnopqrstuvwxyz_0123456789';
  $n = strlen($c) - 1;
  for ($i = 0; $i < $l; $i++) {
    $rand .= $c[mt_rand(0, $n)];
  }
  return $rand;
}

function tirm_all($str) {
  return filter1(strtolower($str), 0);
}

function md162100($str) {
  return substr(sha1(md5($str)), 4, 32);
}



?>