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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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-2014 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-2014 Poweradmin Development Team
  28. * @license http://opensource.org/licenses/GPL-3.0 GPL
  29. */
  30. global $db_layer;
  31. // if DB abstraction layer is not defined in configuration file then try to
  32. // auto-configure otherwise fail gracefully
  33. if (!isset($db_layer)) {
  34. // Use MDB2 by default because PDO support is quite experimental
  35. if (@include_once 'MDB2.php') {
  36. $db_layer = 'MDB2';
  37. } elseif (class_exists('PDO', false)) {
  38. $db_layer = 'PDO';
  39. include_once 'PDOLayer.php';
  40. } else {
  41. die(error('You have to install MDB2 or PDO library!'));
  42. }
  43. } else {
  44. if ($db_layer == 'MDB2') {
  45. (@include_once 'MDB2.php') or die(error('You have to install MDB2 library!'));
  46. }
  47. if ($db_layer == 'PDO') {
  48. if (class_exists('PDO', false)) {
  49. include_once 'PDOLayer.php';
  50. } else {
  51. die(error('You have to install PDO library!'));
  52. }
  53. }
  54. }
  55. /** Print database error message
  56. *
  57. * @param object $msg Database error object
  58. */
  59. function dbError($msg) {
  60. $debug = $msg->getDebugInfo();
  61. if (preg_match("/Unknown column 'zone_templ_id'/", $debug)) {
  62. $debug = ERR_DB_NO_DB_UPDATE;
  63. }
  64. echo " <div class=\"error\">Error: " . $debug . "</div>\n";
  65. include_once("footer.inc.php");
  66. die();
  67. }
  68. if (isset($db_layer) && $db_layer == 'MDB2') {
  69. @PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'dbError');
  70. }
  71. /** Connect to Database
  72. *
  73. * @return object $db Database object
  74. */
  75. function dbConnect() {
  76. // XXX: one day all globals will die, I promise
  77. global $db_type;
  78. global $db_user;
  79. global $db_pass;
  80. global $db_host;
  81. global $db_port;
  82. global $db_name;
  83. global $db_file;
  84. global $db_layer;
  85. global $db_debug;
  86. global $db_ssl_ca;
  87. global $sql_regexp;
  88. if (!(isset($db_type) && $db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'pgsql' || $db_type == 'sqlite' || $db_type == 'sqlite3' || $db_type == 'oci8')) {
  89. include_once("header.inc.php");
  90. error(ERR_DB_NO_DB_TYPE);
  91. include_once("footer.inc.php");
  92. exit;
  93. }
  94. if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_user) && $db_user != "")) {
  95. include_once("header.inc.php");
  96. error(ERR_DB_NO_DB_USER);
  97. include_once("footer.inc.php");
  98. exit;
  99. }
  100. if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_pass) && $db_pass != '')) {
  101. include_once("header.inc.php");
  102. error(ERR_DB_NO_DB_PASS);
  103. include_once("footer.inc.php");
  104. exit;
  105. }
  106. if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_host) && $db_host != '')) {
  107. include_once("header.inc.php");
  108. error(ERR_DB_NO_DB_HOST);
  109. include_once("footer.inc.php");
  110. exit;
  111. }
  112. if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_name) && $db_name != '')) {
  113. include_once("header.inc.php");
  114. error(ERR_DB_NO_DB_NAME);
  115. include_once("footer.inc.php");
  116. exit;
  117. }
  118. if ($db_type != 'sqlite' && $db_type != 'sqlite3' && !(isset($db_port)) || $db_port == '') {
  119. if ($db_type == "mysql" || $db_type == "mysqli") {
  120. $db_port = 3306;
  121. } else if ($db_type == 'oci8') {
  122. $db_port = 1521;
  123. } else {
  124. $db_port = 5432;
  125. }
  126. }
  127. if (($db_type == 'sqlite' || $db_type == 'sqlite3') && (!(isset($db_file) && $db_file != ''))) {
  128. include_once("header.inc.php");
  129. error(ERR_DB_NO_DB_FILE);
  130. include_once("footer.inc.php");
  131. exit;
  132. }
  133. if ($db_layer == 'MDB2') {
  134. if ($db_type == 'sqlite') {
  135. $dsn = "$db_type:///$db_file";
  136. } else if ($db_type == 'sqlite3') {
  137. $dsn = "pdoSqlite:///$db_file";
  138. } else {
  139. if ($db_type == 'oci8') {
  140. $db_name = '?service=' . $db_name;
  141. }
  142. $dsn = "$db_type://$db_user:$db_pass@$db_host:$db_port/$db_name";
  143. if (($db_type == 'mysqli') && (isset($db_ssl_ca))) {
  144. $dsn .= "?ca=$db_ssl_ca";
  145. }
  146. }
  147. }
  148. if ($db_layer == 'PDO') {
  149. if ($db_type == 'sqlite' || $db_type == 'sqlite3') {
  150. $dsn = "$db_type:$db_file";
  151. } else {
  152. if ($db_type == 'oci8') {
  153. $db_name = '?service=' . $db_name;
  154. }
  155. $dsn = "$db_type:host=$db_host;port=$db_port;dbname=$db_name";
  156. }
  157. }
  158. if ($db_layer == 'MDB2') {
  159. $options = array(
  160. 'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL,
  161. );
  162. if (($db_type == 'mysqli') && (isset($db_ssl_ca))) {
  163. $options['ssl'] = true;
  164. }
  165. $db = MDB2::connect($dsn, $options);
  166. }
  167. if ($db_layer == 'PDO') {
  168. $db = new PDOLayer($dsn, $db_user, $db_pass);
  169. }
  170. if (isset($db_debug) && $db_debug) {
  171. $db->setOption('debug', 1);
  172. }
  173. // FIXME - it's strange, but this doesn't work, perhaps bug in MDB2 library
  174. if (@PEAR::isError($db)) {
  175. // Error handling should be put.
  176. error(MYSQL_ERROR_FATAL, $db->getMessage());
  177. }
  178. // Do an ASSOC fetch. Gives us the ability to use ["id"] fields.
  179. if ($db_layer == 'MDB2') {
  180. $db->setFetchMode(MDB2_FETCHMODE_ASSOC);
  181. }
  182. /* erase info */
  183. $dsn = '';
  184. // Add support for regular expressions in both MySQL and PostgreSQL
  185. if ($db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'sqlite' || $db_type == 'sqlite3') {
  186. $sql_regexp = "REGEXP";
  187. } elseif ($db_type == "oci8") {
  188. # TODO: what is regexp syntax in Oracle?
  189. $sql_regexp = "";
  190. } elseif ($db_type == "pgsql") {
  191. $sql_regexp = "~";
  192. } else {
  193. include_once("header.inc.php");
  194. error(ERR_DB_NO_DB_TYPE);
  195. include_once("footer.inc.php");
  196. exit;
  197. }
  198. return $db;
  199. }