Skip to content

Commit

Permalink
PHP 8: Handle more errors explicitly
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jbeyerstedt committed Oct 22, 2023
1 parent fcda8a1 commit 7c0ab40
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 33 deletions.
6 changes: 3 additions & 3 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
* 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/';
}
Expand Down Expand Up @@ -104,7 +104,7 @@
/**
* 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.
*/

Expand Down
11 changes: 6 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
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))
Expand Down Expand Up @@ -87,11 +87,12 @@
'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(),
));
}

Expand Down
16 changes: 14 additions & 2 deletions lib/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
2 changes: 1 addition & 1 deletion model/Conference.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getTitle() {
}

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']))
Expand Down
34 changes: 20 additions & 14 deletions model/ConferenceJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,32 @@ public function __construct($json, $mandator)
$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 ?: []
);
Expand All @@ -44,15 +50,15 @@ function($r) { return $r->slug; },
$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
Expand Down Expand Up @@ -140,21 +146,21 @@ public function hasBannerHtml() {
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 : "";
}

}
5 changes: 3 additions & 2 deletions model/Conferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public static function getFinishedConferencesSorted() {
}

public static function getLastConference() {
return @Conferences::getFinishedConferencesSorted()[0];
$conferences = Conferences::getFinishedConferencesSorted();
return isset($conferences[0]) ? $conferences[0] : null;
}

public static function exists($mandator) {
Expand Down Expand Up @@ -133,7 +134,7 @@ public static function loadConferenceConfig($mandator) {
}

// 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();;
}

Expand Down
3 changes: 2 additions & 1 deletion model/Feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public function __construct(Conference $conference)
}

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() {
Expand Down
10 changes: 9 additions & 1 deletion model/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,15 @@ public function getScheduleToRoomSlugMapping()
$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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion view/allclosed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'page' => 'allclosed',
'title' => 'See you soon … somewhere else!',

'next' => @$events[0],
'next' => isset($events[0]) ? $events[0] : null,
'events' => $events,
'last' => Conferences::getLastConference(),
));
2 changes: 1 addition & 1 deletion view/closed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
'page' => 'closed',
'title' => 'See you soon … somewhere else!',

'next' => @$events[0],
'next' => isset($events[0]) ? $events[0] : null,
'events' => $events,
));
2 changes: 1 addition & 1 deletion view/embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
'room' => $room,
'stream' => $stream,

'autoplay' => @$_GET['autoplay'],
'autoplay' => isset($_GET['autoplay']) ? $_GET['autoplay'] : false,
));
2 changes: 1 addition & 1 deletion view/multiview.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
'title' => 'Stream-Übersicht',

'rooms' => $conference->getRooms(),
'selection' => @$_GET['selection'],
'selection' => isset($_GET['selection']) ? $_GET['selection'] : "",
));

0 comments on commit 7c0ab40

Please sign in to comment.