<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Response;

class StatusController extends Controller
{

    public $layout = 'main';//'response_layout';

    CONST OLDAL=[
        'fiz_mod_valt',  // fizetési mód váltás
        'reszletfiz_1',  // részletfizetési kérés
    ];

    CONST WELCOME = [ 'KauAdapter' => 'v.1.1', 'result'=>null ];
    CONST ERROR = [  'message' => null  ];
    CONST AUTH_KEY = "KADF45-I2DG46-LHA4J2-02ADO8";


    private $_welcome;
    private $_error;
    private $_header;    

    public function __construct($id, $module)
    {
        parent::__construct($id,$module);
    }

    public function beforeAction($action)
    {

        $this->_error = self::ERROR;

        $this->_welcome = self::WELCOME;
        //$this->_welcome['hash'] = hash("SHA256",self::AUTH_KEY);
        $this->_welcome['timestamp'] = date("Y-m-d H:i:s",time());
        $this->_welcome['remoteIP'] = $_SERVER['REMOTE_ADDR'];

        if(!empty($_GET['guid'])){
            $this->_welcome['guid'] = $_GET['guid'];
        }

        //$this->_header['auth'] = @$_SERVER['HTTP_AUTH'];
/*
        if(empty($this->_header['auth'])){
            $this->_welcome['result'] = "error";
            $this->_error['message'] = "Auth key missing";
        }elseif( $this->_header['auth'] != $this->_welcome['hash'] ){
            $this->_welcome['result'] = "error";
            $this->_error['message'] = "Auth key mismatch";
        }
*/
        if( !empty($this->_error['message']) ){
            $this->action->actionMethod = 'actionError';
            $this->action->id = 'error';
        }
        else
        {
             $this->_welcome['result'] = "success";
        }

        return true;
    }


    private function _securingSensitiveData($data){

        $key = openssl_random_pseudo_bytes(32,$cstrong);//'test1';

        $method = "aes-256-cbc";    
        $iv_length = openssl_cipher_iv_length($method);
        $iv = openssl_random_pseudo_bytes($iv_length);
        
        $result = openssl_encrypt($data,$method,$key, OPENSSL_RAW_DATA ,$iv); 

        $this->_welcome['secureData']['key'] = bin2hex($key);
        $this->_welcome['secureData']['StrongKey'] = $cstrong;
        $this->_welcome['secureData']['iv_base64_encoded'] = base64_encode($iv);
        //$this->_welcome['secureData']['iv_length'] = $iv_length;

        return base64_encode($result); 
        //return $result; 
    }
	

	/**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        if(empty( $_REQUEST ) || empty( $_REQUEST['guid'] )){
            //print "Nem vart oldal kérése";
            //Yii::$app->end();
            return $this->runAction('error',[
                "result"  => "error",
                "message" => "Hianyzo parameter!",
            ]); 
        }
        
//        $this->_welcome['status'] = $this->readLog4guid( $_REQUEST['guid'] );
        
        return $this->render('index',[
                   'data'  => $this->readLog4guid( $_REQUEST['guid'] )
        ]);

    }

    private function readLog4guid( $guid ){

        $file = Yii::$app->basePath."/log/".$guid.".log";

        if( file_exists( $file ) ){
        
            $data = file_get_contents( $file ); 
        
            return nl2br( $data );
            
        }else{
            return $this->runAction('error',[
                "result"  => "error",
                "message" => "nincs ilyen guid",
            ]);
        }
        
        return true;
    
    }

    public function actionError($result=null,$message=null)
    {
        if( !empty($result)) $this->_welcome['result'] = $result;
        if( !empty($message)) $this->_error['message'] = $message;
        //print_r($this);

        return $this->render('error',[
            "data" =>   array_merge($this->_welcome , $this->_error) ,
        ]);
    }

}