* @copyright 2001-2007 VIKO team and contributors * @license http://www.gnu.org/licenses/gpl.html GPL 2.0 */ require_once 'Module.php'; require_once 'HTML/QuickForm.php'; require_once 'HTML/QuickForm/Renderer/VIKO.php'; require_once 'UserFactory.php'; require_once 'I18n.php'; /** * Manages the login page of VIKO * */ class Module_Login extends Module { /** * Returns the module identifier string * * @access public * @static * @return string Identificator of module */ function getID() { return "login"; } /** * Returns special stylesheet for login page * * @access public * @return array list of stylesheets */ function getStylesheets() { return array( array( 'uri' => 'login.css' ) ); } /** * Returns login page in HTML * * @return string HTML fragment */ function toHTML() { $this->title( _("VIKO learning environment") ); $form = $this->_createLoginForm(); if ( $form->validate() ) { return $this->_login( $form->exportValue('username'), $form->exportValue('password') ); } else { return $this->_createLoginPage(); } } /** * Returns HTML of the login-page * * @access private * @return string HTML fragment */ function _createLoginPage() { $login_title = HTML::h3( _("Log in") ); $form_html = $this->_createLoginFormHTML(); $register_link = HTML::a( $this->URI("register"), _("Register as a new user.") ); $forgot_link = HTML::a( $this->URI("forgot-password"), _("Forgot your password?") ); // Only show registration link if registration is enabled in configuration if ( Configuration::getAllowRegistration() ) { $links = HTML::ul( $register_link, $forgot_link ); } else { $links = HTML::ul( $forgot_link ); } // get the localized text for the main content area. $main_content = I18n::getFileContents( "login.html" ); return HTML::element("div", $login_title . $form_html . $links, array("class"=>"col-1") ). HTML::element("div", $main_content, array("class"=>"col-2") ); } /** * Returns HTML of the login-form * * @access private * @return string HTML fragment */ function _createLoginFormHTML() { $username_label = _("Username") . ":"; $password_label = _("Password") . ":"; $submit_label = _("Get in"); return <<

EOHTML; } /** * Performes the login * * If login succeeds, stores user object into session and * sends user to my-viko page, * otherwise returns HTML, that says "Login failed." * * @access private * @param string $username * @param string $password * @return string HTML fragment */ function _login( $username, $password ) { $user = UserFactory::createUserByLogin( $username, $password ); if ( $user ) { // save user info to session $_SESSION['user'] = $user; // Redirect to the main page of VIKO $this->redirect("my-viko"); return false; } else { $html = $this->title( _("Login Failed!") ); $html.= HTML::p( _("Either the username or password you entered was wrong.") ); $html.= HTML::p( HTML::a( $this->URI(), _("Try again.") ) ); return $html; } } /** * Returns the login form object * * @access private * @return HTML_QuickForm form object */ function _createLoginForm() { $form =& new HTML_QuickForm(); $form->addElement('text', 'username', _('Username') . ':' ); $form->addElement('password', 'password', _('Password') . ':' ); $form->addElement('submit', 'submit', _('Get in') ); return $form; } } ?>