summaryrefslogtreecommitdiffstats
path: root/modules/minilinux.inc.php
blob: 6e68a34eba6adcd969372a0f02d10b060773bc9c (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
<?php

class Page_MiniLinux extends Page
{

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

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

	protected function doRender()
	{
		$files = array();
		mkdir(CONFIG_HTTP_DIR . "/default", 0755, true);
		$this->checkFile($files, 'kernel');
		$this->checkFile($files, 'initramfs-stage31');
		$this->checkFile($files, 'stage32.sqfs');
		$this->checkFile($files, 'vmware.sqfs');
		$this->checkFile($files, 'nvidia_libs.sqfs');
		Render::addTemplate('page-minilinux', array(
			'files' => $files,
			'token' => Session::get('token')
		));
	}

	private function checkFile(&$files, $name)
	{
		static $someId = 0;
		$remote = CONFIG_REMOTE_ML . "/${name}.md5";
		$localTarget = CONFIG_HTTP_DIR . "/default/${name}";
		$local = "${localTarget}.md5";
		$localLock = "${localTarget}.lck";

		// Maybe already in progress?
		if (file_exists($localLock)) {
			$data = explode(' ', file_get_contents($localLock));
			if (count($data) == 2) {
				$pid = (int)$data[0];
				if (posix_kill($pid, 0)) {
					$files[] = array(
						'file'     => $name,
						'id'       => 'id' . $someId++,
						'pid'      => $pid,
						'progress' => $data[1]
					);
					return true;
				} else {
					unlink($localLock);
				}
			 } else {
				unlink($localLock);
			 }
		}

		// Not in progress, normal display
		if (!file_exists($local) || filemtime($local) + 300 < time()) {
			if (file_exists($localTarget)) {
				$existingMd5 = md5_file($localTarget);
			} else {
				$existingMd5 = '<missing>';
			}
			if (file_put_contents($local, $existingMd5) === false) {
				@unlink($local);
				Message::addWarning('error-write', $local);
			}
		} else {
			$existingMd5 = file_get_contents($local);
		}
		$existingMd5 = strtolower(preg_replace('/[^0-9a-f]/is', '', $existingMd5));
		$remoteMd5 = Util::download($remote, 3, $code);
		$remoteMd5 = strtolower(preg_replace('/[^0-9a-f]/is', '', $remoteMd5));
		if ($code != 200) {
			Message::addError('remote-timeout', $remote, $code);
			return false;
		}
		if ($existingMd5 === $remoteMd5) {
			// Up to date
			$files[] = array(
				'file'     => $name,
				'id'       => 'id' . $someId++,
			);
			return true;
		}
		// New version on server
		$files[] = array(
			'file'        => $name,
			'id'          => 'id' . $someId++,
			'update'      => true
		);
		return true;
	}
}