runGallery();
function g2_manage_hmd2user($user_ID)
{
// If G2 User not active then link or create.
    $ret = GalleryEmbed::checkActiveUser($user_ID);
    if ($ret->isError())
    {
        $customer = db_query("SELECT * from customers where customer_id = $user_ID"));
        list ($ret, $g2finduser ) = GalleryCoreApi::fetchUserByUsername ($customer['email_address']);
        //if no error then map the hmd to g2id
         if (!$ret->isError())
         {
            GalleryEmbed::addExternalIdMapEntry($user_ID, $g2finduser->_id, 'GalleryUser');
            if ($ret->isError())
                echo "<h2>Fatal G2 error:</h2> " .$ret->getAsHtml();
		 }
         //otherwise we must create a g2 account
         else
         {
           $ret = GalleryEmbed::createUser( $user_ID, array ( 'username' => $customer['email'], 'email' => $customer['email'], 'fullname' => $customer['firstname'].' '.$customer['lastname'],'hashedpassword' => $customer['password'], 'hashmethod' => 'md5'));
           //Check for G2 Error on Add
            if ($ret->isError())
            {
              echo "<h2>Fatal G2 error:</h2> " .$ret->getAsHtml();
            }
            $ret = GalleryEmbed::checkActiveUser($user_ID);
            //GalleryEmbed::done();
         }
    }
    return $ret;
}
function g2_init()
{
    require_once('./gallery2/embed.php');
    $data = array();
    // initiate G2
    $ret = GalleryEmbed::init(array('embedUri' => 'main.php',
                    'embedPath' => '/',
                    'relativeG2Path' => './gallery2',
                    'loginRedirect' => 'login.php',
                    'activeUserId' => ''));
    if ($ret->isError()) {
        // a real problem, handle it, i.e. raise error or echo error message
        echo "<h2>Fatal G2 error</h2> Here's the error from G2: " .$ret->getAsHtml();
        get_footer();
        exit;
    }
    return $ret;
}
function g2_login() {
    $user_ID = $_SESSION['customer_id'];
    echo 'User Id: '.$user_ID;
    $ret = g2_init();
    if (!$ret->isError())
        if ($user_ID)
            $ret = g2_manage_hmd2user($user_ID);
        else
            GalleryEmbed::checkActiveUser('');
    return $ret;
    }
function runGallery() {
$ret = g2_login();
if ($ret) {
	$g2data = GalleryEmbed::handleRequest();
	if ($g2data['isDone']) {
	exit; // G2 has already sent output (redirect or binary data)
	}
// Display G2 Output
  echo $g2data['headHtml'];    //Include the gallery header
  echo $g2data['bodyHtml'];	 //Display the gallery content
  //Close Gallery Connection
   // flush_it();
	GalleryEmbed::done();
}
Posts: 32509
call ::init once for each http request. no less no more.