gatekeeper();
// Get variables
$title = get_input("title");
$desc = get_input("description");
$tags = get_input("tags");
$access_id = (int) get_input("access_id");
$container_guid = (int) get_input('container_guid', 0);
if (!$container_guid) {
    $container_guid == $_SESSION['user']->getGUID();
}
// Extract file from, save to default filestore (for now)
$prefix = "file/";
$file = new FilePluginFile();
$filestorename = strtolower(time() . $_FILES['upload']['name']);
$file->setFilename($prefix . $filestorename);
$file->setMimeType($_FILES['upload']['type']);
$file->originalfilename = $_FILES['upload']['name'];
$file->subtype = "file";
$file->access_id = $access_id;
$file->open("write");
$file->write(get_uploaded_file('upload'));
$file->close();
$file->title = $title;
$file->description = $desc;
if ($container_guid) {
    $file->container_guid = $container_guid;
}
// Save tags
$tags = explode(",", $tags);
$file->tags = $tags;
$file->simpletype = get_general_file_type($_FILES['upload']['type']);
예제 #2
0
         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";
 }
 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);
function form_handle_file_upload($fieldname, $access_id, $container_guid = 0)
{
    // Extract file from, save to default filestore (for now)
    $prefix = "file/";
    $file = new FilePluginFile();
    $filestorename = strtolower(time() . $_FILES[$fieldname]['name']);
    $file->setFilename($prefix . $filestorename);
    $file->setMimeType($_FILES[$fieldname]['type']);
    $file->originalfilename = $_FILES[$fieldname]['name'];
    $file->subtype = "file";
    $file->access_id = $access_id;
    $file->open("write");
    $file->write(get_uploaded_file($fieldname));
    $file->close();
    if ($container_guid) {
        $file->container_guid = $container_guid;
    }
    $file->simpletype = get_general_file_type($_FILES[$fieldname]['type']);
    $result = $file->save();
    if ($result) {
        form_generate_thumbnail($file, $fieldname);
    }
    return $file->getGUID();
}
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");
                }
            }
        }
    }
}
예제 #5
0
 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);
     }
 }
예제 #6
0
 public function createFile($parent, $params = array())
 {
     if ($parent instanceof ElggUser | $parent instanceof ElggGroup) {
         $container = $parent;
     } else {
         $container = $parent->getContainerEntity();
     }
     if (isset($params['access_id'])) {
         $access_id = $params['access_id'];
     } else {
         if ($parent instanceof ElggObject) {
             // lower level folder
             $access_id = $parent->access_id;
         } elseif ($parent instanceof ElggGroup) {
             // top level folder
             $access_id = $parent->group_acl;
         }
     }
     $file = new FilePluginFile();
     $file->title = $this->generateTitle($params['filename'], $parent);
     $file->access_id = $access_id;
     $file->write_access_id = $params['write_access_id'];
     if (!$file->write_access_id) {
         $file->write_access_id = ACCESS_PRIVATE;
     }
     $file->container_guid = $container->guid;
     $filestorename = elgg_strtolower(time() . $params['filename']);
     $file->setFilename("file/" . $filestorename);
     $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);
     $file->save();
     if ($parent instanceof ElggObject) {
         add_entity_relationship($parent->guid, "folder_of", $file->guid);
     }
     pleiofile_generate_file_thumbs($file);
     return $file;
 }