summaryrefslogtreecommitdiff
path: root/lib/lib_opml.php
blob: b89e929775dc074bd6d1de66bd5ae80313315799 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php

/**
 * lib_opml is a free library to manage OPML format in PHP.
 *
 * By default, it takes in consideration version 2.0 but can be compatible with
 * OPML 1.0. More information on http://dev.opml.org.
 * Difference is "text" attribute is optional in version 1.0. It is highly
 * recommended to use this attribute.
 *
 * lib_opml requires SimpleXML (php.net/simplexml) and DOMDocument (php.net/domdocument)
 *
 * @author   Marien Fressinaud <dev@marienfressinaud.fr>
 * @link     https://github.com/marienfressinaud/lib_opml
 * @version  0.2-FreshRSS~1.5.1
 * @license  public domain
 *
 * Usages:
 * > include('lib_opml.php');
 * > $filename = 'my_opml_file.xml';
 * > $opml_array = libopml_parse_file($filename);
 * > print_r($opml_array);
 *
 * > $opml_string = [...];
 * > $opml_array = libopml_parse_string($opml_string);
 * > print_r($opml_array);
 *
 * > $opml_array = [...];
 * > $opml_string = libopml_render($opml_array);
 * > $opml_object = libopml_render($opml_array, true);
 * > echo $opml_string;
 * > print_r($opml_object);
 *
 * You can set $strict argument to false if you want to bypass "text" attribute
 * requirement.
 *
 * If parsing fails for any reason (e.g. not an XML string, does not match with
 * the specifications), a LibOPML_Exception is raised.
 *
 * lib_opml array format is described here:
 * $array = array(
 *     'head' => array(       // 'head' element is optional (but recommended)
 *         'key' => 'value',  // key must be a part of available OPML head elements
 *     ),
 *     'body' => array(              // body is required
 *         array(                    // this array represents an outline (at least one)
 *             'text' => 'value',    // 'text' element is required if $strict is true
 *             'key' => 'value',     // key and value are what you want (optional)
 *             '@outlines' = array(  // @outlines is a special value and represents sub-outlines
 *                 array(
 *                     [...]         // where [...] is a valid outline definition
 *                 ),
 *             ),
 *         ),
 *         array(                    // other outline definitions
 *             [...]
 *         ),
 *         [...],
 *     )
 * )
 *
 */

/**
 * A simple Exception class which represents any kind of OPML problem.
 * Message should precise the current problem.
 */
class LibOPML_Exception extends Exception {}


// Define the list of available head attributes. All of them are optional.
define('HEAD_ELEMENTS', serialize(array(
	'title', 'dateCreated', 'dateModified', 'ownerName', 'ownerEmail',
	'ownerId', 'docs', 'expansionState', 'vertScrollState', 'windowTop',
	'windowLeft', 'windowBottom', 'windowRight'
)));


/**
 * Parse an XML object as an outline object and return corresponding array
 *
 * @param SimpleXMLElement $outline_xml the XML object we want to parse
 * @param bool $strict true if "text" attribute is required, false else
 * @return array corresponding to an outline and following format described above
 * @throws LibOPML_Exception
 * @access private
 */
function libopml_parse_outline($outline_xml, $strict = true) {
	$outline = array();

	// An outline may contain any kind of attributes but "text" attribute is
	// required !
	$text_is_present = false;
	foreach ($outline_xml->attributes() as $key => $value) {
		$outline[$key] = (string)$value;

		if ($key === 'text') {
			$text_is_present = true;
		}
	}

	if (!$text_is_present && $strict) {
		throw new LibOPML_Exception(
			'Outline does not contain any text attribute'
		);
	}

	if (empty($outline['text']) && isset($outline['title'])) {
		$outline['text'] = $outline['title'];
	}

	foreach ($outline_xml->children() as $key => $value) {
		// An outline may contain any number of outline children
		if ($key === 'outline') {
			$outline['@outlines'][] = libopml_parse_outline($value, $strict);
		} else {
			throw new LibOPML_Exception(
				'Body can contain only outline elements'
			);
		}
	}

	return $outline;
}

/**
 * Reformat the XML document as a hierarchy when
 * the OPML 2.0 category attribute is used
 */
function preprocessing_categories($doc) {
	$outline_categories = array();
	$body = $doc->getElementsByTagName('body')->item(0);
	$xpath = new DOMXpath($doc);
	$outlines = $xpath->query('/opml/body/outline[@category]');
	foreach ($outlines as $outline) {
		$category = trim($outline->getAttribute('category'));
		if ($category != '') {
			$outline_categorie = null;
			if (!isset($outline_categories[$category])) {
				$outline_categorie = $doc->createElement('outline');
				$outline_categorie->setAttribute('text', $category);
				$body->insertBefore($outline_categorie, $body->firstChild);
				$outline_categories[$category] = $outline_categorie;
			} else {
				$outline_categorie = $outline_categories[$category];
			}
			$outline->parentNode->removeChild($outline);
			$outline_categorie->appendChild($outline);
		}
	}
}

/**
 * Parse a string as a XML one and returns the corresponding array
 *
 * @param string $xml is the string we want to parse
 * @param bool $strict true if "text" attribute is required, false else
 * @return array corresponding to the XML string and following format described above
 * @throws LibOPML_Exception
 * @access public
 */
function libopml_parse_string($xml, $strict = true) {
	$dom = new DOMDocument();
	$dom->recover = true;
	$dom->strictErrorChecking = false;
	$dom->loadXML($xml);
	$dom->encoding = 'UTF-8';

	//Partial compatibility with the category attribute of OPML 2.0
	preprocessing_categories($dom);

	$opml = simplexml_import_dom($dom);

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

	$array = array(
		'version' => (string)$opml['version'],
		'head' => array(),
		'body' => array()
	);

	// First, we get all "head" elements. Head is required but its sub-elements
	// are optional.
	foreach ($opml->head->children() as $key => $value) {
		if (in_array($key, unserialize(HEAD_ELEMENTS), true)) {
			$array['head'][$key] = (string)$value;
		} else {
			throw new LibOPML_Exception(
				$key . 'is not part of OPML format'
			);
		}
	}

	// Then, we get body oulines. Body must contain at least one outline
	// element.
	$at_least_one_outline = false;
	foreach ($opml->body->children() as $key => $value) {
		if ($key === 'outline') {
			$at_least_one_outline = true;
			$array['body'][] = libopml_parse_outline($value, $strict);
		} else {
			throw new LibOPML_Exception(
				'Body can contain only outline elements'
			);
		}
	}

	if (!$at_least_one_outline) {
		throw new LibOPML_Exception(
			'Body must contain at least one outline element'
		);
	}

	return $array;
}


/**
 * Parse a string contained into a file as a XML string and returns the corresponding array
 *
 * @param string $filename should indicates a valid XML file
 * @param bool $strict true if "text" attribute is required, false else
 * @return array corresponding to the file content and following format described above
 * @throws LibOPML_Exception
 * @access public
 */
function libopml_parse_file($filename, $strict = true) {
	$file_content = file_get_contents($filename);

	if ($file_content === false) {
		throw new LibOPML_Exception(
			$filename . ' cannot be found'
		);
	}

	return libopml_parse_string($file_content, $strict);
}


/**
 * Create a XML outline object in a parent object.
 *
 * @param SimpleXMLElement $parent_elt is the parent object of current outline
 * @param array $outline array representing an outline object
 * @param bool $strict true if "text" attribute is required, false else
 * @throws LibOPML_Exception
 * @access private
 */
function libopml_render_outline($parent_elt, $outline, $strict) {
	// Outline MUST be an array!
	if (!is_array($outline)) {
		throw new LibOPML_Exception(
			'Outline element must be defined as array'
		);
	}

	$outline_elt = $parent_elt->addChild('outline');
	$text_is_present = false;
	foreach ($outline as $key => $value) {
		// Only outlines can be an array and so we consider children are also
		// outline elements.
		if ($key === '@outlines' && is_array($value)) {
			foreach ($value as $outline_child) {
				libopml_render_outline($outline_elt, $outline_child, $strict);
			}
		} elseif (is_array($value)) {
			throw new LibOPML_Exception(
				'Type of outline elements cannot be array: ' . $key
			);
		} else {
			// Detect text attribute is present, that's good :)
			if ($key === 'text') {
				$text_is_present = true;
			}

			$outline_elt->addAttribute($key, $value);
		}
	}

	if (!$text_is_present && $strict) {
		throw new LibOPML_Exception(
			'You must define at least a text element for all outlines'
		);
	}
}


/**
 * Render an array as an OPML string or a XML object.
 *
 * @param array $array is the array we want to render and must follow structure defined above
 * @param bool $as_xml_object false if function must return a string, true for a XML object
 * @param bool $strict true if "text" attribute is required, false else
 * @return string|SimpleXMLElement XML string corresponding to $array or XML object
 * @throws LibOPML_Exception
 * @access public
 */
function libopml_render($array, $as_xml_object = false, $strict = true) {
	$opml = new SimpleXMLElement('<opml></opml>');
	$opml->addAttribute('version', $strict ? '2.0' : '1.0');

	// Create head element. $array['head'] is optional but head element will
	// exist in the final XML object.
	$head = $opml->addChild('head');
	if (isset($array['head'])) {
		foreach ($array['head'] as $key => $value) {
			if (in_array($key, unserialize(HEAD_ELEMENTS), true)) {
				$head->addChild($key, $value);
			}
		}
	}

	// Check body is set and contains at least one element
	if (!isset($array['body'])) {
		throw new LibOPML_Exception(
			'$array must contain a body element'
		);
	}
	if (count($array['body']) <= 0) {
		throw new LibOPML_Exception(
			'Body element must contain at least one element (array)'
		);
	}

	// Create outline elements
	$body = $opml->addChild('body');
	foreach ($array['body'] as $outline) {
		libopml_render_outline($body, $outline, $strict);
	}

	// And return the final result
	if ($as_xml_object) {
		return $opml;
	} else {
		$dom = dom_import_simplexml($opml)->ownerDocument;
		$dom->formatOutput = true;
		$dom->encoding = 'UTF-8';
		return $dom->saveXML();
	}
}