summaryrefslogtreecommitdiffstats
path: root/inc/property.inc.php
blob: c6f3e8ad03cc0929237ae81a70b503386404d02d (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
<?php

/**
 * Get or set simple key-value-pairs, backed by the database
 * to make them persistent.
 */
class Property
{
	private static $cache = false;
	
	/**
	 * Retrieve value from property store.
	 *
	 * @param string $key key to retrieve the value of
	 * @param mixed $default value to return if $key does not exist in the property store
	 * @return mixed the value attached to $key, or $default if $key does not exist
	 */
	private static function get($key, $default = false)
	{
		if (self::$cache === false) {
			$res = Database::simpleQuery("SELECT name, value FROM property");
			while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
				self::$cache[$row['name']] = $row['value'];
			}
		}
		if (!isset(self::$cache[$key])) return $default;
		return self::$cache[$key];
	}
	
	/**
	 * Set value in property store.
	 *
	 * @param string $key key of value to set
	 * @param type $value the value to store for $key
	 */
	private static function set($key, $value)
	{
		Database::exec("INSERT INTO property (name, value) VALUES (:key, :value)"
			. " ON DUPLICATE KEY UPDATE value = VALUES(value)", array(
				'key' => $key,
				'value' => $value
			));
		if (self::$cache !== false) {
			self::$cache[$key] = $value;
		}
	}
	
	public static function getServerIp()
	{
		return self::get('server-ip', 'none');
	}
	
	public static function setServerIp($value)
	{
		self::set('server-ip', $value);
	}
	
	public static function getIPxeIp()
	{
		return self::get('ipxe-ip', 'none');
	}
	
	public static function setIPxeIp($value)
	{
		self::set('ipxe-ip', $value);
	}
	
	public static function getIPxeTaskId()
	{
		return self::get('ipxe-task');
	}
	
	public static function setIPxeTaskId($value)
	{
		self::set('ipxe-task', $value);
	}

}