From dc57652519f248a2a7190990367f9eddc1add9cf Mon Sep 17 00:00:00 2001 From: Jannik Beyerstedt Date: Tue, 18 Jul 2023 22:28:38 +0200 Subject: [PATCH] PHP 8: Handle more errors explicitly The error control operator "@" has changed behavior in PHP8 and generally seems like a quite bad hack. So let's handle array/ dict access errors explicitly. --- config.php | 6 +++--- index.php | 11 ++++++----- lib/helper.php | 16 ++++++++++++++-- model/Conference.php | 2 +- model/ConferenceJson.php | 34 ++++++++++++++++++++-------------- model/Conferences.php | 5 +++-- model/Feedback.php | 3 ++- model/Schedule.php | 10 +++++++++- view/allclosed.php | 2 +- view/closed.php | 2 +- view/embed.php | 2 +- view/multiview.php | 2 +- 12 files changed, 62 insertions(+), 33 deletions(-) diff --git a/config.php b/config.php index 7c945423..5b00533a 100644 --- a/config.php +++ b/config.php @@ -24,11 +24,11 @@ $GLOBALS['CONFIG']['PREVIEW_DOMAIN'] = 'xlocalhost'; * Protokollfreie URLs (welche, die mit // beginnen), werden automatisch mit dem korrekten Protokoll ergänzt. * In diesem Fall wird auch ein SSL-Umschalt-Button im Header angezeigt */ -if(@$_SERVER['SERVER_NAME'] == 'localhost' || @$_SERVER['SERVER_NAME'] == '0.0.0.0') +if(isset($_SERVER['SERVER_NAME']) && ($_SERVER['SERVER_NAME'] == 'localhost' || $_SERVER['SERVER_NAME'] == '0.0.0.0')) { // keine Konfiguration -> BASEURL wird automatisch erraten } -else if(@$_SERVER['SERVER_NAME'] == 'streaming.test.c3voc.de') +else if(isset($_SERVER['SERVER_NAME']) && ($_SERVER['SERVER_NAME'] == 'streaming.test.c3voc.de')) { $GLOBALS['CONFIG']['BASEURL'] = '//streaming.test.c3voc.de/'; } @@ -104,7 +104,7 @@ $GLOBALS['CONFIG']['FEEDBACK'] = array( /** * Konfiguration der Room-Defaults * - * Falls in der Raum-Konfiguration innerhalb der Konferenz für diese Keys nichts definiert ist, + * Falls in der Raum-Konfiguration innerhalb der Konferenz für diese Keys nichts definiert ist, * fällt das System auf diese Werte zurück. */ diff --git a/index.php b/index.php index 2d5a5fd8..a53b9e0a 100644 --- a/index.php +++ b/index.php @@ -52,11 +52,11 @@ if(isset($argv) && isset($argv[1])) try { if(isset($_GET['htaccess'])) { - $route = @$_GET['route']; + $route = isset($_GET['route']) ? $_GET['route'] : ""; } elseif(isset($_SERVER["REQUEST_URI"])) { - $route = ltrim(@$_SERVER["REQUEST_URI"], '/'); + $route = ltrim($_SERVER["REQUEST_URI"], '/'); // serve static if($route != '' && file_exists($_SERVER["DOCUMENT_ROOT"].'/'.$route)) @@ -87,11 +87,12 @@ try { 'conference' => new GenericConference(), )); - if(isset($GLOBALS['CONFIG']['BASEURL']) && startswith('//', @$GLOBALS['CONFIG']['BASEURL'])) + if(isset($GLOBALS['CONFIG']['BASEURL']) && startswith('//', $GLOBALS['CONFIG']['BASEURL'])) { + $mandator = isset($GLOBALS['MANDATOR']) ? $GLOBALS['MANDATOR'] : ""; $tpl->set(array( - 'httpsurl' => forceslash(forceslash('https:'.$GLOBALS['CONFIG']['BASEURL']).@$GLOBALS['MANDATOR']).forceslash($route).url_params(), - 'httpurl' => forceslash(forceslash('http:'. $GLOBALS['CONFIG']['BASEURL']).@$GLOBALS['MANDATOR']).forceslash($route).url_params(), + 'httpsurl' => forceslash(forceslash('https:'.$GLOBALS['CONFIG']['BASEURL']).$mandator).forceslash($route).url_params(), + 'httpurl' => forceslash(forceslash('http:'. $GLOBALS['CONFIG']['BASEURL']).$mandator).forceslash($route).url_params(), )); } diff --git a/lib/helper.php b/lib/helper.php index 43c87481..b1c86672 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -244,8 +244,20 @@ function query_data($operation, $query, $variables = [], $assoc = false, $cache if (is_null($r)) { throw new NotFoundException(); } - + // TODO: add error handling? // TODO: should we return the cached value, when we did not get an answer? - return $assoc ? @$r['data'] : @$r->data; + if ($assoc) { + if (isset($r['data'])) { + return $r['data']; + } else { + throw new NotFoundException(); + } + } else { + if (isset($r->data)) { + return $r->data; + } else { + throw new NotFoundException(); + } + } } \ No newline at end of file diff --git a/model/Conference.php b/model/Conference.php index 971bccc4..41096408 100644 --- a/model/Conference.php +++ b/model/Conference.php @@ -20,7 +20,7 @@ class Conference extends ModelBase } public function isPreviewEnabled() { - if(@$GLOBALS['forceopen']) + if(isset($GLOBALS['forceopen']) && $GLOBALS['forceopen']) return true; if($this->has('PREVIEW_DOMAIN') && ($this->get('PREVIEW_DOMAIN') == $_SERVER['SERVER_NAME'])) diff --git a/model/ConferenceJson.php b/model/ConferenceJson.php index 89678f6f..409207f9 100644 --- a/model/ConferenceJson.php +++ b/model/ConferenceJson.php @@ -12,26 +12,32 @@ class ConferenceJson extends Conference $c = $json->conference; $this->start = DateTime::createFromFormat(DateTimeInterface::ISO8601, $c->start); $this->end = DateTime::createFromFormat(DateTimeInterface::ISO8601, $c->end); - $this->html = @$c->streamingConfig->html ?: []; + $this->html = isset($c->streamingConfig->html) ? $c->streamingConfig->html : []; $this->rooms = []; - $rooms = (is_array(@$c->rooms) ? $c->rooms : @$c->rooms->nodes) ?: []; + if (isset($c->rooms)) { + if (is_array($c->rooms)) { + $rooms = $c->rooms; + } else { + $rooms = isset($c->rooms->nodes) ? $c->rooms->nodes : []; + } + } foreach($rooms as $r) { if (!$r) { continue; } $this->rooms[$r->slug] = array_merge( ['stream' => $r->streamId], - get_object_vars($r), - @get_object_vars($r->streamingConfig) ?: [], - @get_object_vars($r->streamingConfig->chat) ?: [] + get_object_vars($r), + (isset($r->streamingConfig) ? get_object_vars($r->streamingConfig) : []), + (isset($r->streamingConfig->chat) ? get_object_vars($r->streamingConfig->chat) : []) ); } $groups = []; if ( isset($c->streamingConfig->overviewPage->sections) ) { - foreach(@$c->streamingConfig->overviewPage->sections as $s) { - $groups[@$s->title] = array_map( + foreach($c->streamingConfig->overviewPage->sections as $s) { + $groups[$s->title] = array_map( function($r) { return $r->slug; }, @$s->items ?: @$s->rooms ?: [] ); @@ -44,15 +50,15 @@ class ConferenceJson extends Conference $acronym = $mandator ?: $c->acronym; parent::__construct(array_merge( - @get_object_vars($c->streamingConfig) ?: [], - @get_object_vars($c->streamingConfig->features) ?: [], - @get_object_vars($c->streamingConfig->features->chat) ?: [], + isset($c->streamingConfig) ? get_object_vars($c->streamingConfig) : [], + isset($c->streamingConfig->features) ? get_object_vars($c->streamingConfig->features) : [], + isset($c->streamingConfig->features->chat) ? get_object_vars($c->streamingConfig->features->chat) : [], [ 'conference' => [ 'title' => $c->title, 'author' => $c->organizer, 'description' => $c->description, - 'keywords' => @implode(', ', $c->keywords), + 'keywords' => is_array($c->keywords) ? implode(', ', $c->keywords) : "", // future TODO: change structure "relive_json" => @$c->streamingConfig->features->relive !== false ? "https://cdn.c3voc.de/relive/".$acronym."/index.json" : null, "releases" => @$c->streamingConfig->features->releases !== false ? "https://media.ccc.de/c/".$acronym : null @@ -140,21 +146,21 @@ class ConferenceJson extends Conference return !empty($this->html->banner); } public function getBannerHtml() { - return @$this->html->banner; + return isset($this->html->banner) ? $this->html->banner : ""; } public function hasFooterHtml() { return !empty($this->html->footer); } public function getFooterHtml() { - return @$this->html->footer; + return isset($this->html->footer) ? $this->html->footer : ""; } public function hasNotStartedHtml() { return !empty($this->html->not_started); } public function getNotStartedHtml() { - return @$this->html->not_started; + return isset($this->html->not_started) ? $this->html->not_started : ""; } } diff --git a/model/Conferences.php b/model/Conferences.php index 911b49cc..f2d06a00 100644 --- a/model/Conferences.php +++ b/model/Conferences.php @@ -65,7 +65,8 @@ class Conferences } public static function getLastConference() { - return @Conferences::getFinishedConferencesSorted()[0]; + $conferences = Conferences::getFinishedConferencesSorted(); + return isset($conferences[0]) ? $conferences[0] : null; } public static function exists($mandator) { @@ -133,7 +134,7 @@ class Conferences } // config option for dynamic lookup feature defined below - if (!@$GLOBALS['CONFIG']['DYNAMIC_LOOKUP']) { + if (isset($GLOBALS['CONFIG']['DYNAMIC_LOOKUP']) && !$GLOBALS['CONFIG']['DYNAMIC_LOOKUP']) { throw new NotFoundException();; } diff --git a/model/Feedback.php b/model/Feedback.php index 7b603c94..fc154399 100644 --- a/model/Feedback.php +++ b/model/Feedback.php @@ -10,9 +10,10 @@ class Feedback } private function get($key) { + $global_feedback_elem = isset($GLOBALS['CONFIG']['FEEDBACK'][$key]) ? $GLOBALS['CONFIG']['FEEDBACK'][$key] : ""; return $this->conference->has(['FEEDBACK', $key]) ? $this->conference->get(['FEEDBACK', $key]) - : @$GLOBALS['CONFIG']['FEEDBACK'][$key]; + : $global_feedback_elem; } public function getConference() { diff --git a/model/Schedule.php b/model/Schedule.php index a95e2402..5b3465bc 100644 --- a/model/Schedule.php +++ b/model/Schedule.php @@ -364,7 +364,15 @@ class Schedule $this->mapping = array(); foreach($this->getConference()->get('ROOMS') as $slug => $room) { - $key = @$room['name'] ?: @$room['SCHEDULE_NAME'] ?: @$room['DISPLAY'] ?: $slug; + // json has 'name', config.php has 'SCHEDULE_NAME' and 'DISPLAY' + $key = $slug; + if (isset($room['name'])) { + $key = $room['name']; + } elseif ($room['SCHEDULE_NAME']) { + $key = $room['SCHEDULE_NAME']; + } elseif ($room['DISPLAY']) { + $key = $room['DISPLAY']; + } $this->mapping[$key] = $slug; } } diff --git a/view/allclosed.php b/view/allclosed.php index 535e6e3a..a3f87fde 100644 --- a/view/allclosed.php +++ b/view/allclosed.php @@ -6,7 +6,7 @@ echo $tpl->render(array( 'page' => 'allclosed', 'title' => 'See you soon … somewhere else!', - 'next' => @$events[0], + 'next' => isset($events[0]) ? $events[0] : null, 'events' => $events, 'last' => Conferences::getLastConference(), )); diff --git a/view/closed.php b/view/closed.php index 32a132e1..4ca0c449 100644 --- a/view/closed.php +++ b/view/closed.php @@ -6,6 +6,6 @@ echo $tpl->render(array( 'page' => 'closed', 'title' => 'See you soon … somewhere else!', - 'next' => @$events[0], + 'next' => isset($events[0]) ? $events[0] : null, 'events' => $events, )); diff --git a/view/embed.php b/view/embed.php index 2071c3a1..c0e38f4d 100644 --- a/view/embed.php +++ b/view/embed.php @@ -28,5 +28,5 @@ echo $tpl->render(array( 'room' => $room, 'stream' => $stream, - 'autoplay' => @$_GET['autoplay'], + 'autoplay' => isset($_GET['autoplay']) ? $_GET['autoplay'] : false, )); diff --git a/view/multiview.php b/view/multiview.php index 68259116..91011312 100644 --- a/view/multiview.php +++ b/view/multiview.php @@ -5,5 +5,5 @@ echo $tpl->render(array( 'title' => 'Stream-Übersicht', 'rooms' => $conference->getRooms(), - 'selection' => @$_GET['selection'], + 'selection' => isset($_GET['selection']) ? $_GET['selection'] : "", ));