/** * Display the login page */ public function login() { $form = $this->form(); if (!$form->submitted()) { if (App::request()->getParams('code') == 403) { $form->status = Form::STATUS_ERROR; $form->addReturn('message', Lang::get($this->_plugin . '.403-message')); } // Display the login page in a dialog box return Dialogbox::make(array('page' => $form->__toString(), 'icon' => 'sign-in', 'title' => Lang::get($this->_plugin . '.login-form-title'))); } else { if ($form->check()) { $hash = Crypto::saltHash($form->getData('password')); $example = new DBExample(array('$and' => array(array('email' => $form->getData('login')), array('password' => $hash)))); $user = User::getByExample($example); if ($user) { if (!$user->active) { // The user is not active return $form->response(Form::STATUS_ERROR, Lang::get($this->_plugin . '.login-error-inactive-user')); } // The user can be connected App::session()->setData('user', array('id' => $user->id, 'email' => $user->email, 'username' => $user->getUsername(), 'ip' => App::request()->clientIp())); if (App::request()->getParams('redirect')) { $form->addReturn('redirect', App::request()->getParams('redirect')); } return $form->response(Form::STATUS_SUCCESS, Lang::get($this->_plugin . '.login-success')); } else { return $form->response(Form::STATUS_ERROR, Lang::get($this->_plugin . '.login-error-authentication')); } } } }
/** * Install the application */ public function settings() { $form = new Form(array('id' => 'install-settings-form', 'labelWidth' => '30em', 'fieldsets' => array('global' => array('legend' => Lang::get('install.settings-global-legend', null, null, $this->language), new TextInput(array('name' => 'title', 'required' => true, 'label' => Lang::get('install.settings-title-label', null, null, $this->language), 'default' => DEFAULT_HTML_TITLE)), new TextInput(array('name' => 'rooturl', 'required' => true, 'label' => Lang::get('install.settings-rooturl-label', null, null, $this->language), 'placeholder' => 'http://', 'default' => getenv('REQUEST_SCHEME') . '://' . getenv('SERVER_NAME'))), new SelectInput(array('name' => 'timezone', 'required' => true, 'options' => array_combine(\DateTimeZone::listIdentifiers(), \DateTimeZone::listIdentifiers()), 'default' => DEFAULT_TIMEZONE, 'label' => Lang::get('install.settings-timezone-label')))), 'database' => array('legend' => Lang::get('install.settings-database-legend', null, null, $this->language), new TextInput(array('name' => 'db[host]', 'required' => true, 'label' => Lang::get('install.settings-db-host-label', null, null, $this->language), 'default' => 'localhost')), new TextInput(array('name' => 'db[username]', 'required' => true, 'label' => Lang::get('install.settings-db-username-label', null, null, $this->language))), new PasswordInput(array('name' => 'db[password]', 'required' => true, 'label' => Lang::get('install.settings-db-password-label', null, null, $this->language), 'pattern' => '/^.*$/')), new TextInput(array('name' => 'db[dbname]', 'required' => true, 'pattern' => '/^\\w+$/', 'label' => Lang::get('install.settings-db-dbname-label', null, null, $this->language))), new TextInput(array('name' => 'db[prefix]', 'default' => 'Hawk', 'pattern' => '/^\\w+$/', 'label' => Lang::get('install.settings-db-prefix-label', null, null, $this->language)))), 'admin' => array('legend' => Lang::get('install.settings-admin-legend', null, null, $this->language), new TextInput(array('name' => 'admin[login]', 'required' => true, 'pattern' => '/^\\w+$/', 'label' => Lang::get('install.settings-admin-login-label', null, null, $this->language))), new EmailInput(array('name' => 'admin[email]', 'required' => true, 'label' => Lang::get('install.settings-admin-email-label', null, null, $this->language))), new PasswordInput(array('name' => 'admin[password]', 'required' => true, 'label' => Lang::get('install.settings-admin-password-label', null, null, $this->language))), new PasswordInput(array('name' => 'admin[passagain]', 'required' => true, 'compare' => 'admin[password]', 'label' => Lang::get('install.settings-admin-passagain-label', null, null, $this->language)))), '_submits' => array(new SubmitInput(array('name' => 'valid', 'value' => Lang::get('install.install-button', null, null, $this->language), 'icon' => 'cog')))), 'onsuccess' => 'location.href = data.rooturl;')); if (!$form->submitted()) { // Display the form $body = View::make(Plugin::current()->getView('settings.tpl'), array('form' => $form)); return \Hawk\Plugins\Main\MainController::getInstance()->index($body); } else { // Make the installation if ($form->check()) { /** * Generate Crypto constants */ $salt = Crypto::generateKey(24); $key = Crypto::generateKey(32); $iv = Crypto::generateKey(16); $configMode = 'prod'; /** * Create the database and it tables */ $tmpfile = tempnam(sys_get_temp_dir(), ''); DB::add('tmp', array(array('host' => $form->getData('db[host]'), 'username' => $form->getData('db[username]'), 'password' => $form->getData('db[password]')))); try { DB::get('tmp'); } catch (DBException $e) { return $form->response(Form::STATUS_ERROR, Lang::get('install.install-connection-error')); } try { $param = array('{{ $dbname }}' => $form->getData('db[dbname]'), '{{ $prefix }}' => $form->getData('db[prefix]'), '{{ $language }}' => $this->language, '{{ $timezone }}' => $form->getData('timezone'), '{{ $title }}' => Db::get('tmp')->quote($form->getData('title')), '{{ $email }}' => Db::get('tmp')->quote($form->getData('admin[email]')), '{{ $login }}' => Db::get('tmp')->quote($form->getData('admin[login]')), '{{ $password }}' => Db::get('tmp')->quote(Crypto::saltHash($form->getData('admin[password]'), $salt)), '{{ $ip }}' => Db::get('tmp')->quote(App::request()->clientIp())); $sql = strtr(file_get_contents(Plugin::current()->getRootDir() . 'templates/install.sql.tpl'), $param); // file_put_contents($tmpfile, $sql); Db::get('tmp')->query($sql); /** * Create the config file */ $param = array('{{ $salt }}' => addcslashes($salt, "'"), '{{ $key }}' => addcslashes($key, "'"), '{{ $iv }}' => addcslashes($iv, "'"), '{{ $configMode }}' => $configMode, '{{ $rooturl }}' => $form->getData('rooturl'), '{{ $host }}' => $form->getData('db[host]'), '{{ $username }}' => $form->getData('db[username]'), '{{ $password }}' => $form->getData('db[password]'), '{{ $dbname }}' => $form->getData('db[dbname]'), '{{ $prefix }}' => $form->getData('db[prefix]'), '{{ $sessionEngine }}' => $form->getData('session'), '{{ $version }}' => $form->getData('version')); $config = strtr(file_get_contents(Plugin::current()->getRootDir() . 'templates/config.php.tpl'), $param); file_put_contents(INCLUDES_DIR . 'config.php', $config); /** * Create etc/dev.php */ App::fs()->copy(Plugin::current()->getRootDir() . 'templates/etc-dev.php', ETC_DIR . 'dev.php'); /** * Create etc/prod.php */ App::fs()->copy(Plugin::current()->getRootDir() . 'templates/etc-prod.php', ETC_DIR . 'prod.php'); $form->addReturn('rooturl', $form->getData('rooturl')); return $form->response(Form::STATUS_SUCCESS, Lang::get('install.install-success')); } catch (\Exception $e) { return $form->response(Form::STATUS_ERROR, Lang::get('install.install-error')); } } } }
/** * Change the current user password */ public function changePassword() { $params = array('id' => 'update-password-form', 'fieldsets' => array('form' => array(new PasswordInput(array('name' => 'current-password', 'label' => Lang::get($this->_plugin . '.update-password-current-password-label'), 'required' => true)), new PasswordInput(array('name' => 'new-password', 'required' => true, 'label' => Lang::get($this->_plugin . '.update-password-new-password-label'))), new PasswordInput(array('name' => 'password-confirm', 'required' => true, 'label' => Lang::get($this->_plugin . '.update-password-new-password-confirm-label'), 'compare' => 'new-password'))), '_submits' => array(new SubmitInput(array('name' => 'valid', 'value' => Lang::get($this->_plugin . '.valid-button'))), new ButtonInput(array('name' => 'cancel', 'value' => Lang::get($this->_plugin . '.cancel-button'), 'onclick' => 'app.dialog("close")')))), 'onsuccess' => 'app.dialog("close")'); $form = new Form($params); if (!$form->submitted()) { return View::make(Theme::getSelected()->getView("dialogbox.tpl"), array('title' => Lang::get($this->_plugin . '.update-password-title'), 'icon' => 'lock', 'page' => $form)); } else { if ($form->check()) { $me = Session::getUser(); if ($me->password != Crypto::saltHash($form->getData('current-password'))) { return $form->response(Form::STATUS_ERROR, Lang::get($this->_plugin . '.update-password-bad-current-password')); } try { $me->set('password', Crypto::saltHash($form->getData('new-password'))); $me->save(); return $form->response(Form::STATUS_SUCCESS, Lang::get($this->_plugin . '.update-password-success')); } catch (Exception $e) { return $form->response(Form::STATUS_ERROR, DEBUG_MODE ? $e->getMessage() : Lang::get($this->_plugin . '.update-password-error')); } } } }