| 
									
										
										
										
											2023-01-25 19:03:19 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Settings; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-09 07:06:42 +08:00
										 |  |  | use BookStack\Uploads\FaviconHandler; | 
					
						
							| 
									
										
										
										
											2023-01-25 19:03:19 +08:00
										 |  |  | use BookStack\Uploads\ImageRepo; | 
					
						
							|  |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AppSettingsStore | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-09 07:06:42 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected ImageRepo $imageRepo, | 
					
						
							|  |  |  |         protected FaviconHandler $faviconHandler, | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2023-01-25 19:03:19 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function storeFromUpdateRequest(Request $request, string $category) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->storeSimpleSettings($request); | 
					
						
							|  |  |  |         if ($category === 'customization') { | 
					
						
							|  |  |  |             $this->updateAppLogo($request); | 
					
						
							|  |  |  |             $this->updateAppIcon($request); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function updateAppIcon(Request $request): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-26 00:11:34 +08:00
										 |  |  |         $sizes = [180, 128, 64, 32]; | 
					
						
							| 
									
										
										
										
											2023-01-25 19:03:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Update icon image if set
 | 
					
						
							|  |  |  |         if ($request->hasFile('app_icon')) { | 
					
						
							|  |  |  |             $iconFile = $request->file('app_icon'); | 
					
						
							|  |  |  |             $this->destroyExistingSettingImage('app-icon'); | 
					
						
							|  |  |  |             $image = $this->imageRepo->saveNew($iconFile, 'system', 0, 256, 256); | 
					
						
							|  |  |  |             setting()->put('app-icon', $image->url); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             foreach ($sizes as $size) { | 
					
						
							| 
									
										
										
										
											2023-01-26 00:11:34 +08:00
										 |  |  |                 $this->destroyExistingSettingImage('app-icon-' . $size); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:03:19 +08:00
										 |  |  |                 $icon = $this->imageRepo->saveNew($iconFile, 'system', 0, $size, $size); | 
					
						
							|  |  |  |                 setting()->put('app-icon-' . $size, $icon->url); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-02-09 07:06:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $this->faviconHandler->saveForUploadedImage($iconFile); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:03:19 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Clear icon image if requested
 | 
					
						
							|  |  |  |         if ($request->get('app_icon_reset')) { | 
					
						
							|  |  |  |             $this->destroyExistingSettingImage('app-icon'); | 
					
						
							|  |  |  |             setting()->remove('app-icon'); | 
					
						
							|  |  |  |             foreach ($sizes as $size) { | 
					
						
							|  |  |  |                 $this->destroyExistingSettingImage('app-icon-' . $size); | 
					
						
							|  |  |  |                 setting()->remove('app-icon-' . $size); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-02-09 21:24:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $this->faviconHandler->restoreOriginal(); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:03:19 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function updateAppLogo(Request $request): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Update logo image if set
 | 
					
						
							|  |  |  |         if ($request->hasFile('app_logo')) { | 
					
						
							|  |  |  |             $logoFile = $request->file('app_logo'); | 
					
						
							|  |  |  |             $this->destroyExistingSettingImage('app-logo'); | 
					
						
							|  |  |  |             $image = $this->imageRepo->saveNew($logoFile, 'system', 0, null, 86); | 
					
						
							|  |  |  |             setting()->put('app-logo', $image->url); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Clear logo image if requested
 | 
					
						
							|  |  |  |         if ($request->get('app_logo_reset')) { | 
					
						
							|  |  |  |             $this->destroyExistingSettingImage('app-logo'); | 
					
						
							|  |  |  |             setting()->remove('app-logo'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function storeSimpleSettings(Request $request): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach ($request->all() as $name => $value) { | 
					
						
							|  |  |  |             if (strpos($name, 'setting-') !== 0) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $key = str_replace('setting-', '', trim($name)); | 
					
						
							|  |  |  |             setting()->put($key, $value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function destroyExistingSettingImage(string $settingKey) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $existingVal = setting()->get($settingKey); | 
					
						
							|  |  |  |         if ($existingVal) { | 
					
						
							|  |  |  |             $this->imageRepo->destroyByUrlAndType($existingVal, 'system'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |