<?php
/*************************
  Coppermine Photo Gallery
  ************************
  Copyright (c) 2003-2005 Coppermine Dev Team
  v1.1 originaly written by Gregory DEMAR

  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.
  ********************************************
  Coppermine version: 1.4.1
  $Source: /cvsroot/coppermine/devel/plugins/sample/codebase.php,v $
  $Revision: 1.8 $
  $Author: oddeveloper $
  $Date: 2005/02/20 10:30:05 $
**********************************************/
if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');
include('configuration.php');
$TABLE_PREFIX=$CONFIG['TABLE_PREFIX'];
$RESULT = cpg_db_query	(	"SELECT * FROM {$CONFIG['TABLE_ALBUMS']},{$CONFIG['TABLE_PICTURES']} 
							 WHERE {$CONFIG['TABLE_ALBUMS']}.aid={$CONFIG['TABLE_PICTURES']}.aid
							 ORDER BY {$CONFIG['TABLE_ALBUMS']}.title"
						);
while ($total = mysql_fetch_array($RESULT))
	{
	$ALBUMS_DIR=$CONFIG['fullpath'];									//eg "Albums/"

	$OLD_DIR=$ALBUMS_DIR."".$total[15];									//eg Albums/trees/
	$OLD_FILE=$total[16];												//eg greentree.jpg
	$OLD_PATH_N_FILE=$OLD_DIR."".$OLD_FILE;								//eg Albums/trees/greentree.jpg
	$OLD_PATH_N_FILE_NORMAL=$OLD_DIR."normal_".$OLD_FILE;				//eg Albums/trees/normal_greentree.jpg
	$OLD_PATH_N_FILE_THUMB=$OLD_DIR."thumb_".$OLD_FILE;					//eg Albums/trees/thumb_greentree.jpg

	$NEW_TMP_DIR=str_replace("/","_",$total[1]);						//eg "Forest_winter 2005" instead of "Forest/winter 2005"
	$NEW_TMP_DIR=str_replace(" ","_",$NEW_TMP_DIR);						//eg "Forest_winter_2005" instead of "Forest_winter 2005"	
	$NEW_DIR_NO_AD=$imagefolder."".$NEW_TMP_DIR."/";					//eg images/Forest_winter_2005/ 				(NO_AD=NO Albums Directory, needed for the database)
	$NEW_DIR=$ALBUMS_DIR."".$imagefolder."".$NEW_TMP_DIR."/";			//eg Albums/images/Forest_winter_2005/
	$NEW_PATH_N_FILE=$NEW_DIR."".$OLD_FILE;								//eg Albums/images/Forest_winter_2005/greentree.jpg (I'm using the $OLD_FILE, cause i don't change the filename)
	$NEW_PATH_N_FILE_NORMAL=$NEW_DIR."normal_".$OLD_FILE;				//eg Albums/images/Forest_winter_2005/normal_greentree.jpg
	$NEW_PATH_N_FILE_THUMB=$NEW_DIR."thumb_".$OLD_FILE;					//eg Albums/images/Forest_winter_2005/thumb_greentree.jpg
	
	if ($OLD_PATH_N_FILE != $NEW_PATH_N_FILE)							//If these are the same, file doesn't have to be moved
	{										
		if (!file_exists($NEW_DIR)) 									//Check if directory doesnet exist
			{
			mkdir ($NEW_DIR, 0777);										//If it doesn't create it
			chmod ($NEW_DIR, 0777);										//and change the rights
			}
		if (!file_exists($NEW_PATH_N_FILE))								//Check if $NEW_PATH_N_FILE doesn't exist
			{copy ($OLD_PATH_N_FILE,$NEW_PATH_N_FILE);					//If it doesn't copy it
			 unlink ($OLD_PATH_N_FILE);									//Delete the original file
			}
		if (!file_exists($NEW_PATH_N_FILE_NORMAL))						//Check if $NEW_PATH_N_FILE_NORMAL doesn't exist
			{copy ($OLD_PATH_N_FILE_NORMAL,$NEW_PATH_N_FILE_NORMAL);	//If it doesn't copy it
			 unlink ($OLD_PATH_N_FILE_NORMAL);							//Delete the original normal_file
			}

			if (!file_exists($NEW_PATH_N_FILE_THUMB))					//Check if $NEW_PATH_N_FILE_THUMB doesn't exist
			{copy ($OLD_PATH_N_FILE_THUMB,$NEW_PATH_N_FILE_THUMB);		//If it doesn't copy it
			 unlink ($OLD_PATH_N_FILE_THUMB);							//Delete the original thumb_file
			}			
	$UPDATE_SQL_QUERY="UPDATE ".$CONFIG['TABLE_PICTURES']." SET filepath='".$NEW_DIR_NO_AD."' WHERE pid=".$total[13];	//This is the sql statement that modifies the database so the new dir's are corrected
	cpg_db_query($UPDATE_SQL_QUERY);									//execute the query
	}
	
}
?>