functions.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. #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 == "callofdutyuo" ? "uo" : $mod_gt;
  84. $mod_gt = $query_name == "callofduty2" ? "cod2" : $mod_gt;
  85. $mod_gt = $query_name == "callofduty4mw" ? "cod4" : $mod_gt;
  86. $mod_gt = $query_name == "callofdutywaw" ? "codww" : $mod_gt;
  87. $mod_gt = $query_name == "callofdutymw3" ? "mw3" : $mod_gt;
  88. $mod_gt = $query_name == "conanexiles" ? "conan" : $mod_gt;
  89. $map_paths= array(
  90. "https://image.gametracker.com/images/maps/160x120/$mod_gt/$map.jpg",
  91. "https://image.gametracker.com/images/maps/160x120/$query_name/$map.jpg",
  92. "protocol/lgsl/maps/$query_name/$mod/$map.jpg",
  93. "protocol/lgsl/maps/$query_name/$mod/$map.gif",
  94. "protocol/lgsl/maps/$query_name/$mod/$map.png",
  95. "protocol/lgsl/maps/$query_name/$map.jpg",
  96. "protocol/lgsl/maps/$query_name/$map.gif",
  97. "protocol/lgsl/maps/$query_name/$map.png",
  98. "images/online_big.png"
  99. );
  100. return get_first_existing_file($map_paths, 'http://gametracker.com', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0');
  101. }
  102. // Thanks adjo (http://opengamepanel.org/forum/viewthread.php?thread_id=5209#post_25073)
  103. function curlCacheImage($cachePath, $resource){
  104. if(preg_match('/^(https?:\/\/)/', $resource)){
  105. $map = explode('/', $resource);
  106. if(!file_exists($cachePath . '/cache/' . end($map))){
  107. $ch = curl_init();
  108. curl_setopt($ch, CURLOPT_HEADER, 0);
  109. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  110. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0');
  111. curl_setopt($ch, CURLOPT_REFERER, 'http://gametracker.com');
  112. curl_setopt($ch, CURLOPT_URL, $resource);
  113. $result = curl_exec($ch);
  114. curl_close($ch);
  115. file_put_contents($cachePath . '/cache/' . end($map), $result);
  116. }
  117. return $cachePath . '/cache/' . end($map);
  118. }
  119. return $resource;
  120. }
  121. //Refreshed Div:
  122. //Below is under construction:
  123. // Usage: $data .= PopupData($id);
  124. // PopupBuild($data);
  125. function PopupTrigger($id){
  126. return "<a href='#' class='ex".$id."trigger'></a>";
  127. }
  128. function PopupData($id,$link){//Trigger: ex($id)trigger
  129. return "$('#ex".$id."').jqm({ajax: '$link', trigger: 'a.ex".$id."trigger'});";
  130. }
  131. function PopupBuild($data){
  132. return "<script type='text/javascript'>$(document).ready(function()\{".$data."\});</script>";
  133. }
  134. function create_home_selector($module, $subpage, $server_homes) {
  135. if ($server_homes == "show_all")
  136. {
  137. echo "<form method='GET' action=''>\n";
  138. echo "<input type='hidden' name='m' value='".$module."' />\n";
  139. if( $subpage ) echo "<input type='hidden' name='p' value='".$subpage."' />\n";
  140. echo "<input type='SUBMIT' value='" . get_lang('show_all') . "'/>\n";
  141. echo "</form>\n";
  142. }
  143. else
  144. {
  145. foreach ($server_homes as $key => $row) {
  146. $home_name[$key] = $row['home_name'];
  147. $home_id[$key] = $row['home_id'];
  148. $mod_id[$key] = $row['home_id'];
  149. $ip[$key] = $row['ip'];
  150. $port[$key] = $row['port'];
  151. }
  152. array_multisort($home_name, $ip, $port, $mod_id, $home_id, SORT_DESC, $server_homes);
  153. echo "<form method='GET' name='select' action=''>\n";
  154. echo "<input type='hidden' name='m' value='".$module."' />\n";
  155. if( $subpage ) echo "<input type='hidden' name='p' value='".$subpage."' />\n";
  156. echo "<select onchange=".'"this.form.submit()"'." name='home_id'>\n";
  157. echo "<option></option>\n";
  158. foreach ( $server_homes as $server_home )
  159. {
  160. if( isset( $_GET['home_id'] ) and $_GET['home_id'] == $server_home['home_id'] )
  161. $selected = 'selected="selected"';
  162. else
  163. $selected = '';
  164. echo "<option value='". $server_home['home_id'] . "' $selected >" . htmlentities($server_home['home_name']) . "</option>\n";
  165. }
  166. echo "</select>\n";
  167. echo "</form>";
  168. }
  169. }
  170. function create_home_selector_address($module, $subpage, $server_homes, $extra_inputs = FALSE, $method = "GET") {
  171. if( isset($_GET['home_id-mod_id-ip-port']) and $_GET['home_id-mod_id-ip-port'] != "" )
  172. {
  173. list($get_home_id,
  174. $get_mod_id,
  175. $get_ip,
  176. $get_port) = explode( "-", $_GET['home_id-mod_id-ip-port'] );
  177. }
  178. echo "<form method='$method' name='select' action=''>\n";
  179. echo "<input type='hidden' name='m' value='$module' />\n";
  180. if( $subpage ) echo "<input type='hidden' name='p' value='".$subpage."' />\n";
  181. if($extra_inputs)
  182. {
  183. foreach($extra_inputs as $input)
  184. {
  185. echo "<input type='$input[type]' name='$input[name]' value='$input[value]' />\n";
  186. }
  187. }
  188. echo "<select onchange=\"this.form.submit();\" name='home_id-mod_id-ip-port'>\n";
  189. echo "<option></option>\n";
  190. foreach ($server_homes as $key => $row) {
  191. if( !isset($row['ip']) or !isset($row['mod_id']) )
  192. {
  193. unset($server_homes[$key]);
  194. continue;
  195. }
  196. $home_name[$key] = $row['home_name'];
  197. $home_id[$key] = $row['home_id'];
  198. $mod_id[$key] = $row['home_id'];
  199. $ip[$key] = $row['ip'];
  200. $port[$key] = $row['port'];
  201. }
  202. array_multisort($home_name, $ip, $port, $mod_id,
  203. $home_id, SORT_DESC, $server_homes);
  204. foreach ( $server_homes as $server_home )
  205. {
  206. $display_ip = checkDisplayPublicIP($server_home['display_public_ip'],$server_home['ip']);
  207. if(isset($_GET['home_id-mod_id-ip-port']) and
  208. $get_home_id == $server_home['home_id'] and
  209. $get_mod_id == $server_home['mod_id'] and
  210. $get_ip == $server_home['ip'] and
  211. $get_port == $server_home['port'])
  212. $selected = 'selected="selected"';
  213. else
  214. $selected = '';
  215. echo "<option value='". $server_home['home_id'] .
  216. "-" . $server_home['mod_id'] . "-" . $server_home['ip'] .
  217. "-" . $server_home['port'] . "' $selected >" .
  218. htmlentities($server_home['home_name']) . " - " . $display_ip .
  219. ":" . $server_home['port'] . "</option>\n";
  220. }
  221. echo "</select>\n";
  222. echo "</form>";
  223. }
  224. function create_home_selector_game_type($module, $subpage, $server_homes) {
  225. echo "<form method='GET' name='select' action=''>\n".
  226. "<input type='hidden' name='m' value='".$module."' />\n";
  227. if( $subpage != "" ) echo "<input type='hidden' name='p' value='".$subpage."' />\n";
  228. echo "<select onchange=".'"this.form.submit()"'." name='home_cfg_id'>\n".
  229. "<option>".get_lang('game_type')."</option>\n";
  230. $servers_by_game_name = array();
  231. foreach( $server_homes as $server_home )
  232. {
  233. if( !isset($server_home['ip']) or !isset($server_home['mod_id']) )
  234. continue;
  235. $servers_by_game_name["$server_home[game_name]"] = $server_home['home_cfg_id'];
  236. }
  237. ksort($servers_by_game_name);
  238. foreach( $servers_by_game_name as $game_name => $home_cfg_id )
  239. {
  240. $selected = (isset($_GET['home_cfg_id']) and $_GET['home_cfg_id'] == $home_cfg_id) ? 'selected="selected"' : "";
  241. echo "<option value='". $home_cfg_id . "' $selected >" . $game_name . "</option>\n";
  242. }
  243. echo "</select>\n</form>\n";
  244. }
  245. function mymail($email_address, $subject, $message, $panel_settings, $user_to_panel = FALSE){
  246. global $view;
  247. if( empty( $panel_settings['panel_name'] ) )
  248. $panel_name = "Open Game Panel";
  249. else
  250. $panel_name = $panel_settings['panel_name'];
  251. include('PHPMailer/class.phpmailer.php');
  252. // Create the mail object using the Mail::factory method
  253. $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
  254. $mail->IsSMTP(); // telling the class to use SMTP
  255. $mymail = TRUE;
  256. try
  257. {
  258. $mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
  259. // 1 = errors and messages
  260. // 2 = messages only
  261. // SMTP server
  262. if (isset($panel_settings['smtp_server']) and !empty($panel_settings['smtp_server']))
  263. $mail->Host = $panel_settings['smtp_server'];
  264. else
  265. $mail->Host = 'localhost';
  266. // set the SMTP port
  267. if (isset($panel_settings['smtp_port']) and !empty($panel_settings['smtp_port']))
  268. $mail->Port = $panel_settings['smtp_port'];
  269. else
  270. $mail->Port = 25;
  271. // sets the prefix to the servier
  272. if (isset($panel_settings['smtp_ssl']) and $panel_settings['smtp_ssl'] == 1)
  273. $mail->SMTPSecure = "ssl";
  274. elseif (isset($panel_settings['smtp_tls']) and $panel_settings['smtp_tls'] == 1)
  275. $mail->SMTPSecure = "tls";
  276. if (isset($panel_settings['smtp_login']) and !empty($panel_settings['smtp_login']))
  277. {
  278. // enable SMTP authentication
  279. $mail->SMTPAuth = true;
  280. // SMTP username
  281. $mail->Username = $panel_settings['smtp_login'];
  282. if (isset($panel_settings['smtp_passw']) and !empty($panel_settings['smtp_passw']))
  283. {
  284. // SMTP password
  285. $mail->Password = $panel_settings['smtp_passw'];
  286. }
  287. }
  288. if(empty($panel_settings['panel_email_address'])){
  289. $panel_email = "[email protected]";
  290. }else{
  291. $panel_email = $panel_settings['panel_email_address'];
  292. }
  293. $email_addresses = explode( ",", $email_address );
  294. if( $user_to_panel )
  295. {
  296. $mail->AddAddress($panel_email);
  297. $user_to_panel = is_bool($user_to_panel) ? "" : $user_to_panel; // True boolean or user name string
  298. foreach ( $email_addresses as $address )
  299. {
  300. $mail->SetFrom($address,$user_to_panel);
  301. $mail->AddReplyTo($address,$user_to_panel);
  302. }
  303. }
  304. else // panel to user
  305. {
  306. foreach ( $email_addresses as $address )
  307. {
  308. $mail->AddAddress($address);
  309. }
  310. $mail->SetFrom($panel_email,$panel_name);
  311. $mail->AddReplyTo($panel_email,$panel_name);
  312. }
  313. $mail->CharSet = $view->charset;
  314. $mail->Subject = $subject;
  315. $mail->MsgHTML($message);
  316. $mail->Send();
  317. }
  318. catch (phpmailerException $e)
  319. {
  320. $mymail = FALSE;
  321. echo $e->errorMessage(); //Pretty error messages from PHPMailer
  322. }
  323. catch (Exception $e)
  324. {
  325. $mymail = FALSE;
  326. echo $e->getMessage(); //Boring error messages from anything else!
  327. }
  328. return $mymail;
  329. }
  330. if( !function_exists("sys_get_temp_dir") )
  331. {
  332. function sys_get_temp_dir(){
  333. if ($temp = getenv('TMP') ) {
  334. if (file_exists($temp)) { return realpath($temp); }
  335. }
  336. if ($temp = getenv('TEMP') ) {
  337. if (file_exists($temp)) { return realpath($temp); }
  338. }
  339. if ($temp = getenv('TMPDIR') ) {
  340. if (file_exists($temp)) { return realpath($temp); }
  341. }
  342. // trick for creating a file in system's temporary dir
  343. // without knowing the path of the system's temporary dir
  344. $temp = tempnam(__FILE__, '');
  345. if (file_exists($temp)) {
  346. unlink($temp);
  347. return realpath(dirname($temp));
  348. }
  349. return null;
  350. }
  351. }
  352. function set_firewall($remote, $firewall_settings, $action, $port, $ip = FALSE)
  353. {
  354. if($action == "allow" or $action == "deny")
  355. {
  356. if($ip)
  357. $command = str_replace("%IP%",$ip,str_replace("%PORT%",$port,$firewall_settings[$action.'_ip_port_command']));
  358. else
  359. $command = str_replace("%PORT%",$port,$firewall_settings[$action.'_port_command']);
  360. }
  361. if(isset($command))
  362. return $remote->sudo_exec($command);
  363. else
  364. return FALSE;
  365. }
  366. function strip_real_escape_string($text)
  367. {
  368. $search = array('\"', "\'", "\\r", "\\n","\\\\");
  369. $replace = array('"', "'", "\r", "\n", '\\');
  370. $text = str_replace($search, $replace, $text);
  371. return $text;
  372. }
  373. function get_true_boolean($bool)
  374. {
  375. if ( (int) $bool > 0 )
  376. $ret = true;
  377. else
  378. {
  379. $lowered_bool = strtolower($bool); // that could be 'True' or 'true' or 'TRUE', etc...
  380. if( $lowered_bool === "true" || $lowered_bool === "on" || $lowered_bool === "yes" )
  381. $ret = true;
  382. else
  383. $ret = false;
  384. }
  385. return $ret;
  386. }
  387. function get_temp_dir($curdir)
  388. {
  389. $temp = sys_get_temp_dir();
  390. if( ini_get('open_basedir') )
  391. {
  392. $dirs = preg_split( "/:|;/", ini_get('open_basedir') , -1, PREG_SPLIT_NO_EMPTY );
  393. if( !in_array($temp, $dirs) )
  394. $temp = $curdir . DIRECTORY_SEPARATOR . 'temp';
  395. }
  396. if( $temp == null )
  397. $temp = $curdir . DIRECTORY_SEPARATOR . 'temp';
  398. if( !file_exists($temp) )
  399. if( is_writable( dirname($temp) ) ) mkdir($temp);
  400. return $temp;
  401. }
  402. // ### Escape some characters that could break the server startup or make the user capable to run other programs. ###
  403. // \ (backslash) -> At the end of the string, can scape the next quote,
  404. // and is commonly used to create Windows paths, must be escaped.
  405. // " (quote) -> Not escaped quote, without an ending quote, would break the startup command.
  406. // ' (single quote) -> same than quote.
  407. // | (pipe) -> Not escaped pipe would break the startup command and could use the next argument as new command.
  408. // & (ampersand) -> Same than pipe. If double ampersand is used it would run the command (if any) once the server process ends.
  409. // ; (semicolon) -> Same than double ampersand.
  410. // > (greater than) -> Could redirect the server output and ignore the next arguments.
  411. // < (lower than) -> Could send the content of a file to the server executable and ignore the the next arguments.
  412. // ` (apostrophe) -> Could get the return value of a given (system) command or variable.
  413. // $ (¿sigil?) -> Same than apostrophe.
  414. // ( and ) (parenthesis) -> starts or ends a bash/batch statement, could break the server startup
  415. // [ and ] (test) -> test is part of bash language, could break the server startup
  416. function clean_server_param_value($value, $cli_allow_chars) {
  417. $value = strip_real_escape_string($value);
  418. $escape_chars = array("\\", "\"", "'", "|", "&", ";", ">", "<", "`", "$", "(", ")", "[", "]");
  419. if($cli_allow_chars)
  420. {
  421. $cli_allow_chars = str_split($cli_allow_chars);
  422. $escape_chars = array_diff($escape_chars, $cli_allow_chars);
  423. }
  424. $find = array();
  425. $repl = array();
  426. foreach($escape_chars as $char)
  427. {
  428. $find[] = '%'.preg_quote($char).'%';
  429. $char = $char == '\\' ? preg_quote('\\\\') : $char;
  430. $repl[] = '\\'.$char;
  431. }
  432. return preg_replace($find, $repl, $value);
  433. }
  434. // ### Validate FTP user/password and control_protocol_password. ###
  435. function validate_login($value) {
  436. $value = strip_real_escape_string($value);
  437. $value = trim($value);
  438. $find = '%\\\\|"|\||&|;|>|<|`|\$|\s%';
  439. return preg_match($find, $value) ? FALSE : $value;
  440. }
  441. // Order a multidimensional array by keys. Source http://php.net/manual/es/function.array-multisort.php#100534
  442. function array_orderby()
  443. {
  444. $args = func_get_args();
  445. $data = array_shift($args);
  446. foreach ($args as $n => $field)
  447. {
  448. if (is_string($field))
  449. {
  450. $tmp = array();
  451. foreach ($data as $key => $row)
  452. $tmp[$key] = $row[$field];
  453. $args[$n] = $tmp;
  454. }
  455. }
  456. $args[] = &$data;
  457. call_user_func_array('array_multisort', $args);
  458. return array_pop($args);
  459. }
  460. // Escape a single quote or multiple single quotes
  461. // in a string that is passed to bash
  462. // and this string is single quoted
  463. function esc_squote($str)
  464. {
  465. return preg_replace("#('+)#", "'\"\${1}\"'", $str);
  466. }
  467. function get_game_selector($os, $game_cfgs, $home_cfg_id = FALSE)
  468. {
  469. if(preg_match("/64/", $os))
  470. {
  471. $arch_64_bit = true;
  472. }
  473. if(preg_match("/linux/i", $os))
  474. {
  475. if(preg_match("/wine/i", $os))
  476. {
  477. $os_match = $arch_64_bit ? '/(win|linux)(32|64)?$/i' : '/(win|linux)(32)?$/i';
  478. }
  479. else
  480. {
  481. $os_match = $arch_64_bit ? '/(linux)(32|64)?$/i' : '/(linux)(32)?$/i';
  482. }
  483. }
  484. elseif(preg_match("/cygwin/i", $os))
  485. {
  486. $os_match = $arch_64_bit ? '/(win)(32|64)?$/i' : '/(win)(32)?$/i';
  487. }
  488. else
  489. {
  490. $os_match = '/(win|linux)(32|64)?$/i';
  491. }
  492. $selector = "";
  493. foreach ( $game_cfgs as $row )
  494. {
  495. if ( preg_match($os_match, $row['game_key'], $matches) )
  496. {
  497. $selector .= "<option value='".$row['home_cfg_id']."' ".
  498. ($home_cfg_id == $row['home_cfg_id'] ? 'selected="selected"' : "").
  499. ">".$row['game_name'].
  500. (preg_match('/^linux$/i', $matches[1]) ? " (Linux" : " (Windows").
  501. ((isset($matches[2]) and $matches[2] == '64') ? " 64bit)" : ")").
  502. "</option>\n";
  503. }
  504. }
  505. return $selector;
  506. }
  507. function getClientIPAddress(){
  508. if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) && !empty($_SERVER['HTTP_CF_CONNECTING_IP'])){
  509. return $_SERVER['HTTP_CF_CONNECTING_IP'];
  510. }else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  511. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  512. }else if(isset($_SERVER['HTTP_X_REAL_IP']) && !empty($_SERVER['HTTP_X_REAL_IP'])){
  513. return $_SERVER['HTTP_X_REAL_IP'];
  514. }else{
  515. return $_SERVER['REMOTE_ADDR'];
  516. }
  517. }
  518. function getOGPSiteURL(){
  519. $url = '';
  520. $scheme = ( isset($_SERVER['HTTPS']) and get_true_boolean($_SERVER['HTTPS']) ) ? "https://" : "http://";
  521. $url .= $scheme;
  522. if(strtolower($_SERVER['HTTP_HOST']) == "localhost"){
  523. $ip = getRemoteIPAddressFromSite('http://grabip.tk/');
  524. if(!hasValue($ip)){
  525. if(cURLEnabled()){
  526. $ipOfServer = get_headers_curl("http://grabip.tk/", $referrer, $agent);
  527. if(hasValue($ipOfServer) && is_array($ipOfServer)){
  528. $ipOfServer = $ipOfServer[0];
  529. if (preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $ipOfServer, $ip_match)) {
  530. $ipStr = $ip_match[0];
  531. if(isValidIP($ipStr) && !isPrivateIp($ipStr)){
  532. $ip = $ipStr;
  533. }
  534. }
  535. }
  536. }
  537. }
  538. }
  539. if(hasValue($ip)){
  540. $url .= $ip;
  541. }else{
  542. $url .= $_SERVER['HTTP_HOST'];
  543. }
  544. if(!empty($_SERVER['REQUEST_URI'])){
  545. $lastSlash = strrpos($_SERVER['REQUEST_URI'], "/");
  546. if($lastSlash !== false){
  547. $url .= substr($_SERVER['REQUEST_URI'], 0, $lastSlash);
  548. }
  549. }
  550. if(!empty($url)){
  551. return $url;
  552. }
  553. return false;
  554. }
  555. function getRemoteIPAddressFromSite($site){
  556. $str = "";
  557. if(isset($site) && !empty($site)){
  558. $str=trim(file_get_contents($site));
  559. // Look for an IP
  560. if (preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $str, $ip_match)) {
  561. $ip = $ip_match[0];
  562. if(isValidIP($ip) && !isPrivateIp($ip)){
  563. $str = $ip;
  564. }
  565. }
  566. }
  567. return $str;
  568. }
  569. function isValidIP($ip){
  570. if(preg_match( "/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $ip)){
  571. return True;
  572. }else{
  573. return False;
  574. }
  575. }
  576. function isPrivateIp($ip){
  577. if(is_array($ip)) {
  578. $ret=false;
  579. foreach($ip as $i)
  580. $ret=$ret or isPrivateIp($i);
  581. return $ret;
  582. }
  583. return (substr($ip,0,7)=='192.168' or substr($ip,0,6)=='172.16' or substr($ip,0,3)=='10.');
  584. }
  585. function hasValue($val, $zeroAllowed = false){
  586. if(isset($val) && !empty($val)){
  587. return true;
  588. }else{
  589. if($zeroAllowed == true && $val == 0){
  590. return true;
  591. }else{
  592. return false;
  593. }
  594. }
  595. }
  596. function paginationPages($pageResults, $currentPage, $perPage, $pageUri, $pagesShown, $classPrefix) {
  597. $pagination = '<div id="pagination">';
  598. if ($pageResults > $perPage) {
  599. $totalPages = ceil($pageResults/$perPage);
  600. $pageStart = (($currentPage - $pagesShown) > 0) ? $currentPage - $pagesShown : 1;
  601. $pageEnd = (($currentPage + $pagesShown) < $totalPages) ? $currentPage + $pagesShown : $totalPages;
  602. if ($pageStart > 1) {
  603. $pagination .= '<span class="'.$classPrefix.'_paginationStart">
  604. <a href="'.$pageUri . ($currentPage-1) .'" class="'.$classPrefix.'_previousPageLink">&laquo;</a>
  605. <a href="'.$pageUri .'1" class="'.$classPrefix.'_firstPageLink">1</a>
  606. <span class='.$classPrefix.'_divider">&hellip;</span>
  607. </span>';
  608. }
  609. $pagination .= '<span class="'.$classPrefix.'_paginationPages">';
  610. for ($i=$pageStart; $i<=$pageEnd; ++$i) {
  611. if ($currentPage == $i) {
  612. $pagination .= '<a href="'.$pageUri . $i .'" class="'.$classPrefix.'_currentPageLink">['.$i.']</a>';
  613. } else {
  614. $pagination .= '<a href="'.$pageUri . $i .'" class="'.$classPrefix.'_pageLinks">'.$i.'</a>';
  615. }
  616. $pagination .= ($i < $pageEnd) ? ', ' : ' ';
  617. }
  618. $pagination .= '</span>';
  619. if ($pageEnd < $totalPages) {
  620. $pagination .= '<span class="'.$classPrefix.'_paginationEnd">
  621. <span class='.$classPrefix.'_divider">&hellip;</span>
  622. <a href="'.$pageUri . $totalPages .'" class="'.$classPrefix.'_lastPageLink">'.$totalPages.'</a>
  623. <a href="'.$pageUri . ($currentPage+1) .'" class="'.$classPrefix.'_nextPageLink">&raquo;</a>
  624. </span>';
  625. }
  626. }
  627. $pagination .= '</div>';
  628. return $pagination;
  629. }
  630. function checkDisplayPublicIP($display_public_ip,$internal_ip){
  631. // Set Cache Timer in Seconds
  632. $cache_timer = 600;
  633. // Exit Function when External IP is Internal IP or when Display Public IP is not set
  634. if($display_public_ip==$internal_ip || empty($display_public_ip)){
  635. return $internal_ip;
  636. }
  637. if(!isset($_SESSION['gethostbyname_cache'])){
  638. $_SESSION['gethostbyname_cache'] = array();
  639. }
  640. if(filter_var($display_public_ip, FILTER_VALIDATE_IP)){
  641. return $display_public_ip;
  642. }else{
  643. if(!array_key_exists($display_public_ip, $_SESSION['gethostbyname_cache'])){
  644. $_SESSION['gethostbyname_cache'][$display_public_ip] = array();
  645. $dns_check = dns_get_record($display_public_ip, DNS_A);
  646. $ipcheck = isset($dns_check[0]['ip']) ? $dns_check[0]['ip'] : $internal_ip;
  647. if($ipcheck!=$display_public_ip){
  648. $_SESSION['gethostbyname_cache'][$display_public_ip]['ip'] = $ipcheck;
  649. $_SESSION['gethostbyname_cache'][$display_public_ip]['stamp'] = time();
  650. }else{
  651. unset($_SESSION['gethostbyname_cache'][$display_public_ip]);
  652. return $internal_ip;
  653. }
  654. }else{
  655. if((time()-$_SESSION['gethostbyname_cache'][$display_public_ip]['stamp'])>=$cache_timer){
  656. $dns_check = dns_get_record($display_public_ip, DNS_A);
  657. $ipcheck = isset($dns_check[0]['ip']) ? $dns_check[0]['ip'] : $internal_ip;
  658. if($ipcheck!=$display_public_ip){
  659. $_SESSION['gethostbyname_cache'][$display_public_ip]['ip'] = $ipcheck;
  660. $_SESSION['gethostbyname_cache'][$display_public_ip]['stamp'] = time();
  661. }else{
  662. unset($_SESSION['gethostbyname_cache'][$display_public_ip]);
  663. return $internal_ip;
  664. }
  665. }
  666. }
  667. if(filter_var($_SESSION['gethostbyname_cache'][$display_public_ip]['ip'], FILTER_VALIDATE_IP)){
  668. return $_SESSION['gethostbyname_cache'][$display_public_ip]['ip'];
  669. }
  670. }
  671. return $internal_ip;
  672. }
  673. function startsWith($haystack, $needle) {
  674. // search backwards starting from haystack length characters from the end
  675. return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
  676. }
  677. function endsWith($haystack, $needle) {
  678. // search forward starting from end minus needle length characters
  679. return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
  680. }
  681. ?>