/** * Creates a new device from $_POST data. * * @throws DeviceException * If invalid data was sent. */ public function add() { if (Session::getInstance()->appstate !== AppState::LOGGEDIN) { throw new DeviceException('You can\'t add a new device now.'); } if (Session::getInstance()->incognito) { throw new DeviceException('You can\'t create new devices if your using the incognito login mode'); } if (!valid_token()) { throw new DeviceException('Invalid token.'); } if (empty($_POST['device-name'])) { throw new DeviceException('You need to give your new device a name.'); } if (empty($_POST['device-type']) || !in_array($_POST['device-type'], array_keys(DeviceManager::listAvailableDevices()))) { throw new DeviceException('You need to pick a device from the list.'); } if (empty($_POST['device-country']) || !in_array($_POST['device-country'], array_keys(LanguageCodes::$languages))) { throw new DeviceException('You need to choose a language for the device.'); } // Sanitize name. $name = preg_replace('/[^a-zA-Z0-9äÄöÖüÜß\\.\\-éÉèÈâÂêÊŷŶáÁàÀ@?! ]/', '_', $_POST['device-name']); // Create the new device. Session::getInstance()->device_manager->checkinNewDevice($_POST['device-type'], $name, $_POST['device-country']); $this->render(); }