user_cron.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. function reloadJobs($server_homes, $remote_servers)
  30. {
  31. $remote_servers_offline = array();
  32. $jobsArray = array();
  33. foreach( $remote_servers as $remote_server )
  34. {
  35. $remote = new OGPRemoteLibrary($remote_server['agent_ip'], $remote_server['agent_port'], $remote_server['encryption_key'], $remote_server['timeout']);
  36. $rhost_id = $remote_server['remote_server_id'];
  37. if($remote->status_chk() != 1)
  38. {
  39. $remote_servers_offline[$rhost_id] = $remote_server;
  40. continue;
  41. }
  42. else
  43. {
  44. $jobs = $remote->scheduler_list_tasks();
  45. if($jobs != -1)
  46. {
  47. foreach($jobs as $jobId => $job)
  48. {
  49. $parts = explode(" ", $job);
  50. $minute = $parts[0];
  51. $hour = $parts[1];
  52. $dayOfTheMonth = $parts[2];
  53. $month = $parts[3];
  54. $dayOfTheWeek = $parts[4];
  55. unset($parts[0],$parts[1],$parts[2],$parts[3],$parts[4]);
  56. $command = implode(" ", $parts);
  57. $retval = preg_match_all("/^%ACTION=(start|restart|stop)\|%\|(.*)$/", $command, $job_info );
  58. if($retval and !empty($job_info[1][0]))
  59. {
  60. //print_r($job_info);
  61. $action = $job_info[1][0];
  62. $server_args = explode("|%|", $job_info[2][0]);
  63. switch ($action) {
  64. case 'start':
  65. list($home_id, $home_path, $server_exe, $run_dir,
  66. $startup_cmd, $port, $ip, $cpu, $nice) = $server_args;
  67. break;
  68. case 'restart':
  69. list($home_id, $ip, $port, $control_protocol,
  70. $control_password, $control_type, $home_path,
  71. $server_exe, $run_dir, $startup_cmd, $cpu, $nice) = $server_args;
  72. break;
  73. case 'stop':
  74. list($home_id, $ip, $port, $control_protocol,
  75. $control_password, $control_type, $home_path) = $server_args;
  76. break;
  77. }
  78. if(!isset($server_homes[$home_id."_".$ip."_".$port])) continue;
  79. $jobsArray[$rhost_id][$jobId] = array( 'job' => $job,
  80. 'minute' => $minute,
  81. 'hour' => $hour,
  82. 'dayOfTheMonth' => $dayOfTheMonth,
  83. 'month' => $month,
  84. 'dayOfTheWeek' => $dayOfTheWeek,
  85. 'action' => $action,
  86. 'home_id' => $home_id,
  87. 'ip' => $ip,
  88. 'port' => $port);
  89. }else if(stripos($command, "homeid=")){
  90. $homeId = substr($command, stripos($command, "homeid=") + 7);
  91. if(stripos($homeId, "&")){
  92. $homeId = substr($homeId, 0, stripos($homeId, "&"));
  93. }else{
  94. $homeId = substr($homeId, 0);
  95. }
  96. $jobsArray[$rhost_id][$jobId] = array( 'job' => $job,
  97. 'minute' => $minute,
  98. 'hour' => $hour,
  99. 'dayOfTheMonth' => $dayOfTheMonth,
  100. 'month' => $month,
  101. 'dayOfTheWeek' => $dayOfTheWeek,
  102. 'command' => $command,
  103. 'action' => 'steam_auto_update',
  104. 'home_id' => $homeId);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. return array($jobsArray, $remote_servers_offline);
  111. }
  112. function get_action_selector($action = false) {
  113. $server_actions = array('restart','stop','start','steam_auto_update');
  114. $select_action = '<select name="action">';
  115. foreach($server_actions as $server_action)
  116. {
  117. $selected = ($action and $action == $server_action) ? 'selected="selected"' : '';
  118. $select_action .= '<option value="'.$server_action.'" '.$selected.'>'.get_lang($server_action).'</option>';
  119. }
  120. return $select_action .= '</select>';
  121. }
  122. function get_server_selector($server_homes, $homeid_ip_port = FALSE, $onchange = FALSE) {
  123. $onchange_this_form_submit = $onchange ? 'onchange="this.form.submit();"' : '';
  124. $select_game = "<select style='text-overflow: ellipsis; width: 100%;' name='homeid_ip_port' $onchange_this_form_submit>\n";
  125. if($server_homes != FALSE)
  126. {
  127. foreach ( $server_homes as $server_home )
  128. {
  129. // Find out if it's a steamcmd server
  130. $additionalMarkup = "";
  131. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$server_home['home_cfg_file']);
  132. if( $server_xml->installer == "steamcmd" ){
  133. $additionalMarkup = 'steam="1"';
  134. }
  135. $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"' : '';
  136. $select_game .= "<option value='". $server_home['home_id'] . "_" . $server_home['ip'] .
  137. "_" . $server_home['port'] . "' $selected " . $additionalMarkup . ">" . $server_home['home_name'] .
  138. " - " . checkDisplayPublicIP($server_home['display_public_ip'],$server_home['ip']) . ":" .$server_home['port'] . "</option>\n";
  139. }
  140. }
  141. return $select_game .= "</select>\n";
  142. }
  143. function get_remote_server_selector($r_servers, $remote_servers_offline, $remote_server_id = FALSE, $onchange = FALSE) {
  144. $onchange_this_form_submit = $onchange ? 'onchange="this.form.submit();"' : '';
  145. $select_rserver = "<select style='width: 100px;' name='r_server_id' $onchange_this_form_submit>\n";
  146. foreach ( $r_servers as $r_server )
  147. {
  148. $selected = ($remote_server_id and $remote_server_id == $r_server['remote_server_id']) ? 'selected="selected"' : '';
  149. $offline = isset($remote_servers_offline[$r_server['remote_server_id']]) ? ' (' . offline . ')' : '';
  150. $select_rserver .= "<option value='". $r_server['remote_server_id'] . "' $selected>" . $r_server['remote_server_name'] . "$offline</option>\n";
  151. }
  152. return $select_rserver .= "</select>\n";
  153. }
  154. function exec_ogp_module()
  155. {
  156. global $db;
  157. $isAdmin = $db->isAdmin($_SESSION['user_id']);
  158. $boolShowedAdminLink = false;
  159. $homes = $db->getIpPortsForUser($_SESSION['user_id']);
  160. if(!$homes)
  161. {
  162. print_failure(get_lang('cron_no_servers_tied_to_account'));
  163. if($isAdmin){
  164. $boolShowedAdminLink = true;
  165. echo '<a href="home.php?m=cron&p=cron">' . get_lang('cron_admin_link_display_text') . '</a>';
  166. }
  167. return 0;
  168. }
  169. foreach( $homes as $home )
  170. {
  171. $id = $home['home_id']."_".$home['ip']."_".$home['port'];
  172. $server_homes[$id] = $home;
  173. $server_id = $home['remote_server_id'];
  174. $remote_servers[$server_id] = array("remote_server_id" => $home['remote_server_id'],
  175. "remote_server_name" => $home['remote_server_name'],
  176. "ogp_user" => $home['ogp_user'],
  177. "agent_ip" => $home['agent_ip'],
  178. "agent_port" => $home['agent_port'],
  179. "ftp_port" => $home['ftp_port'],
  180. "encryption_key" => $home['encryption_key'],
  181. "timeout" => $home['timeout'],
  182. "use_nat" => $home['use_nat'],
  183. "ftp_ip" => $home['ftp_ip']);
  184. }
  185. list($jobsArray, $remote_servers_offline) = reloadJobs($server_homes, $remote_servers);
  186. if( isset($_POST['addJob']) or isset($_POST['editJob']) )
  187. {
  188. if ( isset( $_POST['homeid_ip_port'] ) and isset($server_homes[$_POST['homeid_ip_port']]) )
  189. {
  190. $game_home = $server_homes[$_POST['homeid_ip_port']];
  191. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$game_home['home_cfg_file']);
  192. $remote = new OGPRemoteLibrary( $game_home['agent_ip'],
  193. $game_home['agent_port'],
  194. $game_home['encryption_key'],
  195. $game_home['timeout']);
  196. $home_id = $game_home['home_id'];
  197. $ip = $game_home['ip'];
  198. $port = $game_home['port'];
  199. $control_protocol = $server_xml->control_protocol;
  200. $control_password = $game_home['control_password'];
  201. $control_type = $server_xml->control_protocol_type;
  202. $home_path = $game_home['home_path'];
  203. $server_exe = $server_xml->server_exec_name;
  204. $run_dir = $server_xml->exe_location;
  205. $game_home['mods'][$game_home['mod_id']] = Array ("mod_cfg_id" => $game_home['mod_cfg_id'],
  206. "max_players" => $game_home['max_players'],
  207. "extra_params" => $game_home['extra_params'],
  208. "cpu_affinity" => $game_home['cpu_affinity'],
  209. "nice" => $game_home['nice'],
  210. "precmd" => $game_home['precmd'],
  211. "postcmd" => $game_home['postcmd'],
  212. "home_cfg_id" => $game_home['home_cfg_id'],
  213. "mod_key" => $game_home['mod_key'],
  214. "mod_name" => $game_home['mod_name'],
  215. "def_precmd" => $game_home['def_precmd'],
  216. "def_postcmd" => $game_home['def_postcmd']);
  217. $startup_cmd = get_start_cmd($remote,$server_xml,$game_home,$game_home['mod_id'],$game_home['ip'],$game_home['port'], $remote->what_os());
  218. $cpu = $game_home['cpu_affinity'];
  219. $nice = $game_home['nice'];
  220. switch ($_POST['action']) {
  221. case "stop":
  222. $command = "%ACTION=stop|%|$home_id|%|$ip|%|$port|%|".
  223. "$control_protocol|%|$control_password|%|$control_type|%|$home_path";
  224. break;
  225. case "start":
  226. $command = "%ACTION=start|%|$home_id|%|$home_path|%|$server_exe|%|$run_dir|%|".
  227. "$startup_cmd|%|$port|%|$ip|%|$cpu|%|$nice";
  228. break;
  229. case "restart":
  230. $command = "%ACTION=restart|%|$home_id|%|$ip|%|$port|%|$control_protocol|%|".
  231. "$control_password|%|$control_type|%|$home_path|%|$server_exe|%|$run_dir|%|".
  232. "$startup_cmd|%|$cpu|%|$nice";
  233. break;
  234. case "steam_auto_update":
  235. $panelURL = getOGPSiteURL();
  236. if($panelURL !== false){
  237. $command = "wget -N \"" . $panelURL . "/ogp_api.php?action=autoUpdateSteamHome&homeid=" . $home_id . "&controlpass=" . $control_password . "\" > /dev/null 2>&1";
  238. }else{
  239. print_failure('Failed to retrieve panel URL.');
  240. return 0;
  241. }
  242. break;
  243. }
  244. $job = $_POST['minute']." ".
  245. $_POST['hour']." ".
  246. $_POST['dayOfTheMonth']." ".
  247. $_POST['month']." ".
  248. $_POST['dayOfTheWeek']." ".
  249. $command;
  250. if( isset($_POST['editJob']) and isset($jobsArray[$_POST['r_server_id']][$_POST['job_id']]) )
  251. $remote->scheduler_edit_task($_POST['job_id'], $job);
  252. elseif( isset($_POST['addJob']) )
  253. $remote->scheduler_add_task($job);
  254. list($jobsArray, $remote_servers_offline) = reloadJobs($server_homes, $remote_servers);
  255. }
  256. }
  257. elseif( isset($_POST['removeJob']) and isset($remote_servers[$_POST['r_server_id']]) and isset($jobsArray[$_POST['r_server_id']][$_POST['job_id']]) )
  258. {
  259. $remote = new OGPRemoteLibrary( $remote_servers[$_POST['r_server_id']]['agent_ip'],
  260. $remote_servers[$_POST['r_server_id']]['agent_port'],
  261. $remote_servers[$_POST['r_server_id']]['encryption_key'],
  262. $remote_servers[$_POST['r_server_id']]['timeout'] );
  263. $remote->scheduler_del_task($_POST['job_id']);
  264. list($jobsArray, $remote_servers_offline) = reloadJobs($server_homes, $remote_servers);
  265. }
  266. echo "<h2>" . schedule_new_job . "</h2>";
  267. require_once("includes/refreshed.php");
  268. $refresh = new refreshed();
  269. $homeid_ip_port = isset($_POST['homeid_ip_port']) ? $_POST['homeid_ip_port'] : key($server_homes);
  270. $r_server_id = $server_homes[$homeid_ip_port]['remote_server_id'];
  271. $curtime = $refresh->add( "home.php?m=cron&p=thetime&r_server_id=$r_server_id&type=cleared" );
  272. echo "<pre class='log' ><table><tr><td>" . now .
  273. "&nbsp;</td><td><form action='' method='POST' >" . get_server_selector($server_homes, $homeid_ip_port, TRUE) .
  274. "</form></td></tr></table> <b style='font-size:1.4em;'>" . $refresh->getdiv($curtime) . "</b></pre>";
  275. ?>
  276. <form method="POST" >
  277. <table class="center">
  278. <tr>
  279. <th>
  280. <?php echo minute; ?>
  281. </th>
  282. <th>
  283. <?php echo hour; ?>
  284. </th>
  285. <th>
  286. <?php echo day; ?>
  287. </th>
  288. <th>
  289. <?php echo month; ?>
  290. </th>
  291. <th>
  292. <?php echo day_of_the_week; ?>
  293. </th>
  294. <th>
  295. <?php echo action; ?>
  296. </th>
  297. <th>
  298. <?php echo user_games; ?>
  299. </th>
  300. </tr>
  301. <tr>
  302. <td style="width: 35px;" >
  303. <input style="width: 30px;" type="text" name="minute" value="*" />
  304. </td>
  305. <td style="width: 35px;" >
  306. <input style="width: 30px;" type="text" name="hour" value="*" />
  307. </td>
  308. <td style="width: 35px;" >
  309. <input style="width: 30px;" type="text" name="dayOfTheMonth" value="*" />
  310. </td>
  311. <td style="width: 35px;" >
  312. <input style="width: 30px;" type="text" name="month" value="*" />
  313. </td>
  314. <td style="width: 35px;" >
  315. <input style="width: 30px;" type="text" name="dayOfTheWeek" value="*" />
  316. </td>
  317. <td>
  318. <?php echo get_action_selector();?>
  319. </td>
  320. <td>
  321. <?php echo get_server_selector($server_homes, $homeid_ip_port);?>
  322. </td>
  323. <td>
  324. <input style="width:60px;" type="submit" name="addJob" value="<?php echo add; ?>" />
  325. </td>
  326. </tr>
  327. </table>
  328. </form>
  329. <br>
  330. <h2><?php echo scheduled_jobs;?></h2>
  331. <?php
  332. if ( !empty($jobsArray) )
  333. {
  334. ?>
  335. <table class="center">
  336. </tr>
  337. <tr>
  338. <th>
  339. <?php echo minute; ?>
  340. </th>
  341. <th>
  342. <?php echo hour; ?>
  343. </th>
  344. <th>
  345. <?php echo day; ?>
  346. </th>
  347. <th>
  348. <?php echo month; ?>
  349. </th>
  350. <th>
  351. <?php echo day_of_the_week; ?>
  352. </th>
  353. <th>
  354. <?php echo action; ?>
  355. </th>
  356. <th>
  357. <?php echo user_games; ?>
  358. </th>
  359. </tr>
  360. <?php
  361. $user_jobs = "";
  362. foreach( $jobsArray as $remote_server_id => $jobs )
  363. {
  364. foreach($jobs as $jobId => $job)
  365. {
  366. if(isset($job['action']))
  367. {
  368. if(hasValue($job['home_id']) && hasValue(@$job['ip']) && hasValue(@$job['port'])){
  369. $uniqueStr = $job['home_id']."_".$job['ip']."_".$job['port'];
  370. }else if(hasValue($job['home_id'])){
  371. $uniqueStr = $job['home_id'];
  372. }
  373. if(hasValue(@$uniqueStr)){
  374. $user_jobs .= '<tr>
  375. <td style="width: 35px;" >
  376. <form method="POST" >
  377. <input style="width: 30px;" type="text" name="minute" value="'.$job['minute'].'" />
  378. </td>
  379. <td style="width: 35px;" >
  380. <input style="width: 30px;" type="text" name="hour" value="'.$job['hour'].'" />
  381. </td>
  382. <td style="width: 35px;" >
  383. <input style="width: 30px;" type="text" name="dayOfTheMonth" value="'.$job['dayOfTheMonth'].'" />
  384. </td>
  385. <td style="width: 35px;" >
  386. <input style="width: 30px;" type="text" name="month" value="'.$job['month'].'" />
  387. </td>
  388. <td style="width: 35px;" >
  389. <input style="width: 30px;" type="text" name="dayOfTheWeek" value="'.$job['dayOfTheWeek'].'" />
  390. </td>
  391. <td>
  392. '.get_action_selector($job['action'])."</td><td>".
  393. get_server_selector($server_homes, $uniqueStr).'
  394. </td>
  395. <td style="width: 132px;">
  396. <input type="hidden" name="job_id" value=\''.$jobId.'\' />
  397. <input type="hidden" name="r_server_id" value=\''.$remote_server_id.'\' />
  398. <input style="width:60px;" type="submit" name="editJob" value="'. edit .'" />
  399. <input style="width:60px;" type="submit" name="removeJob" value="'. remove .'" />
  400. </form>
  401. </td>
  402. </tr>';
  403. }
  404. }
  405. }
  406. }
  407. echo $user_jobs;
  408. ?>
  409. </table>
  410. <?php
  411. }
  412. else
  413. echo "<h3>". there_are_no_scheduled_jobs ."</h3>";
  414. ?>
  415. <table class='center' ><tr><td><a href='?m=administration&p=main' > << <?php echo back ?></a><?php if(!$boolShowedAdminLink){ echo '&nbsp; &nbsp; | &nbsp; &nbsp; ' . '<a href="home.php?m=cron&p=cron">' . get_lang('cron_admin_link_display_text') . '</a>'; }?></td></tr></table>
  416. <script type="text/javascript">
  417. $(document).ready(function()
  418. {
  419. <?php echo $refresh->build("1000"); ?>
  420. }
  421. );
  422. </script>
  423. <?php
  424. }
  425. ?>