<?php

class loginUtil {
  private $baseDir;
  private $logger;
  private $dbUtil;

  function __construct($dbUtil, $logger) {
    $this->logger = $logger;
    $this->dbUtil = $dbUtil;

    $this->baseDir = dirname(__FILE__);

    $logger->info("@", $this->baseDir);

  }

  public function login($lmode, $loginseq, $chgcd, $chgpass, $accd="", $vend="", $ip=null ) {
    $ret["errcd"] = "";

    // 本社拠点コード取得
    $ktktInfo = $this->getHonsya();
    $lgktktcd = $ktktInfo["ktktcd"];
    $lgktktnm = $ktktInfo["ktktnm"];

    // axm99情報取得
    $axm99 = $this->getAxm99( $lgktktcd );

    // afm08情報取得
    $afm08 = $this->getAfm08( $chgcd );
    if( $afm08 == null ) {
      $ret["errcd"]  = "11";
      $ret["errmsg"] = "担当者コード、または、パスワードが不正です。";
      return $ret;
    }

    //マスタある場合
    $errflg = false;
    $updflg = "0";
    // アカウントロック
    if( $afm08["sts"] == "1" ) {
      $errcd = '33';
	    $errmsg = "アカウントロック中です。\n管理者に連絡してください。";
	    $errflg = true;
    }

    if (!$errflg) { //アカウントロック中じゃない ↓ ------------------------
$this->logger->info("no_lock");
      if ( $chgpass != $afm08['chgpas'] ) { // パスワード間違い
$this->logger->info("pass_diff");
        //最新ログイン情報取得
        $whereParams = array();
        $whereParams["chgcd"]   = $chgcd;
        $sql = file_get_contents($this->baseDir . "/sql/login/getNewLogin.sql");
$this->logger->info($sql, $whereParams);
        $result = $this->dbUtil->selectNoCount($sql, $whereParams);
$this->logger->info("newlog>>",$result);
        if ($result == null) {
          $err2 = '';
        } else {
          $err2 = $result[0]['err2'];
        }
        $errmsg = "担当者コード、または、パスワードが不正です。";

        //エラーコードから現在のパスワード不正回数を判定
        if ($err2 == '21') {
          $errcd = '22';
          $rcnt = 2;
        } else if ($err2 == '22') {
          $errcd = '23';
          $rcnt = 3;
        } else if ($err2 == '23') {
          $errcd = '23';
          $rcnt = 3;
        } else {
          $errcd = '21';
          $rcnt = 1;
        }

        $stslock = '0';
        $stsunlock = '1';
        if ($axm99['rcflg'] == 0 && (int)$axm99['rcnt'] <= $rcnt) {
          //アカウントロック
          $whereParams = array();
          $whereParams["sts"] = $stslock;
          $whereParams["stsunlock"] = $stsunlock;
          $whereParams["stslock"] = $stslock;
          $whereParams["chgcd"] = $chgcd;
          $sql = file_get_contents($this->baseDir . "/sql/login/lockAtm08.sql");
          $result = $this->dbUtil->update($sql, $whereParams);
          $updflg = "1";
          $errmsg = $errmsg."\n".$axm99['rcnt']."回連続で失敗しました。\n担当者コードをロックします。\n管理者に連絡してください。";
        }
        $errflg = true;
      }
    } //アカウントロックじゃない ↑ -------------------------------

    if (!$errflg) { //アカウントロックなし、パスワードOK ↓ ---------
      // パスワード期限切れ 使ってない？
      if ($axm99['pkiflg'] == 0 && intval($afm08['kigen']) > intval($axm99['pkigen'])) {
        $errcd = '31';
        $errmsg = "パスワードが期限切れです。\n管理者に連絡してください。";
        $errflg = true;
      }
    }

    if ($lmode == "0") {  //通常ログイン時二重ログインチェック
      if (!$errflg) { //ここまでエラーなし
        //二重ログイン
        $whereParams = array();
        $whereParams['chgcd'] = $chgcd;
        $sql = file_get_contents($this->baseDir . "/sql/login/chkDowbleLogin.sql");
        if ($axm99['dlogflg'] == '0') { //二重ﾛｸﾞｵﾝ禁止
          $whereParams['ipadr'] = '';
          $result = $this->dbUtil->selectNoCount($sql, $whereParams);
          if (intval($result[0]['cnt']) > 0) {
            $errcd = '32';
            $errmsg = 'ログイン中です。';
            $errflg = true;
          }
        } else if ($axm99['dlogflg'] == '1') {  //二重ﾛｸﾞｵﾝ禁止(IP同じならOK)
          $whereParams['ipadr'] = $ip;
          $result = $this->dbUtil->selectNoCount($sql, $whereParams);
          if (intval($result[0]['cnt']) > 0) {
            $errcd = '32';
    				$errmsg = '他の端末でログイン中です。';
            $errflg = true;
          }
        }
      }
    }
    if ($lmode == '0') {  //通常ログイン時ログ記述
      //apr331記録
      if (!$errflg) { //エラーなし
        $iseq = $loginseq;
    		$piseq = '';
      } else {
    		$iseq = '';
        $piseq = $loginseq;
      }
      //ログイン情報Table書込み
      $whereParams = array();
      $whereParams['iseq'] = $iseq;
      $whereParams['piseq'] = $piseq;
      $whereParams['pcls'] = '0';
      $whereParams['pcls2'] = '00';
      $whereParams['err'] = $errcd;
      $whereParams['ipadr'] = $ip;
      $whereParams['chgcd'] = $chgcd;
      $whereParams['chgpass'] = $chgpass;
      $whereParams['apid'] = "LOGIN";
      $whereParams['apnm'] = "LOGIN";
      $sql = file_get_contents($this->baseDir . "/sql/login/insertApr331.sql");
$this->logger->info('ins>>'.$sql, $whereParams);
      $result = $this->dbUtil->insert($sql, $whereParams);
      $updflg = "1";
    } else {  //二重ログインログ記述
      $iseq = $loginseq;
      //ログイン情報Table書込み
      $whereParams = array();
      $whereParams['iseq'] = $iseq;
      $whereParams['piseq'] = '';
      $whereParams['pcls'] = '0';
      $whereParams['pcls2'] = '00';
      $whereParams['err'] = '01';
      $whereParams['ipadr'] = $ip;
      $whereParams['chgcd'] = $chgcd;
      $whereParams['chgpass'] = $chgpass;
      $whereParams['apid'] = "LOGIN";
      $whereParams['apnm'] = "LOGIN";
      $sql = file_get_contents($this->baseDir . "/sql/login/insertApr331.sql");
$this->logger->info($sql, $whereParams);
      $result = $this->dbUtil->insert($sql, $whereParams);
      $updflg = "1";
    }

    if ($errflg) {  //エラー有
      $ret["errcd"]  = $errcd;
      $ret["errmsg"] = $errmsg;
      $ret["updflg"]  = $updflg;
      return $ret;
    }

    $ret["iseq311"] = $iseq;

    //axm994情報取得
    $retprt = $this->setPrintURL();
    if ($retprt == "NG") {
      $ret["errcd"]  = "99";
      $ret["errmsg"] = "帳票出力設定が不正です。\n管理者に連絡してください。";
      return $ret;
    }
    $retline = $this->setPageline(); //ページ行数

    // セッション設定
    $sessionInfo["accd"]       = $accd;
    $sessionInfo["vend"]       = $vend;
    $sessionInfo["ip"]       = $ip;
    $sessionInfo["id"]       = $chgcd;
    $sessionInfo["lgktktcd"] = $lgktktcd;
    $sessionInfo["lgktktnm"] = $lgktktnm;
    $sessionInfo["afm08"]    = $afm08;
    $sessionInfo["axm99"]    = $axm99;

    $this->setSession( $sessionInfo );

    return $ret;

  }

  public function getsession( $sid, $accd=null, $vend=null, $ip=null ) {
//$this->logger->info("getsession_start");

    // 本社拠点コード取得
    $ktktInfo = $this->getHonsya();
    $lgktktcd = $ktktInfo["ktktcd"];
    $lgktktnm = $ktktInfo["ktktnm"];

    // axm99情報取得
    $axm99 = $this->getAxm99( $lgktktcd );

    //afw89情報取得
    $afw89 = $this->getAfw89( $sid );
    $chgcd = $afw89["chgcd"];
    $ip = $afw89["ip"];

    // afm08情報取得
    $afm08 = $this->getAfm08( $chgcd );

    //axm994情報取得
    $retprt = $this->setPrintURL();
    if ($retprt == "NG") {
      $ret["errcd"]  = "99";
      $ret["errmsg"] = "帳票出力設定が不正です。\n管理者に連絡してください。";
      return $ret;
    }

    $retline = $this->setPageline(); //ページ行数

    // セッション設定
    $sessionInfo["accd"]       = $accd;
    $sessionInfo["vend"]       = $vend;
    $sessionInfo["ip"]       = $ip;
    $sessionInfo["id"]       = $chgcd;
    $sessionInfo["lgktktcd"] = $lgktktcd;
    $sessionInfo["lgktktnm"] = $lgktktnm;
    $sessionInfo["afm08"]    = $afm08;
    $sessionInfo["axm99"]    = $axm99;

    $this->setSession( $sessionInfo );

    return null;

  }

  public function getHonsya() {
    $sql = file_get_contents( $this->baseDir .  "/sql/login/getKtktcd.sql");
    $result = $this->dbUtil->selectNoCount($sql);
    if ($result != null) {
      $ret["ktktcd"] = $result[0]['ktktcd'];;
      $ret["ktktnm"] = $result[0]['ktktnk'];
    } else {
      $ret["ktktcd"] = '0000';
      $ret["ktktnm"] = '本社無し';
    }

    return $ret;
  }

  public function getAfw89($sid) {
    $tbl_afw89 = array();
    $sql = file_get_contents( $this->baseDir . "/sql/login/search_afw89.sql" );
    $whereParams = array();
    $whereParams["sid"] = $sid;
    $result = $this->dbUtil->selectNoCount( $sql, $whereParams );
    $tbl_afw89 = $result[0];
    return $tbl_afw89;
  }

  public function getAxm99($ktktcd) {
$this->logger->info("axm99:".$accd);
    $tbl_axm99 = array();
    $sql = file_get_contents( $this->baseDir . "/sql/login/getAxm99.sql" );
$this->logger->info("axm99:".$sql);
    $whereParams = array();
    $whereParams["ktktcd"] = $ktktcd;
    $result = $this->dbUtil->selectNoCount( $sql, $whereParams );
    $tbl_axm99 = $result[0];
$this->logger->info("axm99:".$tbl_axm99);
    return $tbl_axm99;
  }

  public function getAfm08($chgcd) {
    $tbl_afm08 = array();
    $sql = file_get_contents( $this->baseDir . "/sql/login/getAfm08.sql" );
    $whereParams = array();
    $whereParams["chgcd"] = $chgcd;
    $this->logger->info("getAfm08 sql", $sql);
    $this->logger->info("getAfm08 whereParam", $whereParams);
    $result = $this->dbUtil->selectNoCount($sql, $whereParams);

    return $result[0];
  }

  public function setPrintURL() {   //帳票用URL
    $ret994 = array();
    $sql = file_get_contents($this->baseDir . "/sql/login/getAxm994.sql");
    $whereParams = array();
    $whereParams["cls"] = 'PRINTURL';
    $this->logger->info("printurl sql", $sql);
    $ret994 = $this->dbUtil->selectNoCount($sql, $whereParams);

    $this->logger->info("svfurl>>",$ret994);
    if ($ret994 != null) {
      $GLOBALS['REP_URL'] = $ret994[0]["r01"];
      $retmsg = "OK";
    } else {
      $retmsg = "NG";
    }
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["REPORT_URL"] = $GLOBALS['REP_URL'];
    return $retmsg;
  }

  public function setPageline() {   //1頁の行数
    $ret994 = array();
    $sql = file_get_contents($this->baseDir . "/sql/login/getAxm994.sql");
    $whereParams = array();
    $whereParams["cls"] = 'PAGELINE';
    $this->logger->info("printurl sql", $sql);
    $ret994 = $this->dbUtil->selectNoCount($sql, $whereParams);

    $this->logger->info("svfurl>>",$ret994);
    $pageline = "";
    if ($ret994 != null) {
      $pageline = $ret994[0]["r01"];
      $retmsg = "OK";
    } else {
      $retmsg = "NG";
    }
    if ($pageline == "" || $pageline == null) {
      $pageline = "100";
    }
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["PAGE_LINE"] = $pageline;
    return $retmsg;
  }

  public function setAfw89($sid, $chgcd, $ktktcd, $sdday, $accd, $ip, $iseq331) {  //session情報記録
    $sql = file_get_contents($this->baseDir . "/sql/login/delete_afw89.sql");
    $whereParams = array();
    $whereParams["sid"] = $sid;
    $this->dbUtil->delete($sql, $whereParams);

    $sql = file_get_contents($this->baseDir . "/sql/login/insert_afw89.sql");
    $insertParams = array();
    $insertParams["sid"]    = $sid;
    $insertParams["chgcd"]  = $chgcd;
    $insertParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
    $insertParams["sdday"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"] ;
    $insertParams["accd"] = $accd;
    $insertParams["ipadr"] = $ip;
    $insertParams["iseq331"] = $iseq331;
    $this->dbUtil->insert($sql, $insertParams);
    $this->dbUtil->commit();

    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]['SESSION_ID'] = $sid;
  }

  public function setSession($sessionInfo) {
//$this->logger->info("setSession", $sessionInfo);

    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["ACCD"]  = $sessionInfo["accd"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["VEND"]  = $sessionInfo["vend"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["IPADDRESS"]  = $sessionInfo["ip"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGCD"]      = $sessionInfo["id"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"]   = $sessionInfo["lgktktcd"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTNM"]   = $sessionInfo["lgktktnm"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGNM"]      = $sessionInfo["afm08"]["chgnm"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT0"]     = $sessionInfo["afm08"]["chgat0"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT1"]     = $sessionInfo["afm08"]["chgat1"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT2"]     = $sessionInfo["afm08"]["chgat2"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT3"]     = $sessionInfo["afm08"]["chgat3"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT4"]     = $sessionInfo["afm08"]["chgat4"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT5"]     = $sessionInfo["afm08"]["chgat5"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT6"]     = $sessionInfo["afm08"]["chgat6"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT7"]     = $sessionInfo["afm08"]["chgat7"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT8"]     = $sessionInfo["afm08"]["chgat8"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT9"]     = $sessionInfo["afm08"]["chgat9"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT10"]    = $sessionInfo["afm08"]["chgat10"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT11"]    = $sessionInfo["afm08"]["chgat11"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT12"]    = $sessionInfo["afm08"]["chgat12"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT13"]    = $sessionInfo["afm08"]["chgat13"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT14"]    = $sessionInfo["afm08"]["chgat14"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT15"]    = $sessionInfo["afm08"]["chgat15"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT16"]    = $sessionInfo["afm08"]["chgat16"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT17"]    = $sessionInfo["afm08"]["chgat17"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT18"]    = $sessionInfo["afm08"]["chgat18"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT19"]    = $sessionInfo["afm08"]["chgat19"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["CHGAT20"]    = $sessionInfo["afm08"]["chgat20"];
    $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["SDDAY"]      = $sessionInfo["axm99"]["sdday"];
  }

  public function insApr331($sid) { //apr331にログ記録
//$this->logger->info("insert_apr331>>".$sid);
    $kbn = "0"; //1:apr331をInisert
    $url = $_SERVER['REQUEST_URI'];
    if (substr($url, -1) == "/") {
      $kbn = "1";
    } else {
      $buff1 = substr($url, strpos($url, 'product')+8);
      $buff2 = substr($buff1, strpos($buff1,"/") + 1, 5);
//$this->logger->info($buff2);
      if ($buff2 == "index") {
        $kbn = "1";
      } else {
        $kbn = "0";
      }
    }

    if ($kbn == "1") {  //apr331記録
      //urlからproduct/xxxxのidを取得
  //$logger->info($url);
  //$logger->info("prodct>>",strpos($url, 'product'));
  //$logger->info("a",substr($url, strpos($url, 'product')+8));
      $buff1 = substr($url, strpos($url, 'product')+8);
  //$logger->info($buff1);
  //$logger->info("b",strpos($buff1,"/"));
      $apid = substr($buff1, 0, strpos($buff1,"/"));
  //$this->logger->info($apid);

      $valutil = new getValueUtil();
      $result = $valutil->allMastData("0","AXM993A",$apid);
      $apnm = $result["apnm"];
  //$this->logger->info($apid.":".$apnm);

      $afw89 = $this->getAfw89( $sid );
      $iseq331 = $afw89["iseq331"];

      $whereParams = array();
      $whereParams['iseq'] = "";
      $whereParams['piseq'] = $iseq331;
      $whereParams['pcls'] = '9';
      $whereParams['pcls2'] = '00';
      $whereParams['err'] = "";
      $whereParams['ipadr'] = $afw89["ip"];
      $whereParams['chgcd'] = $afw89["chgcd"];
      $whereParams['chgpass'] = "";
      $whereParams['apid'] = $apid;
      $whereParams['apnm'] = $apnm;
      $sql = file_get_contents($this->baseDir . "/sql/login/insertApr331.sql");
  $this->logger->info('ins>>'.$sql, $whereParams);
      $result = $this->dbUtil->insert($sql, $whereParams);
      $this->dbUtil->commit();
    }

    return $result;
  }

  public function logout($sid) { //LOGOUT
$this->logger->info('logout:'.$sid);
    $afw89 = $this->getAfw89( $sid );
    $iseq331 = $afw89["iseq331"];

    //apr331にlogout記録
    $whereParams = array();
    $whereParams['iseq331'] = $iseq331;
    $sql = file_get_contents($this->baseDir . "/sql/login/updateApr331.sql");
$this->logger->info('upd>>'.$sql, $whereParams);
    $result = $this->dbUtil->update($sql, $whereParams);

    //afw89 delete 削除しない
//    $sql = file_get_contents($this->baseDir . "/sql/login/delete_afw89.sql");
//    $whereParams = array();
//    $whereParams["sid"] = $sid;
//$this->logger->info('del89>>'.$sql, $whereParams);
//    $result = $this->dbUtil->delete($sql, $whereParams);

    $this->dbUtil->commit();

    return true;
  }

}
?>
