2017-02-08 12:21:59 +01:00
|
|
|
<?php
|
2017-02-11 16:16:56 +01:00
|
|
|
class FB2Bridge extends BridgeAbstract {
|
2017-02-08 12:21:59 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
const MAINTAINER = 'teromene';
|
|
|
|
const NAME = 'Facebook Alternate';
|
|
|
|
const URI = 'https://www.facebook.com/';
|
2017-02-08 12:21:59 +01:00
|
|
|
const CACHE_TIMEOUT = 1000;
|
2017-02-11 16:16:56 +01:00
|
|
|
const DESCRIPTION = 'Input a page title or a profile log. For a profile log,
|
|
|
|
please insert the parameter as follow : myExamplePage/132621766841117';
|
2017-02-08 12:21:59 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
const PARAMETERS = array( array(
|
|
|
|
'u' => array(
|
|
|
|
'name' => 'Username',
|
|
|
|
'required' => true
|
|
|
|
)
|
|
|
|
));
|
2017-02-08 12:21:59 +01:00
|
|
|
|
2018-10-26 18:07:34 +02:00
|
|
|
public function getIcon() {
|
|
|
|
return 'https://static.xx.fbcdn.net/rsrc.php/yo/r/iRmz9lCMBD2.ico';
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
public function collectData(){
|
2017-02-08 12:21:59 +01:00
|
|
|
|
|
|
|
//Utility function for cleaning a Facebook link
|
2017-02-11 16:16:56 +01:00
|
|
|
$unescape_fb_link = function($matches){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(is_array($matches) && count($matches) > 1) {
|
2017-02-08 12:21:59 +01:00
|
|
|
$link = $matches[1];
|
2017-02-11 16:16:56 +01:00
|
|
|
if(strpos($link, '/') === 0)
|
2018-09-15 15:16:15 +02:00
|
|
|
$link = self::URI . substr($link, 1);
|
2017-02-11 16:16:56 +01:00
|
|
|
if(strpos($link, 'facebook.com/l.php?u=') !== false)
|
|
|
|
$link = urldecode(extractFromDelimiters($link, 'facebook.com/l.php?u=', '&'));
|
|
|
|
return ' href="' . $link . '"';
|
2017-02-08 12:21:59 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//Utility function for converting facebook emoticons
|
2017-02-11 16:16:56 +01:00
|
|
|
$unescape_fb_emote = function($matches){
|
2017-02-08 12:21:59 +01: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];
|
|
|
|
};
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if($this->getInput('u') !== null) {
|
2017-02-11 16:16:56 +01:00
|
|
|
$page = 'https://touch.facebook.com/' . $this->getInput('u');
|
2017-02-08 12:21:59 +01:00
|
|
|
$cookies = $this->getCookies($page);
|
2018-10-18 18:07:07 +02:00
|
|
|
$pageInfo = $this->getPageInfos($page, $cookies);
|
2017-02-08 12:21:59 +01:00
|
|
|
|
2018-10-18 18:07:07 +02:00
|
|
|
if($pageInfo['userId'] === null) {
|
2017-02-11 16:16:56 +01:00
|
|
|
echo <<<EOD
|
|
|
|
Unable to get the page id. You should consider getting the ID by hand, then importing it into FB2Bridge
|
2017-06-15 12:51:11 +02:00
|
|
|
EOD;
|
|
|
|
die();
|
2018-10-18 18:07:07 +02:00
|
|
|
} elseif($pageInfo['userId'] == -1) {
|
2017-06-15 12:51:11 +02:00
|
|
|
echo <<<EOD
|
|
|
|
This page is not accessible without being logged in.
|
2017-02-11 16:16:56 +01:00
|
|
|
EOD;
|
2017-02-08 12:21:59 +01:00
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Build the string for the first request
|
2018-10-18 18:07:07 +02:00
|
|
|
$requestString = 'https://touch.facebook.com/page_content_list_view/more/?page_id='
|
|
|
|
. $pageInfo['userId']
|
2018-10-18 18:44:11 +02:00
|
|
|
. '&start_cursor=1&num_to_fetch=105&surface_type=timeline';
|
2018-07-10 03:29:47 +02:00
|
|
|
$fileContent = getContents($requestString);
|
2017-02-08 12:21:59 +01:00
|
|
|
$html = $this->buildContent($fileContent);
|
2018-10-18 18:07:07 +02:00
|
|
|
$author = $pageInfo['username'];
|
2017-06-15 12:51:11 +02:00
|
|
|
|
2018-06-29 23:55:33 +02:00
|
|
|
foreach($html->find('article') as $content) {
|
2017-02-08 12:21:59 +01:00
|
|
|
|
|
|
|
$item = array();
|
2018-10-16 18:34:39 +02:00
|
|
|
preg_match('/publish_time\\\":([0-9]+),/', $content->getAttribute('data-store', 0), $match);
|
|
|
|
if(isset($match[1]))
|
|
|
|
$timestamp = $match[1];
|
|
|
|
else
|
|
|
|
$timestamp = 0;
|
|
|
|
|
|
|
|
$item['uri'] = html_entity_decode('http://touch.facebook.com'
|
|
|
|
. $content->find("div[class='_52jc _5qc4 _24u0 _36xo']", 0)->find('a', 0)->getAttribute('href'), ENT_QUOTES);
|
2017-02-08 12:21:59 +01:00
|
|
|
|
2018-10-18 21:10:02 +02:00
|
|
|
//Decode images
|
|
|
|
$imagecleaned = preg_replace_callback('/<i [^>]* style="[^"]*url\(\'(.*?)\'\).*?><\/i>/m', function ($matches) {
|
|
|
|
return "<img src='" . str_replace(['\\3a ', '\\3d ', '\\26 '], [':', '=', '&'], $matches[1]) . "' />";
|
|
|
|
}, $content);
|
|
|
|
$content = str_get_html($imagecleaned);
|
|
|
|
|
2018-06-29 23:55:33 +02:00
|
|
|
if($content->find('header', 0) !== null) {
|
|
|
|
$content->find('header', 0)->innertext = '';
|
2017-06-15 12:42:59 +02:00
|
|
|
}
|
2017-06-15 12:51:11 +02:00
|
|
|
|
2018-06-29 23:55:33 +02:00
|
|
|
if($content->find('footer', 0) !== null) {
|
|
|
|
$content->find('footer', 0)->innertext = '';
|
2017-06-15 12:42:59 +02:00
|
|
|
}
|
2017-02-08 12:21:59 +01:00
|
|
|
|
2018-10-16 19:02:47 +02:00
|
|
|
// Replace emoticon images by their textual representation (part of the span)
|
|
|
|
foreach($content->find('span[title*="emoticon"]') as $emoticon) {
|
|
|
|
$emoticon->innertext = $emoticon->find('span[aria-hidden="true"]', 0)->innertext;
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:21:59 +01:00
|
|
|
//Remove html nodes, keep only img, links, basic formatting
|
2018-10-18 21:10:02 +02:00
|
|
|
$content = strip_tags($content, '<a><img><i><u><br><p><h3><h4><section>');
|
2017-02-08 12:21:59 +01:00
|
|
|
|
|
|
|
//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
|
2017-02-11 16:16:56 +01:00
|
|
|
foreach (array(
|
|
|
|
'onmouseover',
|
|
|
|
'onclick',
|
|
|
|
'target',
|
|
|
|
'ajaxify',
|
|
|
|
'tabindex',
|
|
|
|
'class',
|
|
|
|
'data-[^=]*',
|
|
|
|
'aria-[^=]*',
|
|
|
|
'role',
|
|
|
|
'rel',
|
|
|
|
'id') as $property_name)
|
|
|
|
$content = preg_replace('/ ' . $property_name . '=\"[^"]*\"/i', '', $content);
|
2017-02-08 12:21:59 +01:00
|
|
|
$content = preg_replace('/<\/a [^>]+>/i', '</a>', $content);
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
//Convert textual representation of emoticons eg
|
|
|
|
// "<i><u>smile emoticon</u></i>" back to ASCII emoticons eg ":)"
|
2017-02-08 12:21:59 +01:00
|
|
|
$content = preg_replace_callback('/<i><u>([^ <>]+) ([^<>]+)<\/u><\/i>/i', $unescape_fb_emote, $content);
|
|
|
|
|
2018-10-18 18:07:07 +02:00
|
|
|
//Remove the "...Plus" tag
|
|
|
|
$content = preg_replace(
|
|
|
|
'/… (<span>|)<a href="https:\/\/www\.facebook\.com\/story\.php\?story_fbid=.*?<\/a>/m',
|
|
|
|
'', $content, 1);
|
|
|
|
|
2018-10-18 21:10:02 +02:00
|
|
|
//Remove tracking images
|
|
|
|
$content = preg_replace('/<img src=\'.*?safe_image\.php.*?\' \/>/m', '', $content);
|
|
|
|
|
|
|
|
//Remove the double section tags
|
|
|
|
$content = str_replace(['<section><section>', '</section></section>'], ['<section>', '</section>'], $content);
|
|
|
|
|
|
|
|
//Move the section tag link upper, if it is down
|
|
|
|
$content = str_get_html($content);
|
|
|
|
$sectionContent = $content->find('section', 0);
|
|
|
|
if($sectionContent != null) {
|
|
|
|
$sectionLink = $sectionContent->nextSibling();
|
|
|
|
if($sectionLink != null) {
|
|
|
|
$fullLink = '<a href="' . $sectionLink->getAttribute('href') . '">' . $sectionContent->innertext . '</a>';
|
|
|
|
$sectionContent->innertext = $fullLink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Move the href tag upper if it is inside the section
|
|
|
|
foreach($content->find('section > a') as $sectionToFix) {
|
|
|
|
$sectionLink = $sectionToFix->getAttribute('href');
|
|
|
|
$section = $sectionToFix->parent();
|
|
|
|
$section->outertext = '<a href="' . $sectionLink . '">' . $section . '</a>';
|
|
|
|
}
|
|
|
|
|
2018-10-16 18:34:39 +02:00
|
|
|
$item['content'] = html_entity_decode($content, ENT_QUOTES);
|
2017-02-08 12:21:59 +01:00
|
|
|
|
|
|
|
$title = $author;
|
|
|
|
if (strlen($title) > 24)
|
2017-02-11 16:16:56 +01:00
|
|
|
$title = substr($title, 0, strpos(wordwrap($title, 24), "\n")) . '...';
|
|
|
|
$title = $title . ' | ' . strip_tags($content);
|
2017-02-08 12:21:59 +01:00
|
|
|
if (strlen($title) > 64)
|
2017-02-11 16:16:56 +01:00
|
|
|
$title = substr($title, 0, strpos(wordwrap($title, 64), "\n")) . '...';
|
2017-02-08 12:21:59 +01:00
|
|
|
|
2018-10-16 18:34:39 +02:00
|
|
|
$item['title'] = html_entity_decode($title, ENT_QUOTES);
|
|
|
|
$item['author'] = html_entity_decode($author, ENT_QUOTES);
|
|
|
|
$item['timestamp'] = html_entity_decode($timestamp, ENT_QUOTES);
|
2017-02-08 12:21:59 +01:00
|
|
|
|
2018-10-18 21:10:02 +02:00
|
|
|
//if($item['timestamp'] != 0)
|
2018-10-16 18:34:39 +02:00
|
|
|
array_push($this->items, $item);
|
2017-02-08 12:21:59 +01:00
|
|
|
}
|
2018-10-18 21:10:02 +02:00
|
|
|
|
2017-02-08 12:21:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Builds the HTML from the encoded JS that Facebook provides.
|
2017-02-11 16:16:56 +01:00
|
|
|
private function buildContent($pageContent){
|
2018-07-10 03:29:47 +02:00
|
|
|
// The html ends with:
|
|
|
|
// /div>","replaceifexists
|
|
|
|
$regex = '/\\"html\\":(\".+\/div>"),"replace/';
|
2017-02-08 12:21:59 +01:00
|
|
|
preg_match($regex, $pageContent, $result);
|
2018-10-18 18:07:07 +02:00
|
|
|
|
2018-10-18 21:10:02 +02:00
|
|
|
$htmlContent = html_entity_decode(json_decode($result[1]), ENT_QUOTES, 'UTF-8');
|
|
|
|
|
|
|
|
return str_get_html($htmlContent);
|
2017-02-08 12:21:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
//Builds the cookie from the page, as Facebook sometimes refuses to give
|
|
|
|
//the page if no cookie is provided.
|
|
|
|
private function getCookies($pageURL){
|
2017-02-08 12:21:59 +01:00
|
|
|
|
|
|
|
$ctx = stream_context_create(array(
|
|
|
|
'http' => array(
|
2018-06-29 23:55:33 +02:00
|
|
|
'user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0',
|
2017-02-08 12:21:59 +01:00
|
|
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
|
|
|
|
)
|
|
|
|
)
|
2017-02-11 16:16:56 +01:00
|
|
|
);
|
2017-02-08 12:21:59 +01:00
|
|
|
$a = file_get_contents($pageURL, 0, $ctx);
|
|
|
|
|
|
|
|
//First request to get the cookie
|
2018-06-29 23:55:33 +02:00
|
|
|
$cookies = '';
|
2017-07-29 19:28:00 +02:00
|
|
|
foreach($http_response_header as $hdr) {
|
2018-06-29 23:55:33 +02:00
|
|
|
if(strpos($hdr, 'Set-Cookie') !== false) {
|
|
|
|
$cLine = explode(':', $hdr)[1];
|
|
|
|
$cLine = explode(';', $cLine)[0];
|
|
|
|
$cookies .= ';' . $cLine;
|
2017-02-08 12:21:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return substr($cookies, 1);
|
|
|
|
}
|
|
|
|
|
2018-10-18 18:07:07 +02:00
|
|
|
//Get the page ID and username from the Facebook page.
|
|
|
|
private function getPageInfos($page, $cookies){
|
2017-02-08 12:21:59 +01:00
|
|
|
|
|
|
|
$context = stream_context_create(array(
|
|
|
|
'http' => array(
|
2018-06-29 23:55:33 +02:00
|
|
|
'user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0',
|
2017-02-11 16:16:56 +01:00
|
|
|
'header' => 'Cookie: ' . $cookies
|
2017-02-08 12:21:59 +01:00
|
|
|
)
|
|
|
|
)
|
2017-02-11 16:16:56 +01:00
|
|
|
);
|
2017-02-08 12:21:59 +01:00
|
|
|
|
|
|
|
$pageContent = file_get_contents($page, 0, $context);
|
|
|
|
|
2018-06-29 23:55:33 +02:00
|
|
|
if(strpos($pageContent, 'signup-button') != false) {
|
2017-06-15 12:51:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-18 18:07:07 +02:00
|
|
|
//Get the username
|
|
|
|
$usernameRegex = '/data-nt=\"FB:TEXT4\">(.*?)<\/div>/m';
|
|
|
|
preg_match($usernameRegex, $pageContent, $usernameMatches);
|
|
|
|
if(count($usernameMatches) > 0) {
|
2018-10-18 18:44:11 +02:00
|
|
|
$username = strip_tags($usernameMatches[1]);
|
2018-10-18 18:07:07 +02:00
|
|
|
} else {
|
|
|
|
$username = $this->getInput('u');
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:21:59 +01:00
|
|
|
//Get the page ID if we don't have a captcha
|
2018-06-29 23:55:33 +02:00
|
|
|
$regex = '/page_id=([0-9]*)&/';
|
2017-02-08 12:21:59 +01:00
|
|
|
preg_match($regex, $pageContent, $matches);
|
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(count($matches) > 0) {
|
2018-10-18 18:07:07 +02:00
|
|
|
return array('userId' => $matches[1], 'username' => $username);
|
2017-02-08 12:21:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Get the page ID if we do have a captcha
|
2018-06-29 23:55:33 +02:00
|
|
|
$regex = '/"pageID":"([0-9]*)"/';
|
2017-02-08 12:21:59 +01:00
|
|
|
preg_match($regex, $pageContent, $matches);
|
|
|
|
|
2018-10-18 18:07:07 +02:00
|
|
|
return array('userId' => $matches[1], 'username' => $username);
|
2017-02-08 12:21:59 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
public function getName(){
|
|
|
|
return (isset($this->name) ? $this->name . ' - ' : '') . 'Facebook Bridge';
|
2017-02-08 12:21:59 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
public function getURI(){
|
2017-02-08 12:21:59 +01:00
|
|
|
return 'http://facebook.com';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|