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

Last change on this file since 17430 was 17383, checked in by mistic100, 12 years ago
  • simplify add page
  • simplify migration task
  • add option to add film effect on element edition page
File size: 11.5 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 * download a remote file
199 *  - needs cURL or allow_url_fopen
200 *  - take care of SSL urls
201 *
202 * @param: string source url
203 * @param: mixed destination file (if true, file content is returned)
204 */
205if (!function_exists('download_remote_file'))
206{
207  function download_remote_file($src, $dest)
208  {
209    if (empty($src))
210    {
211      return false;
212    }
213   
214    $return = ($dest === true) ? true : false;
215   
216    /* curl */
217    if (function_exists('curl_init'))
218    {
219      if (!$return)
220      {
221        $newf = fopen($dest, "wb");
222      }
223      $ch = curl_init();
224     
225      curl_setopt($ch, CURLOPT_URL, $src);
226      curl_setopt($ch, CURLOPT_HEADER, 0);
227      curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-language: en"));
228      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
229      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
230      curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
231      if (strpos($src, 'https://') !== false)
232      {
233        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
234        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
235      }
236      if (!$return)
237      {
238        curl_setopt($ch, CURLOPT_FILE, $newf);
239      }
240      else
241      {
242        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
243      }
244     
245      if (($out = curl_exec($ch)) === false)
246      {
247        return 'file_error';
248      }
249     
250      curl_close($ch);
251     
252      if (!$return)
253      {
254        fclose($newf);
255        return true;
256      }
257      else
258      {
259        return $out;
260      }
261    }
262    /* file get content */
263    else if (ini_get('allow_url_fopen'))
264    {
265      if (strpos($src, 'https://') !== false and !extension_loaded('openssl'))
266      {
267        return false;
268      }
269     
270      $opts = array(
271        'http' => array(
272          'method' => "GET",
273          'user_agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
274          'header' => "Accept-language: en",
275        )
276      );
277
278      $context = stream_context_create($opts);
279     
280      if (($file = file_get_contents($src, false, $context)) === false)
281      {
282        return 'file_error';
283      }
284     
285      if (!$return)
286      {
287        file_put_contents($dest, $file);
288        return true;
289      }
290      else
291      {
292        return $file;
293      }
294    }
295   
296    return false;
297  }
298}
299
300/**
301 * and film frame to an image (need GD library)
302 * @param: string source
303 * @param: string destination (if null, the source si modified)
304 * @return: void
305 */
306function add_film_frame($src, $dest=null)
307{
308  if (empty($dest))
309  {
310    $dest = $src;
311  }
312 
313  // we need gd library
314  if (!function_exists('imagecreatetruecolor'))
315  {
316    if ($dest != $src) copy($src, $dest);
317    return;
318  }
319 
320  // open source image
321  switch (strtolower(get_extension($src)))
322  {
323    case 'jpg':
324    case 'jpeg':
325      $srcImage = imagecreatefromjpeg($src);
326      break;
327    case 'png':
328      $srcImage = imagecreatefrompng($src);
329      break;
330    case 'gif':
331      $srcImage = imagecreatefromgif($src);
332      break;
333    default:
334      if ($dest != $src) copy($src, $dest);
335      return;
336  }
337 
338  // source properties
339  $srcWidth = imagesx($srcImage);
340  $srcHeight = imagesy($srcImage);
341  $const = intval($srcWidth * 0.04);
342  $bandRadius = floor($const/8);
343
344  // band properties
345  $imgBand = imagecreatetruecolor($srcWidth + 6*$const, $srcHeight + 3*$const);
346 
347  $black = imagecolorallocate($imgBand, 0, 0, 0);
348  $white = imagecolorallocate($imgBand, 245, 245, 245);
349 
350  // and dots
351  $y_start = intval(($srcHeight + 3*$const) / 2);
352  $aug = intval($y_start / 5) + 1;
353  $i = 0;
354
355  while ($y_start + $i*$aug < $srcHeight + 3*$const)
356  {
357    imagefilledroundrectangle($imgBand, (3/4)*$const, $y_start + $i*$aug - $const/2, (9/4)*$const - 1, $y_start + $i*$aug + $const/2 - 1, $white, $bandRadius);
358    imagefilledroundrectangle($imgBand, (3/4)*$const, $y_start - $i*$aug - $const/2, (9/4)*$const - 1, $y_start - $i*$aug + $const/2 - 1, $white, $bandRadius);
359
360    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);
361    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);
362
363    ++$i;
364  }
365
366  // add source to band
367  imagecopy($imgBand, $srcImage, 3*$const, (3/2)*$const, 0, 0, $srcWidth, $srcHeight);
368 
369  // save image
370  switch (strtolower(get_extension($dest)))
371  {
372    case 'jpg':
373    case 'jpeg':
374      imagejpeg($imgBand, $dest, 85);
375      break;
376    case 'png':
377      imagepng($imgBand, $dest);
378      break;
379    case 'gif':
380      imagegif($imgBand, $dest);
381      break;
382  }
383}
384
385/**
386 * create a rectangle with round corners
387 * http://www.php.net/manual/fr/function.imagefilledrectangle.php#42815
388 */
389function imagefilledroundrectangle(&$img, $x1, $y1, $x2, $y2, $color, $radius)
390{
391  imagefilledrectangle($img, $x1+$radius, $y1, $x2-$radius, $y2, $color);
392 
393  if ($radius > 0)
394  {
395    imagefilledrectangle($img, $x1, $y1+$radius, $x2, $y2-$radius, $color);
396    imagefilledellipse($img, $x1+$radius, $y1+$radius, $radius*2, $radius*2, $color);
397    imagefilledellipse($img, $x2-$radius, $y1+$radius, $radius*2, $radius*2, $color);
398    imagefilledellipse($img, $x1+$radius, $y2-$radius, $radius*2, $radius*2, $color);
399    imagefilledellipse($img, $x2-$radius, $y2-$radius, $radius*2, $radius*2, $color);
400  }
401}
402
403?>
Note: See TracBrowser for help on using the repository browser.