Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
resizeimg.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Resize image (icons) by imagemagick converter
8  *
9  * @author Anakeen
10  * @version $Id: resizeimg.php,v 1.10 2007/11/30 17:14:09 eric Exp $
11  * @package FDL
12  * @subpackage CORE
13  */
14 /**
15  */
16 include_once ("WHAT/Lib.Prefix.php");
17 include_once ("WHAT/Lib.Http.php");
18 include_once ("WHAT/Lib.Common.php");
19 
20 define("MAX_RESIZE_IMG_SIZE", 512); // maximum size to prevent attack
22 {
23  $source = $img;
24 
26  if ($size[0] == 'H') {
27  $size = substr($size, 1);
28  $h = "x";
29  } else {
30  $h = '';
31  }
32 
34 
35  $cmd = sprintf("convert -strip -thumbnail $h%d %s %s", $size, escapeshellarg($source) , escapeshellarg($dest));
36  system($cmd);
37  if (file_exists($dest)) return $basedest;
38  return false;
39 }
40 
41 function getVaultPauth($vid)
42 {
43 
45  $rcore = pg_connect($dbaccess);
46  if ($rcore) {
47  $result = pg_query(sprintf("select id_dir,name from vaultdiskstorage where id_file = %s", pg_escape_literal($vid)));
48  if ($result) {
49  $row = pg_fetch_assoc($result);
50  if ($row) {
51  $iddir = $row["id_dir"];
52  $name = $row["name"];
53 
54  $ext = '';
55  if (preg_match('/\.([^\.]*)$/', $name, $reg)) {
56  $ext = $reg[1];
57  }
58 
59  $result = pg_query(sprintf("SELECT l_path,id_fs from vaultdiskdirstorage where id_dir = %d", $iddir));
60  $row = pg_fetch_assoc($result);
61  $lpath = $row["l_path"];
62  $idfs = $row["id_fs"];
63  $result = pg_query(sprintf("SELECT r_path from vaultdiskfsstorage where id_fs = %d", $idfs));
64  $row = pg_fetch_assoc($result);
65  $rpath = $row["r_path"];
66 
67  $localimg = "$rpath/$lpath/$vid.$ext";
68  if (file_exists($localimg)) return $localimg;
69  }
70  }
71  }
72  return false;
73 }
74 /**
75  * Return true if access granted
76  * @param int $vid vault identifier
77  * @return bool
78  */
79 function verifyAccessByVaultId($vid)
80 {
82  $rcore = pg_connect($dbaccess);
83  if ($rcore) {
84  $result = pg_query(sprintf("select id_dir,name,public_access, id_tmp from vaultdiskstorage where id_file = %s", pg_escape_literal($vid)));
85  if ($result) {
86  $row = pg_fetch_assoc($result);
87  if ($row) {
88  $free = $row["public_access"];
89  $tmpSessId = $row["id_tmp"];
90 
91  if ($free) {
92  return true;
93  } elseif ($tmpSessId) {
94  // Verify if tmp file is produced by current user session
95  include_once ("WHAT/Class.Session.php");
96  if (isset($_COOKIE[Session::PARAMNAME]) && $tmpSessId === $_COOKIE[Session::PARAMNAME]) return true;
97  }
98  }
99  }
100  }
101  return false;
102 }
103 
104 function getVaultCacheImage($vid, $size)
105 {
106  $basedest = sprintf("/var/cache/image/%s-vid%s.png", $size, $vid);
107  return $basedest;
108 }
109 
110 $size = isset($_GET["size"]) ? $_GET["size"] : null;
111 if (!$size) {
112  if (isset($_GET["width"])) {
113  $size = $_GET["width"];
114  }
115 }
116 if (!$size) {
117  $heigth = isset($_GET["height"]) ? $_GET["height"] : null;
118  if ($heigth) {
119  $size = "H" . $heigth;
120  }
121 }
122 
123 if (!preg_match('/^H?[0-9]+(px)?$/', $size)) {
124  header('HTTP/1.0 400 Bad request');
125  print "Wrong image size";
126  exit;
127 } else {
128  if ($size[0] == 'H') {
129  $isize = intval(substr($size, 1));
131  $size = "H" . $isize;
132  } else {
133  $isize = intval($size);
135  $size = $isize;
136  }
137 }
138 $img = isset($_GET["img"]) ? $_GET["img"] : null;
139 if (!$img) {
140  $vid = isset($_GET["vid"]) ? $_GET["vid"] : null;
141  if (ctype_digit($vid)) $img = "vaultid=$vid";
142 }
144 $dir = dirname($_SERVER["SCRIPT_NAME"]);
146 if (preg_match("/vaultid=([0-9]+)/", $img, $vids)) {
147  // vault file
148  $vid = $vids[1];
149 
150  if (!verifyAccessByVaultId($vid)) {
151  header('HTTP/1.0 404 Not found');
152  exit;
153  }
154 
157  if (file_exists($dest)) {
158  $location = $ldir . "/" . $basedest;
159  } else {
160  $localimage = getVaultPauth($vid);
161  if ($localimage) {
162  $newimg = rezizelocalimage($localimage, $size, $basedest);
163  if ($newimg) $location = "$ldir/$newimg";
164  } else {
165  header('HTTP/1.0 404 Not found');
166  exit;
167  }
168  }
169 } else {
170  // local file
171  $turl = (parse_url($img));
172  $path = $turl["path"];
173  if ($path[0] == '/') {
174  $path = substr($path, 1);
175  }
176  $realfile = realpath($path);
177  if (!$realfile) {
178  // try without directory in case of sub http directory
179  $turl = parse_url($_SERVER["REQUEST_URI"]);
180  $directory = dirname($turl["path"]);
181 
182  if (strlen($directory) > 1) {
183  if ('/' . substr($path, 0, strlen($directory) - 1) === $directory) {
184  $img = substr($path, strlen($directory));
185  $path = $img;
186  $realfile = realpath($path);
187  if (!$realfile) {
188  header('HTTP/1.0 404 Not found');
189  }
190  }
191  } else {
192  header('HTTP/1.0 404 Not found');
193  exit;
194  }
195  }
196  $itselfName = $_SERVER["SCRIPT_FILENAME"];
197  $itselfdir = realpath(dirname($itselfName));
198  //printf("\n[%s] [%s]\n", $itselfdir, substr(dirname($realfile), 0,strlen($itselfdir)));
199  if (substr(dirname($realfile) , 0, strlen($itselfdir)) != $itselfdir) {
200  if (!is_link($path)) {
201  header('HTTP/1.0 403 Forbidden');
202  exit;
203  }
204  }
205 
206  if (strtok(substr($realfile, strlen($itselfdir)) , '/') == "var") {
207  header('HTTP/1.0 403 Forbidden');
208  exit;
209  }
210 
211  $cmd = sprintf('file -ib %s', escapeshellarg($realfile));
212 
213  $tsize = getimagesize($realfile);
214  if (!$tsize) {
215  header('HTTP/1.0 403 Forbidden');
216  exit;
217  }
218 
219  if (preg_match('%[0-9]+/[0-9]+\.[a-z]+$%', $realfile)) {
220  header('HTTP/1.0 403 Forbidden');
221  exit;
222  }
223 
224  if (strstr($path, $dir) == $path) {
225  $localimage = substr($path, strlen($dir));
226  } else {
227  $localimage = $img;
228  }
229 
230  $basedest = sprintf("/var/cache/image/%s-%s.png", $size, basename(str_replace("/", "_", $localimage)));
232 
233  if (file_exists($dest) && filemtime($dest) >= filemtime(DEFAULT_PUBDIR . "/$localimage")) {
234  $location = "$ldir/$basedest";
235  } else {
236  $newimg = rezizelocalimage(DEFAULT_PUBDIR . "/$localimage", $size, $basedest);
237  if ($newimg) $location = "$ldir/$newimg";
238  }
239 }
240 //print("<hr>Location: [$dest][$dir]/[$path][$location]<br/>");exit;
241 if ($location) $location = "/" . ltrim($location, "/");
242 else $location = $img;
243 Http_DownloadFile($location, basename($location) , "image/png", true, true);
244 // if here file has not be sent
245 header('HTTP/1.0 404 Not found');
$dest
Definition: resizeimg.php:231
if(!$realfile) $itselfName
Definition: resizeimg.php:196
getVaultCacheImage($vid, $size)
Definition: resizeimg.php:104
$ldir
Definition: resizeimg.php:145
getVaultPauth($vid)
Definition: resizeimg.php:41
$size
Definition: resizeimg.php:110
verifyAccessByVaultId($vid)
Definition: resizeimg.php:79
if($path[0]== '/') $realfile
Definition: resizeimg.php:176
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
$tsize
Definition: resizeimg.php:213
global $_GET
Definition: wsh.php:37
if(!$img) $location
Definition: resizeimg.php:143
$path
Definition: resizeimg.php:172
print
Definition: checklist.php:49
global $_SERVER
if(substr(dirname($realfile), 0, strlen($itselfdir))!=$itselfdir) if(strtok(substr($realfile, strlen($itselfdir)), '/')=="var") $cmd
Definition: resizeimg.php:211
getDbAccess()
Definition: Lib.Common.php:368
switch($command) exit
Definition: checkVault.php:46
$dir
Definition: resizeimg.php:144
Http_DownloadFile($filename, $name, $mime_type= '', $inline=false, $cache=true, $deleteafter=false)
Definition: Lib.Http.php:225
$dbaccess
Definition: checkVault.php:17
rezizelocalimage($img, $size, $basedest)
Definition: resizeimg.php:21
const PARAMNAME
$itselfdir
Definition: resizeimg.php:197
$isize
const MAX_RESIZE_IMG_SIZE
Definition: resizeimg.php:20
$basedest
Definition: resizeimg.php:230
← centre documentaire © anakeen