| 1 |
<?php |
|---|
| 2 |
function opml_import_domdoc($link, $owner_uid) { |
|---|
| 3 |
|
|---|
| 4 |
if (is_file($_FILES['opml_file']['tmp_name'])) { |
|---|
| 5 |
$doc = DOMDocument::load($_FILES['opml_file']['tmp_name']); |
|---|
| 6 |
|
|---|
| 7 |
$result = db_query($link, "SELECT id FROM |
|---|
| 8 |
ttrss_feed_categories WHERE title = 'Imported feeds' AND |
|---|
| 9 |
owner_uid = '$owner_uid' LIMIT 1"); |
|---|
| 10 |
|
|---|
| 11 |
if (db_num_rows($result) == 1) { |
|---|
| 12 |
$default_cat_id = db_fetch_result($result, 0, "id"); |
|---|
| 13 |
} else { |
|---|
| 14 |
$default_cat_id = 0; |
|---|
| 15 |
} |
|---|
| 16 |
|
|---|
| 17 |
if ($doc) { |
|---|
| 18 |
$body = $doc->getElementsByTagName('body'); |
|---|
| 19 |
|
|---|
| 20 |
$xpath = new DOMXpath($doc); |
|---|
| 21 |
$query = "/opml/body//outline"; |
|---|
| 22 |
|
|---|
| 23 |
$outlines = $xpath->query($query); |
|---|
| 24 |
|
|---|
| 25 |
print "<table>"; |
|---|
| 26 |
|
|---|
| 27 |
foreach ($outlines as $outline) { |
|---|
| 28 |
|
|---|
| 29 |
$feed_title = db_escape_string($outline->attributes->getNamedItem('text')->nodeValue); |
|---|
| 30 |
|
|---|
| 31 |
if (!$feed_title) { |
|---|
| 32 |
$feed_title = db_escape_string($outline->attributes->getNamedItem('title')->nodeValue); |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
$cat_title = db_escape_string($outline->attributes->getNamedItem('title')->nodeValue); |
|---|
| 36 |
|
|---|
| 37 |
if (!$cat_title) { |
|---|
| 38 |
$cat_title = db_escape_string($outline->attributes->getNamedItem('text')->nodeValue); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
$feed_url = db_escape_string($outline->attributes->getNamedItem('xmlUrl')->nodeValue); |
|---|
| 42 |
$site_url = db_escape_string($outline->attributes->getNamedItem('htmlUrl')->nodeValue); |
|---|
| 43 |
|
|---|
| 44 |
if ($cat_title && !$feed_url) { |
|---|
| 45 |
|
|---|
| 46 |
db_query($link, "BEGIN"); |
|---|
| 47 |
|
|---|
| 48 |
$result = db_query($link, "SELECT id FROM |
|---|
| 49 |
ttrss_feed_categories WHERE title = '$cat_title' AND |
|---|
| 50 |
owner_uid = '$owner_uid' LIMIT 1"); |
|---|
| 51 |
|
|---|
| 52 |
if (db_num_rows($result) == 0) { |
|---|
| 53 |
|
|---|
| 54 |
printf(__("Adding category <b>%s</b>...<br>"), $cat_title); |
|---|
| 55 |
|
|---|
| 56 |
db_query($link, "INSERT INTO ttrss_feed_categories |
|---|
| 57 |
(title,owner_uid) |
|---|
| 58 |
VALUES ('$cat_title', '$owner_uid')"); |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
db_query($link, "COMMIT"); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
if (!$feed_title || !$feed_url) continue; |
|---|
| 67 |
|
|---|
| 68 |
db_query($link, "BEGIN"); |
|---|
| 69 |
|
|---|
| 70 |
$cat_id = null; |
|---|
| 71 |
|
|---|
| 72 |
$parent_node = $outline->parentNode; |
|---|
| 73 |
|
|---|
| 74 |
if ($parent_node && $parent_node->nodeName == "outline") { |
|---|
| 75 |
$element_category = $parent_node->attributes->getNamedItem('title')->nodeValue; |
|---|
| 76 |
if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue; |
|---|
| 77 |
|
|---|
| 78 |
} else { |
|---|
| 79 |
$element_category = ''; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
if ($element_category) { |
|---|
| 83 |
|
|---|
| 84 |
$element_category = db_escape_string($element_category); |
|---|
| 85 |
|
|---|
| 86 |
$result = db_query($link, "SELECT id FROM |
|---|
| 87 |
ttrss_feed_categories WHERE title = '$element_category' AND |
|---|
| 88 |
owner_uid = '$owner_uid' LIMIT 1"); |
|---|
| 89 |
|
|---|
| 90 |
if (db_num_rows($result) == 1) { |
|---|
| 91 |
$cat_id = db_fetch_result($result, 0, "id"); |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
$result = db_query($link, "SELECT id FROM ttrss_feeds WHERE |
|---|
| 96 |
feed_url = '$feed_url' |
|---|
| 97 |
AND owner_uid = '$owner_uid'"); |
|---|
| 98 |
|
|---|
| 99 |
print "<tr><td><a target='_blank' href='$site_url'><b>$feed_title</b></a></b> |
|---|
| 100 |
(<a target='_blank' href=\"$feed_url\">rss</a>)</td>"; |
|---|
| 101 |
|
|---|
| 102 |
if (db_num_rows($result) > 0) { |
|---|
| 103 |
print "<td>".__('Already imported.')."</td>"; |
|---|
| 104 |
} else { |
|---|
| 105 |
|
|---|
| 106 |
if ($cat_id) { |
|---|
| 107 |
$add_query = "INSERT INTO ttrss_feeds |
|---|
| 108 |
(title, feed_url, owner_uid, cat_id, site_url) VALUES |
|---|
| 109 |
('$feed_title', '$feed_url', '$owner_uid', |
|---|
| 110 |
'$cat_id', '$site_url')"; |
|---|
| 111 |
|
|---|
| 112 |
} else { |
|---|
| 113 |
$add_query = "INSERT INTO ttrss_feeds |
|---|
| 114 |
(title, feed_url, owner_uid, cat_id, site_url) VALUES |
|---|
| 115 |
('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id', |
|---|
| 116 |
'$site_url')"; |
|---|
| 117 |
|
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
db_query($link, $add_query); |
|---|
| 122 |
|
|---|
| 123 |
print "<td><b>".__('Done.')."</b></td>"; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
print "</tr>"; |
|---|
| 127 |
|
|---|
| 128 |
db_query($link, "COMMIT"); |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
print "</table>"; |
|---|
| 132 |
|
|---|
| 133 |
} else { |
|---|
| 134 |
print "<div class=\"error\">".__('Error while parsing document.')."</div>"; |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
} else { |
|---|
| 138 |
print "<div class=\"error\">".__('Error: please upload OPML file.')."</div>"; |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
} |
|---|
| 143 |
?> |
|---|
| 144 |
|
|---|