facebooksettings.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Edit user settings for Facebook
  34. *
  35. * @category Settings
  36. * @package StatusNet
  37. * @author Zach Copley <zach@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://status.net/
  40. *
  41. * @see SettingsAction
  42. */
  43. class FacebooksettingsAction extends SettingsAction {
  44. private $facebook; // Facebook PHP-SDK client obj
  45. private $flink;
  46. private $user;
  47. /**
  48. * For initializing members of the class.
  49. *
  50. * @param array $argarray misc. arguments
  51. *
  52. * @return boolean true
  53. */
  54. function prepare($args) {
  55. parent::prepare($args);
  56. $this->facebook = new Facebook(
  57. array(
  58. 'appId' => common_config('facebook', 'appid'),
  59. 'secret' => common_config('facebook', 'secret'),
  60. 'cookie' => true,
  61. )
  62. );
  63. $this->user = common_current_user();
  64. $this->flink = Foreign_link::getByUserID(
  65. $this->user->id,
  66. FACEBOOK_SERVICE
  67. );
  68. return true;
  69. }
  70. /*
  71. * Check the sessions token and dispatch
  72. */
  73. function handlePost($args) {
  74. // CSRF protection
  75. $token = $this->trimmed('token');
  76. if (!$token || $token != common_session_token()) {
  77. $this->showForm(
  78. // TRANS: Client error displayed when the session token does not match or is not given.
  79. _m('There was a problem with your session token. Try again, please.')
  80. );
  81. return;
  82. }
  83. if ($this->arg('save')) {
  84. $this->saveSettings();
  85. } else if ($this->arg('disconnect')) {
  86. $this->disconnect();
  87. }
  88. }
  89. /**
  90. * Returns the page title
  91. *
  92. * @return string page title
  93. */
  94. function title() {
  95. // TRANS: Page title for Facebook settings.
  96. return _m('TITLE','Facebook settings');
  97. }
  98. /**
  99. * Instructions for use
  100. *
  101. * @return instructions for use
  102. */
  103. function getInstructions() {
  104. // TRANS: Instructions for Facebook settings.
  105. return _m('Facebook settings');
  106. }
  107. /*
  108. * Show the settings form if he/she has a link to Facebook
  109. *
  110. * @return void
  111. */
  112. function showContent() {
  113. if (!empty($this->flink)) {
  114. $this->elementStart(
  115. 'form',
  116. array(
  117. 'method' => 'post',
  118. 'id' => 'form_settings_facebook',
  119. 'class' => 'form_settings',
  120. 'action' => common_local_url('facebooksettings')
  121. )
  122. );
  123. $this->hidden('token', common_session_token());
  124. // TRANS: Form note. User is connected to facebook.
  125. $this->element('p', 'form_note', _m('Connected Facebook user'));
  126. $this->elementStart('p', array('class' => 'facebook-user-display'));
  127. $this->element(
  128. 'fb:profile-pic',
  129. array(
  130. 'uid' => $this->flink->foreign_id,
  131. 'size' => 'small',
  132. 'linked' => 'true',
  133. 'facebook-logo' => 'true'
  134. )
  135. );
  136. $this->element(
  137. 'fb:name',
  138. array('uid' => $this->flink->foreign_id, 'useyou' => 'false')
  139. );
  140. $this->elementEnd('p');
  141. $this->elementStart('ul', 'form_data');
  142. $this->elementStart('li');
  143. $this->checkbox(
  144. 'noticesync',
  145. // TRANS: Checkbox label in Facebook settings.
  146. _m('Publish my notices to Facebook.'),
  147. ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND) : true
  148. );
  149. $this->elementEnd('li');
  150. $this->elementStart('li');
  151. $this->checkbox(
  152. 'replysync',
  153. // TRANS: Checkbox label in Facebook settings.
  154. _m('Send "@" replies to Facebook.'),
  155. ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true
  156. );
  157. $this->elementEnd('li');
  158. $this->elementStart('li');
  159. // TRANS: Submit button to save synchronisation settings.
  160. $this->submit('save', _m('BUTTON', 'Save'));
  161. $this->elementEnd('li');
  162. $this->elementEnd('ul');
  163. $this->elementStart('fieldset');
  164. // TRANS: Fieldset legend for form to disconnect from Facebook.
  165. $this->element('legend', null, _m('Disconnect my account from Facebook'));
  166. if (empty($this->user->password)) {
  167. $this->elementStart('p', array('class' => 'form_guide'));
  168. $msg = sprintf(
  169. // TRANS: Notice in disconnect from Facebook form if user has no local StatusNet password.
  170. _m('Disconnecting your Faceboook would make it impossible to '.
  171. 'log in! Please [set a password](%s) first.'),
  172. common_local_url('passwordsettings')
  173. );
  174. $this->raw(common_markup_to_html($msg));
  175. $this->elementEnd('p');
  176. } else {
  177. // @todo FIXME: i18n: This message is not being used.
  178. // TRANS: Message displayed when initiating disconnect of a StatusNet user
  179. // TRANS: from a Facebook account. %1$s is the StatusNet site name.
  180. $msg = sprintf(_m('Keep your %1$s account but disconnect from Facebook. ' .
  181. 'You\'ll use your %1$s password to log in.'),
  182. common_config('site', 'name')
  183. );
  184. // TRANS: Submit button.
  185. $this->submit('disconnect', _m('BUTTON', 'Disconnect'));
  186. }
  187. $this->elementEnd('fieldset');
  188. $this->elementEnd('form');
  189. }
  190. }
  191. /*
  192. * Save the user's Facebook settings
  193. *
  194. * @return void
  195. */
  196. function saveSettings() {
  197. $noticesync = $this->boolean('noticesync');
  198. $replysync = $this->boolean('replysync');
  199. $original = clone($this->flink);
  200. $this->flink->set_flags($noticesync, false, $replysync, false);
  201. $result = $this->flink->update($original);
  202. if ($result === false) {
  203. // TRANS: Notice in case saving of synchronisation preferences fail.
  204. $this->showForm(_m('There was a problem saving your sync preferences.'));
  205. } else {
  206. // TRANS: Confirmation that synchronisation settings have been saved into the system.
  207. $this->showForm(_m('Sync preferences saved.'), true);
  208. }
  209. }
  210. /*
  211. * Disconnect the user's Facebook account - deletes the Foreign_link
  212. * and shows the user a success message if all goes well.
  213. */
  214. function disconnect() {
  215. $result = $this->flink->delete();
  216. $this->flink = null;
  217. if ($result === false) {
  218. common_log_db_error($user, 'DELETE', __FILE__);
  219. // TRANS: Server error displayed when deleting the link to a Facebook account fails.
  220. $this->serverError(_m('Could not delete link to Facebook.'));
  221. }
  222. // TRANS: Confirmation message. StatusNet account was unlinked from Facebook.
  223. $this->showForm(_m('You have disconnected from Facebook.'), true);
  224. }
  225. }