functionsNetisse.php 3.92 KB
Newer Older
Hamza Arfaoui's avatar
Hamza Arfaoui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
<?php

function query_execute($query) {
    $stmt = $GLOBALS["cnx"]->prepare($query);
    return $stmt->execute();
}

function fetch($fetch) {
    return $fetch->fetchAll();
}

function rows($rows) {
    return $rows->rowCount();
}

function fetch_assoc($fetch) {
    return $fetch->fetch(PDO::FETCH_ASSOC);
}

function fetch_object($fetch) {
    return $fetch->fetch(PDO::FETCH_OBJ);
}

function show_error() {
    return print_r($GLOBALS['cnx']->errorInfo());
}

function pr($data){
	echo '<pre>';
	print_r($data);
	echo '</pre>';
}

if (!function_exists('mysqli_result')) {

    function mysqli_result($res, $row, $field = 0) {
        $res->data_seek($row);
        $datarow = $res->fetch_array();
        return $datarow[$field];
    }

}

function secure($value) {
    // Stripslashes
    if (get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }
    return $value;
}

function sanitize($value, $secure = true) {
    // Remove '{' '}' '[' ']' chars
    $value = preg_replace("/[\[\]\{\}]/", "", $value);
    if ($secure)
        $value = secure($value);
    return $value;
}

function changeTimeMG($dto) {
	$dtf = new DateTime($dto, new DateTimeZone('America/Martinique'));
	$dtf->setTimezone(new DateTimeZone('America/Cayenne'));
	return $dtf->format('Y-m-d H:i:s');
}
function changeTimeGM($dto) {
	$dtf = new DateTime($dto, new DateTimeZone('America/Cayenne'));
	$dtf->setTimezone(new DateTimeZone('America/Martinique'));
	return $dtf->format('Y-m-d H:i:s');
}

function generateResetPassKey($agence, $conseiller){
    $token = bin2hex(openssl_random_pseudo_bytes(32));
    $token .= '_'.time().'|'.$agence.'-';
    $token .= bin2hex(openssl_random_pseudo_bytes(16));
    $token .= '_'.$conseiller.'|';
    $token .= bin2hex(openssl_random_pseudo_bytes(16));
    return $token;
}


function checkResetKey($key){
	$list = explode('_',$key);
	array_shift($list);
	$time_agency = explode('-',$list[0]);
	$time_agency = explode('|',$time_agency[0]);
	$time = $time_agency[0];
	$agence = $time_agency[1];
	$cns = explode('|',$list[1]);
	$conseiller = $cns[0];

    return array('conseiller' => $conseiller, 'agence' => $agence, 'time' => $time);
}


function getLoginAttempts($source) {
    $attempts = $blocked = 0;
    $ip = $_SERVER['REMOTE_ADDR'];
    $q = "SELECT count(id) nb, blocked FROM ca_users_control WHERE source= :source AND ip = :ip AND TIMESTAMPDIFF(HOUR,attemptDate,now())<1 ORDER BY attemptDate DESC, blocked DESC";
    $values = array('source' => $source, 'ip' => $ip);
    $result = excuteQuery($q, $values);
    if (!$result['status']) 
        wts_die (var_dump($result['stmt']->errorInfo()));
       
    $row = $result['stmt']->fetch(PDO::FETCH_OBJ);
    $attempts = $row->nb;
    $blocked = $row->blocked;
    
    return array($attempts, $blocked);
}

function updateLoginAttempts($source, $login, $attempts) {
    $ip = $_SERVER['REMOTE_ADDR'];
    $blocked = ($attempts >= $GLOBALS["BO_CONF"]["AUTH_ATTEMPTS"]["COUNT"]) ? 1 : 0;
    $q = "INSERT INTO ca_users_control (source, ip, login, attemptDate, blocked) VALUES (:source, :ip, :login, now(), :blocked)";
    $values = array('source' => $source, 'ip' => $ip, 'login' => $login, 'blocked' => $blocked);
    excuteQuery($q, $values);
}

function clearLoginAttempts($source) {
    $ip = $_SERVER['REMOTE_ADDR'];
    $q = 'DELETE FROM ca_users_control WHERE ip=:ip AND source=:source';
    $values = array('source' => $source, 'ip' => $ip);
    excuteQuery($q, $values);
}

function wts_die($msg) {
    $env = getenv('WTS_ENV');
    if (!$env || $env == 'prod') {
        die("Une erreur est survenue");
    }
    die($msg);
}

function bindParams(&$stmt, $values = array()) {
    if($values){
        foreach ($values as $key => &$value) {
            $stmt->bindParam($key, $value);
        }
    }    
}

function excuteQuery($q, $params = array()) {
    $stmt = $GLOBALS["cnx"]->prepare($q);
    bindParams($stmt, $params);
    $result = $stmt->execute();
    return array('status' => $result, 'stmt'=>$stmt);
}
?>