1<?php
2/**
3 * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
4 *
5 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
6 * @author Joas Schilling <coding@schilljs.com>
7 * @author Morris Jobke <hey@morrisjobke.de>
8 *
9 * @license GNU AGPL version 3 or any later version
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as
13 * published by the Free Software Foundation, either version 3 of the
14 * License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25namespace OCA\WorkflowEngine\Check;
26
27use OCP\IL10N;
28use OCP\IRequest;
29
30class RequestUserAgent extends AbstractStringCheck {
31
32	/** @var IRequest */
33	protected $request;
34
35	/**
36	 * @param IL10N $l
37	 * @param IRequest $request
38	 */
39	public function __construct(IL10N $l, IRequest $request) {
40		parent::__construct($l);
41		$this->request = $request;
42	}
43
44	/**
45	 * @param string $operator
46	 * @param string $value
47	 * @return bool
48	 */
49	public function executeCheck($operator, $value) {
50		$actualValue = $this->getActualValue();
51		if (in_array($operator, ['is', '!is'], true)) {
52			switch ($value) {
53				case 'android':
54					$operator = $operator === 'is' ? 'matches' : '!matches';
55					$value = IRequest::USER_AGENT_CLIENT_ANDROID;
56					break;
57				case 'ios':
58					$operator = $operator === 'is' ? 'matches' : '!matches';
59					$value = IRequest::USER_AGENT_CLIENT_IOS;
60					break;
61				case 'desktop':
62					$operator = $operator === 'is' ? 'matches' : '!matches';
63					$value = IRequest::USER_AGENT_CLIENT_DESKTOP;
64					break;
65				case 'mail':
66					if ($operator === 'is') {
67						return $this->executeStringCheck('matches', IRequest::USER_AGENT_OUTLOOK_ADDON, $actualValue)
68							|| $this->executeStringCheck('matches', IRequest::USER_AGENT_THUNDERBIRD_ADDON, $actualValue);
69					}
70
71					return $this->executeStringCheck('!matches', IRequest::USER_AGENT_OUTLOOK_ADDON, $actualValue)
72						&& $this->executeStringCheck('!matches', IRequest::USER_AGENT_THUNDERBIRD_ADDON, $actualValue);
73			}
74		}
75		return $this->executeStringCheck($operator, $value, $actualValue);
76	}
77
78	/**
79	 * @return string
80	 */
81	protected function getActualValue() {
82		return $this->request->getHeader('User-Agent');
83	}
84
85	public function isAvailableForScope(int $scope): bool {
86		return true;
87	}
88}
89