function hj_framework_process_inputs($hook, $type, $return, $params) { $entity = elgg_extract('entity', $params, false); $field = elgg_extract('field', $params, false); if (!$entity || !$field || !elgg_instanceof($entity)) { return true; } $type = $entity->getType(); $subtype = $entity->getSubtype(); switch ($field->input_type) { case 'file': if (elgg_is_logged_in()) { global $_FILES; $field_name = $field->name; $file = $_FILES[$field_name]; // Maybe someone doesn't want us to save the file in this particular way if (!empty($file['name']) && !elgg_trigger_plugin_hook('hj:framework:form:fileupload', 'all', array('entity' => $entity, 'file' => $file, 'field_name' => $field_name), false)) { hj_framework_process_file_upload($file, $entity, $field_name); } } break; case 'entity_icon': $field_name = $field->name; global $_FILES; if (isset($_FILES[$field_name]) && substr_count($_FILES[$field_name]['type'], 'image/')) { hj_framework_generate_entity_icons($entity, $field_name); $entity->{$field_name} = null; } break; case 'relationship_tags': $field_name = $field->name; $tags = get_input('relationship_tag_guids'); $relationship_name = get_input('relationship_tags_name', 'tagged_in'); $current_tags = elgg_get_entities_from_relationship(array('relationship' => $relationship_name, 'relationship_guid' => $entity->guid, 'inverse_relationship' => true)); if (is_array($current_tags)) { foreach ($current_tags as $current_tag) { if (!in_array($current_tag->guid, $tags)) { remove_entity_relationship($current_tag->guid, $relationship_name, $entity->guid); } } } if (is_array($tags)) { foreach ($tags as $tag_guid) { add_entity_relationship($tag_guid, $relationship_name, $entity->guid); } $tags = implode(',', $tags); } $entity->{$field_name} = $tags; break; case 'multifile': if (elgg_is_logged_in()) { $values = get_input($field->name); if (is_array($values)) { foreach ($values as $value) { create_metadata($entity->guid, $field->name, $value, '', $entity->owner_guid, $entity->access_id, true); if (!elgg_trigger_plugin_hook('hj:framework:form:multifile', 'all', array('entity' => $entity, 'file_guid' => $value, 'field_name' => $field_name), false)) { make_attachment($entity->guid, $value); } } } } break; } return true; }
function hj_framework_process_entity_icon_input($hook, $type, $return, $params) { $entity = elgg_extract('entity', $params); $name = elgg_extract('name', $params); global $_FILES; if (isset($_FILES[$name]) && substr_count($_FILES[$name]['type'], 'image/') && !$_FILES[$name]['error']) { $file = new hjFile(); $file->owner_guid = $entity->guid; $file->setFilename('hjfile/' . time() . $_FILES[$name]['name']); $file->open("write"); $file->close(); move_uploaded_file($_FILES[$name]['tmp_name'], $file->getFilenameOnFilestore()); hj_framework_generate_entity_icons($entity, $file); $file->delete(); } return true; }
/** * 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; }