xExtension-wallabag/extension.php

108 lines
3.7 KiB
PHP

<?php
class WallabagExtension extends Minz_Extension {
const USE_TAGS_DEFAULT = '1';
const ADD_TAGS_DEFAULT = 'freshrss';
public function init(): void {
$this->registerHook('entry_before_insert', [$this, 'wallabag']);
$save = false;
if (is_null(FreshRSS_Context::$user_conf->wallabag_use_tags)) {
FreshRSS_Context::$user_conf->wallabag_use_tags = self::USE_TAGS_DEFAULT;
$save = true;
}
if (is_null(FreshRSS_Context::$user_conf->wallabag_add_tags)) {
FreshRSS_Context::$user_conf->wallabag_add_tags = self::ADD_TAGS_DEFAULT;
$save = true;
}
if ($save) {
FreshRSS_Context::$user_conf->save();
}
}
public function handleConfigureAction() {
$this->registerTranslates();
if (Minz_Request::isPost()) {
FreshRSS_Context::$user_conf->wallabag_endpoint = Minz_Request::param('wallabag_endpoint');
FreshRSS_Context::$user_conf->wallabag_client_id = Minz_Request::param('wallabag_client_id');
FreshRSS_Context::$user_conf->wallabag_client_secret = Minz_Request::param('wallabag_client_secret');
FreshRSS_Context::$user_conf->wallabag_username = Minz_Request::param('wallabag_username');
FreshRSS_Context::$user_conf->wallabag_password = Minz_Request::param('wallabag_password');
FreshRSS_Context::$user_conf->wallabag_use_tags = Minz_Request::param('wallabag_use_tags', '');
FreshRSS_Context::$user_conf->wallabag_add_tags = Minz_Request::param('wallabag_add_tags', '');
$wallabag_feeds = Minz_Request::paramArray('wallabag_feeds');
if (empty($wallabag_feeds)) {
$wallabag_feeds = array(Minz_Request::param('wallabag_feeds'));
}
FreshRSS_Context::$user_conf->wallabag_feeds = $wallabag_feeds;
FreshRSS_Context::$user_conf->save();
}
}
public function oauth($refresh_token = null) {
$endpoint = FreshRSS_Context::$user_conf->wallabag_endpoint;
$client_id = FreshRSS_Context::$user_conf->wallabag_client_id;
$client_secret = FreshRSS_Context::$user_conf->wallabag_client_secret;
$username = FreshRSS_Context::$user_conf->wallabag_username;
$password = FreshRSS_Context::$user_conf->wallabag_password;
if (is_null($endpoint) || is_null($client_id) || is_null($client_secret) || is_null($password) || is_null($username)) {
return null;
}
if (is_null($refresh_token)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, join_path($endpoint, 'oauth/v2/token'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'grant_type' => 'password',
'client_id' => $client_id,
'client_secret' => $client_secret,
'username' => $username,
'password' => $password,
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return(json_decode($result, true));
}
}
public function wallabag(FreshRSS_Entry $entry): FreshRSS_Entry {
$endpoint = FreshRSS_Context::$user_conf->wallabag_endpoint;
if (is_null($endpoint)) {
return $entry;
}
$feeds = FreshRSS_Context::$user_conf->wallabag_feeds;
$use_tags = FreshRSS_Context::$user_conf->wallabag_use_tags;
$add_tags = explode(',', FreshRSS_Context::$user_conf->wallabag_add_tags);
if (in_array($entry->feedId(), $feeds)) {
$token = $this->oauth()['access_token'];
$tags = $add_tags;
if ($use_tags) {
$tags = array_merge($tags, $entry->tags());
}
$link = $entry->link();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, join_path($endpoint, 'api/entries'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization:Bearer $token",
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'url' => $link,
'tags' => join(',', $tags),
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
}
return $entry;
}
}