2008-11-12 03:40:49 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
|
/**
|
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
2010-03-03 10:15:34 -08:00
|
|
|
* Copyright (C) 2000-2010 Bharat Mediratta
|
2008-11-12 03:40:49 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
|
* your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
class Login_Controller extends Controller {
|
2009-03-16 04:33:45 +00:00
|
|
|
|
|
|
|
|
public function ajax() {
|
|
|
|
|
$view = new View("login_ajax.html");
|
2009-10-30 09:32:18 -07:00
|
|
|
$view->form = auth::get_login_form("login/auth_ajax");
|
2010-07-21 21:30:13 -07:00
|
|
|
json::reply(array("form" => (string) $view));
|
2009-03-16 04:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function auth_ajax() {
|
2009-06-01 22:40:22 -07:00
|
|
|
access::verify_csrf();
|
|
|
|
|
|
2009-03-16 08:05:44 +00:00
|
|
|
list ($valid, $form) = $this->_auth("login/auth_ajax");
|
2009-03-16 04:33:45 +00:00
|
|
|
if ($valid) {
|
2010-07-21 21:30:13 -07:00
|
|
|
json::reply(array("result" => "success"));
|
2008-12-25 05:12:46 +00:00
|
|
|
} else {
|
2010-07-21 21:30:13 -07:00
|
|
|
json::reply(array("result" => "error", "form" => (string) $form));
|
2008-12-25 05:12:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-16 04:33:45 +00:00
|
|
|
public function html() {
|
2010-06-12 15:28:03 -07:00
|
|
|
$view = new Theme_View("page.html", "other", "login");
|
|
|
|
|
$view->page_title = t("Login");
|
|
|
|
|
$view->content = auth::get_login_form("login/auth_html");
|
|
|
|
|
print $view;
|
2009-03-11 13:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
2009-03-16 04:33:45 +00:00
|
|
|
public function auth_html() {
|
2009-06-01 22:40:22 -07:00
|
|
|
access::verify_csrf();
|
|
|
|
|
|
2009-03-16 08:05:44 +00:00
|
|
|
list ($valid, $form) = $this->_auth("login/auth_html");
|
2009-03-16 04:33:45 +00:00
|
|
|
if ($valid) {
|
2010-06-12 15:28:03 -07:00
|
|
|
$continue_url = $form->continue_url->value;
|
|
|
|
|
url::redirect($continue_url ? $continue_url : item::root()->abs_url());
|
2009-03-16 04:33:45 +00:00
|
|
|
} else {
|
2010-01-22 18:12:30 -08:00
|
|
|
$view = new Theme_View("page.html", "other", "login");
|
|
|
|
|
$view->page_title = t("Log in to Gallery");
|
|
|
|
|
$view->content = new View("login_ajax.html");
|
|
|
|
|
$view->content->form = $form;
|
|
|
|
|
print $view;
|
2009-03-16 04:33:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-10-21 16:02:34 -07:00
|
|
|
|
2009-03-16 08:05:44 +00:00
|
|
|
private function _auth($url) {
|
2009-10-30 09:32:18 -07:00
|
|
|
$form = auth::get_login_form($url);
|
2008-12-25 05:12:46 +00:00
|
|
|
$valid = $form->validate();
|
|
|
|
|
if ($valid) {
|
2009-10-22 13:09:20 -07:00
|
|
|
$user = identity::lookup_user_by_name($form->login->inputs["name"]->value);
|
|
|
|
|
if (empty($user) || !identity::is_correct_password($user, $form->login->password->value)) {
|
2008-12-25 05:12:46 +00:00
|
|
|
$form->login->inputs["name"]->add_error("invalid_login", 1);
|
2010-01-30 19:48:57 -08:00
|
|
|
$name = $form->login->inputs["name"]->value;
|
|
|
|
|
log::warning("user", t("Failed login for %name", array("name" => $name)));
|
2010-02-07 08:49:37 -08:00
|
|
|
module::event("user_auth_failed", $name);
|
2008-12-25 05:12:46 +00:00
|
|
|
$valid = false;
|
2008-11-12 03:40:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-11-12 21:42:40 +00:00
|
|
|
|
2008-12-25 05:12:46 +00:00
|
|
|
if ($valid) {
|
2009-10-30 09:32:18 -07:00
|
|
|
auth::login($user);
|
2008-12-25 05:12:46 +00:00
|
|
|
}
|
2009-02-04 05:49:29 +00:00
|
|
|
|
2009-05-27 01:58:46 -07:00
|
|
|
// Either way, regenerate the session id to avoid session trapping
|
|
|
|
|
Session::instance()->regenerate();
|
|
|
|
|
|
2009-03-16 04:33:45 +00:00
|
|
|
return array($valid, $form);
|
2008-11-12 03:40:49 +00:00
|
|
|
}
|
|
|
|
|
}
|