Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Is the "mods" folder local or remote?
- // Valid values here are: true or false
- define('LOCALMODS', false);
- // Database connection info
- define('DBHOST', 'localhost');
- define('DBPORT', '33060');
- define('DBUSER', 'solder');
- define('DBPASS', 'solder');
- define('DBNAME', 'solder');
- define('DBPREFIX', '');
- // Make sure these both have a trailing slash!!!
- // MODFOLDER is only used if LOCALMODS is true
- define('MODFOLDER', 'C:/Apache24/solder/public/mods/');
- define('TEMPFOLDER', 'D:/temp/');
- // Remote target configuration. Can be ignored if LOCALMODS=true
- define('SCPCOMMAND', 'pscp -load RemoteHost');
- define('REMOTETARGET', 'solder@solderhost.com:/var/www/htdocs.solder/public/mods/');
- //
- // No need to edit below here.
- // (but hey, you do you :) )
- //
- //
- // Try to connect to the database. Abort on failure.
- //
- try {
- $globaldbh = new PDO("mysql:host=" . DBHOST . ";port=" . DBPORT . ";dbname=" . DBNAME, DBUSER, DBPASS);
- } catch (PDOException $e) {
- echo "Error connecting to DB!: ", $e->getMessage() . "\n";
- die();
- }
- //
- // Ensure there are at least two command line parameters.
- // Display usage on failure.
- //
- if ( $argc < 3 ) {
- echo "Error: Invalid command line parameters\n\n";
- echo "Usage: addsoldermod <slug> <modfilename> [-forge]\n";
- echo "Info: <param> is required, [param] is optional, omit < > and [ ]\n\n";
- exit();
- }
- $forge = false;
- $fabric = false;
- $config = false;
- //
- // If there are 3 parameters, and the 3rd is '-forge' or '-fabric'
- // set the appropriate boolean to true so we can package
- // up the mod differently
- //
- if ( ($argc == 4) && ($argv[3] == "-forge") ) {
- $forge = true;
- echo "Forge mode specified! Creating /bin/modpack.jar instead of /mods/<modfile>\n";
- } elseif ( ($argc == 4) && ($argv[3] == "-fabric") ) {
- $fabric = true;
- echo "Fabric mode specified! Creating /bin/modpack.jar from version.json instead of /mods/<modfile>\n";
- } elseif ( ($argc == 4) && ($argv[3] == "-config") ) {
- $config = true;
- echo "Config mode specified! Creating Solder zip from config folder instead of /mods/<modfile>\n";
- }
- //
- // Function to validate the format of a slug
- //
- function isValidSlug($slug = "") {
- $validchars = "abcdefghijklmnopqrstuvwxyz1234567890-";
- $splitslug = str_split($slug);
- foreach ( $splitslug as $char ) {
- if ( strpos($validchars, $char) === false ) return false;
- }
- return true;
- }
- //
- // Grab the command line arguments. Abort on failure.
- $slug = trim($argv[1]);
- $modfile = trim($argv[2]);
- echo "Slug: {$slug}\n";
- if ( !isValidSlug($slug) ) {
- echo "Error: Invalid slug! Slugs can only contain lower case characters or hyphens!\n";
- exit();
- }
- echo "Mod File: {$modfile}\n";
- if ( !file_exists($modfile) ) {
- echo "Error: Mod file \"{$modfile}\" does not exist!\n";
- exit();
- }
- //
- // Auto-detect forge or fabric
- //
- if ( !$forge && ($slug == "minecraft-forge") ) {
- echo "*** Forge SLUG detected. Switching to Forge mode now\n";
- $forge = true;
- } elseif ( !$fabric && ($slug == "fabric") ) {
- echo "*** Fabric SLUG detected. Switching to Fabric mode now\n";
- $fabric = true;
- }
- //
- // Query the database for mod metadata based on provided slug
- //
- $query = "SELECT id, name, description, author, link, pretty_name FROM " . DBPREFIX . "mods WHERE name=:name";
- $sth = $globaldbh->prepare($query);
- $fields = array();
- $fields[':name'] = $slug;
- $sth->execute($fields);
- if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
- //
- // If the slug exists, grab its metadata
- //
- $mod_id = $row['id'];
- $mod_name = $slug;
- $mod_pretty_name = $row['pretty_name'];
- $mod_description = $row['description'];
- $mod_author = $row['author'];
- $mod_link = $row['link'];
- $newmod = false;
- } else {
- //
- // If the slug does not exist, try to create a new Solder mod
- //
- $newmod = true;
- $mod_id = null;
- $mod_name = $slug;
- $mod_pretty_name = "";
- $mod_description = null;
- $mod_author = null;
- $mod_link = null;
- echo "The mod slug \"{$slug}\" does not seem to exist yet.\n";
- echo "Let's create a new Solder mod in the system now...\n";
- echo "(<CTRL-C> now if you think you made a typo)\n\n";
- //
- // Get the mod name. This is the human readable name.
- //
- while ( $mod_pretty_name == "" ) {
- $mod_pretty_name = readline("Mod Name (human readable display name): ");
- if ( $mod_pretty_name == "" ) {
- echo "Error: Mod name cannot be blank!\n";
- continue;
- }
- if ( strtolower(trim($mod_pretty_name)) == "stop" ) {
- echo "User interrupt: Stopping script...\n";
- exit();
- }
- // Check to see if that name matches anything in the database
- $query = "SELECT id FROM " . DBPREFIX . "mods WHERE pretty_name like :pretty_name";
- $fields = array();
- $fields[':pretty_name'] = $mod_pretty_name;
- $sth = $globaldbh->prepare($query);
- $sth->execute($fields);
- if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
- echo "Error: A mod with that name already exists!\n";
- $mod_pretty_name = "";
- continue;
- }
- }
- while ( $mod_description === null ) {
- $mod_description = readline("Mod Description (can be blank): ");
- }
- while ( $mod_author === null ) {
- $mod_author = readline("Mod Author (can be blank): ");
- }
- while ( $mod_link === null ) {
- $mod_link = readline("Mod Homepage URL (can be blank): ");
- if ( ($mod_link != "") && !filter_var($mod_link, FILTER_VALIDATE_URL) ) {
- echo "Error: That is not a valid URL!\n";
- $mod_link = null;
- }
- }
- //
- // Allow the user to abort if the information is incorrect.
- //
- echo "\nImportant! Please review the following information!\n\n";
- echo "About to add this mod to solder:\n";
- echo " Mod Name: {$mod_pretty_name}\n";
- echo " Mod Slug: {$mod_name}\n";
- echo " Mod Author: {$mod_author}\n";
- echo " Mod Description: {$mod_description}\n";
- echo " Mod Homepage: {$mod_link}\n";
- echo "\n";
- // Query the user and abort if they don't hit "y" or "yes"
- $response = strtolower(readline("Create this Solder mod now? (y/n): "));
- if ( ($response !== "y") && ($response !== "yes") ) {
- echo "Not creating mod! Aborting now!\n\n";
- exit();
- }
- //
- // Add the mod to the Solder database
- //
- $query = "INSERT INTO " . DBPREFIX . "mods (id, name, description, author, link, created_at, updated_at, pretty_name) ";
- $query .= "VALUES(null, :name, :description, :author, :link, NOW(), NOW(), :pretty_name)";
- $fields = array();
- $fields[':name'] = $mod_name;
- $fields[':description'] = $mod_description;
- $fields[':author'] = $mod_author;
- $fields[':link'] = $mod_link;
- $fields[':pretty_name'] = $mod_pretty_name;
- $sth = $globaldbh->prepare($query);
- $sth->execute($fields);
- if ( !$sth ) {
- echo "Error: Could not add mod to Solder!\n";
- echo "PDO::errorInfo():\n";
- print_r($sth->errorInfo());
- exit();
- }
- //
- // Grab the ID of the newly created mod. Abort if not available.
- //
- $query = "SELECT id FROM " . DBPREFIX . "mods WHERE name=:name";
- $fields = array();
- $fields[':name'] = $mod_name;
- $sth = $globaldbh->prepare($query);
- $sth->execute($fields);
- if ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
- $mod_id = $row['id'];
- } else {
- echo "Could not get ID of new mod! Aborting!\n\n";
- exit();
- }
- echo "New mod successfully created!\n\n";
- }
- //
- // Get the mod version number
- //
- $modfile_basename = basename($modfile);
- echo "Please provide the mod version number for Solder.\n";
- echo "This is typically in the format of <MCVersion>-<ModVersion>\n";
- echo "(i.e. 1.12.2-3.4.8b1).\n";
- echo "Mod File Name: {$modfile_basename}\n";
- $mod_version = "";
- while ( $mod_version == "" ) {
- $mod_version = readline("Mod Version: ");
- }
- //
- // Create the properly formated Solder mod ZIP archive
- //
- // Check to see if a mod ZIP with that version number already exists
- $solderzipname = $mod_name . "-" . $mod_version . ".zip";
- if ( LOCALMODS && file_exists(MODFOLDER . $mod_name . "/" . $solderzipname) ) {
- echo "Error: A mod file named \"{$mod_name}-{$mod_version}.zip\" already exists!\n";
- echo "Aborting!\n\n";
- exit();
- }
- // Delete the existing temp file if it exists
- if ( LOCALMODS && file_exists(TEMPFOLDER . $solderzipname) ) unlink(TEMPFOLDER . $solderzipname);
- mkdir(TEMPFOLDER . $mod_name);
- $zip = new ZipArchive();
- // Abort here if we can't write to the temp folder
- if ( $zip->open(TEMPFOLDER . $mod_name . "/" . $solderzipname, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE ) {
- echo "Error: The solder mod ZIP file could not be created!\n";
- echo "Tried: {$solderzipname}\n";
- echo "Aborting!\n\n";
- }
- // If this is Forge create a bin/modpack.jar. Create mods/modjarname.jar
- if ( $forge ) {
- $zip->addEmptyDir('bin');
- $zip->addFile($modfile, "bin/modpack.jar");
- } elseif ( $fabric ) {
- $fabriczip = new ZipArchive();
- $fabriczip->open(TEMPFOLDER . $mod_name . "/fabric-modpack-{$mod_version}.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
- $fabriczip->addFile($modfile, "version.json");
- $fabriczip->close();
- $zip->addEmptyDir('bin');
- $zip->addFile(TEMPFOLDER . $mod_name . "/fabric-modpack-{$mod_version}.zip", "bin/modpack.jar");
- } elseif ( $config ) {
- $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($modfile_basename), RecursiveIteratorIterator::LEAVES_ONLY);
- foreach ( $files as $name => $file) {
- $filePath = $file->getRealPath();
- $relativePath = str_replace("\\", "/", substr($filePath, strlen(getcwd()) + 1));
- if ( !$file->isDir() ) {
- $zip->addFile($filePath, $relativePath);
- } else {
- if ( $relativePath !== false ) $zip->addEmptyDir($relativePath);
- }
- }
- } else {
- $zip->addEmptyDir('mods');
- $zip->addFile($modfile, "mods/{$modfile_basename}");
- }
- $zip->close();
- if ( $fabric ) unlink(TEMPFOLDER . $mod_name . "/fabric-modpack-{$mod_version}.zip");
- // Get MD5 sum and file size of the new Solder ZIP archive
- $solderzipmd5 = md5_file(TEMPFOLDER . $mod_name . "/" . $solderzipname);
- $solderzipsize = filesize(TEMPFOLDER . $mod_name . "/" . $solderzipname);
- //
- // Display the mod metadata and allow the user to abort
- //
- echo "\nPlease review the mod information carefully!\n\n";
- echo "Ready to add the new mod version:\n";
- echo " Mod Name: {$mod_pretty_name}\n";
- echo " Mod JAR: {$modfile_basename}\n";
- echo " Mod ZIP: {$solderzipname}\n";
- echo " Mod Version: {$mod_version}\n";
- echo "\n";
- $response = strtolower(readline("Add this new mod version? (y/n): "));
- if ( ($response !== "y") && ($response !== "yes") ) {
- echo "Not adding new version! Aborting now!\n\n";
- unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
- rmdir(TEMPFOLDER . $mod_name);
- exit();
- }
- //
- // If mods are local we just make folders and move files around
- //
- if ( LOCALMODS ) {
- // Create the mod's folder under MODFOLDER if not present
- if ( !is_dir(MODFOLDER . $mod_name) ) {
- mkdir(MODFOLDER . $mod_name);
- }
- // Abort if we could not make the folder
- if ( !is_dir(MODFOLDER . $mod_name) ) {
- echo "Error! Could not create mod folder in Solder. Aborting!\n\n";
- unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
- rmdir(TEMPFOLDER . $mod_name);
- exit();
- }
- // Move the Solder ZIP archive to the mod's folder under Solder. Abort on failure.
- if ( rename(TEMPFOLDER . $mod_name . "/" . $solderzipname, MODFOLDER . $mod_name . "/" . $solderzipname) === false ) {
- echo "Error! Could not place mod into Solder's mods folder. Aborting!\n\n";
- unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
- rmdir(TEMPFOLDER . $mod_name);
- exit();
- } else {
- rmdir(TEMPFOLDER . $mod_name);
- }
- } else {
- $junk = system(SCPCOMMAND . " -r \"" . TEMPFOLDER . $mod_name . "\" " . REMOTETARGET);
- unlink(TEMPFOLDER . $mod_name . "/" . $solderzipname);
- rmdir(TEMPFOLDER . $mod_name);
- }
- //
- // Insert the new mod version into the Solder database
- //
- $query = "INSERT INTO " . DBPREFIX . "modversions (id, mod_id, version, md5, created_at, updated_at, filesize) ";
- $query .= "VALUES(null, :mod_id, :version, :md5, NOW(), NOW(), :filesize)";
- $fields = array();
- $fields[':mod_id'] = $mod_id;
- $fields[':version'] = $mod_version;
- $fields[':md5'] = $solderzipmd5;
- $fields[':filesize'] = $solderzipsize;
- $sth = $globaldbh->prepare($query);
- $sth->execute($fields);
- // All done!
- echo "New mod created. Have a nice day!\n";
- exit();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement