2014-10-15 13:58:44 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Georg Ehrke <georg@owncloud.com>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2014-10-15 13:58:44 +04:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-10-15 13:58:44 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-10-15 13:58:44 +04:00
|
|
|
namespace OCA\Files_Sharing\Controllers;
|
|
|
|
|
|
|
|
use OC;
|
|
|
|
use OC\Files\Filesystem;
|
|
|
|
use OC_Files;
|
|
|
|
use OC_Util;
|
|
|
|
use OCP;
|
|
|
|
use OCP\Template;
|
|
|
|
use OCP\Share;
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
2015-03-24 13:21:58 +03:00
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
2014-10-15 13:58:44 +04:00
|
|
|
use OC\URLGenerator;
|
|
|
|
use OC\AppConfig;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCA\Files_Sharing\Helper;
|
|
|
|
use OCP\User;
|
|
|
|
use OCP\Util;
|
2014-12-22 14:35:50 +03:00
|
|
|
use OCA\Files_Sharing\Activity;
|
2015-09-30 14:32:20 +03:00
|
|
|
use \OCP\Files\NotFoundException;
|
2014-10-15 13:58:44 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ShareController
|
|
|
|
*
|
|
|
|
* @package OCA\Files_Sharing\Controllers
|
|
|
|
*/
|
|
|
|
class ShareController extends Controller {
|
|
|
|
|
|
|
|
/** @var \OC\User\Session */
|
|
|
|
protected $userSession;
|
|
|
|
/** @var \OC\AppConfig */
|
|
|
|
protected $appConfig;
|
|
|
|
/** @var \OCP\IConfig */
|
|
|
|
protected $config;
|
|
|
|
/** @var \OC\URLGenerator */
|
|
|
|
protected $urlGenerator;
|
|
|
|
/** @var \OC\User\Manager */
|
|
|
|
protected $userManager;
|
|
|
|
/** @var \OCP\ILogger */
|
|
|
|
protected $logger;
|
2014-12-22 14:35:50 +03:00
|
|
|
/** @var OCP\Activity\IManager */
|
|
|
|
protected $activityManager;
|
2014-10-15 13:58:44 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param OC\User\Session $userSession
|
|
|
|
* @param AppConfig $appConfig
|
|
|
|
* @param OCP\IConfig $config
|
|
|
|
* @param URLGenerator $urlGenerator
|
2015-03-24 13:21:58 +03:00
|
|
|
* @param OCP\IUserManager $userManager
|
2014-10-15 13:58:44 +04:00
|
|
|
* @param ILogger $logger
|
2014-12-22 14:35:50 +03:00
|
|
|
* @param OCP\Activity\IManager $activityManager
|
2014-10-15 13:58:44 +04:00
|
|
|
*/
|
|
|
|
public function __construct($appName,
|
|
|
|
IRequest $request,
|
|
|
|
OC\User\Session $userSession,
|
|
|
|
AppConfig $appConfig,
|
|
|
|
OCP\IConfig $config,
|
|
|
|
URLGenerator $urlGenerator,
|
2015-03-24 13:21:58 +03:00
|
|
|
OCP\IUserManager $userManager,
|
2014-12-22 14:35:50 +03:00
|
|
|
ILogger $logger,
|
|
|
|
OCP\Activity\IManager $activityManager) {
|
2014-10-15 13:58:44 +04:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->appConfig = $appConfig;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->logger = $logger;
|
2014-12-22 14:35:50 +03:00
|
|
|
$this->activityManager = $activityManager;
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
* @return TemplateResponse|RedirectResponse
|
|
|
|
*/
|
|
|
|
public function showAuthenticate($token) {
|
|
|
|
$linkItem = Share::getShareByToken($token, false);
|
|
|
|
|
|
|
|
if(Helper::authenticate($linkItem)) {
|
|
|
|
return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new TemplateResponse($this->appName, 'authenticate', array(), 'guest');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
2014-11-17 15:10:15 +03:00
|
|
|
* @UseSession
|
2014-10-15 13:58:44 +04:00
|
|
|
*
|
|
|
|
* Authenticates against password-protected shares
|
|
|
|
* @param $token
|
|
|
|
* @param string $password
|
|
|
|
* @return RedirectResponse|TemplateResponse
|
|
|
|
*/
|
|
|
|
public function authenticate($token, $password = '') {
|
|
|
|
$linkItem = Share::getShareByToken($token, false);
|
|
|
|
if($linkItem === false) {
|
2015-03-24 13:21:58 +03:00
|
|
|
return new NotFoundResponse();
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$authenticate = Helper::authenticate($linkItem, $password);
|
|
|
|
|
|
|
|
if($authenticate === true) {
|
|
|
|
return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
* @param string $path
|
2015-01-16 21:31:15 +03:00
|
|
|
* @return TemplateResponse|RedirectResponse
|
2015-09-30 14:32:20 +03:00
|
|
|
* @throws NotFoundException
|
2014-10-15 13:58:44 +04:00
|
|
|
*/
|
|
|
|
public function showShare($token, $path = '') {
|
|
|
|
\OC_User::setIncognitoMode(true);
|
|
|
|
|
|
|
|
// Check whether share exists
|
|
|
|
$linkItem = Share::getShareByToken($token, false);
|
|
|
|
if($linkItem === false) {
|
2015-03-24 13:21:58 +03:00
|
|
|
return new NotFoundResponse();
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$shareOwner = $linkItem['uid_owner'];
|
2015-03-24 13:21:58 +03:00
|
|
|
$originalSharePath = $this->getPath($token);
|
2014-10-15 13:58:44 +04:00
|
|
|
|
|
|
|
// Share is password protected - check whether the user is permitted to access the share
|
|
|
|
if (isset($linkItem['share_with']) && !Helper::authenticate($linkItem)) {
|
|
|
|
return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate',
|
|
|
|
array('token' => $token)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Filesystem::isReadable($originalSharePath . $path)) {
|
|
|
|
$getPath = Filesystem::normalizePath($path);
|
|
|
|
$originalSharePath .= $path;
|
2015-03-24 14:21:25 +03:00
|
|
|
} else {
|
2015-09-30 14:32:20 +03:00
|
|
|
throw new NotFoundException();
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$file = basename($originalSharePath);
|
|
|
|
|
2015-03-24 13:21:58 +03:00
|
|
|
$shareTmpl = [];
|
2014-10-15 13:58:44 +04:00
|
|
|
$shareTmpl['displayName'] = User::getDisplayName($shareOwner);
|
|
|
|
$shareTmpl['filename'] = $file;
|
|
|
|
$shareTmpl['directory_path'] = $linkItem['file_target'];
|
|
|
|
$shareTmpl['mimetype'] = Filesystem::getMimeType($originalSharePath);
|
2015-03-16 13:18:25 +03:00
|
|
|
$shareTmpl['previewSupported'] = \OC::$server->getPreviewManager()->isMimeSupported($shareTmpl['mimetype']);
|
2014-10-15 13:58:44 +04:00
|
|
|
$shareTmpl['dirToken'] = $linkItem['token'];
|
|
|
|
$shareTmpl['sharingToken'] = $token;
|
|
|
|
$shareTmpl['server2serversharing'] = Helper::isOutgoingServer2serverShareEnabled();
|
|
|
|
$shareTmpl['protected'] = isset($linkItem['share_with']) ? 'true' : 'false';
|
2014-11-18 16:54:08 +03:00
|
|
|
$shareTmpl['dir'] = '';
|
2015-01-17 14:11:52 +03:00
|
|
|
$nonHumanFileSize = \OC\Files\Filesystem::filesize($originalSharePath);
|
|
|
|
$shareTmpl['nonHumanFileSize'] = $nonHumanFileSize;
|
|
|
|
$shareTmpl['fileSize'] = \OCP\Util::humanFileSize($nonHumanFileSize);
|
2014-10-15 13:58:44 +04:00
|
|
|
|
|
|
|
// Show file list
|
|
|
|
if (Filesystem::is_dir($originalSharePath)) {
|
|
|
|
$shareTmpl['dir'] = $getPath;
|
|
|
|
$maxUploadFilesize = Util::maxUploadFilesize($originalSharePath);
|
|
|
|
$freeSpace = Util::freeSpace($originalSharePath);
|
|
|
|
$uploadLimit = Util::uploadLimit();
|
|
|
|
$folder = new Template('files', 'list', '');
|
|
|
|
$folder->assign('dir', $getPath);
|
|
|
|
$folder->assign('dirToken', $linkItem['token']);
|
2014-11-25 18:28:41 +03:00
|
|
|
$folder->assign('permissions', \OCP\Constants::PERMISSION_READ);
|
2014-10-15 13:58:44 +04:00
|
|
|
$folder->assign('isPublic', true);
|
|
|
|
$folder->assign('publicUploadEnabled', 'no');
|
|
|
|
$folder->assign('uploadMaxFilesize', $maxUploadFilesize);
|
|
|
|
$folder->assign('uploadMaxHumanFilesize', OCP\Util::humanFileSize($maxUploadFilesize));
|
|
|
|
$folder->assign('freeSpace', $freeSpace);
|
|
|
|
$folder->assign('uploadLimit', $uploadLimit); // PHP upload limit
|
|
|
|
$folder->assign('usedSpacePercent', 0);
|
|
|
|
$folder->assign('trash', false);
|
|
|
|
$shareTmpl['folder'] = $folder->fetchPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
$shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', array('token' => $token));
|
2015-01-17 14:11:52 +03:00
|
|
|
$shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10);
|
2015-06-09 17:33:26 +03:00
|
|
|
$shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true);
|
2014-10-15 13:58:44 +04:00
|
|
|
|
2015-03-10 12:06:15 +03:00
|
|
|
$csp = new OCP\AppFramework\Http\ContentSecurityPolicy();
|
|
|
|
$csp->addAllowedFrameDomain('\'self\'');
|
|
|
|
$response = new TemplateResponse($this->appName, 'public', $shareTmpl, 'base');
|
|
|
|
$response->setContentSecurityPolicy($csp);
|
|
|
|
|
|
|
|
return $response;
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
2014-11-24 17:02:49 +03:00
|
|
|
*
|
2014-10-15 13:58:44 +04:00
|
|
|
* @param string $token
|
|
|
|
* @param string $files
|
|
|
|
* @param string $path
|
|
|
|
* @return void|RedirectResponse
|
|
|
|
*/
|
|
|
|
public function downloadShare($token, $files = null, $path = '') {
|
|
|
|
\OC_User::setIncognitoMode(true);
|
|
|
|
|
|
|
|
$linkItem = OCP\Share::getShareByToken($token, false);
|
|
|
|
|
|
|
|
// Share is password protected - check whether the user is permitted to access the share
|
|
|
|
if (isset($linkItem['share_with'])) {
|
|
|
|
if(!Helper::authenticate($linkItem)) {
|
|
|
|
return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate',
|
|
|
|
array('token' => $token)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 17:08:16 +03:00
|
|
|
$files_list = null;
|
|
|
|
if (!is_null($files)) { // download selected files
|
|
|
|
$files_list = json_decode($files);
|
|
|
|
// in case we get only a single file
|
|
|
|
if ($files_list === null) {
|
|
|
|
$files_list = array($files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 13:58:44 +04:00
|
|
|
$originalSharePath = self::getPath($token);
|
|
|
|
|
2015-02-17 17:08:16 +03:00
|
|
|
// Create the activities
|
2014-10-15 13:58:44 +04:00
|
|
|
if (isset($originalSharePath) && Filesystem::isReadable($originalSharePath . $path)) {
|
2014-12-23 14:40:52 +03:00
|
|
|
$originalSharePath = Filesystem::normalizePath($originalSharePath . $path);
|
2015-02-17 17:08:16 +03:00
|
|
|
$isDir = \OC\Files\Filesystem::is_dir($originalSharePath);
|
2014-10-15 13:58:44 +04:00
|
|
|
|
2015-02-17 17:08:16 +03:00
|
|
|
$activities = [];
|
|
|
|
if (!$isDir) {
|
|
|
|
// Single file public share
|
|
|
|
$activities[$originalSharePath] = Activity::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED;
|
|
|
|
} else if (!empty($files_list)) {
|
|
|
|
// Only some files are downloaded
|
|
|
|
foreach ($files_list as $file) {
|
|
|
|
$filePath = Filesystem::normalizePath($originalSharePath . '/' . $file);
|
|
|
|
$isDir = \OC\Files\Filesystem::is_dir($filePath);
|
|
|
|
$activities[$filePath] = ($isDir) ? Activity::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED : Activity::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The folder is downloaded
|
|
|
|
$activities[$originalSharePath] = Activity::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED;
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|
|
|
|
|
2015-02-17 17:08:16 +03:00
|
|
|
foreach ($activities as $filePath => $subject) {
|
|
|
|
$this->activityManager->publishActivity(
|
|
|
|
'files_sharing', $subject, array($filePath), '', array(),
|
|
|
|
$filePath, '', $linkItem['uid_owner'], Activity::TYPE_PUBLIC_LINKS, Activity::PRIORITY_MEDIUM
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// download selected files
|
|
|
|
if (!is_null($files)) {
|
2014-10-15 13:58:44 +04:00
|
|
|
// FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well
|
2014-11-25 12:17:31 +03:00
|
|
|
// after dispatching the request which results in a "Cannot modify header information" notice.
|
2014-10-15 13:58:44 +04:00
|
|
|
OC_Files::get($originalSharePath, $files_list, $_SERVER['REQUEST_METHOD'] == 'HEAD');
|
|
|
|
exit();
|
|
|
|
} else {
|
|
|
|
// FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well
|
|
|
|
// after dispatching the request which results in a "Cannot modify header information" notice.
|
|
|
|
OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $_SERVER['REQUEST_METHOD'] == 'HEAD');
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-24 13:21:58 +03:00
|
|
|
* @param string $token
|
|
|
|
* @return string Resolved file path of the token
|
2015-09-30 14:32:20 +03:00
|
|
|
* @throws NotFoundException In case share could not get properly resolved
|
2014-10-15 13:58:44 +04:00
|
|
|
*/
|
|
|
|
private function getPath($token) {
|
|
|
|
$linkItem = Share::getShareByToken($token, false);
|
|
|
|
if (is_array($linkItem) && isset($linkItem['uid_owner'])) {
|
|
|
|
// seems to be a valid share
|
|
|
|
$rootLinkItem = Share::resolveReShare($linkItem);
|
|
|
|
if (isset($rootLinkItem['uid_owner'])) {
|
2015-03-24 13:21:58 +03:00
|
|
|
if(!$this->userManager->userExists($rootLinkItem['uid_owner'])) {
|
2015-09-30 14:32:20 +03:00
|
|
|
throw new NotFoundException('Owner of the share does not exist anymore');
|
2015-03-24 13:21:58 +03:00
|
|
|
}
|
2014-10-15 13:58:44 +04:00
|
|
|
OC_Util::tearDownFS();
|
|
|
|
OC_Util::setupFS($rootLinkItem['uid_owner']);
|
|
|
|
$path = Filesystem::getPath($linkItem['file_source']);
|
2015-03-24 13:21:58 +03:00
|
|
|
|
|
|
|
if(!empty($path) && Filesystem::isReadable($path)) {
|
|
|
|
return $path;
|
|
|
|
}
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|
|
|
|
}
|
2015-03-24 13:21:58 +03:00
|
|
|
|
2015-09-30 14:32:20 +03:00
|
|
|
throw new NotFoundException('No file found belonging to file.');
|
2014-10-15 13:58:44 +04:00
|
|
|
}
|
|
|
|
}
|