functions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2014 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. #functions go here
  25. //read_expire() converts a time stamp to a human readable form
  26. //Used as a count down to when the user's account expires
  27. //Example would be User's account expires in 200 days, 20 hours, 18 minutes
  28. function read_expire($endtime)
  29. {
  30. #Feed the user's expire time stamp to this, and it returns a human readable date
  31. if($endtime == 'X')
  32. {
  33. $timediff = 'X';
  34. return $timediff;
  35. }
  36. //Thanks to manhon824 at gmail dot com - found on http://us2.php.net/manual/en/function.mktime.php
  37. $starttime=time();
  38. // the start time can change to =strtotime($endtime);
  39. //$endtime=strtotime($expires);
  40. // $endtime = $expires;
  41. //$endtime can be any format as well as it can be converted to secs
  42. $timediff = $endtime-$starttime;
  43. $days=intval($timediff/86400);
  44. $remain=$timediff%86400;
  45. $hours=intval($remain/3600);
  46. $remain=$remain%3600;
  47. $mins=intval($remain/60);
  48. $secs=$remain%60;
  49. //this code is copied from the other note!thx to that guy!
  50. $stampdiff = $timediff;
  51. $timediff=$days.' days '.$hours.' hr '.$mins.' min ';
  52. return $timediff;
  53. }
  54. function genRandomString($length) {
  55. $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
  56. $string = "";
  57. for ($p = 0; $p < $length; $p++) {
  58. $string .= $characters[mt_rand(0, strlen($characters)-1)];
  59. }
  60. return $string;
  61. }
  62. function get_map_path($query_name,$mod,$map) {
  63. $mod_gt = $mod;
  64. if($mod == "cstrike")
  65. {
  66. if ($query_name == "halflife")
  67. $mod_gt = "cs";
  68. elseif($query_name == "source")
  69. $mod_gt = "css";
  70. }
  71. if($mod == "tf")
  72. {
  73. if ($query_name == "halflife")
  74. $mod_gt = "tf";
  75. elseif($query_name == "source")
  76. $mod_gt = "tf2";
  77. }
  78. $mod_gt = $mod == "fof" ? "hl2dm" : $mod_gt;
  79. $mod_gt = $mod == "insurgency" ? "ins" : $mod_gt;
  80. $mod_gt = $mod == "redorchestra2" ? "ro2" : $mod_gt;
  81. $mod_gt = $mod == "killingfloor2" ? "kf2" : $mod_gt;
  82. $mod_gt = $query_name == "callofduty" ? "cod" : $mod_gt;
  83. $mod_gt = $query_name == "callofduty2" ? "cod2" : $mod_gt;
  84. $mod_gt = $query_name == "callofduty4" ? "cod4" : $mod_gt;
  85. $map_paths= array(
  86. "http://image.www.gametracker.com/images/maps/160x120/$mod_gt/$map.jpg",
  87. "http://image.www.gametracker.com/images/maps/160x120/$query_name/$map.jpg",
  88. "protocol/lgsl/maps/$query_name/$mod/$map.jpg",
  89. "protocol/lgsl/maps/$query_name/$mod/$map.gif",
  90. "protocol/lgsl/maps/$query_name/$mod/$map.png",
  91. "protocol/lgsl/maps/$query_name/$map.jpg",
  92. "protocol/lgsl/maps/$query_name/$map.gif",
  93. "protocol/lgsl/maps/$query_name/$map.png",
  94. "images/online_big.png"
  95. );
  96. return get_first_existing_file($map_paths);
  97. }
  98. //Refreshed Div:
  99. //Below is under construction:
  100. // Usage: $data .= PopupData($id);
  101. // PopupBuild($data);
  102. function PopupTrigger($id){
  103. return "<a href='#' class='ex".$id."trigger'></a>";
  104. }
  105. function PopupData($id,$link){//Trigger: ex($id)trigger
  106. return "$('#ex".$id."').jqm({ajax: '$link', trigger: 'a.ex".$id."trigger'});";
  107. }
  108. function PopupBuild($data){
  109. return "<script type='text/javascript'>$(document).ready(function()\{".$data."\});</script>";
  110. }
  111. function create_home_selector($module, $subpage, $server_homes) {
  112. if ($server_homes == "show_all")
  113. {
  114. echo "<form method='GET' action=''>\n";
  115. echo "<input type='hidden' name='m' value='".$module."' />\n";
  116. if( $subpage ) echo "<input type='hidden' name='p' value='".$subpage."' />\n";
  117. echo "<input type='SUBMIT' value='" . get_lang('show_all') . "'/>\n";
  118. echo "</form>\n";
  119. }
  120. else
  121. {
  122. foreach ($server_homes as $key => $row) {
  123. $home_name[$key] = $row['home_name'];
  124. $home_id[$key] = $row['home_id'];
  125. $mod_id[$key] = $row['home_id'];
  126. $ip[$key] = $row['ip'];
  127. $port[$key] = $row['port'];
  128. }
  129. array_multisort($home_name, $ip, $port, $mod_id, $home_id, SORT_DESC, $server_homes);
  130. echo "<form method='GET' name='select' action=''>\n";
  131. echo "<input type='hidden' name='m' value='".$module."' />\n";
  132. if( $subpage ) echo "<input type='hidden' name='p' value='".$subpage."' />\n";
  133. echo "<select onchange=".'"this.form.submit()"'." name='home_id'>\n";
  134. echo "<option></option>\n";
  135. foreach ( $server_homes as $server_home )
  136. {
  137. if( isset( $_GET['home_id'] ) and $_GET['home_id'] == $server_home['home_id'] )
  138. $selected = 'selected="selected"';
  139. else
  140. $selected = '';
  141. echo "<option value='". $server_home['home_id'] . "' $selected >" . htmlentities($server_home['home_name']) . "</option>\n";
  142. }
  143. echo "</select>\n";
  144. echo "</form>";
  145. }
  146. }
  147. function create_home_selector_address($module, $subpage, $server_homes, $extra_inputs = FALSE, $method = "GET") {
  148. if( isset($_GET['home_id-mod_id-ip-port']) and $_GET['home_id-mod_id-ip-port'] != "" )
  149. {
  150. list($get_home_id,
  151. $get_mod_id,
  152. $get_ip,
  153. $get_port) = explode( "-", $_GET['home_id-mod_id-ip-port'] );
  154. }
  155. echo "<form method='$method' name='select' action=''>\n";
  156. echo "<input type='hidden' name='m' value='$module' />\n";
  157. if( $subpage ) echo "<input type='hidden' name='p' value='".$subpage."' />\n";
  158. if($extra_inputs)
  159. {
  160. foreach($extra_inputs as $input)
  161. {
  162. echo "<input type='$input[type]' name='$input[name]' value='$input[value]' />\n";
  163. }
  164. }
  165. echo "<select onchange=\"this.form.submit();\" name='home_id-mod_id-ip-port'>\n";
  166. echo "<option></option>\n";
  167. foreach ($server_homes as $key => $row) {
  168. if( !isset($row['ip']) or !isset($row['mod_id']) )
  169. {
  170. unset($server_homes[$key]);
  171. continue;
  172. }
  173. $home_name[$key] = $row['home_name'];
  174. $home_id[$key] = $row['home_id'];
  175. $mod_id[$key] = $row['home_id'];
  176. $ip[$key] = $row['ip'];
  177. $port[$key] = $row['port'];
  178. }
  179. array_multisort($home_name, $ip, $port, $mod_id,
  180. $home_id, SORT_DESC, $server_homes);
  181. foreach ( $server_homes as $server_home )
  182. {
  183. if(isset($_GET['home_id-mod_id-ip-port']) and
  184. $get_home_id == $server_home['home_id'] and
  185. $get_mod_id == $server_home['mod_id'] and
  186. $get_ip == $server_home['ip'] and
  187. $get_port == $server_home['port'])
  188. $selected = 'selected="selected"';
  189. else
  190. $selected = '';
  191. echo "<option value='". $server_home['home_id'] .
  192. "-" . $server_home['mod_id'] . "-" . $server_home['ip'] .
  193. "-" . $server_home['port'] . "' $selected >" .
  194. htmlentities($server_home['home_name']) . " - " . $server_home['ip'] .
  195. ":" . $server_home['port'] . "</option>\n";
  196. }
  197. echo "</select>\n";
  198. echo "</form>";
  199. }
  200. function create_home_selector_game_type($module, $subpage, $server_homes) {
  201. echo "<form method='GET' name='select' action=''>\n".
  202. "<input type='hidden' name='m' value='".$module."' />\n";
  203. if( $subpage != "" ) echo "<input type='hidden' name='p' value='".$subpage."' />\n";
  204. echo "<select onchange=".'"this.form.submit()"'." name='home_cfg_id'>\n".
  205. "<option>".get_lang('game_type')."</option>\n";
  206. $servers_by_game_name = array();
  207. foreach( $server_homes as $server_home )
  208. {
  209. if( !isset($server_home['ip']) or !isset($server_home['mod_id']) )
  210. continue;
  211. $servers_by_game_name["$server_home[game_name]"] = $server_home['home_cfg_id'];
  212. }
  213. ksort($servers_by_game_name);
  214. foreach( $servers_by_game_name as $game_name => $home_cfg_id )
  215. {
  216. $selected = (isset($_GET['home_cfg_id']) and $_GET['home_cfg_id'] == $home_cfg_id) ? 'selected="selected"' : "";
  217. echo "<option value='". $home_cfg_id . "' $selected >" . $game_name . "</option>\n";
  218. }
  219. echo "</select>\n</form>\n";
  220. }
  221. function mymail($email_address, $subject, $message, $panel_settings, $user_to_panel = FALSE){
  222. global $view;
  223. if( empty( $panel_settings['panel_name'] ) )
  224. $panel_name = "Open Game Panel";
  225. else
  226. $panel_name = $panel_settings['panel_name'];
  227. include('PHPMailer/class.phpmailer.php');
  228. // Create the mail object using the Mail::factory method
  229. $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
  230. $mail->IsSMTP(); // telling the class to use SMTP
  231. $mymail = TRUE;
  232. try
  233. {
  234. $mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
  235. // 1 = errors and messages
  236. // 2 = messages only
  237. // SMTP server
  238. if (isset($panel_settings['smtp_server']) and !empty($panel_settings['smtp_server']))
  239. $mail->Host = $panel_settings['smtp_server'];
  240. else
  241. $mail->Host = 'localhost';
  242. // set the SMTP port
  243. if (isset($panel_settings['smtp_port']) and !empty($panel_settings['smtp_port']))
  244. $mail->Port = $panel_settings['smtp_port'];
  245. else
  246. $mail->Port = 25;
  247. // sets the prefix to the servier
  248. if (isset($panel_settings['smtp_ssl']) and $panel_settings['smtp_ssl'] == 1)
  249. $mail->SMTPSecure = "ssl";
  250. elseif (isset($panel_settings['smtp_tls']) and $panel_settings['smtp_tls'] == 1)
  251. $mail->SMTPSecure = "tls";
  252. if (isset($panel_settings['smtp_login']) and !empty($panel_settings['smtp_login']))
  253. {
  254. // enable SMTP authentication
  255. $mail->SMTPAuth = true;
  256. // SMTP username
  257. $mail->Username = $panel_settings['smtp_login'];
  258. if (isset($panel_settings['smtp_passw']) and !empty($panel_settings['smtp_passw']))
  259. {
  260. // SMTP password
  261. $mail->Password = $panel_settings['smtp_passw'];
  262. }
  263. }
  264. if(empty($panel_settings['panel_email_address'])){
  265. $panel_email = "[email protected]";
  266. }else{
  267. $panel_email = $panel_settings['panel_email_address'];
  268. }
  269. $email_addresses = explode( ",", $email_address );
  270. if( $user_to_panel )
  271. {
  272. $mail->AddAddress($panel_email);
  273. $user_to_panel = is_bool($user_to_panel) ? "" : $user_to_panel; // True boolean or user name string
  274. foreach ( $email_addresses as $address )
  275. {
  276. $mail->SetFrom($address,$user_to_panel);
  277. $mail->AddReplyTo($address,$user_to_panel);
  278. }
  279. }
  280. else // panel to user
  281. {
  282. foreach ( $email_addresses as $address )
  283. {
  284. $mail->AddAddress($address);
  285. }
  286. $mail->SetFrom($panel_email,$panel_name);
  287. $mail->AddReplyTo($panel_email,$panel_name);
  288. }
  289. $mail->CharSet = $view->charset;
  290. $mail->Subject = $subject;
  291. $mail->MsgHTML($message);
  292. $mail->Send();
  293. }
  294. catch (phpmailerException $e)
  295. {
  296. $mymail = FALSE;
  297. echo $e->errorMessage(); //Pretty error messages from PHPMailer
  298. }
  299. catch (Exception $e)
  300. {
  301. $mymail = FALSE;
  302. echo $e->getMessage(); //Boring error messages from anything else!
  303. }
  304. return $mymail;
  305. }
  306. if( !function_exists("sys_get_temp_dir") )
  307. {
  308. function sys_get_temp_dir(){
  309. if ($temp = getenv('TMP') ) {
  310. if (file_exists($temp)) { return realpath($temp); }
  311. }
  312. if ($temp = getenv('TEMP') ) {
  313. if (file_exists($temp)) { return realpath($temp); }
  314. }
  315. if ($temp = getenv('TMPDIR') ) {
  316. if (file_exists($temp)) { return realpath($temp); }
  317. }
  318. // trick for creating a file in system's temporary dir
  319. // without knowing the path of the system's temporary dir
  320. $temp = tempnam(__FILE__, '');
  321. if (file_exists($temp)) {
  322. unlink($temp);
  323. return realpath(dirname($temp));
  324. }
  325. return null;
  326. }
  327. }
  328. function set_firewall($remote, $firewall_settings, $action, $port, $ip = FALSE)
  329. {
  330. if($action == "allow" or $action == "deny")
  331. {
  332. if($ip)
  333. $command = str_replace("%IP%",$ip,str_replace("%PORT%",$port,$firewall_settings[$action.'_ip_port_command']));
  334. else
  335. $command = str_replace("%PORT%",$port,$firewall_settings[$action.'_port_command']);
  336. }
  337. if(isset($command))
  338. return $remote->sudo_exec($command);
  339. else
  340. return FALSE;
  341. }
  342. function strip_real_escape_string($text)
  343. {
  344. $search = array('\"', "\'", "\\r", "\\n","\\\\");
  345. $replace = array('"', "'", "\r", "\n", '\\');
  346. $text = str_replace($search, $replace, $text);
  347. return $text;
  348. }
  349. function get_true_boolean($bool)
  350. {
  351. if ( (int) $bool > 0 )
  352. $ret = true;
  353. else
  354. {
  355. $lowered_bool = strtolower($bool); // that could be 'True' or 'true' or 'TRUE', etc...
  356. if( $lowered_bool === "true" || $lowered_bool === "on" || $lowered_bool === "yes" )
  357. $ret = true;
  358. else
  359. $ret = false;
  360. }
  361. return $ret;
  362. }
  363. function get_temp_dir($curdir)
  364. {
  365. $temp = sys_get_temp_dir();
  366. if( ini_get('open_basedir') )
  367. {
  368. $dirs = preg_split( "/:|;/", ini_get('open_basedir') , -1, PREG_SPLIT_NO_EMPTY );
  369. if( !in_array($temp, $dirs) )
  370. $temp = $curdir . DIRECTORY_SEPARATOR . 'temp';
  371. }
  372. if( $temp == null )
  373. $temp = $curdir . DIRECTORY_SEPARATOR . 'temp';
  374. if( !file_exists($temp) )
  375. if( is_writable( dirname($temp) ) ) mkdir($temp);
  376. return $temp;
  377. }
  378. // ### Escape some characters that could break the server startup or make the user capable to run other programs. ###
  379. // \ (backslash) -> At the end of the string, can scape the next quote,
  380. // and is commonly used to create Windows paths, must be escaped.
  381. // " (quote) -> Not escaped quote, without an ending quote, would break the startup command.
  382. // ' (single quote) -> same than quote.
  383. // | (pipe) -> Not escaped pipe would break the startup command and could use the next argument as new command.
  384. // & (ampersand) -> Same than pipe. If double ampersand is used it would run the command (if any) once the server process ends.
  385. // ; (semicolon) -> Same than double ampersand.
  386. // > (greater than) -> Could redirect the server output and ignore the next arguments.
  387. // < (lower than) -> Could send the content of a file to the server executable and ignore the the next arguments.
  388. // ` (apostrophe) -> Could get the return value of a given (system) command or variable.
  389. // $ (¿sigil?) -> Same than apostrophe.
  390. // ( and ) (parenthesis) -> starts or ends a bash/batch statement, could break the server startup
  391. // [ and ] (test) -> test is part of bash language, could break the server startup
  392. function clean_server_param_value($value, $cli_allow_chars) {
  393. $value = strip_real_escape_string($value);
  394. $escape_chars = array("\\", "\"", "'", "|", "&", ";", ">", "<", "`", "$", "(", ")", "[", "]");
  395. if($cli_allow_chars)
  396. {
  397. $cli_allow_chars = str_split($cli_allow_chars);
  398. $escape_chars = array_diff($escape_chars, $cli_allow_chars);
  399. }
  400. $find = array();
  401. $repl = array();
  402. foreach($escape_chars as $char)
  403. {
  404. $find[] = '%'.preg_quote($char).'%';
  405. $char = $char == '\\' ? preg_quote('\\\\') : $char;
  406. $repl[] = '\\'.$char;
  407. }
  408. return preg_replace($find, $repl, $value);
  409. }
  410. // ### Validate FTP user/password and control_protocol_password. ###
  411. function validate_login($value) {
  412. $value = strip_real_escape_string($value);
  413. $value = trim($value);
  414. $find = '%\\\\|"|\||&|;|>|<|`|\$|\s%';
  415. return preg_match($find, $value) ? FALSE : $value;
  416. }
  417. // Order a multidimensional array by keys. Source http://php.net/manual/es/function.array-multisort.php#100534
  418. function array_orderby()
  419. {
  420. $args = func_get_args();
  421. $data = array_shift($args);
  422. foreach ($args as $n => $field)
  423. {
  424. if (is_string($field))
  425. {
  426. $tmp = array();
  427. foreach ($data as $key => $row)
  428. $tmp[$key] = $row[$field];
  429. $args[$n] = $tmp;
  430. }
  431. }
  432. $args[] = &$data;
  433. call_user_func_array('array_multisort', $args);
  434. return array_pop($args);
  435. }
  436. // Escape a single quote or multiple single quotes
  437. // in a string that is passed to bash
  438. // and this string is single quoted
  439. function esc_squote($str)
  440. {
  441. return preg_replace("#('+)#", "'\"\${1}\"'", $str);
  442. }
  443. function get_game_selector($os, $game_cfgs, $home_cfg_id = FALSE)
  444. {
  445. if(preg_match("/64/", $os))
  446. {
  447. $arch_64_bit = true;
  448. }
  449. if(preg_match("/linux/i", $os))
  450. {
  451. if(preg_match("/wine/i", $os))
  452. {
  453. $os_match = $arch_64_bit ? '/(win|linux)(32|64)?$/i' : '/(win|linux)(32)?$/i';
  454. }
  455. else
  456. {
  457. $os_match = $arch_64_bit ? '/(linux)(32|64)?$/i' : '/(linux)(32)?$/i';
  458. }
  459. }
  460. elseif(preg_match("/cygwin/i", $os))
  461. {
  462. $os_match = $arch_64_bit ? '/(win)(32|64)?$/i' : '/(win)(32)?$/i';
  463. }
  464. else
  465. {
  466. $os_match = '/(win|linux)(32|64)?$/i';
  467. }
  468. $selector = "";
  469. foreach ( $game_cfgs as $row )
  470. {
  471. if ( preg_match($os_match, $row['game_key'], $matches) )
  472. {
  473. $selector .= "<option value='".$row['home_cfg_id']."' ".
  474. ($home_cfg_id == $row['home_cfg_id'] ? 'selected="selected"' : "").
  475. ">".$row['game_name'].
  476. (preg_match('/^linux$/i', $matches[1]) ? " (Linux" : " (Windows").
  477. ((isset($matches[2]) and $matches[2] == '64') ? " 64bit)" : ")").
  478. "</option>\n";
  479. }
  480. }
  481. return $selector;
  482. }
  483. function getClientIPAddress(){
  484. if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) && !empty($_SERVER['HTTP_CF_CONNECTING_IP'])){
  485. return $_SERVER['HTTP_CF_CONNECTING_IP'];
  486. }else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  487. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  488. }else if(isset($_SERVER['HTTP_X_REAL_IP']) && !empty($_SERVER['HTTP_X_REAL_IP'])){
  489. return $_SERVER['HTTP_X_REAL_IP'];
  490. }else{
  491. return $_SERVER['REMOTE_ADDR'];
  492. }
  493. }
  494. ?>