/** * Returns publically accessible URL * @return string|false */ public function getURL() { if (!$this->file instanceof \ElggFile || !$this->file->exists()) { elgg_log("Unable to resolve resource URL for a file that does not exist on filestore"); return false; } $relative_path = ''; $root_prefix = _elgg_services()->config->get('dataroot'); $path = $this->file->getFilenameOnFilestore(); if (substr($path, 0, strlen($root_prefix)) == $root_prefix) { $relative_path = substr($path, strlen($root_prefix)); } if (!$relative_path) { elgg_log("Unable to resolve relative path of the file on the filestore"); return false; } $data = array('expires' => isset($this->expires) ? $this->expires : 0, 'last_updated' => filemtime($this->file->getFilenameOnFilestore()), 'disposition' => $this->disposition == self::DISPOSITION_INLINE ? 'i' : 'a', 'path' => $relative_path); if ($this->use_cookie) { $data['cookie'] = _elgg_services()->session->getId(); if (empty($data['cookie'])) { return false; } $data['use_cookie'] = 1; } else { $data['use_cookie'] = 0; } ksort($data); $mac = elgg_build_hmac($data)->getToken(); return elgg_normalize_url("mod/proxy/e{$data['expires']}/l{$data['last_updated']}/d{$data['disposition']}/c{$data['use_cookie']}/{$mac}/{$relative_path}"); }
/** * {@inheritdoc} */ public function handle(ElggEntity $entity) { $value = get_input($this->getShortname()); if (!$entity->guid) { return $entity; } $old_owner_guid = $entity->owner_guid; $new_owner_guid = $value === null ? $old_owner_guid : (int) $value; $owner_has_changed = false; $old_icontime = null; if (!$new_owner_guid || $new_owner_guid == $old_owner_guid) { return $entity; } $user = elgg_get_logged_in_user_entity(); // verify new owner is member and old owner/admin is logged in if ($entity->isMember(get_user($new_owner_guid)) && ($old_owner_guid == $user->guid || $user->isAdmin())) { $entity->owner_guid = $new_owner_guid; if ($entity->container_guid == $old_owner_guid) { // Even though this action defaults container_guid to the logged in user guid, // the group may have initially been created with a custom script that assigned // a different container entity. We want to make sure we preserve the original // container if it the group is not contained by the original owner. $entity->container_guid = $new_owner_guid; } $metadata = elgg_get_metadata(['guid' => $entity->guid, 'limit' => false]); if ($metadata) { foreach ($metadata as $md) { if ($md->owner_guid == $old_owner_guid) { $md->owner_guid = $new_owner_guid; $md->save(); } } } // @todo Remove this when #4683 fixed $owner_has_changed = true; $old_icontime = $entity->icontime; } $must_move_icons = $owner_has_changed && $old_icontime; if ($must_move_icons) { $filehandler = new ElggFile(); $filehandler->setFilename('groups'); $filehandler->owner_guid = $old_owner_guid; $old_path = $filehandler->getFilenameOnFilestore(); $icon_sizes = hypeApps()->iconFactory->getSizes($entity); $sizes = array_keys($icon_sizes); array_unshift($sizes, ''); // move existing to new owner $filehandler->owner_guid = $entity->owner_guid; $new_path = $filehandler->getFilenameOnFilestore(); foreach ($sizes as $size) { rename("{$old_path}/{$entity->guid}{$size}.jpg", "{$new_path}/{$entity->guid}{$size}.jpg"); } } return $entity; }
function generateMultiSizesForFile($type, $iconName) { $filehandler = new ElggFile(); // 1. must run this code as admin // 2. and put some original icon files in the file dir of the admin // example dir: data/2014/01/18/33/myicon/user, and // data/2014/01/18/33/myicon/group // 3. the files must have the right permission set: // user:group should be like www-data:www-data on linux $filehandler->owner_guid = elgg_get_logged_in_user_guid(); $filehandler->setFilename("myicon/" . $type . "/" . $iconName . ".png"); $fileName = $filehandler->getFilenameOnFilestore(); //fordebug register_error($fileName); $prefix = "myicon/" . $type . "/" . $iconName; $icon_sizes = elgg_get_config('icon_sizes'); $sizes = array('large', 'medium', 'small', 'tiny', 'master', 'topbar'); $thumbs = array(); foreach ($sizes as $size) { //fordebug register_error("{$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 = elgg_get_logged_in_user_guid(); $thumb->setMimeType('image/jpeg'); foreach ($sizes as $size) { $thumb->setFilename("{$prefix}{$size}.jpg"); //fordebug register_error("{$prefix}{$size}.jpg"); $thumb->open("write"); $thumb->write($thumbs[$size]); $thumb->close(); } } }
/** * Returns publicly accessible URL * @return string|false */ public function getURL() { if (!$this->file instanceof \ElggFile || !$this->file->exists()) { elgg_log("Unable to resolve resource URL for a file that does not exist on filestore"); return false; } $relative_path = ''; $root_prefix = _elgg_services()->config->getDataPath(); $path = $this->file->getFilenameOnFilestore(); if (substr($path, 0, strlen($root_prefix)) == $root_prefix) { $relative_path = substr($path, strlen($root_prefix)); } if (!$relative_path) { elgg_log("Unable to resolve relative path of the file on the filestore"); return false; } $data = array('expires' => isset($this->expires) ? $this->expires : 0, 'last_updated' => filemtime($this->file->getFilenameOnFilestore()), 'disposition' => $this->disposition == self::INLINE ? 'i' : 'a', 'path' => $relative_path); if ($this->use_cookie) { $data['cookie'] = _elgg_services()->session->getId(); if (empty($data['cookie'])) { return false; } $data['use_cookie'] = 1; } else { $data['use_cookie'] = 0; } ksort($data); $mac = _elgg_services()->crypto->getHmac($data)->getToken(); $url_segments = array('serve-file', "e{$data['expires']}", "l{$data['last_updated']}", "d{$data['disposition']}", "c{$data['use_cookie']}", $mac, $relative_path); return elgg_normalize_url(implode('/', $url_segments)); }
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(); } }
/** * Delete item photo from diskspace * * @return boolean */ public function del_photo() { $photo_sizes = elgg_get_config('amapnews_photo_sizes'); foreach ($photo_sizes as $name => $photo_info) { $file = new ElggFile(); $file->owner_guid = $this->owner_guid; $file->setFilename("amapnews/{$this->getGUID()}{$name}.jpg"); $filepath = $file->getFilenameOnFilestore(); if (!$file->delete()) { // do nothing } } return true; }
/** * @group FileService */ public function testResponseHeadersMatchFileAttributesForAttachmentUrls() { $file = new \Elgg\FileService\File(); $file->setFile($this->file); $file->setDisposition('attachment'); $file->bindSession(true); $request = $this->createRequest($file); $response = $this->handler->getResponse($request); $this->assertEquals('text/plain', $response->headers->get('Content-Type')); $filesize = filesize($this->file->getFilenameOnFilestore()); $this->assertEquals($filesize, $response->headers->get('Content-Length')); $this->assertContains('attachment', $response->headers->get('Content-Disposition')); $this->assertEquals('"' . $this->file->getModifiedTime() . '"', $response->headers->get('Etag')); }
function testElggFileDelete() { global $CONFIG; $user = $this->createTestUser(); $filestore = $this->filestore; $dir = new \Elgg\EntityDirLocator($user->guid); $file = new \ElggFile(); $file->owner_guid = $user->guid; $file->setFilename('testing/ElggFileDelete'); $this->assertTrue($file->open('write')); $this->assertTrue($file->write('Test')); $this->assertTrue($file->close()); $file->save(); $filename = $file->getFilenameOnFilestore($file); $filepath = $CONFIG->dataroot . $dir . "testing/ElggFileDelete"; $this->assertIdentical($filename, $filepath); $this->assertTrue(file_exists($filepath)); $this->assertTrue($file->delete()); $this->assertFalse(file_exists($filepath)); $user->delete(); }
function viewCK($page, $identifier, $obj) { include_once dirname(dirname(dirname(__FILE__))) . "/engine/start.php"; $file_guid = (int) get_input('file_guid'); $file = get_entity($file_guid); $readfile = new ElggFile(); $readfile->owner_guid = $file->owner_guid; $readfile->setFilename($file->originalfilename); $filename = $readfile->getFilenameOnFilestore(); $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $mime = $file->getMimeType(); $expires = 14 * 60 * 60 * 24; header("Content-Type: {$mime}"); header("Content-Length: " . strlen($contents)); header("Cache-Control: public", true); header("Pragma: public", true); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT', true); echo $contents; return true; }
public function testFilenameOnFilestore() { global $CONFIG; // create a user to own the file $user = $this->createTestUser(); $created = date('Y/m/d', $user->time_created); // setup a test file $file = new ElggFile(); $file->owner_guid = $user->guid; $file->setFilename('testing/filestore.txt'); $file->open('write'); $file->write('Testing!'); $this->assertTrue($file->close()); // ensure filename and path is expected $filename = $file->getFilenameOnFilestore($file); $filepath = "{$CONFIG->dataroot}{$created}/{$user->guid}/testing/filestore.txt"; $this->assertIdentical($filename, $filepath); $this->assertTrue(file_exists($filepath)); // ensure file removed on user delete $user->delete(); $this->assertFalse(file_exists($filepath)); }
* */ $guid = get_input('guid'); $owner = get_entity($guid); if (!$owner || !$owner instanceof ElggUser || !$owner->canEdit()) { register_error(elgg_echo('avatar:crop:fail')); forward(REFERER); } $x1 = (int) get_input('x1', 0); $y1 = (int) get_input('y1', 0); $x2 = (int) get_input('x2', 0); $y2 = (int) get_input('y2', 0); $filehandler = new ElggFile(); $filehandler->owner_guid = $owner->getGUID(); $filehandler->setFilename("profile/" . $owner->guid . "master" . ".jpg"); $filename = $filehandler->getFilenameOnFilestore(); $icon_sizes = elgg_get_config('icon_sizes'); unset($icon_sizes['master']); // get the images and save their file handlers into an array // so we can do clean up if one fails. $files = array(); foreach ($icon_sizes as $name => $size_info) { $resized = get_resized_image_from_existing_file($filename, $size_info['w'], $size_info['h'], $size_info['square'], $x1, $y1, $x2, $y2, $size_info['upscale']); if ($resized) { //@todo Make these actual entities. See exts #348. $file = new ElggFile(); $file->owner_guid = $guid; $file->setFilename("profile/{$guid}{$name}.jpg"); $file->open('write'); $file->write($resized); $file->close();
} $dgroup->save(); if (!$dgroup->isMember($user)) { $dgroup->join($user); } // Creator always a member // Now see if we have a file icon if (isset($_FILES['icon']) && substr_count($_FILES['icon']['type'], 'image/')) { $prefix = "dgroups/" . $dgroup->guid; $filehandler = new ElggFile(); $filehandler->owner_guid = $dgroup->owner_guid; $filehandler->setFilename($prefix . ".jpg"); $filehandler->open("write"); $filehandler->write(get_uploaded_file('icon')); $filehandler->close(); $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 = $dgroup->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();
function testDetectMimeType() { $user = $this->createTestUser(); $file = new \ElggFile(); $file->owner_guid = $user->guid; $file->setFilename('testing/filestore.txt'); $file->open('write'); $file->write('Testing!'); $file->close(); $mime = $file->detectMimeType(null, 'text/plain'); // mime should not be null if default is set $this->assertTrue(isset($mime)); // mime of a file object should match mime of a file path that represents this file on filestore $resource_mime = $file->detectMimeType($file->getFilenameOnFilestore(), 'text/plain'); $this->assertIdentical($mime, $resource_mime); // calling detectMimeType statically raises strict policy warning // @todo: remove this once a new static method has been implemented error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED); // method output should not differ between a static and a concrete call if the file path is set $resource_mime_static = \ElggFile::detectMimeType($file->getFilenameOnFilestore(), 'text/plain'); $this->assertIdentical($resource_mime, $resource_mime_static); error_reporting(E_ALL); $user->delete(); }
<?php /** * Avatar remove action */ $guid = get_input('guid'); $user = get_entity($guid); if ($user) { // Delete all icons from diskspace $icon_sizes = elgg_get_config('icon_sizes'); foreach ($icon_sizes as $name => $size_info) { $file = new ElggFile(); $file->owner_guid = $guid; $file->setFilename("profile/{$guid}{$name}.jpg"); $filepath = $file->getFilenameOnFilestore(); if (!$file->delete()) { elgg_log("Avatar file remove failed. Remove {$filepath} manually, please.", 'WARNING'); } } // Remove crop coords unset($user->x1); unset($user->x2); unset($user->y1); unset($user->y2); // Remove icon unset($user->icontime); system_message(elgg_echo('avatar:remove:success')); } else { register_error(elgg_echo('avatar:remove:fail')); } forward(REFERER);
/** * Get image directory path * * Each album gets a subdirectory based on its container id * * @return string path to image directory */ function tp_get_img_dir($album_guid) { $file = new ElggFile(); $file->setFilename("image/{$album_guid}"); return $file->getFilenameOnFilestore($file); }
function theme_eersel_get_slider_images() { static $result; if (!isset($result)) { $result = false; $plugin = elgg_get_plugin_from_id("theme_eersel"); $fh = new ElggFile(); $fh->owner_guid = $plugin->getGUID(); $prefix = "slider_images/"; for ($i = 1; $i <= 5; $i++) { $fh->setFilename($prefix . $i . ".jpg"); if ($fh->exists()) { $mod_time = filemtime($fh->getFilenameOnFilestore()); if (!$result) { $result = array(); } $result[$i] = "theme_eersel/slider/" . $i . "/" . $mod_time . ".jpg"; } } } return $result; }
$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); // reply to client $reply = array("file_guid" => $file->guid, "document_url" => elgg_get_site_url() . "file/download/{$file->uid}", "file_name" => $file->getFilename(), "lock_guid" => $lock_guid); print json_encode($reply); // remove lock from old file if ($old_file_guid != 0) { $old_file = new ElggFile($old_file_guid); if ($old_file && odt_editor_locking_lock_guid($old_file) == $old_lock_guid && odt_editor_locking_lock_owner_guid($old_file) == $user_guid) { odt_editor_locking_remove_lock($old_file); }
$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(); $sizes = array('', 'tiny', 'small', 'medium', 'large'); if ($has_uploaded_icon) { // delete those under old owner foreach ($sizes as $size) { unlink("{$old_path}/{$group_guid}{$size}.jpg"); } } else { // move existing to new owner $filehandler->owner_guid = $group->owner_guid; $new_path = $filehandler->getFilenameOnFilestore(); foreach ($sizes as $size) { rename("{$old_path}/{$group_guid}{$size}.jpg", "{$new_path}/{$group_guid}{$size}.jpg"); } } }
/** * Delete the album directory on disk */ protected function deleteAlbumDir() { $tmpfile = new ElggFile(); $tmpfile->setFilename('image/' . $this->guid . '/._tmp_del_tidypics_album_'); $tmpfile->subtype = 'image'; $tmpfile->owner_guid = $this->owner_guid; $tmpfile->container_guid = $this->guid; $tmpfile->open("write"); $tmpfile->write(''); $tmpfile->close(); $tmpfile->save(); $albumdir = eregi_replace('/._tmp_del_tidypics_album_', '', $tmpfile->getFilenameOnFilestore()); $tmpfile->delete(); // sanity check: must be a directory if (!($handle = opendir($albumdir))) { return false; } // loop through all files that might still remain undeleted in this directory and delete them // note: this does not delete the corresponding image entities from the database while (($file = readdir($handle)) !== false) { if (in_array($file, array('.', '..'))) { continue; } $path = "{$albumdir}/{$file}"; unlink($path); } // remove empty directory closedir($handle); return rmdir($albumdir); }
/** * Returns the contents of the \ElggFile file. * * @param \ElggFile $file File object * * @return string */ public function grabFile(\ElggFile $file) { return file_get_contents($file->getFilenameOnFilestore()); }
/** * 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; }
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']; if (substr_count($mime_type, 'image/')) { $prefix = "groups/{$group->guid}"; $filehandler = new ElggFile(); $filehandler->owner_guid = $group->owner_guid; $filehandler->setFilename("{$prefix}.jpg"); $filehandler->open('write'); $filehandler->write($file_contents); $filehandler->close(); foreach ($icon_sizes as $name => $size_info) { $resized = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $size_info['w'], $size_info['h'], $size_info['square'], 0, 0, 0, 0, $size_info['upscale']); if ($resized) { $file = new ElggFile(); $file->owner_guid = $group->owner_guid; $file->setFilename("{$prefix}{$name}.jpg"); $file->open('write'); $file->write($resized); $file->close(); $files[] = $file; } else { $avatar_error = true; } } if (!empty($avatar_error)) { foreach ($files as $file) { $file->delete();
/** * * 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'); } } } }
/** * Creates entity media thumbnails * * @param mixed $file Path to file, or an array with 'tmp_name' and 'name', or ElggFile * @param \ElggEntity $entity Entity * @param string $type Media type * @param array $crop_coords Cropping coordinates * @return \ElggFile[]|false */ function elgg_create_media_thumbnails($file, \ElggEntity $entity, $type = 'icon', $crop_coords = []) { if ($file instanceof ElggFile) { $path = $file->getFilenameOnFilestore(); } else { if (is_array($file)) { $path = elgg_extract('tmp_name', (array) $file); } else { $path = (string) $file; } } if (!file_exists($path)) { return false; } $thumb_sizes = elgg_media_get_thumb_sizes($entity, $type); $master_info = $thumb_sizes['master']; unset($thumb_sizes['master']); // master is used as base for cropping $filehandler = elgg_get_media_original($entity, $type); if (!$filehandler) { return false; } $resized = get_resized_image_from_existing_file($path, $master_info['w'], $master_info['h'], $master_info['square'], 0, 0, 0, 0, $master_info['upscale']); if (!$resized) { return false; } $master = new ElggFile(); $master->owner_guid = $entity->guid; $master->setFilename("media/{$type}/master.jpg"); $master->open('write'); $master->write($resized); $master->close(); $thumbs = []; $x1 = (int) elgg_extract('x1', $crop_coords); $y1 = (int) elgg_extract('y1', $crop_coords); $x2 = (int) elgg_extract('x2', $crop_coords); $y2 = (int) elgg_extract('y2', $crop_coords); foreach ($thumb_sizes as $name => $size_info) { $resized = get_resized_image_from_existing_file($path, $size_info['w'], $size_info['h'], $size_info['square'], $x1, $y1, $x2, $y2, $size_info['upscale']); if (!$resized) { foreach ($thumbs as $thumb) { $thumb->delete(); } return false; } $thumb = new ElggFile(); $thumb->owner_guid = $entity->guid; $thumb->setFilename("media/{$type}/{$name}.jpg"); $thumb->open('write'); $thumb->write($resized); $thumb->close(); $thumbs[] = $thumb; } //$entity->{"{$type}time"} = time(); $entity->{"{$type}_time_created"} = time(); // normalize coords to master $natural_image_size = getimagesize($path); $master_image_size = getimagesize($master->getFilenameOnFilestore()); $ratio = $master_image_size[0] / $natural_image_size[0]; $entity->{"{$type}_x1"} = $x1 * $ratio; $entity->{"{$type}_x2"} = $x2 * $ratio; $entity->{"{$type}_y1"} = $y1 * $ratio; $entity->{"{$type}_y2"} = $y2 * $ratio; return $thumbs; }
<?php /** * Elgg video thumbnail * * @package Video */ // Get video GUID $video_guid = (int) get_input('video_guid', 0); // Get video thumbnail size $size = get_input('size', 'small'); $video = get_entity($video_guid); if (!elgg_instanceof($video, 'object', 'video')) { exit; } $filename = "video/{$video_guid}/icon-{$size}.jpg"; // Grab the file $thumb = new ElggFile(); $thumb->owner_guid = $video->owner_guid; $thumb->setFilename($filename); $storename = $thumb->getFilenameOnFilestore(); $contents = $thumb->grabFile(); // caching images for 10 days header("Content-type: image/jpg"); header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', strtotime("+10 days")), true); header("Pragma: public", true); header("Cache-Control: public", true); header("Content-Length: " . strlen($contents)); echo $contents; exit;
$user = get_loggedin_user(); $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 = $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">
/** * Create thumbnails using ImageMagick executables * * @param ElggFile holds the image that was uploaded * @param string folder to store thumbnail in * @param string name of the thumbnail * @return bool TRUE on success */ function tp_create_im_cmdline_thumbnails($file, $prefix, $filestorename) { $image_sizes = elgg_get_plugin_setting('image_sizes', 'tidypics'); if (!$image_sizes) { register_error(elgg_echo('tidypics:nosettings')); return FALSE; } $image_sizes = unserialize($image_sizes); $thumb = new ElggFile(); $thumb->owner_guid = $file->owner_guid; $thumb->container_guid = $file->container_guid; // tiny thumbnail $thumb->setFilename($prefix . "thumb" . $filestorename); $thumbname = $thumb->getFilenameOnFilestore(); if (empty($image_sizes['tiny_image_width'])) { // sites upgraded from 1.6 may not have this set $image_sizes['tiny_image_width'] = $image_sizes['tiny_image_height'] = 60; } $rtn_code = tp_im_cmdline_resize($file->getFilenameOnFilestore(), $thumbname, $image_sizes['tiny_image_width'], $image_sizes['tiny_image_height'], TRUE); if (!$rtn_code) { return FALSE; } $file->thumbnail = $prefix . "thumb" . $filestorename; // album thumbnail $thumb->setFilename($prefix . "smallthumb" . $filestorename); $thumbname = $thumb->getFilenameOnFilestore(); $rtn_code = tp_im_cmdline_resize($file->getFilenameOnFilestore(), $thumbname, $image_sizes['small_image_width'], $image_sizes['small_image_height'], TRUE); if (!$rtn_code) { return FALSE; } $file->smallthumb = $prefix . "smallthumb" . $filestorename; // main image $thumb->setFilename($prefix . "largethumb" . $filestorename); $thumbname = $thumb->getFilenameOnFilestore(); $rtn_code = tp_im_cmdline_resize($file->getFilenameOnFilestore(), $thumbname, $image_sizes['large_image_width'], $image_sizes['large_image_height'], FALSE); if (!$rtn_code) { return FALSE; } $file->largethumb = $prefix . "largethumb" . $filestorename; tp_im_cmdline_watermark($thumbname); unset($thumb); return TRUE; }
/** * Returns an ElggFile containing the entity icon * * @param \ElggEntity $entity Entity * @param string $size Size * @return \ElggFile */ public function getIconFile(\ElggEntity $entity, $size = '') { $dir = $this->getIconDirectory($entity, $size); $filename = $this->getIconFilename($entity, $size); $file = new \ElggFile(); $file->owner_guid = $entity instanceof \ElggUser ? $entity->guid : $entity->owner_guid; $file->setFilename("{$dir}/{$filename}"); if (!file_exists($file->getFilenameOnFilestore())) { $file->open('write'); $file->close(); } $file->mimetype = $file->detectMimeType(); return $file; }
$title = $entity->title; } $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8', false); $url = $entity->getURL(); if (isset($vars['href'])) { $url = $vars['href']; } $icon_sizes = elgg_get_config('icon_sizes'); $size = $vars['size']; // maintain aspect ratio $filehandler = new \ElggFile(); $filehandler->owner_guid = $entity->owner_guid; $filehandler->setFilename("groups/" . $entity->guid . $size . ".jpg"); $location = elgg_get_plugins_path() . "groups/graphics/default{$size}.gif"; if ($filehandler->open("read")) { $location = $filehandler->getFilenameOnFilestore(); } $imginfo = getimagesize($location); if ($imginfo) { $realratio = $imginfo[0] / $imginfo[1]; $img_height = $size != 'master' ? $icon_sizes[$size]['h'] : NULL; $img_width = $size != 'master' ? $icon_sizes[$size]['w'] : NULL; //set ratio greater than realratio by default in case $img_height = 0 $setratio = $realratio + 1; if (!empty($img_height)) { $setratio = $img_width / $img_height; } // set the largest dimension to "auto" if ($realratio > $setratio || empty($img_height)) { // constrain the height $img_height = NULL;
} $what = elgg_extract('what', $vars); $izap_videos = izapVideoCheck_izap_videos($guid); if ($izap_videos) { // check what is needed if ($what == 'image') { $filename = $izap_videos->imagesrc; } elseif (!isset($what) || empty($what) || $what == 'file') { $filename = $izap_videos->videofile; } // only works if there is some file name if ($filename != '') { $fileHandler = new ElggFile(); $fileHandler->owner_guid = $izap_videos->owner_guid; $fileHandler->setFilename($filename); if (file_exists($fileHandler->getFilenameOnFilestore())) { $contents = $fileHandler->grabFile(); } } if ($contents == '') { $contents = elgg_view("izap_videos/izapdesign_logo.gif"); $type = 'image/gif'; } elseif ($what == 'image') { $type = 'image/jpeg'; } elseif (!isset($what) || empty($what) || $what == 'file') { $type = 'application/x-flv'; } header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', strtotime("+10 days")), true); header("Pragma: public"); header("Cache-Control: public"); header("Content-Length: " . strlen($contents));