siteadminpanel.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. * Site administration panel
  18. *
  19. * @category Settings
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Zach Copley <zach@status.net>
  23. * @author Sarven Capadisli <csarven@status.net>
  24. * @copyright 2008-2011 StatusNet, Inc.
  25. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  26. */
  27. defined('GNUSOCIAL') || die();
  28. /**
  29. * Administer site settings
  30. *
  31. * @category Admin
  32. * @package GNUsocial
  33. * @author Evan Prodromou <evan@status.net>
  34. * @author Zach Copley <zach@status.net>
  35. * @author Sarven Capadisli <csarven@status.net>
  36. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  37. */
  38. class SiteadminpanelAction extends AdminPanelAction
  39. {
  40. /**
  41. * Returns the page title
  42. *
  43. * @return string page title
  44. */
  45. public function title()
  46. {
  47. // TRANS: Title for site administration panel.
  48. return _m('TITLE', 'Site');
  49. }
  50. /**
  51. * Instructions for using this form.
  52. *
  53. * @return string instructions
  54. */
  55. public function getInstructions()
  56. {
  57. // TRANS: Instructions for site administration panel.
  58. return _m('Basic settings for this StatusNet site');
  59. }
  60. /**
  61. * Show the site admin panel form
  62. *
  63. * @return void
  64. */
  65. public function showForm()
  66. {
  67. $form = new SiteAdminPanelForm($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. 'site' => array(
  80. 'name',
  81. 'broughtby',
  82. 'broughtbyurl',
  83. 'email',
  84. 'timezone',
  85. 'language',
  86. 'site',
  87. 'textlimit',
  88. 'dupelimit',
  89. 'logo',
  90. 'ssllogo'
  91. )
  92. );
  93. $values = [];
  94. foreach ($settings as $section => $parts) {
  95. foreach ($parts as $setting) {
  96. $values[$section][$setting] = $this->trimmed($setting);
  97. }
  98. }
  99. // This throws an exception on validation errors
  100. $this->validate($values);
  101. // assert(all values are valid);
  102. $config = new Config();
  103. $config->query('START TRANSACTION');
  104. foreach ($settings as $section => $parts) {
  105. foreach ($parts as $setting) {
  106. Config::save($section, $setting, $values[$section][$setting]);
  107. }
  108. }
  109. $config->query('COMMIT');
  110. return;
  111. }
  112. public function validate(&$values)
  113. {
  114. // Validate site name
  115. if (empty($values['site']['name'])) {
  116. // TRANS: Client error displayed trying to save an empty site name.
  117. $this->clientError(_m('Site name must have non-zero length.'));
  118. }
  119. // Validate email
  120. $values['site']['email'] = common_canonical_email($values['site']['email']);
  121. if (empty($values['site']['email'])) {
  122. // TRANS: Client error displayed trying to save site settings without a contact address.
  123. $this->clientError(_m('You must have a valid contact email address.'));
  124. }
  125. if (!Validate::email($values['site']['email'], common_config('email', 'check_domain'))) {
  126. // TRANS: Client error displayed trying to save site settings without a valid contact address.
  127. $this->clientError(_m('Not a valid email address.'));
  128. }
  129. // Validate logos
  130. if (!empty($values['site']['logo']) &&
  131. !common_valid_http_url($values['site']['logo'])) {
  132. // TRANS: Client error displayed when a logo URL is not valid.
  133. $this->clientError(_m('Invalid logo URL.'));
  134. }
  135. if (!empty($values['site']['ssllogo']) &&
  136. !common_valid_http_url($values['site']['ssllogo'], true)) {
  137. // TRANS: Client error displayed when a SSL logo URL is invalid.
  138. $this->clientError(_m('Invalid SSL logo URL.'));
  139. }
  140. // Validate timezone
  141. if (is_null($values['site']['timezone']) ||
  142. !in_array($values['site']['timezone'], DateTimeZone::listIdentifiers())) {
  143. // TRANS: Client error displayed trying to save site settings without a timezone.
  144. $this->clientError(_m('Timezone not selected.'));
  145. }
  146. // Validate language
  147. if (!is_null($values['site']['language']) &&
  148. !in_array($values['site']['language'], array_keys(get_nice_language_list()))) {
  149. // TRANS: Client error displayed trying to save site settings with an invalid language code.
  150. // TRANS: %s is the invalid language code.
  151. $this->clientError(sprintf(_m('Unknown language "%s".'), $values['site']['language']));
  152. }
  153. // Validate text limit
  154. if (!Validate::number($values['site']['textlimit'], array('min' => 0))) {
  155. // TRANS: Client error displayed trying to save site settings with a text limit below 0.
  156. $this->clientError(_m('Minimum text limit is 0 (unlimited).'));
  157. }
  158. // Validate dupe limit
  159. if (!Validate::number($values['site']['dupelimit'], array('min' => 1))) {
  160. // TRANS: Client error displayed trying to save site settings with a text limit below 1.
  161. $this->clientError(_m('Dupe limit must be one or more seconds.'));
  162. }
  163. }
  164. }
  165. // @todo FIXME: Class documentation missing.
  166. class SiteAdminPanelForm extends AdminForm
  167. {
  168. /**
  169. * ID of the form
  170. *
  171. * @return int ID of the form
  172. */
  173. public function id()
  174. {
  175. return 'form_site_admin_panel';
  176. }
  177. /**
  178. * class of the form
  179. *
  180. * @return string class of the form
  181. */
  182. public function formClass()
  183. {
  184. return 'form_settings';
  185. }
  186. /**
  187. * Action of the form
  188. *
  189. * @return string URL of the action
  190. */
  191. public function action()
  192. {
  193. return common_local_url('siteadminpanel');
  194. }
  195. /**
  196. * Data elements of the form
  197. *
  198. * @return void
  199. */
  200. public function formData()
  201. {
  202. $this->out->elementStart('fieldset', array('id' => 'settings_admin_general'));
  203. // TRANS: Fieldset legend on site settings panel.
  204. $this->out->element('legend', null, _m('LEGEND', 'General'));
  205. $this->out->elementStart('ul', 'form_data');
  206. $this->li();
  207. // TRANS: Field label on site settings panel.
  208. $this->input(
  209. 'name',
  210. _m('LABEL', 'Site name'),
  211. // TRANS: Field title on site settings panel.
  212. _m('The name of your site, like "Yourcompany Microblog".')
  213. );
  214. $this->unli();
  215. $this->li();
  216. // TRANS: Field label on site settings panel.
  217. $this->input(
  218. 'broughtby',
  219. _m('Brought by'),
  220. // TRANS: Field title on site settings panel.
  221. _m('Text used for credits link in footer of each page.')
  222. );
  223. $this->unli();
  224. $this->li();
  225. // TRANS: Field label on site settings panel.
  226. $this->input(
  227. 'broughtbyurl',
  228. _m('Brought by URL'),
  229. // TRANS: Field title on site settings panel.
  230. _m('URL used for credits link in footer of each page.')
  231. );
  232. $this->unli();
  233. $this->li();
  234. // TRANS: Field label on site settings panel.
  235. $this->input(
  236. 'email',
  237. _m('Email'),
  238. // TRANS: Field title on site settings panel.
  239. _m('Contact email address for your site.')
  240. );
  241. $this->unli();
  242. $this->out->elementEnd('ul');
  243. $this->out->elementEnd('fieldset');
  244. $this->showLogo();
  245. $this->out->elementStart('fieldset', array('id' => 'settings_admin_local'));
  246. // TRANS: Fieldset legend on site settings panel.
  247. $this->out->element('legend', null, _m('LEGEND', 'Local'));
  248. $this->out->elementStart('ul', 'form_data');
  249. $timezones = [];
  250. foreach (DateTimeZone::listIdentifiers() as $k => $v) {
  251. $timezones[$v] = $v;
  252. }
  253. asort($timezones);
  254. $this->li();
  255. // TRANS: Dropdown label on site settings panel.
  256. $this->out->dropdown(
  257. 'timezone',
  258. _m('Default timezone'),
  259. // TRANS: Dropdown title on site settings panel.
  260. $timezones,
  261. _m('Default timezone for the site; usually UTC.'),
  262. true,
  263. $this->value('timezone')
  264. );
  265. $this->unli();
  266. $this->li();
  267. $this->out->dropdown(
  268. 'language',
  269. // TRANS: Dropdown label on site settings panel.
  270. _m('Default language'),
  271. get_nice_language_list(),
  272. // TRANS: Dropdown title on site settings panel.
  273. _m('The site language when autodetection from browser settings is not available.'),
  274. false,
  275. $this->value('language')
  276. );
  277. $this->unli();
  278. $this->out->elementEnd('ul');
  279. $this->out->elementEnd('fieldset');
  280. $this->out->elementStart('fieldset', array('id' => 'settings_admin_limits'));
  281. // TRANS: Fieldset legend on site settings panel.
  282. $this->out->element('legend', null, _m('LEGEND', 'Limits'));
  283. $this->out->elementStart('ul', 'form_data');
  284. $this->li();
  285. $this->input(
  286. 'textlimit',
  287. // TRANS: Field label on site settings panel.
  288. _m('Text limit'),
  289. // TRANS: Field title on site settings panel.
  290. _m('Maximum number of characters for notices.')
  291. );
  292. $this->unli();
  293. $this->li();
  294. $this->input(
  295. 'dupelimit',
  296. // TRANS: Field label on site settings panel.
  297. _m('Dupe limit'),
  298. // TRANS: Field title on site settings panel.
  299. _m('How long users must wait (in seconds) to post the same thing again.')
  300. );
  301. $this->unli();
  302. $this->out->elementEnd('ul');
  303. $this->out->elementEnd('fieldset');
  304. }
  305. public function showLogo()
  306. {
  307. $this->out->elementStart('fieldset', ['id' => 'settings_site_logo']);
  308. // TRANS: Fieldset legend for form to change logo.
  309. $this->out->element('legend', null, _m('Logo'));
  310. $this->out->elementStart('ul', 'form_data');
  311. $this->li();
  312. $this->input(
  313. 'logo',
  314. // TRANS: Field label for GNU social site logo.
  315. _m('Site logo'),
  316. // TRANS: Title for field label for GNU social site logo.
  317. 'Logo for the site (full URL).'
  318. );
  319. $this->unli();
  320. $this->li();
  321. $this->input(
  322. 'ssllogo',
  323. // TRANS: Field label for SSL GNU social site logo.
  324. _m('SSL logo'),
  325. // TRANS: Title for field label for SSL GNU social site logo.
  326. 'Logo to show on SSL pages (full URL).'
  327. );
  328. $this->unli();
  329. $this->out->elementEnd('ul');
  330. $this->out->elementEnd('fieldset');
  331. }
  332. /**
  333. * Action elements
  334. *
  335. * @return void
  336. */
  337. public function formActions()
  338. {
  339. $this->out->submit(
  340. 'submit',
  341. // TRANS: Button text for saving site settings.
  342. _m('BUTTON', 'Save'),
  343. 'submit',
  344. null,
  345. // TRANS: Button title for saving site settings.
  346. _m('Save the site settings.')
  347. );
  348. }
  349. }