4 * FusionForge Installation Dependency Setup
6 * Copyright 2006 GForge, LLC
11 * This file is part of GInstaller
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 v4.7 #=-");
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/cswpostgres'))
55 $pgservice='/etc/init.d/cswpostgres';
59 die("ERROR: Could not find Postgresql init script\n");
62 # Fedora9 (an maybe newer) requires running initdb
63 if ($pgservice == '/etc/init.d/postgresql') {
64 if (!is_dir("/var/lib/pgsql/data/base")) {
65 run("service postgresql initdb &>/dev/null", true);
69 // Might fail if it's already running, so we'll ingnore the result
70 run("$pgservice start", true);
72 // Where the PHP code will live
73 //$gforge_lib_dir = '/opt/gforge5'; //CAMBIE ESTO
74 $gforge_lib_dir = '/opt/gforge';
76 if (!is_dir($gforge_lib_dir))
78 die("Error: GForge folder doesn't exist. Run gforge-install-2.php first.");
82 // Where the configuration files will live
83 $gforge_etc_dir = getenv('GFORGE_ETC_DIR');
84 if (empty($gforge_etc_dir))
86 $gforge_etc_dir = '/etc/gforge';
89 // Where the PGHBA config file is
90 if (is_file("/var/lib/pgsql/data/pg_hba.conf"))
93 $PGHBA='/var/lib/pgsql/data/pg_hba.conf';
95 elseif (is_file('/etc/postgresql/8.2/main/pg_hba.conf'))
97 $PGHBA='/etc/postgresql/8.2/main/pg_hba.conf';
99 elseif (is_file('/opt/csw/var/pgdata/pg_hba.conf'))
101 $PGHBA='/opt/csw/var/pgdata/pg_hba.conf';
105 die("ERROR: Could not find pg_hba.conf file\n");
109 if (is_file('/usr/share/pgsql/contrib/tsearch2.sql'))
112 $tsearch2_sql='/usr/share/pgsql/contrib/tsearch2.sql';
114 elseif (is_file('/usr/share/postgresql/contrib/tsearch2.sql'))
117 $tsearch2_sql='/usr/share/postgresql/contrib/tsearch2.sql';
119 elseif (is_file('/usr/share/postgresql/8.2/contrib/tsearch2.sql'))
121 $tsearch2_sql='/usr/share/postgresql/8.2/contrib/tsearch2.sql';
123 elseif (is_file('/opt/csw/postgresql/share/contrib/tsearch2.sql'))
126 $tsearch2_sql='/opt/csw/postgresql/share/contrib/tsearch2.sql';
130 die("ERROR: Could not find tsearch2.sql file\n");
136 global $PGHBA, $gforge_lib_dir, $gforge_etc_dir, $tsearch2_sql, $pgservice, $STDIN, $STDOUT;
138 show("\n * Enter the Database Name (gforge): ");
140 if (getenv('FFORGE_DB')) {
141 $gforge_db = getenv('FFORGE_DB');
143 $gforge_db = trim(fgets($STDIN));
144 if (strlen($gforge_db) == 0) {
145 $gforge_db = 'gforge';
148 show(" ...using '$gforge_db'");
150 show(' * Enter the Database Username (gforge): ');
152 if (getenv('FFORGE_USER')) {
153 $gforge_user = getenv('FFORGE_USER');
155 $gforge_user = trim(fgets($STDIN));
156 if (strlen($gforge_user) == 0) {
157 $gforge_user = 'gforge';
160 show(" ...using '$gforge_user'");
162 show(" * Modifying DB Access Permissions...");
163 if (!file_exists("$PGHBA.fforge.backup")) {
164 run("cp $PGHBA $PGHBA.fforge.backup", true);
166 run("echo \"# GFORGE\nlocal all all trust\" > $PGHBA");
167 show(' * Restarting PostgreSQL...');
168 run("$pgservice stop", true);
169 run("$pgservice start");
172 show(" * Creating '$gforge_user' Group...");
173 run("/usr/sbin/groupadd $gforge_user", true);
175 show(" * Creating '$gforge_user' User...");
176 run("/usr/sbin/useradd -g $gforge_user $gforge_user", true);
178 show(" * Creating Database User '$gforge_user'...");
179 run("su - postgres -c \"createuser -A -R -d -E $gforge_user\"", true);
181 show(' * Creating Language...');
182 run("su - postgres -c \"createlang plpgsql template1\"", true);
184 if (!is_dir("/home/$gforge_user")) {
190 show(" * Creating '$gforge_db' Database...");
191 run("su $susufix $gforge_user -c \"createdb $gforge_db\"", true);
193 // # Detect postgresql version, load tsearch2 for pg < 8.3
194 // $pg_version = explode(' ', shell_exec("postgres --version"));
195 // $pgv = $pg_version[2];
197 // if (preg_match('/^(7\.|8\.1|8\.2)/', $pgv)) {
198 // show(" * Dumping tsearch2 Database Into '$gforge_db' DB");
199 // run("su - postgres -c \"psql $gforge_db < $tsearch2_sql\" >> /tmp/gforge-import.log");
201 // $tables = array('pg_ts_cfg', 'pg_ts_cfgmap', 'pg_ts_dict', 'pg_ts_parser');
202 // foreach ($tables as $table) {
203 // run('su - postgres -c "psql '.$gforge_db.' -c \\"GRANT ALL on '.$table.' TO '.$gforge_user.';\\""');
206 // show(" * Creating FTS default configuation (Full Text Search)");
207 // run("su - postgres -c \"psql $gforge_db < $gforge_lib_dir/db/FTS-20081108.sql\" >> /tmp/gforge-import.log");
211 show(' * Dumping FusionForge DB');
212 run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/gforge.sql\" >> /tmp/gforge-import.log");
214 // show(' * Dumping FusionForge FTI DB');
215 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI.sql\" >> /tmp/gforge-import.log");
216 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20050315.sql\" >> /tmp/gforge-import.log");
217 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20050401.sql\" >> /tmp/gforge-import.log");
218 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20050530.sql\" >> /tmp/gforge-import.log");
219 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20060130.sql\" >> /tmp/gforge-import.log");
220 // run("su $susufix $gforge_user -c \"psql $gforge_db < $gforge_lib_dir/db/FTI-20061025.sql\" >> /tmp/gforge-import.log");
222 show(" * Enter the Admin Username (fforgeadmin): ");
223 if (getenv('FFORGE_ADMIN_USER')) {
224 $admin_user = getenv('FFORGE_ADMIN_USER');
226 $admin_user = trim(fgets($STDIN));
228 if (strlen($admin_user) == 0) {
229 $admin_user = 'fforgeadmin';
232 show(" ...using '$admin_user'");
234 if (getenv('FFORGE_ADMIN_PASSWORD')) {
236 $pwd1 = getenv('FFORGE_ADMIN_PASSWORD');
243 while ($bad_pwd && $retries < 5) {
244 if ($bad_pwd && $retries > 0) {
245 show(' * ' . $error);
247 $pwd1 = readMasked(" * Enter the Site Admin Password:");
248 $error = validatePassword($pwd1);
252 $pwd2 = readMasked(" * Please enter it again: \n");
253 if ($pwd1 == $pwd2) {
256 $error = 'Passwords don\'t match. Please try again.';
264 show('Passwords didn\'t match! Aborting.');
267 $pw_md5 = md5($pwd1);
268 $pw_crypt = crypt($pwd1);
269 $pw_crypt = str_replace('$', '\\\\\\$', $pw_crypt);
270 //run( 'su - postgres -c "psql ' .
272 // ' -c \\"UPDATE \\\\\"user\\\\\" SET unix_name=\'' .
273 // $admin_user . '\', password_md5=\'' .
274 // $pw_md5 . '\', password_crypt=\'' .
275 // $pw_crypt . '\' WHERE user_id=101;\\""'); // MODIFIQUE ESTO
277 //run( 'su - postgres -c "psql ' .
279 // ' -c \\"UPDATE \\\\\"users\\\\\" SET user_name=\'' .
280 // $admin_user . '\', user_pw=\'' .
281 // $pw_md5 . '\', unix_pw=\'' .
282 // $pw_crypt . '\' WHERE user_id=101;\\""');
283 //echo "BREAKPOINT 1\n";
284 //$t = trim(fgets($STDIN));
286 // run("su - postgres -c \"psql $gforge_db -c \\\"INSERT INTO users (user_name, user_pw, unix_pw) VALUES ('$admin_user', '$pw_md5', '$pw_crypt')\\\"\"");
287 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')\\\"\"");
289 //echo "BREAKPOINT 2\n";
290 //$t = trim(fgets($STDIN));
292 // 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')\\\"\"" );
294 //echo "BREAKPOINT 3\n";
295 //$t = trim(fgets($STDIN));
298 if (!is_dir($gforge_etc_dir)) {
299 mkdir($gforge_etc_dir);
303 show(' * Saving database configuration in FForge config file');
304 $data = file_get_contents("$gforge_etc_dir/local.inc");
305 $lines = explode("\n",$data);
307 foreach ($lines as $l) {
308 $l = preg_replace("/^.sys_dbname\s*=\s*'(.*)'/", "\$sys_dbname='$gforge_db'", $l);
309 $l = preg_replace("/^.sys_dbuser\s*=\s*'(.*)'/", "\$sys_dbuser='$gforge_user'", $l);
313 if ($fp = fopen("$gforge_etc_dir/local.inc", "w")) {
314 fwrite ($fp, $config);
318 show(' * Saving installation log in /tmp/gforge-import.log');
321 function uninstall() {
322 global $PGHBA, $gforge_lib_dir, $gforge_var_dir, $gforge_etc_dir, $gforge_db, $gforge_user, $tsearch2_sql;
324 show(" * Removing DATABASE \n";
325 system("su - $gforge_user -c \"dropdb $gforge_db\"", $ret );
326 show(" done . ($ret)\n";
328 show(" * Removing Language \n";
329 system("su - postgres -c \"droplang plpgsql template1\"", $ret );
330 show(" done. ($ret)\n";
332 show(" * Removing GForge DATABASE User: \n";
333 system("su - postgres -c \"dropuser $gforge_user\"", $ret );
334 show(" done.($ret)");
336 show(" * Removing GForge User: \n";
337 system("userdel $gforge_user");
340 show(" * Restoring $PGHBA file: ... ";
341 system("cp $PGHBA.gforge.backup $PGHBA");
344 show(" * Restarting PostgreSQL: ...\n";
345 system("/etc/init.d/postgresql restart");
350 function validatePassword($password) {
351 if (strlen($password)<6) {
352 return 'Password is too short. Please try again.';
354 if (!preg_match('/[[:alnum:]]*/', $password)) {
355 return 'Password contains invalid characters. Please try again.';
360 function readMasked($prompt) {
362 if (strtolower(php_uname('s')) == 'sunos') {
364 $text_entered = fgets($STDIN);
366 $options="-er -s -p";
367 $returned=popen("read $options \"".GREEN.$prompt.NORMAL."\n\"; echo \$REPLY", 'r');
368 $text_entered=fgets($returned, 100);
370 $text_entered=substr($text_entered, 0, strlen($text_entered));
374 return trim($text_entered);
378 show(NORMAL."Done.\nYou are ready to run install-gforge-3.php");
381 function show($text, $newLine = true) {
384 $hd = fopen ("/tmp/gforge-import.log", "a+");
385 fwrite($hd, "*** $text\n");
389 $text = GREEN.$text .NORMAL."\n";
391 fwrite($STDOUT, $text);
394 function run($command, $ignore = false) {
396 $hd = fopen ("/tmp/gforge-import.log", "a+");
397 fwrite($hd, "CMD ".$command."\n");
400 system($command, $ret);
409 echo RED.'An error ocurred running the last command... aborting.'.NORMAL."\n";