| 
									
										
										
										
											2024-06-28 17:05:32 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Database { | 
					
						
							|  |  |  |     private $pdo; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |     public function __construct($options) { | 
					
						
							|  |  |  |         // pdo needed
 | 
					
						
							|  |  |  |         if ( !extension_loaded('pdo') ) { | 
					
						
							| 
									
										
										
										
											2024-08-17 08:20:08 +00:00
										 |  |  |             $error = getError('PDO extension not loaded.'); | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // options check
 | 
					
						
							|  |  |  |         if (empty($options['type'])) { | 
					
						
							| 
									
										
										
										
											2024-08-17 08:20:08 +00:00
										 |  |  |             $error = getError('Database type is not set.'); | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // database type
 | 
					
						
							|  |  |  |         switch ($options['type']) { | 
					
						
							|  |  |  |             case 'sqlite': | 
					
						
							|  |  |  |                 $this->connectSqlite($options); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'mysql' || 'mariadb': | 
					
						
							|  |  |  |                 $this->connectMysql($options); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             default: | 
					
						
							| 
									
										
										
										
											2024-08-17 08:20:08 +00:00
										 |  |  |                 $error = getError("Database type \"{$options['type']}\" is not supported."); | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-07-04 19:14:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |     private function connectSqlite($options) { | 
					
						
							|  |  |  |         // pdo_sqlite extension is needed
 | 
					
						
							|  |  |  |         if (!extension_loaded('pdo_sqlite')) { | 
					
						
							| 
									
										
										
										
											2024-08-17 08:20:08 +00:00
										 |  |  |             $error = getError('PDO extension for SQLite not loaded.'); | 
					
						
							| 
									
										
										
										
											2024-06-28 17:05:32 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         // SQLite options
 | 
					
						
							|  |  |  |         if (empty($options['dbFile']) || !file_exists($options['dbFile'])) { | 
					
						
							| 
									
										
										
										
											2024-08-17 08:20:08 +00:00
										 |  |  |             $error = getError("SQLite database file \"{$dbFile}\" not found."); | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // connect to SQLite
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $this->pdo = new PDO("sqlite:" . $options['dbFile']); | 
					
						
							|  |  |  |             $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | 
					
						
							| 
									
										
										
										
											2024-09-11 13:21:05 +00:00
										 |  |  |             // enable foreign key constraints (not ON by default in SQLite3)
 | 
					
						
							|  |  |  |             $this->pdo->exec('PRAGMA foreign_keys = ON;'); | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         } catch (PDOException $e) { | 
					
						
							| 
									
										
										
										
											2024-08-17 08:20:08 +00:00
										 |  |  |             $error = getError('SQLite connection failed: ', $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function connectMysql($options) { | 
					
						
							|  |  |  |         // pdo_mysql extension is needed
 | 
					
						
							|  |  |  |         if (!extension_loaded('pdo_mysql')) { | 
					
						
							| 
									
										
										
										
											2024-08-17 08:20:08 +00:00
										 |  |  |             $error = getError('PDO extension for MySQL not loaded.'); | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // MySQL options
 | 
					
						
							|  |  |  |         if (empty($options['host']) || empty($options['dbname']) || empty($options['user'])) { | 
					
						
							| 
									
										
										
										
											2024-08-17 08:20:08 +00:00
										 |  |  |             $error = getError('MySQL connection data is missing.'); | 
					
						
							| 
									
										
										
										
											2024-07-04 19:14:12 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |         // Connect to MySQL
 | 
					
						
							| 
									
										
										
										
											2024-06-28 17:05:32 +00:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2024-08-10 18:42:44 +00:00
										 |  |  |             $dsn = "mysql:host={$options['host']};port={$options['port']};dbname={$options['dbname']};charset=utf8"; | 
					
						
							|  |  |  |             $this->pdo = new PDO($dsn, $options['user'], $options['password'] ?? ''); | 
					
						
							| 
									
										
										
										
											2024-06-28 17:05:32 +00:00
										 |  |  |             $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | 
					
						
							|  |  |  |         } catch (PDOException $e) { | 
					
						
							| 
									
										
										
										
											2024-08-17 08:20:08 +00:00
										 |  |  |             $error = getError('MySQL connection failed: ', $config['environment'], $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2024-06-28 17:05:32 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getConnection() { | 
					
						
							|  |  |  |         return $this->pdo; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ?>
 |