diff --git a/lib/Provider/BingWallpaperDaily.php b/lib/Provider/BingWallpaperDaily.php index 45ef888..8f49121 100644 --- a/lib/Provider/BingWallpaperDaily.php +++ b/lib/Provider/BingWallpaperDaily.php @@ -84,6 +84,6 @@ public function getRandomImageUrlBySearchTerm($search, $size) } // Return default image if no Bing image is found - return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size); + return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "Nextcloud"))->getRandomImageUrl($size); } -} \ No newline at end of file +} diff --git a/lib/Provider/UnsplashAPI.php b/lib/Provider/UnsplashAPI.php index f011604..4fa4232 100644 --- a/lib/Provider/UnsplashAPI.php +++ b/lib/Provider/UnsplashAPI.php @@ -120,7 +120,7 @@ public function getRandomImageUrlBySearchTerm($search, $size): string if($token === '' && $this->requiresAuth()) { // If the token is empty, return the default image. $this->logger->alert("Unsplash API: the provided token was blank!"); - return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size); + return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "Nextcloud"))->getRandomImageUrl($size); } $url = "https://api.unsplash.com/photos/random?client_id=" . $this->getToken() . "&count=1&query=" . $search; diff --git a/lib/Provider/WallhavenCC.php b/lib/Provider/WallhavenCC.php index 3f56026..789cf88 100644 --- a/lib/Provider/WallhavenCC.php +++ b/lib/Provider/WallhavenCC.php @@ -79,7 +79,7 @@ public function getRandomImageUrlBySearchTerm($search, $size) $this->logger->alert("Your searchterms likely did not yield results for: ".$this->getName()); } - return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size); + return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "Nextcloud"))->getRandomImageUrl($size); } public function getCachedImageURL(): string diff --git a/lib/Provider/WikimediaCommons.php b/lib/Provider/WikimediaCommons.php index f084d81..6780455 100644 --- a/lib/Provider/WikimediaCommons.php +++ b/lib/Provider/WikimediaCommons.php @@ -64,6 +64,7 @@ public function getRandomImageUrlBySearchTerm($search, $size) $url .= '&format=json'; $curl = curl_init($url); + curl_setopt($curl, CURLOPT_USERAGENT, $this->getUserAgent()); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($curl); $json = json_decode($response, true); @@ -75,7 +76,7 @@ public function getRandomImageUrlBySearchTerm($search, $size) $this->logger->alert("Your searchterms likely did not yield results for: ".$this->getName()); } - return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size); + return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "Nextcloud"))->getRandomImageUrl($size); } public function getCachedImageURL(): string diff --git a/lib/Provider/WikimediaCommonsDaily.php b/lib/Provider/WikimediaCommonsDaily.php index 2ca37c2..5180d04 100644 --- a/lib/Provider/WikimediaCommonsDaily.php +++ b/lib/Provider/WikimediaCommonsDaily.php @@ -58,6 +58,7 @@ public function getRandomImageUrlBySearchTerm($search, $size) $url .= '&format=json'; $curl = curl_init($url); + curl_setopt($curl, CURLOPT_USERAGENT, $this->getUserAgent()); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($curl); $json = json_decode($response, true); @@ -69,7 +70,7 @@ public function getRandomImageUrlBySearchTerm($search, $size) $this->logger->alert("Your searchterms likely did not yield results for: ".$this->getName()); } - return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size); + return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "Nextcloud"))->getRandomImageUrl($size); } public function getCachedImageURL(): string diff --git a/lib/ProviderHandler/Provider.php b/lib/ProviderHandler/Provider.php index 278c448..a5eb9b4 100644 --- a/lib/ProviderHandler/Provider.php +++ b/lib/ProviderHandler/Provider.php @@ -24,6 +24,7 @@ namespace OCA\Unsplash\ProviderHandler; +use OCP\App\IAppManager; use OCP\Files\IAppData; use OCP\IConfig; use Psr\Log\LoggerInterface; @@ -61,6 +62,7 @@ public function __construct( protected LoggerInterface $logger, protected IConfig $config, protected IAppData $appData, + protected IAppManager $appManager, protected string $providerName, ) { @@ -245,4 +247,28 @@ protected function getImageFolder() return $rootFolder; } + /** + * + * This returns the user agent string ('unsplash/ (; )'). + * Some image providers require a non-empty user agent, e.g. WikiMedia + * + * @return string + */ + protected function getUserAgent() + { + $info = $this->appManager->getAppInfo($this->appName); + $name = $this->appName; + $version = $this->appManager->getAppVersion($name); + $website = $info['website']; + $mail = ""; + + foreach ($info['author'] as $author) { + if(!isset($author['@attributes']['mail'])) { + continue; + } + $mail = "; ".$author['@attributes']['mail']; + } + + return $name."/".$version." (".$website.$mail.")"; + } } diff --git a/lib/ProviderHandler/ProviderDefinitions.php b/lib/ProviderHandler/ProviderDefinitions.php index cf8e1ee..01849f8 100644 --- a/lib/ProviderHandler/ProviderDefinitions.php +++ b/lib/ProviderHandler/ProviderDefinitions.php @@ -29,6 +29,7 @@ use OCA\Unsplash\Provider\WallhavenCC; use OCA\Unsplash\Provider\WikimediaCommons; use OCA\Unsplash\Provider\WikimediaCommonsDaily; +use OCP\App\IAppManager; use OCP\Files\IAppData; use OCP\IConfig; use Psr\Log\LoggerInterface; @@ -63,22 +64,23 @@ class ProviderDefinitions * @param IConfig $settings * @param IAppData $appData */ - function __construct($appName, LoggerInterface $logger, IConfig $config, IAppData $appData) + function __construct($appName, LoggerInterface $logger, IConfig $config, IAppData $appData, IAppManager $appManager) { $this->appName = $appName; $this->config = $config; $this->appData = $appData; $this->logger = $logger; + $this->appManager = $appManager; $tmp = []; //add all provider to this array. The logic takes care of the rest. - $tmp[] = new UnsplashAPI($this->appName, $logger, $this->config, $appData, "UnsplashAPI"); - $tmp[] = new NextcloudImage($this->appName, $logger, $this->config, $appData, "Nextcloud Image"); - $tmp[] = new WikimediaCommons($this->appName, $logger, $this->config, $appData, "WikimediaCommons"); - $tmp[] = new WikimediaCommonsDaily($this->appName, $logger, $this->config, $appData, "WikimediaCommons - Picture of the Day"); - $tmp[] = new WallhavenCC($this->appName, $logger, $this->config, $appData, "WallhavenCC"); - $tmp[] = new BingWallpaperDaily($this->appName, $logger, $this->config, $appData, "Bing Wallpaper - Picture of the Day"); + $tmp[] = new UnsplashAPI($this->appName, $logger, $this->config, $appData, $appManager, "UnsplashAPI"); + $tmp[] = new NextcloudImage($this->appName, $logger, $this->config, $appData, $appManager, "Nextcloud Image"); + $tmp[] = new WikimediaCommons($this->appName, $logger, $this->config, $appData, $appManager, "WikimediaCommons"); + $tmp[] = new WikimediaCommonsDaily($this->appName, $logger, $this->config, $appData, $appManager, "WikimediaCommons - Picture of the Day"); + $tmp[] = new WallhavenCC($this->appName, $logger, $this->config, $appData, $appManager, "WallhavenCC"); + $tmp[] = new BingWallpaperDaily($this->appName, $logger, $this->config, $appData, $appManager, "Bing Wallpaper - Picture of the Day"); foreach ($tmp as &$value) { $this->definitions[$value->getName()] = $value; @@ -96,11 +98,11 @@ function getProviderByName($name): Provider if (!array_key_exists($name, $this->definitions)) { $this->logger->warning("Selected provider '{$name}' could not be found. Using Default. Please select an existing provider in the settings!"); - return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, "WikimediaCommons - Picture of the Day"); + return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "WikimediaCommons - Picture of the Day"); } $provider = $this->definitions[$name]; if ($provider == null) { - return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, "WikimediaCommons - Picture of the Day"); + return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "WikimediaCommons - Picture of the Day"); } return $this->definitions[$name]; } diff --git a/lib/Services/SettingsService.php b/lib/Services/SettingsService.php index 4a19e71..4d743e7 100644 --- a/lib/Services/SettingsService.php +++ b/lib/Services/SettingsService.php @@ -9,6 +9,7 @@ use OCA\Unsplash\ProviderHandler\CachedProvider; use OCA\Unsplash\ProviderHandler\Provider; use OCA\Unsplash\ProviderHandler\ProviderDefinitions; +use OCP\App\IAppManager; use OCP\Files\IAppData; use OCP\IConfig; use Psr\Log\LoggerInterface; @@ -63,6 +64,11 @@ class SettingsService */ private $defaults; + /** + * @var IAppManager + */ + protected $appManager; + /** * FaviconService constructor. * @@ -73,7 +79,7 @@ class SettingsService * @param Defaults $defaults * @param LoggerInterface $logger */ - public function __construct($userId, $appName, IConfig $config, IAppData $appData, \OC_Defaults $defaults, LoggerInterface $logger) + public function __construct($userId, $appName, IConfig $config, IAppData $appData, \OC_Defaults $defaults, LoggerInterface $logger, IAppManager $appManager) { $this->config = $config; $this->userId = $userId; @@ -82,7 +88,7 @@ public function __construct($userId, $appName, IConfig $config, IAppData $appDat } $this->appName = $appName; - $this->providerDefinitions = new ProviderDefinitions($this->appName, $logger, $this->config, $appData); + $this->providerDefinitions = new ProviderDefinitions($this->appName, $logger, $this->config, $appData, $appManager); $this->defaults = $defaults; }