Subversion Repositories HomeAutomation

Rev

Rev 1159 | Rev 1161 | 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":
17
        break;
18
 
19
        //case "":
20
        //
21
        //break;
1159 arune 22
}
23
 
1160 arune 24
function getMeetingsRestOfDay($shortname)
1151 arune 25
{
1160 arune 26
    global $Calender;
27
    global $wsdl;
28
    $founditem = false;
29
 
30
    foreach($Calender as $item)
31
    {
32
        if ($item->Shortname == $shortname)
33
        {
34
            $founditem = $item;
35
            break;
36
        }
37
    }
38
 
39
    if ($founditem !== false)
40
    {
41
        $user = $founditem->UserName;
42
        $pass = $founditem->Password;
43
 
44
        $FindItem->Traversal = "Shallow";
45
        $FindItem->ItemShape->BaseShape = "AllProperties";
46
        $FindItem->ParentFolderIds->DistinguishedFolderId->Id = "calendar";
47
        $FindItem->CalendarView->StartDate = "2009-04-07T00:00:00Z";
48
        $FindItem->CalendarView->EndDate = "2009-04-08T00:00:00Z";
49
 
50
        stream_wrapper_unregister('https');
51
        stream_wrapper_register('https', 'NTLMStream') or die("Failed to register protocol");
52
 
53
        try
54
        {
55
            $client = new NTLMSoapClient($wsdl);
56
            $result = $client->FindItem($FindItem);
57
            if ($result)
58
            {
59
              //print_array($result);
60
              $calendaritems = $result->ResponseMessages->FindItemResponseMessage->RootFolder->Items->CalendarItem;
61
              //print_array($calendaritems);
62
              foreach($calendaritems as $item)
63
              {
64
                return "Organizer: ".$item->Organizer->Mailbox->Name." Start: ".$item->Start." End: ".$item->End."<br>";
65
              }
66
            }
67
 
68
        }
69
        catch (SoapFault $exception)
70
        {
71
            return "<error>".$exception."</error>";
72
        }
1151 arune 73
 
1160 arune 74
        stream_wrapper_restore('https');
75
    }
76
    else
77
    {
78
        return "<error>Unknown shortname</error>";
79
    }
1151 arune 80
 
81
 
1160 arune 82
//echo "<br>OBS tidszon+sommartid?? nu sommartid, ett möte som ovan gav start 13:00 var egentligen 15:00<br>OBS utf? teckenkodning<br>";
83
}
1151 arune 84
 
85
 
86
function print_array($var)
87
{
88
    echo "<pre>\n";
89
    print_r($var);
90
    echo "</pre>\n";
91
}
92
 
93
class NTLMSoapClient extends SoapClient {
94
  function __doRequest($request, $location, $action, $version)
95
  {
1160 arune 96
    global $user;
97
    global $pass;
98
    global $exchangeurl;
1159 arune 99
    $location=$exchangeurl; //override url to exchange server
100
 
101
    $headers = array( 'Method: POST',
102
            'Connection: Keep-Alive',
103
            'User-Agent: PHP-SOAP-CURL',
104
            'Content-Type: text/xml; charset=utf-8',
105
            'SOAPAction: "'.$action.'"',
106
            );
1151 arune 107
    $this->__last_request_headers = $headers;
108
    $ch = curl_init($location);
109
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
110
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
111
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
112
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
113
    curl_setopt($ch, CURLOPT_POST, true );
114
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
115
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
116
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
1160 arune 117
    curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$pass);
1151 arune 118
    $response = curl_exec($ch);
1159 arune 119
 
120
//$myfile = "/tmp/debugex.txt";
121
//$fh = fopen($myfile, 'a+') or die("can't open file");
122
//fwrite($fh, "req: ". $request ."loc: ". $location ." act: ". $action ." ver: ". $version."\n");
123
//fwrite($fh, "reponse: ". $response."\n\n");
124
//fclose($fh);
125
 
1151 arune 126
    return $response;
127
  }
128
 
129
  function __getLastRequestHeaders()
130
  {
131
    return implode("\n", $this->__last_request_headers)."\n";
132
  }
133
}
134
 
135
 
136
class NTLMStream
137
{
138
  private $path;
139
  private $mode;
140
  private $options;
141
  private $opened_path;
142
  private $buffer;
143
  private $pos;
144
 
1159 arune 145
    /**
146
     * Open the stream
147
     *
148
     * @param unknown_type $path
149
     * @param unknown_type $mode
150
     * @param unknown_type $options
151
     * @param unknown_type $opened_path
152
     * @return unknown
153
     */
1151 arune 154
  public function stream_open($path, $mode, $options, $opened_path)
155
  {
1159 arune 156
    echo "[NTLMStream::stream_open] $path , mode=$mode \n";
157
    $this->path = $path;
158
    $this->mode = $mode;
159
    $this->options = $options;
160
    $this->opened_path = $opened_path;
161
    $this->createBuffer($path);
162
    return true;
1151 arune 163
  }
164
 
1159 arune 165
    /**
166
     * Close the stream
167
     *
168
     */
1151 arune 169
  public function stream_close()
1159 arune 170
  {
171
    echo "[NTLMStream::stream_close] \n";
172
    curl_close($this->ch);
173
  }
1151 arune 174
 
1159 arune 175
    /**
176
     * Read the stream
177
     *
178
     * @param int $count number of bytes to read
179
     * @return content from pos to count
180
     */
181
  public function stream_read($count)
182
  {
183
    echo "[NTLMStream::stream_read] $count \n";
184
    if(strlen($this->buffer) == 0)
185
    {
186
      return false;
187
    }
188
    $read = substr($this->buffer,$this->pos, $count);
189
    $this->pos += $count;
190
    return $read;
191
  }
1151 arune 192
 
1159 arune 193
    /***
194
     * write the stream
195
     *
196
     * @param $data
197
     * @return
198
     */
199
  public function stream_write($data)
200
  {
201
    echo "[NTLMStream::stream_write] \n";
202
    if(strlen($this->buffer) == 0)
203
    {
204
      return false;
205
    }
206
    return true;
207
  }
1151 arune 208
 
1159 arune 209
    /**
210
     *
211
     * @return true if eof else false
212
     */
213
  public function stream_eof()
214
  {
215
    echo "[NTLMStream::stream_eof] ";
216
    if($this->pos > strlen($this->buffer))
217
    {
218
      echo "true \n";
219
      return true;
220
    }
221
    echo "false \n";
222
    return false;
223
  }
1151 arune 224
 
1159 arune 225
    /**
226
     * @return int the position of the current read pointer
227
     */
228
  public function stream_tell()
229
  {
230
    echo "[NTLMStream::stream_tell] \n";
231
    return $this->pos;
232
  }
1151 arune 233
 
1159 arune 234
    /**
235
     * Flush stream data
236
     */
237
  public function stream_flush()
238
  {
239
    echo "[NTLMStream::stream_flush] \n";
240
    $this->buffer = null;
241
    $this->pos = null;
242
  }
1151 arune 243
 
1159 arune 244
    /**
245
     * Stat the file, return only the size of the buffer
246
     *
247
     * @return array stat information
248
     */
249
  public function stream_stat()
250
  {
251
    echo "[NTLMStream::stream_stat] \n";
252
    $this->createBuffer($this->path);
253
    $stat = array( 'size' => strlen($this->buffer), );
254
    return $stat;
255
  }
1151 arune 256
 
1159 arune 257
    /**
258
     * Stat the url, return only the size of the buffer
259
     *
260
     * @return array stat information
261
     */
262
  public function url_stat($path, $flags)
263
  {
264
    echo "[NTLMStream::url_stat] \n";
265
    $this->createBuffer($path);
266
    $stat = array( 'size' => strlen($this->buffer), );
267
    return $stat;
268
  }
1151 arune 269
 
1159 arune 270
  /* Create the buffer by requesting the url through cURL */
271
  private function createBuffer($path)
272
  {
1160 arune 273
    global $user;
274
    global $pass;
1159 arune 275
    if($this->buffer)
276
    {
277
      return;
278
    }
279
    echo "[NTLMStream::createBuffer] create buffer from : $path \n";
280
    $this->ch = curl_init($path);
1151 arune 281
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
282
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
1159 arune 283
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
284
    curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
285
    curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
1160 arune 286
    curl_setopt($this->ch, CURLOPT_USERPWD, $user.':'.$pass);
1159 arune 287
    echo $this->buffer = curl_exec($this->ch);
288
 
289
    echo "[NTLMStream::createBuffer] buffer size : ".strlen($this->buffer)."bytes \n";
290
    $this->pos = 0;
291
  }
292
}
1151 arune 293
 
294
?>