The official, canonical postActiv repository. http://www.postactiv.com

foafgroup.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /* ============================================================================
  3. * Title: FOAFGroup
  4. * FOAF implementation for groups
  5. *
  6. * postActiv:
  7. * the micro-blogging software
  8. *
  9. * Copyright:
  10. * Copyright (C) 2016-2018, Maiyannah Bishop
  11. *
  12. * Derived from code copyright various sources:
  13. * o GNU Social (C) 2013-2016, Free Software Foundation, Inc
  14. * o StatusNet (C) 2008-2012, StatusNet, Inc
  15. * ----------------------------------------------------------------------------
  16. * License:
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * <https://www.gnu.org/licenses/agpl.html>
  31. * ----------------------------------------------------------------------------
  32. * About:
  33. * FOAF implementation for groups
  34. *
  35. * PHP version:
  36. * Tested with PHP 7.0
  37. * ----------------------------------------------------------------------------
  38. * File Authors:
  39. * o Toby Inkster <mail@tobyinkster.co.uk>
  40. * o Evan Prodromou
  41. * o Christopher Vollick <psycotica0@gmail.com>
  42. * o Brion Vibber <brion@pobox.com>
  43. * o Siebrand Mazeland <s.mazeland@xs4all.nl>
  44. * o Mikael Nordfeldth <mmn@hethane.se>
  45. * o Maiyannah Bishop <maiyannah.bishop@postactiv.com>
  46. *
  47. * Web:
  48. * o postActiv <http://www.postactiv.com>
  49. * o GNU social <https://www.gnu.org/s/social/>
  50. * ============================================================================
  51. */
  52. // This file is formatted so that it provides useful documentation output in
  53. // NaturalDocs. Please be considerate of this before changing formatting.
  54. if (!defined('POSTACTIV')) { exit(1); }
  55. // @todo XXX: Documentation missing.
  56. class FoafGroupAction extends Action
  57. {
  58. // -------------------------------------------------------------------------
  59. // Function: isReadOnly
  60. // Extends the Action bit to indicate this is a read-only action
  61. //
  62. // Returns:
  63. // o boolean true
  64. function isReadOnly($args) {
  65. return true;
  66. }
  67. // -------------------------------------------------------------------------
  68. // Function: prepare
  69. // Readies the FOAF entries to be displayed in a RDF document
  70. //
  71. // Parameters:
  72. // o array args - passed to parent prepare() function
  73. //
  74. // Returns:
  75. // o boolean True on success, boolean False on redirect
  76. //
  77. // Error States:
  78. // o No such group - when a group is polled for FOAF and doesn't exist, or
  79. // the group is non-local
  80. // o Redirect on non-canonical nicks
  81. function prepare(array $args = array())
  82. {
  83. parent::prepare($args);
  84. $nickname_arg = $this->arg('nickname');
  85. if (empty($nickname_arg)) {
  86. // TRANS: Client error displayed when requesting Friends of a Friend feed without providing a group nickname.
  87. $this->clientError(_('No such group.'), 404);
  88. }
  89. $this->nickname = common_canonical_nickname($nickname_arg);
  90. // Permanent redirect on non-canonical nickname
  91. if ($nickname_arg != $this->nickname) {
  92. common_redirect(common_local_url('foafgroup',
  93. array('nickname' => $this->nickname)),
  94. 301);
  95. return false;
  96. }
  97. $local = Local_group::getKV('nickname', $this->nickname);
  98. if (!$local) {
  99. // TRANS: Client error displayed when requesting Friends of a Friend feed for a non-local group.
  100. $this->clientError(_('No such group.'), 404);
  101. }
  102. $this->group = User_group::getKV('id', $local->group_id);
  103. if (!$this->group) {
  104. // TRANS: Client error displayed when requesting Friends of a Friend feed for a nickname that is not a group.
  105. $this->clientError(_('No such group.'), 404);
  106. }
  107. common_set_returnto($this->selfUrl());
  108. return true;
  109. }
  110. // -------------------------------------------------------------------------
  111. // Function: handle
  112. // Outputs a RDF document for the FOAF display
  113. //
  114. // Returns:
  115. // o void
  116. function handle() {
  117. parent::handle();
  118. header('Content-Type: application/rdf+xml');
  119. $this->startXML();
  120. $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
  121. 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  122. 'xmlns:dcterms' =>
  123. 'http://purl.org/dc/terms/',
  124. 'xmlns:sioc' =>
  125. 'http://rdfs.org/sioc/ns#',
  126. 'xmlns:foaf' =>
  127. 'http://xmlns.com/foaf/0.1/',
  128. 'xmlns:statusnet' =>
  129. 'http://status.net/ont/',
  130. 'xmlns' => 'http://xmlns.com/foaf/0.1/'));
  131. $this->showPpd(common_local_url('foafgroup', array('nickname' => $this->nickname)), $this->group->permalink());
  132. $this->elementStart('Group', array('rdf:about' =>
  133. $this->group->permalink()));
  134. if ($this->group->fullname) {
  135. $this->element('name', null, $this->group->fullname);
  136. }
  137. if ($this->group->description) {
  138. $this->element('dcterms:description', null, $this->group->description);
  139. }
  140. if ($this->group->nickname) {
  141. $this->element('dcterms:identifier', null, $this->group->nickname);
  142. $this->element('nick', null, $this->group->nickname);
  143. }
  144. foreach ($this->group->getAliases() as $alias) {
  145. $this->element('nick', null, $alias);
  146. }
  147. if ($this->group->homeUrl()) {
  148. $this->element('weblog', array('rdf:resource' => $this->group->homeUrl()));
  149. }
  150. if ($this->group->homepage) {
  151. $this->element('page', array('rdf:resource' => $this->group->homepage));
  152. }
  153. if ($this->group->homepage_logo) {
  154. $this->element('depiction', array('rdf:resource' => $this->group->homepage_logo));
  155. }
  156. $members = $this->group->getMembers();
  157. $member_details = array();
  158. while ($members->fetch()) {
  159. $member_uri = common_local_url('userbyid', array('id'=>$members->id));
  160. $member_details[$member_uri] = array(
  161. 'nickname' => $members->nickname,
  162. 'is_admin' => false,
  163. );
  164. $this->element('member', array('rdf:resource' => $member_uri));
  165. }
  166. $admins = $this->group->getAdmins();
  167. while ($admins->fetch()) {
  168. $admin_uri = common_local_url('userbyid', array('id'=>$admins->id));
  169. $member_details[$admin_uri]['is_admin'] = true;
  170. $this->element('statusnet:groupAdmin', array('rdf:resource' => $admin_uri));
  171. }
  172. $this->elementEnd('Group');
  173. ksort($member_details);
  174. foreach ($member_details as $uri => $details) {
  175. if ($details['is_admin']) {
  176. $this->elementStart('Agent', array('rdf:about' => $uri));
  177. $this->element('nick', null, $details['nickname']);
  178. $this->elementStart('account');
  179. $this->elementStart('sioc:User', array('rdf:about'=>$uri.'#acct'));
  180. $this->elementStart('sioc:has_function');
  181. $this->elementStart('statusnet:GroupAdminRole');
  182. $this->element('sioc:scope', array('rdf:resource' => $this->group->permalink()));
  183. $this->elementEnd('statusnet:GroupAdminRole');
  184. $this->elementEnd('sioc:has_function');
  185. $this->elementEnd('sioc:User');
  186. $this->elementEnd('account');
  187. $this->elementEnd('Agent');
  188. } else {
  189. $this->element('Agent', array(
  190. 'foaf:nick' => $details['nickname'],
  191. 'rdf:about' => $uri,));
  192. }
  193. }
  194. $this->elementEnd('rdf:RDF');
  195. $this->endXML();
  196. }
  197. // -------------------------------------------------------------------------
  198. // Function: showPdp
  199. // Writes a document RDF code for the FOF entry.
  200. //
  201. // Returns:
  202. // o void
  203. function showPpd($foaf_url, $person_uri)
  204. {
  205. $this->elementStart('Document', array('rdf:about' => $foaf_url));
  206. $this->element('primaryTopic', array('rdf:resource' => $person_uri));
  207. $this->elementEnd('Document');
  208. }
  209. }
  210. // END OF FILE
  211. // ============================================================================
  212. ?>