Completed
Push — master ( e1740c...d98dea )
by Morris
14:21
created

EnablePlugin::getPluginName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2017, Georg Ehrke <[email protected]>
4
 *
5
 * @author Georg Ehrke <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\DAV\CalDAV\BirthdayCalendar;
25
26
use OCA\DAV\CalDAV\BirthdayService;
27
use OCA\DAV\CalDAV\CalendarHome;
28
use Sabre\DAV\Server;
29
use Sabre\DAV\ServerPlugin;
30
use Sabre\HTTP\RequestInterface;
31
use Sabre\HTTP\ResponseInterface;
32
use OCP\IConfig;
33
34
/**
35
 * Class EnablePlugin
36
 * allows users to re-enable the birthday calendar via CalDAV
37
 *
38
 * @package OCA\DAV\CalDAV\BirthdayCalendar
39
 */
40
class EnablePlugin extends ServerPlugin {
41
	const NS_Nextcloud = 'http://nextcloud.com/ns';
42
43
	/**
44
	 * @var IConfig
45
	 */
46
	protected $config;
47
48
	/**
49
	 * @var BirthdayService
50
	 */
51
	protected $birthdayService;
52
53
	/**
54
	 * @var Server
55
	 */
56
	protected $server;
57
58
	/**
59
	 * PublishPlugin constructor.
60
	 *
61
	 * @param IConfig $config
62
	 * @param BirthdayService $birthdayService
63
	 */
64
	public function __construct(IConfig $config, BirthdayService $birthdayService) {
65
		$this->config = $config;
66
		$this->birthdayService = $birthdayService;
67
	}
68
69
	/**
70
	 * This method should return a list of server-features.
71
	 *
72
	 * This is for example 'versioning' and is added to the DAV: header
73
	 * in an OPTIONS response.
74
	 *
75
	 * @return string[]
76
	 */
77
	public function getFeatures() {
78
		return ['nc-enable-birthday-calendar'];
79
	}
80
81
	/**
82
	 * Returns a plugin name.
83
	 *
84
	 * Using this name other plugins will be able to access other plugins
85
	 * using Sabre\DAV\Server::getPlugin
86
	 *
87
	 * @return string
88
	 */
89
	public function getPluginName()	{
90
		return 'nc-enable-birthday-calendar';
91
	}
92
93
	/**
94
	 * This initializes the plugin.
95
	 *
96
	 * This function is called by Sabre\DAV\Server, after
97
	 * addPlugin is called.
98
	 *
99
	 * This method should set up the required event subscriptions.
100
	 *
101
	 * @param Server $server
102
	 */
103
	public function initialize(Server $server) {
104
		$this->server = $server;
105
106
		$this->server->on('method:POST', [$this, 'httpPost']);
107
	}
108
109
	/**
110
	 * We intercept this to handle POST requests on calendar homes.
111
	 *
112
	 * @param RequestInterface $request
113
	 * @param ResponseInterface $response
114
	 *
115
	 * @return bool|void
116
	 */
117
	public function httpPost(RequestInterface $request, ResponseInterface $response) {
118
		$node = $this->server->tree->getNodeForPath($this->server->getRequestUri());
119
		if (!($node instanceof CalendarHome)) {
120
			return;
121
		}
122
123
		$requestBody = $request->getBodyAsString();
124
		$this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
0 ignored issues
show
Bug introduced by
The variable $documentType does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
125
		if ($documentType !== '{'.self::NS_Nextcloud.'}enable-birthday-calendar') {
126
			return;
127
		}
128
129
		$principalUri = $node->getOwner();
130
		$userId = substr($principalUri, 17);
131
132
		$this->config->setUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
133
		$this->birthdayService->syncUser($userId);
134
135
		$this->server->httpResponse->setStatus(204);
136
137
		return false;
138
	}
139
}
140