2014-12-09 23:48:16 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2015-01-15 22:33:33 +01:00
|
|
|
* Author: sourcefabric
|
2021-10-11 16:10:47 +02:00
|
|
|
* Date: 08/12/14.
|
2014-12-09 23:48:16 +01:00
|
|
|
*
|
|
|
|
* Class MediaSetup
|
|
|
|
*
|
|
|
|
* Wrapper class for validating and setting up media folder during the installation process
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
class MediaSetup extends Setup
|
|
|
|
{
|
|
|
|
public const MEDIA_FOLDER = 'mediaFolder';
|
2015-06-24 01:02:55 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
public static $path;
|
|
|
|
public static $message;
|
|
|
|
public static $errors = [];
|
2014-12-09 23:48:16 +01:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
public function __construct($settings)
|
|
|
|
{
|
2014-12-09 23:48:16 +01:00
|
|
|
self::$path = $settings[self::MEDIA_FOLDER];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array associative array containing a display message and fields with errors
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function runSetup()
|
|
|
|
{
|
2014-12-09 23:48:16 +01:00
|
|
|
// If the path passed in is empty, set it to the default
|
|
|
|
if (strlen(self::$path) == 0) {
|
2022-02-04 11:00:41 +01:00
|
|
|
self::$path = INSTALLER_DEFAULT_STORAGE_PATH;
|
|
|
|
if (!file_exists(INSTALLER_DEFAULT_STORAGE_PATH)) {
|
|
|
|
mkdir(INSTALLER_DEFAULT_STORAGE_PATH, 0755, true);
|
2014-12-11 18:56:07 +01:00
|
|
|
}
|
2014-12-09 23:48:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append a trailing / if they didn't
|
2021-10-11 16:10:47 +02:00
|
|
|
if (!(substr(self::$path, -1) == '/')) {
|
|
|
|
self::$path .= '/';
|
2014-12-09 23:48:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists(self::$path)) {
|
|
|
|
$this->setupMusicDirectory();
|
|
|
|
} else {
|
2021-10-11 16:10:47 +02:00
|
|
|
self::$message = 'Invalid path!';
|
2014-12-09 23:48:16 +01:00
|
|
|
self::$errors[] = self::MEDIA_FOLDER;
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2022-02-04 11:00:41 +01:00
|
|
|
// Finalize and move installer config file to libretime config file
|
|
|
|
if (file_exists(LIBRETIME_CONFIG_DIR)) {
|
2015-01-12 19:41:39 +01:00
|
|
|
if (!$this->moveAirtimeConfig()) {
|
2022-02-04 11:00:41 +01:00
|
|
|
self::$message = 'Error moving or deleting the installer config!';
|
2021-10-11 16:10:47 +02:00
|
|
|
self::$errors[] = 'ERR';
|
2015-06-24 01:02:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
/*
|
|
|
|
* If we're upgrading from an old Airtime instance (pre-2.5.2) we rename their old
|
2015-01-15 22:33:33 +01:00
|
|
|
* airtime.conf to airtime.conf.tmp during the setup process. Now that we're done,
|
|
|
|
* we can rename it to airtime.conf.bak to avoid confusion.
|
|
|
|
*/
|
2022-02-04 11:00:41 +01:00
|
|
|
$fileName = LIBRETIME_CONFIG_FILEPATH;
|
2017-02-20 21:36:13 +01:00
|
|
|
$tmpFile = $fileName . '.tmp';
|
|
|
|
$bakFile = $fileName . '.bak';
|
|
|
|
if (file_exists($tmpFile)) {
|
|
|
|
rename($tmpFile, $bakFile);
|
2015-01-15 22:33:33 +01:00
|
|
|
}
|
2015-01-12 19:41:39 +01:00
|
|
|
} else {
|
2015-06-24 01:02:55 +02:00
|
|
|
self::$message = "Failed to move airtime.conf; /etc/airtime doesn't exist!";
|
2021-10-11 16:10:47 +02:00
|
|
|
self::$errors[] = 'ERR';
|
2015-01-12 19:41:39 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
return [
|
|
|
|
'message' => self::$message,
|
|
|
|
'errors' => self::$errors,
|
|
|
|
];
|
2014-12-09 23:48:16 +01:00
|
|
|
}
|
2015-06-24 01:02:55 +02:00
|
|
|
|
2015-01-12 19:41:39 +01:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Moves /tmp/airtime.conf.temp to /etc/airtime.conf and then removes it to complete setup.
|
|
|
|
*
|
|
|
|
* @return bool false if either of the copy or removal operations fail
|
2015-01-12 19:41:39 +01:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function moveAirtimeConfig()
|
|
|
|
{
|
2022-02-04 11:00:41 +01:00
|
|
|
return copy(INSTALLER_CONFIG_FILEPATH, LIBRETIME_CONFIG_FILEPATH)
|
|
|
|
&& unlink(INSTALLER_CONFIG_FILEPATH);
|
2015-01-12 19:41:39 +01:00
|
|
|
}
|
2014-12-09 23:48:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the given directory to cc_music_dirs
|
|
|
|
* TODO Should we check for an existing entry in cc_music_dirs?
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function setupMusicDirectory()
|
|
|
|
{
|
2014-12-09 23:48:16 +01:00
|
|
|
try {
|
2022-02-04 11:00:41 +01:00
|
|
|
$_SERVER['LIBRETIME_CONFIG_FILEPATH'] = INSTALLER_CONFIG_FILEPATH;
|
|
|
|
Propel::init(PROPEL_CONFIG_FILEPATH);
|
2014-12-09 23:48:16 +01:00
|
|
|
$con = Propel::getConnection();
|
2021-10-11 16:10:47 +02:00
|
|
|
} catch (Exception $e) {
|
2014-12-09 23:48:16 +01:00
|
|
|
self::$message = "Failed to insert media folder; database isn't configured properly!";
|
|
|
|
self::$errors[] = self::MEDIA_FOLDER;
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-12-09 23:48:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->runMusicDirsQuery($con);
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
public function runMusicDirsQuery($con)
|
|
|
|
{
|
2014-12-09 23:48:16 +01:00
|
|
|
try {
|
|
|
|
if ($this->checkMusicDirectoryExists($con)) {
|
|
|
|
$dir = CcMusicDirsQuery::create()->findPk(1, $con);
|
|
|
|
} else {
|
|
|
|
$dir = new CcMusicDirs();
|
|
|
|
}
|
|
|
|
|
|
|
|
$dir->setDirectory(self::$path)
|
2021-10-11 16:10:47 +02:00
|
|
|
->setType('stor')
|
2022-01-23 19:15:55 +01:00
|
|
|
->save();
|
2021-10-11 16:10:47 +02:00
|
|
|
self::$message = 'Successfully set up media folder!';
|
2014-12-09 23:48:16 +01:00
|
|
|
Propel::close();
|
|
|
|
unset($_SERVER['AIRTIME_CONF']);
|
|
|
|
} catch (Exception $e) {
|
2021-10-11 16:10:47 +02:00
|
|
|
self::$message = 'Failed to insert ' . self::$path . ' into cc_music_dirs';
|
2014-12-09 23:48:16 +01:00
|
|
|
self::$errors[] = self::MEDIA_FOLDER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
public function checkMusicDirectoryExists($con)
|
|
|
|
{
|
2014-12-09 23:48:16 +01:00
|
|
|
$entry = CcMusicDirsQuery::create()->findPk(1, $con);
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-12-09 23:48:16 +01:00
|
|
|
return isset($entry) && $entry;
|
|
|
|
}
|
2017-07-18 22:27:19 +02:00
|
|
|
}
|