2007-01-09 18:30:46 +01:00
|
|
|
<?php
|
2011-04-14 00:22:56 +02:00
|
|
|
//Pear classes.
|
2011-04-15 00:55:04 +02:00
|
|
|
set_include_path(__DIR__.'/../../airtime_mvc/library/pear' . PATH_SEPARATOR . get_include_path());
|
2011-04-14 00:22:56 +02:00
|
|
|
require_once('DB.php');
|
2007-01-09 18:30:46 +01:00
|
|
|
|
2011-04-15 00:55:04 +02:00
|
|
|
class AirtimeInstall
|
|
|
|
{
|
|
|
|
const CONF_DIR_BINARIES = "/usr/lib/airtime";
|
|
|
|
const CONF_DIR_STORAGE = "/srv/airtime";
|
|
|
|
const CONF_DIR_WWW = "/var/www/airtime";
|
2011-04-20 06:46:03 +02:00
|
|
|
const CONF_DIR_LOG = "/var/log/airtime";
|
2011-04-15 00:55:04 +02:00
|
|
|
|
|
|
|
public static function GetAirtimeSrcDir()
|
|
|
|
{
|
|
|
|
return __DIR__."/../../airtime_mvc";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function GetUtilsSrcDir()
|
|
|
|
{
|
|
|
|
return __DIR__."/../../utils";
|
|
|
|
}
|
|
|
|
|
2011-04-12 19:59:08 +02:00
|
|
|
/**
|
|
|
|
* Ensures that the user is running this PHP script with root
|
|
|
|
* permissions. If not running with root permissions, causes the
|
|
|
|
* script to exit.
|
|
|
|
*/
|
2011-04-15 00:55:04 +02:00
|
|
|
public static function ExitIfNotRoot()
|
2011-04-12 19:59:08 +02:00
|
|
|
{
|
|
|
|
// Need to check that we are superuser before running this.
|
|
|
|
if(exec("whoami") != "root"){
|
|
|
|
echo "Must be root user.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-09 18:30:46 +01:00
|
|
|
|
2011-04-15 00:55:04 +02:00
|
|
|
public static function DbTableExists($p_name)
|
2011-03-16 18:21:40 +01:00
|
|
|
{
|
|
|
|
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-04-15 00:55:04 +02:00
|
|
|
public static function InstallQuery($sql, $verbose = true)
|
2011-03-16 18:21:40 +01:00
|
|
|
{
|
|
|
|
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-04-15 00:55:04 +02:00
|
|
|
public static function DbConnect($p_exitOnError = true)
|
2011-03-16 18:21:40 +01:00
|
|
|
{
|
2011-04-15 00:55:04 +02:00
|
|
|
global $CC_DBC, $CC_CONFIG;
|
2011-04-14 00:22:56 +02:00
|
|
|
$CC_DBC = DB::connect($CC_CONFIG['dsn'], FALSE);
|
2011-03-16 18:21:40 +01:00
|
|
|
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-04-15 00:55:04 +02:00
|
|
|
public static function ChangeDirOwnerToWebserver($filePath)
|
2011-03-09 06:44:51 +01:00
|
|
|
{
|
2011-03-16 18:21:40 +01:00
|
|
|
global $CC_CONFIG;
|
2011-04-15 00:55:04 +02:00
|
|
|
echo "* Giving Apache permission to access $filePath".PHP_EOL;
|
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
$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-04-18 20:42:41 +02:00
|
|
|
public static function InstallStorageDirectory()
|
2011-03-16 18:21:40 +01:00
|
|
|
{
|
|
|
|
global $CC_CONFIG, $CC_DBC;
|
2011-04-15 00:55:04 +02:00
|
|
|
echo "* Storage directory setup".PHP_EOL;
|
2011-03-24 19:55:42 +01:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
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;
|
2011-04-15 00:55:04 +02:00
|
|
|
|
|
|
|
echo "* Creating Airtime database user".PHP_EOL;
|
|
|
|
|
2011-04-22 06:55:31 +02:00
|
|
|
$username = $CC_CONFIG['dsn']['username'];
|
|
|
|
$password = $CC_CONFIG['dsn']['password'];
|
2011-05-13 22:47:58 +02:00
|
|
|
$command = "echo \"CREATE USER $username ENCRYPTED PASSWORD '$password' LOGIN CREATEDB NOCREATEUSER;\" | su postgres -c psql 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-04-15 00:55:04 +02:00
|
|
|
echo "* Creating Airtime database".PHP_EOL;
|
|
|
|
|
2011-04-22 06:55:31 +02:00
|
|
|
$database = $CC_CONFIG['dsn']['database'];
|
|
|
|
$username = $CC_CONFIG['dsn']['username'];
|
2011-05-13 22:47:58 +02:00
|
|
|
$command = "echo \"CREATE DATABASE $database OWNER $username\" | su postgres -c psql 2>/dev/null";
|
2011-04-25 21:23:21 +02:00
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
@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;
|
2011-04-15 00:55:04 +02:00
|
|
|
|
|
|
|
echo "* Installing Postgresql scripting language".PHP_EOL;
|
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
// 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()
|
|
|
|
{
|
2011-04-15 00:55:04 +02:00
|
|
|
echo "* Creating database tables".PHP_EOL;
|
|
|
|
|
2011-03-16 18:21:40 +01:00
|
|
|
// Put Propel sql files in Database
|
2011-05-18 20:33:11 +02:00
|
|
|
$command = AirtimeInstall::CONF_DIR_WWW."/library/propel/generator/bin/propel-gen ".AirtimeInstall::CONF_DIR_WWW."/build/ insert-sql";
|
2011-03-16 18:21:40 +01:00
|
|
|
@exec($command, $output, $results);
|
|
|
|
}
|
2011-02-22 18:22:31 +01:00
|
|
|
|
2011-04-08 22:54:24 +02:00
|
|
|
public static function BypassMigrations($dir, $version)
|
2011-03-16 18:21:40 +01:00
|
|
|
{
|
2011-04-15 00:55:04 +02:00
|
|
|
$appDir = AirtimeInstall::GetAirtimeSrcDir();
|
|
|
|
$command = "php $appDir/library/doctrine/migrations/doctrine-migrations.phar ".
|
|
|
|
"--configuration=$dir/../../DoctrineMigrations/migrations.xml ".
|
|
|
|
"--db-configuration=$appDir/library/doctrine/migrations/migrations-db.php ".
|
2011-04-08 23:44:05 +02:00
|
|
|
"--no-interaction --add migrations:version $version";
|
2011-04-08 22:54:24 +02:00
|
|
|
system($command);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function MigrateTablesToVersion($dir, $version)
|
|
|
|
{
|
2011-04-15 00:55:04 +02:00
|
|
|
$appDir = AirtimeInstall::GetAirtimeSrcDir();
|
|
|
|
$command = "php $appDir/library/doctrine/migrations/doctrine-migrations.phar ".
|
|
|
|
"--configuration=$dir/../../DoctrineMigrations/migrations.xml ".
|
|
|
|
"--db-configuration=$appDir/library/doctrine/migrations/migrations-db.php ".
|
2011-04-08 22:54:24 +02:00
|
|
|
"--no-interaction migrations:migrate $version";
|
2011-03-16 18:21:40 +01:00
|
|
|
system($command);
|
|
|
|
}
|
2011-03-09 19:23:05 +01:00
|
|
|
|
2011-04-12 19:42:28 +02:00
|
|
|
public static function SetAirtimeVersion($p_version)
|
|
|
|
{
|
|
|
|
global $CC_DBC;
|
|
|
|
$sql = "DELETE FROM cc_pref WHERE keystr = 'system_version'";
|
|
|
|
$CC_DBC->query($sql);
|
|
|
|
|
2011-04-12 20:39:09 +02:00
|
|
|
$sql = "INSERT INTO cc_pref (keystr, valstr) VALUES ('system_version', '$p_version')";
|
2011-04-12 19:42:28 +02:00
|
|
|
$result = $CC_DBC->query($sql);
|
|
|
|
if (PEAR::isError($result)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function GetAirtimeVersion()
|
|
|
|
{
|
|
|
|
global $CC_DBC;
|
|
|
|
$sql = "SELECT valstr FROM cc_pref WHERE keystr = 'system_version'";
|
|
|
|
$version = $CC_DBC->GetOne($sql);
|
|
|
|
|
|
|
|
if (PEAR::isError($version)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $version;
|
|
|
|
}
|
|
|
|
|
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-04-15 00:55:04 +02:00
|
|
|
public static function CreateSymlinksToUtils()
|
|
|
|
{
|
|
|
|
echo "* Creating /usr/bin symlinks".PHP_EOL;
|
2011-03-24 07:12:08 +01:00
|
|
|
AirtimeInstall::RemoveSymlinks();
|
2011-03-24 19:55:42 +01:00
|
|
|
|
2011-04-15 00:55:04 +02:00
|
|
|
echo "* Installing airtime-import".PHP_EOL;
|
|
|
|
$dir = AirtimeInstall::CONF_DIR_BINARIES."/utils/airtime-import";
|
2011-03-24 07:12:08 +01:00
|
|
|
exec("ln -s $dir /usr/bin/airtime-import");
|
|
|
|
|
2011-04-15 00:55:04 +02:00
|
|
|
echo "* Installing airtime-clean-storage".PHP_EOL;
|
|
|
|
$dir = AirtimeInstall::CONF_DIR_BINARIES."/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
|
|
|
|
2011-04-15 00:55:04 +02:00
|
|
|
echo "* Installing airtime-update-db-settings".PHP_EOL;
|
|
|
|
$dir = AirtimeInstall::CONF_DIR_BINARIES."/utils/airtime-update-db-settings";
|
2011-04-01 21:22:49 +02:00
|
|
|
exec("ln -s $dir /usr/bin/airtime-update-db-settings");
|
2011-03-24 07:12:08 +01:00
|
|
|
}
|
|
|
|
|
2011-04-15 00:55:04 +02: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-04-15 00:55:04 +02:00
|
|
|
|
|
|
|
public static function InstallPhpCode()
|
|
|
|
{
|
|
|
|
global $CC_CONFIG;
|
|
|
|
echo "* Installing PHP code to ".AirtimeInstall::CONF_DIR_WWW.PHP_EOL;
|
|
|
|
exec("mkdir -p ".AirtimeInstall::CONF_DIR_WWW);
|
|
|
|
exec("cp -R ".AirtimeInstall::GetAirtimeSrcDir()."/* ".AirtimeInstall::CONF_DIR_WWW);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function UninstallPhpCode()
|
|
|
|
{
|
|
|
|
echo "* Removing PHP code from ".AirtimeInstall::CONF_DIR_WWW.PHP_EOL;
|
|
|
|
exec("rm -rf ".AirtimeInstall::CONF_DIR_WWW);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function InstallBinaries()
|
|
|
|
{
|
|
|
|
echo "* Installing binaries to ".AirtimeInstall::CONF_DIR_BINARIES.PHP_EOL;
|
|
|
|
exec("mkdir -p ".AirtimeInstall::CONF_DIR_BINARIES);
|
|
|
|
exec("cp -R ".AirtimeInstall::GetUtilsSrcDir()." ".AirtimeInstall::CONF_DIR_BINARIES);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function UninstallBinaries()
|
|
|
|
{
|
|
|
|
echo "* Removing Airtime binaries from ".AirtimeInstall::CONF_DIR_BINARIES.PHP_EOL;
|
|
|
|
exec("rm -rf ".AirtimeInstall::CONF_DIR_BINARIES);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function DirCheck()
|
|
|
|
{
|
|
|
|
echo "Legend: \"+\" means the dir/file exists, \"-\" means that it does not.".PHP_EOL;
|
|
|
|
$dirs = array(AirtimeInstall::CONF_DIR_BINARIES,
|
|
|
|
AirtimeInstall::CONF_DIR_STORAGE,
|
|
|
|
AirtimeInstall::CONF_DIR_WWW,
|
|
|
|
AirtimeIni::CONF_FILE_AIRTIME,
|
|
|
|
AirtimeIni::CONF_FILE_LIQUIDSOAP,
|
|
|
|
AirtimeIni::CONF_FILE_PYPO,
|
|
|
|
AirtimeIni::CONF_FILE_RECORDER,
|
2011-04-15 20:45:10 +02:00
|
|
|
"/usr/lib/airtime/pypo",
|
|
|
|
"/usr/lib/airtime/show-recorder",
|
|
|
|
"/var/log/airtime",
|
|
|
|
"/var/log/airtime/pypo",
|
|
|
|
"/var/log/airtime/show-recorder",
|
|
|
|
"/var/tmp/airtime/pypo",
|
|
|
|
"/var/tmp/airtime/show-recorder");
|
2011-04-15 00:55:04 +02:00
|
|
|
foreach ($dirs as $f) {
|
|
|
|
if (file_exists($f)) {
|
|
|
|
echo "+ $f".PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo "- $f".PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-20 06:46:03 +02:00
|
|
|
|
|
|
|
public static function CreateZendPhpLogFile(){
|
|
|
|
global $CC_CONFIG;
|
|
|
|
|
|
|
|
echo "* Creating logs directory ".AirtimeInstall::CONF_DIR_LOG.PHP_EOL;
|
2011-04-25 23:24:53 +02:00
|
|
|
|
2011-04-20 06:46:03 +02:00
|
|
|
$path = AirtimeInstall::CONF_DIR_LOG;
|
|
|
|
$file = $path.'/zendphp.log';
|
|
|
|
if (!file_exists($path)){
|
|
|
|
mkdir($path, 0755, true);
|
|
|
|
}
|
2011-04-25 23:24:53 +02:00
|
|
|
|
2011-04-20 06:46:03 +02:00
|
|
|
touch($file);
|
|
|
|
chmod($file, 0755);
|
|
|
|
chown($file, $CC_CONFIG['webServerUser']);
|
|
|
|
chgrp($file, $CC_CONFIG['webServerUser']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function RemoveLogDirectories(){
|
|
|
|
$path = AirtimeInstall::CONF_DIR_LOG;
|
|
|
|
echo "* Removing logs directory ".$path.PHP_EOL;
|
2011-04-25 23:24:53 +02:00
|
|
|
|
2011-04-20 06:46:03 +02:00
|
|
|
exec("rm -rf $path");
|
|
|
|
}
|
2011-03-21 20:48:44 +01:00
|
|
|
}
|