Subversion Repositories HomeAutomation

Rev

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