| 
									
										
										
										
											2024-08-28 09:59:13 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 16:34:54 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * class Config | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2025-01-23 10:42:27 +00:00
										 |  |  |  * Handles editing and fetching ot the config files. | 
					
						
							| 
									
										
										
										
											2024-11-29 16:34:54 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-08-28 09:59:13 +00:00
										 |  |  | class Config { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 16:34:54 +00:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-01-23 10:42:27 +00:00
										 |  |  |      * Edits a config file by updating specified options. | 
					
						
							| 
									
										
										
										
											2024-11-29 16:34:54 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-23 10:42:27 +00:00
										 |  |  |      * @param array $updatedConfig Key-value pairs of config options to update. | 
					
						
							|  |  |  |      * @param string $config_file Path to the config file. | 
					
						
							| 
									
										
										
										
											2024-11-29 16:38:49 +00:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2024-11-29 16:34:54 +00:00
										 |  |  |      * @return mixed Returns true on success, or an error message on failure. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-11-01 16:27:57 +00:00
										 |  |  |     public function editConfigFile($updatedConfig, $config_file) { | 
					
						
							| 
									
										
										
										
											2024-11-01 16:23:40 +00:00
										 |  |  |         // first we get a fresh config file contents as text
 | 
					
						
							|  |  |  |         $config_contents = file_get_contents($config_file); | 
					
						
							|  |  |  |         if (!$config_contents) { | 
					
						
							|  |  |  |             return "Failed to read the config file \"$config_file\".";
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // loop through the variables and updated them
 | 
					
						
							|  |  |  |         foreach ($updatedConfig as $key => $newValue) { | 
					
						
							|  |  |  |             // we look for 'option' => value
 | 
					
						
							|  |  |  |             // option is always in single quotes
 | 
					
						
							|  |  |  |             // value is without quotes, because it could be true/false
 | 
					
						
							| 
									
										
										
										
											2024-11-01 16:58:53 +00:00
										 |  |  |             $pattern = "/(['\"]{$key}['\"]\s*=>\s*)([^,]+),/"; | 
					
						
							| 
									
										
										
										
											2024-11-01 16:23:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 15:33:18 +00:00
										 |  |  |             // prepare the value, make booleans w/out single quotes
 | 
					
						
							|  |  |  |             if ($newValue === 'true') { | 
					
						
							| 
									
										
										
										
											2024-11-01 18:01:43 +00:00
										 |  |  |                 $replacementValue = 'true'; | 
					
						
							| 
									
										
										
										
											2024-11-02 15:33:18 +00:00
										 |  |  |             } elseif ($newValue === 'false') { | 
					
						
							| 
									
										
										
										
											2024-11-01 18:01:43 +00:00
										 |  |  |                 $replacementValue = 'false'; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $replacementValue = var_export($newValue, true); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // value replacing
 | 
					
						
							| 
									
										
										
										
											2024-11-01 16:23:40 +00:00
										 |  |  |             $config_contents = preg_replace($pattern, "$1{$replacementValue},", $config_contents); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // write the new config file
 | 
					
						
							|  |  |  |         if (!file_put_contents($config_file, $config_contents)) { | 
					
						
							|  |  |  |             return "Failed to write the config file \"$config_file\".";
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-28 09:59:13 +00:00
										 |  |  | } |