Subversion Repositories HomeAutomation

Rev

Rev 1169 | Rev 1215 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. <?php
  2.  
  3. require(dirname(__FILE__)."/config.php");
  4.  
  5. /*
  6.  
  7.   http://www.howtoforge.com/talking-soap-with-exchange
  8.  
  9. */
  10.  
  11. if (isset($_GET["function"]))
  12. {
  13.     switch ($_GET["function"])
  14.     {
  15.         case "getMeetingsRestOfDay":
  16.         if (isset($_GET["shortname"]))
  17.         {
  18.             echo getMeetingsRestOfDay($_GET["shortname"]);
  19.         }
  20.         break;
  21.        
  22.         case "bookMeeting":
  23.         if (isset($_GET["from"]) && isset($_GET["to"]))
  24.         {
  25.            
  26.         }
  27.         break;
  28.        
  29.         //case "":
  30.         //
  31.         //break;
  32.     }
  33. }
  34.  
  35. /*
  36.     $CreateItem->SendMeetingInvitations = "SendToNone";
  37.     $CreateItem->SavedItemFolderId->DistinguishedFolderId->Id = "calendar";
  38.     $CreateItem->Items->CalendarItem = array();
  39.     $CreateItem->Items->CalendarItem[0]->Subject = "Hello from PHP";
  40.     $CreateItem->Items->CalendarItem[0]->Start = "2010-01-01T16:00:00Z"; # ISO date format. Z denotes UTC time
  41.     $CreateItem->Items->CalendarItem[0]->End = "2010-01-01T17:00:00Z";
  42.     $CreateItem->Items->CalendarItem[0]->IsAllDayEvent = false;
  43.     $CreateItem->Items->CalendarItem[0]->LegacyFreeBusyStatus = "Busy";
  44.     $CreateItem->Items->CalendarItem[0]->Location = "Bahamas";
  45.     $CreateItem->Items->CalendarItem[0]->Categories->String = "MyCategory";
  46.     $result = $client->CreateItem($CreateItem);
  47.     print_array($result);
  48. */
  49.  
  50. /*
  51.   Get all meetings from now and 9 hours later
  52.   Return a JSON formed string
  53. */
  54. function getMeetingsRestOfDay($shortname)
  55. {
  56.     global $Calender;
  57.     global $wsdl;
  58.     $founditem = false;
  59.    
  60.     foreach($Calender as $item)
  61.     {
  62.         if ($item->Shortname == $shortname)
  63.         {
  64.             $founditem = $item;
  65.             break;
  66.         }
  67.     }
  68.    
  69.     if ($founditem !== false)
  70.     {
  71.         $FindItem->Traversal = "Shallow";
  72.         $FindItem->ItemShape->BaseShape = "AllProperties";
  73.         $FindItem->ParentFolderIds->DistinguishedFolderId->Id = "calendar";
  74.         /* Dont forget to use the proper timezone and setting for daylight saving time (php handles this fine) */
  75.         $FindItem->CalendarView->StartDate = date("c");                     //"2009-04-07T08:00:00Z";
  76.         $FindItem->CalendarView->EndDate = date("c", strtotime("+9 hour")); //"2009-04-08T00:00:00Z";
  77.         //echo date("c")." ".date("c", strtotime("+8 hour"))." ".date("c", strtotime("18:00"))."\n";
  78.  
  79.         stream_wrapper_unregister('https');
  80.         stream_wrapper_register('https', 'NTLMStream') or die("Failed to register protocol");
  81.          
  82.         $returndata = "{ \n\tshortname:'".$shortname."',\n\tmeetings:[\n";
  83.         try
  84.         {
  85.             $client = new ExchangeNTLMSoapClient($wsdl);
  86.             $client->user = $founditem->UserName;
  87.             $client->pass = $founditem->Password;
  88.             $result = $client->FindItem($FindItem);
  89.             if ($result)
  90.             {
  91.                 //print_array($result);
  92.                 $calendaritems = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->CalendarItem;
  93.                 //print_array($calendaritems);
  94.                 for ($i = 0; $i < count($calendaritems); $i++)
  95.                 {
  96.                     if (count($calendaritems) > 1)
  97.                     {
  98.                         $item = $calendaritems[$i];
  99.                     }
  100.                     else
  101.                     {
  102.                         $item = $calendaritems;
  103.                     }
  104.                     $returndata .= "\t\t{\n";
  105.                     $returndata .= "\t\torganizer:'".$item->Organizer->Mailbox->Name."',\n";
  106.                     $returndata .= "\t\tstart:'".date("G:i", strtotime($item->Start))."',\n";
  107.                     $returndata .= "\t\tend:'".date("G:i", strtotime($item->End))."'\n";
  108.                     $returndata .= "\t\t}";
  109.                     if ($i+1 < count($calendaritems))
  110.                     {
  111.                         $returndata .= ",\n";
  112.                     }
  113.                 }
  114.             }
  115.            
  116.         }
  117.         catch (SoapFault $exception)
  118.         {
  119.             return "{ error:'".$exception."',\nshortname:'".$shortname."'}\n";
  120.         }
  121.  
  122.         stream_wrapper_restore('https');
  123.         $returndata .= "\n\t]\n}\n";
  124.         return $returndata;
  125.     }
  126.     else
  127.     {
  128.         return "{ error:'Unknown shortname',\nshortname:'".$shortname."'}\n";
  129.     }
  130. }
  131.  
  132.  
  133. function print_array($var)
  134. {
  135.     echo "<pre>\n";
  136.     print_r($var);
  137.     echo "</pre>\n";
  138. }
  139.  
  140. class NTLMSoapClient extends SoapClient {
  141.     function __doRequest($request, $location, $action, $version)
  142.     {
  143.         global $exchangeurl;
  144.         $location=$exchangeurl; //override url to exchange server
  145.  
  146.         $headers = array( 'Method: POST',
  147.             'Connection: Keep-Alive',
  148.             'User-Agent: PHP-SOAP-CURL',
  149.             'Content-Type: text/xml; charset=utf-8',
  150.             'SOAPAction: "'.$action.'"',
  151.             );
  152.         $this->__last_request_headers = $headers;
  153.         $ch = curl_init($location);
  154.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  155.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  156.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  157.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  158.         curl_setopt($ch, CURLOPT_POST, true );
  159.         curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  160.         curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  161.         curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
  162.         curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
  163.         $response = curl_exec($ch);
  164.  
  165. /*$myfile = "/tmp/debugex.txt";
  166. $fh = fopen($myfile, 'w+') or die("can't open file");
  167. fwrite($fh, "user: ". $this->user. " pass: ".$this->pass."\n");
  168. fwrite($fh, "req: ". $request ."loc: ". $location ." act: ". $action ." ver: ". $version."\n");
  169. fwrite($fh, "reponse: ". $response."\n\n");
  170. fclose($fh);
  171. */
  172.        
  173.         return $response;
  174.     }
  175.    
  176.     function __getLastRequestHeaders()
  177.     {
  178.         return implode("\n", $this->__last_request_headers)."\n";
  179.     }
  180. }
  181.  
  182. class ExchangeNTLMSoapClient extends NTLMSoapClient
  183. {
  184.     public $user = '';
  185.     public $password = '';
  186. }
  187.  
  188. class NTLMStream
  189. {
  190.     private $path;
  191.     private $mode;
  192.     private $options;
  193.     private $opened_path;
  194.     private $buffer;
  195.     private $pos;
  196.  
  197.     /**
  198.      * Open the stream
  199.      *
  200.      * @param unknown_type $path
  201.      * @param unknown_type $mode
  202.      * @param unknown_type $options
  203.      * @param unknown_type $opened_path
  204.      * @return unknown
  205.      */
  206.     public function stream_open($path, $mode, $options, $opened_path)
  207.     {
  208.         echo "[NTLMStream::stream_open] $path , mode=$mode \n";
  209.         $this->path = $path;
  210.         $this->mode = $mode;
  211.         $this->options = $options;
  212.         $this->opened_path = $opened_path;
  213.         $this->createBuffer($path);
  214.         return true;
  215.     }
  216.  
  217.     /**
  218.      * Close the stream
  219.      *
  220.      */
  221.     public function stream_close()
  222.     {
  223.         echo "[NTLMStream::stream_close] \n";
  224.         curl_close($this->ch);
  225.     }
  226.  
  227.     /**
  228.      * Read the stream
  229.      *
  230.      * @param int $count number of bytes to read
  231.      * @return content from pos to count
  232.      */
  233.     public function stream_read($count)
  234.     {
  235.         echo "[NTLMStream::stream_read] $count \n";
  236.         if(strlen($this->buffer) == 0)
  237.         {
  238.             return false;
  239.         }
  240.         $read = substr($this->buffer,$this->pos, $count);
  241.         $this->pos += $count;
  242.         return $read;
  243.     }
  244.  
  245.     /***
  246.      * write the stream
  247.      *
  248.      * @param $data
  249.      * @return
  250.      */
  251.     public function stream_write($data)
  252.     {
  253.         echo "[NTLMStream::stream_write] \n";
  254.         if(strlen($this->buffer) == 0)
  255.         {
  256.             return false;
  257.         }
  258.         return true;
  259.     }
  260.  
  261.     /**
  262.      *
  263.      * @return true if eof else false
  264.      */
  265.     public function stream_eof()
  266.     {
  267.         echo "[NTLMStream::stream_eof] ";
  268.         if($this->pos > strlen($this->buffer))
  269.         {
  270.             echo "true \n";
  271.             return true;
  272.         }
  273.         echo "false \n";
  274.         return false;
  275.     }
  276.  
  277.     /**
  278.      * @return int the position of the current read pointer
  279.      */
  280.     public function stream_tell()
  281.     {
  282.         echo "[NTLMStream::stream_tell] \n";
  283.         return $this->pos;
  284.     }
  285.  
  286.     /**
  287.      * Flush stream data
  288.      */
  289.     public function stream_flush()
  290.     {
  291.         echo "[NTLMStream::stream_flush] \n";
  292.         $this->buffer = null;
  293.         $this->pos = null;
  294.     }
  295.  
  296.     /**
  297.      * Stat the file, return only the size of the buffer
  298.      *
  299.      * @return array stat information
  300.      */
  301.     public function stream_stat()
  302.     {
  303.         echo "[NTLMStream::stream_stat] \n";
  304.         $this->createBuffer($this->path);
  305.         $stat = array( 'size' => strlen($this->buffer), );
  306.         return $stat;
  307.     }
  308.  
  309.     /**
  310.      * Stat the url, return only the size of the buffer
  311.      *
  312.      * @return array stat information
  313.      */
  314.     public function url_stat($path, $flags)
  315.     {
  316.         echo "[NTLMStream::url_stat] \n";
  317.         $this->createBuffer($path);
  318.         $stat = array( 'size' => strlen($this->buffer), );
  319.         return $stat;
  320.     }
  321.  
  322.     /* Create the buffer by requesting the url through cURL */
  323.     private function createBuffer($path)
  324.     {
  325.         if($this->buffer)
  326.         {
  327.             return;
  328.         }
  329.         echo "[NTLMStream::createBuffer] create buffer from : $path \n";
  330.         $this->ch = curl_init($path);
  331.         curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  332.         curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
  333.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  334.         curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  335.         curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
  336.         curl_setopt($this->ch, CURLOPT_USERPWD, $user.':'.$pass);
  337.         echo $this->buffer = curl_exec($this->ch);
  338.        
  339.         echo "[NTLMStream::createBuffer] buffer size : ".strlen($this->buffer)."bytes \n";
  340.         $this->pos = 0;
  341. /*$myfile = "/tmp/debugex.txt";
  342. $fh = fopen($myfile, 'w+') or die("can't open file");
  343. fwrite($fh, "user: ". $this->user. " pass: ".$this->pass."\n");
  344. fwrite($fh, "req: ". $request ."loc: ". $location ." act: ". $action ." ver: ". $version."\n");
  345. fwrite($fh, "reponse: ". $response."\n\n");
  346. fclose($fh);
  347. */
  348.     }
  349. }
  350.  
  351. ?>
  352.