Data.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © 2018 Porto. All rights reserved.
  4. */
  5. namespace Smartwave\Socialfeeds\Helper;
  6. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  7. {
  8. public function __construct(
  9. \Magento\Framework\App\Helper\Context $context
  10. ) {
  11. parent::__construct($context);
  12. }
  13. public function getConfig($config_path)
  14. {
  15. return $this->scopeConfig->getValue(
  16. $config_path,
  17. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  18. );
  19. }
  20. public function file_get_contents_curl($url, $useragent = null) {
  21. $ch = curl_init($url);
  22. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  23. if ($useragent)
  24. curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  25. curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  26. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  27. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  28. $data = curl_exec($ch);
  29. curl_close($ch);
  30. return $data;
  31. }
  32. function getAttribute($attrib, $tag){
  33. //get attribute from html tag
  34. $re = '/'.$attrib.'=["\']?([^"\' ]*)["\' ]/is';
  35. preg_match($re, $tag, $match);
  36. if($match){
  37. return urldecode($match[1]);
  38. }else {
  39. return false;
  40. }
  41. }
  42. function fetch_fb_fans($fb_id, $limit = 10){
  43. $ret = array();
  44. $matches = array();
  45. $url = 'https://www.facebook.com/plugins/likebox.php?href=https://www.facebook.com/' . $fb_id . '&connections=' . $limit;
  46. $html = '';
  47. $like_html = $this->file_get_contents_curl($url, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1');
  48. $doc = new \DOMDocument('1.0', 'utf-8');
  49. @$doc->loadHTML('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . $like_html);
  50. $peopleList = array();
  51. $i = 0;
  52. if (!$doc)
  53. return false;
  54. $result = array();
  55. $link = $doc->getElementById('u_0_4');
  56. $result['link'] = 'https://www.facebook.com/'.$fb_id;
  57. $result['like'] = '';
  58. if (isset($link)) {
  59. foreach ($link->childNodes as $child) {
  60. $result['like'] .= $link->ownerDocument->saveHTML($child);
  61. }
  62. }
  63. foreach ($doc->getElementsByTagName('ul')->item(0)->childNodes as $child) {
  64. $raw = $doc->saveXML($child);
  65. $li = preg_replace("/<li[^>]+\>/i", "", $raw);
  66. $peopleList[$i] = preg_replace("/<\/li>/i", "", $li);
  67. $i++;
  68. }
  69. $fb = $this->getConfig("sw_socialfeeds/facebook_fanbox");
  70. $persons = 0;
  71. foreach ($peopleList as $key => $code) {
  72. if($fb['showing_counts'] && $persons >= $fb['showing_counts'])
  73. continue;
  74. $name = $this->getAttribute('title', $code);
  75. $nm = substr($name, 0, 7);
  76. //print_r(strlen($nm));echo "\n";
  77. if (strlen($nm) != strlen($name)) $nm = $nm."...";
  78. $image = $this->getAttribute('src', $code);
  79. $link = $this->getAttribute('href', $code);
  80. //$data = file_get_contents($image);
  81. //$img_in_base64 = 'data:image/jpg;base64,' . base64_encode($data);
  82. $protocols = array("http:","https:");
  83. $img_in_base64 = str_replace($protocols, "", $image);
  84. $html .= '<div class="fb-person">';
  85. if ($link != "") {
  86. $html .= "<a href=\"".$link."\" title=\"".$name."\" target=\"_blank\"><img src=\"".$img_in_base64."\" alt=\"\" /></a>";
  87. } else {
  88. $html .= "<span title=\"".$name."\"><img src=\"".$img_in_base64."\" alt=\"\" /></span>";
  89. }
  90. $html .= $name.'</div>';
  91. $persons ++;
  92. }
  93. $result['fans'] = $html;
  94. return $result;
  95. }
  96. // get facebook fans
  97. public function getFBFans() {
  98. $fb = $this->getConfig("sw_socialfeeds/facebook_fanbox");
  99. if (!$fb['enable'])
  100. return false;
  101. $limit = $fb['showing_counts'];
  102. $fb_name = $fb['facebook_name'];
  103. // get page info from graph
  104. return $this->fetch_fb_fans($fb_name, $limit);
  105. }
  106. }