guts.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. require('../arc/ARC2.php');
  3. if (!function_exists('json_decode')) {
  4. function json_decode($content, $assoc = false) {
  5. require_once('Services/JSON.php');
  6. if ($assoc) {
  7. $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  8. } else {
  9. $json = new Services_JSON;
  10. }
  11. return $json->decode($content);
  12. }
  13. }
  14. if (!function_exists('json_encode')) {
  15. function json_encode($content) {
  16. require_once('Services/JSON.php');
  17. $json = new Services_JSON;
  18. return $json->encode($content);
  19. }
  20. }
  21. function _resolve_relative_url ($absolute, $relative) {
  22. $p = parse_url($relative);
  23. if ($p['scheme']) {
  24. return $relative;
  25. }
  26. extract(parse_url($absolute));
  27. $path = dirname($path);
  28. if ($relative{0} == '/') {
  29. $cparts = array_filter(explode('/', $relative));
  30. } else {
  31. $aparts = array_filter(explode('/', $path));
  32. $rparts = array_filter(explode('/', $relative));
  33. $cparts = array_merge($aparts, $rparts);
  34. foreach ($cparts as $i => $part) {
  35. if ($part == '.') {
  36. $cparts[$i] = null;
  37. } else if ($part == '..') {
  38. $cparts[$i - 1] = null;
  39. $cparts[$i] = null;
  40. }
  41. }
  42. $cparts = array_filter($cparts);
  43. }
  44. $path = implode('/', $cparts);
  45. $url = '';
  46. if ($scheme) {
  47. $url = "$scheme://";
  48. }
  49. if ($user) {
  50. $url .= "$user";
  51. if ($pass) {
  52. $url .= ":$pass";
  53. }
  54. $url .= '@';
  55. }
  56. if ($host) {
  57. $url .= "$host/";
  58. }
  59. $url .= $path;
  60. return $url;
  61. }
  62. function _http($uri) {
  63. if (function_exists('curl_init')) {
  64. $ch = curl_init($uri);
  65. curl_setopt($ch, CURLOPT_HEADER, 0);
  66. ob_start();
  67. curl_exec($ch);
  68. curl_close($ch);
  69. return ob_get_clean();
  70. } else if (function_exists('parse_url')) {
  71. $_uri = parse_url($uri);
  72. if (!$_uri['port']) {
  73. $_uri['port'] = 80;
  74. }
  75. if (!($nh = fsockopen($_uri['host'], $_uri['port'], $errno, $errstr, 20))) {
  76. header('Content-Type: text/plain');
  77. die("Could not open network connection! ($errno - $errstr)\r\n");
  78. }
  79. fwrite($nh, "GET {$_uri[path]}?{$_uri[query]} HTTP/1.0\r\n"
  80. . "Host: {$_uri['host']}\r\n"
  81. . "Connection: close\r\n\r\n"
  82. );
  83. while (!feof($nh)) {
  84. $output .= fgets($nh, 128);
  85. }
  86. fclose($nh);
  87. // Remove HTTP header.
  88. return substr(strstr($output, "\r\n\r\n"), 4);
  89. }
  90. return null;
  91. }
  92. function getFromLaconica($account) {
  93. if (!preg_match('/^https?:\/\//i', $account)) {
  94. $account = "http://identi.ca/{$account}";
  95. }
  96. preg_replace('/\/$/', '', $account);
  97. $foaf = $account . '/foaf';
  98. return getFromFOAF($foaf);
  99. }
  100. function getFromFOAF($foaf, $knownHomepage = null, $data = null) {
  101. $parser = ARC2::getRDFParser();
  102. if (empty($data)) {
  103. $parser->parse($foaf);
  104. } else {
  105. $parser->parse($foaf, $data);
  106. }
  107. $index = $parser->getSimpleIndex();
  108. if ($index[$foaf]['http://xmlns.com/foaf/0.1/primaryTopic'][0]) {
  109. $webid = $index[$foaf]['http://xmlns.com/foaf/0.1/primaryTopic'][0];
  110. }
  111. if (!$webid) {
  112. foreach ($index as $subject => $dummy) {
  113. if ($index[$subject]['http://xmlns.com/foaf/0.1/homepage']) {
  114. foreach ($index[$subject]['http://xmlns.com/foaf/0.1/homepage'] as $homepage) {
  115. if ($homepage == $knownHomepage || $homepage == $foaf) {
  116. $webid = $subject;
  117. break 2;
  118. }
  119. }
  120. }
  121. if ($index[$subject]['http://xmlns.com/foaf/0.1/weblog']) {
  122. foreach ($index[$subject]['http://xmlns.com/foaf/0.1/weblog'] as $homepage) {
  123. if ($homepage == $knownHomepage) {
  124. $webid = $subject;
  125. break 2;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. if ($webid) {
  132. $r = array(
  133. 'WebID' => $webid,
  134. 'Pages' => $index[$webid]['http://xmlns.com/foaf/0.1/homepage'],
  135. 'Name' => $index[$webid]['http://xmlns.com/foaf/0.1/name'][0]
  136. );
  137. if (substr($r['WebID'], 0, 2) == '_:') {
  138. $r['WebID'] = 'http://thing-described-by.org/?'.$foaf;
  139. }
  140. return $r;
  141. }
  142. return null;
  143. }
  144. function getFromMyOpera($account) {
  145. return array(
  146. 'WebID' => "http://my.opera.com/{$account}/xml/foaf#me",
  147. 'Pages' => array("http://my.opera.com/{$account}/")
  148. );
  149. }
  150. function getFromWebsite($url) {
  151. $str = _http($url);
  152. if (preg_match('/xmlns\:[A-Za-z0-9\.\_\-]+\=.?http...xmlns.com.foaf.0.1/', $str)) {
  153. $r = getFromFOAF($url, $url, $str);
  154. if ($r['WebID']) {
  155. return $r;
  156. }
  157. }
  158. $doc = new DOMDocument();
  159. $e = error_reporting(1);
  160. $doc->loadHTML($str);
  161. error_reporting($e);
  162. $links = $doc->getElementsByTagName('link');
  163. foreach ($links as $l) {
  164. if (preg_match('/\b(meta)\b/i', $l->getAttribute('rel'))) {
  165. $foaf = _resolve_relative_url($url, $l->getAttribute('href'));
  166. $info = getFromFOAF($foaf, $url);
  167. if ($info['WebID']) {
  168. return $info;
  169. }
  170. }
  171. }
  172. return getFromGoogleSocialGraphAPI($url);
  173. }
  174. function getFromGoogleSocialGraphAPI($url) {
  175. $api = "http://socialgraph.apis.google.com/lookup?pretty=1&sgn=1&edi=1&edo=1&fme=1&q={$url}";
  176. $data = json_decode(_http($api), 1);
  177. $canon = $data['canonical_mapping'][$url];
  178. if (substr($canon, 0, 3) == 'sgn') {
  179. return array(
  180. 'WebID' => $canon,
  181. 'Homepages' => array($url)
  182. );
  183. }
  184. }
  185. function getFromEmail($addr) {
  186. if (!substr($addr, 0, 7) == 'mailto:') {
  187. $addr = 'mailto:' . $addr;
  188. }
  189. return array(
  190. 'WebID' => 'http://foaf.me/mbox/' . sha1(strtolower($addr)) . '#me'
  191. );
  192. }
  193. function getBestGuess($string) {
  194. if (preg_match('#^http://identi.ca/([^/]+)#i', $string, $matches)) {
  195. return getFromLaconica($string);
  196. } else if (preg_match('/\@/', $string)) {
  197. return getFromEmail($email);
  198. }
  199. return getFromWebsite($string);
  200. }