]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Merge remote-tracking branch 'upstream/2021.06-rc' into forum-handling
[friendica.git] / include / api.php
index 88736e1f0df1370da9520cf05db119a1b417a54d..935bd11d6355c6ab16df816e229deab494d7b5ad 100644 (file)
@@ -44,6 +44,7 @@ use Friendica\Model\Photo;
 use Friendica\Model\Post;
 use Friendica\Model\User;
 use Friendica\Model\Verb;
+use Friendica\Module\BaseApi;
 use Friendica\Network\HTTPException;
 use Friendica\Network\HTTPException\BadRequestException;
 use Friendica\Network\HTTPException\ExpectationFailedException;
@@ -88,6 +89,11 @@ $called_api = [];
  */
 function api_user()
 {
+       $user = BaseApi::getCurrentUserID(true);
+       if (!empty($user)) {
+               return $user;
+       }
+
        if (!empty($_SESSION['allow_api'])) {
                return local_user();
        }
@@ -175,6 +181,7 @@ 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
@@ -185,8 +192,10 @@ 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)
+function api_login(App $a, bool $do_login = true)
 {
+       $_SESSION["allow_api"] = false;
+
        // workaround for HTTP-auth in CGI mode
        if (!empty($_SERVER['REDIRECT_REMOTE_USER'])) {
                $userpass = base64_decode(substr($_SERVER["REDIRECT_REMOTE_USER"], 6));
@@ -216,6 +225,10 @@ function api_login(App $a)
                        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");
@@ -257,6 +270,9 @@ function api_login(App $a)
        }
 
        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');
@@ -1176,12 +1192,12 @@ function api_statuses_update($type)
                                        INNER JOIN `user` ON `user`.`uid` = `photo`.`uid` WHERE `resource-id` IN
                                                (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
                                        ORDER BY `photo`.`width` DESC LIMIT 2", $id, api_user()));
-                               
+
                        if (!empty($media)) {
                                $ressources[] = $media[0]['resource-id'];
                                $phototypes = Images::supportedTypes();
                                $ext = $phototypes[$media[0]['type']];
-                       
+
                                $attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'],
                                        'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
                                        'size' => $media[0]['datasize'],
@@ -1189,7 +1205,7 @@ function api_statuses_update($type)
                                        'description' => $media[0]['desc'] ?? '',
                                        'width' => $media[0]['width'],
                                        'height' => $media[0]['height']];
-                       
+
                                if (count($media) > 1) {
                                        $attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
                                        $attachment['preview-width'] = $media[1]['width'];