<?php
    
    
function endsWith$str$sub ) {
    return ( 
substr$strstrlen$str ) - strlen$sub ) ) === $sub );
    }
    
    
$path $_GET['path'];
    
$dir "icons/" $path;
    
$hash $_GET['hash'];
    
$pic $_GET['pic'];
    if(
$hash == '') { exit(); }
    
    if(
$handle = @opendir($dir)) {
    while(
$addr readdir($handle)) {
        if(
$hash == sha1($addr)) {
        
$dir $dir $addr;
        break;
        }
    }
    
closedir($handle);
    }
    
    if(
$pic == '') {
    echo 
"<html><head><title>Buddy Icons</title></head><body>\n";
        if(
$handle = @opendir($dir)) {
    
        
$allpics = array(
        
"name" => array(),
        
"date" => array()
        );
    
        
$i 0;
        while(
$pic readdir($handle)) {
        if(
endsWith($pic".png")) {
            
$allpics[$i]['name'] = $pic;
            if(
$fp = @fopen("$dir/$pic"'r')) {
            
$stats fstat($fp);
            
fclose($fp);
            }
            
$allpics[$i]['date'] = $stats['mtime'];
            
$i++;
        }
        }
        
closedir($handle);
    }

    
// Sort by last used date
    
foreach($allpics as $key => $row) {
        
$picn[$key] = $row['date'];
        
$picd[$key] = $row['name'];
    }
    
array_multisort($picnSORT_DESC$picdSORT_ASC$allpics);
    
    foreach(
$allpics as $pic) {
        if(
strlen($pic['name']) > 0) {
        if(
$fp = @fopen("$dir/{$pic['name']}"'r')) {
            
fclose($fp);
            
$date date('D M j G:i:s T Y'$pic['date']);
            echo 
"<img src='show.php?path=$path&hash=$hash&pic={$pic['name']}' title='$date'>\n";
        }
        }
    }
    echo 
"</body></html>\n";
    } else {
    
header("Content-Type: image/png");
    if(
$fp = @fopen("$dir/$pic"'r')) {
        
fpassthru($fp);
        
fclose($fp);
    }
    }
?>