| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Settings; | 
					
						
							| 
									
										
										
										
											2021-01-17 21:21:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-19 03:53:39 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2021-01-17 21:21:57 +08:00
										 |  |  | use Illuminate\Support\Facades\Cache; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\Session; | 
					
						
							|  |  |  | use Illuminate\Support\Str; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class StatusController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the system status as a simple json page. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function show() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $statuses = [ | 
					
						
							|  |  |  |             'database' => $this->trueWithoutError(function () { | 
					
						
							|  |  |  |                 return DB::table('migrations')->count() > 0; | 
					
						
							|  |  |  |             }), | 
					
						
							|  |  |  |             'cache' => $this->trueWithoutError(function () { | 
					
						
							| 
									
										
										
										
											2023-08-18 04:24:35 +08:00
										 |  |  |                 $rand = Str::random(12); | 
					
						
							|  |  |  |                 $key = "status_test_{$rand}"; | 
					
						
							|  |  |  |                 Cache::add($key, $rand); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-18 04:24:35 +08:00
										 |  |  |                 return Cache::pull($key) === $rand; | 
					
						
							| 
									
										
										
										
											2021-01-17 21:21:57 +08:00
										 |  |  |             }), | 
					
						
							|  |  |  |             'session' => $this->trueWithoutError(function () { | 
					
						
							|  |  |  |                 $rand = Str::random(); | 
					
						
							|  |  |  |                 Session::put('status_test', $rand); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-17 21:21:57 +08:00
										 |  |  |                 return Session::get('status_test') === $rand; | 
					
						
							|  |  |  |             }), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $hasError = in_array(false, $statuses); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-17 21:21:57 +08:00
										 |  |  |         return response()->json($statuses, $hasError ? 500 : 200); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Check the callable passed returns true and does not throw an exception. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function trueWithoutError(callable $test): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             return $test() === true; | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |