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 * A class for Zabbix re-branding.
24 */
25class CBrandHelper {
26
27	const BRAND_CONFIG_FILE_PATH = '/../../../local/conf/brand.conf.php';
28
29	/**
30	 * Brand configuration array.
31	 *
32	 * @var array
33	 */
34	private static $config = [];
35
36	/**
37	 * Lazy configuration loading.
38	 */
39	private static function loadConfig() {
40		if (!self::$config) {
41			$config_file_path = realpath(dirname(__FILE__).self::BRAND_CONFIG_FILE_PATH);
42
43			if (file_exists($config_file_path)) {
44				self::$config = include $config_file_path;
45				if (is_array(self::$config)) {
46					self::$config['IS_REBRANDED'] = true;
47				}
48				else {
49					self::$config = [];
50				}
51			}
52		}
53	}
54
55	/**
56	 * Get value by key from configuration (load configuration if need).
57	 *
58	 * @param string $key      configuration key
59	 * @param mixed  $default  default value
60	 *
61	 * @return mixed
62	 */
63	private static function getValue($key, $default = false) {
64		self::loadConfig();
65
66		return (array_key_exists($key, self::$config) ? self::$config[$key] : $default);
67	}
68
69	/**
70	 * Is branding active ?
71	 *
72	 * @return boolean
73	 */
74	public static function isRebranded() {
75		return self::getValue('IS_REBRANDED');
76	}
77
78	/**
79	 * Get help URL.
80	 *
81	 * @return string
82	 */
83	public static function getHelpUrl() {
84		return self::getValue('BRAND_HELP_URL', 'https://www.zabbix.com/documentation/'.
85			(preg_match('/^\d+\.\d+/', ZABBIX_VERSION, $version) ? $version[0].'/' : '')
86		);
87	}
88
89	/**
90	 * Get logo of the specified type.
91	 *
92	 * @return string
93	 */
94	public static function getLogo(int $type): ?string {
95		switch ($type) {
96			case LOGO_TYPE_NORMAL:
97				return self::getValue('BRAND_LOGO', null);
98
99			case LOGO_TYPE_SIDEBAR:
100				return self::getValue('BRAND_LOGO_SIDEBAR', null);
101
102			case LOGO_TYPE_SIDEBAR_COMPACT:
103				return self::getValue('BRAND_LOGO_SIDEBAR_COMPACT', null);
104		}
105
106		return null;
107	}
108
109	/**
110	 * Get footer content.
111	 *
112	 * @param boolean $with_version
113	 *
114	 * @return array
115	 */
116	public static function getFooterContent($with_version) {
117		$footer = self::getValue(
118			'BRAND_FOOTER',
119			[
120				$with_version ? 'Zabbix '.ZABBIX_VERSION.'. ' : null,
121				'&copy; '.ZABBIX_COPYRIGHT_FROM.'&ndash;'.ZABBIX_COPYRIGHT_TO.', ',
122				(new CLink('Zabbix SIA', 'https://www.zabbix.com/'))
123					->addClass(ZBX_STYLE_GREY)
124					->addClass(ZBX_STYLE_LINK_ALT)
125					->setTarget('_blank')
126			]
127		);
128
129		if (!is_array($footer)) {
130			$footer = [$footer];
131		}
132
133		return $footer;
134	}
135}
136