PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/Auth/OpenID/Parse.php

https://github.com/chregu/fluxcms
PHP | 282 lines | 136 code | 33 blank | 113 comment | 13 complexity | a59a7a2ce5711f050e463b4b26a34eab MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This module implements a VERY limited parser that finds <link> tags
  4. * in the head of HTML or XHTML documents and parses out their
  5. * attributes according to the OpenID spec. It is a liberal parser,
  6. * but it requires these things from the data in order to work:
  7. *
  8. * - There must be an open <html> tag
  9. *
  10. * - There must be an open <head> tag inside of the <html> tag
  11. *
  12. * - Only <link>s that are found inside of the <head> tag are parsed
  13. * (this is by design)
  14. *
  15. * - The parser follows the OpenID specification in resolving the
  16. * attributes of the link tags. This means that the attributes DO
  17. * NOT get resolved as they would by an XML or HTML parser. In
  18. * particular, only certain entities get replaced, and href
  19. * attributes do not get resolved relative to a base URL.
  20. *
  21. * From http://openid.net/specs.bml:
  22. *
  23. * - The openid.server URL MUST be an absolute URL. OpenID consumers
  24. * MUST NOT attempt to resolve relative URLs.
  25. *
  26. * - The openid.server URL MUST NOT include entities other than &amp;,
  27. * &lt;, &gt;, and &quot;.
  28. *
  29. * The parser ignores SGML comments and <![CDATA[blocks]]>. Both kinds
  30. * of quoting are allowed for attributes.
  31. *
  32. * The parser deals with invalid markup in these ways:
  33. *
  34. * - Tag names are not case-sensitive
  35. *
  36. * - The <html> tag is accepted even when it is not at the top level
  37. *
  38. * - The <head> tag is accepted even when it is not a direct child of
  39. * the <html> tag, but a <html> tag must be an ancestor of the
  40. * <head> tag
  41. *
  42. * - <link> tags are accepted even when they are not direct children
  43. * of the <head> tag, but a <head> tag must be an ancestor of the
  44. * <link> tag
  45. *
  46. * - If there is no closing tag for an open <html> or <head> tag, the
  47. * remainder of the document is viewed as being inside of the
  48. * tag. If there is no closing tag for a <link> tag, the link tag is
  49. * treated as a short tag. Exceptions to this rule are that <html>
  50. * closes <html> and <body> or <head> closes <head>
  51. *
  52. * - Attributes of the <link> tag are not required to be quoted.
  53. *
  54. * - In the case of duplicated attribute names, the attribute coming
  55. * last in the tag will be the value returned.
  56. *
  57. * - Any text that does not parse as an attribute within a link tag
  58. * will be ignored. (e.g. <link pumpkin rel='openid.server' /> will
  59. * ignore pumpkin)
  60. *
  61. * - If there are more than one <html> or <head> tag, the parser only
  62. * looks inside of the first one.
  63. *
  64. * - The contents of <script> tags are ignored entirely, except
  65. * unclosed <script> tags. Unclosed <script> tags are ignored.
  66. *
  67. * - Any other invalid markup is ignored, including unclosed SGML
  68. * comments and unclosed <![CDATA[blocks.
  69. *
  70. * PHP versions 4 and 5
  71. *
  72. * LICENSE: See the COPYING file included in this distribution.
  73. *
  74. * @access private
  75. * @package OpenID
  76. * @author JanRain, Inc. <openid@janrain.com>
  77. * @copyright 2005 Janrain, Inc.
  78. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  79. */
  80. /**
  81. * Require Auth_OpenID::arrayGet().
  82. */
  83. require_once "Auth/OpenID.php";
  84. class Auth_OpenID_Parse {
  85. /**
  86. * Specify some flags for use with regex matching.
  87. */
  88. var $_re_flags = "si";
  89. /**
  90. * Stuff to remove before we start looking for tags
  91. */
  92. var $_removed_re =
  93. "<!--.*?-->|<!\[CDATA\[.*?\]\]>|<script\b(?!:)[^>]*>.*?<\/script>";
  94. /**
  95. * Starts with the tag name at a word boundary, where the tag name
  96. * is not a namespace
  97. */
  98. var $_tag_expr = "<%s\b(?!:)([^>]*?)(?:\/>|>(.*?)(?:<\/?%s[^>]*>|\Z))";
  99. var $_attr_find = '\b(\w+)=("[^"]*"|\'[^\']*\'|[^\'"\s\/<>]+)';
  100. function Auth_OpenID_Parse()
  101. {
  102. $this->_link_find = sprintf("/<link\b(?!:)([^>]*)(?!<)>/%s",
  103. $this->_re_flags);
  104. $this->_entity_replacements = array(
  105. 'amp' => '&',
  106. 'lt' => '<',
  107. 'gt' => '>',
  108. 'quot' => '"'
  109. );
  110. $this->_attr_find = sprintf("/%s/%s",
  111. $this->_attr_find,
  112. $this->_re_flags);
  113. $this->_removed_re = sprintf("/%s/%s",
  114. $this->_removed_re,
  115. $this->_re_flags);
  116. $this->_ent_replace =
  117. sprintf("&(%s);", implode("|",
  118. $this->_entity_replacements));
  119. }
  120. /**
  121. * Returns a regular expression that will match a given tag in an
  122. * SGML string.
  123. */
  124. function tagMatcher($tag_name, $close_tags = null)
  125. {
  126. if ($close_tags) {
  127. $options = implode("|", array_merge(array($tag_name), $close_tags));
  128. $closer = sprintf("(?:%s)", $options);
  129. } else {
  130. $closer = $tag_name;
  131. }
  132. $expr = sprintf($this->_tag_expr, $tag_name, $closer);
  133. return sprintf("/%s/%s", $expr, $this->_re_flags);
  134. }
  135. function htmlFind()
  136. {
  137. return $this->tagMatcher('html');
  138. }
  139. function headFind()
  140. {
  141. return $this->tagMatcher('head', array('body'));
  142. }
  143. function replaceEntities($str)
  144. {
  145. foreach ($this->_entity_replacements as $old => $new) {
  146. $str = preg_replace(sprintf("/&%s;/", $old), $new, $str);
  147. }
  148. return $str;
  149. }
  150. function removeQuotes($str)
  151. {
  152. $matches = array();
  153. $double = '/^"(.*)"$/';
  154. $single = "/^\'(.*)\'$/";
  155. if (preg_match($double, $str, $matches)) {
  156. return $matches[1];
  157. } else if (preg_match($single, $str, $matches)) {
  158. return $matches[1];
  159. } else {
  160. return $str;
  161. }
  162. }
  163. /**
  164. * Find all link tags in a string representing a HTML document and
  165. * return a list of their attributes.
  166. *
  167. * @param string $html The text to parse
  168. * @return array $list An array of arrays of attributes, one for each
  169. * link tag
  170. */
  171. function parseLinkAttrs($html)
  172. {
  173. $dom = new domdocument();
  174. $dom->loadHTML($html);
  175. $link_data = array();
  176. $xp = new domxpath($dom);
  177. $link_matches = $xp->query("/html/head/link");
  178. foreach ($link_matches as $link) {
  179. $link_attrs = array();
  180. foreach ($link->attributes as $attr) {
  181. $link_attrs[strtolower($attr->name)] = $attr->value;
  182. }
  183. $link_data[] = $link_attrs;
  184. }
  185. return $link_data;
  186. }
  187. function relMatches($rel_attr, $target_rel)
  188. {
  189. // Does this target_rel appear in the rel_str?
  190. // XXX: TESTME
  191. $rels = preg_split("/\s+/", trim($rel_attr));
  192. foreach ($rels as $rel) {
  193. $rel = strtolower($rel);
  194. if ($rel == $target_rel) {
  195. return 1;
  196. }
  197. }
  198. return 0;
  199. }
  200. function linkHasRel($link_attrs, $target_rel)
  201. {
  202. // Does this link have target_rel as a relationship?
  203. // XXX: TESTME
  204. $rel_attr = Auth_OpeniD::arrayGet($link_attrs, 'rel', null);
  205. return ($rel_attr && $this->relMatches($rel_attr,
  206. $target_rel));
  207. }
  208. function findLinksRel($link_attrs_list, $target_rel)
  209. {
  210. // Filter the list of link attributes on whether it has
  211. // target_rel as a relationship.
  212. // XXX: TESTME
  213. $result = array();
  214. foreach ($link_attrs_list as $attr) {
  215. if ($this->linkHasRel($attr, $target_rel)) {
  216. $result[] = $attr;
  217. }
  218. }
  219. return $result;
  220. }
  221. function findFirstHref($link_attrs_list, $target_rel)
  222. {
  223. // Return the value of the href attribute for the first link
  224. // tag in the list that has target_rel as a relationship.
  225. // XXX: TESTME
  226. $matches = $this->findLinksRel($link_attrs_list,
  227. $target_rel);
  228. if (!$matches) {
  229. return null;
  230. }
  231. $first = $matches[0];
  232. return Auth_OpenID::arrayGet($first, 'href', null);
  233. }
  234. }
  235. function Auth_OpenID_legacy_discover($html_text)
  236. {
  237. $p = new Auth_OpenID_Parse();
  238. $link_attrs = $p->parseLinkAttrs($html_text);
  239. $server_url = $p->findFirstHref($link_attrs,
  240. 'openid.server');
  241. if ($server_url === null) {
  242. return false;
  243. } else {
  244. $delegate_url = $p->findFirstHref($link_attrs,
  245. 'openid.delegate');
  246. return array($delegate_url, $server_url);
  247. }
  248. }
  249. ?>