|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
15
|
|
|
* |
|
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
17
|
|
|
* and is licensed under the MIT license. For more information, see |
|
18
|
|
|
* @category |
|
19
|
|
|
* @package sistema/ayudantes |
|
20
|
|
|
* @copyright Copyright (c) 2006 - 2014 webcol.net (http://www.webcol.net/calima) |
|
21
|
|
|
* @license https://github.com/webcol/Calima/blob/master/LICENSE MIT |
|
22
|
|
|
* @version ##BETA 1.0##, ##2014 - 2015## |
|
23
|
|
|
* <http://www.calimaframework.com>. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace Sistema\Ayudantes; |
|
27
|
|
|
|
|
28
|
|
|
class CFPHPSeguridad { |
|
29
|
|
|
|
|
30
|
|
|
//Seguridad |
|
31
|
|
|
|
|
32
|
|
|
//xss funciones de mitigacion |
|
33
|
|
|
function xsseguro($texto,$encoding='UTF-8'){ |
|
34
|
|
|
return htmlspecialchars($texto,ENT_QUOTES | ENT_HTML401,$encoding); |
|
35
|
|
|
} |
|
36
|
|
|
function xecho($texto){ |
|
37
|
|
|
echo xsseguro($texto); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function filtrarTexto($texto){ |
|
41
|
|
|
return strip_tags($texto); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
public function filtrarCaracteresEspeciales($texto){ |
|
47
|
|
|
return htmlspecialchars($texto, ENT_QUOTES); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
// Genracion de Cadenas Aleatorias |
|
52
|
|
|
function generarCadenaAleatoria($longitud = 10) { |
|
|
|
|
|
|
53
|
|
|
$caracteres = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
54
|
|
|
$longitudCaracteres = strlen($caracteres); |
|
55
|
|
|
$cadenaAleatoria = ''; |
|
56
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
|
|
|
|
|
57
|
|
|
$cadenaAleatoria .= $caracteres[rand(0, $longitudCaracteres - 1)]; |
|
58
|
|
|
} |
|
59
|
|
|
return $cadenaAleatoria; |
|
60
|
|
|
} |
|
61
|
|
|
// encriptacion |
|
62
|
|
|
function cifrado($clave){ |
|
63
|
|
|
$cfi=sha1(md5($clave)); |
|
64
|
|
|
return $clave=Cf_KEY_MD5.$cfi; |
|
65
|
|
|
} |
|
66
|
|
|
// Protecion CSRF |
|
67
|
|
View Code Duplication |
public function generoTokenDeFormulario($formulario) { |
|
68
|
|
|
$secreta = Cf_CSRF_SECRET.$this->generarCadenaAleatoria(); |
|
69
|
|
|
$sid = session_id(); |
|
70
|
|
|
$token = md5($secreta.$sid.$formulario); |
|
71
|
|
|
return $token; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
View Code Duplication |
public function verificoTokenDeFormulario($formulario, $token) { |
|
75
|
|
|
$secreta = Cf_CSRF_SECRET.$this->generarCadenaAleatoria(); |
|
76
|
|
|
$sid = session_id(); |
|
77
|
|
|
$correcta = md5($secreta.$sid.$formulario); |
|
78
|
|
|
return ($token == $correcta); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
function obtenerDireccionIP(){ |
|
|
|
|
|
|
82
|
|
|
if (!empty($_SERVER ['HTTP_CLIENT_IP'] )) |
|
83
|
|
|
$ip=$_SERVER ['HTTP_CLIENT_IP']; |
|
84
|
|
|
elseif (!empty($_SERVER ['HTTP_X_FORWARDED_FOR'] )) |
|
85
|
|
|
$ip=$_SERVER ['HTTP_X_FORWARDED_FOR']; |
|
86
|
|
|
else |
|
87
|
|
|
$ip=$_SERVER ['REMOTE_ADDR']; |
|
88
|
|
|
|
|
89
|
|
|
return $ip; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
function restringirIp($ip){ |
|
93
|
|
|
$ipCliente = $this->obtenerDireccionIP(); |
|
94
|
|
|
|
|
95
|
|
|
if($ipCliente == $ip){ |
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
else{ |
|
99
|
|
|
header('location: http://www.tusitioweb/redireccion'); |
|
100
|
|
|
exit; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
function restringirConjuntoIps($ips){ |
|
105
|
|
|
$ipCliente = obtenerDireccionIP(); |
|
106
|
|
|
|
|
107
|
|
|
if (in_array($ipCliente,$ips)){ |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
else{ |
|
111
|
|
|
header('location: http://direccion_envio_salida'); |
|
112
|
|
|
exit; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
function restringirRango(){ |
|
117
|
|
|
$ipCliente = obtenerDireccionIP(); |
|
118
|
|
|
|
|
119
|
|
|
if(substr($ipCliente, 0, 8 ) == "150.214."){ |
|
120
|
|
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
else{ |
|
123
|
|
|
header('location: http://direccion_envio_salida'); |
|
124
|
|
|
exit; |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* |
|
131
|
|
|
* @strip injection chars from email headers |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $string |
|
|
|
|
|
|
134
|
|
|
* |
|
135
|
|
|
* return string |
|
136
|
|
|
* |
|
137
|
|
|
*/ |
|
138
|
|
|
function emailSeguro($cadena) { |
|
139
|
|
|
return preg_replace( '((?:\n|\r|\t|%0A|%0D|%08|%09)+)i' , '', $cadena ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/*** example usage |
|
143
|
|
|
$from = "[email protected] |
|
144
|
|
|
Cc:[email protected]"; |
|
145
|
|
|
|
|
146
|
|
|
if(strlen($from) < 100) |
|
147
|
|
|
{ |
|
148
|
|
|
$from = emailSeguro($from); |
|
149
|
|
|
}***/ |
|
150
|
|
|
|
|
151
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.