Passed
Push — master ( 82a437...93dc0a )
by
unknown
13:29 queued 14s
created

class.freebusy.php (10 issues)

1
<?php
2
3
/**
4
 * This class is just static class and will not be instantiate and
5
 * It contains the functionality to get freebusy message and folder.
6
 */
7
class FreeBusy {
8
	/**
9
	 *  PR_FREEBUSY_ENTRYIDS contains 4 entryids
10
	 *	PR_FREEBUSY_ENTRYIDS[0] gives associated freebusy folder in calendar
11
	 *	PR_FREEBUSY_ENTRYIDS[1] Localfreebusy (used for delegate properties)
12
	 *	PR_FREEBUSY_ENTRYIDS[2] global Freebusydata in public store
13
	 *	PR_FREEBUSY_ENTRYIDS[3] Freebusydata in IPM_SUBTREE.
14
	 */
15
	public const ASSOCIATED_FREEBUSY_FOLDER = 0;
16
	public const DELEGATE_PROPERTIES = 1;
17
	public const GLOBAL_FREEBUSYDATA = 2;
18
	public const FREEBUSYDATA_IPM_SUBTREE = 3;
19
20
	/**
21
	 * Function will return resource of the local freebusy message of the user's store.
22
	 *
23
	 * @param mixed $store (optional) user's store
24
	 *
25
	 * @return bool|resource local freebusy message, otherwise false if message not found
26
	 */
27
	public static function getLocalFreeBusyMessage($store = false) {
28
		if (!$store) {
29
			error_log("getLocalFreeBusyMessage: store not available");
30
31
			return false;
32
		}
33
34
		// Check for mapi_freebusy_openmsg function,
35
		// If yes then use mapi function to get freebusy message.
36
		if (function_exists('mapi_freebusy_openmsg')) {
37
			return mapi_freebusy_openmsg($store);
38
		}
39
40
		// Get 'LocalFreeBusy' message from FreeBusy Store
41
		$root = mapi_msgstore_openentry($store, null);
42
		$storeProps = mapi_getprops($root, [PR_FREEBUSY_ENTRYIDS]);
0 ignored issues
show
It seems like $root can also be of type false; however, parameter $any of mapi_getprops() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
		$storeProps = mapi_getprops(/** @scrutinizer ignore-type */ $root, [PR_FREEBUSY_ENTRYIDS]);
Loading history...
43
		$localFreeBusyEntryids = $storeProps[PR_FREEBUSY_ENTRYIDS];
44
45
		try {
46
			return mapi_msgstore_openentry($store, $localFreeBusyEntryids[self::DELEGATE_PROPERTIES]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return mapi_msgstore_ope...::DELEGATE_PROPERTIES]) also could return the type resource which is incompatible with the documented return type boolean|resource.
Loading history...
47
		}
48
		catch (MAPIException $e) {
49
			// Either user store have malformed entryid in PR_FREEBUSY_ENTRYIDS or
50
			// No message found of given entryid in 'Freebusy Data' folder.
51
			if ($e->getCode() == MAPI_E_NOT_FOUND || $e->getCode() == MAPI_E_INVALID_ENTRYID) {
52
				$freeBusyFolder = mapi_msgstore_openentry($store, $localFreeBusyEntryids[self::FREEBUSYDATA_IPM_SUBTREE]);
53
				$table = mapi_folder_getcontentstable($freeBusyFolder);
0 ignored issues
show
It seems like $freeBusyFolder can also be of type false; however, parameter $fld of mapi_folder_getcontentstable() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
				$table = mapi_folder_getcontentstable(/** @scrutinizer ignore-type */ $freeBusyFolder);
Loading history...
54
				mapi_table_restrict(
55
					$table,
0 ignored issues
show
It seems like $table can also be of type false; however, parameter $table of mapi_table_restrict() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
					/** @scrutinizer ignore-type */ $table,
Loading history...
56
					[
57
						RES_CONTENT,
58
						[
59
							FUZZYLEVEL => FL_PREFIX,
60
							ULPROPTAG => PR_MESSAGE_CLASS,
61
							VALUE => [PR_MESSAGE_CLASS => "IPM.Microsoft.ScheduleData.FreeBusy"],
62
						],
63
					]
64
				);
65
66
				$items = mapi_table_queryallrows($table, [PR_ENTRYID]);
0 ignored issues
show
It seems like $table can also be of type false; however, parameter $table of mapi_table_queryallrows() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
				$items = mapi_table_queryallrows(/** @scrutinizer ignore-type */ $table, [PR_ENTRYID]);
Loading history...
67
				if (empty($items)) {
68
					// FIXME recreate local freebusy message in 'Freebusy Data' folder.
69
					error_log("Unable to find local free busy message in 'Freebusy Data' folder");
70
71
					return false;
72
				}
73
74
				$localFreeBusyEntryids[1] = $items[0][PR_ENTRYID];
75
76
				// Updating the entryid in the PR_FREEBUSY_ENTRYIDS property of user store.
77
				mapi_setprops($root, [PR_FREEBUSY_ENTRYIDS => $localFreeBusyEntryids]);
0 ignored issues
show
It seems like $root can also be of type false; however, parameter $any of mapi_setprops() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
				mapi_setprops(/** @scrutinizer ignore-type */ $root, [PR_FREEBUSY_ENTRYIDS => $localFreeBusyEntryids]);
Loading history...
78
				mapi_savechanges($root);
0 ignored issues
show
It seems like $root can also be of type false; however, parameter $any of mapi_savechanges() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
				mapi_savechanges(/** @scrutinizer ignore-type */ $root);
Loading history...
79
80
				return mapi_msgstore_openentry($store, $localFreeBusyEntryids[self::DELEGATE_PROPERTIES]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return mapi_msgstore_ope...::DELEGATE_PROPERTIES]) also could return the type resource which is incompatible with the documented return type boolean|resource.
Loading history...
81
			}
82
83
			// Ensure to return false if an exception occurs
84
			error_log("getLocalFreeBusyMessage: unhandled MAPIException " . $e->getMessage());
85
		        return false;
86
87
		}
88
89
		// Fallback, should not typically reach here.
90
		error_log("getLocalFreeBusyMessage: reached unexpected code path");
91
		return false;
92
93
	}
94
95
	/**
96
	 * Function will return resource of the freebusy folder of the user's store.
97
	 *
98
	 * @param mixed $store (optional) user's store
99
	 *
100
	 * @return bool|resource freebusy folder
101
	 */
102
	public static function getLocalFreeBusyFolder($store = false) {
103
		if (!$store) {
104
			error_log("getLocalFreeBusyFolder: store not available");
105
106
			return false;
107
		}
108
		// Get 'LocalFreeBusy' message from FreeBusy Store
109
		$root = mapi_msgstore_openentry($store, null);
110
		$storeProps = mapi_getprops($root, [PR_FREEBUSY_ENTRYIDS]);
0 ignored issues
show
It seems like $root can also be of type false; however, parameter $any of mapi_getprops() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
		$storeProps = mapi_getprops(/** @scrutinizer ignore-type */ $root, [PR_FREEBUSY_ENTRYIDS]);
Loading history...
111
112
		return mapi_msgstore_openentry($store, $storeProps[PR_FREEBUSY_ENTRYIDS][self::FREEBUSYDATA_IPM_SUBTREE]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return mapi_msgstore_ope...EBUSYDATA_IPM_SUBTREE]) also could return the type resource which is incompatible with the documented return type boolean|resource.
Loading history...
113
	}
114
}
115