. * */ namespace Friendica\Security; use Friendica\Database\DBA; use Friendica\DI; /** * Authentification via the basic auth method */ class BasicAuth { /** * @var bool|int */ protected static $current_user_id = 0; /** * @var array */ protected static $current_token = []; /** * Get current user id, returns 0 if $login is set to false and not logged in. * When $login is true, the execution will stop when not logged in. * * @param bool $login Perform a login request if "true" * * @return int User ID */ public static function getCurrentUserID(bool $login = true) { if (empty(self::$current_user_id)) { api_login(DI::app(), $login); self::$current_user_id = api_user(); } return (int)self::$current_user_id; } /** * Fetch a dummy application token * * @return array token */ public static function getCurrentApplicationToken() { if (empty(self::getCurrentUserID())) { return []; } if (!empty(self::$current_token)) { return self::$current_token; } self::$current_token = [ 'uid' => self::$current_user_id, 'id' => 0, 'name' => api_source(), 'website' => '', 'created_at' => DBA::NULL_DATETIME, 'read' => true, 'write' => true, 'follow' => true, 'push' => false]; return self::$current_token; } }