PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/includes.php

https://github.com/holblin/urt_webpanel
PHP | 329 lines | 246 code | 45 blank | 38 comment | 35 complexity | d6c153934b442f003e02ab93815a11dc MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. function get_servers() {
  3. $servers = get_servers_list();
  4. $auto_start = get_servers_autostart();
  5. $status = get_servers_status();
  6. $result = array();
  7. foreach($servers as $name) {
  8. $result[$name] = array(
  9. 'autostart' => isset($auto_start[$name]),
  10. 'port' => get_server_port($name),
  11. 'status' => isset($status[$name]) ? $status[$name] : 'off' );
  12. }
  13. uasort($result, function($a, $b) {
  14. if ($a['port'] == $b['port']) {
  15. return 0;
  16. }
  17. return ($a['port'] < $b['port']) ? -1 : 1;
  18. });
  19. return $result;
  20. }
  21. //==================================================//
  22. // CONFIGURATIONS FILES //
  23. //==================================================//
  24. {
  25. function get_servers_list() {
  26. // TODO
  27. // code youre own get serveur list
  28. $str = shell_exec('ls /etc/urt.d');
  29. $servers = explode("\n", $str);
  30. array_pop($servers); // remove the last (is empty name server because shell finish with \n)
  31. return $servers;
  32. }
  33. function get_servers_autostart() {
  34. // TODO
  35. // code youre own get serveur autostart list
  36. $result = get_var_conf_file('/etc/urt/serveur.conf','SERVERS_ENABLES');
  37. if ($result) {
  38. $result = explode(' ', $result);
  39. $result = array_flip($result); // met le nom des serveurs en clee (supprime les doublond et enleve les vide ?!)
  40. }
  41. return $result;
  42. }
  43. function get_servers_ip() {
  44. // TODO
  45. // code youre own get serveur ip
  46. return get_var_conf_file('/etc/urt/serveur.conf','SRV_IP');
  47. }
  48. function get_server_port($server_name) {
  49. // TODO
  50. // code youre own get serveur port
  51. if (strpos($server_name, '.') !== false)
  52. return false;
  53. return get_var_conf_file('/etc/urt.d/'.$server_name.'/serveur.conf','SRV_PORT');
  54. }
  55. function get_var_conf_file($conf_file, $var) {
  56. $tmp = file_get_contents($conf_file);
  57. $tmp = explode("\n",$tmp);
  58. $result = false;
  59. foreach ($tmp as $val) {
  60. if ( strstr($val,'=') !== false ) {
  61. list($name, $val) = explode('=', $val, 2);
  62. if (trim($name) == $var) {
  63. $result = trim(str_replace('"','', $val));
  64. }
  65. }
  66. }
  67. return $result;
  68. }
  69. }
  70. //==================================================//
  71. // SKELETON SCRIPT //
  72. //==================================================//
  73. {
  74. function get_servers_status() {
  75. // TODO
  76. // code youre own get status here
  77. $tmp = shell_exec( 'sudo -u my_urt_user /etc/init.d/skeleton_urt status');
  78. $tmp = preg_replace('/'.preg_quote('').'\[[0-9]+;[0-9]+m/','',$tmp);
  79. $tmp = preg_replace('/'.preg_quote('').'\[[0-9]+m/','',$tmp);
  80. $tmp = str_replace('[', '' ,$tmp );
  81. $tmp = explode( "\n" ,$tmp );
  82. $status = array();
  83. foreach ($tmp as $key => $val) {
  84. if ($val == '') continue;
  85. list($name, $action) = explode( ']',$val);
  86. $name = trim($name);
  87. $action = trim($action);
  88. $status[$name] = $action == 'is running.' ? 'on' : ( $action == 'is stoped !' ? 'error' : 'unknow' );
  89. }
  90. return $status;
  91. }
  92. function start( $name = null) {
  93. // TODO
  94. // code youre own start here
  95. if ($name === null)
  96. shell_exec( 'sudo -u my_urt_user /etc/init.d/skeleton_urt start');
  97. else
  98. shell_exec( 'sudo -u my_urt_user /etc/init.d/skeleton_urt start '.$name);
  99. }
  100. function stop( $name = null ) {
  101. // TODO
  102. // code youre own stop here
  103. if ($name === null)
  104. shell_exec( 'sudo -u my_urt_user /etc/init.d/skeleton_urt stop');
  105. else
  106. shell_exec( 'sudo -u my_urt_user /etc/init.d/skeleton_urt stop '.$name);
  107. }
  108. function restart( $name = null ) {
  109. // TODO
  110. // code youre own restart here
  111. if ($name === null)
  112. shell_exec( 'sudo -u my_urt_user /etc/init.d/skeleton_urt restart');
  113. else
  114. shell_exec( 'sudo -u my_urt_user /etc/init.d/skeleton_urt restart '.$name);
  115. }
  116. }
  117. //==================================================//
  118. // RCON FUNCTIONS //
  119. //==================================================//
  120. {
  121. function urt_query($ip, $port, $query, $answer = true) {
  122. $fp = fsockopen("udp://$ip",$port, $errno, $errstr, 2);
  123. stream_set_blocking( $fp , 0);
  124. socket_set_timeout($fp,2);
  125. stream_set_timeout($fp,2);
  126. if (!$fp) {
  127. echo "$errstr ($errno)<br/>\n";
  128. } else {
  129. $query = chr(0xff) . chr(0xff) . chr(0xff) . chr(0xff) . $query;
  130. $tmp = fwrite($fp,$query);
  131. }
  132. if ( !$answer)
  133. return;
  134. $data = '';
  135. $tmp1 = microtime(true);
  136. do {
  137. $d = fread ($fp, 2048);
  138. $data .= $d;
  139. if (strlen($d) == 0)
  140. usleep( 50 );
  141. } while ( strlen($d) == 0 && (microtime(true) - $tmp1 < 3 ));
  142. if (microtime(true) - $tmp1 > 3)
  143. return false;
  144. $nb_zero = 0;
  145. do {
  146. $d = fread ($fp, 2048);
  147. $data .= $d;
  148. if (strlen($d) == 0) {
  149. $nb_zero++;
  150. usleep(50);
  151. }else
  152. $nb_zero = 0;
  153. } while ( $nb_zero < 5 );
  154. fclose ($fp);
  155. return $data;
  156. }
  157. function status ($ip, $port ) {
  158. $data = urt_query($ip, $port, 'getstatus' , true );
  159. return explode_backslash($data);
  160. }
  161. function rcon ($ip, $port, $rcon_pass, $command, $answer = true) {
  162. $data = urt_query($ip, $port, 'rcon "' . $rcon_pass . '" ' . $command , $answer );
  163. if ( substr($data,0 , -1) == chr(0xff) . chr(0xff) . chr(0xff) . chr(0xff) . "print\nBad rconpassword." ){ session_destroy(); echo 'Bad rconpassword'; die(); }
  164. if (! $answer)
  165. return $data;
  166. $data = preg_replace ("/....print\n/", "", $data);
  167. return $data;
  168. }
  169. function rcon_recovery( $ip, $port, $recovery_pass) {
  170. // TODO
  171. // code youre own get rcon password here ( https://github.com/holblin/ioq3-for-UrbanTerror-4/commit/5ba6c87d467a64ace1557f127eb331fcd411ffec )
  172. return 'myrconpassword';
  173. }
  174. function rcon_status ($ip, $port, $rcon_pass) {
  175. $data = rcon ($ip, $port, $rcon_pass, "status");
  176. $data = explode ("\n", $data);
  177. # ok, let's deal with the following :
  178. #
  179. # map: q3wcp9
  180. # num score ping name lastmsg address qport rate
  181. # --- ----- ---- --------------- ------- --------------------- ----- -----
  182. # 1 19 33 l33t^n1ck 33 62.212.106.216:27960 5294 25000
  183. $map = array_shift($data); // 1st line : map q3wcp9
  184. $map = preg_replace('/^map: /','',$map);
  185. array_shift($data); // 2nd line : col headers
  186. array_shift($data); // 3rd line : -- ------ ----
  187. array_pop($data);
  188. array_pop($data); // two empty lines at the end, go figure.
  189. $result = array();
  190. foreach ($data as $line) {
  191. $player = $line;
  192. // preg_match_all("/^\s*(\d+)\s*(\d+)\s*(\d+)(.*?)(\d*)\s*(\S*)\s*(\d*)\s*(\d*)\s*$/", $player, $out); // weeeeeeeeee \o/
  193. preg_match_all("/^\s*(\d+)\s*(\-?\d+)\s*(\d+)\s*(.*?)\s*(\d*)\s*(\S*)\s*(\d*)\s*(\d*)\s*$/", $player, $out);
  194. $num = $out[1][0];
  195. $score = $out[2][0];
  196. $ping = $out[3][0];
  197. $name = trim($out[4][0]);
  198. $lastmsg = $out[5][0];
  199. $address = $out[6][0];
  200. if ($address != 'bot')
  201. $address = preg_replace ("/:.*$/", "", $address);
  202. $qport = $out[7][0];
  203. $rate = $out[8][0];
  204. $result[$num] = array(
  205. 'num'=> $num,
  206. 'score'=> $score,
  207. 'ping'=> $ping,
  208. 'name'=> $name,
  209. 'lastmsg'=> $lastmsg,
  210. 'address'=> $address,
  211. 'qport'=> $qport,
  212. 'rate'=> $rate,);
  213. }
  214. return array( 'map' => $map , 'data' => $result);
  215. }
  216. }
  217. //==================================================//
  218. // URT TOOLS FUNCTIONS //
  219. //==================================================//
  220. {
  221. function clean_and_colors( $str ) {
  222. $colors = array(
  223. 0 => 'black',
  224. 1 => 'red',
  225. 2 => 'green',
  226. 3 => 'yellow',
  227. 4 => 'blue',
  228. 5 => 'cyan',
  229. 6 => 'pink',
  230. 7 => 'white',
  231. 8 => 'black');
  232. $str = htmlentities($str);
  233. $i = 0;
  234. $nb = 0;
  235. $pos = strpos($str, '^' , $i);
  236. while ($pos !== false) {
  237. $i = $pos;
  238. if ( isset( $str[$i+1] , $colors[ $str[$i+1] ] ) ) {
  239. $replace = '<span style="color: '.$colors[ $str[$i+1] ].';">';
  240. if ($nb > 0)
  241. $replace = '</span>'.$replace;
  242. $tmp = $i - 1 + strlen($replace);
  243. $str = substr($str, 0, $i ). $replace . substr($str, $i + 2 );
  244. $i = $tmp;
  245. $nb ++;
  246. }
  247. $pos = strpos($str, '^', $i + 1 );
  248. }
  249. if ( $nb > 0 )
  250. $str .= '</span>';
  251. return $str;
  252. }
  253. // This will take the server response and makes it in to readable key value pair
  254. function explode_backslash($data = array()) {
  255. $haystack = explode("\n", $data, 3);
  256. $stack = explode('\\', $haystack[1]);
  257. for ($i = 1;$i <= count($stack);$i = $i + 2) {
  258. $list[@$stack[$i]] = @$stack[$i + 1];
  259. }
  260. $list[] = @$haystack[2];
  261. foreach ($list as $key => $dum) {
  262. if (empty($dum) || !isset($dum)) {
  263. unset($list[$key]);
  264. }
  265. }
  266. return $list;
  267. }
  268. function gametype_to_int($gt) {
  269. $ref = array('ffa'=>1,'tdm'=>3,'ts'=>4,'ftl'=>5,'c&h'=>6,'ctf'=>7,'bomb'=>8);
  270. if (isset($ref[ strtolower( $gt ) ]))
  271. return $ref[ strtolower( $gt ) ];
  272. else
  273. return false;
  274. }
  275. function int_to_gametype($i) {
  276. $ref = array(0=>'FFA',1=>'FFA',2=>'FFA',3=>'TDM',4=>'TS',5=>'FTL',6=>'C&H',7=>'CTF',8=>'BOMB');
  277. if (isset($ref[ intval( $i ) ]))
  278. return $ref[ intval( $i ) ];
  279. else
  280. return false;
  281. }
  282. }