]>
git.wh0rd.org - tt-rss.git/blob - classes/opml.php
2 class Opml
extends Handler_Protected
{
4 function csrf_ignore($method) {
5 $csrf_ignored = array("export", "import");
7 return array_search($method, $csrf_ignored) !== false;
11 $output_name = $_REQUEST["filename"];
12 if (!$output_name) $output_name = "TinyTinyRSS.opml";
14 $show_settings = $_REQUEST["settings"];
16 $owner_uid = $_SESSION["uid"];
17 return $this->opml_export($output_name, $owner_uid, false, ($show_settings == 1));
21 $owner_uid = $_SESSION["uid"];
23 header('Content-Type: text/html; charset=utf-8');
27 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
28 <title>".__("OPML Utility")."</title>
29 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
32 <div class=\"floatingLogo\"><img src=\"images/logo_small.png\"></div>
33 <h1>".__('OPML Utility')."</h1><div class='content'>";
35 add_feed_category("Imported feeds");
37 $this->opml_notice(__("Importing OPML..."));
38 $this->opml_import($owner_uid);
40 print "<br><form method=\"GET\" action=\"prefs.php\">
41 <input type=\"submit\" value=\"".__("Return to preferences")."\">
44 print "</div></body></html>";
51 private function opml_export_category($owner_uid, $cat_id, $hide_private_feeds=false) {
54 $cat_qpart = "parent_cat = '$cat_id'";
55 $feed_cat_qpart = "cat_id = '$cat_id'";
57 $cat_qpart = "parent_cat IS NULL";
58 $feed_cat_qpart = "cat_id IS NULL";
61 if ($hide_private_feeds)
62 $hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')";
69 $result = db_query("SELECT title FROM ttrss_feed_categories WHERE id = '$cat_id'
70 AND owner_uid = '$owner_uid'");
71 $cat_title = htmlspecialchars(db_fetch_result($result, 0, "title"));
74 if ($cat_title) $out .= "<outline text=\"$cat_title\">\n";
76 $result = db_query("SELECT id,title
77 FROM ttrss_feed_categories WHERE
78 $cat_qpart AND owner_uid = '$owner_uid' ORDER BY order_id, title");
80 while ($line = db_fetch_assoc($result)) {
81 $title = htmlspecialchars($line["title"]);
82 $out .= $this->opml_export_category($owner_uid, $line["id"], $hide_private_feeds);
85 $feeds_result = db_query("select title, feed_url, site_url
86 from ttrss_feeds where $feed_cat_qpart AND owner_uid = '$owner_uid' AND $hide_qpart
87 order by order_id, title");
89 while ($fline = db_fetch_assoc($feeds_result)) {
90 $title = htmlspecialchars($fline["title"]);
91 $url = htmlspecialchars($fline["feed_url"]);
92 $site_url = htmlspecialchars($fline["site_url"]);
95 $html_url_qpart = "htmlUrl=\"$site_url\"";
100 $out .= "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
103 if ($cat_title) $out .= "</outline>\n";
108 function opml_export($name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
109 if (!$owner_uid) return;
111 if (!isset($_REQUEST["debug"])) {
112 header("Content-type: application/xml+opml");
113 header("Content-Disposition: attachment; filename=" . $name );
115 header("Content-type: text/xml");
118 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
120 $out .= "<opml version=\"1.0\">";
122 <dateCreated>" . date("r", time()) . "</dateCreated>
123 <title>Tiny Tiny RSS Feed Export</title>
127 $out .= $this->opml_export_category($owner_uid, false, $hide_private_feeds);
129 # export tt-rss settings
131 if ($include_settings) {
132 $out .= "<outline text=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION
."\">";
134 $result = db_query("SELECT pref_name, value FROM ttrss_user_prefs WHERE
135 profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
137 while ($line = db_fetch_assoc($result)) {
138 $name = $line["pref_name"];
139 $value = htmlspecialchars($line["value"]);
141 $out .= "<outline pref-name=\"$name\" value=\"$value\"/>";
144 $out .= "</outline>";
146 $out .= "<outline text=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION
."\">";
148 $result = db_query("SELECT * FROM ttrss_labels2 WHERE
149 owner_uid = " . $_SESSION['uid']);
151 while ($line = db_fetch_assoc($result)) {
152 $name = htmlspecialchars($line['caption']);
153 $fg_color = htmlspecialchars($line['fg_color']);
154 $bg_color = htmlspecialchars($line['bg_color']);
156 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
160 $out .= "</outline>";
162 $out .= "<outline text=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION
."\">";
164 $result = db_query("SELECT * FROM ttrss_filters2
165 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY id");
167 while ($line = db_fetch_assoc($result)) {
168 foreach (array('enabled', 'match_any_rule') as $b) {
169 $line[$b] = sql_bool_to_bool($line[$b]);
172 $line["rules"] = array();
173 $line["actions"] = array();
175 $tmp_result = db_query("SELECT * FROM ttrss_filters2_rules
176 WHERE filter_id = ".$line["id"]);
178 while ($tmp_line = db_fetch_assoc($tmp_result)) {
179 unset($tmp_line["id"]);
180 unset($tmp_line["filter_id"]);
182 $cat_filter = sql_bool_to_bool($tmp_line["cat_filter"]);
184 if ($cat_filter && $tmp_line["cat_id"] ||
$tmp_line["feed_id"]) {
185 $tmp_line["feed"] = getFeedTitle(
186 $cat_filter ?
$tmp_line["cat_id"] : $tmp_line["feed_id"],
189 $tmp_line["feed"] = "";
192 $tmp_line["cat_filter"] = sql_bool_to_bool($tmp_line["cat_filter"]);
194 unset($tmp_line["feed_id"]);
195 unset($tmp_line["cat_id"]);
197 array_push($line["rules"], $tmp_line);
200 $tmp_result = db_query("SELECT * FROM ttrss_filters2_actions
201 WHERE filter_id = ".$line["id"]);
203 while ($tmp_line = db_fetch_assoc($tmp_result)) {
204 unset($tmp_line["id"]);
205 unset($tmp_line["filter_id"]);
207 array_push($line["actions"], $tmp_line);
211 unset($line["owner_uid"]);
212 $filter = json_encode($line);
214 $out .= "<outline filter-type=\"2\"><![CDATA[$filter]]></outline>";
219 $out .= "</outline>";
222 $out .= "</body></opml>";
225 $doc = new DOMDocument();
226 $doc->formatOutput
= true;
227 $doc->preserveWhiteSpace
= false;
230 $xpath = new DOMXpath($doc);
231 $outlines = $xpath->query("//outline[@title]");
233 // cleanup empty categories
234 foreach ($outlines as $node) {
235 if ($node->getElementsByTagName('outline')->length
== 0)
236 $node->parentNode
->removeChild($node);
239 $res = $doc->saveXML();
241 /* // saveXML uses a two-space indent. Change to tabs.
242 $res = preg_replace_callback('/^(?: )+/mu',
245 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
253 private function opml_import_feed($doc, $node, $cat_id, $owner_uid) {
254 $attrs = $node->attributes
;
256 $feed_title = db_escape_string(mb_substr($attrs->getNamedItem('text')->nodeValue
, 0, 250));
257 if (!$feed_title) $feed_title = db_escape_string(mb_substr($attrs->getNamedItem('title')->nodeValue
, 0, 250));
259 $feed_url = db_escape_string(mb_substr($attrs->getNamedItem('xmlUrl')->nodeValue
, 0, 250));
260 if (!$feed_url) $feed_url = db_escape_string(mb_substr($attrs->getNamedItem('xmlURL')->nodeValue
, 0, 250));
262 $site_url = db_escape_string(mb_substr($attrs->getNamedItem('htmlUrl')->nodeValue
, 0, 250));
264 if ($feed_url && $feed_title) {
265 $result = db_query("SELECT id FROM ttrss_feeds WHERE
266 feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
268 if (db_num_rows($result) == 0) {
269 #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id");
270 $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title));
272 if (!$cat_id) $cat_id = 'NULL';
274 $query = "INSERT INTO ttrss_feeds
275 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
276 ('$feed_title', '$feed_url', '$owner_uid',
277 $cat_id, '$site_url', 0)";
281 $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title));
286 private function opml_import_label($doc, $node, $owner_uid) {
287 $attrs = $node->attributes
;
288 $label_name = db_escape_string($attrs->getNamedItem('label-name')->nodeValue
);
291 $fg_color = db_escape_string($attrs->getNamedItem('label-fg-color')->nodeValue
);
292 $bg_color = db_escape_string($attrs->getNamedItem('label-bg-color')->nodeValue
);
294 if (!label_find_id($label_name, $_SESSION['uid'])) {
295 $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name)));
296 label_create($label_name, $fg_color, $bg_color, $owner_uid);
298 $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)));
303 private function opml_import_preference($doc, $node, $owner_uid) {
304 $attrs = $node->attributes
;
305 $pref_name = db_escape_string($attrs->getNamedItem('pref-name')->nodeValue
);
308 $pref_value = db_escape_string($attrs->getNamedItem('value')->nodeValue
);
310 $this->opml_notice(T_sprintf("Setting preference key %s to %s",
311 $pref_name, $pref_value));
313 set_pref($pref_name, $pref_value);
317 private function opml_import_filter($doc, $node, $owner_uid) {
318 $attrs = $node->attributes
;
320 $filter_type = db_escape_string($attrs->getNamedItem('filter-type')->nodeValue
);
322 if ($filter_type == '2') {
323 $filter = json_decode($node->nodeValue
, true);
326 $match_any_rule = bool_to_sql_bool($filter["match_any_rule"]);
327 $enabled = bool_to_sql_bool($filter["enabled"]);
331 db_query("INSERT INTO ttrss_filters2 (match_any_rule,enabled,owner_uid)
332 VALUES ($match_any_rule, $enabled,".$_SESSION["uid"].")");
334 $result = db_query("SELECT MAX(id) AS id FROM ttrss_filters2 WHERE
335 owner_uid = ".$_SESSION["uid"]);
336 $filter_id = db_fetch_result($result, 0, "id");
339 $this->opml_notice(T_sprintf("Adding filter..."));
341 foreach ($filter["rules"] as $rule) {
345 if (!$rule["cat_filter"]) {
346 $tmp_result = db_query("SELECT id FROM ttrss_feeds
347 WHERE title = '".db_escape_string($rule["feed"])."' AND owner_uid = ".$_SESSION["uid"]);
348 if (db_num_rows($tmp_result) > 0) {
349 $feed_id = db_fetch_result($tmp_result, 0, "id");
352 $tmp_result = db_query("SELECT id FROM ttrss_feed_categories
353 WHERE title = '".db_escape_string($rule["feed"])."' AND owner_uid = ".$_SESSION["uid"]);
355 if (db_num_rows($tmp_result) > 0) {
356 $cat_id = db_fetch_result($tmp_result, 0, "id");
360 $cat_filter = bool_to_sql_bool($rule["cat_filter"]);
361 $reg_exp = db_escape_string($rule["reg_exp"]);
362 $filter_type = (int)$rule["filter_type"];
364 db_query("INSERT INTO ttrss_filters2_rules (feed_id,cat_id,filter_id,filter_type,reg_exp,cat_filter)
365 VALUES ($feed_id, $cat_id, $filter_id, $filter_type, '$reg_exp', $cat_filter)");
368 foreach ($filter["actions"] as $action) {
370 $action_id = (int)$action["action_id"];
371 $action_param = db_escape_string($action["action_param"]);
373 db_query("INSERT INTO ttrss_filters2_actions (filter_id,action_id,action_param)
374 VALUES ($filter_id, $action_id, '$action_param')");
383 private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) {
384 $body = $doc->getElementsByTagName('body');
386 $default_cat_id = (int) get_feed_category('Imported feeds', false);
389 $cat_title = db_escape_string(mb_substr($root_node->attributes
->getNamedItem('text')->nodeValue
, 0, 250));
392 $cat_title = db_escape_string(mb_substr($root_node->attributes
->getNamedItem('title')->nodeValue
, 0, 250));
394 if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) {
395 $cat_id = get_feed_category($cat_title, $parent_id);
397 if ($cat_id === false) {
398 add_feed_category($cat_title, $parent_id);
399 $cat_id = get_feed_category($cat_title, $parent_id);
406 $outlines = $root_node->childNodes
;
409 $xpath = new DOMXpath($doc);
410 $outlines = $xpath->query("//opml/body/outline");
415 #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id");
416 $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ?
$cat_title : __("Uncategorized")));
418 foreach ($outlines as $node) {
419 if ($node->hasAttributes() && strtolower($node->tagName
) == "outline") {
420 $attrs = $node->attributes
;
421 $node_cat_title = db_escape_string($attrs->getNamedItem('text')->nodeValue
);
423 if (!$node_cat_title)
424 $node_cat_title = db_escape_string($attrs->getNamedItem('title')->nodeValue
);
426 $node_feed_url = db_escape_string($attrs->getNamedItem('xmlUrl')->nodeValue
);
428 if ($node_cat_title && !$node_feed_url) {
429 $this->opml_import_category($doc, $node, $owner_uid, $cat_id);
433 $dst_cat_id = $default_cat_id;
435 $dst_cat_id = $cat_id;
438 switch ($cat_title) {
440 $this->opml_import_preference($doc, $node, $owner_uid);
442 case "tt-rss-labels":
443 $this->opml_import_label($doc, $node, $owner_uid);
445 case "tt-rss-filters":
446 $this->opml_import_filter($doc, $node, $owner_uid);
449 $this->opml_import_feed($doc, $node, $dst_cat_id, $owner_uid);
456 function opml_import($owner_uid) {
457 if (!$owner_uid) return;
459 $debug = isset($_REQUEST["debug"]);
462 # if ($debug) $doc = DOMDocument::load("/tmp/test.opml");
464 if ($_FILES['opml_file']['error'] != 0) {
465 print_error(T_sprintf("Upload failed with error code %d",
466 $_FILES['opml_file']['error']));
472 if (is_uploaded_file($_FILES['opml_file']['tmp_name'])) {
473 $tmp_file = tempnam(CACHE_DIR
. '/upload', 'opml');
475 $result = move_uploaded_file($_FILES['opml_file']['tmp_name'],
479 print_error(__("Unable to move uploaded file."));
483 print_error(__('Error: please upload OPML file.'));
487 if (is_file($tmp_file)) {
488 $doc = new DOMDocument();
489 $doc->load($tmp_file);
492 print_error(__('Error: unable to find moved OPML file.'));
497 $this->opml_import_category($doc, false, $owner_uid, false);
499 print_error(__('Error while parsing document.'));
503 private function opml_notice($msg) {
507 static function opml_publish_url(){
509 $url_path = get_self_url_prefix();
510 $url_path .= "/opml.php?op=publish&key=" .
511 get_feed_access_key('OPML:Publish', false, $_SESSION["uid"]);