Advertisement
Guest User

Auto Embed Youtube to PHP

a guest
Sep 10th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.74 KB | None | 0 0
  1. <?php
  2.  
  3. class AutoEmbed {
  4.  
  5.   const AE_TAG = '<!-- Videotag -->';
  6.  
  7.   private $_media_id;
  8.   private $_stub;
  9.   private $_object_attribs;
  10.   private $_object_params;
  11.  
  12.   /**
  13.    * AutoEmbed Constructor
  14.    *
  15.    * @return object - AutoEmbed object
  16.    */
  17.   public function __construct() {
  18.     global $AutoEmbed_stubs;
  19.    
  20. $AutoEmbed_stubs = array(
  21.   array(
  22.     'title' => 'YouTube',
  23.     'website' => 'http://www.youtube.com',
  24.     'url-match' => 'http://(?:video\.google\.(?:com|com\.au|co\.uk|de|es|fr|it|nl|pl|ca|cn)/(?:[^"]*?))?(?:(?:www|au|br|ca|es|fr|de|hk|ie|in|il|it|jp|kr|mx|nl|nz|pl|ru|tw|uk)\.)?youtube\.com(?:[^"]*?)?(?:&|&amp;|/|\?|;|\%3F|\%2F)(?:video_id=|v(?:/|=|\%3D|\%2F))([0-9a-z-_]{11})',
  25.     'embed-src' => 'http://www.youtube.com/v/$2&rel=0&fs=1&hd=1',
  26.     'embed-width' => '480',
  27.     'embed-height' => '295',
  28.     'image-src' => 'http://img.youtube.com/vi/$2/0.jpg',
  29.     'iframe-player' => 'http://www.youtube.com/embed/$2',
  30.   ),
  31. );
  32.   }
  33.  
  34.   /**
  35.    * Parse given URL
  36.    *
  37.    * @param $url string - href to check for embeded video
  38.    *
  39.    * @return boolean - whether or not the url contains valid/supported video
  40.    */
  41.   public function parseUrl($url) {
  42.     global $AutoEmbed_stubs;
  43.  
  44.     foreach ($AutoEmbed_stubs as $stub) {
  45.       if ( preg_match('~'.$stub['url-match'].'~imu', $url, $match) ) {
  46.         $this->_stub = $stub;
  47.  
  48.         if ( isset($stub['fetch-match'] ) ) {
  49.           return $this->_parseLink($url);
  50.  
  51.         } else {
  52.           $this->_media_id = $match;
  53.           $this->_setDefaultParams();
  54.           return true;
  55.         }
  56.       }
  57.     }
  58.  
  59.     unset($stub);
  60.     return false;
  61.   }
  62.  
  63.   /**
  64.    * Create the embed code for a local file
  65.    *
  66.    * @param $file string - the file we are wanting to embed
  67.    *
  68.    * @return boolean - whether or not the url contains valid/supported video
  69.    */
  70.   public function embedLocal($file) {
  71.     return $this->parseUrl("__local__$file");
  72.   }
  73.  
  74.   /**
  75.    * Returns info about the stub
  76.    *
  77.    * @param string $property - (optional) the specific
  78.    *           property of the stub to be returned.  If
  79.    *           ommited, array of all properties are returned
  80.    *
  81.    * @return mixed - details about the stub
  82.    */
  83.   public function getStub($property = null) {
  84.     return isset($property) ? $this->_stub[$property] : $this->_stub;
  85.   }
  86.  
  87.   /**
  88.    * Return object params about the video metadata
  89.    *
  90.    * @return array - object params
  91.    */
  92.   public function getObjectParams() {
  93.     return $this->_object_params;
  94.   }
  95.  
  96.   /**
  97.    * Return object attribute
  98.    *
  99.    * @return array - object attribute
  100.    */
  101.   public function getObjectAttribs() {
  102.     return $this->_object_attribs;
  103.   }
  104.  
  105.   /**
  106.    * Convert the url to an embedable tag
  107.    *
  108.    * return string - the embed html
  109.    */
  110.   public function getEmbedCode() {
  111.     if ( isset($this->_stub['iframe-player']) )
  112.     {
  113.       return $this->_buildiFrame();
  114.     }
  115.     return $this->_buildObject();
  116.   }
  117.  
  118.   /**
  119.    * Return a thumbnail for the embeded video
  120.    *
  121.    * return string - the thumbnail href
  122.    */
  123.   public function getImageURL() {
  124.     if (!isset($this->_stub['image-src'])) return false;
  125.  
  126.     $thumb = $this->_stub['image-src'];
  127.  
  128.     for ($i=1; $i<=count($this->_media_id); $i++) {
  129.       $thumb = str_ireplace('$'.$i, $this->_media_id[$i - 1], $thumb);
  130.     }
  131.  
  132.     return $thumb;
  133.   }
  134.  
  135.   /**
  136.    * Set the height of the object
  137.    *
  138.    * @param mixed - height to set the object to
  139.    *
  140.    * @return boolean - true if the value was set, false
  141.    *                   if parseURL hasn't been called yet
  142.    */
  143.   public function setHeight($height) {
  144.     return $this->setObjectAttrib('height', $height);
  145.   }
  146.  
  147.   /**
  148.    * Set the width of the object
  149.    *
  150.    * @param mixed - width to set the object to
  151.    *
  152.    * @return boolean - true if the value was set, false
  153.    *                   if parseURL hasn't been called yet
  154.    */
  155.   public function setWidth($width) {
  156.     return $this->setObjectAttrib('width', $width);
  157.   }
  158.  
  159.   /**
  160.    * Override a default param value for both the object
  161.    * and flash param list
  162.    *
  163.    * @param $param mixed - the name of the param to be set
  164.    *                       or an array of multiple params to set
  165.    * @param $value string - (optional) the value to set the param to
  166.    *                        if only one param is being set
  167.    *
  168.    * @return boolean - true if the value was set, false
  169.    *                   if parseURL hasn't been called yet
  170.    */
  171.   public function setParam($param, $value = null) {
  172.     return $this->setObjectParam($param, $value);
  173.   }
  174.  
  175.   /**
  176.    * Override a default object param value
  177.    *
  178.    * @param $param mixed - the name of the param to be set
  179.    *                       or an array of multiple params to set
  180.    * @param $value string - (optional) the value to set the param to
  181.    *                        if only one param is being set
  182.    *
  183.    * @return boolean - true if the value was set, false
  184.    *                   if parseURL hasn't been called yet
  185.    */
  186.   public function setObjectParam($param, $value = null) {
  187.     if (!is_array($this->_object_params)) return false;
  188.  
  189.     if ( is_array($param) ) {
  190.       foreach ($param as $p => $v) {
  191.         $this->_object_params[$p] = $v;
  192.       }
  193.  
  194.     } else {
  195.       $this->_object_params[$param] = $value;
  196.     }
  197.  
  198.     return true;
  199.   }
  200.  
  201.   /**
  202.    * Override a default object attribute value
  203.    *
  204.    * @param $param mixed - the name of the attribute to be set
  205.    *                       or an array of multiple attribs to be set
  206.    * @param $value string - (optional) the value to set the param to
  207.    *                        if only one param is being set
  208.    *
  209.    * @return boolean - true if the value was set, false
  210.    *                   if parseURL hasn't been called yet
  211.    */
  212.   public function setObjectAttrib($param, $value = null) {
  213.     if (!is_array($this->_object_attribs)) return false;
  214.  
  215.     if ( is_array($param) ) {
  216.       foreach ($param as $p => $v) {
  217.         $this->_object_attribs[$p] = $v;
  218.       }
  219.  
  220.     } else {
  221.       $this->_object_attribs[$param] = $value;
  222.     }
  223.  
  224.     return true;
  225.   }
  226.  
  227.   /**
  228.    * Attempt to parse the embed id from a given URL
  229.    */
  230.   private function _parseLink($url) {
  231.     $source = preg_replace('/[^(\x20-\x7F)]*/','', file_get_contents($url));
  232.  
  233.     if ( preg_match('~'.$this->_stub['fetch-match'].'~imu', $source, $match) ) {
  234.       $this->_media_id = $match;
  235.       $this->_setDefaultParams();
  236.       return true;
  237.     }
  238.  
  239.     return false;
  240.   }
  241.  
  242.   /**
  243.    * Build a generic object skeleton
  244.    */
  245.   private function _buildObject() {
  246.  
  247.     $object_attribs = $object_params = '';
  248.  
  249.     foreach ($this->_object_attribs as $param => $value) {
  250.       $object_attribs .= '  ' . $param . '="' . $value . '"';    
  251.     }
  252.  
  253.     foreach ($this->_object_params as $param => $value) {
  254.       $object_params .= '<param name="' . $param . '" value="' . $value . '" />';
  255.     }
  256.  
  257.     return sprintf("<object %s> %s  %s</object>", $object_attribs, $object_params, self::AE_TAG);
  258.   }
  259.  
  260.   /**
  261.    * Build an iFrame player
  262.    */
  263.   private function _buildiFrame() {
  264.     $source = $this->_stub['iframe-player'];
  265.    
  266.     for ($i=1; $i<=count($this->_media_id); $i++) {
  267.       $source = str_ireplace('$'.$i, $this->_media_id[$i - 1], $source);
  268.     }
  269.    
  270.     $width = $this->_object_attribs['width'];
  271.     $height = $this->_object_attribs['height'];
  272.  
  273.     return sprintf('<iframe type="text/html" width="%s" height="%s" src="%s" frameborder="0"></iframe>', $width, $height, $source);
  274.   }
  275.  
  276.   /**
  277.    * Set the default params for the type of
  278.    * stub we are working with
  279.    */
  280.   private function _setDefaultParams() {
  281.  
  282.     $source = $this->_stub['embed-src'];
  283.     $flashvars = (isset($this->_stub['flashvars']))? $this->_stub['flashvars'] : null;
  284.  
  285.     for ($i=1; $i<=count($this->_media_id); $i++) {
  286.       $source = str_ireplace('$'.$i, $this->_media_id[$i - 1], $source);
  287.       $flashvars = str_ireplace('$'.$i, $this->_media_id[$i - 1], $flashvars);
  288.     }
  289.  
  290.     $source = htmlspecialchars($source, ENT_QUOTES, null, false);
  291.     $flashvars = htmlspecialchars($flashvars, ENT_QUOTES, null, false);
  292.  
  293.     $this->_object_params = array(
  294.             'movie' => $source,
  295.             'quality' => 'high',
  296.             'allowFullScreen' => 'true',
  297.             'allowScriptAccess' => 'always',
  298.             'pluginspage' => 'http://www.macromedia.com/go/getflashplayer',
  299.             'autoplay' => 'false',
  300.             'autostart' => 'false',
  301.             'flashvars' => $flashvars,
  302.            );
  303.  
  304.     $this->_object_attribs = array(
  305.             'type' => 'application/x-shockwave-flash',
  306.             'data' => $source,
  307.             'width' => $this->_stub['embed-width'],
  308.             'height' => $this->_stub['embed-height'],
  309.            );
  310.   }
  311.  
  312. }
  313.  
  314. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement