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