/** * Process uploaded files * * @param mixed $files Uploaded files * @param mixed $entity If an entity is set and it doesn't belong to one of the file subtypes, uploaded files will be converted into hjFile objects and attached to the entity * @return void */ function hj_framework_process_file_upload($name, $entity = null) { // Normalize the $_FILES array if (is_array($_FILES[$name]['name'])) { $files = hj_framework_prepare_files_global($_FILES); $files = $files[$name]; } else { $files = $_FILES[$name]; $files = array($files); } if (elgg_instanceof($entity)) { if (!$entity instanceof hjFile) { $is_attachment = true; } $subtype = $entity->getSubtype(); } foreach ($files as $file) { if (!is_array($file) || $file['error']) { continue; } if ($is_attachment) { $filehandler = new hjFile(); } else { $filehandler = new hjFile($entity->guid); } $prefix = 'hjfile/'; if ($entity instanceof hjFile) { $filename = $filehandler->getFilenameOnFilestore(); if (file_exists($filename)) { unlink($filename); } $filestorename = $filehandler->getFilename(); $filestorename = elgg_substr($filestorename, elgg_strlen($prefix)); } else { $filestorename = elgg_strtolower(time() . $file['name']); } $filehandler->setFilename($prefix . $filestorename); $filehandler->title = $file['name']; $mime_type = ElggFile::detectMimeType($file['tmp_name'], $file['type']); // hack for Microsoft zipped formats $info = pathinfo($file['name']); $office_formats = array('docx', 'xlsx', 'pptx'); if ($mime_type == "application/zip" && in_array($info['extension'], $office_formats)) { switch ($info['extension']) { case 'docx': $mime_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break; case 'xlsx': $mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break; case 'pptx': $mime_type = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; break; } } // check for bad ppt detection if ($mime_type == "application/vnd.ms-office" && $info['extension'] == "ppt") { $mime_type = "application/vnd.ms-powerpoint"; } $filehandler->setMimeType($mime_type); $filehandler->originalfilename = $file['name']; $filehandler->simpletype = hj_framework_get_simple_type($mime_type); $filehandler->filesize = $file['size']; $filehandler->open("write"); $filehandler->close(); move_uploaded_file($file['tmp_name'], $filehandler->getFilenameOnFilestore()); if ($filehandler->save()) { if ($is_attachment && elgg_instanceof($entity)) { make_attachment($entity->guid, $filehandler->getGUID()); } // Generate icons for images if ($filehandler->simpletype == "image") { if (!elgg_instanceof($entity) || $is_attachment) { // no entity provided or this is an attachment generating icons for self hj_framework_generate_entity_icons($filehandler, $filehandler); } else { if (elgg_instanceof($entity)) { hj_framework_generate_entity_icons($entity, $filehandler); } } // the settings tell us not to keep the original image file, so downsizing to master if (!HYPEFRAMEWORK_FILES_KEEP_ORIGINALS) { $icon_sizes = hj_framework_get_thumb_sizes($subtype); $values = $icon_sizes['master']; $master = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $values['w'], $values['h'], $values['square'], 0, 0, 0, 0, $values['upscale']); $filehandler->open('write'); $filehandler->write($master); $filehandler->close(); } } $return[$file['name']] = $filehandler->getGUID(); } else { $return[$file['name']] = false; } } return $return; }