<?php
if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');

//Obtain the PID and MD5 for intermediate pictures
$thisplugin->add_filter('file_data','download_pid');

function download_pid($data)
{
	global $information, $CONFIG;
	$information['pid'] = $data['pid'];
	$information['MD5'] = MD5($CONFIG['fullpath'] . $data['filepath'] . $data['filename']);
	return $data;
}

//Places a download link above the filename on the information list
$thisplugin->add_filter('file_info','download_link');

function  download_link($info)
{
	global $information;
	$info = array_reverse($info);
	$info['Download'] .= '<a href="./index.php?file=download/picture&pid=' . $information['pid'] . "&pr=" . $information['MD5'] . '&method=dl" >Download Original File</a>';
	$info = array_reverse($info);
	return $info;
}

//Loads all Coppermine files through a PHP script for security
//This requires manually placing a hook in the functions.inc.php file.
//Instructions:
//Find this line: return $pic_row['url'];
//And place this on the line before it:  $pic_row['url'] = CPGPluginAPI::filter('picture_url',$pic_row['url']);
$thisplugin->add_filter('picture_url','download_url');

function download_url($info)
{
	global $CONFIG;
	
	//print_r($info);exit();
	
	$url =& $info['url'];
	$method =& $info['mode'];
	$file =& $info['filename'];
	$path =& $info['filepath'];
	
	//Return the no thumb URL value for non supported file types
	if(substr($info['url'],0,7) == "images/")
		return $info;
	
	//  Set appropriate values for a thumbnail
	if($method == "thumb")
	{
		$modifier = "&method=thumb";
	}
	//  File is an Intermediate file
	else if($method == "normal")
	{
		$modifier = "&method=normal";
	}
	//  If there is no prefix then use the full file and indicate no method
	else if($method == "fullsize")
	{
		$modifier = "&method=fullsize";
	}
	
	//This should be totally unnecessary but just to be safe this will look up a missing PID
	if(!isset($info['pid']))
	{
		//  Query the database to locate the PID of the picture
		$result = cpg_db_query("SELECT pid FROM {$CONFIG['TABLE_PICTURES']} WHERE filepath='" . $path . "' AND filename='" . $file . "'");
		$row = mysql_fetch_array($result);
		mysql_free_result($result);
		$info['pid'] = $row['pid'];
	}

	$md5 = md5(rawurldecode($info['url']));
	
	//  If the PID was found construct the URL using the PID, MD5 Security, and the method
	if($info['pid'] != 0)	
		$url = "index.php?file=download/picture&pid=" . $info['pid'] . "&pr=" . $md5 . $modifier;
	
	//  Return the new URL to the hook
	return $info;
}

// Add an install action
$thisplugin->add_action('plugin_install','download_install');

// Install function
// Simply checks to see if the person clicked the "Go" button
function download_install() {

    // Install
    if ($_POST['test']=='true')
    {
        return true;

    // Loop again
    } else {

        return 1;
    }
}

// Add a configure action
$thisplugin->add_action('plugin_configure','download_configure');

// Configure function
// Displays the mod instructions to fully activate the plugin
function download_configure() {
    echo <<< EOT
    <h2>Modification required</h2>
    For this plugin to fully work a small modification is required to the <i>functions.inc.php</i> file located in the <i>include</i> folder.
	<br><br>
	<b>Find The Line:</b>
	<br><br>
    <i>return \$pic_row['url'];</i>
    <br><br>
	<b>Place This Line of Code Immediately Before It:</b>
	<br><br>
	<i>\$pic_row = CPGPluginAPI::filter('picture_url',\$pic_row);</i>
	<br><br>
	This plugin serves two basic purposes.  The first is adding a download link below all of the intermediate files.  This functionality will work as soon as this plugin is installed.  The second is redirecting all files through a PHP script.  This second functionality is dependant on the above modification.   However, no errors will occur if the above modification is not made and this plugin will simply perform only the first function.
   	<form action="{$_SERVER['REQUEST_URI']}" method="post">
	<input type="hidden" name="test" value="true" /><br />
	<input type="submit" value="Go!" />
	</form>
EOT;
}
?>
