/[web]/planet/common/app/lib/lib.opml.php
ViewVC logotype

Contents of /planet/common/app/lib/lib.opml.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1619 - (show annotations) (download)
Mon Aug 13 10:45:23 2012 UTC (11 years, 8 months ago) by dams
File size: 2922 byte(s)
- Import moonmoon
- Create repo per langs

1 <?php
2 class opml
3 {
4 var $_xml = null;
5 var $_currentTag = '';
6
7 var $title = '';
8 var $entries = array();
9 var $map =
10 array(
11 'URL' => 'website',
12 'HTMLURL' => 'website',
13 'TEXT' => 'name',
14 'TITLE' => 'name',
15 'XMLURL' => 'feed',
16 'DESCRIPTION' => 'description'
17 );
18
19
20 function parse($data)
21 {
22 $this->_xml = xml_parser_create('UTF-8');
23 //xml_parser_set_option($this->_xml, XML_OPTION_CASE_FOLDING, false);
24 //xml_parser_set_option($this->_xml, XML_OPTION_SKIP_WHITE, true);
25 xml_set_object($this->_xml, $this);
26 xml_set_element_handler($this->_xml,'_openTag','_closeTag');
27 xml_set_character_data_handler ($this->_xml, '_cData');
28
29 xml_parse($this->_xml,$data);
30 xml_parser_free($this->_xml);
31 return $this->entries;
32 }
33
34
35 function _openTag($p,$tag,$attrs)
36 {
37 $this->_currentTag = $tag;
38
39 if ($tag == 'OUTLINE')
40 {
41 $i = count($this->entries);
42 foreach (array_keys($this->map) as $key)
43 {
44 if (isset($attrs[$key])) {
45 $this->entries[$i][$this->map[$key]] = $attrs[$key];
46 }
47 }
48 }
49 }
50
51 function _closeTag($p, $tag){
52 $this->_currentTag = '';
53 }
54
55 function _cData($p, $cdata){
56 if ($this->_currentTag == 'TITLE'){
57 $this->title = $cdata;
58 }
59 }
60
61 function getTitle(){
62 return $this->title;
63 }
64
65 function getPeople(){
66 return $this->entries;
67 }
68 }
69
70 class OpmlManager
71 {
72 public function load($file) {
73 if (@file_exists($file)) {
74 $opml = new opml();
75
76 //Remove BOM if needed
77 $BOM = '/^/';
78 $fileContent = file_get_contents($file);
79 $fileContent = preg_replace($BOM, '', $fileContent, 1);
80
81 //Parse
82 $opml->parse($fileContent);
83
84 return $opml;
85 }
86 }
87
88 public function save($opml, $file){
89 $out = '<?xml version="1.0"?>'."\n";
90 $out.= '<opml version="1.1">'."\n";
91 $out.= '<head>'."\n";
92 $out.= '<title>'.htmlspecialchars($opml->getTitle()).'</title>'."\n";
93 $out.= '<dateCreated>'.date('c').'</dateCreated>'."\n";
94 $out.= '<dateModified>'.date('c').'</dateModified>'."\n";
95 $out.= '</head>'."\n";
96 $out.= '<body>'."\n";
97 foreach ($opml->entries as $person){
98 $out.= '<outline text="' . htmlspecialchars($person['name']) . '" htmlUrl="' . htmlspecialchars($person['website']) . '" xmlUrl="' . htmlspecialchars($person['feed']) . '"/>'."\n";
99 }
100 $out.= '</body>'."\n";
101 $out.= '</opml>';
102
103 file_put_contents($file, $out);
104 }
105
106 public function backup($file){
107 copy($file, $file.'.bak');
108 }
109 }

  ViewVC Help
Powered by ViewVC 1.1.30