]> git.wh0rd.org - tt-rss.git/blob - opml.php
code cleanup, test for db_escape() crazyness in DB sanity check
[tt-rss.git] / opml.php
1 <?php
2 error_reporting(E_ERROR | E_WARNING | E_PARSE);
3
4 require_once "sessions.php";
5 require_once "sanity_check.php";
6 require_once "functions.php";
7 require_once "config.php";
8 require_once "db.php";
9 require_once "db-prefs.php";
10
11 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
12
13 init_connection($link);
14 login_sequence($link);
15
16 $owner_uid = $_SESSION["uid"];
17
18 function opml_export($link, $owner_uid) {
19 header("Content-type: application/xml+opml");
20 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
21
22 print "<opml version=\"1.0\">";
23 print "<head>
24 <dateCreated>" . date("r", time()) . "</dateCreated>
25 <title>Tiny Tiny RSS Feed Export</title>
26 </head>";
27 print "<body>";
28
29 $cat_mode = false;
30
31 if (get_pref($link, 'ENABLE_FEED_CATS')) {
32 $cat_mode = true;
33 $result = db_query($link, "SELECT
34 title,feed_url,site_url,
35 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title
36 FROM ttrss_feeds
37 WHERE
38 owner_uid = '$owner_uid'
39 ORDER BY cat_title,title");
40 } else {
41 $result = db_query($link, "SELECT * FROM ttrss_feeds
42 WHERE owner_uid = '$owner_uid' ORDER BY title");
43 }
44
45 $old_cat_title = "";
46
47 while ($line = db_fetch_assoc($result)) {
48 $title = htmlspecialchars($line["title"]);
49 $url = htmlspecialchars($line["feed_url"]);
50 $site_url = htmlspecialchars($line["site_url"]);
51
52 if ($cat_mode) {
53 $cat_title = htmlspecialchars($line["cat_title"]);
54
55 if ($old_cat_title != $cat_title) {
56 if ($old_cat_title) {
57 print "</outline>\n";
58 }
59
60 if ($cat_title) {
61 print "<outline title=\"$cat_title\">\n";
62 }
63
64 $old_cat_title = $cat_title;
65 }
66 }
67
68 if ($site_url) {
69 $html_url_qpart = "htmlUrl=\"$site_url\"";
70 } else {
71 $html_url_qpart = "";
72 }
73
74 print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
75 }
76
77 if ($cat_mode && $old_cat_title) {
78 print "</outline>\n";
79 }
80
81 print "</body></opml>";
82 }
83
84 // FIXME there are some brackets issues here
85
86 $op = $_REQUEST["op"];
87
88 if (!$op) $op = "Export";
89
90 if ($op == "Export") {
91 return opml_export($link, $owner_uid);
92 }
93
94 if ($op == "Import") {
95
96 print "<html>
97 <head>
98 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
99 <title>".__("OPML Utility")."</title>
100 </head>
101 <body>
102 <div class=\"floatingLogo\"><img src=\"images/ttrss_logo.png\"></div>
103 <h1>".__('OPML Utility')."</h1>";
104
105 db_query($link, "BEGIN");
106
107 /* create Imported feeds category just in case */
108
109 $result = db_query($link, "SELECT id FROM
110 ttrss_feed_categories WHERE title = 'Imported feeds' AND
111 owner_uid = '$owner_uid' LIMIT 1");
112
113 if (db_num_rows($result) == 0) {
114 db_query($link, "INSERT INTO ttrss_feed_categories
115 (title,owner_uid)
116 VALUES ('Imported feeds', '$owner_uid')");
117 }
118
119 db_query($link, "COMMIT");
120
121 /* Handle OPML import by DOMXML/DOMDocument */
122
123 if (function_exists('domxml_open_file')) {
124 print "<p>".__("Importing OPML (using DOMXML extension)...")."</p>";
125 require_once "modules/opml_domxml.php";
126 opml_import_domxml($link, $owner_uid);
127 } else if (PHP_VERSION >= 5) {
128 print "<p>".__("Importing OPML (using DOMDocument extension)...")."</p>";
129 require_once "modules/opml_domdoc.php";
130 opml_import_domdoc($link, $owner_uid);
131 } else {
132 print_error(__("DOMXML extension is not found. It is required for PHP versions below 5."));
133 }
134
135 print "<br><form method=\"GET\" action=\"prefs.php\">
136 <input type=\"submit\" value=\"".__("Return to preferences")."\">
137 </form>";
138
139 print "</body></html>";
140
141 }
142
143 // if ($link) db_close($link);
144
145 ?>