Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tools for manually administrating the configured cache #3867

Merged
merged 4 commits into from
Jan 9, 2024
Merged
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
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,37 @@ Browse http://localhost:3000/
[![Deploy to Cloudron](https://cloudron.io/img/button.svg)](https://www.cloudron.io/store/com.rssbridgeapp.cloudronapp.html)
[![Run on PikaPods](https://www.pikapods.com/static/run-button.svg)](https://www.pikapods.com/pods?run=rssbridge)

The Heroku quick deploy currently does not work. It might possibly work if you fork this repo and
The Heroku quick deploy currently does not work. It might work if you fork this repo and
modify the `repository` in `scalingo.json`. See https://github.com/RSS-Bridge/rss-bridge/issues/2688

Learn more in
[Installation](https://rss-bridge.github.io/rss-bridge/For_Hosts/Installation.html).

## How-to

### How to remove all cache items

As current user:

bin/cache-clear

As user rss-bridge:

sudo -u rss-bridge bin/cache-clear

As root:

sudo bin/cache-clear

### How to remove all expired cache items

bin/cache-clear

### How to fix "PHP Fatal error: Uncaught Exception: The FileCache path is not writable"

```shell
# Give rssbridge ownership
chown rssbridge:rssbridge -R /var/www/rss-bridge/cache
# Give rss-bridge ownership
chown rss-bridge:rss-bridge -R /var/www/rss-bridge/cache

# Or, give www-data ownership
chown www-data:www-data -R /var/www/rss-bridge/cache
Expand All @@ -275,6 +293,16 @@ chmod 777 -R /var/www/rss-bridge/cache
rm -rf /var/www/rss-bridge/cache/ && mkdir /var/www/rss-bridge/cache/
```

### How to fix "attempt to write a readonly database"

The sqlite files (db, wal and shm) are not writeable.

chown -v rss-bridge:rss-bridge cache/*

### How to fix "Unable to prepare statement: 1, no such table: storage"

rm cache/*

### How to create a new bridge from scratch

Create the new bridge in e.g. `bridges/BearBlogBridge.php`:
Expand Down Expand Up @@ -389,6 +417,8 @@ These commands require that you have installed the dev dependencies in `composer
./vendor/bin/phpunit
./vendor/bin/phpcs --standard=phpcs.xml --warning-severity=0 --extensions=php -p ./

https://github.com/squizlabs/PHP_CodeSniffer/wiki

### How to spawn a minimal development environment

php -S 127.0.0.1:9001
Expand Down
29 changes: 14 additions & 15 deletions bridges/PixivBridge.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

/**
* Good resource on API return values (Ex: illustType):
* https://hackage.haskell.org/package/pixiv-0.1.0/docs/Web-Pixiv-Types.html
*/
class PixivBridge extends BridgeAbstract
{
// Good resource on API return values (Ex: illustType):
// https://hackage.haskell.org/package/pixiv-0.1.0/docs/Web-Pixiv-Types.html
const NAME = 'Pixiv Bridge';
const URI = 'https://www.pixiv.net/';
const DESCRIPTION = 'Returns the tag search from pixiv.net';
Expand All @@ -19,7 +21,6 @@ class PixivBridge extends BridgeAbstract
]
];


const PARAMETERS = [
'global' => [
'posts' => [
Expand Down Expand Up @@ -251,14 +252,13 @@ public function collectData()
$img_url = preg_replace('/https:\/\/i\.pximg\.net/', $proxy_url, $result['url']);
}
} else {
//else cache and use image.
$img_url = $this->cacheImage(
$result['url'],
$result['id'],
array_key_exists('illustType', $result)
);
$img_url = $result['url'];
// Temporarily disabling caching of the image
//$img_url = $this->cacheImage($result['url'], $result['id'], array_key_exists('illustType', $result));
}
$item['content'] = "<img src='" . $img_url . "' />";

// Currently, this might result in broken image due to their strict referrer check
$item['content'] = sprintf('<a href="%s"><img src="%s"/></a>', $img_url, $img_url);

// Additional content items
if (array_key_exists('pageCount', $result)) {
Expand Down Expand Up @@ -318,16 +318,15 @@ private function checkOptions()
if (
!(strlen($proxy) > 0 && preg_match('/https?:\/\/.*/', $proxy))
) {
return returnServerError('Invalid proxy_url value set. The proxy must include the HTTP/S at the beginning of the url.');
returnServerError('Invalid proxy_url value set. The proxy must include the HTTP/S at the beginning of the url.');
}
}

$cookie = $this->getCookie();
if ($cookie) {
$isAuth = $this->loadCacheValue('is_authenticated');
if (!$isAuth) {
$res = $this->getData('https://www.pixiv.net/ajax/webpush', true, true)
or returnServerError('Invalid PHPSESSID cookie provided. Please check the 🍪 and try again.');
$res = $this->getData('https://www.pixiv.net/ajax/webpush', true, true);
if ($res['error'] === false) {
$this->saveCacheValue('is_authenticated', true);
}
Expand Down Expand Up @@ -374,11 +373,11 @@ private function getData(string $url, bool $cache = true, bool $getJSON = false,
if ($cache) {
$data = $this->loadCacheValue($url);
if (!$data) {
$data = getContents($url, $httpHeaders, $curlOptions, true) or returnServerError("Could not load $url");
$data = getContents($url, $httpHeaders, $curlOptions, true);
$this->saveCacheValue($url, $data);
}
} else {
$data = getContents($url, $httpHeaders, $curlOptions, true) or returnServerError("Could not load $url");
$data = getContents($url, $httpHeaders, $curlOptions, true);
}

$this->checkCookie($data['headers']);
Expand Down
15 changes: 11 additions & 4 deletions docs/10_Bridge_Specific/PixivBridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ PixivBridge
===============

# Image proxy
As Pixiv requires images to be loaded with the `Referer "https://www.pixiv.net/"` header set, caching or image proxy is required to use this bridge.

To turn off image caching, set the `proxy_url` value in this bridge's configuration section of `config.ini.php` to the url of the proxy. The bridge will then use the proxy in this format (essentially replacing `https://i.pximg.net` with the proxy):
As Pixiv requires images to be loaded with the `Referer "https://www.pixiv.net/"` header set,
caching or image proxy is required to use this bridge.

To turn off image caching, set the `proxy_url` value in this bridge's configuration section of `config.ini.php`
to the url of the proxy.

The bridge will then use the proxy in this format (essentially replacing `https://i.pximg.net` with the proxy):

Before: `https://i.pximg.net/img-original/img/0000/00/00/00/00/00/12345678_p0.png`

Expand All @@ -15,9 +20,11 @@ proxy_url = "https://proxy.example.com"
```

# Authentication
Authentication is required to view and search R-18+ and non-public images. To enable this, set the following in this bridge's configuration in `config.ini.php`.

```
Authentication is required to view and search R-18+ and non-public images.
To enable this, set the following in this bridge's configuration in `config.ini.php`.

```ini
; from cookie "PHPSESSID". Recommend to get in incognito browser.
cookie = "00000000_hashedsessionidhere"
```
25 changes: 4 additions & 21 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
<?php

if (version_compare(\PHP_VERSION, '7.4.0') === -1) {
exit('RSS-Bridge requires minimum PHP version 7.4.0!');
}

require_once __DIR__ . '/lib/bootstrap.php';

$errors = Configuration::checkInstallation();
if ($errors) {
print '<pre>' . implode("\n", $errors) . '</pre>';
exit(1);
}

$customConfig = [];
if (file_exists(__DIR__ . '/config.ini.php')) {
$customConfig = parse_ini_file(__DIR__ . '/config.ini.php', true, INI_SCANNER_TYPED);
}
Configuration::loadConfiguration($customConfig, getenv());

// Consider: ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
date_default_timezone_set(Configuration::getConfig('system', 'timezone'));

$rssBridge = new RssBridge();

set_exception_handler(function (\Throwable $e) {
http_response_code(500);
print render(__DIR__ . '/templates/exception.html.php', ['e' => $e]);
RssBridge::getLogger()->error('Uncaught Exception', ['e' => $e]);
exit(1);
http_response_code(500);
exit(render(__DIR__ . '/templates/exception.html.php', ['e' => $e]));
});

set_error_handler(function ($code, $message, $file, $line) {
Expand Down Expand Up @@ -63,4 +44,6 @@
}
});

$rssBridge = new RssBridge();

$rssBridge->main($argv ?? []);
1 change: 1 addition & 0 deletions lib/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function create(string $name = null): CacheInterface
if ($index === false) {
throw new \InvalidArgumentException(sprintf('Invalid cache name: "%s"', $name));
}

$className = $cacheNames[$index] . 'Cache';
if (!preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $className)) {
throw new \InvalidArgumentException(sprintf('Invalid cache classname: "%s"', $className));
Expand Down
2 changes: 1 addition & 1 deletion lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function loadConfiguration(array $customConfig = [], array $env =
}
$config = parse_ini_file(__DIR__ . '/../config.default.ini.php', true, INI_SCANNER_TYPED);
if (!$config) {
throw new \Exception('Error parsing config');
throw new \Exception('Error parsing ini config');
}
foreach ($config as $header => $section) {
foreach ($section as $key => $value) {
Expand Down
15 changes: 15 additions & 0 deletions lib/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

if (version_compare(\PHP_VERSION, '7.4.0') === -1) {
exit('RSS-Bridge requires minimum PHP version 7.4.0!');
}

// Path to the formats library
const PATH_LIB_FORMATS = __DIR__ . '/../formats/';

Expand Down Expand Up @@ -49,3 +53,14 @@
}
}
});

$errors = Configuration::checkInstallation();
if ($errors) {
exit('<pre>' . implode("\n", $errors) . '</pre>');
}

$customConfig = [];
if (file_exists(__DIR__ . '/../config.ini.php')) {
$customConfig = parse_ini_file(__DIR__ . '/../config.ini.php', true, INI_SCANNER_TYPED);
}
Configuration::loadConfiguration($customConfig, getenv());
1 change: 1 addition & 0 deletions lib/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public function __invoke(array $record)
);
error_log($text);
if ($record['level'] < Logger::ERROR && Debug::isEnabled()) {
// The record level is INFO or WARNING here
// Not a good idea to print here because http headers might not have been sent
print sprintf("<pre>%s</pre>\n", e($text));
}
Expand Down
8 changes: 7 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="RSS-Bridge Ruleset">
<description>Created with the PHP Coding Standard Generator. http://edorian.github.com/php-coding-standard-generator/</description>
<description>
Originally created with the PHP Coding Standard Generator.
But later manually tweaked.
http://edorian.github.com/php-coding-standard-generator/
</description>

<exclude-pattern>./static</exclude-pattern>
<exclude-pattern>./vendor</exclude-pattern>
<exclude-pattern>./templates</exclude-pattern>
Expand All @@ -11,6 +16,7 @@
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
<exclude name="PSR1.Classes.ClassDeclaration.MultipleClasses"/>
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/>
<exclude name="PSR2.Files.EndFileNewline"/>
<exclude name="PSR12.Properties.ConstantVisibility.NotFound"/>
</rule>

Expand Down
8 changes: 8 additions & 0 deletions templates/exception.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
</p>
<?php endif; ?>

<?php if ($e->getCode() === 403): ?>
<h2>403 Forbidden</h2>
<p>
The HTTP 403 Forbidden response status code indicates that the
server understands the request but refuses to authorize it.
</p>
<?php endif; ?>

<?php if ($e->getCode() === 404): ?>
<h2>404 Page Not Found</h2>
<p>
Expand Down