From 6776c4e3f97e03d396ba4973cf5e444a64ce1a3d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 1 Jul 2018 00:15:11 -0400 Subject: [PATCH] Fix some undefined variable/index notices in tests --- include/api.php | 40 ++++++++++++++++++++++------------------ include/security.php | 4 ++-- src/Core/System.php | 1 + src/Network/Probe.php | 2 ++ tests/ApiTest.php | 3 ++- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/include/api.php b/include/api.php index 32fe6c651d..6d38be95b7 100644 --- a/include/api.php +++ b/include/api.php @@ -90,11 +90,15 @@ function api_source() } // Support for known clients that doesn't send a source name - if (strpos($_SERVER['HTTP_USER_AGENT'], "Twidere") !== false) { - return "Twidere"; + if (!empty($_SERVER['HTTP_USER_AGENT'])) { + if(strpos($_SERVER['HTTP_USER_AGENT'], "Twidere") !== false) { + return "Twidere"; + } + + logger("Unrecognized user-agent ".$_SERVER['HTTP_USER_AGENT'], LOGGER_DEBUG); } - logger("Unrecognized user-agent ".$_SERVER['HTTP_USER_AGENT'], LOGGER_DEBUG); + logger("Empty user-agent", LOGGER_DEBUG); return "api"; } @@ -193,8 +197,8 @@ function api_login(App $a) throw new UnauthorizedException("This API requires login"); } - $user = $_SERVER['PHP_AUTH_USER']; - $password = $_SERVER['PHP_AUTH_PW']; + $user = defaults($_SERVER, 'PHP_AUTH_USER', ''); + $password = defaults($_SERVER, 'PHP_AUTH_PW', ''); // allow "user@server" login (but ignore 'server' part) $at = strstr($user, "@", true); @@ -258,7 +262,7 @@ function api_check_method($method) if ($method == "*") { return true; } - return (strpos($method, $_SERVER['REQUEST_METHOD']) !== false); + return (stripos($method, defaults($_SERVER, 'REQUEST_METHOD', 'GET')) !== false); } /** @@ -298,7 +302,7 @@ function api_call(App $a) //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) { + if (!empty($info['auth']) && api_user() === false) { api_login($a); } @@ -659,7 +663,7 @@ function api_get_user(App $a, $contact_id = null) 'geo_enabled' => false, 'verified' => false, 'statuses_count' => 0, - 'lang' => '', + 'language' => '', 'contributors_enabled' => false, 'is_translator' => false, 'is_translation_enabled' => false, @@ -740,7 +744,7 @@ function api_get_user(App $a, $contact_id = null) 'geo_enabled' => false, 'verified' => true, 'statuses_count' => intval($countitems), - 'lang' => '', + 'language' => '', 'contributors_enabled' => false, 'is_translator' => false, 'is_translation_enabled' => false, @@ -773,13 +777,13 @@ function api_get_user(App $a, $contact_id = null) $link_color = PConfig::get($ret['uid'], 'frio', 'link_color'); $bgcolor = PConfig::get($ret['uid'], 'frio', 'background_color'); } - if (!$nav_bg) { + if (empty($nav_bg)) { $nav_bg = "#708fa0"; } - if (!$link_color) { + if (empty($link_color)) { $link_color = "#6fdbe8"; } - if (!$bgcolor) { + if (empty($bgcolor)) { $bgcolor = "#ededed"; } @@ -801,12 +805,12 @@ function api_get_user(App $a, $contact_id = null) */ function api_item_get_user(App $a, $item) { - $status_user = api_get_user($a, $item["author-id"]); + $status_user = api_get_user($a, defaults($item, 'author-id', null)); - $status_user["protected"] = $item["private"]; + $status_user["protected"] = defaults($item, 'private', 0); - if ($item['thr-parent'] == $item['uri']) { - $owner_user = api_get_user($a, $item["owner-id"]); + if (defaults($item, 'thr-parent', '') == defaults($item, 'uri', '')) { + $owner_user = api_get_user($a, defaults($item, 'author-id', null)); } else { $owner_user = $status_user; } @@ -1308,7 +1312,7 @@ function api_status_show($type) 'favorited' => $lastwall['starred'] ? true : false, 'retweeted' => false, 'possibly_sensitive' => false, - 'lang' => "", + 'language' => "", 'statusnet_html' => $converted["html"], 'statusnet_conversation_id' => $lastwall['parent'], 'external_url' => System::baseUrl() . "/display/" . $lastwall['guid'], @@ -2205,7 +2209,7 @@ function api_favorites_create_destroy($type) // for versioned api. /// @TODO We need a better global soluton $action_argv_id = 2; - if ($a->argv[1] == "1.1") { + if (count($a->argv) > 1 && $a->argv[1] == "1.1") { $action_argv_id = 3; } diff --git a/include/security.php b/include/security.php index e8a03ad0fe..dbba09172e 100644 --- a/include/security.php +++ b/include/security.php @@ -41,7 +41,7 @@ function new_cookie($time, $user = []) if ($user) { $value = json_encode(["uid" => $user["uid"], "hash" => cookie_hash($user), - "ip" => $_SERVER['REMOTE_ADDR']]); + "ip" => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')]); } else { $value = ""; } @@ -70,7 +70,7 @@ function authenticate_success($user_record, $login_initial = false, $interactive $_SESSION['page_flags'] = $user_record['page-flags']; $_SESSION['my_url'] = System::baseUrl() . '/profile/' . $user_record['nickname']; $_SESSION['my_address'] = $user_record['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3); - $_SESSION['addr'] = $_SERVER['REMOTE_ADDR']; + $_SESSION['addr'] = defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0'); $a->user = $user_record; diff --git a/src/Core/System.php b/src/Core/System.php index ded781da83..abc39e5a2a 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -72,6 +72,7 @@ class System extends BaseObject } } elseif (!in_array($func['function'], $ignore)) { $callstack[] = $func['function']; + $func['class'] = ''; $previous = $func; } } diff --git a/src/Network/Probe.php b/src/Network/Probe.php index 5f665814b4..8e44c8a50b 100644 --- a/src/Network/Probe.php +++ b/src/Network/Probe.php @@ -109,6 +109,7 @@ class Probe $redirects = 0; logger("Probing for ".$host, LOGGER_DEBUG); + $xrd = null; $ret = Network::curl($ssl_url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']); if ($ret['success']) { @@ -1510,6 +1511,7 @@ class Probe return false; } $feed = $ret['body']; + $dummy1 = $dummy2 = $dummy3 = null; $feed_data = Feed::import($feed, $dummy1, $dummy2, $dummy3, true); if (!$feed_data) { diff --git a/tests/ApiTest.php b/tests/ApiTest.php index c21f651d5f..4c63af7a91 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -629,7 +629,7 @@ class ApiTest extends DatabaseTest */ public function testApiRssExtra() { - $user_info = ['url' => 'user_url']; + $user_info = ['url' => 'user_url', 'language' => 'en']; $result = api_rss_extra($this->app, [], $user_info); $this->assertEquals($user_info, $result['$user']); $this->assertEquals($user_info['url'], $result['$rss']['alternate']); @@ -1073,6 +1073,7 @@ class ApiTest extends DatabaseTest 'width' => 666, 'height' => 666, 'tmp_name' => $this->getTempImage(), + 'name' => 'spacer.png', 'type' => 'image/png' ] ];