Elgg  Version 1.9
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['name'] = null;
41  $this->attributes['description'] = null;
42  $this->attributes['url'] = null;
43  $this->tables_split = 2;
44  }
45 
57  public function __construct($row = null) {
58  $this->initializeAttributes();
59 
60  // compatibility for 1.7 api.
61  $this->initialise_attributes(false);
62 
63  if (!empty($row)) {
64  // Is $row is a DB entity table row
65  if ($row instanceof stdClass) {
66  // Load the rest
67  if (!$this->load($row)) {
68  $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
69  throw new IOException($msg);
70  }
71  } else if ($row instanceof ElggSite) {
72  // $row is an ElggSite so this is a copy constructor
73  elgg_deprecated_notice('This type of usage of the ElggSite constructor was deprecated. Please use the clone method.', 1.7);
74  foreach ($row->attributes as $key => $value) {
75  $this->attributes[$key] = $value;
76  }
77  } else if (strpos($row, "http") !== false) {
78  // url so retrieve by url
79  elgg_deprecated_notice("Passing URL to constructor is deprecated. Use get_site_by_url()", 1.9);
81  foreach ($row->attributes as $key => $value) {
82  $this->attributes[$key] = $value;
83  }
84  } else if (is_numeric($row)) {
85  // $row is a GUID so load
86  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
87  if (!$this->load($row)) {
88  throw new IOException("Failed to load new " . get_class() . " from GUID:" . $row);
89  }
90  } else {
91  throw new InvalidParameterException("Unrecognized value passed to constuctor.");
92  }
93  }
94  }
95 
104  protected function load($guid) {
105  $attr_loader = new Elgg_AttributeLoader(get_class(), 'site', $this->attributes);
106  $attr_loader->requires_access_control = !($this instanceof ElggPlugin);
107  $attr_loader->secondary_loader = 'get_site_entity_as_row';
108 
109  $attrs = $attr_loader->getRequiredAttributes($guid);
110  if (!$attrs) {
111  return false;
112  }
113 
114  $this->attributes = $attrs;
115  $this->tables_loaded = 2;
116  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
117  _elgg_cache_entity($this);
118 
119  return true;
120  }
121 
125  protected function create() {
126  global $CONFIG;
127 
128  $guid = parent::create();
129 
130  $name = sanitize_string($this->attributes['name']);
131  $description = sanitize_string($this->attributes['description']);
132  $url = sanitize_string($this->attributes['url']);
133 
134  $query = "INSERT into {$CONFIG->dbprefix}sites_entity
135  (guid, name, description, url) values ($guid, '$name', '$description', '$url')";
136 
137  $result = $this->getDatabase()->insertData($query);
138  if ($result === false) {
139  // TODO(evan): Throw an exception here?
140  return false;
141  }
142 
143  // make sure the site guid is set to self if not already set
144  if (!$this->site_guid) {
145  $this->site_guid = $guid;
146  $this->getDatabase()->updateData("UPDATE {$CONFIG->dbprefix}entities
147  SET site_guid = $guid WHERE guid = $guid");
148  }
149 
150  return $guid;
151  }
152 
156  protected function update() {
157  global $CONFIG;
158 
159  if (!parent::update()) {
160  return false;
161  }
162 
163  $guid = (int)$this->guid;
164  $name = sanitize_string($this->name);
166  $url = sanitize_string($this->url);
167 
168  $query = "UPDATE {$CONFIG->dbprefix}sites_entity
169  SET name='$name', description='$description', url='$url' WHERE guid=$guid";
170 
171  return $this->getDatabase()->updateData($query) !== false;
172  }
173 
182  public function delete() {
183  global $CONFIG;
184  if ($CONFIG->site->getGUID() == $this->guid) {
185  throw new SecurityException('You cannot delete the current site');
186  }
187 
188  return parent::delete();
189  }
190 
202  public function disable($reason = "", $recursive = true) {
203  global $CONFIG;
204 
205  if ($CONFIG->site->getGUID() == $this->guid) {
206  throw new SecurityException('You cannot disable the current site');
207  }
208 
209  return parent::disable($reason, $recursive);
210  }
211 
217  public function getURL() {
218  return $this->url;
219  }
220 
224  public function getDisplayName() {
225  return $this->name;
226  }
227 
231  public function setDisplayName($displayName) {
232  $this->name = $displayName;
233  }
234 
247  public function getMembers($options = array(), $offset = 0) {
248  elgg_deprecated_notice('ElggSite::getMembers() is deprecated. Use ElggSite::getEntities()', 1.9);
249  if (!is_array($options)) {
250  elgg_deprecated_notice("ElggSite::getMembers uses different arguments!", 1.8);
251  $options = array(
252  'limit' => $options,
253  'offset' => $offset,
254  );
255  }
256 
257  $defaults = array(
258  'site_guids' => ELGG_ENTITIES_ANY_VALUE,
259  'relationship' => 'member_of_site',
260  'relationship_guid' => $this->getGUID(),
261  'inverse_relationship' => true,
262  'type' => 'user',
263  );
264 
265  $options = array_merge($defaults, $options);
266 
268  }
269 
281  public function listMembers($options = array()) {
282  elgg_deprecated_notice('ElggSite::listMembers() is deprecated. Use elgg_list_entities_from_relationship()', 1.9);
283  $defaults = array(
284  'site_guids' => ELGG_ENTITIES_ANY_VALUE,
285  'relationship' => 'member_of_site',
286  'relationship_guid' => $this->getGUID(),
287  'inverse_relationship' => true,
288  'type' => 'user',
289  );
290 
291  $options = array_merge($defaults, $options);
292 
294  }
295 
306  public function addEntity(ElggEntity $entity) {
307  if (elgg_instanceof($entity, 'site')) {
308  return false;
309  }
310  return add_entity_relationship($entity->guid, "member_of_site", $this->guid);
311  }
312 
320  public function removeEntity($entity) {
321  if (elgg_instanceof($entity, 'site')) {
322  return false;
323  }
324  return remove_entity_relationship($entity->guid, "member_of_site", $this->guid);
325  }
326 
338  public function getEntities(array $options = array()) {
339  $options['relationship'] = 'member_of_site';
340  $options['relationship_guid'] = $this->guid;
341  $options['inverse_relationship'] = true;
342  if (!isset($options['site_guid']) || !isset($options['site_guids'])) {
343  $options['site_guids'] = ELGG_ENTITIES_ANY_VALUE;
344  }
345 
347  }
348 
357  public function addUser($user_guid) {
358  elgg_deprecated_notice('ElggSite::addUser() is deprecated. Use ElggEntity::addEntity()', 1.9);
359  return add_site_user($this->getGUID(), $user_guid);
360  }
361 
370  public function removeUser($user_guid) {
371  elgg_deprecated_notice('ElggSite::removeUser() is deprecated. Use ElggEntity::removeEntity()', 1.9);
372  return remove_site_user($this->getGUID(), $user_guid);
373  }
374 
388  public function getObjects($subtype = "", $limit = 10, $offset = 0) {
389  elgg_deprecated_notice('ElggSite::getObjects() is deprecated. Use ElggSite::getEntities()', 1.9);
390  return get_site_objects($this->getGUID(), $subtype, $limit, $offset);
391  }
392 
401  public function addObject($object_guid) {
402  elgg_deprecated_notice('ElggSite::addObject() is deprecated. Use ElggEntity::addEntity()', 1.9);
403  return add_site_object($this->getGUID(), $object_guid);
404  }
405 
414  public function removeObject($object_guid) {
415  elgg_deprecated_notice('ElggSite::removeObject() is deprecated. Use ElggEntity::removeEntity()', 1.9);
416  return remove_site_object($this->getGUID(), $object_guid);
417  }
418 
429  public function getCollections($subtype = "", $limit = 10, $offset = 0) {
430  elgg_deprecated_notice("ElggSite::getCollections() is deprecated", 1.8);
432  }
433 
437  protected function prepareObject($object) {
438  $object = parent::prepareObject($object);
439  $object->name = $this->getDisplayName();
440  $object->description = $this->description;
441  unset($object->read_access);
442  return $object;
443  }
444 
445  /*
446  * EXPORTABLE INTERFACE
447  */
448 
455  public function getExportableValues() {
456  return array_merge(parent::getExportableValues(), array(
457  'name',
458  'description',
459  'url',
460  ));
461  }
462 
469  public function getDomain() {
470  $breakdown = parse_url($this->url);
471  return $breakdown['host'];
472  }
473 
482  public function checkWalledGarden() {
483  global $CONFIG;
484 
485  // command line calls should not invoke the walled garden check
486  if (PHP_SAPI === 'cli') {
487  return;
488  }
489 
490  if ($CONFIG->walled_garden) {
491  if ($CONFIG->default_access == ACCESS_PUBLIC) {
492  $CONFIG->default_access = ACCESS_LOGGED_IN;
493  }
495  'access:collections:write',
496  'all',
497  '_elgg_walled_garden_remove_public_access',
498  9999);
499 
500  if (!elgg_is_logged_in()) {
501  // override the front page
502  elgg_register_page_handler('', '_elgg_walled_garden_index');
503 
504  if (!$this->isPublicPage()) {
505  if (!elgg_is_xhr()) {
506  _elgg_services()->session->set('last_forward_from', current_page_url());
507  }
508  register_error(elgg_echo('loggedinrequired'));
509  forward('', 'walled_garden');
510  }
511  }
512  }
513  }
514 
525  public function isPublicPage($url = '') {
526  global $CONFIG;
527 
528  if (empty($url)) {
530 
531  // do not check against URL queries
532  if ($pos = strpos($url, '?')) {
533  $url = substr($url, 0, $pos);
534  }
535  }
536 
537  // always allow index page
538  if ($url == elgg_get_site_url($this->guid)) {
539  return true;
540  }
541 
542  // default public pages
543  $defaults = array(
544  'walled_garden/.*',
545  'action/.*',
546  'login',
547  'register',
548  'forgotpassword',
549  'changepassword',
550  'refresh_token',
551  'ajax/view/js/languages',
552  'upgrade\.php',
553  'css/.*',
554  'js/.*',
555  'cache/[0-9]+/\w+/js|css/.*',
556  'cron/.*',
557  'services/.*',
558  );
559 
560  // include a hook for plugin authors to include public pages
561  $plugins = elgg_trigger_plugin_hook('public_pages', 'walled_garden', null, array());
562 
563  // allow public pages
564  foreach (array_merge($defaults, $plugins) as $public) {
565  $pattern = "`^{$CONFIG->url}$public/*$`i";
566  if (preg_match($pattern, $url)) {
567  return true;
568  }
569  }
570 
571  // non-public page
572  return false;
573  }
574 }
prepareObject($object)
{}
Definition: ElggSite.php:437
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_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:56
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:237
remove_site_object($site_guid, $object_guid)
Remove an object from a site.
__construct($row=null)
Create a new ElggSite.
Definition: ElggSite.php:57
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:320
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:106
addObject($object_guid)
Adds an object to the site.
Definition: ElggSite.php:401
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:482
$object
Definition: upgrade.php:12
$CONFIG site_guid
The guid of the current site object.
Definition: config.php:129
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
getObjects($subtype="", $limit=10, $offset=0)
Returns an array of ElggObject entities that belong to the site.
Definition: ElggSite.php:388
removeUser($user_guid)
Removes a user from the site.
Definition: ElggSite.php:370
$value
Definition: longtext.php:29
add_site_object($site_guid, $object_guid)
Add an object to a site.
$comment description
Definition: save.php:49
if($screenshots) $description
Definition: full.php:173
addUser($user_guid)
Adds a user to the site.
Definition: ElggSite.php:357
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
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
Definition: elgglib.php:853
update()
{}
Definition: ElggSite.php:156
$url
Definition: exceptions.php:24
listMembers($options=array())
List the members of this site.
Definition: ElggSite.php:281
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:202
$plugins
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an ElggEntity and optionally for type and subtype.
Definition: entities.php:1886
setDisplayName($displayName)
{}
Definition: ElggSite.php:231
$limit
Definition: userpicker.php:33
isPublicPage($url= '')
Returns if a URL is public for this site when in Walled Garden mode.
Definition: ElggSite.php:525
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
getCollections($subtype="", $limit=10, $offset=0)
Get the collections associated with a site.
Definition: ElggSite.php:429
$key
Definition: summary.php:34
addEntity(ElggEntity $entity)
Adds an entity to the site.
Definition: ElggSite.php:306
elgg menu widget elgg menu item delete
Definition: admin.php:1075
_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:2134
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:925
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
getDisplayName()
{}
Definition: ElggSite.php:224
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
elgg global
Pointer to the global context.
Definition: elgglib.js:12
elgg_get_site_url($site_guid=0)
Get the URL for the current (or specified) site.
load($guid)
Loads the full ElggSite when given a guid.
Definition: ElggSite.php:104
$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:217
const ACCESS_PUBLIC
Definition: elgglib.php:2123
$CONFIG url
The full URL where Elgg is installed.
Definition: config.php:94
create()
{}
Definition: ElggSite.php:125
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:455
getMembers($options=array(), $offset=0)
Gets an array of ElggUser entities who are members of the site.
Definition: ElggSite.php:247
$row
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
const ACCESS_LOGGED_IN
Definition: elgglib.php:2122
$user_guid
Avatar remove action.
Definition: remove.php:6
$defaults
Definition: access.php:19
$entity
Definition: delete.php:10
$subtype
Definition: river.php:10
getDomain()
Get the domain for this site.
Definition: ElggSite.php:469
removeObject($object_guid)
Remvoes an object from the site.
Definition: ElggSite.php:414
A Site entity.
Definition: ElggSite.php:28
_elgg_cache_entity(ElggEntity $entity)
Cache an entity.
Definition: entities.php:101
getEntities(array $options=array())
Get an array of entities that belong to the site.
Definition: ElggSite.php:338
initializeAttributes()
Initialize the attributes array.
Definition: ElggSite.php:36