| 
									
										
										
										
											2018-04-18 19:03:39 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Grocy\Helpers; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UrlManager | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-16 17:32:08 +02:00
										 |  |  | 	public function __construct(string $basePath) | 
					
						
							| 
									
										
										
										
											2018-04-18 19:03:39 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2021-07-16 17:32:08 +02:00
										 |  |  | 		if ($basePath === '/') | 
					
						
							| 
									
										
										
										
											2018-06-15 20:50:40 +02:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2021-07-16 17:32:08 +02:00
										 |  |  | 			$this->BasePath = $this->GetBaseUrl(); | 
					
						
							| 
									
										
										
										
											2018-06-15 20:50:40 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-09-01 21:29:47 +02:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2021-07-16 17:32:08 +02:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			$this->BasePath = $basePath; | 
					
						
							| 
									
										
										
										
											2018-06-15 20:50:40 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-16 17:32:08 +02:00
										 |  |  | 	protected $BasePath; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public function ConstructUrl($relativePath, $isResource = false) | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2021-07-16 17:32:08 +02:00
										 |  |  | 		if (GROCY_DISABLE_URL_REWRITING === false || $isResource === true) | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2021-07-16 17:32:08 +02:00
										 |  |  | 			return rtrim($this->BasePath, '/') . $relativePath; | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							| 
									
										
										
										
											2021-07-16 17:32:08 +02:00
										 |  |  | 		{ // Is not a resource and URL rewriting is disabled
 | 
					
						
							|  |  |  | 			return rtrim($this->BasePath, '/') . '/index.php' . $relativePath; | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-18 19:03:39 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-21 19:18:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	private function GetBaseUrl() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-09-21 12:49:01 +02:00
										 |  |  | 		if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			$_SERVER['HTTPS'] = 'on'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-31 20:40:31 +02:00
										 |  |  | 		return (isset($_SERVER['HTTPS']) ? 'https' : 'http') . "://$_SERVER[HTTP_HOST]"; | 
					
						
							| 
									
										
										
										
											2018-04-21 19:18:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-18 19:03:39 +02:00
										 |  |  | } |