| 
									
										
										
										
											2013-09-24 03:39:05 +03:00
										 |  |  | <?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 | 
					
						
							| 
									
										
										
										
											2013-09-24 03:39:05 +03:00
										 |  |  |  * License along with this library.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace OCA\Documents; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-11 03:22:31 +03:00
										 |  |  | use \OC\Files\View; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-24 03:39:05 +03:00
										 |  |  | class File { | 
					
						
							|  |  |  | 	protected $fileId; | 
					
						
							| 
									
										
										
										
											2013-09-25 16:34:35 +03:00
										 |  |  | 	protected $owner; | 
					
						
							| 
									
										
										
										
											2014-11-11 01:49:32 +03:00
										 |  |  | 	protected $sharing; | 
					
						
							| 
									
										
										
										
											2015-09-18 00:15:18 +03:00
										 |  |  | 	protected $token; | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 	protected $passwordProtected = false; | 
					
						
							| 
									
										
										
										
											2015-09-17 21:33:24 +03:00
										 |  |  | 	protected $ownerView; | 
					
						
							|  |  |  | 	protected $ownerViewFiles; | 
					
						
							|  |  |  | 	protected $path; | 
					
						
							|  |  |  | 	protected $pathFiles; | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 00:15:18 +03:00
										 |  |  | 	public function __construct($fileId, $shareOps = null, $token = ''){ | 
					
						
							| 
									
										
										
										
											2013-09-24 03:39:05 +03:00
										 |  |  | 		if (!$fileId){ | 
					
						
							|  |  |  | 			throw new \Exception('No valid file has been passed'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$this->fileId = $fileId; | 
					
						
							| 
									
										
										
										
											2014-11-11 01:49:32 +03:00
										 |  |  | 		$this->sharing = $shareOps; | 
					
						
							| 
									
										
										
										
											2015-09-17 21:33:24 +03:00
										 |  |  | 		$this->token = $token; | 
					
						
							| 
									
										
										
										
											2015-09-18 00:15:18 +03:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		if ($this->isPublicShare()) { | 
					
						
							|  |  |  | 			if (isset($this->sharing['uid_owner'])){ | 
					
						
							|  |  |  | 				$this->owner = $this->sharing['uid_owner']; | 
					
						
							|  |  |  | 				if (!\OC::$server->getUserManager()->userExists($this->sharing['uid_owner'])) { | 
					
						
							|  |  |  | 					throw new \Exception('Share owner' . $this->sharing['uid_owner'] . ' does not exist '); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				\OC_Util::tearDownFS(); | 
					
						
							|  |  |  | 				\OC_Util::setupFS($this->sharing['uid_owner']); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				throw new \Exception($this->fileId . ' is a broken share'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			$this->owner = \OC::$server->getUserSession()->getUser()->getUID(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-17 21:33:24 +03:00
										 |  |  | 		$this->initViews(); | 
					
						
							| 
									
										
										
										
											2013-09-24 03:39:05 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2014-04-09 16:57:42 +03:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-09-25 16:34:35 +03:00
										 |  |  | 	public static function getByShareToken($token){ | 
					
						
							| 
									
										
										
										
											2014-03-05 18:47:06 +03:00
										 |  |  | 		$linkItem = \OCP\Share::getShareByToken($token, false); | 
					
						
							| 
									
										
										
										
											2013-09-25 16:34:35 +03:00
										 |  |  | 		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'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2015-09-17 21:33:24 +03:00
										 |  |  | 		$file = new File($rootLinkItem['file_source'], $rootLinkItem, $token); | 
					
						
							| 
									
										
										
										
											2013-12-04 21:33:37 +03:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 		if (isset($linkItem['share_with']) && !empty($linkItem['share_with'])){ | 
					
						
							|  |  |  | 			$file->setPasswordProtected(true); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-09-25 16:34:35 +03:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		return $file; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 19:34:33 +03:00
										 |  |  | 	public function getToken(){ | 
					
						
							| 
									
										
										
										
											2014-04-11 00:59:51 +03:00
										 |  |  | 		return $this->token; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-09-25 16:34:35 +03:00
										 |  |  | 	public function getFileId(){ | 
					
						
							|  |  |  | 		return $this->fileId; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2014-04-10 21:23:26 +03:00
										 |  |  | 	public function setToken($token){ | 
					
						
							|  |  |  | 		$this->token = $token; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-09-26 21:01:41 +03:00
										 |  |  | 	public function isPublicShare(){ | 
					
						
							| 
									
										
										
										
											2014-04-10 21:23:26 +03:00
										 |  |  | 		return  !empty($this->token); | 
					
						
							| 
									
										
										
										
											2013-09-26 21:01:41 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	public function isPasswordProtected(){ | 
					
						
							|  |  |  | 		return $this->passwordProtected; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-08 22:05:31 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 18:06:05 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param string $password | 
					
						
							|  |  |  | 	 * @return boolean | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 	public function checkPassword($password){ | 
					
						
							| 
									
										
										
										
											2014-11-11 01:49:32 +03:00
										 |  |  | 		$shareId  = $this->sharing['id']; | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 		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; | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		// Check Password
 | 
					
						
							| 
									
										
										
										
											2014-11-17 14:11:37 +01:00
										 |  |  | 		$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); | 
					
						
							| 
									
										
										
										
											2014-11-17 14:11:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			/** | 
					
						
							|  |  |  | 			 * 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)) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2014-10-29 18:06:05 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param boolean $value | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 	public function setPasswordProtected($value){ | 
					
						
							|  |  |  | 		$this->passwordProtected = $value; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-17 21:33:24 +03:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	public function getOwner(){ | 
					
						
							|  |  |  | 		return $this->owner; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	public function getOwnerView($relativeToFiles = false){ | 
					
						
							|  |  |  | 		return $relativeToFiles ? $this->ownerViewFiles : $this->ownerView; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	public function getPath($relativeToFiles = false){ | 
					
						
							|  |  |  | 		return $relativeToFiles ? $this->pathFiles : $this->path; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2015-09-18 00:15:18 +03:00
										 |  |  | 	public function getPermissions(){ | 
					
						
							|  |  |  | 		$fileInfo = $this->ownerView->getFileInfo($this->path); | 
					
						
							|  |  |  | 		return $fileInfo->getPermissions(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2015-09-17 21:33:24 +03:00
										 |  |  | 	protected function initViews(){ | 
					
						
							|  |  |  | 		$this->ownerView = new View('/' . $this->owner); | 
					
						
							|  |  |  | 		$this->ownerViewFiles = new View('/' . $this->owner . '/files'); | 
					
						
							|  |  |  | 		$this->path = $this->ownerView->getPath($this->fileId); | 
					
						
							|  |  |  | 		$this->pathFiles = $this->ownerViewFiles->getPath($this->fileId); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if (!$this->path || !$this->pathFiles) { | 
					
						
							| 
									
										
										
										
											2014-04-11 00:59:51 +03:00
										 |  |  | 			throw new \Exception($this->fileId . ' can not be resolved'); | 
					
						
							| 
									
										
										
										
											2013-09-24 03:39:05 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2015-09-17 21:33:24 +03:00
										 |  |  | 		if (!$this->ownerView->file_exists($this->path)) { | 
					
						
							| 
									
										
										
										
											2013-09-26 21:01:41 +03:00
										 |  |  | 			throw new \Exception($this->path . ' doesn\'t exist'); | 
					
						
							| 
									
										
										
										
											2013-09-24 03:39:05 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-17 21:33:24 +03:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		if (!$this->ownerViewFiles->file_exists($this->pathFiles)) { | 
					
						
							|  |  |  | 			throw new \Exception($this->pathFiles . ' doesn\'t exist'); | 
					
						
							| 
									
										
										
										
											2013-09-26 21:01:41 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-18 00:15:18 +03:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		if (!$this->ownerView->is_file($this->path)){ | 
					
						
							|  |  |  | 			throw new \Exception('Object ' . $this->path . ' is not a file.'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		//TODO check if it is a valid odt
 | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		$mimetype = $this->ownerView->getMimeType($this->path); | 
					
						
							|  |  |  | 		if (!Filter::isSupportedMimetype($mimetype)){ | 
					
						
							|  |  |  | 			throw new \Exception( $this->path . ' is ' . $mimetype . ' and is not supported by Documents app'); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-09-26 21:01:41 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 	protected function getPassword(){ | 
					
						
							| 
									
										
										
										
											2014-11-11 01:49:32 +03:00
										 |  |  | 		return $this->sharing['share_with']; | 
					
						
							| 
									
										
										
										
											2013-11-10 12:50:06 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-09-26 21:01:41 +03:00
										 |  |  | } |