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

HierarchyNotifier   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 82
dl 0
loc 168
rs 10
c 0
b 0
f 0
wmc 26

3 Methods

Rating   Name   Duplication   Size   Complexity  
D update() 0 126 24
A __construct() 0 10 1
A getEvents() 0 3 1
1
<?php
2
	/**
3
	 * Hierarchy Notifier
4
	 *
5
	 * Generates notifications which might be created
6
	 * inside the hierarchy.
7
	 */
8
	class HierarchyNotifier extends Notifier
9
	{
10
		/**
11
		 * Flag to indicate that we need to reopen store object to get updated message size
12
		 */
13
		 private $reopenStore;
14
15
		/**
16
		 * previous store size of the default store of user, this is updated whenever
17
		 * notification is sent to client to update quota details
18
		 */
19
		 private $storeSize;
20
21
		function __construct()
22
		{
23
			/*
24
			 * Initialize hierarchy store's size, so in future on change events of
25
			 * folder object or message item we can check store's previous size,
26
			 * and if it is changed then send notification to server.
27
			 */
28
			$this->storeSize = -1;
29
30
			parent::__construct();
31
		}
32
33
		/**
34
		 * @return Number Return the bitmask of events which are handled
35
		 * by this notifier. The bitmask can consist of the
36
		 * OBJECT_SAVE, OBJECT_DELETE, TABLE_SAVE, TABLE_DELETE, REQUEST_START and REQUEST_END flags
37
		 */
38
		public function getEvents()
39
		{
40
			return OBJECT_SAVE | OBJECT_DELETE | TABLE_SAVE | TABLE_DELETE | REQUEST_START | REQUEST_END;
41
		}
42
43
		/**
44
		 * If an event elsewhere has occurred, it enters in this method. This method
45
		 * executes one ore more actions, depends on the event.
46
		 * @param int $event Event.
47
		 * @param string $entryid Entryid.
48
		 * @param array $data array of data.
49
		 */
50
		public function update($event, $entryid, $props)
51
		{
52
			switch($event) {
53
				case REQUEST_START:
54
					$this->reopenStore = false;
55
					break;
56
				case OBJECT_SAVE:
57
				case TABLE_SAVE:
58
				case TABLE_DELETE:
59
					$data = array();
60
61
					// If no PR_ENTRYID is given, we will settle with PR_PARENT_ENTRYID
62
					$folderEntryid = false;
63
					if (isset($props[PR_PARENT_ENTRYID])) {
64
						$folderEntryid = $props[PR_PARENT_ENTRYID];
65
					} else if(isset($props[PR_ENTRYID])) {
66
						$folderEntryid = $props[PR_ENTRYID];
67
					}
68
69
					// We won't send notifiers for changes to the todolist folder, since there is nothing to
70
					// be updated by the client.
71
					$entryIdUtil = new EntryId();
72
					if ( $entryIdUtil->compareEntryIds(bin2hex($folderEntryid), bin2hex(TodoList::getEntryId())) ){
73
						return;
74
					}
75
76
					if (!isset($props[PR_STORE_ENTRYID]) && !$folderEntryid) {
77
						break;
78
					}
79
80
					$store = $GLOBALS["mapisession"]->openMessageStore($props[PR_STORE_ENTRYID]);
81
82
					$folder = mapi_msgstore_openentry($store, $folderEntryid);
83
					if ($folder) {
84
						$properties = $GLOBALS["properties"]->getFolderListProperties();
85
						$folderProps = mapi_getprops($folder, $properties);
86
87
						// If this folder belongs to Favorites folder,then change PARENT_ENTRYID manually.
88
						if ($GLOBALS["entryid"]->isFavoriteFolder($folderProps[PR_ENTRYID])) {
89
							$storeProps = mapi_getprops($store, array(PR_IPM_FAVORITES_ENTRYID));
90
91
							if (isset($storeProps[PR_IPM_FAVORITES_ENTRYID])) {
92
								$favFolder = mapi_msgstore_openentry($store, $storeProps[PR_IPM_FAVORITES_ENTRYID]);
93
								$favHierarchyTable = mapi_folder_gethierarchytable($favFolder, MAPI_DEFERRED_ERRORS);
94
								$folders = mapi_table_queryallrows($favHierarchyTable, array(PR_DISPLAY_NAME, PR_STORE_ENTRYID),
95
									array(RES_PROPERTY,
96
										array(
97
											RELOP => RELOP_EQ,
98
											ULPROPTAG => PR_ENTRYID,
99
											VALUE => array(
100
												PR_ENTRYID => $folderProps[PR_ENTRYID]
101
											)
102
										)
103
									)
104
								);
105
106
								if (!empty($folders)) {
107
									// Update folderProps to properties of folder which is under 'FAVORITES'
108
									$folderProps[PR_DISPLAY_NAME] = $folders[0][PR_DISPLAY_NAME];
109
									$folderProps[PR_PARENT_ENTRYID] = $storeProps[PR_IPM_FAVORITES_ENTRYID];
110
								}
111
							}
112
						}
113
114
						$data[] = $GLOBALS["operations"]->setFolder($folderProps);
115
					}
116
117
					$this->addNotificationActionData("folders", array( "item" => $data ));
118
					$GLOBALS["bus"]->addData($this->createNotificationResponseData());
119
120
					// data is changed in store so message size will be updated so reopen store to get correct data
121
					$this->reopenStore = true;
122
					break;
123
				case OBJECT_DELETE:
124
					if (isset($props[PR_ENTRYID]) && isset($props[PR_PARENT_ENTRYID])) {
125
						$data = array();
126
						$data["folderdelete"] = 1;
127
						$data["entryid"] = bin2hex($props[PR_ENTRYID]);
128
						$data["parent_entryid"] = bin2hex($props[PR_PARENT_ENTRYID]);
129
						$data["store_entryid"] = bin2hex($props[PR_STORE_ENTRYID]);
130
131
						$this->addNotificationActionData("folders", array( "item" => $data ));
132
						$GLOBALS["bus"]->addData($this->createNotificationResponseData());
133
134
						// data is changed in store so message size will be updated so reopen store to get correct data
135
						$this->reopenStore = true;
136
					}
137
					break;
138
				case REQUEST_END:
139
					if($this->reopenStore) {
140
						// @FIXME this actually very heavy operation for performance point of view
141
						// we need to remove this in future
142
						$store = $GLOBALS["mapisession"]->getDefaultMessageStore(true);
143
					} else {
144
						$store = $GLOBALS["mapisession"]->getDefaultMessageStore();
145
					}
146
147
					// reset flag for further use
148
					$this->reopenStore = false;
149
150
					// Send notification for any changes in store's properties
151
					if($store) {
152
						$storeProps = mapi_getprops($store, array(PR_ENTRYID, PR_STORE_ENTRYID, PR_MDB_PROVIDER, PR_OBJECT_TYPE, PR_QUOTA_WARNING_THRESHOLD, PR_QUOTA_SEND_THRESHOLD, PR_QUOTA_RECEIVE_THRESHOLD, PR_MESSAGE_SIZE_EXTENDED));
153
154
						$storeSize = round($storeProps[PR_MESSAGE_SIZE_EXTENDED]/1024);
155
156
						// Check whether size of the store is changed, if it is changed then
157
						// send latest store size and quota information to client-side.
158
						if($this->storeSize != $storeSize) {
159
							$data = array();
160
							$data["props"] = array();
161
							$data["store_entryid"] = bin2hex($storeProps[PR_STORE_ENTRYID]);
162
							$data["props"]["object_type"] = $storeProps[PR_OBJECT_TYPE];
163
							$data["props"]["store_size"] = $storeSize;
164
							$data["props"]["quota_warning"] = isset($storeProps[PR_QUOTA_WARNING_THRESHOLD]) ? $storeProps[PR_QUOTA_WARNING_THRESHOLD] : 0;
165
							$data["props"]["quota_soft"] = isset($storeProps[PR_QUOTA_SEND_THRESHOLD]) ? $storeProps[PR_QUOTA_SEND_THRESHOLD] : 0;
166
							$data["props"]["quota_hard"] = isset($storeProps[PR_QUOTA_RECEIVE_THRESHOLD]) ? $storeProps[PR_QUOTA_RECEIVE_THRESHOLD] : 0;
167
168
							$this->addNotificationActionData("stores", array( "item" => array($data) ));
169
							$GLOBALS["bus"]->addData($this->createNotificationResponseData());
170
171
							// assign size for next checking
172
							$this->storeSize = $storeSize;
173
						}
174
					}
175
					break;
176
			}
177
		}
178
	}
179
?>
0 ignored issues
show
Best Practice introduced by
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...
180