summaryrefslogtreecommitdiffstats
path: root/management-interface/api/models/Satellites.php
blob: 456d74b02e13b83ca96b0b5ac5cbfbae603546a9 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
 *	Renders the satellite tab
*/
class Satellites {
	
	public function __construct() {
		
	}

	public function tabSatellites($f3) {
		$this->action($f3);

		// load items for table
		if (isset($f3->get('GET')['order'])) {
			$order = $f3->get('GET')['order'];
			if ($order != 'organization' && $order != 'address' && $order != 'name' && $order != 'prefix') {
				$order = 'prefix';
			}
		} else {
			$order = 'prefix';
		}
		
		if (isset($f3->get('GET')['di'])) {
			$di = $f3->get('GET')['di'];
			$di = ($di === 'asc')? 'ASC':'DESC';
		} else {
			$di = 'ASC';
		}
		
		$f3->set('order', $order);
		$f3->set('di', $di);
		$f3->set('result', $f3->get('DB')->exec('SELECT organization, address, name, prefix, publickey FROM satellite ORDER BY '.$order.' '.$di));

        if (isset( $f3->get('GET')['prefix'] )) {
			$f3->set('prefix', $f3->get('GET')['prefix']);
		} else {
			$f3->set('prefix', '');
		}
	  // now render the view
	  echo Template::instance()->render('views/satellites.htm');
	}
	
	public function action($f3) {
	  if ($f3->get('action') === 'done') return;
	  if (isset( $f3->get('REQUEST')['action'] ) && $f3->get('REQUEST')['action'] === 'new') {
	    // we want to add a new entry
	    $f3->set('action', 'new');

	    // set the already entered values if possible
	    if (isset($f3->get('POST')['organization'])) {
	       $organization = htmlspecialchars($f3->get('POST')['organization']);
	    } else {
	       $organization = '';
	    }

	    if (isset($f3->get('POST')['address'])) {
	       $address = htmlspecialchars($f3->get('POST')['address']);
	    } else {
	       $address = '';
	    }

	    if (isset($f3->get('POST')['name'])) {
	       $name = htmlspecialchars($f3->get('POST')['name']);
	    } else {
	       $name= '';
	    }

	    if (isset($f3->get('POST')['prefix'])) {
	       $prefix = htmlspecialchars($f3->get('POST')['prefix']);
	    } else {
	       $prefix= '';
	    }

	    if (isset($f3->get('POST')['publickey'])) {
	       $publickey = htmlspecialchars($f3->get('POST')['publickey']);
	    } else {
	       $publickey= '';
	    }

	   // put all the values into a nice array
	   $f3->set('new', array(
					'organization' => $organization,
					'address' => $address,
					'name' => $name,
					'prefix' => $prefix,
					'publickey' => $publickey
				)); 
          } else if ((isset( $f3->get('GET')['action'] ) && isset( $f3->get('GET')['prefix']))
				|| isset($f3->get('POST')['prefix'])) {
	    if (isset($f3->get('POST')['prefix'])) {
	      $action = 'edit';
	      $wasSubmit = true;
	    } else {
              $action = $f3->get('GET')['action'];
	      $wasSubmit = false;
	    }
	    $prefix = $f3->get('REQUEST')['prefix'];

	    // check if actions and prefixes are valid
	    if ($action === 'edit') {
	      if (!$wasSubmit && !$this->checkPrefix($f3, $prefix)) {
		$action = '';
	      } else {
		// get entry from db
		$result = $f3->get('DB')->exec('SELECT organization, address, name, prefix, publickey FROM satellite WHERE prefix=?', $prefix);
		$f3->set('editprefix', $result[0]);
	        $f3->set('base64key', base64_encode($f3->get('editprefix')['publickey']));
	      }

	      if ($wasSubmit) {
		$f3->set('editprefix', array(
						'organization' => htmlspecialchars($f3->get('POST')['organization']),
						'address' => htmlspecialchars($f3->get('POST')['address']),
						'name' => htmlspecialchars($f3->get('POST')['name']),
						'prefix' => htmlspecialchars($f3->get('POST')['prefix']),
						'publickey' => htmlspecialchars($f3->get('POST')['publickey'])));
		$f3->set('base64key', $f3->get('POST')['publickey']);

	      }
	    } else if ($action === 'delete') {
	      foreach($prefix as $p) {
			if (!$this->checkPrefix($f3, $p)) {
		  		$action = '';
		  		$msg = 'One or more of your Satellites was not valid.';
		  		break; // found one invalid prefix --> stop
			} else {
				$this->deletePrefix($f3, $p);
		  	}
	      }
	    } else {
	      $action = '';
	    }

	    // action is save
            $f3->set('action', $action);
	  } else {
	    $f3->set('action', '');
	  }
	}
	
	/*
	 *	Checks the prefix against the db and saves the unique result to global variable editprefix
	 */
	public function checkPrefix($f3, $prefix) {
	  $result = $f3->get('DB')->exec('SELECT organization, address, name, prefix, publickey FROM satellite WHERE prefix=?', $prefix);
	  if (sizeof($result) != 1) {
	    return false;
	  }
	  return true;
	}

	/*
	 *	Saves a new satellite
	 */
	public function save($f3, $organization, $address, $name, $prefix, $publickey) {
	  // check inputs
	  if (empty($organization) || empty($address) || empty($name) || empty($prefix)) return 'Organization, address, name and prefix must not be empty.';
	  if (!preg_match('/^[a-zA-Z-]{3,20}\.[a-zA-Z]{2,3}$/', $organization)) return 'Organization must be in form something.de';
	  if (!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $address)
		&& !preg_match('/^[a-zA-Z-]*\.*[a-zA-Z-]+\.[a-zA-Z]{2,3}$/', $address)) return 'Address must be an ip or hostname.';
	  if (!preg_match('/^[\a-zA-ZäüöÄÜÖß \.()-_]*$/', $name)) return "Name must be a string between 0 and 255 characters. (Special chars: ._-())";
	  if (!preg_match('/^[a-z]{2,3}$/', $prefix)) return "Prefix must be a string between 2 and 3 characters.";
	  if (!empty($publickey) && !preg_match('/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/', $publickey)) return 'Public key must be base64 encoded';
	  
	  $pubkeybin = base64_decode($publickey);

	  // It will not be checked if this prefix is already in use. It is the primary key and an entry that is existing will be overwritten
	  $result = $f3->get('DB')->exec('UPDATE satellite SET organization=?, address=?, name=?, publickey=? WHERE prefix=?',
		array( 1 => $organization, 2 => $address, 3 => $name, 4 => $pubkeybin, 5 => $prefix));
	  
	  if ($result == 1 || $result == 0) return '';
	  else return 'Some weird error occured.'; 
	}

	public function saveNew($f3, $organization, $address, $name, $prefix, $publickey) {
	  // check inputs
	  if (empty($organization) || empty($address) || empty($name) || empty($prefix)) return 'Organization, address, name and prefix must not be empty.';
	  if (!preg_match('/^[a-zA-Z-]{3,20}\.[a-zA-Z]{2,3}$/', $organization)) return 'Organization must be in form something.de';
	  if (!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $address)
		&& !preg_match('/^[a-zA-Z-]*\.*[a-zA-Z-]+\.[a-zA-Z]{2,3}$/', $address)) return 'Address must be an ip or hostname.';
	  if (!preg_match('/^[a-zA-ZäüöÄÜÖß \.()-_]*$/', $name)) return "Name must be a string between 0 and 255 characters. (Special chars: ._-())";
	  if (!preg_match('/^[a-z]{2,3}$/', $prefix)) return "Prefix must be a string between 2 and 3 characters.";
	  if (!empty($publickey) && !preg_match('/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/', $publickey)) return 'Public key must be base64 encoded';
	  
	  $pubkeybin = base64_decode($publickey);

	  $result = $f3->get('DB')->exec('INSERT INTO satellite SET organization=?, address=?, name=?, publickey=?, prefix=?',
		array(1 => $organization, 2 => $address, 3 => $name, 4 => $publickey, 5 => $prefix));

	  if ($result == true) return '';
	  else return 'Error while inserting satellite.';
	}


	public function deletePrefix($f3, $prefix) {
		$result = $f3->get('DB')->exec('DELETE FROM satellite WHERE prefix=?', $prefix);
		if ($result === 1) return true;
		else return false;
	}
}

?>