source: spip-zone/_plugins_/plugins_seenthis/autoembed/AutoEmbed.class.php @ 108574

Last change on this file since 108574 was 108574, checked in by arno@…, 3 years ago

Passer les iframes en responsive.

  • Property svn:executable set to *
File size: 9.4 KB
Line 
1<?php
2/**
3 * This file is part of AutoEmbed.
4 * http://autoembed.com
5 *
6 * $Id: AutoEmbed.class.php 214 2010-02-25 15:56:12Z phpuser $
7 *
8 * AutoEmbed is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * AutoEmbed is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with AutoEmbed.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22class AutoEmbed {
23
24  const AE_TAG = '<!-- Generated by AutoEmbed (http://autoembed.com) -->';
25
26  private $_media_id;
27  private $_stub;
28  private $_object_attribs;
29  private $_object_params;
30
31  /**
32   * AutoEmbed Constructor
33   *
34   * @return object - AutoEmbed object
35   */
36  public function __construct() {
37    global $AutoEmbed_stubs;
38   
39    include_once 'stubs.php';
40  }
41
42  /**
43   * Parse given URL
44   *
45   * @param $url string - href to check for embeded video
46   *
47   * @return boolean - whether or not the url contains valid/supported video
48   */
49  public function parseUrl($url) {
50    global $AutoEmbed_stubs;
51
52        $url = preg_replace(',^https:,', 'http:', $url);
53
54    foreach ($AutoEmbed_stubs as $stub) { 
55      if ( preg_match('~'.$stub['url-match'].'~imu', $url, $match) ) {
56        $this->_stub = $stub;
57       
58        if ( isset($stub['fetch-match'] ) ) {
59          return $this->_parseLink($url);
60
61        } else {
62          $this->_media_id = $match;
63          $this->_setDefaultParams();
64          return true;
65        }
66      }
67    }
68
69    unset($stub);
70    return false;
71  }
72
73  /**
74   * Create the embed code for a local file
75   *
76   * @param $file string - the file we are wanting to embed
77   *
78   * @return boolean - whether or not the url contains valid/supported video
79   */
80  public function embedLocal($file) {
81    return $this->parseUrl("__local__$file");
82  }
83
84  /**
85   * Returns info about the stub
86   *
87   * @param string $property - (optional) the specific
88   *           property of the stub to be returned.  If
89   *           ommited, array of all properties are returned
90   *
91   * @return mixed - details about the stub
92   */
93  public function getStub($property = null) {
94    return isset($property) ? $this->_stub[$property] : $this->_stub;
95  }
96
97  /**
98   * Return object params about the video metadata
99   *
100   * @return array - object params
101   */
102  public function getObjectParams() {
103    return $this->_object_params;
104  }
105
106  /**
107   * Return object attribute
108   *
109   * @return array - object attribute
110   */
111  public function getObjectAttribs() {
112    return $this->_object_attribs;
113  }
114
115  /**
116   * Convert the url to an embedable tag
117   *
118   * return string - the embed html
119   */
120  public function getEmbedCode() {
121    if ( isset($this->_stub['iframe-player']) )
122    {
123      return $this->_buildiFrame();
124    }
125    return $this->_buildObject();
126  }
127
128  /**
129   * Return a thumbnail for the embeded video
130   *
131   * return string - the thumbnail href
132   */
133  public function getImageURL() {
134    if (!isset($this->_stub['image-src'])) return false;
135
136    $thumb = $this->_stub['image-src'];
137
138    for ($i=1; $i<=count($this->_media_id); $i++) {
139      $thumb = str_ireplace('$'.$i, $this->_media_id[$i - 1], $thumb);
140    }
141
142    return $thumb;
143  }
144
145  /**
146   * Set the height of the object
147   *
148   * @param mixed - height to set the object to
149   *
150   * @return boolean - true if the value was set, false
151   *                   if parseURL hasn't been called yet
152   */
153  public function setHeight($height) {
154    return $this->setObjectAttrib('height', $height);
155  }
156
157  /**
158   * Set the width of the object
159   *
160   * @param mixed - width to set the object to
161   *
162   * @return boolean - true if the value was set, false
163   *                   if parseURL hasn't been called yet
164   */
165  public function setWidth($width) {
166    return $this->setObjectAttrib('width', $width);
167  }
168
169  /**
170   * Override a default param value for both the object
171   * and flash param list
172   *
173   * @param $param mixed - the name of the param to be set
174   *                       or an array of multiple params to set
175   * @param $value string - (optional) the value to set the param to
176   *                        if only one param is being set
177   *
178   * @return boolean - true if the value was set, false
179   *                   if parseURL hasn't been called yet
180   */
181  public function setParam($param, $value = null) {
182    return $this->setObjectParam($param, $value);
183  }
184
185  /**
186   * Override a default object param value
187   *
188   * @param $param mixed - the name of the param to be set
189   *                       or an array of multiple params to set
190   * @param $value string - (optional) the value to set the param to
191   *                        if only one param is being set
192   *
193   * @return boolean - true if the value was set, false
194   *                   if parseURL hasn't been called yet
195   */
196  public function setObjectParam($param, $value = null) {
197    if (!is_array($this->_object_params)) return false;
198
199    if ( is_array($param) ) {
200      foreach ($param as $p => $v) {
201        $this->_object_params[$p] = $v;
202      }
203
204    } else {
205      $this->_object_params[$param] = $value;
206    }
207
208    return true;
209  }
210
211  /**
212   * Override a default object attribute value
213   *
214   * @param $param mixed - the name of the attribute to be set
215   *                       or an array of multiple attribs to be set
216   * @param $value string - (optional) the value to set the param to
217   *                        if only one param is being set
218   *
219   * @return boolean - true if the value was set, false
220   *                   if parseURL hasn't been called yet
221   */
222  public function setObjectAttrib($param, $value = null) {
223    if (!is_array($this->_object_attribs)) return false;
224
225    if ( is_array($param) ) {
226      foreach ($param as $p => $v) {
227        $this->_object_attribs[$p] = $v;
228      }
229
230    } else {
231      $this->_object_attribs[$param] = $value;
232    }
233
234    return true;
235  }
236
237  /**
238   * Attempt to parse the embed id from a given URL
239   */ 
240  private function _parseLink($url) {
241    $source = preg_replace('/[^(\x20-\x7F)]*/','', file_get_contents($url));
242    // echo $this->_stub['fetch-match'];
243    if ( preg_match('~'.$this->_stub['fetch-match'].'~imu', $source, $match) ) {
244      $this->_media_id = $match;
245      $this->_setDefaultParams();
246      return true;
247    }
248
249    return false;
250  }
251
252  /**
253   * Build a generic object skeleton
254   */
255  private function _buildObject() {
256
257    $object_attribs = $object_params = '';
258
259    foreach ($this->_object_attribs as $param => $value) {
260      $object_attribs .= '  ' . $param . '="' . $value . '"';   
261    }
262
263    foreach ($this->_object_params as $param => $value) {
264      $object_params .= '<param name="' . $param . '" value="' . $value . '" />';
265    }
266
267    return sprintf("<object %s> %s  %s</object>", $object_attribs, $object_params, self::AE_TAG);
268  }
269
270  /**
271   * Build an iFrame player
272   */
273  private function _buildiFrame() {
274    $source = $this->_stub['iframe-player'];
275   
276    for ($i=1; $i<=count($this->_media_id); $i++) {
277      $source = str_ireplace('$'.$i, $this->_media_id[$i - 1], $source);
278    }
279   
280    $width = $this->_object_attribs['width'];
281    $height = $this->_object_attribs['height'];
282    $prop = ($height/$width)*100;
283    $source = preg_replace(',^http:,', 'https:', $source);
284
285    return sprintf('<div style="position: relative; width:100%%; height:0; padding-bottom:%s%%"><iframe type="text/html" style="width:100%%;height:100%%;position: absolute;top:0; left:0;" src="%s" frameborder="0" allowFullScreen></iframe>', $prop, $source);
286
287        //          return sprintf('<iframe type="text/html" width="%s" height="%s" src="%s" frameborder="0" allowFullScreen></iframe>', $width, $height, $source);
288  }
289
290  /**
291   * Set the default params for the type of
292   * stub we are working with
293   */
294  private function _setDefaultParams() {
295
296    $source = $this->_stub['embed-src'];
297    $flashvars = (isset($this->_stub['flashvars']))? $this->_stub['flashvars'] : null;
298
299    for ($i=1; $i<=count($this->_media_id); $i++) {
300      $source = str_ireplace('$'.$i, $this->_media_id[$i - 1], $source);
301      $flashvars = str_ireplace('$'.$i, $this->_media_id[$i - 1], $flashvars);
302    }
303
304    $source = htmlspecialchars($source, ENT_QUOTES, null, false);
305    $flashvars = htmlspecialchars($flashvars, ENT_QUOTES, null, false);
306
307    $this->_object_params = array(
308            'movie' => $source,
309            'quality' => 'high',
310            'allowFullScreen' => 'true',
311            'allowScriptAccess' => 'always',
312            'pluginspage' => 'http://www.macromedia.com/go/getflashplayer',
313            'autoplay' => 'false',
314            'autostart' => 'false',
315            'flashvars' => $flashvars,
316           );
317
318    $this->_object_attribs = array(
319            'type' => 'application/x-shockwave-flash',
320            'data' => $source,
321            'width' => $this->_stub['embed-width'],
322            'height' => $this->_stub['embed-height'],
323           );
324  }
325
326}
327
328?>
Note: See TracBrowser for help on using the repository browser.