licenseadminpanel.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * License 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. * License 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 LicenseadminpanelAction extends AdminPanelAction
  42. {
  43. /**
  44. * Returns the page title
  45. *
  46. * @return string page title
  47. */
  48. function title()
  49. {
  50. // TRANS: User admin panel title
  51. return _m('TITLE', 'License');
  52. }
  53. /**
  54. * Instructions for using this form.
  55. *
  56. * @return string instructions
  57. */
  58. function getInstructions()
  59. {
  60. // TRANS: Form instructions for the site license admin panel.
  61. return _('License for this StatusNet site');
  62. }
  63. /**
  64. * Show the site admin panel form
  65. *
  66. * @return void
  67. */
  68. function showForm()
  69. {
  70. $form = new LicenseAdminPanelForm($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. 'license' => array('type', 'owner', 'url', 'title', 'image')
  83. );
  84. $values = array();
  85. foreach ($settings as $section => $parts) {
  86. foreach ($parts as $setting) {
  87. $values[$section][$setting] = $this->trimmed($setting);
  88. }
  89. }
  90. // This throws an exception on validation errors
  91. $this->validate($values);
  92. // assert(all values are valid);
  93. $config = new Config();
  94. $config->query('BEGIN');
  95. foreach ($settings as $section => $parts) {
  96. foreach ($parts as $setting) {
  97. Config::save($section, $setting, $values[$section][$setting]);
  98. }
  99. }
  100. $config->query('COMMIT');
  101. return;
  102. }
  103. /**
  104. * Validate License admin form values
  105. *
  106. * @param array &$values from the form
  107. *
  108. * @return nothing
  109. */
  110. function validate(&$values)
  111. {
  112. // Validate license type (shouldn't have to do it, but just in case)
  113. $types = array('private', 'allrightsreserved', 'cc');
  114. if (!in_array($values['license']['type'], $types)) {
  115. // TRANS: Client error displayed selecting an invalid license in the license admin panel.
  116. $this->clientError(_('Invalid license selection.'));
  117. }
  118. // Make sure the user has set an owner if the site has a private
  119. // license
  120. if ($values['license']['type'] == 'allrightsreserved'
  121. && empty($values['license']['owner'])
  122. ) {
  123. $this->clientError(
  124. // TRANS: Client error displayed when not specifying an owner for the all rights reserved license in the license admin panel.
  125. _('You must specify the owner of the content when using the All Rights Reserved license.')
  126. );
  127. }
  128. // Make sure the license title is not too long
  129. if (mb_strlen($values['license']['type']) > 255) {
  130. $this->clientError(
  131. // TRANS: Client error displayed selecting a too long license title in the license admin panel.
  132. _('Invalid license title. Maximum length is 255 characters.')
  133. );
  134. }
  135. // URLs should be set for cc license
  136. if ($values['license']['type'] == 'cc') {
  137. if (!common_valid_http_url($values['license']['url'])) {
  138. // TRANS: Client error displayed specifying an invalid license URL in the license admin panel.
  139. $this->clientError(_('Invalid license URL.'));
  140. }
  141. if (!common_valid_http_url($values['license']['image'])) {
  142. // TRANS: Client error displayed specifying an invalid license image URL in the license admin panel.
  143. $this->clientError(_('Invalid license image URL.'));
  144. }
  145. }
  146. // can be either blank or a valid URL for private & allrightsreserved
  147. if (!empty($values['license']['url'])) {
  148. if (!common_valid_http_url($values['license']['url'])) {
  149. // TRANS: Client error displayed specifying an invalid license URL in the license admin panel.
  150. $this->clientError(_('License URL must be blank or a valid URL.'));
  151. }
  152. }
  153. // can be either blank or a valid URL for private & allrightsreserved
  154. if (!empty($values['license']['image'])) {
  155. if (!common_valid_http_url($values['license']['image'])) {
  156. // TRANS: Client error displayed specifying an invalid license image URL in the license admin panel.
  157. $this->clientError(_('License image must be blank or valid URL.'));
  158. }
  159. }
  160. }
  161. }
  162. class LicenseAdminPanelForm extends AdminForm
  163. {
  164. /**
  165. * ID of the form
  166. *
  167. * @return int ID of the form
  168. */
  169. function id()
  170. {
  171. return 'licenseadminpanel';
  172. }
  173. /**
  174. * class of the form
  175. *
  176. * @return string class of the form
  177. */
  178. function formClass()
  179. {
  180. return 'form_settings';
  181. }
  182. /**
  183. * Action of the form
  184. *
  185. * @return string URL of the action
  186. */
  187. function action()
  188. {
  189. return common_local_url('licenseadminpanel');
  190. }
  191. /**
  192. * Data elements of the form
  193. *
  194. * @return void
  195. */
  196. function formData()
  197. {
  198. $this->out->elementStart(
  199. 'fieldset', array('id' => 'settings_license-selection')
  200. );
  201. // TRANS: Form legend in the license admin panel.
  202. $this->out->element('legend', null, _('License selection'));
  203. $this->out->elementStart('ul', 'form_data');
  204. $this->li();
  205. $types = array(
  206. // TRANS: License option in the license admin panel.
  207. 'private' => _('Private'),
  208. // TRANS: License option in the license admin panel.
  209. 'allrightsreserved' => _('All Rights Reserved'),
  210. // TRANS: License option in the license admin panel.
  211. 'cc' => _('Creative Commons')
  212. );
  213. $this->out->dropdown(
  214. 'type',
  215. // TRANS: Dropdown field label in the license admin panel.
  216. _('Type'),
  217. $types,
  218. // TRANS: Dropdown field instructions in the license admin panel.
  219. _('Select a license.'),
  220. false,
  221. $this->value('type', 'license')
  222. );
  223. $this->unli();
  224. $this->out->elementEnd('ul');
  225. $this->out->elementEnd('fieldset');
  226. $this->out->elementStart(
  227. 'fieldset',
  228. array('id' => 'settings_license-details')
  229. );
  230. // TRANS: Form legend in the license admin panel.
  231. $this->out->element('legend', null, _('License details'));
  232. $this->out->elementStart('ul', 'form_data');
  233. $this->li();
  234. $this->input(
  235. 'owner',
  236. // TRANS: Field label in the license admin panel.
  237. _('Owner'),
  238. // TRANS: Field title in the license admin panel.
  239. _('Name of the owner of the site\'s content (if applicable).'),
  240. 'license'
  241. );
  242. $this->unli();
  243. $this->li();
  244. $this->input(
  245. 'title',
  246. // TRANS: Field label in the license admin panel.
  247. _('License Title'),
  248. // TRANS: Field title in the license admin panel.
  249. _('The title of the license.'),
  250. 'license'
  251. );
  252. $this->unli();
  253. $this->li();
  254. $this->input(
  255. 'url',
  256. // TRANS: Field label in the license admin panel.
  257. _('License URL'),
  258. // TRANS: Field title in the license admin panel.
  259. _('URL for more information about the license.'),
  260. 'license'
  261. );
  262. $this->unli();
  263. $this->li();
  264. $this->input(
  265. // TRANS: Field label in the license admin panel.
  266. 'image', _('License Image URL'),
  267. // TRANS: Field title in the license admin panel.
  268. _('URL for an image to display with the license.'),
  269. 'license'
  270. );
  271. $this->unli();
  272. $this->out->elementEnd('ul');
  273. $this->out->elementEnd('fieldset');
  274. }
  275. /**
  276. * Action elements
  277. *
  278. * @return void
  279. */
  280. function formActions()
  281. {
  282. $this->out->submit(
  283. 'submit',
  284. // TRANS: Button text in the license admin panel.
  285. _m('BUTTON','Save'),
  286. 'submit',
  287. null,
  288. // TRANS: Button title in the license admin panel.
  289. _('Save license settings.')
  290. );
  291. }
  292. }