pathsadminpanel.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. * Paths 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. * Paths 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 PathsadminpanelAction 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 Paths admin panel.
  48. return _('Paths');
  49. }
  50. /**
  51. * Instructions for using this form.
  52. *
  53. * @return string instructions
  54. */
  55. public function getInstructions()
  56. {
  57. // TRANS: Form instructions for Path admin panel.
  58. return _('Path and server settings for this StatusNet site');
  59. }
  60. /**
  61. * Show the paths admin panel form
  62. *
  63. * @return void
  64. */
  65. public function showForm()
  66. {
  67. $form = new PathsAdminPanelForm($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 = [
  79. 'site' => ['path', 'locale_path', 'ssl', 'sslserver'],
  80. 'theme' => ['server', 'dir', 'path', 'sslserver', 'sslpath'],
  81. 'avatar' => ['server', 'dir', 'path'],
  82. 'attachments' => ['server', 'dir', 'path', 'sslserver', 'sslpath'],
  83. ];
  84. // XXX: If we're only going to have one boolean on thi page we
  85. // can remove some of the boolean processing code --Z
  86. static $booleans = array('site' => array('fancy'));
  87. $values = array();
  88. foreach ($settings as $section => $parts) {
  89. foreach ($parts as $setting) {
  90. $values[$section][$setting] = $this->trimmed("$section-$setting");
  91. }
  92. }
  93. foreach ($booleans as $section => $parts) {
  94. foreach ($parts as $setting) {
  95. $values[$section][$setting] = ($this->boolean($setting)) ? 1 : 0;
  96. }
  97. }
  98. $this->validate($values);
  99. // assert(all values are valid);
  100. $config = new Config();
  101. $config->query('START TRANSACTION');
  102. foreach ($settings as $section => $parts) {
  103. foreach ($parts as $setting) {
  104. Config::save($section, $setting, $values[$section][$setting]);
  105. }
  106. }
  107. foreach ($booleans as $section => $parts) {
  108. foreach ($parts as $setting) {
  109. Config::save($section, $setting, $values[$section][$setting]);
  110. }
  111. }
  112. $config->query('COMMIT');
  113. return;
  114. }
  115. /**
  116. * Attempt to validate setting values
  117. *
  118. * @return void
  119. */
  120. public function validate(&$values)
  121. {
  122. // Validate theme dir
  123. if (!empty($values['theme']['dir']) && !is_readable($values['theme']['dir'])) {
  124. // TRANS: Client error in Paths admin panel.
  125. // TRANS: %s is the directory that could not be read from.
  126. $this->clientError(sprintf(_('Theme directory not readable: %s.'), $values['theme']['dir']));
  127. }
  128. // Validate avatar dir
  129. if (empty($values['avatar']['dir']) || !is_writable($values['avatar']['dir'])) {
  130. // TRANS: Client error in Paths admin panel.
  131. // TRANS: %s is the avatar directory that could not be written to.
  132. $this->clientError(sprintf(_('Avatar directory not writable: %s.'), $values['avatar']['dir']));
  133. }
  134. // Validate locales dir
  135. // XXX: What else do we need to validate for lacales path here? --Z
  136. if (!empty($values['site']['locale_path']) && !is_readable($values['site']['locale_path'])) {
  137. // TRANS: Client error in Paths admin panel.
  138. // TRANS: %s is the locales directory that could not be read from.
  139. $this->clientError(sprintf(_('Locales directory not readable: %s.'), $values['site']['locale_path']));
  140. }
  141. // Validate SSL setup
  142. if (mb_strlen($values['site']['sslserver']) > 255) {
  143. // TRANS: Client error in Paths admin panel.
  144. // TRANS: %s is the SSL server URL that is too long.
  145. $this->clientError(_('Invalid SSL server. The maximum length is 255 characters.'));
  146. }
  147. }
  148. }
  149. class PathsAdminPanelForm extends AdminForm
  150. {
  151. /**
  152. * ID of the form
  153. *
  154. * @return int ID of the form
  155. */
  156. public function id()
  157. {
  158. return 'form_paths_admin_panel';
  159. }
  160. /**
  161. * class of the form
  162. *
  163. * @return string class of the form
  164. */
  165. public function formClass()
  166. {
  167. return 'form_settings';
  168. }
  169. /**
  170. * Action of the form
  171. *
  172. * @return string URL of the action
  173. */
  174. public function action()
  175. {
  176. return common_local_url('pathsadminpanel');
  177. }
  178. /**
  179. * Data elements of the form
  180. *
  181. * @return void
  182. */
  183. public function formData()
  184. {
  185. $this->out->elementStart('fieldset', array('id' => 'settings_paths_locale'));
  186. // TRANS: Fieldset legend in Paths admin panel.
  187. $this->out->element('legend', null, _('Site'), 'site');
  188. $this->out->elementStart('ul', 'form_data');
  189. $this->li();
  190. $this->input(
  191. 'server',
  192. // TRANS: Field label in Paths admin panel.
  193. _('Server'),
  194. // TRANS: Field title in Paths admin panel.
  195. _('Site\'s server hostname.')
  196. );
  197. $this->unli();
  198. $this->li();
  199. $this->input(
  200. 'path',
  201. // TRANS: Field label in Paths admin panel.
  202. _('Path'),
  203. // TRANS: Field title in Paths admin panel.
  204. _('Site path.')
  205. );
  206. $this->unli();
  207. $this->li();
  208. $this->input(
  209. 'locale_path',
  210. // TRANS: Field label in Paths admin panel.
  211. _('Locale directory'),
  212. // TRANS: Field title in Paths admin panel.
  213. _('Directory path to locales.'),
  214. 'site'
  215. );
  216. $this->unli();
  217. $this->li();
  218. $this->out->checkbox(
  219. 'fancy',
  220. // TRANS: Checkbox label in Paths admin panel.
  221. _('Fancy URLs'),
  222. (bool) $this->value('fancy'),
  223. // TRANS: Field title in Paths admin panel.
  224. _('Use fancy URLs (more readable and memorable)?')
  225. );
  226. $this->unli();
  227. $this->out->elementEnd('ul');
  228. $this->out->elementEnd('fieldset');
  229. $this->out->elementStart('fieldset', array('id' => 'settings_paths_theme'));
  230. // TRANS: Fieldset legend in Paths admin panel.
  231. $this->out->element('legend', null, _m('LEGEND', 'Theme'));
  232. $this->out->elementStart('ul', 'form_data');
  233. $this->li();
  234. $this->input(
  235. 'server',
  236. // TRANS: Field label in Paths admin panel.
  237. _('Server'),
  238. // TRANS: Tooltip for field label in Paths admin panel.
  239. _('Server for themes.'),
  240. 'theme'
  241. );
  242. $this->unli();
  243. $this->li();
  244. $this->input(
  245. 'path',
  246. // TRANS: Field label in Paths admin panel.
  247. _('Path'),
  248. // TRANS: Tooltip for field label in Paths admin panel.
  249. _('Web path to themes.'),
  250. 'theme'
  251. );
  252. $this->unli();
  253. $this->li();
  254. $this->input(
  255. 'sslserver',
  256. // TRANS: Field label in Paths admin panel.
  257. _('SSL server'),
  258. // TRANS: Tooltip for field label in Paths admin panel.
  259. _('SSL server for themes (default: SSL server).'),
  260. 'theme'
  261. );
  262. $this->unli();
  263. $this->li();
  264. $this->input(
  265. 'sslpath',
  266. // TRANS: Field label in Paths admin panel.
  267. _('SSL path'),
  268. // TRANS: Tooltip for field label in Paths admin panel.
  269. _('SSL path to themes (default: /theme/).'),
  270. 'theme'
  271. );
  272. $this->unli();
  273. $this->li();
  274. $this->input(
  275. 'dir',
  276. // TRANS: Field label in Paths admin panel.
  277. _('Directory'),
  278. // TRANS: Tooltip for field label in Paths admin panel.
  279. _('Directory where themes are located.'),
  280. 'theme'
  281. );
  282. $this->unli();
  283. $this->out->elementEnd('ul');
  284. $this->out->elementEnd('fieldset');
  285. $this->out->elementStart(
  286. 'fieldset',
  287. ['id' => 'settings_avatar-paths']
  288. );
  289. // TRANS: Fieldset legend in Paths admin panel.
  290. $this->out->element('legend', null, _('Avatars'));
  291. $this->out->elementStart('ul', 'form_data');
  292. $this->li();
  293. $this->input(
  294. 'server',
  295. // TRANS: Field label in Paths admin panel.
  296. _('Avatar server'),
  297. // TRANS: Tooltip for field label in Paths admin panel.
  298. _('Server for avatars.'),
  299. 'avatar'
  300. );
  301. $this->unli();
  302. $this->li();
  303. $this->input(
  304. 'path',
  305. // TRANS: Field label in Paths admin panel.
  306. _('Avatar path'),
  307. // TRANS: Tooltip for field label in Paths admin panel.
  308. _('Web path to avatars.'),
  309. 'avatar'
  310. );
  311. $this->unli();
  312. $this->li();
  313. $this->input(
  314. 'dir',
  315. // TRANS: Field label in Paths admin panel.
  316. _('Avatar directory'),
  317. // TRANS: Tooltip for field label in Paths admin panel.
  318. _('Directory where avatars are located.'),
  319. 'avatar'
  320. );
  321. $this->unli();
  322. $this->out->elementEnd('ul');
  323. $this->out->elementEnd('fieldset');
  324. $this->out->elementStart(
  325. 'fieldset',
  326. ['id' => 'settings_attachments-paths']
  327. );
  328. // TRANS: Fieldset legens in Paths admin panel.
  329. $this->out->element('legend', null, _('Attachments'));
  330. $this->out->elementStart('ul', 'form_data');
  331. $this->li();
  332. $this->input(
  333. 'server',
  334. // TRANS: Field label in Paths admin panel.
  335. _('Server'),
  336. // TRANS: Tooltip for field label in Paths admin panel.
  337. _('Server for attachments.'),
  338. 'attachments'
  339. );
  340. $this->unli();
  341. $this->li();
  342. $this->input(
  343. 'path',
  344. // TRANS: Field label in Paths admin panel.
  345. _('Path'),
  346. // TRANS: Tooltip for field label in Paths admin panel.
  347. _('Web path to attachments.'),
  348. 'attachments'
  349. );
  350. $this->unli();
  351. $this->li();
  352. $this->input(
  353. 'sslserver',
  354. // TRANS: Field label in Paths admin panel.
  355. _('SSL server'),
  356. // TRANS: Tooltip for field label in Paths admin panel.
  357. _('Server for attachments on SSL pages.'),
  358. 'attachments'
  359. );
  360. $this->unli();
  361. $this->li();
  362. $this->input(
  363. 'sslpath',
  364. // TRANS: Field label in Paths admin panel.
  365. _('SSL path'),
  366. // TRANS: Tooltip for field label in Paths admin panel.
  367. _('Web path to attachments on SSL pages.'),
  368. 'attachments'
  369. );
  370. $this->unli();
  371. $this->li();
  372. $this->input(
  373. 'dir',
  374. // TRANS: Field label in Paths admin panel.
  375. _('Directory'),
  376. // TRANS: Tooltip for field label in Paths admin panel.
  377. _('Directory where attachments are located.'),
  378. 'attachments'
  379. );
  380. $this->unli();
  381. $this->out->elementEnd('ul');
  382. $this->out->elementEnd('fieldset');
  383. $this->out->elementStart('fieldset', array('id' => 'settings_admin_ssl'));
  384. // TRANS: Fieldset legend in Paths admin panel.
  385. $this->out->element('legend', null, _m('LEGEND', 'SSL'));
  386. $this->out->elementStart('ul', 'form_data');
  387. $this->li();
  388. // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
  389. $ssl = [
  390. 'never' => _('Never'),
  391. // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
  392. 'always' => _('Always'),
  393. ];
  394. $this->out->dropdown(
  395. 'site-ssl',
  396. // TRANS: Drop down label in Paths admin panel.
  397. _('Use SSL'),
  398. // TRANS: Tooltip for field label in Paths admin panel.
  399. $ssl,
  400. _('When to use SSL.'),
  401. false,
  402. $this->value('ssl', 'site')
  403. );
  404. $this->unli();
  405. $this->li();
  406. $this->input(
  407. 'sslserver',
  408. // TRANS: Field label in Paths admin panel.
  409. _('SSL server'),
  410. // TRANS: Tooltip for field label in Paths admin panel.
  411. _('Server to direct SSL requests to.'),
  412. 'site'
  413. );
  414. $this->unli();
  415. $this->out->elementEnd('ul');
  416. $this->out->elementEnd('fieldset');
  417. }
  418. /**
  419. * Action elements
  420. *
  421. * @return void
  422. */
  423. public function formActions()
  424. {
  425. // TRANS: Button text to store form data in the Paths admin panel.
  426. $this->out->submit(
  427. 'save',
  428. _m('BUTTON', 'Save'),
  429. 'submit',
  430. // TRANS: Button title text to store form data in the Paths admin panel.
  431. 'save',
  432. _('Save path settings.')
  433. );
  434. }
  435. /**
  436. * Utility to simplify some of the duplicated code around
  437. * params and settings. Overriding the input() in the base class
  438. * to handle a whole bunch of cases of settings with the same
  439. * name under different sections.
  440. *
  441. * @param string $setting Name of the setting
  442. * @param string $title Title to use for the input
  443. * @param string $instructions Instructions for this field
  444. * @param string $section config section, default = 'site'
  445. *
  446. * @return void
  447. */
  448. public function input($setting, $title, $instructions, $section='site')
  449. {
  450. $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);
  451. }
  452. }