bitlyadminpanel.php 6.8 KB

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