-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.php
189 lines (158 loc) · 3.9 KB
/
reader.php
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
<?php
class OPML_reader {
/**
* OPML file url/path
* @var string
*/
public $file_url = '';
/**
* Parsed OPML file data
* @var SimpleXMLObject
*/
public $opml_file;
/**
* Construct an OPML_Reader object
*
* Accepts a file url string and opens the OPML file for reading
*
* @param string $file url
*/
function __construct( $url = '' ) {
$this->file_url = $url;
$this->open_file();
// $this->get_OPML_obj();
}
/**
* Opens the OPML file
*
* Uses the object's $url property to pull down and get the
* OPML file data, assigned to $opml_file
*/
function open_file() {
if ( empty( $this->file_url ) ) {
return;
}
if ( 1 == ini_get( 'allow_url_fopen' ) ) {
$file = simplexml_load_file( $this->file_url );
} else {
$response = file_get_contents( $this->file_url);
if ( is_wp_error( $response ) ) {
$this->opml_file = false;
return;
}
$file = simplexml_load_string( wp_remote_retrieve_body( $response ) );
}
if ( empty( $file ) ) {
$file = false;
}
$this->opml_file = $file;
}
/**
* Retrieves the OPML_Object from the provided url
*
* @return OPML_Object
*/
public function get_OPML_obj() {
$this->opml = new OPML_Object( $this->file_url );
$this->opml->set_title( (string) $this->opml_file->head->title );
foreach ( $this->opml_file->body->outline as $folder ) {
$this->make_OPML_obj( $folder );
}
return $this->opml;
}
/**
* Recursively builds the OPML_Object for each folder
*
* @param SimpleXMLObject $entry
* @param boolean|SimpleXMLObject $parent
*/
public function make_OPML_obj( $entry, $parent = false ) {
$entry_a = $this->get_opml_properties($entry);
if ( isset($entry_a['xmlUrl']) ){
$feed_obj = $this->opml->make_a_feed_obj($entry_a);
$this->opml->set_feed($feed_obj, $parent);
} else {
$folder_obj = $this->opml->make_a_folder_obj($entry_a);
$this->opml->set_folder($folder_obj);
foreach ($entry as $feed){
$this->make_OPML_obj($feed, $folder_obj);
}
}
}
/**
* Builds the SimpleXMLObject's attributes into an array
*
* @param SimpleXMLObject $simple_xml_obj
* @return array
*/
public function get_opml_properties( $simple_xml_obj ) {
$obj = $simple_xml_obj->attributes();
$array = array();
foreach ($obj as $key=>$value){
$array[$key] = (string) $value;
}
return $array;
}
function open_OPML($file) {
if(1 == ini_get('allow_url_fopen')){
$file = simplexml_load_file($file);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
$file = simplexml_load_string($data);
}
if (empty($file)) {
pf_log('Received an empty file.');
return false;
} else {
pf_log('Received:');
pf_log($file);
$opml_data = $file;
return $opml_data;
}
}
# Pass the URL and if you want to return an array of objects or of urls.
# @todo remove this function
function get_OPML_data($url, $is_array = true){
$opml_data = $this->open_OPML($url);
if (!$opml_data || empty($opml_data)){
return false;
}
//Site data
$a = array();
//Feed URI
$b = array();
$c = 0;
/** Get XML data:
* supplies:
* [text] - Text version of title
* [text] - Text version of title
* [type] - Feed type (should be rss)
* [xmlUrl] - location of the RSS feed on the site.
* [htmlUrl] - The site home URI.
**/
foreach ($opml_data->body->outline as $folder){
# Check if there are no folders.
if (isset($folder['xmlUrl'])){
$b[] = $folder['xmlUrl']->__toString();
}
foreach ($folder->outline as $data){
$a[] = reset($data);
}
// Pulls out the feed location.
foreach ($a as $outline) {
$b[] = $outline['xmlUrl'];
}
}
if ($is_array){
return $b;
} else {
return $a;
}
}
}