Test Failed
Push — master ( 647c72...cd42b5 )
by
unknown
10:25
created

server/includes/modules/class.freebusymodule.php (1 issue)

Severity
1
<?php
2
	/**
3
	 * FreeBusyModule Module
4
	 */
5
	class FreeBusyModule extends Module
6
	{
7
		function __construct($id, $data)
8
		{
9
			parent::__construct($id, $data);
10
		}
11
		
12
		function execute()
13
		{
14
			foreach($this->data as $actionType => $selUser)
15
			{
16
				if(isset($actionType)) {
17
					try {
18
						switch($actionType){
19
							case "list":
20
								$this->addUserData($selUser);
21
								break;
22
							default:
23
								$this->handleUnknownActionType($actionType);
24
						}
25
					} catch (MAPIException $e) {
26
						$this->processException($e, $actionType);
27
					}
28
				}
29
			}
30
		}
31
32
		/**
33
		 * This function will get user info from address book and add freebusy data of the user to
34
		 * response.
35
		 *
36
		 * @param {Array} $selUser User that should be resolved
37
		 */
38
		function addUserData($selUser)
39
		{
40
			$data = array();
41
			$data["users"] = array();
42
43
			foreach ($selUser["users"] as $fbUser) {
44
				$user = array();
45
46
				// Copy the identifier of the user.
47
				$user["userid"] = $fbUser["userid"]; 
48
				$user["entryid"] = $fbUser["entryid"];
49
50
				// Obtain the Freebusy data for this user
51
				$busyArray = $this->getFreeBusyInfo($fbUser["entryid"], $selUser["start"], $selUser["end"]);
52
53
				if ($busyArray) {
54
					// We have freebusy information, go over the data
55
					// and insert the blocks into the user object.
56
					foreach ($busyArray as $busyItem) {
57
						$busy = array();
58
						$busy["status"] = $busyItem["status"];
59
						$busy["start"] = $busyItem["start"];
60
						$busy["isRestrictedRange"] = isset($busyItem["isRestrictedRange"]) ? $busyItem["isRestrictedRange"] : false;
61
						$busy["end"] = $busyItem["end"];
62
						$user["items"][] = $busy;
63
					}
64
				} else {
65
					// No freebusy data available, create a single empty block
66
					$busy["status"] = -1;
67
					$busy["start"] = $selUser["start"];
68
					$busy["end"] = $selUser["end"];
69
					$user["items"][] = $busy;
70
				}
71
72
				$data["users"][] = $user;
73
			}
74
75
			$this->addActionData("list", $data);
76
			$GLOBALS["bus"]->addData($this->getResponseData());
77
		}
78
79
		/**
80
		 * This function will get freebusy data for user based on the timeframe passed in arguments.
81
		 *
82
		 * @param {String} $entryID Entryid of the user for which we need to get freebusy data
83
		 * @param {Number} $start start offset for freebusy publish range
84
		 * @param {Number} $end end offset for freebusy publish range
85
		 * @return {Array} freebusy blocks for passed publish range
86
		 */
87
		function getFreeBusyInfo($entryID, $start, $end)
88
		{
89
			$result = array();
90
			
91
			$retval = mapi_getuseravailability($GLOBALS['mapisession']->getSession(), hex2bin($entryID), $start, $end);
92
			if (empty($retval)) {
93
				return $result;
94
			}
95
			$freebusy = json_decode($retval, true);
96
			if (0 == strcasecmp($freebusy['permission'], 'none')) {
97
				return $result;
98
			}
99
			$last_end = $start;
100
			foreach ($freebusy['events'] as $event) {
101
				$blockItem = array();
102
				$blockItem['start'] = $event['StartTime'];
103
				$blockItem['end'] = $event['EndTime'];
104
				if ('Free' == $event['BusyType']) {
105
					$blockItem['status'] = 0;
106
				} else if ('Tentative' == $event['BusyType']) {
107
					$blockItem['status'] = 1;
108
				} else if ('Busy' == $event['BusyType']) {
109
					$blockItem['status'] = 2;
110
				} else if ('OOF' == $event['BusyType']) {
111
					$blockItem['status'] = 3;
112
				} else if ('WorkingElsewhere' == $event['BusyType']) {
113
					$blockItem['status'] = 4;
114
				} else {
115
					$blockItem['status'] = -1;
116
				}
117
				$last_end = $event['EndTime'];
118
				$result[] = $blockItem;	
119
			}
120
121
			// Add restricted Free/Busy range information for recipient.
122
			$noInfoItem = array();
123
			$noInfoItem["status"] = -1;
124
			$noInfoItem["isRestrictedRange"] = true;
125
			// Last day of visible Free/Busy range was first day of restricted Free/Busy range.
126
			$noInfoItem["start"] = $last_end;
127
			// Scheduler's last day.
128
			$noInfoItem["end"] = $end;
129
			$result[] = $noInfoItem;
130
			
131
			return $result;
132
		}
133
	}
134
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
135