Jump to content


Photo

Birthdays hook can throw foreach loop error


If a user doesn't have access to any calendars, the birthday hook code can cause

Warning: Invalid argument supplied for foreach() in /admin/applications_addon/ips/calendar/sources/hooks.php on line 151



            foreach( $calendars as $calendar )
            {
                /* We need to grab first calendar that allows birthdays */
                if( !$calendar['cal_bday_limit'] )
                {
                    continue;
                }

                $data            = array(
                                        'id'    => $calendar['cal_id'],
                                        'title'    => $calendar['cal_title_seo'],
                                        'year'    => $year,
                                        'month'    => $month,
                                        'day'    => $day,
                                        );

                break;
            }

To

            if (! empty($calendars) )
            {
            foreach( $calendars as $calendar )
            {
                /* We need to grab first calendar that allows birthdays */
                if( !$calendar['cal_bday_limit'] )
                {
                    continue;
                }

                $data            = array(
                                        'id'    => $calendar['cal_id'],
                                        'title'    => $calendar['cal_title_seo'],
                                        'year'    => $year,
                                        'month'    => $month,
                                        'day'    => $day,
                                        );

                break;
            }
            }

Seems to clear it.. I went with ! empty because count and if ($calendars) has given me fits.. Any should probably work though.

Status: Fixed
Version: 3.3.0
Fixed In: 3.3.1


7 Comments

Yes, on two of my boards when viewing as a guest without calendar perms.
As mentioned on AIM, I fixed this in a different way. In functions.php I changed

protected $calendars;

to

protected $calendars    = array();

This should resolve the warning, but has the secondary benefit that it ensures the returned data type from a call to getCalendars() is consistent for other methods that are calling it (i.e. to prevent potential similar bugs elsewhere).
I just encountered this problem on IPB 3.3.1 with the latest Calendar 3.3.0. Please fix it properly!
Posted Image
How is this still happening with 3-3-2?
We have released IP.Board 3.3.2 but not Calendar 3.3.1 (it's still on 3.3.0) ;)
I'm still not used to this not being part of the board download, thanks Tera :)