/include/serverconfig.class.php

https://github.com/ultramega/tempservers · PHP · 143 lines · 85 code · 0 blank · 58 comment · 11 complexity · 3631a1d59202669efa94bfbbdf4a264e MD5 · raw file

  1. <?php
  2. /**
  3. * ServerConfig class
  4. *
  5. * Handles generating and parsing game server configuration files. Specifically
  6. * designed to work with Valve dedicated servers.
  7. */
  8. class ServerConfig {
  9. public $sid, $cfgData, $isRaw = false;
  10. /**
  11. * Restricted cvars
  12. *
  13. * @var <array> List of variables to block
  14. */
  15. private $restrictedCvars = array('rcon_password');
  16. /**
  17. * Default cvar values
  18. *
  19. * @var <array> Associative array of default cvar values
  20. */
  21. private $defaultCvars = array('hostname' => 'TempServers.com');
  22. /**
  23. * Initialize the configuration
  24. *
  25. * @param <int> $sid Reservation ID
  26. */
  27. public function __construct($sid) {
  28. $this->sid = $sid;
  29. self::set(array_keys($this->defaultCvars), array_values($this->defaultCvars));
  30. }
  31. /**
  32. * Set a list of cvars
  33. *
  34. * @param <array> $cvars List of cvars to set
  35. * @param <array> $vals List of associated values
  36. */
  37. public function set($cvars, $vals) {
  38. $cfgData = '';
  39. foreach($cvars as $i => $cvar) {
  40. $cvar = trim($cvar);
  41. if(!in_array($cvar, $this->restrictedCvars)) {
  42. $cfgData .= sprintf('%s "%s"%s', $cvar, $vals[$i], "\n");
  43. }
  44. }
  45. $this->cfgData = $cfgData;
  46. }
  47. /**
  48. * Parse a configuration file and strip restricted cvars
  49. *
  50. * @param <string> $cfgInput Raw configuration data
  51. */
  52. public function parseCfg($cfgInput) {
  53. $cfgDataList = preg_split('/[\n\r;]/', $cfgInput, -1, PREG_SPLIT_NO_EMPTY);
  54. $cfgData = '';
  55. foreach($cfgDataList as $line) {
  56. if(!in_array(substr(ltrim($line), 0, strpos($line, ' ')), $this->restrictedCvars)) {
  57. $cfgData .= $line . "\n";
  58. }
  59. }
  60. $this->cfgData = $cfgData;
  61. $this->isRaw = true;
  62. }
  63. /**
  64. * Get the full configuration
  65. *
  66. * @param <bool> $array Set to TRUE to return an array
  67. * @return <mixed> Configuration data as text or array
  68. */
  69. public function get($array = false) {
  70. if($array) {
  71. $cfgDataList = explode("\n", $this->cfgData);
  72. $cfgDataArray = array();
  73. foreach($cfgDataList as $line) {
  74. if(preg_match('#^([a-z_]+)\ \"?(.*?)\"?(?:\s*)?(?://.*)?$#i', $line, $matches) > 0) {
  75. $cfgDataArray[$matches[1]] = $matches[2];
  76. }
  77. }
  78. return $cfgDataArray;
  79. }
  80. else {
  81. return $this->cfgData;
  82. }
  83. }
  84. /**
  85. * Check if configuration is raw
  86. *
  87. * @return <bool> TRUE if object contains raw configuration data
  88. */
  89. public function isRaw() {
  90. return $this->isRaw;
  91. }
  92. /**
  93. * Load the configuration from the database
  94. *
  95. * @return <bool> TRUE on success
  96. */
  97. public function fetch() {
  98. if($result = DB::get()->query("SELECT `cfg`, `raw` FROM `config` WHERE `sid` = '" . $this->sid . "'")) {
  99. $row = $result->fetch_assoc();
  100. $result->close();
  101. $this->cfgData = $row['cfg'];
  102. $this->isRaw = (bool)$row['raw'];
  103. return true;
  104. }
  105. return false;
  106. }
  107. /**
  108. * Save the configuration in the database
  109. *
  110. * @return <bool> TRUE on success
  111. */
  112. public function store() {
  113. $cfgData = DB::get()->escape_string($this->cfgData);
  114. return DB::get()->query("INSERT INTO `config` VALUES ('" . $this->sid . "', '" . $cfgData . "', '" . $this->isRaw . "', '') ON DUPLICATE KEY UPDATE `cfg` = '" . $cfgData . "', `raw` = '" . $this->isRaw . "'");
  115. }
  116. /**
  117. * Reset configuration to default
  118. *
  119. * @return <bool> TRUE on success
  120. */
  121. public function resetConfig() {
  122. if(self::fetch()) {
  123. $newConfigArray = array();
  124. $oldConfigArray = self::get(true);
  125. if(array_key_exists('hostname', $oldConfigArray)) {
  126. $newConfigArray['hostname'] = $oldConfigArray['hostname'];
  127. }
  128. else {
  129. $newConfigArray['hostname'] = $this->defaultCvars['hostname'];
  130. }
  131. if(array_key_exists('sv_password', $oldConfigArray)) {
  132. $newConfigArray['sv_password'] = $oldConfigArray['sv_password'];
  133. }
  134. if(array_key_exists('mp_timelimit', $oldConfigArray)) {
  135. $newConfigArray['mp_timelimit'] = $oldConfigArray['mp_timelimit'];
  136. }
  137. self::set(array_keys($newConfigArray), array_values($newConfigArray));
  138. self::store();
  139. return true;
  140. }
  141. return false;
  142. }
  143. }