| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright Copyright (c) 2016 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\WOPI; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use OCP\AppFramework\Utility\ITimeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use OCP\Files\IAppData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use OCP\Files\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCP\Files\SimpleFS\ISimpleFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCP\Http\Client\IClientService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCP\IConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use OCP\IL10N; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | class DiscoveryManager { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** @var IClientService */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	private $clientService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** @var ISimpleFolder */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	private $appData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** @var IConfig */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** @var IL10N */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	private $l10n; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	/** @var ITimeFactory */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	private $timeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * @param IClientService $clientService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * @param IAppData $appData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * @param IConfig $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @param IL10N $l10n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param ITimeFactory $timeFactory | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  | 	 */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 51 |  | View Code Duplication | 	public function __construct(IClientService $clientService, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 								IAppData $appData, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 								IConfig $config, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 								IL10N $l10n, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 								ITimeFactory $timeFactory) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 		$this->clientService = $clientService; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 		try { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 			$this->appData = $appData->getFolder('richdocuments'); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 		} catch (NotFoundException $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 			$this->appData = $appData->newFolder('richdocuments'); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 		$this->config = $config; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 		$this->timeFactory = $timeFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	public function get() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		// First check if there is a local valid discovery file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			$file = $this->appData->getFile('discovery.xml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			$decodedFile = json_decode($file->getContent(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			if($decodedFile['timestamp'] + 3600 > $this->timeFactory->getTime()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 				return $decodedFile['data']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		} catch (NotFoundException $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			$file = $this->appData->newFile('discovery.xml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$response = $this->fetchFromRemote(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$responseBody = $response->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$file->putContent( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			json_encode([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 				'data' => $responseBody, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 				'timestamp' => $this->timeFactory->getTime(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		return $responseBody; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 * @return \OCP\Http\Client\IResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	public function fetchFromRemote() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		$wopiDiscovery = $remoteHost . '/hosting/discovery'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$client = $this->clientService->newClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$options = ['timeout' => 5]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			$options['verify'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			return $client->get($wopiDiscovery, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		} catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	public function refretch() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			$this->appData->getFile('discovery.xml')->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		} catch(\Exception $e) {} | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  | 
            
                        
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.