/*========================================================================*/
/* source name : アクセスカウント処理                                     */
/* create date : 10/01/11 MSS m.sugiura                                   */
/* last update : 10/01/11                                                 */
/* revition    : 1.0.0                                                    */
/* desc or note: none                                                     */
/*========================================================================*/
  require_once ('./const_counter.inc');
  require_once ('./const_counter_parms.inc');
  require_once ('./lib/FileCtrl.inc');
  //---- 変数定義 ----
  $my_input_charcode = "SJIS";
  $my_inner_charcode = "UTF-8";
  $my_output_html_charcode = "SJIS";
  $my_output_mail_charcode = "ISO-2022-JP";
  $my_checked_parms  = array();
  $my_checked_fields = array();
  //---- 受信変数の文字コード変換 ----
  $my_checked_parms = conv_request_parms();
  f_PutLog("/tmp/ac_counter.log","mode=".$my_checked_parms['mode']);
  //---- 実行処理の分岐---------------------------------------------------
  if( "setup" == $my_checked_parms['mode'] ){
      //--> mode='rec' (アクセスログ記録実行)
      set_rec_scripts($my_checked_parms['pgid']);
      exit;
  } elseif ( "start" == $my_checked_parms['mode'] ){
      //->記録開始(onLoad時)
      inc_file_count($my_checked_parms['pgid']);		// 指定ページのファイルカウンタ++
      // 結果用画像表示
      show_dynamic_image($myExecParms['home_dir'].'./img/myhome.gif');
      exit;
  } else {
      // modeパラメタが想定外の時は初期表示
      show_page($my_checked_parms);
      exit;
  }
exit;
//--------------------------------------------------------------------------
// 動的JS出力処理
//--------------------------------------------------------------------------
function set_rec_scripts($vPgid, $debug = 0){
   //---- phpのプロセスIDをjs変数として事前に出力する ----
   header("Content-type: text/html");
   echo "var gPid=".getmypid().";\n";
   echo "var pgid='".$vPgid."';\n";
   //---- 動的表示用JSファイル表示 ----
   // edit read js fname
   $js_src = f_readFileToArray("./ac_counter.ihtml");
   // output source code
   foreach($js_src as $line){
        echo $line;
   }
   //---- debug 情報表示 ----
#   if( $debug = 1 ){
#       echo "document.write('HTTP_REFERER(php)=".$_SERVER["HTTP_REFERER"]."
');";
#       echo "document.write('REMOTE_HOST=".$_SERVER["REMOTE_HOST"]."
');";
#       echo "document.write('REMOTE_ADDR=".$_SERVER["REMOTE_ADDR"]."
');";
#       echo "document.write('SERVER_NAME=".$_SERVER["SERVER_NAME"]."
');";
#       echo "document.write('HTTP_USER_AGENT=".$_SERVER["HTTP_USER_AGENT"]."
');";
#       echo "document.write('SCRIPT_NAME=".$_SERVER["SCRIPT_NAME"]."
');";
#      #echo "document.write('');";
#   }
}
//--------------------------------------------------------------------------
// 動的画像表示
//--------------------------------------------------------------------------
function show_dynamic_image($vFname){
      header("Content-type: image/gif");
      echo file_get_contents($vFname);
      f_PutLog($gLogFname,"show_dynamic_image file-path=".$vFname);
      exit;
}
//--------------------------------------------------------------------------
// ファイルカウントをINCする
//--------------------------------------------------------------------------
function inc_file_count($vPageID){
    //---- define valiable ----
    $myFn="inc_file_count()";
    $mySaveCount = 1;
    // global valiables
    global $myExecParms;
    global $page_count_parms;
    global $gLogFname;
    if( $myExecParms['debug'] ){
        f_PutLog($gLogFname,"### ".$myFn."args -> vPageID=".$vPageID);
    }
    // edit count file name
    $myReadFname = $page_count_parms[$vPageID]['cfile'];
    f_PutLog($gLogFname,"myReadFname = ".$myReadFname);
    // File Open
    $myFh=fopen($myReadFname,'r');
    if( $myFh ){
        //-> File Open
        // read 1 line
        $str = fgets($myFh);
        // File Close
        fclose($myFh);
        f_PutLog($gLogFname,"ac:read count=".$str);
    }
    $mySaveCount = $str + 1;
    f_PutLog($gLogFname,"save count=".$mySaveCount);
    
    $myFh=fopen($myReadFname,'w');
    // save 1 line
    $a = fputs($myFh, $mySaveCount);
    // File Close
    fclose($myFh);
    //----------------------------------------------
    return;
}
//==========================================================================
// 受信項目の文字コード変換を行う
//==========================================================================
function conv_request_parms(){
    //---- 変数初期化 ----
    $rs_request_parms = array();
    global $my_input_charcode, $my_inner_charcode, $my_output_html_charcode, $my_output_mail_charcode;
    //---- post/getのマージ(with文字コード変換) ----
    foreach($_GET as $k => $v){
        $rs_request_parms[$k] = mb_convert_encoding($v, $my_inner_charcode, $my_input_charcode);  
    }
    foreach($_POST as $k => $v){
        $rs_request_parms[$k] = mb_convert_encoding($v, $my_inner_charcode, $my_input_charcode);  
    }
#print_r($_GET);
#print "