diff --git a/appinfo/info.xml b/appinfo/info.xml index bee4b4e..5a5faea 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -7,7 +7,7 @@ 📸🔀☁️ Zufällige Hintergrundbilder von Unsplash Show a new random featured nature photo in your nextcloud. Now with choosable motives! Zeigt ein zufällig ausgewähltes Naturfoto in ihrer Nextcloud. Jetzt auch mit selbstwählbaren Motiven! - 3.1.0 + 3.2.0 agpl Jan C. Borchardt Marius Wieschollek @@ -22,8 +22,8 @@ https://raw.githubusercontent.com/nextcloud/unsplash/master/.meta/unsplash.jpg https://raw.githubusercontent.com/nextcloud/unsplash/master/.meta/unsplash-header.jpg - - + + OCA\Unsplash\Settings\AdminSettings diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 1476be9..c5d827d 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -11,7 +11,6 @@ use OCA\Unsplash\EventListener\AddContentSecurityPolicyEventListener; use OCA\Unsplash\EventListener\BeforeTemplateRenderedEventListener; -use OCA\Unsplash\Services\LegacyInitialisationService; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; diff --git a/lib/Controller/AdminSettingsController.php b/lib/Controller/AdminSettingsController.php index 71baaff..04628e7 100644 --- a/lib/Controller/AdminSettingsController.php +++ b/lib/Controller/AdminSettingsController.php @@ -70,10 +70,10 @@ public function set(string $key, bool|string|int|array|null $value): JSONRespons $this->settings->setServerStyleDashboardEnabled(0); } } else if ($key === 'provider/provider') { - $this->settings->setImageProviderSanitized(filter_var($value, FILTER_SANITIZE_STRING)); + $this->settings->setImageProviderSanitized((string)$value); return $this->generateProviderResponse($value); } else if ($key === 'provider/customization') { - $this->settings->setImageProviderCustomization(filter_var($value, FILTER_SANITIZE_STRING)); + $this->settings->setImageProviderCustomization((string)$value); } else if ($key === 'style/tint') { if ($value) { $this->settings->setTint(1); @@ -111,7 +111,11 @@ public function set(string $key, bool|string|int|array|null $value): JSONRespons */ public function getCustomization(string $providername): JSONResponse { - $provider = $this->settings->getImageProvider(filter_var($providername, FILTER_SANITIZE_STRING)); + $validProviders = $this->settings->getAllImageProvider(); + if (!in_array($providername, $validProviders, true)) { + return new JSONResponse(['status' => 'error', 'message' => 'Unknown provider'], Http::STATUS_BAD_REQUEST); + } + $provider = $this->settings->getImageProvider($providername); return new JSONResponse(['status' => 'ok', 'customization' => $provider->getCustomSearchterms()]); } diff --git a/lib/EventListener/AddContentSecurityPolicyEventListener.php b/lib/EventListener/AddContentSecurityPolicyEventListener.php index 910c148..5c11b68 100644 --- a/lib/EventListener/AddContentSecurityPolicyEventListener.php +++ b/lib/EventListener/AddContentSecurityPolicyEventListener.php @@ -9,7 +9,7 @@ namespace OCA\Unsplash\EventListener; -use OC\Security\CSP\ContentSecurityPolicy; +use OCP\AppFramework\Http\ContentSecurityPolicy; use OCA\Unsplash\Services\SettingsService; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; diff --git a/lib/EventListener/BeforeTemplateRenderedEventListener.php b/lib/EventListener/BeforeTemplateRenderedEventListener.php index 28706b1..8a9a2db 100644 --- a/lib/EventListener/BeforeTemplateRenderedEventListener.php +++ b/lib/EventListener/BeforeTemplateRenderedEventListener.php @@ -53,9 +53,6 @@ public function handle(Event $event): void switch ($route) { case 'core.TwoFactorChallenge.showChallenge': case 'files_sharing.Share.authenticate': - // Nextcloud <= 28 - case 'core.login.showLoginForm': - // Nextcloud >= 29 case 'core.login.showloginform': case 'files_sharing.Share.showAuthenticate': if ($serverstyleLogin) { diff --git a/lib/Provider/BingWallpaperDaily.php b/lib/Provider/BingWallpaperDaily.php index 45ef888..c47fbeb 100644 --- a/lib/Provider/BingWallpaperDaily.php +++ b/lib/Provider/BingWallpaperDaily.php @@ -22,7 +22,6 @@ namespace OCA\Unsplash\Provider; -use OC\AppFramework\Http\Request; use OCA\Unsplash\ProviderHandler\Provider; class BingWallpaperDaily extends Provider @@ -74,7 +73,7 @@ public function getRandomImageUrl($size) public function getRandomImageUrlBySearchTerm($search, $size) { // Fetch the daily image JSON from Bing - $bing_daily_image_json = file_get_contents('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'); + $bing_daily_image_json = $this->getData('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'); if ($bing_daily_image_json !== false) { $matches = json_decode($bing_daily_image_json); if (isset($matches->images[0]->url)) { @@ -84,6 +83,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, "Nextcloud", $this->clientService))->getRandomImageUrl($size); } } \ No newline at end of file diff --git a/lib/Provider/UnsplashAPI.php b/lib/Provider/UnsplashAPI.php index f011604..3706c30 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, "Nextcloud", $this->clientService))->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..82fe9ad 100644 --- a/lib/Provider/WallhavenCC.php +++ b/lib/Provider/WallhavenCC.php @@ -22,7 +22,6 @@ namespace OCA\Unsplash\Provider; -use OC\AppFramework\Http\Request; use OCA\Unsplash\ProviderHandler\Provider; class WallhavenCC extends Provider @@ -67,10 +66,8 @@ public function getRandomImageUrlBySearchTerm($search, $size) } - $curl = curl_init('https://wallhaven.cc/api/v1/search?sorting=random&ratios=16x9,16x10&resolutions=' . $resolution . '&q=' . $search); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - $response = curl_exec($curl); - $json = json_decode($response, true); + $response = $this->getData('https://wallhaven.cc/api/v1/search?sorting=random&ratios=16x9,16x10&resolutions=' . $resolution . '&q=' . $search); + $json = $response !== false ? json_decode($response, true) : null; try { $images = $json['data'][array_rand($json['data'])]; @@ -79,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, "Nextcloud", $this->clientService))->getRandomImageUrl($size); } public function getCachedImageURL(): string diff --git a/lib/Provider/WikimediaCommons.php b/lib/Provider/WikimediaCommons.php index f084d81..5b7f408 100644 --- a/lib/Provider/WikimediaCommons.php +++ b/lib/Provider/WikimediaCommons.php @@ -22,7 +22,6 @@ namespace OCA\Unsplash\Provider; -use OC\AppFramework\Http\Request; use OCA\Unsplash\ProviderHandler\Provider; class WikimediaCommons extends Provider @@ -63,10 +62,8 @@ public function getRandomImageUrlBySearchTerm($search, $size) $url .= '&iiprop=url'; $url .= '&format=json'; - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - $response = curl_exec($curl); - $json = json_decode($response, true); + $response = $this->getData($url); + $json = $response !== false ? json_decode($response, true) : null; try { $images = $json['query']['pages'][array_rand($json['query']['pages'])]; @@ -75,7 +72,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, "Nextcloud", $this->clientService))->getRandomImageUrl($size); } public function getCachedImageURL(): string diff --git a/lib/Provider/WikimediaCommonsDaily.php b/lib/Provider/WikimediaCommonsDaily.php index 2ca37c2..e1cd04b 100644 --- a/lib/Provider/WikimediaCommonsDaily.php +++ b/lib/Provider/WikimediaCommonsDaily.php @@ -22,7 +22,6 @@ namespace OCA\Unsplash\Provider; -use OC\AppFramework\Http\Request; use OCA\Unsplash\ProviderHandler\Provider; class WikimediaCommonsDaily extends Provider @@ -57,10 +56,8 @@ public function getRandomImageUrlBySearchTerm($search, $size) $url .= '&iiprop=url'; $url .= '&format=json'; - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - $response = curl_exec($curl); - $json = json_decode($response, true); + $response = $this->getData($url); + $json = $response !== false ? json_decode($response, true) : null; try { $images = $json['query']['pages'][array_rand($json['query']['pages'])]; @@ -69,7 +66,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, "Nextcloud", $this->clientService))->getRandomImageUrl($size); } public function getCachedImageURL(): string diff --git a/lib/ProviderHandler/Provider.php b/lib/ProviderHandler/Provider.php index 278c448..c7985bd 100644 --- a/lib/ProviderHandler/Provider.php +++ b/lib/ProviderHandler/Provider.php @@ -25,6 +25,7 @@ namespace OCA\Unsplash\ProviderHandler; use OCP\Files\IAppData; +use OCP\Http\Client\IClientService; use OCP\IConfig; use Psr\Log\LoggerInterface; use OCP\Files\NotFoundException; @@ -54,7 +55,9 @@ abstract class Provider * @param $appName * @param LoggerInterface $logger * @param IConfig $config - * @param $pName + * @param IAppData $appData + * @param string $providerName + * @param IClientService $clientService */ public function __construct( protected string $appName, @@ -62,6 +65,7 @@ public function __construct( protected IConfig $config, protected IAppData $appData, protected string $providerName, + protected IClientService $clientService, ) { } @@ -209,25 +213,21 @@ public abstract function getRandomImageUrlBySearchTerm($search, $size); /** + * Fetch data from a remote URL using the Nextcloud HTTP client. * - * This doesnt really belong here. I should create a utils class or something like it - * @param $host - * @return bool|string + * @param string $host + * @return string|false Response body, or false on failure */ - protected function getData($host) + protected function getData(string $host): string|false { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $host); - if ($this->config->getSystemValueBool('debug', false)) { - curl_setopt($ch, CURLOPT_VERBOSE, 1); + try { + $client = $this->clientService->newClient(); + $response = $client->get($host); + return $response->getBody(); + } catch (\Exception $e) { + $this->logger->warning('Failed to fetch data from ' . $host . ': ' . $e->getMessage()); + return false; } - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_AUTOREFERER, false); - curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); - curl_setopt($ch, CURLOPT_HEADER, 0); - $result = curl_exec($ch); - curl_close($ch); - return $result; } diff --git a/lib/ProviderHandler/ProviderDefinitions.php b/lib/ProviderHandler/ProviderDefinitions.php index cf8e1ee..4268fb7 100644 --- a/lib/ProviderHandler/ProviderDefinitions.php +++ b/lib/ProviderHandler/ProviderDefinitions.php @@ -30,6 +30,7 @@ use OCA\Unsplash\Provider\WikimediaCommons; use OCA\Unsplash\Provider\WikimediaCommonsDaily; use OCP\Files\IAppData; +use OCP\Http\Client\IClientService; use OCP\IConfig; use Psr\Log\LoggerInterface; @@ -55,30 +56,35 @@ class ProviderDefinitions /** @var LoggerInterface */ private $logger; + /** @var IClientService */ + private $clientService; + /** * ProviderDefinitions constructor. * * @param String $appName * @param LoggerInterface $logger - * @param IConfig $settings + * @param IConfig $config * @param IAppData $appData + * @param IClientService $clientService */ - function __construct($appName, LoggerInterface $logger, IConfig $config, IAppData $appData) + function __construct($appName, LoggerInterface $logger, IConfig $config, IAppData $appData, IClientService $clientService) { $this->appName = $appName; $this->config = $config; $this->appData = $appData; $this->logger = $logger; + $this->clientService = $clientService; $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, "UnsplashAPI", $clientService); + $tmp[] = new NextcloudImage($this->appName, $logger, $this->config, $appData, "Nextcloud Image", $clientService); + $tmp[] = new WikimediaCommons($this->appName, $logger, $this->config, $appData, "WikimediaCommons", $clientService); + $tmp[] = new WikimediaCommonsDaily($this->appName, $logger, $this->config, $appData, "WikimediaCommons - Picture of the Day", $clientService); + $tmp[] = new WallhavenCC($this->appName, $logger, $this->config, $appData, "WallhavenCC", $clientService); + $tmp[] = new BingWallpaperDaily($this->appName, $logger, $this->config, $appData, "Bing Wallpaper - Picture of the Day", $clientService); foreach ($tmp as &$value) { $this->definitions[$value->getName()] = $value; @@ -96,11 +102,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, "WikimediaCommons - Picture of the Day", $this->clientService); } $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, "WikimediaCommons - Picture of the Day", $this->clientService); } return $this->definitions[$name]; } diff --git a/lib/Services/SettingsService.php b/lib/Services/SettingsService.php index 4a19e71..ad8bc30 100644 --- a/lib/Services/SettingsService.php +++ b/lib/Services/SettingsService.php @@ -9,7 +9,9 @@ use OCA\Unsplash\ProviderHandler\CachedProvider; use OCA\Unsplash\ProviderHandler\Provider; use OCA\Unsplash\ProviderHandler\ProviderDefinitions; +use OCP\Defaults; use OCP\Files\IAppData; +use OCP\Http\Client\IClientService; use OCP\IConfig; use Psr\Log\LoggerInterface; @@ -59,7 +61,7 @@ class SettingsService protected $providerDefinitions; /** - * @var \OC_Defaults + * @var \OCP\Defaults */ private $defaults; @@ -69,11 +71,12 @@ class SettingsService * @param string|null $userId * @param $appName * @param IConfig $config - * @param IAppData $config + * @param IAppData $appData * @param Defaults $defaults * @param LoggerInterface $logger + * @param IClientService $clientService */ - public function __construct($userId, $appName, IConfig $config, IAppData $appData, \OC_Defaults $defaults, LoggerInterface $logger) + public function __construct($userId, $appName, IConfig $config, IAppData $appData, Defaults $defaults, LoggerInterface $logger, IClientService $clientService) { $this->config = $config; $this->userId = $userId; @@ -82,7 +85,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, $clientService); $this->defaults = $defaults; } @@ -94,11 +97,7 @@ public function __construct($userId, $appName, IConfig $config, IAppData $appDat public function getUserStyleDashboardEnabled(): bool { // for magic value see: https://github.com/nextcloud/server/blame/master/apps/theming/lib/Service/BackgroundService.php - $themingSettingsKey = "background"; - if ($this->getNextcloudVersion() > 26) { - $themingSettingsKey = "background_image"; - } - $themingAppDashboard = $this->config->getUserValue($this->userId, "theming", $themingSettingsKey, 'default'); + $themingAppDashboard = $this->config->getUserValue($this->userId, "theming", "background_image", 'default'); // dont add custom css when custom image was selected if ($themingAppDashboard == 'default') { @@ -107,19 +106,6 @@ public function getUserStyleDashboardEnabled(): bool return false; } - /** - * Todo: refactor this function to a "has dash" function that also checks wether the dashboard is actually enabled. - * and then dont show the entries. - * @return int - */ - public function getNextcloudVersion(): int - { - $version = $this->config->getSystemValue('version', '0.0.0'); - $parts = explode('.', $version, 2); - - return intval($parts[0]); - } - /** * If the page dashboard be styled by default * diff --git a/lib/Settings/AdminSettings.php b/lib/Settings/AdminSettings.php index 967222d..1f89115 100644 --- a/lib/Settings/AdminSettings.php +++ b/lib/Settings/AdminSettings.php @@ -48,7 +48,8 @@ public function getForm(): TemplateResponse ), 'styleLogin' => $this->settings->getServerStyleLoginEnabled(), 'styleDashboard' => $this->settings->getServerStyleDashboardEnabled(), - 'hasDashboard' => $this->settings->getNextcloudVersion() > 19, + // Dashboard was introduced in NC20; always available in supported range NC31+ + 'hasDashboard' => true, 'availableProvider' => $this->settings->getAllImageProvider(), 'selectedProvider' => $this->settings->getImageProviderName(), 'availableCustomizations' => $this->settings->getAllCustomizableImageProvider(), diff --git a/lib/Settings/PersonalSettings.php b/lib/Settings/PersonalSettings.php index caa028a..d2cf50c 100644 --- a/lib/Settings/PersonalSettings.php +++ b/lib/Settings/PersonalSettings.php @@ -9,9 +9,9 @@ namespace OCA\Unsplash\Settings; -use OC_Defaults; use OCA\Unsplash\Services\SettingsService; use OCP\AppFramework\Http\TemplateResponse; +use OCP\Defaults; use OCP\Settings\ISettings; /** @@ -26,11 +26,11 @@ class PersonalSettings implements ISettings * PersonalSection constructor. * * @param SettingsService $settings - * @param OC_Defaults $theming + * @param Defaults $theming */ public function __construct( private SettingsService $settings, - private OC_Defaults $theming, + private Defaults $theming, ) { } diff --git a/templates/settings/admin.php b/templates/settings/admin.php index 92c9736..7439f0b 100644 --- a/templates/settings/admin.php +++ b/templates/settings/admin.php @@ -27,13 +27,11 @@ type="checkbox" > - -
- > - -
- +
+ > + +