FB2Bridge.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. class FB2Bridge extends BridgeAbstract {
  3. const MAINTAINER = 'teromene';
  4. const NAME = 'Facebook Alternate';
  5. const URI = 'https://www.facebook.com/';
  6. const CACHE_TIMEOUT = 1000;
  7. const DESCRIPTION = 'Input a page title or a profile log. For a profile log,
  8. please insert the parameter as follow : myExamplePage/132621766841117';
  9. const PARAMETERS = array( array(
  10. 'u' => array(
  11. 'name' => 'Username',
  12. 'required' => true
  13. )
  14. ));
  15. public function collectData(){
  16. function extractFromDelimiters($string, $start, $end){
  17. if(strpos($string, $start) !== false){
  18. $section_retrieved = substr($string, strpos($string, $start) + strlen($start));
  19. $section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
  20. return $section_retrieved;
  21. }
  22. return false;
  23. }
  24. //Utility function for cleaning a Facebook link
  25. $unescape_fb_link = function($matches){
  26. if(is_array($matches) && count($matches) > 1){
  27. $link = $matches[1];
  28. if(strpos($link, '/') === 0)
  29. $link = self::URI . $link . '"';
  30. if(strpos($link, 'facebook.com/l.php?u=') !== false)
  31. $link = urldecode(extractFromDelimiters($link, 'facebook.com/l.php?u=', '&'));
  32. return ' href="' . $link . '"';
  33. }
  34. };
  35. //Utility function for converting facebook emoticons
  36. $unescape_fb_emote = function($matches){
  37. static $facebook_emoticons = array(
  38. 'smile' => ':)',
  39. 'frown' => ':(',
  40. 'tongue' => ':P',
  41. 'grin' => ':D',
  42. 'gasp' => ':O',
  43. 'wink' => ';)',
  44. 'pacman' => ':<',
  45. 'grumpy' => '>_<',
  46. 'unsure' => ':/',
  47. 'cry' => ':\'(',
  48. 'kiki' => '^_^',
  49. 'glasses' => '8-)',
  50. 'sunglasses' => 'B-)',
  51. 'heart' => '<3',
  52. 'devil' => ']:D',
  53. 'angel' => '0:)',
  54. 'squint' => '-_-',
  55. 'confused' => 'o_O',
  56. 'upset' => 'xD',
  57. 'colonthree' => ':3',
  58. 'like' => '&#x1F44D;');
  59. $len = count($matches);
  60. if ($len > 1)
  61. for ($i = 1; $i < $len; $i++)
  62. foreach ($facebook_emoticons as $name => $emote)
  63. if ($matches[$i] === $name)
  64. return $emote;
  65. return $matches[0];
  66. };
  67. if($this->getInput('u') !== null){
  68. $page = 'https://touch.facebook.com/' . $this->getInput('u');
  69. $cookies = $this->getCookies($page);
  70. $pageID = $this->getPageID($page, $cookies);
  71. if($pageID === null){
  72. echo <<<EOD
  73. Unable to get the page id. You should consider getting the ID by hand, then importing it into FB2Bridge
  74. EOD;
  75. die();
  76. }
  77. }
  78. //Build the string for the first request
  79. $requestString = 'https://touch.facebook.com/pages_reaction_units/more/?page_id='
  80. . $pageID
  81. . '&cursor={"card_id"%3A"videos"%2C"has_next_page"%3Atrue}&surface=mobile_page_home&unit_count=8';
  82. $fileContent = file_get_contents($requestString);
  83. $articleIndex = 0;
  84. $maxArticle = 3;
  85. $html = $this->buildContent($fileContent);
  86. $author = $this->getInput('u');
  87. foreach($html->find("article") as $content){
  88. $item = array();
  89. $item['uri'] = "http://touch.facebook.com"
  90. . $content->find("div._52jc", 0)->find("a", 0)->getAttribute("href");
  91. $content->find("header", 0)->innertext = "";
  92. $content->find("footer", 0)->innertext = "";
  93. //Remove html nodes, keep only img, links, basic formatting
  94. $content = strip_tags($content, '<a><img><i><u><br><p>');
  95. //Adapt link hrefs: convert relative links into absolute links and bypass external link redirection
  96. $content = preg_replace_callback('/ href=\"([^"]+)\"/i', $unescape_fb_link, $content);
  97. //Clean useless html tag properties and fix link closing tags
  98. foreach (array(
  99. 'onmouseover',
  100. 'onclick',
  101. 'target',
  102. 'ajaxify',
  103. 'tabindex',
  104. 'class',
  105. 'style',
  106. 'data-[^=]*',
  107. 'aria-[^=]*',
  108. 'role',
  109. 'rel',
  110. 'id') as $property_name)
  111. $content = preg_replace('/ ' . $property_name . '=\"[^"]*\"/i', '', $content);
  112. $content = preg_replace('/<\/a [^>]+>/i', '</a>', $content);
  113. //Convert textual representation of emoticons eg
  114. // "<i><u>smile emoticon</u></i>" back to ASCII emoticons eg ":)"
  115. $content = preg_replace_callback('/<i><u>([^ <>]+) ([^<>]+)<\/u><\/i>/i', $unescape_fb_emote, $content);
  116. $item['content'] = $content;
  117. $title = $author;
  118. if (strlen($title) > 24)
  119. $title = substr($title, 0, strpos(wordwrap($title, 24), "\n")) . '...';
  120. $title = $title . ' | ' . strip_tags($content);
  121. if (strlen($title) > 64)
  122. $title = substr($title, 0, strpos(wordwrap($title, 64), "\n")) . '...';
  123. $item['title'] = $title;
  124. $item['author'] = $author;
  125. array_push($this->items, $item);
  126. }
  127. }
  128. // Currently not used. Is used to get more than only 3 elements, as they appear on another page.
  129. private function computeNextLink($string, $pageID){
  130. $regex = "/timeline_unit\\\\\\\\u00253A1\\\\\\\\u00253A([0-9]*)\\\\\\\\u00253A([0-9]*)\\\\\\\\u00253A([0-9]*)\\\\\\\\u00253A([0-9]*)/";
  131. preg_match($regex, $string, $result);
  132. return "https://touch.facebook.com/pages_reaction_units/more/?page_id="
  133. . $pageID
  134. . "&cursor=%7B%22timeline_cursor%22%3A%22timeline_unit%3A1%3A"
  135. . $result[1]
  136. . "%3A"
  137. . $result[2]
  138. . "%3A"
  139. . $result[3]
  140. . "%3A"
  141. . $result[4]
  142. . "%22%2C%22timeline_section_cursor%22%3A%7B%7D%2C%22has_next_page%22%3Atrue%7D&surface=mobile_page_home&unit_count=3";
  143. }
  144. //Builds the HTML from the encoded JS that Facebook provides.
  145. private function buildContent($pageContent){
  146. $regex = "/\\\"html\\\":\\\"(.*?)\\\",\\\"replace/";
  147. preg_match($regex, $pageContent, $result);
  148. return str_get_html(html_entity_decode(json_decode('"' . $result[1] . '"')));
  149. }
  150. //Builds the cookie from the page, as Facebook sometimes refuses to give
  151. //the page if no cookie is provided.
  152. private function getCookies($pageURL){
  153. $ctx = stream_context_create(array(
  154. 'http' => array(
  155. 'user_agent' => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0",
  156. 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  157. )
  158. )
  159. );
  160. $a = file_get_contents($pageURL, 0, $ctx);
  161. //First request to get the cookie
  162. $cookies = "";
  163. foreach($http_response_header as $hdr){
  164. if(strpos($hdr, "Set-Cookie") !== false){
  165. $cLine = explode(":", $hdr)[1];
  166. $cLine = explode(";", $cLine)[0];
  167. $cookies .= ";" . $cLine;
  168. }
  169. }
  170. return substr($cookies, 1);
  171. }
  172. //Get the page ID from the Facebook page.
  173. private function getPageID($page, $cookies){
  174. $context = stream_context_create(array(
  175. 'http' => array(
  176. 'user_agent' => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0",
  177. 'header' => 'Cookie: ' . $cookies
  178. )
  179. )
  180. );
  181. $pageContent = file_get_contents($page, 0, $context);
  182. //Get the page ID if we don't have a captcha
  183. $regex = "/page_id=([0-9]*)&/";
  184. preg_match($regex, $pageContent, $matches);
  185. if(count($matches) > 0){
  186. return $matches[1];
  187. }
  188. //Get the page ID if we do have a captcha
  189. $regex = "/\"pageID\":\"([0-9]*)\"/";
  190. preg_match($regex, $pageContent, $matches);
  191. return $matches[1];
  192. }
  193. public function getName(){
  194. return (isset($this->name) ? $this->name . ' - ' : '') . 'Facebook Bridge';
  195. }
  196. public function getURI(){
  197. return 'http://facebook.com';
  198. }
  199. public function getCacheDuration(){
  200. return 60 * 60 * 3; // 5 minutes
  201. }
  202. }