4 * FusionForge Installation Dependency Setup
6 * Copyright 2006 GForge, LLC
11 * This file is part of GInstaller, it is called by install.sh.
13 * GForge is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * GForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with GForge; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
29 define ('GREEN', "\033[01;32m" );
30 define ('NORMAL', "\033[00m" );
31 define ('RED', "\033[01;31m" );
33 $STDOUT = fopen('php://stdout','w');
34 $STDIN = fopen('php://stdin','r');
36 show("\n-=# Welcome to FusionForge DB-Installer #=-");
38 //TO DO: add dependency check
39 //if (!run("php check-deps.php", true)) {
40 // echo RED.'Not all the necessary dependencies were found. aborting.'.NORMAL."\n";
44 // Make sure the DB is initialized by starting postgresql service
45 if (is_file('/etc/init.d/postgresql'))
47 $pgservice='/etc/init.d/postgresql';
49 elseif (is_file('/etc/init.d/postgresql-8.2'))
51 $pgservice='/etc/init.d/postgresql-8.2';
53 elseif (is_file('/etc/init.d/postgresql-8.3'))
55 $pgservice='/etc/init.d/postgresql-8.3';
57 elseif (is_file('/etc/init.d/cswpostgres'))
59 $pgservice='/etc/init.d/cswpostgres';
63 die("ERROR: Could not find Postgresql init script\n");
66 # Fedora9 (an maybe newer) requires running initdb
67 if ($pgservice == '/etc/init.d/postgresql') {
68 if (!is_dir("/var/lib/pgsql/data/base")) {
69 run("service postgresql initdb &>/dev/null", true);
73 // Might fail if it's already running, so we'll ingnore the result
74 run("$pgservice start", true);
76 // Where the PHP code will live
77 //$gforge_lib_dir = '/opt/gforge5'; //CAMBIE ESTO
78 $gforge_lib_dir = '/opt/gforge';
80 if (!is_dir($gforge_lib_dir))
82 die("Error: GForge folder doesn't exist. Run gforge-install-2.php first.");
86 // Where the configuration files will live
87 $gforge_etc_dir = getenv('GFORGE_ETC_DIR');
88 if (empty($gforge_etc_dir))
90 $gforge_etc_dir = '/etc/gforge';
93 // Where the PGHBA config file is
94 if (is_file("/var/lib/pgsql/data/pg_hba.conf"))
97 $PGHBA='/var/lib/pgsql/data/pg_hba.conf';
99 elseif (is_file('/etc/postgresql/8.2/main/pg_hba.conf'))
101 $PGHBA='/etc/postgresql/8.2/main/pg_hba.conf';
103 elseif (is_file('/opt/csw/var/pgdata/pg_hba.conf'))
105 $PGHBA='/opt/csw/var/pgdata/pg_hba.conf';
109 die("ERROR: Could not find pg_hba.conf file\n");
113 if (is_file('/usr/share/pgsql/contrib/tsearch2.sql'))
116 $tsearch2_sql='/usr/share/pgsql/contrib/tsearch2.sql';
118 elseif (is_file('/usr/share/postgresql/contrib/tsearch2.sql'))
121 $tsearch2_sql='/usr/share/postgresql/contrib/tsearch2.sql';
123 elseif (is_file('/usr/share/postgresql/8.2/contrib/tsearch2.sql'))
125 $tsearch2_sql='/usr/share/postgresql/8.2/contrib/tsearch2.sql';
127 elseif (is_file('/opt/csw/postgresql/share/contrib/tsearch2.sql'))
130 $tsearch2_sql='/opt/csw/postgresql/share/contrib/tsearch2.sql';
134 die("ERROR: Could not find tsearch2.sql file\n");
140 global $PGHBA, $gforge_lib_dir, $gforge_etc_dir, $tsearch2_sql, $pgservice, $STDIN, $STDOUT;
142 show("\n * Enter the Database Name (gforge): ");
144 if (getenv('FFORGE_DB')) {
145 $gforge_db = getenv('FFORGE_DB');
147 $gforge_db = trim(fgets($STDIN));
148 if (strlen($gforge_db) == 0) {
149 $gforge_db = 'gforge';
152 show(" ...using '$gforge_db'");
154 show(' * Enter the Database Username (gforge): ');
156 if (getenv('FFORGE_USER')) {
157 $gforge_user = getenv('FFORGE_USER');
159 $gforge_user = trim(fgets($STDIN));
160 if (strlen($gforge_user) == 0) {
161 $gforge_user = 'gforge';
164 show(" ...using '$gforge_user'");
166 show(" * Modifying DB Access Permissions...");
167 if (!file_exists("$PGHBA.fforge.backup")) {
168 run("cp $PGHBA $PGHBA.fforge.backup", true);
170 run("echo \"# GFORGE\nlocal all all trust\" > $PGHBA");
171 show(' * Restarting PostgreSQL...');
172 run("$pgservice stop", true);
173 run("$pgservice start");
176 show(" * Creating '$gforge_user' Group...");
177 run("/usr/sbin/groupadd $gforge_user", true);
179 show(" * Creating '$gforge_user' User...");
180 run("/usr/sbin/useradd -g $gforge_user $gforge_user", true);
182 show(" * Creating Database User '$gforge_user'...");
183 run("su - postgres -c \"createuser -A -R -d -E $gforge_user\"", true);
185 show(' * Creating Language...');
186 run("su - postgres -c \"createlang plpgsql template1\"", true);
188 if (!is_dir("/home/$gforge_user")) {
194 show(" * Creating '$gforge_db' Database...");
195 run("su $susufix $gforge_user -c \"createdb --encoding UNICODE $gforge_db\"", true);
197 # Detect postgresql version, load tsearch2 for pg < 8.3
198 $pg_version = explode(' ', shell_exec("postgres --version"));
199 $pgv = $pg_version[2];
201 if (preg_match('/^(7\.|8\.1|8\.2)/', $pgv)) {
202 show(" * Dumping tsearch2 Database Into '$gforge_db' DB");
203 run("su - postgres -c \"psql $gforge_db < $tsearch2_sql\" >> /tmp/gforge-import.log");
205 $tables = array('pg_ts_cfg', 'pg_ts_cfgmap', 'pg_ts_dict', 'pg_ts_parser');
206 foreach ($tables as $table) {
207 run('su - postgres -c "psql '.$gforge_db.' -c \\"GRANT ALL on '.$table.' TO '.$gforge_user.';\\""');
210 // show(" * Creating FTS default configuation (Full Text Search)");
211 // run("su - postgres -c \"psql $gforge_db < $gforge_lib_dir/db/FTS-20081108.sql\" >> /tmp/gforge-import.log");
215 show(' * Dumping FusionForge DB');
216 run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/gforge.sql\" >> /tmp/gforge-import.log");
218 // show(' * Dumping FusionForge FTI DB');
219 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI.sql\" >> /tmp/gforge-import.log");
220 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20050315.sql\" >> /tmp/gforge-import.log");
221 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20050401.sql\" >> /tmp/gforge-import.log");
222 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20050530.sql\" >> /tmp/gforge-import.log");
223 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20060130.sql\" >> /tmp/gforge-import.log");
224 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20061025.sql\" >> /tmp/gforge-import.log");
226 show(" * Enter the Admin Username (fforgeadmin): ");
227 if (getenv('FFORGE_ADMIN_USER')) {
228 $admin_user = getenv('FFORGE_ADMIN_USER');
230 $admin_user = trim(fgets($STDIN));
232 if (strlen($admin_user) == 0) {
233 $admin_user = 'fforgeadmin';
236 show(" ...using '$admin_user'");
238 if (getenv('FFORGE_ADMIN_PASSWORD')) {
240 $pwd1 = getenv('FFORGE_ADMIN_PASSWORD');
247 while ($bad_pwd && $retries < 5) {
248 if ($bad_pwd && $retries > 0) {
249 show(' * ' . $error);
251 $pwd1 = readMasked(" * Enter the Site Admin Password:");
252 $error = validatePassword($pwd1);
256 $pwd2 = readMasked(" * Please enter it again: \n");
257 if ($pwd1 == $pwd2) {
260 $error = 'Passwords don\'t match. Please try again.';
268 show('Passwords didn\'t match! Aborting.');
271 $pw_md5 = md5($pwd1);
272 $pw_crypt = crypt($pwd1);
273 $pw_crypt = str_replace('$', '\\\\\\$', $pw_crypt);
274 //run( 'su - postgres -c "psql ' .
276 // ' -c \\"UPDATE \\\\\"user\\\\\" SET unix_name=\'' .
277 // $admin_user . '\', password_md5=\'' .
278 // $pw_md5 . '\', password_crypt=\'' .
279 // $pw_crypt . '\' WHERE user_id=101;\\""'); // MODIFIQUE ESTO
281 //run( 'su - postgres -c "psql ' .
283 // ' -c \\"UPDATE \\\\\"users\\\\\" SET user_name=\'' .
284 // $admin_user . '\', user_pw=\'' .
285 // $pw_md5 . '\', unix_pw=\'' .
286 // $pw_crypt . '\' WHERE user_id=101;\\""');
287 //echo "BREAKPOINT 1\n";
288 //$t = trim(fgets($STDIN));
290 // run("su - postgres -c \"psql $gforge_db -c \\\"INSERT INTO users (user_name, user_pw, unix_pw) VALUES ('$admin_user', '$pw_md5', '$pw_crypt')\\\"\"");
291 run("su - postgres -c \"psql $gforge_db -c \\\"INSERT INTO users (user_name, email, user_pw, unix_pw, status, theme_id) VALUES ('$admin_user', 'root@localhost.localdomain', '$pw_md5', '$pw_crypt', 'A', 1); INSERT INTO user_group (user_id, group_id, admin_flags) VALUES (currval('users_pk_seq'), 1, 'A')\\\"\"");
293 //echo "BREAKPOINT 2\n";
294 //$t = trim(fgets($STDIN));
296 // run("su - postgres -c \"psql $gforge_db -c \\\"INSERT INTO user_group (user_id, group_id, admin_flags) VALUES (currval('users_pk_seq'), 1, 'A')\\\"\"" );
298 //echo "BREAKPOINT 3\n";
299 //$t = trim(fgets($STDIN));
302 if (!is_dir($gforge_etc_dir)) {
303 mkdir($gforge_etc_dir);
307 show(' * Saving database configuration in FForge config file');
308 $data = file_get_contents("$gforge_etc_dir/local.inc");
309 $lines = explode("\n",$data);
311 foreach ($lines as $l) {
312 $l = preg_replace("/^.sys_dbname\s*=\s*'(.*)'/", "\$sys_dbname='$gforge_db'", $l);
313 $l = preg_replace("/^.sys_dbuser\s*=\s*'(.*)'/", "\$sys_dbuser='$gforge_user'", $l);
317 if ($fp = fopen("$gforge_etc_dir/local.inc", "w")) {
318 fwrite ($fp, $config);
322 show(' * Saving installation log in /tmp/gforge-import.log');
325 function uninstall() {
326 global $PGHBA, $gforge_lib_dir, $gforge_var_dir, $gforge_etc_dir, $gforge_db, $gforge_user, $tsearch2_sql;
328 show(" * Removing DATABASE \n";
329 system("su - $gforge_user -c \"dropdb $gforge_db\"", $ret );
330 show(" done . ($ret)\n";
332 show(" * Removing Language \n";
333 system("su - postgres -c \"droplang plpgsql template1\"", $ret );
334 show(" done. ($ret)\n";
336 show(" * Removing GForge DATABASE User: \n";
337 system("su - postgres -c \"dropuser $gforge_user\"", $ret );
338 show(" done.($ret)");
340 show(" * Removing GForge User: \n";
341 system("userdel $gforge_user");
344 show(" * Restoring $PGHBA file: ... ";
345 system("cp $PGHBA.gforge.backup $PGHBA");
348 show(" * Restarting PostgreSQL: ...\n";
349 system("/etc/init.d/postgresql restart");
354 function validatePassword($password) {
355 if (strlen($password)<6) {
356 return 'Password is too short. Please try again.';
358 if (!preg_match('/[[:alnum:]]*/', $password)) {
359 return 'Password contains invalid characters. Please try again.';
364 function readMasked($prompt) {
366 if (strtolower(php_uname('s')) == 'sunos') {
368 $text_entered = fgets($STDIN);
370 $options="-er -s -p";
371 $returned=popen("read $options \"".GREEN.$prompt.NORMAL."\n\"; echo \$REPLY", 'r');
372 $text_entered=fgets($returned, 100);
374 $text_entered=substr($text_entered, 0, strlen($text_entered));
378 return trim($text_entered);
382 show(NORMAL."Done.\nYou are ready to run install-gforge-3.php");
385 function show($text, $newLine = true) {
388 $hd = fopen ("/tmp/gforge-import.log", "a+");
389 fwrite($hd, "*** $text\n");
393 $text = GREEN.$text .NORMAL."\n";
395 fwrite($STDOUT, $text);
398 function run($command, $ignore = false) {
400 $hd = fopen ("/tmp/gforge-import.log", "a+");
401 fwrite($hd, "CMD ".$command."\n");
404 system($command, $ret);
413 echo RED.'An error ocurred running the last command... aborting.'.NORMAL."\n";