plugins/gallery/fetcher.php
changeset 0 7caf561c50ee
child 9 ebd7003e73c6
equal deleted inserted replaced
-1:000000000000 0:7caf561c50ee
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Snapr
       
     5  * Version 0.1 beta 1
       
     6  * Copyright (C) 2007 Dan Fuhry
       
     7  *
       
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    13  */
       
    14 
       
    15 ##
       
    16 ## IMAGE FILE FETCHER
       
    17 ##
       
    18 
       
    19 $plugins->attachHook('base_classes_initted', '
       
    20   global $paths;
       
    21     $paths->add_page(Array(
       
    22       \'name\'=>\'Image fetcher pagelet\',
       
    23       \'urlname\'=>\'GalleryFetcher\',
       
    24       \'namespace\'=>\'Special\',
       
    25       \'special\'=>0,\'visible\'=>0,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
       
    26       ));
       
    27   ');
       
    28 
       
    29 function page_Special_GalleryFetcher()
       
    30 {
       
    31   global $db, $session, $paths, $template, $plugins; // Common objects
       
    32   
       
    33   $type = $paths->getParam(0);
       
    34   if ( !in_array($type, array('thumb', 'preview', 'full')) )
       
    35   {
       
    36     die('Hack attempt');
       
    37   }
       
    38   
       
    39   $id = intval($paths->getParam(1));
       
    40   if ( !$id )
       
    41   {
       
    42     die('Hack attempt');
       
    43   }
       
    44   
       
    45   // Permissions object
       
    46   $perms = $session->fetch_page_acl($id, 'Gallery');
       
    47   
       
    48   if ( !$perms->get_permissions('gal_full_res') && $type == 'full' )
       
    49   {
       
    50     $type = 'preview';
       
    51   }
       
    52   
       
    53   $q = $db->sql_query('SELECT img_filename, img_time_mod, is_folder FROM '.table_prefix.'gallery WHERE img_id=' . $id . ';');
       
    54   if ( !$q )
       
    55     $db->_die();
       
    56   
       
    57   if ( $db->numrows() < 1 )
       
    58     die('Image not found');
       
    59   
       
    60   $row = $db->fetchrow();
       
    61   
       
    62   switch ( $type )
       
    63   {
       
    64     case 'thumb':
       
    65       $filename = ENANO_ROOT . '/cache/' . $row['img_filename'] . '-thumb.jpg';
       
    66       $mimetype = 'image/jpeg';
       
    67       break;
       
    68     case 'preview':
       
    69       $filename = ENANO_ROOT . '/cache/' . $row['img_filename'] . '-preview.jpg';
       
    70       $mimetype = 'image/jpeg';
       
    71       break;
       
    72     case 'full':
       
    73       $filename = ENANO_ROOT . '/files/' . $row['img_filename'];
       
    74       $ext = get_file_extension($filename);
       
    75       switch($ext)
       
    76       {
       
    77         case 'png': $mimetype = 'image/png'; break;
       
    78         case 'gif': $mimetype = 'image/gif'; break;
       
    79         case 'bmp': $mimetype = 'image/bmp'; break;
       
    80         case 'jpg': case 'jpeg': $mimetype = 'image/jpeg'; break;
       
    81         case 'tif': case 'tiff': $mimetype = 'image/tiff'; break;
       
    82         default: $mimetype = 'application/octet-stream';
       
    83       }
       
    84       break;
       
    85     default:
       
    86       die('PHP...insane...');
       
    87       break;
       
    88   }
       
    89   
       
    90   // Make sure we have permission to read this image
       
    91   if ( !$perms->get_permissions('read') )
       
    92   {
       
    93     $filename = ENANO_ROOT . '/plugins/gallery/denied.png';
       
    94     $mimetype = 'image/png';
       
    95   }
       
    96   
       
    97   if ( $row['is_folder'] == '1' )
       
    98   {
       
    99     $filename = ENANO_ROOT . '/plugins/gallery/folder.png';
       
   100     $mimetype = 'image/png';
       
   101   }
       
   102   
       
   103   if ( !file_exists($filename) )
       
   104     die('Can\'t retrieve image file ' . $filename);
       
   105   
       
   106   $contents = file_get_contents($filename);
       
   107   
       
   108   header('Content-type: '   . $mimetype);
       
   109   header('Content-length: ' . strlen($contents));
       
   110   header('Last-Modified: '  . date('r', $row['img_time_mod']));
       
   111   
       
   112   echo $contents;
       
   113   
       
   114 }
       
   115 
       
   116 ?>