elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Únete al Grupo Steam elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  [Aporte] UTF-8 Sanitize Primitivo
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Aporte] UTF-8 Sanitize Primitivo  (Leído 1,177 veces)
z3nth10n


Desconectado Desconectado

Mensajes: 1.583


"Jack of all trades, master of none." - Zenthion


Ver Perfil WWW
[Aporte] UTF-8 Sanitize Primitivo
« en: 25 Febrero 2014, 18:35 pm »

Hola buenas.

Pues acabo de terminar de hacer un PHP super básico, que básicamente sirve para aquella gente tan despistada como yo, que no haya usado SET NAMES utf8 antes, haya cambiado de idea, y haya jodido todo.

Básicamente, si tenéis algún carácter que no sea UTF-8 y al cambiar la cotejación de PHP o MySQL se haya jodido todo, aquí podréis volver todo UTF-8.

Aquí el código:

Código
  1. <?php
  2.  
  3. //Functions
  4.  
  5. function strposa($haystack, $needles=array(), $offset=0) {
  6.        $chr = array();
  7.        foreach($needles as $needle) {
  8.                $res = strpos($haystack, $needle, $offset);
  9.                if ($res !== false) $chr[$needle] = $res;
  10.        }
  11.        if(empty($chr)) return false;
  12.        return min($chr);
  13. }
  14.  
  15. //Valores de conexion
  16. $localhost = '';
  17. $dbuser =  '';
  18. $dbpass = '';
  19. $dbname = '';
  20.  
  21. //Conexión + UTF-8
  22. $conn = mysqli_connect($localhost, $dbuser, $dbpass, $dbname) or die('Could not connect: ' . mysqli_error());
  23.  
  24. mysqli_query($conn, "SET NAMES utf8");
  25.  
  26. echo '<html>
  27. <head>
  28. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  29. </head>
  30. <body>';
  31.  
  32. $unwanted_array = array(    'Š' , 'š' , 'Ž' , 'ž' , 'À' , 'Á' , '' , 'Ã' , 'Ä' , 'Å' , 'Æ' , 'Ç' , 'È' , 'É' ,
  33.                            'Ê' , 'Ë' , 'Ì' , 'Í' , 'Î' , 'Ï' , 'Ñ' , 'Ò' , 'Ó' , 'Ô' , 'Õ' , 'Ö' , 'Ø' , 'Ù' ,
  34.                            'Ú' , 'Û' , 'Ü' , 'Ý' , 'Þ' , 'ß' , 'à' , 'á' , 'â' , 'ã' , 'ä' , 'å' , 'æ' , 'ç' ,
  35.                            'è' , 'é' , 'ê' , 'ë' , 'ì' , 'í' , 'î' , 'ï' , 'ð' , 'ñ' , 'ò' , 'ó' , 'ô' , 'õ' ,
  36.                            'ö' , 'ø' , 'ù' , 'ú' , 'û' , 'ý' , 'ý' , 'þ' , 'ÿ'  );
  37.  
  38. $sql = "SHOW TABLES FROM toomanyd_database";
  39. $resultado = mysqli_query($conn, $sql);
  40.  
  41. if (!$resultado) {
  42.    echo "Error de BD, no se pudieron listar las tablas\n";
  43.    echo 'Error MySQL: ' . mysqli_error();
  44.    exit;
  45. }
  46.  
  47. while ($fila = mysqli_fetch_row($resultado)) {
  48. $curfila = $fila[0];
  49.    $resultado1 = mysqli_query($conn, "SHOW COLUMNS FROM `$curfila`");
  50. if (!$resultado1) {
  51.    echo 'No se pudo ejecutar la consulta: ' . mysqli_error();
  52.    exit;
  53. }
  54.  
  55. $beta_number = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `$curfila`"));
  56. $anum = mysqli_fetch_array(mysqli_query($conn, "SELECT MAX(id) as last FROM `$curfila`"));
  57. $number = $anum['last'];
  58. if (mysqli_num_rows($resultado1) > 0 && $number > 0) {
  59.    while ($fila = mysqli_fetch_assoc($resultado1)) {
  60. for($i = 1; $i <= $number; $i++)
  61. {
  62. $field = $fila['Field'];
  63. $acon = mysqli_fetch_array(mysqli_query($conn, "SELECT `$field` as content FROM `$curfila` WHERE id='$i'"));
  64. $content = $acon['content'];
  65. if(!is_numeric($content)) {
  66. if($content != null) {
  67. if(ip2long($content) === false) {
  68. if(mb_detect_encoding($content) === "UTF-8") {
  69. $sanitized = utf8_decode($content);
  70. if($sanitized != $content && strposa($sanitized, $unwanted_array, 1)) {
  71. mysqli_query($conn, "UPDATE `$curfila` SET `$field`='$sanitized' WHERE id='$i'");
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78.    }
  79. }
  80. }
  81.  
  82. echo '</body>
  83. </html>';
  84.  
  85.  
  86. ?>

Puede que haya errores puesto que uso mysql, y he hecho una conversión rapida a mysqli... :-(

NOTA: Haced una copia de seguridad de vuestra base de datos.
NOTA2: Debéis guardar el archivo en UTF-8 (no es necesario, pero una recomendación)

Un saludo.


« Última modificación: 25 Febrero 2014, 22:33 pm por Ikillnukes » En línea


Interesados hablad por Discord.
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
pequeño aporte(proxy),pero aporte al fin.:D
Programación Visual Basic
Tengu 0 2,419 Último mensaje 22 Julio 2007, 17:33 pm
por Tengu
Aporte
Ingeniería Inversa
R6ID 0 2,200 Último mensaje 28 Febrero 2008, 11:35 am
por R6ID
Chat Primitivo en C/C++
Programación C/C++
SpesMod 2 3,271 Último mensaje 13 Diciembre 2010, 20:16 pm
por Garfield07
Aporte c++
Programación C/C++
Stakewinner00 8 4,768 Último mensaje 2 Septiembre 2012, 08:33 am
por Stakewinner00
[Aporte] Aporte
Programación C/C++
Stakewinner00 3 2,514 Último mensaje 14 Noviembre 2012, 16:39 pm
por Stakewinner00
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines