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);
125 if(!strcmp($debugOutput,"true")) {
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)
233 * TODO: How can we check if the remote file exists without actually
234 * downloading the entire file? For now, if the file doesn't exist,
235 * a gallery item is still added, and the embedded player won't play it.
239 * Format the description to hold a reference to the embedded video
240 * This reference will be embedded using the G2 internal player,
241 * or an external player if provided by the user.
243 if(!strcmp($useInternalFlvPlayer,"false")) {
246 * The user has indicated they want to use an external flv player
247 * Make sure one is defined!
249 if(!preg_match("/\w+/",$externalFlvPlayer)) {
250 return array(GalleryCoreApi::error(ERROR_CONFIGURATION_REQUIRED,__FILE__,__LINE__,
251 "Invalid/missing external player parameter"),null,null);
254 /* Format the description to hold a reference to the embedded video */
255 $description ='<embed src="'.$externalFlvPlayer.'" ';
256 $description.= 'width="'.$width.'" height="'.$height.'" ';
257 $description.= 'bgcolor="#C0C0C0" allowfullscreen="true" ';
258 $description.= 'type="application/x-shockwave-flash" ';
259 $description.= 'pluginspage="http://www.macromedia.com/go/getflashplayer" ';
260 $description.= 'flashvars="file='.$url;
261 $description.= '&fullscreenpage='.$thumbnail;
262 $description.= '&linktarget=_Blank&image='.$thumbnail;
264 if(!preg_match("/\w+/",$externalFlvPlayerVars)) {
265 /* Format the flashvars for the internal G2 flv player */
266 $description.= '&showdigits=true&autostart=false&showfsbutton=true&';
267 $description.= '&repeat=false&lightcolor=0x9999FF';
268 $description.= '&backcolor=0x888888&frontcolor=0x000000"';
270 /* Format the flashvars for the external G2 flv player */
271 $description.= '&'.$externalFlvPlayerVars;
273 $description.= ' /> </p>';
275 /* Internal FLV player */
277 /* Format the description to hold a reference to the embedded video */
278 $macromedia_url = "http://download.macromedia.com/pub/shockwave/cabs/flash/";
279 $description = '<script type="text/javascript">'."\n";
280 $description.= '// <![CDATA['."\n";
281 $description.= 'function divResize(id, nw, nh) {'."\n";
282 $description.= 'var obj = document.getElementById(id);'."\n";
283 $description.= 'obj.style.width = nw + "px";'."\n";
284 $description.= 'obj.style.height = nh + "px";'."\n";
285 $description.= '}'."\n";
286 $description.= '// ]]>'."\n";
287 $description.= '</script>'."\n";
288 $description.= '<div id="flashvideo" style="align:left;width:525px;height:392px">'."\n";
289 $description.= '<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000"';
290 $description.= 'codebase="'.$macromedia_url.'swflash.cab#version=8,0,0,0"';
291 $description.= 'width="100%" height="100%" id="IFid1" class="ImageFrame_image">';
292 $description.= '<param name="movie" value="'.$gallery2_url.$gallery2_flv_player.'"/>';
293 $description.= '<param name="FlashVars" value="flvUrl='.$url;
294 $description.= '&Width='.$width.'&Height='.$height.'&title='.$title;
295 $description.= '&allowDl=true&thumbUrl='.$thumbnail;
296 $description.= '&langDownload=Download&langLarge=Large&langNormal=Normal"/>';
297 $description.= '<param name="quality" value="high"/>';
298 $description.= '<param name="scale" value="noscale"/>';
299 $description.= '<param name="salign" value="lt"/>';
300 $description.= '<param name="wmode" value="transparent"/>';
301 $description.= '<param name="allowScriptAccess" value="always"/>';
302 $description.= '<embed src="'.$gallery2_url.$gallery2_flv_player.'" ';
303 $description.= 'flashvars="flvUrl='.$url;
304 $description.= '&Width='.$width.'&Height='.$height.'&title='.$title;
305 $description.= '&allowDl=true&thumbUrl='.$thumbnail;
306 $description.= '&langDownload=Download&langLarge=Large&langNormal=Normal" ';
307 $description.= 'type="application/x-shockwave-flash" ';
308 $description.= 'width="100%" height="100%" quality="high" scale="noscale" salign="lt" ';
309 $description.= 'wmode="transparent" allowScriptAccess="always" ';
310 $description.= 'pluginspage="http://www.macromedia.com/go/getflashplayer"/>';
311 $description.= '</object></div>';
315 **********************************
316 * Unsupported URL to embed
317 **********************************
320 return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_FILE_TYPE,__FILE__,__LINE__,
321 "Unable to embed video from: $url"),null,null);
326 **********************************
327 * Add the video to Gallery
328 **********************************
331 /* Get a local tmp file to save the thumbnail URL to */
332 $tmpDir = $gallery->getConfig('data.gallery.tmp');
333 $tmpFile = $platform->tempnam($tmpDir, 'add');
336 if(!strcmp($debugOutput,"true")) {
337 print "thumbnail: $thumbnail <br>";
340 /* Fetch the thumbnail and save it to a local file */
341 list ($successfullyCopied, $response, $headers) =
342 GalleryCoreApi::fetchWebFile($thumbnail, $tmpFile, $extraHeaders);
343 if (!$successfullyCopied) {
344 return array(GalleryCoreApi::error(ERROR_STORAGE_FAILURE,__FILE__,__LINE__,
345 "Unable to copy thumbnail from url: $url"),null,null);
348 /* Obtain the mimeType of the thumbnail */
349 list ($ret, $mimeType) = GalleryCoreApi::getMimeType($tmpFile);
351 /* Set the filename for the item we want to add */
353 $fileName = preg_replace("/\s+/","_",$fileName);
354 $fileName = preg_replace("/'/","",$fileName);
355 $fileName = preg_replace("/\"/","",$fileName);
356 $fileName = preg_replace("/&#\d+;/","",$fileName);
358 /* General debug output */
359 if(!strcmp($debugOutput,"true")) {
360 print "<p><a href=\"".$title."\" target=\"_blank\">";
361 print "<img src=\"".$thumbnail."\">\n</a>".$summary."</p>";
362 print "<p>$description</p>";
363 print "thumbnail: $tmpFile <br>";
364 print "mimeType: $mimeType <br>";
365 print "fileName: $fileName <br>";
368 /* Make the gallery2 call to add this item to the album */
369 list ($ret, $newItem) = GalleryCoreApi::addItemToAlbum($tmpFile,
378 return array($ret, null, null);
381 $status['addedFiles'][] = array('fileName' => $url,
382 'id' => $newItem->getId(),
383 'warnings' => array());
385 @$platform->unlink($tmpFile);
388 return array(null, $error, $status);
392 * @see ItemAdd:loadTemplate
394 function loadTemplate(&$template, &$form, $item) {
397 if ($form['formName'] != 'ItemAddEmbedVideo') {
398 /* First time around, load the form with item data */
399 $form['webPage'] = '';
400 $form['formName'] = 'ItemAddEmbedVideo';
403 $session =& $gallery->getSession();
405 $template->setVariable('ItemAddEmbedVideo', $ItemAddEmbedVideo);
408 'modules/embedvideo/templates/ItemAddEmbedVideo.tpl',
409 'modules_embedvideo');
413 * @see ItemAddPlugin::getTitle
415 function getTitle() {
416 list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'embedvideo');
418 return array($ret, null);
421 return array(null, $module->translate('Embed Video'));