Log of messages
18 | * 19 | *"; 82 | $col = ""; 83 | } else { 84 | echo " | |
"; 85 | $col = " | "; 86 | } 87 | $display = str_replace($replace_this, $replace_with_that, $key); 88 | if(is_string($value)||is_int($value)) { 89 | if($key=="allow_commands") { 90 | if($value=="true") { 91 | echo "$display:$col"; 92 | } elseif($value=="false") { 93 | echo "$display:$col"; 94 | } else { 95 | echo "$display:$col"; 96 | } 97 | } else { 98 | //ghetto way to add version selection to this page 99 | if($key == "max_players") { 100 | echo "Server Version:$col Version Manager"; 109 | echo " |
";
111 | }
112 | echo "$display:$col "; 113 | } 114 | } elseif(is_array($value)) { 115 | if($key == "visibility") { 116 | echo "$display:$col"; 117 | foreach($value as $sub_key => $sub_value) { 118 | if($sub_value=="true") { 119 | echo "$sub_key: "; 120 | } else { 121 | echo "$sub_key: "; 122 | } 123 | } 124 | echo " "; 125 | } else { 126 | echo "$display:$col"; 127 | $sub_value = ""; 128 | if($value!="") { 129 | $sub_value = implode(", ", $value); 130 | } 131 | echo " "; 132 | echo " "; 133 | } 134 | } elseif(is_bool($value)) { 135 | if($value==true) { 136 | echo "$display:$col "; 137 | } else { 138 | echo "$display:$col "; 139 | } 140 | } else { 141 | echo "$key:$col"; 142 | var_dump($value); 143 | echo " "; 144 | } 145 | echo " |
"; 153 | //echo json_encode($server_settings, JSON_PRETTY_PRINT); 154 | //echo ""; 155 | } 156 | } 157 | die(); 158 | } elseif(isset($_REQUEST['server_select'])) { 159 | $verified_data = []; 160 | $err_data["error"] = true; 161 | $err = 0; 162 | $total_array = array(); 163 | $ignore_array = array("d","server_select"); 164 | $settype_string = array("name","description","game_password","allow_commands"); 165 | $settype_integers = array("max_players","max_upload_in_kilobytes_per_second","autosave_interval","autosave_slots","afk_autokick_interval","minimum_latency_in_ticks"); 166 | $settype_boolean = array("visibility-public","visibility-lan","require_user_verification","ignore_player_limit_for_returning_players","auto_pause","only_admins_can_pause_the_game","autosave_only_on_server","non_blocking_saving"); 167 | $settype_array = array("tags","admins"); 168 | $check_array_admin = array("true","false","admins-only"); 169 | foreach($_REQUEST as $key => $value) { 170 | $clean_key = preg_replace('/[^\da-z]_/i', '', $key); 171 | $clean_value = preg_replace(array("/\", "/\>/", "/\s+/"), array("", "", " "), $value); 172 | if(in_array($clean_key, $settype_string) || ($clean_key == "allow_commands" && in_array($clean_value, $check_array_admin))) { 173 | $verified_data[$clean_key] = $clean_value; 174 | continue; 175 | } elseif(in_array($clean_key, $settype_integers)) { 176 | if(is_numeric($clean_value)) { 177 | settype($clean_value, "integer"); 178 | $verified_data[$clean_key] = $clean_value; 179 | } else { 180 | $err_data[$clean_key]=$clean_value; 181 | $err++; 182 | } 183 | continue; 184 | } elseif(in_array($clean_key, $settype_array)) { 185 | //work this 186 | $raw_array = explode(',', $clean_value); 187 | $trimmed_array=array_map('trim',$raw_array); 188 | $verified_data[$clean_key] = $trimmed_array; 189 | continue; 190 | } elseif(in_array($clean_key, $settype_boolean)) { 191 | if($clean_value == "true") { 192 | $clean_value = true; 193 | } elseif($clean_value == "false") { 194 | $clean_value = false; 195 | } else { 196 | $err_data[$clean_key]=$clean_value; 197 | $err++; 198 | continue; 199 | } 200 | if($clean_key == "visibility-public" || $clean_key == "visibility-lan") { 201 | $raw_value = explode('-', $clean_key); 202 | $verified_data["visibility"][$raw_value[1]] = $clean_value; 203 | } else { 204 | $verified_data[$clean_key] = $clean_value; 205 | } 206 | continue; 207 | } elseif(!in_array($clean_key, $ignore_array)) { 208 | if($clean_key == "s_version") { 209 | $s_version = $clean_value; 210 | continue; 211 | } 212 | $err_data[$clean_key]=$clean_value; 213 | $err++; 214 | continue; 215 | } 216 | } 217 | 218 | if(isset($err) && $err > 0) { 219 | echo json_encode($err_data, JSON_PRETTY_PRINT); 220 | } else { 221 | $date = date('Y-m-d'); 222 | $time = date('H:i:s'); 223 | $server_dir = $base_dir . $server_select . "/"; 224 | $server_config_path = $server_dir . "config/config.ini"; 225 | $server_settings_path = $server_dir . "server-settings.json"; 226 | $server_settings_web_path = $server_dir . "server-settings-web.json"; 227 | $server_settings_run_path = $server_dir . "running-server-settings.json"; 228 | $server_log_loc = $server_dir . "logs/"; 229 | $server_log_path = $server_dir . "logs/server-settings-update-$date.log"; 230 | 231 | if(isset($s_version)) { 232 | if(isset($server_installed_versions[$s_version])) { 233 | $server_settings_web['version']=$s_version; 234 | $newJsonString = json_encode($server_settings_web, JSON_PRETTY_PRINT); 235 | file_put_contents($server_settings_web_path, $newJsonString); 236 | //also want to update the config.ini file 237 | if(file_exists($server_config_path)) { 238 | $lines = file($server_config_path); 239 | $new_config = array(); 240 | foreach($lines as $line) { 241 | if(substr($line, 0, 10) == 'read-data=') { 242 | $new_config[] = "read-data=".$server_installed_versions[$s_version]."/data\n"; 243 | } else { 244 | $new_config[] = $line; 245 | } 246 | } 247 | file_put_contents($server_config_path, $new_config); 248 | } 249 | } 250 | } 251 | if(file_exists($server_settings_path)) { 252 | $server_settings = json_decode(file_get_contents("$base_dir$server_select/server-settings.json"), true); 253 | foreach($verified_data as $key => $value) { 254 | $server_settings[$key] = $verified_data[$key]; 255 | } 256 | $newJsonString = json_encode($server_settings, JSON_PRETTY_PRINT); 257 | $newJsonStringUgly = json_encode($server_settings); 258 | $newRawQuery = http_build_query($_REQUEST); 259 | $log_record = "\xA$date-$time\t".$user_name."\xA$newJsonStringUgly\xA$newRawQuery\xA"; 260 | if($log_record != "") { 261 | if (!is_dir($server_log_loc)) { 262 | // dir doesn't exist, make it 263 | mkdir($server_log_loc); 264 | } 265 | file_put_contents($server_log_path, $log_record, FILE_APPEND); 266 | } 267 | file_put_contents($server_settings_path, $newJsonString); 268 | $output = json_encode("Settings Updated"); 269 | die($output); 270 | } else { 271 | $output = json_encode("No settings file found"); 272 | die($output); 273 | } 274 | } 275 | die(); 276 | } 277 | } 278 | ?> 279 | 280 | 281 | 282 | 396 | 397 | 398 | 399 | 400 | 401 |
Updating...\r\n"; 18 | ob_flush(); 19 | flush(); 20 | //retrieve $count to know how mant times to loop later 21 | exec('bash update.sh count', $count); 22 | ob_flush(); 23 | flush(); 24 | //we loop here so we can flush the output and view the update progress in the web control. 25 | for($n=1; $n<=$count[0]; $n++) { 26 | system('bash update.sh '.$n.''); 27 | ob_flush(); 28 | flush(); 29 | } 30 | echo "Done\r\n\r\n\r\n"; 31 | echo ""; 32 | ob_flush(); 33 | flush(); 34 | } 35 | } else { 36 | header("Location: ./login.php?POSTnotset"); 37 | die(); 38 | } 39 | } else { 40 | header("Location: ./login.php?notadmin"); 41 | die(); 42 | } 43 | -------------------------------------------------------------------------------- /html/version_manager.php: -------------------------------------------------------------------------------- 1 | "install", "username" => $user_name, "time" => $GLOBALS['date'] ." ". $GLOBALS['time']), JSON_PRETTY_PRINT)); 89 | if(is_dir($program_dir)) { 90 | unlink($tmp_file); 91 | return "Install failed. Directory exists."; 92 | } else { 93 | $url = "https://www.factorio.com/download/archive/"; 94 | //run this script on each url in the array until a match is found 95 | $server_matched_versions = get_url($url); 96 | //if a download link is found, iterate the results 97 | if(isset($server_matched_versions[1])) { 98 | foreach($server_matched_versions[1] as $key => $value) { 99 | $direct_url = "https://factorio.com/get-download/$version/headless/linux64"; 100 | } 101 | } 102 | if(isset($direct_url)) { 103 | //create status files periodically so other users know whats going on. Should be able to use this for active user status updates as well 104 | file_put_contents($tmp_file, json_encode(array("action" => "downloading", "username" => $user_name, "time" => $GLOBALS['date'] ." ". $GLOBALS['time']), JSON_PRETTY_PRINT)); 105 | //get's filename and download url, actually... 106 | $file = getFilename($direct_url); 107 | //make sure we get both in return 108 | if(isset($file[0])&&isset($file[1])) { 109 | //define the function so we can get download status as we download 110 | function progressCallback( $resource, $download_size, $downloaded_size, $upload_size, $uploaded_size ) 111 | { 112 | global $progress_file; 113 | static $previousProgress = 0; 114 | if ( $download_size == 0 ) 115 | $progress = 0; 116 | else 117 | $progress = round( $downloaded_size * 100 / $download_size ); 118 | if ( $progress > $previousProgress) 119 | { 120 | $previousProgress = $progress; 121 | //this *should* replace the file contents on each update. ajax can check for updates for a pretty progress bar and/or percentage 122 | file_put_contents( $progress_file, "$progress" ); 123 | } 124 | } 125 | //clean up the URL, filename and set the temporary path 126 | $url = $file[0]; 127 | $filename_loc = "/tmp/".$file[1]; 128 | file_put_contents( $progress_file, '0' ); 129 | $targetFile = fopen( $filename_loc, 'w' ); 130 | $ch = curl_init(); 131 | curl_setopt($ch, CURLOPT_URL, $url); 132 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 133 | curl_setopt($ch, CURLOPT_HEADER, false); 134 | curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 135 | curl_setopt($ch, CURLOPT_NOPROGRESS, false ); 136 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 137 | curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback' ); 138 | curl_setopt($ch, CURLOPT_FILE, $targetFile ); 139 | $result = curl_exec($ch); 140 | curl_close($ch); 141 | fclose( $targetFile ); 142 | if($result === false) 143 | { 144 | return 'Curl error: ' . __LINE__ . ' ' . curl_error($ch); 145 | } //continue if successful 146 | unlink($progress_file); 147 | file_put_contents($tmp_file, json_encode(array("action" => "unpacking", "username" => $user_name, "time" => $GLOBALS['date'] ." ". $GLOBALS['time']), JSON_PRETTY_PRINT)); 148 | if(is_dir($program_dir)) { 149 | return "directory exists"; 150 | } else { 151 | $fileType = mime_content_type($filename_loc); 152 | switch ($fileType) { 153 | case "application/x-xz": 154 | //unlink($filename_loc); 155 | //return "filetype not yet implemented: $fileType"; 156 | $tar_dir = "/tmp/$version/"; 157 | if(is_dir($tar_dir)) { 158 | rrmdir($tar_dir); 159 | } 160 | mkdir($tar_dir); 161 | exec("tar -xf $filename_loc -C $tar_dir"); 162 | function is_dir_empty($dir) { 163 | if (!is_readable($dir)) return NULL; 164 | $handle = opendir($dir); 165 | while (false !== ($entry = readdir($handle))) { 166 | if ($entry != "." && $entry != "..") { 167 | return FALSE; 168 | } 169 | } 170 | return TRUE; 171 | } 172 | unlink($filename_loc); 173 | if(is_dir_empty($tar_dir)) { 174 | return "install fail. 'tar_dir' is empty $tar_dir"; 175 | } else { 176 | $files_dir = $tar_dir."factorio"; 177 | move_dir($files_dir, $program_dir); 178 | rrmdir($tar_dir); 179 | if(is_dir_empty($program_dir)) { 180 | return "failed to move from tmp to $program_dir"; 181 | } else { 182 | return "Install Successfull! $program_dir"; 183 | } 184 | } 185 | break; 186 | case "application/x-gzip"; 187 | $filename_tar = pathinfo( $filename_loc, PATHINFO_FILENAME ).".tar"; 188 | $filepath_tar = "/tmp/$filename_tar"; 189 | if(file_exists($filepath_tar)) { 190 | unlink($filepath_tar); 191 | } 192 | $p = new PharData($filename_loc); 193 | $p->decompress(); // creates /path/to/my.tar 194 | unlink($filename_loc); 195 | $i = 0; 196 | while ( $i < 8 ) { 197 | if(!file_exists($filepath_tar)) { 198 | usleep(250000); 199 | } else { 200 | $i=10; 201 | } 202 | $i++; 203 | } 204 | if(!file_exists($filepath_tar)) { 205 | return "unable to make tar file"; 206 | } 207 | // unarchive from the tar 208 | try { 209 | $phar = new PharData($filepath_tar); 210 | $tar_dir = "/tmp/$version/"; 211 | //mkdir($tar_dir); 212 | $phar->extractTo($tar_dir); 213 | } catch (Exception $e) { 214 | unlink($filepath_tar); 215 | if(is_dir($tar_dir)) rrmdir($tar_dir); 216 | return "tar extract failure: $e"; 217 | // handle errors 218 | } 219 | function is_dir_empty($dir) { 220 | if (!is_readable($dir)) return NULL; 221 | $handle = opendir($dir); 222 | while (false !== ($entry = readdir($handle))) { 223 | if ($entry != "." && $entry != "..") { 224 | return FALSE; 225 | } 226 | } 227 | return TRUE; 228 | } 229 | unlink($filepath_tar); 230 | if(is_dir_empty($tar_dir)) { 231 | return "install fail. Dir is empty"; 232 | } else { 233 | $files_dir = $tar_dir."factorio"; 234 | move_dir($files_dir, $program_dir); 235 | rmdir($tar_dir); 236 | if(is_dir_empty($program_dir)) { 237 | return "failed to move from tmp to $program_dir"; 238 | } else { 239 | return "success"; 240 | } 241 | } 242 | break; 243 | default: 244 | return "unsupported filetyle: $fileType"; 245 | } 246 | } 247 | } else { 248 | return "issue finding remote file ".$file[0]." ".$file[1]; 249 | } 250 | } else { 251 | return "no download found"; 252 | } 253 | } 254 | } 255 | function delete($version, $program_dir, $tmp_file) { 256 | file_put_contents($tmp_file, json_encode(array("action" => "deleting", "username" => $user_name, "time" => $GLOBALS['date'] ." ". $GLOBALS['time']), JSON_PRETTY_PRINT)); 257 | rrmdir($program_dir); 258 | if(is_dir($program_dir)) { 259 | unlink($tmp_file); 260 | return "delete failed"; 261 | } else { 262 | unlink($tmp_file); 263 | return "success"; 264 | } 265 | } 266 | $log_dir = "/var/www/factorio/logs"; 267 | $log_path = "$log_dir/version-manager-".$GLOBALS['date'].".log"; 268 | if(isset($_REQUEST)) { 269 | if(isset($_REQUEST['status'])&&$_REQUEST['status']!="") { 270 | if( $user_level == "viewonly" ) { 271 | die('View-only may not manage versions'); 272 | } 273 | if($_REQUEST['status']!="") { 274 | $js_value = preg_replace('/_/', '.', $_REQUEST['status']); 275 | $version = preg_replace('/[^0-9.]+/', '', $js_value); 276 | $tmp_file = "/tmp/factorio-version-manager_progress.$version.txt"; 277 | //factorio-version-manager_progress.0.12.35.txt 278 | if(file_exists($tmp_file)) { 279 | $result = file_get_contents($tmp_file); 280 | } else { 281 | $result = 0; 282 | } 283 | } else { 284 | $result = "NVP"; 285 | } 286 | echo $result; 287 | die(); 288 | } if(isset($_REQUEST['install'])) { 289 | if( $user_level == "viewonly" ) { 290 | die('View-only may not manage versions'); 291 | } 292 | if($_REQUEST['install']!="") { 293 | $js_value = preg_replace('/_/', '.', $_REQUEST['install']); 294 | $version = preg_replace('/[^0-9.]+/', '', $js_value); 295 | $program_dir = $program_dir.$version."/"; 296 | $tmp_file = "/tmp/factorio-version-manager_status.$version.txt"; 297 | if(is_dir($program_dir)) { 298 | $result = "Install failed. Directory exists."; 299 | } else { 300 | if(file_exists($tmp_file)) { 301 | $tmp_file_contents = json_decode(file_get_contents($tmp_file)); 302 | die('Action in progress: '.$tmp_file_contents->action.' by '.$tmp_file_contents->username); 303 | } else { 304 | $result = install($version, $program_dir, $tmp_file); 305 | unlink($tmp_file); 306 | } 307 | } 308 | } else { 309 | $result = "No Version provided"; 310 | } 311 | echo $result; 312 | $log_record = $GLOBALS['time'] ." ". $GLOBALS['date'] ." $version $result : $username \xA"; 313 | file_put_contents( $log_path, $log_record, FILE_APPEND ); 314 | die(); 315 | } elseif( isset( $_REQUEST['delete'] ) ) { 316 | if( $user_level == "viewonly" ) { 317 | die('View-only may not manage versions'); 318 | } 319 | if( $_REQUEST['delete']!="" ) { 320 | $js_value = preg_replace('/_/', '.', $_REQUEST['delete']); 321 | $version = preg_replace( '/[^0-9.]+/', '', $js_value ); 322 | $program_dir = $program_dir.$version."/"; 323 | $tmp_file = "/tmp/factorio-version-manager_status.$version.txt"; 324 | if(is_dir($program_dir)) { 325 | $dir_user = posix_getpwuid( fileowner( $program_dir )); 326 | if( isset( $dir_user['name'] ) && $dir_user['name'] != "www-data" ) { 327 | $result = "Invalid filesystem permissions to remove installation."; 328 | } else { 329 | if( file_exists( $tmp_file ) ) { 330 | $tmp_file_contents = json_decode( file_get_contents( $tmp_file ) ); 331 | die('Action in progress: '.$tmp_file_contents->action.' by '.$tmp_file_contents->username); 332 | } else { 333 | $result = delete($version, $program_dir, $tmp_file); 334 | } 335 | } 336 | } else { 337 | $result = "Version $version not found"; 338 | } 339 | } else { 340 | $result = "No Version provided"; 341 | } 342 | echo $result; 343 | $log_record = $GLOBALS['time'] ." ". $GLOBALS['date'] ." $version $result : $username \xA"; 344 | file_put_contents($log_path, $log_record, FILE_APPEND); 345 | die(); 346 | } elseif(isset($_REQUEST['show'])) { 347 | if($_REQUEST['show']=="true") { 348 | //print_r($server_installed_versions); 349 | echo "
Version | Control | \xA"; 367 | foreach($GLOBALS['total_versions'] as $value) { 368 | $js_value = preg_replace('#\.#', '_', $value); 369 | echo "||
$value | "; 370 | if(isset($server_available_versions[$value])) { 371 | echo " | "; 372 | } else { 373 | echo "depreciated | "; 374 | } 375 | //if the server is working on installing a version, this file will exist and hold the status of the install 376 | $tmp_file = "/tmp/factorio-version-manager_status.$value.txt"; 377 | if(file_exists($tmp_file)) { 378 | $tmp_status[$value] = file_get_contents($tmp_file); 379 | } 380 | if(isset($tmp_status[$value])) { 381 | echo "$tmp_status[$value]"; 382 | } else { 383 | //if tmp_file doesn't exist, general rules for if it's installed or not can be displayed 384 | if(isset($server_installed_versions[$value])) { 385 | $path = "/usr/share/factorio/$value"; 386 | $user = posix_getpwuid( fileowner( $path )); 387 | if(isset($user['name'])&&$user['name']!="www-data") { 388 | echo "Installed. Invalid filesystem permissions to delete."; 389 | } else { 390 | echo " - installed"; 391 | } 392 | } else { 393 | echo " "; 394 | } 395 | } 396 | echo " |