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/postgresql-8.4'))
59 $pgservice='/etc/init.d/postgresql-8.4';
61 elseif (is_file('/etc/init.d/cswpostgres'))
63 $pgservice='/etc/init.d/cswpostgres';
67 die("ERROR: Could not find Postgresql init script\n");
70 # Fedora9 (an maybe newer) requires running initdb
71 if ($pgservice == '/etc/init.d/postgresql') {
72 if (!is_dir("/var/lib/pgsql/data/base")) {
73 run("service postgresql initdb &>/dev/null", true);
77 // Might fail if it's already running, so we'll ingnore the result
78 run("$pgservice start", true);
80 // Where the PHP code will live
81 //$gforge_lib_dir = '/opt/gforge5'; //CAMBIE ESTO
82 $gforge_lib_dir = '/opt/gforge';
84 if (!is_dir($gforge_lib_dir))
86 die("Error: GForge folder doesn't exist. Run fusionforge-install-2.php first.");
90 // Where the configuration files will live
91 $gforge_etc_dir = getenv('GFORGE_ETC_DIR');
92 if (empty($gforge_etc_dir))
94 $gforge_etc_dir = '/etc/gforge';
97 // Where the PGHBA config file is
98 if (is_file("/var/lib/pgsql/data/pg_hba.conf"))
101 $PGHBA='/var/lib/pgsql/data/pg_hba.conf';
103 elseif (is_file('/etc/postgresql/8.2/main/pg_hba.conf'))
105 $PGHBA='/etc/postgresql/8.2/main/pg_hba.conf';
107 elseif (is_file('/etc/postgresql/8.4/main/pg_hba.conf'))
109 $PGHBA='/etc/postgresql/8.4/main/pg_hba.conf';
111 elseif (is_file('/opt/csw/var/pgdata/pg_hba.conf'))
113 $PGHBA='/opt/csw/var/pgdata/pg_hba.conf';
117 die("ERROR: Could not find pg_hba.conf file\n");
121 if (is_file('/usr/share/pgsql/contrib/tsearch2.sql'))
124 $tsearch2_sql='/usr/share/pgsql/contrib/tsearch2.sql';
126 elseif (is_file('/usr/share/postgresql/contrib/tsearch2.sql'))
129 $tsearch2_sql='/usr/share/postgresql/contrib/tsearch2.sql';
131 elseif (is_file('/usr/share/postgresql/8.2/contrib/tsearch2.sql'))
133 $tsearch2_sql='/usr/share/postgresql/8.2/contrib/tsearch2.sql';
135 elseif (is_file('/usr/share/postgresql/8.3/contrib/tsearch2.sql'))
137 $tsearch2_sql='/usr/share/postgresql/8.3/contrib/tsearch2.sql';
139 elseif (is_file('/usr/share/postgresql/8.4/contrib/tsearch2.sql'))
141 $tsearch2_sql='/usr/share/postgresql/8.4/contrib/tsearch2.sql';
143 elseif (is_file('/opt/csw/postgresql/share/contrib/tsearch2.sql'))
146 $tsearch2_sql='/opt/csw/postgresql/share/contrib/tsearch2.sql';
150 die("ERROR: Could not find tsearch2.sql file\n");
156 global $PGHBA, $gforge_lib_dir, $gforge_etc_dir, $tsearch2_sql, $pgservice, $STDIN, $STDOUT;
158 show("\n * Enter the Database Name (gforge): ");
160 if (getenv('FFORGE_DB')) {
161 $gforge_db = getenv('FFORGE_DB');
163 $gforge_db = trim(fgets($STDIN));
164 if (strlen($gforge_db) == 0) {
165 $gforge_db = 'gforge';
168 show(" ...using '$gforge_db'");
170 show(' * Enter the Database Username (gforge): ');
172 if (getenv('FFORGE_USER')) {
173 $gforge_user = getenv('FFORGE_USER');
175 $gforge_user = trim(fgets($STDIN));
176 if (strlen($gforge_user) == 0) {
177 $gforge_user = 'gforge';
180 show(" ...using '$gforge_user'");
182 show(" * Modifying DB Access Permissions...");
183 if (!file_exists("$PGHBA.fforge.backup")) {
184 run("cp $PGHBA $PGHBA.fforge.backup", true);
186 run("echo \"# GFORGE\nlocal all all trust\" > $PGHBA");
187 show(' * Restarting PostgreSQL...');
188 run("$pgservice stop", true);
189 run("$pgservice start");
192 show(" * Creating '$gforge_user' Group...");
193 run("/usr/sbin/groupadd $gforge_user", true);
195 show(" * Creating '$gforge_user' User...");
196 run("/usr/sbin/useradd -g $gforge_user $gforge_user", true);
198 show(" * Creating Database User '$gforge_user'...");
199 run("su - postgres -c \"createuser -A -R -d -E $gforge_user\"", true);
201 show(' * Creating Language...');
202 run("su - postgres -c \"createlang plpgsql template1\"", true);
204 if (!is_dir("/home/$gforge_user")) {
210 show(" * Creating '$gforge_db' Database...");
211 run("su $susufix $gforge_user -c \"createdb --encoding UNICODE $gforge_db\"", true);
213 # Detect postgresql version, load tsearch2 for pg < 8.3
214 $pg_version = explode(' ', shell_exec("postgres --version"));
215 $pgv = $pg_version[2];
217 if (preg_match('/^(7\.|8\.1|8\.2)/', $pgv)) {
218 show(" * Dumping tsearch2 Database Into '$gforge_db' DB");
219 run("su - postgres -c \"psql $gforge_db < $tsearch2_sql\" >> /tmp/gforge-import.log");
221 $tables = array('pg_ts_cfg', 'pg_ts_cfgmap', 'pg_ts_dict', 'pg_ts_parser');
222 foreach ($tables as $table) {
223 run('su - postgres -c "psql '.$gforge_db.' -c \\"GRANT ALL on '.$table.' TO '.$gforge_user.';\\""');
226 // show(" * Creating FTS default configuation (Full Text Search)");
227 // run("su - postgres -c \"psql $gforge_db < $gforge_lib_dir/db/FTS-20081108.sql\" >> /tmp/gforge-import.log");
231 show(' * Dumping FusionForge DB');
232 run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/gforge.sql\" >> /tmp/gforge-import.log");
234 // show(' * Dumping FusionForge FTI DB');
235 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI.sql\" >> /tmp/gforge-import.log");
236 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20050315.sql\" >> /tmp/gforge-import.log");
237 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20050401.sql\" >> /tmp/gforge-import.log");
238 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20050530.sql\" >> /tmp/gforge-import.log");
239 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20060130.sql\" >> /tmp/gforge-import.log");
240 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20061025.sql\" >> /tmp/gforge-import.log");
242 show(" * Enter the Admin Username (fforgeadmin): ");
243 if (getenv('FFORGE_ADMIN_USER')) {
244 $admin_user = getenv('FFORGE_ADMIN_USER');
246 $admin_user = trim(fgets($STDIN));
248 if (strlen($admin_user) == 0) {
249 $admin_user = 'fforgeadmin';
252 show(" ...using '$admin_user'");
254 if (getenv('FFORGE_ADMIN_PASSWORD')) {
256 $pwd1 = getenv('FFORGE_ADMIN_PASSWORD');
263 while ($bad_pwd && $retries < 5) {
264 if ($bad_pwd && $retries > 0) {
265 show(' * ' . $error);
267 $pwd1 = readMasked(" * Enter the Site Admin Password:");
268 $error = validatePassword($pwd1);
272 $pwd2 = readMasked(" * Please enter it again: \n");
273 if ($pwd1 == $pwd2) {
276 $error = 'Passwords don\'t match. Please try again.';
284 show('Passwords didn\'t match! Aborting.');
287 $pw_md5 = md5($pwd1);
288 $pw_crypt = crypt($pwd1);
289 $pw_crypt = str_replace('$', '\\\\\\$', $pw_crypt);
290 //run( 'su - postgres -c "psql ' .
292 // ' -c \\"UPDATE \\\\\"user\\\\\" SET unix_name=\'' .
293 // $admin_user . '\', password_md5=\'' .
294 // $pw_md5 . '\', password_crypt=\'' .
295 // $pw_crypt . '\' WHERE user_id=101;\\""'); // MODIFIQUE ESTO
297 //run( 'su - postgres -c "psql ' .
299 // ' -c \\"UPDATE \\\\\"users\\\\\" SET user_name=\'' .
300 // $admin_user . '\', user_pw=\'' .
301 // $pw_md5 . '\', unix_pw=\'' .
302 // $pw_crypt . '\' WHERE user_id=101;\\""');
303 //echo "BREAKPOINT 1\n";
304 //$t = trim(fgets($STDIN));
306 // run("su - postgres -c \"psql $gforge_db -c \\\"INSERT INTO users (user_name, user_pw, unix_pw) VALUES ('$admin_user', '$pw_md5', '$pw_crypt')\\\"\"");
307 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')\\\"\"");
309 //echo "BREAKPOINT 2\n";
310 //$t = trim(fgets($STDIN));
312 // 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')\\\"\"" );
314 //echo "BREAKPOINT 3\n";
315 //$t = trim(fgets($STDIN));
318 if (!is_dir($gforge_etc_dir)) {
319 mkdir($gforge_etc_dir);
323 show(' * Saving database configuration in FForge config file');
324 $data = file_get_contents("$gforge_etc_dir/local.inc");
325 $lines = explode("\n",$data);
327 foreach ($lines as $l) {
328 $l = preg_replace("/^.sys_dbname\s*=\s*'(.*)'/", "\$sys_dbname='$gforge_db'", $l);
329 $l = preg_replace("/^.sys_dbuser\s*=\s*'(.*)'/", "\$sys_dbuser='$gforge_user'", $l);
333 if ($fp = fopen("$gforge_etc_dir/local.inc", "w")) {
334 fwrite ($fp, $config);
338 show(' * Saving installation log in /tmp/gforge-import.log');
341 function uninstall() {
342 global $PGHBA, $gforge_lib_dir, $gforge_var_dir, $gforge_etc_dir, $gforge_db, $gforge_user, $tsearch2_sql;
344 show(" * Removing DATABASE \n";
345 system("su - $gforge_user -c \"dropdb $gforge_db\"", $ret );
346 show(" done . ($ret)\n";
348 show(" * Removing Language \n";
349 system("su - postgres -c \"droplang plpgsql template1\"", $ret );
350 show(" done. ($ret)\n";
352 show(" * Removing GForge DATABASE User: \n";
353 system("su - postgres -c \"dropuser $gforge_user\"", $ret );
354 show(" done.($ret)");
356 show(" * Removing GForge User: \n";
357 system("userdel $gforge_user");
360 show(" * Restoring $PGHBA file: ... ";
361 system("cp $PGHBA.gforge.backup $PGHBA");
364 show(" * Restarting PostgreSQL: ...\n";
365 system("/etc/init.d/postgresql restart");
370 function validatePassword($password) {
371 if (strlen($password)<6) {
372 return 'Password is too short. Please try again.';
374 if (!preg_match('/[[:alnum:]]*/', $password)) {
375 return 'Password contains invalid characters. Please try again.';
380 function readMasked($prompt) {
382 if (strtolower(php_uname('s')) == 'sunos') {
384 $text_entered = fgets($STDIN);
386 $options="-er -s -p";
387 $returned=popen("read $options \"".GREEN.$prompt.NORMAL."\n\"; echo \$REPLY", 'r');
388 $text_entered=fgets($returned, 100);
390 $text_entered=substr($text_entered, 0, strlen($text_entered));
394 return trim($text_entered);
398 show(NORMAL."Done.\nYou are ready to run install-gforge-3.php");
401 function show($text, $newLine = true) {
404 $hd = fopen ("/tmp/gforge-import.log", "a+");
405 fwrite($hd, "*** $text\n");
409 $text = GREEN.$text .NORMAL."\n";
411 fwrite($STDOUT, $text);
414 function run($command, $ignore = false) {
416 $hd = fopen ("/tmp/gforge-import.log", "a+");
417 fwrite($hd, "CMD ".$command."\n");
420 system($command, $ret);
429 echo RED.'An error ocurred running the last command... aborting.'.NORMAL."\n";