]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Merge pull request #3365 from Hypolite/issue/remove-wrong-profile-link-for-contacts
[friendica.git] / include / api.php
index af719521c2fb51ccd4ad4d8a9616a37e12050b19..97c09c20a907acf8dc41c2e052bd8bc239f936e3 100644 (file)
@@ -155,8 +155,6 @@ $called_api = null;
                        logger($e);
                }
 
-
-
                // workaround for HTTP-auth in CGI mode
                if (x($_SERVER, 'REDIRECT_REMOTE_USER')) {
                        $userpass = base64_decode(substr($_SERVER["REDIRECT_REMOTE_USER"], 6)) ;
@@ -167,7 +165,7 @@ $called_api = null;
                        }
                }
 
-               if (!isset($_SERVER['PHP_AUTH_USER'])) {
+               if (!x($_SERVER, 'PHP_AUTH_USER')) {
                        logger('API_login: ' . print_r($_SERVER,true), LOGGER_DEBUG);
                        header('WWW-Authenticate: Basic realm="Friendica"');
                        throw new UnauthorizedException("This API requires login");
@@ -282,6 +280,8 @@ $called_api = null;
 
                                        $called_api = explode("/", $p);
                                        //unset($_SERVER['PHP_AUTH_USER']);
+
+                                       /// @TODO should be "true ==[=] $info['auth']", if you miss only one = character, you assign a variable (only with ==). Let's make all this even.
                                        if ($info['auth'] === true && api_user() === false) {
                                                api_login($a);
                                        }
@@ -353,7 +353,7 @@ $called_api = null;
                                                        header ("Content-Type: application/json");
                                                        foreach ($r as $rr)
                                                                $json = json_encode($rr);
-                                                               if ($_GET['callback']) {
+                                                               if (x($_GET, 'callback')) {
                                                                        $json = $_GET['callback'] . "(" . $json . ")";
                                                                }
                                                                return $json;
@@ -770,7 +770,11 @@ $called_api = null;
                                                ($item["deny_gid"] != "") OR
                                                $item["private"]);
 
-               $owner_user = api_get_user($a, $item["owner-link"]);
+               if ($item['thr-parent'] == $item['uri']) {
+                       $owner_user = api_get_user($a, $item["owner-link"]);
+               } else {
+                       $owner_user = $status_user;
+               }
 
                return (array($status_user, $owner_user));
        }
@@ -954,10 +958,10 @@ $called_api = null;
         * get data from $_POST or $_GET
         */
        function requestdata($k) {
-               if (isset($_POST[$k])) {
+               if (x($_POST, $k)) {
                        return $_POST[$k];
                }
-               if (isset($_GET[$k])) {
+               if (x($_GET, $k)) {
                        return $_GET[$k];
                }
                return null;
@@ -1390,7 +1394,7 @@ $called_api = null;
                unset($user_info["uid"]);
                unset($user_info["self"]);
 
-               return  api_format_data("user", $type, array('user' => $user_info));
+               return api_format_data("user", $type, array('user' => $user_info));
 
        }
 
@@ -1603,7 +1607,7 @@ $called_api = null;
                                break;
                }
 
-               return  api_format_data("statuses", $type, $data);
+               return api_format_data("statuses", $type, $data);
        }
 
        /// @TODO move to top of file or somewhere better
@@ -1672,7 +1676,7 @@ $called_api = null;
                        return api_format_data("statuses", $type, $data);
                } else {
                        $data = array('status' => $ret[0]);
-                       return  api_format_data("status", $type, $data);
+                       return api_format_data("status", $type, $data);
                }
        }
 
@@ -2032,7 +2036,7 @@ $called_api = null;
                                break;
                }
 
-               return  api_format_data("statuses", $type, $data);
+               return api_format_data("statuses", $type, $data);
        }
 
        /// @TODO move to top of file or somwhere better
@@ -2183,7 +2187,7 @@ $called_api = null;
                                $data = api_rss_extra($a, $data, $user_info);
                }
 
-               return  api_format_data("statuses", $type, $data);
+               return api_format_data("statuses", $type, $data);
        }
 
        /// @TODO move to top of file or somwhere better
@@ -2224,7 +2228,7 @@ $called_api = null;
                } else {
                        $ret['text'] = $item['title'] . "\n" . html2plain(bbcode(api_clean_plain_items($item['body']), false, false, 2, true), 0);
                }
-               if (x($_GET, 'getUserObjects']) && $_GET['getUserObjects'] == 'false') {
+               if (x($_GET, 'getUserObjects') && $_GET['getUserObjects'] == 'false') {
                        unset($ret['sender']);
                        unset($ret['recipient']);
                }
@@ -2642,20 +2646,22 @@ $called_api = null;
                foreach ($r as $item) {
 
                        localize_item($item);
-                       list($status_user, $owner_user) = api_item_get_user($a,$item);
+                       list($status_user, $owner_user) = api_item_get_user($a, $item);
 
                        // Look if the posts are matching if they should be filtered by user id
-                       if ($filter_user AND ($status_user["id"] != $user_info["id"]))
+                       if ($filter_user AND ($status_user["id"] != $user_info["id"])) {
                                continue;
+                       }
 
                        $in_reply_to = api_in_reply_to($item);
 
                        $converted = api_convert_item($item);
 
-                       if ($type == "xml")
+                       if ($type == "xml") {
                                $geo = "georss:point";
-                       else
+                       } else {
                                $geo = "geo";
+                       }
 
                        $status = array(
                                'text'          => $converted["text"],
@@ -2706,7 +2712,7 @@ $called_api = null;
                                if ($retweeted_item !== false) {
                                        $retweeted_status = $status;
                                        try {
-                                               $retweeted_status["user"] = api_get_user($a,$retweeted_item["author-link"]);
+                                               $retweeted_status["user"] = api_get_user($a, $retweeted_item["author-link"]);
                                        } catch( BadRequestException $e ) {
                                                // user not found. should be found?
                                                /// @todo check if the user should be always found
@@ -2828,14 +2834,17 @@ $called_api = null;
                        return false;
                }
 
-               if ($qtype == 'friends')
+               if ($qtype == 'friends') {
                        $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
-               if ($qtype == 'followers')
+               }
+               if ($qtype == 'followers') {
                        $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
+               }
 
                // friends and followers only for self
-               if ($user_info['self'] == 0)
+               if ($user_info['self'] == 0) {
                        $sql_extra = " AND false ";
+               }
 
                $r = q("SELECT `nurl` FROM `contact` WHERE `uid` = %d AND NOT `self` AND (NOT `blocked` OR `pending`) $sql_extra",
                        intval(api_user())
@@ -2848,31 +2857,35 @@ $called_api = null;
                        unset($user["uid"]);
                        unset($user["self"]);
 
-                       if ($user)
+                       if ($user) {
                                $ret[] = $user;
+                       }
                }
 
                return array('user' => $ret);
 
        }
+
        function api_statuses_friends($type) {
                $data =  api_statuses_f($type, "friends");
-               if ($data===false) return false;
-               return  api_format_data("users", $type, $data);
+               if ($data === false) {
+                       return false;
+               }
+               return api_format_data("users", $type, $data);
        }
+
        function api_statuses_followers($type) {
                $data = api_statuses_f($type, "followers");
-               if ($data===false) return false;
-               return  api_format_data("users", $type, $data);
+               if ($data === false) {
+                       return false;
+               }
+               return api_format_data("users", $type, $data);
        }
+
+       /// @TODO move to top of file or somewhere better
        api_register_func('api/statuses/friends','api_statuses_friends',true);
        api_register_func('api/statuses/followers','api_statuses_followers',true);
 
-
-
-
-
-
        function api_statusnet_config($type) {
 
                $a = get_app();
@@ -2908,6 +2921,8 @@ $called_api = null;
                return api_format_data('config', $type, array('config' => $config));
 
        }
+
+       /// @TODO move to top of file or somewhere better
        api_register_func('api/gnusocial/config','api_statusnet_config', false);
        api_register_func('api/statusnet/config','api_statusnet_config', false);
 
@@ -2917,6 +2932,8 @@ $called_api = null;
 
                return api_format_data('version', $type, array('version' => $fake_statusnet_version));
        }
+
+       /// @TODO move to top of file or somewhere better
        api_register_func('api/gnusocial/version','api_statusnet_version', false);
        api_register_func('api/statusnet/version','api_statusnet_version', false);
 
@@ -2957,12 +2974,13 @@ $called_api = null;
                }
 
                $ids = array();
-               foreach ($r as $rr)
-                       if ($stringify_ids)
+               foreach ($r as $rr) {
+                       if ($stringify_ids) {
                                $ids[] = $rr['id'];
                        } else {
                                $ids[] = intval($rr['id']);
                        }
+               }
 
                return api_format_data("ids", $type, array('id' => $ids));
        }
@@ -2970,14 +2988,15 @@ $called_api = null;
        function api_friends_ids($type) {
                return api_ff_ids($type,'friends');
        }
+
        function api_followers_ids($type) {
                return api_ff_ids($type,'followers');
        }
 
+       /// @TODO move to top of file or somewhere better
        api_register_func('api/friends/ids','api_friends_ids',true);
        api_register_func('api/followers/ids','api_followers_ids',true);
 
-
        function api_direct_messages_new($type) {
 
                $a = get_app();
@@ -3033,10 +3052,11 @@ $called_api = null;
                                $data = api_rss_extra($a, $data, $user_info);
                }
 
-               return  api_format_data("direct-messages", $type, $data);
+               return api_format_data("direct-messages", $type, $data);
 
        }
 
+       /// @TODO move to top of file or somewhere better
        api_register_func('api/direct_messages/new','api_direct_messages_new',true, API_METHOD_POST);
 
        /**
@@ -3109,8 +3129,9 @@ $called_api = null;
                /// @todo return JSON data like Twitter API not yet implemented
 
        }
-       api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', true, API_METHOD_DELETE);
 
+       /// @TODO move to top of file or somewhere better
+       api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', true, API_METHOD_DELETE);
 
        function api_direct_messages_box($type, $box, $verbose) {
 
@@ -3198,7 +3219,7 @@ $called_api = null;
                                $data = api_rss_extra($a, $data, $user_info);
                }
 
-               return  api_format_data("direct-messages", $type, $data);
+               return api_format_data("direct-messages", $type, $data);
 
        }
 
@@ -3206,14 +3227,17 @@ $called_api = null;
                $verbose = (x($_GET, 'friendica_verbose') ? strtolower($_GET['friendica_verbose']) : "false");
                return api_direct_messages_box($type, "sentbox", $verbose);
        }
+
        function api_direct_messages_inbox($type) {
                $verbose = (x($_GET, 'friendica_verbose') ? strtolower($_GET['friendica_verbose']) : "false");
                return api_direct_messages_box($type, "inbox", $verbose);
        }
+
        function api_direct_messages_all($type) {
                $verbose = (x($_GET, 'friendica_verbose') ? strtolower($_GET['friendica_verbose']) : "false");
                return api_direct_messages_box($type, "all", $verbose);
        }
+
        function api_direct_messages_conversation($type) {
                $verbose = (x($_GET, 'friendica_verbose') ? strtolower($_GET['friendica_verbose']) : "false");
                return api_direct_messages_box($type, "conversation", $verbose);
@@ -3259,7 +3283,7 @@ $called_api = null;
 
                $r = q("SELECT `resource-id`, MAX(`scale`) AS `scale`, `album`, `filename`, `type`
                                FROM `photo`
-                               WHERE `uid` = %d AND `album` != 'Contact Photos' GROUP BY `resource-id`",
+                               WHERE `uid` = %d AND `album` != 'Contact Photos' GROUP BY `resource-id`, `album`, `filename`, `type`",
                        intval(local_user())
                );
                $typetoext = array(
@@ -3285,7 +3309,7 @@ $called_api = null;
                                }
                        }
                }
-               return  api_format_data("photos", $type, $data);
+               return api_format_data("photos", $type, $data);
        }
 
        function api_fr_photo_detail($type) {
@@ -3296,12 +3320,15 @@ $called_api = null;
                }
 
                $scale = (x($_REQUEST, 'scale') ? intval($_REQUEST['scale']) : false);
-               $scale_sql = ($scale === false ? "" : sprintf("and scale=%d", intval($scale)));
-               $data_sql = ($scale === false ? "" : "data, ");
-
-               $r = q("SELECT %s `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`,
-                                               `type`, `height`, `width`, `datasize`, `profile`, MIN(`scale`) AS `minscale`, MAX(`scale`) AS `maxscale`
-                               FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' %s GROUP BY `resource-id`",
+               $scale_sql = ($scale === false ? "" : sprintf("AND `scale`=%d",intval($scale)));
+               $data_sql = ($scale === false ? "" : "ANY_VALUE(`data`) AS data`,");
+
+               $r = q("SELECT %s ANY_VALUE(`resource-id`) AS `resource-id`, ANY_VALUE(`created`) AS `created`,
+                               ANY_VALUE(`edited`) AS `edited`, ANY_VALUE(`title`) AS `title`, ANY_VALUE(`desc`) AS `desc`,
+                               ANY_VALUE(`album`) AS `album`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
+                               ANY_VALUE(`height`) AS `height`, ANY_VALUE(`width`) AS `width`, ANY_VALUE(`datasize`) AS `datasize`,
+                               ANY_VALUE(`profile`) AS `profile`, min(`scale`) as minscale, max(`scale`) as maxscale
+                               FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' %s",
                        $data_sql,
                        intval(local_user()),
                        dbesc($_REQUEST['photo_id']),