home *** CD-ROM | disk | FTP | other *** search
/ notepage.net / notepage.net.tar / notepage.net / dev / temp / rss2html.php < prev    next >
PHP Script  |  2015-09-23  |  54KB  |  1,280 lines

  1. <?PHP
  2. //
  3. // rss2html.php RSS feed to HTML webpage script
  4. //
  5. // Copyright 2004-2007 NotePage, Inc.
  6. // http://www.feedforall.com
  7. //
  8. // This script may be used and modified freely for business or personal use
  9. // This script may not be resold in any form
  10. // This script may only be redistributed in its original form
  11. //
  12. //
  13. // $Id: rss2html.php,v 3.13 2008/10/18 12:52:36 housley Exp $
  14. //
  15.  
  16. //
  17. // ==========================================================================
  18. // Configuration options
  19. // ==========================================================================
  20. //
  21. // Set the following variable useFopenURL to one if you want/need to use
  22. // fopen() instead of CURL or FeedForAll_fopen()
  23. $useFopenURL = 0;
  24.  
  25. //
  26. // If XLMFILE is passed as part of the URL, XMLFILE=, then it will be used
  27. // otherwise the the file below is used.
  28. //$XMLfilename = 'http://examlple.com/sample.xml';
  29. $XMLfilename = 'sample.xml';
  30.  
  31. //
  32. // If TEMPLATE is passed as part of the URL. TEMPLATE=, then it will be used
  33. // otherwise the the file below is used.
  34. //$TEMPLATEfilename = 'http://examlple.com/sample-template.html';
  35. $TEMPLATEfilename = 'sample-template.html';
  36.  
  37. //
  38. // Since some feeds may have titles or descriptins in the feed or items that
  39. // are longer then want fits in your HTML page it is possible to trim them
  40. // with the following 4 variables.  A values of 0 (ZERO) displays the full
  41. // length.
  42. // CAUTION:  Do not limit a title or description that has HTML in it, the
  43. //           will not produce a valid HTML page.
  44. $limitFeedTitleLength = 0;        // Not limited, in the URL as FeedTitleLength=
  45. $limitFeedDescriptionLength = 0;  // Not limited, in the URL as FeedDescriptionLength=
  46. $limitItemTitleLength = 0;        // Not limited, in the URL as ItemTitleLength=
  47. $limitItemDescriptionLength = 0;  // Not limited, in the URL as ItemDescriptionLength=
  48.  
  49. //
  50. // date() function documented http://www.php.net/manual/en/function.date.php
  51. $LongDateFormat = 'F jS, Y';    // ie, 'Jan 21st, 2004'
  52. $ShortDateFormat = 'm/d/Y';     // ie, '1/21/2004'
  53. //$ShortDateFormat = 'd/m/Y';     // ie, '21/1/2004'
  54. $LongTimeFormat = 'H:i:s T O';  // ie, '13:24:30 EDT -0400'
  55. $ShortTimeFormat = 'h:i A';     // ie, '1:24 PM'
  56.  
  57. //
  58. // Timezone - If your server is not in the same timezone as you are the timezone
  59. // of the times and dates produced in the above from can be controlled with the
  60. // below code.  Just uncomment the following line and change to the correct
  61. // zonename.  A full list is available here, http://www.php.net/manual/en/timezones.php
  62. // You town.city probably isn't listed, so look for a neighboring major city
  63. // putenv('TZ=America/New_York');
  64.  
  65. //
  66. // Registered user of FeedForAll and FeedForAll Mac product(s) have access
  67. // to a caching module.  This enables it's use if it is installed.
  68. $allowCachingXMLFiles = 0;
  69.  
  70. //
  71. // File access level:  The variable $fileAccessLevel can be used limit what files
  72. // and type of files (local or remote) can be used with rss2html.php
  73. // -1 = Remote files are NOT allowed, only local files allowed for template
  74. //      and feed which have filenames ending in extensions in the
  75. //      $allowedTemplateExtensions and $allowedFeedExtensions lists below
  76. //  0 = Remote files and any local files allowed for template and feed
  77. //  1 = Remote files and only local files allowed for template and feed
  78. //      which have filenames ending in extensions in the
  79. //      $allowedTemplateExtensions and $allowedFeedExtensions lists below
  80. //  2 = No local files allowed, remote files only.
  81. $fileAccessLevel = 1;
  82.  
  83. //
  84. // Allowed file extensions is a list of the allowable extensions for local for
  85. // the template and the feed.  New entries can be added by following the example
  86. // below.
  87. $allowedTemplateExtensions = Array('.html', '.htm', '.shtml');
  88. $allowedFeedExtensions = Array('.xml', '.rss');
  89.  
  90. //
  91. // Destination Encoding:  By default rss2html.php converts all feeds to UTF-8
  92. // and then produces webpages in UTF-8 because UTF-8 is capable of displaying
  93. // all possible characters.
  94. $destinationEncoding = 'UTF-8';
  95.  
  96. //
  97. // Missing Encoding Default:  Some feeds do not specify the character set 
  98. // they are encoded in.  The XML specification states that if there is no
  99. // encoding specified the XML file, all RSS feeds are XML, must be encoded
  100. // in UTF-8, but experience has show differently.  This specifies the 
  101. // encoding that will be used for feeds that don't specify the encoding.
  102. //$missingEncodingDefault = 'UTF-8';
  103. $missingEncodingDefault = 'ISO-8859-1';
  104.  
  105. //
  106. // Escape Ampersand In Links:  Proper HTML requires that a link with an
  107. // apersand in while inside of an HTML page have that '&' converted to
  108. // '&'.
  109. $escapeAmpInLinks = 1;
  110.  
  111. //
  112. // $connectTimeoutLimit allows the limiting the amount of time cURL will
  113. // wait to successfully connect to a remote server.  Use with caution,
  114. // a value too small will cause all connections to fail.
  115. //$connectTimeoutLimit = 30;
  116.  
  117. //
  118. // $hideErrors: This will prevent all error messages from being displayed.
  119. // CAUTION enabling this will cause rss2html.php to fail silently with
  120. // no indication to why there was no output
  121. // $hideErrors = 1;
  122.  
  123. // ==========================================================================
  124. // Below this point of the file there are no user editable options.  Your
  125. // are welcome to make any modifications that you wish to any of the code
  126. // below, but that is not necessary for normal use.
  127. // ==========================================================================
  128.  
  129. // $Log: rss2html.php,v $
  130. // Revision 3.13  2008/10/18 12:52:36  housley
  131. // Update useragent so feedburner won't detect and change functionality
  132. // for us
  133. //
  134. // Revision 3.12  2008/05/14 00:45:46  housley
  135. // Fix for negative MAXITEMS greater then the number of items
  136. //
  137. // Revision 3.11  2007/09/30 15:23:23  housley
  138. // Check for the existance of fsockopen()
  139. //
  140. // Revision 3.10  2007/07/16 16:48:48  housley
  141. // On some systems the exit statements in here was terminating all PHP
  142. // processing.
  143. //
  144. // Revision 3.9  2007/07/16 13:06:37  housley
  145. // Add ~~~NumberOfFeedItems~~~ to allow access to the number of items that
  146. // will be processed for display.
  147. //
  148. // Revision 3.8  2007/07/08 13:42:39  housley
  149. // Create my own version of fopen() to try and get files when cURL is not
  150. // available.  FeedForAll_fopen() is based on just connecting to the server
  151. // and reading the results.
  152. //
  153. // Revision 3.7  2007/06/25 13:55:10  housley
  154. // Fix mistake where ?buildURL would falsely say a file could be opened,
  155. // when in reality it could not be opened
  156. //
  157. // Revision 3.6  2007/06/05 13:39:38  housley
  158. // Enable access to output caching in rss2html.php
  159. //
  160. // Revision 3.5  2007/05/27 14:32:05  housley
  161. // Add a debug message when switching to fopen() because curl_init() does
  162. // not exist
  163. //
  164. // Revision 3.4  2007/05/04 11:54:19  housley
  165. // When checking for caching, check a function only in the caching module
  166. //
  167. // Revision 3.3  2007/05/03 18:52:16  housley
  168. // Fix typo in debug statement
  169. //
  170. // Revision 3.2  2007/05/03 16:14:07  housley
  171. // * It seems the XML parser doesn't like most of the HTML entities, process them by hand
  172. // * The check to display errors was backwards
  173. //
  174. // Revision 3.1  2007/04/24 11:36:09  housley
  175. // Fix an error that prevented using PHP includes to display more then one
  176. // feed on a page
  177. //
  178. // Revision 3.0  2007/04/16 14:23:03  housley
  179. // Release version 3.0 of the scripts
  180. //
  181. // Revision 2.76  2007/04/13 18:30:10  housley
  182. // * Atom:content might need whole string so always make it available
  183. // * atom:content of type xhtml is in a div that needs to be stripped and
  184. //   then used as is.
  185. //
  186. // Revision 2.75  2007/04/11 19:59:43  housley
  187. // Add an option to hide error messages
  188. //
  189. // Revision 2.74  2007/04/11 18:38:08  housley
  190. // Update the user agent to be 3.0
  191. //
  192. // Revision 2.73  2007/04/11 12:13:12  housley
  193. // * Fix the code to limit the number of items
  194. // * Add some debug messages
  195. //
  196. // Revision 2.72  2007/04/11 10:40:38  housley
  197. // Add some debug messages
  198. //
  199. // Revision 2.71  2007/04/10 17:16:45  housley
  200. // Allow the input caching to be set during buildURL, only for one PHP include
  201. // type
  202. //
  203. // Revision 2.70  2007/04/10 15:19:28  housley
  204. // Allow for the caching for the HTML output of rss2html.php
  205. //
  206. // Revision 2.69  2007/04/04 20:55:46  housley
  207. // Add the ability to set CURLOPT_CONNECTTIMEOUT
  208. //
  209. // Revision 2.68  2007/03/30 13:18:33  housley
  210. // Use getArrayOfFields() and getValueOf() to simplfy tag replacement, except
  211. // where special processing needs to be done
  212. //
  213. // Revision 2.67  2007/03/05 18:23:58  housley
  214. // Don't abort processing on XML parse error, just don't do anything else
  215. //
  216. // Revision 2.66  2007/03/05 01:33:44  housley
  217. // Use a command convert and readFile routines
  218. //
  219. // Revision 2.65  2007/03/05 01:19:45  housley
  220. // Rename FeedForAll_rss2html_readFile() to FeedForAll_scripts_readFile()
  221. //
  222. // Revision 2.64  2007/03/04 13:41:53  housley
  223. // * Pass the parsing mode to the item class
  224. // * Cleanup the feed level processing
  225. // * rss2html uses the separate parser too
  226. //
  227. // Revision 2.63  2007/03/03 21:10:09  housley
  228. // * Make the item a full class object
  229. // * Support parsing the iTunes(R) extension
  230. //
  231. // Revision 2.62  2007/02/26 00:33:53  housley
  232. // Fix assignment in comparison
  233. //
  234. // Revision 2.61  2007/02/14 01:05:52  housley
  235. // Add the option of encoding the '#' in URLs in some conditions
  236. //
  237. // Revision 2.60  2007/01/26 14:08:46  housley
  238. // Show a better method to change timezone
  239. //
  240. // Revision 2.59  2007/01/04 19:01:51  housley
  241. // Parse the new rssMesh.php fields:
  242. //
  243. // <rssMesh:feedImageTitle> => ~~~ItemRssMeshFeedImageTitle~~~
  244. // <rssMesh:feedImageUrl> =>  ~~~ItemRssMeshFeedImageUrl~~~
  245. // <rssMesh:feedImageLink> =>  ~~~ItemRssMeshFeedImageLink~~~
  246. // <rssMesh:feedImageDescription> =>  ~~~ItemRssMeshFeedImageDescription~~~
  247. // <rssMesh:feedImageHeight> =>  ~~~ItemRssMeshFeedImageHeight~~~
  248. // <rssMesh:feedImageWidth> =>  ~~~ItemRssMeshFeedImageWidth~~~
  249. //
  250. // Revision 2.58  2006/12/22 16:30:21  housley
  251. // Add a check to see if cURL is allowed to follow redirects
  252. //
  253. // Revision 2.57  2006/11/06 14:59:22  housley
  254. // Minor text changes in buildURL
  255. //
  256. // Revision 2.56  2006/11/06 14:39:24  housley
  257. // Create a new method of including rss2html.php that will work on servers
  258. // with remote URL restrictions (allow_url_fopen) that many ISPs are currently
  259. // using.
  260. //
  261. // Revision 2.55  2006/10/17 16:05:05  housley
  262. // Since some of the newer versions of the XML parser look at the encoding
  263. // in the feed and ignore the one passed in on the call, change the encoding
  264. // in the feed before parsing.
  265. //
  266. // Revision 2.54  2006/09/29 19:50:33  housley
  267. // Add a function to convert & in a certian RSS fields to &, so it will be proper HTML
  268. //
  269. // Revision 2.53  2006/09/22 20:21:55  housley
  270. // Fix the problem of displaying an invalid date with an odd number of items
  271. //
  272. // Revision 2.51  2006/09/04 12:33:17  housley
  273. // Exit after a parser error.  The parser stopped, so should we.
  274. //
  275. // Revision 2.50  2006/08/29 18:58:38  housley
  276. // Changes to handle when there are not string conversion modules
  277. //
  278. // Revision 2.49  2006/08/25 15:09:22  housley
  279. // Add hooks for a new feature in rss2html-pro
  280. //
  281. // Revision 2.48  2006/08/25 11:36:37  housley
  282. // * Add the capability to change the character set that feeds are converted to
  283. // * Allow specifying the encoding to use for feeds that don't specify the encoding
  284. //
  285. // Revision 2.46  2006/08/24 20:23:56  housley
  286. // Over come a well meaning, but very misguided ISP removing file:// from
  287. // all scripts.  Not only did the remove it in a place that was doing good,
  288. // it was extremely simple to bypass.
  289. //
  290. // Revision 2.45  2006/08/21 20:19:32  housley
  291. // Use special routines so that rss2html-pro will work with RSS fields that
  292. // have quotes in them.
  293. //
  294. // Revision 2.43  2006/08/18 23:42:16  housley
  295. // Add hooks for rss2html-pro post processing
  296. //
  297. // Revision 2.42  2006/08/11 17:15:45  housley
  298. // Add the ability to restrict the use of the rss2html.php script.
  299. //
  300. // Revision 2.41  2006/08/09 23:57:35  housley
  301. // If <?xml ... is missing add it
  302. //
  303. // Revision 2.40  2006/08/09 15:32:58  housley
  304. // If mb_string_convert fails, try iconv
  305. //
  306. // Revision 2.39  2006/08/04 19:59:02  housley
  307. // Assuming 0xa9 is (c) was bad
  308. //
  309. // Revision 2.38  2006/08/03 11:06:45  housley
  310. // * Don't change the encoding string in the header
  311. // * Give access to the first category in a feed
  312. //
  313. // Revision 2.37  2006/07/29 14:29:40  housley
  314. // Add support for <category> in items.  If there are more then one <category>
  315. // element, only the first is accessable.  The 2 new tags are
  316. // ~~~ItemCategory~~~ and ~~~ItemCategoryDomain~~~
  317. //
  318. // Revision 2.36  2006/07/29 13:19:23  housley
  319. // Trim any leading BOM because some PHP5 installations have problems if it
  320. // is there.
  321. //
  322. // Revision 2.35  2006/07/27 00:27:03  housley
  323. // * Add support for <source> and <comments> in <item>
  324. // * Add support for <rssMesh:extra>
  325. //
  326. // Revision 2.34  2006/07/21 12:23:36  housley
  327. // * If there is no encoding, default to ISO-8859-1
  328. // * Modify the XML to show the encoding we used
  329. //
  330. // Revision 2.33  2006/07/16 10:33:23  housley
  331. // Force $useFopenURL if cURL is not installed
  332. //
  333. // Revision 2.32  2006/07/13 17:05:08  housley
  334. // Remove space that causes problems
  335. //
  336. // Revision 2.31  2006/07/12 12:48:27  housley
  337. // Try the iconv() conversion option if mb_string_encode() doesn't exist
  338. //
  339. // Revision 2.30  2006/05/28 17:52:10  housley
  340. // Handle no encoding specified in the feed
  341. //
  342. // Revision 2.29  2006/05/28 17:51:28  housley
  343. // Allow displaying of the Creative Commons License URL
  344. //
  345. // Revision 2.28  2006/05/28 14:21:47  housley
  346. // Add additional capabilities to work with enclosures.  The 3 new fields are 
  347. // ~~~ItemEnclosureType~~~, ~~~ItemEnclosureLength~~~ and ~~~ItemEnclosureLengthFormatted~~~
  348. //
  349. // Revision 2.27  2006/05/27 19:27:45  housley
  350. // * Show a more universal TZ offset example
  351. // * Make setting contentEncoded more robust
  352. //
  353. // Revision 2.26  2006/04/08 23:17:22  housley
  354. // A "%" should not be encoded to "%2525", but to "%25"
  355. //
  356. // Revision 2.25  2006/04/08 23:16:17  housley
  357. // Indicate that this is the last parse of XML
  358. //
  359. // Revision 2.24  2006/03/23 12:10:30  housley
  360. // Add a simple way to change the timezone of produced times and dates
  361. //
  362. // Revision 2.23  2006/03/10 14:21:04  housley
  363. // Update the licenses
  364. //
  365. // Revision 2.22  2006/03/06 15:01:57  housley
  366. // Trim white space before and after the XML
  367. //
  368. // Revision 2.21  2006/03/05 15:15:11  housley
  369. // Rename rss2html_CachingExtension.php to FeedForAll_Scripts_CachingExtension.php
  370. //
  371. // Revision 2.20  2006/03/05 14:43:59  housley
  372. // Fix the testing for the character set conversion function
  373. //
  374. // Revision 2.19  2006/02/28 02:00:04  housley
  375. // Add support for ~~~FeedXMLFilename~~~
  376. //
  377. // Revision 2.18  2006/02/26 15:24:15  housley
  378. // Add the capability to limit the length of feed and item titles and descriptions
  379. //
  380. // Revision 2.17  2006/02/13 18:00:27  housley
  381. // Fix the initialization of the item arrays
  382. //
  383. // Revision 2.16  2006/02/12 14:43:18  housley
  384. // If possible convert the feed to UTF-8, for uniformity
  385. //
  386. // Revision 2.15  2006/02/12 00:21:13  housley
  387. // Fix the offsetting of the time
  388. //
  389. // Revision 2.14  2006/01/26 15:52:37  housley
  390. // Fix the error message for opening a feed, it was displaying the template filename.
  391. //
  392. // Revision 2.13  2006/01/08 23:25:44  housley
  393. // Move all user configuration options at the top of the file to make them
  394. // easier to find
  395. //
  396. // Revision 2.12  2005/12/12 16:27:26  housley
  397. // Add an interface to allow FeedForAll_scripts_readFile() to be replaced
  398. // by one that does caching of the XML files
  399. //
  400. // Revision 2.11  2005/12/09 19:08:26  housley
  401. // Remove the first "banner" since IE barfs
  402. //
  403. // Revision 2.10  2005/10/22 18:51:47  housley
  404. // Improve the formatting
  405. //
  406. // Revision 2.9  2005/10/22 14:27:57  housley
  407. // Fix label in buildURL
  408. //
  409. // Revision 2.8  2005/10/22 14:20:31  housley
  410. // Add buildURL to assist in creating properly encoded links.  Show proper
  411. // include methods and contents of the files.
  412. //
  413. // Revision 2.7  2005/10/16 17:54:10  housley
  414. // Improvements when using CURL:
  415. // - Use the requested file as the REFERER, for sites that might require one
  416. // - Allow to follow up to 10 redirects, some sites redirect to real content
  417. //
  418. // Revision 2.6  2005/10/16 17:32:27  housley
  419. // Use lastBuildDate as another possible source if pubDate is empty at the
  420. // <channel> level.
  421. //
  422. // Revision 2.5  2005/09/28 02:08:15  housley
  423. // Fix the storage of pubDate at the feed level
  424. //
  425. // Revision 2.4  2005/09/12 18:56:31  housley
  426. // Set a user agent for both fopen and curl transerfers
  427. //
  428. // Revision 2.3  2005/09/06 22:55:27  housley
  429. // GUID doesn't need urlencode()
  430. //
  431. // Revision 2.2  2005/08/16 19:53:15  housley
  432. // Add the ~~~ItemAuthor~~~ subsitution that uses first <author> and then
  433. // <dc:creator> for its contents
  434. //
  435. // Revision 2.1  2005/08/15 14:49:24  housley
  436. // Convert ' to ' since ' is not HTML
  437. //
  438. // Revision 2.0  2005/07/30 14:09:38  housley
  439. // Allow "allow_url_fopen" to be sellected, incase CURL is not available.
  440. //
  441. //
  442.  
  443. $debugLevel = 0;
  444. //
  445. // If using cURL, make sure it exists
  446. if (($useFopenURL == 0) && !function_exists('curl_init')) {
  447.   $useFopenURL = -1;
  448.   if (isset($debugLevel) && ($debugLevel >= 3)) {
  449.     echo 'DIAG: setting $useFopenURL=-1 because curl_init() doesn\'t exist<br />'."\n";
  450.   }
  451. }
  452.  
  453. if (($useFopenURL == -1) && !function_exists('fsockopen')) {
  454.   $useFopenURL = 1;
  455.   if (isset($debugLevel) && ($debugLevel >= 3)) {
  456.     echo 'DIAG: setting $useFopenURL=1 because fsockopen() doesn\'t exist<br />'."\n";
  457.   }
  458. }
  459.  
  460. if (($useFopenURL == -1) && !function_exists("fsockopen")) {
  461.   $useFopenURL = 1;
  462.   if (isset($debugLevel) && ($debugLevel >= 3)) {
  463.     echo "DIAG: setting \$useFopenURL=1 because fsockopen() doesn't exist<br>\n";
  464.   }
  465. }
  466.  
  467. if ($useFopenURL == 1) {
  468.   ini_set("allow_url_fopen", "1");
  469.   ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1;) Gecko/2008070208 Firefox/3.0.1");
  470. }
  471.  
  472. $FeedMaxItems = 10000;
  473. $NoFutureItems = FALSE;
  474.  
  475. @include('FeedForAll_rss2html_pro.php');
  476.  
  477. if (function_exists('FeedForAll_rss2html_pro') === FALSE) {
  478.   Function FeedForAll_rss2html_pro($source) {
  479.     //
  480.     // This is the place to do any processing that is desired
  481.     return $source;
  482.   }
  483. }
  484.  
  485. if (function_exists('FeedForAll_parseExtensions') === FALSE) {
  486.   Function FeedForAll_parseExtensions() {
  487.     return FALSE;
  488.   }
  489. }
  490.  
  491. @include('FeedForAll_Scripts_CachingExtension.php');
  492.  
  493. @include_once('FeedForAll_XMLParser.inc.php');
  494.  
  495. if (function_exists('FeedForAll_rss2html_limitLength') === FALSE) {
  496.   Function FeedForAll_rss2html_limitLength($initialValue, $limit = 0) {
  497.     if (($limit == 0) || (strlen($initialValue) <= $limit )) {
  498.       // ZERO is for not limited
  499.       return $initialValue;
  500.     }
  501.  
  502.     // Cut the text at the exact point, ignoring if it is in a word.
  503.     $result = substr($initialValue, 0, $limit);
  504.  
  505.     // Check to see if there are any space we can trim at and if it is not
  506.     // too far from where we are
  507.     $lastSpace = strrchr($result,' ');
  508.     if (($lastSpace !== FALSE) && (strlen($lastSpace) < 20)) {
  509.       // lose any incomplete word at the end
  510.       $result = substr($result, 0, -(strlen($lastSpace)));
  511.  
  512.       // Append elipses, ... , to show it was truncated
  513.       $result .= ' ...';
  514.     }
  515.  
  516.     return $result;
  517.   }
  518. }
  519.  
  520. if (function_exists('FeedForAll_rss2html_sizeToString') === FALSE) {
  521.   Function FeedForAll_rss2html_sizeToString($filesize) {
  522.     if ($filesize == '') {
  523.       return '';
  524.     }
  525.     elseif ($filesize >= 1073741824) {
  526.       return number_format($filesize/1073741824, 1, '.', ',').' GBytes';
  527.     }
  528.     elseif ($filesize >= 1048576) {
  529.       return number_format($filesize/1048576, 1, '.', ',').' MBytes';
  530.     }
  531.     elseif ($filesize >= 1024) {
  532.       return number_format($filesize/1024, 1, '.', ',').' KBytes';
  533.     }
  534.     else {
  535.       return $filesize.' Bytes';
  536.     }
  537.   }
  538. }
  539.  
  540. if (function_exists('FeedForAll_rss2html_isTemplate') === FALSE) {
  541.   Function FeedForAll_rss2html_isTemplate($templateData) {
  542.     if ((strstr($templateData, '~~~Feed') !== FALSE) || (strstr($templateData, '~~~Item') !== FALSE)) {
  543.       return TRUE;
  544.     }
  545.     return FALSE;
  546.   }
  547. }
  548.  
  549. if (function_exists('FeedForAll_rss2html_validExtension') === FALSE) {
  550.   Function FeedForAll_rss2html_validExtension($filename, $extensions) {
  551.     $foundValid = FALSE;
  552.     foreach ($extensions as $value) {
  553.       if (strtolower($value) == strtolower(substr($filename, -strlen($value)))) {
  554.         $foundValid = TRUE;
  555.         break;
  556.       }
  557.     }
  558.     return $foundValid;
  559.   }
  560. }
  561.  
  562. if (function_exists('FeedForAll_rss2html_str_replace') === FALSE) {
  563.   Function FeedForAll_rss2html_str_replace($search, $replace, $subject) {
  564.     return str_replace($search, $replace, $subject);
  565.   }
  566. }
  567.  
  568. if (function_exists('FeedForAll_rss2html_encodeURL') === FALSE) {
  569.   Function FeedForAll_rss2html_encodeURL($URLstring, $includePND = 0) {
  570.     $result = '';
  571.     for ($x = 0; $x < strlen($URLstring); $x++) {
  572.       if ($URLstring[$x] == '%') {
  573.         $result = $result.'%25';
  574.       }
  575.       elseif ($URLstring[$x] == '?') {
  576.         $result = $result.'%3f';
  577.       }
  578.       elseif ($URLstring[$x] == '&') {
  579.         $result = $result.'%26';
  580.       }
  581.       elseif ($URLstring[$x] == '=') {
  582.         $result = $result.'%3d';
  583.       }
  584.       elseif ($URLstring[$x] == '+') {
  585.         $result = $result.'%2b';
  586.       }
  587.       elseif ($URLstring[$x] == ' ') {
  588.         $result = $result.'%20';
  589.       }
  590.       elseif ($includePND && ($URLstring[$x] == '#')) {
  591.         $result = $result.'%23';
  592.       }else {
  593.         $result = $result.$URLstring[$x];
  594.       }
  595.     }
  596.     return $result;
  597.   }
  598. }
  599.  
  600. if (function_exists('FeedForAll_rss2html_CreateUniqueLink') === FALSE) {
  601.   Function FeedForAll_rss2html_CreateUniqueLink($title, $description, $link, $guid, $XMLfilename, $itemTemplate) {
  602.     GLOBAL $TEMPLATEfilename;
  603.     $match = Array();
  604.     
  605.     while (preg_match('/~~~ItemUniqueLinkWithTemplate=.*~~~/', $itemTemplate, $match) !== FALSE) {
  606.       if ((count($match) == 0) || ($match[0] == '')) {
  607.         // All done
  608.         return $itemTemplate;
  609.       }
  610.       
  611.       $replace = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'?XMLFILE='.FeedForAll_rss2html_encodeURL($XMLfilename).'&TEMPLATE='.FeedForAll_rss2html_encodeURL($TEMPLATEfilename);
  612.       $itemTemplate = FeedForAll_rss2html_str_replace($match[0], $replace, $itemTemplate);
  613.     }
  614.     if ($title)
  615.     if ($description)
  616.     if ($link)
  617.     if ($guid)
  618.     return $itemTemplate;
  619.   }
  620. }
  621.  
  622. if (function_exists('FeedForAll_rss2html_UseUniqueLink') === FALSE) {
  623.   Function FeedForAll_rss2html_UseUniqueLink($title, $description, $link, $guid) {
  624.     if ($title)
  625.     if ($description)
  626.     if ($link)
  627.     if ($guid)
  628.     return -1;
  629.   }
  630. }
  631.  
  632. if (function_exists('FeedForAll_rss2html_EscapeLink') === FALSE) {
  633.   Function FeedForAll_rss2html_EscapeLink($link) {
  634.     GLOBAL $escapeAmpInLinks;
  635.     
  636.     if ((strstr($link, '://') !== FALSE) && $escapeAmpInLinks) {
  637.       // In HTML a link with an & must be converted to &
  638.       // And for here without :// it is not a link, since relative
  639.       // URLs are not allowed
  640.       $link = str_replace('&', '&', $link);
  641.     }
  642.     return $link;
  643.   }
  644. }
  645.  
  646. if (function_exists('FeedForAll_rss2html_AddIdentity') === FALSE) {
  647.   Function FeedForAll_rss2html_AddIdentity($itemString) {
  648.     return '<!-- HTML generated from an RSS Feed by rss2html.php, http://www.FeedForAll.com/ a NotePage, Inc. product (http://www.notepage.com/) -->'.$itemString;
  649.   }
  650. }
  651.  
  652. if (!isset($_REQUEST['buildURL'])) {
  653.   //
  654.   // Check variables that could be used if URL wrapper are disable or not working
  655.   if (isset($GLOBALS['XMLFILE'])) {
  656.     $XMLfilename = $GLOBALS['XMLFILE'];
  657.   }
  658.   if (isset($GLOBALS['TEMPLATE'])) {
  659.     $TEMPLATEfilename = $GLOBALS['TEMPLATE'];
  660.   }
  661.   if (isset($GLOBALS['FeedTitleLength'])) {
  662.     $limitFeedTitleLength = abs($GLOBALS['FeedTitleLength']);
  663.   }
  664.   if (isset($GLOBALS['FeedDescriptionLength'])) {
  665.     $limitFeedDescriptionLength = abs($GLOBALS['FeedDescriptionLength']);
  666.   }
  667.   if (isset($GLOBALS['ItemTitleLength'])) {
  668.     $limitItemTitleLength = abs($GLOBALS['ItemTitleLength']);
  669.   }
  670.   if (isset($GLOBALS['ItemDescriptionLength'])) {
  671.     $limitItemDescriptionLength = abs($GLOBALS['ItemDescriptionLength']);
  672.   }
  673.   if (isset($GLOBALS['MAXITEMS'])) {
  674.     $FeedMaxItems = $GLOBALS['MAXITEMS'];
  675.   }
  676.   if (isset($GLOBALS['NOFUTUREITEMS'])) {
  677.     $NoFutureItems = TRUE;
  678.   }
  679.  
  680.   
  681.   if (isset($_REQUEST['XMLFILE'])) {
  682.     if (stristr($_REQUEST['XMLFILE'], 'file'.'://')) {
  683.       // Not allowed
  684.       ;
  685.     }
  686.     elseif (stristr($_REQUEST['XMLFILE'], '://')) {
  687.       if ($fileAccessLevel == -1) {
  688.         echo 'Configuration setting prohibit using remote files, exiting'."\n";
  689.         return;
  690.       } else {
  691.         // URL files are allowed
  692.         $XMLfilename = $_REQUEST['XMLFILE'];
  693.       }
  694.     } else {
  695.       if (($fileAccessLevel == 1) || ($fileAccessLevel == -1)) {
  696.         if (FeedForAll_rss2html_validExtension(basename($_REQUEST['XMLFILE']), $allowedFeedExtensions) === FALSE) {
  697.           echo 'Configuration setting prohibit using the specified feed file, exiting'."\n";
  698.           return;
  699.         }
  700.         $XMLfilename = basename($_REQUEST['XMLFILE']);
  701.       }
  702.       elseif ($fileAccessLevel == 2) {
  703.         echo 'Configuration setting prohibit using local files, exiting'."\n";
  704.         return;
  705.       } else {
  706.         // It is local and must be in the same directory
  707.         $XMLfilename = basename($_REQUEST['XMLFILE']);
  708.       }
  709.     }
  710.   }
  711.  
  712.   if (isset($_REQUEST['TEMPLATE'])) {
  713.     if (stristr($_REQUEST['TEMPLATE'], 'file'.'://')) {
  714.       // Not allowed
  715.       ;
  716.     }
  717.     elseif (stristr($_REQUEST['TEMPLATE'], '://')) {
  718.       if ($fileAccessLevel == -1) {
  719.         echo 'Configuration setting prohibit using remote files, exiting'."\n";
  720.         return;
  721.       } else {
  722.         // URL files are allowed
  723.         $TEMPLATEfilename = $_REQUEST['TEMPLATE'];
  724.       }
  725.     } else {
  726.       if (($fileAccessLevel == 1) || ($fileAccessLevel == -1)) {
  727.         if (FeedForAll_rss2html_validExtension(basename($_REQUEST['TEMPLATE']), $allowedTemplateExtensions) === FALSE) {
  728.           echo 'Configuration setting prohibit using the specified template file, exiting'."\n";
  729.           return;
  730.         }
  731.         $TEMPLATEfilename = basename($_REQUEST['TEMPLATE']);
  732.       }
  733.       elseif ($fileAccessLevel == 2) {
  734.         echo 'Configuration setting prohibit using local files, exiting'."\n";
  735.         return;
  736.       } else {
  737.         // It is local and must be in the same directory
  738.         $TEMPLATEfilename = basename($_REQUEST['TEMPLATE']);
  739.       }
  740.     }
  741.   }
  742.  
  743.   if (isset($_REQUEST['FeedTitleLength'])) {
  744.     $limitFeedTitleLength = abs($_REQUEST['FeedTitleLength']);
  745.   }
  746.   if (isset($_REQUEST['FeedDescriptionLength'])) {
  747.     $limitFeedDescriptionLength = abs($_REQUEST['FeedDescriptionLength']);
  748.   }
  749.   if (isset($_REQUEST['ItemTitleLength'])) {
  750.     $limitItemTitleLength = abs($_REQUEST['ItemTitleLength']);
  751.   }
  752.   if (isset($_REQUEST['ItemDescriptionLength'])) {
  753.     $limitItemDescriptionLength = abs($_REQUEST['ItemDescriptionLength']);
  754.   }
  755.  
  756.   //
  757.   // Maximum number of items to be displayed
  758.   //
  759.   if (isset($_REQUEST['MAXITEMS'])) {
  760.     $FeedMaxItems = $_REQUEST['MAXITEMS'];
  761.   }
  762.   if (isset($_REQUEST['NOFUTUREITEMS'])) {
  763.     $NoFutureItems = TRUE;
  764.   }
  765.  
  766.   if (isset($outputCacheTTL) && function_exists('FeedForAll_scripts_readOutputCacheFile') && (($cacheContents = FeedForAll_scripts_readOutputCacheFile($XMLfilename, $TEMPLATEfilename)) !== FALSE)) {
  767.     if (!headers_sent()) {
  768.       // Send the Content-Type to force $destinationEncoding
  769.       header('Content-Type: text/html; charset='.$destinationEncoding);
  770.     }
  771.     echo $cacheContents;
  772.   } else {
  773.     if (($template = FeedForAll_scripts_readFile($TEMPLATEfilename, $useFopenURL)) === FALSE) {
  774.       if (!isset($hideErrors)) {
  775.         if ($ReadErrorString == '') {
  776.           echo 'Unable to open template $TEMPLATEfilename, exiting'."\n";
  777.         } else {
  778.           echo 'Unable to open template $TEMPLATEfilename with error <b>'.$ReadErrorString.'</b>, exiting'."\n";
  779.         }
  780.       }
  781.       return;
  782.     }
  783.     if (FeedForAll_rss2html_isTemplate($template) === FALSE) {
  784.       if (!isset($hideErrors)) {
  785.         echo $TEMPLATEfilename.' is not a valid rss2html.php template file, exiting'."\n";
  786.       }
  787.       return;
  788.     }
  789.  
  790.     if (strstr($template, '~~~NoFutureItems~~~')) {
  791.       $NoFutureItems = TRUE;
  792.     }
  793.  
  794.     if (($XML = FeedForAll_scripts_readFile($XMLfilename, $useFopenURL, $allowCachingXMLFiles)) === FALSE) {
  795.       if (!isset($hideErrors)) {
  796.         if ($ReadErrorString == '') {
  797.           echo 'Unable to open RSS Feed '.$XMLfilename.', exiting'."\n";
  798.         } else {
  799.           echo 'Unable to open RSS Feed $XMLfilename with error <b>'.$ReadErrorString.'</b>, exiting'."\n";
  800.         }
  801.       }
  802.       return;
  803.     }
  804.  
  805.     if (strstr(trim($XML), '<?xml') === FALSE) {
  806.       $XML = '<?xml version="1.0"?>'."\n".$XML;
  807.     }
  808.     $XML = strstr(trim($XML), '<?xml');
  809.     $XML = FeedForAll_preProcessXML($XML);
  810.     if (($convertedXML = FeedForAll_scripts_convertEncoding($XML, $missingEncodingDefault, $destinationEncoding)) === FALSE) {
  811.       // Conversions failed, probably becasue it was wrong or the routines were missing
  812.       $convertedXML = $XML;
  813.       $xml_parser = xml_parser_create();
  814.     } else {
  815.       $xml_parser = xml_parser_create($destinationEncoding);
  816.     }
  817.  
  818.     $rss_parser = new baseParserClass('rss2html');
  819.     $rss_parser->noFutureItems = $NoFutureItems;
  820.     $rss_parser->wholeString = $convertedXML;
  821.     xml_set_object($xml_parser, $rss_parser);
  822.     xml_set_element_handler($xml_parser, 'startElement', 'endElement');
  823.     xml_set_character_data_handler($xml_parser, 'characterData');
  824.     xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING,1);
  825.     $parseResult = xml_parse($xml_parser, $convertedXML, TRUE);
  826.     if ($parseResult == 0) {
  827.       if (!isset($hideErrors)) {
  828.         $errorCode = xml_get_error_code($xml_parser);
  829.         echo '$errorCode = '.$errorCode.'<br />'."\n";
  830.         echo 'xml_error_string() = '.xml_error_string($errorCode).'<br />'."\n";
  831.         echo 'xml_get_current_line_number() = '.xml_get_current_line_number($xml_parser).'<br />'."\n";
  832.         echo 'xml_get_current_column_number() = '.xml_get_current_column_number($xml_parser).'<br />'."\n";
  833.         echo 'xml_get_current_byte_index() = '.xml_get_current_byte_index($xml_parser).'<br />'."\n";
  834.       }
  835.     } else {
  836.       xml_parser_free($xml_parser);
  837.  
  838.       // make sure the channel contentEncoded is not blank
  839.       if ($rss_parser->FeedContentEncoded == '') {
  840.         $rss_parser->FeedContentEncoded = $rss_parser->FeedDescription;
  841.       }
  842.       $template = FeedForAll_rss2html_str_replace('~~~FeedXMLFilename~~~', FeedForAll_rss2html_EscapeLink($XMLfilename), $template);
  843.       $template = FeedForAll_rss2html_str_replace('~~~FeedTitle~~~', FeedForAll_rss2html_limitLength($rss_parser->FeedTitle, $limitFeedTitleLength), $template);
  844.       $template = FeedForAll_rss2html_str_replace('~~~FeedDescription~~~', FeedForAll_rss2html_limitLength($rss_parser->FeedDescription, $limitFeedDescriptionLength), $template);
  845.       $template = FeedForAll_rss2html_str_replace('~~~FeedContentEncoded~~~', $rss_parser->FeedContentEncoded, $template);
  846.       $template = FeedForAll_rss2html_str_replace('~~~FeedLink~~~', FeedForAll_rss2html_EscapeLink($rss_parser->FeedLink), $template);
  847.       $template = FeedForAll_rss2html_str_replace('~~~FeedPubDate~~~', $rss_parser->FeedPubDate, $template);
  848.       $template = FeedForAll_rss2html_str_replace('~~~FeedPubLongDate~~~', date($LongDateFormat, $rss_parser->FeedPubDate_t), $template);
  849.       $template = FeedForAll_rss2html_str_replace('~~~FeedPubShortDate~~~', date($ShortDateFormat, $rss_parser->FeedPubDate_t), $template);
  850.       $template = FeedForAll_rss2html_str_replace('~~~FeedPubLongTime~~~', date($LongTimeFormat, $rss_parser->FeedPubDate_t), $template);
  851.       $template = FeedForAll_rss2html_str_replace('~~~FeedPubShortTime~~~', date($ShortTimeFormat, $rss_parser->FeedPubDate_t), $template);
  852.       $template = FeedForAll_rss2html_str_replace('~~~FeedImageUrl~~~', FeedForAll_rss2html_EscapeLink($rss_parser->FeedImageURL), $template);
  853.       $template = FeedForAll_rss2html_str_replace('~~~FeedImageTitle~~~', $rss_parser->FeedImageTitle, $template);
  854.       $template = FeedForAll_rss2html_str_replace('~~~FeedImageLink~~~', FeedForAll_rss2html_EscapeLink($rss_parser->FeedImageLink), $template);
  855.       $template = FeedForAll_rss2html_str_replace('~~~FeedImageDescription~~~', $rss_parser->FeedImageDescription, $template);
  856.       $template = FeedForAll_rss2html_str_replace('~~~FeedImageHeight~~~', $rss_parser->FeedImageWidth, $template);
  857.       $template = FeedForAll_rss2html_str_replace('~~~FeedImageWidth~~~', $rss_parser->FeedImageWidth, $template);
  858.       $template = FeedForAll_rss2html_str_replace('~~~FeedCreativeCommons~~~', FeedForAll_rss2html_EscapeLink($rss_parser->FeedCreativeCommons), $template);
  859.       if (FeedForAll_parseExtensions() === TRUE) {
  860.         $template = FeedForAll_parseExtensions_replaceInChannel($rss_parser, $template);
  861.       }
  862.  
  863.       $match = NULL;
  864.  
  865.       $template = str_replace('~~~NoFutureItems~~~', '', $template);
  866.  
  867.       //
  868.       // Sort by PubDate if requested
  869.       if (strstr($template, '~~~SortByPubDate~~~')) {
  870.         $template = str_replace('~~~SortByPubDate~~~', '', $template);
  871.  
  872.         for ($x = 0; $x < count($rss_parser->Items)-1; $x++) {
  873.           for ($y = $x+1; $y < count($rss_parser->Items); $y++) {
  874.             if ($rss_parser->Items[$x]->pubDate_t < $rss_parser->Items[$y]->pubDate_t) {
  875.               // Swap them
  876.               $swapTemp = $rss_parser->Items[$x]; $rss_parser->Items[$x] = $rss_parser->Items[$y]; $rss_parser->Items[$y] = $swapTemp;
  877.             }
  878.           }
  879.         }
  880.       }
  881.  
  882.       if (isset($debugLevel) && ($debugLevel >= 3)) {
  883.         echo 'DIAG: adding to items, count='.count($rss_parser->Items).'<br />'."\n";
  884.       }
  885.       
  886.       // The the maximum items requested
  887.       if (strstr($template, '~~~FeedMaxItems=')) {
  888.         // Limit the maximun number of items displayed
  889.         if (preg_match('/~~~FeedMaxItems=([0-9-]*)~~~/', $template, $match) !== FALSE) {
  890.           if (($match[0] != '') && ($match[1] != '')) {
  891.             $FeedMaxItems = $match[1];
  892.             $template = str_replace("~~~FeedMaxItems=$match[1]~~~", "", $template);
  893.           }
  894.         }
  895.       }
  896.       if (abs($FeedMaxItems) > count($rss_parser->Items)) {
  897.         if ($FeedMaxItems > 0) {
  898.           $FeedMaxItems = count($rss_parser->Items);
  899.         } else {
  900.           $FeedMaxItems = -count($rss_parser->Items);
  901.         }
  902.       }
  903.  
  904.       if (!function_exists('FeedForALL_rss2html_replaceInItem')) {
  905.         Function FeedForALL_rss2html_replaceInItem($source, $currentItem) {
  906.           GLOBAL $limitFeedTitleLength;
  907.           GLOBAL $limitFeedDescriptionLength;
  908.           GLOBAL $limitItemTitleLength;
  909.           GLOBAL $limitItemDescriptionLength;
  910.           GLOBAL $LongDateFormat;
  911.           GLOBAL $ShortDateFormat;
  912.           GLOBAL $LongTimeFormat;
  913.           GLOBAL $ShortTimeFormat;
  914.           GLOBAL $XMLfilename;
  915.  
  916.           $item = FeedForAll_rss2html_str_replace('~~~ItemTitle~~~', FeedForAll_rss2html_limitLength($currentItem->title, $limitItemTitleLength), $source);
  917.           $item = FeedForAll_rss2html_str_replace('~~~ItemDescription~~~', FeedForAll_rss2html_limitLength($currentItem->description, $limitItemDescriptionLength), $item);
  918.           $item = FeedForAll_rss2html_str_replace('~~~ItemEnclosureLengthFormatted~~~', FeedForAll_rss2html_sizeToString($currentItem->enclosureLength), $item);
  919.           $item = FeedForAll_rss2html_str_replace('~~~ItemPubLongDate~~~', date($LongDateFormat, $currentItem->pubDate_t), $item);
  920.           $item = FeedForAll_rss2html_str_replace('~~~ItemPubShortDate~~~', date($ShortDateFormat, $currentItem->pubDate_t), $item);
  921.           $item = FeedForAll_rss2html_str_replace('~~~ItemPubLongTime~~~', date($LongTimeFormat, $currentItem->pubDate_t), $item);
  922.           $item = FeedForAll_rss2html_str_replace('~~~ItemPubShortTime~~~', date($ShortTimeFormat, $currentItem->pubDate_t), $item);
  923.  
  924.           $knownFields = $currentItem->getArrayOfFields();
  925.           foreach ($knownFields as $field) {
  926.             $item = FeedForAll_rss2html_str_replace($field, $currentItem->getValueOf($field), $item);
  927.           }
  928.  
  929.           $item = FeedForAll_rss2html_CreateUniqueLink($currentItem->title, $currentItem->description, $currentItem->link, $currentItem->guid, $XMLfilename, $item);
  930.           if (FeedForAll_parseExtensions() === TRUE) {
  931.             $item = FeedForAll_parseExtensions_replaceInItem($currentItem, $item);
  932.           }
  933.           return FeedForAll_rss2html_AddIdentity($item);
  934.         }
  935.       }
  936.  
  937.       //
  938.       // Allow access to the number of times that will be processed in the feed
  939.       $template = FeedForAll_rss2html_str_replace('~~~NumberOfFeedItems~~~', min(abs($FeedMaxItems), count($rss_parser->Items)), $template);
  940.  
  941.       //
  942.       // Find the string, if it exists, between the ~~~EndItemsRecord~~~ and ~~~BeginItemsRecord~~~
  943.       //
  944.       while ((strstr($template, '~~~BeginItemsRecord~~~')) !== FALSE) {
  945.         $match = NULL;
  946.         $allitems = NULL;
  947.         $loop_limit = min(abs($FeedMaxItems), count($rss_parser->Items));
  948.         if (($parts = explode('~~~BeginItemsRecord~~~', $template)) !== FALSE) {
  949.           if (($parts = explode('~~~EndItemsRecord~~~', $parts[1])) !== FALSE) {
  950.             $WholeBlock = $parts[0];
  951.             //
  952.             // Check for ~~~BeginAlternateItemsRecord~~~
  953.             //
  954.             if (strstr($WholeBlock, '~~~BeginAlternateItemsRecord~~~')) {
  955.               $parts = explode('~~~BeginAlternateItemsRecord~~~', $WholeBlock);
  956.               $block1 = $parts[0];
  957.               $block2 = $parts[1];
  958.             } else {
  959.               $block1 = $WholeBlock;
  960.               $block2 = $WholeBlock;
  961.             }
  962.             if ($FeedMaxItems < 0) {
  963.               for ($x = count($rss_parser->Items)-1; $x >= count($rss_parser->Items) + $FeedMaxItems; $x--) {
  964.                 $allitems .= FeedForALL_rss2html_replaceInItem($block1, $rss_parser->Items[$x]);
  965.                 $x--;
  966.                 if ($x >= count($rss_parser->Items) + $FeedMaxItems) {
  967.                   //
  968.                   // This is at least one more item so use the Alternate definition
  969.                   //
  970.                   $allitems .= FeedForALL_rss2html_replaceInItem($block2, $rss_parser->Items[$x]);
  971.                 }
  972.               }
  973.             } else {
  974.               for ($x = 0; $x < $loop_limit; $x++) {
  975.                 if (isset($debugLevel) && ($debugLevel >= 2)) {
  976.                   echo 'DIAG: Doing item fillin, $x = '.$x.'; $loop_limit = '.$loop_limit.'<br />'."\n";
  977.                 }
  978.  
  979.                 $allitems .= FeedForALL_rss2html_replaceInItem($block1, $rss_parser->Items[$x]);
  980.                 $x++;
  981.                 if ($x < $loop_limit) {
  982.                   //
  983.                   // This is at least one more item so use the Alternate definition
  984.                   //
  985.                   if (isset($debugLevel) && ($debugLevel >= 2)) {
  986.                     echo 'DIAG: Doing item fillin, $x = '.$x.'; $loop_limit = '.$loop_limit.'<br />'."\n";
  987.                   }
  988.  
  989.                   $allitems .= FeedForALL_rss2html_replaceInItem($block2, $rss_parser->Items[$x]);
  990.                 }
  991.               }
  992.             }
  993.             $template = str_replace('~~~BeginItemsRecord~~~'.$WholeBlock.'~~~EndItemsRecord~~~', $allitems, $template);
  994.           }
  995.         }
  996.       }
  997.  
  998.       // Since ' is not HTML, but is XML convert.
  999.       $template = str_replace(''', '\'', $template);
  1000.  
  1001.       if (!headers_sent()) {
  1002.         // Send the Content-Type to force $destinationEncoding
  1003.         header('Content-Type: text/html; charset='.$destinationEncoding);
  1004.       }
  1005.       $resultHTML = FeedForAll_rss2html_pro($template);
  1006.       echo $resultHTML;
  1007.       if (isset($outputCacheTTL) && function_exists('FeedForAll_scripts_writeOutputCacheFile')) {
  1008.         FeedForAll_scripts_writeOutputCacheFile($XMLfilename, $TEMPLATEfilename, $resultHTML);
  1009.       }
  1010.     }
  1011.   }
  1012. } else {
  1013.   if (!headers_sent()) {
  1014.     // Send the Content-Type to force $destinationEncoding
  1015.     header('Content-Type: text/html; charset='.$destinationEncoding);
  1016.   }
  1017.   echo '<html><head><title>rss2html.php URL tool</title><meta http-equiv="content-type" content="text/html;charset='.$destinationEncoding.'"></head><body bgcolor="#EEEEFF">'."\n";
  1018.   //
  1019.   // We are in "buildURL" mode to help create properly encoded URLs to pass to rss2html.php
  1020.  
  1021.   $_xml = '';
  1022.   if (isset($_POST['XML'])) {
  1023.     $_xml = $_POST['XML'];
  1024.   }
  1025.   $_template = '';
  1026.   if (isset($_POST['TEMPLATE'])) {
  1027.     $_template = $_POST['TEMPLATE'];
  1028.   }
  1029.   $_maxitems = '';
  1030.   if (isset($_POST['MAXITEMS'])) {
  1031.     $_maxitems = $_POST['MAXITEMS'];
  1032.   }
  1033.   $_nofutureitems = '';
  1034.   if (isset($_POST['NOFUTUREITEMS'])) {
  1035.     $_nofutureitems = $_POST['NOFUTUREITEMS'];
  1036.   }
  1037.   if (function_exists('FeedForAll_scripts_contentOfCache')) {
  1038.     $_cacheTTL = '';
  1039.     if (isset($_POST['XMLCACHETTL'])) {
  1040.       $_cacheTTL = $_POST['XMLCACHETTL'];
  1041.     }
  1042.     $_allowCachingXMLFiles = '';
  1043.     if (isset($_POST['ALLOWXMLCACHE'])) {
  1044.       $_allowCachingXMLFiles = $_POST['ALLOWXMLCACHE'];
  1045.     }
  1046.     $_outputCacheTTL = '';
  1047.     if (isset($_POST['OUTCACHETTL'])) {
  1048.       $_outputCacheTTL = $_POST['OUTCACHETTL'];
  1049.     }
  1050.     $_outputCacheFileName = '';
  1051.     if (isset($_POST['OUTCACHENAME'])) {
  1052.       $_outputCacheFileName = $_POST['OUTCACHENAME'];
  1053.     }
  1054.   }
  1055.  
  1056.   // Display the entry form
  1057.   echo '<center><h1>RSS2HTML.PHP LINK TOOL</h1></center>'."\n";
  1058.   echo '<p>To assist with the with the creation of properly encoded URLs for use with rss2html.php this tool has been created.  Fill in the URLs or file paths for both the XML file and your template file in the boxes below and then click "Submit".  The program will then return the URLs properly encoded in a string that calls rss2html.php.  You can click on this link to test the results.  The program will also indicate if it was unable to open either of the URLs it was given.</p>'."\n";
  1059.   echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">'."\n";
  1060.   echo '<input type="hidden" name="buildURL" value="1">'."\n";
  1061.   echo 'URL for the XML file: (ie. http://www.myserver.com/file.xml)<br /><input type="text" name="XML" size="100" value="'.$_xml.'"><br />'."\n";
  1062.   echo 'URL for the template file: (ie. http://www.myserver.com/template.html)<br /><input type="text" name="TEMPLATE" size="100" value="'.$_template.'"><br />'."\n";
  1063.   echo '<b>Optional items:</b><br />'."\n";
  1064.   echo 'Maximum items: <input type="text" name="MAXITEMS" size="5" value="'.$_maxitems.'"> (Use negative numbers for the last X items)<br />'."\n";
  1065.   echo 'No future items: <input type="checkbox" name="NOFUTUREITEMS" ';
  1066.   if ($_nofutureitems == 'on') {
  1067.     echo 'CHECKED';
  1068.   }
  1069.   echo '><br />'."\n";
  1070.   if (function_exists('FeedForAll_scripts_contentOfCache')) {
  1071.     echo '<table cellpadding="2" cellspacing="2" width="100%" border="1"><tr><td>'."\n";
  1072.     echo '<strong>XML (input) Cache Settings</strong><br />'."\n";
  1073.     echo 'Allow Caching of the feed: <input type="checkbox" name="ALLOWXMLCACHE" ';
  1074.     if ($_allowCachingXMLFiles == 'on') {
  1075.       echo 'CHECKED';
  1076.     }
  1077.     echo '><br />'."\n";
  1078.     echo 'Cache Time: <input type="text" name="XMLCACHETTL" size="5" value="'.$_cacheTTL.'"> (The number of seconds a file may be cached for before being fetched again)<br />'."\n";
  1079.     echo '<hr />'."\n";
  1080.     echo '<strong>HTML (output) Cache Settings</strong><br />'."\n";
  1081.     echo 'Output Cache Time: <input type="text" name="OUTCACHETTL" size="5" value="'.$_outputCacheTTL.'"> (The number of seconds the output may be cached for before being recreated)<br />'."\n";
  1082.     echo 'Output Cache Name: <input type="text" name="OUTCACHENAME" size="40" value="'.$_outputCacheFileName.'"> (This should be a unique name to prevent conflicts)<br />'."\n";
  1083.     echo '</td></tr></table>'."\n";
  1084.   }
  1085.   echo '<input type="submit" name="submit" value="Submit">'."\n";
  1086.   echo '</form>'."\n";
  1087.  
  1088.   $xmlContents = '';
  1089.   $templateContents = '';
  1090.  
  1091.   if (isset($_POST['submit'])) {
  1092.     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  1093.       return;
  1094.     }
  1095.     echo '<hr />'."\n";
  1096.  
  1097.     $answer = '';
  1098.     $answerAlt = '';
  1099.     $ssi = '';
  1100.     $xmlurl = '';
  1101.     $templateurl = '';
  1102.     if ((isset($_POST['XML']) && $_POST['XML'] != '') || (isset($_POST['TEMPLATE']) && $_POST['TEMPLATE'] != '')) {
  1103.       $answer .= 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?';
  1104.     }
  1105.     if (isset($_POST['XML']) && $_POST['XML'] != '') {
  1106.       $answer .= 'XMLFILE='.FeedForAll_rss2html_encodeURL($_POST['XML']);
  1107.       $answerAlt .= '$XMLFILE = \''.str_replace('&', '&', $_POST['XML']).'\';<br />';
  1108.       $ssi .= 'XMLFILE='.FeedForAll_rss2html_encodeURL($_POST['XML']);
  1109.       $xmlurl = FeedForAll_rss2html_encodeURL($_POST['XML']);
  1110.     }
  1111.     if ((isset($_POST['XML']) && $_POST['XML'] != '') && (isset($_POST['TEMPLATE']) && $_POST['TEMPLATE'] != '')) {
  1112.       $answer .=  '&';
  1113.       $ssi .=  '&';
  1114.     }
  1115.     if (isset($_POST['TEMPLATE']) && $_POST['TEMPLATE'] != '') {
  1116.       $answer .=  'TEMPLATE='.FeedForAll_rss2html_encodeURL($_POST['TEMPLATE']);
  1117.       $answerAlt .= '$TEMPLATE = \''.str_replace('&', '&', $_POST['TEMPLATE']).'\';<br />';
  1118.       $ssi .=  'TEMPLATE='.FeedForAll_rss2html_encodeURL($_POST['TEMPLATE']);
  1119.       $templateurl = FeedForAll_rss2html_encodeURL($_POST['TEMPLATE']);
  1120.     }
  1121.     if (isset($_POST['MAXITEMS']) && $_POST['MAXITEMS'] != '' && intval($_POST['MAXITEMS'] != 0)) {
  1122.       $answer .=  '&MAXITEMS=$_POST[MAXITEMS]';
  1123.       $answerAlt .= '$MAXITEMS = \''.$_POST['MAXITEMS'].'\';<br />'."\n";
  1124.       $ssi .=  '&MAXITEMS='.$_POST[MAXITEMS];
  1125.     }
  1126.     if (isset($_POST['NOFUTUREITEMS']) && $_POST['NOFUTUREITEMS'] == 'on') {
  1127.       $answer .=  '&NOFUTUREITEMS=1';
  1128.       $answerAlt .= '$NOFUTUREITEMS = 1;<br />'."\n";
  1129.       $ssi .=  '&NOFUTUREITEMS=1';
  1130.     }
  1131.     if (function_exists('FeedForAll_scripts_contentOfCache')) {
  1132.       if (isset($_POST['ALLOWXMLCACHE']) && $_POST['ALLOWXMLCACHE'] == 'on') {
  1133.         $answerAlt .= '$ALLOWXMLCACHE = 1;<br />'."\n";
  1134.       }
  1135.       if (isset($_POST['XMLCACHETTL']) && ($_POST['XMLCACHETTL'] != '') && (intval($_POST['XMLCACHETTL']) != 0)) {
  1136.         $answerAlt .= '$XMLCACHETTL = \''.$_POST['XMLCACHETTL'].'\';<br />'."\n";
  1137.       }
  1138.       if (isset($_POST['OUTCACHETTL']) && isset($_POST['OUTCACHENAME'])) {
  1139.         if (($_POST['OUTCACHETTL'] != '') && (intval($_POST['OUTCACHETTL']) != 0) && ($_POST['OUTCACHENAME'] != '')) {
  1140.           $answerAlt .= '$OUTCACHETTL = \''.$_POST['OUTCACHETTL'].'\';<br />'."\n";
  1141.           $answerAlt .= '$OUTCACHENAME = \''.$_POST['OUTCACHENAME'].'\';<br />'."\n";
  1142.         }
  1143.       }
  1144.     }
  1145.  
  1146.     echo '<h1>Results</h1>'."\n";
  1147.  
  1148.     if (isset($_POST['XML']) && $_POST['XML'] != '') {
  1149.       $XMLfilename = '';
  1150.       if (stristr($_POST['XML'], 'file'.'://')) {
  1151.         // Not allowed
  1152.         ;
  1153.       }
  1154.       elseif (stristr($_POST['XML'], '://')) {
  1155.         if ($fileAccessLevel == -1) {
  1156.           echo '<p style="color: red;">Configuration setting prohibit using remote files</p>'."\n";
  1157.         } else {
  1158.           // URL files are allowed
  1159.           $XMLfilename = $_POST['XML'];
  1160.         }
  1161.       } else {
  1162.         if (($fileAccessLevel == 1) || ($fileAccessLevel == -1)) {
  1163.           if (FeedForAll_rss2html_validExtension(basename($_POST['XML']), $allowedFeedExtensions) === FALSE) {
  1164.             echo '<p style="color: red;">Configuration setting prohibit using the specified feed file</p>'."\n";
  1165.           } else {
  1166.             $XMLfilename = basename($_POST['XML']);
  1167.           }
  1168.         }
  1169.         elseif ($fileAccessLevel == 2) {
  1170.           echo '<p style="color: red;">Configuration setting prohibit using local files</p>'."\n";
  1171.         } else {
  1172.           // It is local and must be in the same directory
  1173.           $XMLfilename = basename($_POST['XML']);
  1174.         }
  1175.       }
  1176.       if ($XMLfilename != '') {
  1177.         if (($xmlContents = FeedForAll_scripts_readFile($XMLfilename, $useFopenURL)) === FALSE) {
  1178.           if ($ReadErrorString == '') {
  1179.             echo '<p>The XML file <b>'.$_POST['XML'].'</b> could not be opened.</p>'."\n";
  1180.           } else {
  1181.             echo '<p>The XML file <b>'.$_POST['XML'].'</b> could not be opened with the error <b>'.$ReadErrorString.'</b>.</p>'."\n";
  1182.           }
  1183.         } else {
  1184.           echo '<p>The XML file <b>'.$_POST['XML'].'</b> was SUCCESSFULLY opened</p>'."\n";
  1185.         }
  1186.       }
  1187.     }
  1188.  
  1189.     if (isset($_POST['TEMPLATE']) && $_POST['TEMPLATE'] != '') {
  1190.       $TEMPLATEfilename = '';
  1191.       if (stristr($_POST['TEMPLATE'], 'file'.'://')) {
  1192.         // Not allowed
  1193.         ;
  1194.       }
  1195.       elseif (stristr($_POST['TEMPLATE'], '://')) {
  1196.         if ($fileAccessLevel == -1) {
  1197.           echo '<p style="color: red;">Configuration setting prohibit using remote files</p>'."\n";
  1198.         } else {
  1199.           // URL files are allowed
  1200.           $TEMPLATEfilename = $_POST['TEMPLATE'];
  1201.         }
  1202.       } else {
  1203.         if (($fileAccessLevel == 1) || ($fileAccessLevel == -1)) {
  1204.           if (FeedForAll_rss2html_validExtension(basename($_POST['TEMPLATE']), $allowedTemplateExtensions) === FALSE) {
  1205.             echo '<p style="color: red;">Configuration setting prohibit using the specified template file</p>'."\n";
  1206.           } else {
  1207.             $TEMPLATEfilename = basename($_POST['TEMPLATE']);
  1208.           }
  1209.         }
  1210.         elseif ($fileAccessLevel == 2) {
  1211.           echo '<p style="color: red;">Configuration setting prohibit using local files</p>'."\n";
  1212.         } else {
  1213.           // It is local and must be in the same directory
  1214.           $TEMPLATEfilename = basename($_POST['TEMPLATE']);
  1215.         }
  1216.       }
  1217.       if ($TEMPLATEfilename != '') {
  1218.         if (($templateContents = FeedForAll_scripts_readFile($TEMPLATEfilename, $useFopenURL)) === FALSE) {
  1219.           if ($ReadErrorString == '') {
  1220.             echo '<p>The template file <b>'.$_POST['TEMPLATE'].'</b> could not be opened.</p>'."\n";
  1221.           } else {
  1222.             echo '<p>The template file <b>'.$_POST['TEMPLATE'].'</b> could not be opened with the error <b>'.$ReadErrorString.'</b>.</p>'."\n";
  1223.           }
  1224.         }
  1225.         elseif (FeedForAll_rss2html_isTemplate($templateContents) === FALSE) {
  1226.           echo $_POST['TEMPLATE'].' is not a valid rss2html.php template file'."\n";
  1227.           $templateContents = '';
  1228.         } else {
  1229.           echo '<p>The template file <b>'.$_POST['TEMPLATE'].'</b> was SUCCESSFULLY opened</p>'."\n";
  1230.         }
  1231.       }
  1232.     }
  1233.  
  1234.     if ($xmlurl != '') {
  1235.       echo '<p>URL for the XML file properly encoded:<br /><pre>'.$xmlurl.'</pre></p>'."\n";
  1236.     }
  1237.  
  1238.     if ($templateurl != '') {
  1239.       echo '<p>URL for the template file properly encoded:<br /><pre>'.$templateurl.'</pre></p>'."\n";
  1240.     }
  1241.  
  1242.     echo '<h2>Test Link</h2>'."\n";
  1243.  
  1244.     echo '<p>Click on link to view results: <a href="'.$answer.'" target="_blank">'.$answer.'</a></p>'."\n";
  1245.  
  1246.     echo '<h2>Example Usage</h2>'."\n";
  1247.  
  1248.     echo '<p>Server Side Include:<br /><nobr style="font-weight: bolder; color: red;"><!--#INCLUDE VIRTUAL="'.basename($_SERVER['PHP_SELF']).'?'.$ssi.'" --></nobr></p>'."\n";
  1249.  
  1250.     echo '<p>Prefered PHP Include:<br /><nobr style="font-weight: bolder; color: red;"><?php<br />'.$answerAlt."\n".'include("'.basename($_SERVER['PHP_SELF']).'");<br />?></nobr></p>'."\n";
  1251.  
  1252.     echo '<p>PHP Include (Due to security concerns many ISP have configured their servers to prevent this from working):<br /><nobr style="font-weight: bolder; color: red;"><?php<br />include("'.$answer.'");<br />?></nobr></p>'."\n";
  1253.  
  1254.   }
  1255.  
  1256.   if ($xmlContents != '' || $templateContents != '') {
  1257.     echo '<br /><hr /><br />'."\n";
  1258.     if ($xmlContents != '') {
  1259.       echo '<h1>XML file</h1>'."\n";
  1260.       if (($convertedXML = FeedForAll_scripts_convertEncoding($xmlContents, $missingEncodingDefault, $destinationEncoding)) === FALSE) {
  1261.         // Conversions failed, probably becasue it was wrong or the routines were missing
  1262.         $convertedXML = $xmlContents;
  1263.       }
  1264.       $convertedXML = str_replace('&', '&', $convertedXML);
  1265.       $convertedXML = str_replace('<', '<', $convertedXML);
  1266.       $convertedXML = str_replace('>', '>', $convertedXML);
  1267.       echo '<pre>'.$convertedXML.'</pre><br />'."\n";
  1268.     }
  1269.     if ($templateContents != '') {
  1270.       echo '<h1>Template file</h1>'."\n";
  1271.       $templateContents = str_replace('&', '&', $templateContents);
  1272.       $templateContents = str_replace('<', '<', $templateContents);
  1273.       $templateContents = str_replace('>', '>', $templateContents);
  1274.       echo '<pre>'.$templateContents.'</pre><br />'."\n";
  1275.     }
  1276.   }
  1277. }
  1278.  
  1279. ?>
  1280.