helpers.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) Copyright (C) 2008 - 2013 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. }
  157. function cURLEnabled(){
  158. return function_exists('curl_version');
  159. }
  160. function get_headers_curl($url, $referrer = "", $agent = "")
  161. {
  162. $ch = curl_init();
  163. curl_setopt($ch, CURLOPT_URL, $url);
  164. curl_setopt($ch, CURLOPT_HEADER, true);
  165. curl_setopt($ch, CURLOPT_NOBODY, true);
  166. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  167. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  168. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  169. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  170. if(isset($referrer) && !empty($referrer)){
  171. curl_setopt($ch, CURLOPT_REFERER, $referrer);
  172. }
  173. if(isset($agent) && !empty($agent)){
  174. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  175. }
  176. // 5 second timeout should be reasonable...
  177. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  178. $r = curl_exec($ch);
  179. $r = explode("\n", $r);
  180. if(is_array($r)){
  181. $r = array_filter($r, 'strlen');
  182. // Get rid of the last index value which appears to be holding odd characters
  183. array_pop($r);
  184. }
  185. return $r;
  186. }
  187. function clean_path($path)
  188. {
  189. // Replace multiple / or \ marks with one /.
  190. return preg_replace("/[\/|\\\\]+/","/",$path);
  191. }
  192. function sanitizeInputStr($strToProcess, $removeHTML = true, $trim = true, $removeQuotes = true){
  193. // Remove quotes from string
  194. if($removeQuotes){
  195. // For magic quotes or addslashes values
  196. $strToProcess = str_replace('\"', '', $strToProcess);
  197. $strToProcess = str_replace("\'", "", $strToProcess);
  198. // Remove any possible leftovers
  199. $strToProcess = str_replace('"', '', $strToProcess);
  200. $strToProcess = str_replace("'", "", $strToProcess);
  201. }
  202. // Trim string value
  203. if($trim){
  204. $strToProcess = trim($strToProcess);
  205. }
  206. // Remove HTML tags
  207. if($removeHTML){
  208. $strToProcess = strip_tags($strToProcess);
  209. }
  210. // Return the processed string
  211. return $strToProcess;
  212. }
  213. function startSession(){
  214. if(!isset($_SESSION)){
  215. session_name("opengamepanel_web");
  216. session_start();
  217. }
  218. }
  219. function updateGameConfigsPostInstall($clear_old = false){
  220. global $db;
  221. if(file_exists('modules/config_games/server_config_parser.php')){
  222. require_once('modules/config_games/server_config_parser.php');
  223. }else{
  224. require_once(__DIR__ . '/../modules/config_games/server_config_parser.php');
  225. }
  226. if(function_exists("read_server_config")){
  227. removeOldGameConfigs();
  228. $files = glob(SERVER_CONFIG_LOCATION."*.xml");
  229. if ( empty($files) )
  230. {
  231. return false;
  232. }
  233. $db->clearGameCfgs($clear_old);
  234. $counter = 0;
  235. foreach ( $files as $config_file )
  236. {
  237. $config = read_server_config($config_file);
  238. if ( !$db->addGameCfg($config) )
  239. {
  240. $counter++;
  241. continue;
  242. }
  243. }
  244. if($counter == count($files)){
  245. return false;
  246. }
  247. return true;
  248. }
  249. return false;
  250. }
  251. function isCoreModule($module){
  252. $coreModules = array('modulemanager', 'server', 'settings', 'gamemanager', 'config_games', 'administration', 'user_games', 'user_admin', 'update');
  253. if(in_array($module, $coreModules)){
  254. return true;
  255. }
  256. return false;
  257. }
  258. function recursiveDelete($str) {
  259. if (is_file($str)) {
  260. return @unlink($str);
  261. }else if(is_dir($str)){
  262. // Strip the trailing slash from the directory if there is one
  263. $str = rtrim($str,'/');
  264. // Get the index of the last slash in the path so that we can pull just the relative folder name being scanned
  265. $lastSlash = strrpos($str, "/");
  266. if($lastSlash != false){
  267. // Get the folder name so we can ignore "." and ".." which relates to current directory and up a level
  268. $folder = substr($str, $lastSlash + 1);
  269. if($folder != ".." && $folder != "."){
  270. $scan = glob($str . '/{,.}*', GLOB_BRACE);
  271. if(isset($scan) && is_array($scan)){
  272. foreach($scan as $index=>$path) {
  273. recursiveDelete($path);
  274. }
  275. }
  276. return @rmdir($str);
  277. }
  278. }
  279. }
  280. return true;
  281. }
  282. function removeOldGameConfigs(){ // Wrote this function in-case we rename config files like we did for TS3 (https://sourceforge.net/p/hldstart/svn/3376/)
  283. $oldConfigsToRemove = array(
  284. 'modules/config_games/server_configs/teamspeak3_32bit.xml',
  285. 'modules/config_games/server_configs/teamspeak3_64bit.xml'
  286. );
  287. foreach($oldConfigsToRemove as $config){
  288. if(file_exists($config)){
  289. unlink($config);
  290. }else{
  291. if(file_exists(__DIR__ . "/../" . $config)){
  292. unlink($config);
  293. }
  294. }
  295. }
  296. }
  297. ?>