Vice City: Multiplayer

VC:MP Discussion => General Discussion => Topic started by: vcmptr on Mar 11, 2015, 05:44 PM

Title: Can I Call Function with PHP
Post by: vcmptr on Mar 11, 2015, 05:44 PM
I wonder can I call squirrel function with PHP?
Title: Re: Can I Call Function with PHP
Post by: . on Mar 11, 2015, 05:49 PM
Quote from: vcmptr on Mar 11, 2015, 05:44 PMI wonder can i call squirrel function with PHP?

No, you can't. Not unless you write a specialized PHP extension that allows you to do that. Like this extension (http://php.net/manual/en/book.lua.php) does for Lua. But the problem isn't just executing a Squirrel function. I'm assuming that you want to execute a Squirrel function from your script loaded by the Squirrel module which is currently executed by the server.

Another idea, which might not be the best and/or fastest solution would be to use the sockets module (https://bitbucket.org/stormeus/0.4-sockets/downloads) of squirrel to create a simple server/daemon like, listener in Squirrel and then, either from PHP or somewhere else, connect to that listener and either send specific commands that the script knows how to react (preferred, for safety) or strings that can then be evaluated in Squirrel as code at runtime.

compilestring(string,[buffername])

compiles a string containing a squirrel script into a function and returns it

local compiledscript=compilestring("::print(\"ciao\")");
//run the script
compiledscript();

Either way, it's not very pretty to do and you haven't told us what you're trying to achieve. Maybe there's a better way.
Title: Re: Can I Call Function with PHP
Post by: Thijn on Mar 11, 2015, 06:48 PM
Making it accept a string that will be evaluated seems like a major security flaw.

Another way I can think of is using MySQL, and then writing to a table with PHP and reading from it within Squirrel.
Title: Re: Can I Call Function with PHP
Post by: vcmptr on Mar 11, 2015, 06:54 PM
Thanks S.L.C and Thijn. I will try this ways.
Title: Re: Can I Call Function with PHP
Post by: . on Mar 11, 2015, 06:55 PM
Quote from: Thijn on Mar 11, 2015, 06:48 PMMaking it accept a string that will be evaluated seems like a major security flaw.

Obviously! Actually, I'm not even sure why these things are implemented. But I assume it's for those rare cases when you need it and it's mostly for testing and/or personal use. I've seen major software doing that and evaluating run-time generated PHP code.
Title: Re: Can I Call Function with PHP
Post by: Stormeus on Mar 11, 2015, 07:53 PM
The above posts answer your question. Personally I'm of the opinion that if you think you need to do this, you don't, and going forward with it would be a terrible idea.

More refined help would be available if you explained what you're actually trying to accomplish using this feature.
Title: Re: Can I Call Function with PHP
Post by: vcmptr on Mar 12, 2015, 08:48 AM
Quote from: stormeus on Mar 11, 2015, 07:53 PMThe above posts answer your question. Personally I'm of the opinion that if you think you need to do this, you don't, and going forward with it would be a terrible idea.

More refined help would be available if you explained what you're actually trying to accomplish using this feature.
Now I need check is player in game.  :)
Title: Re: Can I Call Function with PHP
Post by: . on Mar 12, 2015, 11:51 AM
Quote from: vcmptr on Mar 12, 2015, 08:48 AMNow I need check is player in game.  :)

Something that simple? A database would be the easiest and most simple way. An even simple and easier way if you don't know how to use databases would be .ini files. Anything other than calling Squirrel from PHP would be better.
Title: Re: Can I Call Function with PHP
Post by: Fuzzie on Mar 12, 2015, 11:53 AM
Query Mechanism (http://wiki.vc-mp.org/wiki/Query_Mechanism)
Is this what you are looking for?
Title: Re: Can I Call Function with PHP
Post by: Sk on Mar 12, 2015, 11:57 AM
create a table
When player join insert his data.
When he leaves delete that data now in php all you need to know that if the player exist in that table,then do your stuff.
Title: Re: Can I Call Function with PHP
Post by: vcmptr on Mar 12, 2015, 12:32 PM
Quote from: S.L.C on Mar 12, 2015, 11:51 AM
Quote from: vcmptr on Mar 12, 2015, 08:48 AMNow I need check is player in game.  :)

Something that simple? A database would be the easiest and most simple way. An even simple and easier way if you don't know how to use databases would be .ini files. Anything other than calling Squirrel from PHP would be better.
I just wondered It's possible. I thought call this functions:
function gplayer(name)
{
local player = FindPlayer(name);
if(player)
{
return [player.ID, player.Name, player.IP, player.UniqueID, player.Skin, player.Score, status[ player.ID ].KillingSpree]
}
else return 0;
}
or this:
function rld()
{
ReloadScripts();
print("Script reloaded from website!");
}

But I give up use reload function for now. I decided to use Query Mechanism and MySQL.

Thanks all for helping. :)