Parser.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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, Geoffrey 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, Geoffrey Sneddon, Ryan McCue
  37. * @author Ryan Parman
  38. * @author Geoffrey 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. else
  130. {
  131. $this->version = '';
  132. $this->encoding = '';
  133. $this->standalone = '';
  134. return false;
  135. }
  136. }
  137. /**
  138. * Check whether there is data beyond the pointer
  139. *
  140. * @access private
  141. * @return bool true if there is further data, false if not
  142. */
  143. public function has_data()
  144. {
  145. return (bool) ($this->position < $this->data_length);
  146. }
  147. /**
  148. * Advance past any whitespace
  149. *
  150. * @return int Number of whitespace characters passed
  151. */
  152. public function skip_whitespace()
  153. {
  154. $whitespace = strspn($this->data, "\x09\x0A\x0D\x20", $this->position);
  155. $this->position += $whitespace;
  156. return $whitespace;
  157. }
  158. /**
  159. * Read value
  160. */
  161. public function get_value()
  162. {
  163. $quote = substr($this->data, $this->position, 1);
  164. if ($quote === '"' || $quote === "'")
  165. {
  166. $this->position++;
  167. $len = strcspn($this->data, $quote, $this->position);
  168. if ($this->has_data())
  169. {
  170. $value = substr($this->data, $this->position, $len);
  171. $this->position += $len + 1;
  172. return $value;
  173. }
  174. }
  175. return false;
  176. }
  177. public function before_version_name()
  178. {
  179. if ($this->skip_whitespace())
  180. {
  181. $this->state = 'version_name';
  182. }
  183. else
  184. {
  185. $this->state = false;
  186. }
  187. }
  188. public function version_name()
  189. {
  190. if (substr($this->data, $this->position, 7) === 'version')
  191. {
  192. $this->position += 7;
  193. $this->skip_whitespace();
  194. $this->state = 'version_equals';
  195. }
  196. else
  197. {
  198. $this->state = false;
  199. }
  200. }
  201. public function version_equals()
  202. {
  203. if (substr($this->data, $this->position, 1) === '=')
  204. {
  205. $this->position++;
  206. $this->skip_whitespace();
  207. $this->state = 'version_value';
  208. }
  209. else
  210. {
  211. $this->state = false;
  212. }
  213. }
  214. public function version_value()
  215. {
  216. if ($this->version = $this->get_value())
  217. {
  218. $this->skip_whitespace();
  219. if ($this->has_data())
  220. {
  221. $this->state = 'encoding_name';
  222. }
  223. else
  224. {
  225. $this->state = 'emit';
  226. }
  227. }
  228. else
  229. {
  230. $this->state = false;
  231. }
  232. }
  233. public function encoding_name()
  234. {
  235. if (substr($this->data, $this->position, 8) === 'encoding')
  236. {
  237. $this->position += 8;
  238. $this->skip_whitespace();
  239. $this->state = 'encoding_equals';
  240. }
  241. else
  242. {
  243. $this->state = 'standalone_name';
  244. }
  245. }
  246. public function encoding_equals()
  247. {
  248. if (substr($this->data, $this->position, 1) === '=')
  249. {
  250. $this->position++;
  251. $this->skip_whitespace();
  252. $this->state = 'encoding_value';
  253. }
  254. else
  255. {
  256. $this->state = false;
  257. }
  258. }
  259. public function encoding_value()
  260. {
  261. if ($this->encoding = $this->get_value())
  262. {
  263. $this->skip_whitespace();
  264. if ($this->has_data())
  265. {
  266. $this->state = 'standalone_name';
  267. }
  268. else
  269. {
  270. $this->state = 'emit';
  271. }
  272. }
  273. else
  274. {
  275. $this->state = false;
  276. }
  277. }
  278. public function standalone_name()
  279. {
  280. if (substr($this->data, $this->position, 10) === 'standalone')
  281. {
  282. $this->position += 10;
  283. $this->skip_whitespace();
  284. $this->state = 'standalone_equals';
  285. }
  286. else
  287. {
  288. $this->state = false;
  289. }
  290. }
  291. public function standalone_equals()
  292. {
  293. if (substr($this->data, $this->position, 1) === '=')
  294. {
  295. $this->position++;
  296. $this->skip_whitespace();
  297. $this->state = 'standalone_value';
  298. }
  299. else
  300. {
  301. $this->state = false;
  302. }
  303. }
  304. public function standalone_value()
  305. {
  306. if ($standalone = $this->get_value())
  307. {
  308. switch ($standalone)
  309. {
  310. case 'yes':
  311. $this->standalone = true;
  312. break;
  313. case 'no':
  314. $this->standalone = false;
  315. break;
  316. default:
  317. $this->state = false;
  318. return;
  319. }
  320. $this->skip_whitespace();
  321. if ($this->has_data())
  322. {
  323. $this->state = false;
  324. }
  325. else
  326. {
  327. $this->state = 'emit';
  328. }
  329. }
  330. else
  331. {
  332. $this->state = false;
  333. }
  334. }
  335. }