Completed
Push — 1.0 ( 81da80...5260d2 )
by joanhey
01:27
created

Input::isMobile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://wiki.kumbiaphp.com/Licencia
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * Clase para manejar los datos del request
16
 *
17
 * @category   Kumbia
18
 * @package    Input
19
 * @copyright  Copyright (c) 2005 - 2017 Kumbia Team (http://www.kumbiaphp.com)
20
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
21
 */
22
class Input
23
{
24
    /**
25
     * Verifica o obtiene el metodo de la peticion
26
     *
27
     * @param string $method
28
     * @return mixed
29
     */
30
    public static function is($method = '')
31
    {
32
        if($method){
33
            return $method == $_SERVER['REQUEST_METHOD'];
34
        }
35
        return $_SERVER['REQUEST_METHOD'];
36
    }
37
38
    /**
39
     * Indica si el request es AJAX
40
     *
41
     * @return boolean
42
     */
43
    public static function isAjax()
44
    {
45
        return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
46
    }    
47
    
48
    /**
49
     * Detecta si el Agente de Usuario (User Agent) es un móvil
50
     *
51
     * @return boolean
52
     */
53
    public static function isMobile()
54
    {  
55
        return strpos(mb_strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') ? TRUE : FALSE;
56
    }
57
58
    /**
59
     * Obtiene un valor del arreglo $_POST
60
     *
61
     * @param string $var
62
     * @return mixed
63
     */
64
    public static function post($var = '')
65
    {
66
        return self::getFilter($_POST, $var);
67
    }
68
69
    /**
70
     * Obtiene un valor del arreglo $_GET, aplica el filtro FILTER_SANITIZE_STRING
71
     * por defecto
72
     *
73
     * @param string $var
74
     * @return mixed
75
     */
76
    public static function get($var = '')
77
    {
78
        return self::getFilter($_GET, $var);
79
    }
80
81
    /**
82
     * Obtiene un valor del arreglo $_REQUEST
83
     *
84
     * @param string $var
85
     * @return mixed
86
     */
87
    public static function request($var = '')
88
    {
89
        return self::getFilter($_REQUEST, $var);
90
    }
91
92
93
    /**
94
     * Obtiene un valor del arreglo $_SERVER
95
     *
96
     * @param string $var
97
     * @return mixed
98
     */
99
    public static function server($var = '')
100
    {
101
        return self::getFilter($_SERVER, $var);
102
    }
103
104
    /**
105
     * Verifica si existe el elemento indicado en $_POST
106
     *
107
     * @param string $var elemento a verificar
108
     * @return boolean
109
     */
110
    public static function hasPost($var)
111
    {
112
        return (bool) self::post($var);
113
    }
114
115
    /**
116
     * Verifica si existe el elemento indicado en $_GET
117
     *
118
     * @param string $var elemento a verificar
119
     * @return boolean
120
     */
121
    public static function hasGet($var)
122
    {
123
        return (bool) self::get($var);
124
    }
125
126
    /**
127
     * Verifica si existe el elemento indicado en $_REQUEST
128
     *
129
     * @param string $var elemento a verificar
130
     * @return boolean
131
     */
132
    public static function hasRequest($var)
133
    {
134
        return (bool) self::request($var);
135
    }
136
137
    /**
138
     * Elimina elemento indicado en $_POST
139
     *
140
     * @param string $var elemento a verificar
141
     * @return boolean|null
142
     */
143
    public static function delete($var = '')
144
    {
145
        if($var){
146
            $_POST[$var] = array();
147
            return;
148
        }
149
        $_POST = array();
150
    }
151
152
    /**
153
    * Permite Obtener el Agente de Usuario (User Agent)
154
    * @return String
155
    */
156
    public static function userAgent(){
157
        return $_SERVER['HTTP_USER_AGENT'];
158
    }
159
160
    /**
161
    * Permite obtene la IP del cliente, aún cuando usa proxy
162
    * @return String
163
    */
164
    public static function ip(){
165
        if (!empty($_SERVER['HTTP_CLIENT_IP'])){
166
            return $_SERVER['HTTP_CLIENT_IP'];
167
        }
168
        if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
169
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
170
        }
171
        return $_SERVER['REMOTE_ADDR'];
172
    }
173
174
175
    /**
176
     * Obtiene y filtra un valor del arreglo $_REQUEST
177
     * Por defecto, usa SANITIZE
178
     *
179
     * @param string $var
180
     * @return mixed
181
     */
182
    public static function filter($var)
0 ignored issues
show
Unused Code introduced by
The parameter $var is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
183
    {
184
        //TODO
185
    }
186
187
    /**
188
     * Devuelve el valor dentro de un array con clave en formato uno.dos.tres
189
     * @param Array array que contiene la variable
190
     * @param string $str clave a usar
191
     * @return mixed
192
     */
193
    protected static function getFilter(Array $var, $str)
194
    {
195
        if(empty($str)) {
196
            return filter_var_array($var);
197
        }   
198
        $arr = explode('.', $str);
199
        $value = $var;
200
        foreach ($arr as $key) {
201
            if(isset($value[$key])){
202
                $value = $value[$key];
203
            } else {
204
                $value = NULL;
205
                break;
206
            }
207
        }
208
        return is_array($value) ? filter_var_array($value) : filter_var($value);
209
    }
210
}
211