Source: ews_login.phps - download
<?php
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ews_login.phps 0.1
* by daif alotaibi (http://daif.net)
* daif@daif.net
*
* Link:
* http://daif.net/script/ews_login.phps
*
* Examples:
* $ews_url = 'https://mail.domain.ltd/EWS/exchange.asmx';
* $ews_usr = '';
* $ews_pwd = '';
* if(ews_login($ews_usr, $ews_pwd)) {
* print 'Welcome, $ews_usr';
* } else {
* print 'Wrong username or password';
* }
*
*/
$ews_url = 'https://mail.domain.ltd/EWS/exchange.asmx';
$ews_usr = 'user';
$ews_pwd = '1234';
if(ews_login($ews_usr, $ews_pwd, $ews_url)) {
print 'Welcome, '.$ews_usr;
} else {
print 'Wrong username or password';
}
// Exchange web service function
function ews_login($ews_usr, $ews_pwd, $ews_url){
if(strpos($ews_usr, '@')) {
list($ews_usr, ) = explode('@', $ews_usr, 2);
}
// create soap request
$envelope = '<?xml version="1.0" encoding="utf-8"?>';
$envelope .= '<soap:Envelope xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ';
$envelope .= 'xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">';
$envelope .= ' <soap:Header>';
$envelope .= ' <t:RequestServerVersion Version="Exchange2013" />';
$envelope .= ' </soap:Header>';
$envelope .= ' <soap:Body>';
$envelope .= ' <m:FindPeople>';
$envelope .= ' <m:PersonaShape>';
$envelope .= ' <t:BaseShape>IdOnly</t:BaseShape>';
$envelope .= ' </m:PersonaShape>';
$envelope .= ' <m:IndexedPageItemView BasePoint="Beginning" MaxEntriesReturned="1" Offset="0" />';
$envelope .= ' <m:ParentFolderId>';
$envelope .= ' <t:DistinguishedFolderId Id="directory" />';
$envelope .= ' </m:ParentFolderId>';
$envelope .= ' <m:QueryString>'.trim(strip_tags($ews_usr)).'</m:QueryString>';
$envelope .= ' </m:FindPeople>';
$envelope .= ' </soap:Body>';
$envelope .= '</soap:Envelope>';
// header
$header = [
"Method: POST",
"Content-type: text/xml",
"Connection: KEEP-Alive",
"User-Agent: PHP-SOAP-CURL"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ews_url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 900);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $envelope);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, $ews_usr.':'.$ews_pwd);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$response = curl_exec($ch);
if($response && preg_match('/NoError/', $response)) {
return TRUE;
}
return FALSE;
}
?>