setup-functions.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. define("BUILD_PATH", dirname(dirname(__DIR__)) . "/build/");
  3. define("AIRTIME_CONF_TEMP_PATH", "/tmp/airtime.conf.temp");
  4. /**
  5. * Class Setup
  6. *
  7. * @author sourcefabric
  8. *
  9. * Abstract superclass for the setup and installation process
  10. */
  11. abstract class Setup {
  12. abstract function __construct($settings);
  13. abstract function runSetup();
  14. /**
  15. * Write new property values to a given section in airtime.conf.temp
  16. *
  17. * @param string $section
  18. * the configuration section to write to
  19. * @param array $properties
  20. * the configuration properties and values to overwrite
  21. */
  22. protected function writeToTemp($section, $properties) {
  23. if (!file_exists(AIRTIME_CONF_TEMP_PATH)) {
  24. copy(BUILD_PATH . "airtime.example.conf", AIRTIME_CONF_TEMP_PATH);
  25. }
  26. $file = file(AIRTIME_CONF_TEMP_PATH);
  27. $fileOutput = "";
  28. $inSection = false;
  29. foreach ($file as $line) {
  30. if (strpos($line, $section) !== false) {
  31. $inSection = true;
  32. } else if (strpos($line, "[") !== false) {
  33. $inSection = false;
  34. }
  35. if (substr(trim($line), 0, 1) == "#") {
  36. /* Workaround to strip comments from airtime.conf.
  37. * We need to do this because python's ConfigObj and PHP
  38. * have different (and equally strict) rules about comment
  39. * characters in configuration files.
  40. */
  41. } else if ($inSection) {
  42. $key = trim(@substr($line, 0, strpos($line, "=")));
  43. $fileOutput .= $key && isset($properties[$key]) ? $key . " = " . $properties[$key] . "\n" : $line;
  44. } else {
  45. $fileOutput .= $line;
  46. }
  47. }
  48. file_put_contents(AIRTIME_CONF_TEMP_PATH, $fileOutput);
  49. }
  50. /**
  51. * Generates a random string.
  52. *
  53. * @param integer $p_len
  54. * length of the output string
  55. * @param string $p_chars
  56. * characters to use in the output string
  57. * @return string the generated random string
  58. */
  59. protected function generateRandomString($p_len = 20, $p_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
  60. $string = '';
  61. for($i = 0; $i < $p_len; $i++) {
  62. $pos = mt_rand(0, strlen($p_chars) - 1);
  63. $string .= $p_chars{$pos};
  64. }
  65. return $string;
  66. }
  67. }
  68. /**
  69. * Class AirtimeDatabaseException
  70. *
  71. * @author sourcefabric
  72. *
  73. * Exception class for database setup errors
  74. */
  75. class AirtimeDatabaseException extends Exception {
  76. protected $message = "Unknown Airtime database exception";
  77. protected $errors = array();
  78. public function __construct($message = null, $errors = array(), $code = 0, Exception $previous = null) {
  79. parent::__construct($message, $code, $previous);
  80. $this->errors = $errors;
  81. }
  82. public function getErrorFields() {
  83. return $this->errors;
  84. }
  85. }
  86. // Import Setup subclasses
  87. require_once ('database-setup.php');
  88. require_once ('rabbitmq-setup.php');
  89. require_once ('general-setup.php');
  90. require_once ('media-setup.php');
  91. // If airtime.conf exists, we shouldn't be here
  92. if (!file_exists("/etc/airtime/airtime.conf")) {
  93. if (isset($_GET["obj"]) && $objType = $_GET["obj"]) {
  94. $obj = new $objType($_POST);
  95. if ($obj instanceof Setup) {
  96. try {
  97. $response = $obj->runSetup();
  98. } catch (AirtimeDatabaseException $e) {
  99. $response = array(
  100. "message" => $e->getMessage(),
  101. "errors" => $e->getErrorFields()
  102. );
  103. }
  104. echo json_encode($response);
  105. }
  106. }
  107. }