Subversion Repositories HomeAutomation

Rev

Rev 1169 | Rev 1215 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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