user_cron.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <script type="text/javascript" src="js/modules/cron.js"></script>
  2. <?php
  3. /*
  4. *
  5. * OGP - Open Game Panel
  6. * Copyright (C) 2008 - 2017 The OGP Development Team
  7. *
  8. * http://www.opengamepanel.org/
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. */
  25. error_reporting(E_ALL);
  26. require_once('includes/lib_remote.php');
  27. require_once('modules/gamemanager/home_handling_functions.php');
  28. require_once('modules/config_games/server_config_parser.php');
  29. require_once('modules/cron/shared_cron_functions.php');
  30. function get_action_selector($action = false) {
  31. $server_actions = array('restart','stop','start','steam_auto_update');
  32. $select_action = '<select name="action" style="width: 100%;">';
  33. foreach($server_actions as $server_action)
  34. {
  35. $selected = ($action and $action == $server_action) ? 'selected="selected"' : '';
  36. $select_action .= '<option value="'.$server_action.'" '.$selected.'>'.get_lang($server_action).'</option>';
  37. }
  38. return $select_action .= '</select>';
  39. }
  40. function get_server_selector($server_homes, $homeid_ip_port = FALSE, $onchange = FALSE) {
  41. $onchange_this_form_submit = $onchange ? 'onchange="this.form.submit();"' : '';
  42. $select_game = "<select style='text-overflow: ellipsis; width: 100%;' name='homeid_ip_port' $onchange_this_form_submit>\n";
  43. if($server_homes != FALSE)
  44. {
  45. foreach ( $server_homes as $server_home )
  46. {
  47. // Find out if it's a steamcmd server
  48. $additionalMarkup = "";
  49. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$server_home['home_cfg_file']);
  50. if( $server_xml->installer == "steamcmd" ){
  51. $additionalMarkup = 'steam="1"';
  52. }
  53. $selected = (trim($homeid_ip_port) == trim($server_home['home_id']) || ($homeid_ip_port and $homeid_ip_port == $server_home['home_id']."_".$server_home['ip']."_".$server_home['port'])) ? 'selected="selected"' : '';
  54. $select_game .= "<option value='". $server_home['home_id'] . "_" . $server_home['ip'] .
  55. "_" . $server_home['port'] . "' $selected " . $additionalMarkup . ">" . $server_home['home_name'] .
  56. " - " . checkDisplayPublicIP($server_home['display_public_ip'],$server_home['ip']) . ":" .$server_home['port'] . "</option>\n";
  57. }
  58. }
  59. return $select_game .= "</select>\n";
  60. }
  61. function get_remote_server_selector($r_servers, $remote_servers_offline, $remote_server_id = FALSE, $onchange = FALSE) {
  62. $onchange_this_form_submit = $onchange ? 'onchange="this.form.submit();"' : '';
  63. $select_rserver = "<select style='width: 100%;' name='r_server_id' $onchange_this_form_submit>\n";
  64. foreach ( $r_servers as $r_server )
  65. {
  66. $selected = ($remote_server_id and $remote_server_id == $r_server['remote_server_id']) ? 'selected="selected"' : '';
  67. $offline = isset($remote_servers_offline[$r_server['remote_server_id']]) ? ' (' . offline . ')' : '';
  68. $select_rserver .= "<option value='". $r_server['remote_server_id'] . "' $selected>" . $r_server['remote_server_name'] . "$offline</option>\n";
  69. }
  70. return $select_rserver .= "</select>\n";
  71. }
  72. function exec_ogp_module()
  73. {
  74. global $db;
  75. $isAdmin = $db->isAdmin($_SESSION['user_id']);
  76. $boolShowedAdminLink = false;
  77. $homes = $db->getIpPortsForUser($_SESSION['user_id']);
  78. if(!$homes)
  79. {
  80. print_failure(get_lang('cron_no_servers_tied_to_account'));
  81. if($isAdmin){
  82. $boolShowedAdminLink = true;
  83. echo '<a href="home.php?m=cron&p=cron">' . get_lang('cron_admin_link_display_text') . '</a>';
  84. }
  85. return 0;
  86. }
  87. foreach( $homes as $home )
  88. {
  89. $id = $home['home_id']."_".$home['ip']."_".$home['port'];
  90. $server_homes[$id] = $home;
  91. $server_id = $home['remote_server_id'];
  92. $remote_servers[$server_id] = array("remote_server_id" => $home['remote_server_id'],
  93. "remote_server_name" => $home['remote_server_name'],
  94. "ogp_user" => $home['ogp_user'],
  95. "agent_ip" => $home['agent_ip'],
  96. "agent_port" => $home['agent_port'],
  97. "ftp_port" => $home['ftp_port'],
  98. "encryption_key" => $home['encryption_key'],
  99. "timeout" => $home['timeout'],
  100. "use_nat" => $home['use_nat'],
  101. "ftp_ip" => $home['ftp_ip']);
  102. }
  103. list($jobsArray, $remote_servers_offline) = reloadJobs($server_homes, $remote_servers);
  104. if( isset($_POST['addJob']) or isset($_POST['editJob']) )
  105. {
  106. if ( isset( $_POST['homeid_ip_port'] ) and isset($server_homes[$_POST['homeid_ip_port']]) )
  107. {
  108. $game_home = $server_homes[$_POST['homeid_ip_port']];
  109. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$game_home['home_cfg_file']);
  110. $remote = new OGPRemoteLibrary( $game_home['agent_ip'],
  111. $game_home['agent_port'],
  112. $game_home['encryption_key'],
  113. $game_home['timeout']);
  114. $home_id = $game_home['home_id'];
  115. $ip = $game_home['ip'];
  116. $port = $game_home['port'];
  117. $control_protocol = $server_xml->control_protocol;
  118. $control_password = $game_home['control_password'];
  119. $control_type = $server_xml->control_protocol_type;
  120. $home_path = $game_home['home_path'];
  121. $server_exe = $server_xml->server_exec_name;
  122. $run_dir = $server_xml->exe_location;
  123. $game_home['mods'][$game_home['mod_id']] = Array ("mod_cfg_id" => $game_home['mod_cfg_id'],
  124. "max_players" => $game_home['max_players'],
  125. "extra_params" => $game_home['extra_params'],
  126. "cpu_affinity" => $game_home['cpu_affinity'],
  127. "nice" => $game_home['nice'],
  128. "precmd" => $game_home['precmd'],
  129. "postcmd" => $game_home['postcmd'],
  130. "home_cfg_id" => $game_home['home_cfg_id'],
  131. "mod_key" => $game_home['mod_key'],
  132. "mod_name" => $game_home['mod_name'],
  133. "def_precmd" => $game_home['def_precmd'],
  134. "def_postcmd" => $game_home['def_postcmd']);
  135. $startup_cmd = get_start_cmd($remote,$server_xml,$game_home,$game_home['mod_id'],$game_home['ip'],$game_home['port'], $remote->what_os());
  136. $cpu = $game_home['cpu_affinity'];
  137. $nice = $game_home['nice'];
  138. $panelURL = getOGPSiteURL();
  139. if($panelURL === false){
  140. print_failure('Failed to retrieve panel URL.');
  141. return 0;
  142. }
  143. switch ($_POST['action']) {
  144. case "stop":
  145. $command = "wget -N \"" . $panelURL . "/ogp_api.php?action=stopServer&homeid=" . $home_id . "&controlpass=" . $control_password . "\" --no-check-certificate > /dev/null 2>&1";
  146. break;
  147. case "start":
  148. $command = "wget -N \"" . $panelURL . "/ogp_api.php?action=startServer&homeid=" . $home_id . "&controlpass=" . $control_password . "\" --no-check-certificate > /dev/null 2>&1";
  149. break;
  150. case "restart":
  151. $command = "wget -N \"" . $panelURL . "/ogp_api.php?action=restartServer&homeid=" . $home_id . "&controlpass=" . $control_password . "\" --no-check-certificate > /dev/null 2>&1";
  152. break;
  153. case "steam_auto_update":
  154. $command = "wget -N \"" . $panelURL . "/ogp_api.php?action=autoUpdateSteamHome&homeid=" . $home_id . "&controlpass=" . $control_password . "\" --no-check-certificate > /dev/null 2>&1";
  155. break;
  156. }
  157. $job = $_POST['minute']." ".
  158. $_POST['hour']." ".
  159. $_POST['dayOfTheMonth']." ".
  160. $_POST['month']." ".
  161. $_POST['dayOfTheWeek']." ".
  162. $command;
  163. if( isset($_POST['editJob']) and isset($jobsArray[$_POST['r_server_id']][$_POST['job_id']]) )
  164. $remote->scheduler_edit_task($_POST['job_id'], $job);
  165. elseif( isset($_POST['addJob']) )
  166. $remote->scheduler_add_task($job);
  167. list($jobsArray, $remote_servers_offline) = reloadJobs($server_homes, $remote_servers);
  168. }
  169. }
  170. elseif( isset($_POST['removeJob']) and isset($remote_servers[$_POST['r_server_id']]) and isset($jobsArray[$_POST['r_server_id']][$_POST['job_id']]) )
  171. {
  172. $remote = new OGPRemoteLibrary( $remote_servers[$_POST['r_server_id']]['agent_ip'],
  173. $remote_servers[$_POST['r_server_id']]['agent_port'],
  174. $remote_servers[$_POST['r_server_id']]['encryption_key'],
  175. $remote_servers[$_POST['r_server_id']]['timeout'] );
  176. $remote->scheduler_del_task($_POST['job_id']);
  177. list($jobsArray, $remote_servers_offline) = reloadJobs($server_homes, $remote_servers);
  178. }
  179. echo "<h2>" . schedule_new_job . "</h2>";
  180. require_once("includes/refreshed.php");
  181. $refresh = new refreshed();
  182. $homeid_ip_port = isset($_POST['homeid_ip_port']) ? $_POST['homeid_ip_port'] : key($server_homes);
  183. $r_server_id = $server_homes[$homeid_ip_port]['remote_server_id'];
  184. $curtime = $refresh->add( "home.php?m=cron&p=thetime&r_server_id=$r_server_id&type=cleared" );
  185. echo "<pre class='log' ><table><tr><td>" . now .
  186. "&nbsp;</td><td><form action='' method='POST' >" . get_server_selector($server_homes, $homeid_ip_port, TRUE) .
  187. "</form></td></tr></table> <b style='font-size:1.4em;'>" . $refresh->getdiv($curtime) . "</b></pre>";
  188. ?>
  189. <form method="POST" >
  190. <table class="center hundred">
  191. <tr>
  192. <th>
  193. <?php echo minute; ?>
  194. </th>
  195. <th>
  196. <?php echo hour; ?>
  197. </th>
  198. <th>
  199. <?php echo day; ?>
  200. </th>
  201. <th>
  202. <?php echo month; ?>
  203. </th>
  204. <th>
  205. <?php echo day_of_the_week; ?>
  206. </th>
  207. <th>
  208. <?php echo action; ?>
  209. </th>
  210. <th>
  211. <?php echo user_games; ?>
  212. </th>
  213. </tr>
  214. <tr>
  215. <td style="width: 35px;" >
  216. <input style="width: 30px;" type="text" name="minute" value="*" />
  217. </td>
  218. <td style="width: 35px;" >
  219. <input style="width: 30px;" type="text" name="hour" value="*" />
  220. </td>
  221. <td style="width: 35px;" >
  222. <input style="width: 30px;" type="text" name="dayOfTheMonth" value="*" />
  223. </td>
  224. <td style="width: 35px;" >
  225. <input style="width: 30px;" type="text" name="month" value="*" />
  226. </td>
  227. <td style="width: 35px;" >
  228. <input style="width: 30px;" type="text" name="dayOfTheWeek" value="*" />
  229. </td>
  230. <td>
  231. <?php echo get_action_selector();?>
  232. </td>
  233. <td>
  234. <?php echo get_server_selector($server_homes, $homeid_ip_port);?>
  235. </td>
  236. <td style="width: 132px;">
  237. <input style="" type="submit" name="addJob" value="<?php echo add; ?>" />
  238. </td>
  239. </tr>
  240. </table>
  241. </form>
  242. <br>
  243. <h2><?php echo scheduled_jobs;?></h2>
  244. <?php
  245. if ( !empty($jobsArray) )
  246. {
  247. ?>
  248. <table class="center hundred">
  249. </tr>
  250. <tr>
  251. <th>
  252. <?php echo minute; ?>
  253. </th>
  254. <th>
  255. <?php echo hour; ?>
  256. </th>
  257. <th>
  258. <?php echo day; ?>
  259. </th>
  260. <th>
  261. <?php echo month; ?>
  262. </th>
  263. <th>
  264. <?php echo day_of_the_week; ?>
  265. </th>
  266. <th>
  267. <?php echo action; ?>
  268. </th>
  269. <th>
  270. <?php echo user_games; ?>
  271. </th>
  272. </tr>
  273. <?php
  274. $user_jobs = "";
  275. foreach( $jobsArray as $remote_server_id => $jobs )
  276. {
  277. foreach($jobs as $jobId => $job)
  278. {
  279. if(isset($job['action']))
  280. {
  281. if(hasValue($job['home_id']) && hasValue(@$job['ip']) && hasValue(@$job['port'])){
  282. $uniqueStr = $job['home_id']."_".$job['ip']."_".$job['port'];
  283. }else if(hasValue($job['home_id'])){
  284. $uniqueStr = $job['home_id'];
  285. }
  286. if(hasValue(@$uniqueStr)){
  287. $user_jobs .= '<tr>
  288. <td style="width: 35px;" >
  289. <form method="POST" >
  290. <input style="width: 30px;" type="text" name="minute" value="'.$job['minute'].'" />
  291. </td>
  292. <td style="width: 35px;" >
  293. <input style="width: 30px;" type="text" name="hour" value="'.$job['hour'].'" />
  294. </td>
  295. <td style="width: 35px;" >
  296. <input style="width: 30px;" type="text" name="dayOfTheMonth" value="'.$job['dayOfTheMonth'].'" />
  297. </td>
  298. <td style="width: 35px;" >
  299. <input style="width: 30px;" type="text" name="month" value="'.$job['month'].'" />
  300. </td>
  301. <td style="width: 35px;" >
  302. <input style="width: 30px;" type="text" name="dayOfTheWeek" value="'.$job['dayOfTheWeek'].'" />
  303. </td>
  304. <td>
  305. '.get_action_selector($job['action'])."</td><td>".
  306. get_server_selector($server_homes, $uniqueStr).'
  307. </td>
  308. <td style="width: 132px;">
  309. <input type="hidden" name="job_id" value=\''.$jobId.'\' />
  310. <input type="hidden" name="r_server_id" value=\''.$remote_server_id.'\' />
  311. <input style="" type="submit" name="editJob" value="'. edit .'" />
  312. <input style="" type="submit" name="removeJob" value="'. remove .'" />
  313. </form>
  314. </td>
  315. </tr>';
  316. }
  317. }
  318. }
  319. }
  320. echo $user_jobs;
  321. ?>
  322. </table>
  323. <?php
  324. }
  325. else
  326. echo "<h3>". there_are_no_scheduled_jobs ."</h3>";
  327. ?>
  328. <table class='center hundred' ><tr><td><a href='javascript:history.go(-1)' > << <?php echo back ?></a><?php if(!$boolShowedAdminLink && $isAdmin){ echo '&nbsp; &nbsp; | &nbsp; &nbsp; ' . '<a href="home.php?m=cron&p=cron">' . get_lang('cron_admin_link_display_text') . '</a>'; }?></td></tr></table>
  329. <script type="text/javascript">
  330. $(document).ready(function()
  331. {
  332. <?php echo $refresh->build("1000"); ?>
  333. }
  334. );
  335. </script>
  336. <?php
  337. }
  338. ?>