Completed
Push — master ( 299ef9...8ce5ad )
by Joas
30:20
created

CalendarObject::canWrite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 * @copyright Copyright (c) 2017, Georg Ehrke
5
 *
6
 * @author Thomas Müller <[email protected]>
7
 * @author Georg Ehrke <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
26
namespace OCA\DAV\CalDAV;
27
28
29
use Sabre\VObject\Component;
30
use Sabre\VObject\Property;
31
use Sabre\VObject\Reader;
32
33
class CalendarObject extends \Sabre\CalDAV\CalendarObject {
34
35
	/**
36
	 * @inheritdoc
37
	 */
38
	function get() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
		$data = parent::get();
40
41
		if (!$this->isShared()) {
42
			return $data;
43
		}
44
45
		$vObject = Reader::read($data);
46
47
		// remove VAlarms if calendar is shared read-only
48
		if (!$this->canWrite()) {
49
			$this->removeVAlarms($vObject);
50
		}
51
52
		// shows as busy if event is declared confidential
53
		if ($this->objectData['classification'] === CalDavBackend::CLASSIFICATION_CONFIDENTIAL) {
54
			$this->createConfidentialObject($vObject);
55
		}
56
57
		return $vObject->serialize();
58
	}
59
60 View Code Duplication
	protected function isShared() {
61
		if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
62
			return false;
63
		}
64
65
		return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
66
	}
67
68
	/**
69
	 * @param Component\VCalendar $vObject
70
	 * @return void
71
	 */
72
	private static function createConfidentialObject(Component\VCalendar $vObject) {
73
		/** @var Component $vElement */
74
		$vElement = null;
75
		if(isset($vObject->VEVENT)) {
76
			$vElement = $vObject->VEVENT;
77
		}
78
		if(isset($vObject->VJOURNAL)) {
79
			$vElement = $vObject->VJOURNAL;
80
		}
81
		if(isset($vObject->VTODO)) {
82
			$vElement = $vObject->VTODO;
83
		}
84
		if(!is_null($vElement)) {
85
			foreach ($vElement->children() as &$property) {
0 ignored issues
show
Bug introduced by
The expression $vElement->children() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
86
				/** @var Property $property */
87
				switch($property->name) {
88
					case 'CREATED':
89
					case 'DTSTART':
90
					case 'RRULE':
91
					case 'DURATION':
92
					case 'DTEND':
93
					case 'CLASS':
94
					case 'UID':
95
						break;
96
					case 'SUMMARY':
97
						$property->setValue('Busy');
98
						break;
99
					default:
100
						$vElement->__unset($property->name);
101
						unset($property);
102
						break;
103
				}
104
			}
105
		}
106
	}
107
108
	/**
109
	 * @param Component\VCalendar $vObject
110
	 * @return void
111
	 */
112
	private function removeVAlarms(Component\VCalendar $vObject) {
113
		$subcomponents = $vObject->getComponents();
114
115
		foreach($subcomponents as $subcomponent) {
116
			unset($subcomponent->VALARM);
117
		}
118
	}
119
120
	/**
121
	 * @return bool
122
	 */
123
	private function canWrite() {
124
		if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
125
			return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
126
		}
127
		return true;
128
	}
129
}
130