Created
March 11, 2025 19:22
Revisions
-
pixiekat created this gist
Mar 11, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,194 @@ From 5b0bad9304c44d4f0a1ea25f920e9a0f4d669bbf Mon Sep 17 00:00:00 2001 From: Katie Elizabeth <32232155+pixiekat@users.noreply.github.com> Date: Tue, 11 Mar 2025 15:20:23 -0400 Subject: [PATCH] add try/catch --- src/Controller/SimpleAccessLog.php | 145 +++++++++++++++-------------- 1 file changed, 76 insertions(+), 69 deletions(-) diff --git a/src/Controller/SimpleAccessLog.php b/src/Controller/SimpleAccessLog.php index 707c362..d28efa7 100644 --- a/src/Controller/SimpleAccessLog.php +++ b/src/Controller/SimpleAccessLog.php @@ -25,95 +25,102 @@ class SimpleAccessLog extends ControllerBase implements ContainerInjectionInterf * config prevents logging this request. */ public static function logValues() { - // Load module settings. - $settings = \Drupal::config('simple_access_log.settings'); + try { + // Load module settings. + $settings = \Drupal::config('simple_access_log.settings'); - // Get the request itself, this should supply most of the needed values. - $request = \Drupal::request(); + // Get the request itself, this should supply most of the needed values. + $request = \Drupal::request(); - // Stop log if DNT Header present and option selected. - if($settings->get('respect_dnt') && $request->headers->get('dnt')){ - return FALSE; - } + // Stop log if DNT Header present and option selected. + if($settings->get('respect_dnt') && $request->headers->get('dnt')){ + return FALSE; + } - $access_log = []; - //request Time - $access_log['timestamp'] = \Drupal::time()->getRequestTime(); + $access_log = []; + //request Time + $access_log['timestamp'] = \Drupal::time()->getRequestTime(); - // User or Anon? - $access_log['uid'] = \Drupal::currentUser()->id(); - if(is_null($access_log['uid'])){ - $access_log['uid']=0; - } + // User or Anon? + $access_log['uid'] = \Drupal::currentUser()->id(); + if(is_null($access_log['uid'])){ + $access_log['uid']=0; + } - // skip logging user 0 if enabled - if ($settings->get('do_not_log_0') && $access_log['uid'] == 0) { - return FALSE; - } + // skip logging user 0 if enabled + if ($settings->get('do_not_log_0') && $access_log['uid'] == 0) { + return FALSE; + } - // Skip logging user 1 if enabled. - if ($settings->get('do_not_log_1') && $access_log['uid'] == 1) { - return FALSE; - } + // Skip logging user 1 if enabled. + if ($settings->get('do_not_log_1') && $access_log['uid'] == 1) { + return FALSE; + } - // Skip logging all users with administrator role if enabled. - if ($settings->get('do_not_log_admin')) { - $roles = \Drupal::currentUser()->getRoles(); - foreach ($roles as $rid => $role) { - if ($role == 'administrator') { - return FALSE; + // Skip logging all users with administrator role if enabled. + if ($settings->get('do_not_log_admin')) { + $roles = \Drupal::currentUser()->getRoles(); + foreach ($roles as $rid => $role) { + if ($role == 'administrator') { + return FALSE; + } } } - } - $exclude_or_include = (bool) $settings->get('exclude_or_include'); - - // Get the current path. - $access_log['path'] = $request->getPathInfo(); - // Check exclude paths. - if (!empty($settings->get('exclude_paths'))) { - $path_match = \Drupal::service('path.matcher')->matchPath($access_log['path'], $settings->get('exclude_paths')); - if ($exclude_or_include) { - //include only - if (!$path_match) { - return FALSE; - } - }else{ - //exclude - if ($path_match) { - return FALSE; + $exclude_or_include = (bool) $settings->get('exclude_or_include'); + + // Get the current path. + $access_log['path'] = $request->getPathInfo(); + // Check exclude paths. + if (!empty($settings->get('exclude_paths'))) { + $path_match = \Drupal::service('path.matcher')->matchPath($access_log['path'], $settings->get('exclude_paths')); + if ($exclude_or_include) { + //include only + if (!$path_match) { + return FALSE; + } + }else{ + //exclude + if ($path_match) { + return FALSE; + } } } - } - //Exclude Admin Paths - if ($settings->get('not_admin_paths') && substr($access_log['path'], 0, 7) == '/admin/') { - return FALSE; - } + //Exclude Admin Paths + if ($settings->get('not_admin_paths') && substr($access_log['path'], 0, 7) == '/admin/') { + return FALSE; + } - // Get the Client IP. - $access_log['remote_host'] = $request->getClientIp(); + // Get the Client IP. + $access_log['remote_host'] = $request->getClientIp(); - // Get hostname or host IP. - $access_log['host'] = (strlen($request->server->get('SERVER_NAME')) > 1 ? $request->server->get('SERVER_NAME') : $request->server->get('SERVER_ADDR')); + // Get hostname or host IP. + $access_log['host'] = (strlen($request->server->get('SERVER_NAME')) > 1 ? $request->server->get('SERVER_NAME') : $request->server->get('SERVER_ADDR')); - // Get Page Title from the current route. - if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) { - $access_log['title'] = (is_array(\Drupal::service('title_resolver')->getTitle($request, $route))?\Drupal::service('title_resolver')->getTitle($request, $route)['#markup']:\Drupal::service('title_resolver')->getTitle($request, $route)); - } + // Get Page Title from the current route. + if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) { + $access_log['title'] = (is_array(\Drupal::service('title_resolver')->getTitle($request, $route))?\Drupal::service('title_resolver')->getTitle($request, $route)['#markup']:\Drupal::service('title_resolver')->getTitle($request, $route)); + } - // Ge the URI requested. - $access_log['uri'] = $request->getRequestUri(); + // Ge the URI requested. + $access_log['uri'] = $request->getRequestUri(); - // Get the Referer variable. - $access_log['referer'] = $request->server->get('HTTP_REFERER'); + // Get the Referer variable. + $access_log['referer'] = $request->server->get('HTTP_REFERER'); - // Get the clietn's User Agent. - $access_log['user_agent'] = $request->server->get('HTTP_USER_AGENT'); + // Get the clietn's User Agent. + $access_log['user_agent'] = $request->server->get('HTTP_USER_AGENT'); - // Return our loaded access log array. - return $access_log; + // Return our loaded access log array. + return $access_log; + } + catch (\Exception $e) { + // If we have an exception, return false. + \Drupal::logger('simple_access_log')->error('Error in logValues: @error', ['@error' => $e->getMessage()]); + return FALSE; + } } /** @@ -131,4 +138,4 @@ class SimpleAccessLog extends ControllerBase implements ContainerInjectionInterf SimpleAccessLogDatabaseStorage::purgeOld($oldest); } -} \ No newline at end of file +} -- 2.48.1.windows.1