Utils.php 1.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php

class Utils
{

/*
Cette fonction définit un mot de passe de 12 caractères
 */
    public static function generate_password()
    {
        $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
        $pass = "";
        $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
        for ($i=0;$i<12;$i++) {
            $n = rand(0, $alphaLength);
            $pass .= $alphabet[$n];
        }
        return $pass; //turn the array into a string
    }


22 23 24
    /*
    Cette fonction cherche à nettoyer un mauvais encodage en utf-8.
     */
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    public static function clean_encoding($string)
    {
        $bad_encoding = array("é","à ","è","ç","ë","ô","î","ù");
        $good_encoding = array("é","à","è","ç","ë","ô","î","ù");

        $string = str_replace($bad_encoding, $good_encoding, $string);
        return $string;
    }

    /* AddSlashes contextuel : ne fait addslashes que si magic-quotes est OFF */
    public static function asl($str)
    {
        return addslashes($str);
    }

    public static function sanitize_piplomes($str)
    {
        // - traduire $c[nom] en latin1
        $str = utf8_decode($str);
        // - supprimer les \, {, }, ~,
        $str = preg_replace('/[\\\{\}\~]/', '', $str);
        // - proteger les caracteres foireux ( & => \&, _ => \_, $ => \$ )
        $str = preg_replace('/([&_\$])/', '\\\$1', $str);

        return $str=substr($str, 0, 100);
    }
};