root/trunk/gregarius/api.php

Revision 1629, 7.4 kB (checked in by mbonetti, 2 years ago)

403 Error message, updated Vietnamese, tiny changes to Admin, API

  • Property svn:eolstyle set to native
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
28 require_once('core.php');
29 rss_bootstrap();
30 require_once('init.php');
31
32 if (isset($_REQUEST['method'])) {
33     switch ($_REQUEST['method']) {
34     case 'update':
35             $uc = getUnreadCount(null,null);
36         die("|$uc||");
37         break;
38     case 'listsubs':
39         blOPML();
40         break;
41     case 'getitems':
42         $cid = (isset($_REQUEST['s'])?$_REQUEST['s']:null);
43         $date = (isset($_REQUEST['d'])?$_REQUEST['d']:null);
44         $markread = (isset($_REQUEST['n']) && $_REQUEST['n'] == '1');
45
46         $cid = sanitize($cid,RSS_SANITIZER_NUMERIC);
47         $date = sanitize($date,RSS_SANITIZER_NUMERIC);
48
49         blGetItems($cid,$date,$markread);
50         break;
51     case 'search':
52         rss_require('extlib/JSON.php');
53         $json = new Services_JSON();
54         $query = preg_replace('#[^a-z0-9\s]#','',@$_REQUEST['q']);
55         if ($query) {
56             $res = osSearch($query);
57         } else {
58             $res = array($query,array(),array(),array());
59         }
60         header('Content-Type: text/plain');
61         die ($json->encode($res));
62         break;
63     }
64
65 }
66
67
68 function blOPML() {
69     // Unread count
70     $ucres = rss_query ("select cid, count(*) from " .getTable("item")
71                         ." where unread & "  . RSS_MODE_UNREAD_STATE
72                         . " and not(unread & " . RSS_MODE_DELETED_STATE .") group by cid");
73     $uc = array();
74     while (list($uccid,$ucuc) = rss_fetch_row($ucres)) {
75         $uc[$uccid]=$ucuc;
76     }
77
78
79     $sql = "select "
80            ." c.id, c.title, c.url, c.siteurl, f.name "
81            ." from ".getTable("channels")." c "
82            ." inner join " . getTable("folders")." f "
83            ."  on f.id = c.parent";
84
85     if (hidePrivate()) {
86         $sql .= " and not(c.mode & ".RSS_MODE_PRIVATE_STATE.") ";
87     }
88
89     $sql .= " and not(c.mode & ".RSS_MODE_DELETED_STATE.") ";
90
91     if (getConfig('rss.config.absoluteordering')) {
92         $sql .= " order by f.position asc, c.position asc";
93     } else {
94         $sql .= " order by f.name, c.title asc";
95     }
96     $folders = array();
97     $res = rss_query($sql);
98     while (list($cid,$title,$xmlUrl,$siteUrl,$folder) = rss_fetch_row($res)) {
99         if (!isset($folders[$folder])) {
100             $folders[$folder] = array();
101         }
102         $folders[$folder][] = array(
103                                   'title' => htmlspecialchars($title),
104                                   'htmlUrl' => htmlspecialchars($siteUrl),
105                                   'xmlUrl' => htmlspecialchars($xmlUrl),
106                                   'BloglinesUnread'=>(isset($uc[$cid]) ? $uc[$cid]:0),
107                                   'BloglinesSubId'=>$cid
108                               );
109     }
110
111     header('Content-Type: text/xml; charset=utf-8');
112     echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
113     echo "<opml version=\"1.0\">\n"
114     ."<head>\n"
115     ."\t<title>Gregarius Subscriptions</title>\n"
116     ."\t<dateCreated>". date('D, j M Y H:i:s \G\M\T') ."</dateCreated>\n"
117     ."\t<ownerName></ownerName>\n"
118     ."</head>\n"
119     ."<body>\n";
120
121     foreach ($folders as $fname => $folder) {
122         echo "\t<outline title=\"$fname\">\n";
123         foreach ($folder as $feed) {
124             echo "\t\t<outline type=\"rss\"";
125             foreach ($feed as $key => $value) {
126                 echo " $key=\"$value\"";
127             }
128             echo " />\n";
129         }
130         echo "\t</outline>\n";
131     }
132     echo "</body>\n"
133     ."</opml>\n";
134
135 }
136
137 function blGetItems($cid,$date,$markread) {
138     if (hidePrivate()) {
139         header('HTTP/1.x 401 Not Authorized');
140         exit();
141     }
142
143     if (!$cid) {
144         header ('HTTP/1.x 403 Forbidden');
145         exit();
146     }
147
148     $sql = "select i.title as ititle, i.description as idescr, c.title as ctitle, "
149            ." c.descr as cdescr, c.url as curl, i.author as iauth, i.url as iurl, "
150            ." unix_timestamp(ifnull(i.pubdate, i.added)) as idate ,i.id as iid"
151            ." from ".getTable('item')." i "
152            ." inner join " .getTable('channels') ." c "
153            ."  on c.id = i.cid "
154            ." where i.unread & ". RSS_MODE_UNREAD_STATE ." and c.id=$cid";
155
156     if ($date) {
157         $sql .= " and ifnull(i.pubdate, i.added) > $date ";
158     }
159     $rs = rss_query($sql);
160
161     if (rss_num_rows($rs) == 0) {
162         header('HTTP/1.x 304 Not Modified');
163         exit();
164     }
165     $ids = array();
166     header('Content-Type: text/xml; charset=utf-8');
167     $hdr = false;
168     while($row=rss_fetch_assoc($rs)) {
169         if (!$hdr) {
170             $hdr = true;
171             echo "<" ."?xml version=\"1.0\"?" .">\n"
172             ."<rss version=\"2.0\"\n"
173             ."xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n"
174             ."xmlns:bloglines=\"http://www.bloglines.com/services/module\"\n"
175             ."xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
176
177             ."<channel>\n"
178             ."\t<title>".htmlspecialchars($row['ctitle'])."</title>\n"
179             ."\t<link>".htmlspecialchars($row['curl'])."</link>\n"
180             ."\t<description>".htmlspecialchars($row['cdescr'])."</description>\n"
181
182             ."\t<language>en-us</language>\n"
183             ."\t<webMaster>support@bloglines.com</webMaster>\n"
184             //."\t<bloglines:siteid>66</bloglines:siteid>\n"
185             ;
186         }
187
188         $ids[] = $row['iid'];
189
190         echo "\t<item>\n"
191         ."\t\t<title>".htmlspecialchars($row['ititle'])."</title>\n"
192         ."\t\t<dc:creator>".htmlspecialchars($row['iauth'])."</dc:creator>\n"
193         ."\t\t<guid isPermaLink=\"true\">".htmlspecialchars($row['iurl'])."</guid>\n"
194         ."\t\t<link>".htmlspecialchars($row['iurl'])."</link>\n"
195         ."\t\t<description><![CDATA[".$row['idescr']."]]></description>\n"
196         ."\t\t<pubDate>".date('D, j M Y H:i:s \G\M\T',$row['idate'])."</pubDate>\n"
197         ."\t\t<bloglines:itemid>".$row['iid']."</bloglines:itemid>\n"
198         ."\t</item>\n";
199     }
200     echo "</channel>\n</rss>\n";
201
202     if ($markread) {
203         $sql = "update ".getTable('item')." set unread = unread & " .SET_MODE_READ_STATE
204                ." where id in (" . implode(',',$ids) .")";
205         rss_query($sql);
206         rss_invalidate_cache();
207     }
208
209 }
210
211 function osSearch($q) {
212     rss_require('cls/search.php');
213     $sil = new SearchItemList($q,5);
214     $results = array();
215     foreach($sil -> feeds as $feed) {
216         foreach($feed -> items as $item) {
217             $results[] = $item -> title;
218         }
219     }
220     return array($q,$results,array(),array());
221 }
222 ?>
Note: See TracBrowser for help on using the browser.