2017-07-24 17:48:17 +03:00
|
|
|
<?php
|
2017-11-06 17:56:42 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
2017-11-06 22:15:27 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2017-07-24 17:48:17 +03:00
|
|
|
namespace OCA\Files_Sharing\Controller;
|
|
|
|
|
|
|
|
use OCA\Files_External\NotFoundException;
|
|
|
|
use OCP\AppFramework\ApiController;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\Constants;
|
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\Files\Node;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
|
|
|
use OCP\Share\IManager;
|
|
|
|
|
|
|
|
class ShareInfoController extends ApiController {
|
|
|
|
|
|
|
|
/** @var IManager */
|
|
|
|
private $shareManager;
|
|
|
|
|
2017-08-10 17:25:58 +03:00
|
|
|
/**
|
|
|
|
* ShareInfoController constructor.
|
|
|
|
*
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param IManager $shareManager
|
|
|
|
*/
|
2021-05-11 16:25:31 +03:00
|
|
|
public function __construct(string $appName,
|
2017-07-24 17:48:17 +03:00
|
|
|
IRequest $request,
|
2017-08-10 17:25:58 +03:00
|
|
|
IManager $shareManager) {
|
2017-07-24 17:48:17 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
|
|
|
$this->shareManager = $shareManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
2021-05-11 16:25:31 +03:00
|
|
|
* @BruteForceProtection(action=shareinfo)
|
2017-07-24 17:48:17 +03:00
|
|
|
*
|
|
|
|
* @param string $t
|
|
|
|
* @param null $password
|
|
|
|
* @param null $dir
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
|
|
|
public function info($t, $password = null, $dir = null) {
|
|
|
|
try {
|
|
|
|
$share = $this->shareManager->getShareByToken($t);
|
|
|
|
} catch (ShareNotFound $e) {
|
2021-05-11 16:25:31 +03:00
|
|
|
$response = new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
$response->throttle(['token' => $t]);
|
|
|
|
return $response;
|
2017-07-24 17:48:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($share->getPassword() && !$this->shareManager->checkPassword($share, $password)) {
|
2021-05-11 16:25:31 +03:00
|
|
|
$response = new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
$response->throttle(['token' => $t]);
|
|
|
|
return $response;
|
2017-07-24 17:48:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!($share->getPermissions() & Constants::PERMISSION_READ)) {
|
2021-05-11 16:25:31 +03:00
|
|
|
$response = new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
$response->throttle(['token' => $t]);
|
|
|
|
return $response;
|
2017-07-24 17:48:17 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:57:54 +03:00
|
|
|
$permissionMask = $share->getPermissions();
|
2017-07-24 17:48:17 +03:00
|
|
|
$node = $share->getNode();
|
|
|
|
|
|
|
|
if ($dir !== null && $node instanceof Folder) {
|
|
|
|
try {
|
|
|
|
$node = $node->get($dir);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:57:54 +03:00
|
|
|
return new JSONResponse($this->parseNode($node, $permissionMask));
|
2017-07-24 17:48:17 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:57:54 +03:00
|
|
|
private function parseNode(Node $node, int $permissionMask) {
|
2017-07-24 17:48:17 +03:00
|
|
|
if ($node instanceof File) {
|
2019-11-21 14:57:54 +03:00
|
|
|
return $this->parseFile($node, $permissionMask);
|
2017-07-24 17:48:17 +03:00
|
|
|
}
|
2019-11-21 14:57:54 +03:00
|
|
|
return $this->parseFolder($node, $permissionMask);
|
2017-07-24 17:48:17 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:57:54 +03:00
|
|
|
private function parseFile(File $file, int $permissionMask) {
|
|
|
|
return $this->format($file, $permissionMask);
|
2017-07-24 17:48:17 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:57:54 +03:00
|
|
|
private function parseFolder(Folder $folder, int $permissionMask) {
|
|
|
|
$data = $this->format($folder, $permissionMask);
|
2017-07-24 17:48:17 +03:00
|
|
|
|
|
|
|
$data['children'] = [];
|
|
|
|
|
|
|
|
$nodes = $folder->getDirectoryListing();
|
|
|
|
foreach ($nodes as $node) {
|
2019-11-21 14:57:54 +03:00
|
|
|
$data['children'][] = $this->parseNode($node, $permissionMask);
|
2017-07-24 17:48:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:57:54 +03:00
|
|
|
private function format(Node $node, int $permissionMask) {
|
2017-07-24 17:48:17 +03:00
|
|
|
$entry = [];
|
|
|
|
|
|
|
|
$entry['id'] = $node->getId();
|
|
|
|
$entry['parentId'] = $node->getParent()->getId();
|
|
|
|
$entry['mtime'] = $node->getMTime();
|
|
|
|
|
|
|
|
$entry['name'] = $node->getName();
|
2019-11-21 14:57:54 +03:00
|
|
|
$entry['permissions'] = $node->getPermissions() & $permissionMask;
|
2017-07-24 17:48:17 +03:00
|
|
|
$entry['mimetype'] = $node->getMimetype();
|
|
|
|
$entry['size'] = $node->getSize();
|
|
|
|
$entry['type'] = $node->getType();
|
|
|
|
$entry['etag'] = $node->getEtag();
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
}
|