<?php
/*
Plugin Name: Qdig Photo Gallery
Plugin URI: http://randomfrequency.net/wordpress/qdig/
Description: Allows you to embed <a href="http://qdig.sourceforge.net/">Qdig (Quick Digital Image Gallery)</a> galleries in posts or pages.
Version: 0.1
Author: David B. Nagle
Author URI: http://randomfrequency.net/
*/ 

/*
== License ==

Copyright 2005 David Nagle
This program is distributed under the terms of the
GNU General Public License, Version 2

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, Version 2 as
published by the Free Software Foundation.

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,
Version 2 along with this program; if not, visit GNU's Home Page
http://www.gnu.org/
*/

function qdig_embed($content) {
    
/* Only embed the gallery if this is a page or single post */
    
if(is_page() || is_single()) {
    
        if(
preg_match("/\[qdig.*\]/i"$content$matches)) {
            
$replace $matches[0];
        
            if(
preg_match("/\spath=(\S*?)[\]\s]/i"$replace$matches)) {
                
$_GET['Qwd'] = $matches[1];
            }

            if(
preg_match("/\ssize=(S|M|L|XL|O)[\]\s]/i"$replace$matches)) {
                
$_GET['Qis'] = $matches[1];
            }

            if(
preg_match("/\slinks=(thumbs|name|num|none)[\]\s]/i"$replace$matches)) {
                
$_GET['Qiv'] = $matches[1];
            }

            
/* Qdig needs these to be globals, otherwise it can't find its settings */
            
global $abs_url_path$caption_min_width,
            
$caption_nl2br$caption_padding$caption_path,
            
$chroot_dir$cnvrt_alt$cnvrt_mesgs$cnvrt_path,
            
$cnvrt_size$cnvrt_thmb$cnvrtd_dir$compat_quote,
            
$convert_GD$convert_GD_ver$convert_cmd,
            
$convert_magick$convert_readable$convert_writable,
            
$copyright$ctrl_bar$dir_nav$display_size,
            
$excl_dirs$excl_img_pattern$excl_imgs$excl_main,
            
$extra_param$extra_paranoia$file_exists_disa,
            
$footer_pad_top$get_vars$ignore_dir_links,
            
$ignore_dotdirs$ignore_dotfiles$ignore_img_links,
            
$img_caption_above$img_link$img_links_sort_date,
            
$img_links_sort_rev$img_sz_labels$imgname_maxlen,
            
$imgs$is_readable_disa$lang$namelinks_disa,
            
$namelinks_ena$namelinks_small$namelinks_trunc,
            
$nav_lnk$next_img_msg$prev_img_msg$pwd,
            
$qdig_files_topdir$qdig_homelink$qdig_url,
            
$reqd_image$reqd_img_size_tmp$request_uri,
            
$rootdir$safe_captions$site_link_fnt_size,
            
$site_lnk_title$site_lnk_url$styl_grayout,
            
$subdirs$thmb_enable$thmb_onfly$thmbs_ena,
            
$thumbs_msg$touch_captions;

            
ob_start();
            include(
ABSPATH 'wp-content/plugins/qdig/index.php');
            
$gallery_content ob_get_contents();
            
ob_end_clean();

            
$content str_replace($replace$gallery_content$content);
        }
    }
    return 
$content;
}

function 
qdig_embed_alt($content) {
    if(!(
is_page() || is_single())) {
        if(
preg_match("/\[qdig.*\]/i"$content$matches)) {
            
$replace $matches[0];
            if(
preg_match("/\salt='([^']*)'[\]\s]/i"$replace$matches)) {
                
$link_text $matches[1];
            } elseif(
preg_match("/\salt=\"([^\"]*)\"[\]\s]/i"$replace$matches)) {
                
$link_text $matches[1];
            } elseif(
preg_match("/\salt=(\S*?)[\]\s]/i"$replace$matches)) {
                
$link_text $matches[1];
            } else {
                
$link_text "Visit Gallery";
            }
            
$gallery_link "<a href="get_permalink() . ">"$link_text ."</a>";
            
$content str_replace($replace$gallery_link$content);
        }
    }
    return 
$content;
}

/* The wptexturize function runs on the_content and the_excerpt at level 10. If
   it runs before qdig_embed_alt, it screws up the quotes. If it runs after
   qdig_embed, it screws up the code generated by qdig.
*/

add_filter('the_content''qdig_embed_alt'9);
add_filter('the_excerpt''qdig_embed_alt'9);
add_filter('the_content''qdig_embed'10);

?>