Format code using php-cs-fixer

This commit is contained in:
jo 2021-10-11 16:10:47 +02:00
parent 43d7dc92cd
commit d52c6184b9
352 changed files with 17473 additions and 17041 deletions

View file

@ -1,21 +1,25 @@
<?php
set_include_path(__DIR__.'/../../legacy/library' . PATH_SEPARATOR . get_include_path());
#require_once('Zend/Loader/Autoloader.php');
set_include_path(__DIR__ . '/../../legacy/library' . PATH_SEPARATOR . get_include_path());
//require_once('Zend/Loader/Autoloader.php');
class AirtimeInstall
{
const CONF_DIR_BINARIES = "/usr/lib/airtime";
const CONF_DIR_WWW = "/usr/share/airtime";
const CONF_DIR_LOG = LIBRETIME_LOG_DIR;
public const CONF_DIR_BINARIES = '/usr/lib/airtime';
public const CONF_DIR_WWW = '/usr/share/airtime';
public const CONF_DIR_LOG = LIBRETIME_LOG_DIR;
public static $databaseTablesCreated = false;
public static function GetAirtimeSrcDir()
{
return __DIR__."/../../..";
return __DIR__ . '/../../..';
}
public static function GetUtilsSrcDir()
{
return __DIR__."/../../../../utils";
return __DIR__ . '/../../../../utils';
}
/**
* Ensures that the user is running this PHP script with root
* permissions. If not running with root permissions, causes the
@ -24,16 +28,18 @@ class AirtimeInstall
public static function ExitIfNotRoot()
{
// Need to check that we are superuser before running this.
if(posix_geteuid() != 0){
if (posix_geteuid() != 0) {
echo "Must be root user.\n";
exit(1);
}
}
/**
* Return the version of Airtime currently installed.
* If not installed, return null.
*
* @return NULL|string
* @return null|string
*/
public static function GetVersionInstalled()
{
@ -44,15 +50,14 @@ class AirtimeInstall
}
if (file_exists('/etc/airtime/airtime.conf')) {
$values = parse_ini_file('/etc/airtime/airtime.conf', true);
}
else {
} else {
return null;
}
$sql = "SELECT valstr FROM cc_pref WHERE keystr = 'system_version' LIMIT 1";
try {
$version = $con->query($sql)->fetchColumn(0);
} catch (PDOException $e){
} catch (PDOException $e) {
// no pref table therefore Airtime is not installed.
//We only get here if airtime database exists, but the table doesn't
//This state sometimes happens if a previous Airtime uninstall couldn't remove
@ -63,123 +68,136 @@ class AirtimeInstall
if ($version == '') {
try {
// If this table exists, then it's version 1.7.0
$sql = "SELECT * FROM cc_show_rebroadcast LIMIT 1";
$sql = 'SELECT * FROM cc_show_rebroadcast LIMIT 1';
$result = $con->query($sql)->fetchColumn(0);
$version = "1.7.0";
$version = '1.7.0';
} catch (Exception $e) {
$version = null;
}
}
return $version;
}
public static function DbTableExists($p_name)
{
$con = Propel::getConnection();
try {
$sql = "SELECT * FROM ".$p_name." LIMIT 1";
$sql = 'SELECT * FROM ' . $p_name . ' LIMIT 1';
$con->query($sql);
} catch (PDOException $e){
} catch (PDOException $e) {
return false;
}
return true;
}
public static function InstallQuery($sql, $verbose = true)
{
$con = Propel::getConnection();
try {
$con->exec($sql);
if ($verbose) {
echo "done.\n";
}
} catch (Exception $e) {
echo "Error!\n".$e->getMessage()."\n";
echo "Error!\n" . $e->getMessage() . "\n";
echo " SQL statement was:\n";
echo " ".$sql."\n\n";
echo ' ' . $sql . "\n\n";
}
}
public static function DropSequence($p_sequenceName)
{
AirtimeInstall::InstallQuery("DROP SEQUENCE IF EXISTS $p_sequenceName", false);
AirtimeInstall::InstallQuery("DROP SEQUENCE IF EXISTS {$p_sequenceName}", false);
}
/**
* Try to connect to the database. Return true on success, false on failure.
* @param boolean $p_exitOnError
* Exit the program on failure.
* @return boolean
*
* @param bool $p_exitOnError
* Exit the program on failure
*
* @return bool
*/
public static function DbConnect($p_exitOnError = true)
{
$CC_CONFIG = Config::getConfig();
try {
$con = Propel::getConnection();
} catch (Exception $e) {
echo $e->getMessage().PHP_EOL;
echo "Database connection problem.".PHP_EOL;
echo "Check if database '{$CC_CONFIG['dsn']['database']}' exists".
" with corresponding permissions.".PHP_EOL;
echo $e->getMessage() . 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);
}
return false;
}
return true;
}
/* TODO: This function should be moved to the media-monitor
* install script. */
public static function InstallStorageDirectory()
{
$CC_CONFIG = Config::getConfig();
echo "* Storage directory setup".PHP_EOL;
$ini = parse_ini_file(__DIR__."/airtime-install.ini");
$stor_dir = $ini["storage_dir"];
$dirs = array($stor_dir, $stor_dir."/organize");
foreach ($dirs as $dir){
echo '* Storage directory setup' . PHP_EOL;
$ini = parse_ini_file(__DIR__ . '/airtime-install.ini');
$stor_dir = $ini['storage_dir'];
$dirs = [$stor_dir, $stor_dir . '/organize'];
foreach ($dirs as $dir) {
if (!file_exists($dir)) {
if (mkdir($dir, 02775, true)){
if (mkdir($dir, 02775, true)) {
$rp = realpath($dir);
echo "* Directory $rp created".PHP_EOL;
echo "* Directory {$rp} created" . PHP_EOL;
} else {
echo "* Failed creating {$dir}".PHP_EOL;
echo "* Failed creating {$dir}" . PHP_EOL;
exit(1);
}
}
else if (is_writable($dir)) {
} elseif (is_writable($dir)) {
$rp = realpath($dir);
echo "* Skipping directory already exists: $rp".PHP_EOL;
}
else {
echo "* Skipping directory already exists: {$rp}" . PHP_EOL;
} else {
$rp = realpath($dir);
echo "* Error: Directory already exists, but is not writable: $rp".PHP_EOL;
echo "* Error: Directory already exists, but is not writable: {$rp}" . PHP_EOL;
exit(1);
}
echo "* Giving Apache permission to access $rp".PHP_EOL;
$success = chown($rp, $CC_CONFIG["webServerUser"]);
$success = chgrp($rp, $CC_CONFIG["webServerUser"]);
echo "* Giving Apache permission to access {$rp}" . PHP_EOL;
$success = chown($rp, $CC_CONFIG['webServerUser']);
$success = chgrp($rp, $CC_CONFIG['webServerUser']);
$success = chmod($rp, 0775);
}
}
public static function CreateDatabaseUser()
{
$CC_CONFIG = Config::getConfig();
echo " * Creating Airtime database user".PHP_EOL;
echo ' * Creating Airtime database user' . PHP_EOL;
$username = $CC_CONFIG['dsn']['username'];
$password = $CC_CONFIG['dsn']['password'];
$command = "echo \"CREATE USER $username ENCRYPTED PASSWORD '$password' LOGIN CREATEDB NOCREATEUSER;\" | su postgres -c /usr/bin/psql 2>/dev/null";
$command = "echo \"CREATE USER {$username} ENCRYPTED PASSWORD '{$password}' LOGIN CREATEDB NOCREATEUSER;\" | su postgres -c /usr/bin/psql 2>/dev/null";
@exec($command, $output, $results);
if ($results == 0) {
echo " * Database user '{$CC_CONFIG['dsn']['username']}' created.".PHP_EOL;
echo " * Database user '{$CC_CONFIG['dsn']['username']}' created." . PHP_EOL;
} else {
if (count($output) > 0) {
echo " * Could not create user '{$CC_CONFIG['dsn']['username']}': ".PHP_EOL;
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;
} else {
echo " * Database user '{$CC_CONFIG['dsn']['username']}' already exists." . PHP_EOL;
}
}
}
public static function CreateDatabase()
{
$CC_CONFIG = Config::getConfig();
@ -188,26 +206,30 @@ class AirtimeInstall
$password = $CC_CONFIG['dsn']['password'];
$hostspec = $CC_CONFIG['dsn']['hostspec'];
echo " * Creating Airtime database: " . $database . PHP_EOL;
echo ' * Creating Airtime database: ' . $database . PHP_EOL;
$dbExists = false;
try {
$con = pg_connect('user='.$username.' password='.$password.' host='.$hostspec);
pg_query($con, 'CREATE DATABASE '.$database.' WITH ENCODING \'UTF8\' TEMPLATE template0 OWNER '.$username.';');
$con = pg_connect('user=' . $username . ' password=' . $password . ' host=' . $hostspec);
pg_query($con, 'CREATE DATABASE ' . $database . ' WITH ENCODING \'UTF8\' TEMPLATE template0 OWNER ' . $username . ';');
} catch (Exception $e) {
// rethrow if not a "database already exists" error
if ($e->getCode() != 2 && strpos($e->getMessage(), 'already exists') !== false) throw $e;
echo " * Database already exists." . PHP_EOL;
if ($e->getCode() != 2 && strpos($e->getMessage(), 'already exists') !== false) {
throw $e;
}
echo ' * Database already exists.' . PHP_EOL;
$dbExists = true;
}
if (!$dbExists) {
echo " * Database $database created.".PHP_EOL;
echo " * Database {$database} created." . PHP_EOL;
}
return $dbExists;
}
public static function InstallPostgresScriptingLanguage()
{
$con = Propel::getConnection();
@ -215,29 +237,33 @@ class AirtimeInstall
$sql = 'SELECT COUNT(*) FROM pg_language WHERE lanname = \'plpgsql\'';
$langIsInstalled = $con->query($sql)->fetchColumn(0);
if ($langIsInstalled == '0') {
echo " * Installing Postgres scripting language".PHP_EOL;
echo ' * Installing Postgres scripting language' . PHP_EOL;
$sql = "CREATE LANGUAGE 'plpgsql'";
AirtimeInstall::InstallQuery($sql, false);
} else {
echo " * Postgres scripting language already installed".PHP_EOL;
echo ' * Postgres scripting language already installed' . PHP_EOL;
}
}
public static function CreateDatabaseTables($p_dbuser, $p_dbpasswd, $p_dbname, $p_dbhost)
{
echo " * Creating database tables".PHP_EOL;
echo ' * Creating database tables' . PHP_EOL;
// Put Propel sql files in Database
//$command = AirtimeInstall::CONF_DIR_WWW."/library/propel/generator/bin/propel-gen ".AirtimeInstall::CONF_DIR_WWW."/build/ insert-sql 2>/dev/null";
$dir = self::GetAirtimeSrcDir()."/build/sql/";
$files = array("schema.sql", "sequences.sql", "views.sql", "triggers.sql", "defaultdata.sql");
foreach ($files as $f){
$command = "export PGPASSWORD=$p_dbpasswd && /usr/bin/psql --username $p_dbuser --dbname $p_dbname --host $p_dbhost --file $dir$f 2>&1";
$dir = self::GetAirtimeSrcDir() . '/build/sql/';
$files = ['schema.sql', 'sequences.sql', 'views.sql', 'triggers.sql', 'defaultdata.sql'];
foreach ($files as $f) {
$command = "export PGPASSWORD={$p_dbpasswd} && /usr/bin/psql --username {$p_dbuser} --dbname {$p_dbname} --host {$p_dbhost} --file {$dir}{$f} 2>&1";
@exec($command, $output, $results);
}
AirtimeInstall::$databaseTablesCreated = true;
}
public final static function UpdateDatabaseTables() {
final public static function UpdateDatabaseTables()
{
UpgradeManager::doUpgrade();
}
public static function SetAirtimeVersion($p_version)
{
$con = Propel::getConnection();
@ -245,59 +271,68 @@ class AirtimeInstall
$con->exec($sql);
Application_Model_Preference::SetAirtimeVersion($p_version);
}
public static function SetUniqueId()
{
$uniqueId = md5(uniqid("", true));
$uniqueId = md5(uniqid('', true));
Application_Model_Preference::SetUniqueId($uniqueId);
}
public static function GetAirtimeVersion()
{
$config = Config::getConfig();
return $config['airtime_version'];
}
public static function DeleteFilesRecursive($p_path)
{
$command = "rm -rf \"$p_path\"";
$command = "rm -rf \"{$p_path}\"";
exec($command);
}
public static function InstallPhpCode()
{
$CC_CONFIG = Config::getConfig();
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);
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.'"');
echo '* Removing PHP code from ' . AirtimeInstall::CONF_DIR_WWW . PHP_EOL;
exec('rm -rf "' . AirtimeInstall::CONF_DIR_WWW . '"');
}
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_WWW,
AirtimeIni::CONF_FILE_AIRTIME,
AirtimeIni::CONF_FILE_LIQUIDSOAP,
AirtimeIni::CONF_FILE_PYPO,
AirtimeIni::CONF_FILE_RECORDER,
"/usr/lib/airtime/pypo",
"/var/log/airtime",
"/var/log/airtime/pypo",
"/var/tmp/airtime/pypo");
foreach ($dirs as $f) {
if (file_exists($f)) {
echo "+ $f".PHP_EOL;
} else {
echo "- $f".PHP_EOL;
}
}
echo 'Legend: "+" means the dir/file exists, "-" means that it does not.' . PHP_EOL;
$dirs = [AirtimeInstall::CONF_DIR_BINARIES,
AirtimeInstall::CONF_DIR_WWW,
AirtimeIni::CONF_FILE_AIRTIME,
AirtimeIni::CONF_FILE_LIQUIDSOAP,
AirtimeIni::CONF_FILE_PYPO,
AirtimeIni::CONF_FILE_RECORDER,
'/usr/lib/airtime/pypo',
'/var/log/airtime',
'/var/log/airtime/pypo',
'/var/tmp/airtime/pypo', ];
foreach ($dirs as $f) {
if (file_exists($f)) {
echo "+ {$f}" . PHP_EOL;
} else {
echo "- {$f}" . PHP_EOL;
}
}
}
public static function CreateZendPhpLogFile(){
public static function CreateZendPhpLogFile()
{
$CC_CONFIG = Config::getConfig();
$path = AirtimeInstall::CONF_DIR_LOG;
$file = $path.'/zendphp.log';
if (!file_exists($path)){
$file = $path . '/zendphp.log';
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
touch($file);
@ -305,52 +340,62 @@ class AirtimeInstall
chown($file, $CC_CONFIG['webServerUser']);
chgrp($file, $CC_CONFIG['webServerUser']);
}
public static function RemoveLogDirectories(){
public static function RemoveLogDirectories()
{
$path = AirtimeInstall::CONF_DIR_LOG;
echo "* Removing logs directory ".$path.PHP_EOL;
exec("rm -rf \"$path\"");
echo '* Removing logs directory ' . $path . PHP_EOL;
exec("rm -rf \"{$path}\"");
}
public static function removeVirtualEnvDistributeFile(){
echo "* Removing distribute-0.6.10.tar.gz".PHP_EOL;
if(file_exists('/usr/share/python-virtualenv/distribute-0.6.10.tar.gz')){
exec("rm -f /usr/share/python-virtualenv/distribute-0.6.10.tar.gz");
public static function removeVirtualEnvDistributeFile()
{
echo '* Removing distribute-0.6.10.tar.gz' . PHP_EOL;
if (file_exists('/usr/share/python-virtualenv/distribute-0.6.10.tar.gz')) {
exec('rm -f /usr/share/python-virtualenv/distribute-0.6.10.tar.gz');
}
}
public static function printUsage($opts)
{
$msg = $opts->getUsageMessage();
echo PHP_EOL."Usage: airtime-install [options]";
echo substr($msg, strpos($msg, "\n")).PHP_EOL;
echo PHP_EOL . 'Usage: airtime-install [options]';
echo substr($msg, strpos($msg, "\n")) . PHP_EOL;
}
public static function getOpts()
{
try {
$autoloader = Zend_Loader_Autoloader::getInstance();
$opts = new Zend_Console_Getopt(
array(
[
'help|h' => 'Displays usage information.',
'overwrite|o' => 'Overwrite any existing config files.',
'preserve|p' => 'Keep any existing config files.',
'no-db|n' => 'Turn off database install.',
'reinstall|r' => 'Force a fresh install of this Airtime Version',
'webonly|w' => 'Install only web files'
)
'webonly|w' => 'Install only web files',
]
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
print $e->getMessage() .PHP_EOL;
echo $e->getMessage() . PHP_EOL;
AirtimeInstall::printUsage($opts);
return NULL;
return null;
}
return $opts;
}
public static function checkPHPVersion()
{
if (PHP_VERSION_ID < 50300)
{
echo "Error: Airtime requires PHP 5.3 or greater.";
if (PHP_VERSION_ID < 50300) {
echo 'Error: Airtime requires PHP 5.3 or greater.';
return false;
}
return true;
}
}

View file

@ -8,7 +8,8 @@ class TestHelper
//pass to the adapter the submitted username and password
$authAdapter->setIdentity('admin')
->setCredential('admin');
->setCredential('admin')
;
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
@ -25,13 +26,14 @@ class TestHelper
public static function getDbZendConfig()
{
$config = Config::getConfig();
return new Zend_Config(
array(
'host' => $config['dsn']['hostspec'],
'dbname' => $config['dsn']['database'],
[
'host' => $config['dsn']['hostspec'],
'dbname' => $config['dsn']['database'],
'username' => $config['dsn']['username'],
'password' => $config['dsn']['password']
)
'password' => $config['dsn']['password'],
]
);
}
@ -40,36 +42,35 @@ class TestHelper
//We need to load the config before our app bootstrap runs. The config
//is normally
$CC_CONFIG = Config::getConfig();
$dbuser = $CC_CONFIG['dsn']['username'];
$dbpasswd = $CC_CONFIG['dsn']['password'];
$dbname = $CC_CONFIG['dsn']['database'];
$dbhost = $CC_CONFIG['dsn']['hostspec'];
$databaseAlreadyExists = AirtimeInstall::createDatabase();
if ($databaseAlreadyExists)
{
if ($databaseAlreadyExists) {
//Truncate all the tables
$con = Propel::getConnection();
$sql = "select * from pg_tables where tableowner = '${dbuser}'";
$sql = "select * from pg_tables where tableowner = '{$dbuser}'";
try {
$rows = $con->query($sql)->fetchAll();
} catch (Exception $e) {
$rows = array();
$rows = [];
}
//Add any tables that shouldn't be cleared here.
// cc_subjs - Most of Airtime requires an admin account to work, which has id=1,
// so don't clear it.
// cc_music_dirs - Has foreign key constraints against cc_files, so we clear cc_files
// cc_music_dirs - Has foreign key constraints against cc_files, so we clear cc_files
// first and clear cc_music_dirs after
$tablesToNotClear = array("cc_subjs", "cc_music_dirs");
$tablesToNotClear = ['cc_subjs', 'cc_music_dirs'];
$con->beginTransaction();
foreach ($rows as $row) {
$tablename = $row["tablename"];
if (in_array($tablename, $tablesToNotClear))
{
$tablename = $row['tablename'];
if (in_array($tablename, $tablesToNotClear)) {
continue;
}
//echo " * Clearing database table $tablename...";
@ -77,50 +78,51 @@ class TestHelper
//TRUNCATE is actually slower than DELETE in many cases:
//http://stackoverflow.com/questions/11419536/postgresql-truncation-speed
//$sql = "TRUNCATE TABLE $tablename CASCADE";
$sql = "DELETE FROM $tablename";
$sql = "DELETE FROM {$tablename}";
AirtimeInstall::InstallQuery($sql, false);
}
//Now that cc_files is empty, clearing cc_music_dirs should work
$sql = "DELETE FROM cc_music_dirs";
$sql = 'DELETE FROM cc_music_dirs';
AirtimeInstall::InstallQuery($sql, false);
// Because files are stored relative to their watch directory,
// we need to set the "stor" path before we can successfully
// create a fake file in the database.
//Copy paste from airtime-db-install.php:
$stor_dir = "/tmp";
$stor_dir = '/tmp';
$con = Propel::getConnection();
$sql = "INSERT INTO cc_music_dirs (directory, type) VALUES ('$stor_dir', 'stor')";
$sql = "INSERT INTO cc_music_dirs (directory, type) VALUES ('{$stor_dir}', 'stor')";
try {
$con->exec($sql);
} catch (Exception $e) {
echo " * Failed inserting {$stor_dir} in cc_music_dirs".PHP_EOL;
echo " * Message {$e->getMessage()}".PHP_EOL;
echo " * Failed inserting {$stor_dir} in cc_music_dirs" . PHP_EOL;
echo " * Message {$e->getMessage()}" . PHP_EOL;
return false;
}
$con->commit();
//Because we're DELETEing all the rows instead of using TRUNCATE (for speed),
//we have to reset the sequences so the auto-increment columns (like primary keys)
//all start at 1 again. This is hacky but it still lets all of this execute fast.
$sql = "SELECT c.relname FROM pg_class c WHERE c.relkind = 'S'";
try {
$rows = $con->query($sql)->fetchAll();
} catch (Exception $e) {
$rows = array();
$rows = [];
}
$con->beginTransaction();
foreach ($rows as $row) {
$seqrelname= $row["relname"];
$sql = "ALTER SEQUENCE ${seqrelname} RESTART WITH 1";
$seqrelname = $row['relname'];
$sql = "ALTER SEQUENCE {$seqrelname} RESTART WITH 1";
AirtimeInstall::InstallQuery($sql, false);
}
$con->commit();
}
else
{
} else {
//Create all the database tables
AirtimeInstall::CreateDatabaseTables($dbuser, $dbpasswd, $dbname, $dbhost);
AirtimeInstall::UpdateDatabaseTables();
@ -131,6 +133,7 @@ class TestHelper
{
$application = new Zend_Application(APPLICATION_ENV, CONFIG_PATH . 'application.ini');
$application->bootstrap();
return $application;
}
}