summaryrefslogtreecommitdiff
path: root/inc/SimplePie.php
blob: 1201888a04dc2d3b1800ddc4609fc1fc40b99adb (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
<?php
/****************************************************
SIMPLE PIE
A Simple PHP-Based RSS/Atom Parser
Simplifies the process of displaying the values of commonly used feed tags.

Version: 1.0 Beta
Updated: 29 January 2006
Copyright: 2004-2006 Ryan Parman, Geoffrey Sneddon
http://www.simplepie.org

LICENSE:
Creative Commons Attribution License 2.5
http://www.creativecommons.org/licenses/by/2.5/

You are free:
    - to copy, distribute, display, and perform the work
    - to make derivative works
    - to make commercial use of the work

Under the following conditions:
    - Attribution: You must attribute the work in the manner specified by the author or licensor.

For any reuse or distribution, you must make clear to others the license terms of this work.  Any
of these conditions can be waived if you get permission from the copyright holder.  Your fair use
and other rights are in no way affected by the above.

SPECIFICS OF REQUIRED ATTRIBUTION:
You can use this software for anything you want, but this entire notice must always remain in place.

Please submit all bug reports and feature requests to the SimplePie forums.
http://support.simplepie.org

****************************************************/


class SimplePie {

    // SimplePie Information
    var $name = 'SimplePie';
    var $version = '1.0 Beta';
    var $build = '20060129';
    var $url = 'http://www.simplepie.org/';
    var $useragent;
    var $linkback;

    // Run-time Variables
    var $rss_url;
    var $encoding;
    var $xml_dump = false;
    var $caching = true;
    var $max_minutes = 60;
    var $cache_location = './cache';
    var $replace_headers = false;

    // RSS Auto-Discovery Variables
    var $parsed_url;
    var $local;
    var $elsewhere;

    // XML Parsing Variables
    var $xml;
    var $tagName;
    var $insideItem;
    var $insideChannel;
    var $insideImage;
    var $insideAuthor;
    var $descriptionPriority;
    var $lastDescription;
    var $useGuid = false;
    var $itemNumber = 0;
    var $data = false;




    /****************************************************
    CONSTRUCTOR
    Initiates a couple of variables
    ****************************************************/
    function SimplePie() {
        $this->useragent = $this->name . '/' . $this->version . ' (Feed Parser; ' . $this->url . '; Allow like Gecko) Build/' . $this->build;
        $this->linkback = '<a href="' . $this->url . '" title="' . $this->name . ' ' . $this->version . '">' . $this->name . ' ' . $this->version . '</a>';
    }




    /****************************************************
    CONFIGURE OPTIONS
    Set various options (feed URL, XML dump, caching, etc.)
    ****************************************************/
    // Feed URL
    function feed_url($url) {
        $url = $this->fix_protocol($url, 1);
        $this->rss_url = $url;
        return true;
    }

    // XML Dump
    function enable_xmldump($enable) {
        $this->xml_dump = $enable;
        return true;
    }

    // Caching
    function enable_caching($enable) {
        $this->caching = $enable;
        return true;
    }

    // Cache Timeout
    function cache_max_minutes($minutes) {
        $this->max_minutes = (int) $minutes;
        return true;
    }

    // Cache Location
    function cache_location($location) {
        $this->cache_location = (string) $location;
        return true;
    }

    // Replace H1, H2, and H3 tags with the less important H4 tags.
    function replace_headers($enable) {
        $this->replace_headers = (bool) $enable;
        return true;
    }





    /****************************************************
    MAIN SIMPLEPIE FUNCTION
    Rewrites the feed so that it actually resembles XML, processes the XML,
    and builds an array from the feed.
    ****************************************************/
    function init() {

        // If this is a .Mac Photocast, change it to the real URL.
        if (stristr($this->rss_url, 'http://photocast.mac.com')) {
            $this->rss_url = preg_replace('%http://photocast.mac.com%i', 'http://web.mac.com', $this->rss_url);
        }

        // Return the User-Agent string to the website's logs.
        ini_set('user_agent', $this->useragent);

        // Clear all outdated cache from the server's cache folder
        $this->clear_cache($this->cache_location, $this->max_minutes);

        if ($this->rss_url) {
            // Read the XML file for processing.
            $cache_filename = $this->cache_location . '/' . urlencode($this->rss_url) . '.spc';
            if ($this->caching && !$this->xml_dump && substr($this->rss_url, 0, 7) == 'http://' && file_exists($cache_filename)) {
                if ($fp = fopen($cache_filename, 'r')) {
                    $data = '';
                    while (!feof($fp)) {
                        $data .= fread($fp, 2048);
                    }
                    fclose($fp);
                    $mp_rss = unserialize($data);
                    if (empty($mp_rss)) {
                        $this->caching = false;
                        return $this->init();
                    } elseif (isset($mp_rss['feed_url'])) {
                        $this->rss_url = $mp_rss['feed_url'];
                        return $this->init();
                    } else {
                        $this->data = $mp_rss;
                        return true;
                    }
                } else {
                    $this->caching = false;
                    return $this->init();
                }
            } else {
                // Get the file
                $mp_rss = $this->get_file($this->rss_url);

                // Check if file is a feed or a webpage
                // If it's a webpage, auto-discover the feed and re-pass it to init()
                $discovery = $this->rss_locator($mp_rss, $this->rss_url);
                if ($discovery) {
                    if ($discovery == 'nofeed') {
                        return false;
                    } else {
                        $this->rss_url = $discovery;
                        if ($this->caching && substr($this->rss_url, 0, 7) == 'http://') {
                            if ($this->is_writeable_createable($cache_filename)) {
                                $fp = fopen($cache_filename, 'w');
                                fwrite($fp, serialize(array('feed_url' => $discovery)));
                                fclose($fp);
                            } else trigger_error("$cache_filename is not writeable", E_USER_WARNING);
                        }
                        return $this->init();
                    }
                }

                // Trim out any whitespace at the beginning or the end of the file
                $mp_rss = trim($mp_rss);

                // Get encoding
                // Support everything from http://www.php.net/manual/en/ref.mbstring.php#mbstring.supported-encodings
                $use_mbstring = false;
                if (preg_match('/encoding=["](.*)["]/Ui', $mp_rss, $encoding)) {
                    switch (strtolower($encoding[1])) {

                        // UCS-4
                        case 'ucs-4':
                        case 'ucs4':
                        case 'utf-32':
                        case 'utf32':
                            $encoding = 'UCS-4';
                            $use_mbstring = true;
                            break;

                        // UCS-4BE
                        case 'ucs-4be':
                        case 'ucs4be':
                        case 'utf-32be':
                        case 'utf32be':
                            $encoding = 'UCS-4BE';
                            $use_mbstring = true;
                            break;

                        // UCS-4LE
                        case 'ucs-4le':
                        case 'ucs4le':
                        case 'utf-32le':
                        case 'utf32le':
                            $encoding = 'UCS-4LE';
                            $use_mbstring = true;
                            break;

                        // UCS-2
                        case 'ucs-2':
                        case 'ucs2':
                        case 'utf-16':
                        case 'utf16':
                            $encoding = 'UCS-2';
                            $use_mbstring = true;
                            break;

                        // UCS-2BE
                        case 'ucs-2be':
                        case 'ucs2be':
                        case 'utf-16be':
                        case 'utf16be':
                            $encoding = 'UCS-2BE';
                            $use_mbstring = true;
                            break;

                        // UCS-2LE
                        case 'ucs-2le':
                        case 'ucs2le':
                        case 'utf-16le':
                        case 'utf16le':
                            $encoding = 'UCS-2LE';
                            $use_mbstring = true;
                            break;

                        // UCS-32
                        case 'ucs-32':
                        case 'ucs32':
                            $encoding = 'UCS-32';
                            $use_mbstring = true;
                            break;

                        // UCS-32BE
                        case 'ucs-32be':
                        case 'ucs32be':
                            $encoding = 'UCS-32BE';
                            $use_mbstring = true;
                            break;

                        // UCS-32LE
                        case 'ucs-32le':
                        case 'ucs32le':
                            $encoding = 'UCS-32LE';
                            $use_mbstring = true;
                            break;

                        // UCS-16
                        case 'ucs-16':
                        case 'ucs16':
                            $encoding = 'UCS-16';
                            $use_mbstring = true;
                            break;

                        // UCS-16BE
                        case 'ucs-16be':
                        case 'ucs16be':
                            $encoding = 'UCS-16BE';
                            $use_mbstring = true;
                            break;

                        // UCS-16LE
                        case 'ucs-16le':
                        case 'ucs16le':
                            $encoding = 'UCS-16LE';
                            $use_mbstring = true;
                            break;

                        // UTF-7
                        case 'utf-7':
                        case 'utf7':
                            $encoding = 'UTF-7';
                            $use_mbstring = true;
                            break;

                        // UTF7-IMAP
                        case 'utf-7-imap':
                        case 'utf7-imap':
                        case 'utf7imap':
                            $encoding = 'UTF7-IMAP';
                            $use_mbstring = true;
                            break;

                        // ASCII
                        case 'us-ascii':
                        case 'ascii':
                            $encoding = 'US-ASCII';
                            $use_mbstring = true;
                            break;

                        // EUC-JP
                        case 'euc-jp':
                        case 'eucjp':
                            $encoding = 'EUC-JP';
                            $use_mbstring = true;
                            break;

                        // EUCJP-win
                        case 'euc-jp-win':
                        case 'eucjp-win':
                        case 'eucjpwin':
                            $encoding = 'EUCJP-win';
                            $use_mbstring = true;
                            break;

                        // Shift_JIS
                        case 'shift_jis':
                        case 'sjis':
                        case '932':
                            $encoding = 'Shift_JIS';
                            $use_mbstring = true;
                            break;

                        // SJIS-win
                        case 'sjis-win':
                        case 'sjiswin':
                        case 'shift_jis-win':
                            $encoding = 'SJIS-win';
                            $use_mbstring = true;
                            break;

                        // ISO-2022-JP
                        case 'iso-2022-jp':
                        case 'iso2022-jp':
                        case 'iso2022jp':
                            $encoding = 'ISO-2022-JP';
                            $use_mbstring = true;
                            break;

                        // JIS
                        case 'jis':
                            $encoding = 'JIS';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-1
                        case 'iso-8859-1':
                        case 'iso8859-1':
                            $encoding = 'ISO-8859-1';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-2
                        case 'iso-8859-2':
                        case 'iso8859-2':
                            $encoding = 'ISO-8859-2';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-3
                        case 'iso-8859-3':
                        case 'iso8859-3':
                            $encoding = 'ISO-8859-3';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-4
                        case 'iso-8859-4':
                        case 'iso8859-4':
                            $encoding = 'ISO-8859-4';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-5
                        case 'iso-8859-5':
                        case 'iso8859-5':
                            $encoding = 'ISO-8859-5';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-6
                        case 'iso-8859-6':
                        case 'iso8859-6':
                            $encoding = 'ISO-8859-6';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-7
                        case 'iso-8859-7':
                        case 'iso8859-7':
                            $encoding = 'ISO-8859-7';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-8
                        case 'iso-8859-8':
                        case 'iso8859-8':
                            $encoding = 'ISO-8859-8';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-9
                        case 'iso-8859-9':
                        case 'iso8859-9':
                            $encoding = 'ISO-8859-9';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-10
                        case 'iso-8859-10':
                        case 'iso8859-10':
                            $encoding = 'ISO-8859-10';
                            $use_mbstring = true;
                            break;

                        // mbstring functions don't appear to support 11 & 12

                        // ISO-8859-13
                        case 'iso-8859-13':
                        case 'iso8859-13':
                            $encoding = 'ISO-8859-13';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-14
                        case 'iso-8859-14':
                        case 'iso8859-14':
                            $encoding = 'ISO-8859-14';
                            $use_mbstring = true;
                            break;

                        // ISO-8859-15
                        case 'iso-8859-15':
                        case 'iso8859-15':
                            $encoding = 'ISO-8859-15';
                            $use_mbstring = true;
                            break;

                        // byte2be
                        case 'byte2be':
                            $encoding = 'byte2be';
                            $use_mbstring = true;
                            break;

                        // byte2le
                        case 'byte2le':
                            $encoding = 'byte2le';
                            $use_mbstring = true;
                            break;

                        // byte4be
                        case 'byte4be':
                            $encoding = 'byte4be';
                            $use_mbstring = true;
                            break;

                        // byte4le
                        case 'byte4le':
                            $encoding = 'byte4le';
                            $use_mbstring = true;
                            break;

                        // BASE64
                        case 'base64':
                        case 'base-64':
                            $encoding = 'BASE64';
                            $use_mbstring = true;
                            break;

                        // HTML-ENTITIES
                        case 'html-entities':
                        case 'htmlentities':
                            $encoding = 'HTML-ENTITIES';
                            $use_mbstring = true;
                            break;

                        // 7bit
                        case '7bit':
                        case '7-bit':
                            $encoding = '7bit';
                            $use_mbstring = true;
                            break;

                        // 8bit
                        case '8bit':
                        case '8-bit':
                            $encoding = '8bit';
                            $use_mbstring = true;
                            break;

                        // EUC-CN
                        case 'euc-cn':
                        case 'euccn':
                            $encoding = 'EUC-CN';
                            $use_mbstring = true;
                            break;

                        // EUC-TW
                        case 'euc-tw':
                        case 'euctw':
                            $encoding = 'EUC-TW';
                            $use_mbstring = true;
                            break;

                        // EUC-KR
                        case 'euc-kr':
                        case 'euckr':
                            $encoding = 'EUC-KR';
                            $use_mbstring = true;
                            break;

                        // Traditional Chinese, mainly used in Taiwan
                        case 'big5':
                        case '950':
                            $encoding = 'BIG5';
                            $use_mbstring = true;
                            break;

                        // Simplified Chinese, national standard character set
                        case 'gb2312':
                        case '936':
                            $encoding = 'GB2312';
                            $use_mbstring = true;
                            break;

                        // Big5 with Hong Kong extensions, Traditional Chinese
                        case 'big5-hkscs':
                            $encoding = 'BIG5-HKSCS';
                            $use_mbstring = true;
                            break;

                        // Windows-specific Cyrillic
                        case 'cp1251':
                        case 'windows-1251':
                        case 'win-1251':
                        case '1251':
                            $encoding = 'Windows-1251';
                            $use_mbstring = true;
                            break;

                        // Windows-specific Western Europe
                        case 'cp1252':
                        case 'windows-1252':
                        case '1252':
                            $encoding = 'Windows-1252';
                            $use_mbstring = true;
                            break;

                        // Russian
                        case 'koi8-r':
                        case 'koi8-ru':
                        case 'koi8r':
                            $encoding = 'KOI8-R';
                            $use_mbstring = true;
                            break;

                        // HZ
                        case 'hz':
                            $encoding = 'HZ';
                            $use_mbstring = true;
                            break;

                        // ISO-2022-KR
                        case 'iso-2022-kr':
                        case 'iso2022-kr':
                        case 'iso2022kr':
                            $encoding = 'ISO-2022-KR';
                            $use_mbstring = true;
                            break;

                        // DOS-specific Cyrillic
                        case 'cp866':
                        case 'ibm866':
                        case '866':
                            $encoding = 'cp866';
                            $use_mbstring = true;
                            break;

                        // DOS-specific Cyrillic
                        case 'cp936':
                        case 'ibm936':
                        case '936':
                            $encoding = 'cp936';
                            $use_mbstring = true;
                            break;

                        // DOS-specific Cyrillic
                        case 'cp959':
                        case 'ibm959':
                        case '959':
                            $encoding = 'cp959';
                            $use_mbstring = true;
                            break;

                        // DOS-specific Cyrillic
                        case 'cp949':
                        case 'ibm949':
                        case '949':
                        case 'uhc':
                            $encoding = 'cp949';
                            $use_mbstring = true;
                            break;

                        // Default to UTF-8
                        default:
                            $encoding = 'UTF-8';
                            break;
                    }
                } else {
                    $encoding = 'UTF-8';
                }
                $this->encoding = $encoding;

                // If function is available, convert characters to UTF-8, and overwrite $this->encoding
                if (function_exists('mb_convert_encoding') && ($use_mbstring)) {
                    $mp_rss = mb_convert_encoding($mp_rss, 'UTF-8', $encoding);
                    $this->encoding = 'UTF-8';
                }

                // Encode entities within CDATA
                $mp_rss = preg_replace_callback('/<!\[CDATA\[(.*)\]\]>/Uis', array(&$this, 'cdata_encode'), $mp_rss);

                // Strip out all CDATA tags
                $mp_rss = str_replace('<![CDATA[', '', $mp_rss);
                $mp_rss = str_replace(']]>', '', $mp_rss);

                // Replace any other brackets with their entities
                $mp_rss = str_replace('[', '&#91;', $mp_rss); // [ character -- problems with CDATA
                $mp_rss = str_replace(']', '&#93;', $mp_rss); // ] character -- problems with CDATA

                // Fix tags inside code and pre tags.
                $mp_rss = preg_replace_callback('/<code[ .*]?>(.*)<\/code>/Uis', array(&$this, 'code_encode'), $mp_rss);
                $mp_rss = preg_replace_callback('/<pre[ .*]?>(.*)<\/pre>/Uis', array(&$this, 'code_encode'), $mp_rss);

                // Create an array of all of the elements that SimplePie supports the parsing of.
                $sp_elements = array(
                    // These elements are supported by SimplePie (alphabetical)
                    'category',
                    'content',
                    'copyright',
                    'dc:creator',
                    'dc:date',
                    'dc:description',
                    'dc:language',
                    'dc:subject',
                    'description',
                    'guid',
                    'id',
                    'height',
                    'issued',
                    'language',
                    'logo',
                    'name',
                    'pubDate',
                    'published',
                    'subtitle',
                    'summary',
                    'tagline',
                    'title',
                    'url',
                    'width',

                    // These elements are not currently supported by SimplePie
                    // We'll just CDATA them to be safe.
                    'comments',
                    'dc:contributor',
                    'dc:coverage',
                    'dc:format',
                    'dc:identifier',
                    'dc:publisher',
                    'dc:relation',
                    'dc:rights',
                    'dc:source',
                    'dc:title',
                    'dc:type',
                    'docs',
                    'generator',
                    'icon',
                    'itunes:author',
                    'itunes:duration',
                    'itunes:email',
                    'itunes:explicit',
                    'itunes:keywords',
                    'itunes:name',
                    'itunes:subtitle',
                    'itunes:summary',
                    'lastBuildDate',
                    'managingEditor',
                    'media:credit',
                    'media:text',
                    'rating',
                    'rights',
                    'sy:updatePeriod',
                    'sy:updateFrequency',
                    'sy:updateBase',
                    'ttl',
                    'updated',
                    'webMaster'
                );

                // Store the number of elements in the above array.
                // Helps execution time in JavaScript, why not PHP?
                $sp_elements_size = sizeof($sp_elements);

                $mp_rss = str_replace('content:encoded', 'content', $mp_rss);
                $mp_rss = preg_replace("%<!DOCTYPE(.*)>%i", '', $mp_rss); // Strip out the DOCTYPE since we don't use it anyways.

                for ($i=0; $i < $sp_elements_size; $i++) {
                    $full = $sp_elements[$i];
                    $short = substr($full, 0, -1);

                    $mp_rss = preg_replace('%<' . $short . "[^>/]+((\"[^\"]*\")|(\'[^\']*\')|([^>/]*))((\s*)?|([^\s]))/>%i", '<' . $full . '></' . $full . '>', $mp_rss);
                    $mp_rss = preg_replace('%<' . $full . '(.|\s)*?>%i', '<' . $full . '\\0<![CDATA[', $mp_rss);
                    $mp_rss = preg_replace('%<' . $full . '<' . $full . '%i', '<' . $full, $mp_rss);
                    $mp_rss = preg_replace('%</' . $full . '(.|\s)*?>%i', ']]></' . $full . '>', $mp_rss);
                }

                // Separate rules for some tags.
                if (preg_match('/<rdf:rdf/i', $mp_rss) || preg_match('/<rss/i', $mp_rss)) {
                    // <author>
                    $mp_rss = preg_replace("%<autho[^>/]+((\"[^\"]*\")|(\'[^\']*\')|([^>/]*))((\s*)|([^\s]))/>%i", '<author></author>', $mp_rss);
                    $mp_rss = preg_replace('%<author(.|\s)*?>%i', '<author\\0<![CDATA[', $mp_rss);
                    $mp_rss = preg_replace('%<author<author%i', '<author', $mp_rss);
                    $mp_rss = preg_replace('%</author(.|\s)*?>%i', ']]></author>',$mp_rss);

                    // <link>
                    $mp_rss = preg_replace('%<link(.|\s)*?>%i', '<link\\0<![CDATA[', $mp_rss);
                    $mp_rss = preg_replace('%<link<link%i', '<link', $mp_rss);
                    $mp_rss = preg_replace('%</link(.|\s)*?>%i', ']]></link>', $mp_rss);
                }

                // Strip out HTML tags that might cause various security problems.
                // Based on recommendations by Mark Pilgrim at:
                // http://diveintomark.org/archives/2003/06/12/how_to_consume_rss_safely
                $tags_to_strip = array(
                    'html',
                    'body',
                    'script',
                    'noscript',
                    'embed',
                    'object',
                    'frameset',
                    'frame',
                    'iframe',
                    'meta',
                    'style',
                    'param',
                    'doctype',
                    'form',
                    'input',
                    'blink',
                    'marquee',
                    'font'
                );
                foreach ($tags_to_strip as $tag) {
                    $mp_rss = preg_replace('/<\/?' . $tag . '(.|\s)*?>/i', '', $mp_rss);
                }

                // Strip out HTML attributes that might cause various security problems.
                // Based on recommendations by Mark Pilgrim at:
                // http://diveintomark.org/archives/2003/06/12/how_to_consume_rss_safely
                $stripAttrib = '\' (style|id|class)="(.*?)"\'i';
                $mp_rss = preg_replace($stripAttrib, '', $mp_rss);

                // Swap out problematic characters.
                $mp_rss = str_replace('', '', $mp_rss); // UTF-8 BOM
                $mp_rss = preg_replace("/�|�|–|—/", '--', $mp_rss); // em/en dash
                $mp_rss = preg_replace("/�|�|’|‘/", "'", $mp_rss); // single-quotes
                $mp_rss = preg_replace("/�|�|“|”/", '"', $mp_rss); // double-quotes
                $mp_rss = preg_replace("/�/", '', $mp_rss); // bad character

                // Swap out funky characters with their named entities.
                // Code is from Feedsplitter at chxo.com
                $mp_rss = preg_replace(array('/\&([a-z\d\#]+)\;/i',
                    '/\&/',
                    '/\#\|\|([a-z\d\#]+)\|\|\#/i',
                    '/(\=\"\-\/\%\?\!\'\(\)\[\\{\}\ \#\+\,\@_])/e'
                    ),
                    array('#||\\1||#',
                    '&amp;',
                    '&\\1;',
                    "'&#'.ord('\\1').';'"
                    ),
                    $mp_rss
                );

                // Get rid of invalid UTF-8 characters
                // Code is from chregu at blog.bitflux.ch
                if (function_exists('iconv'))
                    $mp_rss = iconv('UTF-8', 'UTF-8//IGNORE', $mp_rss);

                if ($this->replace_headers) {
                    // Replace H1, H2, and H3 tags with the less important H4 tags.
                    // This is because on a site, the more important headers might make sense,
                    // but it most likely doesn't fit in the context of RSS-in-a-webpage.
                    $mp_rss = preg_replace('/<h[1-3](.|\s)*?>/i', '<h4>', $mp_rss);
                    $mp_rss = preg_replace('/<\/h[1-3](.|\s)*?>/i', '</h4>', $mp_rss);
                }

                // Find the domain name of the feed being read.
                $feed_path = parse_url($this->rss_url);
                if (isset($feed_path['host'])) {
                    $feed_host = $feed_path['host'];

                    // Change certain types of relative URL's into absolute URL's
                    $mp_rss = str_replace('href="/', 'href="http://' . $feed_host . '/', $mp_rss);
                    $mp_rss = str_replace('href=&quot;/', 'href="http://' . $feed_host . '/', $mp_rss);
                    $mp_rss = str_replace('src="/', 'src="http://' . $feed_host . '/', $mp_rss);
                    $mp_rss = str_replace('src=&quot;/', 'src="http://' . $feed_host . '/', $mp_rss);
                }

                // If XML Dump is enabled, send feed to the page and quit.
                if ($this->xml_dump) {
                    header("Content-type: text/xml; charset=" . $this->encoding);
                    echo $mp_rss;
                    exit;
                }

                $this->xml = xml_parser_create($this->encoding);
                xml_parser_set_option($this->xml, XML_OPTION_SKIP_WHITE, 1);
                xml_set_object($this->xml, $this);
                xml_set_character_data_handler($this->xml, 'dataHandler');
                xml_set_element_handler($this->xml, 'startHandler', 'endHandler');
                if (xml_parse($this->xml, $mp_rss))
                {
                    $this->data['feedinfo']['encoding'] = $this->encoding;
                    if ($this->caching && substr($this->rss_url, 0, 7) == 'http://') {
                        if ($this->is_writeable_createable($cache_filename)) {
                            $fp = fopen($cache_filename, 'w');
                            fwrite($fp, serialize($this->data));
                            fclose($fp);
                        }
                        else trigger_error("$cache_filename is not writeable", E_USER_WARNING);
                    }
                    return true;
                }
                else
                {
                    trigger_error(sprintf('XML error: %s at line %d, column %d', xml_error_string(xml_get_error_code($this->xml)), xml_get_current_line_number($this->xml), xml_get_current_column_number($this->xml)), E_USER_WARNING);
                    $this->data = array();
                    xml_parser_free($this->xml);
                    return false;
                }
            }
        }
        else return false;
    }




    /****************************************************
    GET FEED ENCODING
    ****************************************************/
    function get_encoding() {
        if (isset($this->encoding)) {
            return $this->encoding;
        } else if (isset($this->data['feedinfo']['encoding'])) {
            return $this->data['feedinfo']['encoding'];
        } else return false;
    }




    /****************************************************
    GET FEED VERSION NUMBER
    ****************************************************/
    function get_version() {
        if (isset($this->data['feedinfo'])) {
            return (isset($this->data['feedinfo']['version'])) ? $this->data['feedinfo']['type'] . ' ' . $this->data['feedinfo']['version'] : $this->data['feedinfo']['type'];
        }
        else return false;
    }




    /****************************************************
    SUBSCRIPTION URLS
    This allows people to subscribe to the feed in various services.
    ****************************************************/
    function subscribe_url() {
        if (isset($this->rss_url)) {
            $temp = $this->fix_protocol($this->rss_url, 1);
            if (strstr($temp, '../')) {
                $retVal = substr_replace($temp, '', 0, 3);
                return $retVal;
            }
            else return $temp;
        }
        else return false;
    }

    function subscribe_feed() {
        if (isset($this->rss_url)) {
            return $this->fix_protocol($this->rss_url, 2);
        }
        else return false;
    }

    function subscribe_podcast() {
        if (isset($this->rss_url)) {
            return $this->fix_protocol($this->rss_url, 3);
        }
        else return false;
    }

    function subscribe_aol() {
        return 'http://feeds.my.aol.com/add.jsp?url=' . rawurlencode($this->subscribe_url());
    }

    function subscribe_bloglines() {
        return 'http://www.bloglines.com/sub/' . rawurlencode($this->subscribe_url());
    }

    function subscribe_google() {
        return 'http://fusion.google.com/add?feedurl=' . rawurlencode($this->subscribe_url());
    }

    function subscribe_msn() {
        return 'http://my.msn.com/addtomymsn.armx?id=rss&amp;ut=' . rawurlencode($this->subscribe_url()) . '&amp;ru=' . rawurlencode($this->get_feed_link());
    }

    function subscribe_newsburst() {
        return 'http://www.newsburst.com/Source/?add=' . rawurlencode($this->subscribe_url());
    }

    function subscribe_newsgator() {
        return 'http://www.newsgator.com/ngs/subscriber/subext.aspx?url=' . rawurlencode($this->subscribe_url());
    }

    function subscribe_odeo() {
        return 'http://www.odeo.com/listen/subscribe?feed=' . rawurlencode($this->subscribe_url());
    }

    function subscribe_pluck() {
        return 'http://client.pluck.com/pluckit/prompt.aspx?GCID=C12286x053&amp;a=' . rawurlencode($this->subscribe_url());
    }

    function subscribe_podnova() {
        return 'http://www.podnova.com/index_your_podcasts.srf?action=add&url=' . rawurlencode($this->subscribe_url());
    }

    function subscribe_rojo() {
        return 'http://www.rojo.com/add-subscription?resource=' . rawurlencode($this->subscribe_url());
    }

    function subscribe_yahoo() {
        return 'http://add.my.yahoo.com/rss?url=' . rawurlencode($this->subscribe_url());
    }




    /****************************************************
    "ADD TO" LINKS
    Allows people to easily add news postings to social bookmarking sites.
    ****************************************************/
    function add_to_delicious($gitArrayValue) {
        return 'http://del.icio.us/post/?v=3&amp;url=' . rawurlencode($this->get_item_permalink($gitArrayValue)) . '&amp;title=' . rawurlencode($this->get_item_title($gitArrayValue));
    }

    function add_to_digg($gitArrayValue) {
        return 'http://digg.com/submit?phase=2&URL=' . rawurlencode($this->get_item_permalink($gitArrayValue));
    }

    function add_to_furl($gitArrayValue) {
        return 'http://www.furl.net/storeIt.jsp?u=' . rawurlencode($this->get_item_permalink($gitArrayValue)) . '&amp;t=' . rawurlencode($this->get_item_title($gitArrayValue));
    }

    function add_to_myweb20($gitArrayValue) {
        return 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=' . rawurlencode($this->get_item_permalink($gitArrayValue)) . '&amp;t=' . rawurlencode($this->get_item_title($gitArrayValue));
    }

    function add_to_newsvine($gitArrayValue) {
        return 'http://www.newsvine.com/_wine/save?u=' . rawurlencode($this->get_item_permalink($gitArrayValue)) . '&amp;h=' . rawurlencode($this->get_item_title($gitArrayValue));
    }

    function add_to_reddit($gitArrayValue) {
        return 'http://reddit.com/submit?url=' . rawurlencode($this->get_item_permalink($gitArrayValue)) . '&amp;title=' . rawurlencode($this->get_item_title($gitArrayValue));
    }

    function add_to_spurl($gitArrayValue) {
        return 'http://www.spurl.net/spurl.php?v=3&amp;url=' . rawurlencode($this->get_item_permalink($gitArrayValue)) . '&amp;title=' . rawurlencode($this->get_item_title($gitArrayValue));
    }




    /****************************************************
    SEARCHES
    Metadata searches
    ****************************************************/
    function search_technorati($gitArrayValue) {
        return 'http://www.technorati.com/search/' . rawurlencode($this->get_item_permalink($gitArrayValue));
    }




    /****************************************************
    PARSE OUT GENERAL FEED-RELATED DATA
    ****************************************************/
    // Reads the feed's title
    function get_feed_title() {
        if (isset($this->data['info']['name']))
            return $this->prep_output($this->data['info']['name']);
        else return false;
    }

    // Reads the feed's link (URL)
    function get_feed_link() {
        if (isset($this->data['info']['link']))
            return $this->prep_output($this->data['info']['link']);
        else return false;
    }

    // Reads the feed's description
    function get_feed_description() {
        if (isset($this->data['info']['description']))
            return $this->prep_output($this->data['info']['description']);
        else return false;
    }

    // Reads the feed's copyright information.
    function get_feed_copyright() {
        if (isset($this->data['info']['copyright']))
            return $this->prep_output($this->data['info']['copyright']);
        else return false;
    }

    // Reads the feed's language
    function get_feed_language() {
        if (isset($this->data['info']['language']))
            return $this->prep_output($this->data['info']['language']);
        else return false;
    }




    /****************************************************
    PARSE OUT IMAGE-RELATED DATA
    Apparently Atom doesn't have feed images.
    ****************************************************/
    // Check if an image element exists (returns true/false)
    function get_image_exist() {
        return (isset($this->data['info']['image']['url'])) ? true : false;
    }

    // Get the image title (to be used in alt and/or title)
    function get_image_title() {
        if (isset($this->data['info']['image']['title']))
            return $this->prep_output($this->data['info']['image']['title']);
        else return false;
    }

    // The path to the actual image
    function get_image_url() {
        return (isset($this->data['info']['image']['url'])) ? $this->data['info']['image']['url'] : false;
    }

    // The URL that the image is supposed to link to.
    function get_image_link() {
        return (isset($this->data['info']['image']['link'])) ? $this->data['info']['image']['link'] : false;
    }

    // Get the image width
    function get_image_width() {
        return (isset($this->data['info']['image']['width'])) ? $this->data['info']['image']['width'] : false;
    }

    // Get the image height
    function get_image_height() {
        return (isset($this->data['info']['image']['height'])) ? $this->data['info']['image']['height'] : false;
    }




    /****************************************************
    PARSE OUT ITEM-RELATED DATA
    Most of these have one parameter: position in array.
    ****************************************************/
    // Get the size of the array of items (for use in a for-loop)
    function get_item_quantity() {
        return (isset($this->data['items'])) ? sizeof($this->data['items']) : 0;
    }

    // Get the title of the item
    function get_item_title($gitArrayValue) {
        if (isset($this->data['items'][$gitArrayValue]['title']))
            return $this->prep_output($this->data['items'][$gitArrayValue]['title']);
        else return false;
    }

    // Get the description of the item
    function get_item_description($gitArrayValue) {
        if (isset($this->data['items'][$gitArrayValue]['description']))
            return $this->prep_output($this->data['items'][$gitArrayValue]['description']);
        else return false;
    }

    // Get the category of the item
    function get_item_category($gitArrayValue) {
        if (isset($this->data['items'][$gitArrayValue]['category']))
            return $this->prep_output($this->data['items'][$gitArrayValue]['category']);
        else return false;
    }

    // Get the author of the item
    function get_item_author($gitArrayValue) {
        if (isset($this->data['items'][$gitArrayValue]['author']))
            return $this->prep_output(implode(', ', $this->data['items'][$gitArrayValue]['author']));
        else return false;
    }

    // Get the date of the item
    // Also, allow users to set the format of how dates are displayed on a webpage.
    function get_item_date($gitArrayValue, $date_format = 'j F Y, g:i a') {
        return (isset($this->data['items'][$gitArrayValue]['date'])) ? date($date_format, $this->data['items'][$gitArrayValue]['date']) : false;
    }

    // Get the Permalink of the item (checks for link, then guid)
    function get_item_permalink($gitArrayValue) {
        // If there is a link, take it. Fine.
        if (isset($this->data['items'][$gitArrayValue]['link'])) {
            return $this->data['items'][$gitArrayValue]['link'];
        }

        // If there isn't, check for a guid.
        else if (isset($this->data['items'][$gitArrayValue]['guid'])) {
            return $this->data['items'][$gitArrayValue]['guid'];
        }

        // If there isn't, check for an enclosure, if that exists, give that.
        else if ($this->get_item_enclosure($gitArrayValue)) {
            return $this->get_item_enclosure($gitArrayValue);
        }
        else return false;
    }

    // Get the enclosure of the item
    function get_item_enclosure($gitArrayValue) {
        return (isset($this->data['items'][$gitArrayValue]['enclosure'])) ? $this->data['items'][$gitArrayValue]['enclosure'] : false;
    }




    /****************************************************
    FIX PROTOCOL
    Convert feed:// and no-protocol URL's to http://
    Feed is allowed to have no protocol.  Local files are toggled in init().
    This is an internal function and is not intended to be used publically.

    $http=1, http://www.domain.com/feed.xml (absolute)
    $http=2, feed://www.domain.com/feed.xml (absolute)
    $http=3, podcast://www.domain.com/feed.xml (absolute)
    ****************************************************/
    function fix_protocol($mp_feed_proto, $http = 1) {
        $url = $mp_feed_proto;

        // Swap out feed://http:// for http://-only
        if (stristr($mp_feed_proto, 'feed://http://')) {
            $url = substr_replace($mp_feed_proto, 'http://', 0, 14);
        }

        // Swap out feed:http:// for http://
        else if (stristr($mp_feed_proto, 'feed:http://' )) {
            $url = substr_replace($mp_feed_proto, 'http://', 0, 12);
        }

        // Swap out feed:// protocols in favor of http:// protocols
        else if (stristr($mp_feed_proto, 'feed://' )) {
            $url = substr_replace($mp_feed_proto, 'http://', 0, 7);
        }

        // Swap out feed:www. for http://www.
        else if (stristr($mp_feed_proto, 'feed:')) {
            $url = substr_replace($mp_feed_proto, 'http://', 0, 5);
        }

        // If it doesn't have http:// in it, and doesn't exist locally, add http://
        else if (!stristr($mp_feed_proto, 'http://') && !file_exists($mp_feed_proto)) {
            $url = "http://$url";
        }

        if ($http == 1) return $url;
        else if ($http == 2) {
            if (strstr($url, 'http://')) {
                $url = substr_replace($url, 'feed', 0, 4);
                return $url;
            }
            else return $url;
        }
        else if ($http == 3) {
            if (strstr($url, 'http://')) {
                $url = substr_replace($url, 'podcast', 0, 4);
                return $url;
            }
            else return $url;
        }
    }




    /****************************************************
    AUTO DISCOVERY
    Based upon http://diveintomark.org/archives/2002/08/15/ultraliberal_rss_locator
    This function enables support for RSS auto-discovery.
    ****************************************************/
    function rss_locator($data, $url) {

        $this->url = $url;
        $this->parsed_url = parse_url($url);
        if (!isset($this->parsed_url['path'])) {
            $this->parsed_url['path'] = '/';
        }

        // Check is the URL we're given is a feed
        if ($this->is_feed($data, false)) {
            return false;
        }

        // Feeds pointed to by LINK tags in the header of the page (autodiscovery)
        $stage1 = $this->check_link_elements($data);
        if ($stage1) {
            return $stage1;
        }

        // Grab all the links in the page, and put them into two arrays (local, and external)
        if ($this->get_links($data)) {

            // <A> links to feeds on the same server ending in ".rss", ".rdf", ".xml", or ".atom"
            $stage2 = $this->check_link_extension($this->local);
            if ($stage2) {
                return $stage2;
            }

            // <A> links to feeds on the same server containing "rss", "rdf", "xml", or "atom"
            $stage3 = $this->check_link_body($this->local);
            if ($stage3) {
                return $stage3;
            }

            // <A> links to feeds on external servers ending in ".rss", ".rdf", ".xml", or ".atom"
            $stage4 = $this->check_link_extension($this->elsewhere);
            if ($stage4) {
                return $stage4;
            }

            // <A> links to feeds on external servers containing "rss", "rdf", "xml", or "atom"
            $stage5 = $this->check_link_body($this->elsewhere);
            if ($stage5) {
                return $stage5;
            }
        }

        return 'nofeed';
    }

    function check_link_elements($data) {
        if (preg_match_all('/<link (.*)>/siU', $data, $matches)) {
            foreach($matches[1] as $match) {
                if (preg_match('/type=[\'|"](application\/rss\+xml|application\/atom\+xml|application\/rdf\+xml|application\/xml\+rss|application\/xml\+atom|application\/xml\+rdf|application\/xml|application\/x\.atom\+xml|text\/xml)[\'|"]/iU', $match, $type)) {
                    if (preg_match('/href=[\'|"](.*)[\'|"]/iU', $match, $href)) {
                        $href = $this->absolutize_url($href[1]);
                        if ($this->is_feed($href)) {
                            return $href;
                        }
                    }
                }
            }
        } else return false;
    }

    function check_link_extension($array) {
        foreach ($array as $value) {
            $parsed = @parse_url($value);
            if (isset($parsed['path'])) {
                $ext = strtolower(pathinfo($parsed['path'], PATHINFO_EXTENSION));
                if (($ext == 'rss' || $ext == 'rdf' || $ext == 'xml' || $ext == 'atom') && $this->is_feed($value)) {
                    return $this->absolutize_url($value);
                }
            }
        }
        return false;
    }

    function check_link_body($array) {
        foreach ($array as $value) {
            $value2 = parse_url($value);
            if (!empty($value2['path'])) {
                if (strlen(pathinfo($value2['path'], PATHINFO_EXTENSION)) > 0) {
                    $value3 = substr_replace($value, '', strpos($value, $value2['path'])+strpos($value2['path'], pathinfo($value2['path'], PATHINFO_EXTENSION))-1, strlen(pathinfo($value2['path'], PATHINFO_EXTENSION))+1);
                } else {
                    $value3 = $value;
                }
                if ((stristr($value3, 'rss') || stristr($value3, 'rdf') || stristr($value3, 'xml') || stristr($value3, 'atom') || stristr($value3, 'feed')) && $this->is_feed($value)) {
                    return $this->absolutize_url($value);
                }
            }
        }
        return false;
    }

    function get_links($data) {
        if (preg_match_all('/href=["](.*)["]/iU', $data, $matches)) {
            $this->parse_links($matches);
        }
        if (preg_match_all('/href=[\'](.*)[\']/iU', $data, $matches)) {
            $this->parse_links($matches);
        }
        if (preg_match_all('/href=(.*)[ |>]/iU', $data, $matches)) {
            foreach ($matches[1] as $key => $value) {
                if (substr($value, 0, 1) == '"' || substr($value, 0, 1) == "'") {
                    unset($matches[1][$key]);
                }
            }
            $this->parse_links($matches);
        }
        if (!empty($this->local) || !empty($this->elsewhere)) {
            $this->local = array_unique($this->local);
            $this->elsewhere = array_unique($this->elsewhere);
            return true;
        } else return false;
    }

    function parse_links($matches) {
        foreach ($matches[1] as $match) {
            if (strtolower(substr($match, 0, 11)) != 'javascript:') {
                $parsed = parse_url($match);
                if (!isset($parsed['host']) || $parsed['host'] == $this->parsed_url['host']) {
                    $this->local[] = $this->absolutize_url($match);
                } else {
                    $this->elsewhere[] = $this->absolutize_url($match);
                }
            }
        }
    }

    function is_feed($data, $is_url = true) {
        if ($is_url) {
            $data = $this->get_file($data);
        }
        if (stristr($data, '<html') || stristr($data, '<head') || stristr($data, '<body')) {
            return false;
        } else if (stristr($data, '<rss') || stristr($data, '<rdf:rdf') || stristr($data, '<feed')) {
            return true;
        } else {
            return false;
        }
    }

    function absolutize_url($href) {
        if (stristr($href, 'http://') !== false) {
            return $href;
        } else {
            $full_url = $this->parsed_url['scheme'] . '://' . $this->parsed_url['host'];
            if (isset($this->parsed_url['port'])) {
                $full_url .= ':' . $this->parsed_url['port'];
            }
            if ($href{0} != '/') {
                $full_url .= dirname($this->parsed_url['path']);
                if (substr($full_url, -1) != '/') {
                    $full_url .= '/';
                }
            }
            $full_url .= $href;
            return $full_url;
        }
    }




    /****************************************************
    DISPLAY IMAGES
    Some websites have a setting that blocks images from being loaded
    into other pages.  This gets around those blocks by spoofing the referrer.
    ****************************************************/
    function display_image($image_url) {
        $image = $this->get_file($image_url);
        $suffix = explode('.', $image_url);
        $suffix = array_pop($suffix);

        switch($suffix) {
            case 'bmp':
                $mime='image/bmp';
                break;
            case 'gif':
                $mime='image/gif';
                break;
            case 'ico':
                $mime='image/icon';
                break;
            case 'jpe':
            case 'jpg':
            case 'jpeg':
                $mime='image/jpeg';
                break;
            case 'jfif':
                $mime='image/pipeg';
                break;
            case 'png':
                $mime='image/png';
                break;
            case 'tif':
            case 'tiff':
                $mime='image/tiff';
                break;
            default:
                $mime='image';
        }

        header('Content-type: ' . $mime);
        echo $image;
    }




    /****************************************************
    DELETE OUTDATED CACHE FILES
    By adam[at]roomvoter[dot]com
    This function deletes cache files that have not been used in a hour.
    ****************************************************/
    function clear_cache($path, $max_minutes=60) {
        if (is_dir($path) ) {
            $handle = opendir($path);

            while (false !== ($file = readdir($handle))) {
                if ($file != '.' && $file != '..' && pathinfo($file, PATHINFO_EXTENSION) == 'spc') {
                    $diff = (time() - filemtime("$path/$file"))/60;
                    if ($diff > $max_minutes) unlink("$path/$file");
                }
            }
            closedir($handle);
        }
    }




    /****************************************************
    SEES IF A FILE IS WRITEABLE, OR CREATEABLE
    ****************************************************/
    function is_writeable_createable($file) {
        if (file_exists($file))
            return is_writeable($file);
        else
            return is_writeable(dirname($file));
    }




    /****************************************************
    OPENS A FILE, WITH EITHER FOPEN OR cURL
    ****************************************************/
    function get_file($url) {
        if (substr($url, 0, 7) == 'http://' && extension_loaded('curl')) {
            $gch = curl_init();
            curl_setopt ($gch, CURLOPT_URL, $url);
            curl_setopt ($gch, CURLOPT_HEADER, 0);
            curl_setopt ($gch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt ($gch, CURLOPT_TIMEOUT, 10);
            curl_setopt ($gch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($gch, CURLOPT_REFERER, $url);
            curl_setopt ($gch, CURLOPT_USERAGENT, $this->useragent);
            $data = curl_exec ($gch);
            curl_close ($gch);
        } else {
            if ($fp = fopen($url, 'r')) {
                stream_set_blocking($fp, false);
                stream_set_timeout($fp, 10);
                $status = socket_get_status($fp);
                $data = '';
                while (!feof($fp) && !$status['timed_out']) {
                    $data .= fread($fp, 2048);
                    $status = socket_get_status($fp);
                }
                if ($status['timed_out'])
                    return false;
                fclose($fp);
            } else return false;
        }
        return $data;
    }




    /****************************************************
    GET DATA READY TO BE OUTPUTTED
    ****************************************************/
    function prep_output($data) {
        $data = trim($data);
        if ($this->get_encoding() && $this->get_encoding() == 'UTF-8') {
            // Simple htmlspecialchars_decode for less than PHP 5.1 RC1
            $data = strtr($data, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
        } else {
            $data = html_entity_decode($data);
        }
        return $data;
    }




    /****************************************************
    CALLBACK FUNCTIONS FOR PREG_REPLACE_CALLBACK
    ****************************************************/
    function code_encode($regs) {
        return str_replace($regs[1], htmlspecialchars($regs[1]), $regs[0]);
    }

    function cdata_encode($regs) {
        if (isset($this->encoding) && $this->encoding == 'UTF-8')
            return str_replace($regs[1], htmlspecialchars($regs[1]), $regs[0]);
        else
            return str_replace($regs[1], htmlentities($regs[1]), $regs[0]);
    }




    /****************************************************
    FUNCTIONS FOR XML_PARSE
    ****************************************************/
    function startHandler($parser, $name, $attribs) {
        $this->tagName = $name;
        switch ($name) {
            case 'ITEM':
            case 'ENTRY':
                $this->insideItem = true;
                $this->descriptionPriority = 1000;
                $this->lastDescription = 1000;
                break;

            case 'CHANNEL':
                $this->insideChannel = true;
                break;

            case 'RSS':
                $this->data['feedinfo']['type'] = 'RSS';
                if (!empty($attribs['VERSION'])) {
                    $this->data['feedinfo']['version'] = trim($attribs['VERSION']);
                }
                break;

            case 'RDF:RDF':
                $this->data['feedinfo']['type'] = 'RSS';
                $this->data['feedinfo']['version'] = 1;
                break;

            case 'FEED':
                $this->data['feedinfo']['type'] = 'Atom';
                if (!empty($attribs['VERSION'])) {
                    $this->data['feedinfo']['version'] = trim($attribs['VERSION']);
                }
                if (!empty($attribs['XML:LANG'])) {
                    $this->data['info']['language'] = $attribs['XML:LANG'];
                }
                break;

            case 'IMAGE':
                if ($this->insideChannel) $this->insideImage = true;
                break;

            case 'GUID':
                if (isset($attribs['ISPERMALINK']) && $attribs['ISPERMALINK'] == 'false') $this->useGuid = false;
                else $this->useGuid = true;
                break;
        }

        if ($this->data['feedinfo']['type'] == 'Atom') {
            switch ($name) {
                case 'AUTHOR':
                    $this->insideAuthor = true;
                    break;

                case 'LINK':
                    if (!empty($attribs['HREF'])) {
                        if ($this->insideItem) $this->data['items'][$this->itemNumber]['link'] = $attribs['HREF'];
                        else $this->data['info']['link'] = $attribs['HREF'];
                    }
                    break;
            }
        }

        if ($this->insideItem) {
            if ($name == 'ENCLOSURE' || ($name == 'LINK' && isset($attribs['REL']) && strtolower($attribs['REL']) == 'enclosure')) {
                if (isset($attribs['TYPE']) && strpos($attribs['TYPE'], 'audio/') === 0 || strpos($attribs['TYPE'], 'video/') === 0) $use = true;
                else $use = false;

                if (!isset($attribs['TYPE']) || !$use) {
                    if (isset($attribs['URL'])) $ext = pathinfo($attribs['URL'], PATHINFO_EXTENSION);
                    else if (isset($attribs['HREF'])) $ext = pathinfo($attribs['HREF'], PATHINFO_EXTENSION);

                    if (isset($ext)) {
                        switch (strtolower($ext['extension'])) {
                            case 'aif':
                            case 'aifc':
                            case 'aiff':
                            case 'au':
                            case 'funk':
                            case 'gsd':
                            case 'gsm':
                            case 'it':
                            case 'jam':
                            case 'kar':
                            case 'la':
                            case 'lam':
                            case 'lma':
                            case 'm2a':
                            case 'm3u':
                            case 'mid':
                            case 'midi':
                            case 'mjf':
                            case 'mod':
                            case 'mp2':
                            case 'mp3':
                            case 'mpa':
                            case 'mpg':
                            case 'mpga':
                            case 'my':
                            case 'pfunk':
                            case 'qcp':
                            case 'ra':
                            case 'ram':
                            case 'rm':
                            case 'rmm':
                            case 'rmp':
                            case 'rpm':
                            case 's3m':
                            case 'sid':
                            case 'snd':
                            case 'tsi':
                            case 'tsp':
                            case 'voc':
                            case 'vox':
                            case 'vqe':
                            case 'vqf':
                            case 'vql':
                            case 'wav':
                            case 'xm':
                                $use = true;
                                break;

                            default:
                                $use = false;
                                break;
                        }
                    }
                }
                if (isset($use) && $use) {
                    if (isset($attribs['URL'])) $this->data['items'][$this->itemNumber]['enclosure'] = $attribs['URL'];
                    else if (isset($attribs['HREF'])) $this->data['items'][$this->itemNumber]['enclosure'] = $attribs['HREF'];
                }
            }
        }
    }

    function dataHandler($parser, $data) {
        if ($this->insideItem) {
            switch ($this->tagName) {
                case 'TITLE':
                    $this->data['items'][$this->itemNumber]['title'] = $data;
                break;

                case 'CONTENT':
                    if ($this->descriptionPriority > 0) {
                        if ($this->lastDescription > 0)
                            $this->data['items'][$this->itemNumber]['description'] = $data;
                        else
                            $this->data['items'][$this->itemNumber]['description'] .= $data;
                        $this->lastDescription = 0;
                    }
                break;

                case 'SUMMARY':
                    if ($this->descriptionPriority > 1) {
                        if ($this->lastDescription > 1)
                            $this->data['items'][$this->itemNumber]['description'] = $data;
                        else
                            $this->data['items'][$this->itemNumber]['description'] .= $data;
                        $this->lastDescription = 1;
                    }
                break;

                case 'DC:DESCRIPTION':
                    if ($this->descriptionPriority > 2) {
                        if ($this->lastDescription > 2)
                            $this->data['items'][$this->itemNumber]['description'] = $data;
                        else
                            $this->data['items'][$this->itemNumber]['description'] .= $data;
                        $this->lastDescription = 2;
                    }
                break;

                case 'LONGDESC':
                    if ($this->descriptionPriority > 3) {
                        if ($this->lastDescription > 3)
                            $this->data['items'][$this->itemNumber]['description'] = $data;
                        else
                            $this->data['items'][$this->itemNumber]['description'] .= $data;
                        $this->lastDescription = 3;
                    }
                break;

                case 'DESCRIPTION':
                    if ($this->descriptionPriority > 4) {
                        if ($this->lastDescription > 4)
                            $this->data['items'][$this->itemNumber]['description'] = $data;
                        else
                            $this->data['items'][$this->itemNumber]['description'] .= $data;
                        $this->lastDescription = 4;
                    }
                break;

                case 'LINK':
                    $this->data['items'][$this->itemNumber]['link'] = $data;
                break;

                case 'GUID':
                    if ($this->useGuid) $this->data['items'][$this->itemNumber]['guid'] = $data;
                break;

                case 'PUBDATE':
                case 'DC:DATE':
                case 'ISSUED':
                case 'PUBLISHED':
                    $this->data['items'][$this->itemNumber]['date'] = (stristr($data, '-')) ? strtotime(preg_replace('%(\-|\+)[0-1][0-9](:?)[0-9][0-9]%', '', str_replace('Z', '', trim($data)))) : strtotime(trim($data));
                break;

                case 'CATEGORY':
                case 'DC:SUBJECT':
                    $this->data['items'][$this->itemNumber]['category'] = $data;
                break;

                case 'DC:CREATOR':
                    $this->data['items'][$this->itemNumber]['author'][] = $data;
                break;

                case 'AUTHOR':
                    if ($this->data['feedinfo']['type'] != 'Atom') $this->data['items'][$this->itemNumber]['author'][] = $data;
                break;
            }

            if ($this->insideAuthor) {
                if ($this->tagName == 'NAME') $this->data['items'][$this->itemNumber]['author'][] = $data;
            }
        }

        else if ($this->insideChannel) {
            switch ($this->tagName) {
                case 'TITLE':
                    $this->data['info']['name'] = $data;
                break;

                case 'LINK':
                    $this->data['info']['link'] = $data;
                break;

                case 'DESCRIPTION':
                case 'TAGLINE':
                case 'SUBTITLE':
                    $this->data['info']['description'] = $data;
                break;

                case 'COPYRIGHT':
                    $this->data['info']['copyright'] = $data;
                break;

                case 'LANGUAGE':
                case 'DC:LANGUAGE':
                    $this->data['info']['language'] = $data;
                break;
            }
            if ($this->insideImage) {
                switch ($this->tagName) {
                    case 'TITLE':
                        $this->data['info']['image']['title'] = $data;
                    break;

                    case 'URL':
                        $this->data['info']['image']['url'] = $data;
                    break;

                    case 'LINK':
                        $this->data['info']['image']['link'] = $data;
                    break;

                    case 'WIDTH':
                        $this->data['info']['image']['width'] = $data;
                    break;

                    case 'HEIGHT':
                        $this->data['info']['image']['height'] = $data;
                    break;
                }
            }
        }

        else if (!$this->insideItem && $this->data['feedinfo']['type'] == 'Atom') {
            switch ($this->tagName) {
                case 'TITLE':
                    $this->data['info']['name'] = $data;
                break;

                case 'TAGLINE':
                    $this->data['info']['description'] = $data;
                break;

                case 'COPYRIGHT':
                    $this->data['info']['copyright'] = $data;
                break;
            }
        }
    }

    function endHandler($parser, $name) {
        $this->tagName = '';
        switch ($name) {
            case 'CONTENT':
                $this->descriptionPriority = 0;
            break;
            case 'SUMMARY':
                $this->descriptionPriority = 1;
            break;
            case 'DC:DESCRIPTION':
                $this->descriptionPriority = 2;
            break;
            case 'LONGDESC':
                $this->descriptionPriority = 3;
            break;
            case 'DESCRIPTION':
                $this->descriptionPriority = 4;
            break;
            case 'ITEM':
            case 'ENTRY':
                $this->insideItem = false;
                $this->itemNumber++;
            break;

            case 'CHANNEL':
                $this->insideChannel = false;
            break;

            case 'IMAGE':
                if ($this->insideChannel) $this->insideImage = false;
            break;

            case 'AUTHOR':
                if ($this->data['feedinfo']['type'] == 'Atom') $this->insideAuthor = false;
            break;
        }
    }
}

?>