JSON Feed using cpmFetch JSON Feed using cpmFetch
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

JSON Feed using cpmFetch

Started by PeterLawrence, January 03, 2009, 09:53:02 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

PeterLawrence

I recall from a previous post that Vuud was considering including a JSON feed in Cpmfetch.
However, since I've been using a Javascript that works with JSON feeds from Google's Picasa I thought I would have a go at getting a similar JSON feed output from Coppermine. So after playing with the PHP script  Get RSS Feeds from Coppermine now!. I came up with the following experimental JSON PHP code. The code below generates a JSON feed from Coppermine based on the Picasa's JSON feed format http://code.google.com/apis/gdata/json.html
Code for cpgjson.php

<?php
//  Coppermine JSON 2.0 Generator (1.0)
//  Based on Picasa JSON feed format see http://code.google.com/apis/gdata/json.html
//  Peter J Lawrence (December 2008)
//  Based on code from Coppermine RSS 2.0 Generator (1.0) by Mohammad Hafiz bin Ismail 

$upperlimit 6//default = 6

define('INC_COMMENT_TIMES'false);
define('FILEDUMP'false);
define('GZ_COMPRESS'false);
// [0.0 <= priority <= 1.0]
define('P_DISPLAYIMAGE'0.5);
define('P_ALBUM'0.5);
define('P_CATEGORY'0.5);

// [changefreq = always || hourly || daily || weekly || monthly || yearly || never]
define('CF_DISPLAYIMAGE''unspecified');
define('CF_ALBUM''unspecified');
define('CF_CATEGORY''unspecified');

define('IN_COPPERMINE'true);
require(
'include/init.inc.php');

// This should work as it is, but hardcode if necessary.
define('CPG14'version_compare(COPPERMINE_VERSION"1.4.0"">="));
define('PHP5'version_compare(phpversion(), "5"">="));
$base rtrim($CONFIG['ecards_more_pic_target'], '/');
$albumpath "$base/" $CONFIG['fullpath'];

function 
lmdate($timestamp)
{
if (PHP5){
return date('c'$timestamp);
} else {
return date('D, d M Y H:i:s +0800'$timestamp date('Z'));
}
}
ob_flush();
if (isset(
$_GET['callback']))
{
print ($_GET['callback'])."(";
}
header('Content-type: application/x-json');
print 
"{\n";
print 
" \"version\":\"1.0\",\n";
print 
" \"encoding\":\"UTF-8\",\n";
print 
" \"feed\":{\n";

$TheAlbum=null;
if (isset(
$_GET['album']))
{
$TheAlbum = (int) $_GET['album'];
}
if (isset(
$TheAlbum))
{
// find current album
$result mysql_query("SELECT aid, title, description FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid=$TheAlbum");
}
else
{
// find the album last uploaded to
$result mysql_query("SELECT aid, title, description FROM {$CONFIG['TABLE_ALBUMS']} ORDER BY title ASC LIMIT 
0,1"
);
}
while (list(
$aaid$atitle$adescription) = mysql_fetch_row($result)) 
{
if (isset($TheAlbum))
{
$result2 mysql_query("SELECT pid, aid, caption, ctime, title, keywords, filepath, filename, owner_name, pwidth, pheight FROM {$CONFIG['TABLE_PICTURES']} WHERE aid=$aaid ORDER BY ctime DESC, pid DESC");
}
else
{
// set atitle and adescription
$atitle="Recent uploads";
$adescription="Recently uploaded images";
// find the last $upperlimit uploaded images
$result2 mysql_query("SELECT pid, aid, caption, ctime, title, keywords, filepath, filename, owner_name, pwidth, pheight FROM {$CONFIG['TABLE_PICTURES']} ORDER BY ctime DESC, pid DESC LIMIT 
0,
$upperlimit");
}
$result_count=mysql_num_rows($result2);
print ' "id":{'."\n ".'"$t":"'.$base."\"},\n";
print ' "updated":{'."\n ".'"$t":"'.lmdate(time())."\"},\n";
print ' "title":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.$atitle.'"},'."\n";
print ' "subtitle":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":""},'."\n";
print ' "link":'."\n";
print ' ['."\n";
print ' { "rel":"'.$base.'",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'.$base.'"}'."\n";
    if (isset(
$TheAlbum))
{
print ' ,{ "rel":"alternate",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'.$base.'/thumbnails.php?album='.$TheAlbum.'"}'."\n";
}
print ' ],'."\n";
print ' "author":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "name":{'."\n";
print ' "$t":"Unkown"},'."\n";
print ' "uri":{'."\n";
print ' "$t":"'.$base.'"}'."\n";
print ' }'."\n";
print ' ],'."\n";
print ' "generator":{'."\n";
print ' "version":"1.00",'."\n";
print ' "uri":"'.$base.'",'."\n";
print ' "$t":"Coppermine JSON Generator (1.0)"},'."\n";
print ' "openSearch$totalResults":{'."\n";
print ' "$t":"'.$result_count.'"},'."\n";
print ' "openSearch$startIndex":{'."\n";
print ' "$t":"1"},'."\n";
print ' "openSearch$itemsPerPage":{'."\n";
print ' "$t":"1000"},'."\n";
print ' "georss$where":{'."\n";
print ' "gml$Point":{'."\n";
print ' "gml$pos":{'."\n";
print ' "$t":"51.50199 -0.118955"}'."\n";
print ' }'."\n";
print ' },'."\n";

print ' "entry":'."\n";
print ' ['."\n";
$count-0;
while (list($pid$aid,$desc$ctime$title$keywords,$filepath,$filename$owner$width$height) = mysql_fetch_row($result2)) 
{
$count++;
print ' {'."\n"// start of entry
print ' "id":{'."\n";
print ' "$t":"'."$base/displayimage.php?pos=-$pid".'"},'."\n";
print ' "published":{'."\n";
print ' "$t":"'lmdate($ctime) .'"},'."\n";
print ' "updated":{'."\n";
print ' "$t":"'lmdate($ctime) .'"},'."\n";
print ' "title":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.$title.'"},'."\n";
print ' "summary":{'."\n";
print ' "type":"html",'."\n";
print ' "$t":"';
echo addslashes($desc);
print '"},'."\n";
print ' "content":{'."\n";
print ' "type":"image/jpeg",'."\n";
print ' "src":"'.$albumpath.$filepath.$filename.'"},'."\n";
print ' "link":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "rel":"'."$base/displayimage.php?pos=-$pid".'",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'."$base/displayimage.php?pos=-$pid".'"'."\n";
print ' }'."\n";
print ' ],'."\n";

print ' "media$group":{'."\n";
print ' "media$title":{'."\n";
print ' "type":"plain",'."\n";
print ' "$t":"'.$filename.'"},'."\n";
print ' "media$description":{'."\n";
print ' "type":"plain",'."\n";
print ' "$t":"'.addslashes($desc).'"},'."\n";
print ' "media$keywords":{'."\n";
print ' "$t":"';
echo addslashes("$keywords");
print '"},'."\n";
print ' "media$content":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "url":"'."$albumpath$filepath$filename".'",'."\n";
print ' "height":"'.$height.'",'."\n";
print ' "width":"'.$width.'",'."\n";
print ' "type":"image/jpeg",'."\n";
print ' "medium":"image"}'."\n";
print ' ],'."\n";
print ' "media$thumbnail":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "url":"'."$albumpath$filepath"."thumb_$filename".'",'."\n";
print ' "height":"80",'."\n";
print ' "width":"100"}'."\n";
print ' ],'."\n";
print ' "media$credit":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "$t":"'.$owner.'"}'."\n";
print ' ]'."\n";
print ' }'."\n";
if ($count==$result_count)
{
print ' }'."\n"// end of entry last one
}
else
{
print ' },'."\n"// end of entry more to come
}
}
print ' ]'."\n"// entry
}
print 
" }\n"// end feed

if (isset($_GET['callback']))
{
print "});\n"// end opening function
}
else
{
print "}\n"// end opening {
}
?>


The about code takes a parameter album that specifies the album to generate the feed for plus a callback parameter.
If the album parameter is not specified then the last six uploaded images are returned.
Since a JSON feed is not much use without some Javascript code that processes it. I've also attached to this post the Javascript file which I use to embed an album in an html page.
Below is a simple html example which uses the attached java code, which places a Coppermine album at one div location jsondiv1 and a Picasa album at a second div location jsondiv2.

<script type="text/javascript" src="jsonbrowser.js"></script>
<body>
<div id="jsondiv1"></div>
<hr />
<div id="jsondiv2"></div>
<script language="javascript"  type="text/javascript">
function DisplayTheAlbumDiv1(j)
{
    var htmldivtag="jsondiv1";
    g_JSONFeedArray[htmldivtag]=j;
    DisplayAlbum(htmldivtag,0);
}
function DisplayTheAlbumDiv2(j)
{
    var htmldivtag="jsondiv2";
    g_JSONFeedArray[htmldivtag]=j;
    DisplayAlbum(htmldivtag,0);
}
</script>
<script type="text/javascript" src="/cpg/cpgjson.php?album=1&callback=DisplayTheAlbumDiv1"></script>
<script type="text/javascript" src="http://picasaweb.google.com/data/feed/api/user/pearson.dj/albumid/5238947919204339937?category=photo&alt=json&callback=DisplayTheAlbumDiv2"></script>
</body>


For each div statement you need to generate a Javascript function which is called once the JSON feed returns. This user function add the JSON feed data to the g_JSONFeedArray and then calls the DisplayAlbum, see functions DisplayTheAlbumDiv1 and DisplayTheAlbumDiv2 in the above html code.

The reason for using a JSON feed rather than an RSS feed is because JSON is a subset of Javascript programming language, hence tends to be better supported by more browsers. I've also developed a similar Javascript application which works with RSS feeds but found problems with various browsers depending on how XMLHttpRequest was supported.

PeterLawrence

#1
I've updated the above php code. The revised version fixes a bug relating to newline characters being outputted in some strings.
Also it now outputs the album description in the subtitle field.
Note that this php file, cpgjson.php, should be placed in your Coppermine directory.

<?php
//  Coppermine JSON Generator (1.01)
//  Based on Picasa JSON feed format see http://code.google.com/apis/gdata/json.html
//  Peter J Lawrence (December 2008)
//  Based on code from Coppermine RSS 2.0 Generator (1.0) by Mohammad Hafiz bin Ismail 

$upperlimit 6//default = 6

define('INC_COMMENT_TIMES'false);
define('FILEDUMP'false);
define('GZ_COMPRESS'false);
// [0.0 <= priority <= 1.0]
define('P_DISPLAYIMAGE'0.5);
define('P_ALBUM'0.5);
define('P_CATEGORY'0.5);

// [changefreq = always || hourly || daily || weekly || monthly || yearly || never]
define('CF_DISPLAYIMAGE''unspecified');
define('CF_ALBUM''unspecified');
define('CF_CATEGORY''unspecified');

define('IN_COPPERMINE'true);
require(
'include/init.inc.php');

// This should work as it is, but hardcode if necessary.
define('CPG14'version_compare(COPPERMINE_VERSION"1.4.0"">="));
define('PHP5'version_compare(phpversion(), "5"">="));
$base rtrim($CONFIG['ecards_more_pic_target'], '/');
$albumpath "$base/" $CONFIG['fullpath'];

function 
FormatOutput($aString)
{
    
// removes returns and replaces them with html br
    // also add slashes 
    
return (preg_replace("/\\r\\n|\\n\\r|\\n|\\r/""<br />",addslashes($aString)));
}

function 
lmdate($timestamp)
{
if (PHP5){
return date('c'$timestamp);
} else {
return date('D, d M Y H:i:s +0800'$timestamp date('Z'));
}
}
ob_flush();
if (isset(
$_GET['callback']))
{
print ($_GET['callback'])."(";
}
header('Content-type: application/x-json');
print 
"{\n";
print 
" \"version\":\"1.0\",\n";
print 
" \"encoding\":\"UTF-8\",\n";
print 
" \"feed\":{\n";
//cpg_db_connect(); // uncomment this line if you're having problems connecting to the database
$TheAlbum=null;
if (isset(
$_GET['album']))
{
$TheAlbum = (int) $_GET['album'];
$result mysql_query("SELECT aid, title, description FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid=$TheAlbum");
}
else
{
// find the album last uploaded to
$result mysql_query("SELECT aid, title, description FROM {$CONFIG['TABLE_ALBUMS']} ORDER BY title ASC LIMIT 
0,1"
);
}
while (list(
$aaid$atitle$adescription) = mysql_fetch_row($result)) 
{
if (isset($TheAlbum))
{
$result2 mysql_query("SELECT pid, aid, caption, ctime, title, keywords, filepath, filename, owner_name, pwidth, pheight FROM {$CONFIG['TABLE_PICTURES']} WHERE aid=$aaid ORDER BY ctime ASC, pid ASC");
}
else
{
// set atitle and adescription
$atitle="Recent uploads";
$adescription="Recently uploaded images";
// find the last $upperlimit uploaded images
$result2 mysql_query("SELECT pid, aid, caption, ctime, title, keywords, filepath, filename, owner_name, pwidth, pheight FROM {$CONFIG['TABLE_PICTURES']} ORDER BY ctime DESC, pid DESC LIMIT 
0,
$upperlimit");
}
$result_count=mysql_num_rows($result2);
print ' "id":{'."\n ".'"$t":"'.$base."\"},\n";
print ' "updated":{'."\n ".'"$t":"'.lmdate(time())."\"},\n";
print ' "title":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.FormatOutput($atitle).'"},'."\n";
print ' "subtitle":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.FormatOutput($adescription).'"},'."\n";
print ' "link":'."\n";
print ' ['."\n";
print ' { "rel":"'.$base.'",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'.$base.'"}'."\n";
    if (isset(
$TheAlbum))
{
print ' ,{ "rel":"alternate",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'.$base.'/thumbnails.php?album='.$TheAlbum.'"}'."\n";
}
print ' ],'."\n";
print ' "author":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "name":{'."\n";
print ' "$t":"Unkown"},'."\n";
print ' "uri":{'."\n";
print ' "$t":"'.$base.'"}'."\n";
print ' }'."\n";
print ' ],'."\n";
print ' "generator":{'."\n";
print ' "version":"1.00",'."\n";
print ' "uri":"'.$base.'",'."\n";
print ' "$t":"Coppermine JSON Generator (1.01)"},'."\n";
print ' "openSearch$totalResults":{'."\n";
print ' "$t":"'.$result_count.'"},'."\n";
print ' "openSearch$startIndex":{'."\n";
print ' "$t":"1"},'."\n";
print ' "openSearch$itemsPerPage":{'."\n";
print ' "$t":"1000"},'."\n";
print ' "georss$where":{'."\n";
print ' "gml$Point":{'."\n";
print ' "gml$pos":{'."\n";
print ' "$t":"51.50199 -0.118955"}'."\n";
print ' }'."\n";
print ' },'."\n";

print ' "entry":'."\n";
print ' ['."\n";
$count-0;
while (list($pid$aid,$desc$ctime$title$keywords,$filepath,$filename$owner$width$height) = mysql_fetch_row($result2)) 
{
$count++;
print ' {'."\n"// start of entry
print ' "id":{'."\n";
print ' "$t":"'."$base/displayimage.php?pos=-$pid".'"},'."\n";
print ' "published":{'."\n";
print ' "$t":"'lmdate($ctime) .'"},'."\n";
print ' "updated":{'."\n";
print ' "$t":"'lmdate($ctime) .'"},'."\n";
print ' "title":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.FormatOutput($title).'"},'."\n";
print ' "summary":{'."\n";
print ' "type":"html",'."\n";
print ' "$t":"';
//echo nl2br(addslashes($desc));
echo  FormatOutput($desc);
print '"},'."\n";
print ' "content":{'."\n";
print ' "type":"image/jpeg",'."\n";
print ' "src":"'.$albumpath.$filepath.$filename.'"},'."\n";
print ' "link":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "rel":"'."$base/displayimage.php?pos=-$pid".'",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'."$base/displayimage.php?pos=-$pid".'"'."\n";
print ' }'."\n";
print ' ],'."\n";

print ' "media$group":{'."\n";
print ' "media$title":{'."\n";
print ' "type":"plain",'."\n";
print ' "$t":"'.$filename.'"},'."\n";
print ' "media$description":{'."\n";
print ' "type":"plain",'."\n";
print ' "$t":"'.FormatOutput($desc).'"},'."\n";
print ' "media$keywords":{'."\n";
print ' "$t":"';
echo FormatOutput("$keywords");
print '"},'."\n";
print ' "media$content":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "url":"'."$albumpath$filepath$filename".'",'."\n";
print ' "height":"'.$height.'",'."\n";
print ' "width":"'.$width.'",'."\n";
print ' "type":"image/jpeg",'."\n";
print ' "medium":"image"}'."\n";
print ' ],'."\n";
print ' "media$thumbnail":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "url":"'."$albumpath$filepath"."thumb_$filename".'",'."\n";
print ' "height":"80",'."\n";
print ' "width":"100"}'."\n";
print ' ],'."\n";
print ' "media$credit":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "$t":"'.$owner.'"}'."\n";
print ' ]'."\n";
print ' }'."\n";
if ($count==$result_count)
{
print ' }'."\n"// end of entry last one
}
else
{
print ' },'."\n"// end of entry more to come
}
}
print ' ]'."\n"// entry
}
print 
" }\n"// end feed

if (isset($_GET['callback']))
{
print "});\n"// end opening function
}
else
{
print "}\n"// end opening {
}
?>



In addition I've attached another Javascript file which uses the EnlargeIt script to embed an album in an html page.
This Javascript should work OK if placed in the root folder. Note you'll need to login to see the attachment.
If there is a problem you should check the directory paths specified in test.html and
enlargeit_source.js.

PeterLawrence

I been busy redeveloping the original PHP JSON code, above, to try and turn it into more of a JSON/RSS feed API.
Part of this work has been achieved by using the Coppermine function get_pic_data rather than a direct SQL queries.

The new code now generates either a MediaRSS or JSON feeds so many of my original PHP and Javascripts, which I originally used with Picasa (or other media feed website), now interface successfully with Coppermine.

This PHP script can take the following parameters..

format

  • mrss2 generate a media feed (default)
  • json - generate a JSON data feed

album

  • integer id - Coppermine album
  • lastcom - last comments
  • lastcomby - last comments by a specific user add parameter uid=user id
  • lastup - last upload
  • lastupby - last uploaded by specific user
  • topn - most viewed
  • toprated - top rated
  • lasthits - last viewed
  • random - random pictures
  • search lastalb - last albums to which uploads
  • favpics - favourite pictures

cat can be assign either..

  • integer id - category id

start-index

  • integer - Index of the first result to be retrieved

max-results

  • integer - Maximum number of results to be retrieved
  • all - all images (only recognised if an album id is specified)

sort specifies image order

  • na - filename ascending
  • nd - filename descending
  • ta - title ascending
  • td - title descending
  • da - pid ascending
  • dd - pid descending
  • ps - position ascending
  • pd - position descending

callback -- specifies a Javascript callback function for given JSON feed

For example
xrss.php?album=1&format=json&sort=dd&callback=DisplayTheAlbumDiv1
will generate an JSON feed for album 1 with images in a descending order using the callback function DisplayTheAlbumDiv1
xrss.php?album=2&max-results=all
will generate a media rss feed of all images in album 2.


<?php 
//  Coppermine Photo Gallery - XRSS Feed
//  Peter J Lawrence (Feb 2009)
//   Based on code by Mohammad Hafiz, Brent Gerig, Christophe (cly) 
//   and many others who have contributed PHP code for data feeds on this forum        
//
//  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.
//
define('IN_COPPERMINE'true);
define('INDEX_PHP'true);

global 
$USER,$CONFIG,$ALBUM_SET,$META_ALBUM_SET,$CURRENT_CAT_NAME,$FORBIDDEN_SET_DATA;
require(
'include/init.inc.php');

// lang stuff
$lang_plugin_xrss = array(
  
'label_title'   => 'Title',
  
'views'         => 'views',
);

define('CPG14'version_compare(COPPERMINE_VERSION"1.4.0"">="));
define('PHP5'version_compare(phpversion(), "5"">="));

//Default numebr of items  to show in RSS feed
$thumb_per_page 10;

$thumb_count 0;
$lower_limit 0;
$feed_description="";
$allphotos=FALSE;

//cpg_db_connect(); // uncomment this line if you're having problem connecting to the database

if(isset($_GET['max-results']))
{
if (is_numeric($_GET['max-results']))
{
$thumb_per_page = (int) $_GET['max-results'];
}
else if ($_GET['max-results']=='all')
{
$allphotos=TRUE;
}
}

if (isset(
$_GET['start-index']))
{
$lower_limit = (int) $_GET['start-index'];
$thumb_per_page+=$lower_limit;
}

if(isset(
$_GET['sort'])){
$USER['sort']=$_GET['sort'];
}

if(
count($FORBIDDEN_SET_DATA) > ){
    
$forbidden_set_string =" AND aid NOT IN (".implode(","$FORBIDDEN_SET_DATA).")";
} else {
    
$forbidden_set_string '';
}

if(isset(
$_GET['album'])){
    
$album $_GET['album'];
}

//If it is a numeric album get the name and set variables
if ((is_numeric($album))){
     
$album_name_keyword get_album_name($album);
     
$CURRENT_CAT_NAME $album_name_keyword['title'];
     
$META_ALBUM_SET "AND aid IN (".(int)$album.")".$ALBUM_SET;
 
 $AlbumData mysql_query("SELECT description FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid=$album");
 $count mysql_num_rows($AlbumData );
 if ($count>0)
 {
$row mysql_fetch_array($AlbumData);
$feed_description=$row['description'];
 }
 if ($allphotos==FALSE && !isset($_GET['sort']))
 {
     //Set the album to last uploaded
     $album 'lastup';
 }
}

//If the album is not set set it to lastup - this is the default
if(!isset($album)){
     
$album 'lastup';
}

if ((isset(
$_GET['cat']) && $_GET['cat'] > 0)){ 
     
$cat = (int) $_GET['cat'];
     
$album_name_keyword xrss_get_cat_name($cat);
     
$CURRENT_CAT_NAME $album_name_keyword['name'];
     
get_meta_album_set($cat,$META_ALBUM_SET);
}

if ((isset(
$_GET['cat']) && $_GET['cat'] < 0)){ 
     
$cat = (int) $_GET['cat'];
     
$album_name_keyword get_album_name(-$cat);
     
$CURRENT_CAT_NAME $album_name_keyword['title'];
     
$META_ALBUM_SET "AND aid IN (".-$cat.")".$ALBUM_SET;
}

//Changes these to point to your site if the following is not giving correct results.
$base rtrim($CONFIG['ecards_more_pic_target'], '/');
$albumpath "$base/" $CONFIG['fullpath'];
$link_url $base."/displayimage.php?pos=-";
$image_url $base."/albums/";-

$gallery_name=$CONFIG['gallery_name'];

$META_ALBUM_SET .= $forbidden_set_string;

$data get_pic_data($album$thumb_count$album_name$lower_limit$thumb_per_page);

$feed_title=$gallery_name.": ".$album_name;
if (
strlen($feed_description)==0)
{
$feed_description="$CONFIG[gallery_description]";
}

$feedtype="basic";
if (isset(
$_GET['format']))
{
    
$feedtype $_GET['format'];
}

switch (
$feedtype)
{
default:
case "mrss2":
MediaRSS2();
break;
case "json":
JSONFeed();
break;
}
//cpg_debug_output();

function xrss_get_cat_name($cid)
{
global $CONFIG;
global $lang_errors;

$result cpg_db_query("SELECT name from {$CONFIG['TABLE_CATEGORIES']} WHERE cid='$cid'");
$count mysql_num_rows($result);
if ($count 0) {
$row mysql_fetch_array($result);
return $row;
} else {
cpg_die(ERROR$lang_errors['non_exist_ap'], __FILE____LINE__);
}
}

// Routines for Media RSS feed

function getmime ($Filename) {
    
$mimetype="text/plain";
    
// make an array, each value seperated by a period (.)
    
$Extension explode ("."$Filename);

    
// Count how many are in array, and -1 due to
    // php starting an array with 0
    
$Extension_i = (count($Extension) - 1);

    
// Return it..
    
    
$ext $Extension[$Extension_i];
    
    if (
$ext == "jpg") {
       
$mimetype="image/jpeg";      
    } else if (
$ext == "png") {
       
$mimetype="image/png";
    } else if ( 
$ext == "gif") {
      
$mimetype="image/gif";
    }
    return 
$mimetype;


function 
lmdate($timestamp)
{
if (PHP5){
return date('c'$timestamp);
} else {
return date('D, d M Y H:i:s +0800'$timestamp date('Z'));
}
}

function 
MediaRSS2()
{
global $CONFIG;
global $lang_no,$lang_plugin_xrss;

global $base,$link_url,$image_url;
global $data,$album;
global $result,$base,$albumpath;
    global 
$feed_title,$feed_description;
    
//$feed_description=htmlspecialchars(bb_decode($feed_description));

//header("Content-type: text/xml; charset=utf-8");
    
header ("content-type: text/xml");
// was UTF-8
    
print "<?xml version=\"1.0\" encoding=\"iso-8859-7\"?>
\n";
print "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"";
print " xmlns:photo=\"http://www.pheed.com/pheed/\" xmlns:media=\"http://search.yahoo.com/mrss\" >";
print "<channel>\n";
print "<title>$feed_title</title>\n";
    if (is_numeric($album))
    {
        print "<link>$base/thumbnails.php?album=$album</link>\n";
    }
    else
    {
        print "<link>$base</link>\n";
    }
print "<description>".$feed_description."</description>";
print "<lastBuildDate>"  . lmdate(time()) . "</lastBuildDate>\n";
print "<generator>$base/xrss.php</generator>\n";

    foreach($data AS $picture)
{
    $thumb_url = "$image_url$picture[filepath]$CONFIG[thumb_pfx]$picture[filename]";
$imag_url  = "$image_url$picture[filepath]$picture[filename]";

$caption_text = "<br />".$picture['hits']." ";
        $caption_text .= $lang_plugin_xrss['views'];
        $adescription = bb_decode($picture['caption']);
        $fulldescription = $adescription;
        $fulldescription = '<a href="' . $link_url . $picture['pid'] . '"><img src="' . $thumb_url . '" border="1" vspace="2" hspace="2"> <align="center" ></a><br />' . $adescription; 
        $fulldescription .= bb_decode($caption_text);   
    $fulldescription =  htmlspecialchars($fulldescription);
        $adescription =  htmlspecialchars($adescription);
$keywords = explode(" ",trim($picture['keywords']));
$keyword_string="";
$wordcount=0;
foreach($keywords as $keyword) {
if ($wordcount>0) {
$keyword_string .= ",$keyword";
}
else {
$keyword_string = "$keyword";
}
$wordcount++;
    }
        if (!isset($picture['title']) || strlen($picture['title'])==0)
{
$img_title= $picture['filename']?$picture['filename']:$lang_no.$lang_plugin_xrss['label_title'];
}
else
{
$img_title= bb_decode($picture['title']);
}
print "\t<item>\n";
    print "\t\t<title>".$img_title."</title>\n";
print "\t\t<link>{$link_url}{$picture['pid']}</link>\n";
print "\t\t<guid>{$link_url}{$picture['pid']}</guid>\n";
print "\t\t<author>{$picture['owner_name']}</author>\n";
print "\t\t<pubDate>" . lmdate($picture['ctime']) . "</pubDate>\n";
print "\t\t<description>$fulldescription</description>\n";
print "\t\t<comments>{$link_url}{$picture['pid']}#respond</comments>\n";
print "\t\t<content:encoded><![CDATA[" . "<p><img src=\"$thumb_url\" alt=\"\" /> </p><p>$adescription&nbsp;</p><p>$keyword_string</p>" . "]]>\n</content:encoded>\n";
        print "\t\t<media:group>\n";
print "\t\t\t<media:title>$img_title</media:title>\n";
print "\t\t\t<media:description type=\"html\">{$adescription}</media:description>\n";
print "\t\t\t<media:keywords>$keyword_string</media:keywords>\n";
print "\t\t\t<media:content  url=\"$imag_url\" width=\"{$picture['pwidth']}\" height=\"{$picture['pheight']}\" type=\"". getmime($picture['filename']) ."\" />\n";
print "\t\t\t<media:thumbnail url=\"$thumb_url\" />\n";
        print "\t\t\t<media:credit>{$picture['owner_name']}</media:credit>\n";
        print "\t\t</media:group>\n";
print "\t\t<photo:imgsrc>$imag_url</photo:imgsrc>\n";
print "\t\t<photo:thumbnail>$thumb_url</photo:thumbnail>\n";
print "\t\t<enclosure length=\"{$picture['filesize']}\" url=\"$image_url{$picture['filepath']}" . "{$picture['filename']}\" type=\"". getmime($picture['filename']) ."\" />\n";
print "\t</item>\n";
}
print "</channel>";
print "</rss>";
}


// routines for JSON feed
function FormatOutput($aString)
{
    // removes returns and replaces them with html br
    // also add slashes
    return (preg_replace("/\\r\\n|\\n\\r|\\n|\\r/", "<br />",addslashes($aString)));
}

function JSONFeed()
{
global $CONFIG,$gallery_name;
global $lang_no,$lang_plugin_xrss;

global $base,$link_url,$image_url;
global $data,$album;
global $thumb_count;

global $result,$base,$albumpath;
    global $feed_title,$feed_description;

///$result_count = count($data);
$result_count = $thumb_count;

if (isset($_GET['callback']))
{
print ($_GET['callback'])."(";
}
header('Content-type: application/x-json');
print "{\n";
print " \"version\":\"1.0\",\n";
print " \"encoding\":\"UTF-8\",\n";
print " \"feed\":{\n";
print ' "id":{'."\n ".'"$t":"'.$base."\"},\n";
print ' "updated":{'."\n ".'"$t":"'.lmdate(time())."\"},\n";
print ' "title":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.FormatOutput($feed_title).'"},'."\n";
print ' "subtitle":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.FormatOutput($feed_description).'"},'."\n";
print ' "link":'."\n";
print ' ['."\n";
print ' { "rel":"'.$base.'",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'.$base.'"}'."\n";
if (is_numeric($album))
{
print ' ,{ "rel":"alternate",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'.$base.'/thumbnails.php?album='.$album.'"}'."\n";
}
print ' ],'."\n";
print ' "author":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "name":{'."\n";
print ' "$t":"'.$gallery_name.'"},'."\n";
print ' "uri":{'."\n";
print ' "$t":"'.$base.'"}'."\n";
print ' }'."\n";
print ' ],'."\n";
print ' "generator":{'."\n";
print ' "version":"1.00",'."\n";
print ' "uri":"'.$base.'/xrss.php",'."\n";
print ' "$t":"Coppermine JSON Generator (1.01)"},'."\n";
print ' "openSearch$totalResults":{'."\n";
print ' "$t":"'.$result_count.'"},'."\n";
print ' "openSearch$startIndex":{'."\n";
print ' "$t":"1"},'."\n";
print ' "openSearch$itemsPerPage":{'."\n";
print ' "$t":"1000"},'."\n";
print ' "georss$where":{'."\n";
print ' "gml$Point":{'."\n";
print ' "gml$pos":{'."\n";
print ' "$t":"51.50199 -0.118955"}'."\n";
print ' }'."\n";
print ' },'."\n";

print ' "entry":'."\n";
print ' ['."\n";
$count=0;
foreach($data AS $picture)
{
    $thumb_url = "$image_url{$picture['filepath']}{$CONFIG['thumb_pfx']}{$picture['filename']}";
$imag_url = "$image_url{$picture['filepath']}{$picture['filename']}";

$caption_text = "<br />".$picture['hits']." ";
        $caption_text .= $lang_plugin_xrss['views'];
        $adescription = bb_decode($picture['caption']);
        $fulldescription = $adescription;
        $fulldescription = '<a href="' . $link_url . $picture['pid'] . '"><img src="' . $thumb_url . '" border="1" vspace="2" hspace="2"> <align="center" ></a><br />' . $adescription; 
        $fulldescription .= bb_decode($caption_text);   
    //$fulldescription =  htmlspecialchars($fulldescription);
        //$adescription =  htmlspecialchars($adescription);

    $keywords = explode(" ",trim($picture['keywords']));
$keyword_string="";
$wordcount=0;
foreach($keywords as $keyword) {
if ($wordcount>0) {
$keyword_string .= "$keyword";
}
else {
$keyword_string = ",$keyword";
}
$wordcount++;
    }

        if (!isset($picture['title']) || strlen($picture['title'])==0)
{
$img_title= $picture['filename']?$picture['filename']:$lang_no.$lang_plugin_xrss['label_title'];
}
else
{
$img_title= bb_decode($picture['title']);
}
$count++;
print ' {'."\n"; // start of entry
print ' "id":{'."\n";
print ' "$t":"'."{$link_url}{$picture['pid']}".'"},'."\n";
print ' "published":{'."\n";
print ' "$t":"'. lmdate($picture['ctime']) .'"},'."\n";
print ' "updated":{'."\n";
print ' "$t":"'. lmdate($picture['ctime']) .'"},'."\n";
print ' "title":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.$img_title.'"},'."\n";
print ' "summary":{'."\n";
print ' "type":"html",'."\n";
print ' "$t":"';
echo  FormatOutput($fulldescription);
print '"},'."\n";
print ' "content":{'."\n";
print ' "type":"image/jpeg",'."\n";
print ' "src":"'.$imag_url.'"},'."\n";
print ' "link":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "rel":"'."{$link_url}{$picture['pid']}".'",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'."{$link_url}{$picture['pid']}".'"'."\n";
print ' }'."\n";
print ' ],'."\n";
print ' "media$group":{'."\n";
print ' "media$title":{'."\n";
print ' "type":"plain",'."\n";
print ' "$t":"'.$img_title.'"},'."\n";
if (strlen($adescription)>0) {
print ' "media$description":{'."\n";
print ' "type":"html",'."\n";
print ' "$t":"'.FormatOutput($adescription).'"},'."\n";
}
print ' "media$keywords":{'."\n";
print ' "$t":"';
echo FormatOutput($keyword_string);
print '"},'."\n";
print ' "media$content":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "url":"'."$imag_url".'",'."\n";
print ' "height":"'.$picture['pheight'].'",'."\n";
print ' "width":"'.$picture['pwidth'].'",'."\n";
print ' "type":"image/jpeg",'."\n";
print ' "medium":"image"}'."\n";
print ' ],'."\n";
print ' "media$thumbnail":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "url":"'.$thumb_url.'",'."\n";
print ' "height":"80",'."\n";
print ' "width":"100"}'."\n";
print ' ],'."\n";
print ' "media$credit":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "$t":"'.$picture['owner_name'].'"}'."\n";
print ' ]'."\n";
print ' }'."\n";
if ($count==$result_count)
{
print ' }'."\n"; // end of entry last one
}
else
{
print ' },'."\n"; // end of entry more to come
}
}
print ' ]'."\n"; // entry
print " }\n"; // end feed

if (isset($_GET['callback']))
{
print "});\n"; // end opening function
}
else
{
print "}\n"; // end opening {
}
}
?>


capecodgal

Very nice!

Anyway to maybe do a 'group by' or something so that the results are grouped by the album title then move onto the next album?

Also I noticed if there is no photo title the file name is shown as a default, anyway to change that to be the album name as the default instead?

PeterLawrence

Capecodgal are you trying to display the last X uploaded photos?
The displaying of the album name instead of the filename would be a modification for the javascript code and/or possible PHP code depending which parameters you're using. Are you trying to display the last X uploaded photos?
However, the grouping via albums would be a bit more tricky to do since I'm using the standard Coppermine access functions which currently do not support this option. However, I'll look into it.

capecodgal

No... I am trying to have a rss feed that does one post per updated album with about 3 or 4 thumbs per post.... instead of 1 thumb


I got it to the point of last updated albums  ;D now just working on the thumbs...


sample:


http://www.selenagomezweb.com/photos/rss2.php




<?php 
//  Coppermine Photo Gallery - XRSS Feed
//  Peter J Lawrence (Feb 2009)
//   Based on code by Mohammad Hafiz, Brent Gerig, Christophe (cly) 
//   and many others who have contributed PHP code for data feeds on this forum        
//
//  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.
//
define('IN_COPPERMINE'true);
define('INDEX_PHP'true);

global 
$USER,$CONFIG,$ALBUM_SET,$META_ALBUM_SET,$CURRENT_CAT_NAME,$FORBIDDEN_SET_DATA;
require(
'include/init.inc.php');

// lang stuff
$lang_plugin_xrss = array(
  
'label_title'   => 'Title',
  
'views'         => 'views',
);

define('CPG14'version_compare(COPPERMINE_VERSION"1.4.0"">="));
define('PHP5'version_compare(phpversion(), "5"">="));

//Default numebr of items  to show in RSS feed
$thumb_per_page 25;

$thumb_count 0;
$lower_limit 0;
$feed_description="";
$allphotos=FALSE;

//cpg_db_connect(); // uncomment this line if you're having problem connecting to the database

if(isset($_GET['max-results']))
{
if (is_numeric($_GET['max-results']))
{
$thumb_per_page = (int) $_GET['max-results'];
}
else if ($_GET['max-results']=='all')
{
$allphotos=TRUE;
}
}

if (isset(
$_GET['start-index']))
{
$lower_limit = (int) $_GET['start-index'];
$thumb_per_page+=$lower_limit;
}

if(isset(
$_GET['sort'])){
$USER['sort']=$_GET['sort'];
}

if(
count($FORBIDDEN_SET_DATA) > ){
    
$forbidden_set_string =" AND aid NOT IN (".implode(","$FORBIDDEN_SET_DATA).")";
} else {
    
$forbidden_set_string '';
}

if(isset(
$_GET['album'])){
    
$album $_GET['album'];
}

//If it is a numeric album get the name and set variables
if ((is_numeric($album))){
     
$album_name_keyword get_album_name($album);
     
$CURRENT_CAT_NAME $album_name_keyword['title'];
     
$META_ALBUM_SET "AND aid IN (".(int)$album.")".$ALBUM_SET;
 
 $AlbumData mysql_query("SELECT description FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid=$album");
 $count mysql_num_rows($AlbumData );
 if ($count>0)
 {
$row mysql_fetch_array($AlbumData);
$feed_description=$row['description'];
 }
 if ($allphotos==FALSE && !isset($_GET['sort']))
 {
     //Set the album to last uploaded
     $album 'lastalb';
 }
}

//If the album is not set set it to lastup - this is the default
if(!isset($album)){
     
$album 'lastalb';
}

if ((isset(
$_GET['cat']) && $_GET['cat'] > 0)){ 
     
$cat = (int) $_GET['cat'];
     
$album_name_keyword xrss_get_cat_name($cat);
     
$CURRENT_CAT_NAME $album_name_keyword['name'];
     
get_meta_album_set($cat,$META_ALBUM_SET);
}

if ((isset(
$_GET['cat']) && $_GET['cat'] < 0)){ 
     
$cat = (int) $_GET['cat'];
     
$album_name_keyword get_album_name(-$cat);
     
$CURRENT_CAT_NAME $album_name_keyword['title'];
     
$META_ALBUM_SET "AND aid IN (".-$cat.")".$ALBUM_SET;
}

//Changes these to point to your site if the following is not giving correct results.
$base rtrim($CONFIG['ecards_more_pic_target'], '/');
$albumpath "$base/" $CONFIG['fullpath'];
$link_url $base."/displayimage.php?pos=-";
$image_url $base."/albums/";-

$gallery_name=$CONFIG['gallery_name'];

$META_ALBUM_SET .= $forbidden_set_string;

$data get_pic_data($album$thumb_count$album_name$lower_limit$thumb_per_page);

$feed_title=$gallery_name.": ".$album_name;
if (
strlen($feed_description)==0)
{
$feed_description="$CONFIG[gallery_description]";
}

$feedtype="basic";
if (isset(
$_GET['format']))
{
    
$feedtype $_GET['format'];
}

switch (
$feedtype)
{
default:
case "mrss2":
MediaRSS2();
break;
case "json":
JSONFeed();
break;
}
//cpg_debug_output();

function xrss_get_cat_name($cid)
{
global $CONFIG;
global $lang_errors;

$result cpg_db_query("SELECT name from {$CONFIG['TABLE_CATEGORIES']} WHERE cid='$cid'");
$count mysql_num_rows($result);
if ($count 0) {
$row mysql_fetch_array($result);
return $row;
} else {
cpg_die(ERROR$lang_errors['non_exist_ap'], __FILE____LINE__);
}
}

// Routines for Media RSS feed

function getmime ($Filename) {
    
$mimetype="text/plain";
    
// make an array, each value seperated by a period (.)
    
$Extension explode ("."$Filename);

    
// Count how many are in array, and -1 due to
    // php starting an array with 0
    
$Extension_i = (count($Extension) - 1);

    
// Return it..
    
    
$ext $Extension[$Extension_i];
    
    if (
$ext == "jpg") {
       
$mimetype="image/jpeg";      
    } else if (
$ext == "png") {
       
$mimetype="image/png";
    } else if ( 
$ext == "gif") {
      
$mimetype="image/gif";
    }
    return 
$mimetype;


function 
lmdate($timestamp)
{
if (PHP5){
return date('c'$timestamp);
} else {
return date('D, d M Y H:i:s +0800'$timestamp date('Z'));
}
}

function 
MediaRSS2()
{
global $CONFIG;
global $lang_no,$lang_plugin_xrss;

global $base,$link_url,$image_url;
global $data,$album;
global $result,$base,$albumpath;
    global 
$feed_title,$feed_description;
    
//$feed_description=htmlspecialchars(bb_decode($feed_description));

//header("Content-type: text/xml; charset=utf-8");
    
header ("content-type: text/xml");
// was UTF-8
    
print "<?xml version=\"1.0\" encoding=\"iso-8859-7\"?>
\n";
print "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"";
print " xmlns:photo=\"http://www.pheed.com/pheed/\" xmlns:media=\"http://search.yahoo.com/mrss\" >";
print "<channel>\n";
print "<title>$feed_title</title>\n";
    if (is_numeric($album))
    {
        print "<link>$base/thumbnails.php?album=$album</link>\n";
    }
    else
    {
        print "<link>$base</link>\n";
    }
print "<description>".$feed_description."</description>";
print "<lastBuildDate>"  . lmdate(time()) . "</lastBuildDate>\n";
print "<generator>$base/xrss.php</generator>\n";

    foreach($data AS $picture)
{
    $thumb_url = "$image_url$picture[filepath]$CONFIG[thumb_pfx]$picture[filename]";
$imag_url  = "$image_url$picture[filepath]$picture[filename]";

$caption_text = "<br />".$picture['hits']." ";
        $caption_text .= $lang_plugin_xrss['views'];
        $adescription = bb_decode($picture['caption']);
        $fulldescription = $adescription;
        $fulldescription = '<a href="' . $link_url . $picture['pid'] . '"><img src="' . $thumb_url . '" border="1" vspace="2" hspace="2"> <align="center" ></a><br />' . $adescription; 
        $fulldescription .= bb_decode($caption_text);   
    $fulldescription =  htmlspecialchars($fulldescription);
        $adescription =  htmlspecialchars($adescription);
$keywords = explode(" ",trim($picture['keywords']));
$keyword_string="";
$wordcount=0;
foreach($keywords as $keyword) {
if ($wordcount>0) {
$keyword_string .= ",$keyword";
}
else {
$keyword_string = "$keyword";
}
$wordcount++;
    }
        if (!isset($picture['title']) || strlen($picture['title'])==0)
{
$img_title= $picture['filename']?$picture['filename']:$lang_no.$lang_plugin_xrss['label_title'];
}
else
{
$img_title= bb_decode($picture['title']);
}
print "\t<item>\n";
    print "\t\t<title>".$img_title."</title>\n";
print "\t\t<link>{$link_url}{$picture['pid']}</link>\n";
print "\t\t<guid>{$link_url}{$picture['pid']}</guid>\n";
print "\t\t<author>{$picture['owner_name']}</author>\n";
print "\t\t<pubDate>" . lmdate($picture['ctime']) . "</pubDate>\n";
print "\t\t<description>$fulldescription</description>\n";
print "\t\t<comments>{$link_url}{$picture['pid']}#respond</comments>\n";
print "\t\t<content:encoded><![CDATA[" . "<p><img src=\"$thumb_url\" alt=\"\" /> </p><p>$adescription&nbsp;</p><p>$keyword_string</p>" . "]]>\n</content:encoded>\n";
        print "\t\t<media:group>\n";
print "\t\t\t<media:title>$img_title</media:title>\n";
print "\t\t\t<media:description type=\"html\">{$adescription}</media:description>\n";
print "\t\t\t<media:keywords>$keyword_string</media:keywords>\n";
print "\t\t\t<media:content  url=\"$imag_url\" width=\"{$picture['pwidth']}\" height=\"{$picture['pheight']}\" type=\"". getmime($picture['filename']) ."\" />\n";
print "\t\t\t<media:thumbnail url=\"$thumb_url\" />\n";
        print "\t\t\t<media:credit>{$picture['owner_name']}</media:credit>\n";
        print "\t\t</media:group>\n";
print "\t\t<photo:imgsrc>$imag_url</photo:imgsrc>\n";
print "\t\t<photo:thumbnail>$thumb_url</photo:thumbnail>\n";
print "\t\t<enclosure length=\"{$picture['filesize']}\" url=\"$image_url{$picture['filepath']}" . "{$picture['filename']}\" type=\"". getmime($picture['filename']) ."\" />\n";
print "\t</item>\n";
}
print "</channel>";
print "</rss>";
}


// routines for JSON feed
function FormatOutput($aString)
{
    // removes returns and replaces them with html br
    // also add slashes
    return (preg_replace("/\\r\\n|\\n\\r|\\n|\\r/", "<br />",addslashes($aString)));
}

function JSONFeed()
{
global $CONFIG,$gallery_name;
global $lang_no,$lang_plugin_xrss;

global $base,$link_url,$image_url;
global $data,$album;
global $thumb_count;

global $result,$base,$albumpath;
    global $feed_title,$feed_description;

///$result_count = count($data);
$result_count = $thumb_count;

if (isset($_GET['callback']))
{
print ($_GET['callback'])."(";
}
header('Content-type: application/x-json');
print "{\n";
print " \"version\":\"1.0\",\n";
print " \"encoding\":\"UTF-8\",\n";
print " \"feed\":{\n";
print ' "id":{'."\n ".'"$t":"'.$base."\"},\n";
print ' "updated":{'."\n ".'"$t":"'.lmdate(time())."\"},\n";
print ' "title":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.FormatOutput($feed_title).'"},'."\n";
print ' "subtitle":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.FormatOutput($feed_description).'"},'."\n";
print ' "link":'."\n";
print ' ['."\n";
print ' { "rel":"'.$base.'",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'.$base.'"}'."\n";
if (is_numeric($album))
{
print ' ,{ "rel":"alternate",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'.$base.'/thumbnails.php?album='.$album.'"}'."\n";
}
print ' ],'."\n";
print ' "author":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "name":{'."\n";
print ' "$t":"'.$gallery_name.'"},'."\n";
print ' "uri":{'."\n";
print ' "$t":"'.$base.'"}'."\n";
print ' }'."\n";
print ' ],'."\n";
print ' "generator":{'."\n";
print ' "version":"1.00",'."\n";
print ' "uri":"'.$base.'/xrss.php",'."\n";
print ' "$t":"Coppermine JSON Generator (1.01)"},'."\n";
print ' "openSearch$totalResults":{'."\n";
print ' "$t":"'.$result_count.'"},'."\n";
print ' "openSearch$startIndex":{'."\n";
print ' "$t":"1"},'."\n";
print ' "openSearch$itemsPerPage":{'."\n";
print ' "$t":"1000"},'."\n";
print ' "georss$where":{'."\n";
print ' "gml$Point":{'."\n";
print ' "gml$pos":{'."\n";
print ' "$t":"51.50199 -0.118955"}'."\n";
print ' }'."\n";
print ' },'."\n";

print ' "entry":'."\n";
print ' ['."\n";
$count=0;
foreach($data AS $picture)
{
    $thumb_url = "$image_url{$picture['filepath']}{$CONFIG['thumb_pfx']}{$picture['filename']}";
$imag_url = "$image_url{$picture['filepath']}{$picture['filename']}";

$caption_text = "<br />".$picture['hits']." ";
        $caption_text .= $lang_plugin_xrss['views'];
        $adescription = bb_decode($picture['caption']);
        $fulldescription = $adescription;
        $fulldescription = '<a href="' . $link_url . $picture['pid'] . '"><img src="' . $thumb_url . '" border="1" vspace="2" hspace="2"> <align="center" ></a><br />' . $adescription; 
        $fulldescription .= bb_decode($caption_text);   
    //$fulldescription =  htmlspecialchars($fulldescription);
        //$adescription =  htmlspecialchars($adescription);

    $keywords = explode(" ",trim($picture['keywords']));
$keyword_string="";
$wordcount=0;
foreach($keywords as $keyword) {
if ($wordcount>0) {
$keyword_string .= "$keyword";
}
else {
$keyword_string = ",$keyword";
}
$wordcount++;
    }

        if (!isset($picture['title']) || strlen($picture['title'])==0)
{
$img_title= $picture['filename']?$picture['filename']:$lang_no.$lang_plugin_xrss['label_title'];
}
else
{
$img_title= bb_decode($picture['title']);
}
$count++;
print ' {'."\n"; // start of entry
print ' "id":{'."\n";
print ' "$t":"'."{$link_url}{$picture['pid']}".'"},'."\n";
print ' "published":{'."\n";
print ' "$t":"'. lmdate($picture['ctime']) .'"},'."\n";
print ' "updated":{'."\n";
print ' "$t":"'. lmdate($picture['ctime']) .'"},'."\n";
print ' "title":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.$img_title.'"},'."\n";
print ' "summary":{'."\n";
print ' "type":"html",'."\n";
print ' "$t":"';
echo  FormatOutput($fulldescription);
print '"},'."\n";
print ' "content":{'."\n";
print ' "type":"image/jpeg",'."\n";
print ' "src":"'.$imag_url.'"},'."\n";
print ' "link":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "rel":"'."{$link_url}{$picture['pid']}".'",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'."{$link_url}{$picture['pid']}".'"'."\n";
print ' }'."\n";
print ' ],'."\n";
print ' "media$group":{'."\n";
print ' "media$title":{'."\n";
print ' "type":"plain",'."\n";
print ' "$t":"'.$img_title.'"},'."\n";
if (strlen($adescription)>0) {
print ' "media$description":{'."\n";
print ' "type":"html",'."\n";
print ' "$t":"'.FormatOutput($adescription).'"},'."\n";
}
print ' "media$keywords":{'."\n";
print ' "$t":"';
echo FormatOutput($keyword_string);
print '"},'."\n";
print ' "media$content":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "url":"'."$imag_url".'",'."\n";
print ' "height":"'.$picture['pheight'].'",'."\n";
print ' "width":"'.$picture['pwidth'].'",'."\n";
print ' "type":"image/jpeg",'."\n";
print ' "medium":"image"}'."\n";
print ' ],'."\n";
print ' "media$thumbnail":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "url":"'.$thumb_url.'",'."\n";
print ' "height":"80",'."\n";
print ' "width":"100"}'."\n";
print ' ],'."\n";
print ' "media$credit":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "$t":"'.$picture['owner_name'].'"}'."\n";
print ' ]'."\n";
print ' }'."\n";
if ($count==$result_count)
{
print ' }'."\n"; // end of entry last one
}
else
{
print ' },'."\n"; // end of entry more to come
}
}
print ' ]'."\n"; // entry
print " }\n"; // end feed

if (isset($_GET['callback']))
{
print "});\n"; // end opening function
}
else
{
print "}\n"; // end opening {
}
}
?>



I just changed lastup to lastalb and it worked  ;D  ;D


Now I just need to show last updloaded thumbs of that album... like say 3 or 4 thumbs for that album and then remove the media link to the image.

My goal is to import this to wordpress and have automatic posting when the gallery is uploaded but the gallery rss is always the major problem as it needs to be the right format






PeterLawrence

Having now upgraded to CPG 1.5 I thought I'll post an updated version of this script for the latest version of coppermine, just in case someone might be interested.
The main change is use of the Inspekt accessor method rather than $_GET.


<?php 
//  Coppermine Photo Gallery - XRSS Feed -adapted for CPG1.5
//  Peter J Lawrence (Oct 2010)
//   Based on code by Mohammad Hafiz, Brent Gerig, Christophe (cly) 
//   and many others who have contributed PHP code for data feeds on this forum        
//
//  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.
//
define('IN_COPPERMINE'true);
define('INDEX_PHP'true);

global 
$USER,$CONFIG,$ALBUM_SET,$META_ALBUM_SET,$CURRENT_CAT_NAME,$FORBIDDEN_SET_DATA;
require(
'include/init.inc.php');

// lang stuff
$lang_plugin_xrss = array(
  
'label_title'   => 'Title',
  
'views'         => 'views',
);

define('PHP5'version_compare(phpversion(), "5"">="));
$superCage Inspekt::makeSuperCage();


//Default numebr of items  to show in RSS feed
$thumb_per_page 10;

$thumb_count 0;
$lower_limit 0;
$feed_description="";
$allphotos=FALSE;

//cpg_db_connect(); // uncomment this line if you're having problem connecting to the database

if($superCage->get->keyExists('max-results'))
{
if (is_numeric($superCage->get->getRaw('max-results')))
{
$thumb_per_page = (int) $superCage->get->getInt('max-results');
}
else if ($superCage->get->getRaw('max-results')=='all')
{
$allphotos=TRUE;
}
}

if (
$superCage->get->keyExists('start-index'))
{
$lower_limit = (int) $superCage->get->getInt('start-index');
$thumb_per_page+=$lower_limit;
}

if(
$superCage->get->keyExists('sort')){
$USER['sort']=$superCage->get->getEscaped('sort');
}

if(
count($FORBIDDEN_SET_DATA) > ){
    
$forbidden_set_string =" AND aid NOT IN (".implode(","$FORBIDDEN_SET_DATA).")";
} else {
    
$forbidden_set_string '';
}

if (
$superCage->get->keyExists('album')) {
   
$album $superCage->get->getEscaped('album');
}

//If it is a numeric album get the name and set variables
if ((is_numeric($album))){
     
$album_name_keyword get_album_name($album);
     
$CURRENT_CAT_NAME $album_name_keyword['title'];
     
$META_ALBUM_SET "AND aid IN (".(int)$album.")".$ALBUM_SET;
 
 $AlbumData mysql_query("SELECT description FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid=$album");
 $count mysql_num_rows($AlbumData );
 if ($count>0)
 {
$row mysql_fetch_array($AlbumData);
$feed_description=$row['description'];
 }
 if ($allphotos==FALSE && !$superCage->get->keyExists('sort'))
 {
     //Set the album to last uploaded
     $album 'lastup';
 }
}

//If the album is not set set it to lastup - this is the default
if(!isset($album)){
     
$album 'lastup';
}

if ((
$superCage->get->keyExists('cat') && $superCage->get->getInt('cat') > 0)){ 
     
$cat = (int) $superCage->get->getInt('cat');
     
$album_name_keyword xrss_get_cat_name($cat);
     
$CURRENT_CAT_NAME $album_name_keyword['name'];
     
get_meta_album_set($cat,$META_ALBUM_SET);
}

if ((
$superCage->get->keyExists('cat') && $superCage->get->getInt('cat') < 0)){ 
     
$cat = (int) $superCage->get->getInt('cat');
     
$album_name_keyword get_album_name(-$cat);
     
$CURRENT_CAT_NAME $album_name_keyword['title'];
     
$META_ALBUM_SET "AND aid IN (".-$cat.")".$ALBUM_SET;
}

//Changes these to point to your site if the following is not giving correct results.
$base rtrim($CONFIG['ecards_more_pic_target'], '/');
$albumpath "$base/" $CONFIG['fullpath'];
$link_url $base."/displayimage.php?pos=-";
$image_url $base."/albums/";-

$gallery_name=$CONFIG['gallery_name'];

$META_ALBUM_SET .= $forbidden_set_string;

$data get_pic_data($album$thumb_count$album_name$lower_limit$thumb_per_page);

$feed_title=$gallery_name.": ".$album_name;
if (
strlen($feed_description)==0)
{
$feed_description="$CONFIG[gallery_description]";
}

$feedtype="basic";
if (
$superCage->get->keyExists('format'))
{
    
$feedtype $superCage->get->getRaw('format');
}

switch (
$feedtype)
{
default:
case "mrss2":
MediaRSS2();
break;
case "json":
JSONFeed();
break;
}
//cpg_debug_output();

function xrss_get_cat_name($cid)
{
global $CONFIG;
global $lang_errors;

$result cpg_db_query("SELECT name from {$CONFIG['TABLE_CATEGORIES']} WHERE cid='$cid'");
$count mysql_num_rows($result);
if ($count 0) {
$row mysql_fetch_array($result);
return $row;
} else {
cpg_die(ERROR$lang_errors['non_exist_ap'], __FILE____LINE__);
}
}

// Routines for Media RSS feed

function getmime ($Filename) {
    
$mimetype="text/plain";
    
// make an array, each value seperated by a period (.)
    
$Extension explode ("."$Filename);

    
// Count how many are in array, and -1 due to
    // php starting an array with 0
    
$Extension_i = (count($Extension) - 1);

    
// Return it..
    
    
$ext $Extension[$Extension_i];
    
    if (
$ext == "jpg") {
       
$mimetype="image/jpeg";      
    } else if (
$ext == "png") {
       
$mimetype="image/png";
    } else if ( 
$ext == "gif") {
      
$mimetype="image/gif";
    }
    return 
$mimetype;


function 
lmdate($timestamp)
{
if (PHP5){
return date('c'$timestamp);
} else {
return date('D, d M Y H:i:s +0800'$timestamp date('Z'));
}
}

function 
MediaRSS2()
{
global $CONFIG;
global $lang_no,$lang_plugin_xrss;

global $base,$link_url,$image_url;
global $data,$album;
global $result,$base,$albumpath;
    global 
$feed_title,$feed_description;
    
//$feed_description=htmlspecialchars(bb_decode($feed_description));

//header("Content-type: text/xml; charset=utf-8");
    
header ("content-type: text/xml");
// was UTF-8
    
print "<?xml version=\"1.0\" encoding=\"iso-8859-7\"?>
\n";
print "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"";
print " xmlns:photo=\"http://www.pheed.com/pheed/\" xmlns:media=\"http://search.yahoo.com/mrss\" >";
print "<channel>\n";
print "<title>$feed_title</title>\n";
    if (is_numeric($album))
    {
        print "<link>$base/thumbnails.php?album=$album</link>\n";
    }
    else
    {
        print "<link>$base</link>\n";
    }
print "<description>".$feed_description."</description>";
print "<lastBuildDate>"  . lmdate(time()) . "</lastBuildDate>\n";
print "<generator>$base/xrss.php</generator>\n";

    foreach($data AS $picture)
{
    $thumb_url = "$image_url$picture[filepath]$CONFIG[thumb_pfx]$picture[filename]";
$imag_url  = "$image_url$picture[filepath]$picture[filename]";

$caption_text = "<br />".$picture['hits']." ";
        $caption_text .= $lang_plugin_xrss['views'];
        $adescription = bb_decode($picture['caption']);
        $fulldescription = $adescription;
        $fulldescription = '<a href="' . $link_url . $picture['pid'] . '"><img src="' . $thumb_url . '" border="1" vspace="2" hspace="2"> <align="center" ></a><br />' . $adescription; 
        $fulldescription .= bb_decode($caption_text);   
    $fulldescription =  htmlspecialchars($fulldescription);
        $adescription =  htmlspecialchars($adescription);
$keywords = explode(" ",trim($picture['keywords']));
$keyword_string="";
$wordcount=0;
foreach($keywords as $keyword) {
if ($wordcount>0) {
$keyword_string .= ",$keyword";
}
else {
$keyword_string = "$keyword";
}
$wordcount++;
    }
        if (!isset($picture['title']) || strlen($picture['title'])==0)
{
$img_title= $picture['filename']?$picture['filename']:$lang_no.$lang_plugin_xrss['label_title'];
}
else
{
$img_title= bb_decode($picture['title']);
}
print "\t<item>\n";
    print "\t\t<title>".$img_title."</title>\n";
print "\t\t<link>{$link_url}{$picture['pid']}</link>\n";
print "\t\t<guid>{$link_url}{$picture['pid']}</guid>\n";
print "\t\t<author>{$picture['owner_name']}</author>\n";
print "\t\t<pubDate>" . lmdate($picture['ctime']) . "</pubDate>\n";
print "\t\t<description>$fulldescription</description>\n";
print "\t\t<comments>{$link_url}{$picture['pid']}#respond</comments>\n";
print "\t\t<content:encoded><![CDATA[" . "<p><img src=\"$thumb_url\" alt=\"\" /> </p><p>$adescription&nbsp;</p><p>$keyword_string</p>" . "]]>\n</content:encoded>\n";
        print "\t\t<media:group>\n";
print "\t\t\t<media:title>$img_title</media:title>\n";
print "\t\t\t<media:description type=\"html\">{$adescription}</media:description>\n";
print "\t\t\t<media:keywords>$keyword_string</media:keywords>\n";
print "\t\t\t<media:content  url=\"$imag_url\" width=\"{$picture['pwidth']}\" height=\"{$picture['pheight']}\" type=\"". getmime($picture['filename']) ."\" />\n";
print "\t\t\t<media:thumbnail url=\"$thumb_url\" />\n";
        print "\t\t\t<media:credit>{$picture['owner_name']}</media:credit>\n";
        print "\t\t</media:group>\n";
print "\t\t<photo:imgsrc>$imag_url</photo:imgsrc>\n";
print "\t\t<photo:thumbnail>$thumb_url</photo:thumbnail>\n";
print "\t\t<enclosure length=\"{$picture['filesize']}\" url=\"$image_url{$picture['filepath']}" . "{$picture['filename']}\" type=\"". getmime($picture['filename']) ."\" />\n";
print "\t</item>\n";
}
print "</channel>";
print "</rss>";
}


// routines for JSON feed
function FormatOutput($aString)
{
    // removes returns and replaces them with html br
    // also add slashes
    return (preg_replace("/\\r\\n|\\n\\r|\\n|\\r/", "<br />",addslashes($aString)));
}

function JSONFeed()
{
global $CONFIG,$gallery_name;
global $lang_no,$lang_plugin_xrss;

global $base,$link_url,$image_url;
global $data,$album;
global $thumb_count;

global $result,$base,$albumpath;
    global $feed_title,$feed_description;
    global $superCage;

///$result_count = count($data);
$result_count = $thumb_count;

header('Content-type: application/x-json');
if ($superCage->get->keyExists('callback'))
{
print ($superCage->get->getRaw('callback'))."(";
}
print "{\n";
print " \"version\":\"1.0\",\n";
print " \"encoding\":\"UTF-8\",\n";
print " \"feed\":{\n";
print ' "id":{'."\n ".'"$t":"'.$base."\"},\n";
print ' "updated":{'."\n ".'"$t":"'.lmdate(time())."\"},\n";
print ' "title":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.FormatOutput($feed_title).'"},'."\n";
print ' "subtitle":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.FormatOutput($feed_description).'"},'."\n";
print ' "link":'."\n";
print ' ['."\n";
print ' { "rel":"'.$base.'",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'.$base.'"}'."\n";
if (is_numeric($album))
{
print ' ,{ "rel":"alternate",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'.$base.'/thumbnails.php?album='.$album.'"}'."\n";
}
print ' ],'."\n";
print ' "author":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "name":{'."\n";
print ' "$t":"'.$gallery_name.'"},'."\n";
print ' "uri":{'."\n";
print ' "$t":"'.$base.'"}'."\n";
print ' }'."\n";
print ' ],'."\n";
print ' "generator":{'."\n";
print ' "version":"1.00",'."\n";
print ' "uri":"'.$base.'/xrss.php",'."\n";
print ' "$t":"Coppermine JSON Generator (1.01)"},'."\n";
print ' "openSearch$totalResults":{'."\n";
print ' "$t":"'.$result_count.'"},'."\n";
print ' "openSearch$startIndex":{'."\n";
print ' "$t":"1"},'."\n";
print ' "openSearch$itemsPerPage":{'."\n";
print ' "$t":"1000"},'."\n";
print ' "georss$where":{'."\n";
print ' "gml$Point":{'."\n";
print ' "gml$pos":{'."\n";
print ' "$t":"51.50199 -0.118955"}'."\n";
print ' }'."\n";
print ' },'."\n";

print ' "entry":'."\n";
print ' ['."\n";
$count=0;
foreach($data AS $picture)
{
    $thumb_url = "$image_url{$picture['filepath']}{$CONFIG['thumb_pfx']}{$picture['filename']}";
$imag_url = "$image_url{$picture['filepath']}{$picture['filename']}";

$caption_text = "<br />".$picture['hits']." ";
        $caption_text .= $lang_plugin_xrss['views'];
        $adescription = bb_decode($picture['caption']);
        $fulldescription = $adescription;
        $fulldescription = '<a href="' . $link_url . $picture['pid'] . '"><img src="' . $thumb_url . '" border="1" vspace="2" hspace="2"> <align="center" ></a><br />' . $adescription; 
        $fulldescription .= bb_decode($caption_text);   
    //$fulldescription =  htmlspecialchars($fulldescription);
        //$adescription =  htmlspecialchars($adescription);

    $keywords = explode(" ",trim($picture['keywords']));
$keyword_string="";
$wordcount=0;
foreach($keywords as $keyword) {
if ($wordcount>0) {
$keyword_string .= "$keyword";
}
else {
$keyword_string = ",$keyword";
}
$wordcount++;
    }

        if (!isset($picture['title']) || strlen($picture['title'])==0)
{
$img_title= $picture['filename']?$picture['filename']:$lang_no.$lang_plugin_xrss['label_title'];
}
else
{
$img_title= bb_decode($picture['title']);
}
$count++;
print ' {'."\n"; // start of entry
print ' "id":{'."\n";
print ' "$t":"'."{$link_url}{$picture['pid']}".'"},'."\n";
print ' "published":{'."\n";
print ' "$t":"'. lmdate($picture['ctime']) .'"},'."\n";
print ' "updated":{'."\n";
print ' "$t":"'. lmdate($picture['ctime']) .'"},'."\n";
print ' "title":{'."\n";
print ' "type":"text",'."\n";
print ' "$t":"'.$img_title.'"},'."\n";
print ' "summary":{'."\n";
print ' "type":"html",'."\n";
print ' "$t":"';
echo  FormatOutput($fulldescription);
print '"},'."\n";
print ' "content":{'."\n";
print ' "type":"image/jpeg",'."\n";
print ' "src":"'.$imag_url.'"},'."\n";
print ' "link":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "rel":"'."{$link_url}{$picture['pid']}".'",'."\n";
print ' "type":"text/html",'."\n";
print ' "href":"'."{$link_url}{$picture['pid']}".'"'."\n";
print ' }'."\n";
print ' ],'."\n";
print ' "media$group":{'."\n";
print ' "media$title":{'."\n";
print ' "type":"plain",'."\n";
print ' "$t":"'.$img_title.'"},'."\n";
if (strlen($adescription)>0) {
print ' "media$description":{'."\n";
print ' "type":"html",'."\n";
print ' "$t":"'.FormatOutput($adescription).'"},'."\n";
}
print ' "media$keywords":{'."\n";
print ' "$t":"';
echo FormatOutput($keyword_string);
print '"},'."\n";
print ' "media$content":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "url":"'."$imag_url".'",'."\n";
print ' "height":"'.$picture['pheight'].'",'."\n";
print ' "width":"'.$picture['pwidth'].'",'."\n";
print ' "type":"image/jpeg",'."\n";
print ' "medium":"image"}'."\n";
print ' ],'."\n";
print ' "media$thumbnail":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "url":"'.$thumb_url.'",'."\n";
print ' "height":"80",'."\n";
print ' "width":"100"}'."\n";
print ' ],'."\n";
print ' "media$credit":'."\n";
print ' ['."\n";
print ' {'."\n";
print ' "$t":"'.$picture['owner_name'].'"}'."\n";
print ' ]'."\n";
print ' }'."\n";
if ($count==$result_count)
{
print ' }'."\n"; // end of entry last one
}
else
{
print ' },'."\n"; // end of entry more to come
}
}
print ' ]'."\n"; // entry
print " }\n"; // end feed

if ($superCage->get->keyExists('callback'))
{
print "});\n"; // end opening function
}
else
{
print "}\n"; // end opening {
}
}
?>

Joachim Müller

Please start a new thread for cpg1.5.x in the corresponding support board section, as this board will soon be closed and moved to a place where it stands out less, as support for cpg1.4.x is running out by the end of the year and we're not fond of this very board. Thanks.

Joachim