install_cli.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * CLI Installer
  19. *
  20. * @package Installation
  21. * @author Brion Vibber <brion@pobox.com>
  22. * @author Mikael Nordfeldth <mmn@hethane.se>
  23. * @author Diogo Cordeiro <diogo@fc.up.pt>
  24. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  25. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  26. */
  27. if (php_sapi_name() !== 'cli') {
  28. exit(1);
  29. }
  30. define('INSTALLDIR', dirname(__DIR__));
  31. set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/extlib');
  32. require_once INSTALLDIR . '/lib/installer.php';
  33. require_once 'Console/Getopt.php';
  34. class CliInstaller extends Installer
  35. {
  36. public $verbose = true;
  37. /**
  38. * Go for it!
  39. * @return bool success
  40. */
  41. public function main(): bool
  42. {
  43. if ($this->prepare()) {
  44. if (!$this->checkPrereqs()) {
  45. return false;
  46. }
  47. return $this->handle();
  48. } else {
  49. $this->showHelp();
  50. return false;
  51. }
  52. }
  53. /**
  54. * Get our input parameters...
  55. * @return bool success
  56. */
  57. public function prepare(): bool
  58. {
  59. $shortoptions = 'qvh';
  60. $longoptions = ['quiet', 'verbose', 'help', 'skip-config'];
  61. $map = [
  62. '-s' => 'server',
  63. '--server' => 'server',
  64. '-p' => 'path',
  65. '--path' => 'path',
  66. '--sitename' => 'sitename',
  67. '--fancy' => 'fancy',
  68. '--ssl' => 'ssl',
  69. '--dbtype' => 'dbtype',
  70. '--host' => 'host',
  71. '--database' => 'database',
  72. '--username' => 'username',
  73. '--password' => 'password',
  74. '--admin-nick' => 'adminNick',
  75. '--admin-pass' => 'adminPass',
  76. '--admin-email' => 'adminEmail',
  77. '--site-profile' => 'siteProfile'
  78. ];
  79. foreach ($map as $arg => $target) {
  80. if (substr($arg, 0, 2) == '--') {
  81. $longoptions[] = substr($arg, 2) . '=';
  82. } else {
  83. $shortoptions .= substr($arg, 1) . ':';
  84. }
  85. }
  86. $parser = new Console_Getopt();
  87. $result = $parser->getopt($_SERVER['argv'], $shortoptions, $longoptions);
  88. if (PEAR::isError($result)) {
  89. $this->warning($result->getMessage());
  90. return false;
  91. }
  92. list($options, $args) = $result;
  93. // defaults
  94. $this->dbtype = 'mysql';
  95. $this->verbose = true;
  96. // ssl is defaulted in lib/installer.php
  97. foreach ($options as $option) {
  98. $arg = $option[0];
  99. if (isset($map[$arg])) {
  100. $var = $map[$arg];
  101. $this->$var = $option[1];
  102. if ($arg == '--fancy') {
  103. $this->$var = ($option[1] != 'false') && ($option[1] != 'no');
  104. }
  105. } elseif ($arg == '--skip-config') {
  106. $this->skipConfig = true;
  107. } elseif ($arg == 'q' || $arg == '--quiet') {
  108. $this->verbose = false;
  109. } elseif ($arg == 'v' || $arg == '--verbose') {
  110. $this->verbose = true;
  111. } elseif ($arg == 'h' || $arg == '--help') {
  112. // will go back to show help
  113. return false;
  114. }
  115. }
  116. $fail = false;
  117. if (empty($this->server)) {
  118. $this->updateStatus("You must specify a web server for the site.", true);
  119. // path is optional though
  120. $fail = true;
  121. }
  122. if (!$this->validateDb()) {
  123. $fail = true;
  124. }
  125. if (!$this->validateAdmin()) {
  126. $fail = true;
  127. }
  128. return !$fail;
  129. }
  130. public function handle()
  131. {
  132. return $this->doInstall();
  133. }
  134. public function showHelp()
  135. {
  136. echo <<<END_HELP
  137. install_cli.php - GNU social command-line installer
  138. -s --server=<name> Use <name> as server name (required)
  139. -p --path=<path> Use <path> as path name
  140. --sitename User-friendly site name (required)
  141. --fancy Whether to use fancy URLs (default no)
  142. --ssl Server SSL enabled (default never),
  143. [never | always]
  144. --dbtype 'mysql' (default) or 'pgsql'
  145. --host Database hostname (required)
  146. --database Database/schema name (required)
  147. --username Database username (required)
  148. --password Database password (required)
  149. --admin-nick Administrator nickname (required)
  150. --admin-pass Initial password for admin user (required)
  151. --admin-email Initial email address for admin user
  152. --admin-updates 'yes' (default) or 'no', whether to subscribe
  153. admin to update@status.net (default yes)
  154. --site-profile site profile ['public', 'private' (default), 'community', 'singleuser']
  155. --skip-config Don't write a config.php -- use with caution,
  156. requires a global configuration file.
  157. General options:
  158. -q --quiet Quiet (little output)
  159. -v --verbose Verbose (lots of output)
  160. -h --help Show this message and quit.
  161. END_HELP;
  162. }
  163. /**
  164. * @param string $message
  165. * @param string $submessage
  166. */
  167. public function warning(string $message, string $submessage = '')
  168. {
  169. print $this->html2text($message) . "\n";
  170. if ($submessage != '') {
  171. print " " . $this->html2text($submessage) . "\n";
  172. }
  173. print "\n";
  174. }
  175. /**
  176. * @param string $status
  177. * @param bool $error
  178. */
  179. public function updateStatus(string $status, bool $error = false)
  180. {
  181. if ($this->verbose || $error) {
  182. if ($error) {
  183. print "ERROR: ";
  184. }
  185. print $this->html2text($status);
  186. print "\n";
  187. }
  188. }
  189. /**
  190. * @param string $html
  191. * @return string
  192. */
  193. private function html2text(string $html): string
  194. {
  195. // break out any links for text legibility
  196. $breakout = preg_replace(
  197. '/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
  198. '\2 &lt;\1&gt;',
  199. $html
  200. );
  201. return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
  202. }
  203. }
  204. $installer = new CliInstaller();
  205. $ok = $installer->main();
  206. exit($ok ? 0 : 1);