<?php

namespace api\components;

use yii;


class MyGlobalClass extends \yii\base\Component
{

    protected $requestData;
    public function init()
    {
        // TODO: Decrypt the code and push it back into request(POST / GET) variable, so that it will be available in further actions.
        $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

        if ($uriSegments[1] != 'uploads') {
            $params = [];

            $encString = Yii::$app->request->post('encData');

            $cipher = 'AES-128-CBC';
            $options = 0;
            $encryption_iv  = Yii::$app->params['encryption_iv'];
            $encryption_key = Yii::$app->params['encryption_key'];
            $decrypted = openssl_decrypt($encString, $cipher, $encryption_key, $options, $encryption_iv);

            $arr = explode('&', $decrypted);
            if (count($arr) > 1) {
                foreach ($arr as $data) {
                    $decrypted = explode('=', $data);
                    $params[$decrypted[0]] = $decrypted[1];
                }
            } else {
                $decrypted = explode('=', $decrypted);
                if (count($decrypted) > 1) {
                    $params[$decrypted[0]] = $decrypted[1];
                } else {
                    $params = $decrypted;
                }
            }

            Yii::$app->request->setBodyParams($params);
        }
        parent::init(); // Always add in last
    }
}
