<?php

$time = time();

//Initialize values
$video = "";
$from = "";
$format = "";
$type = "";
$bandwidth = "";
$size = "";

//Get the video name
$video = $_GET['video'];
$from = $_GET['from'];
$format = $_GET['format'];       // real, mov, wmv
$type = $_GET['type'];           // streaming, download
$bandwidth = $_GET['bandwidth']; // 56, 256, 768 (only for streaming)
$size = $_GET['size'];           // medium, full (only for download)

// I think this is wrong and not used anymore
//$video_downloadpath = "http://www.media.mit.edu/speech/images/movies/";
//$video_download = $video_downloadpath . $video;


//If the popup window is called from main web page, let's load the cookie.
if (($from == "main") OR ($from == "popup_newprefs")){

    //Check if video_prefs cookie is set, or has to be dis-regarded
    if (!isset($_COOKIE['video_prefs']) OR ($from == "popup_newprefs")) {

       //No cookie yet (or we should ignore it), so we have to ask for the preferences first:

       //Get all the file sizes
       $mov_medium = "http://www.media.mit.edu/speech/videos/" . $video . "_medium.mov";
       //$mov_medium_size = round(remote_file_size ($mov_medium)/1000) . "kb";
       $mov_medium_size = remote_file_size ($mov_medium);
       $mov_full   = "http://www.media.mit.edu/speech/videos/" . $video . "_full.mov";
       $mov_full_size = remote_file_size ($mov_full);
       $wmv_medium = "http://www.media.mit.edu/speech/videos/" . $video . "_medium.wmv";
       $wmv_medium_size = remote_file_size ($wmv_medium);
       $wmv_full   = "http://www.media.mit.edu/speech/videos/" . $video . "_full.wmv";
       $wmv_full_size = remote_file_size ($wmv_full);

       echo "
       <HTML>
          <HEAD>
             <TITLE>Video {$video}: format selection</TITLE>
             <STYLE TYPE='text/css'>
               <!--
                 TABLE, BODY, P, TD, TH, DIR, A:ACTIVE, A:LINK, A:VISITED { font-family:Arial, Helvetica, sans-serif; }
               -->
             </STYLE>
          </HEAD>
          <BODY>

          <center>
          <font size=4>\"{$video}\"<br><br>
          <font size=2>Please choose format:<br><br>
<!--


          <table border=0 cellpadding=5 cellspacing=5 width=330>
            <tr>
              <td colspan=4 bgcolor=999999><font size=2><b>Streaming</b>
            <tr>
              <td bgcolor=cccccc><img src='images/real_logo_small.gif' width=20 align=middle><font size=2> RealVideo
              <td bgcolor=cccccc><font size=2>[<a href='popup_video.php?video={$video}&from=popup&type=streaming&format=real&bandwidth=56'>56kbps</a>]
              <td bgcolor=cccccc><font size=2>[<a href='popup_video.php?video={$video}&from=popup&type=streaming&format=real&bandwidth=256'>256kbps</a>]
              <td bgcolor=cccccc><font size=2>[<a href='popup_video.php?video={$video}&from=popup&type=streaming&format=real&bandwidth=768'>768kbps</a>]
          </table>

-->

          <table border=0 cellpadding=5 cellspacing=5 width=330>
            <tr>
              <td colspan=3 bgcolor=999999><font size=2><b>Download</b>
            <tr>
              <td bgcolor=cccccc><img src='images/quicktime_logo_small.gif' width=20 align=middle><font size=2>&nbsp;Quicktime&nbsp;MOV
              <td bgcolor=cccccc align=center><font size=2>[<a href='popup_video.php?video={$video}&from=popup&type=download&format=mov&size=medium'>medium</a>]<br>$mov_medium_size
              <td bgcolor=cccccc align=center><font size=2>[<a href='popup_video.php?video={$video}&from=popup&type=download&format=mov&size=full'>full</a>]<br>$mov_full_size
            <tr>
              <td bgcolor=cccccc><img src='images/win_logo_small.gif' width=20 align=middle><font size=2>&nbsp;WinMedia&nbsp;WMV
              <td bgcolor=cccccc align=center><font size=2>[<a href='popup_video.php?video={$video}&from=popup&type=download&format=wmv&size=medium'>medium</a>]<br>$wmv_medium_size
              <td bgcolor=cccccc align=center><font size=2>[<a href='popup_video.php?video={$video}&from=popup&type=download&format=wmv&size=full'>full</a>]<br>$wmv_full_size

              ";

    }else{
       //Yes, there was a cookie. Let's load it.

       echo "
       <HTML>
          <HEAD>
             <TITLE>Playing {$video}</TITLE>
             <STYLE TYPE='text/css'>
               <!--
                 TABLE, BODY, P, TD, TH, DIR, A:ACTIVE, A:LINK, A:VISITED { font-family:Arial, Helvetica, sans-serif; }
               -->
             </STYLE>
          </HEAD>
          <BODY>";
       //echo "<h3>Found cookie!</h3>";

       $cookie_info = explode("-", $_COOKIE['video_prefs']);  //Extract the Data
       $format    = $cookie_info[0];
       $type      = $cookie_info[1];
       $bandwidth = $cookie_info[2];
       $size      = $cookie_info[3];

       show_video($video,$format,$type,$bandwidth,$size);
    }
}

if ($from == "popup") {

  //This Popup window was called from itself: use the GET data instead,
  //and write this to a cookie!

  $cookie_data = $format . "-" . $type . "-" . $bandwidth . "-" . $size;
  if (setcookie("video_prefs",$cookie_data, $time+(60*60*24*7))) {
     $success = true;
  }else{
     $success = false;
  }

  echo "
  <HTML>
     <HEAD>
        <TITLE>Noted your preference; now playing {$video}</TITLE>
             <STYLE TYPE='text/css'>
               <!--
                 TABLE, BODY, P, TD, TH, DIR, A:ACTIVE, A:LINK, A:VISITED { font-family:Arial, Helvetica, sans-serif; }
               -->
             </STYLE>
     </HEAD>
     <BODY>";
     //echo "<h3>Popup called from itself!</h3>";

  if ($success) {
     //echo "<h3>Cookie SET: $cookie_data</h3>";
  }else{
     //echo "<h3>ERROR setting Cookie: $cookie_data</h3>";
  }

  show_video($video,$format,$type,$bandwidth,$size);

}

function show_video($video,$format,$type,$bandwidth,$size) {

  //echo "<p>Playing video: {$video}, format: {$format}, type: {$type}, bandwidth: {$bandwidth}, size: {$size}";
  echo "<center>";

  if ($type == 'streaming') {

  	$settings = "Streaming ";

  	//$video_URL = $video . "_edited" . "_" . $bandwidth . "k";
  	$video_URL = $video . "_" . $bandwidth . "k";

        switch ($format) {
          case "real":
             $video_URL = "http://helix.media.mit.edu/ramgen/public/speech/" . $video_URL . ".rm";
             echo "<p><EMBED SRC=$video_URL type='audio/x-pn-realaudio-plugin' controls='ImageWindow' width='320' height='240' nojava='true' console='video' LOOP='TRUE' AUTOSTART='TRUE'>";
             echo "<p><EMBED SRC=$video_URL type='audio/x-pn-realaudio-plugin' controls='ControlPanel' width='320' height='36' nojava='true' console='video' LOOP='TRUE' AUTOSTART='TRUE'>";
             $settings .= "Real Video ";
             break;
          case "wmv": //WMV can be also streaming, but we have no streaming server right now
             $video_URL = "mms://helix.media.mit.edu/public/speech/" . $video_URL . ".wmv";
             echo "<p><EMBED SRC=$video_URL LOOP='TRUE' AUTOSTART='TRUE'>";
             //echo "<p>URL: {$video_URL}";
             $settings .= "Windows Media ";
             break;
        }

        $settings .= "$bandwidth kbps";

  }

  if ($type == 'download') {

  	$settings = "Download ";

  	//$video_URL = $video . "_edited" . "_" . $size;
  	$video_URL = $video . "_" . $size;

        switch ($format) {
          case "mov":
             $video_URL = "http://www.media.mit.edu/speech/videos/" . $video_URL . ".mov";
             //echo "<font size=3>URL: {$video_URL} <br>";
             $file_size = remote_file_size ($video_URL);
             echo "<br><br><font size=2><b>$video:</b><br>";
             echo "<font size=3><a href=$video_URL><b>Click here to download</b></a><br>";
             echo "<font size=2><b>MOV file, $size size, $file_size</b>";
             echo "<p><font size=2>To save file locally, right mouse click, then \"Save Target As...\"";
             $settings .= "Quicktime ";
             break;
          case "wmv":
             $video_URL = "http://www.media.mit.edu/speech/videos/" . $video_URL . ".wmv";
             //echo "<font size=3>URL: {$video_URL} <br>";
             $file_size = remote_file_size ($video_URL);
             echo "<br><br><font size=2><b>$video:</b><br>";
             echo "<font size=3><a href=$video_URL><b>Click here to download</b></a><br>";
             echo "<font size=2><b>WMV file, $size size, $file_size</b>";
             echo "<p><font size=2>To save file locally, right mouse click, then \"Save Target As...\"";
             $settings .= "Windows Media ";
             break;
        }

        $settings .= "$size";

  }


  echo "<center>
        <p><hr>
        <p><font size=2>Your preferences: {$settings}<br>
           <font size=2><a href='popup_video.php?video={$video}&from=popup_newprefs'>Modify your preferences</a>";

} // end function show_video


function remote_file_size ($url)
{
   $head = "";
   $url_p = parse_url($url);
   $host = $url_p["host"];
   $path = $url_p["path"];

   $fp = fsockopen($host, 80, $errno, $errstr, 20);
   if(!$fp)
   { return false; }
   else
   {
       fputs($fp, "HEAD ".$url." HTTP/1.1\r\n");
       fputs($fp, "HOST: dummy\r\n");
       fputs($fp, "Connection: close\r\n\r\n");
       $headers = "";
       while (!feof($fp)) {
           $headers .= fgets ($fp, 128);
       }
   }
   fclose ($fp);
   $return = false;
   $arr_headers = explode("\n", $headers);
   foreach($arr_headers as $header) {
       $s = "Content-Length: ";
       if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {
           $return = substr($header, strlen($s));
           break;
       }
   }

   if ($return >= 1000000000) { //GB
       $return = round($return/1000000000 , 1) . "GB";
   }else{
      if ($return >= 1000000) { //MB
          $return = round($return/1000000 , 1) . "MB";
      }else{
         if ($return >= 1000) { //KB
             $return = round($return/1000 , 1) . "KB";
         }else{ // Less than 1KB
             $return = $return . "Bytes";
         }
      }
   }

   return $return;
}

?>

</BODY>
</HTML>