Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<summary lang="de">📸🔀☁️ Zufällige Hintergrundbilder von Unsplash</summary>
<description>Show a new random featured nature photo in your nextcloud. Now with choosable motives!</description>
<description lang="de">Zeigt ein zufällig ausgewähltes Naturfoto in ihrer Nextcloud. Jetzt auch mit selbstwählbaren Motiven!</description>
<version>3.1.0</version>
<version>3.2.0</version>
<licence>agpl</licence>
<author homepage="https://jancborchardt.net">Jan C. Borchardt</author>
<author homepage="https://github.com/marius-wieschollek">Marius Wieschollek</author>
Expand All @@ -22,8 +22,8 @@
<screenshot>https://raw.githubusercontent.com/nextcloud/unsplash/master/.meta/unsplash.jpg</screenshot>
<screenshot>https://raw.githubusercontent.com/nextcloud/unsplash/master/.meta/unsplash-header.jpg</screenshot>
<dependencies>
<php min-version="8.0" max-version="8.4"/>
<nextcloud min-version="26" max-version="32"/>
<php min-version="8.2" max-version="8.5"/>
<nextcloud min-version="31" max-version="33"/>
</dependencies>
<settings>
<admin>OCA\Unsplash\Settings\AdminSettings</admin>
Expand Down
1 change: 0 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions lib/Controller/AdminSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions lib/EventListener/BeforeTemplateRenderedEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions lib/Provider/BingWallpaperDaily.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

namespace OCA\Unsplash\Provider;

use OC\AppFramework\Http\Request;
use OCA\Unsplash\ProviderHandler\Provider;

class BingWallpaperDaily extends Provider
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion lib/Provider/UnsplashAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 3 additions & 6 deletions lib/Provider/WallhavenCC.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

namespace OCA\Unsplash\Provider;

use OC\AppFramework\Http\Request;
use OCA\Unsplash\ProviderHandler\Provider;

class WallhavenCC extends Provider
Expand Down Expand Up @@ -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'])];
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions lib/Provider/WikimediaCommons.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

namespace OCA\Unsplash\Provider;

use OC\AppFramework\Http\Request;
use OCA\Unsplash\ProviderHandler\Provider;

class WikimediaCommons extends Provider
Expand Down Expand Up @@ -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'])];
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions lib/Provider/WikimediaCommonsDaily.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

namespace OCA\Unsplash\Provider;

use OC\AppFramework\Http\Request;
use OCA\Unsplash\ProviderHandler\Provider;

class WikimediaCommonsDaily extends Provider
Expand Down Expand Up @@ -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'])];
Expand All @@ -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
Expand Down
32 changes: 16 additions & 16 deletions lib/ProviderHandler/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,14 +55,17 @@ 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,
protected LoggerInterface $logger,
protected IConfig $config,
protected IAppData $appData,
protected string $providerName,
protected IClientService $clientService,
)
{
}
Expand Down Expand Up @@ -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;
}


Expand Down
26 changes: 16 additions & 10 deletions lib/ProviderHandler/ProviderDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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];
}
Expand Down
Loading