twitteradminpanel.php 9.0 KB

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