-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.php
223 lines (214 loc) · 6.02 KB
/
object.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
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
<?php
class OPML_Object {
function __construct($url){
$this->url = $url;
$this->folders = array();
$this->feeds = array();
}
function set_folder($folder_obj){
$folder_obj->slug = $this->slugify($folder_obj->title);
$this->folders[$folder_obj->slug] = $folder_obj;
}
function set_title($string){
if (empty($string)){
$this->title = $this->url;
} else {
$this->title = (string) $string;
}
}
function get_title(){
if (empty($this->title)){
return $this->url;
} else {
return $this->title;
}
}
function get_folder( $key ){
$folders = $this->folders;
$key = $this->slugify($key);
return $folders[$key];
}
function set_feed($feed_obj, $folder = false){
if (!$folder){
//Do not set an unsorted feed if it has already been set
//as a sorted feed.
if (!isset($this->feeds[$feed_obj->id])){
$feed_obj->folder = false;
return array_push($this->feeds, $feed_obj);
}
} else {
if (isset($this->feeds[$feed_obj->id])){
$feed_obj = $this->feeds[$feed_obj->id];
} elseif (empty($feed_obj->folder) || !is_array($feed_obj->folder)) {
$feed_obj->folder = array();
}
if (is_array($folder)){
foreach ($folder as $folder_type){
$feed_obj->folder[] = $folder_type;
}
} else {
$feed_obj->folder[] = $folder;
}
$this->feeds[$feed_obj->id] = $feed_obj;
}
}
public function check_keys($array, $keys, $strict = false){
$array['missing'] = array();
foreach($keys as $key){
if ( !array_key_exists($key, $array) ){
if ($strict) {
return false;
} else {
$array[$key] = '';
$array['missing'][] = $key;
}
}
}
return $array;
}
function assure_title_and_text($entry){
if (!empty($entry['title']) && empty($entry['text']) ){
$entry['text'] = $entry['title'];
} elseif (!empty($entry['text']) && empty($entry['title'])) {
$entry['title'] = $entry['text'];
} elseif ( empty($entry['title']) && empty($entry['text']) && !empty($entry['feedUrl']) ) {
$entry['text'] = $entry['feedUrl'];
$entry['title'] = $entry['feedUrl'];
}
return $entry;
}
function make_a_folder_obj($entry){
$folder = new stdClass();
$entry = (array) $entry;
$entry = $this->check_keys($entry, array('title', 'text') );
$entry['title'] = (!empty($entry['title']) ? $entry['title'] : false);
$entry['text'] = (!empty($entry['text']) ? $entry['text'] : false);
$entry = $this->assure_title_and_text($entry);
$folder->title = $entry['title'];
$folder->text = $entry['text'];
return $folder;
}
function make_a_feed_obj($entry){
$feed = new stdClass();
$entry = (array) $entry;
if ( empty( $entry['xmlUrl'] ) ){
$entry['xmlUrl'] = $entry['htmlUrl'];
}
if ( empty($entry['feedUrl']) ){
$entry['feedUrl'] = $entry['xmlUrl'];
}
$entry = $this->assure_title_and_text($entry);
$entry = $this->check_keys($entry, array( 'title', 'text', 'type', 'xmlUrl', 'htmlUrl', 'feedUrl' ) );
$feed->title = $entry['title'];
$feed->text = $entry['text'];
$feed->type = $entry['type'];
$feed->xmlUrl = str_replace('&', '&', $entry['xmlUrl']);
$feed->feedUrl = str_replace('&', '&', $entry['feedUrl']);
$feed->htmlUrl = str_replace('&', '&', $entry['htmlUrl']);
$feed->id = md5($feed->feedUrl);
return $feed;
}
function order_opml_entries($a, $b){
if (empty($a->folder)){
return 1;
}
if (empty($b->folder)){
return -1;
}
$a = $a->folder[0];
$b = $b->folder[0];
if (!$a){
return -1;
}
if (strcasecmp($a, $b) == 0){
return 0;
}
if (strcasecmp($a, $b) < 0){
return -1;
} else {
return 1;
}
}
function order_feeds_by_folder(){
usort($this->feeds, array($this, 'order_opml_entries'));
}
function get_feeds_by_folder($folder){
$folder_a = array();
if (is_array($folder) && !empty($folder[0]) ){
$folder = $folder[0];
} elseif ( is_array($folder) && !empty($folder['slug']) ){
$folder = $folder['slug'];
}
foreach ( $this->feeds as $feed ){
if ( !empty($feed->folder) ){
foreach($feed->folder as $feed_folder){
if ( !is_object($feed_folder) ){
var_dump('Not an object');
var_dump($feed_folder);
}
if ($feed_folder->slug == $this->slugify($folder)){
$folder_a[] = $feed;
}
}
}
}
if ( empty($folder_a) ){
return false;
}
return $folder_a;
}
public function get_feeds_without_folder(){
$folder_a = array();
foreach ( $this->feeds as $feed ){
if ( empty($feed->folder) ){
$folder_a[] = $feed;
}
}
if ( empty($folder_a) ){
return false;
}
return $folder_a;
}
function get_feed_by_id($unique_id){
return $this->feeds[$unique_id];
}
function sanitize($string, $force_lowercase = true, $anal = false) {
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
"}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—",
"", "", ",", "<", ".", ">", "/", "?");
if (is_array($string)){
$string = implode(' ', $string);
}
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "-", $clean);
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
return ($force_lowercase) ?
(function_exists('mb_strtolower')) ?
mb_strtolower($clean, 'UTF-8') :
strtolower($clean) :
$clean;
}
public function slugify($string, $case = true, $strict = false, $spaces = false) {
if (is_array($string) ){
$string = $string[0];
}
$string = strip_tags($string);
// replace non letter or digits by -
$string = preg_replace('~[^\\pL\d]+~u', '-', $string);
if ($spaces == false){
$stringSlug = str_replace(' ', '-', $string);
$stringSlug = trim($stringSlug);
$stringSlug = str_replace('&','&', $stringSlug);
$stringSlug = $this->sanitize($stringSlug, $case, $strict);
} else {
$stringSlug = str_replace('&','&', $string);
$stringSlug = $this->sanitize($stringSlug, $case, $strict);
}
$stringSlug = htmlspecialchars( $stringSlug, null, null, false );
if (empty($stringSlug))
{
return 'empty';
}
return $stringSlug;
}
}