Custom php login page outside of Gallery

Marty McFly
Marty McFly's picture

Joined: 2006-08-28
Posts: 1
Posted: Mon, 2006-08-28 04:30

I would like to create my own login page that integrates with Gallery. When users visit the page for the first time, they enter their username and password, it is authenticated against the Gallery database, and they are sent to their user album. When users are already logged in (with a cookie), the page should send them straight to their user album without requiring them to login again. I would think this would be pretty simple, but I've searched the forums for days and I can't seem to find someone that has done this before. I've pieced together this code from what I've learned so far:

<?php

require_once('embed.php');
$ret = GalleryEmbed::init(array('fullInit' => true));
if ($ret->isError()) {
     print $ret->getAsHtml();
     exit;
}

$username = "";
$password = "";

if (isset($_POST['submit'])) {
     $username = $_POST["username"];
     $password = $_POST["password"];
	
     list ($ret, $user) = GalleryCoreApi::fetchUserByUserName($username);
     if ($ret->isError()) {
          print $ret->getAsHtml();
     } else {
          if ($user->isCorrectPassword($password)) {
               $gallery->setActiveUser($user);
          }
     }
}

$user = $gallery->getActiveUser();

if ($user->getfullName() != "Guest") {
     header("Location: http://www.domain.com/gallery2/main.php?g2_controller=useralbum.UserAlbum");
}

?>

<form method="post" action="<?php echo $PHP_SELF;?>">
     Username <input type="text" size="16" name="username" value="<?php echo $username ?>" /><br />
     Password <input type="password" size="16" name="password" value="<?php echo $password ?>" />
     <input type="submit" name="submit" value="submit"/>
</form>

It works great in that it authenticates the user correctly, and subsequent visits to the page show that the user is already logged in. Unfortunately, after logging in, the user is correctly redirected to their user album, but Gallery shows the user as logged out and doesn't give them permission to view their album.

Is setActiveUser() the only function that I need to call to log the user into Gallery? I've tried this:

GalleryEmbed::init(array('fullInit' => true, 'activeUserId' => $user->getId()));

but Gallery throws an error saying that there is no link between the supplied ID and an ID for an embeded application. There is no other embeded application, so I don't know how to pass a Gallery user ID to the Gallery init function.

After authenticating the user, I've also tried redirecting them to the Gallery login page with the information supplied:

header("Location: http://www.domain.com/gallery2/main.php?g2_controller=core.UserLogin
&g2_form[formName]=UserLogin&g2_form[username]=" . $username . "&g2_form[password]="
. $password . "&g2_form[action][login]=Login");

but that doesn't work if the user is already logged in with a cookie because there doesn't apear to be a function for getting the user's password to add to the url.

I'm afraid I've run out of things to try - any idea why Gallery is ignoring the logged in user?