/dev/null 2>&1');
$arch = ((-1^0xffffffff) ? 'x86_64' : 'i386');
//Arguments
$GLOBALS['pretend'] = false;
foreach($argv as $value) {
if($value == '-p' || $value == '--pretend') {
$GLOBALS['pretend'] = true;
}
}
//Check if distribution is supported
$GLOBALS['osData'] = getOs();
//Ubuntu
if ($GLOBALS['osData']['DISTRIB_ID'] == 'Ubuntu') {
if($GLOBALS['osData']['DISTRIB_RELEASE'] != '10.04' && $GLOBALS['osData']['DISTRIB_RELEASE'] != '12.04') {
error("{$osData['DISTRIB_ID']} {$GLOBALS['osData']['DISTRIB_RELEASE']} is not supported\n");
}
//CentOS, RHEL
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'CentOS' || $GLOBALS['osData']['DISTRIB_ID'] == 'RHEL') {
if((intval($GLOBALS['osData']['DISTRIB_RELEASE'][0]) != 5 && intval($GLOBALS['osData']['DISTRIB_RELEASE'][2]) < 6) &&
intval($GLOBALS['osData']['DISTRIB_RELEASE'][0]) != 6
) {
error("{$osData['DISTRIB_ID']} {$GLOBALS['osData']['DISTRIB_RELEASE']} is not supported\n");
}
//openSUSE, SLE
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'openSUSE' || $GLOBALS['osData']['DISTRIB_ID'] == 'SLE') {
if($GLOBALS['osData']['DISTRIB_RELEASE'] != '11' &&
$GLOBALS['osData']['DISTRIB_RELEASE'] != '11.4' &&
$GLOBALS['osData']['DISTRIB_RELEASE'] != '12.1' &&
$GLOBALS['osData']['DISTRIB_RELEASE'] != '12.2') {
error("{$osData['DISTRIB_ID']} {$GLOBALS['osData']['DISTRIB_RELEASE']} is not supported\n");
}
//Debian
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'Debian') {
if($GLOBALS['osData']['DISTRIB_RELEASE'] != '6') {
error("{$osData['DISTRIB_ID']} {$GLOBALS['osData']['DISTRIB_RELEASE']} is not supported\n");
}
} else {
error("{$osData['DISTRIB_ID']} is not supported\n");
}
//Check if root
if ($_SERVER['USER'] != 'root') {
error("You need to run the install with root privileges\n");
}
//Check
fwrite(STDOUT, "Giscuit setup will
* Add and update packages
* Add repositories
* Add software that is not distributed via package managers like IonCube and Giscuit it self
* Allow PostgreSQL users to authenticate using a md5 password from localhost
Continue installation ? (yes/no)\n");
$stdin = trim(fgets(STDIN));
if(strtolower($stdin) != 'yes' && strtolower($stdin) != 'y') {
return;
}
//Install name
$GLOBALS['config']['installName'] = 'giscuit';
fwrite(STDOUT, "Install name ({$GLOBALS['config']['installName']}):\n");
$stdin = trim(fgets(STDIN));
if(! empty($stdin)) {
$GLOBALS['config']['installName'] = trim($stdin);
}
if(! ctype_alnum($GLOBALS['config']['installName'])) {
error("Only alphanumeric characters are allowed for install name");
}
if(file_exists("/etc/apache2/sites-available/{$GLOBALS['config']['installName']}") ||
file_exists("/etc/httpd/conf.d/{$GLOBALS['config']['installName']}.conf")) {
error("Virtual host ({$GLOBALS['config']['installName']}) already exists");
}
$GLOBALS['config']['installPath'] .= '/' . $GLOBALS['config']['installName'];
//Install path
fwrite(STDOUT, "Install path ({$GLOBALS['config']['installPath']}):\n");
$stdin = trim(fgets(STDIN));
if(! empty($stdin)) {
$GLOBALS['config']['installPath'] = rtrim(trim($stdin), '/');
}
if(file_exists($GLOBALS['config']['installPath'])) {
error("Install path ({$GLOBALS['config']['installPath']}) already exists");
}
//Database name
$dbName = $GLOBALS['config']['installName'];
fwrite(STDOUT, "NOTICE: This will create a new database\nDatabase name ($dbName):\n");
$stdin = trim(fgets(STDIN));
if(! empty($stdin)) {
$dbName = $stdin;
}
if(! ctype_alnum($dbName)) {
error("Only alphanumeric characters are allowed for database name");
}
if(database_exists($dbName)) {
error("Database name ($dbName) already exists");
}
//Database user
$dbUser = $GLOBALS['config']['installName'];
fwrite(STDOUT, "NOTICE: This will create a new database user\nDatabase username ($dbUser):\n");
$stdin = trim(fgets(STDIN));
if(! empty($stdin)) {
$dbUser = $stdin;
}
if(! ctype_alnum($dbUser)) {
error("Only alphanumeric characters are allowed for database username");
}
//Database password
fwrite(STDOUT, "Database password:\n");
system('stty -echo');
$dbPassword = trim(fgets(STDIN));
system('stty echo');
fwrite(STDOUT, "Repeat database password:\n");
system('stty -echo');
$dbPasswordRepeat = trim(fgets(STDIN));
system('stty echo');
if(empty($dbPassword)) {
error('Password is required');
}
if($dbPasswordRepeat != $dbPassword) {
error('Passwords do not match');
}
//Virtual host
if ($GLOBALS['osData']['DISTRIB_ID'] == 'Ubuntu' || $GLOBALS['osData']['DISTRIB_ID'] == 'Debian') {
$apacheAccessLog = '/var/log/apache2/access.log';
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'CentOS' || $GLOBALS['osData']['DISTRIB_ID'] == 'RHEL') {
$apacheAccessLog = '/var/log/httpd/access_log';
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'openSUSE' || $GLOBALS['osData']['DISTRIB_ID'] == 'SLE') {
$apacheAccessLog = '/var/log/apache2/access_log';
}
$virtualHost = "
ServerAdmin webmaster@localhost
#ServerName localhost
DocumentRoot {$GLOBALS['config']['installPath']}/public/
Options FollowSymLinks
AllowOverride All
Order Deny,Allow
Allow from all
AddHandler cgi-script .cgi
Options +ExecCGI
ErrorLog {$GLOBALS['config']['installPath']}/logs/error.log
CustomLog $apacheAccessLog combined
";
run("mkdir -p {$GLOBALS['config']['installPath']} ");
if ($GLOBALS['osData']['DISTRIB_ID'] == 'Ubuntu' || $GLOBALS['osData']['DISTRIB_ID'] == 'Debian') {
//Utils
fwrite(STDOUT, "Downloading and installing utilities...\n");
run('apt-get -y -qq install unzip python-imaging ttf-freefont ttf-liberation python-software-properties');
//Repository
if($GLOBALS['osData']['DISTRIB_ID'] == 'Ubuntu') {
fwrite(STDOUT, "Adding UbuntuGIS, pgRouting repositories...\n");
$addAptRepositoryOptions = '';
if($GLOBALS['osData']['DISTRIB_RELEASE'] == '12.04') {
$addAptRepositoryOptions = '-y';
}
run("add-apt-repository {$addAptRepositoryOptions} ppa:ubuntugis/ppa");
run("add-apt-repository {$addAptRepositoryOptions} ppa:georepublic/pgrouting");
run('apt-get -y -qq update');
}
//Apache
fwrite(STDOUT, "Downloading and installing Apache...\n");
run('apt-get -y -qq install apache2');
run('a2enmod rewrite');
run('a2enmod expires');
if($GLOBALS['pretend'] == false) {
file_put_contents("{$GLOBALS['config']['installPath']}/apache.conf", $virtualHost);
run("ln -s {$GLOBALS['config']['installPath']}/apache.conf /etc/apache2/sites-available/{$GLOBALS['config']['installName']}");
}
fwrite(STDOUT, "Disabling apache2 \"default\" site...\n");
run('a2dissite default');
run("a2ensite {$GLOBALS['config']['installName']}");
//PHP
fwrite(STDOUT, "Downloading and installing PHP...\n");
run('apt-get -y -qq install php5 php5-pgsql php5-mapscript php5-gd php-apc');
//IonCube
installIonCube();
//PostgreSQL
$postgreSqlVersion = 8.4;
$pgRoutingPath = '';
$pgRoutingPackages = '';
if($GLOBALS['osData']['DISTRIB_ID'] == 'Ubuntu') {
if($GLOBALS['osData']['DISTRIB_RELEASE'] == '12.04') {
$postgreSqlVersion = '9.1';
}
$pgRoutingPath = "/usr/share/postlbs";
$pgRoutingPackages = "postgresql-{$postgreSqlVersion}-pgrouting postgresql-{$postgreSqlVersion}-pgrouting-tsp postgresql-{$postgreSqlVersion}-pgrouting-dd ";
}
fwrite(STDOUT, "Downloading and installing PostgreSQL and PostGIS...\n");
run("apt-get -y -qq install postgresql postgresql-{$postgreSqlVersion}-postgis " .
$pgRoutingPackages .
"postgresql-contrib-{$postgreSqlVersion}");
addPostgreSqlUserAndDatabase(
$dbName,
$dbUser,
$dbPassword,
"/etc/postgresql/{$postgreSqlVersion}/main/pg_hba.conf",
"`find /usr/share/postgresql/{$postgreSqlVersion} -name postgis.sql`",
"`find /usr/share/postgresql/{$postgreSqlVersion} -name spatial_ref_sys.sql`",
"`find /usr/share/postgresql/{$postgreSqlVersion} -name pg_trgm.sql`",
$pgRoutingPath
);
//GDAL
fwrite(STDOUT, "Downloading and installing GDAL...\n");
run('apt-get -y -qq install gdal-bin');
//
run('chown -R www-data:www-data /var/lib/php5');
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'CentOS' || $GLOBALS['osData']['DISTRIB_ID'] == 'RHEL') {
//FIXME:
putenv('PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin');
//Config
$redhatMajorVersion = $GLOBALS['osData']['DISTRIB_RELEASE'][0];
//Repositories
fwrite(STDOUT, "Adding EPEL, ELGIS repositories...\n");
if($redhatMajorVersion == 5) {
run("rpm -i --quiet http://dl.fedoraproject.org/pub/epel/{$redhatMajorVersion}/i386/epel-release-{$redhatMajorVersion}-4.noarch.rpm");
run("rpm -i --quiet http://elgis.argeo.org/repos/{$redhatMajorVersion}/elgis-release-{$redhatMajorVersion}-5_0.noarch.rpm");
//FIXME: Activate elgis-plus
$elgisRepo = file_get_contents('/etc/yum.repos.d/elgis.repo');
$elgisRepo = str_replace(
"[elgis-plus]\nname=EL GIS 5 Plus - \$basearch\nbaseurl=http://elgis.argeo.org/repos/5/elgis-plus/\$basearch\nenabled=0",
"[elgis-plus]\nname=EL GIS 5 Plus - \$basearch\nbaseurl=http://elgis.argeo.org/repos/5/elgis-plus/\$basearch\nenabled=1",
$elgisRepo
);
file_put_contents('/etc/yum.repos.d/elgis.repo', $elgisRepo);
} else if($redhatMajorVersion == 6) {
run("rpm -i --quiet http://dl.fedoraproject.org/pub/epel/{$redhatMajorVersion}/i386/epel-release-{$redhatMajorVersion}-7.noarch.rpm");
run("rpm -i --quiet http://elgis.argeo.org/repos/{$redhatMajorVersion}/elgis-release-{$redhatMajorVersion}-6_0.noarch.rpm");
}
run('yum -y -q check-update');
//Utils
fwrite(STDOUT, "Downloading and installing utilities...\n");
run("yum -y -q install unzip python-imaging fcgi fribidi gd");
//Fonts
if($redhatMajorVersion == 5) {
run("yum -y -q install freefont liberation-fonts");
} else if($redhatMajorVersion == 6) {
run("yum -y -q install gnu-free-mono-fonts gnu-free-sans-fonts gnu-free-serif-fonts liberation-mono-fonts liberation-sans-fonts liberation-serif-fonts");
}
//Apache
fwrite(STDOUT, "Downloading and installing Apache...\n");
run('yum -y -q install httpd');
if($GLOBALS['pretend'] == false) {
file_put_contents("{$GLOBALS['config']['installPath']}/apache.conf", $virtualHost);
run("ln -s {$GLOBALS['config']['installPath']}/apache.conf /etc/httpd/conf.d/{$GLOBALS['config']['installName']}.conf");
}
run('setsebool -P httpd_can_network_connect=1');
run("chcon -Rv --type=httpd_sys_content_t {$GLOBALS['config']['installPath']}");
//PHP
fwrite(STDOUT, "Downloading and installing PHP...\n");
if($redhatMajorVersion == 5) {
run("yum -y -q install php53 php53-pgsql php53-xml php53-mbstring php53-gd php53-pecl-apc");
} else if($redhatMajorVersion == 6) {
run("yum -y -q install php php-pgsql php-xml php-mbstring php-gd php-pecl-apc");
}
//IonCube
installIonCube();
//PostgreSQL
if($redhatMajorVersion == 5) {
$postgreSqlVersion = '84';
} else if($redhatMajorVersion == 6) {
$postgreSqlVersion = '';
}
fwrite(STDOUT, "Downloading and installing PostgreSQL and PostGIS...\n");
run("yum -y -q install postgresql{$postgreSqlVersion} postgresql{$postgreSqlVersion}-server postgresql{$postgreSqlVersion}-libs postgis postgis-utils " .
"pgrouting " .
"postgresql{$postgreSqlVersion}-contrib");
run('service postgresql initdb');
run('service postgresql start');
if(file_exists('/usr/share/pgsql/contrib/postgis-64.sql')) {
$postgisSqlPapth = '/usr/share/pgsql/contrib/postgis-64.sql';
} else if(file_exists('/usr/share/pgsql/contrib/postgis.sql')) {
$postgisSqlPapth = '/usr/share/pgsql/contrib/postgis.sql';
} else {
error("postgis-64.sql or postgis.sql file not found in /usr/share/pgsql/contrib");
}
//FIXME: PgRouting is missing in stable install from testing
if($redhatMajorVersion == 6) {
run("rpm -i --quiet http://elgis.argeo.org/repos/testing/{$redhatMajorVersion}/elgis/x86_64/gaul-devel-0.1849-0_1.el6.{$arch}.rpm");
run("rpm -i --quiet http://elgis.argeo.org/repos/testing/{$redhatMajorVersion}/elgis/x86_64/pgrouting-1.05-1_0.el6.{$arch}.rpm");
}
addPostgreSqlUserAndDatabase(
$dbName,
$dbUser,
$dbPassword,
'/var/lib/pgsql/data/pg_hba.conf',
$postgisSqlPapth,
"/usr/share/pgsql/contrib/spatial_ref_sys.sql",
"/usr/share/pgsql/contrib/pg_trgm.sql",
"/usr/share/postlbs"
);
run('setsebool -P httpd_can_network_connect_db=1');
//GDAL
fwrite(STDOUT, "Downloading and installing GDAL...\n");
run("yum -y -q install gdal proj-epsg");
//Mapserver
fwrite(STDOUT, "Downloading and installing PHP Mapscript...\n");
if($redhatMajorVersion == 5) {
run("yum -y -q install php-mapserver6");
//FIXME:
run("mv /etc/php.d/mapserver6.ini /etc/php.d/z_mapserver6.ini");
} else if($redhatMajorVersion == 6) {
run("yum -y -q install php-mapserver");
}
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'openSUSE' || $GLOBALS['osData']['DISTRIB_ID'] == 'SLE') {
//Config
$zypperOptions = "-n --gpg-auto-import-keys";
//Repositories
fwrite(STDOUT, "Adding application-geo, server-http, server-php-extensions repositories...\n");
if ($GLOBALS['osData']['DISTRIB_ID'] == 'openSUSE') {
run("zypper {$zypperOptions} ar http://download.opensuse.org/repositories/Application:/Geo/openSUSE_{$GLOBALS['osData']['DISTRIB_RELEASE']}/Application:Geo.repo");
run("zypper {$zypperOptions} ar http://download.opensuse.org/repositories/server:/php:/extensions/server_php_openSUSE_{$GLOBALS['osData']['DISTRIB_RELEASE']}/server:php:extensions.repo");
//FIXME:
run("zypper {$zypperOptions} remove patterns-openSUSE-minimal_base");
} elseif($GLOBALS['osData']['DISTRIB_ID'] == 'SLE') {
run("zypper {$zypperOptions} ar http://download.opensuse.org/repositories/Application:/Geo/SLE_{$GLOBALS['osData']['DISTRIB_RELEASE']}/Application:Geo.repo");
run("zypper {$zypperOptions} ar http://download.opensuse.org/repositories/server:/http/SLE_{$GLOBALS['osData']['DISTRIB_RELEASE']}/server:http.repo");
run("zypper {$zypperOptions} ar http://download.opensuse.org/repositories/server:/php:/extensions/SLE_{$GLOBALS['osData']['DISTRIB_RELEASE']}/server:php:extensions.repo");
}
run("zypper {$zypperOptions} refresh");
//Utils
fwrite(STDOUT, "Downloading and installing utilities...\n");
run("zypper {$zypperOptions} install python-imaging liberation-fonts freefont FastCGI gd");
//Apache
fwrite(STDOUT, "Downloading and installing Apache...\n");
run("zypper {$zypperOptions} install apache2");
run('a2enmod rewrite');
if($GLOBALS['pretend'] == false) {
file_put_contents("{$GLOBALS['config']['installPath']}/apache.conf", $virtualHost);
run("ln -s {$GLOBALS['config']['installPath']}/apache.conf /etc/apache2/vhosts.d/{$GLOBALS['config']['installName']}.conf");
}
//PHP
fwrite(STDOUT, "Downloading and installing PHP...\n");
run("zypper {$zypperOptions} install apache2-mod_php5 php5-zip php5-pgsql php5-gd php5-openssl");
if ($GLOBALS['osData']['DISTRIB_ID'] == 'openSUSE') {
run("zypper {$zypperOptions} install php5-APC");
}
run("a2enmod php5");
//IonCube
installIonCube();
//PostgreSQL
fwrite(STDOUT, "Downloading and installing PostgreSQL and PostGIS...\n");
run("zypper {$zypperOptions} install postgresql-server postgis postgresql-contrib");
//HACK:
if ($GLOBALS['osData']['DISTRIB_ID'] == 'openSUSE') {
run("ln -s /usr/lib64/postgresql/postgis*.so /etc/alternatives/postgresql/lib64");
} elseif($GLOBALS['osData']['DISTRIB_ID'] == 'SLE') {
run("ln -s /usr/lib64/postgresql/postgis*.so /usr/lib/postgresql83/lib64");
}
run('chkconfig --add postgresql');
run('sudo /etc/init.d/postgresql start');
addPostgreSqlUserAndDatabase(
$dbName,
$dbUser,
$dbPassword,
'/var/lib/pgsql/data/pg_hba.conf',
"`find /usr/share/postgresql*/contrib -name postgis.sql`",
"`find /usr/share/postgresql*/contrib -name spatial_ref_sys.sql`",
"`find /usr/share/postgresql*/contrib -name pg_trgm.sql`",
""
);
//HACK:
if($GLOBALS['osData']['DISTRIB_ID'] == 'SLE') {
run('psql ' . $dbName . ' -c \"create aggregate array_agg(anyelement) (SFUNC=array_append, STYPE=anyarray, INITCOND=\'{}\');"', 'postgres');
}
//GDAL
fwrite(STDOUT, "Downloading and installing GDAL...\n");
run("zypper {$zypperOptions} install gdal proj");
//Mapserver
fwrite(STDOUT, "Downloading and installing PHP Mapscript...\n");
run("zypper {$zypperOptions} install php-mapserver");
//HACK: No php-mapserver in openSUSE 12.2 and SLE 11 SP 2
$hasMapScript = trim(implode('', run("php -m | grep MapScript")));
if(! $hasMapScript) {
$thisArch = ((-1^0xffffffff) ? 'x86_64' : 'i586');
if ($GLOBALS['osData']['DISTRIB_ID'] == 'openSUSE') {
$phpMapserverUrl = getPackageUrl('php-mapserver', "http://download.opensuse.org/repositories/Application:/Geo/openSUSE_Factory/{$thisArch}/");
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'SLE') {
$phpMapserverUrl = getPackageUrl('php-mapserver', "http://download.opensuse.org/repositories/Application:/Geo/SLE_{$GLOBALS['osData']['DISTRIB_RELEASE']}/{$thisArch}/");
}
run("rpm -i --quiet {$phpMapserverUrl}");
}
}
//Giscuit
fwrite(STDOUT, "Downloading and installing Giscuit...\n");
run("wget -q -O {$GLOBALS['config']['installPath']}/giscuit.zip {$downloadsUrl}/giscuit-{$giscuitVersion}-linux.zip");
run("cd {$GLOBALS['config']['installPath']} && unzip -o -d . giscuit.zip && mv -f giscuit/* . && rmdir giscuit");
if($GLOBALS['pretend'] == false) {
unlink("{$GLOBALS['config']['installPath']}/giscuit.zip");
}
//Premissions
if($GLOBALS['osData']['DISTRIB_ID'] == 'Ubuntu' || $GLOBALS['osData']['DISTRIB_ID'] == 'Debian') {
run("chown -R www-data:www-data {$GLOBALS['config']['installPath']}");
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'CentOS' || $GLOBALS['osData']['DISTRIB_ID'] == 'RHEL') {
run("chown -R apache:apache {$GLOBALS['config']['installPath']}");
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'openSUSE' || $GLOBALS['osData']['DISTRIB_ID'] == 'SLE') {
run("chown -R wwwrun:www {$GLOBALS['config']['installPath']}");
}
run("chmod 0700 {$GLOBALS['config']['installPath']}/configs/config.xml");
//Restart services
fwrite(STDOUT, "Restarting services...\n");
if ($GLOBALS['osData']['DISTRIB_ID'] == 'Ubuntu' || $GLOBALS['osData']['DISTRIB_ID'] == 'Debian') {
run('/etc/init.d/apache2 restart');
run("/etc/init.d/postgresql-{$postgreSqlVersion} restart");
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'CentOS' || $GLOBALS['osData']['DISTRIB_ID'] == 'RHEL') {
run('service httpd restart');
run('service postgresql restart');
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'openSUSE' || $GLOBALS['osData']['DISTRIB_ID'] == 'SLE') {
run('/etc/init.d/apache2 restart');
run('/etc/init.d/postgresql restart');
}
fwrite(STDOUT, "\nPlease access http://YOUR_DOMAIN/install.php to proceed with the installation.\n");
//Functions
function addPostgreSqlUserAndDatabase($dbName, $dbUser, $dbPassword, $hbaConfPath, $postgisTemplatePath, $spatialRefSysTemplatePath, $pgTrgmPath, $pgRoutingPath) {
//Create template_postgis
if(! database_exists('template_postgis')) {
run('psql template1 -c \"create database template_postgis with template = template1\"', 'postgres');
run('psql template1 -c \"update pg_database set datistemplate = true where datname = \'template_postgis\'\"', 'postgres');
run('psql template_postgis -c \"create language plpgsql\"', 'postgres');
//PostGIS
run("psql -q template_postgis -f $postgisTemplatePath", 'postgres');
run("psql -q template_postgis -f $spatialRefSysTemplatePath", 'postgres');
//TRGM
run("psql -q template_postgis -f $pgTrgmPath", 'postgres');
//pgRouting
if(! empty($pgRoutingPath)) {
run("psql -q template_postgis -f $pgRoutingPath/routing_core.sql", 'postgres');
run("psql -q template_postgis -f $pgRoutingPath/routing_core_wrappers.sql", 'postgres');
run("psql -q template_postgis -f $pgRoutingPath/matching.sql", 'postgres');
run("psql -q template_postgis -f $pgRoutingPath/routing_topology.sql", 'postgres');
run("psql -q template_postgis -f $pgRoutingPath/routing_tsp.sql", 'postgres');
run("psql -q template_postgis -f $pgRoutingPath/routing_tsp_wrappers.sql", 'postgres');
if(file_exists("{$pgRoutingPath}/routing_dd.sql")) {
run("psql -q template_postgis -f $pgRoutingPath/routing_dd.sql", 'postgres');
run("psql -q template_postgis -f $pgRoutingPath/routing_dd_wrappers.sql", 'postgres');
}
}
run('psql template_postgis -c \"grant all on geometry_columns to PUBLIC\"', 'postgres');
run('psql template_postgis -c \"grant all on spatial_ref_sys to PUBLIC\"', 'postgres');
run('psql template_postgis -c \"grant all on geography_columns to PUBLIC\"', 'postgres');
}
//Add user and database
run("createuser -E -S -D -R $dbUser", 'postgres');
run('psql template1 -c \"ALTER USER ' . $dbUser . ' with encrypted password \'' . $dbPassword . '\'\"', 'postgres');
run("createdb -O $dbUser -T template_postgis -E UNICODE $dbName", 'postgres');
run('psql ' . $dbName . ' -c \"alter table public.geometry_columns owner to ' . $dbUser . ';\"', 'postgres');
run('psql ' . $dbName . ' -c \"alter table public.spatial_ref_sys owner to ' . $dbUser . ';\"', 'postgres');
//Check if connections are allowed to localhost
$count = 0;
$hbaConf = file_get_contents($hbaConfPath);
//Create hba conf backup
$replacement = '$1md5';
$result = preg_replace('/(host\s+all\s+all\s+127.0.0.1\/32\s+)(ident)/i', $replacement, $hbaConf, -1 , $count);
$result = preg_replace('/(host\s+all\s+all\s+\:\:1\/128\s+)(ident)/i', $replacement, $result, -1 , $count);
if($count > 0 && $GLOBALS['pretend'] == false) {
fwrite(STDOUT, "PostgreSQL's \"pg_hba.conf\" was changed, backup created at {$hbaConfPath}.backup\n");
file_put_contents($hbaConfPath . '.backup', $hbaConf);
run("chmod 0640 {$hbaConfPath}.backup");
file_put_contents($hbaConfPath, $result);
}
}
function run($cmd, $user = NULL, $force = false) {
if($user != NULL) {
$cmd = "sudo su -l $user bash -c \"$cmd\"";
}
if($force == true || $GLOBALS['pretend'] == false) {
$output = NULL;
exec($cmd, $output);
return $output;
} else {
fwrite(STDOUT, "$cmd\n");
}
}
function getOs() {
$result = array();
$matches = NULL;
//CentOS
if (file_exists('/etc/redhat-release')
&& preg_match('/^CentOS (Linux release|release) (\d+\.\d+)/i', file_get_contents('/etc/redhat-release'), $matches)) {
$result['DISTRIB_ID'] = 'CentOS';
$result['DISTRIB_RELEASE'] = $matches[2];
//RHEL
} elseif (file_exists('/etc/redhat-release')
&& preg_match('/^Red Hat Enterprise Linux Server release (\d+\.\d+)/i', file_get_contents('/etc/redhat-release'), $matches)) {
$result['DISTRIB_ID'] = 'RHEL';
$result['DISTRIB_RELEASE'] = $matches[1];
//openSUSE
} elseif (file_exists('/etc/SuSE-release')
&& preg_match('/^openSUSE (\d+\.\d+)/i', file_get_contents('/etc/SuSE-release'), $matches)) {
$result['DISTRIB_ID'] = 'openSUSE';
$result['DISTRIB_RELEASE'] = $matches[1];
//SLE
} elseif (file_exists('/etc/SuSE-release')
&& preg_match('/^SUSE Linux Enterprise Server (\d+)/i', file_get_contents('/etc/SuSE-release'), $matches)) {
$result['DISTRIB_ID'] = 'SLE';
$result['DISTRIB_RELEASE'] = $matches[1];
//Ubuntu
} else if (file_exists('/etc/lsb-release')) {
$lsbRelease = parse_ini_file('/etc/lsb-release');
if($lsbRelease['DISTRIB_ID'] == 'Ubuntu') {
$result = $lsbRelease;
}
//Debian
} else if (file_exists('/etc/debian_version')) {
$lsbRelease = explode('.', file_get_contents('/etc/debian_version'));
$result['DISTRIB_ID'] = 'Debian';
$result['DISTRIB_RELEASE'] = $lsbRelease[0];
}
return $result;
}
function error($str) {
fwrite(STDOUT, "ERROR: $str\n");
exit;
}
function installIonCube() {
if(file_exists($GLOBALS['config']['ioncube']['installPath'] . '/ioncube')) {
return;
}
fwrite(STDOUT, "Downloading and installing IonCube...\n");
run("wget -q -O {$GLOBALS['config']['installPath']}/ioncube.zip {$GLOBALS['config']['ioncube']['downloadUrl']}");
if(! file_exists("{$GLOBALS['config']['installPath']}/ioncube.zip")) {
error("Could not download IonCube");
}
run("unzip -o -d {$GLOBALS['config']['ioncube']['installPath']} {$GLOBALS['config']['installPath']}/ioncube.zip");
if($GLOBALS['pretend'] == false) {
unlink("{$GLOBALS['config']['installPath']}/ioncube.zip");
}
//Php version
$phpVersion = substr(PHP_VERSION, 0, 3);
//Thread safety
ob_start();
phpinfo(INFO_GENERAL);
$phpinfo = ob_get_contents();
ob_end_clean();
preg_match('/thread safety => (disabled|enabled)/i', $phpinfo, $matches);
$ts = ($matches[1] == 'enabled' ? true : false);
$ionCubePath = $GLOBALS['config']['ioncube']['installPath'] . '/ioncube/ioncube_loader_lin_' . $phpVersion . ($ts ? '_ts' : '') . '.so';
if($GLOBALS['pretend'] == false) {
if ($GLOBALS['osData']['DISTRIB_ID'] == 'Ubuntu' || $GLOBALS['osData']['DISTRIB_ID'] == 'Debian') {
file_put_contents("/etc/php5/conf.d/ioncube.ini", "zend_extension=$ionCubePath");
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'CentOS' || $GLOBALS['osData']['DISTRIB_ID'] == 'RHEL') {
file_put_contents("/etc/php.d/ioncube.ini", "zend_extension=$ionCubePath");
run("restorecon {$GLOBALS['config']['ioncube']['installPath']}/ioncube/*.so");
} else if ($GLOBALS['osData']['DISTRIB_ID'] == 'openSUSE' || $GLOBALS['osData']['DISTRIB_ID'] == 'SLE') {
file_put_contents("/etc/php5/conf.d/ioncube.ini", "zend_extension=$ionCubePath");
}
}
}
function database_exists($dbName) {
$dbList = array();
$result = run('id -u postgres 2> /dev/null');
if(! empty($result) && ctype_digit($result[0])) {
$result = run('psql -ltA -F \'|\'', 'postgres');
foreach($result as $line) {
$line = explode('|', $line);
$dbList[] = trim($line[0]);
}
if(in_array($dbName, $dbList)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
function getPackageUrl($name, $url) {
$data = file_get_contents($url);
$matches = array();
preg_match("/{$name}(.*?)<\/a>/", $data, $matches);
return $url . $name . $matches[2];
}