<?php
/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
+use Friendica\Core\Search;
use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
+use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
use Friendica\Network\HTTPException;
use Friendica\Object\Image;
*
* @return array system account
*/
- public static function getSystemAccount()
+ public static function getSystemAccount(): array
{
$system = Contact::selectFirst([], ['self' => true, 'uid' => 0]);
if (!DBA::isResult($system)) {
*
* @return string actor account name
*/
- public static function getActorName()
+ public static function getActorName(): string
{
$system_actor_name = DI::config()->get('system', 'actor_name');
if (!empty($system_actor_name)) {
* @return boolean
* @throws Exception
*/
- public static function exists($uid)
+ public static function exists(int $uid): bool
{
return DBA::exists('user', ['uid' => $uid]);
}
* @return array|boolean User record if it exists, false otherwise
* @throws Exception
*/
- public static function getById($uid, array $fields = [])
+ public static function getById(int $uid, array $fields = [])
{
return !empty($uid) ? DBA::selectFirst('user', $fields, ['uid' => $uid]) : [];
}
* @return array|boolean User record if it exists, false otherwise
* @throws Exception
*/
- public static function getByNickname($nickname, array $fields = [])
+ public static function getByNickname(string $nickname, array $fields = [])
{
return DBA::selectFirst('user', $fields, ['nickname' => $nickname]);
}
* @return integer user id
* @throws Exception
*/
- public static function getIdForURL(string $url)
+ public static function getIdForURL(string $url): int
{
// Avoid database queries when the local node hostname isn't even part of the url.
if (!Contact::isLocal($url)) {
* @param array $fields
* @return array user
*/
- public static function getFirstAdmin(array $fields = [])
+ public static function getFirstAdmin(array $fields = []) : array
{
if (!empty(DI::config()->get('config', 'admin_nickname'))) {
return self::getByNickname(DI::config()->get('config', 'admin_nickname'), $fields);
* @return boolean|array
* @throws Exception
*/
- public static function getOwnerDataByNick($nick)
+ public static function getOwnerDataByNick(string $nick)
{
$user = DBA::selectFirst('user', ['uid'], ['nickname' => $nick]);
/**
* Returns the default group for a given user and network
*
- * @param int $uid User id
- * @param string $network network name
+ * @param int $uid User id
*
* @return int group id
* @throws Exception
*/
- public static function getDefaultGroup($uid, $network = '')
+ public static function getDefaultGroup(int $uid): int
{
$user = DBA::selectFirst('user', ['def_gid'], ['uid' => $uid]);
if (DBA::isResult($user)) {
return $default_group;
}
-
- /**
- * Authenticate a user with a clear text password
- *
- * @param mixed $user_info
- * @param string $password
- * @param bool $third_party
- * @return int|boolean
- * @deprecated since version 3.6
- * @see User::getIdFromPasswordAuthentication()
- */
- public static function authenticate($user_info, $password, $third_party = false)
- {
- try {
- return self::getIdFromPasswordAuthentication($user_info, $password, $third_party);
- } catch (Exception $ex) {
- return false;
- }
- }
-
/**
* Authenticate a user with a clear text password
*
* @throws HTTPException\ForbiddenException
* @throws HTTPException\NotFoundException
*/
- public static function getIdFromPasswordAuthentication($user_info, $password, $third_party = false)
+ public static function getIdFromPasswordAuthentication($user_info, string $password, bool $third_party = false)
{
// Addons registered with the "authenticate" hook may create the user on the
// fly. `getAuthenticationInfo` will fail if the user doesn't exist yet. If
* @return int User Id if authentication is successful
* @throws HTTPException\ForbiddenException
*/
- public static function getIdFromAuthenticateHooks($username, $password)
+ public static function getIdFromAuthenticateHooks(string $username, string $password): int
{
$addon_auth = [
'username' => $username,
* - User array with at least the uid and the hashed password
*
* @param mixed $user_info
- * @return array
+ * @return array|null Null if not found/determined
* @throws HTTPException\NotFoundException
*/
public static function getAuthenticationInfo($user_info)
* @return string
* @throws Exception
*/
- public static function generateNewPassword()
+ public static function generateNewPassword(): string
{
return ucfirst(Strings::getRandomName(8)) . random_int(1000, 9999);
}
* @return bool
* @throws Exception
*/
- public static function isPasswordExposed($password)
+ public static function isPasswordExposed(string $password): bool
{
$cache = new CacheItemPool();
$cache->changeConfig([
- 'cacheDirectory' => get_temppath() . '/password-exposed-cache/',
+ 'cacheDirectory' => System::getTempPath() . '/password-exposed-cache/',
]);
try {
* @param string $password
* @return string
*/
- private static function hashPasswordLegacy($password)
+ private static function hashPasswordLegacy(string $password): string
{
return hash('whirlpool', $password);
}
* @return string
* @throws Exception
*/
- public static function hashPassword($password)
+ public static function hashPassword(string $password): string
{
if (!trim($password)) {
throw new Exception(DI::l10n()->t('Password can\'t be empty'));
* @return bool
* @throws Exception
*/
- public static function updatePassword($uid, $password)
+ public static function updatePassword(int $uid, string $password): bool
{
$password = trim($password);
* @return bool
* @throws Exception
*/
- private static function updatePasswordHashed($uid, $pasword_hashed)
+ private static function updatePasswordHashed(int $uid, string $pasword_hashed): bool
{
$fields = [
'password' => $pasword_hashed,
* @param string $nickname The nickname that should be checked
* @return boolean True is the nickname is blocked on the node
*/
- public static function isNicknameBlocked($nickname)
+ public static function isNicknameBlocked(string $nickname): bool
{
$forbidden_nicknames = DI::config()->get('system', 'forbidden_nicknames', '');
if (!empty($forbidden_nicknames)) {
* Get avatar link for given user
*
* @param array $user
- * @param string $size One of the ProxyUtils::SIZE_* constants
+ * @param string $size One of the Proxy::SIZE_* constants
* @return string avatar link
* @throws Exception
*/
- public static function getAvatarUrl(array $user, string $size = ''):string
+ public static function getAvatarUrl(array $user, string $size = ''): string
{
+ if (empty($user['nickname'])) {
+ DI::logger()->warning('Missing user nickname key', ['trace' => System::callstack(20)]);
+ }
+
$url = DI::baseUrl() . '/photo/';
switch ($size) {
break;
}
- $updated = '';
- $imagetype = IMAGETYPE_JPEG;
+ $updated = '';
+ $mimetype = '';
$photo = Photo::selectFirst(['type', 'created', 'edited', 'updated'], ["scale" => $scale, 'uid' => $user['uid'], 'profile' => true]);
if (!empty($photo)) {
- $updated = max($photo['created'], $photo['edited'], $photo['updated']);
+ $updated = max($photo['created'], $photo['edited'], $photo['updated']);
+ $mimetype = $photo['type'];
+ }
- if (in_array($photo['type'], ['image/png', 'image/gif'])) {
- $imagetype = IMAGETYPE_PNG;
- }
+ return $url . $user['nickname'] . Images::getExtensionByMimeType($mimetype) . ($updated ? '?ts=' . strtotime($updated) : '');
+ }
+
+ /**
+ * Get banner link for given user
+ *
+ * @param array $user
+ * @return string banner link
+ * @throws Exception
+ */
+ public static function getBannerUrl(array $user): string
+ {
+ if (empty($user['nickname'])) {
+ DI::logger()->warning('Missing user nickname key', ['trace' => System::callstack(20)]);
+ }
+
+ $url = DI::baseUrl() . '/photo/banner/';
+
+ $updated = '';
+ $mimetype = '';
+
+ $photo = Photo::selectFirst(['type', 'created', 'edited', 'updated'], ["scale" => 3, 'uid' => $user['uid'], 'photo-type' => Photo::USER_BANNER]);
+ if (!empty($photo)) {
+ $updated = max($photo['created'], $photo['edited'], $photo['updated']);
+ $mimetype = $photo['type'];
+ } else {
+ // Only for the RC phase: Don't return an image link for the default picture
+ return '';
}
- return $url . $user['uid'] . image_type_to_extension($imagetype) . ($updated ? '?ts=' . strtotime($updated) : '');
+ return $url . $user['nickname'] . Images::getExtensionByMimeType($mimetype) . ($updated ? '?ts=' . strtotime($updated) : '');
}
/**
* @throws ImagickException
* @throws Exception
*/
- public static function create(array $data)
+ public static function create(array $data): array
{
$return = ['user' => null, 'password' => ''];
$using_invites = DI::config()->get('system', 'invitation_only');
- $invite_id = !empty($data['invite_id']) ? Strings::escapeTags(trim($data['invite_id'])) : '';
- $username = !empty($data['username']) ? Strings::escapeTags(trim($data['username'])) : '';
- $nickname = !empty($data['nickname']) ? Strings::escapeTags(trim($data['nickname'])) : '';
- $email = !empty($data['email']) ? Strings::escapeTags(trim($data['email'])) : '';
- $openid_url = !empty($data['openid_url']) ? Strings::escapeTags(trim($data['openid_url'])) : '';
- $photo = !empty($data['photo']) ? Strings::escapeTags(trim($data['photo'])) : '';
- $password = !empty($data['password']) ? trim($data['password']) : '';
- $password1 = !empty($data['password1']) ? trim($data['password1']) : '';
- $confirm = !empty($data['confirm']) ? trim($data['confirm']) : '';
+ $invite_id = !empty($data['invite_id']) ? trim($data['invite_id']) : '';
+ $username = !empty($data['username']) ? trim($data['username']) : '';
+ $nickname = !empty($data['nickname']) ? trim($data['nickname']) : '';
+ $email = !empty($data['email']) ? trim($data['email']) : '';
+ $openid_url = !empty($data['openid_url']) ? trim($data['openid_url']) : '';
+ $photo = !empty($data['photo']) ? trim($data['photo']) : '';
+ $password = !empty($data['password']) ? trim($data['password']) : '';
+ $password1 = !empty($data['password1']) ? trim($data['password1']) : '';
+ $confirm = !empty($data['confirm']) ? trim($data['confirm']) : '';
$blocked = !empty($data['blocked']);
$verified = !empty($data['verified']);
- $language = !empty($data['language']) ? Strings::escapeTags(trim($data['language'])) : 'en';
+ $language = !empty($data['language']) ? trim($data['language']) : 'en';
$netpublish = $publish = !empty($data['profile_publish_reg']);
$username_max_length = max(1, min(64, intval(DI::config()->get('system', 'username_max_length', 48))));
if ($username_min_length > $username_max_length) {
- Logger::log(DI::l10n()->t('system.username_min_length (%s) and system.username_max_length (%s) are excluding each other, swapping values.', $username_min_length, $username_max_length), Logger::WARNING);
+ Logger::error(DI::l10n()->t('system.username_min_length (%s) and system.username_max_length (%s) are excluding each other, swapping values.', $username_min_length, $username_max_length));
$tmp = $username_min_length;
$username_min_length = $username_max_length;
$username_max_length = $tmp;
$photo_failure = false;
$filename = basename($photo);
- $curlResult = DI::httpClient()->get($photo);
+ $curlResult = DI::httpClient()->get($photo, HttpClientAccept::IMAGE);
if ($curlResult->isSuccess()) {
+ Logger::debug('Got picture', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $photo]);
$img_str = $curlResult->getBody();
$type = $curlResult->getContentType();
} else {
$resource_id = Photo::newResource();
- $r = Photo::store($Image, $uid, 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 4);
+ // Not using Photo::PROFILE_PHOTOS here, so that it is discovered as translateble string
+ $profile_album = DI::l10n()->t('Profile Photos');
+
+ $r = Photo::store($Image, $uid, 0, $resource_id, $filename, $profile_album, 4);
if ($r === false) {
$photo_failure = true;
$Image->scaleDown(80);
- $r = Photo::store($Image, $uid, 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 5);
+ $r = Photo::store($Image, $uid, 0, $resource_id, $filename, $profile_album, 5);
if ($r === false) {
$photo_failure = true;
$Image->scaleDown(48);
- $r = Photo::store($Image, $uid, 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 6);
+ $r = Photo::store($Image, $uid, 0, $resource_id, $filename, $profile_album, 6);
if ($r === false) {
$photo_failure = true;
}
if (!$photo_failure) {
- Photo::update(['profile' => 1], ['resource-id' => $resource_id]);
+ Photo::update(['profile' => true, 'photo-type' => Photo::USER_AVATAR], ['resource-id' => $resource_id]);
}
}
* @throws Exception
*/
- public static function block(int $uid, bool $block = true)
+ public static function block(int $uid, bool $block = true): bool
{
return DBA::update('user', ['blocked' => $block], ['uid' => $uid]);
}
* @throws HTTPException\InternalServerErrorException
* @throws Exception
*/
- public static function allow(string $hash)
+ public static function allow(string $hash): bool
{
$register = Register::getByHash($hash);
if (!DBA::isResult($register)) {
$profile = DBA::selectFirst('profile', ['net-publish'], ['uid' => $register['uid']]);
- if (DBA::isResult($profile) && $profile['net-publish'] && DI::config()->get('system', 'directory')) {
+ if (DBA::isResult($profile) && $profile['net-publish'] && Search::getGlobalDirectory()) {
$url = DI::baseUrl() . '/profile/' . $user['nickname'];
Worker::add(PRIORITY_LOW, "Directory", $url);
}
* @return bool True, if the deny was successfull
* @throws Exception
*/
- public static function deny(string $hash)
+ public static function deny(string $hash): bool
{
$register = Register::getByHash($hash);
if (!DBA::isResult($register)) {
* @throws ErrorException
* @throws ImagickException
*/
- public static function createMinimal(string $name, string $email, string $nick, string $lang = L10n::DEFAULT)
+ public static function createMinimal(string $name, string $email, string $nick, string $lang = L10n::DEFAULT): bool
{
if (empty($name) ||
empty($email) ||
* @return NULL|boolean from notification() and email() inherited
* @throws HTTPException\InternalServerErrorException
*/
- public static function sendRegisterPendingEmail($user, $sitename, $siteurl, $password)
+ public static function sendRegisterPendingEmail(array $user, string $sitename, string $siteurl, string $password)
{
$body = Strings::deindent(DI::l10n()->t(
'
* @return NULL|boolean from notification() and email() inherited
* @throws HTTPException\InternalServerErrorException
*/
- public static function sendRegisterOpenEmail(L10n $l10n, $user, $sitename, $siteurl, $password)
+ public static function sendRegisterOpenEmail(L10n $l10n, array $user, string $sitename, string $siteurl, string $password)
{
$preamble = Strings::deindent($l10n->t(
'
* @return bool
* @throws HTTPException\InternalServerErrorException
*/
- public static function remove(int $uid)
+ public static function remove(int $uid): bool
{
if (empty($uid)) {
return false;
}
- Logger::log('Removing user: ' . $uid);
+ Logger::notice('Removing user', ['user' => $uid]);
$user = DBA::selectFirst('user', [], ['uid' => $uid]);
* ]
* @throws Exception
*/
- public static function identities($uid)
+ public static function identities(int $uid): array
{
if (empty($uid)) {
return [];
* @param int $uid
* @return bool
*/
- public static function hasIdentities(int $uid):bool
+ public static function hasIdentities(int $uid): bool
{
if (empty($uid)) {
return false;
*
* @throws Exception
*/
- public static function getStatistics()
+ public static function getStatistics(): array
{
$statistics = [
'total_users' => 0,
* @param string $order Order of the user list (Default is 'contact.name')
* @param bool $descending Order direction (Default is ascending)
*
- * @return array The list of the users
+ * @return array|bool The list of the users
* @throws Exception
*/
- public static function getList($start = 0, $count = Pager::ITEMS_PER_PAGE, $type = 'all', $order = 'name', bool $descending = false)
+ public static function getList(int $start = 0, int $count = Pager::ITEMS_PER_PAGE, string $type = 'all', string $order = 'name', bool $descending = false)
{
$param = ['limit' => [$start, $count], 'order' => [$order => $descending]];
$condition = [];