1
0

shared_cron_functions.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. 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. $parts = explode(" ", $job);
  156. $command = $parts[5];
  157. $homeId = getURLParam($homeIdStr, $command);
  158. $action = getURLParam($actionStr, $command);
  159. if($homeId !== false && $action !== false){
  160. if($homeId == $changedHomeId){
  161. $curPass = getURLParam($cPassStr, $command);
  162. if($curPass != $control_password){
  163. $command = str_replace($cPassStr . $curPass, $cPassStr . $control_password, $command);
  164. $minute = $parts[0];
  165. $hour = $parts[1];
  166. $dayOfTheMonth = $parts[2];
  167. $month = $parts[3];
  168. $dayOfTheWeek = $parts[4];
  169. $job = $minute." ".
  170. $hour ." ".
  171. $dayOfTheMonth." ".
  172. $month." ".
  173. $dayOfTheWeek." ".
  174. $command;
  175. $remote->scheduler_edit_task($jobId, $job);
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. ?>