facebooksettings.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Edit user settings for Facebook
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Settings
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2010 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /**
  31. * Edit user settings for Facebook
  32. *
  33. * @category Settings
  34. * @package StatusNet
  35. * @author Zach Copley <zach@status.net>
  36. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  37. * @link http://status.net/
  38. *
  39. * @see SettingsAction
  40. */
  41. class FacebooksettingsAction extends SettingsAction {
  42. private $facebook; // Facebook PHP-SDK client obj
  43. protected $flink;
  44. protected function doPreparation()
  45. {
  46. $this->facebook = new Facebook(
  47. array(
  48. 'appId' => common_config('facebook', 'appid'),
  49. 'secret' => common_config('facebook', 'secret'),
  50. 'cookie' => true,
  51. )
  52. );
  53. $this->flink = Foreign_link::getByUserID(
  54. $this->scoped->getID(),
  55. FACEBOOK_SERVICE
  56. );
  57. }
  58. protected function doPost()
  59. {
  60. if ($this->arg('save')) {
  61. return $this->saveSettings();
  62. } else if ($this->arg('disconnect')) {
  63. return $this->disconnect();
  64. }
  65. throw new ClientException(_('No action to take on POST.'));
  66. }
  67. /**
  68. * Returns the page title
  69. *
  70. * @return string page title
  71. */
  72. function title() {
  73. // TRANS: Page title for Facebook settings.
  74. return _m('TITLE','Facebook settings');
  75. }
  76. /**
  77. * Instructions for use
  78. *
  79. * @return instructions for use
  80. */
  81. function getInstructions() {
  82. // TRANS: Instructions for Facebook settings.
  83. return _m('Facebook settings');
  84. }
  85. /*
  86. * Show the settings form if he/she has a link to Facebook
  87. *
  88. * @return void
  89. */
  90. function showContent() {
  91. if (!$this->flink instanceof Foreign_link) {
  92. throw new ServerException(_m('You have not linked this account to Facebook.'));
  93. }
  94. $this->elementStart(
  95. 'form',
  96. array(
  97. 'method' => 'post',
  98. 'id' => 'form_settings_facebook',
  99. 'class' => 'form_settings',
  100. 'action' => common_local_url('facebooksettings')
  101. )
  102. );
  103. $this->hidden('token', common_session_token());
  104. // TRANS: Form note. User is connected to facebook.
  105. $this->element('p', 'form_note', _m('Connected Facebook user'));
  106. $this->elementStart('p', array('class' => 'facebook-user-display'));
  107. $this->element(
  108. 'fb:profile-pic',
  109. array(
  110. 'uid' => $this->flink->foreign_id,
  111. 'size' => 'small',
  112. 'linked' => 'true',
  113. 'facebook-logo' => 'true'
  114. )
  115. );
  116. $this->element(
  117. 'fb:name',
  118. array('uid' => $this->flink->foreign_id, 'useyou' => 'false')
  119. );
  120. $this->elementEnd('p');
  121. $this->elementStart('ul', 'form_data');
  122. $this->elementStart('li');
  123. $this->checkbox(
  124. 'noticesync',
  125. // TRANS: Checkbox label in Facebook settings.
  126. _m('Publish my notices to Facebook.'),
  127. $this->flink->noticesync & FOREIGN_NOTICE_SEND
  128. );
  129. $this->elementEnd('li');
  130. $this->elementStart('li');
  131. $this->checkbox(
  132. 'replysync',
  133. // TRANS: Checkbox label in Facebook settings.
  134. _m('Send "@" replies to Facebook.'),
  135. $this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY
  136. );
  137. $this->elementEnd('li');
  138. $this->elementStart('li');
  139. // TRANS: Submit button to save synchronisation settings.
  140. $this->submit('save', _m('BUTTON', 'Save'));
  141. $this->elementEnd('li');
  142. $this->elementEnd('ul');
  143. $this->elementStart('fieldset');
  144. // TRANS: Fieldset legend for form to disconnect from Facebook.
  145. $this->element('legend', null, _m('Disconnect my account from Facebook'));
  146. if (!$this->scoped->hasPassword()) {
  147. $this->elementStart('p', array('class' => 'form_guide'));
  148. $msg = sprintf(
  149. // TRANS: Notice in disconnect from Facebook form if user has no local StatusNet password.
  150. _m('Disconnecting your Faceboook would make it impossible to '.
  151. 'log in! Please [set a password](%s) first.'),
  152. common_local_url('passwordsettings')
  153. );
  154. $this->raw(common_markup_to_html($msg));
  155. $this->elementEnd('p');
  156. } else {
  157. // @todo FIXME: i18n: This message is not being used.
  158. // TRANS: Message displayed when initiating disconnect of a StatusNet user
  159. // TRANS: from a Facebook account. %1$s is the StatusNet site name.
  160. $msg = sprintf(_m('Keep your %1$s account but disconnect from Facebook. ' .
  161. 'You\'ll use your %1$s password to log in.'),
  162. common_config('site', 'name')
  163. );
  164. // TRANS: Submit button.
  165. $this->submit('disconnect', _m('BUTTON', 'Disconnect'));
  166. }
  167. $this->elementEnd('fieldset');
  168. $this->elementEnd('form');
  169. }
  170. /*
  171. * Save the user's Facebook settings
  172. *
  173. * @return void
  174. */
  175. function saveSettings() {
  176. $noticesync = $this->boolean('noticesync');
  177. $replysync = $this->boolean('replysync');
  178. $original = clone($this->flink);
  179. $this->flink->set_flags($noticesync, false, $replysync, false);
  180. $result = $this->flink->update($original);
  181. if ($result === false) {
  182. // TRANS: Notice in case saving of synchronisation preferences fail.
  183. throw new ServerException(_m('There was a problem saving your sync preferences.'));
  184. }
  185. // TRANS: Confirmation that synchronisation settings have been saved into the system.
  186. return _m('Sync preferences saved.');
  187. }
  188. /*
  189. * Disconnect the user's Facebook account - deletes the Foreign_link
  190. * and shows the user a success message if all goes well.
  191. */
  192. function disconnect() {
  193. $result = $this->flink->delete();
  194. $this->flink = null;
  195. if ($result === false) {
  196. common_log_db_error($this->flink, 'DELETE', __FILE__);
  197. // TRANS: Server error displayed when deleting the link to a Facebook account fails.
  198. throw new ServerException(_m('Could not delete link to Facebook.'));
  199. }
  200. // TRANS: Confirmation message. GNU social account was unlinked from Facebook.
  201. return _m('You have disconnected this account from Facebook.');
  202. }
  203. }