Subversion Repositories HomeAutomation

Rev

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