pathsadminpanel.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Paths 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 Evan Prodromou <evan@status.net>
  25. * @author Zach Copley <zach@status.net>
  26. * @author Sarven Capadisli <csarven@status.net>
  27. * @copyright 2008-2011 StatusNet, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('STATUSNET')) {
  32. exit(1);
  33. }
  34. /**
  35. * Paths settings
  36. *
  37. * @category Admin
  38. * @package StatusNet
  39. * @author Evan Prodromou <evan@status.net>
  40. * @author Zach Copley <zach@status.net>
  41. * @author Sarven Capadisli <csarven@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. */
  45. class PathsadminpanelAction extends AdminPanelAction
  46. {
  47. /**
  48. * Returns the page title
  49. *
  50. * @return string page title
  51. */
  52. function title()
  53. {
  54. // TRANS: Title for Paths admin panel.
  55. return _('Paths');
  56. }
  57. /**
  58. * Instructions for using this form.
  59. *
  60. * @return string instructions
  61. */
  62. function getInstructions()
  63. {
  64. // TRANS: Form instructions for Path admin panel.
  65. return _('Path and server settings for this StatusNet site');
  66. }
  67. /**
  68. * Show the paths admin panel form
  69. *
  70. * @return void
  71. */
  72. function showForm()
  73. {
  74. $form = new PathsAdminPanelForm($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. 'site' => array('path', 'locale_path', 'ssl', 'sslserver'),
  87. 'theme' => array('server', 'dir', 'path', 'sslserver', 'sslpath'),
  88. 'avatar' => array('server', 'dir', 'path'),
  89. 'attachments' => array('server', 'dir', 'path', 'sslserver', 'sslpath')
  90. );
  91. // XXX: If we're only going to have one boolean on thi page we
  92. // can remove some of the boolean processing code --Z
  93. static $booleans = array('site' => array('fancy'));
  94. $values = array();
  95. foreach ($settings as $section => $parts) {
  96. foreach ($parts as $setting) {
  97. $values[$section][$setting] = $this->trimmed("$section-$setting");
  98. }
  99. }
  100. foreach ($booleans as $section => $parts) {
  101. foreach ($parts as $setting) {
  102. $values[$section][$setting] = ($this->boolean($setting)) ? 1 : 0;
  103. }
  104. }
  105. $this->validate($values);
  106. // assert(all values are valid);
  107. $config = new Config();
  108. $config->query('BEGIN');
  109. foreach ($settings as $section => $parts) {
  110. foreach ($parts as $setting) {
  111. Config::save($section, $setting, $values[$section][$setting]);
  112. }
  113. }
  114. foreach ($booleans as $section => $parts) {
  115. foreach ($parts as $setting) {
  116. Config::save($section, $setting, $values[$section][$setting]);
  117. }
  118. }
  119. $config->query('COMMIT');
  120. return;
  121. }
  122. /**
  123. * Attempt to validate setting values
  124. *
  125. * @return void
  126. */
  127. function validate(&$values)
  128. {
  129. // Validate theme dir
  130. if (!empty($values['theme']['dir']) && !is_readable($values['theme']['dir'])) {
  131. // TRANS: Client error in Paths admin panel.
  132. // TRANS: %s is the directory that could not be read from.
  133. $this->clientError(sprintf(_('Theme directory not readable: %s.'), $values['theme']['dir']));
  134. }
  135. // Validate avatar dir
  136. if (empty($values['avatar']['dir']) || !is_writable($values['avatar']['dir'])) {
  137. // TRANS: Client error in Paths admin panel.
  138. // TRANS: %s is the avatar directory that could not be written to.
  139. $this->clientError(sprintf(_('Avatar directory not writable: %s.'), $values['avatar']['dir']));
  140. }
  141. // Validate locales dir
  142. // XXX: What else do we need to validate for lacales path here? --Z
  143. if (!empty($values['site']['locale_path']) && !is_readable($values['site']['locale_path'])) {
  144. // TRANS: Client error in Paths admin panel.
  145. // TRANS: %s is the locales directory that could not be read from.
  146. $this->clientError(sprintf(_('Locales directory not readable: %s.'), $values['site']['locale_path']));
  147. }
  148. // Validate SSL setup
  149. if (mb_strlen($values['site']['sslserver']) > 255) {
  150. // TRANS: Client error in Paths admin panel.
  151. // TRANS: %s is the SSL server URL that is too long.
  152. $this->clientError(_('Invalid SSL server. The maximum length is 255 characters.'));
  153. }
  154. }
  155. }
  156. class PathsAdminPanelForm extends AdminForm
  157. {
  158. /**
  159. * ID of the form
  160. *
  161. * @return int ID of the form
  162. */
  163. function id()
  164. {
  165. return 'form_paths_admin_panel';
  166. }
  167. /**
  168. * class of the form
  169. *
  170. * @return string class of the form
  171. */
  172. function formClass()
  173. {
  174. return 'form_settings';
  175. }
  176. /**
  177. * Action of the form
  178. *
  179. * @return string URL of the action
  180. */
  181. function action()
  182. {
  183. return common_local_url('pathsadminpanel');
  184. }
  185. /**
  186. * Data elements of the form
  187. *
  188. * @return void
  189. */
  190. function formData()
  191. {
  192. $this->out->elementStart('fieldset', array('id' => 'settings_paths_locale'));
  193. // TRANS: Fieldset legend in Paths admin panel.
  194. $this->out->element('legend', null, _('Site'), 'site');
  195. $this->out->elementStart('ul', 'form_data');
  196. $this->li();
  197. $this->input('server',
  198. // TRANS: Field label in Paths admin panel.
  199. _('Server'),
  200. // TRANS: Field title in Paths admin panel.
  201. _('Site\'s server hostname.'));
  202. $this->unli();
  203. $this->li();
  204. $this->input('path',
  205. // TRANS: Field label in Paths admin panel.
  206. _('Path'),
  207. // TRANS: Field title in Paths admin panel.
  208. _('Site path.'));
  209. $this->unli();
  210. $this->li();
  211. $this->input('locale_path',
  212. // TRANS: Field label in Paths admin panel.
  213. _('Locale directory'),
  214. // TRANS: Field title in Paths admin panel.
  215. _('Directory path to locales.'),
  216. 'site');
  217. $this->unli();
  218. $this->li();
  219. $this->out->checkbox('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. $this->unli();
  226. $this->out->elementEnd('ul');
  227. $this->out->elementEnd('fieldset');
  228. $this->out->elementStart('fieldset', array('id' => 'settings_paths_theme'));
  229. // TRANS: Fieldset legend in Paths admin panel.
  230. $this->out->element('legend', null, _m('LEGEND','Theme'));
  231. $this->out->elementStart('ul', 'form_data');
  232. $this->li();
  233. $this->input('server',
  234. // TRANS: Field label in Paths admin panel.
  235. _('Server'),
  236. // TRANS: Tooltip for field label in Paths admin panel.
  237. _('Server for themes.'),
  238. 'theme');
  239. $this->unli();
  240. $this->li();
  241. $this->input('path',
  242. // TRANS: Field label in Paths admin panel.
  243. _('Path'),
  244. // TRANS: Tooltip for field label in Paths admin panel.
  245. _('Web path to themes.'),
  246. 'theme');
  247. $this->unli();
  248. $this->li();
  249. $this->input('sslserver',
  250. // TRANS: Field label in Paths admin panel.
  251. _('SSL server'),
  252. // TRANS: Tooltip for field label in Paths admin panel.
  253. _('SSL server for themes (default: SSL server).'),
  254. 'theme');
  255. $this->unli();
  256. $this->li();
  257. $this->input('sslpath',
  258. // TRANS: Field label in Paths admin panel.
  259. _('SSL path'),
  260. // TRANS: Tooltip for field label in Paths admin panel.
  261. _('SSL path to themes (default: /theme/).'),
  262. 'theme');
  263. $this->unli();
  264. $this->li();
  265. $this->input('dir',
  266. // TRANS: Field label in Paths admin panel.
  267. _('Directory'),
  268. // TRANS: Tooltip for field label in Paths admin panel.
  269. _('Directory where themes are located.'),
  270. 'theme');
  271. $this->unli();
  272. $this->out->elementEnd('ul');
  273. $this->out->elementEnd('fieldset');
  274. $this->out->elementStart('fieldset', array('id' => 'settings_avatar-paths'));
  275. // TRANS: Fieldset legend in Paths admin panel.
  276. $this->out->element('legend', null, _('Avatars'));
  277. $this->out->elementStart('ul', 'form_data');
  278. $this->li();
  279. $this->input('server',
  280. // TRANS: Field label in Paths admin panel.
  281. _('Avatar server'),
  282. // TRANS: Tooltip for field label in Paths admin panel.
  283. _('Server for avatars.'),
  284. 'avatar');
  285. $this->unli();
  286. $this->li();
  287. $this->input('path',
  288. // TRANS: Field label in Paths admin panel.
  289. _('Avatar path'),
  290. // TRANS: Tooltip for field label in Paths admin panel.
  291. _('Web path to avatars.'),
  292. 'avatar');
  293. $this->unli();
  294. $this->li();
  295. $this->input('dir',
  296. // TRANS: Field label in Paths admin panel.
  297. _('Avatar directory'),
  298. // TRANS: Tooltip for field label in Paths admin panel.
  299. _('Directory where avatars are located.'),
  300. 'avatar');
  301. $this->unli();
  302. $this->out->elementEnd('ul');
  303. $this->out->elementEnd('fieldset');
  304. $this->out->elementStart('fieldset', array('id' =>
  305. 'settings_attachments-paths'));
  306. // TRANS: Fieldset legens in Paths admin panel.
  307. $this->out->element('legend', null, _('Attachments'));
  308. $this->out->elementStart('ul', 'form_data');
  309. $this->li();
  310. $this->input('server',
  311. // TRANS: Field label in Paths admin panel.
  312. _('Server'),
  313. // TRANS: Tooltip for field label in Paths admin panel.
  314. _('Server for attachments.'),
  315. 'attachments');
  316. $this->unli();
  317. $this->li();
  318. $this->input('path',
  319. // TRANS: Field label in Paths admin panel.
  320. _('Path'),
  321. // TRANS: Tooltip for field label in Paths admin panel.
  322. _('Web path to attachments.'),
  323. 'attachments');
  324. $this->unli();
  325. $this->li();
  326. $this->input('sslserver',
  327. // TRANS: Field label in Paths admin panel.
  328. _('SSL server'),
  329. // TRANS: Tooltip for field label in Paths admin panel.
  330. _('Server for attachments on SSL pages.'),
  331. 'attachments');
  332. $this->unli();
  333. $this->li();
  334. $this->input('sslpath',
  335. // TRANS: Field label in Paths admin panel.
  336. _('SSL path'),
  337. // TRANS: Tooltip for field label in Paths admin panel.
  338. _('Web path to attachments on SSL pages.'),
  339. 'attachments');
  340. $this->unli();
  341. $this->li();
  342. $this->input('dir',
  343. // TRANS: Field label in Paths admin panel.
  344. _('Directory'),
  345. // TRANS: Tooltip for field label in Paths admin panel.
  346. _('Directory where attachments are located.'),
  347. 'attachments');
  348. $this->unli();
  349. $this->out->elementEnd('ul');
  350. $this->out->elementEnd('fieldset');
  351. $this->out->elementStart('fieldset', array('id' => 'settings_admin_ssl'));
  352. // TRANS: Fieldset legend in Paths admin panel.
  353. $this->out->element('legend', null, _m('LEGEND','SSL'));
  354. $this->out->elementStart('ul', 'form_data');
  355. $this->li();
  356. // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
  357. $ssl = array('never' => _('Never'),
  358. // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
  359. 'sometimes' => _('Sometimes'),
  360. // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
  361. 'always' => _('Always'));
  362. $this->out->dropdown('site-ssl',
  363. // TRANS: Drop down label in Paths admin panel.
  364. _('Use SSL'),
  365. // TRANS: Tooltip for field label in Paths admin panel.
  366. $ssl, _('When to use SSL.'),
  367. false,
  368. $this->value('ssl', 'site'));
  369. $this->unli();
  370. $this->li();
  371. $this->input('sslserver',
  372. // TRANS: Field label in Paths admin panel.
  373. _('SSL server'),
  374. // TRANS: Tooltip for field label in Paths admin panel.
  375. _('Server to direct SSL requests to.'),
  376. 'site');
  377. $this->unli();
  378. $this->out->elementEnd('ul');
  379. $this->out->elementEnd('fieldset');
  380. }
  381. /**
  382. * Action elements
  383. *
  384. * @return void
  385. */
  386. function formActions()
  387. {
  388. // TRANS: Button text to store form data in the Paths admin panel.
  389. $this->out->submit('save', _m('BUTTON','Save'), 'submit',
  390. // TRANS: Button title text to store form data in the Paths admin panel.
  391. 'save', _('Save path settings.'));
  392. }
  393. /**
  394. * Utility to simplify some of the duplicated code around
  395. * params and settings. Overriding the input() in the base class
  396. * to handle a whole bunch of cases of settings with the same
  397. * name under different sections.
  398. *
  399. * @param string $setting Name of the setting
  400. * @param string $title Title to use for the input
  401. * @param string $instructions Instructions for this field
  402. * @param string $section config section, default = 'site'
  403. *
  404. * @return void
  405. */
  406. function input($setting, $title, $instructions, $section='site')
  407. {
  408. $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);
  409. }
  410. }