DB_Helper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * DB helper file.
  4. *
  5. * Provides a helper to perform database operations.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package IV
  10. */
  11. require_once(__DIR__ . "/Helper.php");
  12. /**
  13. * Database helper.
  14. *
  15. * Contains database utilities to connect.
  16. *
  17. * @category Helper.
  18. */
  19. final class DB extends Helper{
  20. private static function generate_dsn($db_configuration){
  21. $type = $db_configuration["type"];
  22. $host = $db_configuration["host"];
  23. $port = $db_configuration["port"];
  24. $name = $db_configuration["name"];
  25. Log::debug("Database type: $type");
  26. Log::debug("Database host: $host");
  27. Log::debug("Database port: $port");
  28. Log::debug("Database name: $name");
  29. $dsn = "";
  30. switch ($type){
  31. case "mysql":
  32. $dsn = "mysql:";
  33. if (filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) == false){
  34. Log::fatal(
  35. "MySQL driver specified, but hostname is invalid: $host",
  36. EXCEPTION_CODE::PDO_MYSQL_HOSTNAME
  37. );
  38. throw new UnexpectedValueException(
  39. "MySQL driver specified, but hostname is invalid: $host",
  40. EXCEPTION_CODE::PDO_MYSQL_HOSTNAME
  41. );
  42. }
  43. else{
  44. $dsn .= "host=$host;";
  45. }
  46. if (
  47. strlen($name) > 64 ||
  48. strpos($name, "\\") !== false ||
  49. strpos($name, "/") !== false ||
  50. strpos($name, ".") !== false
  51. ){
  52. Log::fatal(
  53. "MySQL driver specified, but database name is invalid: $name",
  54. EXCEPTION_CODE::PDO_MYSQL_DBNAME
  55. );
  56. throw new UnexpectedValueException(
  57. "MySQL driver specified, but database name is invalid: $name",
  58. EXCEPTION_CODE::PDO_MYSQL_DBNAME
  59. );
  60. }
  61. else{
  62. $dsn .= "dbname=$name;";
  63. }
  64. if (intval($port) != 0){
  65. $port = intval($port);
  66. if ($port < 1 || $port > 65534){
  67. Log::fatal(
  68. "MySQL driver specified, but port number name is invalid: $port",
  69. EXCEPTION_CODE::PDO_MYSQL_PORT
  70. );
  71. throw new UnexpectedValueException(
  72. "MySQL driver specified, but port number name is invalid: $port",
  73. EXCEPTION_CODE::PDO_MYSQL_PORT
  74. );
  75. }
  76. else{
  77. $dsn .= "port=$port;";
  78. }
  79. }
  80. if ($host != null && strlen($host) > 0){
  81. $dsn .= "host=$host;";
  82. }
  83. break;
  84. default:
  85. Log::fatal(
  86. "Unsupported batabase driver '$type'.",
  87. EXCEPTION_CODE::PDO_UNSUPPORTED
  88. );
  89. throw new UnexpectedValueException(
  90. "Unsupported batabase driver '$type'.",
  91. EXCEPTION_CODE::PDO_UNSUPPORTED
  92. );
  93. }
  94. $dsn .= "charset=UTF8;";
  95. Log::debug("Database dsn: $dsn");
  96. return $dsn;
  97. }
  98. /**
  99. * Creates a database connection using the data in the config files.
  100. *
  101. * @return resource Connection to the database.
  102. */
  103. public static function start($db_configuration){
  104. //ini_set('mssql.charset', 'UTF8');
  105. $dsn = DB::generate_dsn($db_configuration);
  106. $user = $db_configuration["user"];
  107. $pass = $db_configuration["pass"];
  108. try{
  109. $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
  110. Log::info("Successfully connected with database");
  111. //$db->exec('SET NAMES utf8');
  112. //$db->exec('SET CHARACTER SET utf8');
  113. return $db;
  114. }
  115. catch(Exception $e){
  116. Log::fatal("Connection failed" . $e->getMessage());
  117. }
  118. }
  119. }