<?php


require("./total.inc");
require("./enco.inc");
require("./list_file_upload.inc");
require("./socket.inc");
require("./db.inc");


// 最初のログイン確認用
function ninsyou_out($namae, $pass)
{

$conn = db_connect();
if(!$conn){
   echo "ninsyou_out(): not connection!<br>";
   return -1;
   }

$result = db_query($conn, "select * from ninsyou where namae='$namae' and passw='$pass';");
if(!$result){
   print("ninsyou_out(): banem select , bud query\n");
   return -1;
   }
$num = db_num_rows($result);
if($num == 1){
   print("ninsyou_out(): ok");
   return 1;
   }
else{
   print("ninsyou_out(): malltipull data bud query\n");
   return -1;
   }

db_free_result($result);
db_Close($conn);
}



// ログイン時のタイムスタンプ更新
function ninsyou_in($namae, $pass)
{

$myutime = date("U");

$conn = db_connect();
if(!$conn){
   echo "ninsyou_in(): not connection!<br>";
   return -1;
   }

$result = db_query($conn, "update ninsyou set last='$myutime' where namae='$namae' and passw='$pass';");
if(!$result){
   print("ninsyou_out(): banem select , bud query\n");
   return -1;
   }
else{
   print("ninsyou_out(): ok set data\n");
   }

db_free_result($result);
db_Close($conn);
}




// 各ファイルのセッション確認
function ninsyou_check($file_rev)
{

if(!isset($file_rev)){
   echo "rev not set.";
   return -1;
   }

if(0 != strcmp("ASCII", mb_detect_encoding($file_rev))){
   echo "ninsyou_check(): rev encode err. not ascii.";
   return -1;
   }

if(!isset($_SESSION['g_namae'])){
   echo "ninsyou_check(): namae not set session err..";
   echo "<META http-equiv=refresh content='0; url=./index.html' >";
   return -1;
   }

if(!isset($_SESSION['g_pass'])){
   echo "ninsyou_check(): pass not set session err..";
   echo "<META http-equiv=refresh content='0; url=./index.html' >";
   return -1;
   }

$namae = $_SESSION['g_namae'];
$pass = $_SESSION['g_pass'];


$conn = db_connect();
if(!$conn){
   echo "ninsyou_check(): not connection!<br>";
   return -1;
   }

$result = db_query($conn, "select * from ninsyou where namae='$namae' and passw='$pass';");
if(!$result){
   print("ninsyou_check(): banem select , bud query\n");
   return -1;
   }
$num = db_num_rows($result);
if($num == 1){
// print("ninsyou_check(): ok");

   $inputencoding = mb_convert_variables("EUC-JP",  "ASCII,UTF-8,SJIS-win",  $result);

   $my_rev = enco_rtrim(db_fetch_Result($result, 0, rev));
   if($file_rev < $my_rev){
      echo "ninsyou_check(): あなたにはこのファイルにアクセスする権限がありません \n";
      echo "<META http-equiv=refresh content='5; url=./index.html' >";
      return -1;
      }
   else{
      return 1;
      }
   }
else{
   print("ninsyou_check(): $num malltipull data bud query\n");
   return -1;
   }

db_free_result($result);
db_Close($conn);

}



?>




