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; } } }
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; }
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); } } }
/** * 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; }
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(); } }
/** * {@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 setUp() { $session = \ElggSession::getMock(); _elgg_services()->setValue('session', $session); _elgg_services()->session->start(); $this->handler = _elgg_services()->serveFileHandler; $file = new \ElggFile(); $file->owner_guid = 1; $file->setFilename("foobar.txt"); $this->file = $file; }
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; }
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 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; }
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; } } } } }
/** * 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; }
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(); }
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; }
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 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; }
/** * 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(); }
/** * 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); }
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(); }
/** * 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; }
if (isset($entity->name)) { $title = $entity->name; } else { $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"
/** * Override the default entity icon for groups * * @param string $hook * @param string $type * @param string $url * @param array $params * @return string Relative URL */ function groups_set_icon_url($hook, $type, $url, $params) { /* @var ElggGroup $group */ $group = $params['entity']; $size = $params['size']; $icontime = $group->icontime; // handle missing metadata (pre 1.7 installations) 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}/{$icontime}.jpg"; } return elgg_get_simplecache_url("groups/default{$size}.gif"); }
$publication->tags = $tags; $publication->pubtype = $type; // save custom data foreach ($data as $key => $value) { $publication->{$key} = $value; } // trigger event to save other custom data elgg_trigger_event('save:data', 'publications', $publication); // files $file_contents = get_uploaded_file('attachment'); if (!empty($file_contents)) { $fh = new ElggFile(); $fh->owner_guid = $publication->getGUID(); $file_name = $_FILES['attachment']['name']; $mime = $_FILES['attachment']['type']; $fh->setFilename($file_name); if ($fh->open('write')) { $fh->write($file_contents); $fh->close(); $publication->attached_file = $file_name; $publication->attached_file_mime_type = $mime; } } $publication->save(); if ($new_entity) { elgg_create_river_item(['view' => 'river/object/publication/create', 'action_type' => 'create', 'subject_guid' => $publication->getOwnerGUID(), 'object_guid' => $publication->getGUID(), 'target_guid' => $publication->getContainerGUID(), 'access_id' => $publication->access_id]); } system_message(elgg_echo('publication:posted')); /* todo: activate add_to_river on settings */ #add_to_river('river/object/publication/update','update',$_SESSION['user']->guid,$publication->guid); forward($publication->getURL());
// Get the size $size = strtolower(get_input('size')); if (!in_array($size, array('master', 'large', 'medium', 'small', 'tiny', 'topbar'))) { $size = 'medium'; } // If user doesn't exist, return default icon if (!$user) { $url = "_graphics/icons/default/{$size}.png"; $url = elgg_normalize_url($url); forward($url); } $user_guid = $user->getGUID(); // Try and get the icon $filehandler = new ElggFile(); $filehandler->owner_guid = $user_guid; $filehandler->setFilename("profile/{$user_guid}{$size}.jpg"); $success = false; try { if ($filehandler->open("read")) { if ($contents = $filehandler->read($filehandler->getSize())) { $success = true; } } } catch (InvalidParameterException $e) { elgg_log("Unable to get avatar for user with GUID {$user_guid}", 'ERROR'); } if (!$success) { $url = "_graphics/icons/default/{$size}.png"; $url = elgg_normalize_url($url); forward($url); }
/** * Helper function to transfer the ownership of a group to a new user * * @param ElggGroup $group the group to transfer * @param ElggUser $new_owner the new owner * * @return boolean */ function group_tools_transfer_group_ownership(ElggGroup $group, ElggUser $new_owner) { $result = false; if (empty($group) || !elgg_instanceof($group, "group") || !$group->canEdit()) { return $result; } if (empty($new_owner) || !elgg_instanceof($new_owner, "user")) { return $result; } $loggedin_user = elgg_get_logged_in_user_entity(); // register plugin hook to make sure transfer can complete elgg_register_plugin_hook_handler("permissions_check", "group", "group_tools_admin_transfer_permissions_hook"); $old_owner = $group->getOwnerEntity(); // transfer ownership $group->owner_guid = $new_owner->getGUID(); $group->container_guid = $new_owner->getGUID(); // make sure user is added to the group $group->join($new_owner); if ($group->save()) { // remove existing group administrator role for new owner remove_entity_relationship($new_owner->getGUID(), "group_admin", $group->getGUID()); // check for group icon if (!empty($group->icontime)) { $prefix = "groups/" . $group->getGUID(); $sizes = array("", "tiny", "small", "medium", "large"); $ofh = new ElggFile(); $ofh->owner_guid = $old_owner->getGUID(); $nfh = new ElggFile(); $nfh->owner_guid = $group->getOwnerGUID(); foreach ($sizes as $size) { // set correct file to handle $ofh->setFilename($prefix . $size . ".jpg"); $nfh->setFilename($prefix . $size . ".jpg"); // open files $ofh->open("read"); $nfh->open("write"); // copy file $nfh->write($ofh->grabFile()); // close file $ofh->close(); $nfh->close(); // cleanup old file $ofh->delete(); } $group->icontime = time(); } // move metadata of the group to the new owner $options = array("guid" => $group->getGUID(), "limit" => false); $metadata = elgg_get_metadata($options); if (!empty($metadata)) { foreach ($metadata as $md) { if ($md->owner_guid == $old_owner->getGUID()) { $md->owner_guid = $new_owner->getGUID(); $md->save(); } } } // notify new owner if ($new_owner->getGUID() != $loggedin_user->getGUID()) { $subject = elgg_echo("group_tools:notify:transfer:subject", array($group->name)); $message = elgg_echo("group_tools:notify:transfer:message", array($new_owner->name, $loggedin_user->name, $group->name, $group->getURL())); notify_user($new_owner->getGUID(), $group->getGUID(), $subject, $message); } $result = true; } // unregister plugin hook to make sure transfer can complete elgg_unregister_plugin_hook_handler("permissions_check", "group", "group_tools_admin_transfer_permissions_hook"); return $result; }
if ($group->save()) { $forward_url = $group->getURL(); // remove existing group administrator role for new owner remove_entity_relationship($new_owner->getGUID(), "group_admin", $group->getGUID()); // check for group icon if (!empty($group->icontime)) { $prefix = "groups/" . $group->getGUID(); $sizes = array("tiny", "small", "medium", "large"); $ofh = new ElggFile(); $ofh->owner_guid = $old_owner->getGUID(); $nfh = new ElggFile(); $nfh->owner_guid = $group->getOwnerGUID(); foreach ($sizes as $size) { // set correct file to handle $ofh->setFilename($prefix . $size . ".jpg"); $nfh->setFilename($prefix . $size . ".jpg"); // open files $ofh->open("read"); $nfh->open("write"); // copy file $nfh->write($ofh->grabFile()); // close file $ofh->close(); $nfh->close(); // cleanup old file $ofh->delete(); } $group->icontime = time(); } // move metadata of the group to the new owner $options = array("guid" => $group->getGUID(), "limit" => false);