| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * phpBB Media Embed PlugIn extension for the phpBB Forum Software package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright (c) 2020 phpBB Limited <https://www.phpbb.com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license GNU General Public License, version 2 (GPL-2.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace phpbb\mediaembed\cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use phpbb\cache\driver\driver_interface as cache_driver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Media Embed cache handling class. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | class cache | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 	/** @var cache_driver */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | 	protected $cache; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 	/** @var string Cache key used for the parser */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 	protected $cache_key_parser; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 	/** @var string Cache key used for the renderer */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 	protected $cache_key_renderer; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 	 * Constructor | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 	 * @param cache_driver $cache              Cache driver object | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 	 * @param string       $cache_key_parser   Cache key used for the parser | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 	 * @param string       $cache_key_renderer Cache key used for the renderer | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	public function __construct(cache_driver $cache, $cache_key_parser, $cache_key_renderer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		$this->cache = $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		$this->cache_key_parser = $cache_key_parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$this->cache_key_renderer = $cache_key_renderer; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 	 * Purge cached MediaEmbed files | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	 * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	public function purge_mediaembed_cache() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$finder = new Finder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$finder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			->name('http.*') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			->in($this->cache->cache_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			->files(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		foreach ($finder as $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			$this->cache->remove_file($file->getRealPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 	 * Purge cached TextFormatter files | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 	 * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	public function purge_textformatter_cache() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$this->cache->destroy($this->cache_key_parser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$this->cache->destroy($this->cache_key_renderer); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |