<?php

/* $Id:$ */

require_once 'HTTP/Request.php';
require_once
'PEAR.php';

/**
*
* This is the short Description for the Class
*
* This is the long description for the Class
*
* @author     Shoma Suzuki <shoma@catbot.net>
* @package    Web_Service_Bloglines
*/
class Web_Services_Bloglines{
    
// {{{ class property
    /**
     * @var    string
     * @access public
     */
    
var $user;

    
/**
     * @var    string
     * @access private
     */
    
var $_passwd;

    
/**
     * @var    object
     * @access public
     */
    
var $ua = 'catbot.net::Services_Bloglines';
    
// }}}

    // {{{ Web_Services_Bloglines($user="", $pass="")
    /**
    *
    * Constructor
    *
    * @return  string   Description
    * @param   string $user
    * @param   string $pass
    * @access  public
    */
    
function Web_Services_Bloglines($user="", $pass=""){
        if(
$user != "" AND $pass != ""){
            
$this->user = $user;
            
$this->_passwd = $pass;
        }else{
            
// Constructor Error
        
}
    }
    
// }}}
    // {{{ notify()
    /**
    *
    * http://www.bloglines.com/services/api/notifier
    *
    * @return  int unread num
    * @param  string $hoge
    * @access  public
    */
    
function notify(){
        
$url = "http://rpc.bloglines.com/update?";
        
$url .= $this->_http_build_query(array('user'=> $this->user, 'ver'=> 1));
        
$res = $this->_sendRequest($url);
        if(!
preg_match("/\|([\-\d]+)|(.*)|/", $res, $matchs)){
            
// Error
        
}else{
            return
$matchs[1];
        }
        
    }
    
// }}}
    // {{{ listsubs()
    /**
    *
    * http://www.bloglines.com/services/api/listsubs
    *
    * @return  mixed   Description
    * @param  string $hoge
    * @access  public
    */
    
function listsubs(){
        
$url = "http://rpc.bloglines.com/listsubs?";
        
$res = $this->_sendRequest($url);
        
        return
$this->_getSubscriptions($res);
    }
    
// }}}
    // {{{ getitems()
    /**
    *
    * http://www.bloglines.com/services/api/getitems
    *
    * @return string XML RSS
    * @param  string $subid
    * @param  string $mark_read
    * @param  string $time
    * @access  public
    */
    
function getitems($subid="", $mark_read="", $time=""){
        
$url = "http://rpc.bloglines.com/getitems?";
        
$param = array();
        if(
$subid     !== ""){ $param['s'] = $subid; }
        if(
$mark_read !== ""){ $param['n'] = $subid; }
        if(
$time      !== ""){ $param['d'] = $subid; }
        
$url.= $this->_http_build_query($param);
        
$res = $this->_sendRequest($url);
        
var_dump($res);
        
        return
$this->_getSubscriptions($res);
    }
    
// }}}
    // {{{ _sendRequest($url)
    /**
    *
    * Send request to Bloglines API
    *
    * @return  string   Description
    * @param   string $url
    * @access  private
    */
    
function _sendRequest($url){
        
$request = &new HTTP_Request($url);
        
$request->setBasicAuth($this->user, $this->_passwd);
        
$request->addHeader('User-Agent', $this->ua);
        
$request->sendRequest();
        
$responsecode = $request->getResponseCode();

        if (
$responsecode > 299 ) {
            return
PEAR::raiseError('Invalid Response Code', $request->getResponseCode());
        }else{
            return
$request->getResponseBody();
        }

    }
    
// }}}
    // {{{  _http_build_query($formdata, $numeric_prefix = "")
    /**
    *
    * same as http://www.php.net/http-build-query of PHP5
    *
    * @return  string http query string
    * @param   array $formdata
    * @param   int   $numeric_prefix
    * @access  private
    */
    
function _http_build_query($formdata, $numeric_prefix = ""){
        
$arr = array();
        foreach (
$formdata as $key => $val){
            
$arr[] = urlencode($numeric_prefix.$key)."=".urlencode($val);
            return
implode($arr, "&");
        }
    }
    
// }}}
    // {{{ _getSubscriptions($xml)
    /**
    *
    * This is the short Description for the Function
    *
    * @return  mixed   Description
    * @param   string  $xml OPML
    * @return  array   
    * @access  private
    */
    
function _getSubscriptions($xml){
        
$all = explode("\n", $xml);
        
$rtn = array();
        
$i = 0;
        foreach(
$all as $line){
            
$line = trim($line);
            if(
preg_match('|^<outline (.*?)/>$|', $line, $match)){
                if(
preg_match_all('/\s*(\w+)="([^\"]*)"/', $match[1], $attrs)){
                    foreach(
$attrs[1] as $key => $val){
                        
$rtn[$i][urldecode($val)] = urldecode($attrs[2][$key]);
                    }
                }
                
$i++;
            }
        }
        return
$rtn;
    }
    
// }}}
}
?>