PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/mailz/lists/admin/onyxrss/onyx-rss.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 381 lines | 297 code | 34 blank | 50 comment | 66 complexity | a9225010d71adc3a18e426e76e26e595 MD5 | raw file
  1. <?php
  2. /* Copyright 2002-2003 Edward Swindelles (ed@readinged.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. if (!defined('ONYX_RSS_VERS'))
  19. {
  20. define('ONYX_RSS_VERS', '1.0');
  21. define('ONYX_ERR_NO_PARSER', '<a href="http://www.php.net/manual/en/ref.xml.php">PHP\'s XML Extension</a> is not loaded or available.');
  22. define('ONYX_ERR_NOT_WRITEABLE', 'The specified cache directory is not writeable.');
  23. define('ONYX_ERR_INVALID_URI', 'The specified file could not be opened.');
  24. define('ONYX_ERR_INVALID_ITEM', 'Invalid item index specified.');
  25. define('ONYX_ERR_NO_STREAM', 'Could not open the specified file. Check the path, and make sure that you have write permissions to this file.');
  26. define('ONYX_META', 'meta');
  27. define('ONYX_ITEMS', 'items');
  28. define('ONYX_IMAGE', 'image');
  29. define('ONYX_TEXTINPUT', 'textinput');
  30. define('ONYX_NAMESPACES', 'namespaces');
  31. define('ONYX_CACHE_AGE', 'cache_age');
  32. define('ONYX_FETCH_ASSOC', 1);
  33. define('ONYX_FETCH_OBJECT', 2);
  34. }
  35. class ONYX_RSS
  36. {
  37. var $parser;
  38. var $conf;
  39. var $rss;
  40. var $data;
  41. var $type;
  42. var $lasterror;
  43. /* For when PHP v.5 is released
  44. * http://www.phpvolcano.com/eide/php5.php?page=variables
  45. * private $parser;
  46. * private $conf;
  47. * private $rss;
  48. * private $data;
  49. * private $type;
  50. */
  51. function ONYX_RSS()
  52. {
  53. $this->__construct();
  54. }
  55. // Forward compatibility with PHP v.5
  56. // http://www.phpvolcano.com/eide/php5.php?page=start
  57. function __construct()
  58. {
  59. $this->conf = array();
  60. $this->conf['error'] = '<br /><strong>Error on line %s of '.__FILE__.'</strong>: %s<br />';
  61. $this->conf['cache_path'] = dirname(__FILE__);
  62. $this->conf['cache_time'] = 180;
  63. $this->conf['debug_mode'] = true;
  64. $this->conf['fetch_mode'] = ONYX_FETCH_ASSOC;
  65. $this->lasterror = "";
  66. if (!function_exists('xml_parser_create'))
  67. {
  68. $this->raiseError((__LINE__-2), ONYX_ERR_NO_PARSER);
  69. return false;
  70. }
  71. $this->parser = @xml_parser_create();
  72. if (!is_resource($this->parser))
  73. {
  74. $this->raiseError((__LINE__-3), ONYX_ERR_NO_PARSER);
  75. return false;
  76. }
  77. xml_set_object($this->parser, $this);
  78. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
  79. xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
  80. xml_set_character_data_handler($this->parser, 'cdata');
  81. }
  82. function parse($uri, $file=false, $time=false, $local=false)
  83. {
  84. $this->rss = array();
  85. $this->rss['cache_age'] = 0;
  86. $this->rss['current_tag'] = '';
  87. $this->rss['index'] = 0;
  88. $this->rss['output_index'] = -1;
  89. $this->data = array();
  90. if ($file)
  91. {
  92. if (!is_writeable($this->conf['cache_path']))
  93. {
  94. $this->raiseError((__LINE__-2), ONYX_ERR_NOT_WRITEABLE);
  95. return false;
  96. }
  97. $file = str_replace('//', '/', $this->conf['cache_path'].'/'.$file);
  98. if (!$time)
  99. $time = $this->conf['cache_time'];
  100. $this->rss['cache_age'] = file_exists($file) ? ceil((time() - filemtime($file)) / 60) : 0;
  101. clearstatcache();
  102. if (!$local && file_exists($file))
  103. if (($mod = $this->mod_time($uri)) === false)
  104. {
  105. $this->raiseError((__LINE__-2), ONYX_ERR_INVALID_URI);
  106. return false;
  107. }
  108. else
  109. $mod = ($mod !== 0) ? strtotime($mod) : (time()+3600);
  110. elseif ($local)
  111. $mod = (file_exists($file) && ($m = filemtime($uri))) ? $m : time()+3600;
  112. }
  113. if ( !$file ||
  114. ($file && !file_exists($file)) ||
  115. ($file && file_exists($file) && $time <= $this->rss['cache_age'] && $mod >= (time() - ($this->rss['cache_age'] * 60))))
  116. {
  117. clearstatcache();
  118. if (!($fp = @fopen($uri, 'r')))
  119. {
  120. $this->raiseError((__LINE__-2), ONYX_ERR_INVALID_URI);
  121. return false;
  122. }
  123. while ($chunk = fread($fp, 4096))
  124. {
  125. $parsedOkay = xml_parse($this->parser, $chunk, feof($fp));
  126. if (!$parsedOkay && xml_get_error_code($this->parser) != XML_ERROR_NONE)
  127. {
  128. $this->raiseError((__LINE__-3), 'File has an XML error (<em>'.xml_error_string(xml_get_error_code($this->parser)).'</em> at line <em>'.xml_get_current_line_number($this->parser).'</em>).');
  129. return false;
  130. }
  131. }
  132. fclose($fp);
  133. clearstatcache();
  134. if ($file)
  135. {
  136. if (!($cache = @fopen($file, 'w')))
  137. {
  138. $this->raiseError((__LINE__-2), 'Could not write to cache file (<em>'.$file.'</em>). The path may be invalid or you may not have write permissions.');
  139. return false;
  140. }
  141. fwrite($cache, serialize($this->data));
  142. fclose($cache);
  143. $this->rss['cache_age'] = 0;
  144. }
  145. }
  146. else
  147. {
  148. clearstatcache();
  149. if (!($fp = @fopen($file, 'r')))
  150. {
  151. $this->raiseError((__LINE__-2), 'Could not read contents of cache file (<em>'.$cache_file.'</em>).');
  152. return false;
  153. }
  154. $this->data = unserialize(fread($fp, filesize($file)));
  155. fclose($fp);
  156. }
  157. return true;
  158. }
  159. function parseLocal($uri, $file=false, $time=false)
  160. {
  161. return $this->parse($uri, $file, $time, true);
  162. }
  163. //private function tag_open($parser, $tag, $attrs)
  164. function tag_open($parser, $tag, $attrs)
  165. {
  166. $this->rss['current_tag'] = $tag = strtolower($tag);
  167. switch ($tag)
  168. {
  169. case 'channel':
  170. case 'image':
  171. case 'textinput':
  172. $this->type = $tag;
  173. break;
  174. case 'item':
  175. $this->type = $tag;
  176. $this->rss['index']++;
  177. break;
  178. default:
  179. break;
  180. }
  181. if (sizeof($attrs))
  182. foreach ($attrs as $k => $v)
  183. if (strpos($k, 'xmlns') !== false)
  184. $this->data['namespaces'][$k] = $v;
  185. }
  186. //private function tag_close($parser, $tag){}
  187. function tag_close($parser, $tag){}
  188. //private function cdata($parser, $cdata)
  189. function cdata($parser, $cdata)
  190. {
  191. if (strlen(trim($cdata)) && $cdata != "\n")
  192. switch ($this->type)
  193. {
  194. case 'channel':
  195. case 'image':
  196. case 'textinput':
  197. (!isset($this->data[$this->type][$this->rss['current_tag']]) ||
  198. !strlen($this->data[$this->type][$this->rss['current_tag']])) ?
  199. $this->data[$this->type][$this->rss['current_tag']] = $cdata :
  200. $this->data[$this->type][$this->rss['current_tag']].= $cdata;
  201. break;
  202. case 'item':
  203. (!isset($this->data['items'][$this->rss['index']-1][$this->rss['current_tag']]) ||
  204. !strlen($this->data['items'][$this->rss['index']-1][$this->rss['current_tag']])) ?
  205. $this->data['items'][$this->rss['index']-1][$this->rss['current_tag']] = $cdata :
  206. $this->data['items'][$this->rss['index']-1][$this->rss['current_tag']].= $cdata;
  207. break;
  208. }
  209. }
  210. function getData($type)
  211. {
  212. if ($type == ONYX_META)
  213. return $this->conf['fetch_mode'] == 1 ? $this->data['channel'] : (object)$this->data['channel'];
  214. if ($type == ONYX_IMAGE)
  215. return $this->conf['fetch_mode'] == 1 ? $this->data['image'] : (object)$this->data['image'];
  216. if ($type == ONYX_TEXTINPUT)
  217. return $this->conf['fetch_mode'] == 1 ? $this->data['textinput'] : (object)$this->data['textinput'];
  218. if ($type == ONYX_ITEMS)
  219. {
  220. if ($this->conf['fetch_mode'] == 1)
  221. return $this->data['items'];
  222. $temp = array();
  223. for ($i=0; $i < sizeof($this->data['items']); $i++)
  224. $temp[] = (object)$this->data['items'][$i];
  225. return $temp;
  226. }
  227. if ($type == ONYX_NAMESPACES)
  228. return $this->conf['fetch_mode'] == 1 ? $this->data['namespaces'] : (object)$this->data['namespaces'];
  229. if ($type == ONYX_CACHE_AGE)
  230. return $this->rss['cache_age'];
  231. return false;
  232. }
  233. function numItems()
  234. {
  235. return sizeof($this->data['items']);
  236. }
  237. function getNextItem($max=false)
  238. {
  239. $type = $this->conf['fetch_mode'];
  240. $this->rss['output_index']++;
  241. if (($max && $this->rss['output_index'] > $max) || !isset($this->data['items'][$this->rss['output_index']]))
  242. return false;
  243. return ($type == ONYX_FETCH_ASSOC) ? $this->data['items'][$this->rss['output_index']] :
  244. (($type == ONYX_FETCH_OBJECT) ? (object)$this->data['items'][$this->rss['output_index']] : false);
  245. }
  246. function itemAt($num)
  247. {
  248. if (!isset($this->data['items'][$num]))
  249. {
  250. $this->raiseError((__LINE__-3), ONYX_ERR_INVALID_ITEM);
  251. return false;
  252. }
  253. $type = $this->conf['fetch_mode'];
  254. return ($type == ONYX_FETCH_ASSOC) ? $this->data['items'][$num] :
  255. (($type == ONYX_FETCH_OBJECT) ? (object)$this->data['items'][$num] : false);
  256. }
  257. function startBuffer($file=false)
  258. {
  259. $this->conf['output_file'] = $file;
  260. ob_start();
  261. }
  262. function endBuffer()
  263. {
  264. if (!$this->conf['output_file'])
  265. ob_end_flush();
  266. else
  267. {
  268. if (!($fp = @fopen($this->conf['output_file'], 'w')))
  269. {
  270. $this->raiseError((__LINE__-2), ONYX_ERR_NO_STREAM);
  271. ob_end_flush();
  272. return;
  273. }
  274. fwrite($fp, ob_get_contents());
  275. fclose($fp);
  276. chmod($this->conf['output_file'],0666);
  277. ob_end_clean();
  278. }
  279. }
  280. //private function raiseError($line, $err)
  281. function raiseError($line, $err)
  282. {
  283. if ($this->conf['debug_mode'])
  284. printf($this->conf['error'], $line, $err);
  285. else
  286. $this->lasterror = $err;
  287. }
  288. function setCachePath($path)
  289. {
  290. $this->conf['cache_path'] = $path;
  291. }
  292. function setExpiryTime($time)
  293. {
  294. $this->conf['cache_time'] = $time;
  295. }
  296. function setDebugMode($state)
  297. {
  298. $this->conf['debug_mode'] = (bool)$state;
  299. }
  300. function setFetchMode($mode)
  301. {
  302. $this->conf['fetch_mode'] = $mode;
  303. }
  304. //private function mod_time($uri)
  305. function mod_time($uri)
  306. {
  307. if (function_exists('version_compare') && version_compare(phpversion(), '4.3.0') >= 0)
  308. {
  309. if (!($fp = @fopen($uri, 'r')))
  310. return false;
  311. $meta = stream_get_meta_data($fp);
  312. for ($j = 0; isset($meta['wrapper_data'][$j]); $j++)
  313. if (strpos(strtolower($meta['wrapper_data'][$j]), 'last-modified') !== false)
  314. {
  315. $modtime = substr($meta['wrapper_data'][$j], 15);
  316. break;
  317. }
  318. fclose($fp);
  319. }
  320. else
  321. {
  322. $parts = parse_url($uri);
  323. $host = $parts['host'];
  324. $path = $parts['path'];
  325. if (!($fp = @fsockopen($host, 80)))
  326. return false;
  327. $req = "HEAD $path HTTP/1.1\r\nUser-Agent: PHP/".phpversion();
  328. $req.= "\r\nHost: $host\r\nAccept: */*\r\n\r\n";
  329. fputs($fp, $req);
  330. while (!feof($fp))
  331. {
  332. $str = fgets($fp, 4096);
  333. if (strpos(strtolower($str), 'last-modified') !== false)
  334. {
  335. $modtime = substr($str, 15);
  336. break;
  337. }
  338. }
  339. fclose($fp);
  340. }
  341. return (isset($modtime)) ? $modtime : 0;
  342. }
  343. }
  344. ?>