PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/masterserver/web/server/include/C4Masterserver.php

https://bitbucket.org/mortimer/openclonk2
PHP | 199 lines | 84 code | 15 blank | 100 comment | 13 complexity | 4e6b63eb75f9a48b84f0c4b64a8cee68 MD5 | raw file
Possible License(s): WTFPL, 0BSD, LGPL-2.1, CC-BY-3.0
  1. <?php
  2. /**
  3. * Provides functionality to add, update and remove
  4. * game references based on a MySQL table.
  5. *
  6. * @package C4Masterserver
  7. * @version 1.2.0-en
  8. * @author Benedict Etzel <b.etzel@live.de>
  9. * @license http://creativecommons.org/licenses/by/3.0/ CC-BY 3.0
  10. */
  11. class C4Masterserver {
  12. /**
  13. * Stores the masterserver version.
  14. *
  15. * @var string
  16. */
  17. static public $version = '1.2.0';
  18. /**
  19. * Stores the MySQL connection resource.
  20. *
  21. * @var resource
  22. */
  23. private $link;
  24. /**
  25. * Stores the MySQL table prefix.
  26. *
  27. * @var string
  28. */
  29. private $config;
  30. /**
  31. * Stores the seconds after which games timeout.
  32. *
  33. * @var int
  34. */
  35. private $timeoutgames;
  36. /**
  37. * Stores the seconds after which games are removed.
  38. *
  39. * @var int
  40. */
  41. private $deletegames;
  42. /**
  43. * Stores the maximum amount of games per ip.
  44. *
  45. * @var int
  46. */
  47. private $maxgames;
  48. /**
  49. * The C4Masterserver constructor.
  50. *
  51. * @param resource $link
  52. * @return C4Masterserver
  53. */
  54. public function __construct($link, $config) {
  55. $this->link = $link;
  56. $this->config = $config;
  57. $this->timeoutgames = 600;
  58. $this->deletegames = 60 * 60 * 24;
  59. $this->maxgames = 5;
  60. }
  61. /**
  62. * Returns the C4Masterserver version.
  63. *
  64. * @param void
  65. * @return string
  66. */
  67. public static function getVersion() {
  68. return(C4Masterserver::$version);
  69. }
  70. /**
  71. * Sets the seconds after which games timeout.
  72. *
  73. * @param int $timeoutgames
  74. * @return void
  75. */
  76. public function setTimeoutgames($timeoutgames) {
  77. $this->timeoutgames = $timeoutgames;
  78. }
  79. /**
  80. * Sets the seconds after which games are removed.
  81. *
  82. * @param int $deletegames
  83. * @return void
  84. */
  85. public function setDeletegames($deletegames) {
  86. $this->deletegames = $deletegames;
  87. }
  88. /**
  89. * Sets the maximum amount of games per ip.
  90. *
  91. * @param int $maxgames
  92. * @return void
  93. */
  94. public function setMaxgames($maxgames) {
  95. $this->maxgames = $maxgames;
  96. }
  97. /**
  98. * Returns all valid references. If $show_all is true, it returns all references.
  99. *
  100. * @param bool $show_all
  101. * @return array
  102. */
  103. public function getReferenceArray($show_all = false) {
  104. if (!$this->link)
  105. return false;
  106. $append = $show_all ? '' : 'WHERE `valid` = \'1\'';
  107. $result = mysql_query('SELECT * FROM `' . ParseINI::parseValue('mysql_prefix', $this->config) . 'games`' . $append);
  108. $list = array();
  109. while ($row = mysql_fetch_assoc($result)) {
  110. $list[$row['id']] = $row;
  111. }
  112. return $list;
  113. }
  114. /**
  115. * Adds a new reference and returns the new CSID.
  116. *
  117. * @param string $reference
  118. * @return string
  119. */
  120. public function addReference($reference) {
  121. if (!$this->link)
  122. return false;
  123. $reference = str_replace('0.0.0.0', $_SERVER['REMOTE_ADDR'], $reference);
  124. $query = mysql_query('SELECT * FROM `' . ParseINI::parseValue('mysql_prefix', $this->config) . 'games` WHERE `ip` = \'' . $_SERVER['REMOTE_ADDR'] . '\' AND `valid` = \'1\'', $this->link);
  125. if (mysql_num_rows($query) > $this->maxgames) {
  126. return false;
  127. }
  128. $csid = sha1(uniqid(mt_rand(), true));
  129. mysql_query('INSERT INTO `' . ParseINI::parseValue('mysql_prefix', $this->config) . 'games` (`id`, `ip`,`csid`, `data`, `start`, `time`, `valid`) VALUES (\'\', \'' . $_SERVER['REMOTE_ADDR'] . '\', \'' . $csid . '\', \'' . $reference . '\', \'' . time() . '\', \'' . time() . '\', \'1\')', $this->link);
  130. return $csid;
  131. }
  132. /**
  133. * Updates a reference.
  134. *
  135. * @param string $csid
  136. * @param string $newreference
  137. * @return bool
  138. */
  139. public function updateReference($csid, $newreference) {
  140. if (!$this->link)
  141. return false;
  142. $newreference = str_replace('0.0.0.0', $_SERVER['REMOTE_ADDR'], $newreference);
  143. mysql_query('UPDATE `' . ParseINI::parseValue('mysql_prefix', $this->config) . 'games` SET `data`=\'' . $newreference . '\',`time` = \'' . time() . '\', `valid` = \'1\' WHERE `csid` = \'' . $csid . '\'', $this->link);
  144. return true;
  145. }
  146. /**
  147. * Removes a reference by setting it invalid.
  148. *
  149. * @param string $csid
  150. * @return bool
  151. */
  152. public function removeReference($csid) {
  153. if (!$this->link)
  154. return false;
  155. mysql_query('UPDATE `' . ParseINI::parseValue('mysql_prefix', $this->config) . 'games` SET `time` = \'' . time() . '\', `valid` = \'0\' WHERE `csid` = \'' . $csid . '\'', $this->link);
  156. return true;
  157. }
  158. /**
  159. * Sets old references invalid and removes very old ones if $remove is true.
  160. *
  161. * @param void
  162. * @return void
  163. */
  164. public function cleanUp() {
  165. if (!$this->link)
  166. return false;
  167. if ($this->timeoutgames != 0) {
  168. mysql_query('UPDATE `' . ParseINI::parseValue('mysql_prefix', $this->config) . 'games` SET `valid` = \'0\' WHERE `time` < \'' . (time() - $this->timeoutgames) . '\'', $this->link);
  169. }
  170. if ($this->deletegames != 0) {
  171. mysql_query('DELETE FROM `' . ParseINI::parseValue('mysql_prefix', $this->config) . 'games` WHERE `valid` = \'0\' AND `time` < \'' . (time() - $this->deletegames) . '\'', $this->link);
  172. }
  173. }
  174. public function getDownloadURL($platform) {
  175. $result = mysql_query('SELECT `file` FROM `' . ParseINI::parseValue('mysql_prefix', $this->config) . 'update` WHERE `old_version` = \'\' AND `platform` = \'' . $platform . '\'');
  176. if ($result) {
  177. $row = mysql_fetch_assoc($result);
  178. if ($row['file'])
  179. return ParseINI::parseValue('oc_update_url', $this->config) . $row['file'];
  180. }
  181. return false;
  182. }
  183. }
  184. ?>