Scripts

GTK - RSS Reader

Een PHP-GTK Rss reader, aangezien er nog maar 1 php-gtk script was, en ik hiermee bezig was dacht ik, laat ik hem eens posten. Hij is niet erg uitgebreid, maar ik hoop toch een goed voorbeeld van wat je met PHP-GTK kan doen. Het voorbeeld is een screenshot, want je kan PHP-GTK niet op een website draaien. Als je php-gtk niet hebt geinstalleerd klik dan Hier! - Ja ik heb functies van anderen gebruikt, maar zover ik weet zitten daar geen beperkingen op, qua licensies. Dus ook dit script mag voor alles gebruikt worden (als ik er geen last van krijg iig ;) Als ik iets vergeten ben dan hoor ik het wel :)

gtk-rss-reader
[b] // rss_class.php [/b]

<?
if(!extension_loaded('gtk')) {    
    dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
} 

class rss_reader {
var $CONFIG, $widgets, $rss_array;

function rss_reader ($url = "http://www.phphulp.nl/feeds/phphulp_forum.xml",$rate = 30, $browser = "firefox.exe",$tooltips = true) {
if (preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url)) {
$this->CONFIG['feed'] = $url; // set de url van de rss feed.
} else {
$this->CONFIG['feed'] = false;
}
if (is_numeric($rate)) {
$this->CONFIG['refresh'] = $rate; // set de refresh snelheid
} else {
$this->CONFIG['refresh'] = 30;
}
if (is_bool($tooltips)) {
$this->CONFIG['tooltips'] = $tooltips;
} else {
$this->CONFIG['tooltips'] = true;
}
$this->CONFIG['browser'] = $browser;
}

function change_refresh ($rate) {
if (is_numeric($rate)) {
$this->CONFIG['refresh'] = $rate; // set de refresh snelheid
} else {
$this->CONFIG['refresh'] = 30;
}
}

function change_browser ($browser) {
$this->CONFIG['browser'] = $browser;
}

function change_tooltips ($tooltips) {
if (is_bool($tooltips)) {
$this->CONFIG['tooltips'] = $tooltips;
} else {
$this->CONFIG['tooltips'] = true;
}
}

function change_url ($url) {
if (preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url)) {
$this->CONFIG['feed'] = $url; // set de url van de rss feed.
} else {
$this->CONFIG['feed'] = false;
}
}


function build_window () {
$this->widgets['window'] = &new GtkWindow(GTK_WINDOW_DIALOG);
$this->widgets['window']->set_name('Rss feeds');
$this->widgets['window']->set_title('Rss feeds');
$this->widgets['window']->set_usize(500,400);
$this->widgets['window']->connect_object('destroy', array('gtk', 'main_quit'));
$this->widgets['window']->set_border_width(10);
$this->widgets['window']->set_position(GTK_WIN_POS_CENTER);
}


function show_rss () {

// Get rss feeds;
$old_rss = $this->rss_array;
$this->rss_array = $this->getRssFeeds("Rss feed", $this->CONFIG['feed']);
if (count($this->rss_array) > 0 && $this->rss_array != $old_rss) {
if ($this->widgets['window']->child) $this->widgets['window']->remove($this->widgets['window']->child);

if ($this->CONFIG['tooltips']) $tooltips = &new GtkTooltips();

$scrolledwindow = &new GtkScrolledWindow();
$scrolledwindow->set_policy(GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);

$this->widgets['window']->add($scrolledwindow);

$table = &new GtkTable(count($rss_array),1,true);

	$scrolledwindow->add_with_viewport($table);

foreach ($this->rss_array as $key => $val) {
if ($val['pubdate']) {
$date = date("H:i",$val['pubdate']);
}
$button[$key] = &new GtkButton($date . " " . $this->dec_str(strip_tags(html_entity_decode($val['title']))));
$button[$key]->connect('released', array($this,'open_url'),$val['link']);
$button[$key]->set_relief(GTK_RELIEF_NONE);
if ($this->CONFIG['tooltips']) $tooltips->set_tip($button[$key], trim(strip_tags(html_entity_decode(str_replace("\n"," ",$val['description'])))), '');
$table->attach_defaults($button[$key],0,1,$key,$key + 1);
$this->change_style($button[$key]);
}
$this->change_style($table);
$this->change_style($scrolledwindow);
$this->change_style($this->widgets['window']);
$this->widgets['window']->show_all();
} elseif (!$this->widgets['window']->child) {
$scrolledwindow = &new GtkScrolledWindow();
$scrolledwindow->set_policy(GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);

$this->widgets['window']->add($scrolledwindow);

$table = &new GtkTable(count($rss_array),1,true);

	$scrolledwindow->add_with_viewport($table);
$key = 0;
$button[$key] = &new GtkButton("Click to refresh");
$button[$key]->connect('released', '$this->show_rss');
$button[$key]->set_relief(GTK_RELIEF_NONE);
$table->attach_defaults($button[$key],0,1,$key,$key + 1);
$this->change_style($button[$key]);

$this->change_style($table);
$this->change_style($scrolledwindow);
$this->change_style($this->widgets['window']);
$this->widgets['window']->show_all();
}
return true;
}

function start_reading () {
gtk::timeout_add($this->CONFIG['refresh'] * 1000, array($this,'show_rss')); 
gtk::main_iteration(); 
Gtk::main();
}

// hieronder alleen internal functies :)

function change_style (&$style_widget) {

 $style = $style_widget->style;
$style = $style->copy();
$cmap = $style_widget->get_colormap();
 $blue = $cmap->alloc('#406090');
$white = $cmap->alloc('#FFFFFF');
$style->bg[GTK_STATE_NORMAL] = $blue;
$style->bg[GTK_STATE_ACTIVE] = $white;
$style->bg[GTK_STATE_PRELIGHT] = $blue;

$style_widget->set_style($style);

$label = $style_widget->child;
$style = $label->style;
if ($style) {
$style = $style->copy();
$style->font = gdk::font_load('-*-helvetica-medium-r-normal--*-130-*-*-p-0-iso8859-1');
$label->set_style($style);
}
}

function dec_str($line, $len = 70) {
    if (strlen($line) > $len) {
        $afgekort = substr($line, 0, $len) . "...";
   } else {
        $afgekort = $line;
}
return $afgekort;
} 

function open_url ($widget,$url) {
exec("\"Start ".$this->CONFIG['browser']." \"" . $url . "");
}

function getRssFeeds($name, $url)
{
    if($file = fopen($url,"r"))
    {
        while(!feof($file))
        {
            $data .= fgets($file, 1024);
        }
        fclose($file);
    }
$data = html_entity_decode($data);

    $p = explode("<item>", $data);
    for($i = 0; $i < count($p); $i++)
    {
        $item = $p[$i];

        if($q = stristr($item, "<title"))
        {
            $r = stristr($q, "</title");
            $rss[$i]['title'] = substr($q, 7, strlen($q) - strlen($r) - 7);
        }

        if($q = stristr($item, "<description"))
        {
            $r = stristr($q, "</description");
            $rss[$i]['description'] = substr($q, 13, strlen($q) - strlen($r) - 13);          
                $rss[$i]['description'] = preg_replace('/&lt;(.*?)&gt;/i', "[HTML]", $rss[$i]['description']);
        }

        if($q = stristr($item, "<pubDate"))
        {
            $r = stristr($q, "</pubDate");
            $rss[$i]['pubdate'] = strtotime(substr($q, 9, strlen($q) - strlen($r) - 9));
        }

        if($q = stristr($item, "<link"))
        {
            $r = stristr($q, "</link");
            $rss[$i]['link'] = substr($q, 6, strlen($q) - strlen($r) - 6);
        }
    }

    return $rss;
}
}
?>

[b]// rss_reader.php: voorbeeld [/b]

<?
include('rss_class.php');  // includen van de class, spreekt voor zichzelf

$reader = new rss_reader; // class 'starten' 
// kan ook: $reader = new rss_reader(url,refresh rate, browser, tooltips true/false)


$reader->build_window(); // maak de window

$reader->change_url("http://rss.fok.nl/feeds/nieuws"); // verander de url van de standaard naar fok.nl, dit kan ook via de eerste parameter van rss_reader();

$reader->change_refresh(30); // stel de refreshrate in op 30 seconden, zie 2de parameter rss_reader

$reader->change_browser("firefox.exe"); // stel de browser in, 3de parameter..

$reader->change_tooltips(true); // zet tooltips aan, zie 4de parameter...

$reader->show_rss(); // laat de rss feed direct zien

$reader->start_reading(); // BELANGRIJK, deze functie niet vergeten anders refreshed hij niet, en werkt het script sowieso niet :)

?>

Reacties

0
Nog geen reacties.