5 # Part of the Collection Workflow Integration System (CWIS) 6 # Copyright 2010-2017 Edward Almasy and Internet Scout Research Group 7 # http://scout.wisc.edu/cwis/ 16 # ---- PUBLIC INTERFACE -------------------------------------------------- 18 # status codes (set by constructor and returned by File::Status()) 34 public static function Create($SourceFile, $DesiredFileName = NULL)
36 # check that file exists 37 if (!file_exists($SourceFile))
39 return self::FILESTAT_DOESNOTEXIST;
42 # check that file is readable 43 if (!is_readable($SourceFile))
45 return self::FILESTAT_UNREADABLE;
48 # check that file is not zero length 49 $FileSize = filesize($SourceFile);
52 return self::FILESTAT_ZEROLENGTH;
55 # generate secret string (used to protect from unauthorized download) 56 srand((
double)microtime() * 1000000);
57 $SecretString = sprintf(
"%04X", rand(1, 30000));
59 # get next file ID by adding file to database 61 $DB->Query(
"INSERT INTO Files (SecretString) VALUES ('".$SecretString.
"')");
62 $FileId =
$DB->LastInsertId();
64 # build name for stored file 65 $BaseFileName = ($DesiredFileName === NULL)
66 ? basename($SourceFile) : basename($DesiredFileName);
67 $StoredFile = sprintf(self::GetStorageDirectory().
"/%06d-%s-%s",
68 $FileId, $SecretString, $BaseFileName);
70 # attempt to copy file to storage 71 $Result = copy($SourceFile, $StoredFile);
73 # if copy attempt failed 74 if ($Result === FALSE)
76 # remove file from database 77 $DB->Query(
"DELETE FROM Files WHERE FileId = ".$FileId);
79 # report error to caller 80 return self::FILESTAT_COPYERROR;
83 # attempt to get file type 84 $FileType = self::DetermineFileType($SourceFile);
86 # save file info in database 87 $DB->Query(
"UPDATE Files SET" 88 .
" FileName = '".addslashes($BaseFileName).
"'," 89 .
" FileType = '".addslashes($FileType).
"'," 90 .
" FileLength = '".addslashes($FileSize).
"'" 91 .
" WHERE FileId = ".$FileId);
93 # instantiate new object and return it to caller 94 return new File($FileId);
105 if (!$Copy instanceof
self)
107 throw new Exception(
"Copy failed with error ".$Copy);
110 $Copy->FieldId($this->
FieldId());
120 return $this->ValueCache[
"FileLength"];
129 return $this->ValueCache[
"FileType"];
139 return $this->
UpdateValue(
"FileComment", $NewValue);
159 return $this->
UpdateValue(
"ResourceId", $NewValue);
168 return strlen($this->
GetType())
169 ? $this->
GetType() :
"application/octet-stream";
179 # if CleanURLs are enabled, use the redirect that includes 180 # the file name so that browsers don't use index.php as the name 181 # for the downloaded file 182 if ($GLOBALS[
"G_PluginManager"]->PluginEnabled(
"CleanURLs"))
184 return "downloads/".$this->Id.
"/".rawurlencode($this->
Name());
187 # otherwise use the download portal 190 return "index.php?P=DownloadFile&Id=".$this->Id;
202 if (file_exists($FileName))
227 # for each possible storage location 228 foreach (self::$StorageLocations as $Dir)
230 # build file name for that location 231 $FileName = sprintf($Dir.
"/%06d-%s-%s",
232 $this->Id, $this->ValueCache[
"SecretString"], $this->Name());
234 # if file can be found in that location 235 if (file_exists($FileName))
237 # return file name to caller 242 # build file name for default (most preferred) location 243 $FileName = sprintf(self::GetStorageDirectory().
"/%06d-%s-%s",
244 $this->
Id, $this->ValueCache[
"SecretString"], $this->
Name());
246 # return file name to caller 256 # for each possible storage location 257 foreach (self::$StorageLocations as $Dir)
259 # if location exists and is writeable 260 if (is_dir($Dir) && is_writeable($Dir))
262 # return location to caller 267 # return default (most preferred) location to caller 268 return self::$StorageLocations[0];
272 # ---- PRIVATE INTERFACE ------------------------------------------------- 275 static private $StorageLocations = array(
289 if (function_exists(
"mime_content_type"))
291 $FileType = mime_content_type($FileName);
293 # Although mime_content_type is baked into PHP5 and PHP7, it is still 294 # part of extensions in some versions of PHP 5.x. 295 elseif (function_exists(
"finfo_open"))
297 $FInfoHandle = finfo_open(FILEINFO_MIME);
300 $Result = finfo_file($FInfoHandle, $FileName);
301 finfo_close($FInfoHandle);
304 $FileType = $FInfoMime;
309 # handle Office XML formats 310 # These are recognized by PHP as zip files (because they are), but 311 # IE (and maybe other things?) need a special-snowflake MIME type to 312 # handle them properly. 313 # For a list of the required types, see 314 # https://technet.microsoft.com/en-us/library/ee309278(office.12).aspx 315 if ($FileType ==
"application/zip; charset=binary")
317 $MsftPrefix =
"application/vnd.openxmlformats-officedocument";
319 $FileExt = strtolower(pathinfo($FileName, PATHINFO_EXTENSION));
324 $FileType = $MstfPrefix.
".wordprocessingml.document";
328 $FileType = $MsftPrefix.
".spreadsheetml.sheet";
332 $FileType = $MsftPrefix.
".presentationml.slideshow";
const FILESTAT_ZEROLENGTH
GetNameOfStoredFile()
Returns the relative link to the stored file.
Name($NewValue=DB_NOVALUE)
Get/set name of item.
const FILESTAT_UNREADABLE
GetType()
Gets the file's type.
const FILESTAT_DOESNOTEXIST
UpdateValue($ColumnName, $NewValue=DB_NOVALUE)
Convenience function to supply parameters to Database::UpdateValue().
SQL database abstraction object with smart query caching.
Destroy()
Deletes the file and removes its entry from the database.
static Create($SourceFile, $DesiredFileName=NULL)
Create a new File object using an existing file.
FieldId($NewValue=DB_NOVALUE)
Gets or sets the field ID of the File.
static DetermineFileType($FileName)
Get MIME type for specified file, if possible.
GetLink()
Returns the relative download link to download the file.
const FILESTAT_PARAMERROR
Delete()
Deprecated method to delete file and remove entry from database.
GetMimeType()
Gets the MIME type of the file.
Common base class for persistent items store in database.
CreateCopy()
Create copy of File object.
GetLength()
Gets the length of the file.
static GetStorageDirectory()
Get file storage directory.
ResourceId($NewValue=DB_NOVALUE)
Gets or sets the resource ID of the File.
Class representing a stored (usually uploaded) file.
Comment($NewValue=DB_NOVALUE)
Gets or sets the comment on the file.