install_cli.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  32. set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/extlib');
  33. require_once INSTALLDIR . '/lib/installer.php';
  34. require_once 'Console/Getopt.php';
  35. class CliInstaller extends Installer
  36. {
  37. public $verbose = true;
  38. /**
  39. * Go for it!
  40. * @return bool success
  41. */
  42. public function main(): bool
  43. {
  44. if ($this->prepare()) {
  45. if (!$this->checkPrereqs()) {
  46. return false;
  47. }
  48. return $this->handle();
  49. } else {
  50. $this->showHelp();
  51. return false;
  52. }
  53. }
  54. /**
  55. * Get our input parameters...
  56. * @return bool success
  57. */
  58. public function prepare(): bool
  59. {
  60. $shortoptions = 'qvh';
  61. $longoptions = ['quiet', 'verbose', 'help', 'skip-config'];
  62. $map = [
  63. '-s' => 'server',
  64. '--server' => 'server',
  65. '-p' => 'path',
  66. '--path' => 'path',
  67. '--sitename' => 'sitename',
  68. '--fancy' => 'fancy',
  69. '--ssl' => 'ssl',
  70. '--dbtype' => 'dbtype',
  71. '--host' => 'host',
  72. '--database' => 'database',
  73. '--username' => 'username',
  74. '--password' => 'password',
  75. '--admin-nick' => 'adminNick',
  76. '--admin-pass' => 'adminPass',
  77. '--admin-email' => 'adminEmail',
  78. '--site-profile' => 'siteProfile'
  79. ];
  80. foreach ($map as $arg => $target) {
  81. if (substr($arg, 0, 2) == '--') {
  82. $longoptions[] = substr($arg, 2) . '=';
  83. } else {
  84. $shortoptions .= substr($arg, 1) . ':';
  85. }
  86. }
  87. $parser = new Console_Getopt();
  88. $result = $parser->getopt($_SERVER['argv'], $shortoptions, $longoptions);
  89. if (PEAR::isError($result)) {
  90. $this->warning($result->getMessage());
  91. return false;
  92. }
  93. list($options, $args) = $result;
  94. // defaults
  95. $this->dbtype = 'mysql';
  96. $this->verbose = true;
  97. // ssl is defaulted in lib/installer.php
  98. foreach ($options as $option) {
  99. $arg = $option[0];
  100. if (isset($map[$arg])) {
  101. $var = $map[$arg];
  102. $this->$var = $option[1];
  103. if ($arg == '--fancy') {
  104. $this->$var = ($option[1] != 'false') && ($option[1] != 'no');
  105. }
  106. } elseif ($arg == '--skip-config') {
  107. $this->skipConfig = true;
  108. } elseif ($arg == 'q' || $arg == '--quiet') {
  109. $this->verbose = false;
  110. } elseif ($arg == 'v' || $arg == '--verbose') {
  111. $this->verbose = true;
  112. } elseif ($arg == 'h' || $arg == '--help') {
  113. // will go back to show help
  114. return false;
  115. }
  116. }
  117. $fail = false;
  118. if (empty($this->server)) {
  119. $this->updateStatus("You must specify a web server for the site.", true);
  120. // path is optional though
  121. $fail = true;
  122. }
  123. if (!$this->validateDb()) {
  124. $fail = true;
  125. }
  126. if (!$this->validateAdmin()) {
  127. $fail = true;
  128. }
  129. return !$fail;
  130. }
  131. public function handle()
  132. {
  133. return $this->doInstall();
  134. }
  135. public function showHelp()
  136. {
  137. echo <<<END_HELP
  138. install_cli.php - GNU social command-line installer
  139. -s --server=<name> Use <name> as server name (required)
  140. -p --path=<path> Use <path> as path name
  141. --sitename User-friendly site name (required)
  142. --fancy Whether to use fancy URLs (default no)
  143. --ssl Server SSL enabled (default never),
  144. [never | always]
  145. --dbtype 'mysql' (default) or 'pgsql'
  146. --host Database hostname (required)
  147. --database Database/schema name (required)
  148. --username Database username (required)
  149. --password Database password (required)
  150. --admin-nick Administrator nickname (required)
  151. --admin-pass Initial password for admin user (required)
  152. --admin-email Initial email address for admin user
  153. --admin-updates 'yes' (default) or 'no', whether to subscribe
  154. admin to update@status.net (default yes)
  155. --site-profile site profile ['public', 'private' (default), 'community', 'singleuser']
  156. --skip-config Don't write a config.php -- use with caution,
  157. requires a global configuration file.
  158. General options:
  159. -q --quiet Quiet (little output)
  160. -v --verbose Verbose (lots of output)
  161. -h --help Show this message and quit.
  162. END_HELP;
  163. }
  164. /**
  165. * @param string $message
  166. * @param string $submessage
  167. */
  168. public function warning(string $message, string $submessage = '')
  169. {
  170. print $this->html2text($message) . "\n";
  171. if ($submessage != '') {
  172. print " " . $this->html2text($submessage) . "\n";
  173. }
  174. print "\n";
  175. }
  176. /**
  177. * @param string $status
  178. * @param bool $error
  179. */
  180. public function updateStatus(string $status, bool $error = false)
  181. {
  182. if ($this->verbose || $error) {
  183. if ($error) {
  184. print "ERROR: ";
  185. }
  186. print $this->html2text($status);
  187. print "\n";
  188. }
  189. }
  190. /**
  191. * @param string $html
  192. * @return string
  193. */
  194. private function html2text(string $html): string
  195. {
  196. // break out any links for text legibility
  197. $breakout = preg_replace(
  198. '/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
  199. '\2 &lt;\1&gt;',
  200. $html
  201. );
  202. return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
  203. }
  204. }
  205. $installer = new CliInstaller();
  206. $ok = $installer->main();
  207. exit($ok ? 0 : 1);