2017-03-14 18:18:20 +00:00
|
|
|
<?php
|
2019-05-28 17:46:36 +00:00
|
|
|
|
2017-03-14 18:18:20 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
|
2017-03-14 18:18:20 +00: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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-05-28 17:46:36 +00:00
|
|
|
|
2017-03-14 18:18:20 +00:00
|
|
|
namespace OC\Share20;
|
|
|
|
|
2017-08-03 13:14:29 +00:00
|
|
|
use OCP\Files\File;
|
2017-03-14 18:18:20 +00:00
|
|
|
use OCP\Share\IShare;
|
2019-05-28 17:46:36 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2017-03-14 18:18:20 +00:00
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2018-01-25 22:16:13 +00:00
|
|
|
use OCP\Share;
|
2017-03-14 18:18:20 +00:00
|
|
|
|
|
|
|
class LegacyHooks {
|
2019-05-28 17:46:36 +00:00
|
|
|
|
|
|
|
/** @var EventDispatcherInterface */
|
2017-03-14 18:18:20 +00:00
|
|
|
private $eventDispatcher;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LegacyHooks constructor.
|
|
|
|
*
|
2019-05-28 17:46:36 +00:00
|
|
|
* @param EventDispatcherInterface $eventDispatcher
|
2017-03-14 18:18:20 +00:00
|
|
|
*/
|
2019-05-28 17:46:36 +00:00
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher) {
|
2017-03-14 18:18:20 +00:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
|
|
|
|
|
|
|
$this->eventDispatcher->addListener('OCP\Share::preUnshare', [$this, 'preUnshare']);
|
|
|
|
$this->eventDispatcher->addListener('OCP\Share::postUnshare', [$this, 'postUnshare']);
|
2017-07-16 14:26:11 +00:00
|
|
|
$this->eventDispatcher->addListener('OCP\Share::postUnshareFromSelf', [$this, 'postUnshareFromSelf']);
|
2017-08-03 13:14:29 +00:00
|
|
|
$this->eventDispatcher->addListener('OCP\Share::preShare', [$this, 'preShare']);
|
|
|
|
$this->eventDispatcher->addListener('OCP\Share::postShare', [$this, 'postShare']);
|
2017-03-14 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param GenericEvent $e
|
|
|
|
*/
|
|
|
|
public function preUnshare(GenericEvent $e) {
|
|
|
|
/** @var IShare $share */
|
|
|
|
$share = $e->getSubject();
|
|
|
|
|
|
|
|
$formatted = $this->formatHookParams($share);
|
2018-01-25 22:16:13 +00:00
|
|
|
\OC_Hook::emit(Share::class, 'pre_unshare', $formatted);
|
2017-03-14 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param GenericEvent $e
|
|
|
|
*/
|
|
|
|
public function postUnshare(GenericEvent $e) {
|
|
|
|
/** @var IShare $share */
|
|
|
|
$share = $e->getSubject();
|
|
|
|
|
|
|
|
$formatted = $this->formatHookParams($share);
|
|
|
|
|
|
|
|
/** @var IShare[] $deletedShares */
|
|
|
|
$deletedShares = $e->getArgument('deletedShares');
|
|
|
|
|
|
|
|
$formattedDeletedShares = array_map(function($share) {
|
|
|
|
return $this->formatHookParams($share);
|
|
|
|
}, $deletedShares);
|
|
|
|
|
|
|
|
$formatted['deletedShares'] = $formattedDeletedShares;
|
|
|
|
|
2018-01-25 22:16:13 +00:00
|
|
|
\OC_Hook::emit(Share::class, 'post_unshare', $formatted);
|
2017-03-14 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
2017-07-16 14:26:11 +00:00
|
|
|
/**
|
|
|
|
* @param GenericEvent $e
|
|
|
|
*/
|
|
|
|
public function postUnshareFromSelf(GenericEvent $e) {
|
|
|
|
/** @var IShare $share */
|
|
|
|
$share = $e->getSubject();
|
|
|
|
|
|
|
|
$formatted = $this->formatHookParams($share);
|
|
|
|
$formatted['itemTarget'] = $formatted['fileTarget'];
|
|
|
|
$formatted['unsharedItems'] = [$formatted];
|
|
|
|
|
2018-01-25 22:16:13 +00:00
|
|
|
\OC_Hook::emit(Share::class, 'post_unshareFromSelf', $formatted);
|
2017-07-16 14:26:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 18:18:20 +00:00
|
|
|
private function formatHookParams(IShare $share) {
|
|
|
|
// Prepare hook
|
|
|
|
$shareType = $share->getShareType();
|
|
|
|
$sharedWith = '';
|
|
|
|
if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
|
|
|
|
$shareType === \OCP\Share::SHARE_TYPE_GROUP ||
|
|
|
|
$shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
|
|
|
|
$sharedWith = $share->getSharedWith();
|
|
|
|
}
|
|
|
|
|
|
|
|
$hookParams = [
|
|
|
|
'id' => $share->getId(),
|
|
|
|
'itemType' => $share->getNodeType(),
|
|
|
|
'itemSource' => $share->getNodeId(),
|
|
|
|
'shareType' => $shareType,
|
|
|
|
'shareWith' => $sharedWith,
|
|
|
|
'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
|
|
|
|
'uidOwner' => $share->getSharedBy(),
|
|
|
|
'fileSource' => $share->getNodeId(),
|
|
|
|
'fileTarget' => $share->getTarget()
|
|
|
|
];
|
|
|
|
return $hookParams;
|
|
|
|
}
|
2017-08-03 13:14:29 +00:00
|
|
|
|
|
|
|
public function preShare(GenericEvent $e) {
|
|
|
|
/** @var IShare $share */
|
|
|
|
$share = $e->getSubject();
|
|
|
|
|
|
|
|
// Pre share hook
|
|
|
|
$run = true;
|
|
|
|
$error = '';
|
|
|
|
$preHookData = [
|
|
|
|
'itemType' => $share->getNode() instanceof File ? 'file' : 'folder',
|
|
|
|
'itemSource' => $share->getNode()->getId(),
|
|
|
|
'shareType' => $share->getShareType(),
|
|
|
|
'uidOwner' => $share->getSharedBy(),
|
|
|
|
'permissions' => $share->getPermissions(),
|
|
|
|
'fileSource' => $share->getNode()->getId(),
|
|
|
|
'expiration' => $share->getExpirationDate(),
|
|
|
|
'token' => $share->getToken(),
|
|
|
|
'itemTarget' => $share->getTarget(),
|
|
|
|
'shareWith' => $share->getSharedWith(),
|
|
|
|
'run' => &$run,
|
|
|
|
'error' => &$error,
|
|
|
|
];
|
2018-01-25 22:16:13 +00:00
|
|
|
\OC_Hook::emit(Share::class, 'pre_shared', $preHookData);
|
2017-08-03 13:14:29 +00:00
|
|
|
|
|
|
|
if ($run === false) {
|
|
|
|
$e->setArgument('error', $error);
|
|
|
|
$e->stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postShare(GenericEvent $e) {
|
|
|
|
/** @var IShare $share */
|
|
|
|
$share = $e->getSubject();
|
|
|
|
|
|
|
|
$postHookData = [
|
|
|
|
'itemType' => $share->getNode() instanceof File ? 'file' : 'folder',
|
|
|
|
'itemSource' => $share->getNode()->getId(),
|
|
|
|
'shareType' => $share->getShareType(),
|
|
|
|
'uidOwner' => $share->getSharedBy(),
|
|
|
|
'permissions' => $share->getPermissions(),
|
|
|
|
'fileSource' => $share->getNode()->getId(),
|
|
|
|
'expiration' => $share->getExpirationDate(),
|
|
|
|
'token' => $share->getToken(),
|
|
|
|
'id' => $share->getId(),
|
|
|
|
'shareWith' => $share->getSharedWith(),
|
|
|
|
'itemTarget' => $share->getTarget(),
|
|
|
|
'fileTarget' => $share->getTarget(),
|
|
|
|
];
|
|
|
|
|
2018-01-25 22:16:13 +00:00
|
|
|
\OC_Hook::emit(Share::class, 'post_shared', $postHookData);
|
2017-08-03 13:14:29 +00:00
|
|
|
|
|
|
|
}
|
2017-03-14 18:18:20 +00:00
|
|
|
}
|