Loads and returns an entity object from a guid.
public get ( integer $guid, string $type = '' ) : ElggEntit\ElggEntity | stdClas\stdClass | false | ||
$guid | integer | The GUID of the entity |
$type | string | The type of the entity. If given, even an existing entity with the given GUID will not be returned unless its type matches. |
Résultat | ElggEntit\ElggEntity | stdClas\stdClass | false | The correct Elgg or custom object based upon entity type and subtype |
/** * Can a user edit this entity? * * @tip Can be overridden by registering for the permissions_check plugin hook. * * @param ElggEntity $entity Object entity * @param int $user_guid The user GUID, optionally (default: logged in user) * * @return bool Whether this entity is editable by the given user. * @see elgg_set_ignore_access() */ public function canEdit(ElggEntity $entity, $user_guid = 0) { try { $user = $this->entities->getUserForPermissionsCheck($user_guid); } catch (UserFetchFailureException $e) { return false; } // Test user if possible - should default to false unless a plugin hook says otherwise $default = call_user_func(function () use($entity, $user) { if (!$user) { return false; } // favor the persisted attributes if not saved $attrs = array_merge(['owner_guid' => $entity->owner_guid, 'container_guid' => $entity->container_guid], $entity->getOriginalAttributes()); if ($attrs['owner_guid'] == $user->guid) { return true; } if ($attrs['container_guid'] == $user->guid) { return true; } if ($entity->guid == $user->guid) { return true; } $container = $this->entities->get($attrs['container_guid']); return $container && $container->canEdit($user->guid); }); $params = array('entity' => $entity, 'user' => $user); return $this->hooks->trigger('permissions_check', $entity->getType(), $params, $default); }
/** * Deletes all private settings for an entity * * @param int $entity_guid The Entity GUID * @return bool */ function removeAllForEntity($entity_guid) { $entity_guid = (int) $entity_guid; $entity = $this->entities->get($entity_guid); if (!$entity instanceof \ElggEntity) { return false; } return $this->db->deleteData("DELETE FROM {$this->table}\n\t\t\tWHERE entity_guid = {$entity_guid}"); }
/** * Send a notification to a subscriber * * @param \Elgg\Notifications\Event $event The notification event * @param int $guid The guid of the subscriber * @param string $method The notification method * @return bool * @access private */ protected function sendNotification(\Elgg\Notifications\Event $event, $guid, $method) { $recipient = $this->entities->get($guid, 'user'); /* @var \ElggUser $recipient */ if (!$recipient || $recipient->isBanned()) { return false; } // don't notify the creator of the content if ($recipient->getGUID() == $event->getActorGUID()) { return false; } $actor = $event->getActor(); $object = $event->getObject(); if (!$actor || !$object) { return false; } if ($object instanceof ElggEntity && !has_access_to_entity($object, $recipient)) { return false; } $language = $recipient->language; $params = array('event' => $event, 'method' => $method, 'recipient' => $recipient, 'language' => $language, 'object' => $object); $subject = $this->getNotificationSubject($event, $recipient); $body = $this->getNotificationBody($event, $recipient); $notification = new \Elgg\Notifications\Notification($event->getActor(), $recipient, $language, $subject, $body, '', $params); $type = 'notification:' . $event->getDescription(); if ($this->hooks->hasHandler('prepare', $type)) { $notification = $this->hooks->trigger('prepare', $type, $params, $notification); } else { // pre Elgg 1.9 notification message generation $notification = $this->getDeprecatedNotificationBody($notification, $event, $method); } if ($this->hooks->hasHandler('send', "notification:{$method}")) { // return true to indicate the notification has been sent $params = array('notification' => $notification, 'event' => $event); return $this->hooks->trigger('send', "notification:{$method}", $params, false); } else { // pre Elgg 1.9 notification handler $userGuid = $notification->getRecipientGUID(); $senderGuid = $notification->getSenderGUID(); $subject = $notification->subject; $body = $notification->body; $params = $notification->params; return (bool) _elgg_notify_user($userGuid, $senderGuid, $subject, $body, $params, array($method)); } }
/** * Handle request to /serve-icon handler * * @param bool $allow_removing_headers Alter PHP's global headers to allow caching * @return BinaryFileResponse */ public function handleServeIconRequest($allow_removing_headers = true) { $response = new Response(); $response->setExpires($this->getCurrentTime('-1 day')); $response->prepare($this->request); if ($allow_removing_headers) { // clear cache-boosting headers set by PHP session header_remove('Cache-Control'); header_remove('Pragma'); header_remove('Expires'); } $path = implode('/', $this->request->getUrlSegments()); if (!preg_match('~serve-icon/(\\d+)/(.*+)$~', $path, $m)) { return $response->setStatusCode(400)->setContent('Malformatted request URL'); } list(, $guid, $size) = $m; $entity = $this->entities->get($guid); if (!$entity instanceof \ElggEntity) { return $response->setStatusCode(404)->setContent('Item does not exist'); } $thumbnail = $entity->getIcon($size); if (!$thumbnail->exists()) { return $response->setStatusCode(404)->setContent('Icon does not exist'); } $if_none_match = $this->request->headers->get('if_none_match'); if (!empty($if_none_match)) { // strip mod_deflate suffixes $this->request->headers->set('if_none_match', str_replace('-gzip', '', $if_none_match)); } $filenameonfilestore = $thumbnail->getFilenameOnFilestore(); $last_updated = filemtime($filenameonfilestore); $etag = '"' . $last_updated . '"'; $response->setPrivate()->setEtag($etag)->setExpires($this->getCurrentTime('+1 day'))->setMaxAge(86400); if ($response->isNotModified($this->request)) { return $response; } $headers = ['Content-Type' => (new MimeTypeDetector())->getType($filenameonfilestore)]; $response = new BinaryFileResponse($filenameonfilestore, 200, $headers, false, 'inline'); $response->prepare($this->request); $response->setPrivate()->setEtag($etag)->setExpires($this->getCurrentTime('+1 day'))->setMaxAge(86400); return $response; }
/** * Send a notification to a subscriber * * @param NotificationEvent $event The notification event * @param int $guid The guid of the subscriber * @param string $method The notification method * @param array $params Default notification params * @return bool * @access private */ protected function sendNotification(NotificationEvent $event, $guid, $method, array $params = []) { $actor = $event->getActor(); $object = $event->getObject(); if ($event instanceof InstantNotificationEvent) { $recipient = $this->entities->get($guid); /* @var \ElggEntity $recipient */ $subject = elgg_extract('subject', $params, ''); $body = elgg_extract('body', $params, ''); $summary = elgg_extract('summary', $params, ''); } else { $recipient = $this->entities->get($guid, 'user'); /* @var \ElggUser $recipient */ if (!$recipient || $recipient->isBanned()) { return false; } if ($recipient->getGUID() == $event->getActorGUID()) { // Content creators should not be receiving subscription // notifications about their own content return false; } if (!$actor || !$object) { return false; } if ($object instanceof ElggEntity && !has_access_to_entity($object, $recipient)) { // Recipient does not have access to the notification object // The access level may have changed since the event was enqueued return false; } $subject = $this->getNotificationSubject($event, $recipient); $body = $this->getNotificationBody($event, $recipient); $summary = ''; $params['origin'] = Notification::ORIGIN_SUBSCRIPTIONS; } $language = $recipient->language; $params['event'] = $event; $params['method'] = $method; $params['sender'] = $actor; $params['recipient'] = $recipient; $params['language'] = $language; $params['object'] = $object; $params['action'] = $event->getAction(); $notification = new Notification($actor, $recipient, $language, $subject, $body, $summary, $params); $notification = $this->hooks->trigger('prepare', 'notification', $params, $notification); if (!$notification instanceof Notification) { throw new RuntimeException("'prepare','notification' hook must return an instance of " . Notification::class); } $type = 'notification:' . $event->getDescription(); if ($this->hooks->hasHandler('prepare', $type)) { $notification = $this->hooks->trigger('prepare', $type, $params, $notification); if (!$notification instanceof Notification) { throw new RuntimeException("'prepare','{$type}' hook must return an instance of " . Notification::class); } } else { // pre Elgg 1.9 notification message generation $notification = $this->getDeprecatedNotificationBody($notification, $event, $method); } $notification = $this->hooks->trigger('format', "notification:{$method}", [], $notification); if (!$notification instanceof Notification) { throw new RuntimeException("'format','notification:{$method}' hook must return an instance of " . Notification::class); } if ($this->hooks->hasHandler('send', "notification:{$method}")) { // return true to indicate the notification has been sent $params = array('notification' => $notification, 'event' => $event); $result = $this->hooks->trigger('send', "notification:{$method}", $params, false); if ($this->logger->getLevel() == Logger::INFO) { $logger_data = print_r((array) $notification->toObject(), true); if ($result) { $this->logger->info("Notification sent: " . $logger_data); } else { $this->logger->info("Notification was not sent: " . $logger_data); } } return $result; } else { // pre Elgg 1.9 notification handler $userGuid = $notification->getRecipientGUID(); $senderGuid = $notification->getSenderGUID(); $subject = $notification->subject; $body = $notification->body; $params = $notification->params; return (bool) _elgg_notify_user($userGuid, $senderGuid, $subject, $body, $params, array($method)); } }
/** * Populate the boot data * * @param \stdClass $config Elgg CONFIG object * @param \Elgg\Database $db Elgg database * @param EntityTable $entities Entities service * @param Plugins $plugins Plugins service * * @return void * @throws \InstallationException */ public function populate(\stdClass $config, Database $db, EntityTable $entities, Plugins $plugins) { // get subtypes $rows = $db->getData("\n\t\t\tSELECT *\n\t\t\tFROM {$db->prefix}entity_subtypes\n\t\t"); foreach ($rows as $row) { $this->subtype_data[$row->id] = $row; } // get config $rows = $db->getData("\n\t\t\tSELECT *\n\t\t\tFROM {$db->prefix}config\n\t\t"); foreach ($rows as $row) { $this->config_values[$row->name] = unserialize($row->value); } if (!array_key_exists('installed', $this->config_values)) { // try to fetch from old pre 3.0 datalists table // need to do this to be able to perform an upgrade from 2.x to 3.0 try { $rows = $db->getData("\n\t\t\t\t\tSELECT *\n\t\t\t\t\tFROM {$db->prefix}datalists\n\t\t\t\t"); foreach ($rows as $row) { $value = $row->value; if ($row->name == 'processed_upgrades') { // config table already serializes data so no need to double serialize $value = unserialize($value); } $this->config_values[$row->name] = $value; } } catch (\Exception $e) { } } // get site entity $this->site = $entities->get($this->config_values['default_site'], 'site'); if (!$this->site) { throw new \InstallationException("Unable to handle this request. This site is not configured or the database is down."); } // get plugins $this->active_plugins = $plugins->find('active'); // get plugin settings if (!$this->active_plugins) { return; } // find GUIDs with not too many private settings $guids = array_map(function (\ElggPlugin $plugin) { return $plugin->guid; }, $this->active_plugins); // find plugin GUIDs with not too many settings $limit = 40; $set = implode(',', $guids); $sql = "\n\t\t\tSELECT entity_guid\n\t\t\tFROM {$db->prefix}private_settings\n\t\t\tWHERE entity_guid IN ({$set})\n\t\t\t AND name NOT LIKE 'plugin:user_setting:%'\n\t\t\t AND name NOT LIKE 'elgg:internal:%'\n\t\t\tGROUP BY entity_guid\n\t\t\tHAVING COUNT(*) > {$limit}\n\t\t"; $unsuitable_guids = $db->getData($sql, function ($row) { return (int) $row->entity_guid; }); $guids = array_values($guids); $guids = array_diff($guids, $unsuitable_guids); if ($guids) { // get the settings $set = implode(',', $guids); $rows = $db->getData("\n\t\t\t\tSELECT entity_guid, `name`, `value`\n\t\t\t\tFROM {$db->prefix}private_settings\n\t\t\t\tWHERE entity_guid IN ({$set})\n\t\t\t\t AND name NOT LIKE 'plugin:user_setting:%'\n\t\t\t\t AND name NOT LIKE 'elgg:internal:%'\n\t\t\t\tORDER BY entity_guid\n\t\t\t"); // make sure we show all entities as loaded $this->plugin_settings = array_fill_keys($guids, []); foreach ($rows as $i => $row) { $this->plugin_settings[$row->entity_guid][$row->name] = $row->value; } } }