source: extensions/gvideo/include/functions.inc.php @ 19056

Last change on this file since 19056 was 19056, checked in by mistic100, 11 years ago

allow to add private dailymotion videos, remove videobb support, add tags support

File size: 11.2 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4function parse_video_url($source_url)
5{
6  $source_url = 'http://'.preg_replace('#^http(s?)://#', null, $source_url);
7 
8  $url = parse_url($source_url);
9  $url['host'] = str_replace('www.', null, $url['host']);
10  $url['host'] = explode('.', $url['host']);
11 
12  $video = array();
13  switch ($url['host'][0])
14  {
15    /* youtube */
16    case 'youtube':
17    {
18      $video['type'] = 'youtube';
19
20      parse_str($url['query'], $url['query']);
21      if (empty($url['query']['v'])) return false;
22     
23      $video['video_id'] = $url['query']['v'];
24    }
25   
26    case 'youtu': // youtu.be (short-url service)
27    {
28      if (empty($video))
29      {
30        $video['type'] = 'youtube';
31       
32        $url['path'] = explode('/', $url['path']);
33        $video['video_id'] = $url['path'][1];
34      }
35     
36      $api_url = 'http://gdata.youtube.com/feeds/api/videos/'.$video['video_id'].'?v=2&alt=json';
37      $json = download_remote_file($api_url, true);
38      if ($json === false or $json == 'file_error') return false;
39     
40      $json = json_decode($json, true);
41      $video = array_merge($video, array(
42        'url' => 'http://youtube.com/watch?v='.$video['video_id'],
43        'title' => $json['entry']['title']['$t'],
44        'description' => $json['entry']['media$group']['media$description']['$t'],
45        'thumbnail' => $json['entry']['media$group']['media$thumbnail'][2]['url'],
46        'author' => $json['entry']['author'][0]['name']['$t'],
47        'tags' => null,
48        ));
49      break;
50    }
51     
52    /* vimeo */
53    case 'vimeo':
54    {
55      $video['type'] = 'vimeo';
56     
57      $url['path'] = explode('/', $url['path']);
58      $video['video_id'] = $url['path'][1];
59     
60      $api_url = 'http://vimeo.com/api/v2/video/'.$video['video_id'].'.json';
61      $json = download_remote_file($api_url, true);
62      if ($json === false or $json == 'file_error') return false;
63     
64      $json = json_decode($json, true);
65      $video = array_merge($video, array(
66        'url' => 'http://vimeo.com/'.$video['video_id'],
67        'title' => $json[0]['title'],
68        'description' => $json[0]['description'],
69        'thumbnail' => $json[0]['thumbnail_large'],
70        'author' => $json[0]['user_name'],
71        'tags' => array_map('trim', explode(',', $json[0]['tags'])),
72        ));
73      break;
74    }
75     
76    /* dailymotion */
77    case 'dailymotion':
78    {
79      $video['type'] = 'dailymotion';
80     
81      $url['path'] = explode('/', $url['path']);
82      if ($url['path'][1] != 'video') return false;
83      $video['video_id'] = $url['path'][2];
84     
85      $api_url = 'https://api.dailymotion.com/video/'.$video['video_id'].'?fields=description,thumbnail_large_url,title,owner.username,tags'; // DM doesn't accept non secure connection
86      $json = download_remote_file($api_url, true);
87      if ($json === false or $json == 'file_error') return false;
88     
89      $json = json_decode($json, true);
90      $json['thumbnail_large_url'] = preg_replace('#\?([0-9]+)$#', null, $json['thumbnail_large_url']);
91     
92      $video = array_merge($video, array(
93        'url' => 'http://dailymotion.com/video/'.$video['video_id'],
94        'title' => $json['title'],
95        'description' => $json['description'],
96        'thumbnail' => $json['thumbnail_large_url'],
97        'author' => $json['owner.username'],
98        'tags' => $json['tags'],
99        ));
100      break;
101    }
102     
103    /* wat */
104    case 'wat':
105    {
106      $video['type'] = 'wat';
107     
108      $html = download_remote_file($source_url, true);
109      if ($html === false or $html == 'file_error') return false;
110     
111      preg_match('#<meta property="og:video" content="http://www.wat.tv/swf2/([^"/>]+)" />#', $html, $matches);
112      if (empty($matches[1])) return false;
113      $video['video_id'] = $matches[1];
114     
115      $video['url'] = $source_url;
116     
117      preg_match('#<meta name="name" content="([^">]*)" />#', $html, $matches);
118      $video['title'] = $matches[1];
119     
120      preg_match('#<p class="description"(?:[^>]*)>(.*?)</p>#s', $html, $matches);
121      $video['description'] = $matches[1];
122     
123      preg_match('#<meta property="og:image" content="([^">]+)" />#', $html, $matches);
124      $video['thumbnail'] = $matches[1];
125     
126      $video['author'] = null;
127     
128      preg_match_all('#<meta property="video:tag" content="([^">]+)" />#', $html, $matches);
129      $video['tags'] = $matches[1];
130      break;
131    }
132     
133    /* wideo */
134    case 'wideo':
135    {
136      $video['type'] = 'wideo';
137     
138      $url['path'] = explode('/', $url['path']);
139      $video['video_id'] = rtrim($url['path'][2], '.html');
140     
141      $html = download_remote_file($source_url, true);
142      if ($html === false or $html == 'file_error') return false;
143     
144      $video['url'] = 'http://wideo.fr/video/'.$video['video_id'].'.html';
145     
146      preg_match('#<meta property="og:title" content="([^">]*)" />#', $html, $matches);
147      $video['title'] = $matches[1];
148     
149      preg_match('#<meta property="og:description" content="([^">]*)" />#', $html, $matches);
150      $video['description'] = $matches[1];
151     
152      preg_match('#<meta property="og:image" content="([^">]+)" />#', $html, $matches);
153      $video['thumbnail'] = $matches[1];
154     
155      preg_match('#<li id="li_author">Auteur :  <a href=(?:[^>]*)><span>(.*?)</span></a>#', $html, $matches);
156      $video['author'] = $matches[1];
157     
158      preg_match('#<meta name="keywords" content="([^">]+)" />#', $html, $matches);
159      $video['tags'] = array_map('trim', explode(',', $matches[1]));
160      break;
161    }
162     
163    default:
164      return false;   
165  }
166 
167  return $video;
168}
169
170/**
171 * test if a download method is available
172 * @return: bool
173 */
174if (!function_exists('test_remote_download'))
175{
176  function test_remote_download()
177  {
178    return function_exists('curl_init') || ini_get('allow_url_fopen');
179  }
180}
181
182/**
183 * download a remote file
184 *  - needs cURL or allow_url_fopen
185 *  - take care of SSL urls
186 *
187 * @param: string source url
188 * @param: mixed destination file (if true, file content is returned)
189 */
190if (!function_exists('download_remote_file'))
191{
192  function download_remote_file($src, $dest)
193  {
194    if (empty($src))
195    {
196      return false;
197    }
198   
199    $return = ($dest === true) ? true : false;
200   
201    /* curl */
202    if (function_exists('curl_init'))
203    {
204      if (!$return)
205      {
206        $newf = fopen($dest, "wb");
207      }
208      $ch = curl_init();
209     
210      curl_setopt($ch, CURLOPT_URL, $src);
211      curl_setopt($ch, CURLOPT_HEADER, false);
212      curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-language: en"));
213      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
214      if (!ini_get('safe_mode'))
215      {
216        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
217        curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
218      }
219      if (strpos($src, 'https://') !== false)
220      {
221        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
222        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
223      }
224      if (!$return)
225      {
226        curl_setopt($ch, CURLOPT_FILE, $newf);
227      }
228      else
229      {
230        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
231      }
232     
233      $out = curl_exec($ch);
234      curl_close($ch);
235     
236      if ($out === false)
237      {
238        return 'file_error';
239      }
240      else if (!$return)
241      {
242        fclose($newf);
243        return true;
244      }
245      else
246      {
247        return $out;
248      }
249    }
250    /* file get content */
251    else if (ini_get('allow_url_fopen'))
252    {
253      if (strpos($src, 'https://') !== false and !extension_loaded('openssl'))
254      {
255        return false;
256      }
257     
258      $opts = array(
259        'http' => array(
260          'method' => "GET",
261          'user_agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
262          'header' => "Accept-language: en",
263        )
264      );
265
266      $context = stream_context_create($opts);
267     
268      if (($file = file_get_contents($src, false, $context)) === false)
269      {
270        return 'file_error';
271      }
272     
273      if (!$return)
274      {
275        file_put_contents($dest, $file);
276        return true;
277      }
278      else
279      {
280        return $file;
281      }
282    }
283   
284    return false;
285  }
286}
287
288/**
289 * and film frame to an image (need GD library)
290 * @param: string source
291 * @param: string destination (if null, the source si modified)
292 * @return: void
293 */
294function add_film_frame($src, $dest=null)
295{
296  if (empty($dest))
297  {
298    $dest = $src;
299  }
300 
301  // we need gd library
302  if (!function_exists('imagecreatetruecolor'))
303  {
304    if ($dest != $src) copy($src, $dest);
305    return;
306  }
307 
308  // open source image
309  switch (strtolower(get_extension($src)))
310  {
311    case 'jpg':
312    case 'jpeg':
313      $srcImage = imagecreatefromjpeg($src);
314      break;
315    case 'png':
316      $srcImage = imagecreatefrompng($src);
317      break;
318    case 'gif':
319      $srcImage = imagecreatefromgif($src);
320      break;
321    default:
322      if ($dest != $src) copy($src, $dest);
323      return;
324  }
325 
326  // source properties
327  $srcWidth = imagesx($srcImage);
328  $srcHeight = imagesy($srcImage);
329  $const = intval($srcWidth * 0.04);
330  $bandRadius = floor($const/8);
331
332  // band properties
333  $imgBand = imagecreatetruecolor($srcWidth + 6*$const, $srcHeight + 3*$const);
334 
335  $black = imagecolorallocate($imgBand, 0, 0, 0);
336  $white = imagecolorallocate($imgBand, 245, 245, 245);
337 
338  // and dots
339  $y_start = intval(($srcHeight + 3*$const) / 2);
340  $aug = intval($y_start / 5) + 1;
341  $i = 0;
342
343  while ($y_start + $i*$aug < $srcHeight + 3*$const)
344  {
345    imagefilledroundrectangle($imgBand, (3/4)*$const, $y_start + $i*$aug - $const/2, (9/4)*$const - 1, $y_start + $i*$aug + $const/2 - 1, $white, $bandRadius);
346    imagefilledroundrectangle($imgBand, (3/4)*$const, $y_start - $i*$aug - $const/2, (9/4)*$const - 1, $y_start - $i*$aug + $const/2 - 1, $white, $bandRadius);
347
348    imagefilledroundrectangle($imgBand, $srcWidth + (15/4)*$const, $y_start + $i*$aug - $const/2, $srcWidth + (21/4)*$const - 1, $y_start + $i*$aug + $const/2 - 1, $white, $bandRadius);
349    imagefilledroundrectangle($imgBand, $srcWidth + (15/4)*$const, $y_start - $i*$aug - $const/2, $srcWidth + (21/4)*$const - 1, $y_start - $i*$aug + $const/2 - 1, $white, $bandRadius);
350
351    ++$i;
352  }
353
354  // add source to band
355  imagecopy($imgBand, $srcImage, 3*$const, (3/2)*$const, 0, 0, $srcWidth, $srcHeight);
356 
357  // save image
358  switch (strtolower(get_extension($dest)))
359  {
360    case 'jpg':
361    case 'jpeg':
362      imagejpeg($imgBand, $dest, 85);
363      break;
364    case 'png':
365      imagepng($imgBand, $dest);
366      break;
367    case 'gif':
368      imagegif($imgBand, $dest);
369      break;
370  }
371}
372
373/**
374 * create a rectangle with round corners
375 * http://www.php.net/manual/fr/function.imagefilledrectangle.php#42815
376 */
377function imagefilledroundrectangle(&$img, $x1, $y1, $x2, $y2, $color, $radius)
378{
379  imagefilledrectangle($img, $x1+$radius, $y1, $x2-$radius, $y2, $color);
380 
381  if ($radius > 0)
382  {
383    imagefilledrectangle($img, $x1, $y1+$radius, $x2, $y2-$radius, $color);
384    imagefilledellipse($img, $x1+$radius, $y1+$radius, $radius*2, $radius*2, $color);
385    imagefilledellipse($img, $x2-$radius, $y1+$radius, $radius*2, $radius*2, $color);
386    imagefilledellipse($img, $x1+$radius, $y2-$radius, $radius*2, $radius*2, $color);
387    imagefilledellipse($img, $x2-$radius, $y2-$radius, $radius*2, $radius*2, $color);
388  }
389}
390
391?>
Note: See TracBrowser for help on using the repository browser.