2016-07-29 13:38:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
|
|
|
|
*
|
|
|
|
* @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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\ShareByMail;
|
|
|
|
|
2016-10-26 09:04:00 +00:00
|
|
|
use OC\HintException;
|
2016-11-01 19:12:01 +00:00
|
|
|
use OC\Share20\Exception\InvalidShare;
|
2017-03-30 13:23:44 +00:00
|
|
|
use OCA\ShareByMail\Settings\SettingsManager;
|
2016-11-07 14:08:56 +00:00
|
|
|
use OCP\Activity\IManager;
|
2017-03-29 14:50:23 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2017-04-18 20:23:07 +00:00
|
|
|
use OCP\Defaults;
|
2016-11-01 19:05:09 +00:00
|
|
|
use OCP\Files\Folder;
|
2016-07-29 13:38:31 +00:00
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\Files\Node;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
2016-10-26 09:04:00 +00:00
|
|
|
use OCP\IURLGenerator;
|
2017-01-11 17:11:27 +00:00
|
|
|
use OCP\IUser;
|
2016-07-29 13:38:31 +00:00
|
|
|
use OCP\IUserManager;
|
2016-10-26 09:04:00 +00:00
|
|
|
use OCP\Mail\IMailer;
|
2016-07-29 13:38:31 +00:00
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
use OC\Share20\Share;
|
2016-10-26 10:18:39 +00:00
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
2016-07-29 13:38:31 +00:00
|
|
|
use OCP\Share\IShare;
|
|
|
|
use OCP\Share\IShareProvider;
|
2016-11-02 23:09:19 +00:00
|
|
|
use OCP\Template;
|
2016-07-29 13:38:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ShareByMail
|
|
|
|
*
|
|
|
|
* @package OCA\ShareByMail
|
|
|
|
*/
|
|
|
|
class ShareByMailProvider implements IShareProvider {
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
private $dbConnection;
|
|
|
|
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $secureRandom;
|
|
|
|
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/** @var IRootFolder */
|
|
|
|
private $rootFolder;
|
|
|
|
|
|
|
|
/** @var IL10N */
|
|
|
|
private $l;
|
|
|
|
|
2016-10-26 09:04:00 +00:00
|
|
|
/** @var IMailer */
|
|
|
|
private $mailer;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
|
2016-11-07 14:08:56 +00:00
|
|
|
/** @var IManager */
|
|
|
|
private $activityManager;
|
|
|
|
|
2017-03-30 13:23:44 +00:00
|
|
|
/** @var SettingsManager */
|
|
|
|
private $settingsManager;
|
|
|
|
|
2017-04-18 20:23:07 +00:00
|
|
|
/** @var Defaults */
|
|
|
|
private $defaults;
|
|
|
|
|
2016-07-29 13:38:31 +00:00
|
|
|
/**
|
|
|
|
* Return the identifier of this provider.
|
|
|
|
*
|
|
|
|
* @return string Containing only [a-zA-Z0-9]
|
|
|
|
*/
|
|
|
|
public function identifier() {
|
2017-03-28 12:39:01 +00:00
|
|
|
return 'ocMailShare';
|
2016-07-29 13:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DefaultShareProvider constructor.
|
|
|
|
*
|
|
|
|
* @param IDBConnection $connection
|
|
|
|
* @param ISecureRandom $secureRandom
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IRootFolder $rootFolder
|
|
|
|
* @param IL10N $l
|
|
|
|
* @param ILogger $logger
|
2016-10-26 09:04:00 +00:00
|
|
|
* @param IMailer $mailer
|
|
|
|
* @param IURLGenerator $urlGenerator
|
2016-11-07 14:08:56 +00:00
|
|
|
* @param IManager $activityManager
|
2017-03-30 13:23:44 +00:00
|
|
|
* @param SettingsManager $settingsManager
|
2017-04-18 20:23:07 +00:00
|
|
|
* @param Defaults $defaults
|
2016-07-29 13:38:31 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
IDBConnection $connection,
|
|
|
|
ISecureRandom $secureRandom,
|
|
|
|
IUserManager $userManager,
|
|
|
|
IRootFolder $rootFolder,
|
|
|
|
IL10N $l,
|
2016-10-26 09:04:00 +00:00
|
|
|
ILogger $logger,
|
|
|
|
IMailer $mailer,
|
2016-11-07 14:08:56 +00:00
|
|
|
IURLGenerator $urlGenerator,
|
2017-03-30 13:23:44 +00:00
|
|
|
IManager $activityManager,
|
2017-04-18 20:23:07 +00:00
|
|
|
SettingsManager $settingsManager,
|
|
|
|
Defaults $defaults
|
2016-07-29 13:38:31 +00:00
|
|
|
) {
|
|
|
|
$this->dbConnection = $connection;
|
|
|
|
$this->secureRandom = $secureRandom;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->rootFolder = $rootFolder;
|
|
|
|
$this->l = $l;
|
|
|
|
$this->logger = $logger;
|
2016-10-26 09:04:00 +00:00
|
|
|
$this->mailer = $mailer;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2016-11-07 14:08:56 +00:00
|
|
|
$this->activityManager = $activityManager;
|
2017-03-30 13:23:44 +00:00
|
|
|
$this->settingsManager = $settingsManager;
|
2017-04-18 20:23:07 +00:00
|
|
|
$this->defaults = $defaults;
|
2016-07-29 13:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Share a path
|
|
|
|
*
|
|
|
|
* @param IShare $share
|
|
|
|
* @return IShare The share object
|
|
|
|
* @throws ShareNotFound
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function create(IShare $share) {
|
|
|
|
|
|
|
|
$shareWith = $share->getSharedWith();
|
|
|
|
/*
|
|
|
|
* Check if file is not already shared with the remote user
|
|
|
|
*/
|
|
|
|
$alreadyShared = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_EMAIL, $share->getNode(), 1, 0);
|
|
|
|
if (!empty($alreadyShared)) {
|
|
|
|
$message = 'Sharing %s failed, this item is already shared with %s';
|
|
|
|
$message_t = $this->l->t('Sharing %s failed, this item is already shared with %s', array($share->getNode()->getName(), $shareWith));
|
|
|
|
$this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
|
|
|
|
throw new \Exception($message_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
$shareId = $this->createMailShare($share);
|
2016-11-07 14:08:56 +00:00
|
|
|
$this->createActivity($share);
|
2016-07-29 13:38:31 +00:00
|
|
|
$data = $this->getRawShare($shareId);
|
|
|
|
return $this->createShareObject($data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-07 14:08:56 +00:00
|
|
|
/**
|
|
|
|
* create activity if a file/folder was shared by mail
|
|
|
|
*
|
|
|
|
* @param IShare $share
|
|
|
|
*/
|
|
|
|
protected function createActivity(IShare $share) {
|
|
|
|
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
|
|
|
|
|
|
|
|
$this->publishActivity(
|
|
|
|
Activity::SUBJECT_SHARED_EMAIL_SELF,
|
|
|
|
[$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()],
|
|
|
|
$share->getSharedBy(),
|
|
|
|
$share->getNode()->getId(),
|
|
|
|
$userFolder->getRelativePath($share->getNode()->getPath())
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($share->getShareOwner() !== $share->getSharedBy()) {
|
|
|
|
$ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
|
|
|
|
$fileId = $share->getNode()->getId();
|
2017-01-11 17:11:27 +00:00
|
|
|
$nodes = $ownerFolder->getById($fileId);
|
|
|
|
$ownerPath = $nodes[0]->getPath();
|
2016-11-07 14:08:56 +00:00
|
|
|
$this->publishActivity(
|
|
|
|
Activity::SUBJECT_SHARED_EMAIL_BY,
|
|
|
|
[$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()],
|
|
|
|
$share->getShareOwner(),
|
|
|
|
$fileId,
|
2017-01-11 17:11:27 +00:00
|
|
|
$ownerFolder->getRelativePath($ownerPath)
|
2016-11-07 14:08:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* publish activity if a file/folder was shared by mail
|
|
|
|
*
|
|
|
|
* @param $subject
|
|
|
|
* @param $parameters
|
|
|
|
* @param $affectedUser
|
|
|
|
* @param $fileId
|
|
|
|
* @param $filePath
|
|
|
|
*/
|
|
|
|
protected function publishActivity($subject, $parameters, $affectedUser, $fileId, $filePath) {
|
|
|
|
$event = $this->activityManager->generateEvent();
|
|
|
|
$event->setApp('sharebymail')
|
|
|
|
->setType('shared')
|
|
|
|
->setSubject($subject, $parameters)
|
|
|
|
->setAffectedUser($affectedUser)
|
|
|
|
->setObject('files', $fileId, $filePath);
|
|
|
|
$this->activityManager->publish($event);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-29 13:38:31 +00:00
|
|
|
/**
|
|
|
|
* @param IShare $share
|
|
|
|
* @return int
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2016-10-31 13:19:34 +00:00
|
|
|
protected function createMailShare(IShare $share) {
|
2016-10-26 09:04:00 +00:00
|
|
|
$share->setToken($this->generateToken());
|
2016-07-29 13:38:31 +00:00
|
|
|
$shareId = $this->addShareToDB(
|
|
|
|
$share->getNodeId(),
|
|
|
|
$share->getNodeType(),
|
|
|
|
$share->getSharedWith(),
|
|
|
|
$share->getSharedBy(),
|
|
|
|
$share->getShareOwner(),
|
|
|
|
$share->getPermissions(),
|
2016-10-26 09:04:00 +00:00
|
|
|
$share->getToken()
|
2016-07-29 13:38:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
2016-10-26 09:04:00 +00:00
|
|
|
$link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
|
|
|
|
['token' => $share->getToken()]);
|
2017-04-18 20:23:07 +00:00
|
|
|
$this->sendMailNotification(
|
|
|
|
$share->getNode()->getName(),
|
2016-10-26 09:04:00 +00:00
|
|
|
$link,
|
|
|
|
$share->getShareOwner(),
|
2017-04-18 20:23:07 +00:00
|
|
|
$share->getSharedBy(),
|
|
|
|
$share->getSharedWith()
|
|
|
|
);
|
2016-10-26 09:04:00 +00:00
|
|
|
} catch (HintException $hintException) {
|
|
|
|
$this->logger->error('Failed to send share by mail: ' . $hintException->getMessage());
|
|
|
|
$this->removeShareFromTable($shareId);
|
|
|
|
throw $hintException;
|
2016-07-29 13:38:31 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-10-26 09:04:00 +00:00
|
|
|
$this->logger->error('Failed to send share by mail: ' . $e->getMessage());
|
2016-07-29 13:38:31 +00:00
|
|
|
$this->removeShareFromTable($shareId);
|
2016-10-26 09:04:00 +00:00
|
|
|
throw new HintException('Failed to send share by mail',
|
|
|
|
$this->l->t('Failed to send share by E-mail'));
|
2016-07-29 13:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $shareId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-18 20:23:07 +00:00
|
|
|
/**
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $link
|
|
|
|
* @param string $owner
|
|
|
|
* @param string $initiator
|
|
|
|
* @param string $shareWith
|
|
|
|
* @throws \Exception If mail couldn't be sent
|
|
|
|
*/
|
|
|
|
protected function sendMailNotification($filename,
|
|
|
|
$link,
|
|
|
|
$owner,
|
|
|
|
$initiator,
|
|
|
|
$shareWith) {
|
2017-01-11 17:11:27 +00:00
|
|
|
$ownerUser = $this->userManager->get($owner);
|
|
|
|
$initiatorUser = $this->userManager->get($initiator);
|
|
|
|
$ownerDisplayName = ($ownerUser instanceof IUser) ? $ownerUser->getDisplayName() : $owner;
|
|
|
|
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
|
2016-10-26 09:04:00 +00:00
|
|
|
if ($owner === $initiator) {
|
2017-01-11 17:11:27 +00:00
|
|
|
$subject = (string)$this->l->t('%s shared »%s« with you', array($ownerDisplayName, $filename));
|
2016-10-26 09:04:00 +00:00
|
|
|
} else {
|
2017-01-11 17:11:27 +00:00
|
|
|
$subject = (string)$this->l->t('%s shared »%s« with you on behalf of %s', array($ownerDisplayName, $filename, $initiatorDisplayName));
|
2016-10-26 09:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$message = $this->mailer->createMessage();
|
|
|
|
|
2017-04-11 21:18:31 +00:00
|
|
|
$emailTemplate = $this->mailer->createEMailTemplate();
|
2016-10-26 09:04:00 +00:00
|
|
|
|
2017-04-11 21:18:31 +00:00
|
|
|
$emailTemplate->addHeader();
|
2017-04-12 09:52:33 +00:00
|
|
|
$emailTemplate->addHeading($this->l->t('%s shared »%s« with you', [$ownerDisplayName, $filename]), false);
|
2017-04-11 21:18:31 +00:00
|
|
|
|
|
|
|
if ($owner === $initiator) {
|
|
|
|
$text = $this->l->t('%s shared »%s« with you.', [$ownerDisplayName, $filename]);
|
|
|
|
} else {
|
|
|
|
$text= $this->l->t('%s shared »%s« with you on behalf of %s.', [$ownerDisplayName, $filename, $initiator]);
|
2016-10-26 09:04:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 13:33:35 +00:00
|
|
|
$emailTemplate->addBodyText(
|
|
|
|
$text . ' ' . $this->l->t('Click the button below to open it.'),
|
|
|
|
$text
|
|
|
|
);
|
2017-04-11 21:18:31 +00:00
|
|
|
|
|
|
|
$emailTemplate->addBodyButton(
|
|
|
|
$this->l->t('Open »%s«', [$filename]),
|
|
|
|
$link
|
|
|
|
);
|
|
|
|
|
|
|
|
$message->setTo([$shareWith]);
|
2017-04-18 20:23:07 +00:00
|
|
|
|
|
|
|
// The "From" contains the sharers name
|
|
|
|
$instanceName = $this->defaults->getName();
|
|
|
|
$senderName = $this->l->t(
|
|
|
|
'%s via %s',
|
|
|
|
[
|
|
|
|
$ownerDisplayName,
|
|
|
|
$instanceName
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]);
|
|
|
|
|
|
|
|
// The "Reply-To" is set to the sharer if an mail address is configured
|
|
|
|
// also the default footer contains a "Do not reply" which needs to be adjusted.
|
|
|
|
$ownerEmail = $ownerUser->getEMailAddress();
|
|
|
|
if($ownerEmail !== null) {
|
|
|
|
$message->setReplyTo([$ownerEmail => $ownerDisplayName]);
|
|
|
|
$emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
|
|
|
|
} else {
|
|
|
|
$emailTemplate->addFooter();
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:18:31 +00:00
|
|
|
$message->setSubject($subject);
|
2017-04-18 20:23:07 +00:00
|
|
|
$message->setPlainBody($emailTemplate->renderText());
|
2017-04-11 21:18:31 +00:00
|
|
|
$message->setHtmlBody($emailTemplate->renderHTML());
|
|
|
|
$this->mailer->send($message);
|
2016-10-26 09:04:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 12:39:38 +00:00
|
|
|
/**
|
|
|
|
* send password to recipient of a mail share
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $initiator
|
|
|
|
* @param string $shareWith
|
|
|
|
*/
|
|
|
|
protected function sendPassword($filename, $initiator, $shareWith, $password) {
|
2017-03-30 13:23:44 +00:00
|
|
|
|
|
|
|
if ($this->settingsManager->sendPasswordByMail() === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-28 12:39:38 +00:00
|
|
|
$initiatorUser = $this->userManager->get($initiator);
|
|
|
|
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
|
|
|
|
$subject = (string)$this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName]);
|
|
|
|
|
|
|
|
$message = $this->mailer->createMessage();
|
|
|
|
|
2017-04-11 21:18:31 +00:00
|
|
|
$emailTemplate = $this->mailer->createEMailTemplate();
|
2017-03-28 12:39:38 +00:00
|
|
|
|
2017-04-11 21:18:31 +00:00
|
|
|
$emailTemplate->addHeader();
|
|
|
|
$emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]));
|
2017-03-28 12:39:38 +00:00
|
|
|
|
2017-04-11 21:18:31 +00:00
|
|
|
$emailTemplate->addBodyText($this->l->t(
|
|
|
|
'%s shared »%s« with you. You should have already received a separate mail with a link to access it.',
|
|
|
|
[$initiatorDisplayName, $filename]
|
|
|
|
));
|
|
|
|
$emailTemplate->addBodyText($this->l->t('It is protected with the following password: %s', [$password]));
|
2017-03-28 12:39:38 +00:00
|
|
|
|
2017-04-11 21:18:31 +00:00
|
|
|
$emailTemplate->addFooter();
|
|
|
|
|
|
|
|
$message->setTo([$shareWith]);
|
|
|
|
$message->setSubject($subject);
|
|
|
|
$message->setBody($emailTemplate->renderText(), 'text/plain');
|
|
|
|
$message->setHtmlBody($emailTemplate->renderHTML());
|
|
|
|
$this->mailer->send($message);
|
2017-03-28 12:39:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-29 13:38:31 +00:00
|
|
|
/**
|
|
|
|
* generate share token
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-10-31 13:19:34 +00:00
|
|
|
protected function generateToken() {
|
2016-07-29 13:38:31 +00:00
|
|
|
$token = $this->secureRandom->generate(
|
|
|
|
15, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all children of this share
|
|
|
|
*
|
|
|
|
* @param IShare $parent
|
|
|
|
* @return IShare[]
|
|
|
|
*/
|
|
|
|
public function getChildren(IShare $parent) {
|
|
|
|
$children = [];
|
|
|
|
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
|
|
|
|
->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
|
|
|
|
->orderBy('id');
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
while($data = $cursor->fetch()) {
|
|
|
|
$children[] = $this->createShareObject($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $children;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add share to the database and return the ID
|
|
|
|
*
|
|
|
|
* @param int $itemSource
|
|
|
|
* @param string $itemType
|
|
|
|
* @param string $shareWith
|
|
|
|
* @param string $sharedBy
|
|
|
|
* @param string $uidOwner
|
|
|
|
* @param int $permissions
|
|
|
|
* @param string $token
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-10-31 13:19:34 +00:00
|
|
|
protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token) {
|
2016-07-29 13:38:31 +00:00
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->insert('share')
|
|
|
|
->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
|
|
|
|
->setValue('item_type', $qb->createNamedParameter($itemType))
|
|
|
|
->setValue('item_source', $qb->createNamedParameter($itemSource))
|
|
|
|
->setValue('file_source', $qb->createNamedParameter($itemSource))
|
|
|
|
->setValue('share_with', $qb->createNamedParameter($shareWith))
|
|
|
|
->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
|
|
|
|
->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
|
|
|
|
->setValue('permissions', $qb->createNamedParameter($permissions))
|
|
|
|
->setValue('token', $qb->createNamedParameter($token))
|
|
|
|
->setValue('stime', $qb->createNamedParameter(time()));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Added to fix https://github.com/owncloud/core/issues/22215
|
|
|
|
* Can be removed once we get rid of ajax/share.php
|
|
|
|
*/
|
|
|
|
$qb->setValue('file_target', $qb->createNamedParameter(''));
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
$id = $qb->getLastInsertId();
|
|
|
|
|
|
|
|
return (int)$id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a share
|
|
|
|
*
|
|
|
|
* @param IShare $share
|
2017-03-28 12:39:38 +00:00
|
|
|
* @param string|null $plainTextPassword
|
2016-07-29 13:38:31 +00:00
|
|
|
* @return IShare The share object
|
|
|
|
*/
|
2017-03-28 12:39:38 +00:00
|
|
|
public function update(IShare $share, $plainTextPassword = null) {
|
|
|
|
|
|
|
|
$originalShare = $this->getShareById($share->getId());
|
|
|
|
|
|
|
|
// a real password was given
|
|
|
|
$validPassword = $plainTextPassword !== null && $plainTextPassword !== '';
|
|
|
|
|
|
|
|
if($validPassword && $originalShare->getPassword() !== $share->getPassword()) {
|
|
|
|
$this->sendPassword($share->getNode()->getName(), $share->getSharedBy(), $share->getSharedWith(), $plainTextPassword);
|
|
|
|
}
|
2016-07-29 13:38:31 +00:00
|
|
|
/*
|
2017-03-28 12:39:38 +00:00
|
|
|
* We allow updating the permissions and password of mail shares
|
2016-07-29 13:38:31 +00:00
|
|
|
*/
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
2017-03-28 12:39:38 +00:00
|
|
|
$qb->update('share')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
|
|
|
|
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
|
|
|
|
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
|
|
|
|
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
|
|
|
|
->set('password', $qb->createNamedParameter($share->getPassword()))
|
2017-03-29 14:50:23 +00:00
|
|
|
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
|
2017-03-28 12:39:38 +00:00
|
|
|
->execute();
|
2016-07-29 13:38:31 +00:00
|
|
|
|
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function move(IShare $share, $recipient) {
|
|
|
|
/**
|
|
|
|
* nothing to do here, mail shares are only outgoing shares
|
|
|
|
*/
|
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a share (owner unShares the file)
|
|
|
|
*
|
|
|
|
* @param IShare $share
|
|
|
|
*/
|
|
|
|
public function delete(IShare $share) {
|
|
|
|
$this->removeShareFromTable($share->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function deleteFromSelf(IShare $share, $recipient) {
|
|
|
|
// nothing to do here, mail shares are only outgoing shares
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share');
|
|
|
|
|
|
|
|
$qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reshares for this user are shares where they are the owner.
|
|
|
|
*/
|
|
|
|
if ($reshares === false) {
|
|
|
|
//Special case for old shares created via the web UI
|
|
|
|
$or1 = $qb->expr()->andX(
|
|
|
|
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
|
|
|
|
$qb->expr()->isNull('uid_initiator')
|
|
|
|
);
|
|
|
|
|
|
|
|
$qb->andWhere(
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
|
|
|
|
$or1
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$qb->andWhere(
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
|
|
|
|
$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node !== null) {
|
|
|
|
$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($limit !== -1) {
|
|
|
|
$qb->setMaxResults($limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb->setFirstResult($offset);
|
|
|
|
$qb->orderBy('id');
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$shares = [];
|
|
|
|
while($data = $cursor->fetch()) {
|
|
|
|
$shares[] = $this->createShareObject($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $shares;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getShareById($id, $recipientId = null) {
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
|
|
|
->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
throw new ShareNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$share = $this->createShareObject($data);
|
|
|
|
} catch (InvalidShare $e) {
|
|
|
|
throw new ShareNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get shares for a given path
|
|
|
|
*
|
|
|
|
* @param \OCP\Files\Node $path
|
|
|
|
* @return IShare[]
|
|
|
|
*/
|
|
|
|
public function getSharesByPath(Node $path) {
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
|
|
|
|
$cursor = $qb->select('*')
|
|
|
|
->from('share')
|
|
|
|
->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
|
|
|
|
->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$shares = [];
|
|
|
|
while($data = $cursor->fetch()) {
|
|
|
|
$shares[] = $this->createShareObject($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $shares;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
|
|
|
|
/** @var IShare[] $shares */
|
|
|
|
$shares = [];
|
|
|
|
|
|
|
|
//Get shares directly with this user
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share');
|
|
|
|
|
|
|
|
// Order by id
|
|
|
|
$qb->orderBy('id');
|
|
|
|
|
|
|
|
// Set limit and offset
|
|
|
|
if ($limit !== -1) {
|
|
|
|
$qb->setMaxResults($limit);
|
|
|
|
}
|
|
|
|
$qb->setFirstResult($offset);
|
|
|
|
|
|
|
|
$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
|
|
|
|
$qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
|
|
|
|
|
|
|
|
// Filter by node if provided
|
|
|
|
if ($node !== null) {
|
|
|
|
$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
|
|
|
|
}
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
|
|
|
|
while($data = $cursor->fetch()) {
|
|
|
|
$shares[] = $this->createShareObject($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
|
|
|
|
return $shares;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a share by token
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
* @return IShare
|
|
|
|
* @throws ShareNotFound
|
|
|
|
*/
|
|
|
|
public function getShareByToken($token) {
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
|
|
|
|
$cursor = $qb->select('*')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
|
|
|
|
->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$share = $this->createShareObject($data);
|
|
|
|
} catch (InvalidShare $e) {
|
|
|
|
throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove share from table
|
|
|
|
*
|
|
|
|
* @param string $shareId
|
|
|
|
*/
|
2016-10-31 13:19:34 +00:00
|
|
|
protected function removeShareFromTable($shareId) {
|
2016-07-29 13:38:31 +00:00
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->delete('share')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a share object from an database row
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return IShare
|
|
|
|
* @throws InvalidShare
|
|
|
|
* @throws ShareNotFound
|
|
|
|
*/
|
2016-10-31 13:19:34 +00:00
|
|
|
protected function createShareObject($data) {
|
2016-07-29 13:38:31 +00:00
|
|
|
|
|
|
|
$share = new Share($this->rootFolder, $this->userManager);
|
|
|
|
$share->setId((int)$data['id'])
|
|
|
|
->setShareType((int)$data['share_type'])
|
|
|
|
->setPermissions((int)$data['permissions'])
|
|
|
|
->setTarget($data['file_target'])
|
|
|
|
->setMailSend((bool)$data['mail_send'])
|
|
|
|
->setToken($data['token']);
|
|
|
|
|
|
|
|
$shareTime = new \DateTime();
|
|
|
|
$shareTime->setTimestamp((int)$data['stime']);
|
|
|
|
$share->setShareTime($shareTime);
|
|
|
|
$share->setSharedWith($data['share_with']);
|
2017-03-28 12:39:38 +00:00
|
|
|
$share->setPassword($data['password']);
|
2016-07-29 13:38:31 +00:00
|
|
|
|
|
|
|
if ($data['uid_initiator'] !== null) {
|
|
|
|
$share->setShareOwner($data['uid_owner']);
|
|
|
|
$share->setSharedBy($data['uid_initiator']);
|
|
|
|
} else {
|
|
|
|
//OLD SHARE
|
|
|
|
$share->setSharedBy($data['uid_owner']);
|
|
|
|
$path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
|
|
|
|
|
|
|
|
$owner = $path->getOwner();
|
|
|
|
$share->setShareOwner($owner->getUID());
|
|
|
|
}
|
|
|
|
|
2017-03-29 14:50:23 +00:00
|
|
|
if ($data['expiration'] !== null) {
|
|
|
|
$expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
|
|
|
|
if ($expiration !== false) {
|
|
|
|
$share->setExpirationDate($expiration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-29 13:38:31 +00:00
|
|
|
$share->setNodeId((int)$data['file_source']);
|
|
|
|
$share->setNodeType($data['item_type']);
|
|
|
|
|
|
|
|
$share->setProviderId($this->identifier());
|
|
|
|
|
|
|
|
return $share;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the node with file $id for $user
|
|
|
|
*
|
|
|
|
* @param string $userId
|
|
|
|
* @param int $id
|
|
|
|
* @return \OCP\Files\File|\OCP\Files\Folder
|
|
|
|
* @throws InvalidShare
|
|
|
|
*/
|
|
|
|
private function getNode($userId, $id) {
|
|
|
|
try {
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($userId);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
throw new InvalidShare();
|
|
|
|
}
|
|
|
|
|
|
|
|
$nodes = $userFolder->getById($id);
|
|
|
|
|
|
|
|
if (empty($nodes)) {
|
|
|
|
throw new InvalidShare();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nodes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A user is deleted from the system
|
|
|
|
* So clean up the relevant shares.
|
|
|
|
*
|
|
|
|
* @param string $uid
|
|
|
|
* @param int $shareType
|
|
|
|
*/
|
|
|
|
public function userDeleted($uid, $shareType) {
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
|
|
|
|
$qb->delete('share')
|
2016-10-31 13:19:34 +00:00
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
|
2016-07-29 13:38:31 +00:00
|
|
|
->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This provider does not support group shares
|
|
|
|
*
|
|
|
|
* @param string $gid
|
|
|
|
*/
|
|
|
|
public function groupDeleted($gid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This provider does not support group shares
|
|
|
|
*
|
|
|
|
* @param string $uid
|
|
|
|
* @param string $gid
|
|
|
|
*/
|
|
|
|
public function userDeletedFromGroup($uid, $gid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get database row of a give share
|
|
|
|
*
|
|
|
|
* @param $id
|
|
|
|
* @return array
|
|
|
|
* @throws ShareNotFound
|
|
|
|
*/
|
2016-10-31 13:19:34 +00:00
|
|
|
protected function getRawShare($id) {
|
2016-07-29 13:38:31 +00:00
|
|
|
|
|
|
|
// Now fetch the inserted share and create a complete share object
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$data = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
if ($data === false) {
|
|
|
|
throw new ShareNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:05:09 +00:00
|
|
|
public function getSharesInFolder($userId, Folder $node, $reshares) {
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->select('*')
|
|
|
|
->from('share', 's')
|
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
))
|
|
|
|
->andWhere(
|
|
|
|
$qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reshares for this user are shares where they are the owner.
|
|
|
|
*/
|
|
|
|
if ($reshares === false) {
|
|
|
|
$qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
|
|
|
|
} else {
|
|
|
|
$qb->andWhere(
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
|
|
|
|
$qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-10 15:59:40 +00:00
|
|
|
$qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
|
2016-11-01 19:05:09 +00:00
|
|
|
$qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
|
|
|
|
|
|
|
|
$qb->orderBy('id');
|
|
|
|
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
$shares = [];
|
|
|
|
while ($data = $cursor->fetch()) {
|
|
|
|
$shares[$data['fileid']][] = $this->createShareObject($data);
|
|
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $shares;
|
|
|
|
}
|
|
|
|
|
2017-04-11 10:40:36 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2016-12-22 20:44:21 +00:00
|
|
|
public function getAccessList($nodes, $currentAccess) {
|
2017-01-04 07:59:43 +00:00
|
|
|
$ids = [];
|
|
|
|
foreach ($nodes as $node) {
|
|
|
|
$ids[] = $node->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb = $this->dbConnection->getQueryBuilder();
|
|
|
|
$qb->select('share_with')
|
|
|
|
->from('share')
|
|
|
|
->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
|
|
|
|
->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
|
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
|
|
|
|
$qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
|
|
|
|
))
|
|
|
|
->setMaxResults(1);
|
|
|
|
$cursor = $qb->execute();
|
|
|
|
|
|
|
|
$mail = $cursor->fetch() !== false;
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
2017-04-13 13:14:30 +00:00
|
|
|
return ['public' => $mail];
|
2016-12-22 20:44:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 13:38:31 +00:00
|
|
|
}
|