2018-02-13 23:46:33 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class IPBBridge extends FeedExpander
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
const NAME = 'IPB Bridge';
|
|
|
|
const URI = 'https://www.invisionpower.com';
|
|
|
|
const DESCRIPTION = 'Returns feeds for forums powered by IPB';
|
|
|
|
const MAINTAINER = 'logmanoriginal';
|
|
|
|
const PARAMETERS = [
|
2022-07-01 16:10:30 +03:00
|
|
|
[
|
|
|
|
'uri' => [
|
2018-02-13 23:46:33 +03:00
|
|
|
'name' => 'URI',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'title' => 'Insert forum, subforum or topic URI',
|
|
|
|
'exampleValue' => 'https://invisioncommunity.com/forums/forum/499-feedback-and-ideas/'
|
2022-07-01 16:10:30 +03:00
|
|
|
],
|
2018-02-13 23:46:33 +03:00
|
|
|
'limit' => [
|
|
|
|
'name' => 'Limit',
|
|
|
|
'type' => 'number',
|
|
|
|
'required' => false,
|
|
|
|
'title' => 'Specifies the number of items to return on each request (-1: all)',
|
2018-04-06 23:13:52 +03:00
|
|
|
'defaultValue' => 10
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2018-02-13 23:46:33 +03:00
|
|
|
const CACHE_TIMEOUT = 3600;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// Constants for internal use
|
|
|
|
const FORUM_TYPE_LIST_FILTER = '.cForumTopicTable';
|
|
|
|
const FORUM_TYPE_TABLE_FILTER = '#forum_table';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
const TOPIC_TYPE_ARTICLE = 'article';
|
|
|
|
const TOPIC_TYPE_DIV = 'div.post_block';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
public function getURI()
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
return $this->getInput('uri') ?: parent::getURI();
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
public function collectData()
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
// The URI cannot be the mainpage (or anything related)
|
|
|
|
switch (parse_url($this->getInput('uri'), PHP_URL_PATH)) {
|
|
|
|
case null:
|
|
|
|
case '/index.php':
|
|
|
|
returnClientError('Provided URI is invalid!');
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// Sanitize the URI (because else it won't work)
|
|
|
|
$uri = rtrim($this->getInput('uri'), '/'); // No trailing slashes!
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// Forums might provide feeds, though that's optional *facepalm*
|
|
|
|
// Let's check if there is a valid feed available
|
|
|
|
$headers = get_headers($uri . '.xml');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
if ($headers[0] === 'HTTP/1.1 200 OK') { // Heureka! It's a valid feed!
|
2022-03-25 16:49:57 +03:00
|
|
|
return $this->collectExpandableDatas($uri . '.xml');
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// No valid feed, so do it the hard way
|
2022-01-02 12:36:09 +03:00
|
|
|
$html = getSimpleHTMLDOM($uri);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$limit = $this->getInput('limit');
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// Determine if this is a topic or a forum
|
|
|
|
switch (true) {
|
|
|
|
case $this->isTopic($html):
|
|
|
|
$this->collectTopic($html, $limit);
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
2022-06-24 19:29:35 +03:00
|
|
|
case $this->isForum($html):
|
2018-02-13 23:46:33 +03:00
|
|
|
$this->collectForum($html);
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
|
|
|
default:
|
2018-02-13 23:46:33 +03:00
|
|
|
returnClientError('Unknown type!');
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
private function isForum($html)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
return !is_null($html->find('div[data-controller*=forums.front.forum.forumPage]', 0))
|
|
|
|
|| !is_null($html->find(static::FORUM_TYPE_TABLE_FILTER, 0));
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
private function isTopic($html)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
return !is_null($html->find('div[data-controller*=core.front.core.commentFeed]', 0))
|
|
|
|
|| !is_null($html->find(static::TOPIC_TYPE_DIV, 0));
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
private function collectForum($html)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
// There are multiple forum designs in use (depends on version?)
|
|
|
|
// 1 - Uses an ordered list (based on https://invisioncommunity.com/forums)
|
|
|
|
// 2 - Uses a table (based on https://onehallyu.com)
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
switch (true) {
|
|
|
|
case !is_null($html->find(static::FORUM_TYPE_LIST_FILTER, 0)):
|
|
|
|
$this->collectForumList($html);
|
|
|
|
break;
|
|
|
|
case !is_null($html->find(static::FORUM_TYPE_TABLE_FILTER, 0)):
|
|
|
|
$this->collectForumTable($html);
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
|
|
|
default:
|
2018-02-13 23:46:33 +03:00
|
|
|
returnClientError('Unknown forum format!');
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
private function collectForumList($html)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
foreach ($html->find(static::FORUM_TYPE_LIST_FILTER, 0)->children() as $row) {
|
|
|
|
// Columns: Title, Statistics, Last modified
|
2022-07-01 16:10:30 +03:00
|
|
|
$item = [];
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$item['uri'] = $row->find('a', 0)->href;
|
|
|
|
$item['title'] = $row->find('a', 0)->title;
|
|
|
|
$item['author'] = $row->find('a', 1)->innertext;
|
|
|
|
$item['timestamp'] = strtotime($row->find('time', 0)->getAttribute('datetime'));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
private function collectForumTable($html)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
foreach ($html->find(static::FORUM_TYPE_TABLE_FILTER, 0)->children() as $row) {
|
|
|
|
// Columns: Icon, Content, Preview, Statistics, Last modified
|
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// Skip header row
|
|
|
|
if (!is_null($row->find('th', 0))) {
|
|
|
|
continue;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$item['uri'] = $row->find('a', 0)->href;
|
|
|
|
$item['title'] = $row->find('.title', 0)->plaintext;
|
|
|
|
$item['timestamp'] = strtotime($row->find('[itemprop=dateCreated]', 0)->plaintext);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
private function collectTopic($html, $limit)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
// There are multiple topic designs in use (depends on version?)
|
|
|
|
// 1 - Uses articles (based on https://invisioncommunity.com/forums)
|
|
|
|
// 2 - Uses divs (based on https://onehallyu.com)
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
switch (true) {
|
|
|
|
case !is_null($html->find(static::TOPIC_TYPE_ARTICLE, 0)):
|
|
|
|
$this->collectTopicHistory($html, $limit, 'collectTopicArticle');
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
2018-02-13 23:46:33 +03:00
|
|
|
case !is_null($html->find(static::TOPIC_TYPE_DIV, 0)):
|
|
|
|
$this->collectTopicHistory($html, $limit, 'collectTopicDiv');
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
|
|
|
default:
|
2018-02-13 23:46:33 +03:00
|
|
|
returnClientError('Unknown topic format!');
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
private function collectTopicHistory($html, $limit, $callback)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
// Make sure the callback is valid!
|
|
|
|
if (!method_exists($this, $callback)) {
|
|
|
|
returnServerError('Unknown function (\'' . $callback . '\')!');
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$next = null; // Holds the URI of the next page
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-04-06 23:13:52 +03:00
|
|
|
while (true) {
|
|
|
|
$next = $this->$callback($html, is_null($next));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-04-06 23:13:52 +03:00
|
|
|
if (is_null($next) || ($limit > 0 && count($this->items) >= $limit)) {
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-04-06 23:13:52 +03:00
|
|
|
$html = getSimpleHTMLDOMCached($next);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-04-06 23:13:52 +03:00
|
|
|
// We might have more items than specified, remove excess
|
|
|
|
$this->items = array_slice($this->items, 0, $limit);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
private function collectTopicArticle($html, $firstrun = true)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
$title = $html->find('h1.ipsType_pageTitle', 0)->plaintext;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// Are we on last page?
|
|
|
|
if ($firstrun && !is_null($html->find('.ipsPagination', 0))) {
|
|
|
|
$last = $html->find('.ipsPagination_last a', 0)->{'data-page'};
|
|
|
|
$active = $html->find('.ipsPagination_active a', 0)->{'data-page'};
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
if ($active !== $last) {
|
|
|
|
// Load last page into memory (cached)
|
|
|
|
$html = getSimpleHTMLDOMCached($html->find('.ipsPagination_last a', 0)->href);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
foreach (array_reverse($html->find(static::TOPIC_TYPE_ARTICLE)) as $article) {
|
2022-07-01 16:10:30 +03:00
|
|
|
$item = [];
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$item['uri'] = $article->find('time', 0)->parent()->href;
|
|
|
|
$item['author'] = $article->find('aside a', 0)->plaintext;
|
|
|
|
$item['title'] = $item['author'] . ' - ' . $title;
|
|
|
|
$item['timestamp'] = strtotime($article->find('time', 0)->getAttribute('datetime'));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$content = $article->find('[data-role=commentContent]', 0);
|
|
|
|
$content = $this->scaleImages($content);
|
|
|
|
$item['content'] = $this->fixContent($content);
|
|
|
|
$item['enclosures'] = $this->findImages($article->find('[data-role=commentContent]', 0)) ?: null;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// Return whatever page comes next (previous, as we add in inverse order)
|
|
|
|
// Do we have a previous page? (inactive means no)
|
|
|
|
if (!is_null($html->find('li[class=ipsPagination_prev ipsPagination_inactive]', 0))) {
|
|
|
|
return null; // No, or no more
|
|
|
|
} elseif (!is_null($html->find('li[class=ipsPagination_prev]', 0))) {
|
|
|
|
return $html->find('.ipsPagination_prev a', 0)->href;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
return null;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
private function collectTopicDiv($html, $firstrun = true)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
$title = $html->find('h1.ipsType_pagetitle', 0)->plaintext;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// Are we on last page?
|
|
|
|
if ($firstrun && !is_null($html->find('.pagination', 0))) {
|
|
|
|
$active = $html->find('li[class=page active]', 0)->plaintext;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// There are two ways the 'last' page is displayed:
|
|
|
|
// - With a distict 'last' button (only if there are enough pages)
|
|
|
|
// - With a button for each page (use last button)
|
|
|
|
if (!is_null($html->find('li.last', 0))) {
|
|
|
|
$last = $html->find('li.last a', 0);
|
2022-07-01 16:10:30 +03:00
|
|
|
} else {
|
2018-02-13 23:46:33 +03:00
|
|
|
$last = $html->find('li[class=page] a', -1);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
if ($active !== $last->plaintext) {
|
|
|
|
// Load last page into memory (cached)
|
|
|
|
$html = getSimpleHTMLDOMCached($last->href);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
foreach (array_reverse($html->find(static::TOPIC_TYPE_DIV)) as $article) {
|
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$item['uri'] = $article->find('a[rel=bookmark]', 0)->href;
|
|
|
|
$item['author'] = $article->find('.author', 0)->plaintext;
|
|
|
|
$item['title'] = $item['author'] . ' - ' . $title;
|
|
|
|
$item['timestamp'] = strtotime($article->find('.published', 0)->getAttribute('title'));
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$content = $article->find('[itemprop=commentText]', 0);
|
|
|
|
$content = $this->scaleImages($content);
|
|
|
|
$item['content'] = $this->fixContent($content);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$item['enclosures'] = $this->findImages($article->find('.post_body', 0)) ?: null;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
// Return whatever page comes next (previous, as we add in inverse order)
|
|
|
|
// Do we have a previous page?
|
|
|
|
if (!is_null($html->find('li.prev', 0))) {
|
|
|
|
return $html->find('li.prev a', 0)->href;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
return null;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
/** Returns all images from the provide HTML DOM */
|
|
|
|
private function findImages($html)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
$images = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
foreach ($html->find('img') as $img) {
|
|
|
|
$images[] = $img->src;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
return $images;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
/** Sets the maximum width and height for all images */
|
|
|
|
private function scaleImages($html, $width = 400, $height = 400)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2018-02-13 23:46:33 +03:00
|
|
|
foreach ($html->find('img') as $img) {
|
|
|
|
$img->style = "max-width: {$width}px; max-height: {$height}px;";
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
return $html;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-02-13 23:46:33 +03:00
|
|
|
/** Removes all unnecessary tags and adds formatting */
|
|
|
|
private function fixContent($html)
|
|
|
|
{
|
|
|
|
// Restore quote highlighting
|
|
|
|
foreach ($html->find('blockquote') as $quote) {
|
|
|
|
$quote->style = <<<EOD
|
|
|
|
padding: 0px 15px;
|
|
|
|
border-width: 1px 1px 1px 2px;
|
|
|
|
border-style: solid;
|
|
|
|
border-color: #ededed #e8e8e8 #dbdbdb #666666;
|
|
|
|
background: #fbfbfb;
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove unnecessary tags
|
|
|
|
$content = strip_tags(
|
|
|
|
$html->innertext,
|
|
|
|
'<p><a><img><ol><ul><li><table><tr><th><td><strong><blockquote><br><hr><h>'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
}
|