PHP exec svn export - php

I'm trying to run this exec
exec('svn export -r '.$rev2.' '.$repourl.'/'.$dir.$parts[$j].' cache/diff/'.$dir.$parts[$j]);
which is equals to:
svn export -r 1192 file:///var/svn/Repo/folder/file.xml cache/diff/folder/file.xml
when i try the script it simply doesn't export the file (the folde is created with another function, so that one is there.)
but if i try to run the svn export as user www-data (from command line) it works.
so i don't get why it should not work from php.. maybe some configuration?
hope someone can help,
thanks in advance
EDIT
Tried also with full path to svn (which svn)
the exec:
exec('/usr/bin/svn export -r '.$rev2.' '.$repourl.'/'.$dir.$parts[$j].'
the result
/usr/bin/svn export -r 1192 file:///var/svn/Repo/folder/file.xml cache/diff/folder/file.xml
Still no changes, the file is not exported.

It is not configuration. Or rather, not directly. Your PHP instance seems to be set to a user which cannot write to the folder you've specified. Either chmod or chown it to something that the user which PHP runs as can write to.
Side note: I wrote a SVN lib for laravel. It is, however, standalone. You might want to ease your burden by using it - it itself uses the PECL SVN extension.

found the solution,
the problem was related to a global variable not letting the exec run.

Related

php shell_exec() command is not working

I am trying to run a .sh file from php.
I tried doing it with shell_exec(). but its not working
I refered many questions related to this in stack overflow but could not solve
my php code is(web.php)
<?php
echo shell_exec('/var/www/project/xxe.sh');
echo "done";
?>
only done is printed. but it is working from terminal(php /var/www/project/web.php)
In xxe.sh I am calling a python file
python vin.py
I have also changed the file permission to 777 for both .sh n .py files
please help
If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
shell_exec might not know what directory to look in for your executable's location directory. What solved it for me was this before the shell_exec:
putenv('PATH=/usr/local/bin');
Then the terminal can find the executable. Also check permissions on every part of the command to make sure apache user has read and execute permissions.
If it works well in shell, I think apache is chrooted. So php can't find /var/...
Or user of httpd user does not have permission to enter /var/...
If you are good at PHP. Open dir /var/... And readdir() and check dir exists and check file exists.
This question might help you. scanning /home/ with opendir()
The problem is usually that when you exec code from within php it is run as the webservers user www-data in alot of linux distros. Normaly this user does not have an enviroment set up, and because of that no PATH. By using full paths in your files you can usually overcome this.
xxe.sh
/usr/bin/python /path/to/script/vin.py
While trying to run a script triggered by github post-receive webhook.
Here is where my project directory is located(cloned git repo):
/var/www/html/my-repo
I create a script inside the above directory called webhook.php:
<?php
#webhook.php
$cmd = shell_exec("git pull 2>&1");
#for debugging
echo $cmd;
?>
Execute the following command inside /var/www/html
sudo chown www-data:www-data -R my-repo/
Test it by going to http://www.myserver.com/my-repo/webhook.php
Add the path to your script to github webhooks.
I have been stuck in this problem for several hours.
I have thought about a solution.
1. move your script to a python file "script.py" and place this file to your server root.
2. shell_exec("python script.py");
Any way, it works for me.
On my host I had to give a different path for my php file to be executed from shell_exec().
This didn't work shell_exec('/usr/bin/php backgroundtask.php');.
While this did shell_exec('/opt/php/php-5.5.0/bin/php backgroundtask.php');.
You can visit this Reference.
I had the same issue because PHP backslashes.
PHP escapes the backslashes, so the command that reaches the shell
'COPY E:path1\path2\file.prn /B \127.0.0.1\"PRINTER NAME"'
so I gave command like this
'COPY E:\\path1\\path2\\file.prn /B \\\\127.0.0.1\"PRINTER NAME"'.
You have to double-escape the backslashes: once for PHP and once for the shell.

command works in the terminal, wont work in php

I've made an extension for my web application in php. I want the php script to remake the extension, so I'm trying to send a command to the terminal. It looks like this:
shell_exec("make -C /folder1/folder2");
And it won't make the .so file no matter what. I've used chmod 777 on that folder to make it editable but it didn't help. When I'm directly using the command in the terminal it works just fine:
make -C /folder1/folder2
The .so file gets created without a problem, functions from my extension work like they should. Does anyone have a solution for this?
Make sure that PHP is not running in safe mode and that the PHP user has read/write access to the files and folders. Read the comments at shell-exec for more information.

Using PHP to execute terminal commands

How can PHP be used to create a number of commands in the terminal? For example, if I want to change the permissions on a folder or a file, I might run
Sudo Chmod 777 FileName
from the command line. How could I do this with PHP?
Look at exec, system etc: http://www.php.net/manual/en/book.exec.php
Obviously, if you are going to use sudo for root access stuff (not recommended) then you would need to either supply a password somehow or set the user which PHP is running as to not require a password.
It may be better to use an SSH call for this and supply the command to execute via that command.
To sum up, I really don't recommend using PHP (or any language for that matter) to run a series of commands which then uses sudo
To give permission to folder or file in php use following line of code
chmod($file,0777);
this will change the permission of file or folder.

How to make a PATH variable availailable to Apache? (OSX Lion)

I have a Python script that is using MySQLdb on OSX Lion. To get MySQLdb to work, I had to add
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
to my ~/.bash_profile. The python script, when executed from the command line, works just fine.
When I try executing the same python script from a PHP script using the exec() function, the python script is unable to locate the library path. Here's the err msg from the Apache log:
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Library/Python/2.7/site-packages/_mysql.so
Reason: image not found
I'm assuming this is a user issue though I'm unclear on how to fix it.
Where should I put this path so that it will be globally available to all scripts and users on my machine? (well at least, to Apache and Root)
Solution: creating a soft link did the trick:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
This solution was taken from comments on this post. Perhaps this solution will help another struggling with MySQLdb on OSX.
Though I'm still not clear on why it worked on one user and not another - or, rather, to which file the above mentioned export should have been saved in order to make it available to all. Any explanation on this would be appreciated.
It looks like you need to modify sys.path array. It initialized from the environment variable PYTHONPATH: http://docs.python.org/library/sys.html#sys.path
For Apache you can add PYTHONPATH entry to the EnvironmentVariables section in /System/Library/LaunchDaemons/org.apache.httpd.plist that will make it available to anything Apache runs. See man launchd.plist for details.
For root you can add an export PYTHONPATH to /etc/profile which will make it available to all sh and bash login shells. See man bash for details.

Calling an svn update from a php script via a browser is not working

I have two scripts.
running an update and calling shell_exec('svn update') and shell_exec('svn st')
running a mysqldump shell_exec('mysqldump params')
The svn script is not running the update command, the svn st is printing results but not the svn update
I tried to declare parameters when calling svn update eg
'svn update ' . dir . ' --username myuser --password mypasswd --non-interactive';
-- still nothing
Played with most of the params
If this is something related to binaries/permissions/groups, I don't see it.
The mysqldump command works fine and is producing a file, so why isn't the svn updating the filesystem?
Please do not advise using core SVN classes in PHP. This is not an option, I don't have complete control over the server and the module is not available.
Thanks for your help,
-hbt
PS: important thing to mention here. The scripts works when called via the command line. It only fails when called via a web browser.
I was also encountering the same problem but not even permissions solved it.
Based on some of the other advice here, I did:
<?php
echo shell_exec('2>&1 svn update /path/to/checked/out/directory/ --non-interactive');
I then got an error dumped into my browser:
svn: warning: Can't open file '/root/.subversion/servers': Permission denied
svn: OPTIONS of 'http://my.svn.server/svn/project/trunk': authorization failed: Could not authenticate to server: rejected Basic challenge (http://my.svn.server)
Not sure why my web server user tried to access /root but I fixed the problem without changing any permissions by adding --config-dir to the svn up command:
<?php
echo shell_exec('2>&1 svn update /path/to/checked/out/directory/ --non-interactive --config-dir /path/to/my/home/.subversion');
*Note that /path/to/my/home/.subversion exists because the initial checkout was done on the command line
to get the standard error in the return value use :
shell_exec('2>&1 svn update')
it doesn't work if you put the 2>&1 at the end
It might be permission problem: script called via a web browser runs under different username than svn working directory's owner, therefore it has read-only access. Read-only access should be enough for svn status to execute, but not for svn update (though in this case there should be an error like "svn: Can't open file '.svn/lock': Permission denied").
Have you tried the PECL svn extension? You don't need to use shell_exec for this.
OK. I got it.
It is an issue with permissions. The .svn directory must have the right permissions because the svn update command is using those directories to write stuff.
So!
---Make sure you run all chmod commands as sudo or root----
run a chmod 777 on .svn directory
run an svn update via command line
call script
If nothing. You must run chmod 777 recursively for all .svn directories then run another svn update
Still nothing?
Make sure you don't have two svn clients
In my case, the svn client used by the UI is different from the svn (command line)
If you have two clients, make sure they are running the same version
Or update your script to call the client directly.
Still nothing?
Run a chmod 777 -R *
svn update
and try again
If you can make it work with another set of permissions, please let me know. I know that 777 is not ideal, but I can't make it work with something lower.
Thanks again guys.
Yes, the problem is the permissions with the .svn directory.
Make sure it has the correct permissions for the user that PHP is running as (in my case it was apache) and chmod it to 775.

Categories