+ /**
+ * Returns the numeric account type by their string
+ *
+ * @param string $accounttype as string constant
+ * @return int|null Numeric account type - or null when not set
+ */
+ public static function getAccountTypeByString(string $accounttype)
+ {
+ switch ($accounttype) {
+ case 'person':
+ return User::ACCOUNT_TYPE_PERSON;
+ case 'organisation':
+ return User::ACCOUNT_TYPE_ORGANISATION;
+ case 'news':
+ return User::ACCOUNT_TYPE_NEWS;
+ case 'community':
+ return User::ACCOUNT_TYPE_COMMUNITY;
+ default:
+ return null;
+ break;
+ }
+ }
+
+ /**
+ * Fetch the system account
+ *
+ * @return array system account
+ */
+ public static function getSystemAccount()
+ {
+ $system = Contact::selectFirst([], ['self' => true, 'uid' => 0]);
+ if (!DBA::isResult($system)) {
+ self::createSystemAccount();
+ $system = Contact::selectFirst([], ['self' => true, 'uid' => 0]);
+ if (!DBA::isResult($system)) {
+ return [];
+ }
+ }
+
+ $system['sprvkey'] = $system['uprvkey'] = $system['prvkey'];
+ $system['spubkey'] = $system['upubkey'] = $system['pubkey'];
+ $system['nickname'] = $system['nick'];
+
+ // Ensure that the user contains data
+ $user = DBA::selectFirst('user', ['prvkey'], ['uid' => 0]);
+ if (empty($user['prvkey'])) {
+ $fields = [
+ 'username' => $system['name'],
+ 'nickname' => $system['nick'],
+ 'register_date' => $system['created'],
+ 'pubkey' => $system['pubkey'],
+ 'prvkey' => $system['prvkey'],
+ 'spubkey' => $system['spubkey'],
+ 'sprvkey' => $system['sprvkey'],
+ 'verified' => true,
+ 'page-flags' => User::PAGE_FLAGS_SOAPBOX,
+ 'account-type' => User::ACCOUNT_TYPE_RELAY,
+ ];
+
+ DBA::update('user', $fields, ['uid' => 0]);
+ }
+
+ return $system;
+ }
+
+ /**
+ * Create the system account
+ *
+ * @return void
+ */
+ private static function createSystemAccount()
+ {
+ $system_actor_name = self::getActorName();
+ if (empty($system_actor_name)) {
+ return;
+ }
+
+ $keys = Crypto::newKeypair(4096);
+ if ($keys === false) {
+ throw new Exception(DI::l10n()->t('SERIOUS ERROR: Generation of security keys failed.'));
+ }
+
+ $system = [];
+ $system['uid'] = 0;
+ $system['created'] = DateTimeFormat::utcNow();
+ $system['self'] = true;
+ $system['network'] = Protocol::ACTIVITYPUB;
+ $system['name'] = 'System Account';
+ $system['addr'] = $system_actor_name . '@' . DI::baseUrl()->getHostname();
+ $system['nick'] = $system_actor_name;
+ $system['url'] = DI::baseUrl() . '/friendica';
+
+ $system['avatar'] = $system['photo'] = Contact::getDefaultAvatar($system, Proxy::SIZE_SMALL);
+ $system['thumb'] = Contact::getDefaultAvatar($system, Proxy::SIZE_THUMB);
+ $system['micro'] = Contact::getDefaultAvatar($system, Proxy::SIZE_MICRO);
+
+ $system['nurl'] = Strings::normaliseLink($system['url']);
+ $system['pubkey'] = $keys['pubkey'];
+ $system['prvkey'] = $keys['prvkey'];
+ $system['blocked'] = 0;
+ $system['pending'] = 0;
+ $system['contact-type'] = Contact::TYPE_RELAY; // In AP this is translated to 'Application'
+ $system['name-date'] = DateTimeFormat::utcNow();
+ $system['uri-date'] = DateTimeFormat::utcNow();
+ $system['avatar-date'] = DateTimeFormat::utcNow();
+ $system['closeness'] = 0;
+ $system['baseurl'] = DI::baseUrl();
+ $system['gsid'] = GServer::getID($system['baseurl']);
+ DBA::insert('contact', $system);
+ }
+
+ /**
+ * Detect a usable actor name
+ *
+ * @return string actor account name
+ */
+ public static function getActorName()
+ {
+ $system_actor_name = DI::config()->get('system', 'actor_name');
+ if (!empty($system_actor_name)) {
+ $self = Contact::selectFirst(['nick'], ['uid' => 0, 'self' => true]);
+ if (!empty($self['nick'])) {
+ if ($self['nick'] != $system_actor_name) {
+ // Reset the actor name to the already used name
+ DI::config()->set('system', 'actor_name', $self['nick']);
+ $system_actor_name = $self['nick'];
+ }
+ }
+ return $system_actor_name;
+ }
+
+ // List of possible actor names
+ $possible_accounts = ['friendica', 'actor', 'system', 'internal'];
+ foreach ($possible_accounts as $name) {
+ if (!DBA::exists('user', ['nickname' => $name, 'account_removed' => false, 'expire' => false]) &&
+ !DBA::exists('userd', ['username' => $name])) {
+ DI::config()->set('system', 'actor_name', $name);
+ return $name;
+ }
+ }
+ return '';
+ }
+