sessions and cookies

gsrtek

Joined: 2007-09-09
Posts: 20
Posted: Fri, 2007-11-30 01:40

In my application more often than not a user will enter parts of the site where I have gallery2 images embedded.

I use the following code:

require_once("$required_pathToGallery2/embed.php");

// URI to Gallery2
$gallery2Uri = "$required_UriToGallery2/main.php";

// Get the current Joomla User
$embedUserId = $getUser();

$error = GalleryEmbed::init(array(
'g2Uri' => $gallery2Uri,
'fullInit' => true,
'activeUserId' => $embedUserId
)
);

// todo-> some way better error trapping
if($error) {
print_r($error);
}

// Start a Gallery 2 session
$session = $gallery->getSession();
$session->start();

Although the documentation states that $session->start() will take care of creating the cookie, it does not seem to.

I have discovered through trial and error specifically calling GalleryEmbed::login($embedUserId) prior to session start will sometimes create the cookie.

If a user enters the actual gallery2 and clicks a link within the main gallery the cookie is created.

My problem is that because of the way my application works more than likely the user will not enter the gallery before clicking on an image some other place in site.

I have no problem if I set all my images permissions to Everyone or Registered Users "core can view all".

This is not what I want I have close to 20 groups in Gallery2 and the embedded images must conform to the permissions set in Gallery2. All works fine in Gallery2 itself it is just in the embedded images.

After much experimentation I have determined the problem to be what I stated at the beginning the cookie is not being created, and since I do not append the gallerySID to my /2.5/gallery2/main.php?g2_view=core.DownloadItem&g2_itemId=22 I need the cookie to be in place.

Any ideas or help is greatly appreciated.

Thanks
Gary

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Sun, 2007-12-09 22:59

don't forget to commit your transaction.

GalleryEmbed::done() does just that.

instead of all that code, you might also want to use GalleryEmbed::login().

--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage

 
gsrtek

Joined: 2007-09-09
Posts: 20
Posted: Sun, 2007-12-09 23:26

Thanks for the help I created the following php5 code that works just fine. In my case since I have gallery2 images embedded all over the site it made sense to always initialize gallery2 thus the $_MAMBOTS->registerFunction( 'onAfterStart', 'gTekInit' );

This ensures gallery2 is always initialized (although I am still confused of what seems to erase the cookie) the following code works fine and since I am no Gallery2 or Joomla expert it can probably be improved:

$_MAMBOTS->registerFunction( 'onAfterStart', 'gTekInit' );

class Gtek_IniFileParser {

private $iniFileData;

public function __construct($fileName, $parseEntireFile = false) {
$this->iniFileData = parse_ini_file($fileName, $parseEntireFile);
}

public function __get($key) {
if(array_key_exists($key, $this->iniFileData)) {
return $this->iniFileData[$key];
}
}
}

class gTekGallery2JoomlaEmbed {

public $id;
public $link;
private $database;

private static $instance = null;

private function __construct($joomlaUserId, $database) {

$this->database = $database;

$iniParser = new Gtek_IniFileParser('gsrTekConfig.ini');
//print "{$iniParser->pathToGallery2} </br> {$iniParser->pathToJoomla} </br> {$iniParser->uriToJoomla}";

// Include the Gallery2 Embed API

require_once("{$iniParser->pathToJoomla}/{$iniParser->pathToGallery2}/embed.php");

// URI to Gallery2
$gallery2Uri = "{$iniParser->uriToJoomla}/{$iniParser->pathToGallery2}/main.php";
$embedUri = $iniParser->uriToJoomla . $this->getEmbedUri();

// Intialize Gallery2
// using of course the current Joomla User
$error = GalleryEmbed::init(array(
'g2Uri' => $gallery2Uri,
'embedUri' => $embedUri,
'fullInit' => false,
'activeUserId' => $joomlaUserId,
'apiVersion' => array(1,0)
)
);

// todo-> some way better error trapping
if($error) {
print_r($error);
}

$session = $gallery->getSession();
$session->start();

GalleryEmbed::done();
}

private function getEmbedUri() {

$query = "SELECT id, link"
. "\n FROM #__menu"
. "\n WHERE link = 'index.php?option=com_gsrTekGallery2JoomlaEmbed'"
. "\n AND published = '1'";
$this->database->setQuery($query);
$this->database->loadObject($this);

$uri .= '/' . $this->link . '&Itemid=' . $this->id;
return $uri;

}

private function __clone() {}

public static function getInstance($joomlaUserId = null, $database = null) {

if (is_null(self::$instance)) {
self::$instance = new gTekGallery2JoomlaEmbed($joomlaUserId, $database);
}

return self::$instance;
}

}

function gTekInit() {

global $mainframe, $database;

$joomlaUser = $mainframe->getUser();
$gallery2 = gTekGallery2JoomlaEmbed::getInstance($joomlaUser->id, $database);
}
?>

Since I use an ini file to get my info here it is

pathToGallery2 = gallery2
pathToJoomla = "c:/www/2.5"
uriToJoomla = "http://localhost/2.5"

I think what you are saying is if I use the GalleryEmbed::done(); I do not need to use session->start() is this correct. I ran into problems unless I specifically called session start.

I really love gallery2 and the amount of features is mind boggling but the more I get into using it the more I realize the power of it. My only complaint would be the documentation (not the content) that is fine, I just prefer something more javadoc like. No big deal I am use to it now.

Thanks for your help and for a great product if you have any suggestions on the above let me know.

Gary

PS This is probably in the wrong place (the post) but I did not want to start another thread