setMimeType() public method

Set the mime type of the file.
public setMimeType ( string $mimetype ) : boolean
$mimetype string The mimetype
return boolean
Beispiel #1
0
 /**
  * @group FileService
  */
 public function testCanSetMimeType()
 {
     unset($this->file->mimetype);
     $mimetype = 'application/plain';
     $this->file->setMimeType($mimetype);
     $this->assertEquals($mimetype, $this->file->getMimeType());
 }
function form_generate_thumbnail($file, $fieldname)
{
    // Generate thumbnail (if image)
    $prefix = "file/";
    $filestorename = strtolower(time() . $_FILES[$fieldname]['name']);
    if (substr_count($file->getMimeType(), 'image/')) {
        $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true);
        $thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 153, 153, true);
        $thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 600, 600, false);
        if ($thumbnail) {
            $thumb = new ElggFile();
            $thumb->setMimeType($_FILES[$fieldname]['type']);
            $thumb->setFilename($prefix . "thumb" . $filestorename);
            $thumb->open("write");
            $thumb->write($thumbnail);
            $thumb->close();
            $file->thumbnail = $prefix . "thumb" . $filestorename;
            $thumb->setFilename($prefix . "smallthumb" . $filestorename);
            $thumb->open("write");
            $thumb->write($thumbsmall);
            $thumb->close();
            $file->smallthumb = $prefix . "smallthumb" . $filestorename;
            $thumb->setFilename($prefix . "largethumb" . $filestorename);
            $thumb->open("write");
            $thumb->write($thumblarge);
            $thumb->close();
            $file->largethumb = $prefix . "largethumb" . $filestorename;
        }
    }
}
Beispiel #3
0
function zhsocial_apply_icon($zh_user, $icon_url)
{
    // 	if($zh_user->icontime)
    // 		return;
    $icon_sizes = elgg_get_config('icon_sizes');
    $prefix = "profile/{$zh_user->guid}";
    $filehandler = new ElggFile();
    $filehandler->owner_guid = $zh_user->guid;
    $filehandler->setFilename($prefix . ".jpg");
    $filehandler->open("write");
    $filehandler->write(file_get_contents($icon_url));
    $filehandler->close();
    $filename = $filehandler->getFilenameOnFilestore();
    $sizes = array('topbar', 'tiny', 'small', 'medium', 'large', 'master');
    $thumbs = array();
    foreach ($sizes as $size) {
        $thumbs[$size] = get_resized_image_from_existing_file($filename, $icon_sizes[$size]['w'], $icon_sizes[$size]['h'], $icon_sizes[$size]['square']);
    }
    if ($thumbs['tiny']) {
        // just checking if resize successful
        $thumb = new ElggFile();
        $thumb->owner_guid = $zh_user->guid;
        $thumb->setMimeType('image/jpeg');
        foreach ($sizes as $size) {
            $thumb->setFilename("{$prefix}{$size}.jpg");
            $thumb->open("write");
            $thumb->write($thumbs[$size]);
            $thumb->close();
        }
        $zh_user->icontime = time();
    }
}
Beispiel #4
0
function pleiofile_generate_file_thumbs(ElggObject $file)
{
    if ($file->simpletype != "image") {
        return null;
    }
    $file->icontime = time();
    $sizes = array(60 => "thumb", 153 => "tinythumb", 153 => "smallthumb", 600 => "largethumb");
    $filename = str_replace("file/", "", $file->getFilename());
    foreach ($sizes as $size => $description) {
        if ($size < 600) {
            $upscale = true;
        } else {
            $upscale = false;
        }
        $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), $size, $size, $upscale);
        if ($thumbnail) {
            $path = "file/" . $description . "_" . $filename;
            $thumb = new ElggFile();
            $thumb->setMimeType($_FILES['upload']['type']);
            $thumb->setFilename($path);
            $thumb->open("write");
            $thumb->write($thumbnail);
            $thumb->close();
            if ($description == "thumb") {
                $file->thumbnail = $path;
            } else {
                $file->{$description} = $path;
            }
            unset($thumbnail);
        }
    }
}
Beispiel #5
0
function CreateLTIGroup($user, $name, $context_id, $consumer_key)
{
    $group_guid = 0;
    $group = new ElggGroup($group_guid);
    // Set the group properties that we can!
    $group->name = $name;
    $group->context_id = $context_id;
    // This is a unique identifier from the consumer for this context
    $group->consumer_key = $consumer_key;
    // Which consumer is creating this group
    $group->membership = ACCESS_PRIVATE;
    $group->access_id = ACCESS_PUBLIC;
    $group->briefdescription = elgg_echo('LTI:provision:group');
    $consumer_instance = new LTI_Tool_Consumer_Instance($group->consumer_key, elgg_get_config('dbprefix'));
    $context = new LTI_Context($consumer_instance, $group->context_id);
    $group->description = $context->title;
    $group->save();
    $group->join($user);
    // Add images
    $prefix = 'groups/' . $group->guid;
    $filename = GetImage($consumer_key, '.jpg');
    $thumbtiny = get_resized_image_from_existing_file($filename, 25, 25, true);
    $thumbsmall = get_resized_image_from_existing_file($filename, 40, 40, true);
    $thumbmedium = get_resized_image_from_existing_file($filename, 100, 100, true);
    $thumblarge = get_resized_image_from_existing_file($filename, 200, 200, false);
    if ($thumbtiny) {
        $thumb = new ElggFile();
        $thumb->owner_guid = $group->owner_guid;
        $thumb->setMimeType('image/jpeg');
        $thumb->setFilename($prefix . "tiny.jpg");
        $thumb->open("write");
        $thumb->write($thumbtiny);
        $thumb->close();
        $thumb->setFilename($prefix . "small.jpg");
        $thumb->open("write");
        $thumb->write($thumbsmall);
        $thumb->close();
        $thumb->setFilename($prefix . "medium.jpg");
        $thumb->open("write");
        $thumb->write($thumbmedium);
        $thumb->close();
        $thumb->setFilename($prefix . "large.jpg");
        $thumb->open("write");
        $thumb->write($thumblarge);
        $thumb->close();
        $group->icontime = time();
    }
    // return the URL
    return $group;
}
Beispiel #6
0
function uploadCK($page, $identifier, $obj)
{
    $funcNum2 = get_Input('CKEditorFuncNum', 'CKEditorFuncNum');
    $file = new ElggFile();
    $filestorename = strtolower(time() . $_FILES['upload']['name']);
    $file->setFilename($filestorename);
    $file->setMimeType($_FILES['upload']['type']);
    $file->owner_guid = elgg_get_logged_in_user_guid();
    $file->subtype = "file";
    $file->originalfilename = $filestorename;
    $file->access_id = ACCESS_PUBLIC;
    $file->open("write");
    $file->write(get_uploaded_file('upload'));
    $file->close();
    $result = $file->save();
    if ($result) {
        $master = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 550, 550);
        if ($master !== false) {
            $_SESSION['UPLOAD_DATA']['file_save'] = "started";
            $filehandler = new ElggFile();
            $filehandler->setFilename($filestorename);
            $filehandler->setMimeType($_FILES['upload']['type']);
            $filehandler->owner_guid = $user->guid;
            $filehandler->subtype = "file";
            $filehandler->originalfilename = $filestorename;
            $filehandler->access_id = ACCESS_PUBLIC;
            $filehandler->open("write");
            $filehandler->write($master);
            $filehandler->close();
            $filehandler->save();
            // Dev URL
            $url = elgg_get_site_url() . 'CKEditorView?file_guid=' . $filehandler->guid;
            //Production URL
            //$url ='/CKEditorView?file_guid='.$filehandler->guid;
            echo '<script type="text/javascript">
		window.parent.CKEDITOR.tools.callFunction(' . $funcNum2 . ', "' . $url . '","");
		</script>';
            exit;
        } else {
            echo '<script type="text/javascript">
		window.parent.CKEDITOR.tools.callFunction(' . $funcNum2 . ', "","");
		</script>';
            exit;
        }
    }
    return true;
}
 protected function saveThumbnail($image, $name)
 {
     try {
         $thumbnail = get_resized_image_from_existing_file($image->getFilenameOnFilestore(), 60, 60, true);
     } catch (Exception $e) {
         return FALSE;
     }
     $thumb = new ElggFile();
     $thumb->setMimeType('image/jpeg');
     $thumb->access_id = $this->access_id;
     $thumb->setFilename($name);
     $thumb->open("write");
     $thumb->write($thumbnail);
     $thumb->save();
     $image->thumbnail_guid = $thumb->getGUID();
     if (!$thumb->getGUID()) {
         $thumb->delete();
         return FALSE;
     }
     return TRUE;
 }
Beispiel #8
0
/**
 * This makes sure that the image is present (builds it if it isn't) and then
 * displays it.
 */
function identicon_check($entity)
{
    //make sure the image functions are available before trying to make avatars
    if (function_exists("imagecreatetruecolor")) {
        // entity is group, user or something else?
        if ($entity instanceof ElggGroup) {
            $file = new ElggFile();
            $file->owner_guid = $entity->owner_guid;
            $seed = identicon_seed($entity);
            $file->setFilename('identicon/' . $seed . '/master.jpg');
            $file->setMimeType('image/jpeg');
            if (!$file->exists()) {
                if (identicon_build_group($seed, $file)) {
                    return true;
                } else {
                    // there was some error building the icon
                    return false;
                }
            } else {
                // file's already there
                return true;
            }
        } else {
            if ($entity instanceof ElggUser) {
                $file = new ElggFile();
                $file->owner_guid = $entity->getGUID();
                $seed = identicon_seed($entity);
                $file->setFilename('identicon/' . $seed . '/master.jpg');
                $file->setMimeType('image/jpeg');
                if (!$file->exists()) {
                    if (identicon_build($seed, $file)) {
                        return true;
                    } else {
                        // there was some error building the icon
                        return false;
                    }
                } else {
                    // file's already there
                    return true;
                }
            } else {
                // neither group nor user
                return false;
            }
        }
    }
    // we can't build the icon
    return false;
}
/**
 * Convert a video/audio file to a web compatible format
 * 
 * @param ElggFile $file   File entity
 * @param string   $format Format to convert to (extension)
 * @return ElggFile|false
 */
function elgg_file_viewer_convert_file($file, $format)
{
    if (!$file instanceof ElggFile || !$format) {
        return false;
    }
    $ffmpeg_path = elgg_get_plugin_setting('ffmpeg_path', 'elgg_file_viewer');
    if (!$ffmpeg_path) {
        return false;
    }
    $info = pathinfo($file->getFilenameOnFilestore());
    $filename = $info['filename'];
    $output = new ElggFile();
    $output->owner_guid = $file->owner_guid;
    $output->setFilename("projekktor/{$file->guid}/{$filename}.{$format}");
    $output->open('write');
    $output->close();
    try {
        $FFmpeg = new FFmpeg($ffmpeg_path);
        if (!$file->icontime) {
            $icon = new ElggFile();
            $icon->owner_guid = $file->owner_guid;
            $icon->setFilename("projekktor/{$file->guid}/{$filename}.jpg");
            $FFmpeg->input($file->getFilenameOnFilestore())->thumb(0, 1)->output($icon->getFilenameOnFilestore())->ready();
            if ($icon->exists()) {
                $file->icontime = time();
                $file->ffmpeg_thumb = $icon->getFilename();
                $prefix = 'file/';
                $filestorename = $file->icontime . $filename . '.jpg';
                $thumbnail = get_resized_image_from_existing_file($icon->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($icon->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($icon->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);
                }
            }
        }
        $FFmpeg->input($file->getFilenameOnFilestore())->output($output->getFilenameOnFilestore())->ready();
        elgg_log("Converting file {$file->guid} to {$format}: {$FFmpeg->command}", 'NOTICE');
    } catch (Exception $ex) {
        elgg_log($ex->getMessage(), 'ERROR');
        return false;
    }
    return $output;
}
Beispiel #10
0
/**
 * 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;
}
Beispiel #11
0
 /**
  * saves the image file
  * @param array $file_array
  *                      destination => path where to save the file
  *                      content => content of image
  *                      owner_id => owner id of the image file
  *
  * @return boolean
  */
 public static function saveImageFile($file_array)
 {
     if (!is_array($file_array)) {
         return FALSE;
     }
     $destination = $file_array['destination'];
     $content = $file_array['content'];
     $filehandler = new ElggFile();
     $filehandler->owner_guid = $file_array['owner_guid'] ? $file_array['owner_guid'] : elgg_get_logged_in_user_guid();
     $filehandler->setFilename($destination . '.jpg');
     $filehandler->open("write");
     $return = $filehandler->write($content);
     $filehandler->close();
     if ($file_array['create_thumbs'] && $return) {
         $thumbtopbar = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 16, 16, true);
         $thumbtiny = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 25, 25, true);
         $thumbsmall = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 40, 40, true);
         $thumbmedium = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 100, 100, true);
         $thumblarge = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 200, 200, false);
         if ($thumbtiny) {
             $thumb = new ElggFile();
             $thumb->owner_guid = $file_array['owner_guid'] ? $file_array['owner_guid'] : elgg_get_logged_in_user_guid();
             $thumb->setMimeType('image/jpeg');
             $thumb->setFilename($destination . 'topbar.jpg');
             $thumb->open("write");
             $thumb->write($thumbtopbar);
             $thumb->close();
             $thumb->setFilename($destination . 'tiny.jpg');
             $thumb->open("write");
             $thumb->write($thumbtiny);
             $thumb->close();
             $thumb->setFilename($destination . 'small.jpg');
             $thumb->open("write");
             $thumb->write($thumbsmall);
             $thumb->close();
             $thumb->setFilename($destination . 'medium.jpg');
             $thumb->open("write");
             $thumb->write($thumbmedium);
             $thumb->close();
             $thumb->setFilename($destination . 'large.jpg');
             $thumb->open("write");
             $thumb->write($thumblarge);
             $thumb->close();
         }
     }
     return $return;
 }
Beispiel #12
0
/**
 * Add images to market post
 *
 * @param Market $post
 * @return array
 */
function market_add_image($post = NULL, $data = NULL, $imagenum = 0)
{
    if (!$post || !$data) {
        return false;
    }
    $filenum = $imagenum;
    if ($imagenum == 1) {
        $filenum = '';
    }
    $prefix = "market/" . $post->guid;
    $filehandler = new ElggFile();
    $filehandler->owner_guid = $post->owner_guid;
    $filehandler->setFilename($prefix . $filenum . ".jpg");
    $filehandler->open("write");
    $filehandler->write($data);
    $filehandler->close();
    $small = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 40, 40, true);
    $medium = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 153, 153, true);
    $large = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 200, 200, false);
    $master = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 600, 800, false);
    if ($small) {
        $sizes = array('small' => $small, 'medium' => $medium, 'large' => $large, 'master' => $master);
        foreach ($sizes as $size => $imgdata) {
            $thumb = new ElggFile();
            $thumb->owner_guid = $post->owner_guid;
            $thumb->setMimeType('image/jpeg');
            $thumb->setFilename($prefix . $size . $filenum . '.jpg');
            $thumb->open('write');
            $thumb->write($imgdata);
            $thumb->close();
        }
        // Set image in metadata array
        market_set_images($post, $imagenum);
    }
}
$file->setMimeType($_FILES['upload']['type']);
$file->owner_guid = $user->guid;
$file->subtype = "file";
$file->originalfilename = $filestorename;
$file->access_id = ACCESS_DEFAULT;
$file->open("write");
$file->write(get_uploaded_file('upload'));
$file->close();
$result = $file->save();
if ($result) {
    $master = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 550, 550);
    if ($master !== false) {
        $_SESSION['UPLOAD_DATA']['file_save'] = "started";
        $filehandler = new ElggFile();
        $filehandler->setFilename($filestorename);
        $filehandler->setMimeType($_FILES['upload']['type']);
        $filehandler->owner_guid = $user->guid;
        $filehandler->subtype = "file";
        $filehandler->originalfilename = $filestorename;
        $filehandler->access_id = ACCESS_DEFAULT;
        $filehandler->open("write");
        $filehandler->write($master);
        $filehandler->close();
        $filehandler->save();
        $url = '' . $CONFIG->url . 'mod/CKEditor/image_viewer.php?file_guid=' . $filehandler->guid;
        echo '<script type="text/javascript">
		window.parent.CKEDITOR.tools.callFunction(' . $funcNum2 . ', "' . $url . '","Success");
		</script>';
        exit;
    } else {
        echo '<script type="text/javascript">
 protected function createIconFile($size = 'small')
 {
     $file = new ElggFile();
     $sizes = elgg_get_config('icon_sizes');
     $tmp_name = tempnam("asd", "qerty");
     $contents = "";
     $success = false;
     if ($this->open("read")) {
         $contents = $this->read($this->getSize());
         if ($contents) {
             file_put_contents($tmp_name, $contents);
             $success = true;
         }
         $this->close();
     }
     if ($success == false) {
         register_error("cant read file");
         return null;
     }
     $imgsizearray = getimagesize($tmp_name);
     if ($imgsizearray == false) {
         register_error("bad file");
         return null;
     }
     $width = $imgsizearray[0];
     $height = $imgsizearray[1];
     $size_info = $sizes[$size];
     $file->setFilename(self::genGUID());
     $file->access_id = 2;
     $file->setMimeType($this->getMimeType());
     $file->save();
     $file->open("write");
     $file->write(self::cropImage($tmp_name, $width, $height, $size_info['w'], $size_info['h']));
     $file->close();
     unlink($tmp_name);
     return $file->guid;
 }
Beispiel #15
0
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 = sanitize_string(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);
                        $file_size = zip_entry_filesize($zip_entry);
                        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 = mime_content_type($filehandler->getFilenameOnFilestore());
                            $simple_type = explode("/", $mime_type);
                            $filehandler->setMimeType($mime_type);
                            $filehandler->simpletype = $simple_type[0];
                            if ($simple_type[0] == "image") {
                                $filestorename = elgg_strtolower(time() . $folder);
                                $thumbnail = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 60, 60, true);
                                if ($thumbnail) {
                                    $thumb = new ElggFile();
                                    $thumb->setMimeType($mime_type);
                                    $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);
                                }
                            }
                            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;
}
Beispiel #16
0
/**
 * 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;
}
Beispiel #17
0
function hj_framework_handle_multifile_upload($user_guid)
{
    if (!empty($_FILES)) {
        $access = elgg_get_ignore_access();
        elgg_set_ignore_access(true);
        $file = $_FILES['Filedata'];
        $filehandler = new hjFile();
        $filehandler->owner_guid = (int) $user_guid;
        $filehandler->container_guid = (int) $user_guid;
        $filehandler->access_id = ACCESS_DEFAULT;
        $filehandler->data_pattern = hj_framework_get_data_pattern('object', 'hjfile');
        $filehandler->title = $file['name'];
        $filehandler->description = '';
        $prefix = "hjfile/";
        $filestorename = elgg_strtolower($file['name']);
        $mime = hj_framework_get_mime_type($file['name']);
        $filehandler->setFilename($prefix . $filestorename);
        $filehandler->setMimeType($mime);
        $filehandler->originalfilename = $file['name'];
        $filehandler->simpletype = hj_framework_get_simple_type($mime);
        $filehandler->filesize = round($file['size'] / (1024 * 1024), 2) . "Mb";
        $filehandler->open("write");
        $filehandler->close();
        move_uploaded_file($file['tmp_name'], $filehandler->getFilenameOnFilestore());
        $file_guid = $filehandler->save();
        hj_framework_set_entity_priority($filehandler);
        elgg_trigger_plugin_hook('hj:framework:file:process', 'object', array('entity' => $filehandler));
        if ($file_guid) {
            $meta_value = $filehandler->getGUID();
        } else {
            $meta_value = $filehandler->getFilenameOnFilestore();
        }
        if ($file_guid && $filehandler->simpletype == "image") {
            $thumb_sizes = hj_framework_get_thumb_sizes();
            foreach ($thumb_sizes as $thumb_type => $thumb_size) {
                $thumbnail = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $thumb_size['w'], $thumb_size['h'], $thumb_size['square'], 0, 0, 0, 0, true);
                if ($thumbnail) {
                    $thumb = new ElggFile();
                    $thumb->setMimeType($file['type']);
                    $thumb->owner_guid = $user_guid;
                    $thumb->setFilename("{$prefix}{$filehandler->getGUID()}{$thumb_type}.jpg");
                    $thumb->open("write");
                    $thumb->write($thumbnail);
                    $thumb->close();
                    $thumb_meta = "{$thumb_type}thumb";
                    $filehandler->{$thumb_meta} = $thumb->getFilename();
                    unset($thumbnail);
                }
            }
        }
        $response = array('status' => 'OK', 'value' => $meta_value);
    } else {
        $response = array('status' => 'FAIL');
    }
    echo json_encode($response);
    elgg_set_ignore_access($access);
    return;
}
 			trigger_error('Tidypics warning: image memory size too large for resizing so rejecting', E_USER_WARNING);
 			continue;
 		}
 	} else if ($image_lib == 'ImageMagickPHP') {
 		// haven't been able to determine a limit like there is for GD
 	}
 */
 $mime = "image/jpeg";
 //not sure how to get this from the file if we aren't posting it
 //this will save to users folder in /image/ and organize by photo album
 $prefix = "image/" . $container_guid . "/";
 $file = new ElggFile();
 $filestorename = elgg_strtolower(time() . $name);
 $file->setFilename($prefix . $filestorename . ".jpg");
 //that's all flickr stores so I think this is safe
 $file->setMimeType($mime);
 $file->originalfilename = $name;
 $file->subtype = "image";
 $file->simpletype = "image";
 $file->access_id = $access_id;
 if ($container_guid) {
     $file->container_guid = $container_guid;
 }
 // get the file from flickr and save it locally
 $filename = $file->getFilenameOnFilestore();
 $destination = fopen($filename, "w");
 $source = fopen($photo["url"], "r");
 while ($a = fread($source, 1024)) {
     fwrite($destination, $a);
 }
 fclose($source);
Beispiel #19
0
/**
 *
 * 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');
            }
        }
    }
}
function file_handle_upload($prefix, $subtype, $plugin)
{
    $desc = get_input("description");
    $tags = get_input("tags");
    $tags = explode(",", $tags);
    $folder = get_input("folder_text");
    if (!$folder) {
        $folder = get_input("folder_select");
    }
    $access_id = (int) get_input("access_id");
    $container_guid = (int) get_input('container_guid', 0);
    if (!$container_guid) {
        $container_guid == get_loggedin_userid();
    }
    // Extract file from, save to default filestore (for now)
    // see if a plugin has set a quota for this user
    $file_quota = trigger_plugin_hook("{$plugin}:quotacheck", 'user', array('container_guid' => $container_guid));
    if (!$file_quota) {
        // no, see if there is a generic quota set
        $file_quota = get_plugin_setting('quota', $plugin);
    }
    if ($file_quota) {
        // convert to megabytes
        $file_quota = $file_quota * 1000 * 1024;
    }
    // handle uploaded files
    $number_of_files = get_input('number_of_files', 0);
    $quota_exceeded = false;
    $bad_mime_type = false;
    for ($i = 0; $i < $number_of_files; $i++) {
        $title = get_input("title_" . $i);
        $uploaded = $_FILES["upload_" . $i];
        if (!$uploaded || !$uploaded['name']) {
            // no such file, so skip it
            continue;
        }
        if ($plugin == "photo") {
            // do a mime type test
            if (in_array($uploaded['type'], array('image/jpeg', 'image/gif', 'image/png', 'image/jpg', 'image/jpe', 'image/pjpeg', 'image/x-png'))) {
                $file = new PhotoPluginFile();
            } else {
                $bad_mime_type = true;
                break;
            }
        } else {
            $file = new FilePluginFile();
        }
        $dir_size = $file->getFilestoreSize($prefix, $container_guid);
        $filestorename = strtolower(time() . $uploaded['name']);
        $file->setFilename($prefix . $filestorename);
        $file->setMimeType($uploaded['type']);
        $file->originalfilename = $uploaded['name'];
        $file->subtype = $subtype;
        $file->access_id = $access_id;
        $uf = get_uploaded_file('upload_' . $i);
        if ($file_quota) {
            $file_size = strlen($uf);
            if ($dir_size + $file_size > $file_quota) {
                $quota_exceeded = true;
            }
        }
        if (!$quota_exceeded) {
            // all clear, so try to save the data
            $file->open("write");
            $file->write($uf);
            $file->close();
            $file->title = $title;
            $file->description = $desc;
            if ($container_guid) {
                $file->container_guid = $container_guid;
            }
            // Save tags
            $file->tags = $tags;
            $file->simpletype = file_get_general_file_type($uploaded['type']);
            $file->folder = $folder;
            $result = $file->save();
            if ($result) {
                // Generate thumbnail (if image)
                if (substr_count($file->getMimeType(), 'image/')) {
                    $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true);
                    $thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 153, 153, true);
                    $thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 600, 600, false);
                    if ($thumbnail) {
                        $thumb = new ElggFile();
                        $thumb->setMimeType($uploaded['type']);
                        $thumb->setFilename($prefix . "thumb" . $filestorename);
                        $thumb->open("write");
                        $thumb->write($thumbnail);
                        $thumb->close();
                        $file->thumbnail = $prefix . "thumb" . $filestorename;
                        $thumb->setFilename($prefix . "smallthumb" . $filestorename);
                        $thumb->open("write");
                        $thumb->write($thumbsmall);
                        $thumb->close();
                        $file->smallthumb = $prefix . "smallthumb" . $filestorename;
                        $thumb->setFilename($prefix . "largethumb" . $filestorename);
                        $thumb->open("write");
                        $thumb->write($thumblarge);
                        $thumb->close();
                        $file->largethumb = $prefix . "largethumb" . $filestorename;
                    }
                }
                // add to this user's file folders
                file_add_to_folders($folder, $container_guid, $plugin);
                add_to_river("river/object/{$plugin}/create", 'create', $_SESSION['user']->guid, $file->guid);
            } else {
                break;
            }
        } else {
            break;
        }
    }
    if ($quota_exceeded) {
        echo elgg_echo("{$plugin}:quotaexceeded");
    } else {
        if ($bad_mime_type) {
            echo elgg_echo("{$plugin}:badmimetype");
        } else {
            if ($result) {
                if ($number_of_files > 1) {
                    echo elgg_echo("{$plugin}:saved_multi");
                } else {
                    echo elgg_echo("{$plugin}:saved");
                }
            } else {
                if ($number_of_files > 1) {
                    echo elgg_echo("{$plugin}:uploadfailed_multi");
                } else {
                    echo elgg_echo("{$plugin}:uploadfailed");
                }
            }
        }
    }
}
Beispiel #21
0
/**
 * 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 create_file($container_guid, $title, $desc, $access_id, $guid, $tags, $new_file)
{
    // register_error("Creating file: " . $container_guid . ", vars: " . print_r(array($title, $desc, $access_id, $guid, $tags, $new_file), true));
    if ($new_file) {
        // must have a file if a new file upload
        if (empty($_FILES['upload']['name'])) {
            // cache information in session
            $_SESSION['uploadtitle'] = $title;
            $_SESSION['uploaddesc'] = $desc;
            $_SESSION['uploadtags'] = $tags;
            $_SESSION['uploadaccessid'] = $access_id;
            register_error(elgg_echo('file:nofile') . "no file new");
            forward($_SERVER['HTTP_REFERER']);
        }
        $file = new FilePluginFile();
        $file->subtype = "file";
        // if no title on new upload, grab filename
        if (empty($title)) {
            $title = $_FILES['upload']['name'];
        }
    } else {
        // load original file object
        $file = get_entity($guid);
        if (!$file) {
            register_error(elgg_echo('file:cannotload') . 'can"t load existing');
            forward($_SERVER['HTTP_REFERER']);
        }
        // user must be able to edit file
        if (!$file->canEdit()) {
            register_error(elgg_echo('file:noaccess') . 'no access to existing');
            forward($_SERVER['HTTP_REFERER']);
        }
    }
    $file->title = $title;
    $file->description = $desc;
    $file->access_id = $access_id;
    $file->container_guid = $container_guid;
    $tags = explode(",", $tags);
    $file->tags = $tags;
    // we have a file upload, so process it
    if (isset($_FILES['upload']['name']) && !empty($_FILES['upload']['name'])) {
        $prefix = "file/";
        // 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 = get_general_file_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;
                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);
            }
        }
    } else {
        // not saving a file but still need to save the entity to push attributes to database
        $file->save();
    }
    return array($file, $guid);
}
Beispiel #23
0
function profile_manager_profileupdate_user_event($event, $object_type, $user)
{
    if (!empty($user) && $user instanceof ElggUser) {
        // upload a file to your profile
        $accesslevel = get_input('accesslevel');
        if (!is_array($accesslevel)) {
            $accesslevel = array();
        }
        $options = array("type" => "object", "subtype" => CUSTOM_PROFILE_FIELDS_PROFILE_SUBTYPE, "limit" => false, "metadata_name_value_pairs" => array("name" => "metadata_type", "value" => "pm_file"));
        if ($configured_fields = elgg_get_entities_from_metadata($options)) {
            foreach ($configured_fields as $field) {
                // check for uploaded files
                $metadata_name = $field->metadata_name;
                $current_file_guid = $user->{$metadata_name};
                if (isset($accesslevel[$metadata_name])) {
                    $access_id = (int) $accesslevel[$metadata_name];
                } else {
                    // this should never be executed since the access level should always be set
                    $access_id = ACCESS_PRIVATE;
                }
                if (isset($_FILES[$metadata_name]) && $_FILES[$metadata_name]['error'] == 0) {
                    // uploaded file exists so, save it to an ElggFile object
                    // use current_file_guid to overwrite previously uploaded files
                    $filehandler = new ElggFile($current_file_guid);
                    $filehandler->owner_guid = $user->getGUID();
                    $filehandler->container_guid = $user->getGUID();
                    $filehandler->subtype = "file";
                    $filehandler->access_id = $access_id;
                    $filehandler->title = $field->getTitle();
                    $filehandler->setFilename("profile_manager/" . $_FILES[$metadata_name]["name"]);
                    $filehandler->setMimeType($_FILES[$metadata_name]["type"]);
                    $filehandler->open("write");
                    $filehandler->write(get_uploaded_file($metadata_name));
                    $filehandler->close();
                    if ($filehandler->save()) {
                        $filehandler->profile_manager_metadata_name = $metadata_name;
                        // used to retrieve user file when deleting
                        $filehandler->originalfilename = $_FILES[$metadata_name]["name"];
                        create_metadata($user->guid, $metadata_name, $filehandler->getGUID(), 'text', $user->guid, $access_id);
                    }
                } else {
                    // if file not uploaded should it be deleted???
                    if (empty($current_file_guid)) {
                        // find the previously uploaded file and if exists... delete it
                        $options = array("type" => "object", "subtype" => "file", "owner_guid" => $user->getGUID(), "limit" => 1, "metadata_name_value_pairs" => array("name" => "profile_manager_metadata_name", "value" => $metadata_name));
                        if ($files = elgg_get_entities_from_metadata($options)) {
                            $file = $files[0];
                            $file->delete();
                        }
                    } else {
                        if ($file = get_entity($current_file_guid)) {
                            // maybe we need to update the access id
                            $file->access_id = $access_id;
                            $file->save();
                        }
                    }
                }
            }
        }
        // update profile completeness
        profile_manager_profile_completeness($user);
    }
}
Beispiel #24
0
/**
 * @param $title
 * @param $description
 * @param $username
 * @param $access
 * @param $tags
 * @return array
 * @throws InvalidParameterException
 * @internal param $guid
 * @internal param $size
 */
function file_save_post($title, $description, $username, $access, $tags)
{
    $return = array();
    if (!$username) {
        $user = elgg_get_logged_in_user_entity();
    } else {
        $user = get_user_by_username($username);
        if (!$user) {
            throw new InvalidParameterException('registration:usernamenotvalid');
        }
    }
    $loginUser = elgg_get_logged_in_user_entity();
    $container_guid = $loginUser->guid;
    if ($access == 'ACCESS_FRIENDS') {
        $access_id = -2;
    } elseif ($access == 'ACCESS_PRIVATE') {
        $access_id = 0;
    } elseif ($access == 'ACCESS_LOGGED_IN') {
        $access_id = 1;
    } elseif ($access == 'ACCESS_PUBLIC') {
        $access_id = 2;
    } else {
        $access_id = -2;
    }
    $file = $_FILES["upload"];
    if (empty($file)) {
        $response['status'] = 1;
        $response['result'] = elgg_echo("file:blank");
        return $response;
    }
    $new_file = true;
    if ($new_file) {
        $file = new ElggFile();
        $file->subtype = "file";
        // if no title on new upload, grab filename
        if (empty($title)) {
            $title = htmlspecialchars($_FILES['upload']['name'], ENT_QUOTES, 'UTF-8');
        }
    }
    $file->title = $title;
    $file->description = $description;
    $file->access_id = $access_id;
    $file->container_guid = $container_guid;
    $file->tags = string_to_tag_array($tags);
    // we have a file upload, so process it
    if (isset($_FILES['upload']['name']) && !empty($_FILES['upload']['name'])) {
        $prefix = "file/";
        $filestorename = elgg_strtolower(time() . $_FILES['upload']['name']);
        $file->setFilename($prefix . $filestorename);
        $file->originalfilename = $_FILES['upload']['name'];
        $mime_type = $file->detectMimeType($_FILES['upload']['tmp_name'], $_FILES['upload']['type']);
        $file->setMimeType($mime_type);
        $file->simpletype = elgg_get_file_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());
        $fileSaved = $file->save();
        // if image, we need to create thumbnails (this should be moved into a function)
        if ($fileSaved && $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);
            }
        } elseif ($file->icontime) {
            // if it is not an image, we do not need thumbnails
            unset($file->icontime);
            $thumb = new ElggFile();
            $thumb->setFilename($prefix . "thumb" . $filestorename);
            $thumb->delete();
            unset($file->thumbnail);
            $thumb->setFilename($prefix . "smallthumb" . $filestorename);
            $thumb->delete();
            unset($file->smallthumb);
            $thumb->setFilename($prefix . "largethumb" . $filestorename);
            $thumb->delete();
            unset($file->largethumb);
        }
    } else {
        // not saving a file but still need to save the entity to push attributes to database
        $fileSaved = $file->save();
    }
    // handle results differently for new files and file updates
    if ($new_file) {
        if ($fileSaved) {
            elgg_create_river_item(array('view' => 'river/object/file/create', 'action_type' => 'create', 'subject_guid' => elgg_get_logged_in_user_guid(), 'object_guid' => $file->guid));
            $return['guid'] = $file->guid;
            $return['message'] = 'success';
        } else {
            // failed to save file object - nothing we can do about this
            $return['guid'] = 0;
            $return['message'] = elgg_echo("file:uploadfailed");
        }
    } else {
        $return['guid'] = 0;
        $return['message'] = elgg_echo("file:uploadfailed");
    }
    return $return;
}
Beispiel #25
0
$access_id = (int) get_input("access_id", ACCESS_DEFAULT);
$container_guid = (int) get_input('container_guid');
// fallback to user if somebody deleted the group container behind our back
// TODO: ask the user for another group container instead
$container = get_entity($container_guid);
if (!$container) {
    $container_guid = elgg_get_logged_in_user_guid();
}
// create new file object
$file = new ElggFile();
$file->title = $title;
$file->access_id = $access_id;
$file->description = $description;
$file->container_guid = $container_guid;
$file->tags = $tags;
$file->setMimeType("application/vnd.oasis.opendocument.text");
$file->simpletype = "document";
// same naming pattern as in file/actions/file/upload.php
$filestorename = "file/" . elgg_strtolower(time() . $_FILES['upload']['name']);
$file->setFilename($filestorename);
// Open the file to guarantee the directory exists
$file->open("write");
$file->close();
// now put file into destination
move_uploaded_file($_FILES['upload']['tmp_name'], $file->getFilenameOnFilestore());
// create lock
$lock_guid = odt_editor_locking_create_lock($file, $user_guid);
$file->save();
// log success
system_message(elgg_echo("file:saved"));
add_to_river('river/object/file/create', 'create', $user_guid, $file->guid);
Beispiel #26
0
 $file->title = $faker->sentence(6);
 $file->description = $faker->text(500);
 $file->tags = $faker->words(5);
 $file->access_id = $access_id;
 $file->__faker = true;
 $file_url = $faker->imageURL();
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $file_url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 $file_contents = curl_exec($ch);
 $curl_info = curl_getinfo($ch);
 curl_close($ch);
 $mime_type = $curl_info['content_type'];
 $file->setMimeType($mime_type);
 $file->simpletype = file_get_simple_type($mime_type);
 $file->open('write');
 $file->write($file_contents);
 $file->close();
 if ($file->save()) {
     $success++;
     if (substr_count($mime_type, 'image/')) {
         $file->icontime = time();
         $prefix = "files/";
         $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true);
         if ($thumbnail) {
             $thumb = new ElggFile();
             $thumb->owner_guid = $file->owner_guid;
             $thumb->setFilename($prefix . "thumb" . $file->originalfilename);
             $thumb->open("write");
     $filestorename = strtolower(time() . $_FILES['upload']['name']);
 }
 $file->setFilename($prefix . $filestorename);
 $file->setMimeType($_FILES['upload']['type']);
 $file->originalfilename = $_FILES['upload']['name'];
 $file->simpletype = get_general_file_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;
         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);
Beispiel #28
0
 /**
  * 
  * @param string $tmp_name path to file
  * @param string $type mime type
  * @return self
  * @throws Exception
  */
 public static function createFromFile($tmp_name, $type)
 {
     $mime_type = ElggFile::detectMimeType($tmp_name, $type);
     if (false == in_array($mime_type, array("image/jpeg", "image/jpg"))) {
         register_error(elgg_echo("gallery_field:only_jpg"));
         return null;
     }
     $ext = "jpg";
     $file = new self();
     $thumb_file = new ElggFile();
     $random_guid = self::genGUID();
     $file->setFilename($random_guid . "." . $ext);
     $thumb_file->setFilename($random_guid . "_thumb." . $ext);
     $file->setMimeType($mime_type);
     $thumb_file->setMimeType($mime_type);
     $imgsizearray = getimagesize($tmp_name);
     if ($imgsizearray == false) {
         register_error("bad file");
         return null;
     }
     $width = $imgsizearray[0];
     $height = $imgsizearray[1];
     $file->open("write");
     $file->write(self::cropImage($tmp_name, $width, $height, 760, 580));
     $file->close();
     $thumb_file->open("write");
     $thumb_file->write(self::cropImage($tmp_name, $width, $height, 200, 140));
     $thumb_file->close();
     $thumb_file->save();
     $file->thumb_file_guid = $thumb_file->guid;
     $file->save();
     return $file;
 }
Beispiel #29
0
    $filehandler->owner_guid = $group->owner_guid;
    $filehandler->setFilename($prefix . ".jpg");
    $filehandler->open("write");
    $filehandler->write(get_uploaded_file('icon'));
    $filehandler->close();
    $filename = $filehandler->getFilenameOnFilestore();
    $sizes = array('tiny', 'small', 'medium', 'large');
    $thumbs = array();
    foreach ($sizes as $size) {
        $thumbs[$size] = get_resized_image_from_existing_file($filename, $icon_sizes[$size]['w'], $icon_sizes[$size]['h'], $icon_sizes[$size]['square']);
    }
    if ($thumbs['tiny']) {
        // just checking if resize successful
        $thumb = new ElggFile();
        $thumb->owner_guid = $group->owner_guid;
        $thumb->setMimeType('image/jpeg');
        foreach ($sizes as $size) {
            $thumb->setFilename("{$prefix}{$size}.jpg");
            $thumb->open("write");
            $thumb->write($thumbs[$size]);
            $thumb->close();
        }
        $group->icontime = time();
    }
}
// @todo Remove this when #4683 fixed
if ($must_move_icons) {
    $filehandler = new ElggFile();
    $filehandler->setFilename('groups');
    $filehandler->owner_guid = $old_owner_guid;
    $old_path = $filehandler->getFilenameOnFilestore();
Beispiel #30
0
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"));
}