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
22class CScreenClock extends CScreenBase {
23
24	/**
25	 * Process screen.
26	 *
27	 * @return CDiv (screen inside container)
28	 */
29	public function get() {
30		$time = null;
31		$title = null;
32		$time_zone_string = null;
33		$time_zone_offset = null;
34		$error = null;
35
36		switch ($this->screenitem['style']) {
37			case TIME_TYPE_HOST:
38				$itemid = $this->screenitem['resourceid'];
39
40				if (!empty($this->hostid)) {
41					$new_itemid = get_same_item_for_host($itemid, $this->hostid);
42					$itemid = !empty($new_itemid) ? $new_itemid : '';
43				}
44
45				$items = API::Item()->get([
46					'output' => ['itemid', 'value_type'],
47					'selectHosts' => ['name'],
48					'itemids' => [$itemid]
49				]);
50
51				if ($items) {
52					$item = $items[0];
53					$title = $item['hosts'][0]['name'];
54					unset($items, $item['hosts']);
55
56					$last_value = Manager::History()->getLastValues([$item]);
57
58					if ($last_value) {
59						$last_value = $last_value[$item['itemid']][0];
60
61						try {
62							$now = new DateTime($last_value['value']);
63
64							$time_zone_string = _s('GMT%1$s', $now->format('P'));
65							$time_zone_offset = $now->format('Z');
66
67							$time = time() - ($last_value['clock'] - $now->getTimestamp());
68						}
69						catch (Exception $e) {
70							$error = _('No data');
71						}
72					}
73					else {
74						$error = _('No data');
75					}
76				}
77				else {
78					$error = _('No data');
79				}
80				break;
81
82			case TIME_TYPE_SERVER:
83				$title = _('Server');
84
85				$now = new DateTime();
86				$time = $now->getTimestamp();
87				$time_zone_string = _s('GMT%1$s', $now->format('P'));
88				$time_zone_offset = $now->format('Z');
89				break;
90
91			default:
92				$title = _('Local');
93				break;
94		}
95
96		if ($this->screenitem['width'] > $this->screenitem['height']) {
97			$this->screenitem['width'] = $this->screenitem['height'];
98		}
99
100		$clock = (new CClock())
101			->setWidth($this->screenitem['width'])
102			->setHeight($this->screenitem['height'])
103			->setTimeZoneString($time_zone_string);
104
105		if ($error !== null) {
106			$clock->setError($error);
107		}
108
109		if ($time !== null) {
110			$clock->setTime($time);
111		}
112
113		if ($time_zone_offset !== null) {
114			$clock->setTimeZoneOffset($time_zone_offset);
115		}
116
117		if ($error === null) {
118			if (!defined('ZBX_CLOCK')) {
119				define('ZBX_CLOCK', 1);
120				insert_js(file_get_contents($clock->getScriptFile()));
121			}
122			zbx_add_post_js($clock->getScriptRun());
123		}
124
125		$item = [];
126		$item[] = $clock->getTimeDiv()
127			->addClass(ZBX_STYLE_TIME_ZONE);
128		$item[] = $clock;
129		$item[] = (new CDiv($title))
130			->addClass(ZBX_STYLE_LOCAL_CLOCK)
131			->addClass(ZBX_STYLE_GREY);
132
133		return $this->getOutput($item);
134	}
135}
136