1
0

shared_cron_functions.php 9.8 KB

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