| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * CMS Pico - Integration of Pico within your files to create websites. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * later. See the COPYING file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Maxence Lange <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @copyright 2017 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | namespace OCA\CMSPico\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCA\CMSPico\Db\WebsitesRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use OCA\CMSPico\Exceptions\WebsiteAlreadyExistException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use OCA\CMSPico\Exceptions\WebsiteDoesNotExistException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use OCA\CMSPico\Model\Website; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use OCP\IL10N; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | class WebsitesService { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 	/** @var IL10N */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	private $l10n; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 	/** @var WebsitesRequest */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	private $websiteRequest; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	/** @var TemplatesService */ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	private $templatesService; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	/** @var PicoService */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	private $picoService; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 	/** @var MiscService */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 	private $miscService; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 	 * WebsitesService constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 	 * @param IL10N $l10n | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 	 * @param WebsitesRequest $websiteRequest | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 	 * @param TemplatesService $templatesService | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 	 * @param PicoService $picoService | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 	 * @param MiscService $miscService | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 	function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 		IL10N $l10n, WebsitesRequest $websiteRequest, TemplatesService $templatesService, | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 		PicoService $picoService, MiscService $miscService | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 	) { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 		$this->l10n = $l10n; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 		$this->websiteRequest = $websiteRequest; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 		$this->templatesService = $templatesService; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 		$this->picoService = $picoService; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 		$this->miscService = $miscService; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 	 * @param string $name | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 	 * @param string $userId | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 	 * @param string $site | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 	 * @param string $path | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	 * @param int $template | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 	 * @throws WebsiteAlreadyExistException | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 	public function createWebsite($name, $userId, $site, $path, $template) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 		$this->templatesService->templateHasToExist($template); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 		$website = new Website(); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 		$website->setName($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 				->setUserId($userId) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 				->setSite($site) | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 				->setPath($path) | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 				->setTemplateSource(TemplatesService::TEMPLATES[$template]); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 		try { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 			$website->hasToBeFilledWithValidEntries(); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  | 			$website = $this->websiteRequest->getWebsiteFromSite($website->getSite()); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  | 			throw new WebsiteAlreadyExistException($this->l10n->t('Website already exist.')); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  | 		} catch (WebsiteDoesNotExistException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 			// In fact we want the website to not exist (yet). | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | 		$this->templatesService->installTemplates($website); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 		$this->websiteRequest->create($website); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 	 * @param int $siteId | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 	 * @param string $userId | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 	public function deleteWebsite($siteId, $userId) { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 		$website = $this->getWebsiteFromId($siteId); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 		$website->hasToBeOwnedBy($userId); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 		$this->forceDeleteWebsite($website); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  | 	 * @param Website $website | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  | 	public function forceDeleteWebsite(Website $website) { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  | 		$this->websiteRequest->delete($website); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  | 	 * @param string $userId | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | 	public function onUserRemoved($userId) { | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  | 		$this->websiteRequest->deleteAllFromUser($userId); | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  | 	 * @param int $siteId | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  | 	 * @return Website | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  | 	public function getWebsiteFromId($siteId) { | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  | 		return $this->websiteRequest->getWebsiteFromId($siteId); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  | 	 * @param Website $website | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	public function updateWebsite(Website $website) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  | 		$this->websiteRequest->update($website); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 150 |  |  | 	} | 
            
                                                                        
                                                                
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 152 |  |  | 	/** | 
            
                                                                        
                                                                
            
                                    
            
            
                | 153 |  |  | 	 * @param string $userId | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  | 	 * @return Website[] | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	public function getWebsitesFromUser($userId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		$websites = $this->websiteRequest->getWebsitesFromUserId($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 160 |  |  | 		return $websites; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 161 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  | 	 * @param string $site | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  | 	 * @return Website | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	public function getWebsiteFromSite($site) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		$website = $this->websiteRequest->getWebsiteFromSite($site); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 173 |  |  | 		return $website; | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  | 	 * @param string $site | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  | 	 * @param string $viewer | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  | 	 * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	public function getWebpageFromSite($site, $viewer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 		$website = $this->websiteRequest->getWebsiteFromSite($site); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 		$website->setViewer($viewer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 188 |  |  | 		return $this->picoService->getContent($website); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 192 |  |  | } |