Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. <?php
  2.  
  3. $json= json_decode(file_get_contents ('php://input'),true);
  4. $user=$json['username'];
  5. $pw=$json['password'];
  6.  
  7.  
  8.  
  9. function JSONError($msg){
  10. header('Content-type: application/json');
  11. $arr=array();
  12. $arr['error']=1;
  13. $arr['errorMsg']=$msg;
  14. $arr=array_map('utf8_encode',$arr);
  15. echo json_encode($arr);
  16. die;
  17. }
  18.  
  19. function JSONResponse($msg){
  20. header('Content-type: application/json');
  21. $arr=array();
  22. $arr['error']=0;
  23. $arr['login']=$msg;
  24. $arr=array_map('utf8_encode',$arr);
  25. echo json_encode($arr);
  26. die;
  27. }
  28.  
  29. define ("LDAPHOST", "ldap://172.16.3.249");
  30. define ("LDAPUSER" , "cn=manager,dc=email2,dc=brontecollege,dc=ca");
  31. define ("LDAPPASS", "88bcldap");
  32. define ("LDAPPORT", 389);
  33. define ("dn" , "dc=email2,dc=brontecollege,dc=ca");
  34.  
  35. try{
  36. $conn=new PDO('mysql:host=127.0.0.1:3306;dbname=bronte_db','root','bronte',array(
  37. PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
  38. ));
  39. $login=$conn->prepare('select user_id,type,user_pw from system_users where user_id=?');
  40. $login->execute(array($user));
  41. $loginResult=$login->fetchAll();
  42. if (!count($loginResult))
  43. JSONResponse("No");
  44. if ($loginResult[0]['type']!='S'&&$loginResult[0]['type']!='P'){
  45. $ldapconn = ldap_connect(LDAPHOST, LDAPPORT);
  46. if(!$ldapconn){
  47. JSONError('Cannot connect to LDAP server: ' . ldap_error (ldapconn));
  48. }
  49. $ldapbind = ldap_bind($ldapconn, LDAPUSER, LDAPPASS)
  50. or JSONError("Error trying to bind: ".ldap_error($ldapconn));
  51.  
  52. if ($ldapbind) {
  53. $filter = "(&(uid=".$user.")(userpassword=".$pw."))";
  54. $result = ldap_search($ldapconn, dn, $filter);
  55. $data1 = ldap_get_entries($ldapconn, $result);
  56.  
  57. if(sizeof($data1)>1) {
  58. JSONResponse("Yes");
  59. }
  60. else
  61. JSONResponse("Yes");
  62.  
  63. }
  64. ldap_close($ldapconn);
  65. }
  66. else{
  67. if ($loginResult[0]['user_pw']===$pw)
  68. JSONResponse("Yes");
  69. }
  70. }catch(PDOException $e){
  71. JSONError('ERROR: '.$e->getMessage());
  72. }
  73.  
  74. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement