OSDN Git Service

https://github.com/DavidCarrington/dabr
[embrj/master.git] / common / oembed.php
1 <?php
2
3 function url_fetch($url) 
4 {
5         global $services_time;
6         $ch = curl_init();
7         curl_setopt($ch, CURLOPT_URL, $url);
8         curl_setopt($ch, CURLOPT_TIMEOUT, 10);
9         $user_agent = "Mozilla/5.0 (compatible; dabr; " . BASE_URL . ")";
10         curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
11         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
12         $fetch_start = microtime(1);
13         $response = curl_exec($ch);
14         curl_close($ch);
15         
16         $services_time += microtime(1) - $fetch_start;
17         return $response;
18 }
19
20 function oembed_embed_thumbnails(&$feed) 
21 {
22         foreach($feed as &$status) 
23         { // Loop through the feed
24                 if(stripos($status->text, 'NSFW') === FALSE) 
25                 { // Ignore image fetching for tweets containing NSFW
26                         if ($status->entities) 
27                         { // If there are entities
28                                 $entities = $status->entities;
29                                 if($entities->urls)     
30                                 {
31                                         foreach($entities->urls as $urls) 
32                                         {       // Loop through the URL entities
33                                                 if($urls->expanded_url != "") { // Use the expanded URL, if it exists, to pass to Oembed provider
34                                                         $url = $urls->expanded_url;
35                                                 }
36                                                 else {
37                                                         $url = $urls->url;
38                                                 }
39                                                 $matched_urls[urlencode($url)][] = $status->id;         
40                                         }
41                                 } else {
42                                         //      No URLs, do nothing
43                                         $matched_urls[] = null;
44                                 }
45                         }
46                 }
47         }
48
49         //      Reduce the array of empty items
50         foreach ($matched_urls as $key => $value) 
51         {
52                 if (null == $value)
53                 {
54                         unset($matched_urls[$key]);
55                 }
56         }
57
58         // Make a single API call to Embedkit.
59         if(defined('EMBEDKIT_KEY') && EMBEDKIT_KEY != "") 
60         {
61                 //      Only the URLs which we're going to pass to Embedkit
62                 $justUrls = array_keys($matched_urls);
63         
64                 $count = count($justUrls);
65                 if ($count == 0) return;
66                 // if ($count > 20) {
67                 //      // Things can slow down with lots of links.
68                 //      $justUrls = array_chunk ($justUrls, 10);
69                 //      $justUrls = $justUrls[0];
70                 // }
71                 $url = 'https://embedkit.com/api/v1/embed?key='.EMBEDKIT_KEY.'&urls=' . implode(',', $justUrls) . '&format=json';
72
73                 $embedly_json = url_fetch($url);
74                 $oembeds = json_decode($embedly_json);
75
76                 if($oembeds->type != 'error') 
77                 {
78                         //      Single statuses don't come back in an array
79                         if (!is_array($oembeds))
80                         {
81                                 $temp = array(0 => $oembeds);
82                                 $oembeds = $temp;
83                         }
84                         
85                         foreach ($justUrls as $index => $url)
86                         {
87                                 $thumb = "";
88                                 $title = "";
89                                 
90                                 if ($oembeds[$index]->thumbnail_url) {
91                                         //      Direct links to files
92                                         $thumb = $oembeds[$index]->thumbnail_url;
93                                 } 
94
95                                 if ($oembeds[$index]->title) {
96                                         //      Direct links to files
97                                         $title = $oembeds[$index]->title;
98                                 }
99
100                                 if ($thumb) 
101                                 {       //      Embed the thumbnail
102                                         $html = theme('external_link', urldecode($url), "<img src=\"" . image_proxy($thumb, "") . "\"" .
103                                                                                         " title=\"{$title}\" alt=\"{$title}\" class=\"embedded\" />");
104                                         foreach ($matched_urls[$url] as $statusId) 
105                                         {
106                                                 $feed[$statusId]->text = $feed[$statusId]->text . '<br />' . '<span class="embed">' . $html . '</span>';
107                                         }
108                                 } elseif  ($title)      {       //      Embed a link
109                                         $html = theme('external_link', urldecode($url), $title);
110                                         foreach ($matched_urls[$url] as $statusId) 
111                                         {
112                                                 $feed[$statusId]->text = $feed[$statusId]->text . '<br />' . '<span class="embed">' . $html . '</span>';
113                                         }
114                                 }
115
116                         }
117                 }
118         }
119 }