3 * Gallery - a web based photo album viewer and editor
4 * Copyright (C) 2000-2007 Bharat Mediratta
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22 * This plugin will handle the addition of embedded video objects
24 * @subpackage UserInterface
25 * @author Alan Pippin <apippin@pippins.net>
26 * @version $Revision: 1.1 $
28 class ItemAddEmbedVideo extends ItemAddPlugin {
31 * @see ItemAddPlugin::handleRequest
33 function handleRequest($form, &$item) {
36 $status = $error = array();
38 if (isset($form['action']['addEmbedVideoPage'])) {
40 $platform =& $gallery->getPlatform();
42 if (empty($extraHeaders)) {
43 $extraHeaders = array('Referer' => str_replace('&', '&', $url));
46 if(isset($form['webPage']['URL'])) {
48 /* Load any stored/set Parameters */
49 list ($ret, $params) =
50 GalleryCoreApi::fetchAllPluginParameters('module', 'embedvideo');
52 return array($ret, null, null);
54 foreach (array('default', 'override') as $type) {
55 $ItemAddUploadApplet[$type] = array();
56 if (!empty($params['embedvideo' . $type . 'Variables'])) {
57 $variablesArray = explode('|', $params['embedvideo' . $type . 'Variables']);
58 foreach ($variablesArray as $variable) {
59 list ($name, $value) = explode('=', $variable);
60 $ItemAddEmbedVideo[$type][$name] = $value;
65 /* Store any Parameters into some simpler, shorter, local variables */
66 $debugOutput = $ItemAddEmbedVideo['default']['debugOutput'];
67 $useInternalFlvPlayer = $ItemAddEmbedVideo['default']['useInternalFlvPlayer'];
68 $youtubeDevId = $ItemAddEmbedVideo['default']['youtubeDevId'];
69 $width=$ItemAddEmbedVideo['default']['width'];
70 $height=$ItemAddEmbedVideo['default']['height'];
71 $externalFlvPlayer = $ItemAddEmbedVideo['default']['externalFlvPlayer'];
72 $externalFlvPlayerVars = $ItemAddEmbedVideo['default']['externalFlvPlayerVars'];
73 $flvThumbnail = $ItemAddEmbedVideo['default']['flvThumbnail'];
75 /* Store other string constants we'll use later */
76 $youtubeUrlPattern="www.youtube.com";
77 $youtubeApiUrl="http://www.youtube.com/api2_rest";
78 $googleUrlPattern="video.google.com";
79 $googlePlayer="http://video.google.com/googleplayer.swf";
81 /* Gallery2 specific paths and variables */
82 $urlGenerator =& $gallery->getUrlGenerator();
83 $gallery2_url = $urlGenerator->getCurrentUrlDir();
84 $gallery2_flv_thumbnail = "modules/thumbnail/images/G2video.jpg";
85 $gallery2_flv_player = "modules/flashvideo/lib/G2flv.swf";
87 /* Store the passed URL in a shorter local variable */
88 $url = $form['webPage']['URL'];
91 *****************************
92 * Embed a Youtube Video
93 *****************************
95 if(preg_match("/$youtubeUrlPattern/",$url)) {
97 /* Make sure we can find a video_id in the URL */
98 if(preg_match("/watch\?v=(.*)/",$url,$matches)) {
99 $video_id = $matches[1];
101 return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER,__FILE__,__LINE__,
102 "Unable to extract video id from url: $url"),null,null);
105 /* Make sure we have a valid youtube developer id */
106 $dev_id = $youtubeDevId;
107 if(!preg_match("/\w+/",$dev_id)) {
108 return array(GalleryCoreApi::error(ERROR_CONFIGURATION_REQUIRED,__FILE__,__LINE__,
109 "Invalid/missing YouTube developer ID: $dev_id"),null,null);
112 /* Youtube api feed */
113 $feed = $youtubeApiUrl.'?method=youtube.videos.get_details';
114 $feed.= "&dev_id=$dev_id&video_id=$video_id";
116 /* Get the youtube xml feed as a string data source */
117 list ($successfullyCopied, $xml, $response, $headers) =
118 GalleryCoreApi::fetchWebPage($feed, $extraHeaders);
119 if (!$successfullyCopied) {
120 return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
121 "Unable to get video information at url: $url"),null,null);
129 /* Extract certain information from the xml feed */
130 preg_match_all("/\<title\>(.+?)\<\/title\>/smi",$xml, $title);
131 preg_match_all("/\<description\>(.+?)\<\/description\>/smi",$xml, $description);
132 preg_match_all("/\<thumbnail_url\>(.+?)\<\/thumbnail_url\>/smi",$xml, $thumbnail);
135 array_shift($thumbnail);
136 array_shift($description);
138 /* Replace html characters. More can be added but this seems to work */
139 for($i=0;$i<count($description[0]);$i++){
140 $description[0][$i] = preg_replace("/</","<",$description[0][$i]);
141 $description[0][$i] = html_entity_decode($description[0][$i],ENT_QUOTES);
144 /* Store the information found in some local variables */
145 $title = $title[0][0];
146 $summary = $description[0][0];
147 $thumbnail = $thumbnail[0][0];
149 /* Format the description to hold a reference to the embedded video */
150 $description = '<object width="'.$width.'" height="'.$height.'">';
151 $description.= '<param name="movie" ';
152 $description.= 'value="http://www.youtube.com/v/'.$video_id.'"></param>';
153 $description.= '<param name="wmode" value="transparent"></param>';
154 $description.= '<embed src="http://www.youtube.com/v/'.$video_id.'" ';
155 $description.= 'type="application/x-shockwave-flash" wmode="transparent" ';
156 $description.= 'width="'.$width.'" height="'.$height.'"></embed></object>';
157 $description.= "<br>$summary";
160 **********************************
161 * Embed a Google Video
162 **********************************
164 } else if(preg_match("/$googleUrlPattern/",$url)) {
166 /* Make sure we can extract a docID */
167 if(preg_match("/docid=(.*)/",$url,$matches)) {
168 $doc_id = $matches[1];
170 return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER,__FILE__,__LINE__,
171 "Unable to extract doc id from url: $url"),null,null);
174 /* Grab the contents of the webpage used to display the video on video.google.com */
175 list ($successfullyCopied, $contents, $response, $headers) =
176 GalleryCoreApi::fetchWebPage($url, $extraHeaders);
177 if (!$successfullyCopied) {
178 return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
179 "Unable to get video information at url: $url"),null,null);
182 /* Extract the summary from the webpage contents */
183 preg_match('/<meta content="(.+?)\. \w+ \d+, \d+.*" name="description">/i',
184 $contents, $matches);
185 $summary=$matches[1];
187 /* Extract the title from the webpage contents */
188 preg_match('/<title>(.+?)\s+- Google Video<\/title>/i',
189 $contents, $matches);
192 /* Extract the thumbnail URL from the webpage contents */
193 preg_match('/<img src="(http:\/\/video\.google\.com\/ThumbnailServer2.+?)" /i',
194 $contents, $matches);
195 $thumbnail=$matches[1];
196 $thumbnail=preg_replace("/offsetms=0/","offsetms=0",$thumbnail);
198 /* Format the description to hold a reference to the embedded video */
199 $description = '<embed FlashVars="autoPlay=true" ';
200 $description.= 'style="width:'.$width.'px; height:'.$height.'px;" id="VideoPlayback" ';
201 $description.= 'type="application/x-shockwave-flash" ';
202 $description.= 'src="'.$googlePlayer.'?docId='.$doc_id.'"> ';
203 $description.= '</embed>';
204 $description.= "<br>$summary";
207 **********************************
208 * Embed a remote .flv file
209 **********************************
211 } else if(preg_match("/.*\/(.+?)\.flv/i",$url,$matches)) {
213 /* Set the title and summary to the name of the file */
214 $title = $matches[1];
215 $summary = $matches[1];
218 * Set the thumbnail to some generic jpg image,
219 * since we can't extract it from the remote flv file.
220 * If no parameter is set, set it to a default value.
222 if(preg_match("/\w+/", $flvThumbnail)) {
223 $thumbnail = $flvThumbnail;
225 $thumbnail = $gallery2_url.$gallery2_flv_thumbnail;
229 * Check to make sure the URL to the remote flv file is valid
230 * (That the file exists at the URL given)
232 list ($successfullyCopied, $response, $headers) =
233 GalleryCoreApi::fetchWebPage($url, $extraHeaders);
234 if (!$successfullyCopied) {
235 return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
236 "Unable to locate a video at url: $url"),null,null);
240 * Format the description to hold a reference to the embedded video
241 * This reference will be embedded using the G2 internal player,
242 * or an external player if provided by the user.
244 if(!$useInternalFlvPlayer) {
247 * The user has indicated they want to use an external flv player
248 * Make sure one is defined!
250 if(!preg_match("/\w+/",$externalFlvPlayer)) {
251 return array(GalleryCoreApi::error(ERROR_CONFIGURATION_REQUIRED,__FILE__,__LINE__,
252 "Invalid/missing external player parameter"),null,null);
255 /* Format the description to hold a reference to the embedded video */
256 $description ='<embed src="'.$externalFlvPlayer.'" ';
257 $description.= 'width="'.$width.'" height="'.$height.'" ';
258 $description.= 'bgcolor="#C0C0C0" allowfullscreen="true" ';
259 $description.= 'type="application/x-shockwave-flash" ';
260 $description.= 'pluginspage="http://www.macromedia.com/go/getflashplayer" ';
261 $description.= 'flashvars="file='.$url;
262 $description.= '&fullscreenpage='.$thumbnail;
263 $description.= '&linktarget=_Blank&image='.$thumbnail;
265 if(!preg_match("/\w+/",$externalFlvPlayerVars)) {
266 /* Format the flashvars for the internal G2 flv player */
267 $description.= '&showdigits=true&autostart=false&showfsbutton=true&';
268 $description.= '&repeat=false&lightcolor=0x9999FF';
269 $description.= '&backcolor=0x888888&frontcolor=0x000000"';
271 /* Format the flashvars for the external G2 flv player */
272 $description.= '&'.$externalFlvPlayerVars;
274 $description.= ' /> </p>';
276 /* Internal FLV player */
278 /* Format the description to hold a reference to the embedded video */
279 $macromedia_url = "http://download.macromedia.com/pub/shockwave/cabs/flash/";
280 $description = '<script type="text/javascript">'."\n";
281 $description.= '// <![CDATA['."\n";
282 $description.= 'function divResize(id, nw, nh) {'."\n";
283 $description.= 'var obj = document.getElementById(id);'."\n";
284 $description.= 'obj.style.width = nw + "px";'."\n";
285 $description.= 'obj.style.height = nh + "px";'."\n";
286 $description.= '}'."\n";
287 $description.= '// ]]>'."\n";
288 $description.= '</script>'."\n";
289 $description.= '<div id="flashvideo" style="align:left;width:525px;height:392px">'."\n";
290 $description.= '<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000"';
291 $description.= 'codebase="'.$macromedia_url.'swflash.cab#version=8,0,0,0"';
292 $description.= 'width="100%" height="100%" id="IFid1" class="ImageFrame_image">';
293 $description.= '<param name="movie" value="'.$gallery2_url.$gallery2_flv_player.'"/>';
294 $description.= '<param name="FlashVars" value="flvUrl='.$url;
295 $description.= '&Width='.$width.'&Height='.$height.'&title='.$title;
296 $description.= '&allowDl=true&thumbUrl='.$thumbnail;
297 $description.= '&langDownload=Download&langLarge=Large&langNormal=Normal"/>';
298 $description.= '<param name="quality" value="high"/>';
299 $description.= '<param name="scale" value="noscale"/>';
300 $description.= '<param name="salign" value="lt"/>';
301 $description.= '<param name="wmode" value="transparent"/>';
302 $description.= '<param name="allowScriptAccess" value="always"/>';
303 $description.= '<embed src="'.$gallery2_url.$gallery2_flv_player.'" ';
304 $description.= 'flashvars="flvUrl='.$url;
305 $description.= '&Width='.$width.'&Height='.$height.'&title='.$title;
306 $description.= '&allowDl=true&thumbUrl='.$thumbnail;
307 $description.= '&langDownload=Download&langLarge=Large&langNormal=Normal" ';
308 $description.= 'type="application/x-shockwave-flash" ';
309 $description.= 'width="100%" height="100%" quality="high" scale="noscale" salign="lt" ';
310 $description.= 'wmode="transparent" allowScriptAccess="always" ';
311 $description.= 'pluginspage="http://www.macromedia.com/go/getflashplayer"/>';
312 $description.= '</object></div>';
316 **********************************
317 * Unsupported URL to embed
318 **********************************
321 return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_FILE_TYPE,__FILE__,__LINE__,
322 "Unable to embed video from: $url"),null,null);
327 **********************************
328 * Add the video to Gallery
329 **********************************
332 /* Get a local tmp file to save the thumbnail URL to */
333 $tmpDir = $gallery->getConfig('data.gallery.tmp');
334 $tmpFile = $platform->tempnam($tmpDir, 'add');
338 print "thumbnail: $thumbnail <br>";
341 /* Fetch the thumbnail and save it to a local file */
342 list ($successfullyCopied, $response, $headers) =
343 GalleryCoreApi::fetchWebFile($thumbnail, $tmpFile, $extraHeaders);
344 if (!$successfullyCopied) {
345 return array(GalleryCoreApi::error(ERROR_STORAGE_FAILURE,__FILE__,__LINE__,
346 "Unable to copy thumbnail from url: $url"),null,null);
349 /* Obtain the mimeType of the thumbnail */
350 list ($ret, $mimeType) = GalleryCoreApi::getMimeType($tmpFile);
352 /* Set the filename for the item we want to add */
354 $fileName = preg_replace("/\s+/","_",$fileName);
355 $fileName = preg_replace("/'/","",$fileName);
356 $fileName = preg_replace("/\"/","",$fileName);
357 $fileName = preg_replace("/&#\d+;/","",$fileName);
359 /* General debug output */
361 print "<p><a href=\"".$title."\" target=\"_blank\">";
362 print "<img src=\"".$thumbnail."\">\n</a>".$summary."</p>";
363 print "<p>$description</p>";
364 print "thumbnail: $tmpFile <br>";
365 print "mimeType: $mimeType <br>";
366 print "fileName: $fileName <br>";
369 /* Make the gallery2 call to add this item to the album */
370 list ($ret, $newItem) = GalleryCoreApi::addItemToAlbum($tmpFile,
379 return array($ret, null, null);
382 $status['addedFiles'][] = array('fileName' => $url,
383 'id' => $newItem->getId(),
384 'warnings' => array());
386 @$platform->unlink($tmpFile);
389 return array(null, $error, $status);
393 * @see ItemAdd:loadTemplate
395 function loadTemplate(&$template, &$form, $item) {
398 if ($form['formName'] != 'ItemAddEmbedVideo') {
399 /* First time around, load the form with item data */
400 $form['webPage'] = '';
401 $form['formName'] = 'ItemAddEmbedVideo';
404 $session =& $gallery->getSession();
406 $template->setVariable('ItemAddEmbedVideo', $ItemAddEmbedVideo);
409 'modules/embedvideo/templates/ItemAddEmbedVideo.tpl',
410 'modules_embedvideo');
414 * @see ItemAddPlugin::getTitle
416 function getTitle() {
417 list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'embedvideo');
419 return array($ret, null);
422 return array(null, $module->translate('Embed Video'));