1 | <?php |
---|
2 | defined('PHPWG_ROOT_PATH') || die('hacking'); |
---|
3 | |
---|
4 | // fast bootstrap - no db connection |
---|
5 | $prefixeTable = 'toto'; |
---|
6 | include(PHPWG_ROOT_PATH . 'include/config_default.inc.php'); |
---|
7 | @include(PHPWG_ROOT_PATH. 'local/config/config.inc.php'); |
---|
8 | |
---|
9 | defined('PWG_LOCAL_DIR') or define('PWG_LOCAL_DIR', 'local/'); |
---|
10 | defined('PWG_DERIVATIVE_DIR') or define('PWG_DERIVATIVE_DIR', PWG_LOCAL_DIR.'i/'); |
---|
11 | |
---|
12 | function trigger_action() {} |
---|
13 | function get_extension( $filename ) |
---|
14 | { |
---|
15 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
16 | } |
---|
17 | |
---|
18 | function mkgetdir($dir) |
---|
19 | { |
---|
20 | if ( !is_dir($dir) ) |
---|
21 | { |
---|
22 | if (substr(PHP_OS, 0, 3) == 'WIN') |
---|
23 | { |
---|
24 | $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir); |
---|
25 | } |
---|
26 | $umask = umask(0); |
---|
27 | $mkd = @mkdir($dir, 0755, true); |
---|
28 | umask($umask); |
---|
29 | if ($mkd==false) |
---|
30 | { |
---|
31 | return false; |
---|
32 | } |
---|
33 | |
---|
34 | $file = $dir.'/index.htm'; |
---|
35 | file_exists($file) or @file_put_contents( $file, 'Not allowed!' ); |
---|
36 | } |
---|
37 | if ( !is_writable($dir) ) |
---|
38 | { |
---|
39 | return false; |
---|
40 | } |
---|
41 | return true; |
---|
42 | } |
---|
43 | |
---|
44 | // end fast bootstrap |
---|
45 | |
---|
46 | |
---|
47 | function ierror($msg, $code) |
---|
48 | { |
---|
49 | if ($code==301 || $code==302) |
---|
50 | { |
---|
51 | if (ob_get_length () !== FALSE) |
---|
52 | { |
---|
53 | ob_clean(); |
---|
54 | } |
---|
55 | // default url is on html format |
---|
56 | $url = html_entity_decode($msg); |
---|
57 | header('Request-URI: '.$url); |
---|
58 | header('Content-Location: '.$url); |
---|
59 | header('Location: '.$url); |
---|
60 | exit(); |
---|
61 | } |
---|
62 | //todo improve |
---|
63 | echo $msg; |
---|
64 | exit; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | function parse_request() |
---|
69 | { |
---|
70 | global $conf, $page; |
---|
71 | |
---|
72 | if ( $conf['question_mark_in_urls']==false and |
---|
73 | isset($_SERVER["PATH_INFO"]) and !empty($_SERVER["PATH_INFO"]) ) |
---|
74 | { |
---|
75 | $req = $_SERVER["PATH_INFO"]; |
---|
76 | $req = str_replace('//', '/', $req); |
---|
77 | $path_count = count( explode('/', $req) ); |
---|
78 | $page['root_path'] = PHPWG_ROOT_PATH.str_repeat('../', $path_count-1); |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | $req = $_SERVER["QUERY_STRING"]; |
---|
83 | /*foreach (array_keys($_GET) as $keynum => $key) |
---|
84 | { |
---|
85 | $req = $key; |
---|
86 | break; |
---|
87 | }*/ |
---|
88 | $page['root_path'] = PHPWG_ROOT_PATH; |
---|
89 | } |
---|
90 | |
---|
91 | $req = ltrim($req, '/'); |
---|
92 | !preg_match('#[^a-zA-Z0-9/_.-]#', $req) or ierror('Invalid chars in request', 400); |
---|
93 | |
---|
94 | $page['derivative_path'] = PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$req; |
---|
95 | |
---|
96 | $pos = strrpos($req, '.'); |
---|
97 | $pos!== false || ierror('Missing .', 400); |
---|
98 | $ext = substr($req, $pos); |
---|
99 | $req = substr($req, 0, $pos); |
---|
100 | |
---|
101 | $pos = strrpos($req, '-'); |
---|
102 | $pos!== false || ierror('Missing -', 400); |
---|
103 | $deriv = substr($req, $pos+1); |
---|
104 | $req = substr($req, 0, $pos); |
---|
105 | |
---|
106 | $deriv = explode('_', $deriv); |
---|
107 | foreach (ImageStdParams::get_all_types() as $type) |
---|
108 | { |
---|
109 | if (substr($type,0,2) == $deriv[0]) |
---|
110 | { |
---|
111 | $page['derivative_type'] = $type; |
---|
112 | $page['derivative_params'] = ImageStdParams::get_by_type($type); |
---|
113 | break; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | if (!isset($page['derivative_type'])) |
---|
118 | { |
---|
119 | if (substr(IMG_CUSTOM,0,2) == $deriv[0]) |
---|
120 | { |
---|
121 | $page['derivative_type'] = IMG_CUSTOM; |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | ierror('Unknown parsing type', 400); |
---|
126 | } |
---|
127 | } |
---|
128 | array_shift($deriv); |
---|
129 | |
---|
130 | $page['coi'] = ''; |
---|
131 | if (count($deriv) && $deriv[0][0]=='c' && $deriv[0][1]=='i') |
---|
132 | { |
---|
133 | $page['coi'] = substr(array_shift($deriv), 2); |
---|
134 | preg_match('#^[a-z]{4}$#', $page['coi']) or ierror('Invalid center of interest', 400); |
---|
135 | } |
---|
136 | |
---|
137 | if ($page['derivative_type'] == IMG_CUSTOM) |
---|
138 | { |
---|
139 | try |
---|
140 | { |
---|
141 | $page['derivative_params'] = ImageParams::from_url_tokens($deriv); |
---|
142 | } |
---|
143 | catch (Exception $e) |
---|
144 | { |
---|
145 | ierror($e->getMessage(), 400); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | if ($req[0]!='g' && $req[0]!='u') |
---|
150 | $req = '../'.$req; |
---|
151 | |
---|
152 | $page['src_location'] = $req.$ext; |
---|
153 | $page['src_path'] = PHPWG_ROOT_PATH.$page['src_location']; |
---|
154 | $page['src_url'] = $page['root_path'].$page['src_location']; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | $page=array(); |
---|
160 | |
---|
161 | |
---|
162 | include_once( dirname(__FILE__).'/include/derivative_params.inc.php'); |
---|
163 | include_once( dirname(__FILE__).'/include/derivative_std_params.inc.php'); |
---|
164 | ImageStdParams::load_from_file(); |
---|
165 | |
---|
166 | |
---|
167 | parse_request(); |
---|
168 | //var_export($page); |
---|
169 | |
---|
170 | $params = $page['derivative_params']; |
---|
171 | if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20) |
---|
172 | { |
---|
173 | ierror('Invalid size', 400); |
---|
174 | } |
---|
175 | if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1) |
---|
176 | { |
---|
177 | ierror('Invalid crop', 400); |
---|
178 | } |
---|
179 | |
---|
180 | include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php'); |
---|
181 | $image = new pwg_image($page['src_path']); |
---|
182 | |
---|
183 | if (!mkgetdir(dirname($page['derivative_path']))) |
---|
184 | { |
---|
185 | ierror("dir create error", 500); |
---|
186 | } |
---|
187 | |
---|
188 | $changes = 0; |
---|
189 | |
---|
190 | // todo rotate |
---|
191 | |
---|
192 | // Crop & scale |
---|
193 | $params->sizing->compute( array($image->get_width(),$image->get_height()), $page['coi'], $crop_rect, $scale_width ); |
---|
194 | if ($crop_rect) |
---|
195 | { |
---|
196 | $changes++; |
---|
197 | $image->crop( $crop_rect->width(), $crop_rect->height(), $crop_rect->l, $crop_rect->t); |
---|
198 | } |
---|
199 | |
---|
200 | if ($scale_width) |
---|
201 | { |
---|
202 | $changes++; |
---|
203 | $image->resize( $scale_width[0], $scale_width[1] ); |
---|
204 | } |
---|
205 | |
---|
206 | // no change required - redirect to source |
---|
207 | if (!$changes) |
---|
208 | { |
---|
209 | header("X-i: No change"); |
---|
210 | ierror( $page['src_url'], 301); |
---|
211 | } |
---|
212 | |
---|
213 | $image->write( $page['derivative_path'] ); |
---|
214 | $image->destroy(); |
---|
215 | |
---|
216 | $fp = fopen($page['derivative_path'], 'rb'); |
---|
217 | |
---|
218 | $fstat = fstat($fp); |
---|
219 | $gmt_mtime = gmdate('D, d M Y H:i:s', $fstat['mtime']).' GMT'; |
---|
220 | header('Last-Modified: '.$gmt_mtime); |
---|
221 | header('Content-length: '.$fstat['size']); |
---|
222 | header('Connection: close'); |
---|
223 | |
---|
224 | // todo send the right headers |
---|
225 | header("Content-Type: image/jpeg"); |
---|
226 | |
---|
227 | fpassthru($fp); |
---|
228 | exit; |
---|
229 | ?> |
---|