Subversion Repositories HomeAutomation

Rev

Rev 1217 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1217 Rev 1308
1
<?php
1
<?php
2
 
2
 
3
require(dirname(__FILE__)."/config.php");
3
require(dirname(__FILE__)."/config.php");
4
 
4
 
5
/*
5
/*
6
 
6
 
7
  http://www.howtoforge.com/talking-soap-with-exchange
7
  http://www.howtoforge.com/talking-soap-with-exchange
8
 
8
 
9
*/
9
*/
10
 
10
 
11
if (isset($_GET["function"]))
11
if (isset($_GET["function"]))
12
{
12
{
13
    switch ($_GET["function"])
13
    switch ($_GET["function"])
14
    {
14
    {
15
        case "getMeetingsRestOfDay":
15
        case "getMeetingsRestOfDay":
16
        if (isset($_GET["shortname"]))
16
        if (isset($_GET["shortname"]))
17
        {
17
        {
18
            echo getMeetingsRestOfDay($_GET["shortname"]);
18
            echo getMeetingsRestOfDay($_GET["shortname"]);
19
        }
19
        }
20
        break;
20
        break;
21
       
21
       
22
        case "bookMeeting":
22
        case "bookMeeting":
23
        if (isset($_GET["from"]) && isset($_GET["to"]) && isset($_GET["shortname"]))
23
        if (isset($_GET["from"]) && isset($_GET["to"]) && isset($_GET["shortname"]))
24
        {
24
        {
25
            if (preg_match("/^\d\d:\d\d$/", $_GET["from"]) && preg_match("/^\d\d:\d\d$/", $_GET["to"]))
25
            if (preg_match("/^\d\d:\d\d$/", $_GET["from"]) && preg_match("/^\d\d:\d\d$/", $_GET["to"]))
26
            {
26
            {
27
                echo bookMeeting($_GET["shortname"], $_GET["from"], $_GET["to"]);
27
                echo bookMeeting($_GET["shortname"], $_GET["from"], $_GET["to"]);
28
            }
28
            }
29
        }
29
        }
30
        break;
30
        break;
31
       
31
       
32
        //case "":
32
        //case "":
33
        //
33
        //
34
        //break;
34
        //break;
35
    }
35
    }
36
}
36
}
37
 
37
 
38
 
38
 
39
/*
39
/*
40
  Book meeting
40
  Book meeting
41
  Return a JSON formed string with the result
41
  Return a JSON formed string with the result
42
*/
42
*/
43
function bookMeeting($shortname, $bookFrom, $bookTo)
43
function bookMeeting($shortname, $bookFrom, $bookTo)
44
{
44
{
45
    global $Calender;
45
    global $Calender;
46
    global $wsdl;
46
    global $wsdl;
47
    $founditem = false;
47
    $founditem = false;
48
   
48
   
49
    foreach($Calender as $item)
49
    foreach($Calender as $item)
50
    {
50
    {
51
        if ($item->Shortname == $shortname)
51
        if ($item->Shortname == $shortname)
52
        {
52
        {
53
            $founditem = $item;
53
            $founditem = $item;
54
            break;
54
            break;
55
        }
55
        }
56
    }
56
    }
57
   
57
   
58
    $bookingsOk = true;
58
    $bookingsOk = true;
59
 
59
 
60
    $bookings = json_decode(getMeetingsRestOfDay($shortname));
60
    $bookings = json_decode(getMeetingsRestOfDay($shortname));
61
    //print_array($bookings);
61
    //print_array($bookings);
62
    //echo $bookings;
62
    //echo $bookings;
63
    if ($bookings->{'error'})
63
    if ($bookings->{'error'})
64
    {
64
    {
65
        $bookingsOk = false;
65
        $bookingsOk = false;
66
    }
66
    }
67
   
67
   
68
    if ($bookings->{'meetings'})
68
    if ($bookings->{'meetings'})
69
    {
69
    {
70
        foreach ($bookings->{'meetings'} as $meeting)
70
        foreach ($bookings->{'meetings'} as $meeting)
71
        {
71
        {
72
            $meetStart = strtotime($meeting->{'start'});
72
            $meetStart = strtotime($meeting->{'start'});
73
            $meetEnd = strtotime($meeting->{'end'});
73
            $meetEnd = strtotime($meeting->{'end'});
74
            $start = strtotime($bookFrom);
74
            $start = strtotime($bookFrom);
75
            $end = strtotime($bookTo);
75
            $end = strtotime($bookTo);
76
            //echo $meeting->{'start'}." ";
76
            //echo $meeting->{'start'}." ";
77
            if (($start >= $meetStart && $start < $meetEnd)
77
            if (($start >= $meetStart && $start < $meetEnd)
78
                || ($end > $meetStart && $end <= $meetEnd)
78
                || ($end > $meetStart && $end <= $meetEnd)
79
                || ($start <= $meetStart && $end >= $meetEnd))
79
                || ($start <= $meetStart && $end >= $meetEnd))
80
            {
80
            {
81
                $bookingsOk = false;
81
                $bookingsOk = false;
82
                break;
82
                break;
83
            }
83
            }
84
        }
84
        }
85
    }
85
    }
86
 
86
 
87
    if ($founditem !== false && $bookingsOk)
87
    if ($founditem !== false && $bookingsOk)
88
    {
88
    {
89
        $CreateItem->SendMeetingInvitations = "SendToNone";
89
        $CreateItem->SendMeetingInvitations = "SendToNone";
90
        $CreateItem->SavedItemFolderId->DistinguishedFolderId->Id = "calendar";
90
        $CreateItem->SavedItemFolderId->DistinguishedFolderId->Id = "calendar";
91
        $CreateItem->Items->CalendarItem = array();
91
        $CreateItem->Items->CalendarItem = array();
92
        $CreateItem->Items->CalendarItem[0]->Subject = "Booking from panel";
92
        $CreateItem->Items->CalendarItem[0]->Subject = "Booking from panel";
93
        /* Dont forget to use the proper timezone and setting for daylight saving time (php handles this fine) */
93
        /* Dont forget to use the proper timezone and setting for daylight saving time (php handles this fine) */
94
        $CreateItem->Items->CalendarItem[0]->Start = date("c",strtotime($bookFrom)); // "2009-05-22T17:00:00Z" ISO date format. Z denotes UTC time
94
        $CreateItem->Items->CalendarItem[0]->Start = date("c",strtotime($bookFrom)); // "2009-05-22T17:00:00Z" ISO date format. Z denotes UTC time
95
        $CreateItem->Items->CalendarItem[0]->End = date("c",strtotime($bookTo));
95
        $CreateItem->Items->CalendarItem[0]->End = date("c",strtotime($bookTo));
96
        $CreateItem->Items->CalendarItem[0]->IsAllDayEvent = false;
96
        $CreateItem->Items->CalendarItem[0]->IsAllDayEvent = false;
97
        $CreateItem->Items->CalendarItem[0]->LegacyFreeBusyStatus = "Busy";
97
        $CreateItem->Items->CalendarItem[0]->LegacyFreeBusyStatus = "Busy";
98
        $CreateItem->Items->CalendarItem[0]->Location = $founditem->Shortname;
98
        $CreateItem->Items->CalendarItem[0]->Location = $founditem->Shortname;
99
        $CreateItem->Items->CalendarItem[0]->Categories->String = "Meeting";
99
        $CreateItem->Items->CalendarItem[0]->Categories->String = "Meeting";
100
 
100
 
101
        stream_wrapper_unregister('https');
101
        stream_wrapper_unregister('https');
102
        stream_wrapper_register('https', 'NTLMStream') or die("Failed to register protocol");
102
        stream_wrapper_register('https', 'NTLMStream') or die("Failed to register protocol");
103
         
103
         
104
        try
104
        try
105
        {
105
        {
106
            $client = new ExchangeNTLMSoapClient($wsdl);
106
            $client = new ExchangeNTLMSoapClient($wsdl);
107
            $client->user = $founditem->UserName;
107
            $client->user = $founditem->UserName;
108
            $client->pass = $founditem->Password;
108
            $client->pass = $founditem->Password;
109
            $result = $client->CreateItem($CreateItem);
109
            $result = $client->CreateItem($CreateItem);
110
            if ($result)
110
            if ($result)
111
            {
111
            {
112
                //print_array($result);
112
                //print_array($result);
113
                if ($result->ResponseMessages->CreateItemResponseMessage->ResponseClass == "Success")
113
                if ($result->ResponseMessages->CreateItemResponseMessage->ResponseClass == "Success")
114
                {
114
                {
115
                    $returndata = "{ result:'success',\nshortname:'".$shortname."'}\n";
115
                    $returndata = "{ result:'success',\nshortname:'".$shortname."'}\n";
116
                }
116
                }
117
                else
117
                else
118
                {
118
                {
119
                    $errorcode=$result->ResponseMessages->CreateItemResponseMessage->MessageText;
119
                    $errorcode=$result->ResponseMessages->CreateItemResponseMessage->MessageText;
120
                    $returndata = "{ result:'error',\nerror:'".$errorcode."',\nshortname:'".$shortname."'}\n";
120
                    $returndata = "{ result:'error',\nerror:'".$errorcode."',\nshortname:'".$shortname."'}\n";
121
                }
121
                }
122
            }
122
            }
123
            else
123
            else
124
            {
124
            {
125
                $returdata = "{ error:'No result',\nshortname:'".$shortname."'}\n";
125
                $returdata = "{ error:'No result',\nshortname:'".$shortname."'}\n";
126
            }
126
            }
127
           
127
           
128
        }
128
        }
129
        catch (SoapFault $exception)
129
        catch (SoapFault $exception)
130
        {
130
        {
131
            $returdata = "{ error:'".$exception."',\nshortname:'".$shortname."'}\n";
131
            $returdata = "{ error:'".$exception."',\nshortname:'".$shortname."'}\n";
132
        }
132
        }
133
 
133
 
134
        stream_wrapper_restore('https');
134
        stream_wrapper_restore('https');
135
        return $returndata;
135
        return $returndata;
136
    }
136
    }
137
    else if ($founditem === false)
137
    else if ($founditem === false)
138
    {
138
    {
139
        return "{ error:'Unknown shortname',\nshortname:'".$shortname."'}\n";
139
        return "{ error:'Unknown shortname',\nshortname:'".$shortname."'}\n";
140
    }
140
    }
141
    else
141
    else
142
    {
142
    {
143
        return "{ error:'Double booking',\nshortname:'".$shortname."'}\n";
143
        return "{ error:'Double booking',\nshortname:'".$shortname."'}\n";
144
    }
144
    }
145
}
145
}
146
 
146
 
147
 
147
 
148
/*
148
/*
149
 
149
 
150
stdClass Object
150
stdClass Object
151
(
151
(
152
    [ResponseMessages] => stdClass Object
152
    [ResponseMessages] => stdClass Object
153
        (
153
        (
154
            [CreateItemResponseMessage] => stdClass Object
154
            [CreateItemResponseMessage] => stdClass Object
155
                (
155
                (
156
                    [ResponseCode] => NoError
156
                    [ResponseCode] => NoError
157
                    [ResponseClass] => Success
157
                    [ResponseClass] => Success
158
                    [Items] => stdClass Object
158
                    [Items] => stdClass Object
159
                        (
159
                        (
160
                            [CalendarItem] => stdClass Object
160
                            [CalendarItem] => stdClass Object
161
                                (
161
                                (
162
                                    [ItemId] => stdClass Object
162
                                    [ItemId] => stdClass Object
163
                                        (
163
                                        (
164
                                            [Id] =>
164
                                            [Id] =>
165
AAAcAEVudGVyLktvbmZlcmVuc3J1bUBxcnRlY2guc2UARgAAAAAAWx3xkPMlnUyMluTTqIdb4AcAdM/rfM3YaEeMsdYGn8jOeAAAAClWOAAAK68xOGjBUkCXHxa/+2eMqAAah/ErtAAA
165
AAAcAEVudGVyLktvbmZlcmVuc3J1bUBxcnRlY2guc2UARgAAAAAAWx3xkPMlnUyMluTTqIdb4AcAdM/rfM3YaEeMsdYGn8jOeAAAAClWOAAAK68xOGjBUkCXHxa/+2eMqAAah/ErtAAA
166
                                            [ChangeKey] => DwAAABYAAAArrzE4aMFSQJcfFr/7Z4yoABqH8Tu3
166
                                            [ChangeKey] => DwAAABYAAAArrzE4aMFSQJcfFr/7Z4yoABqH8Tu3
167
                                        )
167
                                        )
168
                                )
168
                                )
169
                        )
169
                        )
170
                )
170
                )
171
        )
171
        )
172
)
172
)
173
 
173
 
174
stdClass Object
174
stdClass Object
175
(
175
(
176
    [ResponseMessages] => stdClass Object
176
    [ResponseMessages] => stdClass Object
177
        (
177
        (
178
            [CreateItemResponseMessage] => stdClass Object
178
            [CreateItemResponseMessage] => stdClass Object
179
                (
179
                (
180
                    [MessageText] => EndDate is earlier than StartDate
180
                    [MessageText] => EndDate is earlier than StartDate
181
                    [ResponseCode] => ErrorCalendarEndDateIsEarlierThanStartDate
181
                    [ResponseCode] => ErrorCalendarEndDateIsEarlierThanStartDate
182
                    [DescriptiveLinkKey] => 0
182
                    [DescriptiveLinkKey] => 0
183
                    [ResponseClass] => Error
183
                    [ResponseClass] => Error
184
                    [Items] => stdClass Object
184
                    [Items] => stdClass Object
185
                        (
185
                        (
186
                        )
186
                        )
187
                )
187
                )
188
        )
188
        )
189
)
189
)
190
*/
190
*/
191
 
191
 
192
 
192
 
193
 
193
 
194
/*
194
/*
195
  Get all meetings from now and 9 hours later
195
  Get all meetings from now and 9 hours later
196
  Return a JSON formed string
196
  Return a JSON formed string
197
*/
197
*/
198
function getMeetingsRestOfDay($shortname)
198
function getMeetingsRestOfDay($shortname)
199
{
199
{
200
    global $Calender;
200
    global $Calender;
201
    global $wsdl;
201
    global $wsdl;
202
    $founditem = false;
202
    $founditem = false;
203
   
203
   
204
    foreach($Calender as $item)
204
    foreach($Calender as $item)
205
    {
205
    {
206
        if ($item->Shortname == $shortname)
206
        if ($item->Shortname == $shortname)
207
        {
207
        {
208
            $founditem = $item;
208
            $founditem = $item;
209
            break;
209
            break;
210
        }
210
        }
211
    }
211
    }
212
   
212
   
213
    if ($founditem !== false)
213
    if ($founditem !== false)
214
    {
214
    {
215
        $FindItem->Traversal = "Shallow";
215
        $FindItem->Traversal = "Shallow";
216
        $FindItem->ItemShape->BaseShape = "AllProperties";
216
        $FindItem->ItemShape->BaseShape = "AllProperties";
217
        $FindItem->ParentFolderIds->DistinguishedFolderId->Id = "calendar";
217
        $FindItem->ParentFolderIds->DistinguishedFolderId->Id = "calendar";
218
        /* Dont forget to use the proper timezone and setting for daylight saving time (php handles this fine) */
218
        /* Dont forget to use the proper timezone and setting for daylight saving time (php handles this fine) */
219
        $FindItem->CalendarView->StartDate = date("c");                     //"2009-04-07T08:00:00Z";
219
        $FindItem->CalendarView->StartDate = date("c");                     //"2009-04-07T08:00:00Z";
220
        $FindItem->CalendarView->EndDate = date("c", strtotime("+12 hour"));    //"2009-04-08T00:00:00Z";
220
        $FindItem->CalendarView->EndDate = date("c", strtotime("+12 hour"));    //"2009-04-08T00:00:00Z";
221
        //echo date("c")." ".date("c", strtotime("+8 hour"))." ".date("c", strtotime("18:00"))."\n";
221
        //echo date("c")." ".date("c", strtotime("+8 hour"))." ".date("c", strtotime("18:00"))."\n";
222
 
222
 
223
        stream_wrapper_unregister('https');
223
        stream_wrapper_unregister('https');
224
        stream_wrapper_register('https', 'NTLMStream') or die("Failed to register protocol");
224
        stream_wrapper_register('https', 'NTLMStream') or die("Failed to register protocol");
225
         
225
         
226
        $returndata = "{ \n\t\"shortname\":\"".$shortname."\",\n\t\"meetings\":[\n";
226
        $returndata = "{ \n\t\"shortname\":\"".$shortname."\",\n\t\"meetings\":[\n";
227
        try
227
        try
228
        {
228
        {
229
            $client = new ExchangeNTLMSoapClient($wsdl);
229
            $client = new ExchangeNTLMSoapClient($wsdl);
230
            $client->user = $founditem->UserName;
230
            $client->user = $founditem->UserName;
231
            $client->pass = $founditem->Password;
231
            $client->pass = $founditem->Password;
232
            $result = $client->FindItem($FindItem);
232
            $result = $client->FindItem($FindItem);
233
            if ($result)
233
            if ($result)
234
            {
234
            {
235
                //print_array($result);
235
                //print_array($result);
236
                $calendaritems = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->CalendarItem;
236
                $calendaritems = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->CalendarItem;
237
                //print_array($calendaritems);
237
                //print_array($calendaritems);
238
                for ($i = 0; $i < count($calendaritems); $i++)
238
                for ($i = 0; $i < count($calendaritems); $i++)
239
                {
239
                {
240
                    if (count($calendaritems) > 1)
240
                    if (count($calendaritems) > 1)
241
                    {
241
                    {
242
                        $item = $calendaritems[$i];
242
                        $item = $calendaritems[$i];
243
                    }
243
                    }
244
                    else
244
                    else
245
                    {
245
                    {
246
                        $item = $calendaritems;
246
                        $item = $calendaritems;
247
                    }
247
                    }
248
                    $returndata .= "\t\t{\n";
248
                    $returndata .= "\t\t{\n";
249
                    $returndata .= "\t\t\"organizer\":\"".$item->Organizer->Mailbox->Name."\",\n";
249
                    $returndata .= "\t\t\"organizer\":\"".$item->Organizer->Mailbox->Name."\",\n";
250
                    $returndata .= "\t\t\"subject\":\"".$item->Subject."\",\n";
250
                    $returndata .= "\t\t\"subject\":\"".$item->Subject."\",\n";
251
                    $returndata .= "\t\t\"start\":\"".date("G:i", strtotime($item->Start))."\",\n";
251
                    $returndata .= "\t\t\"start\":\"".date("G:i", strtotime($item->Start))."\",\n";
252
                    $returndata .= "\t\t\"end\":\"".date("G:i", strtotime($item->End))."\"\n";
252
                    $returndata .= "\t\t\"end\":\"".date("G:i", strtotime($item->End))."\"\n";
253
                    $returndata .= "\t\t}";
253
                    $returndata .= "\t\t}";
254
                    if ($i+1 < count($calendaritems))
254
                    if ($i+1 < count($calendaritems))
255
                    {
255
                    {
256
                        $returndata .= ",\n";
256
                        $returndata .= ",\n";
257
                    }
257
                    }
258
                }
258
                }
259
            }
259
            }
260
            else
260
            else
261
            {
261
            {
262
                //return "{ \"error\":\"No result\",\n\"shortname\":\"".$shortname."\"}\n";
262
                //return "{ \"error\":\"No result\",\n\"shortname\":\"".$shortname."\"}\n";
263
            }
263
            }
264
        }
264
        }
265
        catch (SoapFault $exception)
265
        catch (SoapFault $exception)
266
        {
266
        {
267
            //return "{ \"error\":\"".$exception."\",\n\"shortname\":\"".$shortname."\"}\n"; 
267
            //return "{ \"error\":\"".$exception."\",\n\"shortname\":\"".$shortname."\"}\n"; 
268
        }
268
        }
269
 
269
 
270
        stream_wrapper_restore('https');
270
        stream_wrapper_restore('https');
271
        $returndata .= "\n\t]\n}\n";
271
        $returndata .= "\n\t]\n}\n";
272
        return $returndata;
272
        return $returndata;
273
    }
273
    }
274
    else
274
    else
275
    {
275
    {
276
        return "{ \"error\":\"Unknown shortname\",\n\"shortname\":\"".$shortname."\"}\n";
276
        return "{ \"error\":\"Unknown shortname\",\n\"shortname\":\"".$shortname."\"}\n";
277
    }
277
    }
278
}
278
}
279
 
279
 
280
 
280
 
281
function print_array($var)
281
function print_array($var)
282
{
282
{
283
    echo "<pre>\n";
283
    echo "<pre>\n";
284
    print_r($var);
284
    print_r($var);
285
    echo "</pre>\n";
285
    echo "</pre>\n";
286
}
286
}
287
 
287
 
288
class NTLMSoapClient extends SoapClient {
288
class NTLMSoapClient extends SoapClient {
289
    function __doRequest($request, $location, $action, $version)
289
    function __doRequest($request, $location, $action, $version)
290
    {
290
    {
291
        global $exchangeurl;
291
        global $exchangeurl;
292
        $location=$exchangeurl; //override url to exchange server
292
        $location=$exchangeurl; //override url to exchange server
293
 
293
 
294
        $headers = array( 'Method: POST',
294
        $headers = array( 'Method: POST',
295
            'Connection: Keep-Alive',
295
            'Connection: Keep-Alive',
296
            'User-Agent: PHP-SOAP-CURL',
296
            'User-Agent: PHP-SOAP-CURL',
297
            'Content-Type: text/xml; charset=utf-8',
297
            'Content-Type: text/xml; charset=utf-8',
298
            'SOAPAction: "'.$action.'"',
298
            'SOAPAction: "'.$action.'"',
299
            );
299
            );
300
        $this->__last_request_headers = $headers;
300
        $this->__last_request_headers = $headers;
301
        $ch = curl_init($location);
301
        $ch = curl_init($location);
302
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
302
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
303
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
303
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
304
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
304
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
305
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
305
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
306
        curl_setopt($ch, CURLOPT_POST, true );
306
        curl_setopt($ch, CURLOPT_POST, true );
307
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
307
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
308
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
308
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
309
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
309
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
310
        curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
310
        curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
311
        $response = curl_exec($ch);
311
        $response = curl_exec($ch);
312
 
312
 
313
/*$myfile = "/tmp/debugex.txt";
313
/*$myfile = "/tmp/debugex.txt";
314
$fh = fopen($myfile, 'w+') or die("can't open file");
314
$fh = fopen($myfile, 'w+') or die("can't open file");
315
fwrite($fh, "user: ". $this->user. " pass: ".$this->pass."\n");
315
fwrite($fh, "user: ". $this->user. " pass: ".$this->pass."\n");
316
fwrite($fh, "req: ". $request ."loc: ". $location ." act: ". $action ." ver: ". $version."\n");
316
fwrite($fh, "req: ". $request ."loc: ". $location ." act: ". $action ." ver: ". $version."\n");
317
fwrite($fh, "reponse: ". $response."\n\n");
317
fwrite($fh, "reponse: ". $response."\n\n");
318
fclose($fh);
318
fclose($fh);
319
*/
319
*/
320
       
320
       
321
        return $response;
321
        return $response;
322
    }
322
    }
323
   
323
   
324
    function __getLastRequestHeaders()
324
    function __getLastRequestHeaders()
325
    {
325
    {
326
        return implode("\n", $this->__last_request_headers)."\n";
326
        return implode("\n", $this->__last_request_headers)."\n";
327
    }
327
    }
328
}
328
}
329
 
329
 
330
class ExchangeNTLMSoapClient extends NTLMSoapClient
330
class ExchangeNTLMSoapClient extends NTLMSoapClient
331
{
331
{
332
    public $user = '';
332
    public $user = '';
333
    public $password = '';
333
    public $password = '';
334
}
334
}
335
 
335
 
336
class NTLMStream
336
class NTLMStream
337
{
337
{
338
    private $path;
338
    private $path;
339
    private $mode;
339
    private $mode;
340
    private $options;
340
    private $options;
341
    private $opened_path;
341
    private $opened_path;
342
    private $buffer;
342
    private $buffer;
343
    private $pos;
343
    private $pos;
344
 
344
 
345
    /**
345
    /**
346
     * Open the stream
346
     * Open the stream
347
     *
347
     *
348
     * @param unknown_type $path
348
     * @param unknown_type $path
349
     * @param unknown_type $mode
349
     * @param unknown_type $mode
350
     * @param unknown_type $options
350
     * @param unknown_type $options
351
     * @param unknown_type $opened_path
351
     * @param unknown_type $opened_path
352
     * @return unknown
352
     * @return unknown
353
     */
353
     */
354
    public function stream_open($path, $mode, $options, $opened_path)
354
    public function stream_open($path, $mode, $options, $opened_path)
355
    {
355
    {
356
        echo "[NTLMStream::stream_open] $path , mode=$mode \n";
356
        echo "[NTLMStream::stream_open] $path , mode=$mode \n";
357
        $this->path = $path;
357
        $this->path = $path;
358
        $this->mode = $mode;
358
        $this->mode = $mode;
359
        $this->options = $options;
359
        $this->options = $options;
360
        $this->opened_path = $opened_path;
360
        $this->opened_path = $opened_path;
361
        $this->createBuffer($path);
361
        $this->createBuffer($path);
362
        return true;
362
        return true;
363
    }
363
    }
364
 
364
 
365
    /**
365
    /**
366
     * Close the stream
366
     * Close the stream
367
     *
367
     *
368
     */
368
     */
369
    public function stream_close()
369
    public function stream_close()
370
    {
370
    {
371
        echo "[NTLMStream::stream_close] \n";
371
        echo "[NTLMStream::stream_close] \n";
372
        curl_close($this->ch);
372
        curl_close($this->ch);
373
    }
373
    }
374
 
374
 
375
    /**
375
    /**
376
     * Read the stream
376
     * Read the stream
377
     *
377
     *
378
     * @param int $count number of bytes to read
378
     * @param int $count number of bytes to read
379
     * @return content from pos to count
379
     * @return content from pos to count
380
     */
380
     */
381
    public function stream_read($count)
381
    public function stream_read($count)
382
    {
382
    {
383
        echo "[NTLMStream::stream_read] $count \n";
383
        echo "[NTLMStream::stream_read] $count \n";
384
        if(strlen($this->buffer) == 0)
384
        if(strlen($this->buffer) == 0)
385
        {
385
        {
386
            return false;
386
            return false;
387
        }
387
        }
388
        $read = substr($this->buffer,$this->pos, $count);
388
        $read = substr($this->buffer,$this->pos, $count);
389
        $this->pos += $count;
389
        $this->pos += $count;
390
        return $read;
390
        return $read;
391
    }
391
    }
392
 
392
 
393
    /***
393
    /***
394
     * write the stream
394
     * write the stream
395
     *
395
     *
396
     * @param $data
396
     * @param $data
397
     * @return
397
     * @return
398
     */
398
     */
399
    public function stream_write($data)
399
    public function stream_write($data)
400
    {
400
    {
401
        echo "[NTLMStream::stream_write] \n";
401
        echo "[NTLMStream::stream_write] \n";
402
        if(strlen($this->buffer) == 0)
402
        if(strlen($this->buffer) == 0)
403
        {
403
        {
404
            return false;
404
            return false;
405
        }
405
        }
406
        return true;
406
        return true;
407
    }
407
    }
408
 
408
 
409
    /**
409
    /**
410
     *
410
     *
411
     * @return true if eof else false
411
     * @return true if eof else false
412
     */
412
     */
413
    public function stream_eof()
413
    public function stream_eof()
414
    {
414
    {
415
        echo "[NTLMStream::stream_eof] ";
415
        echo "[NTLMStream::stream_eof] ";
416
        if($this->pos > strlen($this->buffer))
416
        if($this->pos > strlen($this->buffer))
417
        {
417
        {
418
            echo "true \n";
418
            echo "true \n";
419
            return true;
419
            return true;
420
        }
420
        }
421
        echo "false \n";
421
        echo "false \n";
422
        return false;
422
        return false;
423
    }
423
    }
424
 
424
 
425
    /**
425
    /**
426
     * @return int the position of the current read pointer
426
     * @return int the position of the current read pointer
427
     */
427
     */
428
    public function stream_tell()
428
    public function stream_tell()
429
    {
429
    {
430
        echo "[NTLMStream::stream_tell] \n";
430
        echo "[NTLMStream::stream_tell] \n";
431
        return $this->pos;
431
        return $this->pos;
432
    }
432
    }
433
 
433
 
434
    /**
434
    /**
435
     * Flush stream data
435
     * Flush stream data
436
     */
436
     */
437
    public function stream_flush()
437
    public function stream_flush()
438
    {
438
    {
439
        echo "[NTLMStream::stream_flush] \n";
439
        echo "[NTLMStream::stream_flush] \n";
440
        $this->buffer = null;
440
        $this->buffer = null;
441
        $this->pos = null;
441
        $this->pos = null;
442
    }
442
    }
443
 
443
 
444
    /**
444
    /**
445
     * Stat the file, return only the size of the buffer
445
     * Stat the file, return only the size of the buffer
446
     *
446
     *
447
     * @return array stat information
447
     * @return array stat information
448
     */
448
     */
449
    public function stream_stat()
449
    public function stream_stat()
450
    {
450
    {
451
        echo "[NTLMStream::stream_stat] \n";
451
        echo "[NTLMStream::stream_stat] \n";
452
        $this->createBuffer($this->path);
452
        $this->createBuffer($this->path);
453
        $stat = array( 'size' => strlen($this->buffer), );
453
        $stat = array( 'size' => strlen($this->buffer), );
454
        return $stat;
454
        return $stat;
455
    }
455
    }
456
 
456
 
457
    /**
457
    /**
458
     * Stat the url, return only the size of the buffer
458
     * Stat the url, return only the size of the buffer
459
     *
459
     *
460
     * @return array stat information
460
     * @return array stat information
461
     */
461
     */
462
    public function url_stat($path, $flags)
462
    public function url_stat($path, $flags)
463
    {
463
    {
464
        echo "[NTLMStream::url_stat] \n";
464
        echo "[NTLMStream::url_stat] \n";
465
        $this->createBuffer($path);
465
        $this->createBuffer($path);
466
        $stat = array( 'size' => strlen($this->buffer), );
466
        $stat = array( 'size' => strlen($this->buffer), );
467
        return $stat;
467
        return $stat;
468
    }
468
    }
469
 
469
 
470
    /* Create the buffer by requesting the url through cURL */
470
    /* Create the buffer by requesting the url through cURL */
471
    private function createBuffer($path)
471
    private function createBuffer($path)
472
    {
472
    {
473
        if($this->buffer)
473
        if($this->buffer)
474
        {
474
        {
475
            return;
475
            return;
476
        }
476
        }
477
        echo "[NTLMStream::createBuffer] create buffer from : $path \n";
477
        echo "[NTLMStream::createBuffer] create buffer from : $path \n";
478
        $this->ch = curl_init($path);
478
        $this->ch = curl_init($path);
479
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
479
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
480
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
480
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
481
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
481
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
482
        curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
482
        curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
483
        curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
483
        curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
484
        curl_setopt($this->ch, CURLOPT_USERPWD, $user.':'.$pass);
484
        curl_setopt($this->ch, CURLOPT_USERPWD, $user.':'.$pass);
485
        echo $this->buffer = curl_exec($this->ch);
485
        echo $this->buffer = curl_exec($this->ch);
486
       
486
       
487
        echo "[NTLMStream::createBuffer] buffer size : ".strlen($this->buffer)."bytes \n";
487
        echo "[NTLMStream::createBuffer] buffer size : ".strlen($this->buffer)."bytes \n";
488
        $this->pos = 0;
488
        $this->pos = 0;
489
/*$myfile = "/tmp/debugex.txt";
489
/*$myfile = "/tmp/debugex.txt";
490
$fh = fopen($myfile, 'w+') or die("can't open file");
490
$fh = fopen($myfile, 'w+') or die("can't open file");
491
fwrite($fh, "user: ". $this->user. " pass: ".$this->pass."\n");
491
fwrite($fh, "user: ". $this->user. " pass: ".$this->pass."\n");
492
fwrite($fh, "req: ". $request ."loc: ". $location ." act: ". $action ." ver: ". $version."\n");
492
fwrite($fh, "req: ". $request ."loc: ". $location ." act: ". $action ." ver: ". $version."\n");
493
fwrite($fh, "reponse: ". $response."\n\n");
493
fwrite($fh, "reponse: ". $response."\n\n");
494
fclose($fh);
494
fclose($fh);
495
*/
495
*/
496
    }
496
    }
497
}
497
}
498
 
498
 
499
?>
499
?>
500
 
500