xExtensionTGLinkFixer/extension.php

46 lines
1.2 KiB
PHP

<?php
/**
* Name: Telegram Link Fixer
* Description: Rewrite the <link> from the feed with one extracted from <description>.
* Author: Your Name
* Version: 1.0
*/
class TelegramLinkFixerExtension extends Minz_Extension
{
/**
* Called automatically at extension load-time. Set up hooks here.
*/
public function init()
{
// Hook into FreshRSS just before each entry is saved to database:
$this->registerHook('entry_before_insert', array($this, 'fixLink'));
}
/**
* Our main function to rewrite the <link> from <description>.
*/
public static function fixLink($entry)
{
// Check if it's a telegram link
$link = $entry->link();
if (strpos($link, 't.me') === false) {
return; // skip if not a telegram link
}
$description = $entry->description();
if (empty($description)) {
return;
}
// Look specifically for Telegraph links
if (preg_match('/<a[^>]*?href="(https:\/\/telegra\.ph\/[^"]+)"[^>]*>Telegraph<\/a>/i', $description, $matches)) {
$telegraphLink = $matches[1];
if (!empty($telegraphLink)) {
$entry->_link($telegraphLink);
}
}
}
}