0

Sorry for this beginner question, I never used js server-side before.

here is my problem:

I have some javascript downloaded from a remote page (it's encrypted, I can't convert it to php), I need to execute it and read its output.

How can I do it? I'm thinking about something like this:

shell_exec('nodejs code...')

but how to pass the code? It's quite long, about 10 lines of javascript.

Another way would be to store the js to a file and run nodejs script.js, but that would be a useless and slow disk IO...

Sofia Grillo
  • 405
  • 2
  • 13
  • 1
    Take a look [here](https://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php) – Hamza Fatmi Apr 12 '18 at 13:52
  • "downloaded from a remote page" — Then it probably isn't designed to work with Node.js and will likely fail because there is no DOM for it to access. – Quentin Apr 12 '18 at 13:55
  • @quentin I aready tested and it works, I only need to edit the code a little before sending it to node. But I'm trying with the php V8Js class – Sofia Grillo Apr 12 '18 at 14:06

2 Answers2

1

Important caveat about using exec/shell_exec

I feel the need to prepend a caveat about security to this answer. Always be careful when using exec or shell_exec. You almost always should not be taking data over the network to inject into a shell command for security reasons. Writing the script to a file would be much safer because there is no risk of command injection. If you are confident that this approach is required then I strongly advise you to.

  1. Use the PHP function escapeshellcmd which will try to escape shell control characters.
  2. Really ask yourself how much you trust the source? And how much you trust their security?

Having said that. Here's my original answer to the question as asked:


It sounds like the missing piece for your puzzle is the -e parameter for node. This will allow you to pass a script as part of the command invocation.

E.g.

 C:\Users\Cmonahan>node -e "console.log('hello world');"
 hello world

You can then use PHP exec or shell exec to get the output.

More information:

PHP shell_exec() vs exec()

Node CLI documentation

Edit: Regarding passing multiline arguments to the command line. This can be a bit of a minefield. For example: It depends on whether it is a Unix-like or Windows environment and then, if Unix-like, what shell is parsing the command.

See for example:

Windows: How to specify multiline command on command prompt?

End of line (new line) escapes in bash

I would recommend just making sure the argument is a single line. In the case of JS you can try minification first, which typically strips out all newlines, and see if that works for you.

Here's a popular PHP based minifier https://github.com/mrclay/minify I believe you should be able to install via composer.

ChrisM
  • 1,565
  • 14
  • 24
  • Thanks, the question was also how to pass a multi-line script as shell argument, something like base64 encode? – Sofia Grillo Apr 12 '18 at 14:57
  • I have updated my answer to include information about passing multi-line arguments. It's generally easier to make sure that the argument is a single line, so my suggestion is to use a JS minifier. However I have included a couple of pointers on passing multi-line arguments if that is the way you want to go. I have also included a note about security. This is as much for anyone else looking at the answer but I would strongly encourage you to make sure you know what you're doing when passing data into exec or shell_exec – ChrisM Apr 12 '18 at 16:07
  • Actually - have you verified that exec / shell_exec actually interpret a literal newline as a new command? If not then you don't have any problem. I can't find anything online that clarifies and don't have the ability to check right now. – ChrisM Apr 12 '18 at 16:17
-2

dont know am not that at home with php, with isnt it true that php serves html files to the user at home, so why would it be any different from using a javascript file in a normal html page?

cant you just use:<script src="myscripts.js"></script>" or <script>"with here your script"</script>

in the file that need to load this javascript?

Adri
  • 13
  • 5
  • 1
    That would run the JS on the client. The question is asking about running it on the server. – Quentin Apr 12 '18 at 14:10
  • Yeah, but OP has already said they think node JS is useless and slow. I'm not sure if OP really understands JS and Node. He's trying to execute server side JS without Node, and also do it from a PHP file. – Nerdi.org Apr 12 '18 at 14:29
  • 1
    I was talking about creating a file to disk which is slow, not node.js – Sofia Grillo Apr 12 '18 at 14:53