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	protected $error;
30
31	/**
32	 * Check if 'dbversion' table exists.
33	 *
34	 * @return boolean
35	 */
36	abstract protected function checkDbVersionTable();
37
38	/**
39	 * Check if connected database version matches with frontend version.
40	 *
41	 * @return bool
42	 */
43	public function checkDbVersion() {
44		if (!$this->checkDbVersionTable()) {
45			return false;
46		}
47
48		$version = DBfetch(DBselect('SELECT dv.mandatory FROM dbversion dv'));
49
50		if ($version['mandatory'] != ZABBIX_DB_VERSION) {
51			$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.',
52				$version['mandatory'], ZABBIX_DB_VERSION
53			));
54
55			return false;
56		}
57
58		return true;
59	}
60
61	/**
62	 * Check the integrity of the table "config".
63	 *
64	 * @return bool
65	 */
66	public function checkConfig() {
67		if (!DBfetch(DBselect('SELECT NULL FROM config c'))) {
68			$this->setError(_('Unable to select configuration.'));
69			return false;
70		}
71
72		return true;
73	}
74
75	/**
76	 * Create INSERT SQL query for MySQL, PostgreSQL and IBM DB2.
77	 * Creation example:
78	 *	INSERT INTO applications (name,hostid,templateid,applicationid)
79	 *	VALUES ('CPU','10113','13','868'),('Filesystems','10113','5','869'),('General','10113','21','870');
80	 *
81	 * @param string $table
82	 * @param array $fields
83	 * @param array $values
84	 *
85	 * @return string
86	 */
87	public function createInsertQuery($table, array $fields, array $values) {
88		$sql = 'INSERT INTO '.$table.' ('.implode(',', $fields).') VALUES ';
89
90		foreach ($values as $row) {
91			$sql .= '('.implode(',', array_values($row)).'),';
92		}
93
94		$sql = substr($sql, 0, -1);
95
96		return $sql;
97	}
98
99	/**
100	 * Set error string.
101	 *
102	 * @param string $error
103	 */
104	public function setError($error) {
105		$this->error = $error;
106	}
107
108	/**
109	 * Return error or null if no error occurred.
110	 *
111	 * @return mixed
112	 */
113	public function getError() {
114		return $this->error;
115	}
116
117	/**
118	 * Check database and table fields encoding.
119	 *
120	 * @return bool
121	 */
122	abstract public function checkEncoding();
123
124	/**
125	 * Set warning message.
126	 *
127	 * @param string $message
128	 */
129	public function setWarning($message) {
130		$this->warning = $message;
131	}
132
133	/**
134	 * Get warning message.
135	 *
136	 * @return mixed
137	 */
138	public function getWarning() {
139		return $this->warning;
140	}
141}
142