VkBridge.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. class VkBridge extends BridgeAbstract
  3. {
  4. const MAINTAINER = 'ahiles3005';
  5. const NAME = 'VK.com';
  6. const URI = 'https://vk.com/';
  7. const CACHE_TIMEOUT = 300; // 5min
  8. const DESCRIPTION = 'Working with open pages';
  9. const PARAMETERS = array(
  10. array(
  11. 'u' => array(
  12. 'name' => 'Group or user name',
  13. 'required' => true
  14. )
  15. )
  16. );
  17. protected $pageName;
  18. public function getURI()
  19. {
  20. if (!is_null($this->getInput('u'))) {
  21. return static::URI . urlencode($this->getInput('u'));
  22. }
  23. return parent::getURI();
  24. }
  25. public function getName()
  26. {
  27. if ($this->pageName) {
  28. return $this->pageName;
  29. }
  30. return parent::getName();
  31. }
  32. public function collectData()
  33. {
  34. $text_html = $this->getContents()
  35. or returnServerError('No results for group or user name "' . $this->getInput('u') . '".');
  36. $text_html = iconv('windows-1251', 'utf-8', $text_html);
  37. // makes album link generating work correctly
  38. $text_html = str_replace('"class="page_album_link">', '" class="page_album_link">', $text_html);
  39. $html = str_get_html($text_html);
  40. $pageName = $html->find('.page_name', 0);
  41. if (is_object($pageName)) {
  42. $pageName = $pageName->plaintext;
  43. $this->pageName = htmlspecialchars_decode($pageName);
  44. }
  45. $pinned_post_item = null;
  46. $last_post_id = 0;
  47. foreach ($html->find('.post') as $post) {
  48. $is_pinned_post = false;
  49. if (strpos($post->getAttribute('class'), 'post_fixed') !== false) {
  50. $is_pinned_post = true;
  51. }
  52. if (is_object($post->find('a.wall_post_more', 0))) {
  53. //delete link "show full" in content
  54. $post->find('a.wall_post_more', 0)->outertext = '';
  55. }
  56. $content_suffix = '';
  57. // looking for external links
  58. $external_link_selectors = array(
  59. 'a.page_media_link_title',
  60. 'div.page_media_link_title > a',
  61. 'div.media_desc > a.lnk',
  62. );
  63. foreach($external_link_selectors as $sel) {
  64. if (is_object($post->find($sel, 0))) {
  65. $a = $post->find($sel, 0);
  66. $innertext = $a->innertext;
  67. $parsed_url = parse_url($a->getAttribute('href'));
  68. if (strpos($parsed_url['path'], '/away.php') !== 0) continue;
  69. parse_str($parsed_url['query'], $parsed_query);
  70. $content_suffix .= "<br>External link: <a href='" . $parsed_query['to'] . "'>$innertext</a>";
  71. }
  72. }
  73. // remove external link from content
  74. $external_link_selectors_to_remove = array(
  75. 'div.page_media_thumbed_link',
  76. 'div.page_media_link_desc_wrap',
  77. 'div.media_desc > a.lnk',
  78. );
  79. foreach($external_link_selectors_to_remove as $sel) {
  80. if (is_object($post->find($sel, 0))) {
  81. $post->find($sel, 0)->outertext = '';
  82. }
  83. }
  84. // looking for article
  85. $article = $post->find('a.article_snippet', 0);
  86. if (is_object($article)) {
  87. if (strpos($article->getAttribute('class'), 'article_snippet_mini') !== false) {
  88. $article_title_selector = 'div.article_snippet_mini_title';
  89. $article_author_selector = 'div.article_snippet_mini_info > .mem_link,
  90. div.article_snippet_mini_info > .group_link';
  91. $article_thumb_selector = 'div.article_snippet_mini_thumb';
  92. } else {
  93. $article_title_selector = 'div.article_snippet__title';
  94. $article_author_selector = 'div.article_snippet__author';
  95. $article_thumb_selector = 'div.article_snippet__image';
  96. }
  97. $article_title = $article->find($article_title_selector, 0)->innertext;
  98. $article_author = $article->find($article_author_selector, 0)->innertext;
  99. $article_link = self::URI . ltrim($article->getAttribute('href'), '/');
  100. $article_img_element_style = $article->find($article_thumb_selector, 0)->getAttribute('style');
  101. preg_match('/background-image: url\((.*)\)/', $article_img_element_style, $matches);
  102. if (count($matches) > 0) {
  103. $content_suffix .= "<br><img src='" . $matches[1] . "'>";
  104. }
  105. $content_suffix .= "<br>Article: <a href='$article_link'>$article_title ($article_author)</a>";
  106. $article->outertext = '';
  107. }
  108. // get video on post
  109. $video = $post->find('div.post_video_desc', 0);
  110. if (is_object($video)) {
  111. $video_title = $video->find('div.post_video_title', 0)->plaintext;
  112. $video_link = self::URI . ltrim( $video->find('a.lnk', 0)->getAttribute('href'), '/' );
  113. $content_suffix .= "<br>Video: <a href='$video_link'>$video_title</a>";
  114. $video->outertext = '';
  115. }
  116. // get all other videos
  117. foreach($post->find('a.page_post_thumb_video') as $a) {
  118. $video_title = $a->getAttribute('aria-label');
  119. $temp = explode(' ', $video_title, 2);
  120. if (count($temp) > 1) $video_title = $temp[1];
  121. $video_link = self::URI . ltrim( $a->getAttribute('href'), '/' );
  122. $content_suffix .= "<br>Video: <a href='$video_link'>$video_title</a>";
  123. $a->outertext = '';
  124. }
  125. // get all photos
  126. foreach($post->find('div.wall_text > a.page_post_thumb_wrap') as $a) {
  127. $result = $this->getPhoto($a);
  128. if ($result == null) continue;
  129. $a->outertext = '';
  130. $content_suffix .= "<br>$result";
  131. }
  132. // get albums
  133. foreach($post->find('.page_album_wrap') as $el) {
  134. $a = $el->find('.page_album_link', 0);
  135. $album_title = $a->find('.page_album_title_text', 0)->getAttribute('title');
  136. $album_link = self::URI . ltrim($a->getAttribute('href'), '/');
  137. $el->outertext = '';
  138. $content_suffix .= "<br>Album: <a href='$album_link'>$album_title</a>";
  139. }
  140. // get photo documents
  141. foreach($post->find('a.page_doc_photo_href') as $a) {
  142. $doc_link = self::URI . ltrim($a->getAttribute('href'), '/');
  143. $doc_gif_label_element = $a->find('.page_gif_label', 0);
  144. $doc_title_element = $a->find('.doc_label', 0);
  145. if (is_object($doc_gif_label_element)) {
  146. $gif_preview_img = backgroundToImg($a->find('.page_doc_photo', 0));
  147. $content_suffix .= "<br>Gif: <a href='$doc_link'>$gif_preview_img</a>";
  148. } else if (is_object($doc_title_element)) {
  149. $doc_title = $doc_title_element->innertext;
  150. $content_suffix .= "<br>Doc: <a href='$doc_link'>$doc_title</a>";
  151. } else {
  152. continue;
  153. }
  154. $a->outertext = '';
  155. }
  156. // get other documents
  157. foreach($post->find('div.page_doc_row') as $div) {
  158. $doc_title_element = $div->find('a.page_doc_title', 0);
  159. if (is_object($doc_title_element)) {
  160. $doc_title = $doc_title_element->innertext;
  161. $doc_link = self::URI . ltrim($doc_title_element->getAttribute('href'), '/');
  162. $content_suffix .= "<br>Doc: <a href='$doc_link'>$doc_title</a>";
  163. } else {
  164. continue;
  165. }
  166. $div->outertext = '';
  167. }
  168. // get polls
  169. foreach($post->find('div.page_media_poll_wrap') as $div) {
  170. $poll_title = $div->find('.page_media_poll_title', 0)->innertext;
  171. $content_suffix .= "<br>Poll: $poll_title";
  172. foreach($div->find('div.page_poll_text') as $poll_stat_title) {
  173. $content_suffix .= '<br>- ' . $poll_stat_title->innertext;
  174. }
  175. $div->outertext = '';
  176. }
  177. // get sign
  178. $post_author = $pageName;
  179. foreach($post->find('a.wall_signed_by') as $a) {
  180. $post_author = $a->innertext;
  181. $a->outertext = '';
  182. }
  183. if (is_object($post->find('div.copy_quote', 0))) {
  184. $copy_quote = $post->find('div.copy_quote', 0);
  185. if ($copy_post_header = $copy_quote->find('div.copy_post_header', 0)) {
  186. $copy_post_header->outertext = '';
  187. }
  188. $copy_quote_content = $copy_quote->innertext;
  189. $copy_quote->outertext = "<br>Reposted: <br>$copy_quote_content";
  190. }
  191. $item = array();
  192. $item['content'] = strip_tags(backgroundToImg($post->find('div.wall_text', 0)->innertext), '<br><img>');
  193. $item['content'] .= $content_suffix;
  194. // get post link
  195. $post_link = $post->find('a.post_link', 0)->getAttribute('href');
  196. preg_match('/wall-?\d+_(\d+)/', $post_link, $preg_match_result);
  197. $item['post_id'] = intval($preg_match_result[1]);
  198. if (substr(self::URI, -1) == '/') {
  199. $post_link = self::URI . ltrim($post_link, '/');
  200. } else {
  201. $post_link = self::URI . $post_link;
  202. }
  203. $item['uri'] = $post_link;
  204. $item['timestamp'] = $this->getTime($post);
  205. $item['title'] = $this->getTitle($item['content']);
  206. $item['author'] = $post_author;
  207. if ($is_pinned_post) {
  208. // do not append it now
  209. $pinned_post_item = $item;
  210. } else {
  211. $last_post_id = $item['post_id'];
  212. $this->items[] = $item;
  213. }
  214. }
  215. if (is_null($pinned_post_item)) {
  216. return;
  217. } else if (count($this->items) == 0) {
  218. $this->items[] = $pinned_post_item;
  219. } else if ($last_post_id < $pinned_post_item['post_id']) {
  220. $this->items[] = $pinned_post_item;
  221. usort($this->items, function ($item1, $item2) {
  222. return $item2['post_id'] - $item1['post_id'];
  223. });
  224. }
  225. }
  226. private function getPhoto($a) {
  227. $onclick = $a->getAttribute('onclick');
  228. preg_match('/return showPhoto\(.+?({.*})/', $onclick, $preg_match_result);
  229. if (count($preg_match_result) == 0) return;
  230. $arg = htmlspecialchars_decode( str_replace('queue:1', '"queue":1', $preg_match_result[1]) );
  231. $data = json_decode($arg, true);
  232. if ($data == null) return;
  233. $thumb = $data['temp']['base'] . $data['temp']['x_'][0] . '.jpg';
  234. $original = '';
  235. foreach(array('y_', 'z_', 'w_') as $key) {
  236. if (!isset($data['temp'][$key])) continue;
  237. if (!isset($data['temp'][$key][0])) continue;
  238. if (substr($data['temp'][$key][0], 0, 4) == 'http') {
  239. $base = '';
  240. } else {
  241. $base = $data['temp']['base'];
  242. }
  243. $original = $base . $data['temp'][$key][0] . '.jpg';
  244. }
  245. if ($original) {
  246. return "<a href='$original'><img src='$thumb'></a>";
  247. } else {
  248. return "<img src='$thumb'>";
  249. }
  250. }
  251. private function getTitle($content)
  252. {
  253. preg_match('/^["\w\ \p{Cyrillic}\(\)\?#«»-]+/mu', htmlspecialchars_decode($content), $result);
  254. if (count($result) == 0) return 'untitled';
  255. return $result[0];
  256. }
  257. private function getTime($post)
  258. {
  259. if ($time = $post->find('span.rel_date', 0)->getAttribute('time')) {
  260. return $time;
  261. } else {
  262. $strdate = $post->find('span.rel_date', 0)->plaintext;
  263. $date = date_parse($strdate);
  264. if (!$date['year']) {
  265. if (strstr($strdate, 'today') !== false) {
  266. $strdate = date('d-m-Y') . ' ' . $strdate;
  267. } elseif (strstr($strdate, 'yesterday ') !== false) {
  268. $time = time() - 60 * 60 * 24;
  269. $strdate = date('d-m-Y', $time) . ' ' . $strdate;
  270. } else {
  271. $strdate = $strdate . ' ' . date('Y');
  272. }
  273. $date = date_parse($strdate);
  274. }
  275. return strtotime($date['day'] . '-' . $date['month'] . '-' . $date['year'] . ' ' .
  276. $date['hour'] . ':' . $date['minute']);
  277. }
  278. }
  279. public function getContents()
  280. {
  281. ini_set('user-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0');
  282. $header = array('Accept-language: en', 'Cookie: remixlang=3');
  283. return getContents($this->getURI(), $header);
  284. }
  285. }