]>
git.wh0rd.org - tt-rss.git/blob - opml.php
2 set_include_path(get_include_path() . PATH_SEPARATOR
.
3 dirname(__FILE__
) . "/include");
5 require_once "functions.php";
6 require_once "sessions.php";
7 require_once "sanity_check.php";
8 require_once "config.php";
10 require_once "db-prefs.php";
12 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
14 if (!init_connection($link)) return;
16 function opml_import_domdoc($link, $owner_uid) {
18 if (is_file($_FILES['opml_file']['tmp_name'])) {
19 $doc = DOMDocument
::load($_FILES['opml_file']['tmp_name']);
21 $result = db_query($link, "SELECT id FROM
22 ttrss_feed_categories WHERE title = 'Imported feeds' AND
23 owner_uid = '$owner_uid' LIMIT 1");
25 if (db_num_rows($result) == 1) {
26 $default_cat_id = db_fetch_result($result, 0, "id");
32 $body = $doc->getElementsByTagName('body');
34 $xpath = new DOMXpath($doc);
35 $query = "/opml/body//outline";
37 $outlines = $xpath->query($query);
39 foreach ($outlines as $outline) {
41 $attributes = $outline->attributes
;
43 $feed_title = db_escape_string($attributes->getNamedItem('text')->nodeValue
);
44 if (!$feed_title) $feed_title = db_escape_string($attributes->getNamedItem('title')->nodeValue
);
46 $cat_title = db_escape_string($attributes->getNamedItem('title')->nodeValue
);
47 if (!$cat_title) $cat_title = db_escape_string($attributes->getNamedItem('text')->nodeValue
);
49 $feed_url = db_escape_string($attributes->getNamedItem('xmlUrl')->nodeValue
);
50 if (!$feed_url) $feed_url = db_escape_string($attributes->getNamedItem('xmlURL')->nodeValue
);
52 $site_url = db_escape_string($attributes->getNamedItem('htmlUrl')->nodeValue
);
54 $pref_name = db_escape_string($attributes->getNamedItem('pref-name')->nodeValue
);
55 $label_name = db_escape_string($attributes->getNamedItem('label-name')->nodeValue
);
57 if ($cat_title && !$feed_url) {
59 if ($cat_title != "tt-rss-prefs" && $cat_title != 'tt-rss-labels') {
61 db_query($link, "BEGIN");
63 $result = db_query($link, "SELECT id FROM
64 ttrss_feed_categories WHERE title = '$cat_title' AND
65 owner_uid = '$owner_uid' LIMIT 1");
67 if (db_num_rows($result) == 0) {
69 printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title);
71 db_query($link, "INSERT INTO ttrss_feed_categories
73 VALUES ('$cat_title', '$owner_uid')");
76 db_query($link, "COMMIT");
80 // print "$active_category : $feed_title : $feed_url<br>";
83 $parent_node = $outline->parentNode
;
85 if ($parent_node && $parent_node->nodeName
== "outline") {
86 $cat_check = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
87 if ($cat_check == "tt-rss-prefs") {
88 $pref_value = db_escape_string($outline->attributes
->getNamedItem('value')->nodeValue
);
91 __("Setting preference key %s to %s")."</li>",
92 $pref_name, $pref_value);
94 set_pref($link, $pref_name, $pref_value);
101 $fg_color = db_escape_string($attributes->getNamedItem('label-fg-color')->nodeValue
);
102 $bg_color = db_escape_string($attributes->getNamedItem('label-bg-color')->nodeValue
);
104 if (!label_find_id($link, $label_name, $_SESSION['uid'])) {
106 printf("<li>".__("Adding label %s")."</li>", $label_name);
108 label_create($link, $label_name, $fg_color, $bg_color);
112 if (!$feed_title ||
!$feed_url) continue;
114 db_query($link, "BEGIN");
118 $parent_node = $outline->parentNode
;
120 if ($parent_node && $parent_node->nodeName
== "outline") {
121 $element_category = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
122 if (!$element_category) $element_category = $parent_node->attributes
->getNamedItem('text')->nodeValue
;
125 $element_category = '';
128 if ($element_category) {
130 $element_category = db_escape_string($element_category);
132 $result = db_query($link, "SELECT id FROM
133 ttrss_feed_categories WHERE title = '$element_category' AND
134 owner_uid = '$owner_uid' LIMIT 1");
136 if (db_num_rows($result) == 1) {
137 $cat_id = db_fetch_result($result, 0, "id");
141 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
142 feed_url = '$feed_url'
143 AND owner_uid = '$owner_uid'");
145 print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b>
146 (<a target='_blank' href=\"$feed_url\">rss</a>) ";
148 if (db_num_rows($result) > 0) {
149 print __('is already imported.');
153 $add_query = "INSERT INTO ttrss_feeds
154 (title, feed_url, owner_uid, cat_id, site_url) VALUES
155 ('$feed_title', '$feed_url', '$owner_uid',
156 '$cat_id', '$site_url')";
159 $add_query = "INSERT INTO ttrss_feeds
160 (title, feed_url, owner_uid, cat_id, site_url) VALUES
161 ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
167 db_query($link, $add_query);
174 db_query($link, "COMMIT");
178 print_error(__('Error while parsing document.'));
182 print_error(__('Error: please upload OPML file.'));
188 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
189 if (!$_REQUEST["debug"]) {
190 header("Content-type: application/xml+opml");
191 header("Content-Disposition: attachment; filename=" . $name );
193 header("Content-type: text/xml");
196 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
198 print "<opml version=\"1.0\">";
200 <dateCreated>" . date("r", time()) . "</dateCreated>
201 <title>Tiny Tiny RSS Feed Export</title>
207 $select = "SELECT * ";
208 $where = "WHERE owner_uid = '$owner_uid'";
209 $orderby = "ORDER BY title";
210 if ($hide_private_feeds){
211 $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
212 auth_login = '' AND auth_pass = ''";
217 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
220 title, feed_url, site_url,
221 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
222 $orderby = "ORDER BY cat_title, title";
226 $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
227 print "<!-- feeding cats is not enabled -->";
228 print "<!-- $cat_feed -->";
233 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
237 while ($line = db_fetch_assoc($result)) {
238 $title = htmlspecialchars($line["title"]);
239 $url = htmlspecialchars($line["feed_url"]);
240 $site_url = htmlspecialchars($line["site_url"]);
243 $cat_title = htmlspecialchars($line["cat_title"]);
245 if ($old_cat_title != $cat_title) {
246 if ($old_cat_title) {
247 print "</outline>\n";
251 print "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
254 $old_cat_title = $cat_title;
259 $html_url_qpart = "htmlUrl=\"$site_url\"";
261 $html_url_qpart = "";
264 print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
267 if ($cat_mode && $old_cat_title) {
268 print "</outline>\n";
271 # export tt-rss settings
273 if ($include_settings) {
274 print "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION
."\">";
276 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
277 profile IS NULL AND owner_uid = " . $_SESSION["uid"]);
279 while ($line = db_fetch_assoc($result)) {
281 $name = $line["pref_name"];
282 $value = htmlspecialchars($line["value"]);
284 print "<outline pref-name=\"$name\" value=\"$value\">";
292 print "<outline title=\"tt-rss-labels\">";
294 $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
295 owner_uid = " . $_SESSION['uid']);
297 while ($line = db_fetch_assoc($result)) {
298 $name = htmlspecialchars($line['caption']);
299 $fg_color = htmlspecialchars($line['fg_color']);
300 $bg_color = htmlspecialchars($line['bg_color']);
302 print "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
308 print "<outline title=\"tt-rss-filters\">";
310 $result = db_query($link, "SELECT filter_type,
318 ttrss_feeds.title AS feed_title,
319 ttrss_feed_categories.title AS cat_title
321 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
322 LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
324 ttrss_filters.owner_uid = " . $_SESSION['uid']);
326 while ($line = db_fetch_assoc($result)) {
327 $name = htmlspecialchars($line['reg_exp']);
329 foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
330 $line[$b] = sql_bool_to_bool($line[$b]);
333 $filter = json_encode($line);
335 print "<outline filter-name=\"$name\">$filter</outline>";
343 print "</body></opml>";
346 // FIXME there are some brackets issues here
348 $op = $_REQUEST["op"];
349 if (!$op) $op = "Export";
351 $output_name = $_REQUEST["filename"];
352 if (!$output_name) $output_name = "TinyTinyRSS.opml";
354 $show_settings = $_REQUEST["settings"];
356 if ($op == "Export") {
358 login_sequence($link);
359 $owner_uid = $_SESSION["uid"];
360 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
363 if ($op == "publish"){
364 $key = db_escape_string($_REQUEST["key"]);
366 $result = db_query($link, "SELECT owner_uid
367 FROM ttrss_access_keys WHERE
368 access_key = '$key' AND feed_id = 'OPML:Publish'");
370 if (db_num_rows($result) == 1) {
371 $owner_uid = db_fetch_result($result, 0, "owner_uid");
372 return opml_export($link, "", $owner_uid, true, false);
374 print "<error>User not found</error>";
378 if ($op == "Import") {
380 login_sequence($link);
381 $owner_uid = $_SESSION["uid"];
383 header('Content-Type: text/html; charset=utf-8');
387 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
388 <title>".__("OPML Utility")."</title>
389 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
392 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
393 <h1>".__('OPML Utility')."</h1>";
395 db_query($link, "BEGIN");
397 /* create Imported feeds category just in case */
399 $result = db_query($link, "SELECT id FROM
400 ttrss_feed_categories WHERE title = 'Imported feeds' AND
401 owner_uid = '$owner_uid' LIMIT 1");
403 if (db_num_rows($result) == 0) {
404 db_query($link, "INSERT INTO ttrss_feed_categories
406 VALUES ('Imported feeds', '$owner_uid')");
409 db_query($link, "COMMIT");
411 print "<p>".__("Importing OPML...")."</p>";
412 opml_import_domdoc($link, $owner_uid);
414 print "<br><form method=\"GET\" action=\"prefs.php\">
415 <input type=\"submit\" value=\"".__("Return to preferences")."\">
418 print "</body></html>";
422 // if ($link) db_close($link);