| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // connect to database
 | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  | function connectDB($config) { | 
					
						
							|  |  |  |     // sqlite database file
 | 
					
						
							|  |  |  |     if ($config['db_type'] === 'sqlite') { | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |             $dbFile = $config['sqlite']['sqlite_file'] ?? null; | 
					
						
							| 
									
										
										
										
											2024-08-17 08:20:08 +00:00
										 |  |  |             if (!$dbFile || !file_exists($dbFile)) { | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |                 throw new Exception(getError("Database file \"{$dbFile}\"not found.")); | 
					
						
							| 
									
										
										
										
											2024-08-17 07:01:48 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |             $db = new Database([ | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |                 'type'		=> $config['db_type'], | 
					
						
							| 
									
										
										
										
											2024-08-17 07:01:48 +00:00
										 |  |  |                 'dbFile'	=> $dbFile, | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |             $pdo = $db->getConnection(); | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |             Feedback::flash('ERROR', 'DEFAULT', getError('Error connecting to DB.', $e->getMessage())); | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |         return $db; | 
					
						
							| 
									
										
										
										
											2024-08-10 18:57:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |     // mysql/mariadb database
 | 
					
						
							|  |  |  |     } elseif ($config['db_type'] === 'mysql' || $config['db_type'] === 'mariadb') { | 
					
						
							|  |  |  |         $db = new Database([ | 
					
						
							|  |  |  |             'type'		=> $config['db_type'], | 
					
						
							|  |  |  |             'host'		=> $config['sql']['sql_host'] ?? 'localhost', | 
					
						
							|  |  |  |             'port'		=> $config['sql']['sql_port'] ?? '3306', | 
					
						
							|  |  |  |             'dbname'		=> $config['sql']['sql_database'], | 
					
						
							|  |  |  |             'user'		=> $config['sql']['sql_username'], | 
					
						
							|  |  |  |             'password'		=> $config['sql']['sql_password'], | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $pdo = $db->getConnection(); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             Feedback::flash('ERROR', 'DEFAULT', getError('Error connecting to DB.', $e->getMessage())); | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |         return $db; | 
					
						
							| 
									
										
										
										
											2024-08-10 18:57:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |     // unknown database
 | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         Feedback::flash('ERROR', 'DEFAULT', getError("Error: unknown database type \"{$config['db_type']}\"")); | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-08-10 18:57:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // connect to Jilo database
 | 
					
						
							|  |  |  | function connectJiloDB($config, $dbFile = '', $platformId = '') { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |         if (!$dbFile || !file_exists($dbFile)) { | 
					
						
							|  |  |  |             throw new Exception(getError("Invalid platform ID \"{$platformId}\", database file \"{$dbFile}\" not found.")); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $db = new Database([ | 
					
						
							|  |  |  |             'type'		=> 'sqlite', | 
					
						
							|  |  |  |             'dbFile'	=> $dbFile, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         return ['db' => $db, 'error' => null]; | 
					
						
							|  |  |  |     } catch (Exception $e) { | 
					
						
							|  |  |  |         return ['db' => null, 'error' => getError('Error connecting to DB.', $e->getMessage())]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |