PlowmanPlow

AddSolderMod

Jun 14th, 2019
154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // Is the "mods" folder local or remote?
  4. // Valid values here are: true or false
  5. define('LOCALMODS', true);
  6.  
  7. // Database connection info
  8. define('DBHOST', 'localhost');
  9. define('DBPORT', '3306');
  10. define('DBUSER', 'root');
  11. define('DBPASS', '');
  12. define('DBNAME', 'solder');
  13. define('DBPREFIX', '');
  14.  
  15. // Make sure these both have a trailing slash!!!
  16. define('MODFOLDER', 'C:/Apache24/solder/public/mods/');
  17. define('TEMPFOLDER', 'C:/Apache24/Temp/');
  18.  
  19. // Remote target configuration. Can be ignored if LOCALMODS=true
  20. define('SCPCOMMAND', 'pscp -i C:\Full\Path\To\PuttyIdentity.ppk');
  21. define('REMOTETARGET', 'user@solderhost.com:/var/www/htdocs.solder/public/mods/');
  22.  
  23. //
  24. // No need to edit below here.
  25. // (but hey, you do you :) )
  26. //
  27.  
  28. //
  29. // Try to connect to the database. Abort on failure.
  30. //
  31. try {
  32.    $globaldbh = new PDO("mysql:host=" . DBHOST . ";port=" . DBPORT . ";dbname=" . DBNAME, DBUSER, DBPASS);
  33. } catch (PDOException $e) {
  34.    echo "Error connecting to DB!: ", $e->getMessage() . "\n";
  35.    die();
  36. }
  37.  
  38. //
  39. // Ensure there are at least two command line parameters.
  40. // Display usage on failure.
  41. //
  42. if ( $argc < 3 ) {
  43.    echo "Error: Invalid command line parameters\n\n";
  44.    echo "Usage: addsoldermod <slug> <modfilename> [-forge]\n";
  45.    echo "Info: <param> is required, [param] is optional, omit < > and [ ]\n\n";
  46.    exit();
  47. }
  48.  
  49. //
  50. // If there are 3 parameters, and the 3rd is '-forge'
  51. // set the $forge boolean to true so we can package
  52. // up the mod differently
  53. //
  54. if ( ($argc == 4) && ($argv[3] == "-forge") ) {
  55.    echo "Forge mode enabled! Using /bin/ instead of /mods/\n";
  56.    $forge = true;
  57. } else {
  58.    $forge = false;
  59. }
  60.  
  61. //
  62. // Function to validate the format of a slug
  63. //
  64. function isValidSlug($slug = "") {
  65.    $validchars = "abcdefghijklmnopqrstuvwxyz1234567890-";
  66.    $splitslug = str_split($slug);
  67.    foreach ( $splitslug as $char ) {
  68.       if ( strpos($validchars, $char) === false ) return false;
  69.    }
  70.    return true;
  71. }
  72.  
  73. //
  74. // Grab the command line arguments. Abort on failure.
  75. $slug = trim($argv[1]);
  76. $modfile = trim($argv[2]);
  77. echo "Slug: {$slug}\n";
  78. if ( !isValidSlug($slug) ) {
  79.    echo "Error: Invalid slug! Slugs can only contain lower case characters or hyphens!\n";
  80.    exit();
  81. }
  82. echo "Mod File: {$modfile}\n";
  83. if ( !file_exists($modfile) ) {
  84.    echo "Error: Mod file \"{$modfile}\" does not exist!\n";
  85.    exit();
  86. }
  87.  
  88. //
  89. // Query the database for mod metadata based on provided slug
  90. //
  91. $query = "SELECT id, name, description, author, link, pretty_name FROM " . DBPREFIX . "mods WHERE name=:name";
  92. $sth = $globaldbh->prepare($query);
  93. $fields = array();
  94. $fields[':name'] = $slug;
  95. $sth->execute($fields);
  96. if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
  97.    //
  98.    // If the slug exists, grab its metadata
  99.    //
  100.    $mod_id = $row['id'];
  101.    $mod_name = $slug;
  102.    $mod_pretty_name = $row['pretty_name'];
  103.    $mod_description = $row['description'];
  104.    $mod_author = $row['author'];
  105.    $mod_link = $row['link'];
  106.    $newmod = false;
  107. } else {
  108.    //
  109.    // If the slug does not exist, try to create a new Solder mod
  110.    //
  111.    $newmod = true;
  112.    $mod_id = null;
  113.    $mod_name = $slug;
  114.    $mod_pretty_name = "";
  115.    $mod_description = null;
  116.    $mod_author = null;
  117.    $mod_link = null;
  118.    echo "The mod slug \"{$slug}\" does not seem to exist yet.\n";
  119.    echo "Let's create a new Solder mod in the system now...\n";
  120.    echo "(<CTRL-C> now if you think you made a typo)\n\n";
  121.    //
  122.    // Get the mod name. This is the human readable name.
  123.    //
  124.    while ( $mod_pretty_name == "" ) {
  125.       $mod_pretty_name = readline("Mod Name (human readable display name): ");
  126.       if ( $mod_pretty_name == "" ) {
  127.          echo "Error: Mod name cannot be blank!\n";
  128.          continue;
  129.       }
  130.       // Check to see if that name matches anything in the database
  131.       $query = "SELECT id FROM " . DBPREFIX . "mods WHERE pretty_name like :pretty_name";
  132.       $fields = array();
  133.       $fields[':pretty_name'] = $mod_pretty_name;
  134.       $sth = $globaldbh->prepare($query);
  135.       $sth->execute($fields);
  136.       if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
  137.          echo "Error: A mod with that name already exists!\n";
  138.          $mod_pretty_name = "";
  139.          continue;
  140.       }
  141.    }
  142.    while ( $mod_description === null ) {
  143.       $mod_description = readline("Mod Description (can be blank): ");
  144.    }
  145.    while ( $mod_author === null ) {
  146.       $mod_author = readline("Mod Author (can be blank): ");
  147.    }
  148.    while ( $mod_link === null ) {
  149.       $mod_link = readline("Mod Homepage URL (can be blank): ");
  150.       if ( ($mod_link != "") && !filter_var($mod_link, FILTER_VALIDATE_URL) ) {
  151.          echo "Error: That is not a valid URL!\n";
  152.          $mod_link = null;
  153.       }
  154.    }
  155.    //
  156.    // Allow the user to abort if the information is incorrect.
  157.    //
  158.    echo "\nImportant! Please review the following information!\n\n";
  159.    echo "About to add this mod to solder:\n";
  160.    echo "   Mod Name: {$mod_pretty_name}\n";
  161.    echo "   Mod Slug: {$mod_name}\n";
  162.    echo "   Mod Author: {$mod_author}\n";
  163.    echo "   Mod Description: {$mod_description}\n";
  164.    echo "   Mod Homepage: {$mod_link}\n";
  165.    echo "\n";
  166.    // Query the user and abort if they don't hit "y" or "yes"
  167.    $response = strtolower(readline("Create this Solder mod now? (y/n): "));
  168.    if ( ($response !== "y") && ($response !== "yes") ) {
  169.       echo "Not creating mod! Aborting now!\n\n";
  170.       exit();
  171.    }
  172.    //
  173.    // Add the mod to the Solder database
  174.    //
  175.    $query = "INSERT INTO " . DBPREFIX . "mods (id, name, description, author, link, created_at, pretty_name) ";
  176.    $query .= "VALUES(null, :name, :description, :author, :link, NOW(), :pretty_name)";
  177.    $fields = array();
  178.    $fields[':name'] = $mod_name;
  179.    $fields[':description'] = $mod_description;
  180.    $fields[':author'] = $mod_author;
  181.    $fields[':link'] = $mod_link;
  182.    $fields[':pretty_name'] = $mod_pretty_name;
  183.    $sth = $globaldbh->prepare($query);
  184.    $sth->execute($fields);
  185.    if ( !$sth ) {
  186.       echo "Error: Could not add mod to Solder!\n";
  187.       echo "PDO::errorInfo():\n";
  188.       print_r($sth->errorInfo());
  189.       exit();
  190.    }
  191.    //
  192.    // Grab the ID of the newly created mod. Abort if not available.
  193.    //
  194.    $query = "SELECT id FROM " . DBPREFIX . "mods WHERE name=:name";
  195.    $fields = array();
  196.    $fields[':name'] = $mod_name;
  197.    $sth = $globaldbh->prepare($query);
  198.    $sth->execute($fields);
  199.    if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
  200.       $mod_id = $row['id'];
  201.    } else {
  202.       echo "Could not get ID of new mod! Aborting!\n\n";
  203.       exit();
  204.    }
  205.    echo "New mod successfully created!\n\n";
  206. }
  207. //
  208. // Get the mod version number
  209. //
  210. $modfile_basename = basename($modfile);
  211. echo "Please provide the mod version number for Solder.\n";
  212. echo "This is typically in the format of <MCVersion>-<ModVersion>\n";
  213. echo "(i.e. 1.12.2-3.4.8b1).\n";
  214. echo "Mod File Name: {$modfile_basename}\n";
  215. $mod_version = "";
  216. while ( $mod_version == "" ) {
  217.    $mod_version = readline("Mod Version: ");
  218. }
  219.  
  220. //
  221. // Create the properly formated Solder mod ZIP archive
  222. //
  223.  
  224. // Check to see if a mod ZIP with that version number already exists
  225. $solderzipname = $mod_name . "-" . $mod_version . ".zip";
  226. if ( LOCALMODS && file_exists(MODFOLDER . $mod_name . "/" . $solderzipname) ) {
  227.    echo "Error: A mod file named \"{$mod_name}-{$mod_version}.zip\" already exists!\n";
  228.    echo "Aborting!\n\n";
  229.    exit();
  230. }
  231. // Delete the existing temp file if it exists
  232. if ( LOCALMODS && file_exists(TEMPFOLDER . $solderzipname) ) unlink(TEMPFOLDER . $solderzipname);
  233. mkdir(TEMPFOLDER . $mod_name);
  234. $zip = new ZipArchive();
  235. // Abort here if we can't write to the temp folder
  236. if ( $zip->open(TEMPFOLDER . $mod_name . "/" . $solderzipname, ZipArchive::CREATE) !== TRUE ) {
  237.    echo "Error: The solder mod ZIP file could not be created!\n";
  238.    echo "Tried: {$solderzipname}\n";
  239.    echo "Aborting!\n\n";
  240. }
  241. // If this is Forge create a bin/modpack.jar. Create mods/modjarname.jar
  242. if ( $forge ) {
  243.    $zip->addEmptyDir('bin');
  244.    $zip->addFile($modfile, "bin/modpack.jar");
  245. } else {
  246.    $zip->addEmptyDir('mods');
  247.    $zip->addFile($modfile, "mods/{$modfile_basename}");
  248. }
  249. $zip->close();
  250. // Get MD5 sum and file size of the new Solder ZIP archive
  251. $solderzipmd5 = md5_file(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  252. $solderzipsize = filesize(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  253. //
  254. // Display the mod metadata and allow the user to abort
  255. //
  256. echo "\nPlease review the mod information carefully!\n\n";
  257. echo "Ready to add the new mod version:\n";
  258. echo "   Mod Name: {$mod_pretty_name}\n";
  259. echo "   Mod JAR: {$modfile_basename}\n";
  260. echo "   Mod ZIP: {$solderzipname}\n";
  261. echo "   Mod Version: {$mod_version}\n";
  262. echo "\n";
  263. $response = strtolower(readline("Add this new mod version? (y/n): "));
  264. if ( ($response !== "y") && ($response !== "yes") ) {
  265.    echo "Not adding new version! Aborting now!\n\n";
  266.    exit();
  267. }
  268. //
  269. // If mods are local we just make folders and move files around
  270. //
  271. if ( LOCALMODS ) {
  272.    // Create the mod's folder under MODFOLDER if not present
  273.    if ( !is_dir(MODFOLDER . $mod_name) ) {
  274.       mkdir(MODFOLDER . $mod_name);
  275.    }
  276.    // Abort if we could not make the folder
  277.    if ( !is_dir(MODFOLDER . $mod_name) ) {
  278.       echo "Error! Could not create mod folder in Solder. Aborting!\n\n";
  279.       unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  280.       rmdir(TEMPFOLDER . $mod_name);
  281.       exit();
  282.    }
  283.    // Move the Solder ZIP archive to the mod's folder under Solder. Abort on failure.
  284.    if ( rename(TEMPFOLDER . $mod_name . "/" . $solderzipname, MODFOLDER . $mod_name . "/" . $solderzipname) === false ) {
  285.       echo "Error! Could not place mod into Solder's mods folder. Aborting!\n\n";
  286.       unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  287.       rmdir(TEMPFOLDER . $mod_name);
  288.       exit();
  289.    } else {
  290.       rmdir(TEMPFOLDER . $mod_name);
  291.    }
  292. } else {
  293.    $junk = system(SCPCOMMAND . " -r \"" . TEMPFOLDER . $mod_name . "\" " . REMOTETARGET);
  294.    unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
  295.    rmdir(TEMPFOLDER . $mod_name);
  296. }
  297. //
  298. // Insert the new mod version into the Solder database
  299. //
  300. $query = "INSERT INTO " . DBPREFIX . "modversions (id, mod_id, version, md5, created_at, filesize) ";
  301. $query .= "VALUES(null, :mod_id, :version, :md5, NOW(), :filesize)";
  302. $fields = array();
  303. $fields[':mod_id'] = $mod_id;
  304. $fields[':version'] = $mod_version;
  305. $fields[':md5'] = $solderzipmd5;
  306. $fields[':filesize'] = $solderzipsize;
  307. $sth = $globaldbh->prepare($query);
  308. $sth->execute($fields);
  309.  
  310. // All done!
  311. echo "New mod created. Have a nice day!\n";
  312. exit();
RAW Paste Data