richdocuments/lib/file.php

196 lines
4.8 KiB
PHP
Raw Normal View History

<?php
/**
* ownCloud - Documents App
*
* @author Victor Dubiniuk
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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.
*
2014-05-17 15:29:51 -04:00
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Documents;
use \OC\Files\View;
class File {
protected $fileId;
protected $owner;
protected $path;
protected $sharing;
protected $token ='';
protected $passwordProtected = false;
public function __construct($fileId, $shareOps = null){
if (!$fileId){
throw new \Exception('No valid file has been passed');
}
$this->fileId = $fileId;
$this->sharing = $shareOps;
}
2014-04-09 16:57:42 +03:00
public static function getByShareToken($token){
2014-03-05 18:47:06 +03:00
$linkItem = \OCP\Share::getShareByToken($token, false);
if (is_array($linkItem) && isset($linkItem['uid_owner'])) {
// seems to be a valid share
$rootLinkItem = \OCP\Share::resolveReShare($linkItem);
} else {
throw new \Exception('This file was probably unshared');
}
$file = new File($rootLinkItem['file_source'], $rootLinkItem);
$file->setToken($token);
if (isset($linkItem['share_with']) && !empty($linkItem['share_with'])){
$file->setPasswordProtected(true);
}
return $file;
}
2014-04-14 19:34:33 +03:00
public function getToken(){
2014-04-11 00:59:51 +03:00
return $this->token;
}
public function getFileId(){
return $this->fileId;
}
public function setOwner($owner){
$this->owner = $owner;
}
public function setPath($path){
$this->path = $path;
}
2013-09-26 21:01:41 +03:00
public function setToken($token){
$this->token = $token;
}
2013-09-26 21:01:41 +03:00
public function isPublicShare(){
return !empty($this->token);
2013-09-26 21:01:41 +03:00
}
public function isPasswordProtected(){
return $this->passwordProtected;
}
2014-09-08 22:05:31 +03:00
/**
* @param string $password
* @return boolean
*/
public function checkPassword($password){
$shareId = $this->sharing['id'];
if (!$this->isPasswordProtected()
2014-07-17 12:02:43 +02:00
|| (\OC::$server->getSession()->exists('public_link_authenticated')
&& \OC::$server->getSession()->get('public_link_authenticated') === $shareId
)
){
2014-10-16 23:34:43 +03:00
return true;
}
// Check Password
$newHash = '';
if(\OC::$server->getHasher()->verify($password, $this->getPassword(), $newHash)) {
2014-07-17 12:02:43 +02:00
\OC::$server->getSession()->set('public_link_authenticated', $shareId);
/**
* FIXME: Migrate old hashes to new hash format
* Due to the fact that there is no reasonable functionality to update the password
* of an existing share no migration is yet performed there.
* The only possibility is to update the existing share which will result in a new
* share ID and is a major hack.
*
* In the future the migration should be performed once there is a proper method
* to update the share's password. (for example `$share->updatePassword($password)`
*
* @link https://github.com/owncloud/core/issues/10671
*/
if(!empty($newHash)) {
}
return true;
}
return false;
}
/**
* @param boolean $value
*/
public function setPasswordProtected($value){
$this->passwordProtected = $value;
}
/**
*
* @return string owner of the current file item
* @throws \Exception
*/
2014-06-26 18:21:51 +03:00
public function getOwnerViewAndPath($useDefaultRoot = false){
2014-04-11 00:59:51 +03:00
if ($this->isPublicShare()){
if (isset($this->sharing['uid_owner'])){
$owner = $this->sharing['uid_owner'];
2015-08-27 01:57:31 +03:00
if (!\OC::$server->getUserManager()->userExists($this->sharing['uid_owner'])) {
throw new \Exception('Share owner' . $this->sharing['uid_owner'] . ' does not exist ');
}
2014-07-04 19:10:25 +03:00
\OC_Util::tearDownFS();
\OC_Util::setupFS($this->sharing['uid_owner']);
} else {
2014-04-11 00:59:51 +03:00
throw new \Exception($this->fileId . ' is a broken share');
}
2014-04-11 00:59:51 +03:00
$view = new View('/' . $owner . '/files');
} else {
2015-08-27 01:57:31 +03:00
$owner = \OC::$server->getUserSession()->getUser()->getUID();
2014-06-26 18:21:51 +03:00
$root = '/' . $owner;
if ($useDefaultRoot){
$root .= '/' . 'files';
}
$view = new View($root);
2014-04-11 00:59:51 +03:00
}
2014-07-04 18:22:07 +03:00
$path = $view->getPath($this->fileId);
2014-04-11 00:59:51 +03:00
if (!$path){
throw new \Exception($this->fileId . ' can not be resolved');
}
2014-04-11 00:59:51 +03:00
$this->path = $path;
$this->owner = $owner;
if (!$view->file_exists($this->path)){
2013-09-26 21:01:41 +03:00
throw new \Exception($this->path . ' doesn\'t exist');
}
return array($view, $this->path);
}
2014-04-11 00:59:51 +03:00
2013-09-26 21:01:41 +03:00
public function getOwner(){
if (!$this->owner){
2013-09-26 21:01:41 +03:00
$this->getOwnerViewAndPath();
}
return $this->owner;
}
protected function getPassword(){
return $this->sharing['share_with'];
}
2013-09-26 21:01:41 +03:00
}