]>
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
);
56 $filter_name = db_escape_string($attributes->getNamedItem('filter-name')->nodeValue
);
58 if ($cat_title && !$feed_url) {
60 if ($cat_title != "tt-rss-prefs" && $cat_title != 'tt-rss-labels' && $cat_title != 'tt-rss-filters') {
62 db_query($link, "BEGIN");
64 $result = db_query($link, "SELECT id FROM
65 ttrss_feed_categories WHERE title = '$cat_title' AND
66 owner_uid = '$owner_uid' LIMIT 1");
68 if (db_num_rows($result) == 0) {
70 printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title);
72 db_query($link, "INSERT INTO ttrss_feed_categories
74 VALUES ('$cat_title', '$owner_uid')");
77 db_query($link, "COMMIT");
81 // print "$active_category : $feed_title : $feed_url<br>";
84 $parent_node = $outline->parentNode
;
86 if ($parent_node && $parent_node->nodeName
== "outline") {
87 $cat_check = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
88 if ($cat_check == "tt-rss-prefs") {
89 $pref_value = db_escape_string($outline->attributes
->getNamedItem('value')->nodeValue
);
92 __("Setting preference key %s to %s")."</li>",
93 $pref_name, $pref_value);
95 set_pref($link, $pref_name, $pref_value);
102 $parent_node = $outline->parentNode
;
104 if ($parent_node && $parent_node->nodeName
== "outline") {
105 $cat_check = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
106 if ($cat_check == "tt-rss-labels") {
108 $fg_color = db_escape_string($attributes->getNamedItem('label-fg-color')->nodeValue
);
109 $bg_color = db_escape_string($attributes->getNamedItem('label-bg-color')->nodeValue
);
111 if (!label_find_id($link, $label_name, $_SESSION['uid'])) {
112 printf("<li>".__("Adding label %s")."</li>", htmlspecialchars($label_name));
113 label_create($link, $label_name, $fg_color, $bg_color);
115 printf("<li>".__("Duplicate label: %s")."</li>",
116 htmlspecialchars($label_name));
123 $parent_node = $outline->parentNode
;
125 if ($parent_node && $parent_node->nodeName
== "outline") {
126 $cat_check = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
127 if ($cat_check == "tt-rss-filters") {
128 $filter = json_decode($outline->nodeValue
, true);
131 $reg_exp = db_escape_string($filter['reg_exp']);
132 $filter_type = (int)$filter['filter_type'];
133 $action_id = (int)$filter['action_id'];
135 $result = db_query($link, "SELECT id FROM ttrss_filters WHERE
136 reg_exp = '$reg_exp' AND
137 filter_type = '$filter_type' AND
138 action_id = '$action_id' AND
139 owner_uid = " .$_SESSION['uid']);
141 if (db_num_rows($result) == 0) {
142 $enabled = bool_to_sql_bool($filter['enabled']);
143 $action_param = db_escape_string($filter['action_param']);
144 $inverse = bool_to_sql_bool($filter['inverse']);
145 $filter_param = db_escape_string($filter['filter_param']);
146 $cat_filter = bool_to_sql_bool($filter['cat_filter']);
148 $feed_url = db_escape_string($filter['feed_url']);
149 $cat_title = db_escape_string($filter['cat_title']);
151 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
152 feed_url = '$feed_url' AND owner_uid = ".$_SESSION['uid']);
154 if (db_num_rows($result) != 0) {
155 $feed_id = db_fetch_result($result, 0, "id");
160 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
161 title = '$cat_title' AND owner_uid = ".$_SESSION['uid']);
163 if (db_num_rows($result) != 0) {
164 $cat_id = db_fetch_result($result, 0, "id");
169 printf("<li>".__("Adding filter %s")."</li>", htmlspecialchars($reg_exp));
171 $query = "INSERT INTO ttrss_filters (filter_type, action_id,
172 enabled, inverse, action_param, filter_param,
176 VALUES ($filter_type, $action_id,
177 $enabled, $inverse, '$action_param', '$filter_param',
178 $cat_filter, $feed_id,
179 $cat_id, '$reg_exp', ".
180 $_SESSION['uid'].")";
182 db_query($link, $query);
185 printf("<li>".__("Duplicate filter %s")."</li>", htmlspecialchars($reg_exp));
193 if (!$feed_title ||
!$feed_url) continue;
195 db_query($link, "BEGIN");
199 $parent_node = $outline->parentNode
;
201 if ($parent_node && $parent_node->nodeName
== "outline") {
202 $element_category = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
203 if (!$element_category) $element_category = $parent_node->attributes
->getNamedItem('text')->nodeValue
;
206 $element_category = '';
209 if ($element_category) {
211 $element_category = db_escape_string($element_category);
213 $result = db_query($link, "SELECT id FROM
214 ttrss_feed_categories WHERE title = '$element_category' AND
215 owner_uid = '$owner_uid' LIMIT 1");
217 if (db_num_rows($result) == 1) {
218 $cat_id = db_fetch_result($result, 0, "id");
222 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
223 feed_url = '$feed_url'
224 AND owner_uid = '$owner_uid'");
226 print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b>
227 (<a target='_blank' href=\"$feed_url\">rss</a>) ";
229 if (db_num_rows($result) > 0) {
230 print __('is already imported.');
234 $add_query = "INSERT INTO ttrss_feeds
235 (title, feed_url, owner_uid, cat_id, site_url) VALUES
236 ('$feed_title', '$feed_url', '$owner_uid',
237 '$cat_id', '$site_url')";
240 $add_query = "INSERT INTO ttrss_feeds
241 (title, feed_url, owner_uid, cat_id, site_url) VALUES
242 ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
248 db_query($link, $add_query);
255 db_query($link, "COMMIT");
259 print_error(__('Error while parsing document.'));
263 print_error(__('Error: please upload OPML file.'));
269 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
270 if (!$_REQUEST["debug"]) {
271 header("Content-type: application/xml+opml");
272 header("Content-Disposition: attachment; filename=" . $name );
274 header("Content-type: text/xml");
277 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
279 $out .= "<opml version=\"1.0\">";
281 <dateCreated>" . date("r", time()) . "</dateCreated>
282 <title>Tiny Tiny RSS Feed Export</title>
288 $select = "SELECT * ";
289 $where = "WHERE owner_uid = '$owner_uid'";
290 $orderby = "ORDER BY order_id, title";
291 if ($hide_private_feeds){
292 $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
293 auth_login = '' AND auth_pass = ''";
298 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
301 title, feed_url, site_url, order_id,
302 (SELECT order_id FROM ttrss_feed_categories WHERE id = cat_id) AS cat_order_id,
303 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
304 $orderby = "ORDER BY cat_order_id, cat_title, order_id, title";
308 $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
309 $out .= "<!-- feeding cats is not enabled -->";
310 $out .= "<!-- $cat_feed -->";
315 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
319 while ($line = db_fetch_assoc($result)) {
320 $title = htmlspecialchars($line["title"]);
321 $url = htmlspecialchars($line["feed_url"]);
322 $site_url = htmlspecialchars($line["site_url"]);
325 $cat_title = htmlspecialchars($line["cat_title"]);
327 if ($old_cat_title != $cat_title) {
328 if ($old_cat_title) {
329 $out .= "</outline>\n";
333 $out .= "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
336 $old_cat_title = $cat_title;
341 $html_url_qpart = "htmlUrl=\"$site_url\"";
343 $html_url_qpart = "";
346 $out .= "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
349 if ($cat_mode && $old_cat_title) {
350 $out .= "</outline>\n";
353 # export tt-rss settings
355 if ($include_settings) {
356 $out .= "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION
."\">";
358 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
359 profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
361 while ($line = db_fetch_assoc($result)) {
363 $name = $line["pref_name"];
364 $value = htmlspecialchars($line["value"]);
366 $out .= "<outline pref-name=\"$name\" value=\"$value\">";
368 $out .= "</outline>";
372 $out .= "</outline>";
374 $out .= "<outline title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION
."\">";
376 $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
377 owner_uid = " . $_SESSION['uid']);
379 while ($line = db_fetch_assoc($result)) {
380 $name = htmlspecialchars($line['caption']);
381 $fg_color = htmlspecialchars($line['fg_color']);
382 $bg_color = htmlspecialchars($line['bg_color']);
384 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
388 $out .= "</outline>";
390 $out .= "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION
."\">";
392 $result = db_query($link, "SELECT filter_type,
400 ttrss_feeds.feed_url AS feed_url,
401 ttrss_feed_categories.title AS cat_title
403 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
404 LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
406 ttrss_filters.owner_uid = " . $_SESSION['uid']);
408 while ($line = db_fetch_assoc($result)) {
409 $name = htmlspecialchars($line['reg_exp']);
411 foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
412 $line[$b] = sql_bool_to_bool($line[$b]);
415 $filter = json_encode($line);
417 $out .= "<outline filter-name=\"$name\">$filter</outline>";
422 $out .= "</outline>";
425 $out .= "</body></opml>";
428 $doc = new DOMDocument();
429 $doc->formatOutput
= true;
430 $doc->preserveWhiteSpace
= false;
432 $res = $doc->saveXML();
434 // saveXML uses a two-space indent. Change to tabs.
435 $res = preg_replace_callback('/^(?: )+/mu',
438 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
444 // FIXME there are some brackets issues here
446 $op = $_REQUEST["op"];
447 if (!$op) $op = "Export";
449 $output_name = $_REQUEST["filename"];
450 if (!$output_name) $output_name = "TinyTinyRSS.opml";
452 $show_settings = $_REQUEST["settings"];
454 if ($op == "Export") {
456 login_sequence($link);
457 $owner_uid = $_SESSION["uid"];
458 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
461 if ($op == "publish"){
462 $key = db_escape_string($_REQUEST["key"]);
464 $result = db_query($link, "SELECT owner_uid
465 FROM ttrss_access_keys WHERE
466 access_key = '$key' AND feed_id = 'OPML:Publish'");
468 if (db_num_rows($result) == 1) {
469 $owner_uid = db_fetch_result($result, 0, "owner_uid");
470 return opml_export($link, "", $owner_uid, true, false);
472 print "<error>User not found</error>";
476 if ($op == "Import") {
478 login_sequence($link);
479 $owner_uid = $_SESSION["uid"];
481 header('Content-Type: text/html; charset=utf-8');
485 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
486 <title>".__("OPML Utility")."</title>
487 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
490 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
491 <h1>".__('OPML Utility')."</h1>";
493 db_query($link, "BEGIN");
495 /* create Imported feeds category just in case */
497 $result = db_query($link, "SELECT id FROM
498 ttrss_feed_categories WHERE title = 'Imported feeds' AND
499 owner_uid = '$owner_uid' LIMIT 1");
501 if (db_num_rows($result) == 0) {
502 db_query($link, "INSERT INTO ttrss_feed_categories
504 VALUES ('Imported feeds', '$owner_uid')");
507 db_query($link, "COMMIT");
509 print "<p>".__("Importing OPML...")."</p>";
510 opml_import_domdoc($link, $owner_uid);
512 print "<br><form method=\"GET\" action=\"prefs.php\">
513 <input type=\"submit\" value=\"".__("Return to preferences")."\">
516 print "</body></html>";
520 // if ($link) db_close($link);