/** * Unzip an uploaded zip file * * @param array $file the $_FILES information * @param int $container_guid the container to put the files/folders under * @param int $parent_guid the parrent folder * * @return bool */ function file_tools_unzip($file, $container_guid, $parent_guid = 0) { $extracted = false; if (!empty($file) && !empty($container_guid)) { $allowed_extensions = file_tools_allowed_extensions(); $zipfile = elgg_extract("tmp_name", $file); $container_entity = get_entity($container_guid); $access_id = get_input("access_id", false); if ($access_id === false) { if (!empty($parent_guid) && ($parent_folder = get_entity($parent_guid)) && elgg_instanceof($parent_folder, "object", FILE_TOOLS_SUBTYPE)) { $access_id = $parent_folder->access_id; } else { if (elgg_instanceof($container_entity, "group")) { $access_id = $container_entity->group_acl; } else { $access_id = get_default_access($container_entity); } } } // open the zip file $zip = zip_open($zipfile); while ($zip_entry = zip_read($zip)) { // open the zip entry zip_entry_open($zip, $zip_entry); // set some variables $zip_entry_name = zip_entry_name($zip_entry); $filename = basename($zip_entry_name); // check for folder structure if (strlen($zip_entry_name) != strlen($filename)) { // there is a folder structure, check it and create missing items file_tools_create_folders($zip_entry, $container_guid, $parent_guid); } // extract the folder structure from the zip entry $folder_array = explode("/", $zip_entry_name); $parent = $parent_guid; foreach ($folder_array as $folder) { $folder = utf8_encode($folder); if ($entity = file_tools_check_foldertitle_exists($folder, $container_guid, $parent)) { $parent = $entity->getGUID(); } else { if ($folder == end($folder_array)) { $prefix = "file/"; $extension_array = explode('.', $folder); $file_extension = end($extension_array); if (in_array(strtolower($file_extension), $allowed_extensions)) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); // create the file $filehandler = new ElggFile(); $filehandler->setFilename($prefix . $folder); $filehandler->title = $folder; $filehandler->originalfilename = $folder; $filehandler->owner_guid = elgg_get_logged_in_user_guid(); $filehandler->container_guid = $container_guid; $filehandler->access_id = $access_id; $filehandler->open("write"); $filehandler->write($buf); $filehandler->close(); $mime_type = $filehandler->detectMimeType($filehandler->getFilenameOnFilestore()); // hack for Microsoft zipped formats $info = pathinfo($folder); $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"; } $simple_type = file_get_simple_type($mime_type); $filehandler->setMimeType($mime_type); $filehandler->simpletype = $simple_type; if ($simple_type == "image") { $filestorename = elgg_strtolower(time() . $folder); $thumb = new ElggFile(); $thumb->owner_guid = elgg_get_logged_in_user_guid(); $thumbnail = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 60, 60, true); if ($thumbnail) { $thumb->setFilename($prefix . "thumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbnail); $thumb->close(); $filehandler->thumbnail = $prefix . "thumb" . $filestorename; unset($thumbnail); } $thumbsmall = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 153, 153, true); if ($thumbsmall) { $thumb->setFilename($prefix . "smallthumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbsmall); $thumb->close(); $filehandler->smallthumb = $prefix . "smallthumb" . $filestorename; unset($thumbsmall); } $thumblarge = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 600, 600, false); if ($thumblarge) { $thumb->setFilename($prefix . "largethumb" . $filestorename); $thumb->open("write"); $thumb->write($thumblarge); $thumb->close(); $filehandler->largethumb = $prefix . "largethumb" . $filestorename; unset($thumblarge); } unset($thumb); } set_input('folder_guid', $parent); $filehandler->save(); $extracted = true; if (!empty($parent)) { add_entity_relationship($parent, FILE_TOOLS_RELATIONSHIP, $filehandler->getGUID()); } } } } } zip_entry_close($zip_entry); } zip_close($zip); } return $extracted; }
function get_general_file_type($mimetype) { elgg_deprecated_notice('Use file_get_simple_type() instead of get_general_file_type()', 1.8); return file_get_simple_type($mimetype); }
/** * * upload attachments for a project * @param ElggEntity * */ function projects_upload_attachments($attachments, $project) { $count = count($attachments['name']); for ($i = 0; $i < $count; $i++) { if ($attachments['error'][$i] || !$attachments['name'][$i]) { continue; } $name = $attachments['name'][$i]; $file = new ElggFile(); $file->container_guid = $project->guid; $file->title = $name; $file->access_id = (int) $project->access_id; $prefix = "file/"; $filestorename = elgg_strtolower(time() . $name); $file->setFilename($prefix . $filestorename); $file->open("write"); $file->close(); move_uploaded_file($attachments['tmp_name'][$i], $file->getFilenameOnFilestore()); $saved = $file->save(); if ($saved) { $mime_type = ElggFile::detectMimeType($attachments['tmp_name'][$i], $attachments['type'][$i]); $info = pathinfo($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"; } //add_metastring("projectId"); //$file->projectId = $project_guid; $file->setMimeType($mime_type); $file->originalfilename = $name; if (elgg_is_active_plugin('file')) { $file->simpletype = file_get_simple_type($mime_type); } $saved = $file->save(); if ($saved) { $file->addRelationship($project->guid, 'attachment'); } } } }
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"; } if ($mime_type == "application/vnd.ms-powerpoint") { $mime_type = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; } $file->setMimeType($mime_type); $file->originalfilename = $_FILES['upload']['name']; $file->simpletype = file_get_simple_type($mime_type); // Open the file to guarantee the directory exists $file->open("write"); $file->close(); move_uploaded_file($_FILES['upload']['tmp_name'], $file->getFilenameOnFilestore()); $guid = $file->save(); // if image, we need to create thumbnails (this should be moved into a function) if ($guid && $file->simpletype == "image") { $file->icontime = time(); $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true); if ($thumbnail) { $thumb = new ElggFile(); $thumb->setMimeType($_FILES['upload']['type']); $thumb->setFilename($prefix . "thumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbnail);
// if previous file, delete it if ($new_file == false) { $filename = $file->getFilenameOnFilestore(); if (file_exists($filename)) { unlink($filename); } // use same filename on the disk - ensures thumbnails are overwritten $filestorename = $file->getFilename(); $filestorename = elgg_substr($filestorename, elgg_strlen($prefix)); } else { $filestorename = elgg_strtolower(time() . $_FILES['upload']['name']); } $file->setFilename($prefix . $filestorename); $file->setMimeType($_FILES['upload']['type']); $file->originalfilename = $_FILES['upload']['name']; $file->simpletype = file_get_simple_type($_FILES['upload']['type']); $file->open("write"); $file->write(get_uploaded_file('upload')); $file->close(); $guid = $file->save(); // if image, we need to create thumbnails (this should be moved into a function) if ($guid && $file->simpletype == "image") { $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true); if ($thumbnail) { $thumb = new ElggFile(); $thumb->setMimeType($_FILES['upload']['type']); $thumb->setFilename($prefix . "thumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbnail); $thumb->close(); $file->thumbnail = $prefix . "thumb" . $filestorename;
/** * Unzip an uploaded zip file * * @param array $file the $_FILES information * @param int $container_guid the container to put the files/folders under * @param int $parent_guid the parrent folder * * @return bool */ function file_tools_unzip($file, $container_guid, $parent_guid = 0) { $container_guid = (int) $container_guid; $parent_guid = (int) $parent_guid; if (empty($file) || empty($container_guid)) { return false; } $container_entity = get_entity($container_guid); if (empty($container_entity)) { return false; } $extracted = false; $allowed_extensions = file_tools_allowed_extensions(); $zipfile = elgg_extract('tmp_name', $file); $access_id = get_input('access_id', false); if ($access_id === false) { $parent_folder = get_entity($parent_guid); if (elgg_instanceof($parent_folder, 'object', FILE_TOOLS_SUBTYPE)) { $access_id = $parent_folder->access_id; } else { if ($container_entity instanceof ElggGroup) { $access_id = $container_entity->group_acl; } else { $access_id = get_default_access($container_entity); } } } // open the zip file $zip = zip_open($zipfile); while ($zip_entry = zip_read($zip)) { // open the zip entry zip_entry_open($zip, $zip_entry); // set some variables $zip_entry_name = zip_entry_name($zip_entry); $filename = basename($zip_entry_name); // check for folder structure if (strlen($zip_entry_name) != strlen($filename)) { // there is a folder structure, check it and create missing items file_tools_create_folders($zip_entry, $container_guid, $parent_guid); } // extract the folder structure from the zip entry $folder_array = explode('/', $zip_entry_name); $parent = $parent_guid; foreach ($folder_array as $folder) { $folder = utf8_encode($folder); $entity = file_tools_check_foldertitle_exists($folder, $container_guid, $parent); if (!empty($entity)) { $parent = $entity->getGUID(); } else { if ($folder !== end($folder_array)) { continue; } $prefix = 'file/'; $extension_array = explode('.', $folder); $file_extension = end($extension_array); if (!in_array(strtolower($file_extension), $allowed_extensions)) { continue; } $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); $filestorename = elgg_strtolower(time() . $folder); // create the file $filehandler = new ElggFile(); $filehandler->setFilename($prefix . $filestorename); $filehandler->title = $folder; $filehandler->originalfilename = $folder; $filehandler->owner_guid = elgg_get_logged_in_user_guid(); $filehandler->container_guid = $container_guid; $filehandler->access_id = $access_id; if (!$filehandler->save()) { continue; } $filehandler->open('write'); $filehandler->write($buf); $filehandler->close(); $mime_type = $filehandler->detectMimeType($filehandler->getFilenameOnFilestore()); // hack for Microsoft zipped formats $info = pathinfo($folder); $office_formats = ['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'; } $simple_type = file_get_simple_type($mime_type); $filehandler->setMimeType($mime_type); $filehandler->simpletype = $simple_type; if ($simple_type == 'image') { if ($filehandler->saveIconFromElggFile($filehandler)) { $filehandler->thumbnail = $filehandler->getIcon('small')->getFilename(); $filehandler->smallthumb = $filehandler->getIcon('medium')->getFilename(); $filehandler->largethumb = $filehandler->getIcon('large')->getFilename(); } } set_input('folder_guid', $parent); $filehandler->save(); $extracted = true; if (!empty($parent)) { add_entity_relationship($parent, FILE_TOOLS_RELATIONSHIP, $filehandler->getGUID()); } } } zip_entry_close($zip_entry); } zip_close($zip); return $extracted; }
function pleio_api_save_file($data = "", $file_name = "", $title = "", $description = "", $tags = "", $file_id = null, $folder_id = 0, $group_id = 0, $access_id = "", $wiki_id = "", $mimetype = "") { $file_id = $file_id ? $file_id : null; $user = elgg_get_logged_in_user_entity(); $user_id = $user !== false ? $user->guid : 0; if (!$data && !$file_id) { return new ErrorResult(elgg_echo("file:uploadfailed")); } $swordfish_group = $group_id ? pleio_api_swordfish_group($group_id) : false; if ($swordfish_group) { $group = get_entity($group_id); $url = pleio_api_swordfish_baseurl($group->site_guid) . "post-file"; $swordfish_name = pleio_api_swordfish_username($user->username); $params = array("data" => $data, "title" => $title); if ($file_id) { $params["fileId"] = $file_id; } elseif ($folder_id) { $params["folderId"] = $folder_id; } elseif ($group_id) { $params["groupId"] = $swordfish_group; } else { return new ErrorResult("Vul minimaal een bestand, folder of groep in"); } if ($wiki_id) { $params["wikiId"] = $wiki_id; } if ($access_id != ACCESS_PRIVATE) { $params["visibility"] = "internally_published"; } else { $params["visibility"] = "private"; } $params["filename"] = $file_name; $result = pleio_api_call_swordfish_api($swordfish_name, $url, "POST", $params); if ($result->ok) { if (strpos($result->headers["CONTENT-TYPE"], "json")) { $response = json_decode($result->response); return new SaveSuccessResult(elgg_echo("file:saved"), $response->id); } else { return new ErrorResult($result->headers["BOBO-EXCEPTION-VALUE"]); } } else { return new ErrorResult($result->headers["BOBO-EXCEPTION-VALUE"]); } } else { if ($file_id) { $file = get_entity($file_id); } if (!$file) { $file = new ElggFile(); $file->owner_guid = $user_id; } if ($title) { $file->title = $title; } if ($description) { $file->setDescription($description); } if ($tags) { $file->setMetaData("tags", $tags); } if ($group_id) { $file->setContainerGUID($group_id); } if ($access_id) { $file->access_id = $access_id; } if ($data) { $file->setFilename(basename($file_name)); $data = base64_decode($data); $fh = $file->open("write"); if ($fh) { $file->write($data); $file->close(); } if (!$mimetype) { $mimetype = $file->detectMimeType($file->getFilenameOnFilestore()); } $file->setMimeType($mimetype); $file->simpletype = file_get_simple_type($mimetype); } if (!$file->save()) { return new ErrorResult(elgg_echo("file:uploadfailed")); } if ($folder_id) { remove_entity_relationships($file->guid, "folder_of", 1); add_entity_relationship($folder_id, "folder_of", $file->guid); } if (!$file_id) { add_to_river('river/object/file/create', 'create', $user_id, $file->guid); } return new SaveSuccessResult(elgg_echo("file:saved"), $file->guid); } return new ErrorResult(elgg_echo("file:uploadfailed")); }
/** * Send a message to specified recipients * * @param int $sender_guid GUID of the sender entity * @param array $recipient_guids An array of recipient GUIDs * @param str $subject Subject of the message * @param str $message Body of the message * @param str $message_type Type of the message * @param array $params Additional parameters, e.g. 'message_hash', 'attachments' * @return boolean */ function hj_inbox_send_message($sender_guid, $recipient_guids, $subject = '', $message = '', $message_type = '', array $params = array()) { $ia = elgg_set_ignore_access(); if (!is_array($recipient_guids)) { $recipient_guids = array($recipient_guids); } if (isset($params['message_hash'])) { $message_hash = elgg_extract('message_hash', $params); } if (isset($params['attachments'])) { $attachments = elgg_extract('attachments', $params); } $user_guids = $recipient_guids; $user_guids[] = $sender_guid; sort($user_guids); if (!$message_hash) { $title = strtolower($subject); $title = trim(str_replace('re:', '', $title)); $message_hash = sha1(implode(':', $user_guids) . $title); } $acl_hash = sha1(implode(':', $user_guids)); $dbprefix = elgg_get_config('dbprefix'); $query = "SELECT * FROM {$dbprefix}access_collections WHERE name = '{$acl_hash}'"; $collection = get_data_row($query); //error_log(print_r($collection, true)); $acl_id = $collection->id; if (!$acl_id) { $site = elgg_get_site_entity(); $acl_id = create_access_collection($acl_hash, $site->guid); update_access_collection($acl_id, $user_guids); } //error_log($acl_id); $message_sent = new ElggObject(); $message_sent->subtype = "messages"; $message_sent->owner_guid = $sender_guid; $message_sent->container_guid = $sender_guid; $message_sent->access_id = ACCESS_PRIVATE; $message_sent->title = $subject; $message_sent->description = $message; $message_sent->toId = $recipient_guids; // the users receiving the message $message_sent->fromId = $sender_guid; // the user sending the message $message_sent->readYet = 1; // this is a toggle between 0 / 1 (1 = read) $message_sent->hiddenFrom = 0; // this is used when a user deletes a message in their sentbox, it is a flag $message_sent->hiddenTo = 0; // this is used when a user deletes a message in their inbox $message_sent->msg = 1; $message_sent->msgType = $message_type; $message_sent->msgHash = $message_hash; $message_sent->save(); if ($attachments) { $count = count($attachments['name']); for ($i = 0; $i < $count; $i++) { if ($attachments['error'][$i] || !$attachments['name'][$i]) { continue; } $name = $attachments['name'][$i]; $file = new ElggFile(); $file->container_guid = $message_sent->guid; $file->title = $name; $file->access_id = (int) $acl_id; $prefix = "file/"; $filestorename = elgg_strtolower(time() . $name); $file->setFilename($prefix . $filestorename); $file->open("write"); $file->close(); move_uploaded_file($attachments['tmp_name'][$i], $file->getFilenameOnFilestore()); $saved = $file->save(); if ($saved) { $mime_type = ElggFile::detectMimeType($attachments['tmp_name'][$i], $attachments['type'][$i]); $info = pathinfo($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"; } $file->msgHash = $message_hash; $file->toId = $recipient_guids; $file->fromId = $sender_guid; $file->setMimeType($mime_type); $file->originalfilename = $name; if (elgg_is_active_plugin('file')) { $file->simpletype = file_get_simple_type($mime_type); } $file->save(); $guid = $file->getGUID(); $uploaded_attachments[] = $guid; $attachment_urls .= '<div class="inbox-attachment">' . elgg_view('output/url', array('href' => "messages/download/{$guid}", 'text' => $file->title, 'is_trusted' => true)) . '</div>'; if ($file->simpletype == "image") { $file->icontime = time(); $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true); if ($thumbnail) { $thumb = new ElggFile(); $thumb->setMimeType($attachments['type'][$i]); $thumb->setFilename($prefix . "thumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbnail); $thumb->close(); $file->thumbnail = $prefix . "thumb" . $filestorename; unset($thumbnail); } $thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 153, 153, true); if ($thumbsmall) { $thumb->setFilename($prefix . "smallthumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbsmall); $thumb->close(); $file->smallthumb = $prefix . "smallthumb" . $filestorename; unset($thumbsmall); } $thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 600, 600, false); if ($thumblarge) { $thumb->setFilename($prefix . "largethumb" . $filestorename); $thumb->open("write"); $thumb->write($thumblarge); $thumb->close(); $file->largethumb = $prefix . "largethumb" . $filestorename; unset($thumblarge); } } } } } $success = $error = 0; foreach ($recipient_guids as $recipient_guid) { $message_to = new ElggObject(); $message_to->subtype = "messages"; $message_to->owner_guid = $recipient_guid; $message_to->container_guid = $recipient_guid; $message_to->access_id = ACCESS_PRIVATE; $message_to->title = $subject; $message_to->description = $message; $message_to->toId = $recipient_guids; // the users receiving the message $message_to->fromId = $sender_guid; // the user sending the message $message_to->readYet = 0; // this is a toggle between 0 / 1 (1 = read) $message_to->hiddenFrom = 0; // this is used when a user deletes a message in their sentbox, it is a flag $message_to->hiddenTo = 0; // this is used when a user deletes a message in their inbox $message_to->msg = 1; $message_to->msgType = $message_type; $message_to->msgHash = $message_hash; if ($message_to->save()) { $success++; // Make attachments if ($uploaded_attachments) { foreach ($uploaded_attachments as $attachment_guid) { make_attachment($message_to->guid, $attachment_guid); } } // Send out notifications skipping 'site' notification handler if ($recipient_guid != $sender_guid) { $methods = (array) get_user_notification_settings($recipient_guid); unset($methods['site']); if (count($methods)) { $recipient = get_user($recipient_guid); $sender = get_user($sender_guid); $notification_subject = elgg_echo('messages:email:subject'); $notification_message = strip_tags($message); if ($uploaded_attachments) { $notification_message .= elgg_view_module('inbox-attachments', elgg_echo('messages:attachments'), $attachment_urls); } $notification_body = elgg_echo('messages:email:body', array($sender->name, $notification_message, elgg_get_site_url() . "messages/inbox/{$recipient->username}?message_type={$message_type}", $sender->name, elgg_get_site_url() . "messages/thread/{$message_hash}")); notify_user($recipient_guid, $sender_guid, $notification_subject, $notification_body, null, $methods); } } } else { $error++; } } if ($success > 0) { // Make attachments if ($uploaded_attachments) { foreach ($uploaded_attachments as $attachment_guid) { make_attachment($message_sent->guid, $attachment_guid); } } $return = true; } else { $message_sent->delete(); $return = false; } elgg_set_ignore_access($ia); return $return; }
public function createFile($path = array(), $filename = "", $filestream = false, $access_id = 0) { if (!isset($filename)) { if (count($path) < 1) { throw new Exception('Path length must be at least one.'); } if (count($path) == 1) { $parent = $this->container; } else { $parent_guid = array_slice($path, -2)[0]; $parent = get_entity($parent_guid); } $filename = array_slice($path, -1)[0]; } else { if (count($path) == 0) { $parent = $this->container; } else { $parent_guid = array_slice($path, -1)[0]; $parent = get_entity($parent_guid); } } if (!$parent | !$parent->canWriteToContainer()) { return false; } if (!$access_id) { if ($parent instanceof ElggObject) { // lower level folder $access_id = $parent->access_id; } elseif ($parent instanceof ElggGroup) { // top level folder $access_id = $parent->group_acl; } else { throw new Exception('Invalid container.'); } } $file = new FilePluginFile(); $file->subtype = "file"; $file->title = $filename; $file->access_id = $access_id; $file->container_guid = $this->container->guid; $filestorename = elgg_strtolower(time() . $filename); $file->setFilename("file/" . $filestorename); $file->originalfilename = $filename; if ($filestream) { file_put_contents($file->getFilenameOnFilestore(), $filestream); } else { $input = fopen("php://input", "r"); file_put_contents($file->getFilenameOnFilestore(), $input); } $mime_type = ElggFile::detectMimeType($file->getFilenameOnFilestore(), mime_content_type($filename)); $file->setMimeType($mime_type); $file->simpletype = file_get_simple_type($mime_type); $file->save(); if ($file->simpletype == "image") { $file->icontime = time(); $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true); if ($thumbnail) { $thumb = new ElggFile(); $thumb->setMimeType($_FILES['upload']['type']); $thumb->setFilename($prefix . "thumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbnail); $thumb->close(); $file->thumbnail = $prefix . "thumb" . $filestorename; unset($thumbnail); } $thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 153, 153, true); if ($thumbsmall) { $thumb->setFilename($prefix . "smallthumb" . $filestorename); $thumb->open("write"); $thumb->write($thumbsmall); $thumb->close(); $file->smallthumb = $prefix . "smallthumb" . $filestorename; unset($thumbsmall); } $thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 600, 600, false); if ($thumblarge) { $thumb->setFilename($prefix . "largethumb" . $filestorename); $thumb->open("write"); $thumb->write($thumblarge); $thumb->close(); $file->largethumb = $prefix . "largethumb" . $filestorename; unset($thumblarge); } } if ($parent != $this->container && $parent instanceof ElggObject) { add_entity_relationship($parent->guid, FILE_TOOLS_RELATIONSHIP, $file->guid); } }
public function updateFile($file, $params = array()) { if (!$file->canEdit()) { return true; } if ($params['title']) { $file->title = $params['title']; } if (isset($params['access_id'])) { $file->access_id = $params['access_id']; } if (isset($params['write_access_id'])) { $file->write_access_id = $params['write_access_id']; } else { $file->write_access_id = ACCESS_PRIVATE; } if ($params['tags']) { $file->tags = $params['tags']; } if ($params['stream']) { $file->originalfilename = $params['filename']; // Open the file to guarantee the directory exists $file->open("write"); $file->close(); file_put_contents($file->getFilenameOnFilestore(), $params['stream']); $mime_type = ElggFile::detectMimeType($file->getFilenameOnFilestore(), $params['type']); $file->setMimeType($mime_type); $file->simpletype = file_get_simple_type($mime_type); pleiofile_generate_file_thumbs($file); } $result = $file->save(); if ($params['parent_guid'] != $file->parent_guid) { $new_parent = get_entity($params['parent_guid']); remove_entity_relationships($file->guid, "folder_of", true); if ($new_parent instanceof ElggObject) { add_entity_relationship($new_parent->guid, "folder_of", $file->guid); } } return $result; }
function hj_framework_edit_object_action() { $form_name = get_input('form_name', false); elgg_make_sticky_form($form_name); if (!hj_framework_validate_form($form_name)) { return false; } $guid = get_input('guid', ELGG_ENTITIES_ANY_VALUE); $event = $guid ? 'edit' : 'create'; $type = get_input('type'); $subtype = get_input('subtype'); $class = get_subtype_class($type, $subtype); //get the attachments $attachments = $_FILES['attachments']; if ($class) { $entity = new $class($guid); } else { switch (get_input('type', 'object')) { case 'object': $entity = new ElggObject($guid); break; case 'user': $entity = new ElggUser($guid); break; case 'group': $entity = new ElggGroup($guid); break; default: return false; break; } } if ($guid) { // Entity already exists if ((int) get_input('container_guid', 0) > 0) { $entity->container_guid = get_input('container_guid', ELGG_ENTITIES_ANY_VALUE); } if ($title = get_input('title', '')) { $entity->title = $title; } if ($description = get_input('description', '')) { $entity->description = $description; } if ($access_id = get_input('access_id', ACCESS_DEFAULT)) { $entity->access_id = $access_id; } } else { // Creating new entity $entity->subtype = get_input('subtype', 'hjformsubmission'); if ($owner_guid = get_input('owner_guid', ELGG_ENTITIES_ANY_VALUE)) { $entity->owner_guid = $owner_guid; } if ($container_guid = get_input('container_guid', ELGG_ENTITIES_ANY_VALUE)) { $entity->container_guid = $container_guid; } $entity->title = get_input('title', ''); $entity->description = get_input('description', ''); $entity->access_id = get_input('access_id', ACCESS_DEFAULT); } $guid = $entity->save(); if (!$guid) { register_error(elgg_echo('hj:framework:error:cannotcreateentity')); return false; } else { //check to see if existing attachments $existingAttachments = elgg_get_entities_from_relationship(array("relationship" => "attachment", "relationship_guid" => $guid, "inverse_relationship" => true)); foreach ($existingAttachments as $attachment) { $updatedAttachment = new ElggFile($attachment->guid); $updatedAttachment->access_id = $access_id; $updatedAttachment->save(); } //$entity = get_entity($guid); if ($attachments) { $count = count($attachments['name']); for ($i = 0; $i < $count; $i++) { if ($attachments['error'][$i] || !$attachments['name'][$i]) { continue; } $name = $attachments['name'][$i]; $file = new ElggFile(); $file->container_guid = $guid; $file->title = $name; $file->access_id = (int) $entity->access_id; $prefix = "file/"; $filestorename = elgg_strtolower(time() . $name); $file->setFilename($prefix . $filestorename); $file->open("write"); $file->close(); move_uploaded_file($attachments['tmp_name'][$i], $file->getFilenameOnFilestore()); $saved = $file->save(); if ($saved) { $mime_type = ElggFile::detectMimeType($attachments['tmp_name'][$i], $attachments['type'][$i]); $info = pathinfo($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"; } //add_metastring("projectId"); //$file->projectId = $project_guid; $file->setMimeType($mime_type); $file->originalfilename = $name; if (elgg_is_active_plugin('file')) { $file->simpletype = file_get_simple_type($mime_type); } $saved = $file->save(); if ($saved) { $file->addRelationship($guid, 'attachment'); } } } } $accesslevel = get_input('accesslevel', false); $params = array('entity' => $entity); $form = hj_framework_prepare_form($form_name, $params); $fields = $form['form']['fields']; $ignore_fields = array('guid', 'type', 'subtype', 'owner_guid', 'container_guid', 'access_id', 'title', 'description'); foreach ($fields as $name => $options) { if (in_array($name, $ignore_fields)) { continue; } if (!$options) { continue; } $type = elgg_extract('input_type', $options, 'text'); $accesslevel_id = isset($accesslevel[$name]) ? $accesslevel[$name] : $entity->access_id; $params = array('name' => $name, 'form_name' => $form_name, 'field' => $options, 'access_id' => $accesslevel_id, 'entity' => $entity, 'event' => $event); if (!elgg_trigger_plugin_hook('process:input', "form:input:name:{$name}", $params, false) && !elgg_trigger_plugin_hook('process:input', "form:input:type:{$type}", $params, false)) { $value = get_input($name); set_input($name, null); // if (!$value) { // elgg_delete_metadata(array( // 'guid' => $entity->guid, // 'metadata_name' => $name // )); // // continue; // } // // if (is_array($value) && count($value) > 1) { // elgg_delete_metadata(array( // 'guid' => $entity->guid, // 'metadata_name' => $name // )); // foreach ($value as $val) { // if (!empty($val)) { // create_metadata($entity->guid, $name, $val, '', $entity->owner_guid, $accesslevel_id, true); // } // } // } else { // if (is_array($value)) { // $value = implode(',', $value); // } // create_metadata($entity->guid, $name, $value, '', $entity->owner_guid, $accesslevel_id); // } $entity->{$name} = $value; } } $entity->save(); elgg_trigger_plugin_hook('process:form', "form:{$form_name}", array('form_name' => $form_name, 'entity' => $entity), null); } $forward_url = elgg_trigger_plugin_hook('action:forward', 'form', array('entity' => $entity, 'form_name' => $form_name), $entity->getURL()); foreach ($_POST['user-callout-id'] as $callout_user) { $callout_user_guids[] = $callout_user; } if ($callout_user_guids) { $calloutUsers = new UserCallout(get_entity(elgg_get_logged_in_user_guid()), $callout_user_guids, "a forum post", $forward_url); $calloutUsers->sendUserNotifications(); } system_message(elgg_echo('hj:framework:submit:success')); elgg_clear_sticky_form($form_name); hj_framework_clear_form_validation_status($form_name); return array('entity' => $entity, 'forward' => $forward_url); }