/
fetchfile.php
181 lines (173 loc) · 5.68 KB
/
fetchfile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?
// Copyright 2002 Brian Ronald. All rights reserved.
// Portable PHP/MySQL Corporate Intranet System
//
// 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.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////////////
// This is the downloader for the library. It selects the details
// of the file ffrom the database, and then opens the disk file
// which contains the body (its filename being the same as the
// database index number) and prints it to output, after sending
// headers for filename and MIME type.
ob_start(4096);
set_time_limit(0);
// Session stuff, and general paranoia
extract($_SERVER);
extract($_REQUEST);
if(isset($userid))
{
unset($userid);
unset($username);
unset($firstname);
unset($lastname);
}
session_name('PPCIS');
session_start();
$loginhost=$_SESSION["loginhost"];
$help_keyword=$_SESSION["help_keyword"];
if(!isset($userid))
$userid = 0;
$userid=$_SESSION["userid"];
$firstname=$_SESSION["firstname"];
$lastname=$_SESSION["lastname"];
$help_keyword=$_SESSION["help_keyword"];
if($loginhost != $SERVER_NAME)
{ // This session was created on another vhost
unset($userid);
unset($firstname);
unset($lastname);
}
if(!isset($userid))
$userid = 0;
require("settings.inc");
// Connect to the database
if(!($intranet_db = @ mysqli_connect($db_hostname, $db_username, $db_password, $db_name)))
{
die("Database problem");
}
// Get user's preferences
$sql = "SELECT stylesheet,language FROM users LEFT JOIN preferences USING (userid) WHERE users.userid = $userid";
$result = @ mysqli_query($intranet_db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
// Set variables (hiding warning; there might not be any!)
if(!empty($row))
extract($row);
// Check language, and if necessary set default
if(strlen($language)<1)
$language = $default_lang;
require("languages/$language.inc");
// Check stylesheet, and if necessary set default
if(strlen($stylesheet)<1)
{
foreach($stylesheets as $find_sheet)
{
$stylesheet = $find_sheet;
break;
}
}
if($userid>0)
{
// Get the user's team for security checking
$sql = "SELECT team FROM userteams WHERE userid=$userid";
$result = @ mysqli_query($intranet_db,$sql);
if (mysqli_error($intranet_db))
die("Database problem");
if(@ mysqli_num_rows($result) != 0)
{
$i=0;
while($row = @ mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$userteam[$i]=$row["team"];
$i++;
}
}
// Check the validity of $fileid if set and
// determine how secure it is.
$sql = "SELECT filename,mimetype FROM files WHERE fileid = $fileid ORDER BY filename";
$result = @ mysqli_query($intranet_db,$sql);
if (mysqli_error($intranet_db))
die("Database problem");
if(@ mysqli_num_rows($result) != 0)
{
while($row = @ mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$filename=$row["filename"];
if (!isset($mimetype))
{
$mimetype = $row["mimetype"];
}
}
}
else
{
require("head.inc");
print("<h1>".$lang['file_not_exist']."</h1>");
require("tail.inc");
die();
}
// I re-use my query variables, so this is outside the above
// if statement. If the file doesn't exist, this has no impact
// anyway, as the COUNT(*) should return zero, unless for some
// reason there's duff data in the filesecurity table.
$sql = "SELECT COUNT(*) FROM filesecurity WHERE fileid=$fileid";
$result = @ mysqli_query($intranet_db,$sql);
$securefile = TRUE; // This must be true
$row = mysqli_fetch_array($result,MYSQLI_NUM);
if($row[0] > 0)
{ // This file has one or more security records
$sql = "SELECT teamid FROM filesecurity WHERE fileid=$fileid";
$result = @ mysqli_query($intranet_db,$sql);
while($row = @ mysqli_fetch_array($result,MYSQLI_ASSOC))
{ // Check to see if our team is on the list
foreach($userteam as $teamtest)
if($row["teamid"]==$teamtest)
$securefile = FALSE;
}
}
else // There is no access list, there is no restriction.
$securefile = FALSE;
if($securefile)
{
require("head.inc");
print("<h1>".$lang['file_insufficient_privileges']."</h1>");
require("tail.inc");
die();
}
if(!($file_to_send=@fopen($file_store.$fileid, "rb")))
{
require("head.inc");
print("<h1>".$lang['file_corrupted_missing']."</h1>");
require("tail.inc");
die();
}
header("Content-Type: $mimetype");
header("Content-Disposition: filename=\"$filename\"");
header("Cache-Control: ");// If used in SSL, IE has a bug where
header("Pragma: "); // it can't connect without these lines
header("Content-length: ".(string)(filesize($file_store.$fileid)));
session_write_close();
while(!feof($file_to_send))
{
print(fread($file_to_send,64));
}
fclose($file_to_send);
}
else
{
require("head.inc");
print("<h1>".$lang['file_insufficient_privileges']."</h1>");
require("tail.inc");
}
?>