licenseadminpanel.php 9.6 KB

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