How to execute shell commands from plesk extension - php

i'm making an extension that use a php file in the 'public' directory as a webhook.
From this hook, I will need to run some shell commands (things like npm run build or similar). Is there some kind of api from plesk that let me do this? Or should I use a php function like exec or shell_exec?

I looked through the net, but in the end it looks like what I'm trying to accomplish is not doable. You can indeed use shell_exec to run shell commands, but the user used to run this commands is psaadm, which is a severely restricted user when operating inside a domain document folder. You can see the files, but not create or execute any script, which renders this function useless for what I wanted to do.
If you want to use a webhook tu run your npm build (or other scripts), you can always create a php file in the document root folder and execute your commands using shell_exec. When you call this file, it will execute the commands as the customer user, and will have the ability to run those scripts.
P.S. It might actually be accomplished if you change the permission for the psaadm user, but it's not something a customer which install an extension should do. Still, not sure if it will work for the domain document root folder of your customers.

Related

change working directory from cli php script

I wrote a simple project boostraper using bash.
It asks simple questions such as the parent folder to create the project inside, the type of the project (cli, web php, web symfony, web npm, etc…), the required dependencies, bootstrap everything, init git, and so on.
Then it returns the terminal to the user after changing the working directory to the freshly bootstraped one.
Fine.
I decided to rewrite the whole script using php in order to have a more user friendly cli interface (nice menus, progress bars, etc…) and to be able to pack the whole script in one and only binary (phar).
I struggle with the last part of the script: The one which was the simpliest part of the previous bash script:
cd $newprojectpath
exit 0;
For a reason I can't really figure out, I cannot do the same using a php script.
chdir allows me to change the working directory for the current php process but as soon as it is over, I'm back to working directory from where I invoked the php script (the one from where I spawned the php interpreter sub-process, obviously).
The only way I found in order to achieve this very simple move is to spawn a new shell sub-process from the php script itself.
It works, but I don't like it.
chdir($project->path);
pcntl_exec(getenv('SHELL'));
Did I miss a more simple way to change working directory using PHP cli ?

Running a git post-commit hook

I'm running MAMP locally. I have a php script that I use to build a web site, to generate static HTML files, that I then push to a web server.
This process acts a lot like Jekyll on github pages.
For now, I simply make changes to Markdown files (my content) and then hit a local url, localhost/mysite/build, to which generates the html files. Simple.
I do have git installed locally to version the PHP script itself, along with the markdown files.
Ideally I would like to create a post-commit hook that will simply "ping" that build URL to allow it do run.
A couple things. I have been trying but the post-commit doesn't run the URL.
I would like to use PHP in the post-commit file, is that possible? I don't need any validation or anything yet, just simply want to call the URL to have the process run when I commit.
I have done the chmod to make sure the script is executable.
The post-commit file is named 'post-commit', no extension.
I would think I could just add one simple line, like a file_get_contents(myurl), or something close to that.
I have been googling for a few hours now and found things that are close but not exactly right. Its really very simple, just sort of a noob with git hooks.
Thanks.
Commit hooks are shell scripts, so it is definitely possible to either run a php script (just as you would do from cmd line) or issue a request to your URL using curl: curl --request GET --url http://stackoverflow.com -v in order to fire the build. Depending on the build time you might want to run curl in the background.
First of all, try to prepare a shell script, which runs the build. Then setup the hook itself. See Simple git post-commit hook to copy committed files to a certain folder as an example hook.

How to run a .php file over and over...cron perhaps?

I have a php file that calls exec() on a c++ exe.When the .exe is finishing running I need to run the php file again,repeat .
I am wondering what is the best way to do this? This .php file has no user interaction and needs to be completely automated.
EDIT:Found another solution that would kill the process if it is already running which will cover this issue well.See posts on this page.
http://php.net/manual/en/function.getmypid.php
Here's a simple Linux line that starts up the script in the background and uses the watch command to restart the script whenever it finishes running:
watch -n 0 php /path/to/script.php &
Once you've started it like this, you can use the ps command to list running processes and find the process ID for watch, and then use kill process_id to stop watch.
It's probably not best practice to run commands like this, but it's a quick and easy solution that doesn't require any special access privileges on the system (whereas using cron might), and doesn't involve editing your code (whereas a codeigniter solution will).
I haven't used codeigniter before but there seems to be a solution as described in the wiki.
Depending on how you can access the system (if you are admin or not) and depending on how you are planning to update the automated commands, IMHO, you could use both solution (Linux crontab or codeigniter cron script).

shell_exec on php

I am using the shell_exec command to send commands to my school unix server. I can do things like ls echo and things like that but anything that has to do with creating or deleting a file does not work. I figured I can just create a script for the command I want to perform and run the script with shell_exec, however this is a very tedious process is there any other way around this issue??
Thanks,
shell_exec() is a very bad way of executing commands just because leaving it open, you're leaving your server a whole lot more vulnerable if someone exploits your PHP code, or manages to upload a PHP file. Instead, if you can, you should use PHP functions like mkdir() and fopen($file, '(r|a|w)+) so your file is created if it doesn't exist. In fact, leaving shell_exec(), exec(), pass_thru() and the like in your code, any exploits can execute shell commands on your server.
Now, in relation to the original question, have you checked permissions on the files and directories you're attempting to work with? The user running your PHP needs to have permission to work with the files and directories you're attempting to access. If you're unsure, just chmod 777 any of your files and directories that you need to use in PHP.
Sounds like a permissions problem. Run whoami from within PHP and see what user it's running as. If your school is also using something like SELinux, then there's additional restrictions on process beyond just those set by user/group/mode. But that's a starting point.

Run mediawiki userOptions script without SSH

I'm trying to run the userOptions script for MediaWiki to add a new namespace to the default search namespaces for all existing users. Unfortunately, I don't have shell access to my hosting provider, and to be honest I'm really not at home with using cron jobs.
What's the easiest way for me to run this script on my MediaWiki install:
http://www.mediawiki.org/wiki/Manual:$wgNamespacesToBeSearchedDefault#Changing_options_for_existing_users
(sorry, the url won't parse properly)
For instance, is there a php command that can do this?
Create your own PHP script. In it, populate the $argv and $argc variables, and then just include the userOptions.php file in your own PHP file.

Categories