Elgg  Version 1.11
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  $this->tables_split = 2;
42  }
43 
52  final public static function getExternalAttributes() {
53  return [
54  'name' => null,
55  'description' => null,
56  'url' => null,
57  ];
58  }
59 
71  public function __construct($row = null) {
72  $this->initializeAttributes();
73 
74  // compatibility for 1.7 api.
75  $this->initialise_attributes(false);
76 
77  if (!empty($row)) {
78  // Is $row is a DB entity table row
79  if ($row instanceof \stdClass) {
80  // Load the rest
81  if (!$this->load($row)) {
82  $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
83  throw new \IOException($msg);
84  }
85  } else if ($row instanceof \ElggSite) {
86  // $row is an \ElggSite so this is a copy constructor
87  elgg_deprecated_notice('This type of usage of the \ElggSite constructor was deprecated. Please use the clone method.', 1.7);
88  foreach ($row->attributes as $key => $value) {
89  $this->attributes[$key] = $value;
90  }
91  } else if (strpos($row, "http") !== false) {
92  // url so retrieve by url
93  elgg_deprecated_notice("Passing URL to constructor is deprecated. Use get_site_by_url()", 1.9);
95  foreach ($row->attributes as $key => $value) {
96  $this->attributes[$key] = $value;
97  }
98  } else if (is_numeric($row)) {
99  // $row is a GUID so load
100  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
101  if (!$this->load($row)) {
102  throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
103  }
104  } else {
105  throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
106  }
107  }
108  }
109 
118  protected function load($guid) {
119  $attr_loader = new \Elgg\AttributeLoader(get_class(), 'site', $this->attributes);
120  $attr_loader->requires_access_control = !($this instanceof \ElggPlugin);
121  $attr_loader->secondary_loader = 'get_site_entity_as_row';
122 
123  $attrs = $attr_loader->getRequiredAttributes($guid);
124  if (!$attrs) {
125  return false;
126  }
127 
128  $this->attributes = $attrs;
129  $this->tables_loaded = 2;
130  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
131  _elgg_cache_entity($this);
132 
133  return true;
134  }
135 
139  protected function create() {
140  global $CONFIG;
141 
142  $guid = parent::create();
143 
144  $name = sanitize_string($this->attributes['name']);
145  $description = sanitize_string($this->attributes['description']);
146  $url = sanitize_string($this->attributes['url']);
147 
148  $query = "INSERT into {$CONFIG->dbprefix}sites_entity
149  (guid, name, description, url) values ($guid, '$name', '$description', '$url')";
150 
151  $result = $this->getDatabase()->insertData($query);
152  if ($result === false) {
153  // TODO(evan): Throw an exception here?
154  return false;
155  }
156 
157  // make sure the site guid is set to self if not already set
158  if (!$this->site_guid) {
159  $this->site_guid = $guid;
160  $this->getDatabase()->updateData("UPDATE {$CONFIG->dbprefix}entities
161  SET site_guid = $guid WHERE guid = $guid");
162  }
163 
164  return $guid;
165  }
166 
170  protected function update() {
171  global $CONFIG;
172 
173  if (!parent::update()) {
174  return false;
175  }
176 
177  $guid = (int)$this->guid;
178  $name = sanitize_string($this->name);
180  $url = sanitize_string($this->url);
181 
182  $query = "UPDATE {$CONFIG->dbprefix}sites_entity
183  SET name='$name', description='$description', url='$url' WHERE guid=$guid";
184 
185  return $this->getDatabase()->updateData($query) !== false;
186  }
187 
196  public function delete() {
197  global $CONFIG;
198  if ($CONFIG->site->getGUID() == $this->guid) {
199  throw new \SecurityException('You cannot delete the current site');
200  }
201 
202  return parent::delete();
203  }
204 
216  public function disable($reason = "", $recursive = true) {
217  global $CONFIG;
218 
219  if ($CONFIG->site->getGUID() == $this->guid) {
220  throw new \SecurityException('You cannot disable the current site');
221  }
222 
223  return parent::disable($reason, $recursive);
224  }
225 
231  public function getURL() {
232  return $this->url;
233  }
234 
238  public function getDisplayName() {
239  return $this->name;
240  }
241 
245  public function setDisplayName($displayName) {
246  $this->name = $displayName;
247  }
248 
261  public function getMembers($options = array(), $offset = 0) {
262  elgg_deprecated_notice('\ElggSite::getMembers() is deprecated. Use \ElggSite::getEntities()', 1.9);
263  if (!is_array($options)) {
264  elgg_deprecated_notice("\ElggSite::getMembers uses different arguments!", 1.8);
265  $options = array(
266  'limit' => $options,
267  'offset' => $offset,
268  );
269  }
270 
271  $defaults = array(
272  'site_guids' => ELGG_ENTITIES_ANY_VALUE,
273  'relationship' => 'member_of_site',
274  'relationship_guid' => $this->getGUID(),
275  'inverse_relationship' => true,
276  'type' => 'user',
277  );
278 
279  $options = array_merge($defaults, $options);
280 
282  }
283 
295  public function listMembers($options = array()) {
296  elgg_deprecated_notice('\ElggSite::listMembers() is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
297  $defaults = array(
298  'site_guids' => ELGG_ENTITIES_ANY_VALUE,
299  'relationship' => 'member_of_site',
300  'relationship_guid' => $this->getGUID(),
301  'inverse_relationship' => true,
302  'type' => 'user',
303  );
304 
305  $options = array_merge($defaults, $options);
306 
308  }
309 
320  public function addEntity(\ElggEntity $entity) {
321  if (elgg_instanceof($entity, 'site')) {
322  return false;
323  }
324  return add_entity_relationship($entity->guid, "member_of_site", $this->guid);
325  }
326 
334  public function removeEntity($entity) {
335  if (elgg_instanceof($entity, 'site')) {
336  return false;
337  }
338  return remove_entity_relationship($entity->guid, "member_of_site", $this->guid);
339  }
340 
352  public function getEntities(array $options = array()) {
353  $options['relationship'] = 'member_of_site';
354  $options['relationship_guid'] = $this->guid;
355  $options['inverse_relationship'] = true;
356  if (!isset($options['site_guid']) || !isset($options['site_guids'])) {
357  $options['site_guids'] = ELGG_ENTITIES_ANY_VALUE;
358  }
359 
361  }
362 
371  public function addUser($user_guid) {
372  elgg_deprecated_notice('\ElggSite::addUser() is deprecated. Use \ElggEntity::addEntity()', 1.9);
373  return add_site_user($this->getGUID(), $user_guid);
374  }
375 
384  public function removeUser($user_guid) {
385  elgg_deprecated_notice('\ElggSite::removeUser() is deprecated. Use \ElggEntity::removeEntity()', 1.9);
386  return remove_site_user($this->getGUID(), $user_guid);
387  }
388 
402  public function getObjects($subtype = "", $limit = 10, $offset = 0) {
403  elgg_deprecated_notice('\ElggSite::getObjects() is deprecated. Use \ElggSite::getEntities()', 1.9);
404  return get_site_objects($this->getGUID(), $subtype, $limit, $offset);
405  }
406 
415  public function addObject($object_guid) {
416  elgg_deprecated_notice('\ElggSite::addObject() is deprecated. Use \ElggEntity::addEntity()', 1.9);
417  return add_site_object($this->getGUID(), $object_guid);
418  }
419 
428  public function removeObject($object_guid) {
429  elgg_deprecated_notice('\ElggSite::removeObject() is deprecated. Use \ElggEntity::removeEntity()', 1.9);
430  return remove_site_object($this->getGUID(), $object_guid);
431  }
432 
443  public function getCollections($subtype = "", $limit = 10, $offset = 0) {
444  elgg_deprecated_notice("ElggSite::getCollections() is deprecated", 1.8);
446  }
447 
451  protected function prepareObject($object) {
452  $object = parent::prepareObject($object);
453  $object->name = $this->getDisplayName();
454  $object->description = $this->description;
455  unset($object->read_access);
456  return $object;
457  }
458 
459  /*
460  * EXPORTABLE INTERFACE
461  */
462 
469  public function getExportableValues() {
470  return array_merge(parent::getExportableValues(), array(
471  'name',
472  'description',
473  'url',
474  ));
475  }
476 
483  public function getDomain() {
484  $breakdown = parse_url($this->url);
485  return $breakdown['host'];
486  }
487 
496  public function checkWalledGarden() {
497  global $CONFIG;
498 
499  // command line calls should not invoke the walled garden check
500  if (PHP_SAPI === 'cli') {
501  return;
502  }
503 
504  if ($CONFIG->walled_garden) {
505  if ($CONFIG->default_access == ACCESS_PUBLIC) {
506  $CONFIG->default_access = ACCESS_LOGGED_IN;
507  }
508  _elgg_services()->hooks->registerHandler(
509  'access:collections:write',
510  'all',
511  '_elgg_walled_garden_remove_public_access',
512  9999);
513 
514  if (!_elgg_services()->session->isLoggedIn()) {
515  // override the front page
516  elgg_register_page_handler('', '_elgg_walled_garden_index');
517 
518  if (!$this->isPublicPage()) {
519  if (!elgg_is_xhr()) {
520  _elgg_services()->session->set('last_forward_from', current_page_url());
521  }
522  register_error(_elgg_services()->translator->translate('loggedinrequired'));
523  forward('', 'walled_garden');
524  }
525  }
526  }
527  }
528 
539  public function isPublicPage($url = '') {
540  global $CONFIG;
541 
542  if (empty($url)) {
544 
545  // do not check against URL queries
546  if ($pos = strpos($url, '?')) {
547  $url = substr($url, 0, $pos);
548  }
549  }
550 
551  // always allow index page
552  if ($url == _elgg_services()->config->getSiteUrl($this->guid)) {
553  return true;
554  }
555 
556  // default public pages
557  $defaults = array(
558  'walled_garden/.*',
559  'action/.*',
560  'login',
561  'register',
562  'forgotpassword',
563  'changepassword',
564  'refresh_token',
565  'ajax/view/js/languages',
566  'upgrade\.php',
567  'css/.*',
568  'js/.*',
569  'cache/[0-9]+/\w+/js|css/.*',
570  'cron/.*',
571  'services/.*',
572  );
573 
574  // include a hook for plugin authors to include public pages
575  $plugins = _elgg_services()->hooks->trigger('public_pages', 'walled_garden', null, array());
576 
577  // allow public pages
578  foreach (array_merge($defaults, $plugins) as $public) {
579  $pattern = "`^{$CONFIG->url}$public/*$`i";
580  if (preg_match($pattern, $url)) {
581  return true;
582  }
583  }
584 
585  // non-public page
586  return false;
587  }
588 }
static getExternalAttributes()
Get default values for attributes stored in a separate table.
Definition: ElggSite.php:52
prepareObject($object)
{}
Definition: ElggSite.php:451
remove_site_user($site_guid, $user_guid)
Remove a user from a site.
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:67
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:227
remove_site_object($site_guid, $object_guid)
Remove an object from a site.
__construct($row=null)
Create a new .
Definition: ElggSite.php:71
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:334
get_site_collections($site_guid, $subtype="", $limit=10, $offset=0)
Get the collections belonging to a site.
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
current_page_url()
Returns the current page&#39;s complete URL.
Definition: input.php:65
addObject($object_guid)
Adds an object to the site.
Definition: ElggSite.php:415
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:496
$object
Definition: upgrade.php:12
$CONFIG site_guid
The guid of the current site object.
Definition: config.php:122
$defaults
getObjects($subtype="", $limit=10, $offset=0)
Returns an array of entities that belong to the site.
Definition: ElggSite.php:402
_elgg_cache_entity(\ElggEntity $entity)
Cache an entity.
Definition: entities.php:92
removeUser($user_guid)
Removes a user from the site.
Definition: ElggSite.php:384
$value
Definition: longtext.php:26
add_site_object($site_guid, $object_guid)
Add an object to a site.
if($screenshots) $description
Definition: full.php:173
addUser($user_guid)
Adds a user to the site.
Definition: ElggSite.php:371
if(!$count) $offset
Definition: pagination.php:25
getGUID()
Returns the guid.
$guid
Removes an admin notice.
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:432
add_site_user($site_guid, $user_guid)
Add a user to a site.
elgg forward
Meant to mimic the php forward() function by simply redirecting the user to another page...
Definition: elgglib.js:419
get_site_by_url($url)
Return the site via a url.
Definition: sites.php:58
update()
{}
Definition: ElggSite.php:170
$url
Definition: exceptions.php:24
listMembers($options=array())
List the members of this site.
Definition: ElggSite.php:295
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
$options
Definition: index.php:14
disable($reason="", $recursive=true)
Disable the site.
Definition: ElggSite.php:216
$plugins
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:922
setDisplayName($displayName)
{}
Definition: ElggSite.php:245
addEntity(\ElggEntity $entity)
Adds an entity to the site.
Definition: ElggSite.php:320
$limit
Definition: userpicker.php:31
isPublicPage($url= '')
Returns if a URL is public for this site when in Walled Garden mode.
Definition: ElggSite.php:539
getCollections($subtype="", $limit=10, $offset=0)
Get the collections associated with a site.
Definition: ElggSite.php:443
$key
Definition: summary.php:34
elgg menu widget elgg menu item delete
Definition: admin.php:1101
_elgg_services()
Definition: autoloader.php:14
global $CONFIG
initialise_attributes($pre18_api=true)
Initialise the attributes array.
Definition: ElggData.php:39
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:1967
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
getDisplayName()
{}
Definition: ElggSite.php:238
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
elgg global
Pointer to the global context.
Definition: elgglib.js:12
load($guid)
Loads the full when given a guid.
Definition: ElggSite.php:118
$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:231
const ACCESS_PUBLIC
Definition: elgglib.php:1956
create()
{}
Definition: ElggSite.php:139
$site name
elgg register_error
Wrapper function for system_messages.
Definition: elgglib.js:383
getExportableValues()
Return an array of fields which can be exported.
Definition: ElggSite.php:469
if(!$site) if(!($site instanceof ElggSite)) $site url
getMembers($options=array(), $offset=0)
Gets an array of entities who are members of the site.
Definition: ElggSite.php:261
$row
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
const ACCESS_LOGGED_IN
Definition: elgglib.php:1955
$user_guid
Avatar remove action.
Definition: remove.php:6
$entity
Definition: delete.php:10
$subtype
Definition: river.php:12
getDomain()
Get the domain for this site.
Definition: ElggSite.php:483
removeObject($object_guid)
Remvoes an object from the site.
Definition: ElggSite.php:428
getEntities(array $options=array())
Get an array of entities that belong to the site.
Definition: ElggSite.php:352
initializeAttributes()
Initialize the attributes array.
Definition: ElggSite.php:36