Subversion Repositories HomeAutomation

Rev

Rev 1165 | Rev 1194 | 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 = "{ \n\tshortname:'".$shortname."',\n\tmeetings:[\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\t{\n";
  80.                     $returndata .= "\t\torganizer:'".$item->Organizer->Mailbox->Name."',\n";
  81.                     $returndata .= "\t\tstart:'".date("G:i", strtotime($item->Start))."',\n";
  82.                     $returndata .= "\t\tend:'".date("G:i", strtotime($item->End))."'\n";
  83.                     $returndata .= "\t\t}";
  84.                     if ($i+1 < count($calendaritems))
  85.                     {
  86.                         $returndata .= ",\n";
  87.                     }
  88.                 }
  89.             }
  90.            
  91.         }
  92.         catch (SoapFault $exception)
  93.         {
  94.             return "{ error:'".$exception."',\nshortname:'".$shortname."'}\n";
  95.         }
  96.  
  97.         stream_wrapper_restore('https');
  98.         $returndata .= "\n\t]\n}\n";
  99.         return $returndata;
  100.     }
  101.     else
  102.     {
  103.         return "{ error:'Unknown shortname',\nshortname:'".$shortname."'}\n";
  104.     }
  105. }
  106.  
  107.  
  108. function print_array($var)
  109. {
  110.     echo "<pre>\n";
  111.     print_r($var);
  112.     echo "</pre>\n";
  113. }
  114.  
  115. class NTLMSoapClient extends SoapClient {
  116.     function __doRequest($request, $location, $action, $version)
  117.     {
  118.         global $exchangeurl;
  119.         $location=$exchangeurl; //override url to exchange server
  120.  
  121.         $headers = array( 'Method: POST',
  122.             'Connection: Keep-Alive',
  123.             'User-Agent: PHP-SOAP-CURL',
  124.             'Content-Type: text/xml; charset=utf-8',
  125.             'SOAPAction: "'.$action.'"',
  126.             );
  127.         $this->__last_request_headers = $headers;
  128.         $ch = curl_init($location);
  129.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  130.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  131.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  132.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  133.         curl_setopt($ch, CURLOPT_POST, true );
  134.         curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  135.         curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  136.         curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
  137.         curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
  138.         $response = curl_exec($ch);
  139.  
  140. /*$myfile = "/tmp/debugex.txt";
  141. $fh = fopen($myfile, 'w+') or die("can't open file");
  142. fwrite($fh, "user: ". $this->user. " pass: ".$this->pass."\n");
  143. fwrite($fh, "req: ". $request ."loc: ". $location ." act: ". $action ." ver: ". $version."\n");
  144. fwrite($fh, "reponse: ". $response."\n\n");
  145. fclose($fh);
  146. */
  147.        
  148.         return $response;
  149.     }
  150.    
  151.     function __getLastRequestHeaders()
  152.     {
  153.         return implode("\n", $this->__last_request_headers)."\n";
  154.     }
  155. }
  156.  
  157. class ExchangeNTLMSoapClient extends NTLMSoapClient
  158. {
  159.     public $user = '';
  160.     public $password = '';
  161. }
  162.  
  163. class NTLMStream
  164. {
  165.     private $path;
  166.     private $mode;
  167.     private $options;
  168.     private $opened_path;
  169.     private $buffer;
  170.     private $pos;
  171.  
  172.     /**
  173.      * Open the stream
  174.      *
  175.      * @param unknown_type $path
  176.      * @param unknown_type $mode
  177.      * @param unknown_type $options
  178.      * @param unknown_type $opened_path
  179.      * @return unknown
  180.      */
  181.     public function stream_open($path, $mode, $options, $opened_path)
  182.     {
  183.         echo "[NTLMStream::stream_open] $path , mode=$mode \n";
  184.         $this->path = $path;
  185.         $this->mode = $mode;
  186.         $this->options = $options;
  187.         $this->opened_path = $opened_path;
  188.         $this->createBuffer($path);
  189.         return true;
  190.     }
  191.  
  192.     /**
  193.      * Close the stream
  194.      *
  195.      */
  196.     public function stream_close()
  197.     {
  198.         echo "[NTLMStream::stream_close] \n";
  199.         curl_close($this->ch);
  200.     }
  201.  
  202.     /**
  203.      * Read the stream
  204.      *
  205.      * @param int $count number of bytes to read
  206.      * @return content from pos to count
  207.      */
  208.     public function stream_read($count)
  209.     {
  210.         echo "[NTLMStream::stream_read] $count \n";
  211.         if(strlen($this->buffer) == 0)
  212.         {
  213.             return false;
  214.         }
  215.         $read = substr($this->buffer,$this->pos, $count);
  216.         $this->pos += $count;
  217.         return $read;
  218.     }
  219.  
  220.     /***
  221.      * write the stream
  222.      *
  223.      * @param $data
  224.      * @return
  225.      */
  226.     public function stream_write($data)
  227.     {
  228.         echo "[NTLMStream::stream_write] \n";
  229.         if(strlen($this->buffer) == 0)
  230.         {
  231.             return false;
  232.         }
  233.         return true;
  234.     }
  235.  
  236.     /**
  237.      *
  238.      * @return true if eof else false
  239.      */
  240.     public function stream_eof()
  241.     {
  242.         echo "[NTLMStream::stream_eof] ";
  243.         if($this->pos > strlen($this->buffer))
  244.         {
  245.             echo "true \n";
  246.             return true;
  247.         }
  248.         echo "false \n";
  249.         return false;
  250.     }
  251.  
  252.     /**
  253.      * @return int the position of the current read pointer
  254.      */
  255.     public function stream_tell()
  256.     {
  257.         echo "[NTLMStream::stream_tell] \n";
  258.         return $this->pos;
  259.     }
  260.  
  261.     /**
  262.      * Flush stream data
  263.      */
  264.     public function stream_flush()
  265.     {
  266.         echo "[NTLMStream::stream_flush] \n";
  267.         $this->buffer = null;
  268.         $this->pos = null;
  269.     }
  270.  
  271.     /**
  272.      * Stat the file, return only the size of the buffer
  273.      *
  274.      * @return array stat information
  275.      */
  276.     public function stream_stat()
  277.     {
  278.         echo "[NTLMStream::stream_stat] \n";
  279.         $this->createBuffer($this->path);
  280.         $stat = array( 'size' => strlen($this->buffer), );
  281.         return $stat;
  282.     }
  283.  
  284.     /**
  285.      * Stat the url, return only the size of the buffer
  286.      *
  287.      * @return array stat information
  288.      */
  289.     public function url_stat($path, $flags)
  290.     {
  291.         echo "[NTLMStream::url_stat] \n";
  292.         $this->createBuffer($path);
  293.         $stat = array( 'size' => strlen($this->buffer), );
  294.         return $stat;
  295.     }
  296.  
  297.     /* Create the buffer by requesting the url through cURL */
  298.     private function createBuffer($path)
  299.     {
  300.         if($this->buffer)
  301.         {
  302.             return;
  303.         }
  304.         echo "[NTLMStream::createBuffer] create buffer from : $path \n";
  305.         $this->ch = curl_init($path);
  306.         curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  307.         curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
  308.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  309.         curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  310.         curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
  311.         curl_setopt($this->ch, CURLOPT_USERPWD, $user.':'.$pass);
  312.         echo $this->buffer = curl_exec($this->ch);
  313.        
  314.         echo "[NTLMStream::createBuffer] buffer size : ".strlen($this->buffer)."bytes \n";
  315.         $this->pos = 0;
  316. /*$myfile = "/tmp/debugex.txt";
  317. $fh = fopen($myfile, 'w+') or die("can't open file");
  318. fwrite($fh, "user: ". $this->user. " pass: ".$this->pass."\n");
  319. fwrite($fh, "req: ". $request ."loc: ". $location ." act: ". $action ." ver: ". $version."\n");
  320. fwrite($fh, "reponse: ". $response."\n\n");
  321. fclose($fh);
  322. */
  323.     }
  324. }
  325.  
  326. ?>
  327.