Ignore:
Timestamp:
Feb 18, 2013, 12:20:10 PM (11 years ago)
Author:
mistic100
Message:
  • add support for semi-private Vimeo videos
  • escape tags
  • compatible with safe_mode=On
  • deactivate autosize
File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/gvideo/include/functions.inc.php

    r19213 r20804  
    2626    case 'youtube':
    2727    {
    28       $video['type'] = 'youtube';
    29 
    3028      parse_str($url['query'], $url['query']);
    3129      if (empty($url['query']['v'])) return false;
     
    3634    case 'youtu': // youtu.be (short-url service)
    3735    {
     36      $video['type'] = 'youtube';
     37     
    3838      if (empty($video['video_id']))
    3939      {
    40         $video['type'] = 'youtube';
    41        
    4240        $url['path'] = explode('/', $url['path']);
    4341        $video['video_id'] = $url['path'][1];
     
    4846      if (!$safe_mode)
    4947      {
    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;
     48        $fields = 'entry(id,author,media:group(media:title(text()),media:description(text()),media:thumbnail(@url),media:keywords))';
     49        $api_url = 'http://gdata.youtube.com/feeds/api/videos/'.$video['video_id'].'?v=2&alt=json&fields='.$fields;
     50        $json = gvideo_download_remote_file($api_url, true);
     51       
     52        if ($json===false || $json=='file_error') return false;
    5353       
    5454        $json = json_decode($json, true);
    5555        $video = array_merge($video, array(
    56           'title' => $json['entry']['title']['$t'],
     56          'title' => $json['entry']['media$group']['media$title']['$t'],
    5757          'description' => $json['entry']['media$group']['media$description']['$t'],
    58           'thumbnail' => $json['entry']['media$group']['media$thumbnail'][2]['url'],
     58          'thumbnail' => $json['entry']['media$group']['media$thumbnail'][0]['url'],
    5959          'author' => $json['entry']['author'][0]['name']['$t'],
    6060          ));
     61        if (!empty($json['entry']['media$group']['media$keywords']['$t']))
     62        {
     63          $video['tags'] = $json['entry']['media$group']['media$keywords']['$t'];
     64        }
    6165      }
    6266      else
     
    8084      if (!$safe_mode)
    8185      {
    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           ));
     86        // simple API for public videos
     87        $api_url_1 = 'http://vimeo.com/api/v2/video/'.$video['video_id'].'.json';
     88        $json = gvideo_download_remote_file($api_url_1, true);
     89       
     90        if ($json!==false && $json!='file_error' && trim($json)!=$video['video_id'].' not found.')
     91        {
     92          $json = json_decode($json, true);
     93          $video = array_merge($video, array(
     94            'title' => $json[0]['title'],
     95            'description' => $json[0]['description'],
     96            'thumbnail' => $json[0]['thumbnail_large'],
     97            'author' => $json[0]['user_name'],
     98            'tags' => $json[0]['tags'],
     99            ));
     100        }
     101        else
     102        {
     103          // oEmbed API, for private videos, doesn't return keywords
     104          $api_url_2 = 'http://vimeo.com/api/oembed.json?url='.rawurlencode($video['url']);
     105          $json = gvideo_download_remote_file($api_url_2, true);
     106         
     107          if ($json===false || $json=='file_error') return false;
     108         
     109          $json = json_decode($json, true);
     110          $video = array_merge($video, array(
     111            'title' => $json['title'],
     112            'description' => $json['description'],
     113            'thumbnail' => $json['thumbnail_url'],
     114            'author' => $json['author_name'],
     115            ));
     116        }
    94117      }
    95118      else
     
    115138      {
    116139        $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;
     140        $json = gvideo_download_remote_file($api_url, true);
     141       
     142        if ($json===false || $json=='file_error') return false;
    119143       
    120144        $json = json_decode($json, true);
     
    126150          'thumbnail' => $json['thumbnail_large_url'],
    127151          'author' => $json['owner.username'],
    128           'tags' => $json['tags'],
     152          'tags' => implode(',', $json['tags']),
    129153          ));
    130154      }
     
    143167     
    144168      // no safe_mode for wat.tv
    145       $html = download_remote_file($source_url, true);
    146       if ($html === false or $html == 'file_error') return false;
     169      $html = gvideo_download_remote_file($source_url, true);
     170     
     171      if ($html===false || $html=='file_error') return false;
    147172     
    148173      preg_match('#<meta property="og:video" content="http://www.wat.tv/swf2/([^"/>]+)" />#', $html, $matches);
     
    164189     
    165190      preg_match_all('#<meta property="video:tag" content="([^">]+)" />#', $html, $matches);
    166       $video['tags'] = $matches[1];
     191      $video['tags'] = implode(',',  $matches[1]);
    167192      break;
    168193    }
     
    180205      if (!$safe_mode)
    181206      {
    182         $html = download_remote_file($source_url, true);
    183         if ($html === false or $html == 'file_error') return false;
     207        $html = gvideo_download_remote_file($source_url, true);
     208       
     209        if ($html===false || $html=='file_error') return false;
    184210       
    185211        preg_match('#<meta property="og:title" content="([^">]*)" />#', $html, $matches);
     
    196222       
    197223        preg_match('#<meta name="keywords" content="([^">]+)" />#', $html, $matches);
    198         $video['tags'] = array_map('trim', explode(',', $matches[1]));
     224        $video['tags'] = $matches[1];
    199225      }
    200226      else
     
    211237 
    212238  return $video;
     239}
     240
     241/**
     242 * @params:
     243 *  $video (from parse_video_url)
     244 *  $config :
     245 *    - category, integer
     246 *    - add_film_frame, boolean
     247 *    - sync_description, boolean
     248 *    - sync_tags, boolean
     249 *    - with, integer
     250 *    - height, integer
     251 *    - autoplay, integer (0-1)
     252 */
     253function add_video($video, $config)
     254{
     255  global $page, $conf;
     256 
     257  $query = '
     258SELECT picture_id
     259  FROM '.GVIDEO_TABLE.'
     260  WHERE type = "'.$video['type'].'"
     261    AND video_id = "'.$video['video_id'].'"
     262;';
     263  $result = pwg_query($query);
     264 
     265  if (pwg_db_num_rows($result))
     266  {
     267    $page['warnings'][] = l10n('This video was already registered');
     268    list($image_id) = pwg_db_fetch_row($result);
     269    return $image_id;
     270  }
     271 
     272  include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php');
     273 
     274  // download thumbnail
     275  $thumb_ext = empty($video['thumbnail']) ? 'jpg' : get_extension($video['thumbnail']);
     276  $thumb_name = $video['type'].'-'.$video['video_id'].'-'.uniqid().'.'.$thumb_ext;
     277  $thumb_source = $conf['data_location'].$thumb_name;
     278  if ( empty($video['thumbnail']) or gvideo_download_remote_file($video['thumbnail'], $thumb_source) !== true )
     279  {
     280    $thumb_source = $conf['data_location'].get_filename_wo_extension($thumb_name).'.jpg';
     281    copy(GVIDEO_PATH.'mimetypes/'.$video['type'].'.jpg', $thumb_source);
     282  }
     283 
     284  if ($config['add_film_frame'])
     285  {
     286    add_film_frame($thumb_source);
     287  }
     288 
     289  // add image and update infos
     290  $image_id = add_uploaded_file($thumb_source, $thumb_name, array($config['category']));
     291 
     292  $updates = array(
     293    'name' => pwg_db_real_escape_string($video['title']),
     294    'author' => pwg_db_real_escape_string($video['author']),
     295    'is_gvideo' => 1,
     296    );
     297   
     298  if ( $config['sync_description'] and !empty($video['description']) )
     299  {
     300    $updates['comment'] = pwg_db_real_escape_string($video['description']);
     301  }
     302 
     303  if ( $config['sync_tags'] and !empty($video['tags']) )
     304  {
     305    $tags = pwg_db_real_escape_string($video['tags']);
     306    set_tags(get_tag_ids($tags), $image_id);
     307  }
     308 
     309  single_update(
     310    IMAGES_TABLE,
     311    $updates,
     312    array('id' => $image_id),
     313    true
     314    );
     315 
     316  // register video
     317  if ( !preg_match('#^([0-9]*)$#', $config['width']) or !preg_match('#^([0-9]*)$#', $config['height']) )
     318  {
     319    $config['width'] = $config['height'] = '';
     320  }
     321  if ( $config['autoplay']!='0' and $config['autoplay']!='1' )
     322  {
     323    $config['autoplay'] = '';
     324  }
     325 
     326  $insert = array(
     327    'picture_id' => $image_id,
     328    'url' => $video['url'],
     329    'type' => $video['type'],
     330    'video_id' => $video['video_id'],
     331    'width' => $config['width'],
     332    'height' => $config['height'],
     333    'autoplay' => $config['autoplay'],
     334    );
     335   
     336  single_insert(
     337    GVIDEO_TABLE,
     338    $insert
     339    );
     340   
     341  return $image_id;
    213342}
    214343
     
    233362 * @param: mixed destination file (if true, file content is returned)
    234363 */
    235 if (!function_exists('download_remote_file'))
     364function gvideo_download_remote_file($src, $dest, $headers=array())
    236365{
    237   function download_remote_file($src, $dest)
    238   {
    239     if (empty($src))
     366  if (empty($src))
     367  {
     368    return false;
     369  }
     370 
     371  $return = ($dest === true) ? true : false;
     372 
     373  array_push($headers, 'Accept-language: en');
     374 
     375  /* curl */
     376  if (function_exists('curl_init'))
     377  {
     378    if (!$return)
     379    {
     380      $newf = fopen($dest, "wb");
     381    }
     382    $ch = curl_init();
     383   
     384    curl_setopt($ch, CURLOPT_URL, $src);
     385    curl_setopt($ch, CURLOPT_HEADER, false);
     386    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     387    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
     388    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
     389    if (!ini_get('safe_mode'))
     390    {
     391      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     392      curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
     393    }
     394    if (strpos($src, 'https://') !== false)
     395    {
     396      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
     397      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     398    }
     399    if (!$return)
     400    {
     401      curl_setopt($ch, CURLOPT_FILE, $newf);
     402    }
     403    else
     404    {
     405      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     406    }
     407   
     408    $out = curl_exec($ch);
     409    curl_close($ch);
     410   
     411    if ($out === false)
     412    {
     413      return 'file_error';
     414    }
     415    else if (!$return)
     416    {
     417      fclose($newf);
     418      return true;
     419    }
     420    else
     421    {
     422      return $out;
     423    }
     424  }
     425  /* file get content */
     426  else if (ini_get('allow_url_fopen'))
     427  {
     428    if (strpos($src, 'https://') !== false and !extension_loaded('openssl'))
    240429    {
    241430      return false;
    242431    }
    243432   
    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   }
     433    $opts = array(
     434      'http' => array(
     435        'method' => "GET",
     436        'user_agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
     437        'header' => implode("\r\n", $headers),
     438      )
     439    );
     440
     441    $context = stream_context_create($opts);
     442   
     443    if (($file = file_get_contents($src, false, $context)) === false)
     444    {
     445      return 'file_error';
     446    }
     447   
     448    if (!$return)
     449    {
     450      file_put_contents($dest, $file);
     451      return true;
     452    }
     453    else
     454    {
     455      return $file;
     456    }
     457  }
     458 
     459  return false;
    331460}
    332461
Note: See TracChangeset for help on using the changeset viewer.