Subversion Repositories HomeAutomation

Rev

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

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