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

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