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

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

consolidate upgrade process, cURL compliant with safe_mode

File size: 11.8 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      if (!ini_get('safe_mode'))
242      {
243        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
244        curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
245      }
246      if (strpos($src, 'https://') !== false)
247      {
248        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
249        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
250      }
251      if (!$return)
252      {
253        curl_setopt($ch, CURLOPT_FILE, $newf);
254      }
255      else
256      {
257        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
258      }
259     
260      $out = curl_exec($ch);
261      curl_close($ch);
262     
263      if ($out === false)
264      {
265        return 'file_error';
266      }
267      else if (!$return)
268      {
269        fclose($newf);
270        return true;
271      }
272      else
273      {
274        return $out;
275      }
276    }
277    /* file get content */
278    else if (ini_get('allow_url_fopen'))
279    {
280      if (strpos($src, 'https://') !== false and !extension_loaded('openssl'))
281      {
282        return false;
283      }
284     
285      $opts = array(
286        'http' => array(
287          'method' => "GET",
288          'user_agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
289          'header' => "Accept-language: en",
290        )
291      );
292
293      $context = stream_context_create($opts);
294     
295      if (($file = file_get_contents($src, false, $context)) === false)
296      {
297        return 'file_error';
298      }
299     
300      if (!$return)
301      {
302        file_put_contents($dest, $file);
303        return true;
304      }
305      else
306      {
307        return $file;
308      }
309    }
310   
311    return false;
312  }
313}
314
315/**
316 * and film frame to an image (need GD library)
317 * @param: string source
318 * @param: string destination (if null, the source si modified)
319 * @return: void
320 */
321function add_film_frame($src, $dest=null)
322{
323  if (empty($dest))
324  {
325    $dest = $src;
326  }
327 
328  // we need gd library
329  if (!function_exists('imagecreatetruecolor'))
330  {
331    if ($dest != $src) copy($src, $dest);
332    return;
333  }
334 
335  // open source image
336  switch (strtolower(get_extension($src)))
337  {
338    case 'jpg':
339    case 'jpeg':
340      $srcImage = imagecreatefromjpeg($src);
341      break;
342    case 'png':
343      $srcImage = imagecreatefrompng($src);
344      break;
345    case 'gif':
346      $srcImage = imagecreatefromgif($src);
347      break;
348    default:
349      if ($dest != $src) copy($src, $dest);
350      return;
351  }
352 
353  // source properties
354  $srcWidth = imagesx($srcImage);
355  $srcHeight = imagesy($srcImage);
356  $const = intval($srcWidth * 0.04);
357  $bandRadius = floor($const/8);
358
359  // band properties
360  $imgBand = imagecreatetruecolor($srcWidth + 6*$const, $srcHeight + 3*$const);
361 
362  $black = imagecolorallocate($imgBand, 0, 0, 0);
363  $white = imagecolorallocate($imgBand, 245, 245, 245);
364 
365  // and dots
366  $y_start = intval(($srcHeight + 3*$const) / 2);
367  $aug = intval($y_start / 5) + 1;
368  $i = 0;
369
370  while ($y_start + $i*$aug < $srcHeight + 3*$const)
371  {
372    imagefilledroundrectangle($imgBand, (3/4)*$const, $y_start + $i*$aug - $const/2, (9/4)*$const - 1, $y_start + $i*$aug + $const/2 - 1, $white, $bandRadius);
373    imagefilledroundrectangle($imgBand, (3/4)*$const, $y_start - $i*$aug - $const/2, (9/4)*$const - 1, $y_start - $i*$aug + $const/2 - 1, $white, $bandRadius);
374
375    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);
376    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);
377
378    ++$i;
379  }
380
381  // add source to band
382  imagecopy($imgBand, $srcImage, 3*$const, (3/2)*$const, 0, 0, $srcWidth, $srcHeight);
383 
384  // save image
385  switch (strtolower(get_extension($dest)))
386  {
387    case 'jpg':
388    case 'jpeg':
389      imagejpeg($imgBand, $dest, 85);
390      break;
391    case 'png':
392      imagepng($imgBand, $dest);
393      break;
394    case 'gif':
395      imagegif($imgBand, $dest);
396      break;
397  }
398}
399
400/**
401 * create a rectangle with round corners
402 * http://www.php.net/manual/fr/function.imagefilledrectangle.php#42815
403 */
404function imagefilledroundrectangle(&$img, $x1, $y1, $x2, $y2, $color, $radius)
405{
406  imagefilledrectangle($img, $x1+$radius, $y1, $x2-$radius, $y2, $color);
407 
408  if ($radius > 0)
409  {
410    imagefilledrectangle($img, $x1, $y1+$radius, $x2, $y2-$radius, $color);
411    imagefilledellipse($img, $x1+$radius, $y1+$radius, $radius*2, $radius*2, $color);
412    imagefilledellipse($img, $x2-$radius, $y1+$radius, $radius*2, $radius*2, $color);
413    imagefilledellipse($img, $x1+$radius, $y2-$radius, $radius*2, $radius*2, $color);
414    imagefilledellipse($img, $x2-$radius, $y2-$radius, $radius*2, $radius*2, $color);
415  }
416}
417
418?>
Note: See TracBrowser for help on using the repository browser.