n/oldblog
Archived
1
0
Fork 0
This repository has been archived on 2024-02-13. You can view files and clone it, but cannot push or open issues or pull requests.
oldblog/include/rssparser.inc.php

206 lines
5.4 KiB
PHP
Raw Permalink Normal View History

2023-04-30 21:28:42 +02:00
<?php
//************************************************
//* renvoie les titres d'un fichier backend RSS **
//************************************************
function parse_rss($filename)
{
$root = "/home/www/website/backends/xml/";
global $convert;
$trans["&amp;"] = "[ampersand]";
$trans["& "] = "[ampersand]";
$trans["<EFBFBD>"] = "[agrave]";
$trans["<EFBFBD>"] = "[eacute]";
$trans["<EFBFBD>"] = "[egrave]";
$trans["<EFBFBD>"] = "[ecirc]";
$trans["<EFBFBD>"] = "[icirc]";
$trans["<EFBFBD>"] = "[ugrave]";
$trans["<EFBFBD>"] = "[ccedil]";
$trans[">"] = ">\n";
// php5 ne d<>tecte pas les encodages XML (bug??)
//$trans["iso-8859-15"] = "iso-8859-1";
//$trans["UTF-8"] = "iso-8859-1";
if (isset($convert) && $convert="on")
$data = utf8_decode(strtr(implode("",file($root.$filename)),$trans));
else
$data = implode("",file($root.$filename));
//$data = html_entity_decode($data);
// initialise les compteurs
$cpt = 0;
$index_item = 0;
// parse le fichier et remplit les tableaux
$p = xml_parser_create("");
xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, "iso-8859-1");
xml_parse_into_struct($p,$data,&$vals,&$index);
xml_parse_into_struct($p,$data,$vals,$index);
xml_parser_free($p);
// parcourt et r<>cup<75>re les donn<6E>es
foreach($index as $key->$val)
{
$array = $key->$val;
$pos = $array[0];
$tag = $vals[$pos][tag];
$val = $vals[$pos][value];
if ($tag == "ITEM")
{
while ($index[ITEM][$index_item] != "")
{
$tag2 = $vals[$index[ITEM][$index_item]+1][tag];
$value2 = $vals[$index[ITEM][$index_item]+1][value];
$index_item++;
if ($tag2 == "TITLE")
{
$items[$cpt]["title"] = $value2;
// echo "$value2";
}
if ($tag2 == "LINK")
$items[$cpt++]["link"] = $value2;
}
}
}
return $items;
}
function parse_rss2($filename)
{
$root = "/home/www/website/backends/xml/";
//$xml = simplexml_load_file($root.$filename);
$file = implode(file($rootdir.$filename));
$cleaned = preg_replace("/<copyright>(.?)<\/copyright>/","",$file);
$cleaned2 = preg_replace("/<description>(.?)<\/description>/","",$cleaned);
$xml = simplexml_load_string($cleaned2);
$cpt = 0;
foreach($xml->channel->item as $news)
{
$items[$cpt]["title"] = utf8_decode((string) $news->title);
$items[$cpt++]["link"] = $news->link;
//echo '<a href="'.$news->link.'">', utf8_decode((string) $news->title), '</a><br />';
}
return $items;
}
//*************************************************
//* renvoie les donn<6E>es suppl<70>mentaires d'un site *
//* <20> partir d'un fichier backend RSS *
//*************************************************
function get_sitedata($source)
{
$rootdir = "/home/www/website/backends/xml/";
// extrait le nom du fichier
if (!isset($sitedata))
if (file_exists($rootdir.$source) && filesize($rootdir.$source) != 0)
{
//$rss = simplexml_load_file($rootdir.$source);
$file = implode(file($rootdir.$source));
$cleaned = preg_replace("/<copyright>(.?)<\/copyright>/","",$file);
$cleaned2 = preg_replace("/<description>(.?)<\/description>/","",$cleaned);
$rss = simplexml_load_string($cleaned2);
$sitedata["title_site"] = utf8_decode($rss->channel->title);
$sitedata["link_site"] = $rss->channel->link;
$sitedata["description"] = $rss->channel->description;
$sitedata["language"] = $rss->channel->language;
return $sitedata;
}
}
//***********************************************
//* renvoie les titres d'un fichier backend TXT *
//***********************************************
//* forme la plus simple: *
//***********************************************
//* %% *
//* title *
//* link *
//* %% *
//***********************************************
function parse_text_1($filename)
{
$data = file($filename);
while($data[$index] != "")
{
if (!strchr("%",$data[$index]))
{
$items[$index]["title"] = $data[$index];
$items[$index]["link"] = $data[$index++];
}
else $index++;
}
return $items;
}
//**************************************
//* renvoie les titres d'une page HTML *
//**************************************
function parse_html($filename,$regs)
{
$index=$index_item=0;
$data = file($filename);
ereg($regs,$data[$index],$result);
while($data[$index++] != "")
{
ereg($regs,$data[$index],$result);
if ($result[1] != "")
{
if (strchr($result[1],"http://"))
$item[$index_item]["link"] = strtok(urldecode($result[1]),"\"");
else
$item[$index_item]["link"] = $url_site.strtok(urldecode($result[1]),"\"");
$result[1]="";
}
if ($result[2] != "")
{
$item[$index_item++]["title"] = html2ascii($result[2]);
$result[2] = "";
}
}
return $item;
}
function parse_html2($data,$regs)
{
$index=$index_item=0;
ereg($regs,$data[$index],$result);
while($data[$index++] != "")
{
ereg($regs,$data[$index],$result);
if ($result[1] != "")
{
$item[$index_item]["link"] = strtok(urldecode($result[1]),"\"");
$result[1]="";
}
if ($result[2] != "")
{
$item[$index_item++]["title"] = $result[2];
$result[2] = "";
}
}
return $item;
}
?>