/** * Remove the icon of a blog * * @param ElggBlog $blog The blog to remove the icon from * * @return bool */ function blog_tools_remove_blog_icon(ElggBlog $blog) { $result = false; if (!empty($blog) && elgg_instanceof($blog, "object", "blog", "ElggBlog")) { if (!empty($blog->icontime)) { $icon_sizes = elgg_get_config("icon_sizes"); if (!empty($icon_sizes)) { $fh = new ElggFile(); $fh->owner_guid = $blog->getOwnerGUID(); $prefix = "blogs/" . $blog->getGUID(); foreach ($icon_sizes as $name => $info) { $fh->setFilename($prefix . $name . ".jpg"); if ($fh->exists()) { $fh->delete(); } } } unset($blog->icontime); $result = true; } else { $result = true; } } return $result; }
/** * 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; }
public function delete() { $thumbnails = array($this->thumbnail, $this->smallthumb, $this->largethumb); foreach ($thumbnails as $thumbnail) { if ($thumbnail) { $delfile = new ElggFile(); $delfile->owner_guid = $this->owner_guid; $delfile->setFilename($thumbnail); $delfile->delete(); } } return parent::delete(); }
/** * 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; }
protected function initialise_attributes() { global $IZAPSETTINGS; parent::initialise_attributes(); $this->attributes['subtype'] = 'izap_videos'; $this->IZAPSETTINGS = $IZAPSETTINGS; }
/** * 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)); }
/** * (non-PHPdoc) * @see ElggObject::initializeAttributes() */ protected function initializeAttributes() { parent::initializeAttributes(); $this->attributes['subtype'] = self::SUBTYPE; $this->attributes['owner_guid'] = elgg_get_site_entity()->guid; $this->attributes['container_guid'] = elgg_get_site_entity()->guid; $this->attributes['access_id'] = ACCESS_PUBLIC; }
/** * @group FileService */ public function testSends304WithIfNoneMatchHeadersIncludedAndDeflationEnabled() { $file = new \Elgg\FileService\File(); $file->setFile($this->file); $request = $this->createRequest($file); $request->headers->set('if_none_match', '"' . $this->file->getModifiedTime() . '-gzip"'); $response = $this->handler->getResponse($request); $this->assertEquals(304, $response->getStatusCode()); }
function group_icon_url_override($hook, $type, $returnvalue, $params) { $group = $params['entity']; $size = $params['size']; $icontime = $group->icontime; if (null === $icontime) { $file = new ElggFile(); $file->owner_guid = $group->owner_guid; $file->setFilename("groups/" . $group->guid . "large.jpg"); $icontime = $file->exists() ? time() : 0; create_metadata($group->guid, 'icontime', $icontime, 'integer', $group->owner_guid, ACCESS_PUBLIC); } if ($icontime) { // return thumbnail return "groupicon/{$group->guid}/{$size}/{$group->name}.jpg"; } return "mod/groups/graphics/default{$size}.gif"; }
public function __construct($guid = null) { if ($guid && !is_object($guid)) { // Loading entities via __construct(GUID) is deprecated, so we give it the entity row and the // attribute loader will finish the job. This is necessary due to not using a custom // subtype (see above). $guid = get_entity_as_row($guid); } parent::__construct($guid); }
function blog_tools_icon_hook($hook, $entity_type, $returnvalue, $params) { if (!empty($params) && is_array($params)) { $entity = $params["entity"]; if (elgg_instanceof($entity, "object", "blog")) { $size = $params["size"]; if ($icontime = $entity->icontime) { $icontime = "{$icontime}"; $filehandler = new ElggFile(); $filehandler->owner_guid = $entity->getOwnerGUID(); $filehandler->setFilename("blogs/" . $entity->getGUID() . $size . ".jpg"); if ($filehandler->exists()) { $url = elgg_get_site_url() . "blogicon/{$entity->getGUID()}/{$size}/{$icontime}.jpg"; return $url; } } } } }
public function migrateJsonSteps() { $fh = new \ElggFile(); $fh->owner_guid = $this->getGUID(); $fh->setFilename('steps.json'); if (!$fh->exists()) { return false; } $steps = $fh->grabFile(); $steps = @json_decode($steps, true); foreach ($steps as $step) { $new_step = new WizardStep(); $new_step->container_guid = $this->getGUID(); $new_step->description = $step; $new_step->save(); } $fh->delete(); return true; }
/** * Get the steps from disk * * @param bool $count get the count of the steps * * @return false|string[]|int */ public function getSteps($count = false) { $count = (bool) $count; $fh = new ElggFile(); $fh->owner_guid = $this->getGUID(); $fh->setFilename('steps.json'); if (!$fh->exists()) { return false; } $steps = $fh->grabFile(); unset($fh); $steps = @json_decode($steps, true); if ($count) { return count($steps); } // reset indexing on steps $steps = array_values($steps); return $steps; }
/** * Removes the thumbnail * * @return void */ public function removeThumbnail() { if (empty($this->icontime)) { return; } $fh = new \ElggFile(); $fh->owner_guid = $this->getGUID(); $prefix = 'thumb'; $icon_sizes = elgg_get_config('icon_sizes'); if (empty($icon_sizes)) { return; } foreach ($icon_sizes as $size => $info) { $fh->setFilename($prefix . $size . '.jpg'); if ($fh->exists()) { $fh->delete(); } } unset($this->icontime); }
/** * Downloads the thumbnail and saves into data folder * * @param ElggObject $item * @return bool */ function videolist_2012022501($item) { // do not upgrade videos that have already been upgraded if ($item->thumbnail === true) { return true; } $thumbnail = file_get_contents($item->thumbnail); if (!$thumbnail) { return false; } $prefix = "videolist/" . $item->guid; $filehandler = new ElggFile(); $filehandler->owner_guid = $item->owner_guid; $filehandler->setFilename($prefix . ".jpg"); $filehandler->open("write"); $filehandler->write($thumbnail); $filehandler->close(); $item->thumbnail = true; return true; }
/** * Set the folder for the file * * @param \ElggFile $entity the file to edit * * @return void */ protected static function setFolderGUID(\ElggFile $entity) { $folder_guid = get_input('folder_guid', false); if ($folder_guid === false) { // folder_input was not present in form/action // maybe someone else did something with a file return; } $folder_guid = (int) $folder_guid; if (!empty($folder_guid)) { $folder = get_entity($folder_guid); if (!elgg_instanceof($folder, 'object', FILE_TOOLS_SUBTYPE)) { unset($folder_guid); } } // remove old relationships remove_entity_relationships($entity->getGUID(), FILE_TOOLS_RELATIONSHIP, true); if (!empty($folder_guid)) { add_entity_relationship($folder_guid, FILE_TOOLS_RELATIONSHIP, $entity->getGUID()); } }
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); } } }
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(); } }
/** * Downloads the thumbnail and saves into data folder * * @param ElggObject $item * @return bool */ function videolist_2012022501($item) { require_once elgg_get_plugins_path() . 'upgrade-tools/lib/upgrade_tools.php'; // get thumbnail image $thumbnail = file_get_contents($item->thumbnail); if (!$thumbnail) { return false; } $prefix = "videolist/" . $item->guid; $filehandler = new ElggFile(); $filehandler->owner_guid = $item->owner_guid; $filehandler->setFilename($prefix . ".jpg"); $filehandler->open("write"); $filehandler->write($thumbnail); $filehandler->close(); // update properties if ($item->url) { $item->video_url = $item->url; $item->deleteMetadata('url'); } if ($item->desc) { $item->description = $item->desc; $item->deleteMetadata('desc'); $item->save(); } if ($item->embedurl) { $item->deleteMetadata('embedurl'); } upgrade_change_subtype($item, 'videolist_item'); // update river $options = array('object_guid' => $item->guid); $river_items = elgg_get_river($options); foreach ($river_items as $river_item) { if ($river_item->action_type == 'create') { upgrade_update_river($river_item->id, 'river/object/videolist_item/create', $item->guid, 0); } } return true; }
public function delete() { $icon_sizes = hj_framework_get_thumb_sizes($this->getSubtype()); $prefix_old = "hjfile/{$this->container_guid}/{$this->guid}"; $prefix_old_alt = "hjfile/{$this->guid}"; $prefix = "icons/{$this->guid}"; foreach ($icon_sizes as $size => $values) { $thumb = new ElggFile(); $thumb->owner_guid = elgg_get_logged_in_user_guid(); $thumb->setFilename("{$prefix}{$size}.jpg"); $thumb->delete(); $thumb = new ElggFile(); $thumb->owner_guid = elgg_get_logged_in_user_guid(); $thumb->setFilename("{$prefix_old}{$size}.jpg"); $thumb->delete(); $thumb = new ElggFile(); $thumb->owner_guid = elgg_get_logged_in_user_guid(); $thumb->setFilename("{$prefix_old_alt}{$size}.jpg"); $thumb->delete(); } return parent::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; }
/** * @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('"' . filemtime($this->file->getFilenameOnFilestore()) . '"', $response->headers->get('Etag')); }
/** * Listen to upgrade event * * @param string $event the name of the event * @param string $type the type of the event * @param mixed $object supplied params * * @return void */ public static function migrateSteps($event, $type, $object) { $path = 'admin/upgrades/migrate_wizard_steps'; $upgrade = new \ElggUpgrade(); // ignore acces while checking for existence $ia = elgg_set_ignore_access(true); // already registered? if ($upgrade->getUpgradeFromPath($path)) { // restore access elgg_set_ignore_access($ia); return; } // find if upgrade is needed $upgrade_needed = false; $batch = new \ElggBatch('elgg_get_entities', ['type' => 'object', 'subtype' => \Wizard::SUBTYPE, 'limit' => false]); foreach ($batch as $entity) { $fa = new \ElggFile(); $fa->owner_guid = $entity->getGUID(); $fa->setFilename('steps.json'); if (!$fa->exists()) { continue; } $upgrade_needed = true; break; } if (!$upgrade_needed) { // restore access elgg_set_ignore_access($ia); return; } $upgrade->title = elgg_echo('admin:upgrades:migrate_wizard_steps'); $upgrade->description = elgg_echo('admin:upgrades:migrate_wizard_steps:description'); $upgrade->setPath($path); $upgrade->save(); // restore access elgg_set_ignore_access($ia); }
/** * Remove the icon of a blog * * @param ElggBlog $blog The blog to remove the icon from * * @return bool */ function blog_tools_remove_blog_icon(ElggBlog $blog) { if (!$blog instanceof ElggBlog) { return false; } if (empty($blog->icontime)) { // no icon return true; } $icon_sizes = elgg_get_icon_sizes('object', 'blog'); if (!empty($icon_sizes)) { $fh = new ElggFile(); $fh->owner_guid = $blog->getOwnerGUID(); $prefix = "blogs/{$blog->getGUID()}"; foreach ($icon_sizes as $name => $info) { $fh->setFilename("{$prefix}{$name}.jpg"); if ($fh->exists()) { $fh->delete(); } } } unset($blog->icontime); 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; }
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(); }
/** * Override ElggFile::delete() * * After deleting the file delete also the directory. * * @return bool */ public function delete() { $fs = $this->getFilestore(); $dir = $this->getFileDirectory(); // Delete the file on disc if ($fs->delete($this)) { // Delete the ElggFile entity if (parent::delete()) { // Delete the directory if (is_dir($dir)) { if (rmdir($dir)) { return true; } else { elgg_add_admin_notice('video_dir_delete_failed', elgg_echo('video:dir_delete_failed', $dir)); } } } } return false; }
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)); }
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(); }