-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_class.php
executable file
·202 lines (169 loc) · 6.29 KB
/
file_class.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
error_reporting(E_ALL ^ E_NOTICE);
class file_object{
var $id; // The id of this file, matches up with the id field in anyInventory_files
var $item_id; // The id of the item that owns this file, matches up with the id field in anyInventory_items
var $file_name; // The name of this file.
var $file_type; // The mime type of this file.
var $file_size; // The size of this file, in bytes
var $web_path; // The path to this file on the Web.
var $server_path; // The path to this file on the server.
var $is_remote = false; // Whether or not this is a remote file
function file_object($id){
global $DIR_PREFIX;
global $db;
// Set the id of this file.
$this->id = $id;
// Get the information about this file.
$query = "SELECT * FROM " . $db->quoteIdentifier('anyInventory_files') . " WHERE " . $db->quoteIdentifier('id') . "='".$this->id."'";
$result = $db->query($query);
if(DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
$row = $result->fetchRow();
// Set the id of the item that owns this file.
$this->item_id = $row["key"];
if (trim($row["offsite_link"]) != ''){
$this->is_remote = true;
}
if ($this->is_remote){
$this->web_path = $row["offsite_link"];
}
else{
// Set the file information.
$this->file_name = $row["file_name"];
$this->file_size = $row["file_size"];
$this->file_type = $row["file_type"];
$this->web_path = $DIR_PREFIX."get_file.php?fid=".$this->id;
$this->server_path = realpath($DIR_PREFIX."get_file.php")."?fid=".$this->id;
}
}
// This function returns a link to the file that contains the file name, file type, and file size.
function get_download_link(){
$link = '<a href="'.$this->web_path.'">';
if ($this->is_remote){
if (strlen($this->web_path) > 64){
$link .= substr($this->web_path, 0, 64) . '...';
}
else{
$link .= $this->web_path;
}
}
else{
$link .= $this->file_name.' ('.$this->file_type.', '.round($this->file_size / 1000).' KB)';
}
$link .= '</a>';
return $link;
}
// This function returns true ifthe file is an image, false otherwise.
function is_image(){
if (stristr($this->file_type, "image/") !== false){
return true;
}
else{
return false;
}
}
// This function determines whether a thumbnail can be created for this file.
function has_thumbnail(){
// The only supported types currently are jpeg and png.
if ((stristr($this->file_type, "jpg") !== false) || (stristr($this->file_type, "png") !== false) || (stristr($this->file_type, "jpeg") !== false)){
// Make sure the necessary functions exist.
if (function_exists('getimagesize') &&
function_exists('imagecreate') &&
function_exists('imagecreatefromjpeg') &&
function_exists('imagecopyresized') &&
function_exists('imagedestroy') &&
function_exists('imagecreatefrompng') &&
function_exists('imagejpeg')){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
// This function outputs a thumbnail of the file (if it is an image) directly to the browser.
function output_thumbnail(){
// The maximum width and height for the thumnbail.
$thumb_width = 120;
$thumb_height = 120;
// Get the information about the image.
if (is_file(realpath("get_file.php?fid=".$this->id))){
$image_info = getimagesize("get_file.php?fid=".$this->id);
}
else{
echo realpath("get_file.php?fid=".$this->id);
exit;
}
$image_width = $image_info[0];
$image_height = $image_info[1];
// A thumbnail only needs to be created if the image is bigger than the max width or height.
if (($image_width > $thumb_width) || ($image_height > $thumb_height)){
// Set the ratio from the largest side.
if (($image_width / $thumb_width) > ($image_height > $thumb_width)){
$ratio = $thumb_height / $image_height;
}
else{
$ratio = $thumb_width / $image_width;
}
// Set the dimension of the thumbnail
$new_image_width = round($ratio * $image_width);
$new_image_height = round($ratio * $image_height);
// Create the thumbnail based on the image type.
switch($image_info[2]){
case 2:
// JPG
$thumb = imagecreate($new_image_width, $new_image_height);
$image = imagecreatefromjpeg($this->server_path);
imagecopyresized($thumb, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $image_width, $image_height);
imagedestroy($image);
break;
case 3:
// PNG
$thumb = imagecreate($new_image_width, $new_image_height);
$image = imagecreatefrompng($this->server_path);
imagecopyresized($thumb, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $image_width, $image_height);
imagedestroy($image);
break;
}
}
else{
switch($image_info[2]){
case 2:
// JPG
$thumb = imagecreatefromjpeg($this->server_path);
break;
case 3:
// PNG
$thumb = imagecreatefrompng($this->server_path);
break;
}
}
// Output the new image.
header("Content-type: image/jpeg");
imagejpeg($thumb, '', 100);
// Destroy the new image.
imagedestroy($thumb);
exit;
}
function delete_self(){
global $DIR_PREFIX;
global $db;
if ($this->is_remote){
$query = "DELETE FROM " . $db->quoteIdentifier('anyInventory_files') . " WHERE " . $db->quoteIdentifier('id') . "='".$this->id."'";
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
}
else{
$delquery = "DELETE FROM " . $db->quoteIdentifier('anyInventory_files') . " WHERE " . $db->quoteIdentifier('id') . "='".$this->id."'";
$delresult = $db->query($delquery);
if (DB::isError($delresult)) die($delresult->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $delquery);
$delquery = "DELETE FROM " . $db->quoteIdentifier('anyInventory_file_data') . " WHERE " . $db->quoteIdentifier('file_id') . "='".$this->id."'";
$delresult = $db->query($delquery);
if (DB::isError($delresult)) die($delresult->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $delquery);
}
}
}
?>