<?php

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

trait CommonFunction
{
    private static $fullUrl;

    public static function getHost()
    {
        $host = $_SERVER['HTTP_HOST'];
        $protocol = $_SERVER['PROTOCOL'] = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http';
        return $protocol . '://' . $host;
    }

    public static function setFullUrl($fullUrl = null)
    {
        self::$fullUrl = $fullUrl ?: self::getFullUrl();
    }

    public static function getFullUrl()
    {
        return self::$fullUrl ?: self::getHost() . $_SERVER['REQUEST_URI'];
    }

    public function log()
    {
        log_message('debug', self::getFullUrl());

        /**
         * @var IncomingRequest $request
         */
        if ($this->input->get() !== null) {
            ob_start();
            print_r($this->input->get());
            log_message('debug', 'Request GET Parameters: ' . ob_get_clean());
        }
        /**
         * @var IncomingRequest $request
         */
        if ($this->input->post() !== null) {
            ob_start();
            print_r($this->input->post());
            log_message('debug', 'Request POST Parameters: ' . ob_get_clean());
        }
    }

    public function getUserId()
    {
        return $this->session->userdata('userid');
    }
}
