<?php
define('IN_COPPERMINE', true);

require('include/init.inc.php');

if (!GALLERY_ADMIN_MODE) cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);

pageheader();

//Search and replace: OLD to NEW
//you should probably use the full path, but mine were unique enough to forgo it.
//filepath does not include leading slashes but does include a trailing "/"
$changes[]=array('old','new');
$changes[]=array('old2','new2');
$changes[]=array('old3','new3');
// knock yourself out, I used this with 30 changes at once, I have no idea how well it scales beyond that
echo <<<EOT
<style type="text/css">
.title {
  color:white;
  background-color:black;
}
.red {
  color:white;
  background-color:red;
}
.green {
  color:white;
  background-color:green;
}
</style>
EOT;
echo "<table>";
foreach ($changes as $key => $change) {
    echo "<tr class=\"title\">";
    echo "<td>Key: $key</td>";
    echo "<td>From: {$change[0]}</td>";
    echo "<td>To: {$change[1]}</td>";
    echo "</tr>";
	$query_sql="SELECT filepath,pid,filename FROM {$CONFIG['TABLE_PICTURES']} WHERE filepath LIKE '%{$change[0]}%'";
    $results=cpg_db_query($query_sql);
    echo "<tr>";
    echo "<td>pid</td>";
    echo "<td>filepath</td>";
    echo "<td>filename</td>";
    echo "</tr>";	
    while ($row = mysql_fetch_assoc($results)) {
        $row['filepath']=addslashes(str_replace($change[0],$change[1],$row['filepath']));
        $update_sql="UPDATE {$CONFIG['TABLE_PICTURES']} SET filepath='{$row['filepath']}' WHERE pid='{$row['pid']}' LIMIT 1";
        $update_results=cpg_db_query($update_sql);
        if ($update_results) {
          $class="green";
        } else {
          $class="red";
        }
        echo "<tr class=\"$class\">";
        echo "<td>{$row["pid"]}</td>";
        echo "<td>{$row["filepath"]}</td>";
        echo "<td>{$row["filename"]}</td>";
        echo "</tr>";
    }
}
echo "</table>";
pagefooter();
ob_end_flush();

?>