Hello again,
I now,after a lot of help, got it working with this script;
<?php
$allowed = 1;
include 'config.php';
include 'db_info.php';
$referrer = getenv('HTTP_REFERER');
if('' == $referrer)
{
$allowed = ($allowblank) ? 1 : 0;
}
else
{
$allowed = 0;
foreach($alloweddomains as $domain)
{
if(substr($referrer, 0, strlen($domain)) == $domain)
{
$allowed = 1;
break;
}
}
}
if(!$allowed)
{
if($logging)
{
$status = 'Denied';
include 'logit.php';
}
exit(0);
//quiet leech kill
}
if(!isset($_GET['serve']) || $_GET['serve'] != (string) (int) $_GET['serve'] || (int) $_GET['serve'] <= 0)
{
die('Parameter `serve` must be a positive integer.');
}
$conn = mysql_connect("$sqlhost", "$sqlusername", "$sqlpassword")
or die('Unable to connect to MSQL: '.mysql_error($conn));
mysql_select_db('main', $conn)
or die('Unable to select database: '.mysql_error($conn));
$result = mysql_query('select `file_fullname` from '$file_tbl' where `file_id` = "'.$_GET['serve'].'"', $conn)
or die("Unable to perform query: ".mysql_error($conn));
if(0 == mysql_num_rows($result))
{
die('File not found.');
}
$fileName = mysql_result($result, 0, 0)
or die('Unable to retrieve result: '.mysql_error($conn));
$extension = (FALSE !== ($pos = strrpos($fileName, '.'))) ?
substr($fileName, $pos + 1) :
'';
// Content types block
switch($extension)
{
case 'avi':
$ct = 'video/avi';
break;
case 'bmp':
$ct = 'image/bmp';
break;
case 'gif':
$ct = 'image/gif';
break;
case 'jpeg':
case 'jpg':
case 'jpe':
$ct = 'image/jpeg';
break;
case 'mov':
$ct = 'video/quicktime';
break;
case 'mpeg':
case 'mpg':
case 'mpe':
$ct = 'video/mpeg';
break;
case 'png':
$ct = 'image/png';
break;
case 'swf':
$ct = 'application/x-shockwave-flash';
break;
case 'wmv':
$ct = 'video/x-ms-wmv';
break;
case 'rar':
case 'zip':
$ct = 'application/octet-stream';
break;
//END//
default:
$ct = 'application/octet-stream';
if($logging)
{
$status = 'Generic_Filetype';
include 'logit.php';
}
}
$handle = @fopen($path.$fileName, 'rb') or die('Unable to select file.');
if(!$handle)
{
die('Unable to transer file.');
}
header('Cache-Control: '); //keeps ie happy
header('Pragma: '); //keeps ie happy
header('Content-Type: '.$ct);
if('swf' != $extension) //flash plays, it isnt downloaded as an actual file.
{
header('Content-Disposition: attachment; filename="'.$fileName.'"');
}
header('Content-Length: '.filesize($path.$fileName));
fpassthru($handle);
if($logging)
{
$status = 'Granted';
include 'logit.php';
}
?>
Now I've only one problem left with this part (hopefully) that I can't solve myself...
Different users has permission to download different files. Here is the structure of the files table again;
CREATE TABLE `files` (
`file_id` int(11) NOT NULL auto_increment,
`file_pack` varchar(50) NOT NULL default '',
`file_pack_cat` varchar(50) NOT NULL default '',
`file_cat` varchar(50) NOT NULL default '',
`file_name` varchar(100) NOT NULL default '',
`file_desc` text NOT NULL,
`file_fullname` varchar(100) NOT NULL default '',
`file_downloads` varchar(11) NOT NULL default '',
`file_date` varchar(30) NOT NULL default '',
`file_timestamp` varchar(30) NOT NULL default '',
PRIMARY KEY (`file_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
And as you can see each file has a "file_name" and some of the files belongs to a "file_pack" with several files in it.
I'm planning to store the files each user has permission to download in another seperate table with the name "user_perm", here is the structure of that table;
CREATE TABLE `user_perm` (
`perm_id` int(11) NOT NULL auto_increment,
`perm_user` varchar(50) NOT NULL default '',
`file_pack` varchar(30) NOT NULL default '',
`file_name` varchar(100) NOT NULL default '',
`perm_date` varchar(30) NOT NULL default '',
`perm_timestamp` varchar(30) NOT NULL default '',
PRIMARY KEY (`perm_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
So if a user tries to download one file with, for example, the ID 1 the script must check the "file_name" AND "file_pack" of that file. Then it must check in the "user_perm" and see if the user has permission to download either the "file_name" OR the "file_pack". In other words, it is enough if the user has permission to download the "file_pack" to which the file belongs to.
I've at least started with this (but I don't know if it is right);
$result2 = mysql_query('select `file_name` , `file_pack` from '$file_tbl' where `file_id` = "'.$_GET['serve'].'"')
or die( mysql_error() );
Then I don't know how to check both of the things (both "file_name" and "file_pack"). AND I don't know where in the script I should add the lines.
When the user login the username and password is stored in a session with this lines;
session_register("myusername");
session_register("mypassword");
Also, Should I change the database structure or should I change something else in the structure of the system I'mn trying to build (for example with the user permission system)?
Thanks in advance,
Best Regards
Oskar R