Parser.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * SimplePie
  4. *
  5. * A PHP-Based RSS and Atom Feed Framework.
  6. * Takes the hard work out of managing a complete RSS/Atom solution.
  7. *
  8. * Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  22. * to endorse or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package SimplePie
  36. * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
  37. * @author Ryan Parman
  38. * @author Sam Sneddon
  39. * @author Ryan McCue
  40. * @link http://simplepie.org/ SimplePie
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. */
  43. /**
  44. * Parses the XML Declaration
  45. *
  46. * @package SimplePie
  47. * @subpackage Parsing
  48. */
  49. class SimplePie_XML_Declaration_Parser
  50. {
  51. /**
  52. * XML Version
  53. *
  54. * @access public
  55. * @var string
  56. */
  57. var $version = '1.0';
  58. /**
  59. * Encoding
  60. *
  61. * @access public
  62. * @var string
  63. */
  64. var $encoding = 'UTF-8';
  65. /**
  66. * Standalone
  67. *
  68. * @access public
  69. * @var bool
  70. */
  71. var $standalone = false;
  72. /**
  73. * Current state of the state machine
  74. *
  75. * @access private
  76. * @var string
  77. */
  78. var $state = 'before_version_name';
  79. /**
  80. * Input data
  81. *
  82. * @access private
  83. * @var string
  84. */
  85. var $data = '';
  86. /**
  87. * Input data length (to avoid calling strlen() everytime this is needed)
  88. *
  89. * @access private
  90. * @var int
  91. */
  92. var $data_length = 0;
  93. /**
  94. * Current position of the pointer
  95. *
  96. * @var int
  97. * @access private
  98. */
  99. var $position = 0;
  100. /**
  101. * Create an instance of the class with the input data
  102. *
  103. * @access public
  104. * @param string $data Input data
  105. */
  106. public function __construct($data)
  107. {
  108. $this->data = $data;
  109. $this->data_length = strlen($this->data);
  110. }
  111. /**
  112. * Parse the input data
  113. *
  114. * @access public
  115. * @return bool true on success, false on failure
  116. */
  117. public function parse()
  118. {
  119. while ($this->state && $this->state !== 'emit' && $this->has_data())
  120. {
  121. $state = $this->state;
  122. $this->$state();
  123. }
  124. $this->data = '';
  125. if ($this->state === 'emit')
  126. {
  127. return true;
  128. }
  129. $this->version = '';
  130. $this->encoding = '';
  131. $this->standalone = '';
  132. return false;
  133. }
  134. /**
  135. * Check whether there is data beyond the pointer
  136. *
  137. * @access private
  138. * @return bool true if there is further data, false if not
  139. */
  140. public function has_data()
  141. {
  142. return (bool) ($this->position < $this->data_length);
  143. }
  144. /**
  145. * Advance past any whitespace
  146. *
  147. * @return int Number of whitespace characters passed
  148. */
  149. public function skip_whitespace()
  150. {
  151. $whitespace = strspn($this->data, "\x09\x0A\x0D\x20", $this->position);
  152. $this->position += $whitespace;
  153. return $whitespace;
  154. }
  155. /**
  156. * Read value
  157. */
  158. public function get_value()
  159. {
  160. $quote = substr($this->data, $this->position, 1);
  161. if ($quote === '"' || $quote === "'")
  162. {
  163. $this->position++;
  164. $len = strcspn($this->data, $quote, $this->position);
  165. if ($this->has_data())
  166. {
  167. $value = substr($this->data, $this->position, $len);
  168. $this->position += $len + 1;
  169. return $value;
  170. }
  171. }
  172. return false;
  173. }
  174. public function before_version_name()
  175. {
  176. if ($this->skip_whitespace())
  177. {
  178. $this->state = 'version_name';
  179. }
  180. else
  181. {
  182. $this->state = false;
  183. }
  184. }
  185. public function version_name()
  186. {
  187. if (substr($this->data, $this->position, 7) === 'version')
  188. {
  189. $this->position += 7;
  190. $this->skip_whitespace();
  191. $this->state = 'version_equals';
  192. }
  193. else
  194. {
  195. $this->state = false;
  196. }
  197. }
  198. public function version_equals()
  199. {
  200. if (substr($this->data, $this->position, 1) === '=')
  201. {
  202. $this->position++;
  203. $this->skip_whitespace();
  204. $this->state = 'version_value';
  205. }
  206. else
  207. {
  208. $this->state = false;
  209. }
  210. }
  211. public function version_value()
  212. {
  213. if ($this->version = $this->get_value())
  214. {
  215. $this->skip_whitespace();
  216. if ($this->has_data())
  217. {
  218. $this->state = 'encoding_name';
  219. }
  220. else
  221. {
  222. $this->state = 'emit';
  223. }
  224. }
  225. else
  226. {
  227. $this->state = false;
  228. }
  229. }
  230. public function encoding_name()
  231. {
  232. if (substr($this->data, $this->position, 8) === 'encoding')
  233. {
  234. $this->position += 8;
  235. $this->skip_whitespace();
  236. $this->state = 'encoding_equals';
  237. }
  238. else
  239. {
  240. $this->state = 'standalone_name';
  241. }
  242. }
  243. public function encoding_equals()
  244. {
  245. if (substr($this->data, $this->position, 1) === '=')
  246. {
  247. $this->position++;
  248. $this->skip_whitespace();
  249. $this->state = 'encoding_value';
  250. }
  251. else
  252. {
  253. $this->state = false;
  254. }
  255. }
  256. public function encoding_value()
  257. {
  258. if ($this->encoding = $this->get_value())
  259. {
  260. $this->skip_whitespace();
  261. if ($this->has_data())
  262. {
  263. $this->state = 'standalone_name';
  264. }
  265. else
  266. {
  267. $this->state = 'emit';
  268. }
  269. }
  270. else
  271. {
  272. $this->state = false;
  273. }
  274. }
  275. public function standalone_name()
  276. {
  277. if (substr($this->data, $this->position, 10) === 'standalone')
  278. {
  279. $this->position += 10;
  280. $this->skip_whitespace();
  281. $this->state = 'standalone_equals';
  282. }
  283. else
  284. {
  285. $this->state = false;
  286. }
  287. }
  288. public function standalone_equals()
  289. {
  290. if (substr($this->data, $this->position, 1) === '=')
  291. {
  292. $this->position++;
  293. $this->skip_whitespace();
  294. $this->state = 'standalone_value';
  295. }
  296. else
  297. {
  298. $this->state = false;
  299. }
  300. }
  301. public function standalone_value()
  302. {
  303. if ($standalone = $this->get_value())
  304. {
  305. switch ($standalone)
  306. {
  307. case 'yes':
  308. $this->standalone = true;
  309. break;
  310. case 'no':
  311. $this->standalone = false;
  312. break;
  313. default:
  314. $this->state = false;
  315. return;
  316. }
  317. $this->skip_whitespace();
  318. if ($this->has_data())
  319. {
  320. $this->state = false;
  321. }
  322. else
  323. {
  324. $this->state = 'emit';
  325. }
  326. }
  327. else
  328. {
  329. $this->state = false;
  330. }
  331. }
  332. }