]>
git.wh0rd.org - tt-rss.git/blob - classes/opml.php
f7bab4fff1523c2ff91e0d63cfeeb3a5c6117035
2 class Opml
extends Protected_Handler
{
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_wide.png\"></div>
33 <h1>".__('OPML Utility')."</h1>";
35 add_feed_category($this->link
, "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 "</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($this->link
, "SELECT title FROM ttrss_feed_categories WHERE id = '$cat_id'
70 AND owner_uid = '$owner_uid'");
71 $cat_title = db_fetch_result($result, 0, "title");
74 if ($cat_title) $out .= "<outline title=\"$cat_title\">\n";
76 $result = db_query($this->link
, "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($this->link
, "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 title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION
."\">";
134 $result = db_query($this->link
, "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 title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION
."\">";
148 $result = db_query($this->link
, "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 title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION
."\">";
164 $result = db_query($this->link
, "SELECT filter_type,
172 ttrss_feeds.feed_url AS feed_url,
173 ttrss_feed_categories.title AS cat_title
175 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
176 LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
178 ttrss_filters.owner_uid = " . $_SESSION['uid']);
180 while ($line = db_fetch_assoc($result)) {
181 $name = htmlspecialchars($line['reg_exp']);
183 foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
184 $line[$b] = sql_bool_to_bool($line[$b]);
187 $filter = json_encode($line);
189 $out .= "<outline filter-name=\"$name\">$filter</outline>";
194 $out .= "</outline>";
197 $out .= "</body></opml>";
200 $doc = new DOMDocument();
201 $doc->formatOutput
= true;
202 $doc->preserveWhiteSpace
= false;
204 $res = $doc->saveXML();
206 // saveXML uses a two-space indent. Change to tabs.
207 $res = preg_replace_callback('/^(?: )+/mu',
210 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
218 private function opml_import_feed($doc, $node, $cat_id, $owner_uid) {
219 $attrs = $node->attributes
;
221 $feed_title = db_escape_string($attrs->getNamedItem('text')->nodeValue
);
222 if (!$feed_title) $feed_title = db_escape_string($attrs->getNamedItem('title')->nodeValue
);
224 $feed_url = db_escape_string($attrs->getNamedItem('xmlUrl')->nodeValue
);
225 if (!$feed_url) $feed_url = db_escape_string($attrs->getNamedItem('xmlURL')->nodeValue
);
227 $site_url = db_escape_string($attrs->getNamedItem('htmlUrl')->nodeValue
);
229 if ($feed_url && $feed_title) {
230 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
231 feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
233 if (db_num_rows($result) == 0) {
234 #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id");
235 $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title));
237 $query = "INSERT INTO ttrss_feeds
238 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
239 ('$feed_title', '$feed_url', '$owner_uid',
240 '$cat_id', '$site_url', 0)";
241 db_query($this->link
, $query);
244 $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title));
249 private function opml_import_label($doc, $node, $owner_uid) {
250 $attrs = $node->attributes
;
251 $label_name = db_escape_string($attrs->getNamedItem('label-name')->nodeValue
);
254 $fg_color = db_escape_string($attrs->getNamedItem('label-fg-color')->nodeValue
);
255 $bg_color = db_escape_string($attrs->getNamedItem('label-bg-color')->nodeValue
);
257 if (!label_find_id($this->link
, $label_name, $_SESSION['uid'])) {
258 $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name)));
259 label_create($this->link
, $label_name, $fg_color, $bg_color);
261 $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)));
266 private function opml_import_preference($doc, $node, $owner_uid) {
267 $attrs = $node->attributes
;
268 $pref_name = db_escape_string($attrs->getNamedItem('pref-name')->nodeValue
);
271 $pref_value = db_escape_string($attrs->getNamedItem('value')->nodeValue
);
273 $this->opml_notice(T_sprintf("Setting preference key %s to %s",
274 $pref_name, $pref_value));
276 set_pref($this->link
, $pref_name, $pref_value);
280 private function opml_import_filter($doc, $node, $owner_uid) {
281 $attrs = $node->attributes
;
283 $filter_name = db_escape_string($attrs->getNamedItem('filter-name')->nodeValue
);
287 $filter = json_decode($node->nodeValue
, true);
290 $reg_exp = db_escape_string($filter['reg_exp']);
291 $filter_type = (int)$filter['filter_type'];
292 $action_id = (int)$filter['action_id'];
294 $result = db_query($this->link
, "SELECT id FROM ttrss_filters WHERE
295 reg_exp = '$reg_exp' AND
296 filter_type = '$filter_type' AND
297 action_id = '$action_id' AND
298 owner_uid = " .$_SESSION['uid']);
300 if (db_num_rows($result) == 0) {
301 $enabled = bool_to_sql_bool($filter['enabled']);
302 $action_param = db_escape_string($filter['action_param']);
303 $inverse = bool_to_sql_bool($filter['inverse']);
304 $filter_param = db_escape_string($filter['filter_param']);
305 $cat_filter = bool_to_sql_bool($filter['cat_filter']);
307 $feed_url = db_escape_string($filter['feed_url']);
308 $cat_title = db_escape_string($filter['cat_title']);
310 $result = db_query($this->link
, "SELECT id FROM ttrss_feeds WHERE
311 feed_url = '$feed_url' AND owner_uid = $owner_uid");
313 if (db_num_rows($result) != 0) {
314 $feed_id = db_fetch_result($result, 0, "id");
319 $result = db_query($this->link
, "SELECT id FROM ttrss_feed_categories WHERE
320 title = '$cat_title' AND owner_uid = $owner_uid");
322 if (db_num_rows($result) != 0) {
323 $cat_id = db_fetch_result($result, 0, "id");
328 $this->opml_notice(T_sprintf("Adding filter %s", htmlspecialchars($reg_exp)));
330 $query = "INSERT INTO ttrss_filters (filter_type, action_id,
331 enabled, inverse, action_param, filter_param,
335 VALUES ($filter_type, $action_id,
336 $enabled, $inverse, '$action_param', '$filter_param',
337 $cat_filter, $feed_id,
338 $cat_id, '$reg_exp', ".
339 $_SESSION['uid'].")";
341 db_query($this->link
, $query);
344 $this->opml_notice(T_sprintf("Duplicate filter %s", htmlspecialchars($reg_exp)));
350 private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) {
351 $body = $doc->getElementsByTagName('body');
353 $default_cat_id = (int) get_feed_category($this->link
, 'Imported feeds', false);
356 $cat_title = db_escape_string($root_node->attributes
->getNamedItem('title')->nodeValue
);
358 if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) {
359 $cat_id = get_feed_category($this->link
, $cat_title, $parent_id);
360 db_query($this->link
, "BEGIN");
361 if ($cat_id === false) {
362 add_feed_category($this->link
, $cat_title, $parent_id);
363 $cat_id = get_feed_category($this->link
, $cat_title, $parent_id);
365 db_query($this->link
, "COMMIT");
370 $outlines = $root_node->childNodes
;
373 $xpath = new DOMXpath($doc);
374 $outlines = $xpath->query("//opml/body/outline");
379 #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id");
380 $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ?
$cat_title : __("Uncategorized")));
382 foreach ($outlines as $node) {
383 if ($node->hasAttributes() && strtolower($node->tagName
) == "outline") {
384 $attrs = $node->attributes
;
385 $node_cat_title = db_escape_string($attrs->getNamedItem('title')->nodeValue
);
387 if ($node->hasChildNodes() && $node_cat_title) {
388 $this->opml_import_category($doc, $node, $owner_uid, $cat_id);
392 $dst_cat_id = $default_cat_id;
394 $dst_cat_id = $cat_id;
397 switch ($cat_title) {
399 $this->opml_import_preference($doc, $node, $owner_uid);
401 case "tt-rss-labels":
402 $this->opml_import_label($doc, $node, $owner_uid);
404 case "tt-rss-filters":
405 $this->opml_import_filter($doc, $node, $owner_uid);
408 $this->opml_import_feed($doc, $node, $dst_cat_id, $owner_uid);
415 function opml_import($owner_uid) {
416 if (!$owner_uid) return;
418 $debug = isset($_REQUEST["debug"]);
421 #if ($debug) $doc = DOMDocument::load("/tmp/test.opml");
423 if (is_file($_FILES['opml_file']['tmp_name'])) {
424 $doc = DOMDocument
::load($_FILES['opml_file']['tmp_name']);
426 print_error(__('Error: please upload OPML file.'));
431 $this->opml_import_category($doc, false, $owner_uid, false);
433 print_error(__('Error while parsing document.'));
437 private function opml_notice($msg) {