1<?php
2
3declare(strict_types=1);
4
5/**
6 * @copyright Copyright (c) 2018 Daniel Kesselberg <mail@danielkesselberg.de>
7 *
8 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
9 * @author Daniel Kesselberg <mail@danielkesselberg.de>
10 * @author szaimen <szaimen@e.mail.de>
11 *
12 * @license GNU AGPL version 3 or any later version
13 *
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Affero General Public License as
16 * published by the Free Software Foundation, either version 3 of the
17 * License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Affero General Public License for more details.
23 *
24 * You should have received a copy of the GNU Affero General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28namespace OCA\WorkflowEngine\Check;
29
30use OC\Files\Storage\Local;
31use OCA\WorkflowEngine\Entity\File;
32use OCP\Files\Mount\IMountManager;
33use OCP\IL10N;
34use OCP\IRequest;
35use OCP\WorkflowEngine\IFileCheck;
36
37class FileName extends AbstractStringCheck implements IFileCheck {
38	use TFileCheck;
39
40	/** @var IRequest */
41	protected $request;
42	/** @var IMountManager */
43	private $mountManager;
44
45	/**
46	 * @param IL10N $l
47	 * @param IRequest $request
48	 */
49	public function __construct(IL10N $l, IRequest $request, IMountManager $mountManager) {
50		parent::__construct($l);
51		$this->request = $request;
52		$this->mountManager = $mountManager;
53	}
54
55	/**
56	 * @return string
57	 */
58	protected function getActualValue(): string {
59		$fileName = $this->path === null ? '' : basename($this->path);
60		if ($fileName === '' && (!$this->storage->isLocal() || $this->storage->instanceOfStorage(Local::class))) {
61			// Return the mountpoint name of external storage that are not mounted as user home
62			$mountPoints = $this->mountManager->findByStorageId($this->storage->getId());
63			if (empty($mountPoints) || $mountPoints[0]->getMountType() !== 'external') {
64				return $fileName;
65			}
66			$mountPointPath = rtrim($mountPoints[0]->getMountPoint(), '/');
67			$mountPointPieces = explode('/', $mountPointPath);
68			$mountPointName = array_pop($mountPointPieces);
69			if (!empty($mountPointName) && $mountPointName !== 'files' && count($mountPointPieces) !== 2) {
70				return $mountPointName;
71			}
72		}
73		return $fileName;
74	}
75
76	/**
77	 * @param string $operator
78	 * @param string $checkValue
79	 * @param string $actualValue
80	 * @return bool
81	 */
82	protected function executeStringCheck($operator, $checkValue, $actualValue): bool {
83		if ($operator === 'is' || $operator === '!is') {
84			$checkValue = mb_strtolower($checkValue);
85			$actualValue = mb_strtolower($actualValue);
86		}
87		return parent::executeStringCheck($operator, $checkValue, $actualValue);
88	}
89
90	public function supportedEntities(): array {
91		return [ File::class ];
92	}
93
94	public function isAvailableForScope(int $scope): bool {
95		return true;
96	}
97}
98