r4283 index.php: add "image" case to module dispatcher
authorDenis Ovsienko <infrastation@yandex.ru>
Thu, 3 Mar 2011 18:41:24 +0000 (18:41 +0000)
committerDenis Ovsienko <infrastation@yandex.ru>
Thu, 3 Mar 2011 18:41:24 +0000 (18:41 +0000)
render_image.php: add function wrappings to convert to a pure library file, move to "inc" directory
renderFilePreview(): remove dead code and fix a bug with no preview output for a small image
renderRackspace(): update image URI respectively
renderRow(): idem
getProgressBar(): idem
renderCell(): idem
getFilePreviewCode(): idem

wwwroot/inc/interface.php
wwwroot/inc/render_image.php [moved from wwwroot/render_image.php with 77% similarity]
wwwroot/index.php

index 5202ae3e9935efad5c68f45fd111b33a2e5d2fd3..9b77abb4f09e6466557b96264543a5a0fefcaed5 100644 (file)
@@ -432,7 +432,7 @@ function renderRackspace ()
                                        echo "<img border=0 width=${rackwidth} height=";
                                        echo getRackImageHeight ($rack['height']);
                                        echo " title='${rack['height']} units'";
-                                       echo "src='render_image.php?img=minirack&rack_id=${rack['id']}'>";
+                                       echo "src='?module=image&img=minirack&rack_id=${rack['id']}'>";
                                        echo "<br>${rack['name']}</a></td>";
                                        $rackListIdx++;
                                }
@@ -524,7 +524,7 @@ function renderRow ($row_id)
                echo "<td align=center class=row_${order}><a href='".makeHref(array('page'=>'rack', 'rack_id'=>$rack['id']))."'>";
                echo "<img border=0 width=${rackwidth} height=" . (getRackImageHeight ($rack['height']) * getConfigVar ('ROW_SCALE'));
                echo " title='${rack['height']} units'";
-               echo "src='render_image.php?img=minirack&rack_id=${rack['id']}'>";
+               echo "src='?module=image&img=minirack&rack_id=${rack['id']}'>";
                echo "<br>${rack['name']}</a></td>";
                $order = $nextorder[$order];
                $rackListIdx++;
@@ -5366,7 +5366,7 @@ function renderProgressBar ($percentage = 0, $theme = '')
 function getProgressBar ($percentage = 0, $theme = '')
 {
        $done = ((int) ($percentage * 100));
-       $ret = "<img width=100 height=10 border=0 title='${done}%' src='render_image.php?img=progressbar&done=${done}";
+       $ret = "<img width=100 height=10 border=0 title='${done}%' src='?module=image&img=progressbar&done=${done}";
        if ($theme != '')
                $ret .= "&theme=${theme}";
        $ret .= "'>";
@@ -7085,7 +7085,7 @@ function renderCell ($cell)
                $thumbwidth = getRackImageWidth();
                $thumbheight = getRackImageHeight ($cell['height']);
                echo "<img border=0 width=${thumbwidth} height=${thumbheight} title='${cell['height']} units' ";
-               echo "src='render_image.php?img=minirack&rack_id=${cell['id']}'>";
+               echo "src='?module=image&img=minirack&rack_id=${cell['id']}'>";
                echo "</td><td>";
                printf ("<a href='index.php?page=rack&rack_id=%s'><strong>%s</strong></a>", $cell['id'], niftyString ($cell['name']));
                echo "</td></tr><tr><td>";
@@ -7182,7 +7182,7 @@ function getFilePreviewCode ($file)
                        }
                        if ($resampled)
                                $ret .= "<a href='?module=download&file_id=${file['id']}&asattach=no'>";
-                       $ret .= "<img width=${width} height=${height} src='render_image.php?img=preview&file_id=${file['id']}'>";
+                       $ret .= "<img width=${width} height=${height} src='?module=image&img=preview&file_id=${file['id']}'>";
                        if ($resampled)
                                $ret .= '</a><br>(click to zoom)';
                        break;
similarity index 77%
rename from wwwroot/render_image.php
rename to wwwroot/inc/render_image.php
index 9d14fb1bb71d899358880c7dba9c401591ff35be..186e536d5fa2c8152b30ca873cafdeab20e29739 100644 (file)
@@ -1,11 +1,16 @@
 <?php
 
 define ('CACHE_DURATION', 604800); // 7 * 24 * 3600
-if ( // 'progressbar's never change, force cache hit before loading init.php
-       isset ($_SERVER['HTTP_IF_MODIFIED_SINCE'])
-       && $_REQUEST['img'] == 'progressbar'
-)
+
+function checkIMSCondition()
 {
+       if
+       (
+               ! array_key_exists ('HTTP_IF_MODIFIED_SINCE', $_SERVER)
+               or ! array_key_exists ('img', $_REQUEST)
+               or $_REQUEST['img'] != 'progressbar'
+       )
+               return;
        $client_time = HTTPDateToUnixTime ($_SERVER['HTTP_IF_MODIFIED_SINCE']);
        if ($client_time !== FALSE && $client_time !== -1) // readable
        {
@@ -19,22 +24,20 @@ if ( // 'progressbar's never change, force cache hit before loading init.php
        }
 }
 
-ob_start();
-try {
-require 'inc/init.php';
-
-assertStringArg ('img');
-switch ($_REQUEST['img'])
+function dispatchImageRequest()
 {
+       genericAssertion ('img', 'string');
+       global $pageno, $tabno;
+       switch ($_REQUEST['img'])
+       {
        case 'minirack': // rack security context
-               assertUIntArg ('rack_id');
                $pageno = 'rack';
                $tabno = 'default';
                fixContext();
                if (!permitted())
                        renderAccessDeniedImage();
                else
-                       renderRackThumb ($_REQUEST['rack_id']);
+                       renderRackThumb (getBypassValue());
                break;
        case 'progressbar': // no security context
                assertUIntArg ('done', TRUE);
@@ -44,25 +47,17 @@ switch ($_REQUEST['img'])
                renderProgressBarImage ($_REQUEST['done']);
                break;
        case 'preview': // file security context
-               assertUIntArg ('file_id');
                $pageno = 'file';
                $tabno = 'download';
                fixContext();
                if (!permitted())
                        renderAccessDeniedImage();
                else
-                       renderFilePreview ($_REQUEST['file_id'], $_REQUEST['img']);
+                       renderFilePreview (getBypassValue());
                break;
        default:
                renderError();
-}
-
-ob_end_flush();
-}
-catch (Exception $e)
-{
-       ob_end_clean();
-       renderError();
+       }
 }
 
 //------------------------------------------------------------------------
@@ -261,60 +256,36 @@ function renderAccessDeniedImage ()
        die;
 }
 
-function renderFilePreview ($file_id = 0, $mode = 'view')
+function renderFilePreview ($file_id)
 {
-       switch ($mode)
+       if ($image = getFileCache ($file_id)) //Cache Hit
        {
-       case 'view':
-               // GFX files can become really big, if we uncompress them in memory just to
-               // provide a PNG version of a file. To keep things working, just send the
-               // contents as is for known MIME types.
-               $file = getFile ($file_id);
-               if (!in_array ($file['type'], array ('image/jpeg', 'image/png', 'image/gif')))
-               {
-                       renderError();
-                       break;
-               }
-               header("Content-type: ${file['type']}");
-               echo $file['contents'];
-               break;
-       case 'preview':
-               if ($image = getFileCache ($file_id)) //Cache Hit
-               {
-                       header("Content-type: image/jpeg"); 
-                       echo $image;
-                       break;
-               }
-               //Cache Miss
-               $file = getFile ($file_id);
-               $image = imagecreatefromstring ($file['contents']);
-               unset ($file['contents']);
-               $width = imagesx ($image);
-               $height = imagesy ($image);
-               header ('Content-type: image/jpeg');
-               if ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS'))
-               {
-                       $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
-                       $newwidth = $width * $ratio;
-                       $newheight = $height * $ratio;
-                       $resampled = imagecreatetruecolor ($newwidth, $newheight);
-                       imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
-                       imagedestroy ($image);
-                       $image = $resampled;
-
-                       //TODO: Find a better way to save the stream of the image... Output buffer seems silly.
-                       ob_start();
-                       imagejpeg ($image);
-                       commitAddFileCache ($file_id, ob_get_flush());
-                       imagedestroy ($image);
-                       unset ($file);
-                       unset ($resampled);
-               }
-               break;
-       default:
-               renderError();
-               break;
+               header("Content-type: image/jpeg"); 
+               echo $image;
+               return;
        }
+       //Cache Miss
+       $file = getFile ($file_id);
+       $image = imagecreatefromstring ($file['contents']);
+       unset ($file);
+       $width = imagesx ($image);
+       $height = imagesy ($image);
+       if ($width > getConfigVar ('PREVIEW_IMAGE_MAXPXS') or $height > getConfigVar ('PREVIEW_IMAGE_MAXPXS'))
+       {
+               $ratio = getConfigVar ('PREVIEW_IMAGE_MAXPXS') / max ($width, $height);
+               $newwidth = $width * $ratio;
+               $newheight = $height * $ratio;
+               $resampled = imagecreatetruecolor ($newwidth, $newheight);
+               imagecopyresampled ($resampled, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
+               imagedestroy ($image);
+               $image = $resampled;
+               unset ($resampled);
+       }
+       header ('Content-type: image/jpeg');
+       ob_start();
+       imagejpeg ($image);
+       imagedestroy ($image);
+       commitAddFileCache ($file_id, ob_get_flush());
 }
 
 ?>
index 1a096f1739f9b69fe03f107e2076ab2f7763d992..00aed1a559ff39571cbcdd8ae3964a2854b77d94 100644 (file)
@@ -3,14 +3,15 @@ ob_start();
 try {
 if (array_key_exists ('module', $_REQUEST))
 {
-       require_once 'inc/init.php';
        switch ($_REQUEST['module'])
        {
        case 'tsuri':
+               require_once 'inc/init.php';
                genericAssertion ('uri', 'string');
                proxyStaticURI ($_REQUEST['uri']);
                break;
        case 'download':
+               require_once 'inc/init.php';
                $pageno = 'file';
                $tabno = 'download';
                fixContext();
@@ -28,6 +29,20 @@ if (array_key_exists ('module', $_REQUEST))
                        header("Content-Disposition: attachment; filename={$file['name']}");
                echo $file['contents'];
                break;
+       case 'image':
+               require_once 'inc/render_image.php';
+               // 'progressbar's never change, attempt an IMS chortcut before loading init.php
+               checkIMSCondition();
+               require_once 'inc/init.php';
+               try
+               {
+                       dispatchImageRequest();
+               }
+               catch (Exception $e)
+               {
+                       renderError();
+               }
+               break;
        default:
                throw new InvalidRequestArgException ('module', $_REQUEST['module']);
        }