twitteradminpanel.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Twitter bridge administration panel
  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 GNUsocial
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2010 StatusNet, Inc.
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. */
  28. defined('GNUSOCIAL') || die();
  29. /**
  30. * Administer global Twitter bridge settings
  31. *
  32. * @category Admin
  33. * @package GNUsocial
  34. * @author Zach Copley <zach@status.net>
  35. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  36. */
  37. class TwitteradminpanelAction extends AdminPanelAction
  38. {
  39. /**
  40. * Returns the page title
  41. *
  42. * @return string page title
  43. */
  44. public function title()
  45. {
  46. // TRANS: Page title for Twitter administration panel.
  47. return _m('TITLE', 'Twitter');
  48. }
  49. /**
  50. * Instructions for using this form.
  51. *
  52. * @return string instructions
  53. */
  54. public function getInstructions()
  55. {
  56. // TRANS: Instructions for Twitter bridge administration page.
  57. return _m('Twitter bridge settings');
  58. }
  59. /**
  60. * Show the Twitter admin panel form
  61. *
  62. * @return void
  63. */
  64. public function showForm()
  65. {
  66. $form = new TwitterAdminPanelForm($this);
  67. $form->show();
  68. return;
  69. }
  70. /**
  71. * Save settings from the form
  72. *
  73. * @return void
  74. */
  75. public function saveSettings()
  76. {
  77. static $settings = array(
  78. 'twitter' => array('consumer_key', 'consumer_secret'),
  79. 'integration' => array('source')
  80. );
  81. static $booleans = array(
  82. 'twitter' => array('signin')
  83. );
  84. if (Event::handle('TwitterBridgeAdminImportControl')) {
  85. $booleans['twitterimport'] = array('enabled');
  86. }
  87. $values = array();
  88. foreach ($settings as $section => $parts) {
  89. foreach ($parts as $setting) {
  90. $values[$section][$setting]
  91. = $this->trimmed($setting);
  92. }
  93. }
  94. foreach ($booleans as $section => $parts) {
  95. foreach ($parts as $setting) {
  96. $values[$section][$setting]
  97. = ($this->boolean($setting)) ? 1 : 0;
  98. }
  99. }
  100. // This throws an exception on validation errors
  101. $this->validate($values);
  102. // assert(all values are valid);
  103. $config = new Config();
  104. $config->query('START TRANSACTION');
  105. foreach ($settings as $section => $parts) {
  106. foreach ($parts as $setting) {
  107. Config::save($section, $setting, $values[$section][$setting]);
  108. }
  109. }
  110. foreach ($booleans as $section => $parts) {
  111. foreach ($parts as $setting) {
  112. Config::save($section, $setting, $values[$section][$setting]);
  113. }
  114. }
  115. $config->query('COMMIT');
  116. // Flush the router cache: we may have enabled/disabled bridging,
  117. // which will add or remove some actions.
  118. $cache = Cache::instance();
  119. $cache->delete(Router::cacheKey());
  120. return;
  121. }
  122. public function validate(&$values)
  123. {
  124. // Validate consumer key and secret (can't be too long)
  125. if (mb_strlen($values['twitter']['consumer_key']) > 255) {
  126. $this->clientError(
  127. // TRANS: Client error displayed when a consumer key is invalid because it is too long.
  128. _m('Invalid consumer key. Maximum length is 255 characters.')
  129. );
  130. }
  131. if (mb_strlen($values['twitter']['consumer_secret']) > 255) {
  132. $this->clientError(
  133. // TRANS: Client error displayed when a consumer secret is invalid because it is too long.
  134. _m('Invalid consumer secret. Maximum length is 255 characters.')
  135. );
  136. }
  137. }
  138. public function isImportEnabled()
  139. {
  140. // Since daemon setup isn't automated yet...
  141. // @todo: if merged into main queues, detect presence of daemon config
  142. return true;
  143. }
  144. }
  145. class TwitterAdminPanelForm extends AdminForm
  146. {
  147. /**
  148. * ID of the form
  149. *
  150. * @return int ID of the form
  151. */
  152. public function id()
  153. {
  154. return 'twitteradminpanel';
  155. }
  156. /**
  157. * class of the form
  158. *
  159. * @return string class of the form
  160. */
  161. public function formClass()
  162. {
  163. return 'form_settings';
  164. }
  165. /**
  166. * Action of the form
  167. *
  168. * @return string URL of the action
  169. */
  170. public function action()
  171. {
  172. return common_local_url('twitteradminpanel');
  173. }
  174. /**
  175. * Data elements of the form
  176. *
  177. * @return void
  178. */
  179. public function formData()
  180. {
  181. $this->out->elementStart(
  182. 'fieldset',
  183. array('id' => 'settings_twitter-application')
  184. );
  185. // TRANS: Fieldset legend for Twitter application settings.
  186. $this->out->element('legend', null, _m('Twitter application settings'));
  187. $this->out->elementStart('ul', 'form_data');
  188. $this->li();
  189. $this->input(
  190. 'consumer_key',
  191. // TRANS: Field label for Twitter assigned consumer key.
  192. _m('Consumer key'),
  193. // TRANS: Field title for Twitter assigned consumer key.
  194. _m('The consumer key assigned by Twitter.'),
  195. 'twitter'
  196. );
  197. $this->unli();
  198. $this->li();
  199. $this->input(
  200. 'consumer_secret',
  201. // TRANS: Field label for Twitter assigned consumer secret.
  202. _m('Consumer secret'),
  203. // TRANS: Field title for Twitter assigned consumer secret.
  204. _m('The consumer secret assigned by Twitter.'),
  205. 'twitter'
  206. );
  207. $this->unli();
  208. $globalConsumerKey = common_config('twitter', 'global_consumer_key');
  209. $globalConsumerSec = common_config('twitter', 'global_consumer_secret');
  210. if (!empty($globalConsumerKey) && !empty($globalConsumerSec)) {
  211. $this->li();
  212. // TRANS: Form guide displayed when two required fields have already been provided.
  213. $this->out->element('p', 'form_guide', _m('Note: A global consumer key and secret are set.'));
  214. $this->unli();
  215. }
  216. $this->li();
  217. $this->input(
  218. 'source',
  219. // TRANS: Field label for Twitter application name.
  220. _m('Integration source'),
  221. // TRANS: Field title for Twitter application name.
  222. _m('The name of your Twitter application.'),
  223. 'integration'
  224. );
  225. $this->unli();
  226. $this->out->elementEnd('ul');
  227. $this->out->elementEnd('fieldset');
  228. $this->out->elementStart(
  229. 'fieldset',
  230. array('id' => 'settings_twitter-options')
  231. );
  232. // TRANS: Fieldset legend for Twitter integration options.
  233. $this->out->element('legend', null, _m('Options'));
  234. $this->out->elementStart('ul', 'form_data');
  235. $this->li();
  236. $this->out->checkbox(
  237. // TRANS: Checkbox label for global setting.
  238. 'signin',
  239. _m('Enable "Sign-in with Twitter"'),
  240. (bool) $this->value('signin', 'twitter'),
  241. // TRANS: Checkbox title.
  242. _m('This allow users to login with their Twitter credentials.')
  243. );
  244. $this->unli();
  245. if (Event::handle('TwitterBridgeAdminImportControl')) {
  246. $this->li();
  247. $this->out->checkbox(
  248. // TRANS: Checkbox label for global setting.
  249. 'enabled',
  250. _m('Enable Twitter import'),
  251. (bool) $this->value('enabled', 'twitterimport'),
  252. // TRANS: Checkbox title for global setting.
  253. _m('Allow users to import their Twitter friends\' timelines. Requires daemons to be manually configured.')
  254. );
  255. $this->unli();
  256. }
  257. $this->out->elementEnd('ul');
  258. $this->out->elementEnd('fieldset');
  259. }
  260. /**
  261. * Action elements
  262. *
  263. * @return void
  264. */
  265. public function formActions()
  266. {
  267. // TRANS: Button text for saving the administrative Twitter bridge settings.
  268. $this->out->submit(
  269. 'submit',
  270. _m('BUTTON', 'Save'),
  271. 'submit',
  272. null,
  273. // TRANS: Button title for saving the administrative Twitter bridge settings.
  274. _m('Save the Twitter bridge settings.')
  275. );
  276. }
  277. }