Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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 
185  public function delete() {
186  global $CONFIG;
187  if ($CONFIG->site->getGUID() == $this->guid) {
188  throw new \SecurityException('You cannot delete the current site');
189  }
190 
191  return parent::delete();
192  }
193 
205  public function disable($reason = "", $recursive = true) {
206  global $CONFIG;
207 
208  if ($CONFIG->site->getGUID() == $this->guid) {
209  throw new \SecurityException('You cannot disable the current site');
210  }
211 
212  return parent::disable($reason, $recursive);
213  }
214 
220  public function getURL() {
221  return $this->url;
222  }
223 
227  public function getDisplayName() {
228  return $this->name;
229  }
230 
234  public function setDisplayName($displayName) {
235  $this->name = $displayName;
236  }
237 
248  public function getMembers($options = array()) {
249  elgg_deprecated_notice('\ElggSite::getMembers() is deprecated. Use \ElggSite::getEntities()', 1.9);
250 
251  $defaults = array(
252  'site_guids' => ELGG_ENTITIES_ANY_VALUE,
253  'relationship' => 'member_of_site',
254  'relationship_guid' => $this->getGUID(),
255  'inverse_relationship' => true,
256  'type' => 'user',
257  );
258 
259  $options = array_merge($defaults, $options);
260 
262  }
263 
275  public function listMembers($options = array()) {
276  elgg_deprecated_notice('\ElggSite::listMembers() is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
277  $defaults = array(
278  'site_guids' => ELGG_ENTITIES_ANY_VALUE,
279  'relationship' => 'member_of_site',
280  'relationship_guid' => $this->getGUID(),
281  'inverse_relationship' => true,
282  'type' => 'user',
283  );
284 
285  $options = array_merge($defaults, $options);
286 
288  }
289 
300  public function addEntity(\ElggEntity $entity) {
301  if (elgg_instanceof($entity, 'site')) {
302  return false;
303  }
304  return add_entity_relationship($entity->guid, "member_of_site", $this->guid);
305  }
306 
314  public function removeEntity($entity) {
315  if (elgg_instanceof($entity, 'site')) {
316  return false;
317  }
318  return remove_entity_relationship($entity->guid, "member_of_site", $this->guid);
319  }
320 
332  public function getEntities(array $options = array()) {
333  $options['relationship'] = 'member_of_site';
334  $options['relationship_guid'] = $this->guid;
335  $options['inverse_relationship'] = true;
336  if (!isset($options['site_guid']) || !isset($options['site_guids'])) {
337  $options['site_guids'] = ELGG_ENTITIES_ANY_VALUE;
338  }
339 
341  }
342 
351  public function addUser($user_guid) {
352  elgg_deprecated_notice('\ElggSite::addUser() is deprecated. Use \ElggEntity::addEntity()', 1.9);
353  return add_site_user($this->getGUID(), $user_guid);
354  }
355 
364  public function removeUser($user_guid) {
365  elgg_deprecated_notice('\ElggSite::removeUser() is deprecated. Use \ElggEntity::removeEntity()', 1.9);
366  return remove_site_user($this->getGUID(), $user_guid);
367  }
368 
382  public function getObjects($subtype = "", $limit = 10, $offset = 0) {
383  elgg_deprecated_notice('\ElggSite::getObjects() is deprecated. Use \ElggSite::getEntities()', 1.9);
384  return get_site_objects($this->getGUID(), $subtype, $limit, $offset);
385  }
386 
395  public function addObject($object_guid) {
396  elgg_deprecated_notice('\ElggSite::addObject() is deprecated. Use \ElggEntity::addEntity()', 1.9);
397  return add_site_object($this->getGUID(), $object_guid);
398  }
399 
408  public function removeObject($object_guid) {
409  elgg_deprecated_notice('\ElggSite::removeObject() is deprecated. Use \ElggEntity::removeEntity()', 1.9);
410  return remove_site_object($this->getGUID(), $object_guid);
411  }
412 
416  protected function prepareObject($object) {
417  $object = parent::prepareObject($object);
418  $object->name = $this->getDisplayName();
419  $object->description = $this->description;
420  unset($object->read_access);
421  return $object;
422  }
423 
424  /*
425  * EXPORTABLE INTERFACE
426  */
427 
434  public function getExportableValues() {
435  return array_merge(parent::getExportableValues(), array(
436  'name',
437  'description',
438  'url',
439  ));
440  }
441 
448  public function getDomain() {
449  $breakdown = parse_url($this->url);
450  return $breakdown['host'];
451  }
452 
461  public function checkWalledGarden() {
462  global $CONFIG;
463 
464  // command line calls should not invoke the walled garden check
465  if (PHP_SAPI === 'cli') {
466  return;
467  }
468 
469  if ($CONFIG->walled_garden) {
470  if ($CONFIG->default_access == ACCESS_PUBLIC) {
471  $CONFIG->default_access = ACCESS_LOGGED_IN;
472  }
473  _elgg_services()->hooks->registerHandler(
474  'access:collections:write',
475  'all',
476  '_elgg_walled_garden_remove_public_access',
477  9999);
478 
479  if (!_elgg_services()->session->isLoggedIn()) {
480  // override the front page
481  elgg_register_page_handler('', '_elgg_walled_garden_index');
482 
483  if (!$this->isPublicPage()) {
484  if (!elgg_is_xhr()) {
485  _elgg_services()->session->set('last_forward_from', current_page_url());
486  }
487  register_error(_elgg_services()->translator->translate('loggedinrequired'));
488  forward('', 'walled_garden');
489  }
490  }
491  }
492  }
493 
504  public function isPublicPage($url = '') {
505  global $CONFIG;
506 
507  if (empty($url)) {
509 
510  // do not check against URL queries
511  if ($pos = strpos($url, '?')) {
512  $url = substr($url, 0, $pos);
513  }
514  }
515 
516  // always allow index page
517  if ($url == _elgg_services()->config->getSiteUrl($this->guid)) {
518  return true;
519  }
520 
521  // default public pages
522  $defaults = array(
523  'walled_garden/.*',
524  'action/.*',
525  'login',
526  'register',
527  'forgotpassword',
528  'changepassword',
529  'refresh_token',
530  'ajax/view/languages.js',
531  'upgrade\.php',
532  'css/.*',
533  'js/.*',
534  'cache/[0-9]+/\w+/.*',
535  'cron/.*',
536  'services/.*',
537  'serve-file/.*',
538  'robots.txt',
539  'favicon.ico',
540  );
541 
542  // include a hook for plugin authors to include public pages
543  $plugins = _elgg_services()->hooks->trigger('public_pages', 'walled_garden', null, array());
544 
545  // allow public pages
546  foreach (array_merge($defaults, $plugins) as $public) {
547  $pattern = "`^{$CONFIG->url}$public/*$`i";
548  if (preg_match($pattern, $url)) {
549  return true;
550  }
551  }
552 
553  // non-public page
554  return false;
555  }
556 }
static getExternalAttributes()
Get default values for attributes stored in a separate table.
Definition: ElggSite.php:51
prepareObject($object)
{}
Definition: ElggSite.php:416
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
remove_site_user($site_guid, $user_guid)
Remove a user from a site.
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:46
getMembers($options=array())
Gets an array of entities who are members of the site.
Definition: ElggSite.php:248
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:226
remove_site_object($site_guid, $object_guid)
Remove an object from a site.
__construct($row=null)
Create a new .
Definition: ElggSite.php:70
if(!$site) if(!($site instanceof ElggSite)) $site url
get_site_objects($site_guid, $subtype="", $limit=10, $offset=0)
Get the objects belonging to a site.
loadAdditionalSelectValues(array $data)
Stores non-attributes from the loading of the entity as volatile data.
removeEntity($entity)
Removes an entity from this site.
Definition: ElggSite.php:314
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
addObject($object_guid)
Adds an object to the site.
Definition: ElggSite.php:395
remove_entity_relationship($guid_one, $relationship, $guid_two)
Delete a relationship between two entities.
checkWalledGarden()
Halts bootup and redirects to the site front page if site is in walled garden mode, no user is logged in, and the URL is not a public page.
Definition: ElggSite.php:461
$CONFIG site_guid
The guid of the current site object.
Definition: config.php:65
$defaults
getObjects($subtype="", $limit=10, $offset=0)
Returns an array of entities that belong to the site.
Definition: ElggSite.php:382
removeUser($user_guid)
Removes a user from the site.
Definition: ElggSite.php:364
$value
Definition: longtext.php:26
add_site_object($site_guid, $object_guid)
Add an object to a site.
$site name
$subtype
Definition: delete.php:28
addUser($user_guid)
Adds a user to the site.
Definition: ElggSite.php:351
if(!$count) $offset
Definition: pagination.php:26
getGUID()
Returns the guid.
current_page_url()
Returns the current page's complete URL.
Definition: input.php:65
$guid
Removes an admin notice.
add_site_user($site_guid, $user_guid)
Add a user to a site.
get_site_by_url($url)
Return the site via a url.
Definition: sites.php:55
update()
{}
Definition: ElggSite.php:159
$url
Definition: exceptions.php:24
listMembers($options=array())
List the members of this site.
Definition: ElggSite.php:275
sanitize_string($string)
Sanitizes a string for use in a query.
Definition: database.php:153
$options
Elgg admin footer.
Definition: footer.php:6
register_error($error)
Display an error on next page load.
Definition: elgglib.php:452
disable($reason="", $recursive=true)
Disable the site.
Definition: ElggSite.php:205
if(!$site) if(!($site instanceof ElggSite)) $site description
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:716
setDisplayName($displayName)
{}
Definition: ElggSite.php:234
addEntity(\ElggEntity $entity)
Adds an entity to the site.
Definition: ElggSite.php:300
$limit
Definition: userpicker.php:38
if($categories) $description
Definition: full.php:166
isPublicPage($url= '')
Returns if a URL is public for this site when in Walled Garden mode.
Definition: ElggSite.php:504
$key
Definition: summary.php:34
global $CONFIG
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2001
getDisplayName()
{}
Definition: ElggSite.php:227
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1070
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
load($guid)
Loads the full when given a guid.
Definition: ElggSite.php:108
$attrs
Definition: ajax_loader.php:30
elgg_list_entities_from_relationship(array $options=array())
Returns a viewable list of entities by relationship.
getURL()
Returns the URL for this site.
Definition: ElggSite.php:220
const ACCESS_PUBLIC
Definition: elgglib.php:1990
create()
{}
Definition: ElggSite.php:128
forward($location="", $reason= 'system')
Forward to $location.
Definition: elgglib.php:93
getExportableValues()
Return an array of fields which can be exported.
Definition: ElggSite.php:434
$entity
Definition: delete.php:7
$row
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
const ACCESS_LOGGED_IN
Definition: elgglib.php:1989
$user_guid
Avatar remove action.
Definition: remove.php:6
getDomain()
Get the domain for this site.
Definition: ElggSite.php:448
removeObject($object_guid)
Remvoes an object from the site.
Definition: ElggSite.php:408
getEntities(array $options=array())
Get an array of entities that belong to the site.
Definition: ElggSite.php:332
initializeAttributes()
Initialize the attributes array.
Definition: ElggSite.php:36