| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright 2017, Roeland Jago Douma <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Roeland Jago Douma <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | namespace OC\Template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use OC\SystemConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use OCP\ICache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCP\Files\IAppData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCP\Files\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCP\Files\NotPermittedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use OCP\Files\SimpleFS\ISimpleFolder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use OCP\IURLGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | class JSCombiner { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** @var IAppData */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	protected $appData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** @var IURLGenerator */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	protected $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	/** @var ICache */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	protected $depsCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** @var SystemConfig */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @param IAppData $appData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param IURLGenerator $urlGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @param ICache $depsCache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * @param SystemConfig $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	public function __construct(IAppData $appData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 								IURLGenerator $urlGenerator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 								ICache $depsCache, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 								SystemConfig $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$this->appData = $appData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$this->urlGenerator = $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$this->depsCache = $depsCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * @param string $root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * @param string $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * @param string $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	public function process($root, $file, $app) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		if ($this->config->getValue('debug') || !$this->config->getValue('installed')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		$path = explode('/', $root . '/' . $file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$fileName = array_pop($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		$path = implode('/', $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			$folder = $this->appData->getFolder($app); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		} catch(NotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			// creating css appdata folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			$folder = $this->appData->newFolder($app); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		if($this->isCached($fileName, $folder)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		return $this->cache($path, $fileName, $folder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * @param string $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * @param ISimpleFolder $folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	protected function isCached($fileName, ISimpleFolder $folder) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$fileName = str_replace('.json', '.js', $fileName) . '.deps'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			$deps = $this->depsCache->get($folder->getName() . '-' . $fileName); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 101 |  | View Code Duplication | 			if ($deps === null || $deps === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 				$depFile = $folder->getFile($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 				$deps = $depFile->getContent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 				$this->depsCache->set($folder->getName() . '-' . $fileName, $deps); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			$deps = json_decode($deps, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 108 |  | View Code Duplication | 			foreach ($deps as $file=>$mtime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 				if (!file_exists($file) || filemtime($file) > $mtime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 					return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		} catch(NotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	 * @param string $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * @param ISimpleFolder $folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	protected function cache($path, $fileName, ISimpleFolder $folder) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		$deps = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		$fullPath = $path . '/' . $fileName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		$data = json_decode(file_get_contents($fullPath)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		$deps[$fullPath] = filemtime($fullPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		$res = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		foreach ($data as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			$filePath = $path . '/' . $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			if (is_file($filePath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 				$res .= file_get_contents($filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 				$res .= PHP_EOL . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 				$deps[$filePath] = filemtime($filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		$fileName = str_replace('.json', '.js', $fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			$cachedfile = $folder->getFile($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		} catch(NotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			$cachedfile = $folder->newFile($fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		$depFileName = $fileName . '.deps'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 			$depFile = $folder->getFile($depFileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		} catch (NotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 			$depFile = $folder->newFile($depFileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 			$gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		} catch (NotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			$gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 			$cachedfile->putContent($res); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 			$depFile->putContent(json_encode($deps)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 			$gzipFile->putContent(gzencode($res, 9)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		} catch (NotPermittedException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	 * @param string $appName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	 * @param string $fileName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 178 |  | View Code Duplication | 	public function getCachedJS($appName, $fileName) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 		$tmpfileLoc = explode('/', $fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		$fileName = array_pop($tmpfileLoc); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		$fileName = str_replace('.json', '.js', $fileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		return substr($this->urlGenerator->linkToRoute('core.Js.getJs', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 	 * @param string $root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	 * @param string $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 	 * @return string[] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  | 	 */ | 
            
                                                        
            
                                    
            
            
                | 191 |  |  | 	public function getContent($root, $file) { | 
            
                                                        
            
                                    
            
            
                | 192 |  |  | 		/** @var array $data */ | 
            
                                                        
            
                                    
            
            
                | 193 |  |  | 		$data = json_decode(file_get_contents($root . '/' . $file)); | 
            
                                                        
            
                                    
            
            
                | 194 |  |  | 		if(!is_array($data)) { | 
            
                                                        
            
                                    
            
            
                | 195 |  |  | 			return []; | 
            
                                                        
            
                                    
            
            
                | 196 |  |  | 		} | 
            
                                                        
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 198 |  |  | 		$path = explode('/', $file); | 
            
                                                        
            
                                    
            
            
                | 199 |  |  | 		array_pop($path); | 
            
                                                        
            
                                    
            
            
                | 200 |  |  | 		$path = implode('/', $path); | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 202 |  |  | 		$result = []; | 
            
                                                        
            
                                    
            
            
                | 203 |  |  | 		foreach ($data as $f) { | 
            
                                                        
            
                                    
            
            
                | 204 |  |  | 			$result[] = $path . '/' . $f; | 
            
                                                        
            
                                    
            
            
                | 205 |  |  | 		} | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 207 |  |  | 		return $result; | 
            
                                                        
            
                                    
            
            
                | 208 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 209 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 210 |  |  |  | 
            
                        
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.