Elgg  Version 2.3
ElggSite.php
Go to the documentation of this file.
1 <?php
28 class ElggSite extends \ElggEntity {
29 
36  protected function initializeAttributes() {
37  parent::initializeAttributes();
38 
39  $this->attributes['type'] = "site";
40  $this->attributes += self::getExternalAttributes();
41  }
42 
51  final public static function getExternalAttributes() {
52  return [
53  'name' => null,
54  'description' => null,
55  'url' => null,
56  ];
57  }
58 
70  public function __construct($row = null) {
71  $this->initializeAttributes();
72 
73  if (!empty($row)) {
74  // Is $row is a DB entity table row
75  if ($row instanceof \stdClass) {
76  // Load the rest
77  if (!$this->load($row)) {
78  $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
79  throw new \IOException($msg);
80  }
81  } else if (strpos($row, "http") !== false) {
82  // url so retrieve by url
83  elgg_deprecated_notice("Passing URL to constructor is deprecated. Use get_site_by_url()", 1.9);
85  foreach ($row->attributes as $key => $value) {
86  $this->attributes[$key] = $value;
87  }
88  } else if (is_numeric($row)) {
89  // $row is a GUID so load
90  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
91  if (!$this->load($row)) {
92  throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
93  }
94  } else {
95  throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
96  }
97  }
98  }
99 
108  protected function load($guid) {
109  $attr_loader = new \Elgg\AttributeLoader(get_class(), 'site', $this->attributes);
110  $attr_loader->requires_access_control = !($this instanceof \ElggPlugin);
111  $attr_loader->secondary_loader = 'get_site_entity_as_row';
112 
113  $attrs = $attr_loader->getRequiredAttributes($guid);
114  if (!$attrs) {
115  return false;
116  }
117 
118  $this->attributes = $attrs;
119  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
120  _elgg_services()->entityCache->set($this);
121 
122  return true;
123  }
124 
128  protected function create() {
129  global $CONFIG;
130 
131  $guid = parent::create();
132 
133  $name = sanitize_string($this->attributes['name']);
134  $description = sanitize_string($this->attributes['description']);
135  $url = sanitize_string($this->attributes['url']);
136 
137  $query = "INSERT into {$CONFIG->dbprefix}sites_entity
138  (guid, name, description, url) values ($guid, '$name', '$description', '$url')";
139 
140  $result = $this->getDatabase()->insertData($query);
141  if ($result === false) {
142  // TODO(evan): Throw an exception here?
143  return false;
144  }
145 
146  // make sure the site guid is set to self if not already set
147  if (!$this->site_guid) {
148  $this->site_guid = $guid;
149  $this->getDatabase()->updateData("UPDATE {$CONFIG->dbprefix}entities
150  SET site_guid = $guid WHERE guid = $guid");
151  }
152 
153  return $guid;
154  }
155 
159  protected function update() {
160  global $CONFIG;
161 
162  if (!parent::update()) {
163  return false;
164  }
165 
166  $guid = (int)$this->guid;
167  $name = sanitize_string($this->name);
169  $url = sanitize_string($this->url);
170 
171  $query = "UPDATE {$CONFIG->dbprefix}sites_entity
172  SET name='$name', description='$description', url='$url' WHERE guid=$guid";
173 
174  return $this->getDatabase()->updateData($query) !== false;
175  }
176 
187  public function delete($recursive = true) {
188  global $CONFIG;
189  if ($CONFIG->site->getGUID() == $this->guid) {
190  throw new \SecurityException('You cannot delete the current site');
191  }
192 
193  return parent::delete($recursive);
194  }
195 
207  public function disable($reason = "", $recursive = true) {
208  global $CONFIG;
209 
210  if ($CONFIG->site->getGUID() == $this->guid) {
211  throw new \SecurityException('You cannot disable the current site');
212  }
213 
214  return parent::disable($reason, $recursive);
215  }
216 
222  public function getURL() {
223  return $this->url;
224  }
225 
229  public function getDisplayName() {
230  return $this->name;
231  }
232 
236  public function setDisplayName($displayName) {
237  $this->name = $displayName;
238  }
239 
250  public function getMembers($options = array()) {
251  elgg_deprecated_notice('\ElggSite::getMembers() is deprecated. Use \ElggSite::getEntities()', 1.9);
252 
253  $defaults = array(
254  'site_guids' => ELGG_ENTITIES_ANY_VALUE,
255  'relationship' => 'member_of_site',
256  'relationship_guid' => $this->getGUID(),
257  'inverse_relationship' => true,
258  'type' => 'user',
259  );
260 
261  $options = array_merge($defaults, $options);
262 
264  }
265 
277  public function listMembers($options = array()) {
278  elgg_deprecated_notice('\ElggSite::listMembers() is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
279  $defaults = array(
280  'site_guids' => ELGG_ENTITIES_ANY_VALUE,
281  'relationship' => 'member_of_site',
282  'relationship_guid' => $this->getGUID(),
283  'inverse_relationship' => true,
284  'type' => 'user',
285  );
286 
287  $options = array_merge($defaults, $options);
288 
290  }
291 
302  public function addEntity(\ElggEntity $entity) {
303  if (elgg_instanceof($entity, 'site')) {
304  return false;
305  }
306  return add_entity_relationship($entity->guid, "member_of_site", $this->guid);
307  }
308 
316  public function removeEntity($entity) {
317  if (elgg_instanceof($entity, 'site')) {
318  return false;
319  }
320  return remove_entity_relationship($entity->guid, "member_of_site", $this->guid);
321  }
322 
334  public function getEntities(array $options = array()) {
335  $options['relationship'] = 'member_of_site';
336  $options['relationship_guid'] = $this->guid;
337  $options['inverse_relationship'] = true;
338  if (!isset($options['site_guid']) || !isset($options['site_guids'])) {
339  $options['site_guids'] = ELGG_ENTITIES_ANY_VALUE;
340  }
341 
343  }
344 
353  public function addUser($user_guid) {
354  elgg_deprecated_notice('\ElggSite::addUser() is deprecated. Use \ElggEntity::addEntity()', 1.9);
355  return add_site_user($this->getGUID(), $user_guid);
356  }
357 
366  public function removeUser($user_guid) {
367  elgg_deprecated_notice('\ElggSite::removeUser() is deprecated. Use \ElggEntity::removeEntity()', 1.9);
368  return remove_site_user($this->getGUID(), $user_guid);
369  }
370 
384  public function getObjects($subtype = "", $limit = 10, $offset = 0) {
385  elgg_deprecated_notice('\ElggSite::getObjects() is deprecated. Use \ElggSite::getEntities()', 1.9);
386  return get_site_objects($this->getGUID(), $subtype, $limit, $offset);
387  }
388 
397  public function addObject($object_guid) {
398  elgg_deprecated_notice('\ElggSite::addObject() is deprecated. Use \ElggEntity::addEntity()', 1.9);
399  return add_site_object($this->getGUID(), $object_guid);
400  }
401 
410  public function removeObject($object_guid) {
411  elgg_deprecated_notice('\ElggSite::removeObject() is deprecated. Use \ElggEntity::removeEntity()', 1.9);
412  return remove_site_object($this->getGUID(), $object_guid);
413  }
414 
418  protected function prepareObject($object) {
419  $object = parent::prepareObject($object);
420  $object->name = $this->getDisplayName();
421  $object->description = $this->description;
422  unset($object->read_access);
423  return $object;
424  }
425 
426  /*
427  * EXPORTABLE INTERFACE
428  */
429 
436  public function getExportableValues() {
437  return array_merge(parent::getExportableValues(), array(
438  'name',
439  'description',
440  'url',
441  ));
442  }
443 
450  public function getDomain() {
451  $breakdown = parse_url($this->url);
452  return $breakdown['host'];
453  }
454 
463  public function checkWalledGarden() {
464  // command line calls should not invoke the walled garden check
465  if (PHP_SAPI === 'cli') {
466  return;
467  }
468 
469  if (!elgg_get_config('walled_garden')) {
470  return;
471  }
472 
473  if (elgg_get_config('default_access') == ACCESS_PUBLIC) {
474  elgg_set_config('default_access', ACCESS_LOGGED_IN);
475  }
476 
477  if (!_elgg_services()->session->isLoggedIn()) {
478  // override the front page
479  elgg_register_page_handler('', '_elgg_walled_garden_index');
480 
481  if (!$this->isPublicPage()) {
482  _elgg_services()->redirects->setLastForwardFrom();
483  register_error(_elgg_services()->translator->translate('loggedinrequired'));
484  forward('', 'walled_garden');
485  }
486  }
487  }
488 
499  public function isPublicPage($url = '') {
500  global $CONFIG;
501 
502  if (empty($url)) {
504 
505  // do not check against URL queries
506  if ($pos = strpos($url, '?')) {
507  $url = substr($url, 0, $pos);
508  }
509  }
510 
511  // always allow index page
512  if ($url == _elgg_services()->config->getSiteUrl($this->guid)) {
513  return true;
514  }
515 
516  // default public pages
517  $defaults = array(
518  'walled_garden/.*',
519  'action/.*',
520  'login',
521  'register',
522  'forgotpassword',
523  'changepassword',
524  'refresh_token',
525  'ajax/view/languages.js',
526  'upgrade\.php',
527  'css/.*',
528  'js/.*',
529  'cache/[0-9]+/\w+/.*',
530  'cron/.*',
531  'services/.*',
532  'serve-file/.*',
533  'robots.txt',
534  'favicon.ico',
535  'manifest.json',
536  );
537 
538  // include a hook for plugin authors to include public pages
539  $plugins = _elgg_services()->hooks->trigger('public_pages', 'walled_garden', null, array());
540 
541  // allow public pages
542  foreach (array_merge($defaults, $plugins) as $public) {
543  $pattern = "`^{$CONFIG->url}$public/*$`i";
544  if (preg_match($pattern, $url)) {
545  return true;
546  }
547  }
548 
549  // non-public page
550  return false;
551  }
552 }
if(! $site) if(!($site instanceof ElggSite)) $site url
if(! $site) if(!($site instanceof ElggSite)) $site description
$site name
$options
Elgg admin footer.
Definition: footer.php:6
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
elgg button delete
Definition: admin.css.php:600
$attrs
Definition: ajax_loader.php:30
$defaults
$user_guid
Avatar remove action.
Definition: remove.php:6
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:48
loadAdditionalSelectValues(array $data)
Stores non-attributes from the loading of the entity as volatile data.
getGUID()
Returns the guid.
getExportableValues()
Return an array of fields which can be exported.
Definition: ElggSite.php:436
getMembers($options=array())
Gets an array of \ElggUser entities who are members of the site.
Definition: ElggSite.php:250
isPublicPage($url='')
Returns if a URL is public for this site when in Walled Garden mode.
Definition: ElggSite.php:499
getEntities(array $options=array())
Get an array of entities that belong to the site.
Definition: ElggSite.php:334
static getExternalAttributes()
Get default values for attributes stored in a separate table.
Definition: ElggSite.php:51
listMembers($options=array())
List the members of this site.
Definition: ElggSite.php:277
removeEntity($entity)
Removes an entity from this site.
Definition: ElggSite.php:316
create()
{Create a new entry in the entities table.Saves the base information in the entities table for the en...
Definition: ElggSite.php:128
__construct($row=null)
Create a new \ElggSite.
Definition: ElggSite.php:70
removeObject($object_guid)
Remvoes an object from the site.
Definition: ElggSite.php:410
prepareObject($object)
{Prepare an object copy for toObject()Object representation of the entity \stdClass}
Definition: ElggSite.php:418
disable($reason="", $recursive=true)
Disable the site.
Definition: ElggSite.php:207
addUser($user_guid)
Adds a user to the site.
Definition: ElggSite.php:353
getURL()
Returns the URL for this site.
Definition: ElggSite.php:222
update()
{Update the entity in the database.bool Whether the update was successful.}
Definition: ElggSite.php:159
addEntity(\ElggEntity $entity)
Adds an entity to the site.
Definition: ElggSite.php:302
addObject($object_guid)
Adds an object to the site.
Definition: ElggSite.php:397
getObjects($subtype="", $limit=10, $offset=0)
Returns an array of \ElggObject entities that belong to the site.
Definition: ElggSite.php:384
initializeAttributes()
Initialize the attributes array.
Definition: ElggSite.php:36
getDomain()
Get the domain for this site.
Definition: ElggSite.php:450
checkWalledGarden()
Halts bootup and redirects to the site front page if site is in walled garden mode,...
Definition: ElggSite.php:463
setDisplayName($displayName)
{Sets the title or name of this entity.The title or name of this entity. void}
Definition: ElggSite.php:236
removeUser($user_guid)
Removes a user from the site.
Definition: ElggSite.php:366
getDisplayName()
{Get the entity's display name.string The title or name of this entity.}
Definition: ElggSite.php:229
load($guid)
Loads the full \ElggSite when given a guid.
Definition: ElggSite.php:108
$CONFIG site_guid
The guid of the current site object.
Definition: config.php:65
elgg_set_config($name, $value)
Set an Elgg configuration value.
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$guid
Removes an admin notice.
$row
remove_site_user($site_guid, $user_guid)
Remove a user from a site.
remove_site_object($site_guid, $object_guid)
Remove an object from a site.
add_site_object($site_guid, $object_guid)
Add an object to a site.
add_site_user($site_guid, $user_guid)
Add a user to a site.
get_site_objects($site_guid, $subtype="", $limit=10, $offset=0)
Get the objects belonging to a site.
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2095
register_error($error)
Display an error on next page load.
Definition: elgglib.php:464
const ACCESS_LOGGED_IN
Definition: elgglib.php:2083
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
const ACCESS_PUBLIC
Definition: elgglib.php:2084
forward($location="", $reason='system')
Forward to $location.
Definition: elgglib.php:94
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
sanitize_string($string)
Sanitizes a string for use in a query.
Definition: database.php:153
current_page_url()
Returns the current page's complete URL.
Definition: input.php:65
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an \ElggEntity and optionally for type and subtype.
Definition: entities.php:736
$subtype
Definition: delete.php:28
$entity
Definition: delete.php:7
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
$url
Definition: exceptions.php:24
$value
Definition: longtext.php:42
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
if(! $count) $offset
Definition: pagination.php:26
if($categories) $description
Definition: full.php:176
elgg_list_entities_from_relationship(array $options=array())
Returns a viewable list of entities by relationship.
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
remove_entity_relationship($guid_one, $relationship, $guid_two)
Delete a relationship between two entities.
$key
Definition: summary.php:34
global $CONFIG
get_site_by_url($url)
Return the site via a url.
Definition: sites.php:55
$limit
Definition: userpicker.php:38