]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/install_cli.php
cb1ef4c0e1811e985d017564f3a29108c2948ac8
[quix0rs-gnu-social.git] / scripts / install_cli.php
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 /**
19  * CLI Installer
20  *
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
27  */
28
29 if (php_sapi_name() !== 'cli') {
30     exit(1);
31 }
32
33 define('INSTALLDIR', dirname(__DIR__));
34 set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/extlib');
35
36 require_once INSTALLDIR . '/lib/installer.php';
37 require_once 'Console/Getopt.php';
38
39 class CliInstaller extends Installer
40 {
41     public $verbose = true;
42
43     /**
44      * Go for it!
45      * @return bool success
46      */
47     public function main(): bool
48     {
49         if ($this->prepare()) {
50             if (!$this->checkPrereqs()) {
51                 return false;
52             }
53             return $this->handle();
54         } else {
55             $this->showHelp();
56             return false;
57         }
58     }
59
60     /**
61      * Get our input parameters...
62      * @return bool success
63      */
64     public function prepare(): bool
65     {
66         $shortoptions = 'qvh';
67         $longoptions = ['quiet', 'verbose', 'help', 'skip-config'];
68         $map = [
69             '-s' => 'server',
70             '--server' => 'server',
71             '-p' => 'path',
72             '--path' => 'path',
73             '--sitename' => 'sitename',
74             '--fancy' => 'fancy',
75             '--ssl' => 'ssl',
76
77             '--dbtype' => 'dbtype',
78             '--host' => 'host',
79             '--database' => 'database',
80             '--username' => 'username',
81             '--password' => 'password',
82
83             '--admin-nick' => 'adminNick',
84             '--admin-pass' => 'adminPass',
85             '--admin-email' => 'adminEmail',
86
87             '--site-profile' => 'siteProfile'
88         ];
89         foreach ($map as $arg => $target) {
90             if (substr($arg, 0, 2) == '--') {
91                 $longoptions[] = substr($arg, 2) . '=';
92             } else {
93                 $shortoptions .= substr($arg, 1) . ':';
94             }
95         }
96
97         $parser = new Console_Getopt();
98         $result = $parser->getopt($_SERVER['argv'], $shortoptions, $longoptions);
99         if (PEAR::isError($result)) {
100             $this->warning($result->getMessage());
101             return false;
102         }
103         list($options, $args) = $result;
104
105         // defaults
106         $this->dbtype = 'mysql';
107         $this->verbose = true;
108         // ssl is defaulted in lib/installer.php
109
110         foreach ($options as $option) {
111             $arg = $option[0];
112             if (isset($map[$arg])) {
113                 $var = $map[$arg];
114                 $this->$var = $option[1];
115                 if ($arg == '--fancy') {
116                     $this->$var = ($option[1] != 'false') && ($option[1] != 'no');
117                 }
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
126                 return false;
127             }
128         }
129
130         $fail = false;
131         if (empty($this->server)) {
132             $this->updateStatus("You must specify a web server for the site.", true);
133             // path is optional though
134             $fail = true;
135         }
136
137         if (!$this->validateDb()) {
138             $fail = true;
139         }
140
141         if (!$this->validateAdmin()) {
142             $fail = true;
143         }
144
145         return !$fail;
146     }
147
148     public function handle()
149     {
150         return $this->doInstall();
151     }
152
153     public function showHelp()
154     {
155         echo <<<END_HELP
156 install_cli.php - GNU social command-line installer
157
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), 
163                          [never | always]
164
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)
170
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)
176        
177        --site-profile    site profile ['public', 'private' (default), 'community', 'singleuser']
178        
179        --skip-config     Don't write a config.php -- use with caution,
180                          requires a global configuration file.
181
182       General options:
183
184     -q --quiet           Quiet (little output)
185     -v --verbose         Verbose (lots of output)
186     -h --help            Show this message and quit.
187
188 END_HELP;
189     }
190
191     /**
192      * @param string $message
193      * @param string $submessage
194      */
195     public function warning(string $message, string $submessage = '')
196     {
197         print $this->html2text($message) . "\n";
198         if ($submessage != '') {
199             print "  " . $this->html2text($submessage) . "\n";
200         }
201         print "\n";
202     }
203
204     /**
205      * @param string $status
206      * @param bool $error
207      */
208     public function updateStatus(string $status, bool $error = false)
209     {
210         if ($this->verbose || $error) {
211             if ($error) {
212                 print "ERROR: ";
213             }
214             print $this->html2text($status);
215             print "\n";
216         }
217     }
218
219     /**
220      * @param string $html
221      * @return string
222      */
223     private function html2text(string $html): string
224     {
225         // break out any links for text legibility
226         $breakout = preg_replace(
227             '/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
228             '\2 &lt;\1&gt;',
229             $html
230         );
231         return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
232     }
233 }
234
235 $installer = new CliInstaller();
236 $ok = $installer->main();
237 exit($ok ? 0 : 1);