From aa548be3b95f1a4292f51e0758e80d759a9b14d1 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 1 Mar 2016 14:33:54 +0100 Subject: [PATCH 1/4] Introduced is_filled_array() + added some missing array initialization Signed-off-by: Roland Haeder --- boot.php | 4 ++++ mod/message.php | 10 ++++++---- mod/ping.php | 25 +++++++++++++------------ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/boot.php b/boot.php index d82669f231..bf98cf5c3a 100644 --- a/boot.php +++ b/boot.php @@ -2138,3 +2138,7 @@ function argv($x) { return ''; } + +function is_filled_array ($array) { + return (is_array($array) && count($array) > 0); +} diff --git a/mod/message.php b/mod/message.php index 734bf34710..6e176f7123 100644 --- a/mod/message.php +++ b/mod/message.php @@ -8,7 +8,7 @@ function message_init(&$a) { $tabs = ''; if ($a->argc >1 && is_numeric($a->argv[1])) { - $tabs = render_messages(get_messages(local_user(),0,5), 'mail_list.tpl'); + $tabs = render_messages(get_messages(local_user(),0,5), 'mail_list.tpl'); } $new = array( @@ -360,11 +360,13 @@ function message_content(&$a) { dbesc($myprofile) ); - if(count($r)) $a->set_pager_total($r[0]['total']); + if (is_filled_array($r)) { + $a->set_pager_total($r[0]['total']); + } $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']); - if(! count($r)) { + if(! is_filled_array($r)) { info( t('No messages.') . EOL); return $o; } @@ -542,7 +544,7 @@ function get_messages($user, $lstart, $lend) { ); } -function render_messages($msg, $t) { +function render_messages(array $msg, $t) { $a = get_app(); diff --git a/mod/ping.php b/mod/ping.php index 2eb94576b3..8deab28ed2 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -32,6 +32,8 @@ function ping_init(&$a) { $dislikes = array(); $friends = array(); $posts = array(); + $regs = array(); + $mails = array(); $home = 0; $network = 0; @@ -49,7 +51,7 @@ function ping_init(&$a) { intval(local_user()), intval(local_user()) ); - if(count($r)) { + if(is_filled_array($r)) { $arr = array('items' => $r); call_hooks('network_ping', $arr); @@ -116,8 +118,6 @@ function ping_init(&$a) { $intro = count($intros1) + count($intros2); $intros = $intros1+$intros2; - - $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ; $mails = q("SELECT * FROM `mail` WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ", @@ -150,7 +150,7 @@ function ping_init(&$a) { dbesc(datetime_convert('UTC','UTC','now')) ); - if($ev && count($ev)) { + if(is_filled_array($ev)) { $all_events = intval($ev[0]['total']); if($all_events) { @@ -219,7 +219,7 @@ function ping_init(&$a) { $home\r\n"; if ($register!=0) echo "$register"; - if (count($groups_unseen)) { + if ( is_filled_array($groups_unseen) ) { echo ''; foreach ($groups_unseen as $it) if ($it['count'] > 0) @@ -228,7 +228,7 @@ function ping_init(&$a) { echo ""; } - if (count($forums_unseen)) { + if ( is_filled_array($forums_unseen) ) { echo ''; foreach ($forums_unseen as $it) if ($it['count'] > 0) @@ -245,8 +245,8 @@ function ping_init(&$a) { $birthdays_today\r\n"; - if(count($notifs) && (! $sysnotify)) { - foreach($notifs as $zz) { + if (is_filled_array($notifs) && (! $sysnotify)) { + foreach ($notifs as $zz) { if($zz['seen'] == 0) $sysnotify ++; } @@ -255,7 +255,7 @@ function ping_init(&$a) { echo ' '; // merge all notification types in one array - if ($intro>0){ + if ( is_filled_array($intros) ) { foreach ($intros as $i) { $n = array( 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'], @@ -270,7 +270,7 @@ function ping_init(&$a) { } } - if ($mail>0){ + if ( is_filled_array($mails) ) { foreach ($mails as $i) { $n = array( 'href' => $a->get_baseurl().'/message/'.$i['id'], @@ -285,7 +285,7 @@ function ping_init(&$a) { } } - if ($register>0){ + if ( is_filled_array($regs) ) { foreach ($regs as $i) { $n = array( 'href' => $a->get_baseurl().'/admin/users/', @@ -299,6 +299,7 @@ function ping_init(&$a) { $notifs[] = $n; } } + // sort notifications by $[]['date'] $sort_function = function($a, $b) { $adate = date($a['date']); @@ -310,7 +311,7 @@ function ping_init(&$a) { }; usort($notifs, $sort_function); - if(count($notifs)) { + if( is_filled_array($notifs) ) { foreach($notifs as $n) { echo xmlize($n); } From 78e6569b27260b6cf156c372f22e90f6f122dd67 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 1 Mar 2016 14:38:02 +0100 Subject: [PATCH 2/4] Also use is_filled_array() here, too. if $contacts is FALSE count() issues are E_WARNING Signed-off-by: Roland Haeder --- include/ForumManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/ForumManager.php b/include/ForumManager.php index 6fede0204d..bbf881c9d0 100644 --- a/include/ForumManager.php +++ b/include/ForumManager.php @@ -86,7 +86,7 @@ class ForumManager { $total = count($contacts); $visible_forums = 10; - if(count($contacts)) { + if(is_filled_array($contacts)) { $id = 0; From 858438daec44cad20153b4160f7d1c9ddeac7484 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 1 Mar 2016 16:00:55 +0100 Subject: [PATCH 3/4] More calls replaced with is_filled_array(), ticket #2390 Signed-off-by: Roland Haeder --- boot.php | 10 +++++----- index.php | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/boot.php b/boot.php index bf98cf5c3a..b049099ea8 100644 --- a/boot.php +++ b/boot.php @@ -926,7 +926,7 @@ class App { } else { $r = q("SELECT `contact`.`avatar-date` AS picdate FROM `contact` WHERE `contact`.`thumb` like '%%/%s'", $common_filename); - if(! count($r)){ + if(! is_filled_array($r)){ $this->cached_profile_image[$avatar_image] = $avatar_image; } else { $this->cached_profile_picdate[$common_filename] = "?rev=".urlencode($r[0]['picdate']); @@ -1352,7 +1352,7 @@ function run_update_function($x) { function check_plugins(&$a) { $r = q("SELECT * FROM `addon` WHERE `installed` = 1"); - if(count($r)) + if(is_filled_array($r)) $installed = $r; else $installed = array(); @@ -1680,7 +1680,7 @@ function current_theme(){ $r = q("select theme from user where uid = %d limit 1", intval($a->profile_uid) ); - if($r) + if(is_filled_array($r)) $page_theme = $r[0]['theme']; } @@ -1793,7 +1793,7 @@ function feed_birthday($uid,$tz) { intval($uid) ); - if($p && count($p)) { + if(is_filled_array($p)) { $tmp_dob = substr($p[0]['dob'],5); if(intval($tmp_dob)) { $y = datetime_convert($tz,$tz,'now','Y'); @@ -1838,7 +1838,7 @@ function load_contact_links($uid) { $r = q("SELECT `id`,`network`,`url`,`thumb`, `rel` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `thumb` != ''", intval($uid) ); - if(count($r)) { + if(is_filled_array($r)) { foreach($r as $rr){ $url = normalise_link($rr['url']); $ret[$url] = $rr; diff --git a/index.php b/index.php index e364389b2c..e03d38a0c2 100644 --- a/index.php +++ b/index.php @@ -107,7 +107,7 @@ if (x($_SESSION,'authenticated') && !x($_SESSION,'language')) { // we didn't loaded user data yet, but we need user language $r = q("SELECT language FROM user WHERE uid=%d", intval($_SESSION['uid'])); $_SESSION['language'] = $lang; - if (count($r)>0) $_SESSION['language'] = $r[0]['language']; + if (is_filled_array($r)) $_SESSION['language'] = $r[0]['language']; } if((x($_SESSION,'language')) && ($_SESSION['language'] !== $lang)) { From b56a9bfaccbd22a632f71af96120b35fb3246547 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 4 Mar 2016 22:46:30 +0100 Subject: [PATCH 4/4] Fixes E_WARNING from foreach() because count() seem to return TRUE even when $r is no array ?! Signed-off-by: Roland Haeder --- boot.php | 1 + 1 file changed, 1 insertion(+) diff --git a/boot.php b/boot.php index b049099ea8..ea9f7f9e0c 100644 --- a/boot.php +++ b/boot.php @@ -1838,6 +1838,7 @@ function load_contact_links($uid) { $r = q("SELECT `id`,`network`,`url`,`thumb`, `rel` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `thumb` != ''", intval($uid) ); + if(is_filled_array($r)) { foreach($r as $rr){ $url = normalise_link($rr['url']);