aboutsummaryrefslogtreecommitdiff
path: root/lib/lib_opml.php
blob: 1b5517d7f800a8256d303ca939635f8bf34eee96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
function opml_export ($cats) {
	$txt = '';

	foreach ($cats as $cat) {
		$txt .= '<outline text="' . $cat['name'] . '">' . "\n";

		foreach ($cat['feeds'] as $feed) {
			$txt .= "\t" . '<outline text="' . $feed->name () . '" type="rss" xmlUrl="' . $feed->url () . '" htmlUrl="' . $feed->website () . '" description="' . htmlspecialchars($feed->description(), ENT_COMPAT, 'UTF-8') . '" />' . "\n";
		}

		$txt .= '</outline>' . "\n";
	}

	return $txt;
}

function opml_import ($xml) {
	$xml = html_only_entity_decode($xml);	//!\ Assume UTF-8

	$dom = new DOMDocument();
	$dom->recover = true;
	$dom->strictErrorChecking = false;
	$dom->loadXML($xml);
	$dom->encoding = 'UTF-8';

	$opml = simplexml_import_dom($dom);

	if (!$opml) {
		throw new FreshRSS_Opml_Exception ();
	}

	$catDAO = new FreshRSS_CategoryDAO();
	$catDAO->checkDefault();
	$defCat = $catDAO->getDefault();

	$categories = array ();
	$feeds = array ();

	foreach ($opml->body->outline as $outline) {
		if (!isset ($outline['xmlUrl'])) {
			// Catégorie
			$title = '';

			if (isset ($outline['text'])) {
				$title = (string) $outline['text'];
			} elseif (isset ($outline['title'])) {
				$title = (string) $outline['title'];
			}

			if ($title) {
				// Permet d'éviter les soucis au niveau des id :
				// ceux-ci sont générés en fonction de la date,
				// un flux pourrait être dans une catégorie X avec l'id Y
				// alors qu'il existe déjà la catégorie X mais avec l'id Z
				// Y ne sera pas ajouté et le flux non plus vu que l'id
				// de sa catégorie n'exisera pas
				$title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
				$catDAO = new FreshRSS_CategoryDAO ();
				$cat = $catDAO->searchByName ($title);
				if ($cat === false) {
					$cat = new FreshRSS_Category ($title);
					$values = array (
						'name' => $cat->name (),
						'color' => $cat->color ()
					);
					$cat->_id ($catDAO->addCategory ($values));
				}

				$feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
			}
		} else {
			// Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
			$feeds[] = getFeed ($outline, $defCat->id());
		}
	}

	return array ($categories, $feeds);
}

/**
 * import all feeds of a given outline tag
 */
function getFeedsOutline ($outline, $cat_id) {
	$feeds = array ();

	foreach ($outline->children () as $child) {
		if (isset ($child['xmlUrl'])) {
			$feeds[] = getFeed ($child, $cat_id);
		} else {
			$feeds = array_merge(
				$feeds,
				getFeedsOutline ($child, $cat_id)
			);
		}
	}

	return $feeds;
}

function getFeed ($outline, $cat_id) {
	$url = (string) $outline['xmlUrl'];
	$url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
	$title = '';
	if (isset ($outline['text'])) {
		$title = (string) $outline['text'];
	} elseif (isset ($outline['title'])) {
		$title = (string) $outline['title'];
	}
	$title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
	$feed = new FreshRSS_Feed ($url);
	$feed->_category ($cat_id);
	$feed->_name ($title);
	if (isset($outline['htmlUrl'])) {
		$feed->_website(htmlspecialchars((string)$outline['htmlUrl'], ENT_COMPAT, 'UTF-8'));
	}
	if (isset($outline['description'])) {
		$feed->_description(sanitizeHTML((string)$outline['description']));
	}
	return $feed;
}