media-setup.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. define("CONFIG_PATH", dirname(dirname( __DIR__)) . "/application/configs/");
  3. define("DEFAULT_STOR_DIR", "/srv/airtime/stor/");
  4. require_once(dirname(dirname( __DIR__)) . "/library/propel/runtime/lib/Propel.php");
  5. require_once(CONFIG_PATH . 'conf.php');
  6. require_once(dirname(dirname( __DIR__)) . "/application/models/airtime/map/CcMusicDirsTableMap.php");
  7. require_once(dirname(dirname( __DIR__)) . "/application/models/airtime/om/BaseCcMusicDirsQuery.php");
  8. require_once(dirname(dirname( __DIR__)) . "/application/models/airtime/CcMusicDirsQuery.php");
  9. require_once(dirname(dirname( __DIR__)) . "/application/models/airtime/om/BaseCcMusicDirs.php");
  10. require_once(dirname(dirname( __DIR__)) . "/application/models/airtime/CcMusicDirs.php");
  11. require_once(dirname(dirname( __DIR__)) . "/application/models/airtime/om/BaseCcMusicDirsPeer.php");
  12. require_once(dirname(dirname( __DIR__)) . "/application/models/airtime/CcMusicDirsPeer.php");
  13. /**
  14. * Author: sourcefabric
  15. * Date: 08/12/14
  16. *
  17. * Class MediaSetup
  18. *
  19. * Wrapper class for validating and setting up media folder during the installation process
  20. */
  21. class MediaSetup extends Setup {
  22. const MEDIA_FOLDER = "mediaFolder";
  23. const AIRTIME_CONF_PATH = "/etc/airtime/airtime.conf";
  24. static $path;
  25. static $message = null;
  26. static $errors = array();
  27. function __construct($settings) {
  28. self::$path = $settings[self::MEDIA_FOLDER];
  29. }
  30. /**
  31. * @return array associative array containing a display message and fields with errors
  32. */
  33. function runSetup() {
  34. // If the path passed in is empty, set it to the default
  35. if (strlen(self::$path) == 0) {
  36. self::$path = DEFAULT_STOR_DIR;
  37. if (!file_exists(DEFAULT_STOR_DIR)) {
  38. mkdir(DEFAULT_STOR_DIR, 0755, true);
  39. }
  40. }
  41. // Append a trailing / if they didn't
  42. if (!(substr(self::$path, -1) == "/")) {
  43. self::$path .= "/";
  44. }
  45. if (file_exists(self::$path)) {
  46. $this->setupMusicDirectory();
  47. } else {
  48. self::$message = "Invalid path!";
  49. self::$errors[] = self::MEDIA_FOLDER;
  50. }
  51. // Finalize and move airtime.conf.temp
  52. if (file_exists("/etc/airtime/")) {
  53. if (!$this->moveAirtimeConfig()) {
  54. $message = "Error moving airtime.conf or deleting /tmp/airtime.conf.temp!";
  55. $errors[] = "ERR";
  56. }
  57. /*
  58. * If we're upgrading from an old Airtime instance (pre-2.5.2) we rename their old
  59. * airtime.conf to airtime.conf.tmp during the setup process. Now that we're done,
  60. * we can rename it to airtime.conf.bak to avoid confusion.
  61. */
  62. if (file_exists(self::AIRTIME_CONF_PATH . ".tmp")) {
  63. rename(self::AIRTIME_CONF_PATH . ".tmp", self::AIRTIME_CONF_PATH . ".bak");
  64. }
  65. } else {
  66. $message = "Failed to move airtime.conf; /etc/airtime doesn't exist!";
  67. $errors[] = "ERR";
  68. }
  69. return array(
  70. "message" => self::$message,
  71. "errors" => self::$errors
  72. );
  73. }
  74. /**
  75. * Moves /tmp/airtime.conf.temp to /etc/airtime.conf and then removes it to complete setup
  76. * @return boolean false if either of the copy or removal operations fail
  77. */
  78. function moveAirtimeConfig() {
  79. return copy(AIRTIME_CONF_TEMP_PATH, self::AIRTIME_CONF_PATH)
  80. && unlink(AIRTIME_CONF_TEMP_PATH);
  81. }
  82. /**
  83. * Add the given directory to cc_music_dirs
  84. * TODO Should we check for an existing entry in cc_music_dirs?
  85. */
  86. function setupMusicDirectory() {
  87. try {
  88. $_SERVER['AIRTIME_CONF'] = AIRTIME_CONF_TEMP_PATH;
  89. Propel::init(CONFIG_PATH . "airtime-conf-production.php");
  90. $con = Propel::getConnection();
  91. } catch(Exception $e) {
  92. self::$message = "Failed to insert media folder; database isn't configured properly!";
  93. self::$errors[] = self::MEDIA_FOLDER;
  94. return;
  95. }
  96. $this->runMusicDirsQuery($con);
  97. }
  98. function runMusicDirsQuery($con) {
  99. try {
  100. if ($this->checkMusicDirectoryExists($con)) {
  101. $dir = CcMusicDirsQuery::create()->findPk(1, $con);
  102. } else {
  103. $dir = new CcMusicDirs();
  104. }
  105. $dir->setDirectory(self::$path)
  106. ->setType("stor")
  107. ->save();
  108. self::$message = "Successfully set up media folder!";
  109. Propel::close();
  110. unset($_SERVER['AIRTIME_CONF']);
  111. } catch (Exception $e) {
  112. self::$message = "Failed to insert " . self::$path . " into cc_music_dirs";
  113. self::$errors[] = self::MEDIA_FOLDER;
  114. }
  115. }
  116. function checkMusicDirectoryExists($con) {
  117. $entry = CcMusicDirsQuery::create()->findPk(1, $con);
  118. return isset($entry) && $entry;
  119. }
  120. }