user_cron.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. $homes = $db->getIpPortsForUser($_SESSION['user_id']);
  159. if(!$homes)
  160. {
  161. print_failure(get_lang('cron_no_servers_tied_to_account'));
  162. if($isAdmin){
  163. echo '<a href="home.php?m=cron&p=cron">' . get_lang('cron_admin_link_display_text') . '</a>';
  164. }
  165. return 0;
  166. }
  167. foreach( $homes as $home )
  168. {
  169. $id = $home['home_id']."_".$home['ip']."_".$home['port'];
  170. $server_homes[$id] = $home;
  171. $server_id = $home['remote_server_id'];
  172. $remote_servers[$server_id] = array("remote_server_id" => $home['remote_server_id'],
  173. "remote_server_name" => $home['remote_server_name'],
  174. "ogp_user" => $home['ogp_user'],
  175. "agent_ip" => $home['agent_ip'],
  176. "agent_port" => $home['agent_port'],
  177. "ftp_port" => $home['ftp_port'],
  178. "encryption_key" => $home['encryption_key'],
  179. "timeout" => $home['timeout'],
  180. "use_nat" => $home['use_nat'],
  181. "ftp_ip" => $home['ftp_ip']);
  182. }
  183. list($jobsArray, $remote_servers_offline) = reloadJobs($server_homes, $remote_servers);
  184. if( isset($_POST['addJob']) or isset($_POST['editJob']) )
  185. {
  186. if ( isset( $_POST['homeid_ip_port'] ) and isset($server_homes[$_POST['homeid_ip_port']]) )
  187. {
  188. $game_home = $server_homes[$_POST['homeid_ip_port']];
  189. $server_xml = read_server_config(SERVER_CONFIG_LOCATION."/".$game_home['home_cfg_file']);
  190. $remote = new OGPRemoteLibrary( $game_home['agent_ip'],
  191. $game_home['agent_port'],
  192. $game_home['encryption_key'],
  193. $game_home['timeout']);
  194. $home_id = $game_home['home_id'];
  195. $ip = $game_home['ip'];
  196. $port = $game_home['port'];
  197. $control_protocol = $server_xml->control_protocol;
  198. $control_password = $game_home['control_password'];
  199. $control_type = $server_xml->control_protocol_type;
  200. $home_path = $game_home['home_path'];
  201. $server_exe = $server_xml->server_exec_name;
  202. $run_dir = $server_xml->exe_location;
  203. $game_home['mods'][$game_home['mod_id']] = Array ("mod_cfg_id" => $game_home['mod_cfg_id'],
  204. "max_players" => $game_home['max_players'],
  205. "extra_params" => $game_home['extra_params'],
  206. "cpu_affinity" => $game_home['cpu_affinity'],
  207. "nice" => $game_home['nice'],
  208. "precmd" => $game_home['precmd'],
  209. "postcmd" => $game_home['postcmd'],
  210. "home_cfg_id" => $game_home['home_cfg_id'],
  211. "mod_key" => $game_home['mod_key'],
  212. "mod_name" => $game_home['mod_name'],
  213. "def_precmd" => $game_home['def_precmd'],
  214. "def_postcmd" => $game_home['def_postcmd']);
  215. $startup_cmd = get_start_cmd($remote,$server_xml,$game_home,$game_home['mod_id'],$game_home['ip'],$game_home['port'], $remote->what_os());
  216. $cpu = $game_home['cpu_affinity'];
  217. $nice = $game_home['nice'];
  218. switch ($_POST['action']) {
  219. case "stop":
  220. $command = "%ACTION=stop|%|$home_id|%|$ip|%|$port|%|".
  221. "$control_protocol|%|$control_password|%|$control_type|%|$home_path";
  222. break;
  223. case "start":
  224. $command = "%ACTION=start|%|$home_id|%|$home_path|%|$server_exe|%|$run_dir|%|".
  225. "$startup_cmd|%|$port|%|$ip|%|$cpu|%|$nice";
  226. break;
  227. case "restart":
  228. $command = "%ACTION=restart|%|$home_id|%|$ip|%|$port|%|$control_protocol|%|".
  229. "$control_password|%|$control_type|%|$home_path|%|$server_exe|%|$run_dir|%|".
  230. "$startup_cmd|%|$cpu|%|$nice";
  231. break;
  232. case "steam_auto_update":
  233. $panelURL = getOGPSiteURL();
  234. if($panelURL !== false){
  235. $command = "wget -N \"" . $panelURL . "/ogp_api.php?action=autoUpdateSteamHome&homeid=" . $home_id . "&controlpass=" . $control_password . "\" > /dev/null 2>&1";
  236. }else{
  237. print_failure('Failed to retrieve panel URL.');
  238. return 0;
  239. }
  240. break;
  241. }
  242. $job = $_POST['minute']." ".
  243. $_POST['hour']." ".
  244. $_POST['dayOfTheMonth']." ".
  245. $_POST['month']." ".
  246. $_POST['dayOfTheWeek']." ".
  247. $command;
  248. if( isset($_POST['editJob']) and isset($jobsArray[$_POST['r_server_id']][$_POST['job_id']]) )
  249. $remote->scheduler_edit_task($_POST['job_id'], $job);
  250. elseif( isset($_POST['addJob']) )
  251. $remote->scheduler_add_task($job);
  252. list($jobsArray, $remote_servers_offline) = reloadJobs($server_homes, $remote_servers);
  253. }
  254. }
  255. elseif( isset($_POST['removeJob']) and isset($remote_servers[$_POST['r_server_id']]) and isset($jobsArray[$_POST['r_server_id']][$_POST['job_id']]) )
  256. {
  257. $remote = new OGPRemoteLibrary( $remote_servers[$_POST['r_server_id']]['agent_ip'],
  258. $remote_servers[$_POST['r_server_id']]['agent_port'],
  259. $remote_servers[$_POST['r_server_id']]['encryption_key'],
  260. $remote_servers[$_POST['r_server_id']]['timeout'] );
  261. $remote->scheduler_del_task($_POST['job_id']);
  262. list($jobsArray, $remote_servers_offline) = reloadJobs($server_homes, $remote_servers);
  263. }
  264. echo "<h2>" . schedule_new_job . "</h2>";
  265. require_once("includes/refreshed.php");
  266. $refresh = new refreshed();
  267. $homeid_ip_port = isset($_POST['homeid_ip_port']) ? $_POST['homeid_ip_port'] : key($server_homes);
  268. $r_server_id = $server_homes[$homeid_ip_port]['remote_server_id'];
  269. $curtime = $refresh->add( "home.php?m=cron&p=thetime&r_server_id=$r_server_id&type=cleared" );
  270. echo "<pre class='log' ><table><tr><td>" . now .
  271. "&nbsp;</td><td><form action='' method='POST' >" . get_server_selector($server_homes, $homeid_ip_port, TRUE) .
  272. "</form></td></tr></table> <b style='font-size:1.4em;'>" . $refresh->getdiv($curtime) . "</b></pre>";
  273. ?>
  274. <form method="POST" >
  275. <table class="center">
  276. <tr>
  277. <th>
  278. <?php echo minute; ?>
  279. </th>
  280. <th>
  281. <?php echo hour; ?>
  282. </th>
  283. <th>
  284. <?php echo day; ?>
  285. </th>
  286. <th>
  287. <?php echo month; ?>
  288. </th>
  289. <th>
  290. <?php echo day_of_the_week; ?>
  291. </th>
  292. <th>
  293. <?php echo action; ?>
  294. </th>
  295. <th>
  296. <?php echo user_games; ?>
  297. </th>
  298. </tr>
  299. <tr>
  300. <td style="width: 35px;" >
  301. <input style="width: 30px;" type="text" name="minute" value="*" />
  302. </td>
  303. <td style="width: 35px;" >
  304. <input style="width: 30px;" type="text" name="hour" value="*" />
  305. </td>
  306. <td style="width: 35px;" >
  307. <input style="width: 30px;" type="text" name="dayOfTheMonth" value="*" />
  308. </td>
  309. <td style="width: 35px;" >
  310. <input style="width: 30px;" type="text" name="month" value="*" />
  311. </td>
  312. <td style="width: 35px;" >
  313. <input style="width: 30px;" type="text" name="dayOfTheWeek" value="*" />
  314. </td>
  315. <td>
  316. <?php echo get_action_selector();?>
  317. </td>
  318. <td>
  319. <?php echo get_server_selector($server_homes, $homeid_ip_port);?>
  320. </td>
  321. <td>
  322. <input style="width:60px;" type="submit" name="addJob" value="<?php echo add; ?>" />
  323. </td>
  324. </tr>
  325. </table>
  326. </form>
  327. <br>
  328. <h2><?php echo scheduled_jobs;?></h2>
  329. <?php
  330. if ( !empty($jobsArray) )
  331. {
  332. ?>
  333. <table class="center">
  334. </tr>
  335. <tr>
  336. <th>
  337. <?php echo minute; ?>
  338. </th>
  339. <th>
  340. <?php echo hour; ?>
  341. </th>
  342. <th>
  343. <?php echo day; ?>
  344. </th>
  345. <th>
  346. <?php echo month; ?>
  347. </th>
  348. <th>
  349. <?php echo day_of_the_week; ?>
  350. </th>
  351. <th>
  352. <?php echo action; ?>
  353. </th>
  354. <th>
  355. <?php echo user_games; ?>
  356. </th>
  357. </tr>
  358. <?php
  359. $user_jobs = "";
  360. foreach( $jobsArray as $remote_server_id => $jobs )
  361. {
  362. foreach($jobs as $jobId => $job)
  363. {
  364. if(isset($job['action']))
  365. {
  366. if(hasValue($job['home_id']) && hasValue(@$job['ip']) && hasValue(@$job['port'])){
  367. $uniqueStr = $job['home_id']."_".$job['ip']."_".$job['port'];
  368. }else if(hasValue($job['home_id'])){
  369. $uniqueStr = $job['home_id'];
  370. }
  371. if(hasValue(@$uniqueStr)){
  372. $user_jobs .= '<tr>
  373. <td style="width: 35px;" >
  374. <form method="POST" >
  375. <input style="width: 30px;" type="text" name="minute" value="'.$job['minute'].'" />
  376. </td>
  377. <td style="width: 35px;" >
  378. <input style="width: 30px;" type="text" name="hour" value="'.$job['hour'].'" />
  379. </td>
  380. <td style="width: 35px;" >
  381. <input style="width: 30px;" type="text" name="dayOfTheMonth" value="'.$job['dayOfTheMonth'].'" />
  382. </td>
  383. <td style="width: 35px;" >
  384. <input style="width: 30px;" type="text" name="month" value="'.$job['month'].'" />
  385. </td>
  386. <td style="width: 35px;" >
  387. <input style="width: 30px;" type="text" name="dayOfTheWeek" value="'.$job['dayOfTheWeek'].'" />
  388. </td>
  389. <td>
  390. '.get_action_selector($job['action'])."</td><td>".
  391. get_server_selector($server_homes, $uniqueStr).'
  392. </td>
  393. <td style="width: 132px;">
  394. <input type="hidden" name="job_id" value=\''.$jobId.'\' />
  395. <input type="hidden" name="r_server_id" value=\''.$remote_server_id.'\' />
  396. <input style="width:60px;" type="submit" name="editJob" value="'. edit .'" />
  397. <input style="width:60px;" type="submit" name="removeJob" value="'. remove .'" />
  398. </form>
  399. </td>
  400. </tr>';
  401. }
  402. }
  403. }
  404. }
  405. echo $user_jobs;
  406. ?>
  407. </table>
  408. <?php
  409. }
  410. else
  411. echo "<h3>". there_are_no_scheduled_jobs ."</h3>";
  412. ?>
  413. <table class='center' ><tr><td><a href='?m=administration&p=main' > << <?php echo back ?></a></td></tr></table>
  414. <script type="text/javascript">
  415. $(document).ready(function()
  416. {
  417. <?php echo $refresh->build("1000"); ?>
  418. }
  419. );
  420. </script>
  421. <?php
  422. }
  423. ?>