]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Merge pull request #10362 from tobiasd/2021.06-CHANGELOG
[friendica.git] / include / api.php
index 8e89895ded26fb68eb8eeb71610536d4cd4732a4..492d3d1cb7a7fabb4fb65782aff129c40b955f2f 100644 (file)
@@ -42,6 +42,7 @@ use Friendica\Model\Mail;
 use Friendica\Model\Notification;
 use Friendica\Model\Photo;
 use Friendica\Model\Post;
+use Friendica\Model\Profile;
 use Friendica\Model\User;
 use Friendica\Model\Verb;
 use Friendica\Network\HTTPException;
@@ -57,6 +58,7 @@ use Friendica\Object\Image;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\Diaspora;
 use Friendica\Security\FKOAuth1;
+use Friendica\Security\OAuth;
 use Friendica\Security\OAuth1\OAuthRequest;
 use Friendica\Security\OAuth1\OAuthUtil;
 use Friendica\Util\DateTimeFormat;
@@ -88,6 +90,11 @@ $called_api = [];
  */
 function api_user()
 {
+       $user = OAuth::getCurrentUserID();
+       if (!empty($user)) {
+               return $user;
+       }
+
        if (!empty($_SESSION['allow_api'])) {
                return local_user();
        }
@@ -175,7 +182,6 @@ function api_register_func($path, $func, $auth = false, $method = API_METHOD_ANY
  * Simple Auth allow username in form of <pre>user@server</pre>, ignoring server part
  *
  * @param App $a App
- * @param bool $do_login try to log in when not logged in, otherwise quit silently
  * @throws ForbiddenException
  * @throws InternalServerErrorException
  * @throws UnauthorizedException
@@ -186,7 +192,7 @@ function api_register_func($path, $func, $auth = false, $method = API_METHOD_ANY
  *               'authenticated' => return status,
  *               'user_record' => return authenticated user record
  */
-function api_login(App $a, bool $do_login = true)
+function api_login(App $a)
 {
        $_SESSION["allow_api"] = false;
 
@@ -219,10 +225,6 @@ function api_login(App $a, bool $do_login = true)
                        Logger::warning(API_LOG_PREFIX . 'OAuth error', ['module' => 'api', 'action' => 'login', 'exception' => $e->getMessage()]);
                }
 
-               if (!$do_login) {
-                       return;
-               }
-
                Logger::debug(API_LOG_PREFIX . 'failed', ['module' => 'api', 'action' => 'login', 'parameters' => $_SERVER]);
                header('WWW-Authenticate: Basic realm="Friendica"');
                throw new UnauthorizedException("This API requires login");
@@ -264,9 +266,6 @@ function api_login(App $a, bool $do_login = true)
        }
 
        if (!DBA::isResult($record)) {
-               if (!$do_login) {
-                       return;
-               }
                Logger::debug(API_LOG_PREFIX . 'failed', ['module' => 'api', 'action' => 'login', 'parameters' => $_SERVER]);
                header('WWW-Authenticate: Basic realm="Friendica"');
                //header('HTTP/1.0 401 Unauthorized');
@@ -602,7 +601,7 @@ function api_get_user(App $a, $contact_id = null)
                        api_login($a);
                        return false;
                } else {
-                       $user = $_SESSION['uid'];
+                       $user = api_user();
                        $extra_query = "AND `contact`.`uid` = %d AND `contact`.`self` ";
                }
        }
@@ -4554,12 +4553,7 @@ function api_account_update_profile_image($type)
        Contact::updateSelfFromUserID(api_user(), true);
 
        // Update global directory in background
-       $url = DI::baseUrl() . '/profile/' . DI::app()->user['nickname'];
-       if ($url && strlen(DI::config()->get('system', 'directory'))) {
-               Worker::add(PRIORITY_LOW, "Directory", $url);
-       }
-
-       Worker::add(PRIORITY_LOW, 'ProfileUpdate', api_user());
+       Profile::publishUpdate(api_user());
 
        // output for client
        if ($data) {
@@ -4610,11 +4604,7 @@ function api_account_update_profile($type)
                DBA::update('contact', ['about' => $_POST['description']], ['id' => $api_user['id']]);
        }
 
-       Worker::add(PRIORITY_LOW, 'ProfileUpdate', $local_user);
-       // Update global directory in background
-       if ($api_user['url'] && strlen(DI::config()->get('system', 'directory'))) {
-               Worker::add(PRIORITY_LOW, "Directory", $api_user['url']);
-       }
+       Profile::publishUpdate($local_user);
 
        return api_account_verify_credentials($type);
 }