rss
[ class tree: rss ] [ index: rss ] [ all elements ]

Source for file rss_lib.php

Documentation is available at rss_lib.php

  1. <?php
  2. /**
  3.  * @version $Header: /cvsroot/bitweaver/_bit_rss/rss_lib.php,v 1.11 2007/01/06 09:46:24 squareing Exp $
  4.  * @package rss
  5.  *
  6.  *  Copyright (c) 2004 bitweaver.org
  7.  *  Copyright (c) 2003 tikwiki.org
  8.  *  Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
  9.  *  All Rights Reserved. See copyright.txt for details and a complete list of authors.
  10.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details
  11.  *
  12.  *  $Id: rss_lib.php,v 1.11 2007/01/06 09:46:24 squareing Exp $
  13.  */
  14.  
  15. /**
  16.  * @package rss
  17.  */
  18. class RSSLib extends BitBase {
  19.     function RSSLib({
  20.         BitBase::BitBase();
  21.     }
  22.  
  23.     function list_rss_modules($offset$max_records$sort_mode$find{
  24.  
  25.         if ($find{
  26.             $findesc="%" $find "%";
  27.             $mid " where (`name` like ? or `description` like ?)";
  28.             $bindvars=array($findesc,$findesc);
  29.         else {
  30.             $mid "";
  31.             $bindvars=array();
  32.         }
  33.  
  34.         $query "select * from `".BIT_DB_PREFIX."rss_modules$mid order by ".$this->mDb->convertSortmode($sort_mode);
  35.         $query_cant "select count(*) from `".BIT_DB_PREFIX."rss_modules$mid";
  36.         $result $this->mDb->query($query,$bindvars,$max_records,$offset);
  37.         $cant $this->mDb->getOne($query_cant,$bindvars);
  38.         $ret array();
  39.  
  40.         while ($res $result->fetchRow()) {
  41.             $res["minutes"$res["refresh"60;
  42.  
  43.             $ret[$res;
  44.         }
  45.  
  46.         $retval array();
  47.         $retval["data"$ret;
  48.         $retval["cant"$cant;
  49.         return $retval;
  50.     }
  51.  
  52.     function replace_rss_module($rss_id$name$description$url$refresh$show_title$show_pub_date{
  53.         $ret FALSE;
  54.         ifis_numeric$rss_id ) ) {
  55.             //if($this->rss_module_name_exists($name)) return false; // TODO: Check the name
  56.             $refresh 60 $refresh;
  57.     
  58.             if ($rss_id{
  59.                 $query "update `".BIT_DB_PREFIX."rss_modules` set `name`=?,`description`=?,`refresh`=?,`url`=?,`show_title`=?,`show_pub_date`=? where `rss_id`=?";
  60.                 $bindvars=array($name,$description,$refresh,$url,$show_title,$show_pub_date,$rss_id);
  61.             else {
  62.                 // was: replace into, no clue why.
  63.                 $query "insert into `".BIT_DB_PREFIX."rss_modules`(`name`,`description`,`url`,`refresh`,`content`,`last_updated`,`show_title`,`show_pub_date`)
  64.                     values(?,?,?,?,?,?,?,?)";
  65.                 $bindvars=array($name,$description,$url,$refresh,'',1000000,$show_title,$show_pub_date);
  66.             }
  67.     
  68.             $result $this->mDb->query($query,$bindvars);
  69.             $ret true;
  70.         }
  71.         return $ret;
  72.     }
  73.  
  74.     function remove_rss_module($rss_id{
  75.         $ret FALSE;
  76.         ifis_numeric$rss_id ) ) {
  77.             $query "delete from `".BIT_DB_PREFIX."rss_modules` where `rss_id`=?";
  78.     
  79.             $result $this->mDb->query($query,array($rss_id));
  80.             $ret true;
  81.         }
  82.         return $ret;
  83.     }
  84.  
  85.     function get_rss_module($rss_id{
  86.         $ret FALSE;
  87.         ifis_numeric$rss_id ) ) {
  88.             $query "select * from `".BIT_DB_PREFIX."rss_modules` where `rss_id`=?";
  89.     
  90.             $result $this->mDb->query($query,array($rss_id));
  91.     
  92.             if (!$result->numRows())
  93.                 return false;
  94.     
  95.             $ret $result->fetchRow();
  96.         }
  97.         return $ret;
  98.     }
  99.  
  100.     function startElementHandler($parser$name$attribs{
  101.         if ($this->flag{
  102.             $this->buffer .= '<' $name '>';
  103.         }
  104.  
  105.         if ($name == 'item' || $name == 'items'{
  106.             $this->flag 1;
  107.         }
  108.     }
  109.  
  110.     function endElementHandler($parser$name{
  111.         if ($name == 'item' || $name == 'items'{
  112.             $this->flag 0;
  113.         }
  114.  
  115.         if ($this->flag{
  116.             $this->buffer .= '</' $name '>';
  117.         }
  118.     }
  119.  
  120.     function characterDataHandler($parser$data{
  121.         if ($this->flag{
  122.             $this->buffer .= $data;
  123.         }
  124.     }
  125.  
  126.     function NewsFeed($data$rss_id{
  127.         $news array();
  128.         ifis_numeric$rss_id ) ) {
  129.     
  130.             $show_pub_date $this->get_rss_show_pub_date($rss_id);
  131.     
  132.             $this->buffer '';
  133.             $this->flag 0;
  134.             $this->parser xml_parser_create("UTF-8");
  135.             xml_parser_set_option($this->parserXML_OPTION_CASE_FOLDINGfalse);
  136.             xml_set_object($this->parser$this);
  137.             xml_set_element_handler($this->parser"startElementHandler""endElementHandler");
  138.             xml_set_character_data_handler($this->parser"characterDataHandler");
  139.     
  140.             if (!xml_parse($this->parser$data1)) {
  141. #                print ("<!-- XML Parse error at " . xml_get_current_line_number($this->parser) . ":  "
  142. #                       . xml_error_string(xml_get_error_code($this->parser)) . " -->\n");
  143.                 $news[array('title'=>
  144.                     "XML Parse error at " xml_get_current_line_number($this->parser":  "
  145.                     . xml_error_string(xml_get_error_code($this->parser)) "",
  146.                     'link'=>'',
  147.                     'pubdate'=>'',
  148.                     );
  149.                 return $news;
  150.             }
  151.     
  152.             xml_parser_free ($this->parser);
  153.             preg_match_all("/<title>(.*?)<\/title>/i"$this->buffer$titles);
  154.             preg_match_all("/<link>(.*?)<\/link>/i"$this->buffer$links);
  155.     
  156.             $pubdate array();
  157.             preg_match_all("/<dc:date>(.*?)<\/dc:date>/i"$this->buffer$pubdate);
  158.             if (count($pubdate[1])<1)                
  159.             preg_match_all("/<pubDate>(.*?)<\/pubDate>/i"$this->buffer$pubdate);
  160.     
  161.             for ($i 0$i count($titles[1])$i++{
  162.                 $anew["title"$titles[1][$i];
  163.     
  164.                 if (isset($links[1][$i])) {
  165.                     $anew["link"$links[1][$i];
  166.                 else {
  167.                     $anew["link"'';
  168.                 }
  169.                 if isset($pubdate[1][$i]&& ($show_pub_date == 'y') )
  170.                 {
  171.                     $anew["pubdate"$pubdate[1][$i];
  172.                 else {
  173.                     $anew["pubdate"'';
  174.                 }
  175.                 $news[$anew;
  176.             }
  177.         }
  178.         return $news;
  179.     }
  180.  
  181.     function parse_rss_data($rssdata$rss_id{
  182.         return $this->NewsFeed($rssdata$rss_id);
  183.     }
  184.  
  185.     function refresh_rss_module($rss_id{
  186.         $info $this->get_rss_module($rss_id);
  187.  
  188.         if ($info{
  189.             global $gBitSystem;
  190.             $data $this->rss_iconvtp_http_request($info['url']));
  191.             $now $gBitSystem->getUTCTime();
  192.             $query "update `".BIT_DB_PREFIX."rss_modules` set `content`=?, `last_updated`=? where `rss_id`=?";
  193.             $result $this->mDb->query($query,array((string)$data,(int) $now(int) $rss_id));
  194.             return $data;
  195.         else {
  196.             return false;
  197.         }
  198.     }
  199.  
  200.     function rss_module_name_exists($name{
  201.         $query "select `name` from `".BIT_DB_PREFIX."rss_modules` where `name`=?";
  202.  
  203.         $result $this->mDb->query($query,array($name));
  204.         return $result->numRows();
  205.     }
  206.  
  207.     function get_rss_module_id($name{
  208.         $query "select `rss_id` from `".BIT_DB_PREFIX."rss_modules` where `name`=?";
  209.  
  210.         $id $this->mDb->getOne($query,array($name));
  211.         return $id;
  212.     }
  213.  
  214.     function get_rss_show_title($rss_id{
  215.         $ret FALSE;
  216.         ifis_numeric$rss_id ) ) {
  217.             $query "select `show_title` from `".BIT_DB_PREFIX."rss_modules` where `rss_id`=?";
  218.     
  219.             $ret $this->mDb->getOne($query,array($rss_id));
  220.         }
  221.         return $ret;
  222.     }
  223.  
  224.     function get_rss_show_pub_date($rss_id{
  225.         $ret FALSE;
  226.         ifis_numeric$rss_id ) ) {
  227.             $query "select `show_pub_date` from `".BIT_DB_PREFIX."rss_modules` where `rss_id`=?";
  228.     
  229.             $show_pub_date $this->mDb->getOne($query,array($rss_id));
  230.             $ret $show_pub_date;
  231.         }
  232.         return $ret;
  233.     }
  234.  
  235.     function get_rss_module_content($rss_id{
  236.         $ret FALSE;
  237.         ifis_numeric$rss_id ) ) {
  238.  
  239.             if$info $this->get_rss_module($rss_id) ) {
  240.                 global $gBitSystem;
  241.                 $now $gBitSystem->getUTCTime();
  242.         
  243.         //        if ($info["last_updated"] + $info["refresh"] < $now) {
  244.                     $data $this->refresh_rss_module($rss_id);
  245.         //        }
  246.         
  247.                 $info $this->get_rss_module($rss_id);
  248.                 $ret $info["content"];
  249.             }
  250.         }
  251.         return $ret;
  252.     }
  253.  
  254.     function rss_iconv($xmlstr$tencod "UTF-8"{
  255.         if (preg_match("/<\?xml.*encoding=\"(.*)\".*\?>/"$xmlstr$xml_head)) {
  256.             $sencod strtoupper($xml_head[1]);
  257.  
  258.             switch ($sencod{
  259.             case "ISO-8859-1":
  260.                 // Use utf8_encode a more standard function
  261.                 $xmlstr utf8_encode($xmlstr);
  262.  
  263.                 break;
  264.  
  265.             case "UTF-8":
  266.             case "US-ASCII":
  267.                 // UTF-8 and US-ASCII don't need convertion
  268.                 break;
  269.  
  270.             default:
  271.                 // Not supported encoding, we must use iconv() or recode()
  272.                 if (function_exists('iconv')) {
  273.                     // We have iconv use it
  274.                     $new_xmlstr @iconv($sencod$tencod$xmlstr);
  275.  
  276.                     if ($new_xmlstr === FALSE{
  277.                         // in_encod -> out_encod not supported, may be misspelled encoding
  278.                         $sencod strtr($sencodarray(
  279.                             "-" => "",
  280.                             "_" => "",
  281.                             " " => ""
  282.                         ));
  283.  
  284.                         $new_xmlstr @iconv($sencod$tencod$xmlstr);
  285.  
  286.                         if ($new_xmlstr === FALSE{
  287.                             // in_encod -> out_encod not supported, leave it
  288.                             $tencod $sencod;
  289.  
  290.                             break;
  291.                         }
  292.                     }
  293.  
  294.                     $xmlstr $new_xmlstr;
  295.                     // Fix an iconv bug, a few garbage chars beyound xml...
  296.                     $xmlstr preg_replace("/(.*<\/rdf:RDF>).*/s""\$1"$xmlstr);
  297.                 elseif (function_exists('recode_string')) {
  298.                     // I don't have recode support could somebody test it?
  299.                     $xmlstr @recode_string("$sencod..$tencod"$xmlstr);
  300.                 else {
  301.                 // This PHP intallation don't have any EncodConvFunc...
  302.                 // somebody could create bit_iconv(...)?
  303.                 }
  304.             }
  305.  
  306.             // Replace header, put the new encoding
  307.             $xmlstr preg_replace("/(<\?xml.*)encoding=\".*\"(.*\?>)/""\$1 encoding=\"$tencod\"\$2"$xmlstr);
  308.         }
  309.  
  310.         return $xmlstr;
  311.     }
  312. }
  313.  
  314. global $rsslib;
  315. $rsslib new RSSLib();
  316.  
  317. ?>

Documentation generated on Thu, 15 Feb 2007 20:48:26 +0000 by phpDocumentor 1.3.0