install.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. * Magic quotes are stripped, if provided.
  68. * Missing value will give null.
  69. *
  70. * @param string $name
  71. * @return mixed
  72. */
  73. public function raw(string $name)
  74. {
  75. if (isset($_POST[$name])) {
  76. return $this->dequote($_POST[$name]);
  77. } else {
  78. return null;
  79. }
  80. }
  81. /**
  82. * If necessary, strip magic quotes from the given value.
  83. *
  84. * @param mixed $val
  85. * @return mixed
  86. */
  87. public function dequote($val)
  88. {
  89. if (get_magic_quotes_gpc()) {
  90. if (is_string($val)) {
  91. return stripslashes($val);
  92. } elseif (is_array($val)) {
  93. return array_map([$this, 'dequote'], $val);
  94. }
  95. }
  96. return $val;
  97. }
  98. }
  99. /**
  100. * Web-based installer: provides a form and such.
  101. */
  102. class WebInstaller extends Installer
  103. {
  104. /**
  105. * the actual installation.
  106. * If call libraries are present, then install
  107. *
  108. * @return void
  109. */
  110. public function main()
  111. {
  112. if (!$this->checkPrereqs()) {
  113. $this->warning(_('Please fix the above stated problems and refresh this page to continue installing.'));
  114. return;
  115. }
  116. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  117. $this->handlePost();
  118. } else {
  119. $this->showForm();
  120. }
  121. }
  122. /**
  123. * Web implementation of warning output
  124. * @param string $message
  125. * @param string $submessage
  126. */
  127. public function warning(string $message, string $submessage = '')
  128. {
  129. print "<p class=\"error\">$message</p>\n";
  130. if ($submessage != '') {
  131. print "<p>$submessage</p>\n";
  132. }
  133. }
  134. /**
  135. * Web implementation of status output
  136. * @param string $status
  137. * @param bool $error
  138. */
  139. public function updateStatus(string $status, bool $error = false)
  140. {
  141. echo '<li' . ($error ? ' class="error"' : '') . ">$status</li>";
  142. }
  143. /**
  144. * Show the web form!
  145. */
  146. public function showForm()
  147. {
  148. global $dbModules;
  149. $post = new Posted();
  150. $dbRadios = '';
  151. $dbtype = $post->raw('dbtype');
  152. foreach (self::$dbModules as $type => $info) {
  153. if (extension_loaded($info['check_module'])) {
  154. if ($dbtype == null || $dbtype == $type) {
  155. $checked = 'checked="checked" ';
  156. $dbtype = $type; // if we didn't have one checked, hit the first
  157. } else {
  158. $checked = '';
  159. }
  160. $dbRadios .= sprintf(
  161. '<input type="radio" name="dbtype" id="dbtype-%1$s" value="%1$s" %2$s/>%3$s<br>',
  162. htmlspecialchars($type),
  163. $checked,
  164. htmlspecialchars($info['name'])
  165. );
  166. }
  167. }
  168. $ssl = ['always' => null, 'never' => null];
  169. if (!empty($_SERVER['HTTPS'])) {
  170. $ssl['always'] = 'checked="checked"';
  171. } else {
  172. $ssl['never'] = 'checked="checked"';
  173. }
  174. echo <<<E_O_T
  175. <form method="post" action="install.php" class="form_settings" id="form_install">
  176. <fieldset>
  177. <fieldset id="settings_site">
  178. <legend>Site settings</legend>
  179. <ul class="form_data">
  180. <li>
  181. <label for="sitename">Site name</label>
  182. <input type="text" id="sitename" name="sitename" value="{$post->value('sitename')}">
  183. <p class="form_guide">The name of your site</p>
  184. </li>
  185. <li>
  186. <label for="fancy-enable">Fancy URLs</label>
  187. <input type="radio" name="fancy" id="fancy-enable" value="enable" checked='checked'> enable<br>
  188. <input type="radio" name="fancy" id="fancy-disable" value=""> disable<br>
  189. <p class="form_guide" id='fancy-form_guide'>Enable fancy (pretty) URLs. Auto-detection failed, it depends on Javascript.</p>
  190. </li>
  191. <li>
  192. <label for="ssl">Server SSL</label>
  193. <input type="radio" name="ssl" id="ssl-always" value="always" {$ssl['always']}> enable<br>
  194. <input type="radio" name="ssl" id="ssl-never" value="never" {$ssl['never']}> disable<br>
  195. <input type="radio" name="ssl" id="ssl-proxy" value="proxy"> proxied<br>
  196. <p class="form_guide" id="ssl-form_guide">Enabling SSL (https://) requires extra webserver configuration and certificate generation not offered by this installation.</p>
  197. </li>
  198. </ul>
  199. </fieldset>
  200. <fieldset id="settings_db">
  201. <legend>Database settings</legend>
  202. <ul class="form_data">
  203. <li>
  204. <label for="host">Hostname</label>
  205. <input type="text" id="host" name="host" value="{$post->value('host')}">
  206. <p class="form_guide">Database hostname</p>
  207. </li>
  208. <li>
  209. <label for="dbtype">Type</label>
  210. {$dbRadios}
  211. <p class="form_guide">Database type</p>
  212. </li>
  213. <li>
  214. <label for="database">Name</label>
  215. <input type="text" id="database" name="database" value="{$post->value('database')}">
  216. <p class="form_guide">Database name</p>
  217. </li>
  218. <li>
  219. <label for="dbusername">DB username</label>
  220. <input type="text" id="dbusername" name="dbusername" value="{$post->value('dbusername')}">
  221. <p class="form_guide">Database username</p>
  222. </li>
  223. <li>
  224. <label for="dbpassword">DB password</label>
  225. <input type="password" id="dbpassword" name="dbpassword" value="{$post->value('dbpassword')}">
  226. <p class="form_guide">Database password (optional)</p>
  227. </li>
  228. </ul>
  229. </fieldset>
  230. <fieldset id="settings_admin">
  231. <legend>Administrator settings</legend>
  232. <ul class="form_data">
  233. <li>
  234. <label for="admin_nickname">Administrator nickname</label>
  235. <input type="text" id="admin_nickname" name="admin_nickname" value="{$post->value('admin_nickname')}">
  236. <p class="form_guide">Nickname for the initial user (administrator)</p>
  237. </li>
  238. <li>
  239. <label for="admin_password">Administrator password</label>
  240. <input type="password" id="admin_password" name="admin_password" value="{$post->value('admin_password')}">
  241. <p class="form_guide">Password for the initial user (administrator)</p>
  242. </li>
  243. <li>
  244. <label for="admin_password2">Confirm password</label>
  245. <input type="password" id="admin_password2" name="admin_password2" value="{$post->value('admin_password2')}">
  246. </li>
  247. <li>
  248. <label for="admin_email">Administrator e-mail</label>
  249. <input id="admin_email" name="admin_email" value="{$post->value('admin_email')}">
  250. <p class="form_guide">Optional email address for the initial user (administrator)</p>
  251. </li>
  252. </ul>
  253. </fieldset>
  254. <fieldset id="settings_profile">
  255. <legend>Site profile</legend>
  256. <ul class="form_data">
  257. <li>
  258. <label for="site_profile">Type of site</label>
  259. <select id="site_profile" name="site_profile">
  260. <option value="community">Community</option>
  261. <option value="public">Public (open registration)</option>
  262. <option value="singleuser">Single User</option>
  263. <option value="private">Private (no federation)</option>
  264. </select>
  265. <p class="form_guide">Initial access settings for your site</p>
  266. </li>
  267. </ul>
  268. </fieldset>
  269. <input type="submit" name="submit" class="submit" value="Submit">
  270. </fieldset>
  271. </form>
  272. E_O_T;
  273. }
  274. /**
  275. * Handle a POST submission... if we have valid input, start the install!
  276. * Otherwise shows the form along with any error messages.
  277. */
  278. public function handlePost()
  279. {
  280. echo <<<STR
  281. <dl class="system_notice">
  282. <dt>Page notice</dt>
  283. <dd>
  284. <ul>
  285. STR;
  286. $this->validated = $this->prepare();
  287. if ($this->validated) {
  288. $this->doInstall();
  289. }
  290. echo <<<STR
  291. </ul>
  292. </dd>
  293. </dl>
  294. STR;
  295. if (!$this->validated) {
  296. $this->showForm();
  297. }
  298. }
  299. /**
  300. * Read and validate input data.
  301. * May output side effects.
  302. *
  303. * @return bool success
  304. */
  305. public function prepare(): bool
  306. {
  307. $post = new Posted();
  308. $this->host = $post->string('host');
  309. $this->dbtype = $post->string('dbtype');
  310. $this->database = $post->string('database');
  311. $this->username = $post->string('dbusername');
  312. $this->password = $post->string('dbpassword');
  313. $this->sitename = $post->string('sitename');
  314. $this->fancy = (bool)$post->string('fancy');
  315. $this->adminNick = strtolower($post->string('admin_nickname'));
  316. $this->adminPass = $post->string('admin_password');
  317. $adminPass2 = $post->string('admin_password2');
  318. $this->adminEmail = $post->string('admin_email');
  319. $this->siteProfile = $post->string('site_profile');
  320. $this->ssl = $post->string('ssl');
  321. $this->server = $_SERVER['HTTP_HOST'];
  322. $this->path = substr(dirname($_SERVER['PHP_SELF']), 1);
  323. $fail = false;
  324. if (!$this->validateDb()) {
  325. $fail = true;
  326. }
  327. if (!$this->validateAdmin()) {
  328. $fail = true;
  329. }
  330. if ($this->adminPass != $adminPass2) {
  331. $this->updateStatus("Administrator passwords do not match. Did you mistype?", true);
  332. $fail = true;
  333. }
  334. if (!in_array($this->ssl, ['never', 'always', 'proxy'])) {
  335. $this->updateStatus("Bad value for server SSL enabling.");
  336. $fail = true;
  337. }
  338. if (!$this->validateSiteProfile()) {
  339. $fail = true;
  340. }
  341. return !$fail;
  342. }
  343. }
  344. ?>
  345. <!DOCTYPE html>
  346. <html lang="en">
  347. <head>
  348. <title>Install GNU social</title>
  349. <link rel="shortcut icon" href="favicon.ico">
  350. <link rel="stylesheet" type="text/css" href="theme/base/css/display.css" media="screen, projection, tv">
  351. <link rel="stylesheet" type="text/css" href="theme/neo/css/display.css" media="screen, projection, tv">
  352. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  353. <script src="js/extlib/jquery.js"></script>
  354. <script src="js/install.js"></script>
  355. </head>
  356. <body id="install">
  357. <div id="wrap">
  358. <div id="header">
  359. <address id="site_contact" class="h-card">
  360. <a class="u-url p-name home bookmark org" href=".">
  361. <img class="logo u-photo" src="theme/neo/logo.png" alt="GNU social"/>
  362. GNU social
  363. </a>
  364. </address>
  365. <div id="site_nav_global_primary"></div>
  366. </div>
  367. <div id="core">
  368. <div id="aside_primary_wrapper">
  369. <div id="content_wrapper">
  370. <div id="site_nav_local_views_wrapper">
  371. <div id="site_nav_local_views"></div>
  372. <div id="content">
  373. <div id="content_inner">
  374. <h1>Install GNU social</h1>
  375. <?php
  376. $installer = new WebInstaller();
  377. $installer->main();
  378. ?>
  379. </div>
  380. </div>
  381. <div id="aside_primary" class="aside"></div>
  382. </div>
  383. </div>
  384. </div>
  385. </div>
  386. <div id="footer"></div>
  387. </div>
  388. </body>
  389. </html>