summaryrefslogtreecommitdiffstats
path: root/modules-available/dozmod/page.inc.php
blob: cdb347ab4602299e2c6d3f8d03ea7b2d997db73b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

class Page_DozMod extends Page
{
	/** @var bool true if we have a proper subpage */
	private $haveSubPage = false;

	private $validSections = ['expiredimages', 'mailconfig', 'templates', 'runtimeconfig', 'users', 'actionlog', 'networkshares', 'ldapfilters'];

	private $section;

	private function setupSubPage()
	{
		if ($this->haveSubPage !== false)
			return;
		/* different pages for different sections */
		$this->section = Request::any('section', false, 'string');
		if ($this->section === 'blockstats') // HACK HACK
			return;
		if ($this->section === false) {
			foreach ($this->validSections as $this->section) {
				if (User::hasPermission($this->section . '.*'))
					break;
			}
		} elseif (!in_array($this->section, $this->validSections)) {
			Util::traceError('Invalid section: ' . $this->section);
		}
		// Check permissions
		User::assertPermission($this->section . '.*');
		$include = 'modules/' . Page::getModule()->getIdentifier() . '/pages/' . $this->section . '.inc.php';
		if (!file_exists($include))
			return;

		require_once $include;
		$this->haveSubPage = true;
	}

	protected function doPreprocess()
	{
		User::load();

		if (!User::isLoggedIn()) {
			Message::addError('main.no-permission');
			Util::redirect('?do=Main');
		}

		$this->setupSubPage();
		if ($this->haveSubPage !== false) {
			SubPage::doPreprocess();
		}
		// Catch unhandled POST redirect
		if (Request::isPost()) {
			Util::redirect('?do=dozmod&section=' . $this->section);
		}

		/* Leave this here for translation module
		Dictionary::translate('submenu_expiredimages', true);
		Dictionary::translate('submenu_mailconfig', true);
		Dictionary::translate('submenu_templates', true);
		Dictionary::translate('submenu_runtimeconfig', true);
		Dictionary::translate('submenu_users', true);
		Dictionary::translate('submenu_actionlog', true);
		Dictionary::translate('submenu_networkshares', true);
		Dictionary::translate('submenu_ldapfilters', true);
		*/

		/* add sub-menus */
		foreach ($this->validSections as $section) {
			if (User::hasPermission($section . '.*')) {
				Dashboard::addSubmenu('?do=dozmod&section=' . $section, Dictionary::translate('submenu_' . $section, true));
			}
		}
	}

	protected function doRender()
	{
		/* different pages for different sections */
		if ($this->haveSubPage !== false) {
			SubPage::doRender();
			return;
		}

		if ($this->section === 'blockstats') {
			$this->showBlockStats();
		}

	}

	private function showBlockStats()
	{
		$res = Database::simpleQuery("SELECT blocksha1, blocksize, Count(*) AS blockcount FROM sat.imageblock"
			. " GROUP BY blocksha1, blocksize HAVING blockcount > 1 ORDER BY blockcount DESC, blocksha1 ASC");
		$data = array('hashes' => array());
		$spaceWasted = 0;
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$row['hash_hex'] = bin2hex($row['blocksha1']);
			$row['blocksize_s'] = Util::readableFileSize($row['blocksize']);
			$data['hashes'][] = $row;
			$spaceWasted += $row['blocksize'] * ($row['blockcount'] - 1);
		}
		$data['spacewasted'] = Util::readableFileSize($spaceWasted);
		Render::addTemplate('blockstats', $data);
	}

	protected function doAjax()
	{
		User::load();
		$this->setupSubPage();

		if ($this->haveSubPage !== false) {
			SubPage::doAjax();
			return;
		}

		$action = Request::post('action');

		if ($action === 'getblockinfo') {
			$this->ajaxGetBlockInfo();
		}
	}

	private function ajaxGetBlockInfo()
	{
		$hash = Request::any('hash', false, 'string');
		$size = Request::any('size', false, 'string');
		if ($hash === false || $size === false) {
			die('Missing parameter');
		}
		if (!is_numeric($size) || strlen($hash) !== 40 || !preg_match('/^[a-f0-9]+$/i', $hash)) {
			die('Malformed parameter');
		}
		$res = Database::simpleQuery("SELECT i.displayname, v.createtime, v.filesize, Count(*) AS blockcount FROM sat.imageblock ib"
			. " INNER JOIN sat.imageversion v USING (imageversionid)"
			. " INNER JOIN sat.imagebase i USING (imagebaseid)"
			. " WHERE ib.blocksha1 = :hash AND ib.blocksize = :size"
			. " GROUP BY ib.imageversionid"
			. " ORDER BY i.displayname ASC, v.createtime ASC",
			array('hash' => hex2bin($hash), 'size' => $size), true);
		if ($res === false) {
			die('Database error: ' . Database::lastError());
		}
		$data = array('rows' => array());
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$row['createtime_s'] = date('d.m.Y H:i', $row['createtime']);
			$row['filesize_s'] = Util::readableFileSize($row['filesize']);
			$data['rows'][] = $row;
		}
		die(Render::parse('blockstats-details', $data));
	}

}