/** * Handles POST requests from the Users listing (ie: creating a new user) */ public function post_users() { $this->fetch_users(); $extract = $this->handler_vars->filter_keys('newuser', 'delete', 'new_pass1', 'new_pass2', 'new_email', 'new_username'); foreach ($extract as $key => $value) { ${$key} = $value; } if (isset($newuser)) { $action = 'newuser'; } elseif (isset($delete)) { $action = 'delete'; } $error = ''; if (isset($action) && 'newuser' == $action) { if (!isset($new_pass1) || !isset($new_pass2) || empty($new_pass1) || empty($new_pass2)) { Session::error(_t('Password is required.'), 'adduser'); } else { if ($new_pass1 !== $new_pass2) { Session::error(_t('Password mis-match.'), 'adduser'); } } if (!isset($new_email) || empty($new_email) || !strstr($new_email, '@')) { Session::error(_t('Please supply a valid email address.'), 'adduser'); } if (!isset($new_username) || empty($new_username)) { Session::error(_t('Please supply a user name.'), 'adduser'); } // safety check to make sure no such username exists $user = User::get_by_name($new_username); if (isset($user->id)) { Session::error(_t('That username is already assigned.'), 'adduser'); } if (!Session::has_errors('adduser')) { $user = new User(array('username' => $new_username, 'email' => $new_email, 'password' => Utils::crypt($new_pass1))); if ($user->insert()) { Session::notice(sprintf(_t("Added user '%s'"), $new_username)); } else { $dberror = DB::get_last_error(); Session::error($dberror[2], 'adduser'); } } else { $settings = array(); if (isset($username)) { $settings['new_username'] = $new_username; } if (isset($new_email)) { $settings['new_email'] = $new_email; } $this->theme->assign('settings', $settings); } } else { if (isset($action) && 'delete' == $action) { $this->update_users($this->handler_vars); } } $this->theme->display('users'); }
/** * Attempts to install the database. Returns the result of * the installation, adding errors to the theme if any * occur * * @return bool result of installation */ private function install_db() { $db_host = $this->handler_vars['db_host']; $db_type = $this->handler_vars['db_type']; $db_schema = $this->handler_vars['db_schema']; $db_user = $this->handler_vars['db_user']; $db_pass = $this->handler_vars['db_pass']; switch ($db_type) { case 'mysql': case 'pgsql': // MySQL & PostgreSQL requires specific connection information if (empty($db_user)) { $this->theme->assign('form_errors', array("{$db_type}_db_user" => _t('User is required.'))); return false; } if (empty($db_schema)) { $this->theme->assign('form_errors', array("{$db_type}_db_schema" => _t('Name for database is required.'))); return false; } if (empty($db_host)) { $this->theme->assign('form_errors', array("{$db_type}_db_host" => _t('Host is required.'))); return false; } break; case 'sqlite': // If this is a SQLite database, let's check that the file // exists and that we can access it. if (!$this->check_sqlite()) { return false; } break; } if (isset($this->handler_vars['table_prefix'])) { // store prefix in the Config singleton so DatabaseConnection can access it Config::set('db_connection', array('prefix' => $this->handler_vars['table_prefix'])); } if (!$this->connect_to_existing_db()) { $this->theme->assign('form_errors', array("{$db_type}_db_user" => _t('Problem connecting to supplied database credentials'))); return false; } DB::begin_transaction(); /* Let's install the DB tables now. */ $create_table_queries = $this->get_create_table_queries($this->handler_vars['db_type'], $this->handler_vars['table_prefix'], $this->handler_vars['db_schema']); DB::clear_errors(); DB::dbdelta($create_table_queries, true, true, true); if (DB::has_errors()) { $error = DB::get_last_error(); $this->theme->assign('form_errors', array('db_host' => _t('Could not create schema tables… %s', array($error['message'])))); DB::rollback(); return false; } // Cool. DB installed. Create the default options // but check first, to make sure if (!Options::get('installed')) { if (!$this->create_default_options()) { $this->theme->assign('form_errors', array('options' => _t('Problem creating default options'))); DB::rollback(); return false; } } // Create the Tags vocabulary if (!$this->create_tags_vocabulary()) { $this->theme->assign('form_errors', array('options' => _t('Problem creating tags vocabulary'))); DB::rollback(); return false; } // Create the standard post types and statuses if (!$this->create_base_post_types()) { $this->theme->assign('form_errors', array('options' => _t('Problem creating base post types'))); DB::rollback(); return false; } if (!$this->create_base_comment_types()) { $this->theme->assign('form_errors', array('options' => _t('Problem creating base comment types and statuses'))); DB::rollback(); return false; } // Let's setup the admin user and group now. // But first, let's make sure that no users exist $all_users = Users::get_all(); if (count($all_users) < 1) { $user = $this->create_admin_user(); if (!$user) { $this->theme->assign('form_errors', array('admin_user' => _t('Problem creating admin user.'))); DB::rollback(); return false; } $admin_group = $this->create_admin_group($user); if (!$admin_group) { $this->theme->assign('form_errors', array('admin_user' => _t('Problem creating admin group.'))); DB::rollback(); return false; } // create default tokens ACL::rebuild_permissions($user); } // create a first post, if none exists if (!Posts::get(array('count' => 1))) { if (!$this->create_first_post()) { $this->theme->assign('form_errors', array('post' => _t('Problem creating first post.'))); DB::rollback(); return false; } } /* Post::save_tags() closes transaction, until we fix that, check and reconnect if needed */ if (!DB::in_transaction()) { DB::begin_transaction(); } /* Store current DB version so we don't immediately run dbdelta. */ Version::save_dbversion(); /* Ready to roll. */ DB::commit(); return true; }
public function register_user($form) { $group = UserGroup::get($form->get_option('group_name')); $user = new User(array('username' => $form->username, 'email' => $form->email, 'password' => Utils::crypt($form->password))); if ($user->insert()) { $group->add($user); if ($form->get_option('standalone')) { $user->remember(); $redirect = URL::get('register_success'); } else { Session::notice(sprintf(_t("Added user '%s'", __CLASS__), $form->username)); $redirect = ""; } // Let plugins alter the redirect location. Yes, the string is loooong, but it's propably unique, too. $redirect = Plugins::filter('register_user_success_redirect_location', $redirect, $form); Utils::redirect($redirect); } else { $dberror = DB::get_last_error(); Session::error($dberror[2], 'adduser'); } }
public function action_hconsole_debug() { if (isset($this->code['debug'])) { ob_start(); $res = eval($this->code['debug']); $dat = ob_get_contents(); ob_end_clean(); if ($res === false) { throw Error::raise($dat, E_COMPILE_ERROR); } else { echo $this->htmlspecial ? htmlspecialchars($dat) : $dat; } } if ($this->sql) { $itemlist = array(); if (preg_match('#^\\s*(select|show).*#i', $this->sql)) { $data = DB::get_results($this->sql); if (DB::has_errors()) { throw Error::raise(DB::get_last_error()); } if (is_array($data) && count($data)) { self::sql_dump($data); } else { echo 'empty set, nothing returned.'; } } else { $data = DB::query($this->sql); if (DB::has_errors()) { throw Error::raise(DB::get_last_error()); } echo 'Result: ' . (string) $data; } } }
/** * Success method for the add_user form * @param FormUI $form The add_user form */ public function do_add_user(FormUI $form) { $user = new User(array('username' => $form->username->value, 'email' => $form->email->value, 'password' => Utils::crypt($form->password->value))); if ($user->insert()) { Session::notice(_t("Added user '%s'", array($form->username->value))); $form->clear(); } else { $dberror = DB::get_last_error(); Session::error($dberror[2], 'adduser'); } }