foaf.php 12 KB

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