summaryrefslogtreecommitdiffstats
path: root/modules-available/dozmod/pages/special.inc.php
blob: 5a532b5193725afc8b309cb7619bca805c4de6af (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
<?php

class SubPage
{


	public static function doPreprocess()
	{

	}

	public static function doRender()
	{
		$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);
	}

	public static function doAjax()
	{
		$action = Request::any('action');

		if ($action === 'getblockinfo') {
			self::ajaxGetBlockInfo();
		} elseif ($action === 'dmsd-status') {
			self::ajaxDmsdStatus();
		}
	}

	private static 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));
	}

	private static function ajaxDmsdStatus()
	{
		$ret = Download::asStringPost('http://127.0.0.1:9080/status/fileserver', false, 2, $code);
		$args = array();
		if ($code != 200) {
			$args['error'] = true;
		} else {
			$data = @json_decode($ret, true);
			if (is_array($data)) {
				$args['uploads'] = $data['activeUploads'];
				$args['downloads'] = $data['activeDownloads'];
			}
		}
		Header('Content-Type: application/json');
		echo json_encode($args);
	}

}