diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 5d8773c7..e073223a 100755 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -7,6 +7,8 @@ use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Http\Request; use Illuminate\Routing\Controller as BaseController; +use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\View; @@ -16,7 +18,9 @@ class Controller extends BaseController use AuthorizesRequests, DispatchesJobs, ValidatesRequests; protected $API_URL; - + protected $API_HOST; + + private function getDataFromFileAndConvert($file, $path, $class, $maxItems = 0) { $data = json_decode(Storage::get($file)); @@ -38,6 +42,7 @@ class Controller extends BaseController { View::share('apiUrl', $this->API_URL = env('API_URL', 'http://api.6fm.nl/')); View::share('imgBase', env('IMAGE_BASE_URL', '/')); + $this->API_HOST = env('API_HOST', $this->API_URL); //View::share('onAir', file_get_contents(url('onair'))); View::composer('widgets.laatstenieuws', function ($view) { @@ -92,7 +97,12 @@ class Controller extends BaseController ); }); View::composer('widgets.menu', function($view) { - $view->with('items', json_decode(Storage::get('static/menu.json'))); + try { + $menuData = json_decode(Storage::get('static/menu.json')); + $view->with('items', $menuData ?? []); + } catch (\Exception $e) { + $view->with('items', []); + } }); View::share('disableBanners', env('DISABLE_BANNERS', false)); @@ -102,51 +112,73 @@ class Controller extends BaseController { if (config('app.env') == 'local') { return; - } + } - app('db')->insert( - 'INSERT INTO `pagestats`(`type`, `item_id`, `visitor_ip`, `session`, `referer`) VALUES(:type, :id, :ip, :session, :referer)', - [ - 'type' => $type, - 'id' => $id, - 'ip' => $request->server('REMOTE_ADDR'), - 'session' => md5(Session::getId()), - 'referer' => $request->server('HTTP_REFERRER') - ] - ); + // app('db')->insert( + // 'INSERT INTO `pagestats`(`type`, `item_id`, `visitor_ip`, `session`, `referer`) VALUES(:type, :id, :ip, :session, :referer)', + // [ + // 'type' => $type, + // 'id' => $id, + // 'ip' => $request->server('REMOTE_ADDR'), + // 'session' => md5(Session::getId()), + // 'referer' => $request->server('HTTP_REFERRER') + // ] + // ); } protected function API($url) { - $arrContextOptions = [ - 'ssl' => [ - "verify_peer" => false, - "verify_peer_name" => false, - ], - 'http' => [ - 'method' => 'GET', - 'header' => 'X-Api-Key: ' . sha1(request()->server('REMOTE_ADDR')) . "\r\n" - . 'X-User-Agent: ' . request()->server('HTTP_USER_AGENT') . "\r\n" - ] - ]; - //\dump($http_response_header); - $result = @file_get_contents($this->API_URL . $url, false, stream_context_create($arrContextOptions)); - return $result ? json_decode($result) : null; + try { + $response = Http::connectTimeout(5) + ->timeout(15) + ->withHeaders([ + 'Host' => $this->API_HOST, + 'X-Api-Key' => sha1(request()->server('REMOTE_ADDR')), + 'X-User-Agent' => request()->server('HTTP_USER_AGENT') + ]) + ->get($this->API_URL . $url); + + if ($response->successful()) { + $body = $response->body(); + return $body ? json_decode($body) : null; + } + Log::warning('API request failed', [ + 'url' => $this->API_URL . $url, + 'status' => $response->status() + ]); + return null; + } catch (\Exception $e) { + Log::error('API request error', [ + 'url' => $this->API_URL . $url, + 'error' => $e->getMessage() + ]); + return null; + } } protected function checkAPI($url) { - return $this->get_http_response_code($this->API_URL . $url); + return $this->getHttpResponseCode($url); } - protected function get_http_response_code($url) + protected function getHttpResponseCode($url) { - $headers = get_headers($url); - return substr($headers[0], 9, 3); + try { + $response = Http::connectTimeout(5) + ->timeout(15) + ->withHeaders([ + 'Host' => $this->API_HOST, + 'X-Api-Key' => sha1(request()->server('REMOTE_ADDR')), + 'X-User-Agent' => request()->server('HTTP_USER_AGENT') + ]) + ->head($this->API_URL . $url); + + return $response->status(); + } catch (\Exception $e) { + return null; + } } - - protected static function JsonToDateTime($obj) { return is_object($obj) ? new \DateTime($obj->date, new \DateTimeZone($obj->timezone)) : \Carbon\Carbon::parse($obj)->setTimezone(date_default_timezone_get()); @@ -169,13 +201,13 @@ class Controller extends BaseController public function getSidebareData() { $populair = []; - $apiResult = $this->API('nieuws/populair?aantal=5'); + $apiResult = (object) $this->API('nieuws/populair?aantal=5'); foreach ($apiResult->news as $_newsItem) { $populair[] = new \Model\NewsItem($_newsItem); } $newsItems = []; - $apiResult = $this->API('nieuws/overzicht?aantal=5'); + $apiResult = (object) $this->API('nieuws/overzicht?aantal=5'); foreach ($apiResult->news as $_newsItem) { $newsItems[] = new \Model\NewsItem($_newsItem); } @@ -189,13 +221,15 @@ class Controller extends BaseController return view($slug); } - $page = $this->API('statisch/' . $slug); - if ($page == null) { + $page = (object) $this->API('statisch/' . $slug); + if ($page == null || !isset($page->title)) { return abort(404); } - $page->published = Controller::JsonToDateTime($page->published); - if ($page->edited) { + if(isset($page->published)) { + $page->published = Controller::JsonToDateTime($page->published); + } + if (isset($page->edited) && $page->edited) { $page->edited = Controller::JsonToDateTime($page->edited); } diff --git a/config/logging.php b/config/logging.php new file mode 100644 index 00000000..f4f7fe97 --- /dev/null +++ b/config/logging.php @@ -0,0 +1,105 @@ + env('LOG_CHANNEL', 'stack'), + + 'deprecations' => env('LOG_DEPRECATIONS_CHANNEL', 'null'), + + /* + |-------------------------------------------------------------------------- + | Log Channels + |-------------------------------------------------------------------------- + | + | Here you may configure the log channels for your application. Out of + | the box, Laravel uses the Monolog PHP logging library. This gives + | you a variety of powerful log handlers / formatters to utilize. + | + | Available Drivers: "single", "daily", "slack", "syslog", + | "errorlog", "monolog", "custom", "stack" + | + */ + + 'channels' => [ + 'stack' => [ + 'driver' => 'stack', + 'channels' => ['single'], + 'ignore_exceptions' => false, + ], + + 'single' => [ + 'driver' => 'single', + 'path' => storage_path('logs/laravel.log'), + 'level' => env('LOG_LEVEL', 'debug'), + ], + + 'daily' => [ + 'driver' => 'daily', + 'path' => storage_path('logs/laravel.log'), + 'level' => env('LOG_LEVEL', 'debug'), + 'days' => 14, + ], + + 'slack' => [ + 'driver' => 'slack', + 'url' => env('LOG_SLACK_WEBHOOK_URL'), + 'username' => 'Laravel Log', + 'emoji' => ':boom:', + 'level' => env('LOG_LEVEL', 'critical'), + ], + + 'papertrail' => [ + 'driver' => 'monolog', + 'level' => env('LOG_LEVEL', 'debug'), + 'handler' => \Monolog\Handler\SyslogUdpHandler::class, + 'handler_with' => [ + 'host' => env('PAPERTRAIL_URL'), + 'port' => env('PAPERTRAIL_PORT'), + 'facility' => LOG_USER, + 'level' => env('LOG_LEVEL', 'debug'), + ], + ], + + 'stderr' => [ + 'driver' => 'monolog', + 'level' => env('LOG_LEVEL', 'debug'), + 'handler' => \Monolog\Handler\StreamHandler::class, + 'formatter' => env('LOG_STDERR_FORMATTER'), + 'with' => [ + 'stream' => 'php://stderr', + ], + ], + + 'syslog' => [ + 'driver' => 'syslog', + 'level' => env('LOG_LEVEL', 'debug'), + 'facility' => LOG_USER, + ], + + 'errorlog' => [ + 'driver' => 'errorlog', + 'level' => env('LOG_LEVEL', 'debug'), + ], + + 'null' => [ + 'driver' => 'monolog', + 'handler' => \Monolog\Handler\NullHandler::class, + ], + + 'emergency' => [ + 'path' => storage_path('logs/laravel.log'), + ], + ], + +];