function pieform_element_filebrowser_upload(Pieform $form, $element, $data) { global $USER; $parentfolder = $data['uploadfolder'] ? (int) $data['uploadfolder'] : null; $institution = !empty($element['institution']) ? $element['institution'] : $form->get_property('institution'); $group = !empty($element['group']) ? $element['group'] : $form->get_property('group'); if (get_config('licensemetadata')) { $license = $data['license']; $licensor = $data['licensor']; $licensorurl = $data['licensorurl']; } $uploadnumber = (int) $data['uploadnumber']; $editable = (int) $element['config']['edit']; $selectable = (int) $element['config']['select']; $querybase = $element['page'] . (strpos($element['page'], '?') === false ? '?' : '&'); $prefix = $form->get_name() . '_' . $element['name']; $userfileindex = isset($data['userfileindex']) ? $data['userfileindex'] : null; $resizeonuploadenable = get_config_plugin('artefact', 'file', 'resizeonuploadenable'); $resizeonuploaduseroption = get_config_plugin('artefact', 'file', 'resizeonuploaduseroption'); $resizeonuploaduserenable = (int) $data['resizeonuploaduserenable']; $result = array('error' => false, 'uploadnumber' => $uploadnumber); if ($parentfolder == 0) { $parentfolder = null; } $data = new StdClass(); $data->parent = $parentfolder; $data->owner = $data->group = $data->institution = null; if (get_config('licensemetadata')) { $data->license = $license; $data->licensor = $licensor; $data->licensorurl = $licensorurl; } if ($parentfolder) { $parentartefact = artefact_instance_from_id($parentfolder); if (!$USER->can_edit_artefact($parentartefact)) { $result['error'] = true; $result['message'] = get_string('cannoteditfolder', 'artefact.file'); return $result; } else { if ($parentartefact->get('locked')) { $result['error'] = true; $result['message'] = get_string('cannoteditfoldersubmitted', 'artefact.file'); return $result; } } $parentfoldername = $parentartefact->get('title'); } else { $parentfoldername = null; } if ($institution) { if (!$USER->can_edit_institution($institution)) { $result['error'] = true; $result['message'] = get_string('notadminforinstitution', 'admin'); return $result; } $data->institution = $institution; } else { if ($group) { if (!group_within_edit_window($group)) { return array('error' => true, 'message' => get_string('cannoteditfolder', 'artefact.file')); } if (!$parentfolder) { if (!pieform_element_filebrowser_edit_group_folder($group, 0)) { return array('error' => true, 'message' => get_string('cannoteditfolder', 'artefact.file')); } } $data->group = $group; } else { $data->owner = $USER->get('id'); } } $data->container = 0; if (isset($userfileindex)) { $originalname = $_FILES['userfile']['name'][$userfileindex]; } else { $originalname = $_FILES['userfile']['name']; } $originalname = $originalname ? basename($originalname) : get_string('file', 'artefact.file'); $data->title = ArtefactTypeFileBase::get_new_file_title($originalname, $parentfolder, $data->owner, $group, $institution); // Overwrite image file with resized version if required $resized = false; $resizeattempted = false; // resize specified if (resizing is enabled AND user has enabled resizing) OR (resizing is enabled AND user is not given an option to enable/disable) if ($resizeonuploadenable && $resizeonuploaduserenable || $resizeonuploadenable && !$resizeonuploaduseroption) { require_once 'file.php'; require_once 'imageresizer.php'; $file = $_FILES['userfile']; if (isset($userfileindex)) { $tmpname = $file['tmp_name'][$userfileindex]; } else { $tmpname = $file['tmp_name']; } if (is_image_file($tmpname)) { $imageinfo = getimagesize($tmpname); $mimetype = $imageinfo['mime']; $width = $imageinfo[0]; $height = $imageinfo[1]; $bmptypes = array('image/bmp', 'image/x-bmp', 'image/ms-bmp', 'image/x-ms-bmp'); // resize image if necessary $resizeonuploadmaxwidth = get_config_plugin('artefact', 'file', 'resizeonuploadmaxwidth'); $resizeonuploadmaxheight = get_config_plugin('artefact', 'file', 'resizeonuploadmaxheight'); // Don't support bmps for now if (($width > $resizeonuploadmaxwidth || $height > $resizeonuploadmaxheight) && !in_array($mimetype, $bmptypes)) { $resizeattempted = true; $imgrs = new ImageResizer($tmpname, $mimetype); $img = $imgrs->get_image(); if (!empty($img)) { $imgrs->resize_image(array('w' => $resizeonuploadmaxwidth, 'h' => $resizeonuploadmaxheight), $mimetype); //auto $saveresize = $imgrs->save_image($tmpname, $mimetype, 85); if (!$saveresize) { return array('error' => true, 'message' => get_string('problemresizing', 'artefact.file')); } $resized = true; } } } } try { $newid = ArtefactTypeFile::save_uploaded_file('userfile', $data, $userfileindex, $resized); } catch (QuotaExceededException $e) { prepare_upload_failed_message($result, $e, $parentfoldername, $originalname); // update the file listing if (defined('GROUP')) { $group = group_current_group(); $result['quota'] = $group->quota; $result['quotaused'] = $group->quotaused; } else { $result['quota'] = $USER->get('quota'); $result['quotaused'] = $USER->get('quotaused'); } $result['newlist'] = pieform_element_filebrowser_build_filelist($form, $element, $parentfolder, null, $data->owner, $data->group, $data->institution); return $result; } catch (UploadException $e) { prepare_upload_failed_message($result, $e, $parentfoldername, $originalname); if (defined('GROUP')) { $group = group_current_group(); $result['quota'] = $group->quota; $result['quotaused'] = $group->quotaused; } return $result; } // Upload succeeded if (isset($element['filters'])) { $artefacttypes = isset($element['filters']['artefacttype']) ? $element['filters']['artefacttype'] : null; $filetypes = isset($element['filters']['filetype']) ? $element['filters']['filetype'] : null; if (!empty($artefacttypes) || !empty($filetypes)) { // Need to check the artefacttype or filetype (mimetype) of the uploaded file. $file = artefact_instance_from_id($newid); if (is_array($artefacttypes) && !in_array($file->get('artefacttype'), $artefacttypes) || is_array($filetypes) && !in_array($file->get('filetype'), $filetypes)) { $result['error'] = true; $result['uploaded'] = true; $result['message'] = get_string('wrongfiletypeforblock', 'artefact.file'); return $result; } } } if ($parentfoldername) { if ($data->title == $originalname) { $result['message'] = get_string('uploadoffiletofoldercomplete', 'artefact.file', $originalname, $parentfoldername); } else { $result['message'] = get_string('fileuploadedtofolderas', 'artefact.file', $originalname, $parentfoldername, $data->title); } } else { if ($data->title == $originalname) { $result['message'] = get_string('uploadoffilecomplete', 'artefact.file', $originalname); } else { $result['message'] = get_string('fileuploadedas', 'artefact.file', $originalname, $data->title); } } if ($resizeattempted && !$resized) { $result['message'] .= get_string('insufficientmemoryforresize', 'artefact.file'); } $result['highlight'] = $newid; $artefact = artefact_instance_from_id($newid); $result['artefacttype'] = $artefact->get('artefacttype'); $result['uploaded'] = true; $result['newlist'] = pieform_element_filebrowser_build_filelist($form, $element, $parentfolder, $newid, $data->owner, $data->group, $data->institution); if (defined('GROUP')) { $group = group_current_group(false); $result['quota'] = $group->quota; $result['quotaused'] = $group->quotaused; } else { $result['quota'] = $USER->get('quota'); $result['quotaused'] = $USER->get('quotaused'); } $result['maxuploadsize'] = display_size(get_max_upload_size(!$institution && !$group)); return $result; }