Links open inside random block rather than main frame

cyberclark

Joined: 2002-10-30
Posts: 8
Posted: Sun, 2002-11-03 07:36

Hello all,

I posted this before in <!-- BBCode Start --><A HREF="http://gallery.menalto.com/modules.php?op=modload&amp;name=phpBB_14&amp;file=index&amp;action=viewtopic&amp;topic=903&amp;4" TARGET="_blank">this thread</A><!-- BBCode End --> but I got no answers so I figured I would try again.

At my site, <!-- BBCode Start --><A HREF="http://www.cyberclark.com/community/" TARGET="_blank">CyberClark.com</A><!-- BBCode End --> I have installed Nuke 6.0, <!-- BBCode Start --><A HREF="http://bbtonuke.sourceforge.net" TARGET="_blank">Tom's bbtonuke port</A><!-- BBCode End -->, and of course Gallery.

I installed Bahrat's Random Block code and it all worked fine in stand alone mode but for some reason when I put into a block just using standard .php code it gave me SQL errors for some reason. So per the advice given in the previous thread that I mentioned I put it in an iframe in the block and low and behold it worked, it shows a random image for me. But the one drawback is now when a user clicks on that random image or the links in that block rather than opening in the main frame they open in that same block :???:

I know in the html world I could just add a target=_main somewhere to get my content to go where I want it but when I try to add that to the href's I see in the .php I get SQl Parsing errors. Follows is the code for my block as well as the block-random.php, if any of you could please take a few minutes to look though this and let me know how I could possibly fix it I would be eternally grateful.

Here is the Block Code (the only change I made from how it was downloaded was to change the $rebuild=0 to 1 because I was having troubles with the cache.
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>
<iframe src="http://www.cyberclark.com/community/blocks/block-Random.php"" width="150" height="300" frameborder="0" scrolling="no">
</iframe>
</TD></TR></TABLE><!-- BBCode End -->

and here is the block-random.php
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>
<?
/*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2001 Bharat Mediratta
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

/*
* This block selects a random photo for display. It will only display photos
* from albums that are visible to the public. It will not display hidden
* photos.
*
* Once a day (or whatever you set CACHE_EXPIRED to) we scan all albums and
* create a cache file listing each public album and the number of photos it
* contains. For all subsequent attempts we use that cache file. This means
* that if you change your albums around it may take a day before this block
* starts (or stops) displaying them.
*/

// Hack prevention.
if (!empty($HTTP_GET_VARS["GALLERY_BASEDIR"]) ||
!empty($HTTP_POST_VARS["GALLERY_BASEDIR"]) ||
!empty($HTTP_COOKIE_VARS["GALLERY_BASEDIR"])) {
print "Security violation
";
exit;
}

require($GALLERY_BASEDIR . "init.php");

if ($profile) {
$timer = time();
}

/* Initializing the seed */
srand ((double) microtime() * 1000000);

define(CACHE_FILE, $gallery->app->albumDir . "/block-random.cache");
define(CACHE_EXPIRED, 86400);

// Check the cache file to see if it's up to date
$rebuild = 1;
if (fs_file_exists(CACHE_FILE)) {
$stat = fs_stat(CACHE_FILE);
$mtime = $stat[9];
if (time() - $mtime < CACHE_EXPIRED) {
$rebuild = 1;
}
}

if ($rebuild) {
scanAlbums();
saveCache();
} else {
readCache();
}

$album = chooseAlbum();

if ($album) {
$index = choosePhoto($album);
}

if (isset($index)) {
$id = $album->getPhotoId($index);
echo ""
."<a href=" .makeAlbumUrl($album->fields["name"], $id) .">"
.$album->getThumbnailTag($index)
."</a>";

$caption = $album->getCaption($index);
if ($caption) {
echo "<br><center>$caption</center>";
}

echo "<br><center>From: "
."<a href=" .makeAlbumUrl($album->fields["name"]) .">"
.$album->fields["title"]
."</a></center>";
} else {
print "No photo chosen.";
}

if ($profile) {
$elapsed = time() - $timer;
print "<br>Elapsed: $elapsed secs";
}

/*
* --------------------------------------------------
* Support functions
* --------------------------------------------------
*/

function saveCache() {
global $cache;
if ($fd = fs_fopen(CACHE_FILE, "w")) {
foreach ($cache as $key => $val) {
fwrite($fd, "$key/$val
");
}
fclose($fd);
}
}

function readCache() {
global $cache;
if ($fd = fs_fopen(CACHE_FILE, "r")) {
while ($line = fgets($fd, 4096)) {
list($key, $val) = explode("/", $line);
$cache[$key] = $val;
}
fclose($fd);
}
}

function choosePhoto($album) {
global $cache;

$count = $cache[$album->fields["name"]];
if ($count == 0) {
// Shouldn't happen
return null;
} else if ($count == 1) {
$choose = 1;
} else {
$choose = rand(1, $count);
$wrap = 0;
if ($album->isHidden($choose)) {
$choose++;
if ($choose > $album->numPhotos(1)) {
$choose = 1;
$wrap++;

if ($wrap = 2) {
return null;
}
}
}
}

return $choose;
}

function chooseAlbum() {
global $cache;

/*
* The odds that an album will be selected is proportional
* to the number of (visible) items in the album.
*/

$total = 0;
foreach ($cache as $name => $count) {
if (!$choose) {
$choose = $name;
}

$total += $count;
if ($total != 0 &amp;&amp; ($total == 1 || rand(1, $total) <= $count)) {
$choose = $name;
}
}

if ($choose) {
$album = new Album();
$album->load($choose);
return $album;
} else {
return null;
}
}

function scanAlbums() {
global $cache;
global $gallery;

$cache = array();
$everybody = $gallery->userDB->getEverybody();
$albumDB = new AlbumDB();
foreach ($albumDB->albumList as $tmpAlbum) {
if ($everybody->canReadAlbum($tmpAlbum)) {
$seeHidden = $everybody->canWriteToAlbum($tmpAlbum);
$numPhotos = $tmpAlbum->numPhotos($seeHidden);
$name = $tmpAlbum->fields["name"];
if ($numPhotos > 0) {
$cache[$name] = $numPhotos;
}
}
}
}
?>
</TD></TR></TABLE><!-- BBCode End -->

Thanks in advance for any information or advice I know you are all busy with your own projects but this is the one last thing I really want to fix before I start to publicize my site to my family and friends and start adding all the pictures I have planned for my gallery.

 
joerg
joerg's picture

Joined: 2002-10-12
Posts: 77
Posted: Sun, 2002-11-03 19:25

Hi,

I found this one:

Quote:
If a link in a document displayed in an inline frame is followed, normal rules apply so that the default target (where the linked document is shown) is that inline frame. This can be overridden using the target attribute; in particular, target="_top" means that the linked document is to be opened in the full window. Note that the default target for links in a document can be set using the base element, e.g.
<base target="_top">
which generally acts as partial "protection against framing".

For the random-block this means that the link has to be modified. The corresponding code section is:

<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>$id = $album->getPhotoId($index);
echo ""
."<a href=" .makeAlbumUrl($album->fields["name"], $id) .">"
.$album->getThumbnailTag($index)
."</a>";
$caption = $album->getCaption($index);
if ($caption) {
echo "<br><center>$caption</center>";
}
echo "<br><center>From: "
."<a href=" .makeAlbumUrl($album->fields["name"]) .">"
.$album->fields["title"]
."</a></center>";</TD></TR></TABLE><!-- BBCode End -->
Try to add target="_top" to the href's like in this example (both href's have to be edited):

<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>."<a href=" .makeAlbumUrl($album->fields["name"]) ." target="_top">"</TD></TR></TABLE><!-- BBCode End -->
Does this help?

Joerg

 
cyberclark

Joined: 2002-10-30
Posts: 8
Posted: Sun, 2002-11-03 21:50

Thank you so very much Joerg!!! I knew that it had to be a target command I just must have had the wrong syntax in the .php I'm so new to the php world I don;t know when I need a / or . or " in the code yet :grin:

I greatly appreciate your help!!!