]> git.wh0rd.org Git - tt-rss.git/blob - opml.php
last minute PG fixes
[tt-rss.git] / opml.php
1 <?
2         // FIXME there are some brackets issues here
3
4         $op = $_REQUEST["op"];
5         if ($op == "Export") {
6                 header("Content-type: application/xml");
7                 print "<?xml version=\"1.0\"?>";
8         }
9
10         require_once "config.php";
11         require_once "db.php";
12
13         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
14
15         if (DB_TYPE == "pgsql") {
16                 pg_query($link, "set client_encoding = 'utf-8'");
17         }
18
19         if ($op == "Export") {
20                 print "<opml version=\"1.0\">";
21                 print "<head><dateCreated>" . date("r", time()) . "</dateCreated></head>"; 
22                 print "<body>";
23
24                 $result = db_query($link, "SELECT * FROM ttrss_feeds ORDER BY title");
25
26                 while ($line = db_fetch_assoc($result)) {
27                         $title = $line["title"];
28                         $url = $line["feed_url"];
29
30                         print "<outline text=\"$title\" xmlUrl=\"$url\"/>";
31                 }
32
33                 print "</body></opml>";
34         }
35
36         function startElement($parser, $name, $attrs) {
37
38                 if ($name == "OUTLINE") {
39                         $title = db_escape_string($attrs['TEXT']);
40                         $url = db_escape_string($attrs['XMLURL']);
41
42                         if (!$title || !$url) return;
43
44                         print "Feed <b>$title</b> ($url)... ";
45
46                         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
47
48                         $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
49                                 title = '$title' OR feed_url = '$url'");
50
51                         if ($result && db_num_rows($result) > 0) {
52                                 
53                                 print " Already imported.<br>";
54
55                         } else {
56                                           
57                                 $result = db_query($link, "INSERT INTO ttrss_feeds (title, feed_url) VALUES
58                                         ('$title', '$url')");
59
60                                 print "<b>Done.</b><br>";
61
62                         }
63
64                 }
65         }
66
67         function endElement($parser, $name) {
68
69
70         }
71
72         if ($op == "Import") {
73
74                 print "<html>
75                         <head>
76                                 <link rel=\"stylesheet\" href=\"opml.css\" type=\"text/css\">
77                         </head>
78                         <body><h1>Importing OPML...</h1>
79                         <div>";
80
81                 if (WEB_DEMO_MODE) {
82                         print "OPML import is disabled in demo-mode.";
83                         print "<p><a class=\"button\" href=\"prefs.php\">
84                         Return to preferences</a></div></body></html>";
85
86                         return;
87                 }
88
89                 if (is_file($_FILES['opml_file']['tmp_name'])) {
90                  
91                         $xml_parser = xml_parser_create();
92
93                         xml_set_element_handler($xml_parser, "startElement", "endElement");
94
95                         $fp = fopen($_FILES['opml_file']['tmp_name'], "r");
96
97                         if ($fp) {
98
99                                 while ($data = fread($fp, 4096)) {
100
101                                         if (!xml_parse($xml_parser, $data, feof($fp))) {
102                                                 
103                                                 print sprintf("Unable to parse OPML file, XML error: %s at line %d",
104                                                         xml_error_string(xml_get_error_code($xml_parser)),
105                                                         xml_get_current_line_number($xml_parser));
106
107                                                 print "<p><a class=\"button\" href=\"prefs.php\">
108                                                         Return to preferences</a>";
109
110                                                 return;
111
112                                         }
113                                 }
114
115                                 xml_parser_free($xml_parser);
116                                 fclose($fp);
117
118                         } else {
119                                 print("Error: Could not open OPML input.");
120                         }
121
122                 } else {        
123                         print "Error: please upload OPML file.";
124                 }
125
126                 print "<p><a class=\"button\" href=\"prefs.php\">
127                         Return to preferences</a>";
128
129                 print "</div></body></html>";
130
131         }
132
133         db_close($link);
134
135 ?>