Subversion Repositories HomeAutomation

Rev

Rev 1164 | Rev 1169 | 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
 
1165 arune 57
        $returndata = "<calendar>\n\t<shortname>".$shortname."</shortname>\n";
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
            {
1163 arune 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
                {
1162 arune 71
                if (count($calendaritems) > 1)
72
                {
73
                    $item = $calendaritems[$i];
74
                }
75
                else
76
                {
77
                    $item = $calendaritems;
78
                }
1165 arune 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";
1163 arune 84
                }
1160 arune 85
            }
86
 
87
        }
88
        catch (SoapFault $exception)
89
        {
90
            return "<error>".$exception."</error>";
91
        }
1151 arune 92
 
1164 arune 93
        stream_wrapper_restore('https');
94
        $returndata .= "</calendar>\n";
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
{
1163 arune 106
    echo "<pre>\n";
107
    print_r($var);
108
    echo "</pre>\n";
1151 arune 109
}
110
 
111
class NTLMSoapClient extends SoapClient {
1163 arune 112
    function __doRequest($request, $location, $action, $version)
113
    {
114
        global $exchangeurl;
115
        $location=$exchangeurl; //override url to exchange server
1159 arune 116
 
1163 arune 117
        $headers = array( 'Method: POST',
1159 arune 118
            'Connection: Keep-Alive',
119
            'User-Agent: PHP-SOAP-CURL',
120
            'Content-Type: text/xml; charset=utf-8',
121
            'SOAPAction: "'.$action.'"',
122
            );
1163 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);
133
        curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass);
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
*/
1163 arune 143
 
144
        return $response;
145
    }
146
 
147
    function __getLastRequestHeaders()
148
    {
149
        return implode("\n", $this->__last_request_headers)."\n";
150
    }
1151 arune 151
}
152
 
1161 arune 153
class ExchangeNTLMSoapClient extends NTLMSoapClient
154
{
155
    public $user = '';
156
    public $password = '';
157
}
1151 arune 158
 
159
class NTLMStream
160
{
1163 arune 161
    private $path;
162
    private $mode;
163
    private $options;
164
    private $opened_path;
165
    private $buffer;
166
    private $pos;
1151 arune 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
     */
1163 arune 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
    }
1151 arune 187
 
1159 arune 188
    /**
189
     * Close the stream
190
     *
191
     */
1163 arune 192
    public function stream_close()
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
     */
1163 arune 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
     */
1163 arune 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
     */
1163 arune 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
     */
1163 arune 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
     */
1163 arune 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
     */
1163 arune 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
     */
1163 arune 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
 
1163 arune 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
    }
1159 arune 320
}
1151 arune 321
 
322
?>