helpers.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 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. // Globals
  25. $OGPLangPre = "OGP_LANG_";
  26. // Ignore any request with unwanted values at 'm' or 'p'
  27. if( isset($_REQUEST['m']) and !preg_match("/^([a-z]|[0-9]|_|-)+$/i", $_REQUEST['m']) )
  28. unset($_POST['m'], $_GET['m'], $_REQUEST['m']);
  29. if( isset($_REQUEST['p']) and ( !isset($_REQUEST['m']) or !preg_match("/^([a-z]|[0-9]|_|-)+$/i", $_REQUEST['p']) ) )
  30. {
  31. if( isset($_REQUEST['m']) )
  32. unset($_POST['m'], $_GET['m'], $_REQUEST['m']);
  33. unset($_POST['p'], $_GET['p'], $_REQUEST['p']);
  34. }
  35. if(file_exists(__DIR__ . "/lang.php")){
  36. require_once(__DIR__ . "/lang.php");
  37. }else{
  38. require_once("lang.php");
  39. }
  40. /// \return the database object when the creation was successfull.
  41. /// \return FALSE if database type was invalid.
  42. /// \return negative value in case of error
  43. function createDatabaseConnection($db_type,$db_host,$db_user,$db_pass,$db_name,$table_prefix)
  44. {
  45. if ( $db_type == "mysql" )
  46. {
  47. if ( function_exists('mysqli_connect') )
  48. require_once("includes/database_mysqli.php");
  49. else
  50. die("<p class='failure'>OGP requires the <a href='http://php.net/manual/en/book.mysqli.php' target='_blank'>mysqli PHP extension</a>. Please install it, and then try again.</p>");
  51. $database = new OGPDatabaseMysql();
  52. $connect_value = $database->connect($db_host,$db_user,$db_pass,$db_name,$table_prefix);
  53. if ($connect_value === TRUE)
  54. return $database;
  55. // See return values from database classes.
  56. return $connect_value;
  57. }
  58. else
  59. {
  60. return -98;
  61. }
  62. }
  63. function get_db_error_text ($db_retval, &$error_text)
  64. {
  65. if (is_a($db_retval,"OGPDatabase"))
  66. return FALSE;
  67. switch ($db_retval) {
  68. case -1:
  69. $error_text = get_lang("db_error_invalid_host");
  70. break;
  71. case -11:
  72. $error_text = get_lang("db_error_invalid_user_and_pass");
  73. break;
  74. case -12:
  75. $error_text = get_lang("db_error_invalid_database");
  76. break;
  77. case -98:
  78. $error_text = get_lang("db_error_invalid_db_type");
  79. break;
  80. case -99:
  81. $error_text = get_lang("db_error_module_missing");
  82. break;
  83. default:
  84. $error_text = get_lang_f("db_unknown_error",$db_retval);
  85. break;
  86. }
  87. return TRUE;
  88. }
  89. // Create a list of files or folders and store them in an array
  90. function makefilelist($folder, $filter, $sort=true, $type="files") {
  91. $res = array();
  92. $filter = explode("|", $filter);
  93. $temp = opendir($folder);
  94. while ($file = readdir($temp)) {
  95. if ($type == "files" && !in_array($file, $filter)) {
  96. if (!is_dir($folder.$file)) $res[] = $file;
  97. } elseif ($type == "folders" && !in_array($file, $filter)) {
  98. if (is_dir($folder.$file)) $res[] = $file;
  99. }
  100. }
  101. closedir($temp);
  102. if ($sort) sort($res);
  103. return $res;
  104. }
  105. function isPortValid($port)
  106. {
  107. return ( $port > 0 && $port <= 65535 );
  108. }
  109. function cleanFilenames($file_array)
  110. {
  111. $retval = array();
  112. foreach($file_array as $file_name)
  113. {
  114. if($file_name === "." && $file_name === "..")
  115. continue;
  116. /// \todo @ is because of files without . in the name.extension.
  117. @list($value, $ext) = explode(".", $file_name);
  118. array_push($retval,$value);
  119. }
  120. return $retval;
  121. }
  122. function clean_id_string($id_string)
  123. {
  124. return preg_replace("/-/","",$id_string);
  125. }
  126. function get_first_existing_file($paths, $referrer = "", $agent = "")
  127. {
  128. foreach ($paths as $path)
  129. {
  130. if(preg_match("/^http/", $path))
  131. {
  132. if(cURLEnabled()){
  133. // Get headers using cURL
  134. $file_headers = @get_headers_curl($path, $referrer, $agent);
  135. }else{
  136. // Five second timeout...
  137. $origSocketTimeout = ini_get('default_socket_timeout');
  138. ini_set('default_socket_timeout', 5);
  139. // Get headers with a socket timeout value of 5 seconds...
  140. if(isset($agent) && !empty($agent)){
  141. stream_context_set_default(
  142. array(
  143. 'http' => array(
  144. 'method' => 'GET',
  145. 'user_agent' => $agent
  146. )
  147. )
  148. );
  149. }
  150. $file_headers = @get_headers($path);
  151. // Reset timeout to old value
  152. ini_set('default_socket_timeout', $origSocketTimeout);
  153. }
  154. if(trim($file_headers[0]) == 'HTTP/1.0 200 OK' || trim($file_headers[0]) == 'HTTP/1.1 200 OK') return $path;
  155. }
  156. if (file_exists($path)) return $path;
  157. }
  158. return false;
  159. }
  160. function cURLEnabled(){
  161. return function_exists('curl_version');
  162. }
  163. function get_headers_curl($url, $referrer = "", $agent = "")
  164. {
  165. $ch = curl_init();
  166. curl_setopt($ch, CURLOPT_URL, $url);
  167. curl_setopt($ch, CURLOPT_HEADER, true);
  168. curl_setopt($ch, CURLOPT_NOBODY, true);
  169. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  170. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  171. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  172. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  173. if(isset($referrer) && !empty($referrer)){
  174. curl_setopt($ch, CURLOPT_REFERER, $referrer);
  175. }
  176. if(isset($agent) && !empty($agent)){
  177. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  178. }
  179. // 5 second timeout should be reasonable...
  180. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  181. $r = curl_exec($ch);
  182. $r = explode("\n", $r);
  183. if(is_array($r)){
  184. $r = array_filter($r, 'strlen');
  185. // Get rid of the last index value which appears to be holding odd characters
  186. array_pop($r);
  187. }
  188. return $r;
  189. }
  190. function clean_path($path)
  191. {
  192. // Replace multiple / or \ marks with one /.
  193. return preg_replace("/[\/|\\\\]+/","/",$path);
  194. }
  195. function sanitizeInputStr($strToProcess, $removeHTML = true, $trim = true, $removeQuotes = true){
  196. // Remove quotes from string
  197. if($removeQuotes){
  198. // For magic quotes or addslashes values
  199. $strToProcess = str_replace('\"', '', $strToProcess);
  200. $strToProcess = str_replace("\'", "", $strToProcess);
  201. // Remove any possible leftovers
  202. $strToProcess = str_replace('"', '', $strToProcess);
  203. $strToProcess = str_replace("'", "", $strToProcess);
  204. }
  205. // Trim string value
  206. if($trim){
  207. $strToProcess = trim($strToProcess);
  208. }
  209. // Remove HTML tags
  210. if($removeHTML){
  211. $strToProcess = strip_tags($strToProcess);
  212. }
  213. // Return the processed string
  214. return $strToProcess;
  215. }
  216. function startSession(){
  217. if(!isset($_SESSION)){
  218. session_name("opengamepanel_web");
  219. session_start();
  220. }
  221. }
  222. function updateGameConfigsPostInstall($clear_old = false){
  223. global $db;
  224. if(file_exists('modules/config_games/server_config_parser.php')){
  225. require_once('modules/config_games/server_config_parser.php');
  226. }else{
  227. require_once(__DIR__ . '/../modules/config_games/server_config_parser.php');
  228. }
  229. if(function_exists("read_server_config")){
  230. removeOldGameConfigs();
  231. $files = glob(SERVER_CONFIG_LOCATION."*.xml");
  232. if ( empty($files) )
  233. {
  234. return false;
  235. }
  236. $db->clearGameCfgs($clear_old);
  237. $counter = 0;
  238. foreach ( $files as $config_file )
  239. {
  240. $config = read_server_config($config_file);
  241. if ( !$db->addGameCfg($config) )
  242. {
  243. $counter++;
  244. continue;
  245. }
  246. }
  247. if($counter == count($files)){
  248. return false;
  249. }
  250. return true;
  251. }
  252. return false;
  253. }
  254. function isCoreModule($module){
  255. $coreModules = array('modulemanager', 'server', 'settings', 'gamemanager', 'config_games', 'administration', 'user_games', 'user_admin', 'update');
  256. if(in_array($module, $coreModules)){
  257. return true;
  258. }
  259. return false;
  260. }
  261. function recursiveDelete($str) {
  262. if (file_exists($str) && is_file($str)) {
  263. return @unlink($str);
  264. }else if(file_exists($str) && is_dir($str)){
  265. // Strip the trailing slash from the directory if there is one
  266. $str = rtrim($str,'/');
  267. // Get the index of the last slash in the path so that we can pull just the relative folder name being scanned
  268. $lastSlash = strrpos($str, "/");
  269. if($lastSlash != false){
  270. // Get the folder name so we can ignore "." and ".." which relates to current directory and up a level
  271. $folder = substr($str, $lastSlash + 1);
  272. if($folder != ".." && $folder != "."){
  273. $scan = glob($str . '/{,.}*', GLOB_BRACE);
  274. if(isset($scan) && is_array($scan)){
  275. foreach($scan as $index=>$path) {
  276. recursiveDelete($path);
  277. }
  278. }
  279. return @rmdir($str);
  280. }
  281. }
  282. }
  283. return true;
  284. }
  285. function removeOldGameConfigs(){ // Wrote this function in-case we rename config files like we did for TS3 (https://sourceforge.net/p/hldstart/svn/3376/)
  286. $oldConfigsToRemove = array(
  287. 'modules/config_games/server_configs/ins_win32.xml',
  288. 'modules/config_games/server_configs/ins.xml',
  289. 'modules/config_games/server_configs/insurgency.xml',
  290. 'modules/config_games/server_configs/left_4_dead.xml',
  291. 'modules/config_games/server_configs/left_4_dead2.xml',
  292. 'modules/config_games/server_configs/left_4_dead2_win.xml',
  293. 'modules/config_games/server_configs/warsow.xml',
  294. 'modules/config_games/server_configs/big_brother_bot.xml',
  295. 'modules/config_games/server_configs/big_brother_bot_win.xml',
  296. 'modules/config_games/server_configs/egs_win64.xml',
  297. 'modules/config_games/server_configs/7_days_to_die_linux.xml',
  298. 'modules/config_games/server_configs/7_days_to_die_linux64.xml',
  299. 'modules/config_games/server_configs/TrackManiaForever.xml',
  300. 'modules/config_games/server_configs/trackmania_nations.xml',
  301. 'modules/config_games/server_configs/ventrilo.xml',
  302. 'modules/config_games/server_configs/ventrilo_win.xml',
  303. 'modules/config_games/server_configs/vice_city_multiplayer.xml',
  304. 'modules/config_games/server_configs/vice_city_multiplayer_win.xml',
  305. 'modules/config_games/server_configs/san_andreas_multiplayer.xml',
  306. 'modules/config_games/server_configs/san_andreas_multiplayer_win.xml',
  307. 'modules/config_games/server_configs/MultiTheftAuto.xml',
  308. 'modules/config_games/server_configs/MultiTheftAuto_win.xml',
  309. 'modules/config_games/server_configs/alienvspredator2010.xml',
  310. 'modules/config_games/server_configs/cod_mw2_win.xml',
  311. 'modules/config_games/server_configs/cod_uo_win.xml',
  312. 'modules/config_games/server_configs/cod_1_win.xml',
  313. 'modules/config_games/server_configs/cod_mw3_win.xml',
  314. 'modules/config_games/server_configs/cod2.xml',
  315. 'modules/config_games/server_configs/cod2_win.xml',
  316. 'modules/config_games/server_configs/cod4.xml',
  317. 'modules/config_games/server_configs/cod4_win.xml',
  318. 'modules/config_games/server_configs/cod5.xml',
  319. 'modules/config_games/server_configs/cod_world_at_war_win.xml',
  320. 'modules/config_games/server_configs/minecraft_tekkit.xml',
  321. 'modules/config_games/server_configs/minecraft_tekkit_win.xml',
  322. 'modules/config_games/server_configs/minecraft_bukkit.xml',
  323. 'modules/config_games/server_configs/minecraft_bukkit_win.xml',
  324. 'modules/config_games/server_configs/minecraft_server.xml',
  325. 'modules/config_games/server_configs/minecraft_server_win.xml',
  326. 'modules/config_games/server_configs/life_is_feudal_win32.xml',
  327. 'modules/config_games/server_configs/cs2d.xml',
  328. 'modules/config_games/server_configs/openttd.xml',
  329. 'modules/config_games/server_configs/ark_linux.xml',
  330. 'modules/config_games/server_configs/ark_win.xml',
  331. 'modules/config_games/server_configs/teamspeak3_32bit.xml',
  332. 'modules/config_games/server_configs/teamspeak3_64bit.xml'
  333. );
  334. foreach($oldConfigsToRemove as $config){
  335. recursiveDelete($config);
  336. }
  337. }
  338. function removeOldPanelFiles(){ // Should run post panel update to remove old files that are no longer users
  339. $oldFiles = array(
  340. 'includes/database_mysql.php',
  341. );
  342. foreach($oldFiles as $file){
  343. recursiveDelete($file);
  344. }
  345. }
  346. function runPostUpdateOperations(){
  347. if(file_exists('modules/cron/shared_cron_functions.php')){
  348. // Update cronjobs to use the new token based API
  349. require_once('modules/cron/shared_cron_functions.php');
  350. if(function_exists("updateCronJobsToNewApi")){
  351. updateCronJobsToNewApi();
  352. }
  353. }
  354. if(function_exists("updateAllPanelModules")){
  355. updateAllPanelModules();
  356. }
  357. if(function_exists("removeOldPanelFiles")){
  358. removeOldPanelFiles();
  359. }
  360. if(!array_key_exists("users_api_key", $_SESSION)){
  361. $_SESSION['users_api_key'] = $db->getApiToken($_SESSION['user_id']);
  362. }
  363. }
  364. function getOGPGitHubURL($gitHubUsername, $repo){
  365. $OGPGitHub = "https://github.com/OpenGamePanel/";
  366. $gitHubURL = $OGPGitHub;
  367. if(isset($gitHubUsername) && !empty($gitHubUsername)){
  368. $gitHubURL = "https://github.com/" . $gitHubUsername . "/";
  369. }
  370. $paths[] = $gitHubURL . $repo . "/commits/master.atom";
  371. $exists = get_first_existing_file($paths);
  372. if($exists !== false){
  373. return $gitHubURL;
  374. }
  375. return $OGPGitHub;
  376. }
  377. function getOGPGitHubURLUnstrict($gitHubUsername){
  378. $OGPGitHub = "https://github.com/OpenGamePanel/";
  379. $gitHubURL = $OGPGitHub;
  380. if(isset($gitHubUsername) && !empty($gitHubUsername)){
  381. $gitHubURL = "https://github.com/" . $gitHubUsername . "/";
  382. }
  383. $paths[] = $gitHubURL;
  384. $exists = get_first_existing_file($paths);
  385. if($exists !== false){
  386. return $gitHubURL;
  387. }
  388. return $OGPGitHub;
  389. }
  390. function getGitHubOrganization($gitHubURL){
  391. $gitHubOrg = "OpenGamePanel";
  392. $githubCom = "github.com";
  393. if(substr($gitHubURL, -1) == "/" && stripos($gitHubURL, $githubCom) !== false){
  394. // Get the immediate folder after github.com
  395. $gitHubOrg = substr($gitHubURL, stripos($gitHubURL, $githubCom) + strlen($githubCom) + 1);
  396. // Strip last forward slash
  397. $gitHubOrg = substr($gitHubOrg, 0, -1);
  398. }
  399. return $gitHubOrg;
  400. }
  401. function getOGPLangConstantsJSON(){
  402. global $OGPLangPre;
  403. $finalConsts = array();
  404. $consts = get_defined_constants(true);
  405. foreach($consts["user"] as $key => $value){
  406. if(startsWith($key, $OGPLangPre)){
  407. $finalConsts[$key] = $value;
  408. }
  409. }
  410. if(count($finalConsts) > 0){
  411. return json_encode(utf8ize($finalConsts));
  412. }
  413. return false;
  414. }
  415. ?>