-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileObject.php
More file actions
executable file
·370 lines (330 loc) · 9.97 KB
/
fileObject.php
File metadata and controls
executable file
·370 lines (330 loc) · 9.97 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/*
File: lib/fileObject.php
Author: Nathan Davies
Created: 19th September 2008
Purpose: Used for uploading files.
Ammendments:
*/
class fileObject
{
// General Properties
var $loDataObject ;
var $lcFileName ;
var $lcTmpName ;
var $lnFileSize ;
var $lcFileExt ;
var $lnMaxSize ;
var $lcFileLocation ;
var $lnArrayType ;
var $lcTargetDirectory ;
var $lnStatus ;
// FTP properties
var $ftp_connection_ID ;
var $ftp_server ;
var $ftp_username ;
var $ftp_password ;
var $ftp_isLogedIn ;
var $ftp_change ;
var $ftp_maxSize ;
public function fileObject($poDB, $pnMaxSize, $pcTargetDirectory, $pnArrayType)
{
$this->lnMaxSize = $pnMaxSize ;
$this->lcTargetDirectory = $pcTargetDirectory ;
$this->lnStatus = 0 ;
$this->lnArrayType = $pnArrayType ;
} // end of fileObject function
function setMainProperties($pnResourceArray, $pnMaxSize, $pcResourceTarget)
{
$this->lnArrayType = $pnResourceArray ;
$this->lnMaxSize = $pnMaxSize ;
$this->lcTargetDirectory = $pcResourceTarget ;
} // end of setMainProperties function
/*-----------------------------------------------------------------------
FILE LOADING FUNCTIONS
-----------------------------------------------------------------------*/
function fileUpload($pcFileName, $pcTmpName, $pnFileSize, $pcFileExt)
{
$this->lcFileName = $pcFileName ;
$this->lcTmpName = $pcTmpName ;
$this->lnFileSize = $pnFileSize ;
$this->lcFileExt = $pcFileExt ;
// check the type
$llContinue = $this->checkType() ;
// check the size
$llContinue = $this->checkSize() ;
if($llContinue == true)
{
$lcTarget_path = $this->lcTargetDirectory . $this->lcFileName;
if(is_uploaded_file($this->lcTmpName))
{
if(move_uploaded_file($this->lcTmpName, $lcTarget_path))
{
$llFileLoaded = true ;
$this->setFileLocation($lcTarget_path) ;
}
else
{
$llFileLoaded = false ;
$this->lnStatus = $this->lnStatus + 8 ;
}
}
else
{
$llFileLoaded = false ;
$this->lnStatus = $this->lnStatus + 4 ;
}
}
return $llFileLoaded ;
} // end of fileUpload function
/*-----------------------------------------------------------------------
FTP LOADING FUNCTIONS
-----------------------------------------------------------------------*/
function initialiseFTP($pcFTPServer, $pcFTPUser, $pcFTPPass, $pnMaxSize)
{
// set up the properties relating to FTP usage
$this->ftp_server = $pcFTPServer ;
$this->ftp_username = $pcFTPUser ;
$this->ftp_password = $pcFTPPass ;
$this->ftp_isLoggedID = false ;
$this->ftp_change = false ;
$this->lnMaxSize = $pnMaxSize ;
} // end of initialiseFTP function
function ensureFTPConnection()
{
$llReturn = false ;
if(!$this->ftp_connection_ID)
{
$this->ftp_connection_ID = ftp_connect($this->ftp_server) ;
if($this->ftp_connection_ID)
{
$llReturn = false ;
}
}
return $llReturn ;
} // end of ensureFTPConnection function
function FTPClose()
{
if($this->ftp_connection_ID)
{
ftp_close($this->ftp_connection_ID) ;
}
} // end of FTPClose function()
function FTPLogin()
{
$llLogin = false ;
// this should ensure there is a connection first
if( $this->ensureFTPConnection())
{
$llReturn = @ftp_login($this->ftp_connection_ID, $this->ftp_username, $this->ftp_password);
$this->ftp_isLoggedIn = $llReturn ;
}
return $llReturn ;
} // end of FTPLogin function
function FTPUpload($pcDdestination_file, $pcSource_file, $pnFileSize, $pcFileExt)
{
$llUpload = false ;
if(!$this->ftp_isLoggedIn)
{
$this->FTPLogin() ;
}
$this->lnFileSize = $pnFileSize ;
$this->lcFileExt = $pcFileExt ;
// check the type
$llContinue = $this->checkType() ;
// check the size
$llContinue = $this->checkSize() ;
if($llContinue)
{
$llUpload = ftp_put($this->ftp_connection_ID, $pcDdestination_file, $pcSource_file, FTP_BINARY);
if($llUpload)
{
$this->ftp_change = ftp_site($this->ftp_connection_ID, "chmod777".$this->file_destination_file ) ;
$this->setFileLocation($pcDdestination_file) ;
}
}
return $llUpload ;
} // end of FTPUpload function
/*-----------------------------------------------------------------------
ANCILLIARY FUNCTIONS
-----------------------------------------------------------------------*/
function checkType()
{
$llTypeValid = true ;
switch($this->lnArrayType)
{
case 1:
$laExtList = array (
'zip' => 'application/zip',
'pdf' => 'application/pdf',
'doc' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'exe' => 'application/octet-stream',
) ;
break ;
case 2:
$laExtList = array (
'gif' => 'image/gif',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'pjpeg' => 'image/pjpeg',
) ;
break ;
case 3:
$laExtList = array (
'mp3' => 'audio/mpeg',
'm3u' => 'audio/x-mpegurl',
'wav' => 'audio/x-wav',
) ;
break ;
case 4:
$laExtList = array (
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo'
) ;
break ;
case 5:
$laExtList = array (
'txt' => 'text/plain'
) ;
break ;
default:
$laExtList = array (
'zip' => 'application/zip',
'pdf' => 'application/pdf',
'doc' => 'application/msword',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'exe' => 'application/octet-stream',
'gif' => 'image/gif',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'pjpeg' => 'image/pjpeg',
'mp3' => 'audio/mpeg',
'm3u' => 'audio/x-mpegurl',
'wav' => 'audio/x-wav',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo',
'txt' => 'text/plain'
) ;
break ;
}
// check the file type is valid
if(!in_array($this->lcFileExt, $laExtList))
{
$this->lnStatus = $this->lnStatus + 1 ;
$llTypeValid = false ;
}
return $llTypeValid ;
} // end of checkType function
function checkSize()
{
// check the size of the file is within the preset limit
$llSizeValid = true ;
if($this->lnFileSize > $this->lnMaxSize)
{
$this->lnStatus = $this->lnStauts + 2 ;
$llSizeValid = false ;
}
return $llSizeValid ;
} // end of checkSize function
function checkURL($pcURL, $pcReturnKey)
{
// check the supplied URL and return the value associated with the specified key
$laHeaders = @get_headers($pcURL, 1) ; // return the array with keys set
$lvReturn = $laHeaders[$pcReturnKey] ;
return $lvReturn ;
} // end of checkURL function
function parseFileError($pnErrorNumber)
{
switch($pnErrorNumber)
{
case 1: // bad type
$lcReturn = 'Selected file is not of the correct type' ;
break ;
case 2: // bad size
$lcReturn = 'Selected file exceeds the maximum file size of ' . ($this->lnMaxSize/1024/1024) . 'Mb' ;
break ;
case 3: // bad size and bad type
$lcReturn = $this->parseFileError(1) . '<br />' . $this->parseFileError(2) ;
break ;
case 4: // upload failed
$lcReturn = 'The file is fine but for some reason it was not possible to upload it at this time' ;
break ;
case 5:
$lcReturn = $this->parseFileError(1) . '<br />' . $this->parseFileError(4) ;
break ;
case 6:
$lcReturn = $this->parseFileError(2) . '<br />' . $this->parseFileError(4) ;
break ;
case 7:
$lcReturn = $this->parseFileError(3) . '<br />' . $this->parseFileError(4) ;
break ;
case 8: // move failed
$lcReturn = 'The file was uploaded but it could not be moved to the target location' ;
break ;
}
return $lcReturn ;
} // end of parseFileError function
function setFileLocation($pcDdestination_file)
{
$lnEndPos = strpos($pcDdestination_file, '/htdocs/') ; // this is the start of the string
$lnEndPos = $lnEndPos + 7;
$lcFilePath = "http://" . $_SERVER['HTTP_HOST'] . substr($pcDdestination_file, $lnEndPos) ;
$this->lcFileLocation = $lcFilePath ;
} // end of setFileLocation function
function imageResize($pcImagePath, $pnTargetSize, $pcTitle, $pcAlt, $pcClass, $pcType)
{
$laImage = getimagesize($_SERVER['DOCUMENT_ROOT'] . '/' . $pcImagePath) ;
$lnWidth = $laImage[0] ;
$lnHeight = $laImage[1] ;
//takes the larger size of the width and height and applies the formula accordingly...this is so this script will work dynamically with any size image
switch($pcType)
{
case "w" : // reset the width
$lnPercentage = ($pnTargetSize / $lnWidth);
$lnTestSize = $lnWidth ;
break ;
case "h" : // reset the height
$lnPercentage = ($pnTargetSize / $lnHeight);
$lnTestSize = $lnHeight ;
break ;
case "r" : // reset the biggest dimension retaining aspect ratio
if ($lnWidth > $lnHeight)
{
$lnPercentage = ($pnTargetSize / $lnWidth);
$lnTestSize = $lnWidth ;
}
else
{
$lnPercentage = ($pnTargetSize / $lnHeight);
$lnTestSize = $lnHeight ;
}
break ;
}
//gets the new value and applies the percentage, then rounds the value
if($lnTestSize > $pnTargetSize) // only resize down
{
$lnWidth = round($lnWidth * $lnPercentage);
$lnHeight = round($lnHeight * $lnPercentage);
}
//returns the complete image tag
$lcImageTag = '<img src="' . $pcImagePath . '" title="' . $pcTitle . '" alt="' . $pcAlt .'" ' ;
if(!empty($pcClass))
{
$lcImageTag .= 'class="' . $pcClass . '" ' ;
}
$lcImageTag .= 'height="' . strVal($lnHeight) . '" width="' . strVal($lnWidth) .'" />' ;
return $lcImageTag ;
} // end of imageResizeFunction
} // end of class definition - fileObject
?>