|
@@ -0,0 +1,362 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace Muzich\CoreBundle\ElementFactory\lib;
|
|
4
|
+
|
|
5
|
+//require_once (SITEBASE . '/src/Muzich/CoreBundle/ElementFactory/lib/spyc.php');
|
|
6
|
+use Muzich\CoreBundle\ElementFactory\lib\Spyc;
|
|
7
|
+use \Exception;
|
|
8
|
+
|
|
9
|
+/**
|
|
10
|
+* Copyright (c) 2008, AF83
|
|
11
|
+* All rights reserved.
|
|
12
|
+*
|
|
13
|
+* Redistribution and use in source and binary forms, with or without modification,
|
|
14
|
+* are permitted provided that the following conditions are met:
|
|
15
|
+*
|
|
16
|
+* 1° Redistributions of source code must retain the above copyright notice,
|
|
17
|
+* this list of conditions and the following disclaimer.
|
|
18
|
+*
|
|
19
|
+* 2° Redistributions in binary form must reproduce the above copyright notice,
|
|
20
|
+* this list of conditions and the following disclaimer in the documentation
|
|
21
|
+* and/or other materials provided with the distribution.
|
|
22
|
+*
|
|
23
|
+* 3° Neither the name of AF83 nor the names of its contributors may be used
|
|
24
|
+* to endorse or promote products derived from this software without specific
|
|
25
|
+* prior written permission.
|
|
26
|
+*
|
|
27
|
+* THIS SOFTWARE IS PROVIDED BY THE COMPANY AF83 AND CONTRIBUTORS "AS IS"
|
|
28
|
+* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
29
|
+* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
30
|
+* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
31
|
+* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
32
|
+* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
33
|
+* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
34
|
+* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
35
|
+* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
36
|
+* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
37
|
+* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
38
|
+*
|
|
39
|
+* @copyright 2008 AF83 http://dev.af83.com
|
|
40
|
+* @author Ori Pekelman
|
|
41
|
+* @license BSD License (3 Clause) http://www.opensource.org/licenses/bsd-license.php
|
|
42
|
+* @package Video
|
|
43
|
+* @version $Id$
|
|
44
|
+* @access public
|
|
45
|
+* @todo thumbnails should be cached locally
|
|
46
|
+*
|
|
47
|
+* VideoEmbed class can be used to safely and cleanly embed videos from different sources
|
|
48
|
+*
|
|
49
|
+* The different sources are configured in the video_embed.yaml file
|
|
50
|
+*
|
|
51
|
+* NB: The embed code may be also a url
|
|
52
|
+*
|
|
53
|
+* Usage: set the configuration variables (refer to readme for details):
|
|
54
|
+* VIDEO_EMBED_CONFIG_FILE -- path of video embed config file
|
|
55
|
+*
|
|
56
|
+* $videoEmbed = new VideoEmbed($embed); optional width and height may be passed to the constructor
|
|
57
|
+* //print($videoEmbed->embed);
|
|
58
|
+* $videoEmbed->width = 240; // resize
|
|
59
|
+* $videoEmbed->height = 120;
|
|
60
|
+* //print($videoEmbed->embed);
|
|
61
|
+* //print($videoEmbed->thumb);
|
|
62
|
+
|
|
63
|
+*
|
|
64
|
+* This class requires the SpyC library to read the cobnfiguration file. please adjust the path of the include on the first line of this file
|
|
65
|
+* The video services are configured in the configuration file (video_embed.conf.inc), example for youtube google and daily motion:
|
|
66
|
+*/
|
|
67
|
+
|
|
68
|
+class VideoEmbed {
|
|
69
|
+ private $_video_embed_conf = array();
|
|
70
|
+ private $_embedSource;
|
|
71
|
+ private $_embed;
|
|
72
|
+ private $_id;
|
|
73
|
+ private $_type;
|
|
74
|
+ private $_thumb;
|
|
75
|
+ private $_url;
|
|
76
|
+ private $_width;
|
|
77
|
+ private $_height;
|
|
78
|
+ private $_readOnly = array ('thumb', 'url', 'id', 'type');
|
|
79
|
+ private $_readWrite = array ('width', 'height', 'embed');
|
|
80
|
+
|
|
81
|
+ /**
|
|
82
|
+ * VideoEmbed::__construct() Only embed is mandatory
|
|
83
|
+ *
|
|
84
|
+ * @param mixed $embed
|
|
85
|
+ * @param mixed $width
|
|
86
|
+ * @param mixed $height
|
|
87
|
+ */
|
|
88
|
+ function __construct($embed, $width = null, $height = null)
|
|
89
|
+ {
|
|
90
|
+ // load configuration
|
|
91
|
+// if (!class_exists('Spyc')) {
|
|
92
|
+// throw new exception ('Could not find SpyC library ');
|
|
93
|
+// }
|
|
94
|
+
|
|
95
|
+ $this->_video_embed_conf = Spyc::YAMLLoad(VIDEO_EMBED_CONFIG_FILE);
|
|
96
|
+
|
|
97
|
+ if (!$this->_video_embed_conf) {
|
|
98
|
+ debug_log ("Could not read configruation file or config file empty " . SITE_BASE . VIDEO_EMBED_CONFIG_FILE);
|
|
99
|
+ if (DEBUG) {
|
|
100
|
+ throw new exception ("Could not read configruation file or config file empty " . SITE_BASE . VIDEO_EMBED_CONFIG_FILE);
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ if (!$embed) {
|
|
105
|
+ debug_log ('This must be instantiated with a non empty embed code');
|
|
106
|
+ if (DEBUG) {
|
|
107
|
+ throw new exception ('This must be instantiated with a non empty embed code');
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+ // load arguments
|
|
111
|
+ $this->_embedSource = $embed;
|
|
112
|
+ $this->_width = $width?$width: $this->_video_embed_conf['defaultWidth'];
|
|
113
|
+ $this->_height = $height?$height:$this->_video_embed_conf['defaultHeight'];
|
|
114
|
+
|
|
115
|
+ $this->setup();
|
|
116
|
+ }
|
|
117
|
+ /**
|
|
118
|
+ * VideoEmbed::__set() Make some variables read only
|
|
119
|
+ *
|
|
120
|
+ * @param mixed $n
|
|
121
|
+ * @param mixed $val
|
|
122
|
+ * @return
|
|
123
|
+ */
|
|
124
|
+ function __set($n, $val)
|
|
125
|
+ {
|
|
126
|
+ if (in_array($n, $this->_readOnly)) {
|
|
127
|
+ debug_log ("Trying to set a read only property $n $val");
|
|
128
|
+ if (DEBUG) {
|
|
129
|
+ throw new exception ("Trying to set a read only property" . "$n $val");
|
|
130
|
+ }
|
|
131
|
+ return false;
|
|
132
|
+ } elseif (in_array($n, $this->_readWrite)) {
|
|
133
|
+ if ($n == "embed") {
|
|
134
|
+ $property = '_embedSource';
|
|
135
|
+ } else $property = "_" . $n;
|
|
136
|
+
|
|
137
|
+ $this->$property = $val;
|
|
138
|
+ $this->setup(); // recalculate stuff if we changed a RW property
|
|
139
|
+ return true;
|
|
140
|
+ }
|
|
141
|
+ return false;
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ /**
|
|
145
|
+ * VideoEmbed::__get()
|
|
146
|
+ *
|
|
147
|
+ * @param mixed $n
|
|
148
|
+ * @return
|
|
149
|
+ */
|
|
150
|
+ function __get($n)
|
|
151
|
+ {
|
|
152
|
+ if (in_array($n, array_merge($this->_readOnly, $this->_readWrite))) {
|
|
153
|
+ $propertyName = "_$n";
|
|
154
|
+ return $this->$propertyName;
|
|
155
|
+ } else {
|
|
156
|
+ debug_log('Trying to get a non readble property ' . $n);
|
|
157
|
+ if (DEBUG) {
|
|
158
|
+ throw new exception ('Trying to get a non readble property ' . $n);
|
|
159
|
+ }
|
|
160
|
+ return false;
|
|
161
|
+ }
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ /**
|
|
165
|
+ * VideoEmbed::setup()
|
|
166
|
+ *
|
|
167
|
+ * @return
|
|
168
|
+ */
|
|
169
|
+ private function setup ()
|
|
170
|
+ {
|
|
171
|
+ if (!$this->video_embed_type()) {
|
|
172
|
+ //debug_log ('Could not get embed type :' . $this->_embedSource);
|
|
173
|
+ if (DEBUG) {
|
|
174
|
+ throw new exception ('Could not get embed type :' . $this->_embedSource);
|
|
175
|
+ }
|
|
176
|
+ }
|
|
177
|
+ if (!$this->video_embed_id()) {
|
|
178
|
+ //debug_log ('Could not get embed id :' . $this->_embedSource);
|
|
179
|
+ if (DEBUG) {
|
|
180
|
+ throw new exception ('Could not get embed id :' . $this->_embedSource);
|
|
181
|
+ }
|
|
182
|
+ }
|
|
183
|
+ if (!$this->video_embed_url()) {
|
|
184
|
+ //debug_log ('Problem generating embed url :' . $this->_embedSource);
|
|
185
|
+ if (DEBUG) {
|
|
186
|
+ throw new exception ('Problem generating embed url :' . $this->_embedSource);
|
|
187
|
+ }
|
|
188
|
+ }
|
|
189
|
+ if (!$this->video_embed_thumb()) {
|
|
190
|
+ //debug_log ('Problem generating thumb code :' . $this->_embedSource);
|
|
191
|
+ if (DEBUG) {
|
|
192
|
+ throw new exception ('Problem generatingembed code :' . $this->_embed);
|
|
193
|
+ }
|
|
194
|
+ }
|
|
195
|
+ if (!$this->video_embed_embed()) {
|
|
196
|
+ //debug_log ('Problem generating embed code :' . $this->_embedSource);
|
|
197
|
+ if (DEBUG) {
|
|
198
|
+ throw new exception ('Problem generatingembed code :' . $this->_embedSource);
|
|
199
|
+ }
|
|
200
|
+ }
|
|
201
|
+ }
|
|
202
|
+
|
|
203
|
+ /**
|
|
204
|
+ * VideoEmbed::video_emebd_type()
|
|
205
|
+ *
|
|
206
|
+ * @param mixed $embed
|
|
207
|
+ * @return
|
|
208
|
+ */
|
|
209
|
+ private function video_embed_type()
|
|
210
|
+ {
|
|
211
|
+ $type = null;
|
|
212
|
+ $this->_type = "";
|
|
213
|
+ foreach($this->_video_embed_conf['services'] as $serviceName => $config) {
|
|
214
|
+ if (strpos(strtolower($this->_embedSource), strtolower($config['urlPattern']))) {
|
|
215
|
+ $type = $serviceName;
|
|
216
|
+ }
|
|
217
|
+ }
|
|
218
|
+ if ($type) {
|
|
219
|
+ $this->_type = $type;
|
|
220
|
+ return $this->_type;
|
|
221
|
+ }
|
|
222
|
+ return false;
|
|
223
|
+ }
|
|
224
|
+
|
|
225
|
+ /**
|
|
226
|
+ * VideoEmbed::video_embed_id()
|
|
227
|
+ *
|
|
228
|
+ * @return
|
|
229
|
+ */
|
|
230
|
+ private function video_embed_id()
|
|
231
|
+ {
|
|
232
|
+ $this->_id = "";
|
|
233
|
+
|
|
234
|
+ if (($this->_type)) {
|
|
235
|
+ $regexp = $this->_video_embed_conf['services'][$this->_type]['extractPattern'];
|
|
236
|
+ preg_match($regexp, $this->_embedSource , $match);
|
|
237
|
+
|
|
238
|
+ if ($match[count($match)-1]) $this->_id = $match[count($match)-1];
|
|
239
|
+ return $this->_id;
|
|
240
|
+ }
|
|
241
|
+ return false;
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ /**
|
|
245
|
+ * VideoEmbed::video_embed_url()
|
|
246
|
+ *
|
|
247
|
+ * @return
|
|
248
|
+ */
|
|
249
|
+ private function video_embed_url()
|
|
250
|
+ {
|
|
251
|
+ $this->_url = "";
|
|
252
|
+ if ($this->_type) {
|
|
253
|
+ if ($this->_id) $url = sprintf($this->_video_embed_conf['services'][$this->_type]['embedUrlTemplate'], $this->_id);
|
|
254
|
+ if ($url) $this->_url = $url;
|
|
255
|
+ return $this->_url;
|
|
256
|
+ }
|
|
257
|
+ return false;
|
|
258
|
+ }
|
|
259
|
+
|
|
260
|
+ /**
|
|
261
|
+ * VideoEmbed::video_embed_thumb()
|
|
262
|
+ *
|
|
263
|
+ * @return
|
|
264
|
+ */
|
|
265
|
+ private function video_embed_thumb()
|
|
266
|
+ {
|
|
267
|
+ $conf = $this->_video_embed_conf['services'][$this->_type]; // just here for readability
|
|
268
|
+ $this->_thumb = "";
|
|
269
|
+ if ($this->_type && $this->_id) {
|
|
270
|
+ if (isset($conf['thumbnailUrlExtractPattern'])) { // if we need to parse the response:
|
|
271
|
+ $thumburl = $this->extractThumbUrl($conf['thumbnailUrlTemplate'], $conf['thumbnailUrlExtractPattern']);
|
|
272
|
+ } else $thumburl = sprintf($conf['thumbnailUrlTemplate'], $this->_id);
|
|
273
|
+
|
|
274
|
+ if ($thumburl) $this->_thumb = $thumburl;
|
|
275
|
+ return $this->_thumb;
|
|
276
|
+ }
|
|
277
|
+ return false;
|
|
278
|
+ }
|
|
279
|
+
|
|
280
|
+ /**
|
|
281
|
+ * VideoEmbed::video_embed_embed()
|
|
282
|
+ *
|
|
283
|
+ * @param mixed $width
|
|
284
|
+ * @param mixed $height
|
|
285
|
+ * @return
|
|
286
|
+ */
|
|
287
|
+ private function video_embed_embed()
|
|
288
|
+ {
|
|
289
|
+ if ($this->_type) {
|
|
290
|
+ $template = isset($this->_video_embed_conf['services'][$this->_type]['embedTemplate']) ? $this->_video_embed_conf['services'][$this->_type]['embedTemplate']:$this->_video_embed_conf['embedTemplate'];
|
|
291
|
+ if ($template && $this->_url) {
|
|
292
|
+ $width = $this->_width?$this->_width:$this->_video_embed_conf['services'][$video_type]['defaultWidth'];
|
|
293
|
+ $height = $this->_height?$this->_height:$this->_video_embed_conf['services'][$video_type]['defaultHeight'];
|
|
294
|
+ $embed = sprintf($template, $this->_url, $width, $height);
|
|
295
|
+ if ($embed) $this->_embed = $embed;
|
|
296
|
+ return $this->_embed;
|
|
297
|
+ }
|
|
298
|
+ }
|
|
299
|
+
|
|
300
|
+ return false;
|
|
301
|
+ }
|
|
302
|
+ /**
|
|
303
|
+ * extractThumbUrl()
|
|
304
|
+ *
|
|
305
|
+ * @param mixed $url
|
|
306
|
+ * @param mixed $videoid
|
|
307
|
+ * @param mixed $extractPattern
|
|
308
|
+ * @return
|
|
309
|
+ */
|
|
310
|
+ private function extractThumbUrl($url, $extractPattern)
|
|
311
|
+ {
|
|
312
|
+ $vrss = file_get_contents(sprintf($url, $this->_id));
|
|
313
|
+ if (!empty($vrss)) {
|
|
314
|
+ preg_match($extractPattern, $vrss, $thumbnail_array);
|
|
315
|
+ $thumbnail = $thumbnail_array[1];
|
|
316
|
+ // Remove amp;
|
|
317
|
+ $thumbnail = str_replace('amp;', '', $thumbnail);
|
|
318
|
+ }
|
|
319
|
+
|
|
320
|
+ return $thumbnail;
|
|
321
|
+ }
|
|
322
|
+}
|
|
323
|
+
|
|
324
|
+/**
|
|
325
|
+* test_VideoEmbed() test all is well
|
|
326
|
+*
|
|
327
|
+* @return
|
|
328
|
+*/
|
|
329
|
+function test_VideoEmbed()
|
|
330
|
+{
|
|
331
|
+ $embeds[] = '<div><object width="420" height="365"><param name="movie" value="http://www.dailymotion.com/swf/x3wq9a&v3=1&related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.dailymotion.com/swf/x3wq9a&v3=1&related=0" type="application/x-shockwave-flash" width="420" height="365" allowFullScreen="true" allowScriptAccess="always"></embed></object><br /><b><a href="http://www.dailymotion.com/video/x3wq9a_defi-decorer-les-camions-de-pq_fun">Defi: décorer les camions de PQ</a></b><br /><i>Uploaded by <a href="http://www.dailymotion.com/gonzaguetv">gonzaguetv</a></i></div>';
|
|
332
|
+ $embeds[] = '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/1mXh_tyLlAY&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/1mXh_tyLlAY&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>';
|
|
333
|
+ $embeds[] = '<embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-1182786924290841590&hl=fr" flashvars=""> </embed>';
|
|
334
|
+ foreach($embeds as $embed) {
|
|
335
|
+ $videoEmbed = new VideoEmbed($embed, 420, 365);
|
|
336
|
+
|
|
337
|
+ // Mise en commentaire pour opasser le pre-commit:
|
|
338
|
+
|
|
339
|
+// echo "<h2> Type:</h2>\n";
|
|
340
|
+// print ($videoEmbed->type);
|
|
341
|
+// echo "<h2> ID:</h2>\n";
|
|
342
|
+// print($videoEmbed->id);
|
|
343
|
+// echo "<h2> Video Url:</h2>\n";
|
|
344
|
+// print($videoEmbed->url);
|
|
345
|
+// echo "<h2> Embed:</h2>\n";
|
|
346
|
+// print($videoEmbed->embed);
|
|
347
|
+// echo "<h2> Embed resized:</h2>\n";
|
|
348
|
+// $videoEmbed->width = 240;
|
|
349
|
+// $videoEmbed->height = 120;
|
|
350
|
+// print($videoEmbed->embed);
|
|
351
|
+// echo "<h2> thumb url:</h2>\n";
|
|
352
|
+// print($videoEmbed->thumb);
|
|
353
|
+// echo "<h2> thumb image:</h2>\n";
|
|
354
|
+// print('<img src="' . $videoEmbed->thumb . '" />');
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+ // this should fail
|
|
358
|
+ // $videoEmbed->thumb = "qsdqsdsq";
|
|
359
|
+ // this should fail
|
|
360
|
+ // print('<img src="' . $videoEmbed->image . '" />');
|
|
361
|
+ }
|
|
362
|
+}
|