Elgg  Version master
ElggSite.php
Go to the documentation of this file.
1 <?php
2 
6 
28 class ElggSite extends \ElggEntity {
29 
33  protected function initializeAttributes() {
34  parent::initializeAttributes();
35 
36  $this->attributes['type'] = 'site';
37  $this->attributes['subtype'] = 'site';
38 
39  $this->attributes['owner_guid'] = 0;
40  $this->attributes['container_guid'] = 0;
41 
42  $this->attributes['access_id'] = ACCESS_PUBLIC;
43  }
44 
48  public function save(): bool {
49  $qb = Select::fromTable(EntityTable::TABLE_NAME);
50  $qb->select('*')
51  ->where($qb->compare('type', '=', 'site', ELGG_VALUE_STRING));
52 
53  $row = $this->getDatabase()->getDataRow($qb);
54  if (!empty($row)) {
55  if ($row->guid == $this->attributes['guid']) {
56  // can save active site
57  return parent::save();
58  }
59 
60  _elgg_services()->logger->error('More than 1 site entity cannot be created.');
61  return false;
62  }
63 
64  return parent::save();
65  }
66 
70  public function delete(bool $recursive = true, ?bool $persistent = null): bool {
71  if ($this->guid === 1) {
72  throw new SecurityException('You cannot delete the current site');
73  }
74 
75  return parent::delete($recursive, $persistent);
76  }
77 
89  public function disable(string $reason = '', bool $recursive = true): bool {
90  if ($this->guid == 1) {
91  throw new SecurityException('You cannot disable the current site');
92  }
93 
94  return parent::disable($reason, $recursive);
95  }
96 
100  public function __set($name, $value) {
101  if ($name === 'url') {
102  _elgg_services()->logger->warning('ElggSite::url cannot be set');
103  return;
104  }
105 
106  parent::__set($name, $value);
107  }
108 
112  public function __get($name) {
113  if ($name === 'url') {
114  return $this->getURL();
115  }
116 
117  return parent::__get($name);
118  }
119 
125  public function getURL(): string {
126  return _elgg_services()->config->wwwroot;
127  }
128 
132  public function isCacheable(): bool {
133  return false;
134  }
135 
139  protected function prepareObject(\Elgg\Export\Entity $object) {
140  $object = parent::prepareObject($object);
141  $object->name = $this->getDisplayName();
142  $object->description = $this->description;
143  unset($object->read_access);
144  return $object;
145  }
146 
153  public function getDomain(): string {
154  $breakdown = parse_url($this->url);
155  return $breakdown['host'];
156  }
157 
166  public function getEmailAddress(): string {
167  $email = $this->email;
168  if (empty($email)) {
169  // If all else fails, use the domain of the site.
170  $token = _elgg_services()->crypto->getRandomString(24);
171  $email = "noreply-{$token}@{$this->getDomain()}";
172  }
173 
174  return $email;
175  }
176 
180  public function updateLastAction(?int $posted = null): int {
181  // setting last action on ElggSite makes no sense... just returning current value to be compliant
182  return $this->last_action;
183  }
184 }
$site email
Definition: settings.php:16
$email
Definition: change_email.php:7
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:43
prepareObject(\Elgg\Export\Entity $object)
{Prepare an object copy for toObject()Object representation of the entity\Elgg\Export\Entity}
Definition: ElggSite.php:139
__get($name)
{Get an attribute or metadata value.If the name matches an attribute, the attribute is returned....
Definition: ElggSite.php:112
__set($name, $value)
{Set an attribute or metadata value for this entity.Anything that is not an attribute is saved as met...
Definition: ElggSite.php:100
save()
{Save this data to the appropriate database table.bool}
Definition: ElggSite.php:48
updateLastAction(?int $posted=null)
{Update the last_action column in the entities table.This is different to time_updated....
Definition: ElggSite.php:180
getEmailAddress()
Get the email address for the site.
Definition: ElggSite.php:166
isCacheable()
{Is entity cacheable in the runtime cache.bool}
Definition: ElggSite.php:132
getURL()
Returns the URL for this site.
Definition: ElggSite.php:125
disable(string $reason='', bool $recursive=true)
Disable the site.
Definition: ElggSite.php:89
initializeAttributes()
{Initialize the attributes array.This is vital to distinguish between metadata and base parameters....
Definition: ElggSite.php:33
getDomain()
Get the domain for this site.
Definition: ElggSite.php:153
Entity table database service.
Definition: EntityTable.php:24
Query builder for fetching data from the database.
Definition: Select.php:8
Throw when a Security Exception occurs.
const ELGG_VALUE_STRING
Definition: constants.php:112
const ACCESS_PUBLIC
Definition: constants.php:12
if($email instanceof \Elgg\Email) $object
Definition: body.php:24
$posted
Definition: sidebar.php:21
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
$value
Definition: generic.php:51
$persistent
Definition: login_as.php:21
$qb
Definition: queue.php:12
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$token
$description
Definition: record.php:15
$CONFIG wwwroot
The installation root URL of the site.