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; } } ?>