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.
21 GalleryCoreApi::requireOnce('modules/gd/classes/GdToolkitHelper.class');
25 * This plugin will handle the addition of embedded video objects
27 * @subpackage UserInterface
28 * @author Alan Pippin <apippin@pippins.net>
29 * @version $Revision: 1.1 $
31 class ItemAddEmbedVideo extends ItemAddPlugin {
34 * @see ItemAddPlugin::handleRequest
36 function handleRequest($form, &$item) {
39 $status = $error = array();
41 if (isset($form['action']['addEmbedVideoPage'])) {
43 $platform =& $gallery->getPlatform();
45 if (empty($extraHeaders)) {
46 $extraHeaders = array('Referer' => str_replace('&', '&', $url));
49 if(isset($form['webPage']['URL'])) {
51 /* Load any stored/set Parameters */
52 list ($ret, $params) =
53 GalleryCoreApi::fetchAllPluginParameters('module', 'embedvideo');
55 return array($ret, null, null);
57 foreach (array('default', 'override') as $type) {
58 $ItemAddUploadApplet[$type] = array();
59 if (!empty($params['embedvideo' . $type . 'Variables'])) {
60 $variablesArray = explode('|', $params['embedvideo' . $type . 'Variables']);
61 foreach ($variablesArray as $variable) {
62 list ($name, $value) = explode('=', $variable);
63 $ItemAddEmbedVideo[$type][$name] = $value;
64 /* print "type: $type name: $name value: $value <br>"; */
69 /* Store any Parameters into some simpler, shorter, local variables */
70 global $debugOutput, $useInternalFlvPlayer, $youtubeDevId;
71 global $width, $height, $externalFlvPlayer, $externalFlvPlayerVars;
72 global $flvThumbnail, $useRemoteSize, $autoStart;
74 /* Find out what value our parameters should have by looking to see if they
75 * are defined in our overrides section or default section. If they are not
76 * defined in either of these 2 places, pass in a default value to set them to
78 $debugOutput = $this->getParameter($ItemAddEmbedVideo, 'debugOutput', "false");
79 $useInternalFlvPlayer = $this->getParameter($ItemAddEmbedVideo, 'useInternalFlvPlayer', "true");
80 $youtubeDevId = $this->getParameter($ItemAddEmbedVideo, 'youtubeDevId', "");
81 $width = $this->getParameter($ItemAddEmbedVideo, 'width', "320");
82 $height = $this->getParameter($ItemAddEmbedVideo, 'height', "240");
83 $externalFlvPlayer = $this->getParameter($ItemAddEmbedVideo, 'externalFlvPlayer', "");
84 $externalFlvPlayerVars = $this->getParameter($ItemAddEmbedVideo, 'externalFlvPlayerVars', "");
85 $flvThumbnail = $this->getParameter($ItemAddEmbedVideo, 'flvThumbnail', "");
86 $useRemoteSize = $this->getParameter($ItemAddEmbedVideo, 'useRemoteSize', "false");
87 $autoStart = $this->getParameter($ItemAddEmbedVideo, 'autoStart', "false");
89 /* Store other string constants we'll use later */
92 $youtubeUrlPattern="youtube.com";
93 $youtubeApiUrl="http://www.youtube.com/api2_rest";
98 $googleUrlPattern="video.google.com";
99 $googlePlayer="http://video.google.com/googleplayer.swf";
104 $yahooUrlPattern="video.yahoo.com";
105 $yahooThumbnailUrl="http://thmg01.video.search.yahoo.com/image/";
108 $metacafeUrlPattern="metacafe.com";
109 $metacafeThumbnailUrl="http://www.metacafe.com/thumb/";
111 /* Gallery2 specific paths and variables */
112 $urlGenerator =& $gallery->getUrlGenerator();
113 $gallery2_url = $urlGenerator->getCurrentUrlDir();
114 $gallery2_flv_thumbnail = "modules/thumbnail/images/G2video.jpg";
115 $gallery2_flv_player = "modules/flashvideo/lib/G2flv.swf";
117 /* Store the passed URL in a shorter local variable */
118 $url = $form['webPage']['URL'];
121 *****************************
122 * Embed a Youtube Video
123 *****************************
125 if(preg_match("/$youtubeUrlPattern/",$url)) {
127 /* Make sure we can find a video_id in the URL */
128 if(preg_match("/watch\?v=(.*)/",$url,$matches)) {
129 $video_id = $matches[1];
131 return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER,__FILE__,__LINE__,
132 "Unable to extract video id from url: $url"),null,null);
135 /* Make sure we have a valid youtube developer id */
136 $dev_id = $youtubeDevId;
137 if(!preg_match("/\w+/",$dev_id)) {
138 return array(GalleryCoreApi::error(ERROR_CONFIGURATION_REQUIRED,__FILE__,__LINE__,
139 "Invalid/missing YouTube developer ID: $dev_id"),null,null);
142 /* Youtube api feed */
143 $feed = $youtubeApiUrl.'?method=youtube.videos.get_details';
144 $feed.= "&dev_id=$dev_id&video_id=$video_id";
146 /* Get the youtube xml feed as a string data source */
147 list ($successfullyCopied, $xml, $response, $headers) =
148 GalleryCoreApi::fetchWebPage($feed, $extraHeaders);
149 if (!$successfullyCopied) {
150 return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
151 "Unable to get video information at url: $url - $response"),null,null);
154 if(!strcmp($debugOutput,"true")) {
158 /* Extract certain information from the xml feed */
159 preg_match_all("/\<title\>(.+?)\<\/title\>/smi",$xml, $title);
160 preg_match_all("/\<description\>(.+?)\<\/description\>/smi",$xml, $description);
161 preg_match_all("/\<thumbnail_url\>(.+?)\<\/thumbnail_url\>/smi",$xml, $thumbnail);
164 array_shift($thumbnail);
165 array_shift($description);
167 /* Replace html characters. More can be added but this seems to work */
168 for($i=0;$i<count($description[0]);$i++){
169 $description[0][$i] = preg_replace("/</","<",$description[0][$i]);
170 $description[0][$i] = html_entity_decode($description[0][$i],ENT_QUOTES);
173 /* Store the information found in some local variables */
174 $title = $title[0][0];
175 $summary = $description[0][0];
176 $thumbnail = $thumbnail[0][0];
178 /* Determine what our width and height should be based on our useRemoteSize parameter */
179 if(!strcmp($useRemoteSize,"true")) {
180 $width = $youtubeWidth;
181 $height = $youtubeHeight;
184 /* Determine if the video should autoplay or not based on the autoStart parameter */
186 if(!strcmp($autoStart,"true")) {
187 $autoStartStr="&autoplay=1";
190 /* Format the description to hold a reference to the embedded video */
191 $description = '<object width="'.$width.'" height="'.$height.'">';
192 $description.= '<param name="movie" ';
193 $description.= 'value="http://www.youtube.com/v/'.$video_id.'"></param>';
194 $description.= '<param name="wmode" value="transparent"></param>';
195 $description.= '<embed src="http://www.youtube.com/v/'.$video_id.$autoStartStr.'" ';
196 $description.= 'type="application/x-shockwave-flash" wmode="transparent" ';
197 $description.= 'width="'.$width.'" height="'.$height.'"></embed></object>';
198 $description.= "<br>$summary";
201 **********************************
202 * Embed a Google Video
203 **********************************
205 } else if(preg_match("/$googleUrlPattern/",$url)) {
207 /* Make sure we can extract a docID */
208 if(preg_match("/docid=(.*)/",$url,$matches)) {
209 $doc_id = $matches[1];
211 return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER,__FILE__,__LINE__,
212 "Unable to extract doc id from url: $url"),null,null);
215 /* Grab the contents of the webpage used to display the video on video.google.com */
216 list ($successfullyCopied, $contents, $response, $headers) =
217 GalleryCoreApi::fetchWebPage($url, $extraHeaders);
218 if (!$successfullyCopied) {
219 return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
220 "Unable to get video information at url: $url - $response"),NULL,NULL);
223 /* Extract the summary from the webpage contents */
224 preg_match('/<meta content="(.+?)\. \w+ \d+, \d+.*" name="description">/i',
225 $contents, $matches);
226 $summary=$matches[1];
228 /* Extract the title from the webpage contents */
230 if(preg_match('/<title>(.+?)\s+- Google Video<\/title>/i', $contents, $matches)) {
232 } else if(preg_match('/<title>(.+?)<\/title>/i', $contents, $matches)) {
236 /* Extract the thumbnail URL from the webpage contents */
237 preg_match('/<img src="(http:\/\/video\.google\.com\/ThumbnailServer2.+?)" /i',
238 $contents, $matches);
239 $thumbnail=$matches[1];
240 $thumbnail=preg_replace("/offsetms=0/","offsetms=0",$thumbnail);
242 /* Determine what our width and height should be based on our useRemoteSize parameter */
243 if(!strcmp($useRemoteSize,"true")) {
244 $width = $googleWidth;
245 $height = $googleHeight;
248 /* Determine if the video should autoplay or not based on the autoStart parameter */
250 if(!strcmp($autoStart,"true")) {
251 $autoStartStr="&autoplay=1";
254 /* Format the description to hold a reference to the embedded video */
255 $description = '<embed FlashVars=';
256 $description.= 'style="width:'.$width.'px; height:'.$height.'px;" id="VideoPlayback" ';
257 $description.= 'type="application/x-shockwave-flash" ';
258 $description.= 'src="'.$googlePlayer.'?docId='.$doc_id.$autoStartStr.'"> ';
259 $description.= '</embed>';
260 $description.= "<br>$summary";
263 **********************************
264 * Embed a Yahoo Video
265 **********************************
266 * TODO: The autoStart parameter doesn't work with the yahoo video player
268 } else if(preg_match("/$yahooUrlPattern/",$url)) {
270 /* Make sure we can extract a vidID */
271 if(preg_match("/vid=(.*)/",$url,$matches)) {
272 $vid_id = $matches[1];
274 return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER,__FILE__,__LINE__,
275 "Unable to extract vid id from url: $url"),null,null);
278 /* Grab the contents of the webpage used to display the video on video.google.com */
279 list ($successfullyCopied, $contents, $response, $headers) =
280 GalleryCoreApi::fetchWebPage($url, $extraHeaders);
281 if (!$successfullyCopied) {
282 return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
283 "Unable to get video information at url: $url - $response"),NULL,NULL);
286 /* Extract the summary from the webpage contents */
287 preg_match('/Description:<\/em><p>(.+?)<\/p>/', $contents, $matches);
288 $summary=$matches[1];
290 /* Extract the title from the webpage contents */
292 if(preg_match('/<title>(.+?)\s+- Yahoo! Video<\/title>/i', $contents, $matches)) {
294 } else if(preg_match('/<title>(.+?)<\/title>/i', $contents, $matches)) {
298 /* Build the thumbnail URL from the vid_id */
299 $thumbnail=$yahooThumbnailUrl.$vid_id."_01";
301 /* Format the description to hold a reference to the embedded video */
302 preg_match('/(<embed src.+?<\/embed>)/', $contents, $matches);
303 $description=$matches[1];
304 if(!strcmp($useRemoteSize,"false")) {
305 $description=preg_replace("/width='\d+'/","width='".$width."'",$description);
306 $description=preg_replace("/height='\d+'/","height='".$height."'",$description);
308 $description.= "<br>$summary";
311 **********************************
312 * Embed a MetaCafe Video
313 **********************************
315 } else if(preg_match("/$metacafeUrlPattern/",$url)) {
317 /* Make sure we can extract a itemID */
318 if(preg_match("/\/watch\/(.+?)\/(.+?)/",$url,$matches)) {
319 $item_id = $matches[1];
321 return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER,__FILE__,__LINE__,
322 "Unable to extract item id from url: $url"),null,null);
325 /* Grab the contents of the webpage used to display the video on video.google.com */
326 list ($successfullyCopied, $contents, $response, $headers) =
327 GalleryCoreApi::fetchWebPage($url, $extraHeaders);
328 if (!$successfullyCopied) {
329 return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
330 "Unable to get video information at url: $url - $response"),NULL,NULL);
333 /* Extract the summary from the webpage contents */
334 preg_match('/<meta name="description" content="(.+?)" \/>/i', $contents, $matches);
335 $summary=$matches[1];
337 /* Extract the title from the webpage contents */
338 preg_match('/<title>(.+?)<\/title>/i', $contents, $matches);
341 /* Build the thumbnail URL from the item_id */
342 $thumbnail=$metacafeThumbnailUrl.$item_id.".jpg";
344 /* Format the description to hold a reference to the embedded video */
345 preg_match('/(embed src.+?\/embed)/', $contents, $matches);
346 $description="<".$matches[1];
347 $description=preg_replace("/"/","'",$description);
348 if(!strcmp($useRemoteSize,"false")) {
349 $description=preg_replace("/width='\d+'/","width='".$width."'",$description);
350 $description=preg_replace("/height='\d+'/","height='".$height."'",$description);
352 if(!strcmp($autoStart,"true")) {
353 $description=preg_replace("/\.swf/",".swf?playerVars=autoPlay=yes",$description);
355 $description.= "</embed>";
356 $description.= "<br>$summary";
359 **********************************
360 * Embed a remote .flv file
361 **********************************
363 } else if(preg_match("/.*\/(.+?)\.flv/i",$url,$matches)) {
365 /* Set the title and summary to the name of the file */
366 $title = $matches[1];
367 $summary = $matches[1];
370 * Set the thumbnail to some generic jpg image,
371 * since we can't extract it from the remote flv file.
372 * If no parameter is set, set it to a default value.
374 if(preg_match("/\w+/", $flvThumbnail)) {
375 $thumbnail = $flvThumbnail;
377 $thumbnail = $gallery2_url.$gallery2_flv_thumbnail;
381 * Check to make sure the URL to the remote flv file is valid
382 * (That the file exists at the URL given)
384 list ($successfullyCopied, $response, $headers) =
385 $this->fetchWebFileHeaders($url, $extraHeaders);
386 if (!$successfullyCopied) {
387 return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
388 "Unable to find the video at url: $url - $response"),NULL,NULL);
392 * Format the description to hold a reference to the embedded video
393 * This reference will be embedded using the G2 internal player,
394 * or an external player if provided by the user.
396 if(!strcmp($useInternalFlvPlayer,"false")) {
399 * The user has indicated they want to use an external flv player
400 * Make sure one is defined!
402 if(!preg_match("/\w+/",$externalFlvPlayer)) {
403 return array(GalleryCoreApi::error(ERROR_CONFIGURATION_REQUIRED,__FILE__,__LINE__,
404 "Invalid/missing external player parameter"),null,null);
407 /* Format the description to hold a reference to the embedded video */
408 $description ='<embed src="'.$externalFlvPlayer.'" ';
409 $description.= 'width="'.$width.'" height="'.$height.'" ';
410 $description.= 'bgcolor="#C0C0C0" allowfullscreen="true" ';
411 $description.= 'type="application/x-shockwave-flash" ';
412 $description.= 'pluginspage="http://www.macromedia.com/go/getflashplayer" ';
413 $description.= 'flashvars="file='.$url;
414 $description.= '&fullscreenpage='.$thumbnail;
415 $description.= '&linktarget=_Blank&image='.$thumbnail;
417 if(!preg_match("/\w+/",$externalFlvPlayerVars)) {
418 /* Format the flashvars for the internal G2 flv player */
419 $description.= '&showdigits=true&autostart='.$autoStart.'&showfsbutton=true&';
420 $description.= '&repeat=false&lightcolor=0x9999FF';
421 $description.= '&backcolor=0x888888&frontcolor=0x000000"';
423 /* Format the flashvars for the external G2 flv player */
424 $description.= '&'.$externalFlvPlayerVars;
426 $description.= ' /> </p>';
428 /* Internal FLV player */
430 /* Format the description to hold a reference to the embedded video */
431 $macromedia_url = "http://download.macromedia.com/pub/shockwave/cabs/flash/";
432 $description = '<script type="text/javascript">'."\n";
433 $description.= '// <![CDATA['."\n";
434 $description.= 'function divResize(id, nw, nh) {'."\n";
435 $description.= 'var obj = document.getElementById(id);'."\n";
436 $description.= 'obj.style.width = nw + "px";'."\n";
437 $description.= 'obj.style.height = nh + "px";'."\n";
438 $description.= '}'."\n";
439 $description.= '// ]]>'."\n";
440 $description.= '</script>'."\n";
441 $description.= '<div id="flashvideo" style="align:left;width:525px;height:392px">'."\n";
442 $description.= '<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000"';
443 $description.= 'codebase="'.$macromedia_url.'swflash.cab#version=8,0,0,0"';
444 $description.= 'width="100%" height="100%" id="IFid1" class="ImageFrame_image">';
445 $description.= '<param name="movie" value="'.$gallery2_url.$gallery2_flv_player.'"/>';
446 $description.= '<param name="FlashVars" value="flvUrl='.$url;
447 $description.= '&Width='.$width.'&Height='.$height.'&title='.$title;
448 $description.= '&allowDl=true&thumbUrl='.$thumbnail.'&autoStart='.$autoStart;
449 $description.= '&langDownload=Download&langLarge=Large&langNormal=Normal"/>';
450 $description.= '<param name="quality" value="high"/>';
451 $description.= '<param name="scale" value="noscale"/>';
452 $description.= '<param name="salign" value="lt"/>';
453 $description.= '<param name="wmode" value="transparent"/>';
454 $description.= '<param name="allowScriptAccess" value="always"/>';
455 $description.= '<embed src="'.$gallery2_url.$gallery2_flv_player.'" ';
456 $description.= 'flashvars="flvUrl='.$url;
457 $description.= '&Width='.$width.'&Height='.$height.'&title='.$title;
458 $description.= '&allowDl=true&thumbUrl='.$thumbnail.'&autoStart='.$autoStart;
459 $description.= '&langDownload=Download&langLarge=Large&langNormal=Normal" ';
460 $description.= 'type="application/x-shockwave-flash" ';
461 $description.= 'width="100%" height="100%" quality="high" scale="noscale" salign="lt" ';
462 $description.= 'wmode="transparent" allowScriptAccess="always" ';
463 $description.= 'pluginspage="http://www.macromedia.com/go/getflashplayer"/>';
464 $description.= '</object></div>';
468 **********************************
469 * Unsupported URL to embed
470 **********************************
473 return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_FILE_TYPE,__FILE__,__LINE__,
474 "Unable to embed video from: $url"),null,null);
479 **********************************
480 * Add the video to Gallery
481 **********************************
484 /* Get a local tmp file to save the thumbnail URL to */
485 $tmpDir = $gallery->getConfig('data.gallery.tmp');
486 $tmpFile = $platform->tempnam($tmpDir, 'add');
489 if(!strcmp($debugOutput,"true")) {
490 print "thumbnail: $thumbnail <br>";
493 /* Fetch the thumbnail and save it to a local file */
494 list ($successfullyCopied, $response, $headers) =
495 GalleryCoreApi::fetchWebFile($thumbnail, $tmpFile, $extraHeaders);
496 if (!$successfullyCopied) {
497 return array(GalleryCoreApi::error(ERROR_STORAGE_FAILURE,__FILE__,__LINE__,
498 "Unable to copy thumbnail from url: $url - $response"),null,null);
501 /* Obtain the mimeType of the thumbnail */
502 list ($ret, $mimeType) = GalleryCoreApi::getMimeType($tmpFile);
504 /* Set the filename for the item we want to add */
506 $fileName = preg_replace("/\s+/","_",$fileName);
507 $fileName = preg_replace("/'/","",$fileName);
508 $fileName = preg_replace("/\"/","",$fileName);
509 $fileName = preg_replace("/&#\d+;/","",$fileName);
511 /* General debug output */
512 if(!strcmp($debugOutput,"true")) {
513 print "title: $title <br>";
514 print "<p><a href=\"".$title."\" target=\"_blank\">";
515 print "<img src=\"".$thumbnail."\">\n</a>".$summary."</p>";
516 print "<p>$description</p>";
517 print "thumbnail: $tmpFile <br>";
518 print "mimeType: $mimeType <br>";
519 print "fileName: $fileName <br>";
520 print "<br><br><b>Video Successfully Added to your Gallery Album</b><br><br>";
523 /* Resize the thumbnail image to the size indicated by our album */
525 list ($ret, $toolkit) = GalleryCoreApi::getToolkitByOperation($mimeType, 'resize');
527 $debugString.="Checking to see if a toolkit that can resize images was found. <br>";
528 if (isset($toolkit)) {
529 $newTmpFile = $platform->tempnam($tmpDir, 'add');
530 $newTmpFile.= ".jpg";
531 $thumbnailSize = 150;
532 list ($ret, $preferences) =
533 GalleryCoreApi::fetchDerivativePreferencesForItem($item->getId());
535 foreach ($preferences as $preference) {
536 if (preg_match('/thumbnail\|(\d+)/',
537 $preference['derivativeOperations'], $matches)) {
538 $thumbnailSize = $matches[1];
539 $debugString.="Found thumbnail size in album preferences: $thumbnailSize <br>";
544 $debugString.="Resizing thumbnail image to $thumbnailSize: $tmpFile -> $newTmpFile <br>";
545 $toolkit->performOperation($mimeType, 'resize', $tmpFile, $newTmpFile,
546 array($thumbnailSize,$thumbnailSize));
549 if(!strcmp($debugOutput,"true")) {
550 print "$debugString";
553 /* Make the gallery2 call to add this item to the album */
554 list ($ret, $newItem) = GalleryCoreApi::addItemToAlbum($newTmpFile,
563 return array($ret, null, null);
566 $status['addedFiles'][] = array('fileName' => $url,
567 'id' => $newItem->getId(),
568 'warnings' => array());
571 /* Keep the tmpfiles around if we are in debug mode. Otherwise, remove them. */
572 if(!strcmp($debugOutput,"false")) {
573 @$platform->unlink($tmpFile);
574 @$platform->unlink($newTmpFile);
579 return array(null, $error, $status);
583 * A simple function to resolve the value of a parameter from
584 * the default or override value if it exists, or set it
585 * to the default passed as an argument.
587 function getParameter($params, $name, $default="") {
588 if(isset($params['override'][$name])) {
589 /* print "override $name = ".$params['override'][$name]."<br>"; */
590 return($params['override'][$name]);
591 } else if(isset($params['default'][$name])) {
592 /* print "default $name = ".$params['default'][$name] ."<br>"; */
593 return($params['default'][$name]);
595 /* print "$name = $default <br>"; */
601 * A simple function to get the headers only (no body) for a given URL
602 * This was taken from GalleryCoreApi::requestWebPage
604 function fetchWebFileHeaders($url, $requestHeaders=array()) {
607 $requestMethod='GET';
609 /* Convert illegal characters */
610 $url = str_replace(' ', '%20', $url);
612 /* Unescape ampersands, since if the URL comes from form input it will be escaped */
613 $url = str_replace('&', '&', $url);
615 $platform =& $gallery->getPlatform();
617 $urlComponents = parse_url($url);
618 if (empty($urlComponents['port'])) {
619 $urlComponents['port'] = 80;
621 if (empty($urlComponents['path'])) {
622 $urlComponents['path'] = '/';
625 $handle = @$platform->fsockopen(
626 $urlComponents['host'], $urlComponents['port'], $errno, $errstr, 5);
627 if (empty($handle)) {
628 $gallery->debug("Error $errno: '$errstr' requesting $url");
629 return array(null, null, null);
632 $requestUri = $urlComponents['path'];
633 if (!empty($urlComponents['query'])) {
634 $requestUri .= '?' . $urlComponents['query'];
636 $headerLines = array('Host: ' . $urlComponents['host']);
637 foreach ($requestHeaders as $key => $value) {
638 $headerLines[] = $key . ': ' . $value;
641 $success = $platform->fwrite($handle, sprintf("%s %s HTTP/1.0\r\n%s\r\n\r\n%s",
644 implode("\r\n", $headerLines),
647 /* Zero bytes written or false was returned */
649 "fwrite failed in requestWebPage($url)" . ($success === false ? ' - false' : ''));
650 return array(null, null, null);
652 $platform->fflush($handle);
656 * Read the status line. fgets stops after newlines. The first line is the protocol
657 * version followed by a numeric status code and its associated textual phrase.
659 $responseStatus = trim($platform->fgets($handle, 4096));
660 if (empty($responseStatus)) {
661 $gallery->debug('Empty http response code, maybe timeout');
662 return array(null, null, null);
665 /* Read the headers */
666 $responseHeaders = array();
667 while (!$platform->feof($handle)) {
668 $line = trim($platform->fgets($handle, 4096));
673 /* Normalize the line endings */
674 $line = str_replace("\r", '', $line);
676 list ($key, $value) = explode(':', $line, 2);
677 if (isset($responseHeaders[$key])) {
678 if (!is_array($responseHeaders[$key])) {
679 $responseHeaders[$key] = array($responseHeaders[$key]);
681 $responseHeaders[$key][] = trim($value);
683 $responseHeaders[$key] = trim($value);
686 $platform->fclose($handle);
688 if(preg_match("/Not found/i", $responseStatus)) {
692 //print "success: $success <br>responseStatus: $responseStatus <br>responseHeaders: $responseHeaders <br>";
694 return array($success, $responseStatus, $responseHeaders);
698 * @see ItemAdd:loadTemplate
700 function loadTemplate(&$template, &$form, $item) {
703 if ($form['formName'] != 'ItemAddEmbedVideo') {
704 /* First time around, load the form with item data */
705 $form['webPage'] = '';
706 $form['formName'] = 'ItemAddEmbedVideo';
709 $session =& $gallery->getSession();
711 $template->setVariable('ItemAddEmbedVideo', $ItemAddEmbedVideo);
714 'modules/embedvideo/templates/ItemAddEmbedVideo.tpl',
715 'modules_embedvideo');
719 * @see ItemAddPlugin::getTitle
721 function getTitle() {
722 list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'embedvideo');
724 return array($ret, null);
727 return array(null, $module->translate('Embed Video'));