bitlyadminpanel.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * Admin panel for plugin to use bit.ly URL shortening services.
  18. *
  19. * @category Settings
  20. * @package GNUsocial
  21. * @author Brion Vibber <brion@status.net>
  22. * @copyright 2010 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Administer global bit.ly URL shortener settings
  28. *
  29. * @category Admin
  30. * @package GNUsocial
  31. * @author Brion Vibber <brion@status.net>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class BitlyadminpanelAction extends AdminPanelAction
  35. {
  36. /**
  37. * Returns the page title
  38. *
  39. * @return string page title
  40. */
  41. public function title()
  42. {
  43. // TRANS: Title of administration panel.
  44. return _m('bit.ly URL shortening');
  45. }
  46. /**
  47. * Instructions for using this form.
  48. *
  49. * @return string instructions
  50. */
  51. public function getInstructions()
  52. {
  53. // TRANS: Instructions for administration panel.
  54. // TRANS: This message contains Markdown links in the form [decsription](link).
  55. return _m('URL shortening with bit.ly requires ' .
  56. '[a bit.ly account and API key](http://bit.ly/a/your_api_key). ' .
  57. 'This verifies that this is an authorized account, and ' .
  58. 'allow you to use bit.ly\'s tracking features and custom domains.');
  59. }
  60. /**
  61. * Show the bit.ly admin panel form
  62. *
  63. * @return void
  64. */
  65. public function showForm()
  66. {
  67. $form = new BitlyAdminPanelForm($this);
  68. $form->show();
  69. return;
  70. }
  71. /**
  72. * Save settings from the form
  73. *
  74. * @return void
  75. */
  76. public function saveSettings()
  77. {
  78. static $settings = array(
  79. 'bitly' => array('default_login', 'default_apikey')
  80. );
  81. $values = array();
  82. foreach ($settings as $section => $parts) {
  83. foreach ($parts as $setting) {
  84. $values[$section][$setting]
  85. = $this->trimmed($setting);
  86. }
  87. }
  88. // This throws an exception on validation errors
  89. $this->validate($values);
  90. // assert(all values are valid);
  91. $config = new Config();
  92. $config->query('START TRANSACTION');
  93. foreach ($settings as $section => $parts) {
  94. foreach ($parts as $setting) {
  95. Config::save($section, $setting, $values[$section][$setting]);
  96. }
  97. }
  98. $config->query('COMMIT');
  99. return;
  100. }
  101. public function validate(&$values)
  102. {
  103. // Validate consumer key and secret (can't be too long)
  104. if (mb_strlen($values['bitly']['default_apikey']) > 255) {
  105. $this->clientError(
  106. // TRANS: Client error displayed when using too long a key.
  107. _m('Invalid login. Maximum length is 255 characters.')
  108. );
  109. }
  110. if (mb_strlen($values['bitly']['default_apikey']) > 255) {
  111. $this->clientError(
  112. // TRANS: Client error displayed when using too long a key.
  113. _m('Invalid API key. Maximum length is 255 characters.')
  114. );
  115. }
  116. }
  117. }
  118. class BitlyAdminPanelForm extends AdminForm
  119. {
  120. /**
  121. * ID of the form
  122. *
  123. * @return int ID of the form
  124. */
  125. public function id()
  126. {
  127. return 'bitlyadminpanel';
  128. }
  129. /**
  130. * class of the form
  131. *
  132. * @return string class of the form
  133. */
  134. public function formClass()
  135. {
  136. return 'form_settings';
  137. }
  138. /**
  139. * Action of the form
  140. *
  141. * @return string URL of the action
  142. */
  143. public function action()
  144. {
  145. return common_local_url('bitlyadminpanel');
  146. }
  147. /**
  148. * Data elements of the form
  149. *
  150. * @return void
  151. */
  152. public function formData()
  153. {
  154. $this->out->elementStart(
  155. 'fieldset',
  156. array('id' => 'settings_bitly')
  157. );
  158. // TRANS: Fieldset legend in administration panel for bit.ly username and API key.
  159. $this->out->element('legend', null, _m('LEGEND', 'Credentials'));
  160. // Do we have global defaults to fall back on?
  161. $login = $apiKey = false;
  162. Event::handle('BitlyDefaultCredentials', array(&$login, &$apiKey));
  163. $haveGlobalDefaults = ($login && $apiKey);
  164. if ($login && $apiKey) {
  165. $this->out->element(
  166. 'p',
  167. 'form_guide',
  168. // TRANS: Form guide in administration panel for bit.ly URL shortening.
  169. _m('Leave these empty to use global default credentials.')
  170. );
  171. } else {
  172. $this->out->element(
  173. 'p',
  174. 'form_guide',
  175. // TRANS: Form guide in administration panel for bit.ly URL shortening.
  176. _m('If you leave these empty, bit.ly will be unavailable to users.')
  177. );
  178. }
  179. $this->out->elementStart('ul', 'form_data');
  180. $this->li();
  181. $this->input(
  182. 'default_login',
  183. // TRANS: Field label in administration panel for bit.ly URL shortening.
  184. _m('Login name'),
  185. null,
  186. 'bitly'
  187. );
  188. $this->unli();
  189. $this->li();
  190. $this->input(
  191. 'default_apikey',
  192. // TRANS: Field label in administration panel for bit.ly URL shortening.
  193. _m('API key'),
  194. null,
  195. 'bitly'
  196. );
  197. $this->unli();
  198. $this->out->elementEnd('ul');
  199. $this->out->elementEnd('fieldset');
  200. }
  201. /**
  202. * Action elements
  203. *
  204. * @return void
  205. */
  206. public function formActions()
  207. {
  208. $this->out->submit(
  209. 'submit',
  210. // TRANS: Button text to save setting in administration panel for bit.ly URL shortening.
  211. _m('BUTTON', 'Save'),
  212. 'submit',
  213. null,
  214. // TRANS: Button title to save setting in administration panel for bit.ly URL shortening.
  215. _m('Save bit.ly settings')
  216. );
  217. }
  218. }