generic.inc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. require_once(dirname(__FILE__).'/generic_conf.inc');
  3. function generic_authenticate()
  4. {
  5. global $pluginconfig;
  6. if($_SERVER['PHP_AUTH_USER']!='' && $_SERVER['PHP_AUTH_PW']!='')
  7. {
  8. preg_match('#(https?)://([^/:]+)((?::[0-9]+)?)#i', $pluginconfig['base_url'], $matches);
  9. $hostname_clean=$matches[2];
  10. if($matches[1]=='https')
  11. $hostname='ssl://'.$matches[2];
  12. else
  13. $hostname=$matches[2];
  14. if($matches[3]=='')
  15. {
  16. if($matches[1]=='http')
  17. $port=80;
  18. else if($matches[1]=='https')
  19. $port=443;
  20. }
  21. else
  22. $port=substr($matches[3],1);
  23. $fp=fsockopen($hostname, $port, $errno, $errstr, $pluginconfig['timeout']);
  24. if(!$fp)
  25. {
  26. echo "$errstr ($errno)<br />\n";
  27. return -2;
  28. }
  29. else
  30. {
  31. $request="<?xml version=\"1.0\" encoding=\"utf-8\"?><A:propfind xmlns:A=\"DAV:\"><A:prop><A:current-user-principal/></A:prop></A:propfind>";
  32. $out="PROPFIND ".$pluginconfig['request']." HTTP/1.1\r\n";
  33. $out.="Host: $hostname_clean\r\n";
  34. $out.="Authorization: Basic ".base64_encode($_SERVER['PHP_AUTH_USER'].':'.$_SERVER['PHP_AUTH_PW'])."\r\n";
  35. $out.="Depth: 0\r\n";
  36. $out.="Content-Type: text/xml; charset=\"utf-8\"\r\n";
  37. $out.="Content-Length:". strlen($request)."\r\n\r\n";
  38. $out.=$request;
  39. fwrite($fp, $out);
  40. $result='';
  41. if(!feof($fp))
  42. $result.=fgets($fp);
  43. fclose($fp);
  44. if(strpos($result, 'HTTP/1.1 207')===0)
  45. return 1; // auth successful
  46. else
  47. return -1; // auth unsuccessful
  48. }
  49. }
  50. return 0; // empty username or password
  51. }
  52. ?>