185 lines
5.6 KiB
PHP
185 lines
5.6 KiB
PHP
<?php
|
|
namespace Aiko\Database;
|
|
|
|
include __SITE_PATH . '/Aiko/Includes/db.config.php';
|
|
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
|
|
class Connections
|
|
{
|
|
private static $instance = null;
|
|
private static $instancePMA = NULL;
|
|
private static $instanceCartal=NULL;
|
|
private static $scada=NULL;
|
|
|
|
private function __construct()
|
|
{}
|
|
|
|
public static function getInstance(
|
|
$type,
|
|
$host='127.0.0.1',
|
|
$socket= '/var/lib/mysql/mysql.sock',
|
|
$user='root',
|
|
$password='')
|
|
{
|
|
$aHost=array('127.0.0.1','localhost');
|
|
if(in_array($host,$aHost))
|
|
{
|
|
$host = $host;
|
|
}else {
|
|
$host='1.1.1.1';
|
|
}
|
|
|
|
$db = $db = 'hcportal_local';
|
|
$socket = $socket;
|
|
$user = $user;
|
|
$pass = $password;
|
|
if($type!=='local'){
|
|
$config = getConfig($type);
|
|
$host = $config['host'];
|
|
$db = $config['db'];
|
|
$socket = $config['socket'];
|
|
$user = $config['user'];
|
|
$pass = $config['password'];
|
|
}
|
|
|
|
|
|
if (!self::$instance) {
|
|
try
|
|
{
|
|
switch ($config['dbms']) {
|
|
case 'mysql':
|
|
// self::$instance = new PDO("mysql:host=$host;dbname=$db;unix_socket=$socket;port=4867;", "$user","$pass");
|
|
self::$instance = new PDO("mysql:host=$host;dbname=$db;unix_socket=$socket;", "$user", "$pass");
|
|
break;
|
|
case 'oracle':
|
|
self::$instance = new PDO("oci:host=$host;dbname=$db;", "$user", "$pass");
|
|
break;
|
|
case 'pgsql':
|
|
self::$instance = new PDO("pgsql:host=$host;dbname=$db;", "$user", "$pass");
|
|
break;
|
|
case 'sqlite':
|
|
break;
|
|
self::$instance = new PDO("sqlite:$db;");
|
|
|
|
}
|
|
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
self::$instance->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
|
|
// self::$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
|
|
|
return self::$instance;
|
|
} catch (PDOException $e) {
|
|
self::showerror("Sorry, an error has occured. Please try your request \n" . $e->getMessage());
|
|
die();
|
|
}
|
|
} else {
|
|
return self::$instance;
|
|
}
|
|
|
|
}
|
|
|
|
public static function getInstancePMA() {
|
|
$config = getConfig('pma');
|
|
$host = $config['host'];
|
|
$db = $config['db'];
|
|
$socket = $config['socket'];
|
|
$user = $config['user'];
|
|
$pass = $config['password'];
|
|
if (!self::$instancePMA)
|
|
{
|
|
try
|
|
{
|
|
self::$instancePMA = new PDO("mysql:host=$host;dbname=$db;unix_socket=$socket;", "$user","$pass");
|
|
self::$instancePMA-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
self::$instancePMA->setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
|
|
return self::$instancePMA;
|
|
}catch (PDOException $e)
|
|
{
|
|
self::showerror("Sorry, an error has occured. Please try your request \n".$e->getMessage());
|
|
die();
|
|
}
|
|
}else
|
|
{
|
|
return self::$instancePMA;
|
|
}
|
|
|
|
}
|
|
|
|
public static function getInstanceCartal($type) {
|
|
$config = getConfig($type);
|
|
$host = $config['host'];
|
|
$db = $config['db'];
|
|
$socket = $config['socket'];
|
|
$user = $config['user'];
|
|
$pass = $config['password'];
|
|
if (!self::$instanceCartal)
|
|
{
|
|
try
|
|
{
|
|
self::$instanceCartal = new PDO("mysql:host=$host;dbname=$db;unix_socket=$socket;", "$user","$pass");
|
|
self::$instanceCartal-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
self::$instanceCartal->setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
|
|
return self::$instanceCartal;
|
|
}catch (PDOException $e)
|
|
{
|
|
self::showerror("Sorry, an error has occured. Please try your request \n".$e->getMessage());
|
|
die();
|
|
}
|
|
}else
|
|
{
|
|
return self::$instanceCartal;
|
|
}
|
|
|
|
}
|
|
|
|
public static function getInstanceSCADA($type) {
|
|
$config = getConfig($type);
|
|
$host = $config['host'];
|
|
$db = $config['db'];
|
|
$user = $config['user'];
|
|
$pass = $config['password'];
|
|
$port = $config['port'];
|
|
if (!self::$scada)
|
|
{
|
|
try
|
|
{
|
|
self::$scada = new PDO("pgsql:host=$host;port=$port;dbname=$db;", "$user", "$pass");
|
|
self::$scada-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
self::$scada->setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
|
|
return self::$scada;
|
|
}catch (PDOException $e)
|
|
{
|
|
self::showerror("Sorry, an error has occured. Please try your request \n".$e->getMessage());
|
|
die();
|
|
}
|
|
}else
|
|
{
|
|
return self::$instanceCartal;
|
|
}
|
|
|
|
}
|
|
|
|
public static function exceptionHandler($e)
|
|
{
|
|
set_exception_handler('exceptionHandler');
|
|
self::showerror("Sorry, the site under maintenance \n");
|
|
}
|
|
public static function showerror($m)
|
|
{
|
|
echo "<h2>Error</h2>";
|
|
echo nl2br(htmlspecialchars($m));
|
|
}
|
|
/**
|
|
*
|
|
*
|
|
* Like the constructor, we make __clone private
|
|
* so nobody can clone the instance
|
|
*
|
|
*/
|
|
private function __clone()
|
|
{}
|
|
|
|
}
|