PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/SimplePie/XML/Declaration/Parser.php

https://bitbucket.org/abnopanda/wordpress
PHP | 362 lines | 233 code | 13 blank | 116 comment | 6 complexity | 8fb1da7028c385bb9d4203c9f6732362 MD5 | raw file
  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-2012, 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. * @version 1.3.1
  37. * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  38. * @author Ryan Parman
  39. * @author Geoffrey Sneddon
  40. * @author Ryan McCue
  41. * @link http://simplepie.org/ SimplePie
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. */
  44. /**
  45. * Parses the XML Declaration
  46. *
  47. * @package SimplePie
  48. * @subpackage Parsing
  49. */
  50. class SimplePie_XML_Declaration_Parser
  51. {
  52. /**
  53. * XML Version
  54. *
  55. * @access public
  56. * @var string
  57. */
  58. var $version = '1.0';
  59. /**
  60. * Encoding
  61. *
  62. * @access public
  63. * @var string
  64. */
  65. var $encoding = 'UTF-8';
  66. /**
  67. * Standalone
  68. *
  69. * @access public
  70. * @var bool
  71. */
  72. var $standalone = false;
  73. /**
  74. * Current state of the state machine
  75. *
  76. * @access private
  77. * @var string
  78. */
  79. var $state = 'before_version_name';
  80. /**
  81. * Input data
  82. *
  83. * @access private
  84. * @var string
  85. */
  86. var $data = '';
  87. /**
  88. * Input data length (to avoid calling strlen() everytime this is needed)
  89. *
  90. * @access private
  91. * @var int
  92. */
  93. var $data_length = 0;
  94. /**
  95. * Current position of the pointer
  96. *
  97. * @var int
  98. * @access private
  99. */
  100. var $position = 0;
  101. /**
  102. * Create an instance of the class with the input data
  103. *
  104. * @access public
  105. * @param string $data Input data
  106. */
  107. public function __construct($data)
  108. {
  109. $this->data = $data;
  110. $this->data_length = strlen($this->data);
  111. }
  112. /**
  113. * Parse the input data
  114. *
  115. * @access public
  116. * @return bool true on success, false on failure
  117. */
  118. public function parse()
  119. {
  120. while ($this->state && $this->state !== 'emit' && $this->has_data())
  121. {
  122. $state = $this->state;
  123. $this->$state();
  124. }
  125. $this->data = '';
  126. if ($this->state === 'emit')
  127. {
  128. return true;
  129. }
  130. else
  131. {
  132. $this->version = '';
  133. $this->encoding = '';
  134. $this->standalone = '';
  135. return false;
  136. }
  137. }
  138. /**
  139. * Check whether there is data beyond the pointer
  140. *
  141. * @access private
  142. * @return bool true if there is further data, false if not
  143. */
  144. public function has_data()
  145. {
  146. return (bool) ($this->position < $this->data_length);
  147. }
  148. /**
  149. * Advance past any whitespace
  150. *
  151. * @return int Number of whitespace characters passed
  152. */
  153. public function skip_whitespace()
  154. {
  155. $whitespace = strspn($this->data, "\x09\x0A\x0D\x20", $this->position);
  156. $this->position += $whitespace;
  157. return $whitespace;
  158. }
  159. /**
  160. * Read value
  161. */
  162. public function get_value()
  163. {
  164. $quote = substr($this->data, $this->position, 1);
  165. if ($quote === '"' || $quote === "'")
  166. {
  167. $this->position++;
  168. $len = strcspn($this->data, $quote, $this->position);
  169. if ($this->has_data())
  170. {
  171. $value = substr($this->data, $this->position, $len);
  172. $this->position += $len + 1;
  173. return $value;
  174. }
  175. }
  176. return false;
  177. }
  178. public function before_version_name()
  179. {
  180. if ($this->skip_whitespace())
  181. {
  182. $this->state = 'version_name';
  183. }
  184. else
  185. {
  186. $this->state = false;
  187. }
  188. }
  189. public function version_name()
  190. {
  191. if (substr($this->data, $this->position, 7) === 'version')
  192. {
  193. $this->position += 7;
  194. $this->skip_whitespace();
  195. $this->state = 'version_equals';
  196. }
  197. else
  198. {
  199. $this->state = false;
  200. }
  201. }
  202. public function version_equals()
  203. {
  204. if (substr($this->data, $this->position, 1) === '=')
  205. {
  206. $this->position++;
  207. $this->skip_whitespace();
  208. $this->state = 'version_value';
  209. }
  210. else
  211. {
  212. $this->state = false;
  213. }
  214. }
  215. public function version_value()
  216. {
  217. if ($this->version = $this->get_value())
  218. {
  219. $this->skip_whitespace();
  220. if ($this->has_data())
  221. {
  222. $this->state = 'encoding_name';
  223. }
  224. else
  225. {
  226. $this->state = 'emit';
  227. }
  228. }
  229. else
  230. {
  231. $this->state = false;
  232. }
  233. }
  234. public function encoding_name()
  235. {
  236. if (substr($this->data, $this->position, 8) === 'encoding')
  237. {
  238. $this->position += 8;
  239. $this->skip_whitespace();
  240. $this->state = 'encoding_equals';
  241. }
  242. else
  243. {
  244. $this->state = 'standalone_name';
  245. }
  246. }
  247. public function encoding_equals()
  248. {
  249. if (substr($this->data, $this->position, 1) === '=')
  250. {
  251. $this->position++;
  252. $this->skip_whitespace();
  253. $this->state = 'encoding_value';
  254. }
  255. else
  256. {
  257. $this->state = false;
  258. }
  259. }
  260. public function encoding_value()
  261. {
  262. if ($this->encoding = $this->get_value())
  263. {
  264. $this->skip_whitespace();
  265. if ($this->has_data())
  266. {
  267. $this->state = 'standalone_name';
  268. }
  269. else
  270. {
  271. $this->state = 'emit';
  272. }
  273. }
  274. else
  275. {
  276. $this->state = false;
  277. }
  278. }
  279. public function standalone_name()
  280. {
  281. if (substr($this->data, $this->position, 10) === 'standalone')
  282. {
  283. $this->position += 10;
  284. $this->skip_whitespace();
  285. $this->state = 'standalone_equals';
  286. }
  287. else
  288. {
  289. $this->state = false;
  290. }
  291. }
  292. public function standalone_equals()
  293. {
  294. if (substr($this->data, $this->position, 1) === '=')
  295. {
  296. $this->position++;
  297. $this->skip_whitespace();
  298. $this->state = 'standalone_value';
  299. }
  300. else
  301. {
  302. $this->state = false;
  303. }
  304. }
  305. public function standalone_value()
  306. {
  307. if ($standalone = $this->get_value())
  308. {
  309. switch ($standalone)
  310. {
  311. case 'yes':
  312. $this->standalone = true;
  313. break;
  314. case 'no':
  315. $this->standalone = false;
  316. break;
  317. default:
  318. $this->state = false;
  319. return;
  320. }
  321. $this->skip_whitespace();
  322. if ($this->has_data())
  323. {
  324. $this->state = false;
  325. }
  326. else
  327. {
  328. $this->state = 'emit';
  329. }
  330. }
  331. else
  332. {
  333. $this->state = false;
  334. }
  335. }
  336. }