1
0

shared_cron_functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. function reloadJobs($server_homes, $remote_servers, $getAllJobs = true)
  25. {
  26. global $db;
  27. $remote_servers_offline = array();
  28. $jobsArray = array();
  29. foreach( $remote_servers as $remote_server )
  30. {
  31. $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key'], $remote_server['timeout']);
  32. $rhost_id = $remote_server['remote_server_id'];
  33. if($remote->status_chk() != 1)
  34. {
  35. $remote_servers_offline[$rhost_id] = $remote_server;
  36. continue;
  37. }
  38. else
  39. {
  40. $jobs = $remote->scheduler_list_tasks();
  41. if($jobs != -1)
  42. {
  43. foreach($jobs as $jobId => $job)
  44. {
  45. $parts = explode(" ", $job);
  46. $minute = $parts[0];
  47. $hour = $parts[1];
  48. $dayOfTheMonth = $parts[2];
  49. $month = $parts[3];
  50. $dayOfTheWeek = $parts[4];
  51. unset($parts[0],$parts[1],$parts[2],$parts[3],$parts[4]);
  52. $command = implode(" ", $parts);
  53. $retval = preg_match_all("/^%ACTION=(start|restart|stop)\|%\|(.*)$/", $command, $job_info );
  54. if($retval and !empty($job_info[1][0]))
  55. {
  56. //print_r($job_info);
  57. $action = $job_info[1][0];
  58. $server_args = explode("|%|", $job_info[2][0]);
  59. switch ($action) {
  60. case 'start':
  61. list($home_id, $home_path, $server_exe, $run_dir,
  62. $startup_cmd, $port, $ip, $cpu, $nice) = $server_args;
  63. break;
  64. case 'restart':
  65. list($home_id, $ip, $port, $control_protocol,
  66. $control_password, $control_type, $home_path,
  67. $server_exe, $run_dir, $startup_cmd, $cpu, $nice) = $server_args;
  68. break;
  69. case 'stop':
  70. list($home_id, $ip, $port, $control_protocol,
  71. $control_password, $control_type, $home_path) = $server_args;
  72. break;
  73. }
  74. if(!isset($server_homes[$home_id."_".$ip."_".$port])) continue;
  75. if(!$getAllJobs && !hasAccessToCronjobHomeId($home_id)){
  76. continue;
  77. }
  78. $jobsArray[$rhost_id][$jobId] = array( 'job' => $job,
  79. 'minute' => $minute,
  80. 'hour' => $hour,
  81. 'dayOfTheMonth' => $dayOfTheMonth,
  82. 'month' => $month,
  83. 'dayOfTheWeek' => $dayOfTheWeek,
  84. 'action' => $action,
  85. 'home_id' => $home_id,
  86. 'ip' => $ip,
  87. 'port' => $port);
  88. }
  89. else if(getURLParam("homeid=", $command) !== false){
  90. $homeId = getURLParam("homeid=", $command);
  91. if(!$getAllJobs && !hasAccessToCronjobHomeId($homeId)){
  92. continue;
  93. }
  94. $action = getURLParam("action=", $command);
  95. if($action == "autoUpdateSteamHome"){
  96. $action = "steam_auto_update";
  97. }else if($action == "stopServer"){
  98. $action = "stop";
  99. }else if($action == "startServer"){
  100. $action = "start";
  101. }else if($action == "restartServer"){
  102. $action = "restart";
  103. }
  104. $jobsArray[$rhost_id][$jobId] = array( 'job' => $job,
  105. 'minute' => $minute,
  106. 'hour' => $hour,
  107. 'dayOfTheMonth' => $dayOfTheMonth,
  108. 'month' => $month,
  109. 'dayOfTheWeek' => $dayOfTheWeek,
  110. 'command' => $command,
  111. 'action' => $action,
  112. 'home_id' => $homeId);
  113. }
  114. else
  115. {
  116. if(!$getAllJobs && !$db->isAdmin($_SESSION['user_id'])){
  117. continue;
  118. }
  119. $jobsArray[$rhost_id][$jobId] = array( 'job' => $job,
  120. 'minute' => $minute,
  121. 'hour' => $hour,
  122. 'dayOfTheMonth' => $dayOfTheMonth,
  123. 'month' => $month,
  124. 'dayOfTheWeek' => $dayOfTheWeek,
  125. 'command' => $command);
  126. }
  127. }
  128. }
  129. }
  130. }
  131. return array($jobsArray, $remote_servers_offline);
  132. }
  133. function updateCronJobPasswords($db, $remote, $changedHomeId){
  134. $homes = $db->getIpPorts();
  135. foreach( $homes as $home )
  136. {
  137. $id = $home['home_id']."_".$home['ip']."_".$home['port'];
  138. $server_homes[$id] = $home;
  139. $server_id = $home['remote_server_id'];
  140. $remote_servers[$server_id] = array("remote_server_id" => $home['remote_server_id'],
  141. "remote_server_name" => $home['remote_server_name'],
  142. "ogp_user" => $home['ogp_user'],
  143. "agent_ip" => $home['agent_ip'],
  144. "agent_port" => $home['agent_port'],
  145. "ftp_port" => $home['ftp_port'],
  146. "encryption_key" => $home['encryption_key'],
  147. "timeout" => $home['timeout'],
  148. "use_nat" => $home['use_nat'],
  149. "ftp_ip" => $home['ftp_ip']);
  150. }
  151. list($jobsArray, $remote_servers_offline) = reloadJobs($server_homes, $remote_servers);
  152. $homes = customShift($homes, "home_id", $changedHomeId);
  153. $homeIdStr = "homeid=";
  154. $actionStr = "action=";
  155. $cPassStr = "controlpass=";
  156. if(count($homes) > 0){
  157. $home = $homes[0];
  158. if($home["home_id"] == $changedHomeId){
  159. $control_password = $home['control_password'];
  160. foreach( $jobsArray as $remote_server_id => $jobs )
  161. {
  162. if($home['remote_server_id'] == $remote_server_id){
  163. foreach($jobs as $jobId => $job)
  164. {
  165. $command = $job['command'];
  166. $homeId = getURLParam($homeIdStr, $command);
  167. $action = getURLParam($actionStr, $command);
  168. if($homeId !== false && $action !== false){
  169. if($homeId == $changedHomeId){
  170. $curPass = getURLParam($cPassStr, $command);
  171. if(stripos($curPass, '" --no-check-certificate') !== false){
  172. $curPass = substr($curPass, 0, stripos($curPass, '" --no-check-certificate'));
  173. }else if(strrpos($curPass, '"') !== false){
  174. $curPass = substr($curPass, 0, strrpos($curPass, '"'));
  175. }
  176. if($curPass != $control_password){
  177. $command = str_replace($cPassStr . $curPass, $cPassStr . $control_password, $command);
  178. $minute = $job['minute'];
  179. $hour = $job['hour'];
  180. $dayOfTheMonth = $job['dayOfTheMonth'];
  181. $month = $job['month'];
  182. $dayOfTheWeek = $job['dayOfTheWeek'];
  183. $job = $minute." ".
  184. $hour ." ".
  185. $dayOfTheMonth." ".
  186. $month." ".
  187. $dayOfTheWeek." ".
  188. $command;
  189. $remote->scheduler_edit_task($jobId, $job);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. function get_action_selector($action = false) {
  200. $server_actions = array('restart','stop','start','steam_auto_update');
  201. $select_action = '<select name="action" style="width: 100%;">';
  202. foreach($server_actions as $server_action)
  203. {
  204. $selected = ($action and $action == $server_action) ? 'selected="selected"' : '';
  205. $select_action .= '<option value="'.$server_action.'" '.$selected.'>'.get_lang($server_action).'</option>';
  206. }
  207. return $select_action .= '</select>';
  208. }
  209. function get_server_selector($server_homes, $homeid_ip_port = FALSE, $onchange = FALSE, $includeRemoteName = false) {
  210. $onchange_this_form_submit = $onchange ? 'onchange="this.form.submit();"' : '';
  211. $select_game = "<select style='text-overflow: ellipsis; width: 100%;' name='homeid_ip_port' $onchange_this_form_submit>\n";
  212. if($server_homes != FALSE)
  213. {
  214. foreach ( $server_homes as $server_home )
  215. {
  216. // Find out if it's a steamcmd server
  217. $additionalMarkup = "";
  218. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$server_home['home_cfg_file']);
  219. if( $server_xml->installer == "steamcmd" ){
  220. $additionalMarkup = 'steam="1"';
  221. }
  222. $selected = ($homeid_ip_port and ($homeid_ip_port == $server_home['home_id']."_".$server_home['ip']."_".$server_home['port'] || trim($homeid_ip_port) == trim($server_home['home_id']))) ? 'selected="selected"' : '';
  223. $select_game .= "<option value='". $server_home['home_id'] . "_" . $server_home['ip'] .
  224. "_" . $server_home['port'] . "' $selected " . $additionalMarkup . ">" . $server_home['home_name'] .
  225. " - " . checkDisplayPublicIP($server_home['display_public_ip'],$server_home['ip'] != $server_home['agent_ip'] ? $server_home['ip'] : $server_home['agent_ip']) . ":" .$server_home['port'];
  226. if($includeRemoteName){
  227. $select_game .= " ( " . $server_home['remote_server_name'] . " )";
  228. }
  229. $select_game .= "</option>\n";
  230. }
  231. }
  232. return $select_game .= "</select>\n";
  233. }
  234. function get_remote_server_selector($r_servers, $remote_servers_offline, $remote_server_id = FALSE, $onchange = FALSE, $first_empty = FALSE ) {
  235. $onchange_this_form_submit = $onchange ? 'onchange="this.form.submit();"' : '';
  236. $select_rserver = "<select id='r_server_id' style='width: 100%;' name='r_server_id' $onchange_this_form_submit>\n";
  237. if($first_empty) $select_rserver .= '<option></option>';
  238. foreach ( $r_servers as $r_server )
  239. {
  240. $selected = ($remote_server_id and $remote_server_id == $r_server['remote_server_id']) ? 'selected="selected"' : '';
  241. $offline = isset($remote_servers_offline[$r_server['remote_server_id']]) ? ' (' . offline . ')' : '';
  242. $select_rserver .= "<option value='". $r_server['remote_server_id'] . "' $selected>" . $r_server['remote_server_name'] . "$offline</option>\n";
  243. }
  244. return $select_rserver .= "</select>\n";
  245. }
  246. function checkCronInput($min, $hour, $day, $month, $dayOfWeek) {
  247. $blacklist = '"#$%^&()+=[]\';{}|:<>?~';
  248. $returns = array();
  249. $args = func_get_args();
  250. foreach ($args as $k => $arg) {
  251. if (strlen($arg) == 0 || strpbrk($arg, $blacklist) || preg_match('/\\s/', $arg)) {
  252. $returns[$k] = false;
  253. }
  254. }
  255. return (empty($returns) ? true : false);
  256. }
  257. function hasAccessToCronjobHomeId($home_id){
  258. global $db;
  259. $hasAccess = ($db->isAdmin($_SESSION['user_id'])) ? true : $db->getUserGameHome($_SESSION['user_id'], $home_id);
  260. return $hasAccess;
  261. }
  262. ?>