<?php

/**
 * You can remove these line!!!
 */
if (isset($_GET['get_source']) && $_GET['get_source'] == 'Y'){
    
show_source(__FILE__);
    exit();
}
///

/**
 * Simple wrapper for a lj data feed
 * At this time only latest-image feed is supported
 * @author Francesco Laurita
 * @access public
 */
class LjURL{
    const 
LJ_LATEST_IMG_URL "http://www.livejournal.com/stats/latest-img.bml";
    
    const 
EOL "\r\n";
    
    private 
$user null;
    private 
$email null;
    
    
    
/**
     * Creates a new instance of LjURL
     * See http://www.livejournal.com/bots/ about lj bot's policy
     * @param $user your site
     * @param $email yuour email contact
     */
    
public function __construct($user$email){
        
$this->email $email;
        
$this->user $user;
    }
    
    
/**
     * Create a socket
     *
     * @param string $url
     * @return resource
     */
    
private function createStream ($url){
        
$tok parse_url($url);
        
        
$socket = @fsockopen($tok['host'],80,$errno,$errstr,30);
        
        if (
$socket){
            
$out  "GET {$tok['path']} HTTP/1.1".self::EOL;
            
$out .= "Host: {$tok['host']}".self::EOL;
            
$out .= "User-Agent: {$this->user}; {$this->email}".self::EOL;
            
$out .= "Connection: Close".str_repeat(self::EOL,2);//Double newline
            
            
fwrite($socket,$out,strlen($out));
            
            return 
$socket;
        }else{
            throw new 
Exception(__METHOD__." Socket creation error: $errno $errstr");
        }
    }
    
    
/**
     * Used to retrive xml data from lj
     * @return resource
     */
    
public function getLatestImageStream(){
       return 
$this->createStream(self::LJ_LATEST_IMG_URL);
    }
    
    
/**
     * Read line in a socket
     *
     * @param resource $socket
     * @return string
     */
    
public static function readLine(&$socket){
        if (
feof($socket)) return null;
        
        return 
fgets($socket,4096);
    }
}

/**
 * Simple image handler.
 * It used to hold information about img src and post link
 * @author Francesco Laurita
 * @access public
 */
class LjImage{
    private  
$img_url null;
    private  
$link_url null;
    
   
/**
     * Creates a new instance of LjImage
     * @param $img the source url
     * @param $link post url
     */
    
public function __construct($img,$link){
        
$this->img_url $img;
        
$this->link_url $link;
    }
    
    
/**
     * the image url
     *
     * @return string
     */
    
public function getImgUrl(){
        return 
$this->img_url;
        
    }
    
    
/**
     * set the url
     *
     * @param string $url
     */
    
public function setImgUrl($url){
        
$this->img_url $url;
    }
    
    
/**
     * get the link
     *
     * @return string
     */
    
public function getLinkUrl(){
        return 
$this->link_url;
    }
    
    
/**
     * set the link
     *
     * @param string $link
     */
    
public function setLinkUrl($link){
        
$this->link_url $link;
    }
    
    
/**
     * An html rappresentation
     *
     * @return string
     */
    
public function __toString(){
        return 
"<a href=\"".$this->link_url."\"><img src=\"".$this->img_url."\" alt=\"\" border=\"0\"/></a>";
    }
    
   
/**
     * Factory to create a LjImage object from a xml line
     * @param $line the current xml line
     * @return LjImage a new LjImage object if line is a correct format, otherwise null
     */
    
public static function factory($line){
       
        if (
strlen($line) == 0) return null;
        
        if (
ereg("^<recent-image img='(.*)' url='(.*)' />$",$line,$out)){
            return new 
LjImage($out[1],$out[2]);
        }
        
        return 
null;
        
    }
}

/**
 * Example
 

try {
    $lj = new LjURL("www.yourpage.com","your@email.com");
    $socket = $lj->getLatestImageStream();
    $max_image = 20;
    
    $i = 0;
    while (($line = LjURL::readLine($socket) ) != null && $i < $max_image){
        $lj_image = LjImage::factory(trim($line));
        
        if (!is_null($lj_image)){
            $i++;
            echo "<p>";
            echo $lj_image."\n";
            echo "</p>";
        }
    
    }
    
    @fclose($socket);
}catch (Exception $ex){
    echo $ex->getMessage();
}
*/

?>