Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- class exchangeMail {
- # Mail: New #
- # Mail: Attach #
- # Mail: Send #
- # Calendar: New #
- # Calendar: Edit
- # Calendar: Delete
- # Calendar: Send #
- public $log;
- public $method;
- public $headers;
- public $url;
- public $status;
- public $xmlrequest;
- public $timeout=60;
- public $domain='domain.com'; # This should be domain.com, no http:// or www.
- public $server='http://www.domain.com/Exchange'; # Full path to Exchange folder on server
- public $port=80; # Change to 443 for https
- public $protocol='http://'; # Change to https:// for port 443
- public $username;
- public $encrypted;
- public $unreadEmails=0;
- public $header;
- public $body;
- public $index;
- public $data;
- public $stack;
- public $XMLParser;
- public function __construct($mailbox) {
- $this->log.="New HTTP Request.<br />\n"; $this->reset(0); $this->headers=array('Content-Type'=>'text/xml; charset="UTF-8"','Depth'=>0,'Translate'=>'f');
- $this->username='[enter username here]'; $this->mailbox=$mailbox; $this->encrypted=base64_decode('[enter username:password in base64 here]');
- // http://schemas.microsoft.com/exchange/outlookmessageclass
- // http://www.outlook-tips.net/howto/commandlines.htm
- // http://msdn.microsoft.com/en-us/library/aa486269%28v=EXCHG.65%29.aspx (Exchange Schema)
- $this->xmlrequest="
- <?xml version=\"1.0\"?>
- <dav:propertyupdate
- xmlns:dav=\"DAV:\"
- xmlns:-xml=\"xml:\"
- xmlns:exchange=\"http://schemas.microsoft.com/exchange/\"
- xmlns:security=\"http://schemas.microsoft.com/exchange/security/\"
- xmlns:events=\"http://schemas.microsoft.com/exchange/events/\"
- xmlns:mapi=\"http://schemas.microsoft.com/mapi/\"
- xmlns:proptag=\"http://schemas.microsoft.com/mapi/proptag/\"
- xmlns:mapiext=\"http://schemas.microsoft.com/mapi/id/{00062002-0000-0000-C000-000000000046}/\"
- xmlns:calendar=\"urn:schemas:calendar:\"
- xmlns:contacts=\"urn:schemas:contacts:\"
- xmlns:header=\"urn:schemas:mailheader:\"
- xmlns:httpmail=\"urn:schemas:httpmail:\"
- xmlns:office=\"urn:schemas-microsoft-com:office:office\"
- xmlns:repl=\"http://schemas.microsoft.com/repl/\"
- xmlns:exchdata=\"urn:schemas-microsoft-com:exch-data:\"
- xmlns:datatype=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\"
- xmlns:msdatatype=\"urn:schemas-microsoft-com:datatypes\"
- xmlns:forms=\"urn:schemas-microsoft-com:office:forms\"
- xmlns:-xmldata=\"urn:schemas-microsoft-com:xml-data\"
- xmlns:fulltext=\"urn:schemas-microsoft-com:fulltextqueryinfo\"
- >
- ";
- return true;
- }
- function fetch() {
- $this->reset(0);
- if(!$this->url or $this->url=="") { return false; }
- if(!$fileHandle=$this->getContents()) { return false; }
- $this->header=fgets($fileHandle, 1024); $this->status=substr($this->header,9,3);
- $statusMessages=array('200'=>'Request OK','201'=>'Request Created','202'=>'Request Accepted','301'=>'Page Moved Permanently','304'=>'Page Not Modified','400'=>'Error: Bad Request','401'=>'Error: Unauthorized Request - Username and/or Password incorrect or not supplied','403'=>'Error: Forbidden','404'=>'Error: Not Found','405'=>'Error: Method Not Allowed','408'=>'Request Timed Out','413'=>'Request Entity Too Large','414'=>'Request URI Too Large','500'=>'Server Error: Internal Server Error','501'=>'Server Error: Not Implemented','502'=>'Server Error: Bad Gateway','503'=>'Server Error: Service Unavailable','504'=>'Server Error: Gateway Timed Out','505'=>'Server Error: HTTP Version not supported');
- $this->statusMessage=(array_key_exists($this->status, $statusMessages))?$statusMessages[$this->status]:"Unsupported status code";
- while((trim($line=fgets($fileHandle, 1024))!="") and (!feof($fileHandle))) { $this->header.=$line; if($this->status=="401" and strpos($line,"WWW-Authenticate: Basic realm=\"")===0) { fclose($fileHandle); return false; } }
- while(!feof($fileHandle)) { $this->body.=fgets($fileHandle, 1024); } fclose($fileHandle);
- $this->get($this->body);
- return $this->status;
- }
- function getContents() {
- preg_match("~([a-z]*://)?([^/]*)(/.*)?~i", $this->url, $parts); $path=($parts[3]=="")?"/":$parts[3];
- if(!$sock=@fsockopen($this->domain, $this->port, $errorNumber, $errorString, $this->timeout)) { $this->log.="Could not open connection. Error ".$errorNumber.": ".$errorString."<br />\n"; return false; }
- stream_set_timeout($sock, $this->timeout);
- $this->headers['Host']=$this->domain.":".$this->port;
- $this->headers['Authorization']="Basic ".$this->encrypted;
- if(strlen($this->xmlrequest)>0) { $this->log.="XML request will be sent<br />\n"; $request=$this->method." ".$path." HTTP/1.0\r\n"; $this->headers['Content-Length']=strlen($this->xmlrequest); } else { $this->log.="XML Request not set<br />\n"; exit; }
- if(fwrite($sock, $request)===FALSE) { fclose($sock); return false; } foreach($this->headers as $key=>$value) { if(fwrite($sock, $key.": ".$value."\r\n")===FALSE) { fclose($sock); return false; } } if(fwrite($sock, "\r\n")===FALSE) { fclose($sock); return false; } if(strlen($this->xmlrequest)>0) { if(fwrite($sock, $this->xmlrequest."\r\n")===FALSE) { fclose($sock); return false; } } return $sock;
- }
- function get($inputXML) { $this->reset(1); xml_set_object($this->XMLParser,$this); xml_set_element_handler($this->XMLParser, "parseStart", "parseEnd"); xml_set_character_data_handler($this->XMLParser, "characterData"); xml_parser_set_option($this->XMLParser, XML_OPTION_TARGET_ENCODING, "UTF-8"); if(!$XMLParsed=xml_parse($this->XMLParser, $inputXML, true )) { return false; } xml_parser_free($this->XMLParser); return true; }
- function parseStart($XMLParser, $XMLTag, $XMLAttributes) { $XMLTag=str_replace(":", "_", str_replace("-", "_", $XMLTag)); $object=new stdClass(); $object->_attr=new stdClass(); foreach($XMLAttributes as $key => $value) { $key=str_replace(":", "_", str_replace("-", "_", $key)); $object->_attr->$key=new stdClass(); $object->_attr->$key=$this->clean($value); } $temp=&$this->stack[$this->index]->$XMLTag; $temp[]=&$object; $size=sizeof($temp); $this->stack[]=&$temp[$size-1]; $this->index++; }
- function parseEnd($XMLParser, $XMLTag) { array_pop($this->stack); $this->index--; }
- function characterData($XMLParser, $data) { if(trim($data)) { $this->stack[$this->index]=new stdClass(); $this->stack[$this->index]->_text.=$data; } }
- function clean($input) { return utf8_decode(trim($input)); }
- function reset($type) { if($type==0 OR !$type) { $this->header=""; $this->body=""; $this->status=""; $this->headers['User-Agent']=(isset($_SERVER['HTTP_USER_AGENT']))?$_SERVER['HTTP_USER_AGENT']:"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"; $this->headers['Referer']=(substr($_SERVER['SERVER_PROTOCOL'],0,5)=="HTTPS")?"https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']:"http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; } else if($type==1) { $this->index=0; $this->data=null; $this->stack=array(); $this->stack[]=&$this->data; $this->XMLParser=xml_parser_create("UTF-8"); } }
- /*
- /c ipm.appointment creates an appointment
- /c ipm.note creates an e-mail message
- /c ipm.schedule.meeting.request creates a meeting request
- /c ipm.schedule.meeting.canceled creates a meeting cancellation
- */
- function create($array=array()) {
- $folderName=array('message'=>'Drafts','request'=>'Calendar','cancellation'=>'Calendar');
- if(!isset($array['object_type']) OR empty($array['object_type'])) { return false; } $objectType=$array['object_type']; unset($array['object_type']);
- if(array_key_exists($objectType,$folderName)) {
- $message=array('dav:contentclass'=>'urn:content-classes:message','exchange:outlookmessageclass'=>'IPM.Note');
- $request=array('dav:contentclass'=>'urn:content-classes:calendarmessage','exchange:outlookmessageclass'=>'IPM.Schedule.Meeting.Request');
- $cancellation=array('dav:contentclass'=>'urn:content-classes:calendarmessage','exchange:outlookmessageclass'=>'IPM.Schedule.Meeting.Canceled');
- $array=array_merge($$objectType,array_merge($array,array('header:content-base'=>'http://localhost/exchange/'))); # You may need to change the URL here
- $objectName=$folderName[$objectType].'/'.urlencode($array['httpmail:subject'].'#'.date('YmdHis',time()).'#').'.EML';
- $this->xmlrequest.="<dav:set><dav:prop>"; foreach($array as $key => $value) { $this->xmlrequest.="<$key>$value</$key>"; } $this->xmlrequest.="</dav:prop></dav:set></dav:propertyupdate>";
- $this->method='PROPPATCH'; $this->url=$this->server.'/'.$this->mailbox.'/'.$objectName; $this->fetch();
- }
- }
- function attach($path,$name) {
- $this->method='PUT'; $messageURL=$this->url; $this->url=$this->url.'/'.urlencode($name); $this->xmlrequest='';
- if(!$fileHandle=fopen($path, 'r')) { return 'Unable to open file for reading.'; }
- while(!feof($fileHandle)) { $this->xmlrequest.=fread($fileHandle, 1024); } $this->fetch();
- $this->url=$messageURL; $this->xmlrequest="<?xml version=\"1.0\"?><dav:propertyupdate xmlns:dav=\"DAV:\" xmlns:header=\"urn:schemas:mailheader:\" xmlns:httpmail=\"urn:schemas:httpmail:\"><dav:set><dav:prop><header:content-disposition>attachment; filename=$name</header:content-disposition><httpmail:attachmentfilename>$name</httpmail:attachmentfilename></dav:prop></dav:set></dav:propertyupdate>"; $this->fetch();
- }
- function send() { $this->method="MOVE"; $this->headers["Destination"]=$this->server.'/'.$this->mailbox.'/%23%23DAVMailSubmissionURI%23%23/'; $this->xmlrequest="</dav:propertyupdate>"; $this->fetch(); }
- function unread() {
- global $_SESSION; $this->method='SEARCH'; $this->url=$this->server.'/'.$this->mailbox.'/Inbox/';
- $this->unreadEmails=0; $folderArray=array('Inbox'); if($_SESSION['exchange_search_junk']==1) { $folderArray[]='Junk%20E-mail'; } if($_SESSION['exchange_search_deleted']==1) { $folderArray[]='Deleted%20Items'; }
- $this->xmlrequest="<?xml version=\"1.0\"?><dav:searchrequest xmlns:dav=\"DAV:\" xmlns:security=\"http://schemas.microsoft.com/exchange/security/\"><dav:sql>SELECT \"urn:schemas:httpmail:unreadcount\" FROM \"".$this->server."/".$this->mailbox."/\"</dav:sql></dav:searchrequest>"; $this->fetch();
- foreach($this->data->A_MULTISTATUS[0]->A_RESPONSE as $response) { if(in_array(preg_replace("=/=","", preg_replace("#".$this->server."/".$this->mailbox."/#i","",$response->A_HREF[0]->_text)),$folderArray)) { if(!empty($response->A_PROPSTAT[0]->A_PROP[0]->D_UNREADCOUNT[0]->_text)) { $this->unreadEmails+=($response->A_PROPSTAT[0]->A_PROP[0]->D_UNREADCOUNT[0]->_text); } } }
- if($_SESSION['exchange_search_recursive']==1) { for($i=0; $i<count($folderArray); $i++) { $folderName=$folderArray[$i]; $this->xmlrequest="<?xml version=\"1.0\"?><dav:searchrequest xmlns:dav=\"DAV:\" xmlns:security=\"http://schemas.microsoft.com/exchange/security/\"><dav:sql>SELECT \"urn:schemas:httpmail:unreadcount\"FROM SCOPE('hierarchical traversal of \"".$this->server."/".$this->mailbox."/".$folderName."/\"')</dav:sql></dav:searchrequest>"; $this->fetch(); if(!empty($this->data->A_MULTISTATUS[0]->A_RESPONSE)) { foreach($this->data->A_MULTISTATUS[0]->A_RESPONSE as $response) { $unreadCount=0; if(!empty($response->A_PROPSTAT[0]->A_PROP[0]->D_UNREADCOUNT[0]->_text)) { $unreadCount=$response->A_PROPSTAT[0]->A_PROP[0]->D_UNREADCOUNT[0]->_text; } $folderArray[]=preg_replace("#".$this->server."/".$this->mailbox."/#i","",$response->A_HREF[0]->_text); $this->unreadEmails+=($unreadCount); } } } }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment