/** * Establish a database connection and create the connection object * * @param Setting $settings * @return PDO * @throws Exception */ private function doConnect(Setting $settings) : PDO { try { # Create a new PDO connection $connection = new PDO($settings->getSetting('dsn'), $settings->getSetting('user'), $settings->getSetting('password')); } catch (PDOException $exception) { # Stop the execution throw new Exception($exception->getMessage()); } # Set attributes $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); return $connection; }
/** * Connects to the Redis server * * @param Setting $settings Setting instance with the required attributes * @return Redis Current object to daisy chain method calls * * @throws Exception When the Redis server was not reached */ private function doConnect(Setting $settings) : self { # Connection $connection = new \Redis(); $connection->connect($settings->getSetting('address'), $settings->getSetting('port'), $settings->getSetting('timeout')); # When error occurred if ($connection->getLastError() !== null) { throw new Exception('Failed to connect to the Redis server: ' . $connection->getLastError()); } # Set options $connection->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); # Store the connection $this->connection = $connection; return $this; }
throw new \Exception('Please enter a valid application title.'); } $settings['app_name'] = htmlspecialchars($_POST['app_name']); $settings['app_title'] = htmlspecialchars($_POST['app_title']); $settings['app_copyright'] = htmlspecialchars($_POST['app_copyright']); $settings['app_description'] = empty($_POST['app_description']) ? htmlspecialchars($_POST['app_title']) : htmlspecialchars($_POST['app_description']); $settings['app_robots'] = Setting::ParseRobots(in_array('index', $_POST['app_robots']), in_array('follow', $_POST['app_robots']), in_array('archive', $_POST['app_robots'])); $settings['api_display_doc'] = !empty($_POST['api_display_doc']); $settings['api_requests'] = !empty($_POST['api_requests']); /* if (!empty($_POST['conf_email_sender']) && !filter_var($_POST['conf_email_sender'], FILTER_VALIDATE_EMAIL)) throw new \Exception('Please enter a valid sender email address.'); $settings['conf_email_sender'] = htmlspecialchars($_POST['conf_email_sender']); */ foreach ($settings as $key => $value) { $s = new Setting($key); if ($s->getValue() != $settings[$key]) { $s->setValue($value); if (!$s->save()) { throw new \Exception('Unable to save the new value for ' . $key . '.'); } } } $message_success = true; } catch (\Exception $e) { $this->assign('form_error', $e->getMessage()); } } else { $this->assign('form_data', array('app_name' => (new Setting('app_name'))->getValue(), 'app_title' => (new Setting('app_title'))->getValue(), 'app_copyright' => (new Setting('app_copyright'))->getValue(), 'app_description' => (new Setting('app_description'))->getValue(), 'app_robots' => Setting::GetRobots((new Setting('app_robots'))->getValue()), 'conf_email_sender' => (new Setting('conf_email_sender'))->getValue(), 'api_display_doc' => (new Setting('api_display_doc'))->getValue(), 'api_requests' => (new Setting('api_requests'))->getValue())); } if (isset($message_success)) {