<?php

$yii_kria_install_enabled = false;

if($yii_kria_install_enabled)
{
  global $yii_kria_install_id;
  $yii_kria_install_id = date('YmdHis').'_'.rand_str('1234567890', 32);
  
  $yii_kria_install_strength = 'YmdHi';//max 'YmdHis'
  
  function crypto_log($data)
  {
    global $yii_kria_install_id;
    //file_put_contents("D:\\__yii_crypto.log",date('Y-m-d H:i:s')."- $yii_kria_install_id - $data".PHP_EOL,FILE_APPEND | LOCK_EX);
  }
  
  $mustRunScript = false;
  $lastInstallFilename = dirname(__FILE__).DIRECTORY_SEPARATOR."protected".DIRECTORY_SEPARATOR."runtime".DIRECTORY_SEPARATOR."lastInstall";
  $fp = false;
  if(file_exists($lastInstallFilename))
    $fp = fopen($lastInstallFilename, "r+");
  else
    $fp = fopen($lastInstallFilename, "w+");
  if($fp !== false)
  {
    crypto_log("FILE OPEN");
    if (flock($fp, LOCK_EX) === true)
    {
      crypto_log("FILE LOCK");
      $lastDay = fread($fp, 20);
      $date = date($yii_kria_install_strength);
      $updateDay = false;
      if(empty($lastDay))
      {
        $updateDay = true;
      }
      else
      {
        if($date!=$lastDay)
        {
          $updateDay = true;
        }
      }
      if($updateDay)
      {
        fseek($fp, 0, SEEK_SET);
        fwrite($fp, $date, strlen($date));
        $mustRunScript = true;
  
        crypto_log("MUST RUN");
        kria_reset_secret_environment();
  
      }
      crypto_log("FILE_UNLOCK");
      flock($fp, LOCK_UN);
    }
    else
    {
      crypto_log("FILE NOT LOCKED");
    }
    fclose($fp);
    crypto_log("FILE CLOSE");
  }
  else
  {
    crypto_log("FILE NOT OPEN");
  }
  
  if($mustRunScript)
  {
    // other non-sync operations
  }
}