<?php
defined('BASEPATH') or exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{


    const KEY = 'ctnews^L3wvaMt8GLAatZLE';

    public function __construct()
    {
        parent::__construct();
        // if (!$this->validate()) {
        //     die('Invalid Access');
        // }
    }

    protected function response($code, $message, $data)
    {
        $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode([
                'code' => $code,
                'message' => $message,
                'data' => $data
            ]));
    }

    public function validate()
    {
        $headers = $this->input->request_headers();
        if (!empty($headers['ctaccesstoken'])) {
            $this->load->library('encryption');
            $this->encryption->initialize(
                array(
                    'cipher' => 'aes-256',
                    'mode' => 'ctr',
                    'key' => '2UFdhHgY5n5Smd5RnD3H8Z6rKJ8j3zra'
                )
            );
            $headers = $this->input->request_headers();
            $token = $this->encryption->decrypt($headers['ctaccesstoken']);

            $token = explode('.T', $token);

            if ($token[0] == self::KEY) {
                if (base64_decode($token[1]) > time() - 5) {
                    return true;
                }
            }
        }
    }
}
