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 CControllerReportServices extends CController {
23
24	const YEAR_LEFT_SHIFT = 5;
25
26	private $service = null;
27
28	protected function init() {
29		$this->disableSIDValidation();
30	}
31
32	protected function checkInput() {
33		$fields = [
34			'serviceid' =>	'fatal|required|db services.serviceid',
35			'period' =>		'in daily,weekly,monthly,yearly',
36			'year' =>		'int32'
37		];
38
39		$ret = $this->validateInput($fields);
40
41		if (!$ret) {
42			$this->setResponse(new CControllerResponseFatal());
43		}
44
45		return $ret;
46	}
47
48	protected function checkPermissions() {
49		if ($this->getUserType() < USER_TYPE_ZABBIX_USER) {
50			return false;
51		}
52
53		$services = API::Service()->get([
54			'output' => ['serviceid', 'name', 'showsla', 'goodsla'],
55			'serviceids' => [$this->getInput('serviceid')]
56		]);
57
58		if (!$services) {
59			return false;
60		}
61
62		$this->service = $services[0];
63
64		return true;
65	}
66
67	protected function doAction() {
68		// default values
69		$data = [
70			'period' => $this->getInput('period', 'yearly'),
71			'service' => $this->service,
72			'year' => $this->getInput('year', date('Y')),
73			'YEAR_LEFT_SHIFT' => self::YEAR_LEFT_SHIFT
74		];
75
76		switch ($data['period']) {
77			case 'yearly':
78				$from = date('Y') - self::YEAR_LEFT_SHIFT;
79				$to = date('Y');
80
81				function get_time($year, $y) {
82					return mktime(0, 0, 0, 1, 1, $y);
83				}
84				break;
85
86			case 'monthly':
87				$from = 1;
88				$to = 12;
89
90				function get_time($year, $m) {
91					return mktime(0, 0, 0, $m, 1, $year);
92				}
93				break;
94
95			case 'weekly':
96				$from = 0;
97				$to = 52;
98
99				function get_time($year, $w) {
100					$time = mktime(0, 0, 0, 1, 1, $year);
101					$wd = date('w', $time);
102					$wd = $wd == 0 ? 6 : $wd - 1;
103					$beg =  $time - $wd * SEC_PER_DAY;
104
105					return strtotime("+$w week", $beg);
106				}
107				break;
108
109			case 'daily':
110				$from = 1;
111				$to = DAY_IN_YEAR + date('L', mktime(0, 0, 0, 1, 1, $data['year']));
112
113				function get_time($year, $d) {
114					return mktime(0, 0, 0, 1, $d, $year);
115				}
116				break;
117		}
118
119		$now = time();
120		$intervals = [];
121
122		for ($t = $from; $t <= $to; $t++) {
123			if (($start = get_time($data['year'], $t)) > $now) {
124				break;
125			}
126
127			if (($end = get_time($data['year'], $t + 1)) > $now) {
128				$end = $now;
129			}
130
131			$intervals[] = [
132				'from' => $start,
133				'to' => $end
134			];
135		}
136
137		$sla = API::Service()->getSla([
138			'serviceids' => [$this->service['serviceid']],
139			'intervals' => $intervals
140		]);
141		$data['sla'] = reset($sla);
142
143		CView::$has_web_layout_mode = true;
144
145		$response = new CControllerResponseData($data);
146		$response->setTitle(_('Service availability report'));
147		$this->setResponse($response);
148	}
149}
150