| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * CMS Pico - Create websites using Pico CMS for Nextcloud. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Copyright (c) 2020, Daniel Rudolf (<[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 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | namespace OCA\CMSPico\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCA\CMSPico\Files\AbstractNode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCA\CMSPico\Files\FileInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCA\CMSPico\Files\FolderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use OCA\CMSPico\Files\NodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use OCP\Constants; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use OCP\Files\InvalidPathException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use OCP\Files\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use OCP\Files\NotPermittedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | class DummyPluginFile extends AbstractNode implements FileInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	private $pluginName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	/** @var FileInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	private $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * DummyPluginFile constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * @param string        $pluginName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @param FileInterface $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @throws InvalidPathException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	public function __construct(string $pluginName, FileInterface $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->file = $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$this->rename($pluginName . '.' . $this->getExtension()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 	public function rename(string $name): NodeInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 		$this->assertValidFileName($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 		$extension = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 		if (($extensionPos = strrpos($name, '.')) !== false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 			$extension = substr($name, $extensionPos + 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 			$name = substr($name, 0, $extensionPos); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 		if ($extension !== $this->getExtension()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 			throw new InvalidPathException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		if (!preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 			throw new InvalidPathException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 		$this->pluginName = $name; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 		return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	public function delete(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		// nothing to do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	public function getPath(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		return '/' . $this->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	public function getLocalPath(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		throw new NotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	public function getName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		return $this->pluginName . '.' . $this->getExtension(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	public function getParent(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		return '/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	public function getParentNode(): FolderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		throw new InvalidPathException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	public function isLocal(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	public function getPermissions(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		return Constants::PERMISSION_READ | Constants::PERMISSION_DELETE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	public function getExtension(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		return 'php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	public function getContent(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		return preg_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 			'/^class DummyPlugin(?= |$)/m', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 			'class ' . $this->pluginName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 			$this->file->getContent() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	 * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	public function putContent(string $data): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		throw new NotPermittedException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 175 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |  |