install.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. * Web Installer
  18. *
  19. * @package Installation
  20. * @author Adrian Lang <mail@adrianlang.de>
  21. * @author Brenda Wallace <shiny@cpan.org>
  22. * @author Brett Taylor <brett@webfroot.co.nz>
  23. * @author Brion Vibber <brion@pobox.com>
  24. * @author CiaranG <ciaran@ciarang.com>
  25. * @author Craig Andrews <candrews@integralblue.com>
  26. * @author Eric Helgeson <helfire@Erics-MBP.local>
  27. * @author Evan Prodromou <evan@status.net>
  28. * @author Mikael Nordfeldth <mmn@hethane.se>
  29. * @author Robin Millette <millette@controlyourself.ca>
  30. * @author Sarven Capadisli <csarven@status.net>
  31. * @author Tom Adams <tom@holizz.com>
  32. * @author Zach Copley <zach@status.net>
  33. * @author Diogo Cordeiro <diogo@fc.up.pt>
  34. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  35. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  36. */
  37. define('INSTALLDIR', dirname(__DIR__));
  38. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  39. require INSTALLDIR . '/lib/util/installer.php';
  40. /**
  41. * Helper class for building form
  42. */
  43. class Posted
  44. {
  45. /**
  46. * HTML-friendly escaped string for the POST param of given name, or empty.
  47. * @param string $name
  48. * @return string
  49. */
  50. public function value(string $name): string
  51. {
  52. return htmlspecialchars($this->string($name));
  53. }
  54. /**
  55. * The given POST parameter value, forced to a string.
  56. * Missing value will give ''.
  57. *
  58. * @param string $name
  59. * @return string
  60. */
  61. public function string(string $name): string
  62. {
  63. return strval($this->raw($name));
  64. }
  65. /**
  66. * The given POST parameter value, in its original form.
  67. * Missing value will give null.
  68. *
  69. * @param string $name
  70. * @return mixed
  71. */
  72. public function raw(string $name)
  73. {
  74. return filter_input(INPUT_POST, $name);
  75. }
  76. }
  77. /**
  78. * Web-based installer: provides a form and such.
  79. */
  80. class WebInstaller extends Installer
  81. {
  82. public $validated;
  83. /**
  84. * the actual installation.
  85. * If call libraries are present, then install
  86. *
  87. * @return void
  88. */
  89. public function main()
  90. {
  91. if (!$this->checkPrereqs()) {
  92. $this->warning(_('Please fix the above stated problems and refresh this page to continue installing.'));
  93. return;
  94. }
  95. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  96. $this->handlePost();
  97. } else {
  98. $this->showForm();
  99. }
  100. }
  101. /**
  102. * Web implementation of warning output
  103. * @param string $message
  104. * @param string $submessage
  105. */
  106. public function warning(string $message, string $submessage = '')
  107. {
  108. print "<p class=\"error\">$message</p>\n";
  109. if ($submessage != '') {
  110. print "<p>$submessage</p>\n";
  111. }
  112. }
  113. /**
  114. * Web implementation of status output
  115. * @param string $status
  116. * @param bool $error
  117. */
  118. public function updateStatus(string $status, bool $error = false)
  119. {
  120. echo '<li' . ($error ? ' class="error"' : '') . ">$status</li>";
  121. }
  122. /**
  123. * Show the web form!
  124. */
  125. public function showForm()
  126. {
  127. global $dbModules;
  128. $post = new Posted();
  129. $dbRadios = '';
  130. $dbtype = $post->raw('dbtype');
  131. foreach (self::$dbModules as $type => $info) {
  132. if (extension_loaded($info['check_module'])) {
  133. if ($dbtype == null || $dbtype == $type) {
  134. $checked = 'checked="checked" ';
  135. $dbtype = $type; // if we didn't have one checked, hit the first
  136. } else {
  137. $checked = '';
  138. }
  139. $dbRadios .= sprintf(
  140. '<input type="radio" name="dbtype" id="dbtype-%1$s" value="%1$s" %2$s/>%3$s<br>',
  141. htmlspecialchars($type),
  142. $checked,
  143. htmlspecialchars($info['name'])
  144. );
  145. }
  146. }
  147. $ssl = ['always' => null, 'never' => null];
  148. if (!empty($_SERVER['HTTPS'])) {
  149. $ssl['always'] = 'checked="checked"';
  150. } else {
  151. $ssl['never'] = 'checked="checked"';
  152. }
  153. echo <<<E_O_T
  154. <form method="post" action="install.php" class="form_settings" id="form_install">
  155. <fieldset>
  156. <fieldset id="settings_site">
  157. <legend>Site settings</legend>
  158. <ul class="form_data">
  159. <li>
  160. <label for="sitename">Site name</label>
  161. <input type="text" id="sitename" name="sitename" value="{$post->value('sitename')}">
  162. <p class="form_guide">The name of your site</p>
  163. </li>
  164. <li>
  165. <label for="fancy-enable">Fancy URLs</label>
  166. <input type="radio" name="fancy" id="fancy-enable" value="enable" checked='checked'> enable<br>
  167. <input type="radio" name="fancy" id="fancy-disable" value=""> disable<br>
  168. <p class="form_guide" id='fancy-form_guide'>Enable fancy (pretty) URLs. Auto-detection failed, it depends on Javascript.</p>
  169. </li>
  170. <li>
  171. <label for="ssl">Server SSL</label>
  172. <input type="radio" name="ssl" id="ssl-always" value="always" {$ssl['always']}> enable<br>
  173. <input type="radio" name="ssl" id="ssl-never" value="never" {$ssl['never']}> disable<br>
  174. <input type="radio" name="ssl" id="ssl-proxy" value="proxy"> proxied<br>
  175. <p class="form_guide" id="ssl-form_guide">Enabling SSL (https://) requires extra webserver configuration and certificate generation not offered by this installation.</p>
  176. </li>
  177. </ul>
  178. </fieldset>
  179. <fieldset id="settings_db">
  180. <legend>Database settings</legend>
  181. <ul class="form_data">
  182. <li>
  183. <label for="host">Hostname</label>
  184. <input type="text" id="host" name="host" value="{$post->value('host')}">
  185. <p class="form_guide">Database hostname</p>
  186. </li>
  187. <li>
  188. <label for="dbtype">Type</label>
  189. {$dbRadios}
  190. <p class="form_guide">Database type</p>
  191. </li>
  192. <li>
  193. <label for="database">Name</label>
  194. <input type="text" id="database" name="database" value="{$post->value('database')}">
  195. <p class="form_guide">Database name</p>
  196. </li>
  197. <li>
  198. <label for="dbusername">DB username</label>
  199. <input type="text" id="dbusername" name="dbusername" value="{$post->value('dbusername')}">
  200. <p class="form_guide">Database username</p>
  201. </li>
  202. <li>
  203. <label for="dbpassword">DB password</label>
  204. <input type="password" id="dbpassword" name="dbpassword" value="{$post->value('dbpassword')}">
  205. <p class="form_guide">Database password (optional)</p>
  206. </li>
  207. </ul>
  208. </fieldset>
  209. <fieldset id="settings_admin">
  210. <legend>Administrator settings</legend>
  211. <ul class="form_data">
  212. <li>
  213. <label for="admin_nickname">Administrator nickname</label>
  214. <input type="text" id="admin_nickname" name="admin_nickname" value="{$post->value('admin_nickname')}">
  215. <p class="form_guide">Nickname for the initial user (administrator)</p>
  216. </li>
  217. <li>
  218. <label for="admin_password">Administrator password</label>
  219. <input type="password" id="admin_password" name="admin_password" value="{$post->value('admin_password')}">
  220. <p class="form_guide">Password for the initial user (administrator)</p>
  221. </li>
  222. <li>
  223. <label for="admin_password2">Confirm password</label>
  224. <input type="password" id="admin_password2" name="admin_password2" value="{$post->value('admin_password2')}">
  225. </li>
  226. <li>
  227. <label for="admin_email">Administrator e-mail</label>
  228. <input id="admin_email" name="admin_email" value="{$post->value('admin_email')}">
  229. <p class="form_guide">Optional email address for the initial user (administrator)</p>
  230. </li>
  231. </ul>
  232. </fieldset>
  233. <fieldset id="settings_profile">
  234. <legend>Site profile</legend>
  235. <ul class="form_data">
  236. <li>
  237. <label for="site_profile">Type of site</label>
  238. <select id="site_profile" name="site_profile">
  239. <option value="community">Community</option>
  240. <option value="public">Public (open registration)</option>
  241. <option value="singleuser">Single User</option>
  242. <option value="private">Private (no federation)</option>
  243. </select>
  244. <p class="form_guide">Initial access settings for your site</p>
  245. </li>
  246. </ul>
  247. </fieldset>
  248. <input type="submit" name="submit" class="submit" value="Submit">
  249. </fieldset>
  250. </form>
  251. E_O_T;
  252. }
  253. /**
  254. * Handle a POST submission... if we have valid input, start the install!
  255. * Otherwise shows the form along with any error messages.
  256. */
  257. public function handlePost()
  258. {
  259. echo <<<STR
  260. <dl class="system_notice">
  261. <dt>Page notice</dt>
  262. <dd>
  263. <ul>
  264. STR;
  265. $this->validated = $this->prepare();
  266. if ($this->validated) {
  267. $this->doInstall();
  268. }
  269. echo <<<STR
  270. </ul>
  271. </dd>
  272. </dl>
  273. STR;
  274. if (!$this->validated) {
  275. $this->showForm();
  276. }
  277. }
  278. /**
  279. * Read and validate input data.
  280. * May output side effects.
  281. *
  282. * @return bool success
  283. */
  284. public function prepare(): bool
  285. {
  286. $post = new Posted();
  287. $this->host = $post->string('host');
  288. $this->dbtype = $post->string('dbtype');
  289. $this->database = $post->string('database');
  290. $this->username = $post->string('dbusername');
  291. $this->password = $post->string('dbpassword');
  292. $this->sitename = $post->string('sitename');
  293. $this->fancy = (bool)$post->string('fancy');
  294. $this->adminNick = strtolower($post->string('admin_nickname'));
  295. $this->adminPass = $post->string('admin_password');
  296. $adminPass2 = $post->string('admin_password2');
  297. $this->adminEmail = $post->string('admin_email');
  298. $this->siteProfile = $post->string('site_profile');
  299. $this->ssl = $post->string('ssl');
  300. $this->server = $_SERVER['HTTP_HOST'];
  301. $this->path = substr(dirname($_SERVER['PHP_SELF']), 1);
  302. $fail = false;
  303. if (!$this->validateDb()) {
  304. $fail = true;
  305. }
  306. if (!$this->validateAdmin()) {
  307. $fail = true;
  308. }
  309. if ($this->adminPass != $adminPass2) {
  310. $this->updateStatus("Administrator passwords do not match. Did you mistype?", true);
  311. $fail = true;
  312. }
  313. if (!in_array($this->ssl, ['never', 'always', 'proxy'])) {
  314. $this->updateStatus("Bad value for server SSL enabling.");
  315. $fail = true;
  316. }
  317. if (!$this->validateSiteProfile()) {
  318. $fail = true;
  319. }
  320. return !$fail;
  321. }
  322. }
  323. ?>
  324. <!DOCTYPE html>
  325. <html lang="en">
  326. <head>
  327. <title>Install GNU social</title>
  328. <link rel="shortcut icon" href="favicon.ico">
  329. <link rel="stylesheet" type="text/css" href="theme/base/css/display.css" media="screen, projection, tv">
  330. <link rel="stylesheet" type="text/css" href="theme/neo/css/display.css" media="screen, projection, tv">
  331. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  332. <script src="js/extlib/jquery.js"></script>
  333. <script src="js/install.js"></script>
  334. </head>
  335. <body id="install">
  336. <div id="wrap">
  337. <div id="header">
  338. <address id="site_contact" class="h-card">
  339. <a class="u-url p-name home bookmark org" href=".">
  340. <img class="logo u-photo" src="theme/neo/logo.png" alt="GNU social"/>
  341. GNU social
  342. </a>
  343. </address>
  344. <div id="site_nav_global_primary"></div>
  345. </div>
  346. <div id="core">
  347. <div id="aside_primary_wrapper">
  348. <div id="content_wrapper">
  349. <div id="site_nav_local_views_wrapper">
  350. <div id="site_nav_local_views"></div>
  351. <div id="content">
  352. <div id="content_inner">
  353. <h1>Install GNU social</h1>
  354. <?php
  355. $installer = new WebInstaller();
  356. $installer->main();
  357. ?>
  358. </div>
  359. </div>
  360. <div id="aside_primary" class="aside"></div>
  361. </div>
  362. </div>
  363. </div>
  364. </div>
  365. <div id="footer"></div>
  366. </div>
  367. </body>
  368. </html>