root/trunk/gregarius/opml.php

Revision 1744, 6.8 kB (checked in by mbonetti, 14 months ago)

Fix for [487] - do not export deprecated feeds

  • Property svn:eol-style set to native
  • Property svn:eolstyle set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2###############################################################################
3# Gregarius - A PHP based RSS aggregator.
4# Copyright (C) 2003 - 2006 Marco Bonetti
5#
6###############################################################################
7# This program is free software and open source software; you can redistribute
8# it and/or modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 2 of the License,
10# or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15# more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
20# http://www.gnu.org/licenses/gpl.html
21#
22###############################################################################
23# E-mail:      mbonetti at gmail dot com
24# Web page:    http://gregarius.net/
25#
26###############################################################################
27
28require_once('init.php');
29
30function getOpml($url) {
31    $arr = parse_weblogsDotCom($url);
32
33    if (!$arr) {
34        return false;
35    } else {
36        return $arr;
37    }
38}
39
40/**** mbi: from http://www.stargeek.com/php_scripts.php?script=20&cat=blog */
41function parse_weblogsDotCom($url) {
42    /*
43     Grab weblogs.com list of recently updated RSS feeds
44     $blogs is array() of feeds
45     NAME    name
46     URL        address
47     WHEN        seconds since ping
48     */
49
50    global $blogs,$folder,$inOpmlfolder, $inOpmlItem;
51    $folder = __('Root');
52   
53    $inOpmlfolder = $inOpmlItem = false;
54    $opml = getUrl($url);
55    $opml = str_replace("\r", '', $opml);
56    $opml = str_replace("\n", '', $opml);
57   
58    $xp = xml_parser_create() or xml_error("couldn't create parser");
59
60    xml_set_element_handler($xp, '_xml_startElement', '_xml_endElement') 
61      or xml_error("couldnt set XML handlers");
62   
63    xml_parse($xp, $opml, true) or xml_error("failed parsing xml at line ".xml_get_current_line_number().": " . xml_error_string());
64    xml_parser_free($xp) or xml_error("failed freeing the parser");
65    return $blogs;
66}
67
68function xml_error($error_msg){
69        rss_error($error_msg, RSS_ERROR_ERROR,true);
70        echo("\n</div>\n</body></html>\n");
71        die();
72}
73
74function _xml_startElement($xp, $element, $attr) {
75    global $blogs,$folder,$inOpmlfolder, $inOpmlItem;
76    if (strcasecmp('outline', $element)) {
77        return;
78    }
79    //$attr['__opml_folder__'] = $folder;
80    if (!array_key_exists('XMLURL',$attr) && (array_key_exists('TEXT',$attr)||array_key_exists('TITLE',$attr)) ) {
81                //some opml use title instead of text to define a folder (ex: newzcrawler)
82                $folder = $attr['TEXT']?$attr['TEXT']:$attr['TITLE'];
83                $inOpmlfolder = true;
84                $inOpmlItem = false;
85                //echo "start of folder $folder\n";
86    } else {   
87        $inOpmlItem = true;
88        //echo "start of item $folder/".$attr['TEXT']."\n";
89                if ($folder != '') {
90                $blogs[$folder][] = $attr;
91                } else {
92                    $blogs[] = $attr;
93                }
94    }
95}
96
97function _xml_endElement($xp, $element) {
98    global $blogs,$folder,$inOpmlfolder, $inOpmlItem;
99        if (strcasecmp( $element, "outline") === 0) {
100                if (!$inOpmlItem && $inOpmlfolder) {
101                        //echo "end of folder $folder\n";
102                        // end of folder element!
103                        $inOpmlfolder = false;
104                        $folder = __('Root');
105                } else {
106                        // end of item element
107                        $inOpmlItem = false;
108                        //echo "end of item\n";
109                }       
110        } 
111    return;
112}
113
114/** OPML Export                                                          */
115/** OPML2.0 RFC: http://techno-weenie.com/archives/2003/04/01/003067.php */
116/** OPML1.0 Specs: http://opml.scripting.com/spec                        */
117
118// This is a pretty lame opml 1.1 generation routine, in that it is not
119// recursive, but instead relies on the fact that we only have one level
120// of folders.
121// Output should be valid xml. (*fingers crossed*)
122if (array_key_exists('act',$_REQUEST)) {
123
124        $rs = rss_query( "select fid,tag from " .getTable('tag') ." t "
125                   ."inner join " .getTable('metatag') ." mt on mt.tid = t.id "
126                   ." where ttype='channel'");
127    $cats=array();
128    while(list($cid,$tag) = rss_fetch_row($rs)) {
129        if (!isset($cats[$cid])) {
130                $cats[$cid]=array();
131        }
132        $cats[$cid][]=$tag;
133    }
134   
135
136    $sql = "select "
137      ." c.id, c.title, c.url, c.siteurl, d.name, c.parent, c.descr "
138      ." from ". getTable("channels") . " c "
139      ." inner join " . getTable("folders") ." d on d.id = c.parent "
140          ." where not (c.mode & ".  RSS_MODE_DELETED_STATE .") ";
141     
142        if (hidePrivate()) {
143                $sql .=" and not(c.mode & " . RSS_MODE_PRIVATE_STATE .") ";           
144        }
145       
146        // note: should we export deprecated feeds?
147   
148    if (getConfig('rss.config.absoluteordering')) {
149                $sql .= " order by d.position asc, c.position asc";
150    } else {
151                $sql .=" order by d.name asc, c.title asc";
152    }
153   
154    $res = rss_query($sql);
155
156    $dateRes = rss_query("select max(dateadded) from " . getTable("channels"));
157    list($dateModif) = rss_fetch_row($dateRes);
158    $dateLabel = date("r", strtotime($dateModif));
159
160    header("Content-Type: text/xml");
161    echo "<?xml version=\"1.0\" encoding=\"" . getConfig('rss.output.encoding') . "\"?>\n"
162      ."<?xml-stylesheet type=\"text/xsl\" href=\"".getPath()."css/opml.xsl\"?>\n"
163      ."<!-- Generated by "._TITLE_. " " . _VERSION_ ." -->\n"           
164      ."<opml version=\"2.0\">\n";
165   
166    echo "\t<head>\n"
167      ."\t\t<title>"._TITLE_." OPML Feed</title>\n"
168      ."\t\t<dateModified>$dateLabel</dateModified>\n"
169      ."\t</head>\n"
170      ."\t<body>\n";
171
172    $prev_parent=0;
173    while (list($id, $title, $url, $siteurl, $name, $parent, $descr) = rss_fetch_row($res)) {
174                $descr_ = htmlspecialchars ($descr);
175                $descr_ = trim(preg_replace('/(\r\n|\r|\n)/', ' ', $descr_));
176               
177                $title_ = htmlspecialchars($title);
178               
179                $url_ = preg_replace('|(https?://)([^:]+:[^@]+@)(.+)$|','\1\3',$url);
180                $url_ = htmlspecialchars($url_);
181               
182                $siteurl_ = preg_replace('|(https?://)([^:]+:[^@]+@)(.+)$|','\1\3',$siteurl);
183                $siteurl_ = htmlspecialchars($siteurl_);
184               
185                $name_ = htmlspecialchars($name);
186       
187                if ($parent != $prev_parent) {
188                        if ($prev_parent != 0) {
189                                echo "\t\t</outline>\n";
190                        }
191                        $prev_parent = $parent;
192                        echo "\t\t<outline text=\"$name_\">\n";
193                }
194       
195                if ($parent > 0) {
196                  echo "\t";
197                }
198               
199                echo "\t\t<outline  text=\"$title_\" description=\"$descr_\" type=\"rss\"";
200                if ($siteurl != "") {
201                        echo " htmlUrl=\"$siteurl_\"";
202                }
203                if (isset($cats[$id]) && count($cats[$id])) {
204                        echo " category=\"" . implode(",",$cats[$id]) ."\"";
205                }
206                echo " xmlUrl=\"$url_\" />\n";
207    }
208
209    if ($prev_parent > 0) {
210                echo "\t</outline>\n";
211    }
212
213    echo "\t</body>\n</opml>\n";
214}
215?>
Note: See TracBrowser for help on using the browser.