summaryrefslogtreecommitdiff
path: root/src/lib/DB.class.php
blob: 9f141615a93998763990f29f986a49575238f686 (plain)
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
<?php

require_once('Auth.class.php');
require_once('Login.class.php');

class DB {
	public static function get($table, $unit, $key) {
		switch ($table) {
		case 'conf':
		case 'plugins':
			return self::admin_get($unit, $key);
			break;
		case 'users':
			return self::user_get($unit, $key);
			break;
		default:
			return false;
		}
	}
	public static function set($table, $unit, $key, $value) {
		switch ($table) {
		case 'conf':
		case 'plugins':
			return self::admin_get($unit, $key, $value);
			break;
		case 'users':
			return self::user_set($unit, $key, $value);
			break;
		default:
			return false;
		}
	}
	
	private static function user_get($uid, $key) {
		$user = Auth::getObj($uid);
		$logged_in_user = Auth::getObj(Login::isLoggedIn());
		
		$post_key = $key."[$uid]";
		@$value = $_POST[$post_key];
		$editable = $user->canEdit();
		
		switch ($key) {
		case 'auth_uid':
			$value = $user->getUID();
			$editable = false;
			break;
		case 'auth_name':
			$value = $user->getName();
			break;
		case 'auth_user':
			$editable = $editable && $logged_in_user->isAdmin();
			$value = $user->isUser();
			break;
		case 'auth_admin':
			$editable = $editable && $logged_in_user->isAdmin();
			$value = $user->isAdmin();
			break;
		case 'auth_delete':
			$editable = $editable && $logged_in_user->isAdmin();
			$value = false;
			break;
		default:
			$value = $user->getConf($key);
			if ($value===false) $value='';
			break;
		}
		
		return array('value'=>$value,
		             'post_key'=>$post_key,
		             'editable'=>$editable);
	}
	private static function user_set($uid, $key, $value) {
		$user = Auth::getObj($uid);
		
		switch ($key) {
		case 'auth_uid':
			return false;
			break;
		case 'auth_name':
			return $user->setName($value);
			break;
		case 'auth_user':
			return $user->setUser($value=='true');
			break;
		case 'auth_admin':
			return $user->setAdmin($value=='true');
			break;
		case 'auth_delete':
			if ($value=='true') return $user->delete();
		default: 
			return $user->setConf($key, $value);
			break;
		}
	}
	
	private static function admin_get($plugin, $key) {
		global $mm; $db = $mm->database();
		$user = Auth::getObj(Login::isLoggedIn());
		if ($user->isAdmin()) {
			$editable = true;
			switch ($plugin) {
			case 'system':
				$value = $db->getSysConf($key);
				break;
			default:
				$value =  $db->getPluginConf($plugin, $key);
				break;
			}
		} else {
			$editable = false;
			$value = false;
		}
		
		return array('value'=>$value,
		             'post_key'=>'to be implemented',// FIXME
		             'editable'=>$editable);
	}
	private static function admin_set($plugin, $key, $value) {
		global $mm; $db = $mm->database();
		$user = Auth::getObj(Login::isLoggedIn());
		if (!$user->isAdmin()) {
			return false;
		}
		switch ($plugin) {
		case 'system':
			return $db->setSysConf($key, $value);
		default:
			return $db->setPluginConf($plugin, $key, $value);
		}
	}
}