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