home *** CD-ROM | disk | FTP | other *** search
/ PCNet 2006 April / PCnet 2006-06.4.iso / shareware / nmsetup.exe / WebServer / web / download.php < prev    next >
Encoding:
PHP Script  |  2006-05-01  |  11.1 KB  |  322 lines

  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // <!--Copyright (c) 2005 Pure Networks Inc.  All rights reserved.-->
  4. ////////////////////////////////////////////////////////////////////////////////
  5. //
  6. // Build: 3.0.6121.0 (Stable)
  7. // $Revision: #3 $
  8. //
  9. $sNavLocation = "download";
  10. ob_start();
  11. require "_session_common.php";
  12. ob_get_clean();
  13. //require "styles/" . $sStyleFamily . "/default.inc";
  14. require "styles/" . $sStyleFamily . "/" . $sStyleName . "/colors.php";
  15. $errorOccurred = 0;
  16. $errorText = "";
  17. $sShare = "";
  18. $sPath = "";
  19. $sNetworkName = "";
  20. ///////////////////////////////////////////////////
  21. // Make sure that we have a valid share passed in.
  22. ///////////////////////////////////////////////////
  23. if (!isset($_GET['share']))
  24. {
  25.     $errorOccurred = 200;
  26.     log_activity("QS check", "failure", return_error_text(200, "", $arErrors));
  27. }
  28. else
  29. {
  30.     $sShare = $_GET['share'];
  31.     log_activity("download share", "info", $sShare);
  32. }
  33. ///////////////////////////////////////////////////
  34. // Make sure that we have a valid path passed in.
  35. ///////////////////////////////////////////////////
  36. if ($errorOccurred == 0)
  37. {
  38.     if (!isset($_GET['path']))
  39.     {
  40.         $errorOccurred = 201;
  41.         log_activity("QS check", "failure", return_error_text(201, "", $arErrors));
  42.     }
  43.     else
  44.     {   
  45.         $sPath = urlDecodeString($_GET['path']);
  46.         log_activity("download path", "info", $sPath);
  47.     }
  48. }
  49. ///////////////////////////////////////////////////
  50. // Make sure we have a valid filename passed in.
  51. ///////////////////////////////////////////////////
  52. if ($errorOccurred == 0)
  53. {
  54.     if (!isset($_GET['fileName']))
  55.     {
  56.         $errorOccurred = 204;
  57.         log_activity("QS check", "failure", return_error_text(204, "", $arErrors));
  58.     }
  59.     else
  60.     {
  61.         $sFile = urlDecodeString($_GET['fileName']);
  62.         $sPath .= "\\" . $sFile;
  63.         log_activity("download filename", "info", $sPath);
  64.     }
  65. }
  66. ///////////////////////////////////////////////////
  67. // Are we going to force download of the file?
  68. ///////////////////////////////////////////////////
  69. switch ($_GET['action'])
  70. {
  71.     case "open":
  72.     case "save":
  73.     case "slideshow":
  74.         ini_set ( "max_execution_time", "1800" );
  75.         $sAction = $_GET['action'];
  76.         break;
  77.     case "thumbnail":
  78.         ini_set ( "max_execution_time", "10" );
  79.         $sAction = "systemcreatedimage";
  80.         $iImageHeight   = $iThumbnailHeight;
  81.         $iImageWidth    = $iThumbnailWidth;
  82.         break;
  83.     case "sharethumbnail":
  84.         $sAction = $_GET['action'];
  85.         $iImageHeight   = $iThumbnailHeight;
  86.         $iImageWidth    = $iThumbnailWidth;
  87.         break;
  88.     case "slideimage":
  89.         $sAction = "systemcreatedimage";
  90.         $iImageHeight   = $iSlideshowHeight;
  91.         $iImageWidth    = $iSlideshowWidth;
  92.         break;
  93.     case "detailsview":    
  94.         $sAction = "systemcreatedimage";
  95.         $iImageHeight   = $iMidSizeHeight;
  96.         $iImageWidth    = $iMidSizeWidth;
  97.         break;
  98.     default:
  99.         ini_set ( "max_execution_time", "1800" );
  100.         $sAction = "open";
  101. }
  102. ///////////////////////////////////////////////////
  103. // Force files with specific extensions to behave
  104. // specific ways regardless of requested action
  105. ///////////////////////////////////////////////////
  106. $fileExtension = strtolower(strrchr($sFile, "."));
  107. switch ($fileExtension)
  108. {
  109.     case ".exe":
  110.         if ($sAction != "systemcreatedimage")
  111.         {
  112.             $sAction = "save";
  113.         }
  114.         break;
  115. }
  116. ///////////////////////////////////////////////////
  117. // Let's try to see if we can open the share
  118. ///////////////////////////////////////////////////
  119. try
  120. {
  121.     $nmSharedPlace = $nmNetworkLib->OpenShare($sShare);
  122. }
  123. catch(Exception $ex)
  124. {
  125.     log_activity("Attempting nmNetworkLib->OpenShare($sShare)", "exception", $ex->getMessage());
  126.     gotoAbs('/error/305/return');
  127.     exit();
  128. }
  129. ///////////////////////////////////////////////////
  130. // double check the access level and react
  131. ///////////////////////////////////////////////////
  132. try
  133. {
  134.     $sAccessLevel = $nmSharedPlace->AccessLevel;
  135. }
  136. catch(Exception $ex)
  137. {
  138.     log_activity("Attempting nmSharedPlace->AccessLevel", "exception", $ex->getMessage());
  139.     gotoAbs('/error/305/return');
  140.     exit();
  141. }
  142. switch ($sAccessLevel)
  143. {
  144.     case 0:
  145.         ///////////////////////////////////////////////////
  146.         // Public share, show away.
  147.         ///////////////////////////////////////////////////
  148.         break;
  149.     case 1:
  150.         if (!$bLoggedIn)
  151.         {
  152.             ///////////////////////////////////////////////////
  153.             // Private share and user is not logged in, go away.
  154.             ///////////////////////////////////////////////////
  155.             log_activity("private folder request without session", "error", return_error_text(308, "", $arErrors));
  156.             gotoAbs('/login/308');
  157.             exit();
  158.         }
  159.         break;
  160.     case 2:
  161.         ///////////////////////////////////////////////////
  162.         // Restricted share, go away.
  163.         ///////////////////////////////////////////////////
  164.         log_activity("restricted share request", "error", return_error_text(310, "", $arErrors));
  165.         gotoAbs('/login/310');
  166.         exit();
  167.         break;
  168.     default:
  169.         log_activity("Undefined share access level", "error", return_error_text(311, "", $arErrors));
  170.         gotoAbs('/login/311');
  171.         exit();
  172. }
  173. ///////////////////////////////////////////////////
  174. // Make sure the file exists
  175. ///////////////////////////////////////////////////
  176. if (!file_exists($sPath))
  177. {
  178.     $errorOccurred = 342;
  179.     log_activity("download:" . $sPath, "failure", return_error_text(342, "", $arErrors));
  180. }
  181. if ($errorOccurred == 0)
  182. {
  183.     if ($nmSharedPlace->GetFileTypeDescription($sPath) <> "Internet Shortcut" ||  $sAction == "systemcreatedimage")
  184.     {
  185.         ///////////////////////////////////////////////////
  186.         // We have a non shortcut file, let's start getting the headers ready
  187.         ///////////////////////////////////////////////////
  188.         if ($sAction == "open" || $sAction == "systemcreatedimage")
  189.         {
  190.             if ($sAction == "systemcreatedimage")
  191.             {
  192.                 ///////////////////////////////////////////////////
  193.                 // get the path to the thumbnail and use it to deliver image
  194.                 ///////////////////////////////////////////////////
  195.                 try
  196.                 {
  197.                     $imgLocation = $nmRaManager->GetThumbnailForFile($sPath, $iImageWidth, 
  198.                                                                      $iImageHeight, $sContentBackground );
  199.                 }
  200.                 catch(com_exception $ex)
  201.                 {
  202.                     // An error occurred while attempting to get the image, display bad image pic
  203.                     $imgLocation    = "styles/" . $sStyleFamily . "/" . $sStyleName . "/image.broken.gif";
  204.                     $imgMode        = "Bad";
  205.                 }
  206.                 $sPath = $imgLocation;
  207.             }
  208.             header("Content-Disposition: inline; filename=\"" . basename($sPath) . "\"");
  209.             try 
  210.             {
  211.                 $contentType = $nmSharedPlace->GetContentType(basename($sPath));
  212.             }
  213.             catch(com_exception $ex)
  214.             {
  215.                 // An error occurred while attempting to get the content-type.  Assume octet-stream as
  216.                 // unknown type 
  217.                 $contentType    = "application/octet-stream";
  218.             }
  219.             header("Content-Type: " . $contentType);
  220.         }
  221.         else
  222.         {
  223.             if ($sAction == "sharethumbnail")
  224.             {
  225.                 header("Content-Disposition: inline; filename=\"" . basename($sPath) . "\"");
  226.                 header("Content-Type: " . $nmSharedPlace->GetContentType(basename($sPath)));
  227.             }
  228.             else
  229.             {
  230.                 header("Content-Disposition: attachment; filename=\"" . basename($sPath) . "\"");
  231.                 header("Content-Type: application/octet-stream");
  232.             }
  233.         }
  234.         $sFilesize = 0;
  235.         if (!filesize($sPath))
  236.         {
  237.             if (!file_exists($sPath))
  238.             {
  239.                 $errorOccurred = 341;
  240.             }
  241.         }
  242.         else
  243.         {
  244.             $sFilesize = filesize($sPath);
  245.             header("Content-Length:" . $sFilesize);
  246.         }
  247.         if ($sFilesize != 0)
  248.         {
  249.             ///////////////////////////////////////////////////
  250.             // read file from filesystem
  251.             ///////////////////////////////////////////////////
  252.             $hFileHandle = fopen($sPath, 'rb');
  253.             $iChunkSize = 1*(1024*1024);
  254.             session_write_close();
  255.             if ($hFileHandle === false) 
  256.             {
  257.                 $errorOccurred = 342;
  258.                 log_activity("get image", "error", return_error_text(342, "", $arErrors));
  259.                 ///////////////////////////////////////////////////
  260.                 // something's wrong with this file, it likely 
  261.                 // doesn't exist, let's blow away the folder's 
  262.                 // session vars so the cache won't show it.
  263.                 ///////////////////////////////////////////////////
  264.                 unset($_SESSION['currentFolder']);
  265.                 unset($_SESSION['currentFolderInfo']);
  266.                 unset($_SESSION['currentFolderFiles']);
  267.             }
  268.             if (fread($hFileHandle, 1024) === false)
  269.             {
  270.                 $errorOccurred = 343;
  271.             }
  272.             else
  273.             {
  274.                 rewind($hFileHandle);
  275.                 while (!feof($hFileHandle) && connection_status()==0) 
  276.                 {
  277.                     $buffer = fread($hFileHandle, $iChunkSize);
  278.                     print $buffer;
  279.                 }
  280.             }
  281.             // we either succeeded or failed, either way, we no longer need teh file handle open
  282.             fclose($hFileHandle); 
  283.         }
  284.     }
  285.     else
  286.     {
  287.         ///////////////////////////////////////////////////
  288.         // We have an Internet Shortcut, let's open up
  289.         // a new window to the URL saved in the file
  290.         ///////////////////////////////////////////////////
  291.         $sFileContents = file_get_contents($sPath);
  292.         $arFileContents = explode("\r",$sFileContents);
  293.         foreach ($arFileContents as $line)
  294.         {
  295.             if (strpos($line, "URL=http"))
  296.             {
  297.                 // we've got the URL!! Let's go....
  298.                 header("location: " . substr(strrchr($line,"URL=http"),4) );
  299.             }
  300.         }
  301.         echo $arFileContents[1];
  302.     }
  303.  
  304.     if ($errorOccurred == 0)
  305.     {
  306.         log_activity("download", "success", "");
  307.         exit;
  308.     }
  309.     else
  310.     {
  311.         log_activity("download", "failure", return_error_text($errorOccurred, "", $arErrors));
  312.         gotoAbs('/error/' . $errorOccurred . '/return');
  313.         exit();
  314.     }
  315. }
  316. else
  317. {
  318.     log_activity("download", "failure", return_error_text($errorOccurred, "", $arErrors));
  319.     gotoAbs('/error/' . $errorOccurred . '/return');
  320.     exit();
  321. }
  322. ?>