<?php

ini_set
('allow_url_fopen','1');

define('CACHE_DIR','cache/');
$user = $_GET['user'] or 'wetnun';

print 
"Loading user: $user<br>\n";
$interests = get_user_interests($user);
if (
count($interests) > 0) {
    print 
"Got Interests<br>\n";
    foreach (
$interests as $interest) {
        
$images= get_google_images($interest);
        if (
count($images) > 0) {
            print 
$interest.'<br><img src='.$images[rand(0,count($images)-1)]."><br>\n";
        } else {
            print 
"Got no image for '$interest'<br>\n";
        }
    }
} else {
    print 
"Couldn't load interests for $user<br>\n";
}



##################################################

function get_user_interests($user) {
    
$url = $user.'.livejournal.com';
    
$data = '';
    
$cache_name = create_cache_name($user,1);
    
$cache = load_cache($cache_name);
    if (
count($cache) > 0) {
        return 
$cache;
    }
    
    
$fp = fsockopen($url, 80, $errno, $errstr, 30);
    if (!
$fp) {
        echo 
"$errstr ($errno)<br />\n";
    } else {
        
$out = "GET /data/foaf HTTP/1.1\r\n";
        
$out .= "Host: $url\r\n";
        
$out .= "Accept: text/html,text/plain\r\n";
        
$out .= "Accept-Language: en-us\r\n";
        
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6\r\n";
        
$out .= "Connection: close\r\n\r\n";
    
        
fwrite($fp, $out);
        while (!
feof($fp)) {
            
$data .= fgets($fp, 128);
        }
        
fclose($fp);
    }
    
    
$matches = array( array(), array() );
    
$reg = '|<foaf:interest dc:title="([^"]+)" rdf:resource="[^"]+" />|';
    
preg_match_all($reg,$data,$matches);
    
    if (
is_array($matches)) {
        
$res = create_cache($cache_name,$matches[1]);
        if (
$res == 0) {
            print 
"Couldn't create cache: $cache_name!<br>\n";
        }
    }
    
    return 
$matches[1];
}

function 
get_google_images($interest) {
    
$imgs = array();
    
$cache_name = create_cache_name($interest,2);

    
$cache = load_cache($cache_name);
    if (
count($cache) > 0) {
        return 
$cache;
    }
    
    
$url_interest = urlencode($interest);
    
$url = '/images?svnum=100&imgsz=small%7Cmedium%7Clarge%7Cxlarge&q='.$url_interest.'&btnG=Search';

    
$fp = fsockopen("images.google.com", 80, $errno, $errstr, 30);
    if (!
$fp) {
        echo 
"$errstr ($errno)<br />\n";
    } else {
        
$out = "GET $url HTTP/1.1\r\n";
        
$out .= "Host: images.google.com\r\n";
        
$out .= "Accept: text/html,text/plain\r\n";
        
$out .= "Referer: http://www.google.com/search?num=100&hl=en&lr=&imgsz=small%7Cmedium%7Clarge%7Cxlarge&safe=off&q=php+browser+headers+sent&btnG=Search\r\n";
        
$out .= "Accept-Language: en-us\r\n";
        
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6\r\n";
        
$out .= "Connection: close\r\n\r\n";
    
        
fwrite($fp, $out);
        while (!
feof($fp)) {
            
$data .= fgets($fp, 128);
        }
        
fclose($fp);
    }


    
preg_match_all('|dyn.Img\(([^\)]+)\)|',$data,$images);

    if (
is_array($images)) {
        foreach (
$images[1] as $img) {
            
$attr = split('","',$img);
            
$imgs[] = $attr[3];
        }
        
$res = create_cache($cache_name,$imgs);
        if (
$res == 0) {
            print 
"Couldn't create cache: $cache_name!<br>\n";
        }
    }
    
    return 
$imgs;
}

// 1 = user, 2 = interst images
function create_cache_name($name,$type=1) {
    
$name = str_replace(' ', '_', $name);
    if (
$type == 2) {
        
$name = 'interest-'.$name;
    }
    return 
$name;
}

function 
load_cache($name) {
    
$ttl = 5 * 60 * 60; // 5 hours
    
if (file_exists(CACHE_DIR.$name)) {
        
$timestamp = filemtime(CACHE_DIR.$name);
        
$age = (time() - $timestamp);

        
// got good cache
        
if ($age <= $ttl) {
            return 
unserialize( file_get_contents( CACHE_DIR.$name ) );
        }
    }
    return array();
}

function 
create_cache($name,$data) {

    if (!
$handle = fopen(CACHE_DIR.$name, 'w+')) {
        return 
0;
    }
    
    if (
fwrite($handle, serialize($data)) === FALSE) {
        return 
0;
    }

    
fclose($handle);
    return 
1;
}

?>