<?php

namespace Intgrtn\Lib;

/**
 * @author heminei
 */
class Common
{

    public static function request($name, $default = NULL)
    {
        if (isset($_REQUEST[$name]) AND $_REQUEST[$name] != "") {
            return $_REQUEST[$name];
        }
        return $default;
    }

    public static function post($name, $default = NULL)
    {
        if (isset($_POST[$name]) AND $_POST[$name] != "") {
            return $_POST[$name];
        }
        return $default;
    }

    public static function get($name, $default = NULL)
    {
        if (isset($_GET[$name]) && $_GET[$name] != "") {
            return $_GET[$name];
        }
        return $default;
    }

    public static function cookie($name, $default = NULL)
    {
        if (isset($_COOKIE[$name]) AND $_COOKIE[$name] != "") {
            return $_COOKIE[$name];
        }
        return $default;
    }

    public static function session($name, $default = NULL)
    {
        if (isset($_SESSION[$name]) AND $_SESSION[$name] != "") {
            return $_SESSION[$name];
        }
        return $default;
    }

    public static function getClientIP()
    {
        foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
            if (array_key_exists($key, $_SERVER) === true) {
                foreach (explode(',', $_SERVER[$key]) as $ip) {
                    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
                        return $ip;
                    }
                }
                foreach (explode(',', $_SERVER[$key]) as $ip) {
                    if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                        return $ip;
                    }
                }
            }
        }
    }

}
