<?php
/*
Plugin Name: Coppermine
Version: 0.1
Plugin URI:
Description: Include Coppermine into Wordpress
Author: Jason Goldsmith (Unteins) adapted by Matthias Jell
Author URI: http://www.texx.org
*/

/*
Usage:
   Add one of the following lines into an entry:

   [cpg_image:2,4]   --> would insert the 4th image in the 2nd album
   [cpg_album:4]         inserts clickable album thumbnails

Layout:
   CSS IMG classes:      cpg_img, cpg_normal_image, cpg_thumb_image
   CSS album DIV class:  cpg_album
   CSS album link:       cpg_album_link
*/
/////////////////////////////////////////////////////////////////////

define ('CPG_DBUSER', 'user');
define ('CPG_DBPASS', 'passwd');
define ('CPG_DBSERVER', 'localhost');
define ('CPG_DBNAME', 'dbname');
define ('CPG_TABLE_PREFIX', 'cpg132_');

// text for album link
define ('CPG_ALBUM_TEXT', 'visit the album');
//define ('CPG_ALBUM_TEXT', '');

// image type for a single image
define ('CPG_IMAGE_TYPE' , 'normal_');    // 'normalized' image
//define ('CPG_IMAGE_TYPE' , 'thumb_');   // thumbnail
//define ('CPG_IMAGE_TYPE' , '');         // original image

/////////////////////////////////////////////////////////////////////

function cpg($text) {
   global $cmdb;
   $cmdb = new wpdb(CPG_DBUSER, CPG_DBPASS, CPG_DBNAME, CPG_DBSERVER);
   // look of album tag
   $offset = 0;
   do {
      $start = @strpos($text, '[cpg_album:', $offset);
      if ($start !== false) {
         $end = strpos($text, ']', $start);

         $aid =  substr($text, $start + 11, $end - $start -11);
         $text = substr($text,0 , $start) . get_album($cmdb, $aid) . substr($text, $end + 1);
         $offset = $end;
      }
   } while ($start !== false);
   // look of image tags
   $offset = 0;
   do {
      $start = @strpos($text, '[cpg_image:', $offset);
      if ($start !== false) {
         $comma = strpos($text, ',', $start);
         $end = strpos($text, ']', $start);

         $aid = substr($text, $start + 11, $comma - $start - 11);
         $pos =  substr($text, $comma + 1, $end - $comma - 1);
         $text = substr($text,0 , $start) . get_image($cmdb, $aid, $pos, CPG_IMAGE_TYPE) . substr($text, $end + 1);
         $offset = $end;
      }
   } while ($start !== false);
   //$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
   return $text;
}

/////////////////////////////////////////////////////////////////////

function get_image(& $wpdb, $aid, $pos, $type='normal_') {
   $res = '';
   $wpdb->query('SELECT value FROM '.CPG_TABLE_PREFIX.'config WHERE name="ecards_more_pic_target" OR name="fullpath"');
   $config = $wpdb->get_col();
   $wpdb->query('SELECT filepath, filename, title FROM '.CPG_TABLE_PREFIX.'pictures WHERE aid='.$aid.'  LIMIT 1 OFFSET '.($pos - 1));
   $image = $wpdb->get_row();
   if ($image) {
      $uri = $config[0].'/'.$config[1].$image->filepath.$type.$image->filename;
      $res = '<img border="0" src="'.$uri.'" class="cpg_'.$type.'img" alt="'.$image->title.'" title="'.$image->title.'" />';
   }
   return $res;
}

/////////////////////////////////////////////////////////////////////

function get_album(& $wpdb, $aid) {
   $res = '';
   $wpdb->query('SELECT value FROM '.CPG_TABLE_PREFIX.'config WHERE name="ecards_more_pic_target" OR name="fullpath"');
   $config = $wpdb->get_col();
   $wpdb->query('SELECT filepath, filename, title FROM '.CPG_TABLE_PREFIX.'pictures WHERE aid='.$aid);
   $images = $wpdb->get_results();
   if ($images) {
      $res .= '<div class="cpg_album">';
      foreach($images as $image) {
         $uri = $config[0].'/'.$config[1].$image->filepath;
         $res .= '<a href="javascript:void(0);" onclick="window.open(\''.$uri.'normal_'.$image->filename.'\',\'myWindow\',\'width=585,height=415\');"><img border="0" src="'.$uri.'thumb_'.$image->filename.'" class="cpg_thumb_img" alt="'.$image->title.'" title="'.$image->title.'" /></a>';
      }
      $res .= (CPG_ALBUM_TEXT ? '<br/><a href="'.$config[0].'/thumbnails.php?album='.$aid.'" class="cpg_album_link">'.CPG_ALBUM_TEXT.'</a>' : '');
      $res .= '</div>';
   }
   return $res;
}
/////////////////////////////////////////////////////////////////////

add_filter('the_content', 'cpg');

?>