source: extensions/download_by_size/action.php @ 23162

Last change on this file since 23162 was 23162, checked in by plg, 7 years ago

bug fixed: avoid "Access denied e" if HD is forbidden and XXL is as big as original.

bug fixed: in template use $derivative_type instead of $derivative->get_type() to avoid size=Original

File size: 6.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/**
25 * This file is a duplicate of core action.php, I have only change
26 * PHPWG_ROOT_PATH and added a few lines between "//---- specific download_by_size, start"
27 * and "//---- specific download_by_size, end"
28 */
29
30define('PHPWG_ROOT_PATH','../../'); // path
31session_cache_limiter('public');
32include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
33
34// Check Access and exit when user status is not ok
35check_status(ACCESS_GUEST);
36
37function guess_mime_type($ext)
38{
39  switch ( strtolower($ext) )
40  {
41    case "jpe": case "jpeg":
42    case "jpg": $ctype="image/jpeg"; break;
43    case "png": $ctype="image/png"; break;
44    case "gif": $ctype="image/gif"; break;
45    case "tiff":
46    case "tif": $ctype="image/tiff"; break;
47    case "txt": $ctype="text/plain"; break;
48    case "html":
49    case "htm": $ctype="text/html"; break;
50    case "xml": $ctype="text/xml"; break;
51    case "pdf": $ctype="application/pdf"; break;
52    case "zip": $ctype="application/zip"; break;
53    case "ogg": $ctype="application/ogg"; break;
54    default: $ctype="application/octet-stream";
55  }
56  return $ctype;
57}
58
59function do_error( $code, $str )
60{
61  set_status_header( $code );
62  echo $str ;
63  exit();
64}
65
66
67if (!isset($_GET['id'])
68    or !is_numeric($_GET['id'])
69    or !isset($_GET['part'])
70    or !in_array($_GET['part'], array('e','r') ) )
71{
72  do_error(400, 'Invalid request - id/part');
73}
74
75$query = '
76SELECT * FROM '. IMAGES_TABLE.'
77  WHERE id='.$_GET['id'].'
78;';
79
80$element_info = pwg_db_fetch_assoc(pwg_query($query));
81if ( empty($element_info) )
82{
83  do_error(404, 'Requested id not found');
84}
85
86// $filter['visible_categories'] and $filter['visible_images']
87// are not used because it's not necessary (filter <> restriction)
88$query='
89SELECT id
90  FROM '.CATEGORIES_TABLE.'
91    INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON category_id = id
92  WHERE image_id = '.$_GET['id'].'
93'.get_sql_condition_FandF(
94  array(
95      'forbidden_categories' => 'category_id',
96      'forbidden_images' => 'image_id',
97    ),
98  '    AND'
99  ).'
100  LIMIT 1
101;';
102if ( pwg_db_num_rows(pwg_query($query))<1 )
103{
104  do_error(401, 'Access denied');
105}
106
107include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
108$file='';
109switch ($_GET['part'])
110{
111  case 'e':
112    //---- specific download_by_size, start
113    if (isset($_GET['size']))
114    {
115      if (!in_array($_GET['size'], array_keys(ImageStdParams::get_defined_type_map())))
116      {
117        die('Hacking attempt: unknown size');
118      }
119     
120      $deriv = new DerivativeImage($_GET['size'], new SrcImage($element_info));
121      if (!file_exists($deriv->get_path()))
122      {
123        include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
124       
125        set_make_full_url();
126        fetchRemote($deriv->get_url().'&ajaxload=true', $dest);
127        unset_make_full_url();
128      }
129      $file = $deriv->get_path();
130    }
131    else
132    {
133    //---- specific download_by_size, end
134    if ( !$user['enabled_high'] )
135    {
136      $deriv = new DerivativeImage(IMG_XXLARGE, new SrcImage($element_info));
137      if ( !$deriv->same_as_source() )
138      {
139        do_error(401, 'Access denied e');
140      }
141    }
142    $file = get_element_path($element_info);
143    //---- specific download_by_size, start
144    }
145    //---- specific download_by_size, end
146   
147    break;
148  case 'r':
149    $file = original_to_representative( get_element_path($element_info), $element_info['representative_ext'] );
150    break;
151}
152
153if ( empty($file) )
154{
155  do_error(404, 'Requested file not found');
156}
157
158if ($_GET['part'] == 'e') {
159  pwg_log($_GET['id'], 'high');
160}
161else if ($_GET['part'] == 'e')
162{
163  pwg_log($_GET['id'], 'other');
164}
165
166$http_headers = array();
167
168$ctype = null;
169if (!url_is_remote($file))
170{
171  if ( !@is_readable($file) )
172  {
173    do_error(404, "Requested file not found - $file");
174  }
175  $http_headers[] = 'Content-Length: '.@filesize($file);
176  if ( function_exists('mime_content_type') )
177  {
178    $ctype = mime_content_type($file);
179  }
180
181  $gmt_mtime = gmdate('D, d M Y H:i:s', filemtime($file)).' GMT';
182  $http_headers[] = 'Last-Modified: '.$gmt_mtime;
183
184  // following lines would indicate how the client should handle the cache
185  /* $max_age=300;
186  $http_headers[] = 'Expires: '.gmdate('D, d M Y H:i:s', time()+$max_age).' GMT';
187  // HTTP/1.1 only
188  $http_headers[] = 'Cache-Control: private, must-revalidate, max-age='.$max_age;*/
189
190  if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) )
191  {
192    set_status_header(304);
193    foreach ($http_headers as $header)
194    {
195      header( $header );
196    }
197    exit();
198  }
199}
200
201if (!isset($ctype))
202{ // give it a guess
203  $ctype = guess_mime_type( get_extension($file) );
204}
205
206$http_headers[] = 'Content-Type: '.$ctype;
207
208if (isset($_GET['download']))
209{
210  $http_headers[] = 'Content-Disposition: attachment; filename="'.$element_info['file'].'";';
211  $http_headers[] = 'Content-Transfer-Encoding: binary';
212}
213else
214{
215  $http_headers[] = 'Content-Disposition: inline; filename="'
216            .basename($file).'";';
217}
218
219foreach ($http_headers as $header)
220{
221  header( $header );
222}
223
224// Looking at the safe_mode configuration for execution time
225if (ini_get('safe_mode') == 0)
226{
227  @set_time_limit(0);
228}
229
230@readfile($file);
231
232?>
Note: See TracBrowser for help on using the repository browser.