<?php
$cate_id = "mnu_member"; //현재 메뉴 ID 필수!!!
include_once "{$_SERVER['DOCUMENT_ROOT']}/common.inc.php";
include_once _SVC_INC_PATH_ ."/auth_check.php";
include_once _HOME_INC_PATH_ ."/ajax_init.php";

$today    = $_REQUEST['sdate'] ?: date("Y-m-d");
$startday = strtodate($today." -30 days");

if( !_notAuth("R") ){/************ 권한체크*/
  switch( $_REQUEST['mode'] ){
    case "setMember":
      if( (!$_REQUEST['member_pw'] || !$_REQUEST['member_pwf']) && !$_REQUEST['member_mail'] && !$_REQUEST['member_mobile'] ){
        $retval['mesg'] = "변경할 내용을 정확히 입력하여 주십시오.";
        break;
      }
      if( $_REQUEST['member_pw'] || $_REQUEST['member_pwf'] ){
        if( $_REQUEST['member_pw'] != $_REQUEST['member_pwf'] ){
          $retval['mesg'] = "비밀번호 및 비밀번호 확인이 일치하지 않습니다.";
          break;
        }
        if( !preg_match("/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{6,}$/ims",$_REQUEST["member_pw"]) ){
          $retval['mesg'] = "비밀번호는 대소문자, 숫자, 특수문자 포함 6자리 이상이어야 합니다.";
          break;
        }
        if( !$_REQUEST['member_pwo'] ){
          $retval['mesg'] = "기존 비밀번호를 입력하지 않았습니다.";
          break;
        }
        if( !$baseObj->pwCheck( $GLOBALS['_Umem']['member_id'], $_REQUEST['member_pwo'] ) ){
          $retval['mesg'] = "기존 비밀번호가 일치하지 않습니다.";
          break;
        }
      }
      if( !chkValiMail($_REQUEST['member_mail']) ){
        $retval['mesg'] = "메일주소를 확인해 주세요.";
        break;
      }
      if( !chkValiPhone($_REQUEST['member_mobile']) ){
        $retval['mesg'] = "연락처를 확인해 주세요.";
        break;
      }
      $sqldata = Array(
        "member_id" => $GLOBALS['_Umem']['member_id'],
        "member_mail" => $baseObj->lcrypt->enc_secure($_REQUEST['member_mail']),
        "member_mobile" => $baseObj->lcrypt->enc_secure($_REQUEST['member_mobile']),
      );
      if( $_REQUEST['member_pw'] ){
        $sqldata["member_pw"] = $baseObj->lcrypt->pw_make($_REQUEST['member_pw']);
      }
      $rst = $baseObj->setMemberParts($sqldata);
      if( $rst ){
        $retval['code'] = true;
        $retval['mesg'] = "변경이 완료되었습니다.";
      }
      break;

    case "getRank":
      $_REQUEST['no_echo'] = true;
      break;

    case "getChart":
      $_REQUEST['no_echo'] = true;

      //페이지 Rank
      $sql = "select LOG.visit_page, count(*) as visit_count from";
      $sql.= " ( select substring(visit_url, 0, iif(charindex('?', visit_url)<>0,charindex('?', visit_url),100000)) as visit_page, visit_sess from ".TB_visit_log." where visit_type = 'service' and visit_date >= '{$startday} 00:00:00.000' and visit_date <= '{$today} 23:59:59.999' ) as LOG";
      $sql.= " left join ".TB_visit_sess." as SESS on LOG.visit_sess = SESS.visit_sess";
      $sql.= " where member_id = '{$GLOBALS['_Umem']['member_id']}' group by LOG.visit_page order by visit_count desc ".$baseObj->DB->limit(0,10);
      $rank_row = $baseObj->DB->query($sql);

      //30일 페이지뷰
      $where = new DB_where("and");
      $where->add("member_id = ?", $GLOBALS['_Umem']['member_id']);

      $where = new DB_where("and");
      $where->add("SESS.member_id = ?", $GLOBALS['_Umem']['member_id']);
      $where->add("LOG.visit_date >= ?",$startday);
      $where->add("LOG.visit_date <= ?", $today);
      $wqry = $where->get_where();
      $whereqry = $wqry['qry'];
      $whererow = $wqry['val'];

      $sql = "select LOG.visit_date, count(*) as count from (select substring(convert(varchar(25),visit_date,121),1,10) as visit_date, visit_sess";
      $sql.= " from ".TB_visit_log.") as LOG left join ".TB_visit_sess." as SESS on LOG.visit_sess = SESS.visit_sess";
      $sql.= " where {$whereqry} group by LOG.visit_date order by LOG.visit_date";
      $rst = $baseObj->DB->qry($sql, $whererow);

      $log_row = array();
      $log_temp_row = array();
      $log_week_row = array();
      while( $row = $baseObj->DB->fetch_row($rst) ){
        $log_temp_row[$row[0]] = $row[1];
      }
      for( $a=30 ; $a>=0 ; $a-- ){
        $day = strtodate($today." -{$a}days");
        $log_row[$day] = $log_temp_row[$day]?:0;

        $week = date("w", strtotime($day));
        $log_week_row[$week] += $log_temp_row[$day];
      }

      //요일별 페이지뷰
      $where = new DB_where("and");
      $where->add("member_id = ?", $GLOBALS['_Umem']['member_id']);
      $where->add("visit_sdate >= ?",$startday." 00:00:00.000");
      $where->add("visit_sdate <= ?", $today." 23:59:59.999");
      $wqry = $where->get_where();
      $whereqry = $wqry['qry'];
      $whererow = $wqry['val'];

      $sql = "select visit_date, count(*) as count from (select substring(convert(varchar(25),visit_sdate,121),1,10) as visit_date";
      $sql.= " from ".TB_visit_sess." where {$whereqry} ) as SESS group by SESS.visit_date order by SESS.visit_date";
      $rst = $baseObj->DB->qry($sql, $whererow);

      $sess_row = array();
      $sess_temp_row = array();
      while( $row = $baseObj->DB->fetch_row($rst) ){
        $sess_temp_row[$row[0]] = $row[1];
      }
      for( $a=30 ; $a>=0 ; $a-- ){
        $day = strtodate($today." -{$a}days");
        $sess_row[$day] = $sess_temp_row[$day]?:0;
      }

      for( $a=0 ; $a<7 ; $a++ ){
        if( (7 + $a - date("w", strtotime($startday)))%7 < 3 ){
          $log_week_row[$a] = $log_week_row[$a]/5;
        }else{
          $log_week_row[$a] = $log_week_row[$a]/4;
        }
      }
      break;
  }
}

if( !$_REQUEST['no_echo'] ){
  echo json_encode($retval);
}