mYLA', '0{-f_Z/)J?Dr-=B2G=+U-+&ZTvN2m[+){7nO&rd{t6+|8BW!(+&Lx!U{gF&t.m(8', 'TY@V>x3x6= |?K|.dBEt2U;9su-h4kS_-4~+AuDvV4<0.:}^o:l;*&WC6P:<58?t', '?GOHK0|EpH`X1.1DZ,-r*`?(7+<4rbG?;W*ReX+<#CUuGiQ,G^c[SJh!Q#@aHB #', 'JdF?Zl=7tE{|+VDh%SzDVj[$nV$Emkt!T9&lhUM7Ty/yRadrv,T(S7CEk rIsx,$', 'y:XeG#<+>Jb+cqB*^doW[2|N]ju8l<4?$ek?M?0aFaY>W&TJw rNCz+L7+ZN;#f=', 'E|ngUL],O).^DqOWo~Jzy', 'i2El=I~} en rélité la case de tableau à utiliser @ $salt = (int) $keyset[1]; // on utiulise l'heure comme référence $time = strftime('%Y%m%d%H'); // connection BDD $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbname = 'apib3'; @ $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); // on protège les cractères sensible de la clef interne $mysql_safe_salt = $mysqli->real_escape_string($keyring[$salt]); // on protège les cractères sensible de la clef fournie $mysql_safe_key = $mysqli->real_escape_string($coded_key); //on fait le md5 + la comparaison dans le SQL directement $sql = "SELECT * FROM `apikey` WHERE MD5(CONCAT(`api_key`,'".$mysql_safe_salt."','".$time."')) = '".$mysql_safe_key."'"; //die($sql); $result = $mysqli->query($sql); if ($result->num_rows > 0) { $mysqli->close(); return true; } $mysqli->close(); return false; } function is_valid_api_key($key) { $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbname = 'apib3'; @ $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); $sql = "SELECT * FROM `apikey` WHERE `api_key` LIKE '".$key."'"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { $mysqli->close(); return true; } $mysqli->close(); return false; } //header('Content-type: text/plain;charset=utf-8'); header('Content-type: application/json;charset=utf-8'); $vars=array(); foreach(array('REQUEST_METHOD', 'REDIRECT_URL', 'QUERY_STRING', 'REQUEST_URI', 'REQUEST_TIME_FLOAT', 'REQUEST_TIME') as $key) { if(isset($_SERVER[$key])) { $vars[$key] = $_SERVER[$key]; } else { $vars[$key] = ""; } } //echo json_encode($vars); $query = preg_replace("/^\/api/","",$vars['REDIRECT_URL']); //echo $query; switch($query) { case "/toto/" : case "/test/" : header('HTTP/1.1 200 OK'); echo json_encode( (object) array('status'=>'active') ); break; case '/checksaltedkey/' : if($vars['REQUEST_METHOD'] == "GET") { if(isset($_GET['key'])) { header('HTTP/1.1 200 OK'); echo json_encode( (object) array( 'valid'=>is_valid_api_key2($_GET['key']), 'ip'=>get_client_ip() )); } else { header('HTTP/1.1 403 Forbidden'); echo json_encode( (object) array('error'=>'Please provide a valid key') ); } } else { header('HTTP/1.1 405 Method Not Allowed'); echo json_encode( (object) array('error'=>'/checksaltedkey/ requires GET method') ); } break; case '/users/' : if($vars['REQUEST_METHOD'] == "GET") { if(isset($_GET['key']) && is_valid_api_key($_GET['key'])) { $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbname = 'apib3'; //si je préfixe avec @je n'aurais pas le message d'erreur. @ $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); $sql = "SELECT COUNT(*) AS nbusers FROM `user`"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_array(MYSQLI_ASSOC); header('HTTP/1.1 200 OK'); echo json_encode( (object) array('users'=>$row['nbusers'])); $result->free_result(); $mysqli->close(); } } else { header('HTTP/1.1 403 Forbidden'); echo json_encode( (object) array('error'=>'Please provide a valid key') ); } } else { header('HTTP/1.1 405 Method Not Allowed'); echo json_encode( (object) array('error'=>'/users/ requires GET method') ); } break; case "/key/" : case "/key" : if($vars['REQUEST_METHOD'] == "POST") { if(!empty($_POST['user']) && !empty($_POST['password'])) { $uid = null; $user = $_POST['user']; $password = md5($_POST['password'].AUTH_KEY); $response = array('user'=>$user,'md5'=>$password); //essayer de se connecter à ùysql (nb sur un xampp/wammp souvent l'utilisateur root avec mot de passe vide) //https://www.tutorialspoint.com/mysqli/mysqli_connection.htm // na pas oublier de convertir les → en -> $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbname = 'apib3'; //si je préfixe avec @je n'aurais pas le message d'erreur. @ $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); if($mysqli->connect_errno ) { header('HTTP/1.1 500 Internal Server Error'); echo json_encode( (object) array('error'=>"Connect failed: ".$mysqli->connect_error )); exit(); } else { $response['connection'] = 'successful'; // vérifier si l'utilisateur existe $sql = "SELECT * FROM `user` WHERE `user` LIKE '".$user."' AND `password` ='".$password."'"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_array(MYSQLI_ASSOC); $response['uid'] = $row['id']; $result->free_result(); } else { $sql ="INSERT INTO `user` VALUES (NULL,'".$user."','".$password."')"; $result = $mysqli->query($sql); if($mysqli->connect_errno ) { header('HTTP/1.1 500 Internal Server Error'); echo json_encode( (object) array('error'=>"Insert failed: ".$mysqli->connect_error )); exit(); } else { $uid = $mysqli->insert_id; $response['insert'] = 'successful user id is '.$uid; $response['uid'] = $uid; } } $apikey = md5(array_rand($keyring).$response['uid'].microtime(true)); $sql ="INSERT INTO `apikey` VALUES ('".$response['uid']."','".$apikey."')"; $result = $mysqli->query($sql); $response = array('key'=>$apikey); } $mysqli->close(); header('HTTP/1.1 200 OK'); echo json_encode( (object) $response ); } else { header('HTTP/1.1 403 Forbidden'); echo json_encode( (object) array('error'=>'PLease provide a valid user and matching password') ); } } else { header('HTTP/1.1 405 Method Not Allowed'); echo json_encode( (object) array('error'=>'/key/ requires POST method') ); } break; default : // header('HTTP/1.1 404 Not Found'); header('HTTP/1.1 501 Not Implemented'); echo json_encode( (object) array('error'=>'This path has not been implemented') ); break; }