knightscript Posted January 20, 2016 Share Posted January 20, 2016 Hello Everyone!, this is a quick tutorial on how to restart a resource by using PHP. You will need the following things: -Web hosting with PHP support. -FTP access to your MTA server. First, lets start with the LUA code, so, if you want to implement this code, ignore the meta.xml file, and just add the code from myscript.lua. So lets start the the LUA coding, first, we want to create a folder, for now, lets name it "restartmyresource", and make 2 files inside, meta.xml and myscript.lua. I will comment out everything as best as i can, here we go meta.xml: <meta> <script src="server.lua" type="server" /> <!--defines that our script is named myscript.lua and that it is server-sided, it is not necesary--> <export function="RestartResource" type="server" http="true" /> <!--this allows us to use PHP SDK to execute a function--> <aclrequest> <right name="function.aclGroupAddObject" access="true"></right> <!--makes the request to access ACL group´s--> <right name="function.restartResource" access="true"></right> <!--makes the resource to restart--> </aclrequest> </meta> resourceRoot = getResourceRootElement() function RestartResource () --function that executes the restart of the resource local resource = getResourceFromName("VIP-Cars") --gets the resource that has the name VIP-Cars outputDebugString("checking if VIP-Cars is running.") --outputs a message on the debugscript saying that it is checking if the resource is running. if (resource and getResourceState(resource) == "running") then --if resource is running... outputDebugString("restarting resource VIP-Cars.") --outputs message on the debugscript.. outputChatBox("Resources are being restarted") --outputs message on the chatbox.. restartResource(resource) --restarts the resource.. else --if the resource is not running end --end if (resource and getResourceState(resource) == "loaded") then --if resource is loaded outputDebugString("starting resource VIP-Cars.") --output message on the debugscript.. outputChatBox("Resources are being started") --outputs message on the chatbox.. startResource(resource) --starts the resource.. end end addCommandHandler("restartres",RestartResource) --testing command function createadminacc(res) --function that creates the account and give it the needed rights if getAccount("phpsdkacc") then --if the account already exists outputDebugString("The PHP SDK account already exists.") --output it to debugscript if isObjectInACLGroup ("user.phpsdkacc", aclGetGroup ( "Admin" ) ) then --if the account has the needed rights outputDebugString("The PHP SDK account already has Admin rights.") --output it to debugscript else --if not outputDebugString("The PHP SDK account does not have the needed rights, adding them.") --the account doesnt have the needed rights if aclGroupAddObject (aclGetGroup("Admin"), "user.phpsdkacc") == true then --if the script was able to add the account rights outputDebugString ("Assigned the PHP SDK account the needed rights successfully.") --output message on the debugscript else --if the script wasnt able to add the account rights outputDebugString ("Couldnt assign the rights needed to the PHP SDK account, the resource doesnt have the needed rights, add them by typing on your console: aclrequest allow ".. getResourceName(res) .." all.") --output on debugscript end end else --if the account doesnt exist addAccount("phpsdkacc", "testingpassword" ) --add the account outputDebugString("The PHP SDK account has been created since it didn´t exist.") --outputs on debugscript if aclGroupAddObject (aclGetGroup("Admin"), "user.phpsdkacc") == true then --if the script was able to add the account rights outputDebugString ("Assigned the PHP SDK account the needed rights successfully.") --output it on debugscript else --if not outputDebugString ("Couldnt assign the rights needed to the PHP SDK account, the resource doesnt have the needed rights, add them by typing on your console: aclrequest allow ".. getResourceName(res) .." all.") --output on debugscript end end end addEventHandler("onResourceStart",resourceRoot,createadminacc) --execute this each time the resource starts you are going to need to add this resource to your ACL as admin, or you can use the command on the console "aclrequest allow resourcename all". So, here you can edit 2 things, all phpsdkacc fields and testingpassword, here is an example of what would show up if the account already existed and had rights: example of what would show up if the account didnt exist, created it and added rights: example of what would show up if the account was added, but couldnt add the rights because of permitions: Now you want to add your resource to startup with the server, so first, open up your mtaserver.conf and add this at the ending: <resource src="restartresources" startup="1" protected="0" /><!--WHERE restartresources would be your script folder name--> and that would be it for LUA!, lets jump onto my speciality, PHP!. First, we need to get the MTA PHP SDK, we can get it from here! (version 0.4) Second, we need to extract its contents on our public_html folder, and should look like this: So now, we create a file called index.php, and we paste the following: <meta charset="utf8"> <!--Set our character set to utf8, if it is needed--> <?php include( "mta_sdk.php" ); //includes the MTA SDK $mtaServer = new mta("123.123.123.123", 22005, "phpsdkacc", "testingpassword"); //we create the connection to our server, where first argument would be IP, second argument would be HTTP Port, third argument would be username and forth argument would be password $mtaServer->getResource("restartresources")->call("RestartResource"); //calls the function ?> Now if we want to add a little bit of security, then we can use the following: replace everything under : <?php $password = $_POST['passwd']; //stores the password into a variable $mypassword = "knightsv"; //your password if ($_SERVER['REQUEST_METHOD'] == 'POST') { //checks if the post has been sent if ($password == $mypassword) { //checks if the password that was entered was the same as the defined password echo "<h1>Correct Password! the resource has been restarted, if the correct data has been inserted.</h1>"; //just a simple message include( "mta_sdk.php" ); //includes the mta sdk $mtaServer = new mta("123.123.123.123", 22005, "phpsdkacc", "testingpassword"); //creates the connection to the mta server where first argument is the server ip, second argument is the server HTTP port, third argument is the user account, and fourth argument would be the password of the account. $resource = $mtaServer->getResource ("restartresources"); //calls the resource $returns[] = $resource->call("RestartResource"); //calls the function }else{ //if the password was incorrect echo "Wrong password"; //display message } }else { //if the post wasn´t made ?> <form method="post"> <input type="password" name="passwd"> <input type="submit"> </form> <?php }?> If you´d want you could create multiple functions to restart multiple resources, or restart multiple resources on 1 function: This script is not very good at restarting more than 1 resource on the same function, if you need it, then you can go to this link: https://forum.multitheftauto.com/viewto ... 91&t=37809 There you can find a script which would allow you to get the resources from a file. BUT if you want to do so, then you can just duplicate this lines: local resource = getResourceFromName("VIP-Cars") --gets the resource that has the name VIP-Cars outputDebugString("checking if VIP-Cars is running.") --outputs a message on the debugscript saying that it is checking if the resource is running. if (resource and getResourceState(resource) == "running") then --if resource is running... outputDebugString("restarting resource VIP-Cars.") --outputs message on the debugscript.. outputChatBox("Resources are being restarted") --outputs message on the chatbox.. restartResource(resource) --restarts the resource.. else --if the resource is not running end --end if (resource and getResourceState(resource) == "loaded") then --if resource is loaded outputDebugString("starting resource VIP-Cars.") --output message on the debugscript.. outputChatBox("Resources are being started") --outputs message on the chatbox.. startResource(resource) --starts the resource.. end and just change local resource to for example local resource2, for example: function RestartResource () --function that executes the restart of the resource local resource = getResourceFromName("VIP-Cars") --gets the resource that has the name VIP-Cars outputDebugString("checking if VIP-Cars is running.") --outputs a message on the debugscript saying that it is checking if the resource is running. if (resource and getResourceState(resource) == "running") then --if resource is running... outputDebugString("restarting resource VIP-Cars.") --outputs message on the debugscript.. outputChatBox("Resources are being restarted") --outputs message on the chatbox.. restartResource(resource) --restarts the resource.. else --if the resource is not running end --end if (resource and getResourceState(resource) == "loaded") then --if resource is loaded outputDebugString("starting resource VIP-Cars.") --output message on the debugscript.. outputChatBox("Resources are being started") --outputs message on the chatbox.. startResource(resource) --starts the resource.. end ----------------------------------------------------------------------------------------- local resource2 = getResourceFromName("VIP-Skins") --gets the resource2 that has the name VIP-Skins outputDebugString("checking if VIP-Skins is running.") --outputs a message on the debugscript saying that it is checking if the resource2 is running. if (resource2 and getResourceState(resource2) == "running") then --if resource2 is running... outputDebugString("restarting resource2 VIP-Skins.") --outputs message on the debugscript.. outputChatBox("Resources are being restarted") --outputs message on the chatbox.. restartResource(resource2) --restarts the resource2.. else --if the resource is not running end --end if (resource and getResourceState(resource) == "loaded") then --if resource is loaded outputDebugString("starting resource VIP-Skins.") --output message on the debugscript.. outputChatBox("Resources are being started") --outputs message on the chatbox.. startResource(resource2) --starts the resource.. end end This script would restart both resources, VIP-Skins and VIP-Cars on the call of the function. Now, if you want to have multiple functions for multiple resources, you can just rename the function name, and of course, the resource name, example: function RestartResource () --function that executes the restart of the resource local resource = getResourceFromName("VIP-Cars") --gets the resource that has the name VIP-Cars outputDebugString("checking if VIP-Cars is running.") --outputs a message on the debugscript saying that it is checking if the resource is running. if (resource and getResourceState(resource) == "running") then --if resource is running... outputDebugString("restarting resource VIP-Cars.") --outputs message on the debugscript.. outputChatBox("Resources are being restarted") --outputs message on the chatbox.. restartResource(resource) --restarts the resource.. else --if the resource is not running end --end if (resource and getResourceState(resource) == "loaded") then --if resource is loaded outputDebugString("starting resource VIP-Cars.") --output message on the debugscript.. outputChatBox("Resources are being started") --outputs message on the chatbox.. startResource(resource) --starts the resource.. end end function RestartResource2 () --function that executes the restart of the resource local resource = getResourceFromName("VIP-Skins") --gets the resource that has the name VIP-Cars outputDebugString("checking if VIP-Cars is running.") --outputs a message on the debugscript saying that it is checking if the resource is running. if (resource and getResourceState(resource) == "running") then --if resource is running... outputDebugString("restarting resource VIP-Cars.") --outputs message on the debugscript.. outputChatBox("Resources are being restarted") --outputs message on the chatbox.. restartResource(resource) --restarts the resource.. else --if the resource is not running end --end if (resource and getResourceState(resource) == "loaded") then --if resource is loaded outputDebugString("starting resource VIP-Cars.") --output message on the debugscript.. outputChatBox("Resources are being started") --outputs message on the chatbox.. startResource(resource) --starts the resource.. end end DON´T FORGET to add your export on the meta, like this: <export function="RestartResource2" type="server" http="true" /> <!--this allows us to use PHP SDK to execute a function--> and for the PHP, you have many options, the easiest option would be making a dropdown, so lets get to it: <meta charset="utf8"> <!--Set our character set to password--> <?php $password = $_POST['passwd']; $mypassword = "knightsv"; $whichresource = $_POST['whichresource']; if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($password == $mypassword) { echo "<h1>Correct Password!.</h1>"; include( "mta_sdk.php" ); $mtaServer = new mta("123.123.123.123", 22005, "phpsdkacc", "testingpassword"); $resource = $mtaServer->getResource ("restartresources"); if ($whichresource == "VIP-Cars"){ $returns[] = $resource->call("RestartResource"); } if ($whichresource == "VIP-Skins") { $returns[] = $resource->call("RestartResource2"); } }else{ echo "Wrong password"; } }else{?> <form method="post"> <label>Your Password: </label><input type="password" name="passwd"> <label>Resource Name:</label> <select name="whichresource"> <option value="VIP-Cars">VIP Cars</option> <option value="VIP-Skins">VIP Skins</option> </select> <input type="submit"> </form> <?php }?> Once this is made, i can see the PHP site like this: And, if i click the dropdown, it displays this: If i input the correct password, and select VIP Cars, then when i press the send button, this happens on the server: If i input the correct password, and select VIP Skins, then when i press the send button, this happens on the server: That would be it for today, havent slept in 2 days, I hope you liked my post, if so feel free to leave a reply and I will try to answer any questions I can! Good luck!. Link to comment
Dealman Posted January 21, 2016 Share Posted January 21, 2016 Wrong section, you should be using the tutorial section for threads like these. There is also a dedicated forum for resource related stuff. Link to comment
knightscript Posted January 21, 2016 Author Share Posted January 21, 2016 Sorry, didn´t know! I considered this just a little script. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now