<?php 
 
/**
 * [UCenter Home] (C) 2007-2008 Comsenz Inc.
 * @Id: do_register.php 2010 0902 
 * @author dason  QQ:623466642

 * @copyright http://dason.blog.51cto.com/ 

 */
 
if(!defined('IN_UCHOME')) {
    exit('Access Denied');
}
 
$op = $_GET['op'] ? trim($_GET['op']) : '';
 
if($_SGLOBAL['supe_uid']) { //如果存在uid则直接进行用户空间
    showmessage('do_success', 'space.php?do=home', 0);
}
 
//没有登录表单
$_SGLOBAL['nologinform'] = 1;
 
//好友邀请
$uid = empty($_GET['uid'])?0:intval($_GET['uid']);
$code = empty($_GET['code'])?'':$_GET['code'];
$app = empty($_GET['app'])?'':intval($_GET['app']); //获取应用的id
$invite = empty($_GET['invite'])?'':$_GET['invite'];
$invitearr = array();

view sourceprint?001.$invitepay = getreward('invitecode', 0); //获取邀请码,所应该得的积分与经验
$pay = $app ? 0 : $invitepay['credit'];
 
if($uid &amp;&amp; $code &amp;&amp; !$pay) { //如果存在uid,code,并且积分为0
    $m_space = getspace($uid); //获取邀请人的空间信息
    if($code == space_key($m_space, $app)) {//验证通过
        $invitearr['uid'] = $uid;
        $invitearr['username'] = $m_space['username']; //将邀请人的uid与username赋值给invitearr数组
    }
    $url_plus = "uid=$uid&amp;app=$app&amp;code=$code";
} elseif($uid &amp;&amp; $invite) {
    include_once(S_ROOT.'./source/function_cp.php');
    $invitearr = invite_get($uid, $invite);
    $url_plus = "uid=$uid&amp;invite=$invite";
}
 
$jumpurl = $app?"userapp.php?id=$app&amp;my_extra=invitedby_bi_{$uid}_{$code}&amp;my_suffix=Lw%3D%3D":'space.php?do=home';
 
if(empty($op)) {
 
    if($_SCONFIG['closeregister']) {//判断系统是否关闭注册
        if($_SCONFIG['closeinvite']) {//如果没有开启好友邀请还不能注册,则提示"非常抱歉,本站目前暂时不开放注册";
            showmessage('not_open_registration');
        } elseif(empty($invitearr)) {//否则显示非常抱歉,本站目前暂时不允许用户直接注册,需要有好友邀请链接才能注册
            showmessage('not_open_registration_invite');
        }
    }
 
    //是否关闭站点
    checkclose();
 
    if(submitcheck('registersubmit')) { //判断是否有post提交注册
 
        //已经注册用户
        if($_SGLOBAL['supe_uid']) {
           showmessage('registered', 'space.php');
        }  //如果该uid存在时,则显示"注册成功",跳转到空间
 
       if($_SCONFIG['seccode_register']) {
            include_once(S_ROOT.'./source/function_cp.php');
           if(!ckseccode($_POST['seccode'])) { //验证注册码
                showmessage('incorrect_code');
            }
        }
 
        if(!@include_once S_ROOT.'./uc_client/client.php') { //引入用户操作入口文件
            showmessage('system_error');
       }
 
        if($_POST['password'] != $_POST['password2']) { //判断两次输入的密码是否相同,一般会使用javascript进行判断
            showmessage('password_inconsistency');
       }
 
       if(!$_POST['password'] || $_POST['password'] != addslashes($_POST['password'])) {
            showmessage('profile_passwd_illegal');
       }   //如果密码未真写或是填写的密码与转义后的密码不相同时,对话框提示:密码空或包含非法字符,请返回重新填写。
 
       $username = trim($_POST['username']);
        $password = $_POST['password'];  //获取用户名与密码
 
        $email = isemail($_POST['email'])?$_POST['email']:'';//验证email的格式
       if(empty($email)) {
           showmessage('email_format_is_wrong'); //如果返回为空则显示:填写的 Email 格式有误
      }
       //检查邮件
        if($_SCONFIG['checkemail']) { //是否可以重复注册email
           if($count = getcount('spacefield', array('email'=>$email))) {
                showmessage('email_has_been_registered');
           }
       }
       //检查IP
       $onlineip = getonlineip();  //获取当前的ip
        if($_SCONFIG['regipdate']) {  //是否有限制一个ip注册的间隔时间
            $query = $_SGLOBAL['db']->query("SELECT dateline FROM ".tname('space')." WHERE regip='$onlineip' ORDER BY dateline DESC LIMIT 1");  //查看该ip的注册时间
           if($value = $_SGLOBAL['db']->fetch_array($query)) {
               if($_SGLOBAL['timestamp'] - $value['dateline'] < $_SCONFIG['regipdate']*3600) {  //如果当前时间小于ip注册时间限制
                   showmessage('regip_has_been_registered', '', 1, array($_SCONFIG['regipdate']));  //则提示同一个IP在 1 小时内只能注册一个账号
                }
            }
       }
 
       $newuid = uc_user_register($username, $password, $email);  //通过用户操作接口文件进行用户的注册,传入用户名,密码,Email
        if($newuid <= 0) {  //如果返回uid小于0
            if($newuid == -1) {  //如果等于-1:用户名不合法
               showmessage('user_name_is_not_legitimate');
           } elseif($newuid == -2) {
                showmessage('include_not_registered_words');
            } elseif($newuid == -3) {
               showmessage('user_name_already_exists');
           } elseif($newuid == -4) {
               showmessage('email_format_is_wrong');
           } elseif($newuid == -5) {
                showmessage('email_not_registered');
           } elseif($newuid == -6) {
                showmessage('email_has_been_registered');
           } else {
                showmessage('register_error');
            }
        } else {  //如果uid>0,则表示注册成功
           $setarr = array(
                'uid' => $newuid,
                'username' => $username,
               'password' => md5("$newuid|$_SGLOBAL[timestamp]")//本地密码随机生成,密码由:uid|当前时间 生成
           );
            //更新本地用户库
            inserttable('member', $setarr, 0, true); //将用户这三个字段信息添加到member表中
 
            //开通空间
           include_once(S_ROOT.'./source/function_space.php');
            $space = space_open($newuid, $username, 0, $email); //开通用户空间
 
            //默认好友
           $flog = $inserts = $fuids = $pokes = array();
           if(!empty($_SCONFIG['defaultfusername'])) { //查看是否有设置默认好友
                $query = $_SGLOBAL['db']->query("SELECT uid,username FROM ".tname('space')." WHERE username IN (".simplode(explode(',', $_SCONFIG['defaultfusername'])).")");  //如果有存在设置默认好友,则查着这些好友的uid,username信息
               while ($value = $_SGLOBAL['db']->fetch_array($query)) {
                    $value = saddslashes($value); //将所得的$value数组进行字符过滤
                    $fuids[] = $value['uid'];
                    $inserts[] = "('$newuid','$value[uid]','$value[username]','1','$_SGLOBAL[timestamp]')";
                    $inserts[] = "('$value[uid]','$newuid','$username','1','$_SGLOBAL[timestamp]')";
                    $pokes[] = "('$newuid','$value[uid]','$value[username]','".addslashes($_SCONFIG['defaultpoke'])."','$_SGLOBAL[timestamp]')";
                    //添加好友变更记录
                   $flog[] = "('$value[uid]','$newuid','add','$_SGLOBAL[timestamp]')";
               }
                if($inserts) {  //"REPLACE INTO" 如果表中不存在这一条记录都插入数据库,如果已经存在,则修改数据
                   $_SGLOBAL['db']->query("REPLACE INTO ".tname('friend')." (uid,fuid,fusername,status,dateline) VALUES ".implode(',', $inserts));  //将信息插入到好友表
                   $_SGLOBAL['db']->query("REPLACE INTO ".tname('poke')." (uid,fromuid,fromusername,note,dateline) VALUES ".implode(',', $pokes)); //好友动态表
                    $_SGLOBAL['db']->query("REPLACE INTO ".tname('friendlog')." (uid,fuid,action,dateline) VALUES ".implode(',', $flog));
 
                   //添加到附加表
                    $friendstr = empty($fuids)?'':implode(',', $fuids);
                   updatetable('space', array('friendnum'=>count($fuids), 'pokenum'=>count($pokes)), array('uid'=>$newuid));  //更新用户空间信息,修改好友数与动态数
                    updatetable('spacefield', array('friend'=>$friendstr, 'feedfriend'=>$friendstr), array('uid'=>$newuid));
 
                    //更新默认用户好友缓存
                   include_once(S_ROOT.'./source/function_cp.php');
                   foreach ($fuids as $fuid) {
                       friend_cache($fuid); //对好友数据进行缓存
                   }
                }
            }
 
           //在线session
            insertsession($setarr); //将该用户信息插入到session表中
 
            //设置cookie
           ssetcookie('auth', authcode("$setarr[password]\t$setarr[uid]", 'ENCODE'), 2592000); //设置cookie名为auth的值,并自动设置cookie保存时间为一个月
            ssetcookie('loginuser', $username, 31536000);  //设置cookie名为loginuser的值,并自动保存一年
            ssetcookie('_refer', '');
 
           //好友邀请
           if($invitearr) {
               include_once(S_ROOT.'./source/function_cp.php');
               invite_update($invitearr['id'], $setarr['uid'], $setarr['username'], $invitearr['uid'], $invitearr['username'], $app);
                //更新邀请人空间的信息,包括邀请人与被邀请人的好友数
                //如果提交的邮箱地址与邀请相符的则直接通过邮箱验证
                if($invitearr['email'] == $email) {
                    updatetable('spacefield', array('emailcheck'=>1), array('uid'=>$newuid));
                }
 
               //统计更新
                include_once(S_ROOT.'./source/function_cp.php');
               if($app) {
                    updatestat('appinvite');
                } else {
                    updatestat('invite'); 
               }
           }
 
            //变更记录
            if($_SCONFIG['my_status']) inserttable('userlog', array('uid'=>$newuid, 'action'=>'add', 'dateline'=>$_SGLOBAL['timestamp']), 0, true);
 
            showmessage('registered', $jumpurl);
       }
 
    }
 
    $register_rule = data_get('registerrule');
 
    include template('do_register');
 
} elseif($op == "checkusername") { //ajax验证用户的信息,包括用户名是否为空,用户名在数据库中是否被注册
 
   $username = trim($_GET['username']);
   if(empty($username)) {
       showmessage('user_name_is_not_legitimate'); 
       //用户名为空
    }
    @include_once (S_ROOT.'./uc_client/client.php');
   $ucresult = uc_user_checkname($username); 
    //通过数据库验证用户名信息
 
    if($ucresult == -1) {//返回-1,表示用户名不合法
        showmessage('user_name_is_not_legitimate');
    } elseif($ucresult == -2) {//返回-2,表示用户名包含不允许注册的词语
        showmessage('include_not_registered_words');
    } elseif($ucresult == -3) { //返回-3,表示用户名已经存在
        showmessage('user_name_already_exists');
    } else {//否则返回用户可以使用
       showmessage('succeed');
   }
} elseif($op == "checkseccode") {//判断验证码
 
   include_once(S_ROOT.'./source/function_cp.php');
    if(ckseccode(trim($_GET['seccode']))) {
        showmessage('succeed');
    } else {
        showmessage('incorrect_code');
    }
}
 
?>