1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22/** 23 * Abstract database backend class. 24 */ 25abstract class DbBackend { 26 27 protected $warning; 28 29 /** 30 * Error message. 31 * 32 * @var string 33 */ 34 protected $error; 35 36 /** 37 * TLS encryption on/off. 38 * 39 * @var bool 40 */ 41 protected $tls_encryption = false; 42 43 /** 44 * Path to TLS key file. 45 * 46 * @var string 47 */ 48 protected $tls_key_file = ''; 49 50 /** 51 * Path to TLS cert file. 52 * 53 * @var string 54 */ 55 protected $tls_cert_file = ''; 56 57 /** 58 * Path to TLS ca file. 59 * 60 * @var string 61 */ 62 protected $tls_ca_file = ''; 63 64 /** 65 * True - need host verification.. 66 * 67 * @var bool 68 */ 69 protected $tls_verify_host = true; 70 71 /** 72 * Connection required cipher pattern. 73 * 74 * @var string 75 */ 76 protected $tls_cipher_list = ''; 77 78 /** 79 * Set TLS specific options for db connection. 80 * 81 * @param string $key_file Path to TLS key file. 82 * @param string $cert_file Path to TLS cert file. 83 * @param string $ca_file Path to TLS ca file. 84 * @param bool $verify_host True - need host verification. 85 * @param string $cipher_list Connection required cipher pattern. 86 */ 87 public function setConnectionSecurity($key_file, $cert_file, $ca_file, $verify_host, $cipher_list) { 88 $this->tls_encryption = true; 89 $this->tls_key_file = $key_file; 90 $this->tls_cert_file = $cert_file; 91 $this->tls_ca_file = $ca_file; 92 $this->tls_verify_host = $verify_host; 93 $this->tls_cipher_list = $cipher_list; 94 } 95 96 /** 97 * Check if 'dbversion' table exists. 98 * 99 * @return boolean 100 */ 101 abstract protected function checkDbVersionTable(); 102 103 /** 104 * Create connection to database server. 105 * 106 * @param string $host Host name. 107 * @param string $port Port. 108 * @param string $user User name. 109 * @param string $password Password. 110 * @param string $dbname Database name. 111 * @param string $schema DB schema. 112 * 113 * @return resource|null 114 */ 115 abstract public function connect($host, $port, $user, $password, $dbname, $schema); 116 117 /** 118 * Check if connected database version matches with frontend version. 119 * 120 * @return bool 121 */ 122 public function checkDbVersion() { 123 if (!$this->checkDbVersionTable()) { 124 return false; 125 } 126 127 $version = DBfetch(DBselect('SELECT dv.mandatory FROM dbversion dv')); 128 129 if ($version['mandatory'] != ZABBIX_DB_VERSION) { 130 $this->setError(_s('The Zabbix database version does not match current requirements. Your database version: %1$s. Required version: %2$s. Please contact your system administrator.', 131 $version['mandatory'], ZABBIX_DB_VERSION 132 )); 133 134 return false; 135 } 136 137 return true; 138 } 139 140 /** 141 * Check the integrity of the table "config". 142 * 143 * @return bool 144 */ 145 public function checkConfig() { 146 if (!DBfetch(DBselect('SELECT NULL FROM config c'))) { 147 $this->setError(_('Unable to select configuration.')); 148 return false; 149 } 150 151 return true; 152 } 153 154 /** 155 * Create INSERT SQL query for MySQL, PostgreSQL. 156 * Creation example: 157 * INSERT INTO items (itemid,name,key_,type) 158 * VALUES ('10201','agent.hostname','agent.hostname',1),('10202','agent.ping','agent.ping',1); 159 * 160 * @param string $table 161 * @param array $fields 162 * @param array $values 163 * 164 * @return string 165 */ 166 public function createInsertQuery($table, array $fields, array $values) { 167 $sql = 'INSERT INTO '.$table.' ('.implode(',', $fields).') VALUES '; 168 169 foreach ($values as $row) { 170 $sql .= '('.implode(',', array_values($row)).'),'; 171 } 172 173 $sql = substr($sql, 0, -1); 174 175 return $sql; 176 } 177 178 /** 179 * Set error string. 180 * 181 * @param string $error 182 */ 183 public function setError($error) { 184 $this->error = $error; 185 } 186 187 /** 188 * Return error or null if no error occurred. 189 * 190 * @return mixed 191 */ 192 public function getError() { 193 return $this->error; 194 } 195 196 /** 197 * Check database and table fields encoding. 198 * 199 * @return bool 200 */ 201 abstract public function checkEncoding(); 202 203 /** 204 * Check if database is using IEEE754 compatible double precision columns. 205 * 206 * @return bool 207 */ 208 abstract public function isDoubleIEEE754(); 209 210 /** 211 * Set warning message. 212 * 213 * @param string $message 214 */ 215 public function setWarning($message) { 216 $this->warning = $message; 217 } 218 219 /** 220 * Get warning message. 221 * 222 * @return mixed 223 */ 224 public function getWarning() { 225 return $this->warning; 226 } 227} 228