]> git.wh0rd.org Git - tt-rss.git/blob - classes/opml.php
plugins: remove obsolete plugin_data/stored stuff
[tt-rss.git] / classes / opml.php
1 <?php
2 class Opml extends Handler_Protected {
3
4         function csrf_ignore($method) {
5                 $csrf_ignored = array("export", "import");
6
7                 return array_search($method, $csrf_ignored) !== false;
8         }
9
10         function export() {
11                 $output_name = $_REQUEST["filename"];
12                 if (!$output_name) $output_name = "TinyTinyRSS.opml";
13
14                 $show_settings = $_REQUEST["settings"];
15
16                 $owner_uid = $_SESSION["uid"];
17                 return $this->opml_export($output_name, $owner_uid, false, ($show_settings == 1));
18         }
19
20         function import() {
21                 $owner_uid = $_SESSION["uid"];
22
23                 header('Content-Type: text/html; charset=utf-8');
24
25                 print "<html>
26                         <head>
27                                 <link rel=\"stylesheet\" href=\"css/utility.css\" type=\"text/css\">
28                                 <title>".__("OPML Utility")."</title>
29                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
30                         </head>
31                         <body>
32                         <div class=\"floatingLogo\"><img src=\"images/logo_small.png\"></div>
33                         <h1>".__('OPML Utility')."</h1><div class='content'>";
34
35                 add_feed_category("Imported feeds");
36
37                 $this->opml_notice(__("Importing OPML..."));
38                 $this->opml_import($owner_uid);
39
40                 print "<br><form method=\"GET\" action=\"prefs.php\">
41                         <input type=\"submit\" value=\"".__("Return to preferences")."\">
42                         </form>";
43
44                 print "</div></body></html>";
45
46
47         }
48
49         // Export
50
51         private function opml_export_category($owner_uid, $cat_id, $hide_private_feeds=false) {
52
53                 if ($cat_id) {
54                         $cat_qpart = "parent_cat = '$cat_id'";
55                         $feed_cat_qpart = "cat_id = '$cat_id'";
56                 } else {
57                         $cat_qpart = "parent_cat IS NULL";
58                         $feed_cat_qpart = "cat_id IS NULL";
59                 }
60
61                 if ($hide_private_feeds)
62                         $hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')";
63                 else
64                         $hide_qpart = "true";
65
66                 $out = "";
67
68                 if ($cat_id) {
69                         $result = $this->dbh->query("SELECT title FROM ttrss_feed_categories WHERE id = '$cat_id'
70                                 AND owner_uid = '$owner_uid'");
71                         $cat_title = htmlspecialchars($this->dbh->fetch_result($result, 0, "title"));
72                 }
73
74                 if ($cat_title) $out .= "<outline text=\"$cat_title\">\n";
75
76                 $result = $this->dbh->query("SELECT id,title
77                         FROM ttrss_feed_categories WHERE
78                         $cat_qpart AND owner_uid = '$owner_uid' ORDER BY order_id, title");
79
80                 while ($line = $this->dbh->fetch_assoc($result)) {
81                         $title = htmlspecialchars($line["title"]);
82                         $out .= $this->opml_export_category($owner_uid, $line["id"], $hide_private_feeds);
83                 }
84
85                 $feeds_result = $this->dbh->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");
88
89                 while ($fline = $this->dbh->fetch_assoc($feeds_result)) {
90                         $title = htmlspecialchars($fline["title"]);
91                         $url = htmlspecialchars($fline["feed_url"]);
92                         $site_url = htmlspecialchars($fline["site_url"]);
93
94                         if ($site_url) {
95                                 $html_url_qpart = "htmlUrl=\"$site_url\"";
96                         } else {
97                                 $html_url_qpart = "";
98                         }
99
100                         $out .= "<outline type=\"rss\" text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
101                 }
102
103                 if ($cat_title) $out .= "</outline>\n";
104
105                 return $out;
106         }
107
108         function opml_export($name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
109                 if (!$owner_uid) return;
110
111                 if (!isset($_REQUEST["debug"])) {
112                         header("Content-type: application/xml+opml");
113                         header("Content-Disposition: attachment; filename=" . $name );
114                 } else {
115                         header("Content-type: text/xml");
116                 }
117
118                 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
119
120                 $out .= "<opml version=\"1.0\">";
121                 $out .= "<head>
122                         <dateCreated>" . date("r", time()) . "</dateCreated>
123                         <title>Tiny Tiny RSS Feed Export</title>
124                 </head>";
125                 $out .= "<body>";
126
127                 $out .= $this->opml_export_category($owner_uid, false, $hide_private_feeds);
128
129                 # export tt-rss settings
130
131                 if ($include_settings) {
132                         $out .= "<outline text=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
133
134                         $result = $this->dbh->query("SELECT pref_name, value FROM ttrss_user_prefs WHERE
135                            profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
136
137                         while ($line = $this->dbh->fetch_assoc($result)) {
138                                 $name = $line["pref_name"];
139                                 $value = htmlspecialchars($line["value"]);
140
141                                 $out .= "<outline pref-name=\"$name\" value=\"$value\"/>";
142                         }
143
144                         $out .= "</outline>";
145
146                         $out .= "<outline text=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION."\">";
147
148                         $result = $this->dbh->query("SELECT * FROM ttrss_labels2 WHERE
149                                 owner_uid = " . $_SESSION['uid']);
150
151                         while ($line = $this->dbh->fetch_assoc($result)) {
152                                 $name = htmlspecialchars($line['caption']);
153                                 $fg_color = htmlspecialchars($line['fg_color']);
154                                 $bg_color = htmlspecialchars($line['bg_color']);
155
156                                 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
157
158                         }
159
160                         $out .= "</outline>";
161
162                         $out .= "<outline text=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">";
163
164                         $result = $this->dbh->query("SELECT * FROM ttrss_filters2
165                                 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY id");
166
167                         while ($line = $this->dbh->fetch_assoc($result)) {
168                                 foreach (array('enabled', 'match_any_rule', 'inverse') as $b) {
169                                         $line[$b] = sql_bool_to_bool($line[$b]);
170                                 }
171
172                                 $line["rules"] = array();
173                                 $line["actions"] = array();
174
175                                 $tmp_result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
176                                         WHERE filter_id = ".$line["id"]);
177
178                                 while ($tmp_line = $this->dbh->fetch_assoc($tmp_result)) {
179                                         unset($tmp_line["id"]);
180                                         unset($tmp_line["filter_id"]);
181
182                                         $cat_filter = sql_bool_to_bool($tmp_line["cat_filter"]);
183
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"],
187                                                         $cat_filter);
188                                         } else {
189                                                 $tmp_line["feed"] = "";
190                                         }
191
192                                         $tmp_line["cat_filter"] = sql_bool_to_bool($tmp_line["cat_filter"]);
193                                         $tmp_line["inverse"] = sql_bool_to_bool($tmp_line["inverse"]);
194
195                                         unset($tmp_line["feed_id"]);
196                                         unset($tmp_line["cat_id"]);
197
198                                         array_push($line["rules"], $tmp_line);
199                                 }
200
201                                 $tmp_result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
202                                         WHERE filter_id = ".$line["id"]);
203
204                                 while ($tmp_line = $this->dbh->fetch_assoc($tmp_result)) {
205                                         unset($tmp_line["id"]);
206                                         unset($tmp_line["filter_id"]);
207
208                                         array_push($line["actions"], $tmp_line);
209                                 }
210
211                                 unset($line["id"]);
212                                 unset($line["owner_uid"]);
213                                 $filter = json_encode($line);
214
215                                 $out .= "<outline filter-type=\"2\"><![CDATA[$filter]]></outline>";
216
217                         }
218
219
220                         $out .= "</outline>";
221                 }
222
223                 $out .= "</body></opml>";
224
225                 // Format output.
226                 $doc = new DOMDocument();
227                 $doc->formatOutput = true;
228                 $doc->preserveWhiteSpace = false;
229                 $doc->loadXML($out);
230
231                 $xpath = new DOMXpath($doc);
232                 $outlines = $xpath->query("//outline[@title]");
233
234                 // cleanup empty categories
235                 foreach ($outlines as $node) {
236                         if ($node->getElementsByTagName('outline')->length == 0)
237                                 $node->parentNode->removeChild($node);
238                 }
239
240                 $res = $doc->saveXML();
241
242 /*              // saveXML uses a two-space indent.  Change to tabs.
243                 $res = preg_replace_callback('/^(?:  )+/mu',
244                         create_function(
245                                 '$matches',
246                                 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
247                         $res); */
248
249                 print $res;
250         }
251
252         // Import
253
254         private function opml_import_feed($doc, $node, $cat_id, $owner_uid) {
255                 $attrs = $node->attributes;
256
257                 $feed_title = $this->dbh->escape_string(mb_substr($attrs->getNamedItem('text')->nodeValue, 0, 250));
258                 if (!$feed_title) $feed_title = $this->dbh->escape_string(mb_substr($attrs->getNamedItem('title')->nodeValue, 0, 250));
259
260                 $feed_url = $this->dbh->escape_string($attrs->getNamedItem('xmlUrl')->nodeValue);
261                 if (!$feed_url) $feed_url = $this->dbh->escape_string($attrs->getNamedItem('xmlURL')->nodeValue);
262
263                 $site_url = $this->dbh->escape_string(mb_substr($attrs->getNamedItem('htmlUrl')->nodeValue, 0, 250));
264
265                 if ($feed_url && $feed_title) {
266                         $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE
267                                 feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
268
269                         if ($this->dbh->num_rows($result) == 0) {
270                                 #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id");
271                                 $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title));
272
273                                 if (!$cat_id) $cat_id = 'NULL';
274
275                                 $query = "INSERT INTO ttrss_feeds
276                                         (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
277                                         ('$feed_title', '$feed_url', '$owner_uid',
278                                         $cat_id, '$site_url', 0)";
279                                 $this->dbh->query($query);
280
281                         } else {
282                                 $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title));
283                         }
284                 }
285         }
286
287         private function opml_import_label($doc, $node, $owner_uid) {
288                 $attrs = $node->attributes;
289                 $label_name = $this->dbh->escape_string($attrs->getNamedItem('label-name')->nodeValue);
290
291                 if ($label_name) {
292                         $fg_color = $this->dbh->escape_string($attrs->getNamedItem('label-fg-color')->nodeValue);
293                         $bg_color = $this->dbh->escape_string($attrs->getNamedItem('label-bg-color')->nodeValue);
294
295                         if (!label_find_id($label_name, $_SESSION['uid'])) {
296                                 $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name)));
297                                 label_create($label_name, $fg_color, $bg_color, $owner_uid);
298                         } else {
299                                 $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)));
300                         }
301                 }
302         }
303
304         private function opml_import_preference($doc, $node, $owner_uid) {
305                 $attrs = $node->attributes;
306                 $pref_name = $this->dbh->escape_string($attrs->getNamedItem('pref-name')->nodeValue);
307
308                 if ($pref_name) {
309                         $pref_value = $this->dbh->escape_string($attrs->getNamedItem('value')->nodeValue);
310
311                         $this->opml_notice(T_sprintf("Setting preference key %s to %s",
312                                 $pref_name, $pref_value));
313
314                         set_pref($pref_name, $pref_value);
315                 }
316         }
317
318         private function opml_import_filter($doc, $node, $owner_uid) {
319                 $attrs = $node->attributes;
320
321                 $filter_type = $this->dbh->escape_string($attrs->getNamedItem('filter-type')->nodeValue);
322
323                 if ($filter_type == '2') {
324                         $filter = json_decode($node->nodeValue, true);
325
326                         if ($filter) {
327                                 $match_any_rule = bool_to_sql_bool($filter["match_any_rule"]);
328                                 $enabled = bool_to_sql_bool($filter["enabled"]);
329                                 $inverse = bool_to_sql_bool($filter["inverse"]);
330                                 $title = db_escape_string($filter["title"]);
331
332                                 $this->dbh->query("BEGIN");
333
334                                 $this->dbh->query("INSERT INTO ttrss_filters2 (match_any_rule,enabled,inverse,title,owner_uid)
335                                         VALUES ($match_any_rule, $enabled, $inverse, '$title',
336                                         ".$_SESSION["uid"].")");
337
338                                 $result = $this->dbh->query("SELECT MAX(id) AS id FROM ttrss_filters2 WHERE
339                                         owner_uid = ".$_SESSION["uid"]);
340                                 $filter_id = $this->dbh->fetch_result($result, 0, "id");
341
342                                 if ($filter_id) {
343                                         $this->opml_notice(T_sprintf("Adding filter..."));
344
345                                         foreach ($filter["rules"] as $rule) {
346                                                 $feed_id = "NULL";
347                                                 $cat_id = "NULL";
348
349                                                 if (!$rule["cat_filter"]) {
350                                                         $tmp_result = $this->dbh->query("SELECT id FROM ttrss_feeds
351                                                                 WHERE title = '".$this->dbh->escape_string($rule["feed"])."' AND owner_uid = ".$_SESSION["uid"]);
352                                                         if ($this->dbh->num_rows($tmp_result) > 0) {
353                                                                 $feed_id = $this->dbh->fetch_result($tmp_result, 0, "id");
354                                                         }
355                                                 } else {
356                                                         $tmp_result = $this->dbh->query("SELECT id FROM ttrss_feed_categories
357                                                                 WHERE title = '".$this->dbh->escape_string($rule["feed"])."' AND owner_uid = ".$_SESSION["uid"]);
358
359                                                         if ($this->dbh->num_rows($tmp_result) > 0) {
360                                                                 $cat_id = $this->dbh->fetch_result($tmp_result, 0, "id");
361                                                         }
362                                                 }
363
364                                                 $cat_filter = bool_to_sql_bool($rule["cat_filter"]);
365                                                 $reg_exp = $this->dbh->escape_string($rule["reg_exp"]);
366                                                 $filter_type = (int)$rule["filter_type"];
367                                                 $inverse = bool_to_sql_bool($rule["inverse"]);
368
369                                                 $this->dbh->query("INSERT INTO ttrss_filters2_rules (feed_id,cat_id,filter_id,filter_type,reg_exp,cat_filter,inverse)
370                                                         VALUES ($feed_id, $cat_id, $filter_id, $filter_type, '$reg_exp', $cat_filter,$inverse)");
371                                         }
372
373                                         foreach ($filter["actions"] as $action) {
374
375                                                 $action_id = (int)$action["action_id"];
376                                                 $action_param = $this->dbh->escape_string($action["action_param"]);
377
378                                                 $this->dbh->query("INSERT INTO ttrss_filters2_actions (filter_id,action_id,action_param)
379                                                         VALUES ($filter_id, $action_id, '$action_param')");
380                                         }
381                                 }
382
383                                 $this->dbh->query("COMMIT");
384                         }
385                 }
386         }
387
388         private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) {
389                 $body = $doc->getElementsByTagName('body');
390
391                 $default_cat_id = (int) get_feed_category('Imported feeds', false);
392
393                 if ($root_node) {
394                         $cat_title = $this->dbh->escape_string(mb_substr($root_node->attributes->getNamedItem('text')->nodeValue, 0, 250));
395
396                         if (!$cat_title)
397                                 $cat_title = $this->dbh->escape_string(mb_substr($root_node->attributes->getNamedItem('title')->nodeValue, 0, 250));
398
399                         if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) {
400                                 $cat_id = get_feed_category($cat_title, $parent_id);
401                                 $this->dbh->query("BEGIN");
402                                 if ($cat_id === false) {
403                                         add_feed_category($cat_title, $parent_id);
404                                         $cat_id = get_feed_category($cat_title, $parent_id);
405                                 }
406                                 $this->dbh->query("COMMIT");
407                         } else {
408                                 $cat_id = 0;
409                         }
410
411                         $outlines = $root_node->childNodes;
412
413                 } else {
414                         $xpath = new DOMXpath($doc);
415                         $outlines = $xpath->query("//opml/body/outline");
416
417                         $cat_id = 0;
418                 }
419
420                 #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id");
421                 $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized")));
422
423                 foreach ($outlines as $node) {
424                         if ($node->hasAttributes() && strtolower($node->tagName) == "outline") {
425                                 $attrs = $node->attributes;
426                                 $node_cat_title = $this->dbh->escape_string($attrs->getNamedItem('text')->nodeValue);
427
428                                 if (!$node_cat_title)
429                                         $node_cat_title = $this->dbh->escape_string($attrs->getNamedItem('title')->nodeValue);
430
431                                 $node_feed_url = $this->dbh->escape_string($attrs->getNamedItem('xmlUrl')->nodeValue);
432
433                                 if ($node_cat_title && !$node_feed_url) {
434                                         $this->opml_import_category($doc, $node, $owner_uid, $cat_id);
435                                 } else {
436
437                                         if (!$cat_id) {
438                                                 $dst_cat_id = $default_cat_id;
439                                         } else {
440                                                 $dst_cat_id = $cat_id;
441                                         }
442
443                                         switch ($cat_title) {
444                                         case "tt-rss-prefs":
445                                                 $this->opml_import_preference($doc, $node, $owner_uid);
446                                                 break;
447                                         case "tt-rss-labels":
448                                                 $this->opml_import_label($doc, $node, $owner_uid);
449                                                 break;
450                                         case "tt-rss-filters":
451                                                 $this->opml_import_filter($doc, $node, $owner_uid);
452                                                 break;
453                                         default:
454                                                 $this->opml_import_feed($doc, $node, $dst_cat_id, $owner_uid);
455                                         }
456                                 }
457                         }
458                 }
459         }
460
461         function opml_import($owner_uid) {
462                 if (!$owner_uid) return;
463
464                 $debug = isset($_REQUEST["debug"]);
465                 $doc = false;
466
467 #               if ($debug) $doc = DOMDocument::load("/tmp/test.opml");
468
469                 if ($_FILES['opml_file']['error'] != 0) {
470                         print_error(T_sprintf("Upload failed with error code %d",
471                                 $_FILES['opml_file']['error']));
472                         return;
473                 }
474
475                 $tmp_file = false;
476
477                 if (is_uploaded_file($_FILES['opml_file']['tmp_name'])) {
478                         $tmp_file = tempnam(CACHE_DIR . '/upload', 'opml');
479
480                         $result = move_uploaded_file($_FILES['opml_file']['tmp_name'],
481                                 $tmp_file);
482
483                         if (!$result) {
484                                 print_error(__("Unable to move uploaded file."));
485                                 return;
486                         }
487                 } else {
488                         print_error(__('Error: please upload OPML file.'));
489                         return;
490                 }
491
492                 if (is_file($tmp_file)) {
493                         $doc = new DOMDocument();
494                         $doc->load($tmp_file);
495                         unlink($tmp_file);
496                 } else if (!$doc) {
497                         print_error(__('Error: unable to find moved OPML file.'));
498                         return;
499                 }
500
501                 if ($doc) {
502                         $this->opml_import_category($doc, false, $owner_uid, false);
503                 } else {
504                         print_error(__('Error while parsing document.'));
505                 }
506         }
507
508         private function opml_notice($msg) {
509                 print "$msg<br/>";
510         }
511
512         static function opml_publish_url(){
513
514                 $url_path = get_self_url_prefix();
515                 $url_path .= "/opml.php?op=publish&key=" .
516                         get_feed_access_key('OPML:Publish', false, $_SESSION["uid"]);
517
518                 return $url_path;
519         }
520
521
522 }
523 ?>