I'm looking for a php solution to digitally watermark files. I'll explain the situation so maybe someone can help me.
People login to a member area. There they can download files.
At this time a file is sent to them via a script that hides the download path. When a user has downloaded the file, a log is written into a mysql table with their ip address and other info regarding the download.
My goal is to add a random unique string (doesn't really matter what kind of string) to the file before it is sent to the user. Of couse the string shouldn't render the file unusable. Also it should be possible to add it to all types of files (exe, pdf, etc.)
I plan to add that unique string to the database table too, so if anyone distributes the file illegally and I get a hold of it, I can trace the client that distributed it via that string.
I searched all over the net, but couldn't find a single solution or at least part of it to get started.
Can anyone here tell if it is possible at all and/or give me a lead?
Thanks in advance.
__________________
[AND THEN THERE WERE NONE - SAF OUT]
As I said I have a file which controls the download sending and logging.
First a made a function that returns the unique string, I used a md5 encoding of the current date and time:
PHP Code:
function random()
{
global $randomstring;
$datx = mktime();
$randomstring = md5($datx);
return $randomstring;
}
Next I create a function to control the sending of the file. Here the random string is added...
$name represents the actual name of the file (e.g. download.exe) and $filename represents the name of the file (e.g. Download).
Also: I use a copy function to create a new file with the watermark, the location of that file is stored in the var $dest_file.
PHP Code:
function send_file($name,$filename)
{
$status = FALSE;
$len = strlen($name);
$extension = substr($name,$len-3,$len);
$randomstring = random();
$path = "/path/to/file/".$name;
$dest_file = "/path/to/file/".$randomstring.$name;
copy($path, $dest_file);
$fp = fopen($dest_file,'a');
if(!$fp)
{
echo "Error opening file";
exit;
}
else
{
fwrite($fp, $randomstring);
if(!fclose($fp))
{
echo "Error closing file";
exit;
}
}
if (!is_file($dest_file) or connection_status()!=0) return(FALSE);
header("Content-type: application/octet-stream");
header("Content-Disposition: inline; filename=\"".$filename.".".$extension."\"");
header("Content-length: ".(string)(filesize($dest_file)));
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
And finally to send the file and log stuff to the database:
PHP Code:
if (!send_file($name,$filename))
{
die ("Error sending file...");
}
else
{
mysql_connect($dbserver, $dbuser, $dbpass)
or die ("Database error");
mysql_select_db($dbname);
if (getenv(HTTP_X_FORWARDED_FOR))
{
$ip = getenv(HTTP_X_FORWARDED_FOR);
}
else
{
$ip = getenv(REMOTE_ADDR);
}
$result = mysql($dbname, "INSERT INTO downloads (userid, file, date, time, ip, watermark) VALUES ('$id', '$name', '$currentdate', '$currenttime', '$ip','$randomstring')") or die ("Error adding details to database.");
}
I know my code is quite sloppy, hope you understand how it works, I didn't write the file send code myself, I found it somewhere, and edited it to add the watermark.
If there are any questions I will try to help
__________________
[AND THEN THERE WERE NONE - SAF OUT]