Subversion Repositories HomeAutomation

Rev

Rev 1161 | Rev 1163 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1151 arune 1
<?php
2
 
1158 arune 3
require(dirname(__FILE__)."/config.php");
4
 
1160 arune 5
if (isset($_GET["function"]))
1159 arune 6
{
1160 arune 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":
1162 arune 17
        if (isset($_GET["from"]) && isset($_GET["to"]))
18
        {
19
 
20
        }
1160 arune 21
        break;
22
 
23
        //case "":
24
        //
25
        //break;
1161 arune 26
    }
1159 arune 27
}
28
 
1160 arune 29
function getMeetingsRestOfDay($shortname)
1151 arune 30
{
1160 arune 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";
1162 arune 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
 
1160 arune 54
        stream_wrapper_unregister('https');
55
        stream_wrapper_register('https', 'NTLMStream') or die("Failed to register protocol");
56
 
1162 arune 57
        $returndata = "";
1160 arune 58
        try
59
        {
1161 arune 60
            $client = new ExchangeNTLMSoapClient($wsdl);
61
            $client->user = $founditem->UserName;
62
            $client->pass = $founditem->Password;
1160 arune 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);
1161 arune 69
              $returndata = "";
1162 arune 70
              for ($i = 0; $i < count($calendaritems); $i++)
1161 arune 71
              {
1162 arune 72
                if (count($calendaritems) > 1)
73
                {
74
                    $item = $calendaritems[$i];
75
                }
76
                else
77
                {
78
                    $item = $calendaritems;
79
                }
80
                $returndata .= "<meeting>\n";
81
                $returndata .= "\t<organizer>".$item->Organizer->Mailbox->Name."</organizer>\n";
82
                $returndata .= "\t<start>".date("G:i", strtotime($item->Start))."</start>\n";
83
                $returndata .= "\t<end>".date("G:i", strtotime($item->End))."</end>\n";
84
                $returndata .= "</meeting>\n";
85
              }
1160 arune 86
            }
87
 
88
        }
89
        catch (SoapFault $exception)
90
        {
91
            return "<error>".$exception."</error>";
92
        }
1151 arune 93
 
1160 arune 94
        stream_wrapper_restore('https');
1161 arune 95
          return $returndata;
1160 arune 96
    }
97
    else
98
    {
99
        return "<error>Unknown shortname</error>";
100
    }
101
}
1151 arune 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
  {
1161 arune 114
    global $exchangeurl;
1159 arune 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
            );
1151 arune 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);
1161 arune 133
    curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
1151 arune 134
    $response = curl_exec($ch);
1159 arune 135
 
1161 arune 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
*/
1159 arune 143
 
1151 arune 144
    return $response;
145
  }
146
 
147
  function __getLastRequestHeaders()
148
  {
149
    return implode("\n", $this->__last_request_headers)."\n";
150
  }
151
}
152
 
1161 arune 153
class ExchangeNTLMSoapClient extends NTLMSoapClient
154
{
155
    public $user = '';
156
    public $password = '';
157
}
1151 arune 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
 
1159 arune 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
     */
1151 arune 177
  public function stream_open($path, $mode, $options, $opened_path)
178
  {
1159 arune 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;
1151 arune 186
  }
187
 
1159 arune 188
    /**
189
     * Close the stream
190
     *
191
     */
1151 arune 192
  public function stream_close()
1159 arune 193
  {
194
    echo "[NTLMStream::stream_close] \n";
195
    curl_close($this->ch);
196
  }
1151 arune 197
 
1159 arune 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
  }
1151 arune 215
 
1159 arune 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
  }
1151 arune 231
 
1159 arune 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
  }
1151 arune 247
 
1159 arune 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
  }
1151 arune 256
 
1159 arune 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
  }
1151 arune 266
 
1159 arune 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
  }
1151 arune 279
 
1159 arune 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
  }
1151 arune 292
 
1159 arune 293
  /* Create the buffer by requesting the url through cURL */
294
  private function createBuffer($path)
295
  {
1160 arune 296
    global $user;
297
    global $pass;
1159 arune 298
    if($this->buffer)
299
    {
300
      return;
301
    }
302
    echo "[NTLMStream::createBuffer] create buffer from : $path \n";
303
    $this->ch = curl_init($path);
1151 arune 304
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
305
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
1159 arune 306
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
307
    curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
308
    curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
1160 arune 309
    curl_setopt($this->ch, CURLOPT_USERPWD, $user.':'.$pass);
1159 arune 310
    echo $this->buffer = curl_exec($this->ch);
311
 
312
    echo "[NTLMStream::createBuffer] buffer size : ".strlen($this->buffer)."bytes \n";
313
    $this->pos = 0;
314
  }
315
}
1151 arune 316
 
317
?>