forked from fossar/selfoss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpml.php
More file actions
170 lines (146 loc) · 5.22 KB
/
Opml.php
File metadata and controls
170 lines (146 loc) · 5.22 KB
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
<?php
/**
* Simple Opml loading controller
*
* @package controllers
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
* @author Michael Moore <stuporglue@gmail.com>
*/
namespace controllers;
class Opml extends BaseController {
/**
* Passed to opml.phtml
* @var String
*/
private $msgclass = 'error';
/**
* Passed to opml.phtml
* @var String
*/
private $msg;
/**
* Shows a simple html form
* html
*
*/
public function show(){
$this->view = new \helpers\View();
$this->view->msg = $this->msg;
$this->view->msgclass = $this->msgclass;
echo $this->view->render('templates/opml.phtml');
}
/**
* Add an Opml to the user's subscriptions
* html
*
* @note Borrows from controllers/Sources.php:write
*/
public function add(){
try {
if(!array_key_exists('opml',$_FILES)){
throw new Exception("No file uploaded!");
}
$opml = $_FILES['opml'];
if(!$opml['type'] == 'text/xml'){
throw new Exception("Unsupported file type!: " . $opml['type']);
}
$this->sourcesDao = new \daos\Sources();
$this->tagsDao = new \daos\Tags();
\F3::get('logger')->log('start opml import ', \DEBUG);
$subs = simplexml_load_file($opml['tmp_name']);
$subs = $subs->body;
$errors = $this->processGroup($subs);
// show errors
if(count($errors) > 1){
$this->msg = "The following feeds were not added:<br>";
$this->msg .= implode("<br>",$errors);
$this->show();
// On success bring them back to their subscription list
} else {
$this->msg = "Success! You might want to <a href='update'>Update now</a> or <a href='./'>view your feeds</a>.";
$this->msgclass = 'success';
$this->show();
}
} catch (Exception $e){
$this->msg = "</p>There was a problem importing your OPML file: <p>";
$this->msg .= $e->getMessage();
$this->show();
}
}
/**
* Process a group of outlines
*
* @param $xml (SimpleXML) A SimpleXML object with <outline> children
* @param $tags (Array) An array of tags for the current <outline>
* @note Recursive
* @note We use non-rss outline's text as tags
*/
private function processGroup($xml,$tags = Array()){
$errors = Array();
// tags are the words of the outline parent
if((string)$xml['title'] && $xml['title']!='/'){
$tags[] = (string)$xml['title'];
}
// parse every outline item
foreach($xml->outline as $outline){
if((string)$outline['type'] || !(string)$outline['xmlUrl']) {
//support folders in opml
if($outline['type']=='folder' || !(string)$outline['xmlUrl']) {
$ret = $this->processGroup($outline,$tags);
$errors = array_merge($errors,$ret);
} else {
$ret = $this->addSubscription($outline,$tags);
if($ret!==true) {
$errors[] = $ret;
}
}
} else {
$ret = $this->processGroup($outline,$tags);
$errors = array_merge($errors,$ret);
}
}
return $errors;
}
/**
* Add new feed subscription
*
* @return true on success or item title on error
* @param $xml xml feed entry for item
* @param $tags of the entry
*/
private function addSubscription($xml, $tags){
// OPML Required attributes: text,xmlUrl,type
// Optional attributes: title, htmlUrl, language, title, version
// description
$title = (string)$xml['text'];
if ($title == null) {
$title = (string)$xml['title'];
}
// RSS URL
$data['url'] = (string)$xml['xmlUrl'];
if($xml['type'] == 'rss' || $xml['type'] == 'atom')
$spout = 'spouts\rss\feed';
else {
\F3::get('logger')->log('opml import: invalid type (only rss supported) ' . $title, \DEBUG);
return $title;
}
// validate new item
$validation = $this->sourcesDao->validate($title, 'spouts\rss\feed', $data);
if($validation!==true) {
\F3::get('logger')->log('opml import: invalid item ' . $title, \DEBUG);
return $title;
}
// import tags
$tags = implode(',',$tags);
$id = $this->sourcesDao->add($title, $tags, $spout, $data);
$tags = explode(",",$tags);
foreach($tags as $tag)
$this->tagsDao->autocolorTag(trim($tag));
// cleanup tags
$this->tagsDao->cleanup($this->sourcesDao->getAllTags());
// success
\F3::get('logger')->log('opml import: item successfully imported: ' . $title, \DEBUG);
return true;
}
}