helpers.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2017 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. // Ignore any request with unwanted values at 'm' or 'p'
  25. if( isset($_REQUEST['m']) and !preg_match("/^([a-z]|[0-9]|_|-)+$/i", $_REQUEST['m']) )
  26. unset($_POST['m'], $_GET['m'], $_REQUEST['m']);
  27. if( isset($_REQUEST['p']) and ( !isset($_REQUEST['m']) or !preg_match("/^([a-z]|[0-9]|_|-)+$/i", $_REQUEST['p']) ) )
  28. {
  29. if( isset($_REQUEST['m']) )
  30. unset($_POST['m'], $_GET['m'], $_REQUEST['m']);
  31. unset($_POST['p'], $_GET['p'], $_REQUEST['p']);
  32. }
  33. if(file_exists(__DIR__ . "/lang.php")){
  34. require_once(__DIR__ . "/lang.php");
  35. }else{
  36. require_once("lang.php");
  37. }
  38. /// \return the database object when the creation was successfull.
  39. /// \return FALSE if database type was invalid.
  40. /// \return negative value in case of error
  41. function createDatabaseConnection($db_type,$db_host,$db_user,$db_pass,$db_name,$table_prefix)
  42. {
  43. if ( $db_type == "mysql" )
  44. {
  45. if ( function_exists('mysqli_connect') )
  46. require_once("includes/database_mysqli.php");
  47. else
  48. require_once("includes/database_mysql.php");
  49. $database = new OGPDatabaseMysql();
  50. $connect_value = $database->connect($db_host,$db_user,$db_pass,$db_name,$table_prefix);
  51. if ($connect_value === TRUE)
  52. return $database;
  53. // See return values from database classes.
  54. return $connect_value;
  55. }
  56. else
  57. {
  58. return -98;
  59. }
  60. }
  61. function get_db_error_text ($db_retval, &$error_text)
  62. {
  63. if (is_a($db_retval,"OGPDatabase"))
  64. return FALSE;
  65. switch ($db_retval) {
  66. case -1:
  67. $error_text = get_lang("db_error_invalid_host");
  68. break;
  69. case -11:
  70. $error_text = get_lang("db_error_invalid_user_and_pass");
  71. break;
  72. case -12:
  73. $error_text = get_lang("db_error_invalid_database");
  74. break;
  75. case -98:
  76. $error_text = get_lang("db_error_invalid_db_type");
  77. break;
  78. case -99:
  79. $error_text = get_lang("db_error_module_missing");
  80. break;
  81. default:
  82. $error_text = get_lang_f("db_unknown_error",$db_retval);
  83. break;
  84. }
  85. return TRUE;
  86. }
  87. // Create a list of files or folders and store them in an array
  88. function makefilelist($folder, $filter, $sort=true, $type="files") {
  89. $res = array();
  90. $filter = explode("|", $filter);
  91. $temp = opendir($folder);
  92. while ($file = readdir($temp)) {
  93. if ($type == "files" && !in_array($file, $filter)) {
  94. if (!is_dir($folder.$file)) $res[] = $file;
  95. } elseif ($type == "folders" && !in_array($file, $filter)) {
  96. if (is_dir($folder.$file)) $res[] = $file;
  97. }
  98. }
  99. closedir($temp);
  100. if ($sort) sort($res);
  101. return $res;
  102. }
  103. function isPortValid($port)
  104. {
  105. return ( $port > 0 && $port <= 65535 );
  106. }
  107. function cleanFilenames($file_array)
  108. {
  109. $retval = array();
  110. foreach($file_array as $file_name)
  111. {
  112. if($file_name === "." && $file_name === "..")
  113. continue;
  114. /// \todo @ is because of files without . in the name.extension.
  115. @list($value, $ext) = explode(".", $file_name);
  116. array_push($retval,$value);
  117. }
  118. return $retval;
  119. }
  120. function clean_id_string($id_string)
  121. {
  122. return preg_replace("/-/","",$id_string);
  123. }
  124. function get_first_existing_file($paths, $referrer = "", $agent = "")
  125. {
  126. foreach ($paths as $path)
  127. {
  128. if(preg_match("/^http/", $path))
  129. {
  130. if(cURLEnabled()){
  131. // Get headers using cURL
  132. $file_headers = @get_headers_curl($path, $referrer, $agent);
  133. }else{
  134. // Five second timeout...
  135. $origSocketTimeout = ini_get('default_socket_timeout');
  136. ini_set('default_socket_timeout', 5);
  137. // Get headers with a socket timeout value of 5 seconds...
  138. if(isset($agent) && !empty($agent)){
  139. stream_context_set_default(
  140. array(
  141. 'http' => array(
  142. 'method' => 'GET',
  143. 'user_agent' => $agent
  144. )
  145. )
  146. );
  147. }
  148. $file_headers = @get_headers($path);
  149. // Reset timeout to old value
  150. ini_set('default_socket_timeout', $origSocketTimeout);
  151. }
  152. if(trim($file_headers[0]) == 'HTTP/1.0 200 OK' || trim($file_headers[0]) == 'HTTP/1.1 200 OK') return $path;
  153. }
  154. if (file_exists($path)) return $path;
  155. }
  156. return false;
  157. }
  158. function cURLEnabled(){
  159. return function_exists('curl_version');
  160. }
  161. function get_headers_curl($url, $referrer = "", $agent = "")
  162. {
  163. $ch = curl_init();
  164. curl_setopt($ch, CURLOPT_URL, $url);
  165. curl_setopt($ch, CURLOPT_HEADER, true);
  166. curl_setopt($ch, CURLOPT_NOBODY, true);
  167. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  168. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  169. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  170. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  171. if(isset($referrer) && !empty($referrer)){
  172. curl_setopt($ch, CURLOPT_REFERER, $referrer);
  173. }
  174. if(isset($agent) && !empty($agent)){
  175. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  176. }
  177. // 5 second timeout should be reasonable...
  178. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  179. $r = curl_exec($ch);
  180. $r = explode("\n", $r);
  181. if(is_array($r)){
  182. $r = array_filter($r, 'strlen');
  183. // Get rid of the last index value which appears to be holding odd characters
  184. array_pop($r);
  185. }
  186. return $r;
  187. }
  188. function clean_path($path)
  189. {
  190. // Replace multiple / or \ marks with one /.
  191. return preg_replace("/[\/|\\\\]+/","/",$path);
  192. }
  193. function sanitizeInputStr($strToProcess, $removeHTML = true, $trim = true, $removeQuotes = true){
  194. // Remove quotes from string
  195. if($removeQuotes){
  196. // For magic quotes or addslashes values
  197. $strToProcess = str_replace('\"', '', $strToProcess);
  198. $strToProcess = str_replace("\'", "", $strToProcess);
  199. // Remove any possible leftovers
  200. $strToProcess = str_replace('"', '', $strToProcess);
  201. $strToProcess = str_replace("'", "", $strToProcess);
  202. }
  203. // Trim string value
  204. if($trim){
  205. $strToProcess = trim($strToProcess);
  206. }
  207. // Remove HTML tags
  208. if($removeHTML){
  209. $strToProcess = strip_tags($strToProcess);
  210. }
  211. // Return the processed string
  212. return $strToProcess;
  213. }
  214. function startSession(){
  215. if(!isset($_SESSION)){
  216. session_name("opengamepanel_web");
  217. session_start();
  218. }
  219. }
  220. function updateGameConfigsPostInstall($clear_old = false){
  221. global $db;
  222. if(file_exists('modules/config_games/server_config_parser.php')){
  223. require_once('modules/config_games/server_config_parser.php');
  224. }else{
  225. require_once(__DIR__ . '/../modules/config_games/server_config_parser.php');
  226. }
  227. if(function_exists("read_server_config")){
  228. removeOldGameConfigs();
  229. $files = glob(SERVER_CONFIG_LOCATION."*.xml");
  230. if ( empty($files) )
  231. {
  232. return false;
  233. }
  234. $db->clearGameCfgs($clear_old);
  235. $counter = 0;
  236. foreach ( $files as $config_file )
  237. {
  238. $config = read_server_config($config_file);
  239. if ( !$db->addGameCfg($config) )
  240. {
  241. $counter++;
  242. continue;
  243. }
  244. }
  245. if($counter == count($files)){
  246. return false;
  247. }
  248. return true;
  249. }
  250. return false;
  251. }
  252. function isCoreModule($module){
  253. $coreModules = array('modulemanager', 'server', 'settings', 'gamemanager', 'config_games', 'administration', 'user_games', 'user_admin', 'update');
  254. if(in_array($module, $coreModules)){
  255. return true;
  256. }
  257. return false;
  258. }
  259. function recursiveDelete($str) {
  260. if (is_file($str)) {
  261. return @unlink($str);
  262. }else if(is_dir($str)){
  263. // Strip the trailing slash from the directory if there is one
  264. $str = rtrim($str,'/');
  265. // Get the index of the last slash in the path so that we can pull just the relative folder name being scanned
  266. $lastSlash = strrpos($str, "/");
  267. if($lastSlash != false){
  268. // Get the folder name so we can ignore "." and ".." which relates to current directory and up a level
  269. $folder = substr($str, $lastSlash + 1);
  270. if($folder != ".." && $folder != "."){
  271. $scan = glob($str . '/{,.}*', GLOB_BRACE);
  272. if(isset($scan) && is_array($scan)){
  273. foreach($scan as $index=>$path) {
  274. recursiveDelete($path);
  275. }
  276. }
  277. return @rmdir($str);
  278. }
  279. }
  280. }
  281. return true;
  282. }
  283. function removeOldGameConfigs(){ // Wrote this function in-case we rename config files like we did for TS3 (https://sourceforge.net/p/hldstart/svn/3376/)
  284. $oldConfigsToRemove = array(
  285. 'modules/config_games/server_configs/teamspeak3_32bit.xml',
  286. 'modules/config_games/server_configs/teamspeak3_64bit.xml'
  287. );
  288. foreach($oldConfigsToRemove as $config){
  289. if(file_exists($config)){
  290. unlink($config);
  291. }else{
  292. if(file_exists(__DIR__ . "/../" . $config)){
  293. unlink($config);
  294. }
  295. }
  296. }
  297. }
  298. function getOGPGitHubURL($gitHubUsername, $repo){
  299. $OGPGitHub = "https://github.com/OpenGamePanel/";
  300. $gitHubURL = $OGPGitHub;
  301. if(isset($gitHubUsername) && !empty($gitHubUsername)){
  302. $gitHubURL = "https://github.com/" . $gitHubUsername . "/";
  303. }
  304. $paths[] = $gitHubURL . $repo . "/commits/master.atom";
  305. $exists = get_first_existing_file($paths);
  306. if($exists !== false){
  307. return $gitHubURL;
  308. }
  309. return $OGPGitHub;
  310. }
  311. function getOGPGitHubURLUnstrict($gitHubUsername){
  312. $OGPGitHub = "https://github.com/OpenGamePanel/";
  313. $gitHubURL = $OGPGitHub;
  314. if(isset($gitHubUsername) && !empty($gitHubUsername)){
  315. $gitHubURL = "https://github.com/" . $gitHubUsername . "/";
  316. }
  317. $paths[] = $gitHubURL;
  318. $exists = get_first_existing_file($paths);
  319. if($exists !== false){
  320. return $gitHubURL;
  321. }
  322. return $OGPGitHub;
  323. }
  324. function getGitHubOrganization($gitHubURL){
  325. $gitHubOrg = "OpenGamePanel";
  326. $githubCom = "github.com";
  327. if(substr($gitHubURL, -1) == "/" && stripos($gitHubURL, $githubCom) !== false){
  328. // Get the immediate folder after github.com
  329. $gitHubOrg = substr($gitHubURL, stripos($gitHubURL, $githubCom) + strlen($githubCom) + 1);
  330. // Strip last forward slash
  331. $gitHubOrg = substr($gitHubOrg, 0, -1);
  332. }
  333. return $gitHubOrg;
  334. }
  335. ?>