2007-01-09 18:30:46 +01:00
|
|
|
<?php
|
2011-02-10 01:45:22 +01:00
|
|
|
|
2011-03-30 00:32:53 +02:00
|
|
|
require_once(dirname(__FILE__).'/../../library/pear/DB.php');
|
|
|
|
require_once(dirname(__FILE__).'/../../application/configs/conf.php');
|
2007-01-09 18:30:46 +01:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
class AirtimeInstall {
|
2007-01-09 18:30:46 +01:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
static function DbTableExists($p_name)
|
|
|
|
{
|
|
|
|
global $CC_DBC;
|
|
|
|
$sql = "SELECT * FROM ".$p_name;
|
|
|
|
$result = $CC_DBC->GetOne($sql);
|
|
|
|
if (PEAR::isError($result)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2011-03-09 06:44:51 +01:00
|
|
|
}
|
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
static function InstallQuery($sql, $verbose = true)
|
|
|
|
{
|
|
|
|
global $CC_DBC;
|
|
|
|
$result = $CC_DBC->query($sql);
|
|
|
|
if (PEAR::isError($result)) {
|
|
|
|
echo "Error! ".$result->getMessage()."\n";
|
|
|
|
echo " SQL statement was:\n";
|
|
|
|
echo " ".$sql."\n\n";
|
|
|
|
} else {
|
|
|
|
if ($verbose) {
|
|
|
|
echo "done.\n";
|
|
|
|
}
|
2007-01-09 18:30:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
static function DbConnect($p_exitOnError = true)
|
|
|
|
{
|
|
|
|
global $CC_DBC, $CC_CONFIG;
|
|
|
|
$CC_DBC = DB::connect($CC_CONFIG['dsn'], TRUE);
|
|
|
|
if (PEAR::isError($CC_DBC)) {
|
|
|
|
echo $CC_DBC->getMessage().PHP_EOL;
|
|
|
|
echo $CC_DBC->getUserInfo().PHP_EOL;
|
|
|
|
echo "Database connection problem.".PHP_EOL;
|
|
|
|
echo "Check if database '{$CC_CONFIG['dsn']['database']}' exists".
|
|
|
|
" with corresponding permissions.".PHP_EOL;
|
|
|
|
if ($p_exitOnError) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
echo "* Connected to database".PHP_EOL;
|
|
|
|
$CC_DBC->setFetchMode(DB_FETCHMODE_ASSOC);
|
2007-01-23 21:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
2007-01-09 18:30:46 +01:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
static function ChangeDirOwnerToWebserver($filePath)
|
2011-03-09 06:44:51 +01:00
|
|
|
{
|
2011-03-16 18:21:40 +01:00
|
|
|
global $CC_CONFIG;
|
|
|
|
$success = chgrp($filePath, $CC_CONFIG["webServerUser"]);
|
|
|
|
$fileperms=@fileperms($filePath);
|
|
|
|
$fileperms = $fileperms | 0x0010; // group write bit
|
|
|
|
$fileperms = $fileperms | 0x0400; // group sticky bit
|
|
|
|
chmod($filePath, $fileperms);
|
2011-03-09 06:44:51 +01:00
|
|
|
}
|
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
public static function SetupStorageDirectory($CC_CONFIG)
|
|
|
|
{
|
|
|
|
global $CC_CONFIG, $CC_DBC;
|
2011-03-24 19:55:42 +01:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
echo PHP_EOL."*** Directory Setup ***".PHP_EOL;
|
|
|
|
foreach (array('baseFilesDir', 'storageDir') as $d) {
|
|
|
|
if ( !file_exists($CC_CONFIG[$d]) ) {
|
|
|
|
@mkdir($CC_CONFIG[$d], 02775, true);
|
|
|
|
if (file_exists($CC_CONFIG[$d])) {
|
|
|
|
$rp = realpath($CC_CONFIG[$d]);
|
|
|
|
echo "* Directory $rp created".PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo "* Failed creating {$CC_CONFIG[$d]}".PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} elseif (is_writable($CC_CONFIG[$d])) {
|
2011-03-09 06:44:51 +01:00
|
|
|
$rp = realpath($CC_CONFIG[$d]);
|
2011-03-16 18:21:40 +01:00
|
|
|
echo "* Skipping directory already exists: $rp".PHP_EOL;
|
2011-03-09 06:44:51 +01:00
|
|
|
} else {
|
2011-03-16 18:21:40 +01:00
|
|
|
$rp = realpath($CC_CONFIG[$d]);
|
|
|
|
echo "* WARNING: Directory already exists, but is not writable: $rp".PHP_EOL;
|
2011-03-09 06:44:51 +01:00
|
|
|
}
|
2011-03-16 18:21:40 +01:00
|
|
|
$CC_CONFIG[$d] = $rp;
|
2011-03-09 06:44:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
public static function CreateDatabaseUser()
|
|
|
|
{
|
|
|
|
global $CC_CONFIG;
|
|
|
|
// Create the database user
|
|
|
|
$command = "sudo -u postgres psql postgres --command \"CREATE USER {$CC_CONFIG['dsn']['username']} "
|
|
|
|
." ENCRYPTED PASSWORD '{$CC_CONFIG['dsn']['password']}' LOGIN CREATEDB NOCREATEUSER;\" 2>/dev/null";
|
2011-03-24 19:55:42 +01:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
@exec($command, $output, $results);
|
|
|
|
if ($results == 0) {
|
2011-03-28 21:38:41 +02:00
|
|
|
echo "* Database user '{$CC_CONFIG['dsn']['username']}' created.".PHP_EOL;
|
2011-03-16 18:21:40 +01:00
|
|
|
} else {
|
2011-03-28 21:38:41 +02:00
|
|
|
if (count($output) > 0) {
|
|
|
|
echo "* Could not create user '{$CC_CONFIG['dsn']['username']}': ".PHP_EOL;
|
|
|
|
echo implode(PHP_EOL, $output);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
echo "* Database user '{$CC_CONFIG['dsn']['username']}' already exists.".PHP_EOL;
|
|
|
|
}
|
2011-03-16 18:21:40 +01:00
|
|
|
}
|
2011-03-09 06:44:51 +01:00
|
|
|
}
|
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
public static function CreateDatabase()
|
|
|
|
{
|
|
|
|
global $CC_CONFIG;
|
2011-03-24 19:55:42 +01:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
$command = "sudo -u postgres createdb {$CC_CONFIG['dsn']['database']} --owner {$CC_CONFIG['dsn']['username']} 2> /dev/null";
|
|
|
|
@exec($command, $output, $results);
|
|
|
|
if ($results == 0) {
|
|
|
|
echo "* Database '{$CC_CONFIG['dsn']['database']}' created.".PHP_EOL;
|
|
|
|
} else {
|
2011-03-28 21:38:41 +02:00
|
|
|
if (count($output) > 0) {
|
|
|
|
echo "* Could not create database '{$CC_CONFIG['dsn']['database']}': ".PHP_EOL;
|
|
|
|
echo implode(PHP_EOL, $output);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
echo "* Database '{$CC_CONFIG['dsn']['database']}' already exists.".PHP_EOL;
|
|
|
|
}
|
2011-03-16 18:21:40 +01:00
|
|
|
}
|
2011-03-09 06:44:51 +01:00
|
|
|
}
|
2010-10-04 23:00:20 +02:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
public static function InstallPostgresScriptingLanguage()
|
|
|
|
{
|
|
|
|
global $CC_DBC;
|
|
|
|
// Install postgres scripting language
|
|
|
|
$langIsInstalled = $CC_DBC->GetOne('SELECT COUNT(*) FROM pg_language WHERE lanname = \'plpgsql\'');
|
|
|
|
if ($langIsInstalled == '0') {
|
|
|
|
echo "* Installing Postgres scripting language".PHP_EOL;
|
|
|
|
$sql = "CREATE LANGUAGE 'plpgsql'";
|
|
|
|
AirtimeInstall::InstallQuery($sql, false);
|
|
|
|
} else {
|
|
|
|
echo "* Postgres scripting language already installed".PHP_EOL;
|
|
|
|
}
|
2011-03-09 06:44:51 +01:00
|
|
|
}
|
2010-10-04 23:00:20 +02:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
public static function CreateDatabaseTables()
|
|
|
|
{
|
|
|
|
// Put Propel sql files in Database
|
2011-03-30 00:32:53 +02:00
|
|
|
$command = __DIR__."/../../library/propel/generator/bin/propel-gen ../build/ insert-sql 2>propel-error.log";
|
2011-03-16 18:21:40 +01:00
|
|
|
@exec($command, $output, $results);
|
|
|
|
}
|
2011-02-22 18:22:31 +01:00
|
|
|
|
2011-04-08 22:25:29 +02:00
|
|
|
public static function ExecuteDoctrineMigration($dir, $version)
|
2011-03-16 18:21:40 +01:00
|
|
|
{
|
2011-04-08 22:07:41 +02:00
|
|
|
$command = "php $dir/../../../library/doctrine/migrations/doctrine-migrations.phar ".
|
|
|
|
"--configuration=$dir/../../DoctrineMigrations/migrations.xml ".
|
|
|
|
"--db-configuration=$dir/../../../library/doctrine/migrations/migrations-db.php ".
|
2011-04-08 22:25:29 +02:00
|
|
|
"--no-interaction migrations:execute $version";
|
2011-03-16 18:21:40 +01:00
|
|
|
system($command);
|
|
|
|
}
|
2011-03-09 19:23:05 +01:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
public static function DeleteFilesRecursive($p_path)
|
|
|
|
{
|
|
|
|
$command = "rm -rf $p_path";
|
|
|
|
exec($command);
|
|
|
|
}
|
2011-03-11 19:59:20 +01:00
|
|
|
|
2011-03-24 07:12:08 +01:00
|
|
|
public static function CreateSymlinks(){
|
|
|
|
AirtimeInstall::RemoveSymlinks();
|
2011-03-24 19:55:42 +01:00
|
|
|
|
2011-03-30 00:32:53 +02:00
|
|
|
$dir = realpath(__DIR__."/../../utils/airtime-import");
|
2011-03-24 07:12:08 +01:00
|
|
|
exec("ln -s $dir /usr/bin/airtime-import");
|
|
|
|
|
2011-03-30 00:32:53 +02:00
|
|
|
$dir = realpath(__DIR__."/../../utils/airtime-clean-storage");
|
2011-03-24 07:12:08 +01:00
|
|
|
exec("ln -s $dir /usr/bin/airtime-clean-storage");
|
2011-04-01 21:22:49 +02:00
|
|
|
|
|
|
|
$dir = realpath(__DIR__."/../../utils/airtime-update-db-settings");
|
|
|
|
exec("ln -s $dir /usr/bin/airtime-update-db-settings");
|
2011-03-24 07:12:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function RemoveSymlinks(){
|
2011-03-24 19:55:42 +01:00
|
|
|
exec("rm -f /usr/bin/airtime-import");
|
|
|
|
exec("rm -f /usr/bin/airtime-clean-storage");
|
2011-04-01 21:22:49 +02:00
|
|
|
exec("rm -f /usr/bin/airtime-update-db-settings");
|
2011-03-24 07:12:08 +01:00
|
|
|
}
|
2011-03-21 20:48:44 +01:00
|
|
|
}
|