-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.php
1197 lines (1022 loc) · 38.8 KB
/
util.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
###############################################################################
# Gregarius - A PHP based RSS aggregator.
# Copyright (C) 2003 - 2006 Marco Bonetti
#
###############################################################################
# This program is free software and open source software; you can redistribute
# it and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit
# http://www.gnu.org/licenses/gpl.html
#
###############################################################################
# E-mail: mbonetti at gmail dot com
# Web page: http://gregarius.net/
#
###############################################################################
function getLastModif() {
return getProperty('__meta__','meta.lastupdate');
}
function getETag() {
return md5(getLastModif().$_SERVER['PHP_SELF']);
}
function rss_error($message, $severity = RSS_ERROR_ERROR, $render = false) {
if ($render) {
echo "<p class=\"error\">$message</p>\n";
return;
}
if (!isset($GLOBALS['rss'])) {
rss_require('cls/rss.php');
}
$GLOBALS['rss'] -> error($message, $severity);
}
/** this functions checks whether a URI exists */
function getHttpResponseCode($forUri) {
return getUrl($forUri, 255);
}
function getContentType($link, & $contentType) {
$url_parts = @ parse_url($link);
if (empty ($url_parts["host"])) {
return (false);
}
if (!empty ($url_parts["path"])) {
$documentpath = $url_parts["path"];
} else {
$documentpath = "/";
}
if (!empty ($url_parts["query"])) {
$documentpath .= "?".$url_parts["query"];
}
$host = $url_parts["host"];
$port = (array_key_exists('port', $url_parts) ? $url_parts["port"] : "80");
$socket = @ fsockopen($host, $port, $errno, $errstr, 30);
if (!$socket) {
return (false);
}
$ret = false;
fwrite($socket, "GET ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
while (!feof($socket)) {
$line = fgets($socket, 100);
if (preg_match("/Content-Type: (.*)/i", $line, $matches)) {
$contentType = $matches[1];
$ret = true;
break;
}
}
return $ret;
}
// basically strips folder resources from URIs.
// http://pear.php.net/package/HTTP_Client/ --> http://pear.php.net/
function get_host($url, & $host) {
$ret = preg_match("/^(http:\/\/)?([^\/]+)/i", $url, $matches);
$host = $matches[2];
//ensure we have a slash
if (substr($host, -1) != "/") {
$host .= "/";
}
return $ret;
}
/**
* Builds a title out of an already encoded string.
*/
function makeTitle($title) {
// Let us find out if the user has set a title.
$userTitle = _TITLE_;
if (getConfig('rss.output.title')) {
$userTitle = getConfig('rss.output.title');
}
$ret = "". $userTitle ."";
if ($title) {
if (is_array($title)) {
foreach($title as $token) {
$ret .= " ".TITLE_SEP." ".$token;
}
} else {
$ret .= " ".TITLE_SEP." ".$title;
}
}
return $ret;
}
/*** update the given feed(s) **/
function update($id) {
$kses_allowed = getConfig('rss.input.allowed'); //getAllowedTags();
$updatedIds = array ();
$sql = "select id, url, title, mode from ".getTable("channels");
if ($id != "" && is_numeric($id)) {
$sql .= " where id=$id";
$sql .= " and not(mode & ".RSS_MODE_DELETED_STATE.") ";
} else {
$sql .= " where not(mode & ".RSS_MODE_DELETED_STATE.") ";
}
if (getConfig('rss.config.absoluteordering')) {
$sql .= " order by parent, position";
} else {
$sql .= " order by parent, title";
}
$res = rss_query($sql);
while (list ($cid, $url, $title, $mode) = rss_fetch_row($res)) {
// suppress warnings because Magpie is rather noisy
$old_level = error_reporting(E_ERROR);
$rss = fetch_rss($url);
//reset
error_reporting($old_level);
if (!$rss && $id != "" && is_numeric($id)) {
return array (magpie_error(), array ());
}
elseif (!$rss || !($rss->rss_origin & MAGPIE_FEED_ORIGIN_HTTP_200) ) {
continue; // no need to do anything if we do not get a 200 OK from the feed
}
// base URL for items in this feed.
if (array_key_exists('link', $rss->channel)) {
$baseUrl = $rss->channel['link'];
} else {
$baseUrl = $url; // The feed is invalid
}
// Keep track of guids we've handled, because some feeds (hello,
// Technorati!) have this insane habit of serving the same item
// twice in the same feed.
$guids = array();
// Allow updates in this feed?
$allowUpdates = getProperty($cid,'rss.input.allowupdates');
if ($allowUpdates === null) {
$allowUpdates = getConfig('rss.input.allowupdates');
}
$itemIdsInFeed = array(); // This variable will store the item id's of the elements in the feed
foreach ($rss->items as $item) {
$item = rss_plugin_hook('rss.plugins.rssitem', $item);
// a plugin might delete this item
if(!isset($item))
continue;
// item title: strip out html tags
$title = array_key_exists('title', $item) ? strip_tags($item['title']) : "";
//$title = str_replace('& ', '& ', $title);
$description = "";
// item content, if any
if (array_key_exists('content', $item) && is_array($item['content']) && array_key_exists('encoded', $item['content'])) {
$description = $item['content']['encoded'];
}
elseif (array_key_exists('description', $item)) {
$description = $item['description'];
}
elseif (array_key_exists('atom_content', $item)) {
$description = $item['atom_content'];
}
elseif (array_key_exists('summary', $item)) {
$description = $item['summary'];
}
else {
$description = "";
}
$md5sum = "";
$guid = "";
if(array_key_exists('guid', $item) && $item['guid'] != "") {
$guid = $item['guid'];
}
elseif(array_key_exists('id', $item) && $item['id'] != "") {
$guid = $item['id'];
}
$guid = trim($guid);
$guid = rss_real_escape_string($guid);
// skip this one if it's an in-feed-dupe
if ($guid && isset($guids[$guid])) {
continue;
}
elseif($guid) {
$guids[$guid] = true;
}
if ($description != "") {
$md5sum = md5($description);
$description = kses($description, $kses_allowed); // strip out tags
if ($baseUrl != "") {
$description = relative_to_absolute($description, $baseUrl);
}
}
// Now let plugins modify the description
$description = rss_plugin_hook('rss.plugins.import.description', $description);
// link
if (array_key_exists('link', $item) && $item['link'] != "") {
$url = $item['link'];
}
elseif (array_key_exists('guid', $item) && $item['guid'] != "") {
$url = $item['guid'];
}
elseif (array_key_exists('link_', $item) && $item['link_'] != "") {
$url = $item['link_'];
}
else {
// fall back to something basic
$url = md5($title);
}
// make sure the url is properly escaped
$url = htmlentities($url, ENT_QUOTES );
$url = rss_real_escape_string($url);
// author
if (array_key_exists('dc', $item) && array_key_exists('creator', $item['dc'])) {
// RSS 1.0
$author = $item['dc']['creator'];
} else if (array_key_exists('author_name', $item)) {
// Atom 0.3
$author = $item['author_name'];
} else {
$author = "";
}
$author = trim(strip_tags($author));
// pubdate
$cDate = -1;
if (array_key_exists('dc', $item) && array_key_exists('date', $item['dc'])) {
// RSS 1.0
$cDate = parse_w3cdtf($item['dc']['date']);
}
elseif (array_key_exists('pubdate', $item)) {
// RSS 2.0 (?)
// We use the second param of strtotime here as a workaround
// of a PHP bug with strtotime. If the pubdate field doesn't
// contain seconds, the strtotime function will use the current
// time to fill in seconds in PHP4. This interferes with the
// update mechanism of gregarius. See ticket #328 for the full
// gory details. Giving a known date as a second param to
// strtotime fixes this problem, hence the 0 here.
$cDate = strtotime($item['pubdate'], 0);
}
elseif (array_key_exists('published',$item)) {
// atom 1.0
$cDate = parse_iso8601($item['published']);
}
elseif (array_key_exists('issued', $item)) {
//Atom, alternative
$cDate = parse_iso8601($item['issued']);
}
elseif (array_key_exists('updated', $item)) {
//Atom, alternative
$cDate = parse_iso8601($item['updated']);
}
elseif (array_key_exists('created', $item)) {
// atom 0.3
$cDate = parse_iso8601($item['created']);
}
// enclosure
if (array_key_exists('enclosure@url', $item) ) {
$enclosure = $item['enclosure@url'];
// If the enclosure is an image, append it to the content
// but only if it isn't there yet
if ($enclosure &&
array_key_exists('enclosure@type', $item) &&
preg_match('#image/(png|gif|jpe?g)#', $item['enclosure@type']) &&
(FALSE == strpos($description,$enclosure))) {
$description = '<img src="'.$enclosure.'" alt="" />' . $description;
$enclosure = '';
}
} else {
$enclosure = "";
}
// drop items with an url exceeding our column length: we couldn't provide a
// valid link back anyway.
if (strlen($url) >= 255) {
continue;
}
$dbtitle = rss_real_escape_string($title);
if (strlen($dbtitle) >= 255) {
$dbtitle=substr($dbtitle,0,254);
}
if ($cDate > 0) {
$sec = "FROM_UNIXTIME($cDate)";
} else {
$sec = "null";
}
// check whether we already have this item
if ($guid) {
$sql = "select id,unread, md5sum, guid, pubdate from ".getTable("item")
." where cid=$cid and guid='$guid'";
} else {
$sql = "select id,unread, md5sum, guid, pubdate from ".getTable("item")
." where cid=$cid and url='$url' and title='$dbtitle'"
." and (pubdate is NULL OR pubdate=$sec)";
}
$subres = rss_query($sql);
list ($indb, $state, $dbmd5sum, $dbGuid, $dbPubDate) = rss_fetch_row($subres);
if ($indb) {
$itemIdsInFeed[] = $indb;
if (!($state & RSS_MODE_DELETED_STATE) && $md5sum != $dbmd5sum) {
// the md5sums do not match.
if($allowUpdates) { // Are we allowed update items in the db?
list ($cid, $indb, $description) =
rss_plugin_hook('rss.plugins.items.updated', array ($cid, $indb, $description));
$sql = "update ".getTable("item")
." set "." description='".rss_real_escape_string($description)."', "
." unread = unread | ".RSS_MODE_UNREAD_STATE
.", md5sum='$md5sum'" . " where cid=$cid and id=$indb";
rss_query($sql);
$updatedIds[] = $indb;
continue;
}
}
} else { // $indb = "" . This must be new item then. In you go.
list ($cid, $dbtitle, $url, $description) =
rss_plugin_hook('rss.plugins.items.new', array ($cid, $dbtitle, $url, $description));
$sql = "insert into ".getTable("item")
." (cid, added, title, url, enclosure,"
." description, author, unread, pubdate, md5sum, guid) "
." values ("."$cid, now(), '$dbtitle', "
." '$url', '".rss_real_escape_string($enclosure)."', '"
.rss_real_escape_string($description)."', '"
.rss_real_escape_string($author)."', "
."$mode, $sec, '$md5sum', '$guid')";
rss_query($sql);
$newIid = rss_insert_id();
$itemIdsInFeed[] = $newIid;
$updatedIds[] = $newIid;
rss_plugin_hook('rss.plugins.items.newiid',array($newIid,$item,$cid));
} // end handling of this item
} // end handling of all the items in this feed
$sql = "update " .getTable("channels") . " set "." itemsincache = '"
. serialize($itemIdsInFeed) . "' where id=$cid";
rss_query($sql);
} // end handling all the feeds we were asked to handle
if ($id != "" && is_numeric($id)) {
if ($rss) {
// when everything went well, return the error code
// and numer of new items
return array ($rss->rss_origin, $updatedIds);
} else {
return array (-1, array ());
}
} else {
return array (-1, $updatedIds);
}
}
function getRootFolder() {
$sql = "select id from ".getTable("folders")."where name = '' order by position asc limit 1";
list($root) = rss_fetch_row(rss_query($sql));
if (!$root) {
$root = 0;
}
return $root;
}
function add_channel($url, $folderid = 0, $title_=null,$descr_=null,$tags = null) {
if (!$url || strlen($url) <= 7) {
return array (-2, "Invalid URL $url");
}
if (!is_numeric($folderid)) {
return array (-2, "Invalid folderid $folderid");
}
$url = sanitize(str_replace('&','&',$url), RSS_SANITIZER_URL);
$urlDB = rss_real_escape_string($url); //htmlentities($url);
$res = rss_query("select count(*) as channel_exists from ".getTable("channels")." where url='$urlDB'");
list ($channel_exists) = rss_fetch_row($res);
if ($channel_exists > 0) {
// fatal
return array (-2, "Looks like you are already subscribed to this channel");
}
$res = rss_query("select 1+max(position) as np from ".getTable("channels"));
list ($np) = rss_fetch_row($res);
if (!$np) {
$np = "0";
}
// Here we go!
//error_reporting(E_ALL);
$old_level = error_reporting(E_ERROR);
$rss = fetch_rss($url);
error_reporting($old_level);
if ($rss) {
if ($title_) {
$title = rss_real_escape_string($title_);
}
elseif (is_object($rss) && array_key_exists('title#', $rss->channel)) {
if (array_key_exists('title', $rss->channel)) {
$title = rss_real_escape_string($rss->channel['title']);
} else {
$title = " ";
}
}
else {
$title = "";
}
if (is_object($rss) && array_key_exists('link', $rss->channel)) {
$siteurl = rss_real_escape_string(htmlentities($rss->channel['link']));
} else {
$siteurl = "";
}
$refreshinterval = 0;
if(is_object($rss) && array_key_exists('syn', $rss->channel)) {
$syn = $rss->channel['syn'];
if(array_key_exists('updateperiod', $syn)) {
if("hourly" == $syn['updateperiod']) {
if(array_key_exists('updatefrequency', $syn)) {
$refreshinterval = 60 * $syn['updatefrequency'];
}
}
}
}
if ($descr_) {
$descr = rss_real_escape_string($descr_);
}
elseif (is_object($rss) && array_key_exists('description', $rss->channel)) {
$descr = rss_real_escape_string($rss->channel['description']);
}
else {
$descr = "";
}
//lets see if this server has a favicon
$icon = "";
if (getConfig('rss.output.showfavicons')) {
// if we got nothing so far, lets try to fall back to
// favicons
if ($icon == "" && $siteurl != "") {
$match = get_host($siteurl, $host);
$uri = "http://".$host."favicon.ico";
if ($match && getContentType($uri, $contentType)) {
if (preg_match("/image\/x-icon/", $contentType)) {
$icon = $uri;
}
}
}
}
$private = preg_match('|(https?://)([^:]+:[^@]+@)(.+)$|',$url);
if ($title != "") {
$title = strip_tags($title);
$descr = strip_tags($descr);
// add channel to root folder by default
if(!$folderid) {
$folderid = getRootFolder();
}
list($title,$urlDB,$siteurl,$folderid,$descr,$icon) =
rss_plugin_hook('rss.plugins.feed.new',
array ($title,$urlDB,$siteurl,$folderid,$descr,$icon));
$mode = RSS_MODE_UNREAD_STATE;
if ($private) {
$mode |= RSS_MODE_PRIVATE_STATE;
}
$sql = "insert into ".getTable("channels")
." (title, url, siteurl, parent, descr, dateadded, icon, position, mode, daterefreshed)"
." values ('$title', '$urlDB', '$siteurl', $folderid, '$descr', now(), '$icon', $np, $mode, '0000-00-00 00:00:00')";
rss_query($sql);
$newid = rss_insert_id();
if ($icon && cacheFavicon($icon)) {
rss_query("update " . getTable("channels") . " set icon='blob:".$icon."'"
." where id=$newid");
}
if($tags != "") {
__exp__submitTag($newid,$tags,"'channel'");
}
if(false == empty($refreshinterval)) {
setProperty($newid, 'rss.config.refreshinterval', $refreshinterval);
}
return array ($newid, "");
} else {
// non-fatal, will look further
return array (-1, "I'm sorry, I couldn't extract a valid RSS feed from <a href=\"$url\">$url</a>.");
}
} else {
global $MAGPIE_ERROR;
$retError = "I'm sorry, I couldn't retrieve <a href=\"$url\">$url</a>.";
if ($MAGPIE_ERROR) {
$retError .= "\n<br />$MAGPIE_ERROR\n";
}
// non-fatal, will look further
return array (-1, $retError);
}
}
/**
* Replaces relative urls with absolute ones for anchors and images
* Credits: Julien Mudry
*/
function relative_to_absolute($content, $feed_url) {
preg_match('/(http|https|ftp):\/\//', $feed_url, $protocol);
$server_url = preg_replace("/(http|https|ftp|news):\/\//", "", $feed_url);
$server_url = preg_replace("/\/.*/", "", $server_url);
if ($server_url == '') {
return $content;
}
if (isset($protocol[0])) {
$new_content = preg_replace('/href="\//', 'href="'.$protocol[0].$server_url.'/', $content);
$new_content = preg_replace('/src="\//', 'src="'.$protocol[0].$server_url.'/', $new_content);
} else {
$new_content = $content;
}
return $new_content;
}
/**
* parse an ISO 8601 date, losely based on parse_w3cdtf from MagpieRSS
*/
function parse_iso8601($date_str) {
# regex to match wc3dtf
$pat = "/(\d{4})-?(\d{2})-?(\d{2})T?(\d{2}):?(\d{2})(:?(\d{2}\.?\d*))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";
if (preg_match($pat, $date_str, $match)) {
list ($year, $month, $day, $hours, $minutes, $seconds)
= array (@$match[1], @$match[2], @$match[3], @$match[4], @$match[5], @$match[6]);
// calc epoch for current date assuming GMT
$epoch = gmmktime($hours, $minutes, intval($seconds), $month, $day, $year);
$offset = 0;
if (@$match[10] == 'Z') {
// zulu time, aka GMT
} else {
list ($tz_mod, $tz_hour, $tz_min) = array (@$match[8], @$match[9], @$match[10]);
// zero out the variables
if (!$tz_hour) {
$tz_hour = 0;
}
if (!$tz_min) {
$tz_min = 0;
}
$offset_secs = (($tz_hour * 60) + $tz_min) * 60;
// is timezone ahead of GMT? then subtract offset
if ($tz_mod == '+') {
$offset_secs = $offset_secs * -1;
}
$offset = $offset_secs;
}
$epoch = $epoch + $offset;
return $epoch;
} else {
return -1;
}
}
/**
* Returns the relative path of the install dir, e.g:
* http://host.com/thing/ -> "/thing/"
* http://host.com/ -> "/"
*/
function getPath($path='') {
static $ret;
if ($ret === NULL) {
$ret = dirname($_SERVER['PHP_SELF']);
if (defined('RSS_FILE_LOCATION') && eregi(RSS_FILE_LOCATION."\$", $ret)) {
$ret = substr($ret, 0, strlen($ret) - strlen(RSS_FILE_LOCATION));
}
if (substr($ret, -1) == "\\") { // Take off trailing backslash
$ret = substr($ret, 0, -1);
}
if (substr($ret, -1) != "/") { // Add a frontslash
$ret .= "/";
}
}
return $ret . $path;
}
$dummy = getPath();
/**
* builds an url for an archive link
*/
function makeArchiveUrl($ts, $channel, $cid, $dayView) {
$ret = getPath();
if (getConfig('rss.output.usemodrewrite')) {
if ($channel) {
$ret .= "$channel/";
}
$ret .= rss_date(($dayView ? 'Y/m/d/' : 'Y/m/'), $ts, false);
} else {
$ret .= "feed.php?";
if ($cid) {
$ret .= "channel=$cid&";
}
$ret .= "y=".rss_date('Y', $ts, false)
."&m=".rss_date('m', $ts, false)
. ($dayView ? ("&d=".rss_date('d', $ts, false)) : "");
}
return $ret;
}
/**
* Fetches a remote URL and returns the content
*/
function getUrl($url, $maxlen = 0) {
//Bug: in windows, scheme returned by parse_url contains the drive letter
//of the file so a test like !isset(scheme) does not work
//maybe it would be better to only use is_file() which only detect
//local files?
$urlParts = parse_url($url);
if (@is_file($url) || (!isset($urlParts['scheme']) && !isset($urlParts['host'])) ) {
//local file!
$c = "";
$h = @fopen($url, "r");
if ($h) {
while (!feof($h)) {
$c .= @fread($h, 8192);
}
}
@fclose($h);
return $c;
}
rss_require('extlib/Snoopy.class.inc');
$client = new Snoopy();
$client->agent = MAGPIE_USER_AGENT;
$client->use_gzip = getConfig('rss.output.compression');
if ($maxlen) {
$client->maxlength = $maxlen;
}
@ $client->fetch($url);
return $client->results;
}
/**
* Feed Autodiscovery
*
* returns an array of all (hopefully) rss/atom/rdf feeds in the document,
* pointed by $url.
* See http://diveintomark.org/archives/2002/06/02/important_change_to_the_link_tag
*
* @param string $url URL of a web document containing <link> elements
* @return array Array of feed URLs
*/
function extractFeeds($url) {
rss_require('extlib/uri_util.php');
$cnt = getUrl($url);
$ret = array ();
//find all link tags
if (preg_match_all('|<link\s+\w*=["\'][^"\']+["\']+[^>]*>|Uis', $cnt, $res)) {
while (list ($id, $match) = each($res[0])) {
// we only want '<link alternate=...'
if (strpos(strtolower($match), 'alternate') &&
!strpos(strtolower($match), 'stylesheet') && // extract the attributes
preg_match_all('|([a-zA-Z]*)=["\']([^"\']*)|', $match, $res2, PREG_SET_ORDER)) {
$tmp = array ();
//populate the return array: attr_name => attr_value
while (list ($id2, $match2) = each($res2)) {
$attr = strtolower(trim($match2[1]));
$val = trim($match2[2]);
// make sure we have absolute URI's
if ($attr == "href") {
$val = absolute_uri($val, $url);
}
$tmp[$attr] = $val;
}
$ret[] = $tmp;
}
}
}
return $ret;
}
function real_strip_slashes($string) {
if (stripslashes($string) == $string) {
return $string;
}
return real_strip_slashes(stripslashes($string));
}
function rss_htmlspecialchars($in) {
return htmlspecialchars($in, ENT_NOQUOTES,
(getConfig('rss.output.encoding') ? getConfig('rss.output.encoding') : DEFAULT_OUTPUT_ENCODING));
}
function firstNwords($text, $count=7) {
$new = "";
$expr = '/(.+?\s+){1,' . $count . '}/';
if ( preg_match($expr, $text, $matches) ) {
$result = $matches[0] . '...';
$new = preg_replace('/(\r\n|\r|\n)/', ' ', $result);
$new = strip_tags($new);
}
return $new;
}
/** Props: mr at bbp dot biz - http://ch2.php.net/substr */
function html_substr($posttext, $minimum_length = 200, $length_offset = 20, $cut_words = FALSE, $dots = TRUE) {
// $minimum_length:
// The approximate length you want the concatenated text to be
// $length_offset:
// The variation in how long the text can be in this example text
// length will be between 200 and 200-20=180 characters and the
// character where the last tag ends
// Reset tag counter & quote checker
$tag_counter = 0;
$quotes_on = FALSE;
// Check if the text is too long
if (strlen($posttext) > $minimum_length) {
// Reset the tag_counter and pass through (part of) the entire text
$c = 0;
for ($i = 0; $i < strlen($posttext); $i++) {
// Load the current character and the next one
// if the string has not arrived at the last character
$current_char = substr($posttext,$i,1);
if ($i < strlen($posttext) - 1) {
$next_char = substr($posttext,$i + 1,1);
} else {
$next_char = "";
}
// First check if quotes are on
if (!$quotes_on) {
// Check if it's a tag
// On a "<" add 3 if it's an opening tag (like <a href...)
// or add only 1 if it's an ending tag (like </a>)
if ($current_char == '<') {
if ($next_char == '/') {
$tag_counter += 1;
} else {
$tag_counter += 3;
}
}
// Slash signifies an ending (like </a> or ... />)
// substract 2
if ($current_char == '/' && $tag_counter <> 0)
$tag_counter -= 2;
// On a ">" substract 1
if ($current_char == '>')
$tag_counter -= 1;
// If quotes are encountered, start ignoring the tags
// (for directory slashes)
if ($current_char == '"')
$quotes_on = TRUE;
} else {
// IF quotes are encountered again, turn it back off
if ($current_char == '"')
$quotes_on = FALSE;
}
// Count only the chars outside html tags
if($tag_counter == 2 || $tag_counter == 0) {
$c++;
}
// Check if the counter has reached the minimum length yet,
// then wait for the tag_counter to become 0, and chop the string there
if ($c > $minimum_length - $length_offset && $tag_counter == 0 && ($next_char == ' ' || $cut_words == TRUE)) {
$posttext = substr($posttext,0,$i + 1);
if($dots) {
$posttext .= '...';
}
return $posttext;
}
}
}
return $posttext;
}
function showViewForm($args) { //$curValue, $show_private) {
list($curValue, $show_private) = $args;
// post back to self, we should be able to handle the request, shouldn't we.
echo "\n<form action=\"".$_SERVER['REQUEST_URI'] ."\" method=\"post\" id=\"frmShow\">\n"
."<p><label for=\"".SHOW_WHAT."\">".__('Show items: ')."</label>\n"
."<select name=\"".SHOW_WHAT."\" id=\"".SHOW_WHAT."\" "." onchange=\"document.getElementById('frmShow').submit();\">\n"
."\t<option value=\"".SHOW_UNREAD_ONLY."\"" . (SHOW_UNREAD_ONLY == $curValue ? " selected=\"selected\"" : "") . ">".__('Unread only')."</option>\n"
."\t<option value=\"".SHOW_READ_AND_UNREAD."\"" . (SHOW_READ_AND_UNREAD == $curValue ? " selected=\"selected\"" : "") . ">".__('Read and unread')."</option>\n"
."</select>"
."</p>\n";
/*
if(isLoggedIn()) {
echo "<p><label for=\"chkPrivate\">".__('Show Private:')."</label>\n"
."<input type=\"checkbox\" name=\"chkPrivate\" id=\"chkPrivate\" value=\"1\" onchange=\"if(false == document.getElementById('chkPrivate').checked) { document.getElementById('chkPrivate').value = 0; document.getElementById('chkPrivate').checked = true; } document.getElementById('frmShow').submit();\"" . (1 == $show_private ? " checked" : "") . ">\n"
."</p>\n";
}
*/
echo "</form>\n";
}
function getUnreadCount($cid, $fid) {
static $_uccache = array();
$key_ = "key $cid $fid key";
if (isset($_uccache[$key_])) {
return $_uccache[$key_];
}
$sql = "select count(*) from " . getTable("item") ."i "
."inner join " . getTable('channels')." c on c.id = i.cid "
." where i.unread & ".RSS_MODE_UNREAD_STATE. " and not(i.unread & " .
RSS_MODE_DELETED_STATE .") "
." and not(c.mode & ".RSS_MODE_DELETED_STATE.") ";
if (hidePrivate()) {
$sql .= " and not(i.unread & ".RSS_MODE_PRIVATE_STATE.")";
}
if ($cid) {
$sql .= " and c.id=$cid ";
}
elseif ($fid) {
$sql .= " and c.parent=$fid ";
}
$res = rss_query($sql);
list ($_uccache[$key_]) = rss_fetch_row($res);
return $_uccache[$key_];
}
function rss_locale_date ($fmt, $ts, $addTZOffset = true) {
if (isset($_SERVER["WINDIR"])) {
//%e doesnt' exists under windows!
$fmt=str_replace("%e","%#d",$fmt);
}
if ($addTZOffset) {
return utf8_encode(strftime($fmt, $ts +3600 * getConfig('rss.config.tzoffset')));
}
return utf8_encode(strftime($fmt, $ts));
}
function rss_date($fmt, $ts, $addTZOffset = true) {
if ($addTZOffset) {
return date($fmt, $ts +3600 * getConfig('rss.config.tzoffset'));
}
return date($fmt, $ts);
}
function _pf($msg) {
if (defined('PROFILING') && PROFILING && isset($GLOBALS['rss']) && method_exists($GLOBALS['rss'], "_pf")) {
$GLOBALS['rss'] -> _pf($msg);
}
}
function guessTransportProto() {
if (defined ('RSS_SERVER_PROTO')) {
return RSS_SERVER_PROTO;
}
if (array_key_exists("SERVER_PORT",$_SERVER)) {
if ($_SERVER["SERVER_PORT"] == 443) {
$proto = "https://";
} else {
$proto = "http://";
}
} else {
// best effort
$proto = "http://";
}
return $proto;
}
function rss_redirect($url = "") {
header("Location: " .
(guessTransportProto() . $_SERVER['HTTP_HOST'] . getPath() . $url));
}
/*
fixes #117.
http://www.php.net/manual/en/function.getallheaders.php
*/
function rss_getallheaders() {
$headers = array();
foreach($_SERVER as $h=>$v) {
if(ereg('HTTP_(.+)',$h,$hp)) {
$headers[$hp[1]]=$v;
}
}
return $headers;
}
// moved from ajax.php
function __exp__submitTag($id,$tags,$type = "'item'") {
$tags = strip_tags($tags);
$ftags = utf8_encode( preg_replace(ALLOWED_TAGS_REGEXP,'', trim($tags)));
$tarr = array_slice(explode(" ",$ftags),0,MAX_TAGS_PER_ITEM);
$ftags = implode(" ",__priv__updateTags($id,$tarr,$type));
return "$id,". $ftags;
}
function __priv__updateTags($fid,$tags,$type) {
rss_query("delete from " .getTable('metatag')