| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright Copyright (c) 2016-2017 Lukas Reschke <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | namespace OCA\Richdocuments\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use OC\Files\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use OCA\Richdocuments\Db\WopiMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use OCA\Richdocuments\TemplateManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCA\Richdocuments\TokenManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCA\Richdocuments\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCP\AppFramework\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use OCP\AppFramework\Db\DoesNotExistException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use OCP\AppFramework\Http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use OCP\AppFramework\Http\JSONResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use OCP\Files\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use OCP\Files\IRootFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use OCP\IConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | use OCP\ILogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | use OCP\IRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | use OCP\IURLGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use OCP\AppFramework\Http\StreamResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use OCP\IUserManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | use OCP\IUserSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | class WopiController extends Controller { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** @var IRootFolder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	private $rootFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	/** @var IURLGenerator */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	private $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	/** @var IConfig */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** @var TokenManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	private $tokenManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	/** @var IUserManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	private $userManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	/** @var WopiMapper */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	private $wopiMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	/** @var ILogger */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	private $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	/** @var IUserSession */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	private $userSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	/** @var TemplateManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	private $templateManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	// Signifies LOOL that document has been changed externally in this storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	const LOOL_STATUS_DOC_CHANGED = 1010; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * @param string $appName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * @param IRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * @param IRootFolder $rootFolder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * @param IURLGenerator $urlGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * @param IConfig $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * @param TokenManager $tokenManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @param IUserManager $userManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @param WopiMapper $wopiMapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * @param ILogger $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @param IUserSession $userSession | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * @param TemplateManager $templateManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 79 |  | View Code Duplication | 	public function __construct($appName, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 								IRequest $request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 								IRootFolder $rootFolder, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 								IURLGenerator $urlGenerator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 								IConfig $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 								TokenManager $tokenManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 								IUserManager $userManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 								WopiMapper $wopiMapper, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 								ILogger $logger, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 								IUserSession $userSession, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 								TemplateManager $templateManager) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		parent::__construct($appName, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$this->rootFolder = $rootFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$this->urlGenerator = $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$this->tokenManager = $tokenManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$this->userManager = $userManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		$this->wopiMapper = $wopiMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		$this->logger = $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$this->userSession = $userSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$this->templateManager = $templateManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * Returns general info about a file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * @NoCSRFRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * @PublicPage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 * @param string $fileId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @param string $access_token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * @return JSONResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @throws \OCP\Files\InvalidPathException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * @throws \OCP\Files\NotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	public function checkFileInfo($fileId, $access_token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		list($fileId, , $version) = Helper::parseFileId($fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 			$wopi = $this->wopiMapper->getPathForToken($access_token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		} catch (DoesNotExistException $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			return new JSONResponse([], Http::STATUS_FORBIDDEN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		if ($wopi->isTemplateToken()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 			$this->templateManager->setUserId($wopi->getOwnerUid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 			$file = $this->templateManager->get($wopi->getFileid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			// Login the user to see his mount locations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 			try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 				/** @var File $file */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 				$userFolder = $this->rootFolder->getUserFolder($wopi->getOwnerUid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 				$file = $userFolder->getById($fileId)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			} catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 				return new JSONResponse([], Http::STATUS_FORBIDDEN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		if(!($file instanceof File)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			return new JSONResponse([], Http::STATUS_FORBIDDEN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		$response = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			'BaseFileName' => $file->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 			'Size' => $file->getSize(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			'Version' => $version, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 			'UserId' => !is_null($wopi->getEditorUid()) ? $wopi->getEditorUid() : 'guest', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			'OwnerId' => $wopi->getOwnerUid(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			'UserFriendlyName' => !is_null($wopi->getEditorUid()) ? \OC_User::getDisplayName($wopi->getEditorUid()) : $wopi->getGuestDisplayname(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 			'UserExtraInfo' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 			], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 			'UserCanWrite' => $wopi->getCanwrite(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 			'UserCanNotWriteRelative' => \OC::$server->getEncryptionManager()->isEnabled() ? true : is_null($wopi->getEditorUid()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 			'PostMessageOrigin' => $wopi->getServerHost(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 			'LastModifiedTime' => Helper::toISO8601($file->getMTime()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 			'EnableInsertRemoteImage' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 			'EnableShare' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		if ($wopi->isTemplateToken()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			$userFolder = $this->rootFolder->getUserFolder($wopi->getOwnerUid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 			$file = $userFolder->getById($wopi->getTemplateDestination())[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 			$response['TemplateSaveAs'] = $file->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		$user = $this->userManager->get($wopi->getEditorUid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		if($user !== null && $user->getAvatarImage(32) !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 			$response['UserExtraInfo']['avatar'] = $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $wopi->getEditorUid(), 'size' => 32]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		return new JSONResponse($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	 * Given an access token and a fileId, returns the contents of the file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	 * Expects a valid token in access_token parameter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	 * @PublicPage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	 * @NoCSRFRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	 * @param string $fileId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 	 * @param string $access_token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	 * @return Http\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	public function getFile($fileId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 							$access_token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 		list($fileId, , $version) = Helper::parseFileId($fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 		$wopi = $this->wopiMapper->getPathForToken($access_token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 		if ((int)$fileId !== $wopi->getFileid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 			return new JSONResponse([], Http::STATUS_FORBIDDEN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 		// Template is just returned as there is no version logic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 		if ($wopi->isTemplateToken()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 			$this->templateManager->setUserId($wopi->getOwnerUid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 			$file = $this->templateManager->get($wopi->getFileid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 			$response = new StreamResponse($file->fopen('rb')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 			$response->addHeader('Content-Disposition', 'attachment'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 			$response->addHeader('Content-Type', 'application/octet-stream'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 			return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 			/** @var File $file */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 			$userFolder = $this->rootFolder->getUserFolder($wopi->getOwnerUid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 			$file = $userFolder->getById($fileId)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 			\OC_User::setIncognitoMode(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | 			if ($version !== '0') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | 				$view = new View('/' . $wopi->getOwnerUid() . '/files'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 				$relPath = $view->getRelativePath($file->getPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  | 				$versionPath = '/files_versions/' . $relPath . '.v' . $version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 				$view = new View('/' . $wopi->getOwnerUid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  | 				if ($view->file_exists($versionPath)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | 					$response = new StreamResponse($view->fopen($versionPath, 'rb')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | 				else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | 					return new JSONResponse([], Http::STATUS_NOT_FOUND); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | 			else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  | 				$response = new StreamResponse($file->fopen('rb')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  | 			$response->addHeader('Content-Disposition', 'attachment'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  | 			$response->addHeader('Content-Type', 'application/octet-stream'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  | 			return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 		} catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  | 			return new JSONResponse([], Http::STATUS_FORBIDDEN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  | 	 * Given an access token and a fileId, replaces the files with the request body. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  | 	 * Expects a valid token in access_token parameter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  | 	 * @PublicPage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  | 	 * @NoCSRFRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  | 	 * @param string $fileId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 	 * @param string $access_token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  | 	 * @return JSONResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  | 	public function putFile($fileId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  | 							$access_token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  | 		list($fileId, ,) = Helper::parseFileId($fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  | 		$isPutRelative = ($this->request->getHeader('X-WOPI-Override') === 'PUT_RELATIVE'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  | 		$wopi = $this->wopiMapper->getPathForToken($access_token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 		if (!$wopi->getCanwrite()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  | 			return new JSONResponse([], Http::STATUS_FORBIDDEN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  | 		// Unless the editor is empty (public link) we modify the files as the current editor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  | 		$editor = $wopi->getEditorUid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  | 		if ($editor === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  | 			$editor = $wopi->getOwnerUid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | 			/** @var File $file */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  | 			$userFolder = $this->rootFolder->getUserFolder($editor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  | 			$file = $userFolder->getById($fileId)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  | 			if ($isPutRelative) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 				// the new file needs to be installed in the current user dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  | 				$userFolder = $this->rootFolder->getUserFolder($wopi->getEditorUid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  | 				$file = $userFolder->getById($fileId)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | 				$suggested = $this->request->getHeader('X-WOPI-SuggestedTarget'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | 				$suggested = iconv('utf-7', 'utf-8', $suggested); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 273 |  | View Code Duplication | 				if ($suggested[0] === '.') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  | 					$path = dirname($file->getPath()) . '/New File' . $suggested; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 				else if ($suggested[0] !== '/') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  | 					$path = dirname($file->getPath()) . '/' . $suggested; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  | 				else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  | 					$path = $userFolder->getPath() . $suggested; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  | 				if ($path === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  | 					return new JSONResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  | 						'status' => 'error', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  | 						'message' => 'Cannot create the file' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  | 					]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  | 				$root = \OC::$server->getRootFolder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  | 				// create the folder first | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  | 				if (!$root->nodeExists(dirname($path))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  | 					$root->newFolder(dirname($path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  | 				// create a unique new file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  | 				$path = $root->getNonExistingName($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  | 				$root->newFile($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  | 				$file = $root->get($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  | 			else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  | 				$wopiHeaderTime = $this->request->getHeader('X-LOOL-WOPI-Timestamp'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  | 				if (!is_null($wopiHeaderTime) && $wopiHeaderTime != Helper::toISO8601($file->getMTime())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  | 					$this->logger->debug('Document timestamp mismatch ! WOPI client says mtime {headerTime} but storage says {storageTime}', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  | 						'headerTime' => $wopiHeaderTime, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  | 						'storageTime' => Helper::toISO8601($file->getMTime()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  | 					]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  | 					// Tell WOPI client about this conflict. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  | 					return new JSONResponse(['LOOLStatusCode' => self::LOOL_STATUS_DOC_CHANGED], Http::STATUS_CONFLICT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  | 			$content = fopen('php://input', 'rb'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  | 			// Set the user to register the change under his name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  | 			$editor = $this->userManager->get($wopi->getEditorUid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  | 			if (!is_null($editor)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  | 				$this->userSession->setUser($editor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  | 			$file->putContent($content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  | 			if ($isPutRelative) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  | 				// generate a token for the new file (the user still has to be | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  | 				// logged in) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  | 				list(, $wopiToken) = $this->tokenManager->getToken($file->getId(), null, $wopi->getEditorUid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  | 				$wopi = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopiToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  | 				$url = $this->urlGenerator->getAbsoluteURL($wopi); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  | 				return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $url ], Http::STATUS_OK); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  | 			return new JSONResponse(['LastModifiedTime' => Helper::toISO8601($file->getMTime())]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  | 		} catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  | 			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  | 	 * Given an access token and a fileId, replaces the files with the request body. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  | 	 * Expects a valid token in access_token parameter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  | 	 * Just actually routes to the PutFile, the implementation of PutFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  | 	 * handles both saving and saving as.* Given an access token and a fileId, replaces the files with the request body. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  | 	 * @PublicPage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  | 	 * @NoCSRFRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  | 	 * @param string $fileId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  | 	 * @param string $access_token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  | 	 * @return JSONResponse | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 353 |  |  | 	 */ | 
            
                                                        
            
                                    
            
            
                | 354 |  |  | 	public function putRelativeFile($fileId, | 
            
                                                        
            
                                    
            
            
                | 355 |  |  | 					$access_token) { | 
            
                                                        
            
                                    
            
            
                | 356 |  |  | 		list($fileId, ,) = Helper::parseFileId($fileId); | 
            
                                                        
            
                                    
            
            
                | 357 |  |  | 		$wopi = $this->wopiMapper->getPathForToken($access_token); | 
            
                                                        
            
                                    
            
            
                | 358 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 359 |  |  | 		if (!$wopi->getCanwrite()) { | 
            
                                                        
            
                                    
            
            
                | 360 |  |  | 			return new JSONResponse([], Http::STATUS_FORBIDDEN); | 
            
                                                        
            
                                    
            
            
                | 361 |  |  | 		} | 
            
                                                        
            
                                    
            
            
                | 362 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 363 |  |  | 		// Unless the editor is empty (public link) we modify the files as the current editor | 
            
                                                        
            
                                    
            
            
                | 364 |  |  | 		$editor = $wopi->getEditorUid(); | 
            
                                                        
            
                                    
            
            
                | 365 |  |  | 		if ($editor === null) { | 
            
                                                        
            
                                    
            
            
                | 366 |  |  | 			$editor = $wopi->getOwnerUid(); | 
            
                                                        
            
                                    
            
            
                | 367 |  |  | 		} | 
            
                                                        
            
                                    
            
            
                | 368 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 369 |  |  | 		try { | 
            
                                                        
            
                                    
            
            
                | 370 |  |  | 			// the new file needs to be installed in the current user dir | 
            
                                                        
            
                                    
            
            
                | 371 |  |  | 			$userFolder = $this->rootFolder->getUserFolder($editor); | 
            
                                                        
            
                                    
            
            
                | 372 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 373 |  |  | 			if ($wopi->isTemplateToken()) { | 
            
                                                        
            
                                    
            
            
                | 374 |  |  | 				$this->templateManager->setUserId($wopi->getOwnerUid()); | 
            
                                                        
            
                                    
            
            
                | 375 |  |  | 				$file = $userFolder->getById($wopi->getTemplateDestination())[0]; | 
            
                                                        
            
                                    
            
            
                | 376 |  |  | 			} else { | 
            
                                                        
            
                                    
            
            
                | 377 |  |  | 				$file = $userFolder->getById($fileId)[0]; | 
            
                                                        
            
                                    
            
            
                | 378 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 379 |  |  | 				$suggested = $this->request->getHeader('X-WOPI-SuggestedTarget'); | 
            
                                                        
            
                                    
            
            
                | 380 |  |  | 				$suggested = iconv('utf-7', 'utf-8', $suggested); | 
            
                                                        
            
                                    
            
            
                | 381 |  |  |  | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 382 |  | View Code Duplication | 				if ($suggested[0] === '.') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 383 |  |  | 					$path = dirname($file->getPath()) . '/New File' . $suggested; | 
            
                                                        
            
                                    
            
            
                | 384 |  |  | 				} else if ($suggested[0] !== '/') { | 
            
                                                        
            
                                    
            
            
                | 385 |  |  | 					$path = dirname($file->getPath()) . '/' . $suggested; | 
            
                                                        
            
                                    
            
            
                | 386 |  |  | 				} else { | 
            
                                                        
            
                                    
            
            
                | 387 |  |  | 					$path = $userFolder->getPath() . $suggested; | 
            
                                                        
            
                                    
            
            
                | 388 |  |  | 				} | 
            
                                                        
            
                                    
            
            
                | 389 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 390 |  |  | 				if ($path === '') { | 
            
                                                        
            
                                    
            
            
                | 391 |  |  | 					return new JSONResponse([ | 
            
                                                        
            
                                    
            
            
                | 392 |  |  | 						'status' => 'error', | 
            
                                                        
            
                                    
            
            
                | 393 |  |  | 						'message' => 'Cannot create the file' | 
            
                                                        
            
                                    
            
            
                | 394 |  |  | 					]); | 
            
                                                        
            
                                    
            
            
                | 395 |  |  | 				} | 
            
                                                        
            
                                    
            
            
                | 396 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 397 |  |  | 				// create the folder first | 
            
                                                        
            
                                    
            
            
                | 398 |  |  | 				if (!$this->rootFolder->nodeExists(dirname($path))) { | 
            
                                                        
            
                                    
            
            
                | 399 |  |  | 					$this->rootFolder->newFolder(dirname($path)); | 
            
                                                        
            
                                    
            
            
                | 400 |  |  | 				} | 
            
                                                        
            
                                    
            
            
                | 401 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 402 |  |  | 				// create a unique new file | 
            
                                                        
            
                                    
            
            
                | 403 |  |  | 				$path = $this->rootFolder->getNonExistingName($path); | 
            
                                                        
            
                                    
            
            
                | 404 |  |  | 				$file = $this->rootFolder->newFile($path); | 
            
                                                        
            
                                    
            
            
                | 405 |  |  | 			} | 
            
                                                        
            
                                    
            
            
                | 406 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 407 |  |  | 			$content = fopen('php://input', 'rb'); | 
            
                                                        
            
                                    
            
            
                | 408 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 409 |  |  | 			// Set the user to register the change under his name | 
            
                                                        
            
                                    
            
            
                | 410 |  |  | 			$editor = $this->userManager->get($wopi->getEditorUid()); | 
            
                                                        
            
                                    
            
            
                | 411 |  |  | 			if (!is_null($editor)) { | 
            
                                                        
            
                                    
            
            
                | 412 |  |  | 				$this->userSession->setUser($editor); | 
            
                                                        
            
                                    
            
            
                | 413 |  |  | 			} | 
            
                                                        
            
                                    
            
            
                | 414 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 415 |  |  | 			$file->putContent($content); | 
            
                                                        
            
                                    
            
            
                | 416 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 417 |  |  | 			// generate a token for the new file (the user still has to be | 
            
                                                        
            
                                    
            
            
                | 418 |  |  | 			// logged in) | 
            
                                                        
            
                                    
            
            
                | 419 |  |  | 			list(, $wopiToken) = $this->tokenManager->getToken($file->getId(), null, $wopi->getEditorUid()); | 
            
                                                        
            
                                    
            
            
                | 420 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 421 |  |  | 			$wopi = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopiToken; | 
            
                                                        
            
                                    
            
            
                | 422 |  |  | 			$url = $this->urlGenerator->getAbsoluteURL($wopi); | 
            
                                                        
            
                                    
            
            
                | 423 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 424 |  |  | 			return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $url ], Http::STATUS_OK); | 
            
                                                        
            
                                    
            
            
                | 425 |  |  | 		} catch (\Exception $e) { | 
            
                                                        
            
                                    
            
            
                | 426 |  |  | 			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); | 
            
                                                        
            
                                    
            
            
                | 427 |  |  | 		} | 
            
                                                        
            
                                    
            
            
                | 428 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 429 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 430 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.