public function filedepot::saveFile in filedepot 6
File
- ./
filedepot.class.php, line 851 - filedepot.class.php Main class for the Filedepot module
Class
- filedepot
- @file filedepot.class.php Main class for the Filedepot module
Code
public function saveFile($file, $validators = array()) {
global $user;
$nexcloud = filedepot_nexcloud();
// Check for allowable file type.
if (!$this
->checkFilter($file->name, $file->type)) {
$message = t('The file %name could not be uploaded. Mimetype %mimetype or extension not permitted.', array(
'%name' => $file->name,
'%mimetype' => $file->type,
));
drupal_set_message($message, 'error');
watchdog('filedepot', 'The file %name could not be uploaded. Mimetype %mimetype or extension not permitted.', array(
'%name' => $file->name,
'%mimetype' => $file->type,
));
return FALSE;
}
if ($file->folder > 0 and file_exists($this->tmp_storage_path) and is_writable($this->tmp_storage_path)) {
/* Tried to use the file_save_upload but was getting a PHP error in CCK but field_file_save_upload worked
* $nodefileObj = file_save_upload($file->tmp_name,array(), $this->tmp_storage_path);
*/
/* Attachment will be saved in the temporary directory with the PHPTMP filename
* Drupal files table recorc is created. The node_save API will move and rename the file
*/
$nodefile = field_file_save_file($file->tmp_name, array(), $this->tmp_storage_path);
// Determine the Drupal Files record id that was just created for the new file.
// The return array should have fid set which is the field value used in the CCK table record
// that maintains the attachment info for the CCK Folder Content record.
if (is_array($nodefile) and $nodefile['fid'] > 0) {
// Need to populate the CCK fields for the filefield field - so node_save will update the CCK field
$nodefile['list'] = 1;
$nodefile['data'] = serialize(array(
'description' => $file->description,
));
$nodefile['realname'] = $file->name;
$nodefile['moderated'] = $file->moderated;
if ($file->moderated) {
// Generate random file name for newly submitted file to hide it until approved
$charset = "abcdefghijklmnopqrstuvwxyz";
for ($i = 0; $i < 12; $i++) {
$random_name .= $charset[mt_rand(0, drupal_strlen($charset) - 1)];
}
$ext = end(explode(".", $file->name));
$random_name .= '.' . $ext;
$nodefile['moderated_tmpname'] = $random_name;
}
else {
$nodefile['moderated'] = FALSE;
}
$node = node_load($file->nid);
$content_type = content_types($node->type);
$nodefileObj = new stdClass();
$nodefileObj->fid = $nodefile['fid'];
// file_set_status API expects an object but just needs fid
file_set_status($nodefileObj, 1);
$node->field_filedepot_file[] = $nodefile;
node_save($node);
// After file has been saved and moved to the private filedepot folder via the HOOK_node_api function
// Check and see what the final filename and use that to update the filedepot tables
$rec = db_fetch_object(db_query("SELECT filename,filepath,filemime from {files} WHERE fid=%d", $nodefile['fid']));
$file->name = $rec->filename;
$dest = $rec->filepath;
$ext = end(explode(".", $file->name));
// fix http://drupal.org/node/803694
// seems that SWF (Flash) may always set the Content-Type to 'application/octet-stream'
// no matter what. Check the type and see if this has happened.
// $file->type should have the MIME type guessed by Drupal in this instance.
if ($rec->filemime == 'application/octet-stream') {
db_query("UPDATE {files} SET filemime = '%s' WHERE fid = %d", $file->type, $nodefile['fid']);
}
if ($file->moderated) {
// Save record in submission table and set status to 0 -- not online
$sql = "INSERT INTO {filedepot_filesubmissions} " . "(cid, fname, tempname, title, description, cckfid, version_note, size, mimetype, extension, submitter, date, tags, notify) " . "VALUES (%d,'%s','%s','%s','%s',%d,'%s',%d,'%s','%s',%d,%d,'%s', %d)";
db_query($sql, $file->folder, $nodefile['realname'], $nodefile['moderated_tmpname'], $file->title, $file->description, $nodefile['fid'], $file->vernote, $file->size, $file->type, $ext, $user->uid, time(), $file->tags, $_POST['notify']);
// Get id for the new file record
$args = array(
$file->folder,
$user->uid,
);
$id = db_result(db_query("SELECT id FROM {filedepot_filesubmissions} WHERE cid=%d AND submitter=%d ORDER BY id DESC", $args, 0, 1));
filedepot_sendNotification($id, FILEDEPOT_NOTIFY_ADMIN);
}
else {
// Create filedepot record for file and set status of file to 1 - online
$sql = "INSERT INTO {filedepot_files} (cid,fname,title,description,version,cckfid,size,mimetype,extension,submitter,status,date) " . "VALUES (%d,'%s','%s','%s',1,%d,%d,'%s','%s',%d,1,%d)";
db_query($sql, $file->folder, $file->name, $file->title, $file->description, $nodefile['fid'], $file->size, $file->type, $ext, $user->uid, time());
// Get fileid for the new file record
$args = array(
$file->folder,
$user->uid,
);
$fid = db_result(db_query("SELECT fid FROM {filedepot_files} WHERE cid=%d AND submitter=%d ORDER BY fid DESC", $args, 0, 1));
db_query("INSERT INTO {filedepot_fileversions} (fid,cckfid,fname,version,notes,size,date,uid,status)\n VALUES (%d,%d,'%s','1','%s',%d,%d,%d,1)", $fid, $nodefile['fid'], $file->name, $file->vernote, $file->size, time(), $user->uid);
if (!empty($file->tags) and $this
->checkPermission($file->folder, 'view', 0, FALSE)) {
$nexcloud
->update_tags($fid, $file->tags);
}
// Send out email notifications of new file added to all users subscribed
if ($_POST['notify'] == 1) {
filedepot_sendNotification($fid, FILEDEPOT_NOTIFY_NEWFILE);
}
// Update related folders last_modified_date
$workspaceParentFolder = filedepot_getTopLevelParent($file->folder);
filedepot_updateFolderLastModified($workspaceParentFolder);
}
return TRUE;
}
else {
drupal_set_message('Error saving file - move file failed');
return FALSE;
}
}
else {
drupal_set_message('Error saving file - directory does not exist or not writeable');
return FALSE;
}
}