forked from leftypol/leftypol
Compare commits
24 commits
config
...
embed-fixe
Author | SHA1 | Date | |
---|---|---|---|
![]() |
9f9fba63d9 | ||
![]() |
a5770a2de5 | ||
![]() |
871e8d78dc | ||
![]() |
9df6cdbf28 | ||
fe5368f096 | |||
fbf0c051f0 | |||
83e04c639a | |||
3924b41c46 | |||
28a747b335 | |||
811698d9ef | |||
388fc2c05d | |||
ffe7a44635 | |||
b71d53c1a8 | |||
01811cb50f | |||
cd8d0e060f | |||
256a9682fa | |||
8ac67e9e85 | |||
5a8c661257 | |||
4850a8ddd3 | |||
698451a6d5 | |||
231fcb9ca9 | |||
0e9c9de5c6 | |||
bf42570d5d | |||
a5cc1c2b42 |
13 changed files with 501 additions and 40 deletions
135
inc/Data/Driver/HttpDriver.php
Normal file
135
inc/Data/Driver/HttpDriver.php
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
<?php
|
||||||
|
namespace Vichan\Data\Driver;
|
||||||
|
|
||||||
|
defined('TINYBOARD') or exit;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Honestly this is just a wrapper for cURL. Still useful to mock it and have an OOP API on PHP 7.
|
||||||
|
*/
|
||||||
|
class HttpDriver {
|
||||||
|
private mixed $inner;
|
||||||
|
private int $timeout;
|
||||||
|
private int $max_file_size;
|
||||||
|
|
||||||
|
|
||||||
|
private function resetTowards(string $url, int $timeout): void {
|
||||||
|
\curl_reset($this->inner);
|
||||||
|
\curl_setopt_array($this->inner, [
|
||||||
|
\CURLOPT_URL => $url,
|
||||||
|
\CURLOPT_TIMEOUT => $timeout,
|
||||||
|
\CURLOPT_USERAGENT => 'Tinyboard',
|
||||||
|
\CURLOPT_PROTOCOLS => \CURLPROTO_HTTP | \CURLPROTO_HTTPS,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __construct(int $timeout, int $max_file_size) {
|
||||||
|
$this->inner = \curl_init();
|
||||||
|
$this->timeout = $timeout;
|
||||||
|
$this->max_file_size = $max_file_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __destruct() {
|
||||||
|
\curl_close($this->inner);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a GET request.
|
||||||
|
*
|
||||||
|
* @param string $endpoint Uri endpoint.
|
||||||
|
* @param ?array $data Optional GET parameters.
|
||||||
|
* @param ?array $data Optional HTTP headers.
|
||||||
|
* @param int $timeout Optional request timeout in seconds. Use the default timeout if 0.
|
||||||
|
* @return string Returns the body of the response.
|
||||||
|
* @throws RuntimeException Throws on IO error.
|
||||||
|
*/
|
||||||
|
public function requestGet(string $endpoint, ?array $data, ?array $headers = null, int $timeout = 0): string {
|
||||||
|
if (!empty($data)) {
|
||||||
|
$endpoint .= '?' . \http_build_query($data);
|
||||||
|
}
|
||||||
|
if ($timeout == 0) {
|
||||||
|
$timeout = $this->timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->resetTowards($endpoint, $timeout);
|
||||||
|
if (!empty($headers)) {
|
||||||
|
\curl_setopt($this->inner, \CURLOPT_HTTPHEADER, $headers);
|
||||||
|
}
|
||||||
|
\curl_setopt($this->inner, \CURLOPT_RETURNTRANSFER, true);
|
||||||
|
$ret = \curl_exec($this->inner);
|
||||||
|
|
||||||
|
if ($ret === false) {
|
||||||
|
throw new \RuntimeException(\curl_error($this->inner));
|
||||||
|
}
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a POST request.
|
||||||
|
*
|
||||||
|
* @param string $endpoint Uri endpoint.
|
||||||
|
* @param ?array $data Optional POST parameters.
|
||||||
|
* @param int $timeout Optional request timeout in seconds. Use the default timeout if 0.
|
||||||
|
* @return string Returns the body of the response.
|
||||||
|
* @throws RuntimeException Throws on IO error.
|
||||||
|
*/
|
||||||
|
public function requestPost(string $endpoint, ?array $data, int $timeout = 0): string {
|
||||||
|
if ($timeout == 0) {
|
||||||
|
$timeout = $this->timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->resetTowards($endpoint, $timeout);
|
||||||
|
\curl_setopt($this->inner, \CURLOPT_POST, true);
|
||||||
|
if (!empty($data)) {
|
||||||
|
\curl_setopt($this->inner, \CURLOPT_POSTFIELDS, \http_build_query($data));
|
||||||
|
}
|
||||||
|
\curl_setopt($this->inner, \CURLOPT_RETURNTRANSFER, true);
|
||||||
|
$ret = \curl_exec($this->inner);
|
||||||
|
|
||||||
|
if ($ret === false) {
|
||||||
|
throw new \RuntimeException(\curl_error($this->inner));
|
||||||
|
}
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download the url's target with curl.
|
||||||
|
*
|
||||||
|
* @param string $url Url to the file to download.
|
||||||
|
* @param ?array $data Optional GET parameters.
|
||||||
|
* @param resource $fd File descriptor to save the content to.
|
||||||
|
* @param int $timeout Optional request timeout in seconds. Use the default timeout if 0.
|
||||||
|
* @return bool Returns true on success, false if the file was too large.
|
||||||
|
* @throws RuntimeException Throws on IO error.
|
||||||
|
*/
|
||||||
|
public function requestGetInto(string $endpoint, ?array $data, mixed $fd, int $timeout = 0): bool {
|
||||||
|
if (!empty($data)) {
|
||||||
|
$endpoint .= '?' . \http_build_query($data);
|
||||||
|
}
|
||||||
|
if ($timeout == 0) {
|
||||||
|
$timeout = $this->timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->resetTowards($endpoint, $timeout);
|
||||||
|
// Adapted from: https://stackoverflow.com/a/17642638
|
||||||
|
$opt = (\PHP_MAJOR_VERSION >= 8 && \PHP_MINOR_VERSION >= 2) ? \CURLOPT_XFERINFOFUNCTION : \CURLOPT_PROGRESSFUNCTION;
|
||||||
|
\curl_setopt_array($this->inner, [
|
||||||
|
\CURLOPT_NOPROGRESS => false,
|
||||||
|
$opt => fn($res, $next_dl, $dl, $next_up, $up) => (int)($dl > $this->max_file_size),
|
||||||
|
\CURLOPT_FAILONERROR => true,
|
||||||
|
\CURLOPT_FOLLOWLOCATION => false,
|
||||||
|
\CURLOPT_FILE => $fd,
|
||||||
|
\CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
||||||
|
]);
|
||||||
|
$ret = \curl_exec($this->inner);
|
||||||
|
|
||||||
|
if ($ret === false) {
|
||||||
|
if (\curl_errno($this->inner) === CURLE_ABORTED_BY_CALLBACK) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \RuntimeException(\curl_error($this->inner));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
11
inc/Data/OembedResponse.php
Normal file
11
inc/Data/OembedResponse.php
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
namespace Vichan\Data;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Raw return values, those aren't validated beyond being not null and the type.
|
||||||
|
*/
|
||||||
|
class OembedResponse {
|
||||||
|
public ?string $title;
|
||||||
|
public ?string $thumbnail_url;
|
||||||
|
}
|
150
inc/Service/Embed/EmbedService.php
Normal file
150
inc/Service/Embed/EmbedService.php
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
<?php
|
||||||
|
namespace Vichan\Service\Embed;
|
||||||
|
|
||||||
|
use Vichan\Service\Embed\OembedExtractor;
|
||||||
|
use Vichan\Data\Driver\{HttpDriver, LogDriver};
|
||||||
|
|
||||||
|
|
||||||
|
class EmbedService {
|
||||||
|
private const TMP_FILE_PREFIX = 'oembed-thumb-';
|
||||||
|
|
||||||
|
private LogDriver $log;
|
||||||
|
private OembedExtractor $oembed_extractor;
|
||||||
|
private HttpDriver $http;
|
||||||
|
private string $tmp_dir;
|
||||||
|
private array $embed_entries;
|
||||||
|
private int $thumb_download_timeout;
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
LogDriver $log,
|
||||||
|
OembedExtractor $oembed_extractor,
|
||||||
|
HttpDriver $http,
|
||||||
|
array $embed_entries,
|
||||||
|
int $thumb_download_timeout,
|
||||||
|
string $tmp_dir,
|
||||||
|
) {
|
||||||
|
$this->log = $log;
|
||||||
|
$this->oembed_extractor = $oembed_extractor;
|
||||||
|
$this->http = $http;
|
||||||
|
$this->embed_entries = $embed_entries;
|
||||||
|
$this->thumb_download_timeout = $thumb_download_timeout;
|
||||||
|
$this->tmp_dir = $tmp_dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function make_tmp_file(): string {
|
||||||
|
$ret = \tempnam($this->tmp_dir, self::TMP_FILE_PREFIX);
|
||||||
|
if ($ret === false) {
|
||||||
|
throw new \RuntimeException("Could not create temporary file in {$this->tmp_dir}");
|
||||||
|
}
|
||||||
|
\register_shutdown_function(fn() => @unlink($ret));
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads the thumbnail into a temporary file.
|
||||||
|
*
|
||||||
|
* @return ?string The path to the temporary file, null if the file was too large.
|
||||||
|
*/
|
||||||
|
private function fetchThumbnail(string $thumbnail_url): ?string {
|
||||||
|
$tmp_file = $this->make_tmp_file();
|
||||||
|
$fd = \fopen($tmp_file, 'w+b');
|
||||||
|
if ($fd === false) {
|
||||||
|
throw new \RuntimeException("Could not open temporary file $tmp_file for read/write");
|
||||||
|
}
|
||||||
|
|
||||||
|
$ret = $this->http->requestGetInto($thumbnail_url, null, $fd, $this->thumb_download_timeout);
|
||||||
|
return $ret ? $tmp_file : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the path to the thumbnail from a matched url, if any.
|
||||||
|
*
|
||||||
|
* @param string $url The url to embed.
|
||||||
|
* @param int $entry_index The index of the embedding entry.
|
||||||
|
* @return ?array Returns the url to the thumbnail and the path to the fallback.
|
||||||
|
*/
|
||||||
|
private function extractThumb(string $url, int $entry_index) {
|
||||||
|
$embed_entry = $this->embed_entries[$entry_index];
|
||||||
|
$match_regex = $embed_entry['match_regex'];
|
||||||
|
$type = $embed_entry['type'];
|
||||||
|
|
||||||
|
if ($type === 'oembed') {
|
||||||
|
$thumbnail_url_fallback = $embed_entry['thumbnail_url_fallback'] ?? null;
|
||||||
|
$provider = $embed_entry['provider_url'];
|
||||||
|
$oembed_resp = $this->oembed_extractor->fetch($provider, $url);
|
||||||
|
|
||||||
|
return [ $oembed_resp->thumbnail_url, $thumbnail_url_fallback ];
|
||||||
|
} elseif ($type === 'regex') {
|
||||||
|
$thumbnail_url_regex = $embed_entry['thumbnail_url'];
|
||||||
|
// Plz somebody review this.
|
||||||
|
return [ \preg_replace($match_regex, $thumbnail_url_regex, $url), null ];
|
||||||
|
} else {
|
||||||
|
$this->log->log(LogDriver::ERROR, "Unknown embed type '$type' in embed entry $entry_index, ignoring the entry");
|
||||||
|
return [ null, null ];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the embed entry matching with the url, if any.
|
||||||
|
*
|
||||||
|
* @param string $url Url to embed. MUST BE ALREADY VALIDATED.
|
||||||
|
* @return int The index of the matched embed entry or null.
|
||||||
|
*/
|
||||||
|
public function matchEmbed(string $url): ?int {
|
||||||
|
for ($i = 0; $i < \count($this->embed_entries); $i++) {
|
||||||
|
$match_regex = $this->embed_entries[$i]['match_regex'];
|
||||||
|
if (\preg_match($match_regex, $url, $matches)) {
|
||||||
|
return $i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the embed's thumbnail if possible. May download it from the network into a temporary file, or use a static file.
|
||||||
|
*
|
||||||
|
* @param string $url Url to embed. MUST BE ALREADY VALIDATED.
|
||||||
|
* @param int The index of the matched embed entry.
|
||||||
|
* @return ?array Null if no thumbnail can be selected, otherwise an array with the local file path to the thumbnail
|
||||||
|
* and if the the file is a temporary or a static one.
|
||||||
|
*/
|
||||||
|
public function getEmbedThumb(string $url, int $entry_index): ?array {
|
||||||
|
$ret = $this->extractThumb($url, $entry_index);
|
||||||
|
list($thumbnail_url, $thumbnail_url_fallback) = $ret;
|
||||||
|
if (!isset($thumbnail_url)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (\filter_var($thumbnail_url, \FILTER_VALIDATE_URL) === false) {
|
||||||
|
$this->log->log(LogDriver::ERROR, "Thumbnail URL '$thumbnail_url' from embed entry $entry_index is not a valid URL, trying fallback");
|
||||||
|
} else {
|
||||||
|
$tmp_file = $this->fetchThumbnail($thumbnail_url);
|
||||||
|
if ($tmp_file !== null) {
|
||||||
|
return [ $tmp_file, true ];
|
||||||
|
}
|
||||||
|
$this->log->log(LogDriver::NOTICE, "Thumbnail at '$thumbnail_url' was too large, trying fallback");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($thumbnail_url_fallback === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return [ $thumbnail_url_fallback, false ];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderEmbed(string $url, int $entry_index, string $thumbnail_path): string {
|
||||||
|
$embed_entry = $this->embed_entries[$entry_index];
|
||||||
|
$match_regex = $embed_entry['match_regex'];
|
||||||
|
$html = $embed_entry['html'];
|
||||||
|
|
||||||
|
$ret = \preg_replace($match_regex, $html, $url);
|
||||||
|
if (!\is_string($ret)) {
|
||||||
|
throw new \RuntimeException("Error while applying regex replacement for embed entry $entry_index");
|
||||||
|
}
|
||||||
|
|
||||||
|
\str_replace('%%embed_url%%', $url, $ret);
|
||||||
|
\str_replace('%%thumbnail_path%%', $thumbnail_path, $ret);
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
}
|
66
inc/Service/Embed/OembedExtractor.php
Normal file
66
inc/Service/Embed/OembedExtractor.php
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
<?php
|
||||||
|
namespace Vichan\Service\Embed;
|
||||||
|
|
||||||
|
use Vichan\Data\Driver\{CacheDriver, HttpDriver};
|
||||||
|
use Vichan\Data\OembedResponse;
|
||||||
|
|
||||||
|
|
||||||
|
class OembedExtractor {
|
||||||
|
private const DEFAULT_CACHE_TIMEOUT = 3600; // 1 hour.
|
||||||
|
private const MIN_CACHE_TIMEOUT = 900; // 15 minutes.
|
||||||
|
|
||||||
|
private CacheDriver $cache;
|
||||||
|
private HttpDriver $http;
|
||||||
|
private int $provider_timeout;
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct(CacheDriver $cache, HttpDriver $http, int $provider_timeout) {
|
||||||
|
$this->cache = $cache;
|
||||||
|
$this->http = $http;
|
||||||
|
$this->provider_timeout = $provider_timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the oembed data from the given provider with the given url.
|
||||||
|
*
|
||||||
|
* @param string $identifier Opaque identifier for caching, must be unique for each $url-$provider combination.
|
||||||
|
* @return OembedResponse The serialized remove response. May be cached.
|
||||||
|
*/
|
||||||
|
public function fetch(string $provider_url, string $url): OembedResponse {
|
||||||
|
$ret = $this->cache->get("oembed_embedder_$provider_url$url");
|
||||||
|
if ($ret === null) {
|
||||||
|
$body = $this->http->requestGet(
|
||||||
|
$provider_url,
|
||||||
|
[
|
||||||
|
'url' => $url,
|
||||||
|
'format' => 'json'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'Content-Type: application/json'
|
||||||
|
],
|
||||||
|
$this->provider_timeout
|
||||||
|
);
|
||||||
|
$json = \json_decode($body, true, 512, \JSON_THROW_ON_ERROR);
|
||||||
|
|
||||||
|
$ret = [
|
||||||
|
'title' => $json['title'] ?? null,
|
||||||
|
'thumbnail_url' => $json['thumbnail_url'] ?? null,
|
||||||
|
];
|
||||||
|
|
||||||
|
$cache_timeout = self::DEFAULT_CACHE_TIMEOUT;
|
||||||
|
if (isset($json['cache_age'])) {
|
||||||
|
$cache_age = \intval($json['cache_age']);
|
||||||
|
if ($cache_age > 0) {
|
||||||
|
$cache_age = \max($cache_age, self::MIN_CACHE_TIMEOUT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->cache->set("oembed_embedder_$provider_url$url", $ret, $cache_timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
$resp = new OembedResponse();
|
||||||
|
$resp->title = $ret['title'];
|
||||||
|
$resp->thumbnail_url = $ret['thumbnail_url'];
|
||||||
|
return $resp;
|
||||||
|
}
|
||||||
|
}
|
|
@ -943,6 +943,10 @@
|
||||||
// Location of thumbnail to use for deleted images.
|
// Location of thumbnail to use for deleted images.
|
||||||
$config['image_deleted'] = 'static/deleted.png';
|
$config['image_deleted'] = 'static/deleted.png';
|
||||||
|
|
||||||
|
// When a thumbnailed image is going to be the same (in dimension), just copy the entire file and use
|
||||||
|
// that as a thumbnail instead of resizing/redrawing.
|
||||||
|
$config['minimum_copy_resize'] = false;
|
||||||
|
|
||||||
// Maximum image upload size in bytes.
|
// Maximum image upload size in bytes.
|
||||||
$config['max_filesize'] = 10 * 1024 * 1024; // 10MB
|
$config['max_filesize'] = 10 * 1024 * 1024; // 10MB
|
||||||
// Maximum image dimensions.
|
// Maximum image dimensions.
|
||||||
|
@ -981,6 +985,15 @@
|
||||||
// Set this to true if you're using Linux and you can execute `md5sum` binary.
|
// Set this to true if you're using Linux and you can execute `md5sum` binary.
|
||||||
$config['gnu_md5'] = false;
|
$config['gnu_md5'] = false;
|
||||||
|
|
||||||
|
// Use Tesseract OCR to retrieve text from images, so you can use it as a spamfilter.
|
||||||
|
$config['tesseract_ocr'] = false;
|
||||||
|
|
||||||
|
// Tesseract parameters
|
||||||
|
$config['tesseract_params'] = '';
|
||||||
|
|
||||||
|
// Tesseract preprocess command
|
||||||
|
$config['tesseract_preprocess_command'] = 'convert -monochrome %s -';
|
||||||
|
|
||||||
// Number of posts in a "View Last X Posts" page
|
// Number of posts in a "View Last X Posts" page
|
||||||
$config['noko50_count'] = 50;
|
$config['noko50_count'] = 50;
|
||||||
// Number of posts a thread needs before it gets a "View Last X Posts" page.
|
// Number of posts a thread needs before it gets a "View Last X Posts" page.
|
||||||
|
@ -1252,6 +1265,37 @@
|
||||||
$config['embed_width'] = 300;
|
$config['embed_width'] = 300;
|
||||||
$config['embed_height'] = 246;
|
$config['embed_height'] = 246;
|
||||||
|
|
||||||
|
// Download timeout for the remove embed thumbnails in seconds.
|
||||||
|
$config['embed_thumb_timeout'] = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replacement parameters:
|
||||||
|
* - $1-$N: matched arguments from 'match_regex'.
|
||||||
|
* - %%thumbnail_path%%: Path to the downloaded thumbnail.
|
||||||
|
*/
|
||||||
|
$config['embedding_2'] = [
|
||||||
|
[
|
||||||
|
'match_regex' => '/^(?:(?:https?:)?\/\/)?((?:www|m)\.)?(?:(?:youtube(?:-nocookie)?\.com|youtu\.be))(?:\/(?:[\w\-]+\?v=|embed\/|live\/|v\/)?)([\w\-]{11})((?:\?|\&)\S+)?$/i',
|
||||||
|
'type' => 'regex',
|
||||||
|
'thumbnail_url' => 'https://img.youtube.com/vi/$2/0.jpg',
|
||||||
|
'html' => '<div class="video-container" data-video-id="$2" data-iframe-width="360" data-iframe-height="202">
|
||||||
|
<a href="https://youtu.be/$2" target="_blank" class="file">
|
||||||
|
<img style="width:360px;height:202px;object-fit:cover" src="%%thumbnail_path%%" class="post-image"/>
|
||||||
|
</a>
|
||||||
|
</div>'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'match_regex' => '/^https?:\/\/(\w+\.)?tiktok\.com\/@([a-z0-9\-_]+)\/video\/([0-9]+)\?.*$/i',
|
||||||
|
'type' => 'oembed',
|
||||||
|
'provider_url' => 'https://www.tiktok.com/oembed',
|
||||||
|
'html' => '<div class="tiktok-embed" data-video-author="$2" data-video-id="$3" data-iframe-width="168" data-iframe-height="300">
|
||||||
|
<a href="https://www.tiktok.com/@$2/video/$3" target="_blank" class="file">
|
||||||
|
<img style="width:168px;height:300px;object-fit:cover" src="%%thumbnail_path%%" class="post-image"/>
|
||||||
|
</a>
|
||||||
|
</div>'
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ====================
|
* ====================
|
||||||
* Error messages
|
* Error messages
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
namespace Vichan;
|
namespace Vichan;
|
||||||
|
|
||||||
use Vichan\Data\{IpNoteQueries, ReportQueries, UserPostQueries};
|
use Vichan\Data\{IpNoteQueries, ReportQueries, UserPostQueries};
|
||||||
use Vichan\Data\Driver\{CacheDriver, ErrorLogLogDriver, FileLogDriver, LogDriver, StderrLogDriver, SyslogLogDriver};
|
use Vichan\Data\Driver\{CacheDriver, HttpDriver, ErrorLogLogDriver, FileLogDriver, LogDriver, StderrLogDriver, SyslogLogDriver};
|
||||||
|
use Vichan\Service\Embed\EmbedService;
|
||||||
|
use Vichan\Service\Embed\OembedExtractor;
|
||||||
|
|
||||||
defined('TINYBOARD') or exit;
|
defined('TINYBOARD') or exit;
|
||||||
|
|
||||||
|
@ -63,6 +65,10 @@ function build_context(array $config): Context {
|
||||||
// Use the global for backwards compatibility.
|
// Use the global for backwards compatibility.
|
||||||
return \cache::getCache();
|
return \cache::getCache();
|
||||||
},
|
},
|
||||||
|
HttpDriver::class => function($c) {
|
||||||
|
$config = $c->get('config');
|
||||||
|
return new HttpDriver($config['upload_by_url_timeout'], $config['max_filesize']);
|
||||||
|
},
|
||||||
\PDO::class => function($c) {
|
\PDO::class => function($c) {
|
||||||
global $pdo;
|
global $pdo;
|
||||||
// Ensure the PDO is initialized.
|
// Ensure the PDO is initialized.
|
||||||
|
@ -78,5 +84,21 @@ function build_context(array $config): Context {
|
||||||
return new UserPostQueries($c->get(\PDO::class));
|
return new UserPostQueries($c->get(\PDO::class));
|
||||||
},
|
},
|
||||||
IpNoteQueries::class => fn($c) => new IpNoteQueries($c->get(\PDO::class), $c->get(CacheDriver::class)),
|
IpNoteQueries::class => fn($c) => new IpNoteQueries($c->get(\PDO::class), $c->get(CacheDriver::class)),
|
||||||
|
OembedExtractor::class => fn($c) => new OembedExtractor(
|
||||||
|
$c->get(CacheDriver::class),
|
||||||
|
$c->get(HttpDriver::class),
|
||||||
|
$c->get('config')['embed_thumb_timeout']
|
||||||
|
),
|
||||||
|
EmbedService::class => function($c) {
|
||||||
|
$config = $c->get('config');
|
||||||
|
return new EmbedService(
|
||||||
|
$c->get(LogDriver::class),
|
||||||
|
$c->get(OembedExtractor::class),
|
||||||
|
$c->get(HttpDriver::class),
|
||||||
|
$config['embedding_2'],
|
||||||
|
$config['embed_thumb_timeout'],
|
||||||
|
$config['tmp'],
|
||||||
|
);
|
||||||
|
}
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,10 +104,8 @@ function buildMenu(e) {
|
||||||
|
|
||||||
function addButton(post) {
|
function addButton(post) {
|
||||||
var $ele = $(post);
|
var $ele = $(post);
|
||||||
// Use unicode code with ascii variant selector
|
|
||||||
// https://stackoverflow.com/questions/37906969/how-to-prevent-ios-from-converting-ascii-into-emoji
|
|
||||||
$ele.find('input.delete').after(
|
$ele.find('input.delete').after(
|
||||||
$('<a>', {href: '#', class: 'post-btn', title: 'Post menu'}).text('\u{25B6}\u{fe0e}')
|
$('<a>', {href: '#', class: 'post-btn', title: 'Post menu'}).text('►')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
83
post.php
83
post.php
|
@ -262,6 +262,26 @@ function send_matrix_report(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalize_files(array $file_array) {
|
||||||
|
$out_files = [];
|
||||||
|
// If more than 0 files were uploaded
|
||||||
|
if (!empty($file_array['tmp_name'][0])) {
|
||||||
|
$i = 0;
|
||||||
|
$n = count($file_array['tmp_name']);
|
||||||
|
while ($i < $n) {
|
||||||
|
$out_files[strval($i + 1)] = array(
|
||||||
|
'name' => $file_array['name'][$i],
|
||||||
|
'tmp_name' => $file_array['tmp_name'][$i],
|
||||||
|
'type' => $file_array['type'][$i],
|
||||||
|
'error' => $file_array['error'][$i],
|
||||||
|
'size' => $file_array['size'][$i]
|
||||||
|
);
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $out_files;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the (single) captcha associated with the ip and code.
|
* Deletes the (single) captcha associated with the ip and code.
|
||||||
*
|
*
|
||||||
|
@ -924,7 +944,6 @@ function handle_post(Context $ctx)
|
||||||
isset($post['thread']) ? $post['thread'] : ($config['try_smarter'] && isset($_POST['page']) ? 0 - (int) $_POST['page'] : null)
|
isset($post['thread']) ? $post['thread'] : ($config['try_smarter'] && isset($_POST['page']) ? 0 - (int) $_POST['page'] : null)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
//$post['antispam_hash'] = checkSpam();
|
|
||||||
|
|
||||||
if ($post['antispam_hash'] === true) {
|
if ($post['antispam_hash'] === true) {
|
||||||
error($config['error']['spam']);
|
error($config['error']['spam']);
|
||||||
|
@ -953,7 +972,11 @@ function handle_post(Context $ctx)
|
||||||
// Check for an embed field
|
// Check for an embed field
|
||||||
if ($config['enable_embedding'] && isset($_POST['embed']) && !empty($_POST['embed'])) {
|
if ($config['enable_embedding'] && isset($_POST['embed']) && !empty($_POST['embed'])) {
|
||||||
// yep; validate it
|
// yep; validate it
|
||||||
$value = $_POST['embed'];
|
$value = \trim($_POST['embed']);
|
||||||
|
if (\filter_var($value, \FILTER_VALIDATE_URL) === false) {
|
||||||
|
error($config['error']['invalid_embed']);
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($config['embedding'] as &$embed) {
|
foreach ($config['embedding'] as &$embed) {
|
||||||
if (preg_match($embed[0], $value)) {
|
if (preg_match($embed[0], $value)) {
|
||||||
// Valid link
|
// Valid link
|
||||||
|
@ -990,23 +1013,7 @@ function handle_post(Context $ctx)
|
||||||
// Convert multiple upload format to array of files. This makes the following code
|
// Convert multiple upload format to array of files. This makes the following code
|
||||||
// work the same whether we used the JS or HTML multiple file upload techniques.
|
// work the same whether we used the JS or HTML multiple file upload techniques.
|
||||||
if (array_key_exists('file_multiple', $_FILES)) {
|
if (array_key_exists('file_multiple', $_FILES)) {
|
||||||
$file_array = $_FILES['file_multiple'];
|
$_FILES = normalize_files($_FILES['file_multiple']);
|
||||||
$_FILES = [];
|
|
||||||
// If more than 0 files were uploaded
|
|
||||||
if (!empty($file_array['tmp_name'][0])) {
|
|
||||||
$i = 0;
|
|
||||||
$n = count($file_array['tmp_name']);
|
|
||||||
while ($i < $n) {
|
|
||||||
$_FILES[strval($i + 1)] = array(
|
|
||||||
'name' => $file_array['name'][$i],
|
|
||||||
'tmp_name' => $file_array['tmp_name'][$i],
|
|
||||||
'type' => $file_array['type'][$i],
|
|
||||||
'error' => $file_array['error'][$i],
|
|
||||||
'size' => $file_array['size'][$i]
|
|
||||||
);
|
|
||||||
$i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We must do this check now before the passowrd is hashed and overwritten.
|
// We must do this check now before the passowrd is hashed and overwritten.
|
||||||
|
@ -1402,13 +1409,13 @@ function handle_post(Context $ctx)
|
||||||
$file['thumbwidth'] = $size[0];
|
$file['thumbwidth'] = $size[0];
|
||||||
$file['thumbheight'] = $size[1];
|
$file['thumbheight'] = $size[1];
|
||||||
} elseif (
|
} elseif (
|
||||||
(($config['strip_exif'] && isset($file['exif_stripped']) && $file['exif_stripped']) || !$config['strip_exif']) &&
|
$config['minimum_copy_resize'] &&
|
||||||
$image->size->width <= $config['thumb_width'] &&
|
$image->size->width <= $config['thumb_width'] &&
|
||||||
$image->size->height <= $config['thumb_height'] &&
|
$image->size->height <= $config['thumb_height'] &&
|
||||||
$file['extension'] == ($config['thumb_ext'] ? $config['thumb_ext'] : $file['extension'])
|
$file['extension'] == ($config['thumb_ext'] ? $config['thumb_ext'] : $file['extension'])
|
||||||
) {
|
) {
|
||||||
// Copy, because there's nothing to resize
|
// Copy, because there's nothing to resize
|
||||||
copy($file['tmp_name'], $file['thumb']);
|
coopy($file['tmp_name'], $file['thumb']);
|
||||||
|
|
||||||
$file['thumbwidth'] = $image->size->width;
|
$file['thumbwidth'] = $image->size->width;
|
||||||
$file['thumbheight'] = $image->size->height;
|
$file['thumbheight'] = $image->size->height;
|
||||||
|
@ -1551,6 +1558,35 @@ function handle_post(Context $ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($config['tesseract_ocr'] && $file['thumb'] != 'file') {
|
||||||
|
// Let's OCR it!
|
||||||
|
$fname = $file['tmp_name'];
|
||||||
|
|
||||||
|
if ($file['height'] > 500 || $file['width'] > 500) {
|
||||||
|
$fname = $file['thumb'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($fname == 'spoiler') {
|
||||||
|
// We don't have that much CPU time, do we?
|
||||||
|
} else {
|
||||||
|
$tmpname = __DIR__ . "/tmp/tesseract/" . rand(0, 10000000);
|
||||||
|
|
||||||
|
// Preprocess command is an ImageMagick b/w quantization
|
||||||
|
$error = shell_exec_error(sprintf($config['tesseract_preprocess_command'], escapeshellarg($fname)) . " | " .
|
||||||
|
'tesseract stdin ' . escapeshellarg($tmpname) . ' ' . $config['tesseract_params']);
|
||||||
|
$tmpname .= ".txt";
|
||||||
|
|
||||||
|
$value = @file_get_contents($tmpname);
|
||||||
|
@unlink($tmpname);
|
||||||
|
|
||||||
|
if ($value && trim($value)) {
|
||||||
|
// This one has an effect, that the body is appended to a post body. So you can write a correct
|
||||||
|
// spamfilter.
|
||||||
|
$post['body_nomarkup'] .= "<tinyboard ocr image $key>" . htmlspecialchars($value) . "</tinyboard>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!isset($dont_copy_file) || !$dont_copy_file) {
|
if (!isset($dont_copy_file) || !$dont_copy_file) {
|
||||||
if (isset($file['file_tmp'])) {
|
if (isset($file['file_tmp'])) {
|
||||||
if (!@rename($file['tmp_name'], $file['file'])) {
|
if (!@rename($file['tmp_name'], $file['file'])) {
|
||||||
|
@ -1598,6 +1634,11 @@ function handle_post(Context $ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do filters again if OCRing
|
||||||
|
if ($config['tesseract_ocr'] && !hasPermission($config['mod']['bypass_filters'], $board['uri']) && !$dropped_post) {
|
||||||
|
do_filters($ctx, $post);
|
||||||
|
}
|
||||||
|
|
||||||
if (!hasPermission($config['mod']['postunoriginal'], $board['uri']) && $config['robot_enable'] && checkRobot($post['body_nomarkup']) && !$dropped_post) {
|
if (!hasPermission($config['mod']['postunoriginal'], $board['uri']) && $config['robot_enable'] && checkRobot($post['body_nomarkup']) && !$dropped_post) {
|
||||||
undoImage($post);
|
undoImage($post);
|
||||||
if ($config['robot_mute']) {
|
if ($config['robot_mute']) {
|
||||||
|
|
BIN
static/banners/just-monika-opt.webp
Normal file
BIN
static/banners/just-monika-opt.webp
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
|
@ -380,7 +380,6 @@ form table tr td div.center {
|
||||||
|
|
||||||
.file {
|
.file {
|
||||||
float: left;
|
float: left;
|
||||||
min-width: 100px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.file:not(.multifile) .post-image {
|
.file:not(.multifile) .post-image {
|
||||||
|
@ -391,10 +390,6 @@ form table tr td div.center {
|
||||||
float: none;
|
float: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.file.multifile {
|
|
||||||
margin: 0 10px 0 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.file.multifile > p {
|
.file.multifile > p {
|
||||||
width: 0px;
|
width: 0px;
|
||||||
min-width: 100%;
|
min-width: 100%;
|
||||||
|
|
|
@ -16,13 +16,13 @@
|
||||||
border-width: 2px;
|
border-width: 2px;
|
||||||
margin-right: 15px;
|
margin-right: 15px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.introduction {
|
.introduction {
|
||||||
grid-column: 2 / 9;
|
grid-column: 2 / 9;
|
||||||
grid-row: 1;
|
grid-row: 1;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.content {
|
.content {
|
||||||
grid-column: 2 / 9;
|
grid-column: 2 / 9;
|
||||||
grid-row: 2;
|
grid-row: 2;
|
||||||
|
@ -35,7 +35,7 @@
|
||||||
gap: 20px;
|
gap: 20px;
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
}
|
}
|
||||||
|
|
||||||
.modlog {
|
.modlog {
|
||||||
width: 50%;
|
width: 50%;
|
||||||
text-align: left;
|
text-align: left;
|
||||||
|
@ -69,7 +69,7 @@
|
||||||
li a.system {
|
li a.system {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width:768px) {
|
@media (max-width:768px) {
|
||||||
body{
|
body{
|
||||||
display: grid;
|
display: grid;
|
||||||
|
@ -78,7 +78,7 @@
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.introduction {
|
.introduction {
|
||||||
grid-column: 1;
|
grid-column: 1;
|
||||||
grid-row: 1;
|
grid-row: 1;
|
||||||
|
@ -96,18 +96,17 @@
|
||||||
grid-column: 1;
|
grid-column: 1;
|
||||||
grid-row: 3;
|
grid-row: 3;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
word-break: break-all;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.modlog {
|
.modlog {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
table {
|
table {
|
||||||
table-layout: fixed;
|
table-layout: fixed;
|
||||||
}
|
}
|
||||||
|
|
||||||
table.modlog tr th {
|
table.modlog tr th {
|
||||||
white-space: normal;
|
white-space: normal;
|
||||||
word-wrap: break-word;
|
word-wrap: break-word;
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
.home-description {
|
.home-description {
|
||||||
margin: 20px auto 0 auto;
|
margin: 20px auto 0 auto;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
max-width: 700px;
|
max-width: 700px;"
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
{{ boardlist.top }}
|
{{ boardlist.top }}
|
||||||
|
|
0
tmp/tesseract/.gitkeep
Normal file
0
tmp/tesseract/.gitkeep
Normal file
Loading…
Add table
Add a link
Reference in a new issue