@@ -232,12 +232,6 @@ function parse_request()
232232 }
233233 }
234234 array_shift ($ deriv );
235- $ page ['coi ' ] = '' ;
236- if (count ($ deriv ) && $ deriv [0 ][0 ]=='c ' && $ deriv [0 ][1 ]=='i ' )
237- {
238- $ page ['coi ' ] = substr (array_shift ($ deriv ), 2 );
239- preg_match ('#^[a-zA-Z]{4}$# ' , $ page ['coi ' ]) or ierror ('Invalid center of interest ' , 400 );
240- }
241235
242236 if ($ page ['derivative_type ' ] == IMG_CUSTOM )
243237 {
@@ -256,7 +250,7 @@ function parse_request()
256250 {
257251 ierror ('Too big ' , 403 );
258252 }
259-
253+
260254 $ key = array ();
261255 $ params ->add_url_tokens ($ key );
262256 $ key = implode ('_ ' , $ key );
@@ -266,15 +260,65 @@ function parse_request()
266260 }
267261 }
268262
269- if (!is_file (PHPWG_ROOT_PATH .$ req .$ ext ) and
270- is_file (PHPWG_ROOT_PATH .'../ ' .$ req .$ ext ) )
263+ if (is_file (PHPWG_ROOT_PATH .$ req .$ ext ))
264+ {
265+ $ req = './ ' .$ req ; // will be used to match #iamges.path
266+ }
267+ elseif (is_file (PHPWG_ROOT_PATH .'../ ' .$ req .$ ext ))
268+ {
271269 $ req = '../ ' .$ req ;
270+ }
272271
273272 $ page ['src_location ' ] = $ req .$ ext ;
274273 $ page ['src_path ' ] = PHPWG_ROOT_PATH .$ page ['src_location ' ];
275274 $ page ['src_url ' ] = $ page ['root_path ' ].$ page ['src_location ' ];
276275}
277276
277+ function try_switch_source (DerivativeParams $ params , $ original_mtime )
278+ {
279+ global $ page ;
280+ $ candidates = array ();
281+ foreach (ImageStdParams::get_defined_type_map () as $ candidate )
282+ {
283+ if ($ candidate ->type == $ params ->type )
284+ continue ;
285+ if ($ candidate ->use_watermark != $ params ->use_watermark )
286+ continue ;
287+ if ($ candidate ->max_width () < $ params ->max_width () || $ candidate ->max_height () < $ params ->max_height ())
288+ continue ;
289+ if ($ params ->sizing ->max_crop ==0 )
290+ {
291+ if ($ candidate ->sizing ->max_crop !=0 )
292+ continue ;
293+ }
294+ else
295+ {
296+ if ($ candidate ->sizing ->max_crop !=0 )
297+ continue ; // this could be optimized
298+ if (!isset ($ page ['original_size ' ]))
299+ continue ;
300+ $ candidate_size = $ candidate ->compute_final_size ($ page ['original_size ' ]);
301+ if ($ candidate_size [0 ] < $ params ->sizing ->min_size [0 ] || $ candidate_size [1 ] < $ params ->sizing ->min_size [1 ] )
302+ continue ;
303+ }
304+ $ candidates [] = $ candidate ;
305+ }
306+
307+ foreach ( array_reverse ($ candidates ) as $ candidate )
308+ {
309+ $ candidate_path = $ page ['derivative_path ' ];
310+ $ candidate_path = str_replace ( '- ' .derivative_to_url ($ params ->type ), '- ' .derivative_to_url ($ candidate ->type ), $ candidate_path );
311+ $ candidate_mtime = @filemtime ($ candidate_path );
312+ if ($ candidate_mtime === false
313+ || $ candidate_mtime < $ original_mtime
314+ || $ candidate_mtime < $ candidate ->last_mod_time )
315+ continue ;
316+ $ params ->use_watermark = false ;
317+ $ params ->sharpen = min (1 , $ params ->sharpen );
318+ $ page ['src_path ' ] = $ candidate_path ;
319+ $ page ['src_url ' ] = $ page ['root_path ' ] . substr ($ candidate_path , strlen (PHPWG_ROOT_PATH ));
320+ }
321+ }
278322
279323function send_derivative ($ expires )
280324{
@@ -362,6 +406,40 @@ function send_derivative($expires)
362406 send_derivative ($ expires );
363407}
364408
409+ $ page ['coi ' ] = null ;
410+ if (strpos ($ page ['src_location ' ], '/pwg_representative/ ' )===false
411+ && strpos ($ page ['src_location ' ], 'themes/ ' )===false
412+ && strpos ($ page ['src_location ' ], 'plugins/ ' )===false )
413+ {
414+ @include (PHPWG_ROOT_PATH .PWG_LOCAL_DIR .'config/database.inc.php ' );
415+ include (PHPWG_ROOT_PATH .'include/dblayer/functions_ ' .$ conf ['dblayer ' ].'.inc.php ' );
416+ try
417+ {
418+ $ pwg_db_link = pwg_db_connect ($ conf ['db_host ' ], $ conf ['db_user ' ],
419+ $ conf ['db_password ' ], $ conf ['db_base ' ]);
420+ $ query = 'SELECT coi, width, height FROM ' .$ prefixeTable .'images WHERE path= \'' .$ page ['src_location ' ].'\'' ;
421+ if ( ($ row =pwg_db_fetch_assoc (pwg_query ($ query ))) )
422+ {
423+ if (isset ($ row ['width ' ]))
424+ {
425+ $ page ['original_size ' ] = array ($ row ['width ' ],$ row ['height ' ]);
426+ }
427+ $ page ['coi ' ] = $ row ['coi ' ];
428+ }
429+ mysql_close ($ pwg_db_link );
430+ if (!$ row )
431+ {
432+ ierror ('Db file path not found ' , 404 );
433+ }
434+ }
435+ catch (Exception $ e )
436+ {
437+ ilog ("db error " , $ e ->getMessage ());
438+ }
439+ }
440+
441+ try_switch_source ($ params , $ src_mtime );
442+
365443if (!mkgetdir (dirname ($ page ['derivative_path ' ])))
366444{
367445 ierror ("dir create error " , 500 );
0 commit comments