You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

database.inc.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /* Poweradmin, a friendly web-based admin tool for PowerDNS.
  3. * See <http://www.poweradmin.org> for more details.
  4. *
  5. * Copyright 2007-2009 Rejo Zenger <rejo@zenger.nl>
  6. * Copyright 2010-2017 Poweradmin Development Team
  7. * <http://www.poweradmin.org/credits.html>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * Database functions
  24. *
  25. * @package Poweradmin
  26. * @copyright 2007-2010 Rejo Zenger <rejo@zenger.nl>
  27. * @copyright 2010-2017 Poweradmin Development Team
  28. * @license http://opensource.org/licenses/GPL-3.0 GPL
  29. */
  30. if (class_exists('PDO', false)) {
  31. include_once 'PDOLayer.php';
  32. } else {
  33. die(error('You have to install PDO library!'));
  34. }
  35. /** Print database error message
  36. *
  37. * @param object $msg Database error object
  38. */
  39. function dbError($msg) {
  40. $debug = $msg->getDebugInfo();
  41. if (preg_match("/Unknown column 'zone_templ_id'/", $debug)) {
  42. $debug = ERR_DB_NO_DB_UPDATE;
  43. }
  44. echo " <div class=\"error\">Error: " . $debug . "</div>\n";
  45. include_once("footer.inc.php");
  46. die();
  47. }
  48. /** Connect to Database
  49. *
  50. * @return object $db Database object
  51. */
  52. function dbConnect() {
  53. // XXX: one day all globals will die, I promise
  54. global $db_type;
  55. global $db_user;
  56. global $db_pass;
  57. global $db_host;
  58. global $db_port;
  59. global $db_name;
  60. global $db_charset;
  61. global $db_file;
  62. global $db_debug;
  63. global $db_ssl_ca;
  64. global $sql_regexp;
  65. if (!(isset($db_type) && $db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'pgsql' || $db_type == 'sqlite' || $db_type == 'sqlite3' || $db_type == 'oci8')) {
  66. include_once("header.inc.php");
  67. error(ERR_DB_NO_DB_TYPE);
  68. include_once("footer.inc.php");
  69. exit;
  70. }
  71. if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_user) && $db_user != "")) {
  72. include_once("header.inc.php");
  73. error(ERR_DB_NO_DB_USER);
  74. include_once("footer.inc.php");
  75. exit;
  76. }
  77. if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_pass) && $db_pass != '')) {
  78. include_once("header.inc.php");
  79. error(ERR_DB_NO_DB_PASS);
  80. include_once("footer.inc.php");
  81. exit;
  82. }
  83. if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_host) && $db_host != '')) {
  84. include_once("header.inc.php");
  85. error(ERR_DB_NO_DB_HOST);
  86. include_once("footer.inc.php");
  87. exit;
  88. }
  89. if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_name) && $db_name != '')) {
  90. include_once("header.inc.php");
  91. error(ERR_DB_NO_DB_NAME);
  92. include_once("footer.inc.php");
  93. exit;
  94. }
  95. if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_port)) || $db_port == '') {
  96. if ($db_type == "mysql" || $db_type == "mysqli") {
  97. $db_port = 3306;
  98. } else if ($db_type == 'oci8') {
  99. $db_port = 1521;
  100. } else {
  101. $db_port = 5432;
  102. }
  103. }
  104. if (($db_type == 'sqlite' || $db_type == 'sqlite3') && (!(isset($db_file) && $db_file != ''))) {
  105. include_once("header.inc.php");
  106. error(ERR_DB_NO_DB_FILE);
  107. include_once("footer.inc.php");
  108. exit;
  109. }
  110. if ($db_type == 'sqlite' || $db_type == 'sqlite3') {
  111. $dsn = "$db_type:$db_file";
  112. } else {
  113. if ($db_type == 'oci8') {
  114. $db_name = '?service=' . $db_name;
  115. }
  116. $dsn = "$db_type:host=$db_host;port=$db_port;dbname=$db_name";
  117. }
  118. if ($db_type === 'mysql' && $db_charset === 'utf8') {
  119. $dsn .= ';charset=utf8';
  120. }
  121. $db = new PDOLayer($dsn, $db_user, $db_pass);
  122. // http://stackoverflow.com/a/4361485/567193
  123. if ($db_type === 'mysql' && $db_charset === 'utf8' && version_compare(phpversion(), '5.3.6', '<')) {
  124. $db->exec('set names utf8');
  125. }
  126. if (isset($db_debug) && $db_debug) {
  127. $db->setOption('debug', 1);
  128. }
  129. /* erase info */
  130. $dsn = '';
  131. // Add support for regular expressions in both MySQL and PostgreSQL
  132. if ($db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'sqlite' || $db_type == 'sqlite3') {
  133. $sql_regexp = "REGEXP";
  134. } elseif ($db_type == "oci8") {
  135. # TODO: what is regexp syntax in Oracle?
  136. $sql_regexp = "";
  137. } elseif ($db_type == "pgsql") {
  138. $sql_regexp = "~";
  139. } else {
  140. include_once("header.inc.php");
  141. error(ERR_DB_NO_DB_TYPE);
  142. include_once("footer.inc.php");
  143. exit;
  144. }
  145. return $db;
  146. }