diff --git a/lib/Base.php b/lib/Base.php index 5e27d68..d1f141c 100644 --- a/lib/Base.php +++ b/lib/Base.php @@ -9,6 +9,9 @@ */ namespace OCA\UserExternal; +use OCP\Server; +use Psr\Log\LoggerInterface; + /** * Base class for external auth implementations that stores users * on their first login in a local table. @@ -23,6 +26,7 @@ namespace OCA\UserExternal; */ abstract class Base extends \OC\User\Backend { protected $backend = ''; + protected readonly LoggerInterface $logger; /** * Create new instance, set backend name @@ -31,6 +35,7 @@ abstract class Base extends \OC\User\Backend { */ public function __construct($backend) { $this->backend = $backend; + $this->logger = Server::get(LoggerInterface::class); } /** diff --git a/lib/BasicAuth.php b/lib/BasicAuth.php index fee7a06..f198059 100644 --- a/lib/BasicAuth.php +++ b/lib/BasicAuth.php @@ -37,14 +37,14 @@ class BasicAuth extends Base { ); $canary = get_headers($this->authUrl, 1, $context); if (!$canary) { - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: Not possible to connect to BasicAuth Url: '.$this->authUrl, ['app' => 'user_external'] ); return false; } if (!isset(array_change_key_case($canary, CASE_LOWER)['www-authenticate'])) { - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: Mis-configured BasicAuth Url: '.$this->authUrl.', provided URL does not do authentication!', ['app' => 'user_external'] ); @@ -61,7 +61,7 @@ class BasicAuth extends Base { $headers = get_headers($this->authUrl, 1, $context); if (!$headers) { - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: Not possible to connect to BasicAuth Url: '.$this->authUrl, ['app' => 'user_external'] ); @@ -82,7 +82,7 @@ class BasicAuth extends Base { $this->storeUser($uid); return $uid; case "3": - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: Too many redirects from BasicAuth Url: '.$this->authUrl, ['app' => 'user_external'] ); diff --git a/lib/FTP.php b/lib/FTP.php index 1aa7e38..c1c95e5 100644 --- a/lib/FTP.php +++ b/lib/FTP.php @@ -48,7 +48,7 @@ class FTP extends Base { */ public function checkPassword($uid, $password) { if (false === array_search($this->protocol, stream_get_wrappers())) { - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: Stream wrapper not available: ' . $this->protocol, ['app' => 'user_external'] ); diff --git a/lib/IMAP.php b/lib/IMAP.php index 2ad784f..1695bf3 100644 --- a/lib/IMAP.php +++ b/lib/IMAP.php @@ -71,7 +71,7 @@ class IMAP extends Base { $uid = $pieces[0]; } } else { - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: User has a wrong domain! Expecting: '.$this->domain, ['app' => 'user_external'] ); @@ -111,7 +111,7 @@ class IMAP extends Base { $errorcode === 28) { # This is not defined in PHP-8.x # 28: CURLE_OPERATION_TIMEDOUT - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: Could not connect to imap server via curl: ' . curl_strerror($errorcode), ['app' => 'user_external'] ); @@ -122,12 +122,12 @@ class IMAP extends Base { # 9: CURLE_REMOTE_ACCESS_DENIED # 67: CURLE_LOGIN_DENIED # 94: CURLE_AUTH_ERROR) - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: IMAP Login failed via curl: ' . curl_strerror($errorcode), ['app' => 'user_external'] ); } else { - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: IMAP server returned an error: ' . $errorcode . ' / ' . curl_strerror($errorcode), ['app' => 'user_external'] ); diff --git a/lib/SMB.php b/lib/SMB.php index ec6e7e7..ac4bda9 100644 --- a/lib/SMB.php +++ b/lib/SMB.php @@ -43,7 +43,7 @@ class SMB extends Base { $command = self::SMBCLIENT.' '.escapeshellarg('//' . $this->host . '/dummy').' -U '.$uidEscaped.'%'.$password; $lastline = exec($command, $output, $retval); if ($retval === 127) { - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: smbclient executable missing', ['app' => 'user_external'] ); @@ -56,7 +56,7 @@ class SMB extends Base { goto login; } elseif ($retval !== 0) { //some other error - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: smbclient error: ' . trim($lastline), ['app' => 'user_external'] ); diff --git a/lib/SSH.php b/lib/SSH.php index 0a95a3b..ea5b09f 100644 --- a/lib/SSH.php +++ b/lib/SSH.php @@ -44,7 +44,7 @@ class SSH extends Base { */ public function checkPassword($uid, $password) { if (!extension_loaded('ssh2')) { - \OC::$server->getLogger()->error( + $this->logger->error( 'ERROR: php-ssh2 PECL module missing', ['app' => 'user_external'] ); diff --git a/lib/WebDavAuth.php b/lib/WebDavAuth.php index 73c0556..92876ca 100644 --- a/lib/WebDavAuth.php +++ b/lib/WebDavAuth.php @@ -27,14 +27,14 @@ class WebDavAuth extends Base { public function checkPassword($uid, $password) { $arr = explode('://', $this->webDavAuthUrl, 2); if (! isset($arr) or count($arr) !== 2) { - \OC::$server->getLogger()->error('ERROR: Invalid WebdavUrl: "'.$this->webDavAuthUrl.'" ', ['app' => 'user_external']); + $this->logger->error('ERROR: Invalid WebdavUrl: "'.$this->webDavAuthUrl.'" ', ['app' => 'user_external']); return false; } list($protocol, $path) = $arr; $url = $protocol.'://'.urlencode($uid).':'.urlencode($password).'@'.$path; $headers = get_headers($url); if ($headers === false) { - \OC::$server->getLogger()->error('ERROR: Not possible to connect to WebDAV Url: "'.$protocol.'://'.$path.'" ', ['app' => 'user_external']); + $this->logger->error('ERROR: Not possible to connect to WebDAV Url: "'.$protocol.'://'.$path.'" ', ['app' => 'user_external']); return false; } $returnCode = substr($headers[0], 9, 3);