]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Improved Mime Type detection
[friendica.git] / include / api.php
index eb02a6bfa8d353d241c77cba4189e2a1656f9a63..8291d189269bdbfe26056df68c4a53632fe0e9b3 100644 (file)
@@ -186,23 +186,6 @@ function api_register_func($path, $func, $auth = false, $method = API_METHOD_ANY
  */
 function api_login(App $a)
 {
-       $oauth1 = new FKOAuth1();
-       // login with oauth
-       try {
-               $request = OAuthRequest::from_request();
-               list($consumer, $token) = $oauth1->verify_request($request);
-               if (!is_null($token)) {
-                       $oauth1->loginUser($token->uid);
-                       Session::set('allow_api', true);
-                       return;
-               }
-               echo __FILE__.__LINE__.__FUNCTION__ . "<pre>";
-               var_dump($consumer, $token);
-               die();
-       } catch (Exception $e) {
-               Logger::warning(API_LOG_PREFIX . 'error', ['module' => 'api', 'action' => 'login', 'exception' => $e->getMessage()]);
-       }
-
        // workaround for HTTP-auth in CGI mode
        if (!empty($_SERVER['REDIRECT_REMOTE_USER'])) {
                $userpass = base64_decode(substr($_SERVER["REDIRECT_REMOTE_USER"], 6));
@@ -214,6 +197,24 @@ function api_login(App $a)
        }
 
        if (empty($_SERVER['PHP_AUTH_USER'])) {
+               // Try OAuth when no user is provided
+               $oauth1 = new FKOAuth1();
+               // login with oauth
+               try {
+                       $request = OAuthRequest::from_request();
+                       list($consumer, $token) = $oauth1->verify_request($request);
+                       if (!is_null($token)) {
+                               $oauth1->loginUser($token->uid);
+                               Session::set('allow_api', true);
+                               return;
+                       }
+                       echo __FILE__.__LINE__.__FUNCTION__ . "<pre>";
+                       var_dump($consumer, $token);
+                       die();
+               } catch (Exception $e) {
+                       Logger::warning(API_LOG_PREFIX . 'OAuth error', ['module' => 'api', 'action' => 'login', 'exception' => $e->getMessage()]);
+               }
+
                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");
@@ -783,7 +784,7 @@ function api_item_get_user(App $a, $item)
 
        $author_user = $status_user;
 
-       $status_user["protected"] = $item['private'] ?? 0;
+       $status_user["protected"] = isset($item['private']) && ($item['private'] == Item::PRIVATE);
 
        if (($item['thr-parent'] ?? '') == ($item['uri'] ?? '')) {
                $owner_user = api_get_user($a, $item['owner-id'] ?? null);
@@ -1342,7 +1343,7 @@ function api_get_last_status($ownerId, $uid)
                'author-id'=> $ownerId,
                'uid'      => $uid,
                'gravity'  => [GRAVITY_PARENT, GRAVITY_COMMENT],
-               'private'  => false
+               'private'  => [Item::PUBLIC, Item::UNLISTED]
        ];
 
        $item = api_get_item($condition);
@@ -1732,8 +1733,8 @@ function api_statuses_public_timeline($type)
        $start = max(0, ($page - 1) * $count);
 
        if ($exclude_replies && !$conversation_id) {
-               $condition = ["`gravity` IN (?, ?) AND `iid` > ? AND NOT `private` AND `wall` AND NOT `user`.`hidewall` AND NOT `author`.`hidden`",
-                       GRAVITY_PARENT, GRAVITY_COMMENT, $since_id];
+               $condition = ["`gravity` IN (?, ?) AND `iid` > ? AND `private` = ? AND `wall` AND NOT `author`.`hidden`",
+                       GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, Item::PUBLIC];
 
                if ($max_id > 0) {
                        $condition[0] .= " AND `thread`.`iid` <= ?";
@@ -1745,8 +1746,8 @@ function api_statuses_public_timeline($type)
 
                $r = Item::inArray($statuses);
        } else {
-               $condition = ["`gravity` IN (?, ?) AND `id` > ? AND NOT `private` AND `wall` AND NOT `user`.`hidewall` AND `item`.`origin` AND NOT `author`.`hidden`",
-                       GRAVITY_PARENT, GRAVITY_COMMENT, $since_id];
+               $condition = ["`gravity` IN (?, ?) AND `id` > ? AND `private` = ? AND `wall` AND `item`.`origin` AND NOT `author`.`hidden`",
+                       GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, Item::PUBLIC];
 
                if ($max_id > 0) {
                        $condition[0] .= " AND `item`.`id` <= ?";
@@ -1811,8 +1812,8 @@ function api_statuses_networkpublic_timeline($type)
 
        $start = max(0, ($page - 1) * $count);
 
-       $condition = ["`uid` = 0 AND `gravity` IN (?, ?) AND `thread`.`iid` > ? AND NOT `private`",
-               GRAVITY_PARENT, GRAVITY_COMMENT, $since_id];
+       $condition = ["`uid` = 0 AND `gravity` IN (?, ?) AND `thread`.`iid` > ? AND `private` = ?",
+               GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, Item::PUBLIC];
 
        if ($max_id > 0) {
                $condition[0] .= " AND `thread`.`iid` <= ?";
@@ -2040,7 +2041,7 @@ function api_statuses_repeat($type)
        Logger::log('API: api_statuses_repeat: '.$id);
 
        $fields = ['body', 'title', 'attach', 'tag', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink'];
-       $item = Item::selectFirst($fields, ['id' => $id, 'private' => false]);
+       $item = Item::selectFirst($fields, ['id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED]]);
 
        if (DBA::isResult($item) && $item['body'] != "") {
                if (strpos($item['body'], "[/share]") !== false) {
@@ -3005,7 +3006,7 @@ function api_format_item($item, $type = "json", $status_user = null, $author_use
                'user' =>  $status_user,
                'friendica_author' => $author_user,
                'friendica_owner' => $owner_user,
-               'friendica_private' => $item['private'] == 1,
+               'friendica_private' => $item['private'] == Item::PRIVATE,
                //'entities' => NULL,
                'statusnet_html' => $converted["html"],
                'statusnet_conversation_id' => $item['parent'],
@@ -4733,13 +4734,8 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
                }
        }
 
-       if ($filetype == "") {
-               $filetype = Images::guessType($filename);
-       }
-       $imagedata = @getimagesize($src);
-       if ($imagedata) {
-               $filetype = $imagedata['mime'];
-       }
+       $filetype = Images::getMimeTypeBySource($src, $filename, $filetype);
+
        Logger::log(
                "File upload src: " . $src . " - filename: " . $filename .
                " - size: " . $filesize . " - type: " . $filetype,
@@ -5823,7 +5819,7 @@ function api_friendica_activity($type)
 
        $id = $_REQUEST['id'] ?? 0;
 
-       $res = Item::performLike($id, $verb);
+       $res = Item::performActivity($id, $verb);
 
        if ($res) {
                if ($type == "xml") {