<?php

//----------------------------------------------------------------------

//  Stat Therefor 2002 alpha 0110  (所以统计系统 - 正式版 Build 0210 )

//----------------------------------------------------------------------

// Copyright(C) Dai Zhikang, Crossday Studio, 2002. All rights reserved

//

//          所以网络空间  www.crossday.com  www.therefor.net

//----------------------------------------------------------------------

//     免费软件  ALWAYS FREEWARE!  *技术支持 <webmaster@therefor.net>

//----------------------------------------------------------------------

//   欢 迎 您 指 出 存 在 的 b u g 和 问 题 ,  提 出 您 的 宝 贵 意 见

//----------------------------------------------------------------------

// 软件为作者利用数天的休息时间辛勤劳作而写成,请您保护我所保留的很少一点

// 权利, 您在使用本软件时请勿擅自修改页面上作者署名及版权信息,谢谢!谢谢!

//----------------------------------------------------------------------



function login() {



?>

            <table width="460" border="0" cellspacing="0" cellpadding="0">

              <tr>

                <td background="../images/graph_border1.gif" height="25" valign="top"> 

                  <b style="font-family: 宋体;line-height: 21px">&nbsp;&nbsp;用户管理登录</b> 

                </td>

              </tr>

              <tr>

                <td background="../images/graph_border2.gif">

                  <table width="460" border="0" cellspacing="0" cellpadding="0">

                    <tr> 

                      <td width="11">&nbsp;</td>

                      <td align="center"> 

                        <form name="login" method="post" action="useradmin.php">

                        <table width="50%" border="0" cellspacing="0" cellpadding="0">

                          <tr>

                            <td height="15" colspan="2"></td>

                          </tr>

                          <tr>

                            <td height="20" align="center" colspan="2">请将浏览器cookie打开才能正确登录</td>

                          </tr>

                          <tr>

                            <td height="10" colspan="2"></td>

                          </tr>

                          <tr> 

                            <td height="20" align="center">用户名</td>

                            <td height="20" align="right"><input type="text" name="user" class="input" onMouseOver ="this.style.backgroundColor='#F0F1FF'" onMouseOut ="this.style.backgroundColor=''"></td>

                          </tr>

                          <tr> 

                            <td height="20" align="center">密 码</td>

                            <td height="20" align="right"><input type="password" name="password" class="input" onMouseOver ="this.style.backgroundColor='#F0F1FF'" onMouseOut ="this.style.backgroundColor=''"></td>

                          </tr>

                          <tr>

                            <td height="15"></td>

                          </tr>

                          <tr> 

                            <td height="20" align="center" colspan="2">

                              <input type="submit" name="Submit" value="登  录" class="button" onMouseOver ="this.style.backgroundColor='#EEF7FF'" onMouseOut ="this.style.backgroundColor=''">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" name="Reset" value="清  除" class="button" onMouseOver ="this.style.backgroundColor='#EEF7FF'" onMouseOut ="this.style.backgroundColor=''">

                            </td>

                          </tr>

                        </table>

                        </form>

                      </td>

                      <td width="5"></td>

                      <td width="11" background="../images/graph_border5.gif">&nbsp;</td>

                      <td width="9"></td>

                    </tr>

                  </table>

                </td>

              </tr>

              <tr>

                <td background="../images/graph_border4.gif" height="10"></td>

              </tr>

            </table>

<?



}



function content1() {



global $user;



?>

<html>



<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>用户管理</title>

<base target="_self">

<link rel="stylesheet" type="text/css" href="../images/admin.css">

</head>



<body topmargin="0" leftmargin="0" bgcolor="#CDB800" background="../images/bg.gif">

<script language="JavaScript" src="../images/admin.js"></script>

<table border="0" cellpadding="0" cellspacing="0" width="100%">

  <tr>

    <td height="280" width="115">

      <div id="floater" style="position:absolute; width:115px; height:280px; z-index:10; left: 0px; top: 0px">

        <table width="115" height="280" border="0" cellspacing="0" cellpadding="0" background="../images/float_admin.gif">

          <tr>

            <td width="29" height="95"></td>

            <td width="56" height="95"></td>

            <td width="30" height="95"></td>

          </tr>

          <tr>

            <td width="29" height="105"></td>

            <td width="56" height="105" align="center" valign="center">

              <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">

                <tr>

                  <td align="center" height="18"><a href="useradmin.php?user=<?echo$user;?>&action=getcode" onmouseout=dL(this) onmouseover=hL(this) style="text-decoration: none">取得代码</a></td>

                </tr>

                <tr>

                  <td></td>

                </tr>

                <tr>

                  <td align="center" height="18"><a href="useradmin.php?user=<?echo$user;?>&action=modify" onmouseout=dL(this) onmouseover=hL(this) style="text-decoration: none">修改资料</a></td>

                </tr>

                <tr>

                  <td></td>

                </tr>

                <tr>

                  <td align="center" height="18"><a href="useradmin.php?user=<?echo$user;?>&action=reset" onmouseout=dL(this) onmouseover=hL(this) style="text-decoration: none">数据重置</a></td>

                </tr>

                <tr>

                  <td></td>

                </tr>

                <tr>

                  <td align="center" height="18"><a href="useradmin.php?user=<?echo$user;?>&action=suicide" onmouseout=dL(this) onmouseover=hL(this) style="text-decoration: none">账号自杀</a></td>

                </tr>

                <tr>

                  <td></td>

                </tr>

                <tr>

                  <td align="center" height="18"><a href="useradmin.php?user=<?echo$user;?>&action=logout" onmouseout=dL(this) onmouseover=hL(this) style="text-decoration: none">退出登录</a></td>

                </tr>

              </table>

            </td>

            <td width="30" height="105"></td>

          </tr>

          <tr>

            <td width="29" height="80"></td>

            <td width="56" height="80"></td>

            <td width="30" height="80"></td>

          </tr>

        </table>

      </div>

    </td>

    <td width="1" background="../images/line.gif"></td>

    <td align="center">

      <table border="0" cellpadding="0" cellspacing="0" width="95%">

        <tr>

          <td width="100%" height="20"></td>

        </tr>

        <tr>

          <td width="100%" height="100%" align="center">

<?



}



function content2() {



?>

          </td>

        </tr>

      </table>

    </td>

  </tr>

</table>



</body>



</html>

<?



}



function showmessage($msg) {



?>

            <table width="460" border="0" cellspacing="0" cellpadding="0">

              <tr>

                 <td background="../images/graph_border1.gif" height="25" valign="top">

                   <b style="font-family: 宋体;line-height: 21px">&nbsp;&nbsp;系 统 消 息</b>

                 </td>

              </tr>

              <tr>

                <td background="../images/graph_border2.gif">

                  <table width="460" border="0" cellspacing="0" cellpadding="0">

                    <tr> 

                      <td width="11">&nbsp;</td>

                      <td align="left" width="30" valign="top"><img src="../images/message.gif" width="30" height="30"></td>

                      <td align="center" valign="middle"><br><?echo$msg;?><br></td>

                      <td width="5"></td>

                      <td width="11" background="../images/graph_border5.gif">&nbsp;</td>

                      <td width="9"></td>

                    </tr>

                  </table>

                </td>

              </tr>

              <tr>

                <td background="../images/graph_border4.gif" height="10"></td>

              </tr>

            </table>

<?



}



###########################################################################################



if(file_exists("../config.inc.php"))

  include"../config.inc.php";

else

  die("缺少系统配置文件 config.inc.php，管理程序无法运行！");



if(!$user)

  $user=$HTTP_COOKIE_VARS["st_admin_user"];

if(!$password)

  $password=$HTTP_COOKIE_VARS["st_admin_pwd"]; 



if(!(is_dir("../$config[data]/$user") and @include"../$config[data]/$user/config.inc.php"))

  die("用户不存在！");



if($password==$config[user_passwd]) {

  setcookie("st_admin_user",$user,0);

  setcookie("st_admin_pwd",$password,0);

}else {

  content1();

  login();

  content2();

  exit;

}





if($action=="getcode") {



  $usercode="\n                        <table width=\"350\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n";

  $styles=array("counter","icon","text","null","计数器","图标","文本","隐藏");

  for($i=0;$i<4;$i++) {

    $code="<script>var user=\"$user\";var style=\"$styles[$i]\";var url=\"$config[url]\";</script><script src=\"$config[url]/stat.js\"></script>";

    $code=htmlspecialchars($code);

    $usercode=$usercode."                          <tr>\n";

    $usercode=$usercode."                            <td align=\"center\" width=\"200\">\n";

    $usercode=$usercode."                              <b>$styles[$i](".$styles[$i+4].") 样式调用代码</b>\n";

    $usercode=$usercode."                            </td>\n";

    $usercode=$usercode."                            <td align=\"left\" width=\"150\">\n";

    $usercode=$usercode."                              <input type=\"button\" name=\"btn_cc_".$styles[$i]."\" value=\"将代码复制到剪贴板\" class=\"button\" style=\"width: 120px\" onClick=JM_cc('".$styles[$i]."') onMouseOver =\"this.style.backgroundColor='#EEF7FF'\" onMouseOut =\"this.style.backgroundColor=''\"></b>\n";

    $usercode=$usercode."                            </td>\n";

    $usercode=$usercode."                          </tr>\n";

    $usercode=$usercode."                          <tr>\n";

    $usercode=$usercode."                            <td align=\"center\" colspan=\"2\">\n";

    $usercode=$usercode."                              <textarea name=\"".$styles[$i]."\" wrap=\"virtual\" rows=\"20\" strolling=\"no\" class=\"textarea\" style=\"width: 350px; height: 47px\" onMouseOver =\"this.style.backgroundColor='#F0F1FF'\" onMouseOut =\"this.style.backgroundColor=''\">$code</textarea>\n";

    $usercode=$usercode."                            </td>\n";

    $usercode=$usercode."                          </tr>\n";

    $usercode=$usercode."                          <tr>\n";

    $usercode=$usercode."                            <td height=\"15\" colspan=\"2\"></td>\n";

    $usercode=$usercode."                          </tr>\n";

  }

  $usercode=$usercode."                        </table>\n";

  

  content1();

  showmessage($usercode);

  content2();



}elseif($action=="modify") {



  content1();

  if(!$user_passwd) {



?>

            <table width="460" border="0" cellspacing="0" cellpadding="0">

              <tr> 

                <td background="../images/graph_border1.gif" height="25" valign="top"> 

                  <b style="font-family: 宋体;line-height: 21px">&nbsp;&nbsp;修 改 资 料</b> 

                </td>

              </tr>

              <tr> 

                <td background="../images/graph_border2.gif"> 

                  <table width="460" border="0" cellspacing="0" cellpadding="0">

                    <tr> 

                      <td width="11">&nbsp;</td>

                      <td align="center"> 

                        <form name="login" method="post" action="useradmin.php">

                          <table width="50%" border="0" cellspacing="0" cellpadding="0">

                            <tr> 

                              <td height="15">&nbsp;</td>

                            </tr>

                            <tr> 

                              <td height="20" align="center">管理密码</td>

                              <td height="20" align="right">

                                <input type="password" name="user_passwd" value="<?echo$config[user_passwd];?>" size="20" class="input" onMouseOver ="this.style.backgroundColor='#F0F1FF'" onMouseOut ="this.style.backgroundColor=''">

                              </td>

                            </tr>

                            <tr> 

                              <td height="10" align="center">主页地址</td>

                              <td height="10" align="right">

                                <input type="text" name="user_base" value="<?echo$config[user_base];?>" size="20" class="input" onMouseOver ="this.style.backgroundColor='#F0F1FF'" onMouseOut ="this.style.backgroundColor=''">

                              </td>

                            </tr>

                            <tr> 

                              <td height="10" align="center">站点名称</td>

                              <td height="10" align="right">

                                <input type="text" name="user_name" value="<?echo$config[user_name];?>" size="20" class="input" onMouseOver ="this.style.backgroundColor='#F0F1FF'" onMouseOut ="this.style.backgroundColor=''">

                              </td>

                            </tr>

                            <tr> 

                              <td height="15"></td>

                            </tr>

                            <tr> 

                              <td height="20" align="center" colspan="2"> 

                                <input type="submit" name="Summit" value="修  改" class="button" onMouseOver ="this.style.backgroundColor='#EEF7FF'" onMouseOut ="this.style.backgroundColor=''" onMouseOver ="this.style.backgroundColor='#EEF7FF'" onMouseOut ="this.style.backgroundColor=''">

                                &nbsp;&nbsp;&nbsp;&nbsp; <input type="reset" name="Reset" value="还  原" class="button" onMouseOver ="this.style.backgroundColor='#EEF7FF'" onMouseOut ="this.style.backgroundColor=''" onMouseOver ="this.style.backgroundColor='#EEF7FF'" onMouseOut ="this.style.backgroundColor=''">

                              </td>

                            </tr>

                          </table>

                          <br>

                          <input type="hidden" name="action" value="modify"><input type="hidden" name="user" value="<?echo$user;?>">

                        </form>

                      </td>

                      <td width="5"></td>

                      <td width="11" background="../images/graph_border5.gif">&nbsp;</td>

                      <td width="9"></td>

                    </tr>

                  </table>

                </td>

              </tr>

              <tr> 

                <td background="../images/graph_border4.gif" height="10"></td>

              </tr>

            </table>

<?



  }else {



    if(!($user_passwd and ereg("^[`~!@%^*_A-Za-z0-9-]+$",$user_passwd) and strlen($user_passwd)<=12))

      $error="管理密码空白、含有非法字符或其长度超过限制。";

    if(!(substr($user_base,0,7)=="http://"))

      $error="主页地址应为小写\"http://\"开头的真实地址。";

    if(!($user_name and !ereg("\"|\'",$user_name) and strlen($user_name)<=20))

      $error="站点名称含有非法字符或其长度超过限制。";



    if($error)

      showmessage("$error"."<br><br><a href=\"javascript:history.go(-1)\">返回上页</a><br>");

    else {

      $fp=@fopen("../$config[data]/$user/config.inc.php","w");

      $fw="<?\n\$config[user_passwd]=\"$user_passwd\";\n\$config[user_base]=\"$user_base\";\n\$config[user_name]=\"$user_name\";\n?>";

      if(@fwrite($fp,$fw))

        showmessage("恭喜！您的资料修改成功。<br>");

      else {

        showmessage("文件写入错误，资料修改不成功。<br><br><a href=\"javascript:history.go(-1)\">返回上页</a><br>");

        @fclose($fp);

      }

    }

  }



  content2();



}elseif($action=="reset") {



  content1();

  $error=0;



  if($confirmed=="yes") {



    $items2init=array("agent","click","day","detail","hour","month","pageview");

    for($i=0;$i<7;$i++) {



      $filename="../$config[data]/$user/$items2init[$i].dat";

      $fp=@fopen($filename,"w");

      if(@!fwrite($fp," "))

        $error++;

      @fclose($fp);



    }



    $fp=@fopen("../$config[data]/$user/start.dat","w");

    if(@!fwrite($fp,((date("Y")-1)*365+date("z")-1).".date ".date("Y-m-d")))

      $error++;

    @fclose($fp);

    

    if(!$error)

      showmessage("恭喜您！数据重置成功，统计将从今天重新开始。<br>");

    else

      showmessage($error." 个数据文件写入失败，数据无法重置。<br>请与管理员联系以获得解决。<br><br><a href=\"setup.php\" target=\"_blank\">我要进入安装向导</a><br>");



  }else {



    showmessage("重置数据将使统计重新开始，原有数据将被清除！<br>此操作不可恢复。您确定要这样吗？<br><br><a href=\"useradmin.php?action=reset&confirmed=yes\" style=\"color: #EE0000\">我知道了，我要重置数据</a><br><a href=\"javascript:history.go(-1);\">我按错了，返回上一页</a><br>");

    content2();



  }



}elseif($action=="suicide") {



  content1();

  if($confirmed=="yes") {

    $files2del=array("agent.dat","click.dat","day.dat","detail.dat","hour.dat","month.dat","pageview.dat","start.dat","config.inc.php");

    for($i=0;$i<count($files2del);$i++)

      @unlink("../$config[data]/$user/".$files2del[$i]);

    if(@!rmdir("../$config[data]/$user")) {

      showmessage("文件/目录读写错误，账号自杀不成功！<br><br><a href=\"javascript:history.go(-2);\">返回上页</a><br>");

      content2();

      //exit;

    }else {

      showmessage("账号自杀成功。欢迎您再次使用 创想中文统计！再见。<br>");

      content2();

    }

  }else {

    showmessage("账号自杀将删除您的统计账号及所有数据！<br>此操作不可恢复。您确定要这样吗？<br><br><a href=\"useradmin.php?action=suicide&user=$user&confirmed=yes\" style=\"color: #EE0000\">我知道了，我要账号自杀</a><br><a href=\"javascript:history.go(-1);\">我按错了，返回上一页</a><br>");

    content2();

    //exit;

  }



}elseif($action=="logout") {



  setcookie("st_admin_user");

  setcookie("st_admin_pwd");

  content1();

  showmessage("您已退出登录状态！<br>感谢您使用创想中文统计。<br><br><a href=\"useradmin.php?user=$user\">重新登录</a><br>");

  content2();



}else {



  content1();

  showmessage("您已成功登录，请点击左侧菜单以执行相应操作。<br>");

  content2();



}



?>