3 // This file is part of GNU social - https://www.gnu.org/software/social
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.
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.
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/>.
21 * @package Installation
22 * @author Brion Vibber <brion@pobox.com>
23 * @author Mikael Nordfeldth <mmn@hethane.se>
24 * @author Diogo Cordeiro <diogo@fc.up.pt>
25 * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
26 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
29 if (php_sapi_name() !== 'cli') {
33 define('INSTALLDIR', dirname(__DIR__));
34 set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/extlib');
36 require_once INSTALLDIR . '/lib/installer.php';
37 require_once 'Console/Getopt.php';
39 class CliInstaller extends Installer
41 public $verbose = true;
45 * @return bool success
47 public function main(): bool
49 if ($this->prepare()) {
50 if (!$this->checkPrereqs()) {
53 return $this->handle();
61 * Get our input parameters...
62 * @return bool success
64 public function prepare(): bool
66 $shortoptions = 'qvh';
67 $longoptions = ['quiet', 'verbose', 'help', 'skip-config'];
70 '--server' => 'server',
73 '--sitename' => 'sitename',
77 '--dbtype' => 'dbtype',
79 '--database' => 'database',
80 '--username' => 'username',
81 '--password' => 'password',
83 '--admin-nick' => 'adminNick',
84 '--admin-pass' => 'adminPass',
85 '--admin-email' => 'adminEmail',
87 '--site-profile' => 'siteProfile'
89 foreach ($map as $arg => $target) {
90 if (substr($arg, 0, 2) == '--') {
91 $longoptions[] = substr($arg, 2) . '=';
93 $shortoptions .= substr($arg, 1) . ':';
97 $parser = new Console_Getopt();
98 $result = $parser->getopt($_SERVER['argv'], $shortoptions, $longoptions);
99 if (PEAR::isError($result)) {
100 $this->warning($result->getMessage());
103 list($options, $args) = $result;
106 $this->dbtype = 'mysql';
107 $this->verbose = true;
108 // ssl is defaulted in lib/installer.php
110 foreach ($options as $option) {
112 if (isset($map[$arg])) {
114 $this->$var = $option[1];
115 if ($arg == '--fancy') {
116 $this->$var = ($option[1] != 'false') && ($option[1] != 'no');
118 } elseif ($arg == '--skip-config') {
119 $this->skipConfig = true;
120 } elseif ($arg == 'q' || $arg == '--quiet') {
121 $this->verbose = false;
122 } elseif ($arg == 'v' || $arg == '--verbose') {
123 $this->verbose = true;
124 } elseif ($arg == 'h' || $arg == '--help') {
125 // will go back to show help
131 if (empty($this->server)) {
132 $this->updateStatus("You must specify a web server for the site.", true);
133 // path is optional though
137 if (!$this->validateDb()) {
141 if (!$this->validateAdmin()) {
148 public function handle()
150 return $this->doInstall();
153 public function showHelp()
156 install_cli.php - GNU social command-line installer
158 -s --server=<name> Use <name> as server name (required)
159 -p --path=<path> Use <path> as path name
160 --sitename User-friendly site name (required)
161 --fancy Whether to use fancy URLs (default no)
162 --ssl Server SSL enabled (default never),
165 --dbtype 'mysql' (default) or 'pgsql'
166 --host Database hostname (required)
167 --database Database/schema name (required)
168 --username Database username (required)
169 --password Database password (required)
171 --admin-nick Administrator nickname (required)
172 --admin-pass Initial password for admin user (required)
173 --admin-email Initial email address for admin user
174 --admin-updates 'yes' (default) or 'no', whether to subscribe
175 admin to update@status.net (default yes)
177 --site-profile site profile ['public', 'private' (default), 'community', 'singleuser']
179 --skip-config Don't write a config.php -- use with caution,
180 requires a global configuration file.
184 -q --quiet Quiet (little output)
185 -v --verbose Verbose (lots of output)
186 -h --help Show this message and quit.
192 * @param string $message
193 * @param string $submessage
195 public function warning(string $message, string $submessage = '')
197 print $this->html2text($message) . "\n";
198 if ($submessage != '') {
199 print " " . $this->html2text($submessage) . "\n";
205 * @param string $status
208 public function updateStatus(string $status, bool $error = false)
210 if ($this->verbose || $error) {
214 print $this->html2text($status);
220 * @param string $html
223 private function html2text(string $html): string
225 // break out any links for text legibility
226 $breakout = preg_replace(
227 '/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
231 return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
235 $installer = new CliInstaller();
236 $ok = $installer->main();