Added ability to check for "File not found" HTTP error when embedding link to remote...
[embedvideo/.git] / ItemAddEmbedVideo.inc
1 <?php
2 /*
3  * Gallery - a web based photo album viewer and editor
4  * Copyright (C) 2000-2007 Bharat Mediratta
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 /**
22  * This plugin will handle the addition of embedded video objects
23  * @package embedVideo
24  * @subpackage UserInterface
25  * @author Alan Pippin <apippin@pippins.net>
26  * @version $Revision: 1.1 $
27  */
28 class ItemAddEmbedVideo extends ItemAddPlugin {
29   
30   /**
31    * @see ItemAddPlugin::handleRequest
32    */
33  function handleRequest($form, &$item) {
34    global $gallery;
35     
36    $status = $error = array();
37    
38    if (isset($form['action']['addEmbedVideoPage'])) {
39      
40        $platform =& $gallery->getPlatform();
41
42        if (empty($extraHeaders)) {
43            $extraHeaders = array('Referer' => str_replace('&amp;', '&', $url));
44        }
45
46        if(isset($form['webPage']['URL'])) {
47          
48            /* Load any stored/set Parameters */
49            list ($ret, $params) =
50              GalleryCoreApi::fetchAllPluginParameters('module', 'embedvideo');
51            if ($ret) {
52                return array($ret, null, null);
53            }
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;
61                        /* print "type: $type name: $name value: $value <br>"; */
62                    }
63                }
64            }
65            
66            /* Store any Parameters into some simpler, shorter, local variables */
67            global $debugOutput, $useInternalFlvPlayer, $youtubeDevId;
68            global $width, $height, $externalFlvPlayer, $externalFlvPlayerVars;
69            global $flvThumbnail;
70
71            $debugOutput = $this->getParameter($ItemAddEmbedVideo, 'debugOutput');
72            $useInternalFlvPlayer = $this->getParameter($ItemAddEmbedVideo, 'useInternalFlvPlayer');
73            $youtubeDevId = $this->getParameter($ItemAddEmbedVideo, 'youtubeDevId');
74            $width = $this->getParameter($ItemAddEmbedVideo, 'width');
75            $height = $this->getParameter($ItemAddEmbedVideo, 'height');
76            $externalFlvPlayer = $this->getParameter($ItemAddEmbedVideo, 'externalFlvPlayer');
77            $externalFlvPlayerVars = $this->getParameter($ItemAddEmbedVideo, 'externalFlvPlayerVars');
78            $flvThumbnail = $this->getParameter($ItemAddEmbedVideo, 'flvThumbnail');
79
80            /* Store other string constants we'll use later */
81            $youtubeUrlPattern="youtube.com";
82            $youtubeApiUrl="http://www.youtube.com/api2_rest";
83            $googleUrlPattern="video.google.com";
84            $googlePlayer="http://video.google.com/googleplayer.swf";
85            
86            /* Gallery2 specific paths and variables */
87            $urlGenerator =& $gallery->getUrlGenerator();
88            $gallery2_url = $urlGenerator->getCurrentUrlDir();
89            $gallery2_flv_thumbnail = "modules/thumbnail/images/G2video.jpg";
90            $gallery2_flv_player = "modules/flashvideo/lib/G2flv.swf";
91            
92            /* Store the passed URL in a shorter local variable */
93            $url = $form['webPage']['URL'];
94
95            /*
96             *****************************
97             * Embed a Youtube Video
98             *****************************
99             */
100            if(preg_match("/$youtubeUrlPattern/",$url)) {
101
102                /* Make sure we can find a video_id in the URL */
103                if(preg_match("/watch\?v=(.*)/",$url,$matches)) {
104                    $video_id = $matches[1];
105                } else {
106                    return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER,__FILE__,__LINE__,
107                                 "Unable to extract video id from url: $url"),null,null);
108                }
109                
110                /* Make sure we have a valid youtube developer id */
111                $dev_id = $youtubeDevId;
112                if(!preg_match("/\w+/",$dev_id)) {
113                    return array(GalleryCoreApi::error(ERROR_CONFIGURATION_REQUIRED,__FILE__,__LINE__,
114                                 "Invalid/missing YouTube developer ID: $dev_id"),null,null);
115                }
116                
117                /* Youtube api feed */
118                $feed = $youtubeApiUrl.'?method=youtube.videos.get_details';
119                $feed.= "&dev_id=$dev_id&video_id=$video_id";
120                
121                /* Get the youtube xml feed as a string data source */
122                list ($successfullyCopied, $xml, $response, $headers) =
123                  GalleryCoreApi::fetchWebPage($feed, $extraHeaders);
124                if (!$successfullyCopied) {
125                    return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
126                                 "Unable to get video information at url: $url - $response"),null,null);
127                }
128
129
130                if(!strcmp($debugOutput,"true")) {
131                  print "$xml";
132                }
133                
134                /* Extract certain information from the xml feed */
135                preg_match_all("/\<title\>(.+?)\<\/title\>/smi",$xml, $title);
136                preg_match_all("/\<description\>(.+?)\<\/description\>/smi",$xml, $description);
137                preg_match_all("/\<thumbnail_url\>(.+?)\<\/thumbnail_url\>/smi",$xml, $thumbnail);
138                
139                array_shift($title);
140                array_shift($thumbnail);
141                array_shift($description);
142           
143                /* Replace html characters. More can be added but this seems to work */
144                for($i=0;$i<count($description[0]);$i++){          
145                    $description[0][$i] = preg_replace("/&#60;/","<",$description[0][$i]);
146                    $description[0][$i] = html_entity_decode($description[0][$i],ENT_QUOTES);      
147                }
148
149                /* Store the information found in some local variables */
150                $title = $title[0][0];
151                $summary = $description[0][0];
152                $thumbnail = $thumbnail[0][0];
153
154                /* Format the description to hold a reference to the embedded video */
155                $description = '<object width="'.$width.'" height="'.$height.'">';
156                $description.= '<param name="movie" ';
157                $description.= 'value="http://www.youtube.com/v/'.$video_id.'"></param>';
158                $description.= '<param name="wmode" value="transparent"></param>';
159                $description.= '<embed src="http://www.youtube.com/v/'.$video_id.'" ';
160                $description.= 'type="application/x-shockwave-flash" wmode="transparent" ';
161                $description.= 'width="'.$width.'" height="'.$height.'"></embed></object>';
162                $description.= "<br>$summary";
163
164            /*
165             **********************************
166             * Embed a Google Video
167             **********************************
168             */
169            } else if(preg_match("/$googleUrlPattern/",$url)) {
170
171                /* Make sure we can extract a docID */
172                if(preg_match("/docid=(.*)/",$url,$matches)) {
173                    $doc_id = $matches[1];
174                } else {
175                    return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER,__FILE__,__LINE__,
176                                 "Unable to extract doc id from url: $url"),null,null);
177                }
178
179                /* Grab the contents of the webpage used to display the video on video.google.com */
180                list ($successfullyCopied, $contents, $response, $headers) =
181                  GalleryCoreApi::fetchWebPage($url, $extraHeaders);
182                if (!$successfullyCopied) {
183                    return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
184                                 "Unable to get video information at url: $url - $response"),NULL,NULL);
185                }
186
187                /* Extract the summary from the webpage contents */
188                preg_match('/<meta content="(.+?)\. \w+ \d+, \d+.*" name="description">/i',
189                           $contents, $matches);
190                $summary=$matches[1];
191
192                /* Extract the title from the webpage contents */
193                $title="Unknown";
194                if(preg_match('/<title>(.+?)\s+- Google Video<\/title>/i', $contents, $matches)) {
195                  $title=$matches[1];
196                } else if(preg_match('/<title>(.+?)<\/title>/i', $contents, $matches)) {
197                  $title=$matches[1];
198                }
199
200                /* Extract the thumbnail URL from the webpage contents */
201                preg_match('/<img src="(http:\/\/video\.google\.com\/ThumbnailServer2.+?)" /i',
202                           $contents, $matches);
203                $thumbnail=$matches[1];
204                $thumbnail=preg_replace("/offsetms=0/","offsetms=0",$thumbnail);
205
206                /* Format the description to hold a reference to the embedded video */
207                $description = '<embed FlashVars="autoPlay=true" ';
208                $description.= 'style="width:'.$width.'px; height:'.$height.'px;" id="VideoPlayback" ';
209                $description.= 'type="application/x-shockwave-flash" ';
210                $description.= 'src="'.$googlePlayer.'?docId='.$doc_id.'"> ';
211                $description.= '</embed>';
212                $description.= "<br>$summary";
213          
214            /*
215             **********************************
216             * Embed a remote .flv file
217             **********************************
218             */
219            } else if(preg_match("/.*\/(.+?)\.flv/i",$url,$matches)) {
220
221                /* Set the title and summary to the name of the file */
222                $title = $matches[1];
223                $summary = $matches[1];
224                
225                /*
226                 * Set the thumbnail to some generic jpg image,
227                 * since we can't extract it from the remote flv file.
228                 * If no parameter is set, set it to a default value.
229                 */
230                if(preg_match("/\w+/", $flvThumbnail)) {
231                  $thumbnail = $flvThumbnail;
232                } else {
233                  $thumbnail = $gallery2_url.$gallery2_flv_thumbnail;
234                }
235
236                /*
237                 * Check to make sure the URL to the remote flv file is valid
238                 * (That the file exists at the URL given)
239                 */
240                list ($successfullyCopied, $response, $headers) =
241                  $this->fetchWebFileHeaders($url, $extraHeaders);
242                if (!$successfullyCopied) {
243                  return array(GalleryCoreApi::error(ERROR_BAD_PATH,__FILE__,__LINE__,
244                               "Unable to find the video at url: $url - $response"),NULL,NULL);
245                }
246                
247                /*
248                 * Format the description to hold a reference to the embedded video
249                 * This reference will be embedded using the G2 internal player,
250                 * or an external player if provided by the user.
251                 */
252                if(!strcmp($useInternalFlvPlayer,"false")) {
253
254                    /*
255                     * The user has indicated they want to use an external flv player
256                     * Make sure one is defined!
257                     */
258                    if(!preg_match("/\w+/",$externalFlvPlayer)) {
259                        return array(GalleryCoreApi::error(ERROR_CONFIGURATION_REQUIRED,__FILE__,__LINE__,
260                                    "Invalid/missing external player parameter"),null,null);
261                    }
262
263                    /* Format the description to hold a reference to the embedded video */
264                    $description ='<embed src="'.$externalFlvPlayer.'" ';
265                    $description.= 'width="'.$width.'" height="'.$height.'" ';
266                    $description.= 'bgcolor="#C0C0C0" allowfullscreen="true" ';
267                    $description.= 'type="application/x-shockwave-flash" ';
268                    $description.= 'pluginspage="http://www.macromedia.com/go/getflashplayer" ';
269                    $description.= 'flashvars="file='.$url;
270                    $description.= '&fullscreenpage='.$thumbnail;
271                    $description.= '&linktarget=_Blank&image='.$thumbnail;
272
273                    if(!preg_match("/\w+/",$externalFlvPlayerVars)) {
274                        /* Format the flashvars for the internal G2 flv player */
275                        $description.= '&showdigits=true&autostart=false&showfsbutton=true&';
276                        $description.= '&repeat=false&lightcolor=0x9999FF';
277                        $description.= '&backcolor=0x888888&frontcolor=0x000000"';
278                    } else {
279                        /* Format the flashvars for the external G2 flv player */
280                        $description.= '&'.$externalFlvPlayerVars;
281                    }
282                    $description.=  ' />&nbsp;</p>';
283                
284                /* Internal FLV player */
285                } else {
286                    /* Format the description to hold a reference to the embedded video */
287                    $macromedia_url = "http://download.macromedia.com/pub/shockwave/cabs/flash/";
288                    $description = '<script type="text/javascript">'."\n";
289                    $description.= '// <![CDATA['."\n";
290                    $description.= 'function divResize(id, nw, nh) {'."\n";
291                    $description.= 'var obj = document.getElementById(id);'."\n";
292                    $description.= 'obj.style.width = nw + "px";'."\n";
293                    $description.= 'obj.style.height = nh + "px";'."\n";
294                    $description.= '}'."\n";
295                    $description.= '// ]]>'."\n";
296                    $description.= '</script>'."\n";
297                    $description.= '<div id="flashvideo" style="align:left;width:525px;height:392px">'."\n";
298                    $description.= '<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000"';
299                    $description.= 'codebase="'.$macromedia_url.'swflash.cab#version=8,0,0,0"';
300                    $description.= 'width="100%" height="100%" id="IFid1" class="ImageFrame_image">';
301                    $description.= '<param name="movie" value="'.$gallery2_url.$gallery2_flv_player.'"/>';
302                    $description.= '<param name="FlashVars" value="flvUrl='.$url;
303                    $description.= '&Width='.$width.'&Height='.$height.'&title='.$title;
304                    $description.= '&allowDl=true&thumbUrl='.$thumbnail;
305                    $description.= '&langDownload=Download&langLarge=Large&langNormal=Normal"/>';
306                    $description.= '<param name="quality" value="high"/>';
307                    $description.= '<param name="scale" value="noscale"/>';
308                    $description.= '<param name="salign" value="lt"/>';
309                    $description.= '<param name="wmode" value="transparent"/>';
310                    $description.= '<param name="allowScriptAccess" value="always"/>';
311                    $description.= '<embed src="'.$gallery2_url.$gallery2_flv_player.'" ';
312                    $description.= 'flashvars="flvUrl='.$url;
313                    $description.= '&Width='.$width.'&Height='.$height.'&title='.$title;
314                    $description.= '&allowDl=true&thumbUrl='.$thumbnail;
315                    $description.= '&langDownload=Download&langLarge=Large&langNormal=Normal" ';
316                    $description.= 'type="application/x-shockwave-flash" ';
317                    $description.= 'width="100%" height="100%" quality="high" scale="noscale" salign="lt" ';
318                    $description.= 'wmode="transparent" allowScriptAccess="always" ';
319                    $description.= 'pluginspage="http://www.macromedia.com/go/getflashplayer"/>';
320                    $description.= '</object></div>';
321                }
322                
323           /*
324            **********************************
325            * Unsupported URL to embed
326            **********************************
327            */   
328            } else {
329                return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_FILE_TYPE,__FILE__,__LINE__,
330                             "Unable to embed video from: $url"),null,null);
331            }
332            
333
334            /*
335            **********************************
336            * Add the video to Gallery
337            **********************************
338            */
339            
340            /* Get a local tmp file to save the thumbnail URL to */
341            $tmpDir = $gallery->getConfig('data.gallery.tmp');
342            $tmpFile = $platform->tempnam($tmpDir, 'add');
343            $tmpFile.= ".jpg";
344            
345            if(!strcmp($debugOutput,"true")) {
346                print "thumbnail: $thumbnail <br>";
347            }
348            
349            /* Fetch the thumbnail and save it to a local file */
350            list ($successfullyCopied, $response, $headers) =
351              GalleryCoreApi::fetchWebFile($thumbnail, $tmpFile, $extraHeaders);
352            if (!$successfullyCopied) {
353                return array(GalleryCoreApi::error(ERROR_STORAGE_FAILURE,__FILE__,__LINE__,
354                             "Unable to copy thumbnail from url: $url - $response"),null,null);
355            }
356            
357            /* Obtain the mimeType of the thumbnail */
358            list ($ret, $mimeType) = GalleryCoreApi::getMimeType($tmpFile);
359            
360            /* Set the filename for the item we want to add */
361            $fileName = $title;
362            $fileName = preg_replace("/\s+/","_",$fileName);
363            $fileName = preg_replace("/'/","",$fileName);
364            $fileName = preg_replace("/\"/","",$fileName);
365            $fileName = preg_replace("/&#\d+;/","",$fileName);
366            
367            /* General debug output */
368            if(!strcmp($debugOutput,"true")) {
369                print "<p><a href=\"".$title."\" target=\"_blank\">";
370                print "<img src=\"".$thumbnail."\">\n</a>".$summary."</p>";
371                print "<p>$description</p>";
372                print "thumbnail: $tmpFile <br>";
373                print "mimeType: $mimeType <br>";
374                print "fileName: $fileName <br>";
375                print "<br><br><b>Video Successfully Added to your Gallery Album</b><br><br>";
376            }
377
378            /* Make the gallery2 call to add this item to the album */
379            list ($ret, $newItem) = GalleryCoreApi::addItemToAlbum($tmpFile,
380                                                                   $fileName,
381                                                                   $title,
382                                                                   $summary,
383                                                                   $description,
384                                                                   $mimeType,
385                                                                   $item->getId());
386            
387            if ($ret) {
388                return array($ret, null, null);
389            }
390            
391            $status['addedFiles'][] = array('fileName' => $url,
392                                            'id' => $newItem->getId(),
393                                            'warnings' => array());
394        }
395        @$platform->unlink($tmpFile);
396    }
397    
398    return array(null, $error, $status);
399  }
400
401  /**
402   * A simple function to resolve the value of a parameter from
403   * the default or override value if it exists.
404   */
405  function getParameter($params, $name) {
406    if(isset($params['override'][$name])) {
407      return($params['override'][$name]);
408    } else {
409      return($params['default'][$name]);
410    }
411  }
412
413  /**
414   * A simple function to get the headers only (no body) for a given URL
415   *
416   */
417  function fetchWebFileHeaders($url, $requestHeaders=array()) {
418      global $gallery;
419      
420      $requestMethod='GET';
421
422      /* Convert illegal characters */
423      $url = str_replace(' ', '%20', $url);
424      
425      /* Unescape ampersands, since if the URL comes from form input it will be escaped */
426      $url = str_replace('&amp;', '&', $url);
427
428      $platform =& $gallery->getPlatform();
429      
430      $urlComponents = parse_url($url);
431      if (empty($urlComponents['port'])) {
432          $urlComponents['port'] = 80;
433      }
434      if (empty($urlComponents['path'])) {
435          $urlComponents['path'] = '/';
436      }
437
438      $handle = @$platform->fsockopen(
439                                      $urlComponents['host'], $urlComponents['port'], $errno, $errstr, 5);
440      if (empty($handle)) {
441          $gallery->debug("Error $errno: '$errstr' requesting $url");
442          return array(null, null, null);
443      }
444      
445      $requestUri = $urlComponents['path'];
446      if (!empty($urlComponents['query'])) {
447          $requestUri .= '?' . $urlComponents['query'];
448      }
449      $headerLines = array('Host: ' . $urlComponents['host']);
450      foreach ($requestHeaders as $key => $value) {
451          $headerLines[] = $key . ': ' . $value;
452      }
453      
454      $success = $platform->fwrite($handle, sprintf("%s %s HTTP/1.0\r\n%s\r\n\r\n%s",
455                                                    $requestMethod,
456                                                    $requestUri,
457                                                    implode("\r\n", $headerLines),
458                                                    $requestBody));
459      if (!$success) {
460          /* Zero bytes written or false was returned */
461          $gallery->debug(
462                          "fwrite failed in requestWebPage($url)" . ($success === false ? ' - false' : ''));
463          return array(null, null, null);
464      }
465      $platform->fflush($handle);
466      
467      
468      /*
469       * Read the status line.  fgets stops after newlines.  The first line is the protocol
470       * version followed by a numeric status code and its associated textual phrase.
471       */
472      $responseStatus = trim($platform->fgets($handle, 4096));
473      if (empty($responseStatus)) {
474          $gallery->debug('Empty http response code, maybe timeout');
475          return array(null, null, null);
476      }
477      
478      /* Read the headers */
479      $responseHeaders = array();
480      while (!$platform->feof($handle)) {
481          $line = trim($platform->fgets($handle, 4096));
482          if (empty($line)) {
483              break;
484          }
485        
486          /* Normalize the line endings */
487          $line = str_replace("\r", '', $line);
488          
489          list ($key, $value) = explode(':', $line, 2);
490          if (isset($responseHeaders[$key])) {
491              if (!is_array($responseHeaders[$key])) {
492                $responseHeaders[$key] = array($responseHeaders[$key]);
493              }
494              $responseHeaders[$key][] = trim($value);
495          } else {
496              $responseHeaders[$key] = trim($value);
497          }
498      }
499      $platform->fclose($handle);
500
501      if(preg_match("/Not found/i", $responseStatus)) {
502          $success = 0;
503      }
504
505      //print "success: $success <br>responseStatus: $responseStatus <br>responseHeaders: $responseHeaders <br>";
506      
507      return array($success, $responseStatus, $responseHeaders);
508  }
509  
510  /**
511   * @see ItemAdd:loadTemplate
512   */
513  function loadTemplate(&$template, &$form, $item) {
514     global $gallery;
515     
516     if ($form['formName'] != 'ItemAddEmbedVideo') {
517         /* First time around, load the form with item data */
518         $form['webPage'] = '';
519         $form['formName'] = 'ItemAddEmbedVideo';
520     }
521     
522     $session =& $gallery->getSession();
523     
524     $template->setVariable('ItemAddEmbedVideo', $ItemAddEmbedVideo);
525     
526     return array(null,
527                  'modules/embedvideo/templates/ItemAddEmbedVideo.tpl',
528                  'modules_embedvideo');
529  }
530  
531  /**
532   * @see ItemAddPlugin::getTitle
533   */
534  function getTitle() {
535     list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'embedvideo');
536     if ($ret) {
537         return array($ret, null);
538     }
539     
540     return array(null, $module->translate('Embed Video'));
541  }
542  
543 }       
544 ?>