ostatusinit.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. $validate = new Validate();
  140. $opts = array('allowed_schemes' => array('http', 'https', 'acct'));
  141. if ($validate->uri($this->profile, $opts)) {
  142. $bits = parse_url($this->profile);
  143. if ($bits['scheme'] == 'acct') {
  144. $this->connectWebfinger($bits['path']);
  145. } else {
  146. $this->connectProfile($this->profile);
  147. }
  148. } elseif (strpos($this->profile, '@') !== false) {
  149. $this->connectWebfinger($this->profile);
  150. } else {
  151. // TRANS: Client error.
  152. $this->clientError(_m('Must provide a remote profile.'));
  153. }
  154. }
  155. function connectWebfinger($acct)
  156. {
  157. $target_profile = $this->targetProfile();
  158. $disco = new Discovery;
  159. $xrd = $disco->lookup($acct);
  160. $link = $xrd->get('http://ostatus.org/schema/1.0/subscribe');
  161. if (!is_null($link)) {
  162. // We found a URL - let's redirect!
  163. if (!empty($link->template)) {
  164. $url = Discovery::applyTemplate($link->template, $target_profile);
  165. } else {
  166. $url = $link->href;
  167. }
  168. common_log(LOG_INFO, "Sending remote subscriber $acct to $url");
  169. common_redirect($url, 303);
  170. }
  171. // TRANS: Client error.
  172. $this->clientError(_m('Could not confirm remote profile address.'));
  173. }
  174. function connectProfile($subscriber_profile)
  175. {
  176. $target_profile = $this->targetProfile();
  177. // @fixme hack hack! We should look up the remote sub URL from XRDS
  178. $suburl = preg_replace('!^(.*)/(.*?)$!', '$1/main/ostatussub', $subscriber_profile);
  179. $suburl .= '?profile=' . urlencode($target_profile);
  180. common_log(LOG_INFO, "Sending remote subscriber $subscriber_profile to $suburl");
  181. common_redirect($suburl, 303);
  182. }
  183. /**
  184. * Build the canonical profile URI+URL of the requested user or group
  185. */
  186. function targetProfile()
  187. {
  188. if ($this->nickname) {
  189. $user = User::getKV('nickname', $this->nickname);
  190. if ($user) {
  191. return common_local_url('userbyid', array('id' => $user->id));
  192. } else {
  193. // TRANS: Client error.
  194. $this->clientError(_m('No such user.'));
  195. }
  196. } else if ($this->group) {
  197. $group = Local_group::getKV('nickname', $this->group);
  198. if ($group instanceof Local_group) {
  199. return common_local_url('groupbyid', array('id' => $group->group_id));
  200. } else {
  201. // TRANS: Client error.
  202. $this->clientError(_m('No such group.'));
  203. }
  204. } else if ($this->peopletag && $this->tagger) {
  205. $user = User::getKV('nickname', $this->tagger);
  206. if (empty($user)) {
  207. // TRANS: Client error.
  208. $this->clientError(_m('No such user.'));
  209. }
  210. $peopletag = Profile_list::getByTaggerAndTag($user->id, $this->peopletag);
  211. if ($peopletag) {
  212. return common_local_url('profiletagbyid',
  213. array('tagger_id' => $user->id, 'id' => $peopletag->id));
  214. }
  215. // TRANS: Client error.
  216. $this->clientError(_m('No such list.'));
  217. } else {
  218. // TRANS: Client error.
  219. $this->clientError(_m('No local user or group nickname provided.'));
  220. }
  221. }
  222. function title()
  223. {
  224. // TRANS: Page title.
  225. return _m('OStatus Connect');
  226. }
  227. }