2015-08-31 23:59:08 +03:00
|
|
|
<?php
|
2017-02-11 18:16:56 +03:00
|
|
|
class FacebookBridge extends BridgeAbstract {
|
2015-08-31 23:59:08 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
const MAINTAINER = 'teromene';
|
|
|
|
const NAME = 'Facebook';
|
|
|
|
const URI = 'https://www.facebook.com/';
|
2016-09-25 18:04:28 +03:00
|
|
|
const CACHE_TIMEOUT = 300; // 5min
|
2017-02-11 18:16:56 +03:00
|
|
|
const DESCRIPTION = 'Input a page title or a profile log. For a profile log,
|
|
|
|
please insert the parameter as follow : myExamplePage/132621766841117';
|
2015-11-03 17:36:19 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
const PARAMETERS = array( array(
|
|
|
|
'u' => array(
|
|
|
|
'name' => 'Username',
|
|
|
|
'required' => true
|
2017-07-25 16:39:26 +03:00
|
|
|
),
|
|
|
|
'novideo' => array(
|
|
|
|
'name' => 'No Videos',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'required' => false,
|
|
|
|
'title' => 'Activate to remove posts including (facebook) videos'
|
2017-02-11 18:16:56 +03:00
|
|
|
)
|
|
|
|
));
|
2015-08-31 23:59:08 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
private $authorName = '';
|
2016-08-28 15:24:17 +03:00
|
|
|
|
2016-08-25 02:24:53 +03:00
|
|
|
public function collectData(){
|
2015-08-31 23:59:08 +03:00
|
|
|
|
2015-10-23 13:08:18 +03:00
|
|
|
//Extract a string using start and end delimiters
|
2017-02-11 18:16:56 +03:00
|
|
|
function extractFromDelimiters($string, $start, $end){
|
|
|
|
if(strpos($string, $start) !== false){
|
2015-10-23 13:08:18 +03:00
|
|
|
$section_retrieved = substr($string, strpos($string, $start) + strlen($start));
|
|
|
|
$section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
|
|
|
|
return $section_retrieved;
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-10-23 13:08:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//Utility function for cleaning a Facebook link
|
2017-02-11 18:16:56 +03:00
|
|
|
$unescape_fb_link = function($matches){
|
|
|
|
if(is_array($matches) && count($matches) > 1){
|
2015-10-23 13:08:18 +03:00
|
|
|
$link = $matches[1];
|
2017-02-11 18:16:56 +03:00
|
|
|
if(strpos($link, '/') === 0)
|
|
|
|
$link = self::URI . $link . '"';
|
|
|
|
if(strpos($link, 'facebook.com/l.php?u=') !== false)
|
|
|
|
$link = urldecode(extractFromDelimiters($link, 'facebook.com/l.php?u=', '&'));
|
|
|
|
return ' href="' . $link . '"';
|
2015-10-23 13:08:18 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-24 21:11:21 +03:00
|
|
|
//Utility function for converting facebook emoticons
|
2017-02-11 18:16:56 +03:00
|
|
|
$unescape_fb_emote = function($matches){
|
2015-10-24 21:11:21 +03:00
|
|
|
static $facebook_emoticons = array(
|
|
|
|
'smile' => ':)',
|
|
|
|
'frown' => ':(',
|
|
|
|
'tongue' => ':P',
|
|
|
|
'grin' => ':D',
|
|
|
|
'gasp' => ':O',
|
|
|
|
'wink' => ';)',
|
|
|
|
'pacman' => ':<',
|
|
|
|
'grumpy' => '>_<',
|
|
|
|
'unsure' => ':/',
|
|
|
|
'cry' => ':\'(',
|
|
|
|
'kiki' => '^_^',
|
|
|
|
'glasses' => '8-)',
|
|
|
|
'sunglasses' => 'B-)',
|
|
|
|
'heart' => '<3',
|
|
|
|
'devil' => ']:D',
|
|
|
|
'angel' => '0:)',
|
|
|
|
'squint' => '-_-',
|
|
|
|
'confused' => 'o_O',
|
|
|
|
'upset' => 'xD',
|
|
|
|
'colonthree' => ':3',
|
|
|
|
'like' => '👍');
|
|
|
|
$len = count($matches);
|
|
|
|
if ($len > 1)
|
|
|
|
for ($i = 1; $i < $len; $i++)
|
|
|
|
foreach ($facebook_emoticons as $name => $emote)
|
|
|
|
if ($matches[$i] === $name)
|
|
|
|
return $emote;
|
|
|
|
return $matches[0];
|
|
|
|
};
|
|
|
|
|
2016-01-23 23:53:23 +03:00
|
|
|
$html = null;
|
2015-08-31 23:59:08 +03:00
|
|
|
|
2016-01-23 23:53:23 +03:00
|
|
|
//Handle captcha response sent by the viewer
|
|
|
|
if (isset($_POST['captcha_response']))
|
|
|
|
{
|
|
|
|
if (session_status() == PHP_SESSION_NONE)
|
|
|
|
session_start();
|
|
|
|
if (isset($_SESSION['captcha_fields'], $_SESSION['captcha_action']))
|
|
|
|
{
|
|
|
|
$captcha_action = $_SESSION['captcha_action'];
|
|
|
|
$captcha_fields = $_SESSION['captcha_fields'];
|
|
|
|
$captcha_fields['captcha_response'] = preg_replace("/[^a-zA-Z0-9]+/", "", $_POST['captcha_response']);
|
|
|
|
$http_options = array(
|
|
|
|
'http' => array(
|
|
|
|
'method' => 'POST',
|
2017-02-11 18:16:56 +03:00
|
|
|
'user_agent' => ini_get('user_agent'),
|
|
|
|
'header' => array("Content-type:
|
|
|
|
application/x-www-form-urlencoded\r\nReferer: $captcha_action\r\nCookie: noscript=1\r\n"),
|
|
|
|
'content' => http_build_query($captcha_fields)
|
2016-01-23 23:53:23 +03:00
|
|
|
),
|
|
|
|
);
|
2017-02-11 18:16:56 +03:00
|
|
|
$context = stream_context_create($http_options);
|
2016-09-26 00:22:33 +03:00
|
|
|
$html = getContents($captcha_action, false, $context);
|
2017-02-11 18:16:56 +03:00
|
|
|
|
|
|
|
if($html === false){
|
|
|
|
returnServerError('Failed to submit captcha response back to Facebook');
|
|
|
|
}
|
2016-01-23 23:53:23 +03:00
|
|
|
unset($_SESSION['captcha_fields']);
|
|
|
|
$html = str_get_html($html);
|
|
|
|
}
|
|
|
|
unset($_SESSION['captcha_fields']);
|
|
|
|
unset($_SESSION['captcha_action']);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Retrieve page contents
|
2017-02-11 18:16:56 +03:00
|
|
|
if(is_null($html)){
|
2017-05-07 14:17:43 +03:00
|
|
|
$http_options = array(
|
|
|
|
'http' => array(
|
|
|
|
'method' => 'GET',
|
|
|
|
'user_agent' => ini_get('user_agent'),
|
|
|
|
'header' => 'Accept-Language: ' . getEnv('HTTP_ACCEPT_LANGUAGE') . "\r\n"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$context = stream_context_create($http_options);
|
2017-02-11 18:16:56 +03:00
|
|
|
if(!strpos($this->getInput('u'), "/")){
|
2017-05-07 14:17:43 +03:00
|
|
|
$html = getSimpleHTMLDOM(self::URI . urlencode($this->getInput('u')) . '?_fb_noscript=1',
|
|
|
|
false,
|
|
|
|
$context)
|
2017-02-11 18:16:56 +03:00
|
|
|
or returnServerError('No results for this query.');
|
2015-09-05 15:31:57 +03:00
|
|
|
} else {
|
2017-05-07 14:17:43 +03:00
|
|
|
$html = getSimpleHTMLDOM(self::URI . 'pages/' . $this->getInput('u') . '?_fb_noscript=1',
|
|
|
|
false,
|
|
|
|
$context)
|
2017-02-11 18:16:56 +03:00
|
|
|
or returnServerError('No results for this query.');
|
2015-09-05 15:31:57 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-02 14:49:36 +03:00
|
|
|
|
2016-01-23 23:53:23 +03:00
|
|
|
//Handle captcha form?
|
|
|
|
$captcha = $html->find('div.captcha_interstitial', 0);
|
|
|
|
if (!is_null($captcha))
|
|
|
|
{
|
|
|
|
//Save form for submitting after getting captcha response
|
|
|
|
if (session_status() == PHP_SESSION_NONE)
|
|
|
|
session_start();
|
|
|
|
$captcha_fields = array();
|
|
|
|
foreach ($captcha->find('input, button') as $input)
|
|
|
|
$captcha_fields[$input->name] = $input->value;
|
|
|
|
$_SESSION['captcha_fields'] = $captcha_fields;
|
2017-02-02 19:18:23 +03:00
|
|
|
$_SESSION['captcha_action'] = $captcha->find('form', 0)->action;
|
2016-01-23 23:53:23 +03:00
|
|
|
|
|
|
|
//Show captcha filling form to the viewer, proxying the captcha image
|
2016-09-26 00:22:33 +03:00
|
|
|
$img = base64_encode(getContents($captcha->find('img', 0)->src));
|
2017-02-11 18:16:56 +03:00
|
|
|
header('HTTP/1.1 500 ' . Http::getMessageForCode(500));
|
2016-01-23 23:53:23 +03:00
|
|
|
header('Content-Type: text/html');
|
2017-02-11 18:16:56 +03:00
|
|
|
$message = <<<EOD
|
|
|
|
<form method="post" action="?{$_SERVER['QUERY_STRING']}">
|
|
|
|
<h2>Facebook captcha challenge</h2>
|
|
|
|
<p>Unfortunately, rss-bridge cannot fetch the requested page.<br />
|
|
|
|
Facebook wants rss-bridge to resolve the following captcha:</p>
|
|
|
|
<p><img src="data:image/png;base64,{$img}" /></p>
|
|
|
|
<p><b>Response:</b> <input name="captcha_response" placeholder="please fill in" />
|
|
|
|
<input type="submit" value="Submit!" /></p>
|
|
|
|
</form>
|
|
|
|
EOD;
|
|
|
|
die($message);
|
2016-01-23 23:53:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//No captcha? We can carry on retrieving page contents :)
|
2017-02-11 18:16:56 +03:00
|
|
|
$element = $html
|
|
|
|
->find('#pagelet_timeline_main_column')[0]
|
|
|
|
->children(0)
|
|
|
|
->children(0)
|
|
|
|
->children(0)
|
|
|
|
->next_sibling()
|
|
|
|
->children(0);
|
2015-08-31 23:59:08 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
if(isset($element)){
|
2015-08-31 23:59:08 +03:00
|
|
|
|
2015-09-05 15:31:57 +03:00
|
|
|
$author = str_replace(' | Facebook', '', $html->find('title#pageTitle', 0)->innertext);
|
2017-02-11 18:16:56 +03:00
|
|
|
$profilePic = 'https://graph.facebook.com/'
|
|
|
|
. $this->getInput('u')
|
|
|
|
. '/picture?width=200&height=200';
|
|
|
|
|
2016-08-28 15:24:17 +03:00
|
|
|
$this->authorName = $author;
|
2015-08-31 23:59:08 +03:00
|
|
|
|
2017-04-10 14:04:38 +03:00
|
|
|
foreach($element->children() as $cell){
|
|
|
|
// Manage summary posts
|
|
|
|
if(strpos($cell->class, '_3xaf') !== false){
|
|
|
|
$posts = $cell->children();
|
|
|
|
} else {
|
|
|
|
$posts = array($cell);
|
|
|
|
}
|
2015-09-05 15:31:57 +03:00
|
|
|
|
2017-04-10 14:04:38 +03:00
|
|
|
foreach($posts as $post){
|
|
|
|
|
2017-07-25 16:39:26 +03:00
|
|
|
// (optionally) skip posts that include facebook videos
|
|
|
|
if($this->getInput('novideo') && !empty($post->find('[aria-label=Video]')))
|
|
|
|
continue;
|
|
|
|
|
2017-04-10 14:04:38 +03:00
|
|
|
$item = array();
|
|
|
|
|
|
|
|
if(count($post->find('abbr')) > 0){
|
|
|
|
|
|
|
|
//Retrieve post contents
|
|
|
|
$content = preg_replace(
|
|
|
|
'/(?i)><div class=\"clearfix([^>]+)>(.+?)div\ class=\"userContent\"/i',
|
|
|
|
'',
|
|
|
|
$post);
|
|
|
|
|
|
|
|
$content = preg_replace(
|
|
|
|
'/(?i)><div class=\"_59tj([^>]+)>(.+?)<\/div><\/div><a/i',
|
|
|
|
'',
|
|
|
|
$content);
|
|
|
|
|
|
|
|
$content = preg_replace(
|
|
|
|
'/(?i)><div class=\"_3dp([^>]+)>(.+?)div\ class=\"[^u]+userContent\"/i',
|
|
|
|
'',
|
|
|
|
$content);
|
|
|
|
|
|
|
|
$content = preg_replace(
|
|
|
|
'/(?i)><div class=\"_4l5([^>]+)>(.+?)<\/div>/i',
|
|
|
|
'',
|
|
|
|
$content);
|
|
|
|
|
|
|
|
//Remove html nodes, keep only img, links, basic formatting
|
|
|
|
$content = strip_tags($content, '<a><img><i><u><br><p>');
|
|
|
|
|
|
|
|
//Adapt link hrefs: convert relative links into absolute links and bypass external link redirection
|
|
|
|
$content = preg_replace_callback('/ href=\"([^"]+)\"/i', $unescape_fb_link, $content);
|
|
|
|
|
|
|
|
//Clean useless html tag properties and fix link closing tags
|
|
|
|
foreach (array(
|
|
|
|
'onmouseover',
|
|
|
|
'onclick',
|
|
|
|
'target',
|
|
|
|
'ajaxify',
|
|
|
|
'tabindex',
|
|
|
|
'class',
|
|
|
|
'style',
|
|
|
|
'data-[^=]*',
|
|
|
|
'aria-[^=]*',
|
|
|
|
'role',
|
|
|
|
'rel',
|
|
|
|
'id') as $property_name)
|
|
|
|
$content = preg_replace('/ ' . $property_name . '=\"[^"]*\"/i', '', $content);
|
|
|
|
$content = preg_replace('/<\/a [^>]+>/i', '</a>', $content);
|
|
|
|
|
|
|
|
//Convert textual representation of emoticons eg
|
|
|
|
//"<i><u>smile emoticon</u></i>" back to ASCII emoticons eg ":)"
|
|
|
|
$content = preg_replace_callback(
|
|
|
|
'/<i><u>([^ <>]+) ([^<>]+)<\/u><\/i>/i',
|
|
|
|
$unescape_fb_emote,
|
|
|
|
$content
|
|
|
|
);
|
|
|
|
|
|
|
|
//Retrieve date of the post
|
|
|
|
$date = $post->find("abbr")[0];
|
|
|
|
if(isset($date) && $date->hasAttribute('data-utime')){
|
|
|
|
$date = $date->getAttribute('data-utime');
|
|
|
|
} else {
|
|
|
|
$date = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Build title from username and content
|
|
|
|
$title = $author;
|
|
|
|
if(strlen($title) > 24)
|
|
|
|
$title = substr($title, 0, strpos(wordwrap($title, 24), "\n")) . '...';
|
|
|
|
$title = $title . ' | ' . strip_tags($content);
|
|
|
|
if(strlen($title) > 64)
|
|
|
|
$title = substr($title, 0, strpos(wordwrap($title, 64), "\n")) . '...';
|
|
|
|
|
|
|
|
//Build and add final item
|
|
|
|
$item['uri'] = self::URI . $post->find('abbr')[0]->parent()->getAttribute('href');
|
|
|
|
$item['content'] = $content;
|
|
|
|
$item['title'] = $title;
|
|
|
|
$item['author'] = $author;
|
|
|
|
$item['timestamp'] = $date;
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
2015-09-02 14:49:36 +03:00
|
|
|
}
|
|
|
|
}
|
2015-08-31 23:59:08 +03:00
|
|
|
}
|
2016-01-23 23:53:23 +03:00
|
|
|
}
|
2015-08-31 23:59:08 +03:00
|
|
|
|
2017-02-11 18:16:56 +03:00
|
|
|
public function getName(){
|
2017-02-15 00:20:55 +03:00
|
|
|
if(!empty($this->authorName)){
|
|
|
|
return isset($this->extraInfos['name']) ? $this->extraInfos['name'] : $this->authorName
|
|
|
|
. ' - Facebook Bridge';
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getName();
|
2015-09-05 15:31:57 +03:00
|
|
|
}
|
2015-08-31 23:59:08 +03:00
|
|
|
}
|