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