foaf.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. define('LISTENER', 1);
  21. define('LISTENEE', -1);
  22. define('BOTH', 0);
  23. // @todo XXX: Documentation missing.
  24. class FoafAction extends ManagedAction
  25. {
  26. function isReadOnly($args)
  27. {
  28. return true;
  29. }
  30. protected function doPreparation()
  31. {
  32. $nickname_arg = $this->arg('nickname');
  33. if (empty($nickname_arg)) {
  34. // TRANS: Client error displayed when requesting Friends of a Friend feed without providing a user nickname.
  35. $this->clientError(_('No such user.'), 404);
  36. }
  37. $this->nickname = common_canonical_nickname($nickname_arg);
  38. // Permanent redirect on non-canonical nickname
  39. if ($nickname_arg != $this->nickname) {
  40. common_redirect(common_local_url('foaf',
  41. array('nickname' => $this->nickname)),
  42. 301);
  43. }
  44. $this->user = User::getKV('nickname', $this->nickname);
  45. if (!$this->user) {
  46. // TRANS: Client error displayed when requesting Friends of a Friend feed for an object that is not a user.
  47. $this->clientError(_('No such user.'), 404);
  48. }
  49. $this->profile = $this->user->getProfile();
  50. if (!$this->profile) {
  51. // TRANS: Error message displayed when referring to a user without a profile.
  52. $this->serverError(_('User has no profile.'), 500);
  53. }
  54. return true;
  55. }
  56. public function showPage()
  57. {
  58. header('Content-Type: application/rdf+xml');
  59. $this->startXML();
  60. $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
  61. 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  62. 'xmlns:rdfs' =>
  63. 'http://www.w3.org/2000/01/rdf-schema#',
  64. 'xmlns:geo' =>
  65. 'http://www.w3.org/2003/01/geo/wgs84_pos#',
  66. 'xmlns:bio' =>
  67. 'http://purl.org/vocab/bio/0.1/',
  68. 'xmlns:sioc' =>
  69. 'http://rdfs.org/sioc/ns#',
  70. 'xmlns' => 'http://xmlns.com/foaf/0.1/'));
  71. // This is the document about the user
  72. $this->showPpd('', $this->user->getUri());
  73. // Would be nice to tell if they were a Person or not (e.g. a #person usertag?)
  74. $this->elementStart('Agent', array('rdf:about' => $this->user->getUri()));
  75. if (common_config('foaf', 'mbox_sha1sum') && $this->user->email) {
  76. $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
  77. }
  78. if ($this->profile->fullname) {
  79. $this->element('name', null, $this->profile->fullname);
  80. }
  81. if ($this->profile->homepage) {
  82. $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
  83. }
  84. if ($this->profile->profileurl) {
  85. $this->element('weblog', array('rdf:resource' => $this->profile->profileurl));
  86. }
  87. if ($this->profile->bio) {
  88. $this->element('bio:olb', null, $this->profile->bio);
  89. }
  90. $location = $this->profile->getLocation();
  91. if ($location) {
  92. $attr = array();
  93. if ($location->getRdfURL()) {
  94. $attr['rdf:about'] = $location->getRdfURL();
  95. }
  96. $location_name = $location->getName();
  97. $this->elementStart('based_near');
  98. $this->elementStart('geo:SpatialThing', $attr);
  99. if ($location_name) {
  100. $this->element('name', null, $location_name);
  101. }
  102. if ($location->lat) {
  103. $this->element('geo:lat', null, $location->lat);
  104. }
  105. if ($location->lon) {
  106. $this->element('geo:long', null, $location->lon);
  107. }
  108. if ($location->getURL()) {
  109. $this->element('page', array('rdf:resource'=>$location->getURL()));
  110. }
  111. $this->elementEnd('geo:SpatialThing');
  112. $this->elementEnd('based_near');
  113. }
  114. try {
  115. $avatar = Avatar::getUploaded($this->profile);
  116. $this->elementStart('img');
  117. $this->elementStart('Image', array('rdf:about' => $avatar->displayUrl()));
  118. foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
  119. try {
  120. $scaled = $this->profile->getAvatar($size);
  121. $this->elementStart('thumbnail');
  122. $this->element('Image', array('rdf:about' => $scaled->displayUrl()));
  123. $this->elementEnd('thumbnail');
  124. } catch (Exception $e) {
  125. // This avatar did not exist
  126. }
  127. }
  128. $this->elementEnd('Image');
  129. $this->elementEnd('img');
  130. } catch (NoAvatarException $e) {
  131. // No avatar for this user!
  132. }
  133. $person = $this->showMicrobloggingAccount($this->profile,
  134. common_root_url(), $this->user->getUri(),
  135. /*$fetchSubscriptions*/true,
  136. /*$isSubscriber*/false);
  137. // Get people who subscribe to user
  138. $sub = new Subscription();
  139. $sub->subscribed = $this->profile->id;
  140. $sub->whereAdd('subscriber != subscribed');
  141. if ($sub->find()) {
  142. while ($sub->fetch()) {
  143. $profile = Profile::getKV('id', $sub->subscriber);
  144. if (!$profile instanceof Profile) {
  145. common_debug('Got a bad subscription: '.print_r($sub,true));
  146. continue;
  147. }
  148. $other_uri = $profile->getUri();
  149. if (array_key_exists($other_uri, $person)) {
  150. $person[$other_uri][0] = BOTH;
  151. } else {
  152. $person[$other_uri] = array(LISTENER,
  153. $profile->id,
  154. $profile->nickname,
  155. $profile->isLocal() ? 'local' : 'remote');
  156. }
  157. unset($profile);
  158. }
  159. }
  160. unset($sub);
  161. foreach ($person as $uri => $p) {
  162. list($type, $id, $nickname, $local) = $p;
  163. if ($type == BOTH) {
  164. $this->element('knows', array('rdf:resource' => $uri));
  165. }
  166. }
  167. $this->elementEnd('Agent');
  168. foreach ($person as $uri => $p) {
  169. $foaf_url = null;
  170. list($type, $id, $nickname, $local) = $p;
  171. if ($local == 'local') {
  172. $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
  173. }
  174. $profile = Profile::getKV($id);
  175. $this->elementStart('Agent', array('rdf:about' => $uri));
  176. if ($type == BOTH) {
  177. $this->element('knows', array('rdf:resource' => $this->user->getUri()));
  178. }
  179. $this->showMicrobloggingAccount($profile,
  180. ($local == 'local') ? common_root_url() : null,
  181. $uri,
  182. /*$fetchSubscriptions*/false,
  183. /*$isSubscriber*/($type == LISTENER || $type == BOTH));
  184. if ($foaf_url) {
  185. $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
  186. }
  187. $this->elementEnd('Agent');
  188. if ($foaf_url) {
  189. $this->showPpd($foaf_url, $uri);
  190. }
  191. $profile->free();
  192. $profile = null;
  193. unset($profile);
  194. }
  195. $this->elementEnd('rdf:RDF');
  196. $this->endXML();
  197. }
  198. function showPpd($foaf_url, $person_uri)
  199. {
  200. $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
  201. $this->element('maker', array('rdf:resource' => $person_uri));
  202. $this->element('primaryTopic', array('rdf:resource' => $person_uri));
  203. $this->elementEnd('PersonalProfileDocument');
  204. }
  205. /**
  206. * Output FOAF <account> bit for the given profile.
  207. *
  208. * @param Profile $profile
  209. * @param mixed $service Root URL of this StatusNet instance for a local
  210. * user, otherwise null.
  211. * @param mixed $useruri URI string for the referenced profile..
  212. * @param boolean $fetchSubscriptions Should we load and list all their subscriptions?
  213. * @param boolean $isSubscriber if not fetching subs, we can still mark the user as following the current page.
  214. *
  215. * @return array if $fetchSubscribers is set, return a list of info on those
  216. * subscriptions.
  217. */
  218. function showMicrobloggingAccount($profile, $service=null, $useruri=null, $fetchSubscriptions=false, $isSubscriber=false)
  219. {
  220. $attr = array();
  221. if ($useruri) {
  222. $attr['rdf:about'] = $useruri . '#acct';
  223. }
  224. // Their account
  225. $this->elementStart('account');
  226. $this->elementStart('OnlineAccount', $attr);
  227. if ($service) {
  228. $this->element('accountServiceHomepage', array('rdf:resource' =>
  229. $service));
  230. }
  231. $this->element('accountName', null, $profile->nickname);
  232. $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
  233. if ($useruri) {
  234. $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
  235. }
  236. $person = array();
  237. if ($fetchSubscriptions) {
  238. // Get people user is subscribed to
  239. $sub = new Subscription();
  240. $sub->subscriber = $profile->id;
  241. $sub->whereAdd('subscriber != subscribed');
  242. if ($sub->find()) {
  243. while ($sub->fetch()) {
  244. $profile = Profile::getKV('id', $sub->subscribed);
  245. if (empty($profile)) {
  246. common_debug('Got a bad subscription: '.print_r($sub,true));
  247. continue;
  248. }
  249. $other_uri = $profile->getUri();
  250. $this->element('sioc:follows', array('rdf:resource' => $other_uri.'#acct'));
  251. $person[$other_uri] = array(LISTENEE,
  252. $profile->id,
  253. $profile->nickname,
  254. $profile->isLocal() ? 'local' : 'remote');
  255. unset($profile);
  256. }
  257. }
  258. unset($sub);
  259. } else if ($isSubscriber) {
  260. // Just declare that they follow the user whose FOAF we're showing.
  261. $this->element('sioc:follows', array('rdf:resource' => $this->user->getUri() . '#acct'));
  262. }
  263. $this->elementEnd('OnlineAccount');
  264. $this->elementEnd('account');
  265. return $person;
  266. }
  267. }