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

Last change on this file since 17488 was 17488, checked in by mistic100, 12 years ago

compatibility with Lightbox

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