Guest User

Untitled

a guest
Oct 6th, 2014
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.84 KB | None | 0 0
  1. <?php
  2. class td_social_api {
  3. //the name of the field in the table that holds the cache array
  4. private $cache_var_name_last_val = 'td_social_api_v3_last_val';
  5.  
  6. private $do_transient_save = false;
  7.  
  8. //the cache holding the last value > 0
  9. public $td_cache_last_val = array();
  10.  
  11.  
  12. function __construct() {
  13. $this->td_cache_last_val = get_option($this->cache_var_name_last_val, '');
  14. }
  15.  
  16.  
  17. //check to see if we have the cache
  18. private function in_cache($service_id, $user_id) {
  19. //se poate sa dea worning aici cind nu exista array-ul cache in tabela
  20. //echo '<br>'.$service_id. ": ". (time() - $this->td_cache_last_val[$service_id]['timestamp']) .' > ' . $this->td_cache_last_val[$service_id]['expires'].'<br>';
  21. //e pus asa ( < )ca sa dea false la verificare
  22. if (is_array($this->td_cache_last_val) and isset($this->td_cache_last_val[$service_id]['uid']) and $this->td_cache_last_val[$service_id]['uid'] == $user_id and (time() - $this->td_cache_last_val[$service_id]['timestamp'] < $this->td_cache_last_val[$service_id]['expires']) ) {
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. }
  28.  
  29. //extract the value for a service_id, and return it to the service_id function
  30. private function get_cache($service_id, $user_id) {
  31. if (is_array($this->td_cache_last_val) and isset($this->td_cache_last_val[$service_id]['uid']) and $this->td_cache_last_val[$service_id]['uid'] == $user_id) {
  32. return $this->td_cache_last_val[$service_id];
  33. } else {
  34. return false;
  35. }
  36. }
  37.  
  38. //save the cache array to the database
  39. private function save_cache($service_id, $data) {
  40. if($this->do_transient_save == true) {
  41. $this->td_cache_last_val[$service_id] = $data;
  42.  
  43. //update the database
  44. update_option($this->cache_var_name_last_val, $this->td_cache_last_val);
  45. }
  46.  
  47. $this->do_transient_save = false;
  48. }
  49.  
  50.  
  51. //url fatching
  52. private function get_url($url) {
  53. $td_request_result = @wp_remote_retrieve_body(@wp_remote_get($url, array('timeout' => 5 , 'sslverify' => false)));
  54. return $td_request_result;
  55. }
  56.  
  57. private function get_json($url) {
  58. $td_json = @json_decode($this->get_url($url), true);
  59. return $td_json;
  60. }
  61.  
  62.  
  63.  
  64. public function get_social_counter($service_id, $user_id) {
  65. $buffy_array = 0;
  66.  
  67. //use in g+
  68. $expl_maches = '';
  69.  
  70. if ($this->in_cache($service_id, $user_id) === false) { //disable cache here
  71. try {
  72. switch ($service_id) {
  73. case 'facebook':
  74. $td_data = @$this->get_url("http://facebook.com/$user_id");
  75. $pattern = "/facebook.com\/" . $user_id . "(.*?)likes\"><span(.*?)><span(.*?)>(.*?)<span/i";
  76. preg_match_all($pattern, $td_data, $matches);
  77. if (!empty($matches[4][0])) {
  78. $td_buffer_counter_fix = str_replace(array('.', ',', ' '), array(''), $matches[4][0]);
  79. $buffy_array = (int) $td_buffer_counter_fix;
  80. }
  81. break;
  82.  
  83. case 'twitter':
  84.  
  85. $twitter_worked = false;
  86.  
  87. //check 1 via https
  88. $td_data = @$this->get_url("https://twitter.com/$user_id");
  89. if (!empty($td_data)) {
  90. //$pattern = "/followers.*title=\"([0123456789,]*)\"/i";
  91. $pattern = "/title=\"(.*)\"(.*)data-nav=\"followers\"/i";
  92. preg_match_all($pattern, $td_data, $matches);
  93. if (!empty($matches[1][0])) {
  94. $td_buffer_counter_fix = str_replace(',','', $matches[1][0]);
  95. $buffy_array = (int) $td_buffer_counter_fix;
  96.  
  97. if (!empty($buffy_array) and is_numeric($buffy_array)) {
  98. $twitter_worked = true; //skip twitter second check it worked!
  99. }
  100. }
  101. }
  102.  
  103.  
  104. //check 2 via twitter api client - we only get here if the first check did not returned anything
  105. if ($twitter_worked === false) {
  106. if (!class_exists('TwitterApiClient')) {
  107. require_once 'twitter-client.php';
  108. $Client = new TwitterApiClient;
  109. $Client->set_oauth (YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, SOME_ACCESS_KEY, SOME_ACCESS_SECRET);
  110. try {
  111. $path = 'users/show';
  112. $args = array ('screen_name' => $user_id);
  113. $data = @$Client->call( $path, $args, 'GET' );
  114. if (!empty($data['followers_count'])) {
  115. $buffy_array = (int) $data['followers_count']; //set the buffer
  116. }
  117. }
  118. catch( TwitterApiException $Ex ){
  119. //twitter rate limit will show here
  120. //print_r($Ex);
  121. }
  122. }
  123. }
  124.  
  125.  
  126. break;
  127.  
  128. case 'vimeo':
  129. //$td_data = @$this->get_json("http://vimeo.com/api/v2/channel/$user_id/info.json");
  130. //if (!empty($td_data['total_subscribers'])) {
  131. // $buffy_array = (int) $td_data['total_subscribers'];
  132. //} else {
  133. $td_data = @$this->get_url("http://vimeo.com/$user_id");
  134. $pattern = "/<b class=\"stat_list_count\">(.*?)<\/b>(\s+)<span class=\"stat_list_label\">likes<\/span>/";
  135. preg_match($pattern, $td_data, $matches);
  136. if (!empty($matches[1])) {
  137. $buffy_array = (int) $matches[1];
  138. }
  139. //}
  140. break;
  141.  
  142. case 'youtube':
  143. $td_data = @$this->get_json("http://gdata.youtube.com/feeds/api/users/$user_id?alt=json");
  144. if (!empty($td_data['entry']['yt$statistics']['subscriberCount'])) {
  145. $buffy_array = (int) $td_data['entry']['yt$statistics']['subscriberCount'];
  146. }
  147. break;
  148.  
  149. case 'googleplus':
  150. $td_data = @$this->get_json("https://www.googleapis.com/plus/v1/people/$user_id?key=AIzaSyA1hsdPPNpkS3lvjohwLNkOnhgsJ9YCZWw");
  151. if (!empty($td_data['plusOneCount'])) {
  152. $buffy_array = (int) $td_data['plusOneCount'];
  153. }else{
  154. $td_data = @$this->get_url("https://plus.google.com/$user_id/posts");
  155. $pattern = "/<span role=\"button\" class=\"d-s o5a\" tabindex=\"0\">(.*?)<\/span>/";
  156. preg_match($pattern, $td_data, $matches);
  157. if (!empty($matches[1])) {
  158. $expl_maches = explode(' ', trim($matches[1]));
  159. $buffy_array = str_replace(array('.', ','), array(''), $expl_maches[0]);
  160. }
  161. }
  162. break;
  163.  
  164. case 'instagram':
  165. $td_data = @$this->get_url("http://instagram.com/$user_id#");
  166. $pattern = "/followed_by\":(.*?),\"follows\":/";
  167. preg_match($pattern, $td_data, $matches);
  168. if (!empty($matches[1])) {
  169. $buffy_array = (int) $matches[1];
  170. }
  171. break;
  172.  
  173. case 'soundcloud':
  174. $td_data = @$this->get_json("http://api.soundcloud.com/users/$user_id.json?client_id=97220fb34ad034b5d4b59b967fd1717e");
  175. if (!empty($td_data['followers_count'])) {
  176. $buffy_array = (int) $td_data['followers_count'];
  177. }
  178. break;
  179.  
  180. case 'rss':
  181. $buffy_array = (int) $user_id;
  182. break;
  183. }
  184.  
  185.  
  186.  
  187.  
  188. //case 1; we have response from service; write the cache arrya with new values
  189. if ($buffy_array > 0) {
  190.  
  191. $local_cash['count'] = $buffy_array;
  192. if($buffy_array > 0){
  193. $local_cash['ok_count'] = $buffy_array;
  194. }
  195. $local_cash['timestamp'] = time();
  196. $local_cash['expires'] = 10800;
  197. $local_cash['uid'] = $user_id;
  198.  
  199. //set to true the flag to save
  200. $this->do_transient_save = true;
  201.  
  202. //send the service array to save the cache
  203. $this->save_cache($service_id, $local_cash);
  204.  
  205. /*
  206. * case 2; we DON'T have response from service, then get the cache array for this service,
  207. * and rewrite the array in cache, with default values
  208. */
  209. } else {
  210. $local_cash = $this->get_cache($service_id, $user_id);
  211.  
  212. if (is_array($local_cash) and isset($local_cash['ok_count']) > 0) {
  213. $buffy_array = intval($local_cash['ok_count']);
  214. }else{
  215. $buffy_array = 0;
  216. }
  217.  
  218. $local_cash['timestamp'] = time();
  219. $local_cash['count'] = 0;
  220. $local_cash['uid'] = $user_id;
  221. $local_cash['expires'] = 10800;
  222.  
  223. //set to true the flag to save
  224. $this->do_transient_save = true;
  225.  
  226. //send the service array to save the cache
  227. $this->save_cache($service_id, $local_cash);
  228. }
  229. } catch (Exception $e) {
  230. }
  231. return $buffy_array;
  232. //case 3, the cache is true; time < expire
  233. } else {
  234. $local_cash = $this->get_cache($service_id, $user_id);
  235.  
  236. if (is_array($local_cash) and array_key_exists('ok_count', $local_cash)) {
  237. return intval($local_cash['ok_count']);
  238. } else {
  239. return 0;
  240. }
  241. }
  242. }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment