foaf.php 12 KB

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