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