ostatusinit.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, 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. /**
  20. * @package OStatusPlugin
  21. * @maintainer James Walker <james@status.net>
  22. */
  23. if (!defined('STATUSNET')) {
  24. exit(1);
  25. }
  26. class OStatusInitAction extends Action
  27. {
  28. var $nickname;
  29. var $tagger;
  30. var $peopletag;
  31. var $group;
  32. var $profile;
  33. var $err;
  34. protected function prepare(array $args=array())
  35. {
  36. parent::prepare($args);
  37. if (common_logged_in()) {
  38. // TRANS: Client error.
  39. $this->clientError(_m('You can use the local subscription!'));
  40. }
  41. // Local user or group the remote wants to subscribe to
  42. $this->nickname = $this->trimmed('nickname');
  43. $this->tagger = $this->trimmed('tagger');
  44. $this->peopletag = $this->trimmed('peopletag');
  45. $this->group = $this->trimmed('group');
  46. // Webfinger or profile URL of the remote user
  47. $this->profile = $this->trimmed('profile');
  48. return true;
  49. }
  50. protected function handle()
  51. {
  52. parent::handle();
  53. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  54. /* Use a session token for CSRF protection. */
  55. $token = $this->trimmed('token');
  56. if (!$token || $token != common_session_token()) {
  57. // TRANS: Client error displayed when the session token does not match or is not given.
  58. $this->showForm(_m('There was a problem with your session token. '.
  59. 'Try again, please.'));
  60. return;
  61. }
  62. $this->ostatusConnect();
  63. } else {
  64. $this->showForm();
  65. }
  66. }
  67. function showForm($err = null)
  68. {
  69. $this->err = $err;
  70. if ($this->boolean('ajax')) {
  71. $this->startHTML('text/xml;charset=utf-8');
  72. $this->elementStart('head');
  73. // TRANS: Form title.
  74. $this->element('title', null, _m('TITLE','Subscribe to user'));
  75. $this->elementEnd('head');
  76. $this->elementStart('body');
  77. $this->showContent();
  78. $this->elementEnd('body');
  79. $this->endHTML();
  80. } else {
  81. $this->showPage();
  82. }
  83. }
  84. function showContent()
  85. {
  86. if ($this->group) {
  87. // TRANS: Form legend. %s is a group name.
  88. $header = sprintf(_m('Join group %s'), $this->group);
  89. // TRANS: Button text to join a group.
  90. $submit = _m('BUTTON','Join');
  91. } else if ($this->peopletag && $this->tagger) {
  92. // TRANS: Form legend. %1$s is a list, %2$s is a lister's name.
  93. $header = sprintf(_m('Subscribe to list %1$s by %2$s'), $this->peopletag, $this->tagger);
  94. // TRANS: Button text to subscribe to a list.
  95. $submit = _m('BUTTON','Subscribe');
  96. // TRANS: Button text.
  97. } else {
  98. // TRANS: Form legend. %s is a nickname.
  99. $header = sprintf(_m('Subscribe to %s'), $this->nickname);
  100. // TRANS: Button text to subscribe to a profile.
  101. $submit = _m('BUTTON','Subscribe');
  102. }
  103. $this->elementStart('form', array('id' => 'form_ostatus_connect',
  104. 'method' => 'post',
  105. 'class' => 'form_settings',
  106. 'action' => common_local_url('ostatusinit')));
  107. $this->elementStart('fieldset');
  108. $this->element('legend', null, $header);
  109. $this->hidden('token', common_session_token());
  110. $this->elementStart('ul', 'form_data');
  111. $this->elementStart('li', array('id' => 'ostatus_nickname'));
  112. if ($this->group) {
  113. // TRANS: Field label.
  114. $this->input('group', _m('Group nickname'), $this->group,
  115. // TRANS: Field title.
  116. _m('Nickname of the group you want to join.'));
  117. } else {
  118. // TRANS: Field label.
  119. $this->input('nickname', _m('User nickname'), $this->nickname,
  120. // TRANS: Field title.
  121. _m('Nickname of the user you want to follow.'));
  122. $this->hidden('tagger', $this->tagger);
  123. $this->hidden('peopletag', $this->peopletag);
  124. }
  125. $this->elementEnd('li');
  126. $this->elementStart('li', array('id' => 'ostatus_profile'));
  127. // TRANS: Field label.
  128. $this->input('profile', _m('Profile Account'), $this->profile,
  129. // TRANS: Tooltip for field label "Profile Account".
  130. _m('Your account ID (e.g. user@example.com).'));
  131. $this->elementEnd('li');
  132. $this->elementEnd('ul');
  133. $this->submit('submit', $submit);
  134. $this->elementEnd('fieldset');
  135. $this->elementEnd('form');
  136. }
  137. function ostatusConnect()
  138. {
  139. $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
  140. if (Validate::uri($this->profile, $opts)) {
  141. $bits = parse_url($this->profile);
  142. if ($bits['scheme'] == 'acct') {
  143. $this->connectWebfinger($bits['path']);
  144. } else {
  145. $this->connectProfile($this->profile);
  146. }
  147. } elseif (strpos($this->profile, '@') !== false) {
  148. $this->connectWebfinger($this->profile);
  149. } else {
  150. // TRANS: Client error.
  151. $this->clientError(_m('Must provide a remote profile.'));
  152. }
  153. }
  154. function connectWebfinger($acct)
  155. {
  156. $target_profile = $this->targetProfile();
  157. $disco = new Discovery;
  158. $xrd = $disco->lookup($acct);
  159. $link = $xrd->get('http://ostatus.org/schema/1.0/subscribe');
  160. if (!is_null($link)) {
  161. // We found a URL - let's redirect!
  162. if (!empty($link->template)) {
  163. $url = Discovery::applyTemplate($link->template, $target_profile);
  164. } else {
  165. $url = $link->href;
  166. }
  167. common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
  168. common_redirect($url, 303);
  169. }
  170. // TRANS: Client error.
  171. $this->clientError(_m('Could not confirm remote profile address.'));
  172. }
  173. function connectProfile($subscriber_profile)
  174. {
  175. $target_profile = $this->targetProfile();
  176. // @fixme hack hack! We should look up the remote sub URL from XRDS
  177. $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
  178. $suburl .= '?profile=' . urlencode($target_profile);
  179. common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
  180. common_redirect($suburl, 303);
  181. }
  182. /**
  183. * Build the canonical profile URI+URL of the requested user or group
  184. */
  185. function targetProfile()
  186. {
  187. if ($this->nickname) {
  188. $user = User::getKV('nickname', $this->nickname);
  189. if ($user) {
  190. return common_local_url('userbyid', array('id' => $user->id));
  191. } else {
  192. // TRANS: Client error.
  193. $this->clientError(_m('No such user.'));
  194. }
  195. } else if ($this->group) {
  196. $group = Local_group::getKV('nickname', $this->group);
  197. if ($group instanceof Local_group) {
  198. return common_local_url('groupbyid', array('id' => $group->group_id));
  199. } else {
  200. // TRANS: Client error.
  201. $this->clientError(_m('No such group.'));
  202. }
  203. } else if ($this->peopletag && $this->tagger) {
  204. $user = User::getKV('nickname', $this->tagger);
  205. if (empty($user)) {
  206. // TRANS: Client error.
  207. $this->clientError(_m('No such user.'));
  208. }
  209. $peopletag = Profile_list::getByTaggerAndTag($user->id, $this->peopletag);
  210. if ($peopletag) {
  211. return common_local_url('profiletagbyid',
  212. array('tagger_id' => $user->id, 'id' => $peopletag->id));
  213. }
  214. // TRANS: Client error.
  215. $this->clientError(_m('No such list.'));
  216. } else {
  217. // TRANS: Client error.
  218. $this->clientError(_m('No local user or group nickname provided.'));
  219. }
  220. }
  221. function title()
  222. {
  223. // TRANS: Page title.
  224. return _m('OStatus Connect');
  225. }
  226. }